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