ANSI C
[unix-history] / usr / src / usr.sbin / syslogd / syslogd.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 1983, 1988 Regents of the University of California.
3 * All rights reserved.
4 *
5 * %sccs.include.redist.c%
6 */
7
8#ifndef lint
9char copyright[] =
10"@(#) Copyright (c) 1983, 1988 Regents of the University of California.\n\
11 All rights reserved.\n";
12#endif /* not lint */
13
14#ifndef lint
15static char sccsid[] = "@(#)syslogd.c 5.48 (Berkeley) %G%";
16#endif /* not lint */
17
18/*
19 * syslogd -- log system messages
20 *
21 * This program implements a system log. It takes a series of lines.
22 * Each line may have a priority, signified as "<n>" as
23 * the first characters of the line. If this is
24 * not present, a default priority is used.
25 *
26 * To kill syslogd, send a signal 15 (terminate). A signal 1 (hup) will
27 * cause it to reread its configuration file.
28 *
29 * Defined Constants:
30 *
31 * MAXLINE -- the maximimum line length that can be handled.
32 * DEFUPRI -- the default priority for user messages
33 * DEFSPRI -- the default priority for kernel messages
34 *
35 * Author: Eric Allman
36 * extensive changes by Ralph Campbell
37 * more extensive changes by Eric Allman (again)
38 */
39
40#define MAXLINE 1024 /* maximum line length */
41#define MAXSVLINE 120 /* maximum saved line length */
42#define DEFUPRI (LOG_USER|LOG_NOTICE)
43#define DEFSPRI (LOG_KERN|LOG_CRIT)
44#define TIMERINTVL 30 /* interval for checking flush, mark */
45
46#include <sys/param.h>
47#include <sys/errno.h>
48#include <sys/ioctl.h>
49#include <sys/stat.h>
50#include <sys/wait.h>
51#include <sys/socket.h>
52#include <sys/file.h>
53#include <sys/msgbuf.h>
54#include <sys/uio.h>
55#include <sys/un.h>
56#include <sys/time.h>
57#include <sys/resource.h>
58#include <sys/signal.h>
59
60#include <netinet/in.h>
61#include <netdb.h>
62
63#include <utmp.h>
64#include <setjmp.h>
65#include <stdio.h>
66#include <ctype.h>
67#include <string.h>
68#include <unistd.h>
69#include "pathnames.h"
70
71#define SYSLOG_NAMES
72#include <sys/syslog.h>
73
74char *LogName = _PATH_LOG;
75char *ConfFile = _PATH_LOGCONF;
76char *PidFile = _PATH_LOGPID;
77char ctty[] = _PATH_CONSOLE;
78
79#define FDMASK(fd) (1 << (fd))
80
81#define dprintf if (Debug) printf
82
83#define MAXUNAMES 20 /* maximum number of user names */
84
85/*
86 * Flags to logmsg().
87 */
88
89#define IGN_CONS 0x001 /* don't print on console */
90#define SYNC_FILE 0x002 /* do fsync on file after printing */
91#define ADDDATE 0x004 /* add a date to the message */
92#define MARK 0x008 /* this message is a mark */
93
94/*
95 * This structure represents the files that will have log
96 * copies printed.
97 */
98
99struct filed {
100 struct filed *f_next; /* next in linked list */
101 short f_type; /* entry type, see below */
102 short f_file; /* file descriptor */
103 time_t f_time; /* time this was last written */
104 u_char f_pmask[LOG_NFACILITIES+1]; /* priority mask */
105 union {
106 char f_uname[MAXUNAMES][UT_NAMESIZE+1];
107 struct {
108 char f_hname[MAXHOSTNAMELEN+1];
109 struct sockaddr_in f_addr;
110 } f_forw; /* forwarding address */
111 char f_fname[MAXPATHLEN];
112 } f_un;
113 char f_prevline[MAXSVLINE]; /* last message logged */
114 char f_lasttime[16]; /* time of last occurrence */
115 char f_prevhost[MAXHOSTNAMELEN+1]; /* host from which recd. */
116 int f_prevpri; /* pri of f_prevline */
117 int f_prevlen; /* length of f_prevline */
118 int f_prevcount; /* repetition cnt of prevline */
119 int f_repeatcount; /* number of "repeated" msgs */
120};
121
122/*
123 * Intervals at which we flush out "message repeated" messages,
124 * in seconds after previous message is logged. After each flush,
125 * we move to the next interval until we reach the largest.
126 */
127int repeatinterval[] = { 30, 120, 600 }; /* # of secs before flush */
128#define MAXREPEAT ((sizeof(repeatinterval) / sizeof(repeatinterval[0])) - 1)
129#define REPEATTIME(f) ((f)->f_time + repeatinterval[(f)->f_repeatcount])
130#define BACKOFF(f) { if (++(f)->f_repeatcount > MAXREPEAT) \
131 (f)->f_repeatcount = MAXREPEAT; \
132 }
133
134/* values for f_type */
135#define F_UNUSED 0 /* unused entry */
136#define F_FILE 1 /* regular file */
137#define F_TTY 2 /* terminal */
138#define F_CONSOLE 3 /* console terminal */
139#define F_FORW 4 /* remote machine */
140#define F_USERS 5 /* list of users */
141#define F_WALL 6 /* everyone logged on */
142
143char *TypeNames[7] = {
144 "UNUSED", "FILE", "TTY", "CONSOLE",
145 "FORW", "USERS", "WALL"
146};
147
148struct filed *Files;
149struct filed consfile;
150
151int Debug; /* debug flag */
152char LocalHostName[MAXHOSTNAMELEN+1]; /* our hostname */
153char *LocalDomain; /* our local domain name */
154int InetInuse = 0; /* non-zero if INET sockets are being used */
155int finet; /* Internet datagram socket */
156int LogPort; /* port number for INET connections */
157int Initialized = 0; /* set when we have initialized ourselves */
158int MarkInterval = 20 * 60; /* interval between marks in seconds */
159int MarkSeq = 0; /* mark sequence number */
160
161extern int errno;
162extern char *ctime(), *index(), *calloc();
163
164main(argc, argv)
165 int argc;
166 char **argv;
167{
168 register int i;
169 register char *p;
170 int funix, inetm, fklog, klogm, len;
171 struct sockaddr_un sunx, fromunix;
172 struct sockaddr_in sin, frominet;
173 FILE *fp;
174 int ch;
175 char line[MSG_BSIZE + 1];
176 extern int optind;
177 extern char *optarg;
178 void die(), domark(), init(), reapchild();
179
180 while ((ch = getopt(argc, argv, "df:m:p:")) != EOF)
181 switch((char)ch) {
182 case 'd': /* debug */
183 Debug++;
184 break;
185 case 'f': /* configuration file */
186 ConfFile = optarg;
187 break;
188 case 'm': /* mark interval */
189 MarkInterval = atoi(optarg) * 60;
190 break;
191 case 'p': /* path */
192 LogName = optarg;
193 break;
194 case '?':
195 default:
196 usage();
197 }
198 if (argc -= optind)
199 usage();
200
201 if (!Debug)
202 daemon(0, 0);
203 else
204 setlinebuf(stdout);
205
206 consfile.f_type = F_CONSOLE;
207 (void) strcpy(consfile.f_un.f_fname, ctty);
208 (void) gethostname(LocalHostName, sizeof LocalHostName);
209 if (p = index(LocalHostName, '.')) {
210 *p++ = '\0';
211 LocalDomain = p;
212 }
213 else
214 LocalDomain = "";
215 (void) signal(SIGTERM, die);
216 (void) signal(SIGINT, Debug ? die : SIG_IGN);
217 (void) signal(SIGQUIT, Debug ? die : SIG_IGN);
218 (void) signal(SIGCHLD, reapchild);
219 (void) signal(SIGALRM, domark);
220 (void) alarm(TIMERINTVL);
221 (void) unlink(LogName);
222
223 bzero((char *)&sunx, sizeof(sunx));
224 sunx.sun_family = AF_UNIX;
225 (void) strncpy(sunx.sun_path, LogName, sizeof sunx.sun_path);
226 funix = socket(AF_UNIX, SOCK_DGRAM, 0);
227 if (funix < 0 || bind(funix, (struct sockaddr *) &sunx,
228 sizeof(sunx.sun_family)+sizeof(sunx.sun_len)+
229 strlen(sunx.sun_path)) < 0 ||
230 chmod(LogName, 0666) < 0) {
231 (void) sprintf(line, "cannot create %s", LogName);
232 logerror(line);
233 dprintf("cannot create %s (%d)\n", LogName, errno);
234 die(0);
235 }
236 finet = socket(AF_INET, SOCK_DGRAM, 0);
237 if (finet >= 0) {
238 struct servent *sp;
239
240 sp = getservbyname("syslog", "udp");
241 if (sp == NULL) {
242 errno = 0;
243 logerror("syslog/udp: unknown service");
244 die(0);
245 }
246 bzero((char *)&sin, sizeof(sin));
247 sin.sin_family = AF_INET;
248 sin.sin_port = LogPort = sp->s_port;
249 if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
250 logerror("bind");
251 if (!Debug)
252 die(0);
253 } else {
254 inetm = FDMASK(finet);
255 InetInuse = 1;
256 }
257 }
258 if ((fklog = open(_PATH_KLOG, O_RDONLY, 0)) >= 0)
259 klogm = FDMASK(fklog);
260 else {
261 dprintf("can't open %s (%d)\n", _PATH_KLOG, errno);
262 klogm = 0;
263 }
264
265 /* tuck my process id away */
266 fp = fopen(PidFile, "w");
267 if (fp != NULL) {
268 fprintf(fp, "%d\n", getpid());
269 (void) fclose(fp);
270 }
271
272 dprintf("off & running....\n");
273
274 init();
275 (void) signal(SIGHUP, init);
276
277 for (;;) {
278 int nfds, readfds = FDMASK(funix) | inetm | klogm;
279
280 errno = 0;
281 dprintf("readfds = %#x\n", readfds);
282 nfds = select(20, (fd_set *) &readfds, (fd_set *) NULL,
283 (fd_set *) NULL, (struct timeval *) NULL);
284 if (nfds == 0)
285 continue;
286 if (nfds < 0) {
287 if (errno != EINTR)
288 logerror("select");
289 continue;
290 }
291 dprintf("got a message (%d, %#x)\n", nfds, readfds);
292 if (readfds & klogm) {
293 i = read(fklog, line, sizeof(line) - 1);
294 if (i > 0) {
295 line[i] = '\0';
296 printsys(line);
297 } else if (i < 0 && errno != EINTR) {
298 logerror("klog");
299 fklog = -1;
300 klogm = 0;
301 }
302 }
303 if (readfds & FDMASK(funix)) {
304 len = sizeof fromunix;
305 i = recvfrom(funix, line, MAXLINE, 0,
306 (struct sockaddr *) &fromunix, &len);
307 if (i > 0) {
308 line[i] = '\0';
309 printline(LocalHostName, line);
310 } else if (i < 0 && errno != EINTR)
311 logerror("recvfrom unix");
312 }
313 if (readfds & inetm) {
314 len = sizeof frominet;
315 i = recvfrom(finet, line, MAXLINE, 0,
316 (struct sockaddr *) &frominet, &len);
317 if (i > 0) {
318 extern char *cvthname();
319
320 line[i] = '\0';
321 printline(cvthname(&frominet), line);
322 } else if (i < 0 && errno != EINTR)
323 logerror("recvfrom inet");
324 }
325 }
326}
327
328usage()
329{
330 (void) fprintf(stderr,
331 "usage: syslogd [-f conffile] [-m markinterval] [-p logpath]\n");
332 exit(1);
333}
334
335/*
336 * Take a raw input line, decode the message, and print the message
337 * on the appropriate log files.
338 */
339
340printline(hname, msg)
341 char *hname;
342 char *msg;
343{
344 register char *p, *q;
345 register int c;
346 char line[MAXLINE + 1];
347 int pri;
348
349 /* test for special codes */
350 pri = DEFUPRI;
351 p = msg;
352 if (*p == '<') {
353 pri = 0;
354 while (isdigit(*++p))
355 pri = 10 * pri + (*p - '0');
356 if (*p == '>')
357 ++p;
358 }
359 if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
360 pri = DEFUPRI;
361
362 /* don't allow users to log kernel messages */
363 if (LOG_FAC(pri) == LOG_KERN)
364 pri = LOG_MAKEPRI(LOG_USER, LOG_PRI(pri));
365
366 q = line;
367
368 while ((c = *p++ & 0177) != '\0' &&
369 q < &line[sizeof(line) - 1])
370 if (iscntrl(c))
371 if (c == '\n')
372 *q++ = ' ';
373 else if (c == '\t')
374 *q++ = '\t';
375 else {
376 *q++ = '^';
377 *q++ = c ^ 0100;
378 }
379 else
380 *q++ = c;
381 *q = '\0';
382
383 logmsg(pri, line, hname, 0);
384}
385
386/*
387 * Take a raw input line from /dev/klog, split and format similar to syslog().
388 */
389
390printsys(msg)
391 char *msg;
392{
393 register char *p, *q;
394 register int c;
395 char line[MAXLINE + 1];
396 int pri, flags;
397 char *lp;
398
399 (void) strcpy(line, "vmunix: ");
400 lp = line + strlen(line);
401 for (p = msg; *p != '\0'; ) {
402 flags = SYNC_FILE | ADDDATE; /* fsync file after write */
403 pri = DEFSPRI;
404 if (*p == '<') {
405 pri = 0;
406 while (isdigit(*++p))
407 pri = 10 * pri + (*p - '0');
408 if (*p == '>')
409 ++p;
410 } else {
411 /* kernel printf's come out on console */
412 flags |= IGN_CONS;
413 }
414 if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
415 pri = DEFSPRI;
416 q = lp;
417 while (*p != '\0' && (c = *p++) != '\n' &&
418 q < &line[MAXLINE])
419 *q++ = c;
420 *q = '\0';
421 logmsg(pri, line, LocalHostName, flags);
422 }
423}
424
425time_t now;
426
427/*
428 * Log a message to the appropriate log files, users, etc. based on
429 * the priority.
430 */
431
432logmsg(pri, msg, from, flags)
433 int pri;
434 char *msg, *from;
435 int flags;
436{
437 register struct filed *f;
438 int fac, prilev;
439 int omask, msglen;
440 char *timestamp;
441 time_t time();
442
443 dprintf("logmsg: pri %o, flags %x, from %s, msg %s\n",
444 pri, flags, from, msg);
445
446 omask = sigblock(sigmask(SIGHUP)|sigmask(SIGALRM));
447
448 /*
449 * Check to see if msg looks non-standard.
450 */
451 msglen = strlen(msg);
452 if (msglen < 16 || msg[3] != ' ' || msg[6] != ' ' ||
453 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ')
454 flags |= ADDDATE;
455
456 (void) time(&now);
457 if (flags & ADDDATE)
458 timestamp = ctime(&now) + 4;
459 else {
460 timestamp = msg;
461 msg += 16;
462 msglen -= 16;
463 }
464
465 /* extract facility and priority level */
466 if (flags & MARK)
467 fac = LOG_NFACILITIES;
468 else
469 fac = LOG_FAC(pri);
470 prilev = LOG_PRI(pri);
471
472 /* log the message to the particular outputs */
473 if (!Initialized) {
474 f = &consfile;
475 f->f_file = open(ctty, O_WRONLY, 0);
476
477 if (f->f_file >= 0) {
478 fprintlog(f, flags, msg);
479 (void) close(f->f_file);
480 }
481 (void) sigsetmask(omask);
482 return;
483 }
484 for (f = Files; f; f = f->f_next) {
485 /* skip messages that are incorrect priority */
486 if (f->f_pmask[fac] < prilev ||
487 f->f_pmask[fac] == INTERNAL_NOPRI)
488 continue;
489
490 if (f->f_type == F_CONSOLE && (flags & IGN_CONS))
491 continue;
492
493 /* don't output marks to recently written files */
494 if ((flags & MARK) && (now - f->f_time) < MarkInterval / 2)
495 continue;
496
497 /*
498 * suppress duplicate lines to this file
499 */
500 if ((flags & MARK) == 0 && msglen == f->f_prevlen &&
501 !strcmp(msg, f->f_prevline) &&
502 !strcmp(from, f->f_prevhost)) {
503 (void) strncpy(f->f_lasttime, timestamp, 15);
504 f->f_prevcount++;
505 dprintf("msg repeated %d times, %ld sec of %d\n",
506 f->f_prevcount, now - f->f_time,
507 repeatinterval[f->f_repeatcount]);
508 /*
509 * If domark would have logged this by now,
510 * flush it now (so we don't hold isolated messages),
511 * but back off so we'll flush less often
512 * in the future.
513 */
514 if (now > REPEATTIME(f)) {
515 fprintlog(f, flags, (char *)NULL);
516 BACKOFF(f);
517 }
518 } else {
519 /* new line, save it */
520 if (f->f_prevcount)
521 fprintlog(f, 0, (char *)NULL);
522 f->f_repeatcount = 0;
523 (void) strncpy(f->f_lasttime, timestamp, 15);
524 (void) strncpy(f->f_prevhost, from,
525 sizeof(f->f_prevhost));
526 if (msglen < MAXSVLINE) {
527 f->f_prevlen = msglen;
528 f->f_prevpri = pri;
529 (void) strcpy(f->f_prevline, msg);
530 fprintlog(f, flags, (char *)NULL);
531 } else {
532 f->f_prevline[0] = 0;
533 f->f_prevlen = 0;
534 fprintlog(f, flags, msg);
535 }
536 }
537 }
538 (void) sigsetmask(omask);
539}
540
541fprintlog(f, flags, msg)
542 register struct filed *f;
543 int flags;
544 char *msg;
545{
546 struct iovec iov[6];
547 register struct iovec *v;
548 register int l;
549 char line[MAXLINE + 1], repbuf[80], greetings[200];
550
551 v = iov;
552 if (f->f_type == F_WALL) {
553 v->iov_base = greetings;
554 v->iov_len = sprintf(greetings,
555 "\r\n\7Message from syslogd@%s at %.24s ...\r\n",
556 f->f_prevhost, ctime(&now));
557 v++;
558 v->iov_base = "";
559 v->iov_len = 0;
560 v++;
561 } else {
562 v->iov_base = f->f_lasttime;
563 v->iov_len = 15;
564 v++;
565 v->iov_base = " ";
566 v->iov_len = 1;
567 v++;
568 }
569 v->iov_base = f->f_prevhost;
570 v->iov_len = strlen(v->iov_base);
571 v++;
572 v->iov_base = " ";
573 v->iov_len = 1;
574 v++;
575
576 if (msg) {
577 v->iov_base = msg;
578 v->iov_len = strlen(msg);
579 } else if (f->f_prevcount > 1) {
580 v->iov_base = repbuf;
581 v->iov_len = sprintf(repbuf, "last message repeated %d times",
582 f->f_prevcount);
583 } else {
584 v->iov_base = f->f_prevline;
585 v->iov_len = f->f_prevlen;
586 }
587 v++;
588
589 dprintf("Logging to %s", TypeNames[f->f_type]);
590 f->f_time = now;
591
592 switch (f->f_type) {
593 case F_UNUSED:
594 dprintf("\n");
595 break;
596
597 case F_FORW:
598 dprintf(" %s\n", f->f_un.f_forw.f_hname);
599 l = sprintf(line, "<%d>%.15s %s", f->f_prevpri,
600 iov[0].iov_base, iov[4].iov_base);
601 if (l > MAXLINE)
602 l = MAXLINE;
603 if (sendto(finet, line, l, 0,
604 (struct sockaddr *)&f->f_un.f_forw.f_addr,
605 sizeof f->f_un.f_forw.f_addr) != l) {
606 int e = errno;
607 (void) close(f->f_file);
608 f->f_type = F_UNUSED;
609 errno = e;
610 logerror("sendto");
611 }
612 break;
613
614 case F_CONSOLE:
615 if (flags & IGN_CONS) {
616 dprintf(" (ignored)\n");
617 break;
618 }
619 /* FALLTHROUGH */
620
621 case F_TTY:
622 case F_FILE:
623 dprintf(" %s\n", f->f_un.f_fname);
624 if (f->f_type != F_FILE) {
625 v->iov_base = "\r\n";
626 v->iov_len = 2;
627 } else {
628 v->iov_base = "\n";
629 v->iov_len = 1;
630 }
631 again:
632 if (writev(f->f_file, iov, 6) < 0) {
633 int e = errno;
634 (void) close(f->f_file);
635 /*
636 * Check for errors on TTY's due to loss of tty
637 */
638 if ((e == EIO || e == EBADF) && f->f_type != F_FILE) {
639 f->f_file = open(f->f_un.f_fname,
640 O_WRONLY|O_APPEND, 0);
641 if (f->f_file < 0) {
642 f->f_type = F_UNUSED;
643 logerror(f->f_un.f_fname);
644 } else
645 goto again;
646 } else {
647 f->f_type = F_UNUSED;
648 errno = e;
649 logerror(f->f_un.f_fname);
650 }
651 } else if (flags & SYNC_FILE)
652 (void) fsync(f->f_file);
653 break;
654
655 case F_USERS:
656 case F_WALL:
657 dprintf("\n");
658 v->iov_base = "\r\n";
659 v->iov_len = 2;
660 wallmsg(f, iov);
661 break;
662 }
663 f->f_prevcount = 0;
664}
665
666/*
667 * WALLMSG -- Write a message to the world at large
668 *
669 * Write the specified message to either the entire
670 * world, or a list of approved users.
671 */
672
673wallmsg(f, iov)
674 register struct filed *f;
675 struct iovec *iov;
676{
677 static int reenter; /* avoid calling ourselves */
678 register FILE *uf;
679 register int i;
680 struct utmp ut;
681 char *p, *ttymsg();
682
683 if (reenter++)
684 return;
685 if ((uf = fopen(_PATH_UTMP, "r")) == NULL) {
686 logerror(_PATH_UTMP);
687 reenter = 0;
688 return;
689 }
690 /* NOSTRICT */
691 while (fread((char *) &ut, sizeof ut, 1, uf) == 1) {
692 if (ut.ut_name[0] == '\0')
693 continue;
694 if (f->f_type == F_WALL) {
695 if (p = ttymsg(iov, 6, ut.ut_line, 1, 60*5)) {
696 errno = 0; /* already in msg */
697 logerror(p);
698 }
699 continue;
700 }
701 /* should we send the message to this user? */
702 for (i = 0; i < MAXUNAMES; i++) {
703 if (!f->f_un.f_uname[i][0])
704 break;
705 if (!strncmp(f->f_un.f_uname[i], ut.ut_name,
706 UT_NAMESIZE)) {
707 if (p = ttymsg(iov, 6, ut.ut_line, 1, 60*5)) {
708 errno = 0; /* already in msg */
709 logerror(p);
710 }
711 break;
712 }
713 }
714 }
715 (void) fclose(uf);
716 reenter = 0;
717}
718
719void
720reapchild()
721{
722 union wait status;
723
724 while (wait3((int *)&status, WNOHANG, (struct rusage *) NULL) > 0)
725 ;
726}
727
728/*
729 * Return a printable representation of a host address.
730 */
731char *
732cvthname(f)
733 struct sockaddr_in *f;
734{
735 struct hostent *hp;
736 register char *p;
737 extern char *inet_ntoa();
738
739 dprintf("cvthname(%s)\n", inet_ntoa(f->sin_addr));
740
741 if (f->sin_family != AF_INET) {
742 dprintf("Malformed from address\n");
743 return ("???");
744 }
745 hp = gethostbyaddr((char *)&f->sin_addr,
746 sizeof(struct in_addr), f->sin_family);
747 if (hp == 0) {
748 dprintf("Host name for your address (%s) unknown\n",
749 inet_ntoa(f->sin_addr));
750 return (inet_ntoa(f->sin_addr));
751 }
752 if ((p = index(hp->h_name, '.')) && strcmp(p + 1, LocalDomain) == 0)
753 *p = '\0';
754 return (hp->h_name);
755}
756
757void
758domark()
759{
760 register struct filed *f;
761 time_t time();
762
763 now = time((time_t *)NULL);
764 MarkSeq += TIMERINTVL;
765 if (MarkSeq >= MarkInterval) {
766 logmsg(LOG_INFO, "-- MARK --", LocalHostName, ADDDATE|MARK);
767 MarkSeq = 0;
768 }
769
770 for (f = Files; f; f = f->f_next) {
771 if (f->f_prevcount && now >= REPEATTIME(f)) {
772 dprintf("flush %s: repeated %d times, %d sec.\n",
773 TypeNames[f->f_type], f->f_prevcount,
774 repeatinterval[f->f_repeatcount]);
775 fprintlog(f, 0, (char *)NULL);
776 BACKOFF(f);
777 }
778 }
779 (void) alarm(TIMERINTVL);
780}
781
782/*
783 * Print syslogd errors some place.
784 */
785logerror(type)
786 char *type;
787{
788 char buf[100], *strerror();
789
790 if (errno)
791 (void) sprintf(buf, "syslogd: %s: %s", type, strerror(errno));
792 else
793 (void) sprintf(buf, "syslogd: %s", type);
794 errno = 0;
795 dprintf("%s\n", buf);
796 logmsg(LOG_SYSLOG|LOG_ERR, buf, LocalHostName, ADDDATE);
797}
798
799void
800die(sig)
801{
802 register struct filed *f;
803 char buf[100];
804
805 for (f = Files; f != NULL; f = f->f_next) {
806 /* flush any pending output */
807 if (f->f_prevcount)
808 fprintlog(f, 0, (char *)NULL);
809 }
810 if (sig) {
811 dprintf("syslogd: exiting on signal %d\n", sig);
812 (void) sprintf(buf, "exiting on signal %d", sig);
813 errno = 0;
814 logerror(buf);
815 }
816 (void) unlink(LogName);
817 exit(0);
818}
819
820/*
821 * INIT -- Initialize syslogd from configuration table
822 */
823
824void
825init()
826{
827 register int i;
828 register FILE *cf;
829 register struct filed *f, *next, **nextp;
830 register char *p;
831 char cline[BUFSIZ];
832
833 dprintf("init\n");
834
835 /*
836 * Close all open log files.
837 */
838 Initialized = 0;
839 for (f = Files; f != NULL; f = next) {
840 /* flush any pending output */
841 if (f->f_prevcount)
842 fprintlog(f, 0, (char *)NULL);
843
844 switch (f->f_type) {
845 case F_FILE:
846 case F_TTY:
847 case F_CONSOLE:
848 case F_FORW:
849 (void) close(f->f_file);
850 break;
851 }
852 next = f->f_next;
853 free((char *) f);
854 }
855 Files = NULL;
856 nextp = &Files;
857
858 /* open the configuration file */
859 if ((cf = fopen(ConfFile, "r")) == NULL) {
860 dprintf("cannot open %s\n", ConfFile);
861 *nextp = (struct filed *)calloc(1, sizeof(*f));
862 cfline("*.ERR\t/dev/console", *nextp);
863 (*nextp)->f_next = (struct filed *)calloc(1, sizeof(*f));
864 cfline("*.PANIC\t*", (*nextp)->f_next);
865 Initialized = 1;
866 return;
867 }
868
869 /*
870 * Foreach line in the conf table, open that file.
871 */
872 f = NULL;
873 while (fgets(cline, sizeof cline, cf) != NULL) {
874 /*
875 * check for end-of-section, comments, strip off trailing
876 * spaces and newline character.
877 */
878 for (p = cline; isspace(*p); ++p);
879 if (*p == NULL || *p == '#')
880 continue;
881 for (p = index(cline, '\0'); isspace(*--p););
882 *++p = '\0';
883 f = (struct filed *)calloc(1, sizeof(*f));
884 *nextp = f;
885 nextp = &f->f_next;
886 cfline(cline, f);
887 }
888
889 /* close the configuration file */
890 (void) fclose(cf);
891
892 Initialized = 1;
893
894 if (Debug) {
895 for (f = Files; f; f = f->f_next) {
896 for (i = 0; i <= LOG_NFACILITIES; i++)
897 if (f->f_pmask[i] == INTERNAL_NOPRI)
898 printf("X ");
899 else
900 printf("%d ", f->f_pmask[i]);
901 printf("%s: ", TypeNames[f->f_type]);
902 switch (f->f_type) {
903 case F_FILE:
904 case F_TTY:
905 case F_CONSOLE:
906 printf("%s", f->f_un.f_fname);
907 break;
908
909 case F_FORW:
910 printf("%s", f->f_un.f_forw.f_hname);
911 break;
912
913 case F_USERS:
914 for (i = 0; i < MAXUNAMES && *f->f_un.f_uname[i]; i++)
915 printf("%s, ", f->f_un.f_uname[i]);
916 break;
917 }
918 printf("\n");
919 }
920 }
921
922 logmsg(LOG_SYSLOG|LOG_INFO, "syslogd: restart", LocalHostName, ADDDATE);
923 dprintf("syslogd: restarted\n");
924}
925
926/*
927 * Crack a configuration file line
928 */
929
930cfline(line, f)
931 char *line;
932 register struct filed *f;
933{
934 register char *p;
935 register char *q;
936 register int i;
937 char *bp;
938 int pri;
939 struct hostent *hp;
940 char buf[MAXLINE], ebuf[100];
941
942 dprintf("cfline(%s)\n", line);
943
944 errno = 0; /* keep strerror() stuff out of logerror messages */
945
946 /* clear out file entry */
947 bzero((char *) f, sizeof *f);
948 for (i = 0; i <= LOG_NFACILITIES; i++)
949 f->f_pmask[i] = INTERNAL_NOPRI;
950
951 /* scan through the list of selectors */
952 for (p = line; *p && *p != '\t';) {
953
954 /* find the end of this facility name list */
955 for (q = p; *q && *q != '\t' && *q++ != '.'; )
956 continue;
957
958 /* collect priority name */
959 for (bp = buf; *q && !index("\t,;", *q); )
960 *bp++ = *q++;
961 *bp = '\0';
962
963 /* skip cruft */
964 while (index(", ;", *q))
965 q++;
966
967 /* decode priority name */
968 if (*buf == '*')
969 pri = LOG_PRIMASK + 1;
970 else {
971 pri = decode(buf, prioritynames);
972 if (pri < 0) {
973 (void) sprintf(ebuf,
974 "unknown priority name \"%s\"", buf);
975 logerror(ebuf);
976 return;
977 }
978 }
979
980 /* scan facilities */
981 while (*p && !index("\t.;", *p)) {
982 for (bp = buf; *p && !index("\t,;.", *p); )
983 *bp++ = *p++;
984 *bp = '\0';
985 if (*buf == '*')
986 for (i = 0; i < LOG_NFACILITIES; i++)
987 f->f_pmask[i] = pri;
988 else {
989 i = decode(buf, facilitynames);
990 if (i < 0) {
991 (void) sprintf(ebuf,
992 "unknown facility name \"%s\"",
993 buf);
994 logerror(ebuf);
995 return;
996 }
997 f->f_pmask[i >> 3] = pri;
998 }
999 while (*p == ',' || *p == ' ')
1000 p++;
1001 }
1002
1003 p = q;
1004 }
1005
1006 /* skip to action part */
1007 while (*p == '\t')
1008 p++;
1009
1010 switch (*p)
1011 {
1012 case '@':
1013 if (!InetInuse)
1014 break;
1015 (void) strcpy(f->f_un.f_forw.f_hname, ++p);
1016 hp = gethostbyname(p);
1017 if (hp == NULL) {
1018 extern int h_errno, h_nerr;
1019 extern char *h_errlist[];
1020
1021 logerror((u_int)h_errno < h_nerr ?
1022 h_errlist[h_errno] : "Unknown error");
1023 break;
1024 }
1025 bzero((char *) &f->f_un.f_forw.f_addr,
1026 sizeof f->f_un.f_forw.f_addr);
1027 f->f_un.f_forw.f_addr.sin_family = AF_INET;
1028 f->f_un.f_forw.f_addr.sin_port = LogPort;
1029 bcopy(hp->h_addr, (char *) &f->f_un.f_forw.f_addr.sin_addr, hp->h_length);
1030 f->f_type = F_FORW;
1031 break;
1032
1033 case '/':
1034 (void) strcpy(f->f_un.f_fname, p);
1035 if ((f->f_file = open(p, O_WRONLY|O_APPEND, 0)) < 0) {
1036 f->f_file = F_UNUSED;
1037 logerror(p);
1038 break;
1039 }
1040 if (isatty(f->f_file))
1041 f->f_type = F_TTY;
1042 else
1043 f->f_type = F_FILE;
1044 if (strcmp(p, ctty) == 0)
1045 f->f_type = F_CONSOLE;
1046 break;
1047
1048 case '*':
1049 f->f_type = F_WALL;
1050 break;
1051
1052 default:
1053 for (i = 0; i < MAXUNAMES && *p; i++) {
1054 for (q = p; *q && *q != ','; )
1055 q++;
1056 (void) strncpy(f->f_un.f_uname[i], p, UT_NAMESIZE);
1057 if ((q - p) > UT_NAMESIZE)
1058 f->f_un.f_uname[i][UT_NAMESIZE] = '\0';
1059 else
1060 f->f_un.f_uname[i][q - p] = '\0';
1061 while (*q == ',' || *q == ' ')
1062 q++;
1063 p = q;
1064 }
1065 f->f_type = F_USERS;
1066 break;
1067 }
1068}
1069
1070
1071/*
1072 * Decode a symbolic name to a numeric value
1073 */
1074
1075decode(name, codetab)
1076 char *name;
1077 CODE *codetab;
1078{
1079 register CODE *c;
1080 register char *p;
1081 char buf[40];
1082
1083 if (isdigit(*name))
1084 return (atoi(name));
1085
1086 (void) strcpy(buf, name);
1087 for (p = buf; *p; p++)
1088 if (isupper(*p))
1089 *p = tolower(*p);
1090 for (c = codetab; c->c_name; c++)
1091 if (!strcmp(buf, c->c_name))
1092 return (c->c_val);
1093
1094 return (-1);
1095}