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