Lines 47-48
Link Here
|
47 |
#include <netdb.h> |
47 |
#include <netdb.h> |
|
|
48 |
#include <netinet/in.h> |
48 |
|
49 |
|
Lines 108-132
Link Here
|
108 |
|
109 |
|
109 |
/* |
110 |
static const char * logtohost(host, tbuf, cnt) |
110 |
* syslog, vsyslog -- |
111 |
const char *host; |
111 |
* print message on log file; output is intended for syslogd(8). |
112 |
char *tbuf; |
112 |
*/ |
113 |
int cnt; |
113 |
void |
|
|
114 |
#if __STDC__ |
115 |
syslog(int pri, const char *fmt, ...) |
116 |
#else |
117 |
syslog(pri, fmt, va_alist) |
118 |
int pri; |
119 |
char *fmt; |
120 |
va_dcl |
121 |
#endif |
122 |
{ |
114 |
{ |
123 |
va_list ap; |
115 |
struct hostent *hp; |
|
|
116 |
static int finet; |
117 |
static struct sockaddr_in addr; |
118 |
|
119 |
/* initialize */ |
120 |
if(!finet) { |
121 |
finet = socket(AF_INET, SOCK_DGRAM, 0); |
122 |
if(finet == -1) { |
123 |
finet = 0; |
124 |
return strerror(errno); |
125 |
} |
126 |
} |
127 |
if(!addr.sin_family) { |
128 |
struct servent *sp; |
129 |
sp = getservbyname("syslog", "udp"); |
130 |
if(sp == NULL) |
131 |
return "syslog/udp: unknown service"; |
132 |
addr.sin_port = sp->s_port; |
133 |
addr.sin_family = AF_INET; |
134 |
} |
124 |
|
135 |
|
125 |
#if __STDC__ |
136 |
hp = gethostbyname(host); |
126 |
va_start(ap, fmt); |
137 |
if(hp == NULL) |
127 |
#else |
138 |
return hstrerror(h_errno); |
128 |
va_start(ap); |
139 |
memmove(&addr.sin_addr, hp->h_addr, hp->h_length); |
129 |
#endif |
140 |
|
130 |
vsyslog(pri, fmt, ap); |
141 |
if(sendto(finet, tbuf, cnt, 0, (struct sockaddr *)&addr, |
131 |
va_end(ap); |
142 |
sizeof(addr)) != cnt) |
|
|
143 |
return strerror(errno); |
144 |
|
145 |
return NULL; /* success */ |
132 |
} |
146 |
} |
Lines 134-138
Link Here
|
134 |
void |
148 |
void |
135 |
vsyslog(pri, fmt, ap) |
149 |
vsyslogh(pri, host, fmt, ap) |
136 |
int pri; |
150 |
int pri; |
137 |
register const char *fmt; |
151 |
register const char *fmt; |
|
|
152 |
const char *host; |
138 |
va_list ap; |
153 |
va_list ap; |
Lines 240-241
Link Here
|
240 |
|
255 |
|
|
|
256 |
/* If host is specified, try sending the message to it */ |
257 |
if(host) { |
258 |
const char *err; |
259 |
err = logtohost(host, tbuf, cnt); |
260 |
if(err == NULL) |
261 |
return; /* sent successfully */ |
262 |
else if(sizeof(tbuf) - cnt > 5) { |
263 |
/* if there is any room in tbuf, append the */ |
264 |
/* hostname and the error message from the */ |
265 |
/* logtohost, for it to be logged locally */ |
266 |
strncat(tbuf + cnt++ - 1, " ", 1); |
267 |
strncat(tbuf + cnt - 1, host, sizeof(tbuf) - cnt); |
268 |
cnt += strlen(host); |
269 |
if(sizeof(tbuf) - cnt > 3) { |
270 |
strncat(tbuf + cnt++ - 1, ":", 1); |
271 |
strncat(tbuf + cnt - 1, err, |
272 |
sizeof(tbuf) - cnt); |
273 |
cnt += strlen(err); |
274 |
} |
275 |
} |
276 |
} |
277 |
|
241 |
/* Get connected, output the message to the local logger. */ |
278 |
/* Get connected, output the message to the local logger. */ |
Lines 258-260
Link Here
|
258 |
* Output the message to the console; don't worry about blocking, |
295 |
* Output the message to the console; don't worry about blocking, |
259 |
* if console blocks everything will. Make sure the error reported |
296 |
* if console blocks, everything will. Make sure the error reported |
260 |
* is the one from the syslogd failure. |
297 |
* is the one from the syslogd failure. |
Lines 276-277
Link Here
|
276 |
} |
313 |
} |
|
|
314 |
|
315 |
/* |
316 |
* syslog, vsyslog -- |
317 |
* print message on log file; output is intended for syslogd(8). |
318 |
*/ |
319 |
void |
320 |
#if __STDC__ |
321 |
syslogh(int pri, const char *host, const char *fmt, ...) |
322 |
#else |
323 |
syslogh(pri, host, fmt, va_alist) |
324 |
int pri; |
325 |
char *fmt; |
326 |
const char *host; |
327 |
va_dcl |
328 |
#endif |
329 |
{ |
330 |
va_list ap; |
331 |
|
332 |
#if __STDC__ |
333 |
va_start(ap, fmt); |
334 |
#else |
335 |
va_start(ap); |
336 |
#endif |
337 |
vsyslogh(pri, host, fmt, ap); |
338 |
va_end(ap); |
339 |
} |
340 |
|
341 |
/* |
342 |
* syslog, vsyslog -- |
343 |
* print message on log file; output is intended for syslogd(8). |
344 |
*/ |
345 |
void |
346 |
#if __STDC__ |
347 |
syslog(int pri, const char *fmt, ...) |
348 |
#else |
349 |
syslog(pri, fmt, va_alist) |
350 |
int pri; |
351 |
char *fmt; |
352 |
va_dcl |
353 |
#endif |
354 |
{ |
355 |
va_list ap; |
356 |
|
357 |
#if __STDC__ |
358 |
va_start(ap, fmt); |
359 |
#else |
360 |
va_start(ap); |
361 |
#endif |
362 |
vsyslogh(pri, NULL, fmt, ap); |
363 |
va_end(ap); |
364 |
} |
365 |
|
366 |
void |
367 |
vsyslog(pri, fmt, ap) |
368 |
int pri; |
369 |
register const char *fmt; |
370 |
va_list ap; |
371 |
{ |
372 |
vsyslogh(pri, NULL, fmt, ap); |
373 |
} |
374 |
|
277 |
static void |
375 |
static void |