BSD 4_3_Reno development
[unix-history] / .ref-fdb8e4aac282b72d4670aa3a26d9bba07afc7e6f / 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
ac8f453f 15static char sccsid[] = "@(#)syslogd.c 5.44 (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;
602 if (sendto(finet, line, l, 0, &f->f_un.f_forw.f_addr,
603 sizeof f->f_un.f_forw.f_addr) != l) {
604 int e = errno;
605 (void) close(f->f_file);
606 f->f_type = F_UNUSED;
607 errno = e;
608 logerror("sendto");
609 }
610 break;
611
612 case F_CONSOLE:
613 if (flags & IGN_CONS) {
614 dprintf(" (ignored)\n");
64b300b4 615 break;
92e73e63
MK
616 }
617 /* FALLTHROUGH */
e677da31 618
92e73e63
MK
619 case F_TTY:
620 case F_FILE:
621 dprintf(" %s\n", f->f_un.f_fname);
622 if (f->f_type != F_FILE) {
64b300b4
EA
623 v->iov_base = "\r\n";
624 v->iov_len = 2;
92e73e63
MK
625 } else {
626 v->iov_base = "\n";
627 v->iov_len = 1;
64b300b4 628 }
92e73e63
MK
629 again:
630 if (writev(f->f_file, iov, 6) < 0) {
631 int e = errno;
632 (void) close(f->f_file);
633 /*
604d6f83 634 * Check for errors on TTY's due to loss of tty
92e73e63 635 */
604d6f83 636 if ((e == EIO || e == EBADF) && f->f_type != F_FILE) {
e53dbb38
KB
637 f->f_file = open(f->f_un.f_fname,
638 O_WRONLY|O_APPEND, 0);
92e73e63
MK
639 if (f->f_file < 0) {
640 f->f_type = F_UNUSED;
641 logerror(f->f_un.f_fname);
43d42ac6 642 } else
92e73e63 643 goto again;
92e73e63
MK
644 } else {
645 f->f_type = F_UNUSED;
646 errno = e;
647 logerror(f->f_un.f_fname);
648 }
649 } else if (flags & SYNC_FILE)
650 (void) fsync(f->f_file);
651 break;
e677da31 652
92e73e63
MK
653 case F_USERS:
654 case F_WALL:
655 dprintf("\n");
656 v->iov_base = "\r\n";
657 v->iov_len = 2;
658 wallmsg(f, iov);
659 break;
660 }
661 f->f_prevcount = 0;
e677da31
RC
662}
663
664/*
665 * WALLMSG -- Write a message to the world at large
666 *
667 * Write the specified message to either the entire
668 * world, or a list of approved users.
669 */
670
64b300b4
EA
671wallmsg(f, iov)
672 register struct filed *f;
673 struct iovec *iov;
e677da31 674{
e53dbb38
KB
675 static int reenter; /* avoid calling ourselves */
676 register FILE *uf;
e677da31 677 register int i;
e677da31 678 struct utmp ut;
e53dbb38 679 char *p, *ttymsg();
64b300b4
EA
680
681 if (reenter++)
682 return;
61b108ae 683 if ((uf = fopen(_PATH_UTMP, "r")) == NULL) {
3cdeaa19 684 logerror(_PATH_UTMP);
64b300b4 685 reenter = 0;
e677da31 686 return;
ada83039 687 }
e53dbb38
KB
688 /* NOSTRICT */
689 while (fread((char *) &ut, sizeof ut, 1, uf) == 1) {
690 if (ut.ut_name[0] == '\0')
691 continue;
692 if (f->f_type == F_WALL) {
233e81a0
KB
693 if (p = ttymsg(iov, 6, ut.ut_line, 1)) {
694 errno = 0; /* already in msg */
e53dbb38 695 logerror(p);
233e81a0 696 }
e53dbb38
KB
697 continue;
698 }
699 /* should we send the message to this user? */
700 for (i = 0; i < MAXUNAMES; i++) {
701 if (!f->f_un.f_uname[i][0])
702 break;
703 if (!strncmp(f->f_un.f_uname[i], ut.ut_name,
704 UT_NAMESIZE)) {
233e81a0
KB
705 if (p = ttymsg(iov, 6, ut.ut_line, 1)) {
706 errno = 0; /* already in msg */
e53dbb38 707 logerror(p);
233e81a0 708 }
e53dbb38 709 break;
fcd484c8 710 }
e677da31 711 }
e677da31 712 }
84555b38 713 (void) fclose(uf);
64b300b4 714 reenter = 0;
84555b38
RC
715}
716
981fd33c 717void
84555b38
RC
718reapchild()
719{
720 union wait status;
721
64b300b4 722 while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
84555b38 723 ;
e677da31
RC
724}
725
726/*
64b300b4 727 * Return a printable representation of a host address.
e677da31 728 */
64b300b4
EA
729char *
730cvthname(f)
e677da31
RC
731 struct sockaddr_in *f;
732{
733 struct hostent *hp;
ad58e75e 734 register char *p;
e677da31
RC
735 extern char *inet_ntoa();
736
64b300b4 737 dprintf("cvthname(%s)\n", inet_ntoa(f->sin_addr));
e677da31
RC
738
739 if (f->sin_family != AF_INET) {
740 dprintf("Malformed from address\n");
64b300b4 741 return ("???");
e677da31
RC
742 }
743 hp = gethostbyaddr(&f->sin_addr, sizeof(struct in_addr), f->sin_family);
744 if (hp == 0) {
745 dprintf("Host name for your address (%s) unknown\n",
746 inet_ntoa(f->sin_addr));
64b300b4 747 return (inet_ntoa(f->sin_addr));
e677da31 748 }
ad58e75e
EA
749 if ((p = index(hp->h_name, '.')) && strcmp(p + 1, LocalDomain) == 0)
750 *p = '\0';
64b300b4 751 return (hp->h_name);
e677da31
RC
752}
753
981fd33c 754void
ad58e75e
EA
755domark()
756{
92e73e63 757 register struct filed *f;
e53dbb38 758 time_t time();
ad58e75e 759
e53dbb38 760 now = time((time_t *)NULL);
92e73e63
MK
761 MarkSeq += TIMERINTVL;
762 if (MarkSeq >= MarkInterval) {
c16a39e2 763 logmsg(LOG_INFO, "-- MARK --", LocalHostName, ADDDATE|MARK);
92e73e63
MK
764 MarkSeq = 0;
765 }
ad58e75e 766
92e73e63
MK
767 for (f = Files; f; f = f->f_next) {
768 if (f->f_prevcount && now >= REPEATTIME(f)) {
769 dprintf("flush %s: repeated %d times, %d sec.\n",
770 TypeNames[f->f_type], f->f_prevcount,
771 repeatinterval[f->f_repeatcount]);
35c3f83c 772 fprintlog(f, 0, (char *)NULL);
92e73e63
MK
773 BACKOFF(f);
774 }
775 }
776 (void) alarm(TIMERINTVL);
a49a6aba
RC
777}
778
e677da31
RC
779/*
780 * Print syslogd errors some place.
781 */
782logerror(type)
783 char *type;
784{
e185ee1c 785 char buf[100], *strerror();
e677da31 786
e185ee1c
KB
787 if (errno)
788 (void) sprintf(buf, "syslogd: %s: %s", type, strerror(errno));
e677da31 789 else
e185ee1c 790 (void) sprintf(buf, "syslogd: %s", type);
e677da31 791 errno = 0;
ada83039 792 dprintf("%s\n", buf);
ad58e75e 793 logmsg(LOG_SYSLOG|LOG_ERR, buf, LocalHostName, ADDDATE);
e677da31
RC
794}
795
981fd33c 796void
64b300b4 797die(sig)
e677da31 798{
92e73e63 799 register struct filed *f;
64b300b4 800 char buf[100];
ada83039 801
92e73e63
MK
802 for (f = Files; f != NULL; f = f->f_next) {
803 /* flush any pending output */
804 if (f->f_prevcount)
35c3f83c 805 fprintlog(f, 0, (char *)NULL);
92e73e63 806 }
a075f884 807 if (sig) {
92e73e63
MK
808 dprintf("syslogd: exiting on signal %d\n", sig);
809 (void) sprintf(buf, "exiting on signal %d", sig);
d34bf11a 810 errno = 0;
a075f884
MK
811 logerror(buf);
812 }
64b300b4 813 (void) unlink(LogName);
e677da31
RC
814 exit(0);
815}
a49a6aba
RC
816
817/*
64b300b4 818 * INIT -- Initialize syslogd from configuration table
a49a6aba 819 */
64b300b4 820
e53dbb38 821void
64b300b4 822init()
a49a6aba 823{
a49a6aba 824 register int i;
64b300b4 825 register FILE *cf;
92e73e63 826 register struct filed *f, *next, **nextp;
64b300b4
EA
827 register char *p;
828 char cline[BUFSIZ];
a49a6aba 829
64b300b4
EA
830 dprintf("init\n");
831
64b300b4
EA
832 /*
833 * Close all open log files.
834 */
d34bf11a 835 Initialized = 0;
92e73e63
MK
836 for (f = Files; f != NULL; f = next) {
837 /* flush any pending output */
838 if (f->f_prevcount)
35c3f83c 839 fprintlog(f, 0, (char *)NULL);
92e73e63 840
bb112360
EA
841 switch (f->f_type) {
842 case F_FILE:
843 case F_TTY:
bb112360 844 case F_CONSOLE:
2efc48b0 845 case F_FORW:
64b300b4 846 (void) close(f->f_file);
bb112360
EA
847 break;
848 }
92e73e63
MK
849 next = f->f_next;
850 free((char *) f);
64b300b4 851 }
92e73e63
MK
852 Files = NULL;
853 nextp = &Files;
64b300b4
EA
854
855 /* open the configuration file */
856 if ((cf = fopen(ConfFile, "r")) == NULL) {
857 dprintf("cannot open %s\n", ConfFile);
92e73e63
MK
858 *nextp = (struct filed *)calloc(1, sizeof(*f));
859 cfline("*.ERR\t/dev/console", *nextp);
860 (*nextp)->f_next = (struct filed *)calloc(1, sizeof(*f));
861 cfline("*.PANIC\t*", (*nextp)->f_next);
862 Initialized = 1;
64b300b4
EA
863 return;
864 }
865
866 /*
867 * Foreach line in the conf table, open that file.
868 */
92e73e63
MK
869 f = NULL;
870 while (fgets(cline, sizeof cline, cf) != NULL) {
e15840af
KB
871 /*
872 * check for end-of-section, comments, strip off trailing
873 * spaces and newline character.
874 */
875 for (p = cline; isspace(*p); ++p);
876 if (*p == NULL || *p == '#')
a49a6aba 877 continue;
e15840af
KB
878 for (p = index(cline, '\0'); isspace(*--p););
879 *++p = '\0';
92e73e63
MK
880 f = (struct filed *)calloc(1, sizeof(*f));
881 *nextp = f;
882 nextp = &f->f_next;
92e73e63 883 cfline(cline, f);
64b300b4
EA
884 }
885
886 /* close the configuration file */
887 (void) fclose(cf);
888
889 Initialized = 1;
890
891 if (Debug) {
92e73e63 892 for (f = Files; f; f = f->f_next) {
c16a39e2 893 for (i = 0; i <= LOG_NFACILITIES; i++)
233e81a0 894 if (f->f_pmask[i] == INTERNAL_NOPRI)
64b300b4
EA
895 printf("X ");
896 else
897 printf("%d ", f->f_pmask[i]);
898 printf("%s: ", TypeNames[f->f_type]);
899 switch (f->f_type) {
900 case F_FILE:
901 case F_TTY:
902 case F_CONSOLE:
903 printf("%s", f->f_un.f_fname);
904 break;
905
906 case F_FORW:
907 printf("%s", f->f_un.f_forw.f_hname);
908 break;
909
910 case F_USERS:
911 for (i = 0; i < MAXUNAMES && *f->f_un.f_uname[i]; i++)
912 printf("%s, ", f->f_un.f_uname[i]);
913 break;
914 }
915 printf("\n");
916 }
917 }
918
ad58e75e 919 logmsg(LOG_SYSLOG|LOG_INFO, "syslogd: restart", LocalHostName, ADDDATE);
64b300b4
EA
920 dprintf("syslogd: restarted\n");
921}
922
923/*
924 * Crack a configuration file line
925 */
926
64b300b4
EA
927cfline(line, f)
928 char *line;
929 register struct filed *f;
930{
931 register char *p;
932 register char *q;
933 register int i;
934 char *bp;
935 int pri;
936 struct hostent *hp;
50761dd0 937 char buf[MAXLINE], ebuf[100];
64b300b4
EA
938
939 dprintf("cfline(%s)\n", line);
940
e185ee1c 941 errno = 0; /* keep strerror() stuff out of logerror messages */
d34bf11a 942
64b300b4
EA
943 /* clear out file entry */
944 bzero((char *) f, sizeof *f);
c16a39e2 945 for (i = 0; i <= LOG_NFACILITIES; i++)
233e81a0 946 f->f_pmask[i] = INTERNAL_NOPRI;
64b300b4
EA
947
948 /* scan through the list of selectors */
949 for (p = line; *p && *p != '\t';) {
950
951 /* find the end of this facility name list */
952 for (q = p; *q && *q != '\t' && *q++ != '.'; )
a49a6aba 953 continue;
64b300b4
EA
954
955 /* collect priority name */
956 for (bp = buf; *q && !index("\t,;", *q); )
957 *bp++ = *q++;
958 *bp = '\0';
959
960 /* skip cruft */
961 while (index(", ;", *q))
962 q++;
963
964 /* decode priority name */
50761dd0
KB
965 if (*buf == '*')
966 pri = LOG_PRIMASK + 1;
967 else {
968 pri = decode(buf, prioritynames);
969 if (pri < 0) {
970 (void) sprintf(ebuf,
971 "unknown priority name \"%s\"", buf);
972 logerror(ebuf);
973 return;
974 }
64b300b4
EA
975 }
976
977 /* scan facilities */
978 while (*p && !index("\t.;", *p)) {
64b300b4
EA
979 for (bp = buf; *p && !index("\t,;.", *p); )
980 *bp++ = *p++;
981 *bp = '\0';
982 if (*buf == '*')
983 for (i = 0; i < LOG_NFACILITIES; i++)
984 f->f_pmask[i] = pri;
985 else {
233e81a0 986 i = decode(buf, facilitynames);
64b300b4 987 if (i < 0) {
50761dd0 988 (void) sprintf(ebuf,
e53dbb38
KB
989 "unknown facility name \"%s\"",
990 buf);
50761dd0 991 logerror(ebuf);
64b300b4
EA
992 return;
993 }
994 f->f_pmask[i >> 3] = pri;
995 }
996 while (*p == ',' || *p == ' ')
997 p++;
998 }
999
1000 p = q;
1001 }
1002
1003 /* skip to action part */
1004 while (*p == '\t')
1005 p++;
1006
1007 switch (*p)
1008 {
1009 case '@':
1010 if (!InetInuse)
1011 break;
1012 (void) strcpy(f->f_un.f_forw.f_hname, ++p);
1013 hp = gethostbyname(p);
1014 if (hp == NULL) {
5b39dbe7
KB
1015 extern int h_errno, h_nerr;
1016 extern char **h_errlist;
64b300b4 1017
5b39dbe7
KB
1018 logerror((u_int)h_errno < h_nerr ?
1019 h_errlist[h_errno] : "Unknown error");
64b300b4
EA
1020 break;
1021 }
1022 bzero((char *) &f->f_un.f_forw.f_addr,
1023 sizeof f->f_un.f_forw.f_addr);
1024 f->f_un.f_forw.f_addr.sin_family = AF_INET;
1025 f->f_un.f_forw.f_addr.sin_port = LogPort;
1026 bcopy(hp->h_addr, (char *) &f->f_un.f_forw.f_addr.sin_addr, hp->h_length);
64b300b4
EA
1027 f->f_type = F_FORW;
1028 break;
1029
1030 case '/':
1031 (void) strcpy(f->f_un.f_fname, p);
e53dbb38 1032 if ((f->f_file = open(p, O_WRONLY|O_APPEND, 0)) < 0) {
e15840af 1033 f->f_file = F_UNUSED;
64b300b4
EA
1034 logerror(p);
1035 break;
1036 }
43d42ac6 1037 if (isatty(f->f_file))
64b300b4 1038 f->f_type = F_TTY;
64b300b4
EA
1039 else
1040 f->f_type = F_FILE;
1041 if (strcmp(p, ctty) == 0)
1042 f->f_type = F_CONSOLE;
1043 break;
1044
1045 case '*':
1046 f->f_type = F_WALL;
1047 break;
1048
1049 default:
1050 for (i = 0; i < MAXUNAMES && *p; i++) {
1051 for (q = p; *q && *q != ','; )
1052 q++;
e185ee1c
KB
1053 (void) strncpy(f->f_un.f_uname[i], p, UT_NAMESIZE);
1054 if ((q - p) > UT_NAMESIZE)
1055 f->f_un.f_uname[i][UT_NAMESIZE] = '\0';
64b300b4
EA
1056 else
1057 f->f_un.f_uname[i][q - p] = '\0';
1058 while (*q == ',' || *q == ' ')
1059 q++;
1060 p = q;
1061 }
1062 f->f_type = F_USERS;
1063 break;
a49a6aba 1064 }
64b300b4
EA
1065}
1066
1067
1068/*
1069 * Decode a symbolic name to a numeric value
1070 */
1071
1072decode(name, codetab)
1073 char *name;
233e81a0 1074 CODE *codetab;
64b300b4 1075{
233e81a0 1076 register CODE *c;
64b300b4
EA
1077 register char *p;
1078 char buf[40];
1079
1080 if (isdigit(*name))
1081 return (atoi(name));
1082
1083 (void) strcpy(buf, name);
1084 for (p = buf; *p; p++)
1085 if (isupper(*p))
1086 *p = tolower(*p);
1087 for (c = codetab; c->c_name; c++)
1088 if (!strcmp(buf, c->c_name))
1089 return (c->c_val);
a49a6aba 1090
64b300b4 1091 return (-1);
a49a6aba 1092}