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