source: trunk/minix/commands/syslogd/syslogd.c@ 22

Last change on this file since 22 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 25.2 KB
Line 
1/*
2** Copyright (c) 1983, 1988
3** The Regents of the University of California. All rights reserved.
4**
5** Redistribution and use in source and binary forms, with or without
6** modification, are permitted provided that the following conditions
7** are met:
8** 1. Redistributions of source code must retain the above copyright
9** notice, this list of conditions and the following disclaimer.
10** 2. Redistributions in binary form must reproduce the above copyright
11** notice, this list of conditions and the following disclaimer in the
12** documentation and/or other materials provided with the distribution.
13** 3. All advertising materials mentioning features or use of this software
14** must display the following acknowledgement:
15** This product includes software developed by the University of
16** California, Berkeley and its contributors.
17** 4. Neither the name of the University nor the names of its contributors
18** may be used to endorse or promote products derived from this software
19** without specific prior written permission.
20**
21** THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24** ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31** SUCH DAMAGE.
32**
33** #ifndef lint
34** char copyright2[] =
35** "@(#) Copyright (c) 1983, 1988 Regents of the University of California.\n\
36** All rights reserved.\n";
37** #endif
38**
39** #ifndef lint
40** static char sccsid[] = "@(#)syslogd.c 5.27 (Berkeley) 10/10/88";
41** #endif
42**
43** -----------------------------------------------------------------------
44**
45** SYSLOGD -- log system messages
46** This program implements a system log.
47** It takes a series of lines and outputs them according to the setup
48** defined in the configuration file.
49** Each line may have a priority, signified as "<n>" as
50** the first characters of the line. If this is
51** not present, a default priority is used.
52**
53** To kill syslogd, send a signal 15 (terminate).
54** A signal 1 (hup) will cause it to reread its configuration file.
55**
56** Defined Constants:
57** MAXLINE -- the maximimum line length that can be handled.
58** MAXSVLINE -- the length of saved messages (for filtering)
59** DEFUPRI -- the default priority for user messages
60** DEFSPRI -- the default priority for kernel messages
61**
62** Author: Eric Allman
63** extensive changes by Ralph Campbell
64** more extensive changes by Eric Allman (again)
65** changes by Steve Lord
66**
67** Extensive rewriting by G. Falzoni <gfalzoni@inwind.it> for porting to Minix
68**
69** $Log: syslogd.c,v $
70** Revision 1.3 2006/04/04 14:22:40 beng
71** Fix
72**
73** Revision 1.2 2006/04/04 14:18:16 beng
74** Make syslogd work, even if it can only open klog and not udp or vice versa
75** (but not neither)
76**
77** Revision 1.1 2006/04/03 13:07:42 beng
78** Kick out usyslogd in favour of syslogd Giovanni's syslogd port
79**
80** Revision 1.3 2005/09/16 10:10:12 lsodgf0
81** Rework for Minix 3. Adds kernel logs from /dev/klogd
82**
83** $Id: syslogd.c,v 1.3 2006/04/04 14:22:40 beng Exp $
84*/
85
86#include <sys/types.h>
87#include <stdio.h>
88#include <ctype.h>
89#include <string.h>
90#include <unistd.h>
91#include <stdlib.h>
92#include <signal.h>
93#include <errno.h>
94#include <fcntl.h>
95#include <limits.h>
96#include <time.h>
97#include <sys/ioctl.h>
98#include <sys/select.h>
99#include <sys/wait.h>
100#include <net/netlib.h>
101#include <net/hton.h>
102#include <net/gen/in.h>
103#include <net/gen/udp.h>
104#include <net/gen/udp_io.h>
105#include <net/gen/netdb.h>
106
107#define SYSLOG_NAMES
108#include <syslog.h>
109#define KLOGD 1
110/** Define following values to your requirements **/
111#define MAXLINE 512 /* maximum line length */
112#define MAXSVLINE 256 /* maximum saved line length */
113
114#define DEFUPRI (LOG_USER|LOG_NOTICE)
115#define DEFSPRI (LOG_KERN|LOG_CRIT)
116
117/* Flags to logmsg() */
118#define IGN_CONS 0x001 /* don't print on console */
119#define SYNC_FILE 0x002 /* do fsync on file after printing */
120#define ADDDATE 0x004 /* add a date to the message */
121#define MARK 0x008 /* this message is a mark */
122
123#define CTTY "/dev/log" /* Minix log device (console) */
124
125#define dprintf if(DbgOpt!=0)printf
126#if debug == 0
127#define DEBUG(statement)
128#else
129#define DEBUG(statement) statement
130#endif
131#if !defined PIDFILE
132#define PIDFILE "/var/run/syslogd.pid"
133#endif
134
135#define UNAMESZ 8 /* length of a login name */
136#define MAXUNAMES 20 /* maximum number of user names */
137#define MAXFNAME 200 /* max file pathname length */
138#define MAXHOSTNAMELEN 64 /* max length of FQDN host name */
139
140/* Intervals at which we flush out "message repeated" messages,
141 * in seconds after previous message is logged. After each flush,
142 * we move to the next interval until we reach the largest. */
143#define TIMERINTVL 30 /* interval for checking flush, mark */
144#define INTERVAL1 30
145#define INTERVAL2 60
146#define MAXREPEAT ((sizeof(repeatinterval)/sizeof(repeatinterval[0]))-1)
147#define REPEATTIME(f) ((f)->f_time+repeatinterval[(f)->f_repeatcount])
148#define BACKOFF(f) {if(++(f)->f_repeatcount>MAXREPEAT)(f)->f_repeatcount=MAXREPEAT;}
149
150/* Values for f_type */
151#define F_UNUSED 0 /* unused entry */
152#define F_FILE 1 /* regular file */
153#define F_TTY 2 /* terminal */
154#define F_CONSOLE 3 /* console terminal */
155#define F_FORW 4 /* remote machine */
156#define F_USERS 5 /* list of users */
157#define F_WALL 6 /* everyone logged on */
158
159#define max(a,b) ((a)>=(b)?(a):(b))
160
161/* This structure represents the files that will have log copies printed */
162struct filed {
163 struct filed *f_next; /* next in linked list */
164 short f_type; /* entry type, see below */
165 short f_file; /* file descriptor */
166 time_t f_time; /* time this was last written */
167 char f_pmask[LOG_NFACILITIES + 1]; /* priority mask */
168 union {
169 char f_uname[MAXUNAMES][UNAMESZ + 1];
170 char f_fname[MAXFNAME];
171 } f_un;
172 char f_prevline[MAXSVLINE]; /* last message logged */
173 char f_lasttime[16]; /* time of last occurrence */
174 char f_prevhost[MAXHOSTNAMELEN + 1]; /* host from which recd. */
175 int f_prevpri; /* pri of f_prevline */
176 int f_prevlen; /* length of f_prevline */
177 int f_prevcount; /* repetition cnt of prevline */
178 int f_repeatcount; /* number of "repeated" msgs */
179 int f_flags; /* store some additional flags */
180};
181
182static const char *const TypeNames[] =
183{
184 "UNUSED", "FILE", "TTY", "CONSOLE", "FORW", "USERS", "WALL", NULL,
185};
186
187static struct filed *Files = NULL;
188static struct filed consfile;
189static int DbgOpt = 0; /* debug flag */
190static char LocalHostName[MAXHOSTNAMELEN + 1]; /* our hostname */
191static int Initialized = 0; /* set when we have initialized ourselves */
192static int MarkInterval = 20 * 60; /* interval between marks in seconds */
193static int MarkSeq = 0; /* mark sequence number */
194static time_t now;
195
196static const char *ConfFile = "/etc/syslog.conf";
197static const char *PidFile = PIDFILE; /* "/var/run/syslogd.pid" */
198static const char ctty[] = CTTY;
199
200static const char ProgName[] = "syslogd:";
201static const char version[] = "1.3 (Minix)";
202static const char usage[] =
203 /* */ "usage:\tsyslogd [-d] [-m markinterval] [-f conf-file]\n"
204 "\t\t[-p listeningport] [-v] [-?]\n" ;
205static const int repeatinterval[] =
206 /* */ {INTERVAL1, INTERVAL2,}; /* # of secs before flush */
207
208/*
209** Name: void wallmsg(struct filed *fLog, char *message);
210** Function: Write the specified message to either the entire
211** world, or a list of approved users.
212*/
213void wallmsg(struct filed * fLog, char *message)
214{
215
216 return;
217}
218
219/*
220** Name: void fprintlog(struct filed *fLog, int flags, char *message);
221** Function:
222*/
223void fprintlog(struct filed * fLog, int flags, char *message)
224{
225 int len;
226 char line[MAXLINE + 1];
227 char repbuf[80];
228
229 if (message == NULL) {
230 if (fLog->f_prevcount > 1) {
231 sprintf(repbuf, "last message repeated %d times", fLog->f_prevcount);
232 message = repbuf;
233 } else
234 message = fLog->f_prevline;
235 }
236 sprintf(line, "%s %s %s", fLog->f_lasttime, fLog->f_prevhost, message);
237 DEBUG(dprintf("Logging to %s", TypeNames[fLog->f_type]);)
238 fLog->f_time = now;
239 switch (fLog->f_type) {
240 case F_UNUSED: /* */
241 DEBUG(dprintf("\n");)
242 break;
243 case F_CONSOLE:
244 if (flags & IGN_CONS) {
245 case F_FORW: /* */
246 DEBUG(dprintf(" (ignored)\n");)
247 break;
248 } /* else Fall Through */
249 case F_TTY:
250 case F_FILE:
251 DEBUG(dprintf(" %s\n", fLog->f_un.f_fname);)
252 strcat(line, fLog->f_type != F_FILE ? "\r\n" : "\n");
253 len = strlen(line);
254 if (write(fLog->f_file, line, len) != len) {
255 /* Handle errors */ ;
256 } else if (flags & SYNC_FILE)
257 sync();
258 break;
259 case F_USERS:
260 case F_WALL:
261 DEBUG(dprintf("\n");)
262 strcat(line, "\r\n");
263 wallmsg(fLog, line);
264 break;
265 }
266 fLog->f_prevcount = 0;
267 return;
268}
269
270/*
271** Name: void logmsg(int pri, char *msg, char *from, int flags);
272** Function: Log a message to the appropriate log files, users, etc.
273** based on the priority.
274*/
275void logmsg(int pri, char *msg, char *from, int flags)
276{
277 struct filed *f;
278 int fac, prilev;
279 int omask, msglen;
280 char *timestamp;
281
282 DEBUG(dprintf("logmsg: pri %o, flags %x, from %s, msg %s\n", pri, flags, from, msg);)
283/*
284 omask = sigblock(__sigmask(SIGHUP) | __sigmask(SIGALRM));
285*/
286 /* Check to see if msg looks non-standard. */
287 msglen = strlen(msg);
288 if (msglen < 16 || msg[3] != ' ' || msg[6] != ' ' ||
289 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ')
290 flags |= ADDDATE;
291
292 time(&now);
293 if (flags & ADDDATE)
294 timestamp = ctime(&now) + 4;
295 else {
296 timestamp = msg;
297 msg += 16;
298 msglen -= 16;
299 }
300
301 /* Extract facility and priority level */
302 fac = (flags & MARK) ? LOG_NFACILITIES : LOG_FAC(pri);
303 prilev = LOG_PRI(pri);
304
305 /* Log the message to the particular outputs */
306 if (!Initialized) {
307 /* Not yet initialized. Every message goes to console */
308 f = &consfile;
309 f->f_file = open(ctty, O_WRONLY | O_NOCTTY);
310 if (f->f_file >= 0) {
311 if (!DbgOpt) setsid();
312 fprintlog(f, flags, msg);
313 close(f->f_file);
314 }
315 } else {
316 for (f = Files; f; f = f->f_next) {
317
318 /* Skip messages that are incorrect priority */
319 if (f->f_pmask[fac] < prilev || f->f_pmask[fac] == INTERNAL_NOPRI)
320 continue;
321
322 if (f->f_type == F_CONSOLE && (flags & IGN_CONS)) continue;
323
324 /* Don't output marks to recently written files */
325 if ((flags & MARK) && (now - f->f_time) < MarkInterval / 2)
326 continue;
327
328 /* Suppress duplicate lines to this file */
329 if ((flags & MARK) == 0 && msglen == f->f_prevlen &&
330 !strcmp(msg, f->f_prevline) &&
331 !strcmp(from, f->f_prevhost)) {
332 strncpy(f->f_lasttime, timestamp, 15);
333 f->f_prevcount += 1;
334 DEBUG(dprintf("msg repeated %d times, %ld sec of %d\n",
335 f->f_prevcount, now - f->f_time,
336 repeatinterval[f->f_repeatcount]);)
337 /* If domark would have logged this by now,
338 * flush it now (so we don't hold isolated
339 * messages), but back off so we'll flush
340 * less often in the future. */
341 if (now > REPEATTIME(f)) {
342 fprintlog(f, flags, (char *) NULL);
343 BACKOFF(f);
344 }
345 } else {
346 /* New line, save it */
347 if (f->f_prevcount) fprintlog(f, 0, (char *) NULL);
348 f->f_repeatcount = 0;
349 strncpy(f->f_lasttime, timestamp, 15);
350 strncpy(f->f_prevhost, from, sizeof(f->f_prevhost));
351 if (msglen < MAXSVLINE) {
352 f->f_prevlen = msglen;
353 f->f_prevpri = pri;
354 strcpy(f->f_prevline, msg);
355 fprintlog(f, flags, (char *) NULL);
356 } else {
357 f->f_prevline[0] = 0;
358 f->f_prevlen = 0;
359 fprintlog(f, flags, msg);
360 }
361 }
362 }
363 }
364
365/*
366 sigsetmask(omask);
367*/
368 return;
369}
370
371/*
372** Name: void logerror(char *type);
373** Function: Prints syslogd errors in some place.
374*/
375void logerror(char *type)
376{
377 char buf[100];
378
379 if (errno == 0) sprintf(buf, "%s %s", ProgName, type);
380
381 else if (errno >= _NERROR)
382 sprintf(buf, "%s %s - error %d", ProgName, type, errno);
383
384 else
385 sprintf(buf, "%s %s - %s", ProgName, type, strerror(errno));
386
387 errno = 0;
388 dprintf("%s\n", buf);
389 logmsg(LOG_SYSLOG | LOG_ERR, buf, LocalHostName, ADDDATE);
390 return;
391}
392
393/*
394** Name: void die(int sig);
395** Function: Signal handler for kill signals.
396*/
397void die(int sig)
398{
399 struct filed *f;
400 char buf[100];
401
402 for (f = Files; f != NULL; f = f->f_next) {
403 /* Flush any pending output */
404 if (f->f_prevcount) fprintlog(f, 0, NULL);
405 }
406 if (sig >= 0) {
407 DEBUG(dprintf("%s exiting on signal %d\n", ProgName, sig);)
408 sprintf(buf, "exiting on signal %d", sig);
409 errno = 0;
410 logerror(buf);
411 }
412 unlink(PidFile);
413 exit(sig == (-1) ? EXIT_FAILURE : EXIT_SUCCESS);
414}
415
416/*
417** Name: void domark(int sig);
418** Function: Signal handler for alarm.
419** Used for messages filtering and mark facility.
420*/
421void domark(int sig)
422{
423 struct filed *f;
424
425 now = time(NULL);
426 MarkSeq += TIMERINTVL;
427 if (MarkSeq >= MarkInterval) {
428 logmsg(LOG_INFO, "-- MARK --", LocalHostName, ADDDATE | MARK);
429 MarkSeq = 0;
430 }
431 for (f = Files; f; f = f->f_next) {
432 if (f->f_prevcount && now >= REPEATTIME(f)) {
433 DEBUG(dprintf("flush %s: repeated %d times, %d sec.\n",
434 TypeNames[f->f_type], f->f_prevcount,
435 repeatinterval[f->f_repeatcount]);)
436 fprintlog(f, 0, NULL);
437 BACKOFF(f);
438 }
439 }
440 signal(SIGALRM, domark);
441 alarm(TIMERINTVL);
442 return;
443}
444
445/*
446** Name: int decode(char *name, struct _code *codetab);
447** Function: Decode a symbolic name to a numeric value
448*/
449int decode(char *name, const struct _code *codetab)
450{
451 const struct _code *c;
452 char *p;
453 char buf[40];
454
455 DEBUG(dprintf("symbolic name: %s", name);)
456 if (isdigit(*name)) return (atoi(name));
457
458 strcpy(buf, name);
459 for (p = buf; *p; p += 1) {
460 if (isupper(*p)) *p = tolower(*p);
461 }
462 for (c = codetab; c->c_name; c += 1) {
463 if (!strcmp(buf, c->c_name)) {
464 DEBUG(dprintf(" ==> %d\n", c->c_val);)
465 return (c->c_val);
466 }
467 }
468 return (-1);
469}
470
471/*
472** Name: void cfline(char *line, struct filed *f);
473** Function: Parse a configuration file line
474*/
475void cfline(char *line, struct filed * fLog)
476{
477 char *p, *q, *bp;
478 int ix, pri;
479 char buf[MAXLINE];
480 char xbuf[200];
481
482 DEBUG(dprintf("cfline(%s)\n", line);)
483
484 /* Keep sys_errlist stuff out of logerror messages */
485 errno = 0;
486
487 /* Clear out file entry */
488 memset(fLog, 0, sizeof(*fLog));
489 for (ix = 0; ix <= LOG_NFACILITIES; ix += 1) /* */
490 fLog->f_pmask[ix] = INTERNAL_NOPRI;
491
492 /* Scan through the list of selectors */
493 for (p = line; *p && *p != '\t';) {
494
495 /* Find the end of this facility name list */
496 for (q = p; *q && *q != '\t' && *q++ != '.';) continue;
497
498 /* Collect priority name */
499 for (bp = buf; *q && !strchr("\t,;", *q);) *bp++ = *q++;
500 *bp = '\0';
501
502 /* Skip cruft */
503 while (strchr(", ;", *q)) q++;
504
505 /* Decode priority name */
506 pri = decode(buf, PriNames);
507 if (pri < 0) {
508 sprintf(xbuf, "unknown priority name \"%s\"", buf);
509 logerror(xbuf);
510 return;
511 }
512
513 /* Scan facilities */
514 while (*p && !strchr("\t.;", *p)) {
515 for (bp = buf; *p && !strchr("\t,;.", *p);) *bp++ = *p++;
516 *bp = '\0';
517 if (*buf == '*') {
518 for (ix = 0; ix <= LOG_NFACILITIES; ix += 1)
519 if ((fLog->f_pmask[ix] < pri) ||
520 (fLog->f_pmask[ix] == INTERNAL_NOPRI)) {
521 fLog->f_pmask[ix] = pri;
522 }
523 } else {
524 ix = decode(buf, FacNames);
525 if (ix < 0) {
526 sprintf(xbuf, "unknown facility name \"%s\"", buf);
527 logerror(xbuf);
528 return;
529 }
530 if ((fLog->f_pmask[ix >> 3] < pri) ||
531 (fLog->f_pmask[ix >> 3] == INTERNAL_NOPRI)) {
532 fLog->f_pmask[ix >> 3] = pri;
533 }
534 }
535 while (*p == ',' || *p == ' ') p++;
536 }
537 p = q;
538 }
539
540 /* Skip to action part */
541 while (*p == '\t' || *p == ' ') p++;
542
543 DEBUG(dprintf("leading char in action: %c\n", *p);)
544 switch (*p) {
545 case '@': /* Logging to a remote host */
546 break; /* NOT IMPLEMENTED */
547
548 case '/': /* Logging to a local file/device */
549 strcpy(fLog->f_un.f_fname, p);
550 DEBUG(dprintf("filename: %s\n", p); /* ASP */)
551 if ((fLog->f_file = open(p, O_WRONLY | O_APPEND | O_CREAT | O_NOCTTY, 0644)) < 0) {
552 fLog->f_file = F_UNUSED;
553 sprintf(xbuf, "unknown file/device (%s)", p);
554 logerror(xbuf);
555 break;
556 }
557 if (isatty(fLog->f_file)) {
558 if (!DbgOpt) setsid();
559 fLog->f_type = F_TTY;
560 } else
561 fLog->f_type = F_FILE;
562 if (strcmp(p, ctty) == 0) fLog->f_type = F_CONSOLE;
563 break;
564
565 case '*': /* Logging to all users */
566 DEBUG(dprintf("write-all\n");)
567 fLog->f_type = F_WALL;
568 break;
569
570 default: /* Logging to selected users */
571 DEBUG(dprintf("users: %s\n", p); /* ASP */)
572 for (ix = 0; ix < MAXUNAMES && *p; ix += 1) {
573 for (q = p; *q && *q != ',';) q += 1;
574 strncpy(fLog->f_un.f_uname[ix], p, UNAMESZ);
575 if ((q - p) > UNAMESZ)
576 fLog->f_un.f_uname[ix][UNAMESZ] = '\0';
577 else
578 fLog->f_un.f_uname[ix][q - p] = '\0';
579 while (*q == ',' || *q == ' ') q++;
580 p = q;
581 }
582 fLog->f_type = F_USERS;
583 break;
584 }
585}
586
587/*
588** Name: void printline(char *hname, char *msg);
589** Function: Takes a raw input line, decodes the message and
590** prints the message on the appropriate log files.
591*/
592void printline(char *hname, char *msg)
593{
594 char line[MAXLINE + 1];
595 char *p = msg, *q = line;
596 int ch, pri = DEFUPRI;
597
598 /* Test for special codes */
599 if (*p == '<') {
600 pri = 0;
601 while (isdigit(*++p)) {
602 if ((*p - '0') < 8) {
603 /* Only 3 bits allocated for pri -- ASP */
604 pri = 10 * pri + (*p - '0');
605 } else
606 pri = 10 * pri + 7;
607 }
608 if (*p == '>') ++p;
609 }
610 if (pri & ~(LOG_FACMASK | LOG_PRIMASK)) pri = DEFUPRI;
611
612 /* Does not allow users to log kernel messages */
613 if (LOG_FAC(pri) == LOG_KERN) pri = LOG_MAKEPRI(LOG_USER, LOG_PRI(pri));
614
615 /* Copies message to local buffer, translating control characters */
616 while ((ch = *p++ & 0177) != '\0' && q < &line[sizeof(line) - 1]) {
617 if (ch == '\n') /* Removes newlines */
618 *q++ = ' ';
619 else if (iscntrl(ch)) { /* Translates control characters */
620 *q++ = '^';
621 *q++ = ch ^ 0100;
622 } else
623 *q++ = ch;
624 }
625 *q = '\0';
626
627 logmsg(pri, line, hname, 0);
628 return;
629}
630
631/*
632** Name: void printkline(char *hname, char *msg);
633** Function: Takes a raw input line from kernel and
634** prints the message on the appropriate log files.
635*/
636void printkline(char *hname, char *msg)
637{
638 char line[MAXLINE + 1];
639 char *p = msg, *q = line;
640 int ch, pri = DEFUPRI;
641
642 /* Copies message to local buffer, adding source program tag */
643 sprintf(line, "kernel: %s", msg);
644
645 logmsg(LOG_KERN | LOG_INFO, line, hname, ADDDATE);
646 return;
647}
648
649/*
650** Name: void init(int sig);
651** Function: Initialize syslogd from configuration file.
652** Used at startup or after a SIGHUP signal.
653*/
654void init(int sig)
655{
656 int i;
657 FILE *cf;
658 struct filed *fLog, *next, **nextp;
659 char *p;
660 char cline[BUFSIZ];
661
662 DEBUG(dprintf("init\n");)
663
664 /* Close all open log files. */
665 Initialized = 0;
666 for (fLog = Files; fLog != NULL; fLog = next) {
667
668 /* Flush any pending output */
669 if (fLog->f_prevcount) fprintlog(fLog, 0, NULL);
670
671 switch (fLog->f_type) {
672 case F_FILE:
673 case F_TTY:
674 case F_CONSOLE: close(fLog->f_file); break;
675 }
676 next = fLog->f_next;
677 free((char *) fLog);
678 }
679 Files = NULL;
680 nextp = &Files;
681
682 /* Open the configuration file */
683 if ((cf = fopen(ConfFile, "r")) != NULL) {
684 /* Foreach line in the configuration table, open that file. */
685 fLog = NULL;
686 while (fgets(cline, sizeof(cline), cf) != NULL) {
687 /* Check for end-of-section, comments, strip off
688 * trailing spaces and newline character. */
689 for (p = cline; isspace(*p); p += 1);
690 if (*p == '\0' || *p == '#') continue;
691 for (p = strchr(cline, '\0'); isspace(*--p););
692 *++p = '\0';
693 fLog = (struct filed *) calloc(1, sizeof(*fLog));
694 *nextp = fLog;
695 nextp = &fLog->f_next;
696 cfline(cline, fLog);
697 }
698
699 /* Close the configuration file */
700 fclose(cf);
701 Initialized = 1;
702DEBUG (
703 if (DbgOpt) {
704 for (fLog = Files; fLog; fLog = fLog->f_next) {
705 for (i = 0; i <= LOG_NFACILITIES; i += 1)
706 if (fLog->f_pmask[i] == INTERNAL_NOPRI)
707 printf("X ");
708 else
709 printf("%d ", fLog->f_pmask[i]);
710 printf("%s: ", TypeNames[fLog->f_type]);
711 switch (fLog->f_type) {
712 case F_FILE:
713 case F_TTY:
714 case F_CONSOLE:
715 printf("%s", fLog->f_un.f_fname);
716 break;
717 case F_FORW:
718 break;
719 case F_USERS:
720 for (i = 0; i < MAXUNAMES && *fLog->f_un.f_uname[i]; i += 1)
721 printf("%s, ", fLog->f_un.f_uname[i]);
722 break;
723 }
724 printf("\n");
725 }
726 }
727 )
728 logmsg(LOG_SYSLOG | LOG_INFO, "syslogd: restart", LocalHostName, ADDDATE);
729 signal(SIGHUP, init);
730 DEBUG(dprintf("%s restarted\n", ProgName);)
731 } else {
732 DEBUG(dprintf("cannot open %s\n", ConfFile);)
733 *nextp = (struct filed *) calloc(1, sizeof(*fLog));
734 cfline("*.ERR\t" CTTY, *nextp);
735 (*nextp)->f_next = (struct filed *) calloc(1, sizeof(*fLog));
736 cfline("*.PANIC\t*", (*nextp)->f_next);
737 Initialized = 1;
738 }
739 return;
740}
741
742/*
743** Name: void daemonize(char *line);
744** Function: Clone itself and becomes a daemon releasing unnecessay resources.
745*/
746void daemonize(char *line)
747{
748 int lfd, len, pid;
749
750 if ((lfd = open(PidFile, O_CREAT | O_RDWR, 0600)) > 0) {
751 len = read(lfd, line, 10);
752 line[len] = '\0';
753 close(lfd);
754 if ((kill(len = atoi(line), 0) < 0 && errno == ESRCH) || len == 0) {
755 if (!DbgOpt) {
756 /* Parent ends and child becomes a daemon */
757 if ((pid = fork()) > 0) {
758 /* Write process id. in pid file */
759 lfd = open(PidFile, O_TRUNC | O_WRONLY);
760 len = sprintf(line, "%5d", pid);
761 write(lfd, line, len);
762 close(lfd);
763
764 /* Wait for initialization to complete */
765 exit(EXIT_SUCCESS);
766 }
767 sleep(1);
768 setsid(); /* Set as session leader */
769 chdir("/"); /* Change to the root directory */
770 /* Get rid of all open files */
771 for (lfd = STDERR_FILENO + 1; lfd < OPEN_MAX; lfd += 1)
772 close(lfd);
773 }
774 } else {
775 fprintf(stderr, "\n%s already running\n", ProgName);
776 exit(EXIT_FAILURE);
777 }
778 } else {
779 fprintf(stderr, "\n%s can't open %s (%s)\n", ProgName, PidFile, strerror(errno));
780 exit(EXIT_FAILURE);
781 }
782 return;
783}
784
785/*
786** Name: int main(int argc, char **argv);
787** Function: Syslog daemon entry point
788*/
789int main(int argc, char **argv)
790{
791 char *p, *udpdev, *eol;
792 int nfd, kfd, len, fdmax;
793 int ch, port = 0;
794 fd_set fdset;
795 struct nwio_udpopt udpopt;
796 struct servent *sp;
797 char line[MAXLINE + 1];
798
799 while ((ch = getopt(argc, argv, "df:m:p:v?")) != EOF) {
800 switch ((char) ch) {
801 case 'd': /* Debug */
802 DbgOpt += 1;
803 break;
804 case 'f': /* Set configuration file */
805 ConfFile = optarg;
806 break;
807 case 'm': /* Set mark interval */
808 MarkInterval = atoi(optarg) * 60;
809 break;
810 case 'p': /* Set listening port */
811 port = atoi(optarg);
812 break;
813 case 'v': /* Print version */
814 fprintf(stderr, "%s version %s\n", ProgName, version);
815 return EXIT_FAILURE;
816 case '?': /* Help */
817 default:
818 fprintf(stderr, usage);
819 return EXIT_FAILURE;
820 }
821 }
822 if (argc -= optind) {
823 fprintf(stderr, usage);
824 return EXIT_FAILURE;
825 }
826
827 daemonize(line);
828
829 /* Get the official name of local host. */
830 gethostname(LocalHostName, sizeof(LocalHostName) - 1);
831 if ((p = strchr(LocalHostName, '.'))) *p = '\0';
832
833 udpdev = (p = getenv("UDP_DEVICE")) ? p : UDP_DEVICE;
834 sp = getservbyname("syslog", "udp");
835
836 signal(SIGTERM, die);
837 signal(SIGINT, DbgOpt ? die : SIG_IGN);
838 signal(SIGQUIT, DbgOpt ? die : SIG_IGN);
839 signal(SIGALRM, domark);
840
841 alarm(TIMERINTVL);
842
843 /* Open UDP device */
844 nfd = open(udpdev, O_NONBLOCK | O_RDONLY);
845
846 /* Configures the UDP device */
847 udpopt.nwuo_flags = NWUO_SHARED | NWUO_LP_SET | NWUO_EN_LOC |
848 NWUO_DI_BROAD | NWUO_RP_SET | NWUO_RA_SET |
849 NWUO_RWDATONLY | NWUO_DI_IPOPT;
850 udpopt.nwuo_locport = udpopt.nwuo_remport =
851 port == 0 ? sp->s_port : htons(port);
852 udpopt.nwuo_remaddr = udpopt.nwuo_locaddr = htonl(0x7F000001L);
853
854 if(nfd >= 0) {
855 while (ioctl(nfd, NWIOSUDPOPT, &udpopt) < 0 ||
856 ioctl(nfd, NWIOGUDPOPT, &udpopt) < 0) {
857 if (errno == EAGAIN) {
858 sleep(1);
859 continue;
860 }
861 logerror("Set/Get UDP options failed");
862 return EXIT_FAILURE;
863 }
864 }
865
866 /* Open kernel log device */
867 kfd = open("/dev/klog", O_NONBLOCK | O_RDONLY);
868
869 if(kfd < 0 && nfd < 0) {
870 logerror("open /dev/klog and udp device failed - can't log anything");
871 return EXIT_FAILURE;
872 }
873
874 fdmax = max(nfd, kfd) + 1;
875
876 DEBUG(dprintf("off & running....\n");)
877
878 init(-1); /* Initilizes log data structures */
879
880 for (;;) { /* Main loop */
881
882 FD_ZERO(&fdset); /* Setup descriptors for select */
883 if(nfd >= 0) FD_SET(nfd, &fdset);
884 if(kfd >= 0) FD_SET(kfd, &fdset);
885
886 if (select(fdmax, &fdset, NULL, NULL, NULL) <= 0) {
887 sleep(1);
888 continue;
889
890 }
891 if (nfd >= 0 && FD_ISSET(nfd, &fdset)) {
892
893 /* Read a message from application programs */
894 len = read(nfd, line, MAXLINE);
895 if (len > 0) { /* Got a message */
896 line[len] = '\0';
897 dprintf("got a message (%d, %#x)\n", nfd, len);
898 printline(LocalHostName, line);
899
900 } else if (len < 0) { /* Got an error or signal while reading */
901 if (errno != EINTR) /* */
902 logerror("Receive error from UDP channel");
903
904 } else { /* (len == 0) Channel has been closed */
905 logerror("UDP channel has closed");
906 close(nfd);
907 die(-1);
908 }
909 }
910 if (kfd >= 0 && FD_ISSET(kfd, &fdset)) {
911 static char linebuf[5*1024];
912
913 /* Read a message from kernel (klog) */
914 len = read(kfd, linebuf, sizeof(linebuf)-2);
915 dprintf("got a message (%d, %#x)\n", kfd, len);
916 for (ch = 0; ch < len; ch += 1)
917 if (linebuf[ch] == '\0') linebuf[ch] = ' ';
918 if (linebuf[len - 1] == '\n') len -= 1;
919 linebuf[len] = '\n';
920 linebuf[len + 1] = '\0';
921 p = linebuf;
922 while(eol = strchr(p, '\n')) {
923 *eol = '\0';
924 printkline(LocalHostName, p);
925 p = eol+1;
926 }
927 }
928 }
929 /* Control never gets here */
930}
931
932/** syslogd.c **/
Note: See TracBrowser for help on using the repository browser.