File: rsyslog.c

package info (click to toggle)
super 3.30.0-2+lenny1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,072 kB
  • ctags: 755
  • sloc: ansic: 10,089; sh: 288; makefile: 204
file content (232 lines) | stat: -rw-r--r-- 5,778 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
static const char rcsid[] = "$Id: rsyslog.c,v 1.17 2004/04/30 17:00:58 will Exp $";
/*************************************************************************
**
** Network version of syslog (syslog,openlog,closelog)
**
**
**************************************************************************
**/

/*
 * This code from Jean-luc Szpyrka's modified super. (jls@sophia.inria.fr)

 * Modification history:
 *
 * Will Deich (will@nfra.nl), 20 Dec 1994.
 *    - don't use prototypes for ropenlog(), rcloselog().
 *    - rsyslog() changed to use stdargs if __STDC__, varargs otherwise.
 *    - change ropenlog() to be closer to openlog() in its arguments.
 *    - change rsyslog() to be closer to syslog() in what it prints.
 * Will Deich (will@nfra.nl), 20 Jan 1996.
 *    - replaced most of the #include's with #include "localsys.h"
 */

#include "localsys.h"

/* Just in case there is no errno.h, we provide our own useless errno
 * variable, so that the code below always compiles.
 */
#ifndef HAVE_ERRNO_H
static int errno;
#endif

struct socketd {
    short	        f_socket;			
    struct sockaddr_in	f_addr;
};

static struct socketd LogSocket;

#define MAXIDENT 128	/* keep MAXIDENT << MAXLINE */
#define MAXLINE 1024

static struct {
    char ident[MAXIDENT];
    int log_pid;
    int def_facility;
} loginfo = { "", 0, LOG_USER };

/*
 * This procedure sends a message to and already opened networked syslog
 * It uses varargs to provide a useful interface (a la printf)
 *  
 */
/*VARARGS0*/

/**
 ** Warning -- awful code style here.  Depending on whether you are
 ** compiling with HAVE_STDARG_H set or not, we compile different code for
 ** the function declaration and first few lines of the body.
 **/

#ifdef HAVE_STDARG_H
    /** ** ** ** STD C beginning ** ** ** **/
    void
    rsyslog(unsigned int level, char *fmt, ...)
    {
	va_list args;

#else
    /** ** ** ** K&R C beginning ** ** ** **/

    void
    rsyslog( va_alist )
    va_dcl
    {
	va_list args;
	unsigned int level;
	char *fmt;
#endif
    /** ** ** ** Body of function ** ** ** **/

    char msg[2*MAXLINE];   /* MAXLINE is the maximum size of the message,
			    * but  <%d> ident:   or  <%d> (pid) ident:
			    * is added at the beginning of it before
			    * sending it on the net.
			    */
    int l;

    /*
     * Formats the output 
     */

#ifdef HAVE_STDARG_H
    va_start(args,fmt);
#else
    va_start(args);
    level = va_arg(args, unsigned long);
    fmt = va_arg(args, char *);
#endif

    (void) sprintf(msg,"<%d>", level);
    if (loginfo.log_pid)
	(void) sprintf(msg,"(%d) ", getpid());
    if (*loginfo.ident)
	(void) sprintf(msg+strlen(msg),"%s: ", loginfo.ident);
    (void) vsprintf(msg+strlen(msg), fmt, args);
    va_end(args);

    /*
     * Eventually truncate the results (I hope 2*MAXLINE is enough !!!!)
     */
    l = strlen(msg);
    if (l > MAXLINE)
	l = MAXLINE;

    /*
     * Sends to the socket
     */
    if (sendto(LogSocket.f_socket, msg, l, 0,
	       (struct sockaddr *)&LogSocket.f_addr,
	       sizeof LogSocket.f_addr) != l) {
	int e = errno;
	(void) close(LogSocket.f_socket);
	errno = e;
	perror("rsyslog: sendto");
    }
    /*
     * That's it !
     */
}


/*
 *  This procedure opens a UDP connection to the syslog service of the
 * collecting machine. 
 *
 */
void
ropenlog(ident, logopt, facility, host)
char *ident;	/* same as in ordinary openlog() */
int logopt;	/* same as in openlog(), but only LOG_PID is used.
		 * All other options are quietly ignored.
		 */
int facility;	/* same as in openlog() -- but don't rely on using #define'd
		 * values here (e.g. LOG_USER or LOG_LOCAL1) because the
		 * loghost may have a different set of #define's for the
		 * facilities than the machine running ropenlog(), and thus
		 * may interpret this differently than you think.
		 */
char *host;	/* host is the name of the collecting machine.
		 * If a NULL pointer, gethostname() is used.
		 */
{
    char nhost[500];
    struct hostent *pHost;
    struct servent *pService;
    char   msg[MAXLINE];

    strncpy(loginfo.ident, ident, sizeof(loginfo.ident)-1);
    loginfo.ident[sizeof(loginfo.ident)-1] = '\0';
    loginfo.log_pid = logopt & LOG_PID;
    loginfo.def_facility = facility;

    /*
     * if no argument is given (this test is necessary to avoid
     * future possible core dumps)
     */
    if ( host==NULL ) {
	if (gethostname(nhost, sizeof(nhost)) == -1) {
	  perror("ropenlog(): host not defined and gethostname() failed:");
	  exit(-1);
	}
	host = nhost;
    }

    /*
     *  the hostent structure has to be used later
     */ 
    pHost = gethostbyname(host);

    /*
     * also verify that the host does exists
     */
    if (pHost == NULL) {
	(void) sprintf(msg, "ropenlog: unknown host %s", host);
	errno = 0;
	perror(msg);
	exit(-1);
    }

    /*
     * Network stuff
     */
    memset((char *) &LogSocket.f_addr,0,sizeof LogSocket.f_addr);

    LogSocket.f_addr.sin_family = AF_INET;

    /*
     * Use the service routine to figure what the syslog/udp port is
     * (If it doesn't exists on a machine (you never now !), it could be useful 
     * to force it to 514, but I never had this problem.)
     */
    pService = getservbyname("syslog", "udp");
    if (pService == NULL) {
	errno = 0;
	perror("ropenlog: syslog/udp is an unknown service");
	exit(-1);
    }
    LogSocket.f_addr.sin_port = pService->s_port;

    /*
     * Opens the connection
     */
    memcpy((char *) &LogSocket.f_addr.sin_addr,
	pHost->h_addr, pHost->h_length);
    LogSocket.f_socket = socket(AF_INET, SOCK_DGRAM, 0);
    if (LogSocket.f_socket < 0) {
	perror("ropenlog: socket");
	exit(-1);
    }

}

/*
 * This procedure closes the opened syslog connection.
 */
void
rcloselog()
{
    (void) close(LogSocket.f_socket);
}