ANSI C
[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
a3d08a4f 15static char sccsid[] = "@(#)syslogd.c 5.48 (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 245 }
a3d08a4f 246 bzero((char *)&sin, sizeof(sin));
e677da31 247 sin.sin_family = AF_INET;
64b300b4 248 sin.sin_port = LogPort = sp->s_port;
e53dbb38 249 if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
e677da31 250 logerror("bind");
64b300b4
EA
251 if (!Debug)
252 die(0);
253 } else {
254 inetm = FDMASK(finet);
255 InetInuse = 1;
e677da31 256 }
e677da31 257 }
61b108ae 258 if ((fklog = open(_PATH_KLOG, O_RDONLY, 0)) >= 0)
64b300b4 259 klogm = FDMASK(fklog);
84555b38 260 else {
61b108ae 261 dprintf("can't open %s (%d)\n", _PATH_KLOG, errno);
a49a6aba
RC
262 klogm = 0;
263 }
e677da31
RC
264
265 /* tuck my process id away */
64b300b4 266 fp = fopen(PidFile, "w");
e677da31
RC
267 if (fp != NULL) {
268 fprintf(fp, "%d\n", getpid());
64b300b4 269 (void) fclose(fp);
e677da31
RC
270 }
271
272 dprintf("off & running....\n");
273
e677da31 274 init();
64b300b4 275 (void) signal(SIGHUP, init);
454c883a 276
e677da31 277 for (;;) {
64b300b4 278 int nfds, readfds = FDMASK(funix) | inetm | klogm;
e677da31 279
64b300b4 280 errno = 0;
ff8002b1 281 dprintf("readfds = %#x\n", readfds);
64b300b4 282 nfds = select(20, (fd_set *) &readfds, (fd_set *) NULL,
e53dbb38 283 (fd_set *) NULL, (struct timeval *) NULL);
e677da31
RC
284 if (nfds == 0)
285 continue;
286 if (nfds < 0) {
64b300b4
EA
287 if (errno != EINTR)
288 logerror("select");
e677da31
RC
289 continue;
290 }
92e73e63 291 dprintf("got a message (%d, %#x)\n", nfds, readfds);
a49a6aba 292 if (readfds & klogm) {
84555b38 293 i = read(fklog, line, sizeof(line) - 1);
8a7154b1
RC
294 if (i > 0) {
295 line[i] = '\0';
296 printsys(line);
a49a6aba 297 } else if (i < 0 && errno != EINTR) {
84555b38
RC
298 logerror("klog");
299 fklog = -1;
a49a6aba
RC
300 klogm = 0;
301 }
8a7154b1 302 }
64b300b4 303 if (readfds & FDMASK(funix)) {
e677da31 304 len = sizeof fromunix;
64b300b4 305 i = recvfrom(funix, line, MAXLINE, 0,
e53dbb38 306 (struct sockaddr *) &fromunix, &len);
a24c481a
RC
307 if (i > 0) {
308 line[i] = '\0';
64b300b4 309 printline(LocalHostName, line);
a24c481a 310 } else if (i < 0 && errno != EINTR)
64b300b4 311 logerror("recvfrom unix");
a24c481a 312 }
84555b38 313 if (readfds & inetm) {
e677da31 314 len = sizeof frominet;
e53dbb38
KB
315 i = recvfrom(finet, line, MAXLINE, 0,
316 (struct sockaddr *) &frominet, &len);
64b300b4
EA
317 if (i > 0) {
318 extern char *cvthname();
319
a24c481a 320 line[i] = '\0';
64b300b4 321 printline(cvthname(&frominet), line);
a24c481a 322 } else if (i < 0 && errno != EINTR)
64b300b4 323 logerror("recvfrom inet");
a24c481a 324 }
e677da31
RC
325 }
326}
327
a49a6aba
RC
328usage()
329{
e185ee1c 330 (void) fprintf(stderr,
ac8f453f 331 "usage: syslogd [-f conffile] [-m markinterval] [-p logpath]\n");
a49a6aba
RC
332 exit(1);
333}
334
e677da31
RC
335/*
336 * Take a raw input line, decode the message, and print the message
337 * on the appropriate log files.
338 */
339
64b300b4
EA
340printline(hname, msg)
341 char *hname;
e677da31
RC
342 char *msg;
343{
344 register char *p, *q;
345 register int c;
63cb489e 346 char line[MAXLINE + 1];
e677da31
RC
347 int pri;
348
349 /* test for special codes */
a49a6aba 350 pri = DEFUPRI;
e677da31 351 p = msg;
a49a6aba
RC
352 if (*p == '<') {
353 pri = 0;
354 while (isdigit(*++p))
355 pri = 10 * pri + (*p - '0');
356 if (*p == '>')
357 ++p;
e677da31 358 }
35c3f83c
MK
359 if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
360 pri = DEFUPRI;
e677da31 361
64b300b4 362 /* don't allow users to log kernel messages */
35c3f83c
MK
363 if (LOG_FAC(pri) == LOG_KERN)
364 pri = LOG_MAKEPRI(LOG_USER, LOG_PRI(pri));
64b300b4 365
e677da31 366 q = line;
64b300b4 367
953d8fe3
KB
368 while ((c = *p++ & 0177) != '\0' &&
369 q < &line[sizeof(line) - 1])
0d52e368
KB
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
e677da31 380 *q++ = c;
e677da31
RC
381 *q = '\0';
382
64b300b4 383 logmsg(pri, line, hname, 0);
e677da31
RC
384}
385
63cb489e
RC
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];
8a7154b1 396 int pri, flags;
a49a6aba 397 char *lp;
63cb489e 398
e185ee1c 399 (void) strcpy(line, "vmunix: ");
a49a6aba 400 lp = line + strlen(line);
63cb489e 401 for (p = msg; *p != '\0'; ) {
92e73e63 402 flags = SYNC_FILE | ADDDATE; /* fsync file after write */
a49a6aba
RC
403 pri = DEFSPRI;
404 if (*p == '<') {
405 pri = 0;
406 while (isdigit(*++p))
407 pri = 10 * pri + (*p - '0');
408 if (*p == '>')
409 ++p;
8a7154b1
RC
410 } else {
411 /* kernel printf's come out on console */
412 flags |= IGN_CONS;
413 }
35c3f83c
MK
414 if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
415 pri = DEFSPRI;
a49a6aba
RC
416 q = lp;
417 while (*p != '\0' && (c = *p++) != '\n' &&
418 q < &line[MAXLINE])
63cb489e
RC
419 *q++ = c;
420 *q = '\0';
64b300b4 421 logmsg(pri, line, LocalHostName, flags);
63cb489e
RC
422 }
423}
424
92e73e63
MK
425time_t now;
426
e677da31
RC
427/*
428 * Log a message to the appropriate log files, users, etc. based on
429 * the priority.
430 */
431
8a7154b1 432logmsg(pri, msg, from, flags)
64b300b4
EA
433 int pri;
434 char *msg, *from;
435 int flags;
e677da31 436{
e677da31 437 register struct filed *f;
64b300b4 438 int fac, prilev;
92e73e63
MK
439 int omask, msglen;
440 char *timestamp;
e53dbb38 441 time_t time();
84555b38 442
e185ee1c
KB
443 dprintf("logmsg: pri %o, flags %x, from %s, msg %s\n",
444 pri, flags, from, msg);
64b300b4 445
ad58e75e 446 omask = sigblock(sigmask(SIGHUP)|sigmask(SIGALRM));
ada83039 447
64b300b4
EA
448 /*
449 * Check to see if msg looks non-standard.
450 */
92e73e63
MK
451 msglen = strlen(msg);
452 if (msglen < 16 || msg[3] != ' ' || msg[6] != ' ' ||
ad58e75e 453 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ')
64b300b4 454 flags |= ADDDATE;
a49a6aba 455
ad58e75e
EA
456 (void) time(&now);
457 if (flags & ADDDATE)
92e73e63
MK
458 timestamp = ctime(&now) + 4;
459 else {
460 timestamp = msg;
461 msg += 16;
462 msglen -= 16;
463 }
64b300b4
EA
464
465 /* extract facility and priority level */
c16a39e2
MK
466 if (flags & MARK)
467 fac = LOG_NFACILITIES;
35c3f83c
MK
468 else
469 fac = LOG_FAC(pri);
470 prilev = LOG_PRI(pri);
64b300b4 471
e677da31 472 /* log the message to the particular outputs */
64b300b4 473 if (!Initialized) {
92e73e63 474 f = &consfile;
e53dbb38 475 f->f_file = open(ctty, O_WRONLY, 0);
64b300b4 476
92e73e63 477 if (f->f_file >= 0) {
c49273cf 478 fprintlog(f, flags, msg);
92e73e63 479 (void) close(f->f_file);
64b300b4 480 }
7eefe9c1 481 (void) sigsetmask(omask);
64b300b4
EA
482 return;
483 }
92e73e63 484 for (f = Files; f; f = f->f_next) {
64b300b4 485 /* skip messages that are incorrect priority */
233e81a0
KB
486 if (f->f_pmask[fac] < prilev ||
487 f->f_pmask[fac] == INTERNAL_NOPRI)
a49a6aba 488 continue;
64b300b4 489
35c3f83c
MK
490 if (f->f_type == F_CONSOLE && (flags & IGN_CONS))
491 continue;
492
ad58e75e 493 /* don't output marks to recently written files */
92e73e63 494 if ((flags & MARK) && (now - f->f_time) < MarkInterval / 2)
ad58e75e
EA
495 continue;
496
92e73e63
MK
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++;
953d8fe3 505 dprintf("msg repeated %d times, %ld sec of %d\n",
92e73e63
MK
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)) {
35c3f83c 515 fprintlog(f, flags, (char *)NULL);
92e73e63 516 BACKOFF(f);
8a7154b1 517 }
92e73e63
MK
518 } else {
519 /* new line, save it */
35c3f83c
MK
520 if (f->f_prevcount)
521 fprintlog(f, 0, (char *)NULL);
522 f->f_repeatcount = 0;
92e73e63 523 (void) strncpy(f->f_lasttime, timestamp, 15);
35c3f83c
MK
524 (void) strncpy(f->f_prevhost, from,
525 sizeof(f->f_prevhost));
526 if (msglen < MAXSVLINE) {
92e73e63
MK
527 f->f_prevlen = msglen;
528 f->f_prevpri = pri;
529 (void) strcpy(f->f_prevline, msg);
35c3f83c
MK
530 fprintlog(f, flags, (char *)NULL);
531 } else {
92e73e63 532 f->f_prevline[0] = 0;
35c3f83c
MK
533 f->f_prevlen = 0;
534 fprintlog(f, flags, msg);
535 }
92e73e63
MK
536 }
537 }
538 (void) sigsetmask(omask);
539}
64b300b4 540
35c3f83c 541fprintlog(f, flags, msg)
92e73e63
MK
542 register struct filed *f;
543 int flags;
35c3f83c 544 char *msg;
92e73e63
MK
545{
546 struct iovec iov[6];
e53dbb38 547 register struct iovec *v;
92e73e63 548 register int l;
e53dbb38 549 char line[MAXLINE + 1], repbuf[80], greetings[200];
92e73e63 550
e53dbb38
KB
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 }
92e73e63
MK
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++;
e53dbb38 575
35c3f83c
MK
576 if (msg) {
577 v->iov_base = msg;
578 v->iov_len = strlen(msg);
579 } else if (f->f_prevcount > 1) {
92e73e63 580 v->iov_base = repbuf;
e53dbb38
KB
581 v->iov_len = sprintf(repbuf, "last message repeated %d times",
582 f->f_prevcount);
92e73e63
MK
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);
e53dbb38
KB
599 l = sprintf(line, "<%d>%.15s %s", f->f_prevpri,
600 iov[0].iov_base, iov[4].iov_base);
92e73e63
MK
601 if (l > MAXLINE)
602 l = MAXLINE;
0df908a9
KB
603 if (sendto(finet, line, l, 0,
604 (struct sockaddr *)&f->f_un.f_forw.f_addr,
92e73e63
MK
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");
64b300b4 617 break;
92e73e63
MK
618 }
619 /* FALLTHROUGH */
e677da31 620
92e73e63
MK
621 case F_TTY:
622 case F_FILE:
623 dprintf(" %s\n", f->f_un.f_fname);
624 if (f->f_type != F_FILE) {
64b300b4
EA
625 v->iov_base = "\r\n";
626 v->iov_len = 2;
92e73e63
MK
627 } else {
628 v->iov_base = "\n";
629 v->iov_len = 1;
64b300b4 630 }
92e73e63
MK
631 again:
632 if (writev(f->f_file, iov, 6) < 0) {
633 int e = errno;
634 (void) close(f->f_file);
635 /*
604d6f83 636 * Check for errors on TTY's due to loss of tty
92e73e63 637 */
604d6f83 638 if ((e == EIO || e == EBADF) && f->f_type != F_FILE) {
e53dbb38
KB
639 f->f_file = open(f->f_un.f_fname,
640 O_WRONLY|O_APPEND, 0);
92e73e63
MK
641 if (f->f_file < 0) {
642 f->f_type = F_UNUSED;
643 logerror(f->f_un.f_fname);
43d42ac6 644 } else
92e73e63 645 goto again;
92e73e63
MK
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;
e677da31 654
92e73e63
MK
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;
e677da31
RC
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
64b300b4
EA
673wallmsg(f, iov)
674 register struct filed *f;
675 struct iovec *iov;
e677da31 676{
e53dbb38
KB
677 static int reenter; /* avoid calling ourselves */
678 register FILE *uf;
e677da31 679 register int i;
e677da31 680 struct utmp ut;
e53dbb38 681 char *p, *ttymsg();
64b300b4
EA
682
683 if (reenter++)
684 return;
61b108ae 685 if ((uf = fopen(_PATH_UTMP, "r")) == NULL) {
3cdeaa19 686 logerror(_PATH_UTMP);
64b300b4 687 reenter = 0;
e677da31 688 return;
ada83039 689 }
e53dbb38
KB
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) {
64a60a45 695 if (p = ttymsg(iov, 6, ut.ut_line, 1, 60*5)) {
233e81a0 696 errno = 0; /* already in msg */
e53dbb38 697 logerror(p);
233e81a0 698 }
e53dbb38
KB
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)) {
64a60a45 707 if (p = ttymsg(iov, 6, ut.ut_line, 1, 60*5)) {
233e81a0 708 errno = 0; /* already in msg */
e53dbb38 709 logerror(p);
233e81a0 710 }
e53dbb38 711 break;
fcd484c8 712 }
e677da31 713 }
e677da31 714 }
84555b38 715 (void) fclose(uf);
64b300b4 716 reenter = 0;
84555b38
RC
717}
718
981fd33c 719void
84555b38
RC
720reapchild()
721{
722 union wait status;
723
0df908a9 724 while (wait3((int *)&status, WNOHANG, (struct rusage *) NULL) > 0)
84555b38 725 ;
e677da31
RC
726}
727
728/*
64b300b4 729 * Return a printable representation of a host address.
e677da31 730 */
64b300b4
EA
731char *
732cvthname(f)
e677da31
RC
733 struct sockaddr_in *f;
734{
735 struct hostent *hp;
ad58e75e 736 register char *p;
e677da31
RC
737 extern char *inet_ntoa();
738
64b300b4 739 dprintf("cvthname(%s)\n", inet_ntoa(f->sin_addr));
e677da31
RC
740
741 if (f->sin_family != AF_INET) {
742 dprintf("Malformed from address\n");
64b300b4 743 return ("???");
e677da31 744 }
0df908a9
KB
745 hp = gethostbyaddr((char *)&f->sin_addr,
746 sizeof(struct in_addr), f->sin_family);
e677da31
RC
747 if (hp == 0) {
748 dprintf("Host name for your address (%s) unknown\n",
749 inet_ntoa(f->sin_addr));
64b300b4 750 return (inet_ntoa(f->sin_addr));
e677da31 751 }
ad58e75e
EA
752 if ((p = index(hp->h_name, '.')) && strcmp(p + 1, LocalDomain) == 0)
753 *p = '\0';
64b300b4 754 return (hp->h_name);
e677da31
RC
755}
756
981fd33c 757void
ad58e75e
EA
758domark()
759{
92e73e63 760 register struct filed *f;
e53dbb38 761 time_t time();
ad58e75e 762
e53dbb38 763 now = time((time_t *)NULL);
92e73e63
MK
764 MarkSeq += TIMERINTVL;
765 if (MarkSeq >= MarkInterval) {
c16a39e2 766 logmsg(LOG_INFO, "-- MARK --", LocalHostName, ADDDATE|MARK);
92e73e63
MK
767 MarkSeq = 0;
768 }
ad58e75e 769
92e73e63
MK
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]);
35c3f83c 775 fprintlog(f, 0, (char *)NULL);
92e73e63
MK
776 BACKOFF(f);
777 }
778 }
779 (void) alarm(TIMERINTVL);
a49a6aba
RC
780}
781
e677da31
RC
782/*
783 * Print syslogd errors some place.
784 */
785logerror(type)
786 char *type;
787{
e185ee1c 788 char buf[100], *strerror();
e677da31 789
e185ee1c
KB
790 if (errno)
791 (void) sprintf(buf, "syslogd: %s: %s", type, strerror(errno));
e677da31 792 else
e185ee1c 793 (void) sprintf(buf, "syslogd: %s", type);
e677da31 794 errno = 0;
ada83039 795 dprintf("%s\n", buf);
ad58e75e 796 logmsg(LOG_SYSLOG|LOG_ERR, buf, LocalHostName, ADDDATE);
e677da31
RC
797}
798
981fd33c 799void
64b300b4 800die(sig)
e677da31 801{
92e73e63 802 register struct filed *f;
64b300b4 803 char buf[100];
ada83039 804
92e73e63
MK
805 for (f = Files; f != NULL; f = f->f_next) {
806 /* flush any pending output */
807 if (f->f_prevcount)
35c3f83c 808 fprintlog(f, 0, (char *)NULL);
92e73e63 809 }
a075f884 810 if (sig) {
92e73e63
MK
811 dprintf("syslogd: exiting on signal %d\n", sig);
812 (void) sprintf(buf, "exiting on signal %d", sig);
d34bf11a 813 errno = 0;
a075f884
MK
814 logerror(buf);
815 }
64b300b4 816 (void) unlink(LogName);
e677da31
RC
817 exit(0);
818}
a49a6aba
RC
819
820/*
64b300b4 821 * INIT -- Initialize syslogd from configuration table
a49a6aba 822 */
64b300b4 823
e53dbb38 824void
64b300b4 825init()
a49a6aba 826{
a49a6aba 827 register int i;
64b300b4 828 register FILE *cf;
92e73e63 829 register struct filed *f, *next, **nextp;
64b300b4
EA
830 register char *p;
831 char cline[BUFSIZ];
a49a6aba 832
64b300b4
EA
833 dprintf("init\n");
834
64b300b4
EA
835 /*
836 * Close all open log files.
837 */
d34bf11a 838 Initialized = 0;
92e73e63
MK
839 for (f = Files; f != NULL; f = next) {
840 /* flush any pending output */
841 if (f->f_prevcount)
35c3f83c 842 fprintlog(f, 0, (char *)NULL);
92e73e63 843
bb112360
EA
844 switch (f->f_type) {
845 case F_FILE:
846 case F_TTY:
bb112360 847 case F_CONSOLE:
2efc48b0 848 case F_FORW:
64b300b4 849 (void) close(f->f_file);
bb112360
EA
850 break;
851 }
92e73e63
MK
852 next = f->f_next;
853 free((char *) f);
64b300b4 854 }
92e73e63
MK
855 Files = NULL;
856 nextp = &Files;
64b300b4
EA
857
858 /* open the configuration file */
859 if ((cf = fopen(ConfFile, "r")) == NULL) {
860 dprintf("cannot open %s\n", ConfFile);
92e73e63
MK
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;
64b300b4
EA
866 return;
867 }
868
869 /*
870 * Foreach line in the conf table, open that file.
871 */
92e73e63
MK
872 f = NULL;
873 while (fgets(cline, sizeof cline, cf) != NULL) {
e15840af
KB
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 == '#')
a49a6aba 880 continue;
e15840af
KB
881 for (p = index(cline, '\0'); isspace(*--p););
882 *++p = '\0';
92e73e63
MK
883 f = (struct filed *)calloc(1, sizeof(*f));
884 *nextp = f;
885 nextp = &f->f_next;
92e73e63 886 cfline(cline, f);
64b300b4
EA
887 }
888
889 /* close the configuration file */
890 (void) fclose(cf);
891
892 Initialized = 1;
893
894 if (Debug) {
92e73e63 895 for (f = Files; f; f = f->f_next) {
c16a39e2 896 for (i = 0; i <= LOG_NFACILITIES; i++)
233e81a0 897 if (f->f_pmask[i] == INTERNAL_NOPRI)
64b300b4
EA
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
ad58e75e 922 logmsg(LOG_SYSLOG|LOG_INFO, "syslogd: restart", LocalHostName, ADDDATE);
64b300b4
EA
923 dprintf("syslogd: restarted\n");
924}
925
926/*
927 * Crack a configuration file line
928 */
929
64b300b4
EA
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;
50761dd0 940 char buf[MAXLINE], ebuf[100];
64b300b4
EA
941
942 dprintf("cfline(%s)\n", line);
943
e185ee1c 944 errno = 0; /* keep strerror() stuff out of logerror messages */
d34bf11a 945
64b300b4
EA
946 /* clear out file entry */
947 bzero((char *) f, sizeof *f);
c16a39e2 948 for (i = 0; i <= LOG_NFACILITIES; i++)
233e81a0 949 f->f_pmask[i] = INTERNAL_NOPRI;
64b300b4
EA
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++ != '.'; )
a49a6aba 956 continue;
64b300b4
EA
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 */
50761dd0
KB
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 }
64b300b4
EA
978 }
979
980 /* scan facilities */
981 while (*p && !index("\t.;", *p)) {
64b300b4
EA
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 {
233e81a0 989 i = decode(buf, facilitynames);
64b300b4 990 if (i < 0) {
50761dd0 991 (void) sprintf(ebuf,
e53dbb38
KB
992 "unknown facility name \"%s\"",
993 buf);
50761dd0 994 logerror(ebuf);
64b300b4
EA
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) {
5b39dbe7 1018 extern int h_errno, h_nerr;
ff24423e 1019 extern char *h_errlist[];
64b300b4 1020
5b39dbe7
KB
1021 logerror((u_int)h_errno < h_nerr ?
1022 h_errlist[h_errno] : "Unknown error");
64b300b4
EA
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);
64b300b4
EA
1030 f->f_type = F_FORW;
1031 break;
1032
1033 case '/':
1034 (void) strcpy(f->f_un.f_fname, p);
e53dbb38 1035 if ((f->f_file = open(p, O_WRONLY|O_APPEND, 0)) < 0) {
e15840af 1036 f->f_file = F_UNUSED;
64b300b4
EA
1037 logerror(p);
1038 break;
1039 }
43d42ac6 1040 if (isatty(f->f_file))
64b300b4 1041 f->f_type = F_TTY;
64b300b4
EA
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++;
e185ee1c
KB
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';
64b300b4
EA
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;
a49a6aba 1067 }
64b300b4
EA
1068}
1069
1070
1071/*
1072 * Decode a symbolic name to a numeric value
1073 */
1074
1075decode(name, codetab)
1076 char *name;
233e81a0 1077 CODE *codetab;
64b300b4 1078{
233e81a0 1079 register CODE *c;
64b300b4
EA
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);
a49a6aba 1093
64b300b4 1094 return (-1);
a49a6aba 1095}