1: /*
2: * Copyright (c) 1983, 1988, 1993, 1994
3: * The Regents of the University of California. All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: * 2. Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: * 4. Neither the name of the University nor the names of its contributors
14: * may be used to endorse or promote products derived from this software
15: * without specific prior written permission.
16: *
17: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27: * SUCH DAMAGE.
28: */
29:
30: #ifndef lint
31: static const char copyright[] =
32: "@(#) Copyright (c) 1983, 1988, 1993, 1994\n\
33: The Regents of the University of California. All rights reserved.\n";
34: #endif /* not lint */
35:
36: #ifndef lint
37: #if 0
38: static char sccsid[] = "@(#)syslogd.c 8.3 (Berkeley) 4/4/94";
39: #endif
40: #endif /* not lint */
41:
42: #include <sys/cdefs.h>
43: __FBSDID("$FreeBSD: src/usr.sbin/syslogd/syslogd.c,v 1.167 2010/08/07 16:20:12 olli Exp $");
44:
45: /*
46: * syslogd -- log system messages
47: *
48: * This program implements a system log. It takes a series of lines.
49: * Each line may have a priority, signified as "<n>" as
50: * the first characters of the line. If this is
51: * not present, a default priority is used.
52: *
53: * To kill syslogd, send a signal 15 (terminate). A signal 1 (hup) will
54: * cause it to reread its configuration file.
55: *
56: * Defined Constants:
57: *
58: * MAXLINE -- the maximum line length that can be handled.
59: * DEFUPRI -- the default priority for user messages
60: * DEFSPRI -- the default priority for kernel messages
61: *
62: * Author: Eric Allman
63: * extensive changes by Ralph Campbell
64: * more extensive changes by Eric Allman (again)
65: * Extension to log by program name as well as facility and priority
66: * by Peter da Silva.
67: * -u and -v by Harlan Stenn.
68: * Priority comparison code by Harlan Stenn.
69: */
70:
71: #define MAXLINE 1024 /* maximum line length */
72: #define MAXSVLINE 120 /* maximum saved line length */
73: #define DEFUPRI (LOG_USER|LOG_NOTICE)
74: #define DEFSPRI (LOG_KERN|LOG_CRIT)
75: #define TIMERINTVL 30 /* interval for checking flush, mark */
76: #define TTYMSGTIME 1 /* timeout passed to ttymsg */
77:
78: #include <sys/param.h>
79: #include <sys/ioctl.h>
80: #include <sys/mman.h>
81: #include <sys/stat.h>
82: #include <sys/wait.h>
83: #include <sys/socket.h>
84: #include <sys/queue.h>
85: #include <sys/uio.h>
86: #include <sys/un.h>
87: #include <sys/time.h>
88: #include <sys/resource.h>
89: #include <sys/syslimits.h>
90: #include <sys/types.h>
91:
92: #include <netinet/in.h>
93: #include <netdb.h>
94: #include <arpa/inet.h>
95:
96: #include <ctype.h>
97: #include <err.h>
98: #include <errno.h>
99: #include <fcntl.h>
100: #include <libutil.h>
101: #include <limits.h>
102: #include <paths.h>
103: #include <signal.h>
104: #include <stdio.h>
105: #include <stdlib.h>
106: #include <string.h>
107: #include <sysexits.h>
108: #include <unistd.h>
109: #include <utmpx.h>
110:
111: #include "pathnames.h"
112:
113: // CPP:
114: #include "defs.h"
115: #include "clog.h"
116: #include <sys/mman.h>
117: #include <dirent.h>
118: // CPP::
119:
120: #define SYSLOG_NAMES
121: #include <sys/syslog.h>
122:
123: const char *ConfFile = _PATH_LOGCONF;
124: const char *PidFile = _PATH_LOGPID;
125: const char ctty[] = _PATH_CONSOLE;
126:
127: #define dprintf if (Debug) printf
128:
129: #define MAXUNAMES 20 /* maximum number of user names */
130:
131: /*
132: * Unix sockets.
133: * We have two default sockets, one with 666 permissions,
134: * and one for privileged programs.
135: */
136: struct funix {
137: int s;
138: const char *name;
139: mode_t mode;
140: STAILQ_ENTRY(funix) next;
141: };
142: struct funix funix_secure = { -1, _PATH_LOG_PRIV, S_IRUSR | S_IWUSR,
143: { NULL } };
144: struct funix funix_default = { -1, _PATH_LOG, DEFFILEMODE,
145: { &funix_secure } };
146:
147: STAILQ_HEAD(, funix) funixes = { &funix_default,
148: &(funix_secure.next.stqe_next) };
149:
150: /*
151: * Flags to logmsg().
152: */
153:
154: #define IGN_CONS 0x001 /* don't print on console */
155: #define SYNC_FILE 0x002 /* do fsync on file after printing */
156: #define ADDDATE 0x004 /* add a date to the message */
157: #define MARK 0x008 /* this message is a mark */
158: #define ISKERNEL 0x010 /* kernel generated message */
159:
160: /*
161: * This structure represents the files that will have log
162: * copies printed.
163: * We require f_file to be valid if f_type is F_FILE, F_CONSOLE, F_TTY
164: * or if f_type if F_PIPE and f_pid > 0.
165: */
166:
167: struct filed {
168: struct filed *f_next; /* next in linked list */
169: short f_type; /* entry type, see below */
170: short f_file; /* file descriptor */
171: time_t f_time; /* time this was last written */
172: char *f_host; /* host from which to recd. */
173: u_char f_pmask[LOG_NFACILITIES+1]; /* priority mask */
174: u_char f_pcmp[LOG_NFACILITIES+1]; /* compare priority */
175: #define PRI_LT 0x1
176: #define PRI_EQ 0x2
177: #define PRI_GT 0x4
178: char *f_program; /* program this applies to */
179: union {
180: char f_uname[MAXUNAMES][MAXLOGNAME];
181: struct {
182: char f_hname[MAXHOSTNAMELEN];
183: struct addrinfo *f_addr;
184:
185: } f_forw; /* forwarding address */
186: char f_fname[MAXPATHLEN];
187: struct {
188: char f_pname[MAXPATHLEN];
189: pid_t f_pid;
190: } f_pipe;
191: // CPP:
192: struct {
193: char f_rname[MAXPATHLEN];
194: struct clogFooter *f_footer;
195: size_t f_size;
196: } f_ring;
197: // CPP::
198: } f_un;
199: char f_prevline[MAXSVLINE]; /* last message logged */
200: char f_lasttime[16]; /* time of last occurrence */
201: char f_prevhost[MAXHOSTNAMELEN]; /* host from which recd. */
202: int f_prevpri; /* pri of f_prevline */
203: int f_prevlen; /* length of f_prevline */
204: int f_prevcount; /* repetition cnt of prevline */
205: u_int f_repeatcount; /* number of "repeated" msgs */
206: int f_flags; /* file-specific flags */
207: #define FFLAG_SYNC 0x01
208: #define FFLAG_NEEDSYNC 0x02
209: };
210:
211: /*
212: * Queue of about-to-be dead processes we should watch out for.
213: */
214:
215: TAILQ_HEAD(stailhead, deadq_entry) deadq_head;
216: struct stailhead *deadq_headp;
217:
218: struct deadq_entry {
219: pid_t dq_pid;
220: int dq_timeout;
221: TAILQ_ENTRY(deadq_entry) dq_entries;
222: };
223:
224: /*
225: * The timeout to apply to processes waiting on the dead queue. Unit
226: * of measure is `mark intervals', i.e. 20 minutes by default.
227: * Processes on the dead queue will be terminated after that time.
228: */
229:
230: #define DQ_TIMO_INIT 2
231:
232: typedef struct deadq_entry *dq_t;
233:
234:
235: /*
236: * Struct to hold records of network addresses that are allowed to log
237: * to us.
238: */
239: struct allowedpeer {
240: int isnumeric;
241: u_short port;
242: union {
243: struct {
244: struct sockaddr_storage addr;
245: struct sockaddr_storage mask;
246: } numeric;
247: char *name;
248: } u;
249: #define a_addr u.numeric.addr
250: #define a_mask u.numeric.mask
251: #define a_name u.name
252: };
253:
254:
255: /*
256: * Intervals at which we flush out "message repeated" messages,
257: * in seconds after previous message is logged. After each flush,
258: * we move to the next interval until we reach the largest.
259: */
260: int repeatinterval[] = { 30, 120, 600 }; /* # of secs before flush */
261: #define MAXREPEAT ((sizeof(repeatinterval) / sizeof(repeatinterval[0])) - 1)
262: #define REPEATTIME(f) ((f)->f_time + repeatinterval[(f)->f_repeatcount])
263: #define BACKOFF(f) { if (++(f)->f_repeatcount > MAXREPEAT) \
264: (f)->f_repeatcount = MAXREPEAT; \
265: }
266:
267: /* values for f_type */
268: #define F_UNUSED 0 /* unused entry */
269: #define F_FILE 1 /* regular file */
270: #define F_TTY 2 /* terminal */
271: #define F_CONSOLE 3 /* console terminal */
272: #define F_FORW 4 /* remote machine */
273: #define F_USERS 5 /* list of users */
274: #define F_WALL 6 /* everyone logged on */
275: #define F_PIPE 7 /* pipe to program */
276: #define F_RING 8 /* ring buffer (circular log) */
277:
278: // CPP:
279: const char *TypeNames[] = {
280: "UNUSED", "FILE", "TTY", "CONSOLE",
281: "FORW", "USERS", "WALL", "PIPE",
282: "RING"
283: };
284: // CPP::
285:
286: static struct filed *Files; /* Log files that we write to */
287: static struct filed consfile; /* Console */
288:
289: static int Debug; /* debug flag */
290: static int resolve = 1; /* resolve hostname */
291: static char LocalHostName[MAXHOSTNAMELEN]; /* our hostname */
292: static const char *LocalDomain; /* our local domain name */
293: static int *finet; /* Internet datagram socket */
294: static int fklog = -1; /* /dev/klog */
295: static int Initialized; /* set when we have initialized ourselves */
296: static int MarkInterval = 20 * 60; /* interval between marks in seconds */
297: static int MarkSeq; /* mark sequence number */
298: static int SecureMode; /* when true, receive only unix domain socks */
299: #ifdef INET6
300: static int family = PF_UNSPEC; /* protocol family (IPv4, IPv6 or both) */
301: #else
302: static int family = PF_INET; /* protocol family (IPv4 only) */
303: #endif
304: static int mask_C1 = 1; /* mask characters from 0x80 - 0x9F */
305: static int send_to_all; /* send message to all IPv4/IPv6 addresses */
306: static int use_bootfile; /* log entire bootfile for every kern msg */
307: static int no_compress; /* don't compress messages (1=pipes, 2=all) */
308: static int logflags = O_WRONLY|O_APPEND; /* flags used to open log files */
309:
310: static char bootfile[MAXLINE+1]; /* booted kernel file */
311:
312: struct allowedpeer *AllowedPeers; /* List of allowed peers */
313: static int NumAllowed; /* Number of entries in AllowedPeers */
314: static int RemoteAddDate; /* Always set the date on remote messages */
315:
316: static int UniquePriority; /* Only log specified priority? */
317: static int LogFacPri; /* Put facility and priority in log message: */
318: /* 0=no, 1=numeric, 2=names */
319: static int KeepKernFac; /* Keep remotely logged kernel facility */
320: static int needdofsync = 0; /* Are any file(s) waiting to be fsynced? */
321: static struct pidfh *pfh;
322:
323: volatile sig_atomic_t MarkSet, WantDie;
324:
325: static int allowaddr(char *);
326: static void cfline(const char *, struct filed *,
327: const char *, const char *);
328: static const char *cvthname(struct sockaddr *);
329: static void deadq_enter(pid_t, const char *);
330: static int deadq_remove(pid_t);
331: static int decode(const char *, CODE *);
332: static void die(int);
333: static void dodie(int);
334: static void dofsync(void);
335: static void domark(int);
336: static void fprintlog(struct filed *, int, const char *);
337: static int *socksetup(int, char *);
338: static void init(int);
339: static void logerror(const char *);
340: static void logmsg(int, const char *, const char *, int);
341: static void log_deadchild(pid_t, int, const char *);
342: static void markit(void);
343: static int skip_message(const char *, const char *, int);
344: static void printline(const char *, char *, int);
345: static void printsys(char *);
346: static int p_open(const char *, pid_t *);
347: static void readklog(void);
348: static void reapchild(int);
349: static void usage(void);
350: static int validate(struct sockaddr *, const char *);
351: static void unmapped(struct sockaddr *);
352: static void wallmsg(struct filed *, struct iovec *, const int iovlen);
353: static int waitdaemon(int, int, int);
354: static void timedout(int);
355: static void double_rbuf(int);
356:
357: // CPP:
358: static const char *
359: ttymsg(struct iovec *iov, int iovcnt, const char *line, int tmout)
360: {
361: struct iovec localiov[7];
362: ssize_t left, wret;
363: int cnt, fd;
364: static char device[MAXNAMLEN] = _PATH_DEV;
365: static char errbuf[1024];
366: char *p;
367: int forked;
368:
369: forked = 0;
370: if (iovcnt > (int)(sizeof(localiov) / sizeof(localiov[0])))
371: return ("too many iov's (change code in wall/ttymsg.c)");
372:
373: p = device + sizeof(_PATH_DEV) - 1;
374: strlcpy(p, line, sizeof(device));
375: if (strncmp(p, "pts/", 4) == 0)
376: p += 4;
377: if (strchr(p, '/') != NULL) {
378: /* A slash is an attempt to break security... */
379: (void) snprintf(errbuf, sizeof(errbuf),
380: "Too many '/' in \"%s\"", device);
381: return (errbuf);
382: }
383:
384: /*
385: * open will fail on slip lines or exclusive-use lines
386: * if not running as root; not an error.
387: */
388: if ((fd = open(device, O_WRONLY|O_NONBLOCK, 0)) < 0) {
389: if (errno == EBUSY || errno == EACCES)
390: return (NULL);
391: (void) snprintf(errbuf, sizeof(errbuf), "%s: %s", device,
392: strerror(errno));
393: return (errbuf);
394: }
395:
396: for (cnt = 0, left = 0; cnt < iovcnt; ++cnt)
397: left += iov[cnt].iov_len;
398:
399: for (;;) {
400: wret = writev(fd, iov, iovcnt);
401: if (wret >= left)
402: break;
403: if (wret >= 0) {
404: left -= wret;
405: if (iov != localiov) {
406: bcopy(iov, localiov,
407: iovcnt * sizeof(struct iovec));
408: iov = localiov;
409: }
410: for (cnt = 0; (size_t)wret >= iov->iov_len; ++cnt) {
411: wret -= iov->iov_len;
412: ++iov;
413: --iovcnt;
414: }
415: if (wret) {
416: iov->iov_base = (char *)iov->iov_base + wret;
417: iov->iov_len -= wret;
418: }
419: continue;
420: }
421: if (errno == EWOULDBLOCK) {
422: int cpid;
423:
424: if (forked) {
425: (void) close(fd);
426: _exit(1);
427: }
428: cpid = fork();
429: if (cpid < 0) {
430: (void) snprintf(errbuf, sizeof(errbuf),
431: "fork: %s", strerror(errno));
432: (void) close(fd);
433: return (errbuf);
434: }
435: if (cpid) { /* parent */
436: (void) close(fd);
437: return (NULL);
438: }
439: forked++;
440: /* wait at most tmout seconds */
441: (void) signal(SIGALRM, SIG_DFL);
442: (void) signal(SIGTERM, SIG_DFL); /* XXX */
443: (void) sigsetmask(0);
444: (void) alarm((u_int)tmout);
445: (void) fcntl(fd, F_SETFL, 0); /* clear O_NONBLOCK */
446: continue;
447: }
448: /*
449: * We get ENODEV on a slip line if we're running as root,
450: * and EIO if the line just went away.
451: */
452: if (errno == ENODEV || errno == EIO)
453: break;
454: (void) close(fd);
455: if (forked)
456: _exit(1);
457: (void) snprintf(errbuf, sizeof(errbuf),
458: "%s: %s", device, strerror(errno));
459: return (errbuf);
460: }
461:
462: (void) close(fd);
463: if (forked)
464: _exit(0);
465: return (NULL);
466: }
467:
468: static ssize_t
469: rbwrite(struct filed *f, char *buf, size_t nbytes)
470: {
471: ssize_t err, out = 0;
472: size_t maxwrite = f->f_un.f_ring.f_footer->cf_max - f->f_un.f_ring.f_footer->cf_next;
473:
474: f->f_un.f_ring.f_footer->cf_lock = 1;
475: while (nbytes > 0) {
476: maxwrite = f->f_un.f_ring.f_footer->cf_max - f->f_un.f_ring.f_footer->cf_next;
477: if (maxwrite > nbytes)
478: maxwrite = nbytes;
479: err = pwrite(f->f_file, buf, maxwrite, f->f_un.f_ring.f_footer->cf_next);
480: if (err == -1) {
481: f->f_un.f_ring.f_footer->cf_lock = 0;
482: return -1;
483: }
484: nbytes -= err;
485: out += err;
486: buf += err;
487: f->f_un.f_ring.f_footer->cf_next += err;
488: if (f->f_un.f_ring.f_footer->cf_next == f->f_un.f_ring.f_footer->cf_max) {
489: f->f_un.f_ring.f_footer->cf_next = 0;
490: f->f_un.f_ring.f_footer->cf_wrap = 1;
491: }
492: }
493:
494: f->f_un.f_ring.f_footer->cf_lock = 0;
495: return out;
496: }
497: static ssize_t
498: rbwritev(struct filed *f, struct iovec *iov, int iovcnt)
499: {
500: register int i;
501: ssize_t err, out = 0;
502:
503: for(i = 0; i < iovcnt; i++) {
504: err = rbwrite(f, iov[i].iov_base, iov[i].iov_len);
505: if (err == -1)
506: return -1;
507: out += err;
508: }
509:
510: return out;
511: }
512: // CPP::
513:
514: int
515: main(int argc, char *argv[])
516: {
517: int ch, i, fdsrmax = 0, l;
518: struct sockaddr_un sunx, fromunix;
519: struct sockaddr_storage frominet;
520: fd_set *fdsr = NULL;
521: char line[MAXLINE + 1];
522: char *bindhostname;
523: const char *hname;
524: struct timeval tv, *tvp;
525: struct sigaction sact;
526: struct funix *fx, *fx1;
527: sigset_t mask;
528: pid_t ppid = 1, spid;
529: socklen_t len;
530:
531: if (madvise(NULL, 0, MADV_PROTECT) != 0)
532: dprintf("madvise() failed: %s\n", strerror(errno));
533:
534: bindhostname = NULL;
535: while ((ch = getopt(argc, argv, "468Aa:b:cCdf:kl:m:nop:P:sS:Tuv"))
536: != -1)
537: switch (ch) {
538: case '4':
539: family = PF_INET;
540: break;
541: #ifdef INET6
542: case '6':
543: family = PF_INET6;
544: break;
545: #endif
546: case '8':
547: mask_C1 = 0;
548: break;
549: case 'A':
550: send_to_all++;
551: break;
552: case 'a': /* allow specific network addresses only */
553: if (allowaddr(optarg) == -1)
554: usage();
555: break;
556: case 'b':
557: bindhostname = optarg;
558: break;
559: case 'c':
560: no_compress++;
561: break;
562: case 'C':
563: logflags |= O_CREAT;
564: break;
565: case 'd': /* debug */
566: Debug++;
567: break;
568: case 'f': /* configuration file */
569: ConfFile = optarg;
570: break;
571: case 'k': /* keep remote kern fac */
572: KeepKernFac = 1;
573: break;
574: case 'l':
575: {
576: long perml;
577: mode_t mode;
578: char *name, *ep;
579:
580: if (optarg[0] == '/') {
581: mode = DEFFILEMODE;
582: name = optarg;
583: } else if ((name = strchr(optarg, ':')) != NULL) {
584: *name++ = '\0';
585: if (name[0] != '/')
586: errx(1, "socket name must be absolute "
587: "path");
588: if (isdigit(*optarg)) {
589: perml = strtol(optarg, &ep, 8);
590: if (*ep || perml < 0 ||
591: perml & ~(S_IRWXU|S_IRWXG|S_IRWXO))
592: errx(1, "invalid mode %s, exiting",
593: optarg);
594: mode = (mode_t )perml;
595: } else
596: errx(1, "invalid mode %s, exiting",
597: optarg);
598: } else /* doesn't begin with '/', and no ':' */
599: errx(1, "can't parse path %s", optarg);
600:
601: if (strlen(name) >= sizeof(sunx.sun_path))
602: errx(1, "%s path too long, exiting", name);
603: if ((fx = malloc(sizeof(struct funix))) == NULL)
604: errx(1, "malloc failed");
605: fx->s = -1;
606: fx->name = name;
607: fx->mode = mode;
608: STAILQ_INSERT_TAIL(&funixes, fx, next);
609: break;
610: }
611: case 'm': /* mark interval */
612: MarkInterval = atoi(optarg) * 60;
613: break;
614: case 'n':
615: resolve = 0;
616: break;
617: case 'o':
618: use_bootfile = 1;
619: break;
620: case 'p': /* path */
621: if (strlen(optarg) >= sizeof(sunx.sun_path))
622: errx(1, "%s path too long, exiting", optarg);
623: funix_default.name = optarg;
624: break;
625: case 'P': /* path for alt. PID */
626: PidFile = optarg;
627: break;
628: case 's': /* no network mode */
629: SecureMode++;
630: break;
631: case 'S': /* path for privileged originator */
632: if (strlen(optarg) >= sizeof(sunx.sun_path))
633: errx(1, "%s path too long, exiting", optarg);
634: funix_secure.name = optarg;
635: break;
636: case 'T':
637: RemoteAddDate = 1;
638: break;
639: case 'u': /* only log specified priority */
640: UniquePriority++;
641: break;
642: case 'v': /* log facility and priority */
643: LogFacPri++;
644: break;
645: default:
646: usage();
647: }
648: if ((argc -= optind) != 0)
649: usage();
650:
651: pfh = pidfile_open(PidFile, 0600, &spid);
652: if (pfh == NULL) {
653: if (errno == EEXIST)
654: errx(1, "syslogd already running, pid: %d", spid);
655: warn("cannot open pid file");
656: }
657:
658: if (!Debug) {
659: ppid = waitdaemon(0, 0, 30);
660: if (ppid < 0) {
661: warn("could not become daemon");
662: pidfile_remove(pfh);
663: exit(1);
664: }
665: } else {
666: setlinebuf(stdout);
667: }
668:
669: if (NumAllowed)
670: endservent();
671:
672: consfile.f_type = F_CONSOLE;
673: (void)strlcpy(consfile.f_un.f_fname, ctty + sizeof _PATH_DEV - 1,
674: sizeof(consfile.f_un.f_fname));
675: (void)strlcpy(bootfile, getbootfile(), sizeof(bootfile));
676: (void)signal(SIGTERM, dodie);
677: (void)signal(SIGINT, Debug ? dodie : SIG_IGN);
678: (void)signal(SIGQUIT, Debug ? dodie : SIG_IGN);
679: /*
680: * We don't want the SIGCHLD and SIGHUP handlers to interfere
681: * with each other; they are likely candidates for being called
682: * simultaneously (SIGHUP closes pipe descriptor, process dies,
683: * SIGCHLD happens).
684: */
685: sigemptyset(&mask);
686: sigaddset(&mask, SIGHUP);
687: sact.sa_handler = reapchild;
688: sact.sa_mask = mask;
689: sact.sa_flags = SA_RESTART;
690: (void)sigaction(SIGCHLD, &sact, NULL);
691: (void)signal(SIGALRM, domark);
692: (void)signal(SIGPIPE, SIG_IGN); /* We'll catch EPIPE instead. */
693: (void)alarm(TIMERINTVL);
694:
695: TAILQ_INIT(&deadq_head);
696:
697: #ifndef SUN_LEN
698: #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
699: #endif
700: STAILQ_FOREACH_SAFE(fx, &funixes, next, fx1) {
701: (void)unlink(fx->name);
702: memset(&sunx, 0, sizeof(sunx));
703: sunx.sun_family = AF_LOCAL;
704: (void)strlcpy(sunx.sun_path, fx->name, sizeof(sunx.sun_path));
705: fx->s = socket(PF_LOCAL, SOCK_DGRAM, 0);
706: if (fx->s < 0 ||
707: bind(fx->s, (struct sockaddr *)&sunx, SUN_LEN(&sunx)) < 0 ||
708: chmod(fx->name, fx->mode) < 0) {
709: (void)snprintf(line, sizeof line,
710: "cannot create %s", fx->name);
711: logerror(line);
712: dprintf("cannot create %s (%d)\n", fx->name, errno);
713: if (fx == &funix_default || fx == &funix_secure)
714: die(0);
715: else {
716: STAILQ_REMOVE(&funixes, fx, funix, next);
717: continue;
718: }
719: double_rbuf(fx->s);
720: }
721: }
722: if (SecureMode <= 1)
723: finet = socksetup(family, bindhostname);
724:
725: if (finet) {
726: if (SecureMode) {
727: for (i = 0; i < *finet; i++) {
728: if (shutdown(finet[i+1], SHUT_RD) < 0) {
729: logerror("shutdown");
730: if (!Debug)
731: die(0);
732: }
733: }
734: } else {
735: dprintf("listening on inet and/or inet6 socket\n");
736: }
737: dprintf("sending on inet and/or inet6 socket\n");
738: }
739:
740: if ((fklog = open(_PATH_KLOG, O_RDONLY, 0)) >= 0)
741: if (fcntl(fklog, F_SETFL, O_NONBLOCK) < 0)
742: fklog = -1;
743: if (fklog < 0)
744: dprintf("can't open %s (%d)\n", _PATH_KLOG, errno);
745:
746: /* tuck my process id away */
747: pidfile_write(pfh);
748:
749: dprintf("off & running....\n");
750:
751: init(0);
752: /* prevent SIGHUP and SIGCHLD handlers from running in parallel */
753: sigemptyset(&mask);
754: sigaddset(&mask, SIGCHLD);
755: sact.sa_handler = init;
756: sact.sa_mask = mask;
757: sact.sa_flags = SA_RESTART;
758: (void)sigaction(SIGHUP, &sact, NULL);
759:
760: tvp = &tv;
761: tv.tv_sec = tv.tv_usec = 0;
762:
763: if (fklog != -1 && fklog > fdsrmax)
764: fdsrmax = fklog;
765: if (finet && !SecureMode) {
766: for (i = 0; i < *finet; i++) {
767: if (finet[i+1] != -1 && finet[i+1] > fdsrmax)
768: fdsrmax = finet[i+1];
769: }
770: }
771: STAILQ_FOREACH(fx, &funixes, next)
772: if (fx->s > fdsrmax)
773: fdsrmax = fx->s;
774:
775: fdsr = (fd_set *)calloc(howmany(fdsrmax+1, NFDBITS),
776: sizeof(fd_mask));
777: if (fdsr == NULL)
778: errx(1, "calloc fd_set");
779:
780: for (;;) {
781: if (MarkSet)
782: markit();
783: if (WantDie)
784: die(WantDie);
785:
786: bzero(fdsr, howmany(fdsrmax+1, NFDBITS) *
787: sizeof(fd_mask));
788:
789: if (fklog != -1)
790: FD_SET(fklog, fdsr);
791: if (finet && !SecureMode) {
792: for (i = 0; i < *finet; i++) {
793: if (finet[i+1] != -1)
794: FD_SET(finet[i+1], fdsr);
795: }
796: }
797: STAILQ_FOREACH(fx, &funixes, next)
798: FD_SET(fx->s, fdsr);
799:
800: i = select(fdsrmax+1, fdsr, NULL, NULL,
801: needdofsync ? &tv : tvp);
802: switch (i) {
803: case 0:
804: dofsync();
805: needdofsync = 0;
806: if (tvp) {
807: tvp = NULL;
808: if (ppid != 1)
809: kill(ppid, SIGALRM);
810: }
811: continue;
812: case -1:
813: if (errno != EINTR)
814: logerror("select");
815: continue;
816: }
817: if (fklog != -1 && FD_ISSET(fklog, fdsr))
818: readklog();
819: if (finet && !SecureMode) {
820: for (i = 0; i < *finet; i++) {
821: if (FD_ISSET(finet[i+1], fdsr)) {
822: len = sizeof(frominet);
823: l = recvfrom(finet[i+1], line, MAXLINE,
824: 0, (struct sockaddr *)&frominet,
825: &len);
826: if (l > 0) {
827: line[l] = '\0';
828: hname = cvthname((struct sockaddr *)&frominet);
829: unmapped((struct sockaddr *)&frominet);
830: if (validate((struct sockaddr *)&frominet, hname))
831: printline(hname, line, RemoteAddDate ? ADDDATE : 0);
832: } else if (l < 0 && errno != EINTR)
833: logerror("recvfrom inet");
834: }
835: }
836: }
837: STAILQ_FOREACH(fx, &funixes, next) {
838: if (FD_ISSET(fx->s, fdsr)) {
839: len = sizeof(fromunix);
840: l = recvfrom(fx->s, line, MAXLINE, 0,
841: (struct sockaddr *)&fromunix, &len);
842: if (l > 0) {
843: line[l] = '\0';
844: printline(LocalHostName, line, 0);
845: } else if (l < 0 && errno != EINTR)
846: logerror("recvfrom unix");
847: }
848: }
849: }
850: if (fdsr)
851: free(fdsr);
852: }
853:
854: static void
855: unmapped(struct sockaddr *sa)
856: {
857: struct sockaddr_in6 *sin6;
858: struct sockaddr_in sin4;
859:
860: if (sa->sa_family != AF_INET6)
861: return;
862: if (sa->sa_len != sizeof(struct sockaddr_in6) ||
863: sizeof(sin4) > sa->sa_len)
864: return;
865: sin6 = (struct sockaddr_in6 *)sa;
866: if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
867: return;
868:
869: memset(&sin4, 0, sizeof(sin4));
870: sin4.sin_family = AF_INET;
871: sin4.sin_len = sizeof(struct sockaddr_in);
872: memcpy(&sin4.sin_addr, &sin6->sin6_addr.s6_addr[12],
873: sizeof(sin4.sin_addr));
874: sin4.sin_port = sin6->sin6_port;
875:
876: memcpy(sa, &sin4, sin4.sin_len);
877: }
878:
879: static void
880: usage(void)
881: {
882:
883: fprintf(stderr, "%s\n%s\n%s\n%s\n",
884: "usage: syslogd [-468ACcdknosTuv] [-a allowed_peer]",
885: " [-b bind_address] [-f config_file]",
886: " [-l [mode:]path] [-m mark_interval]",
887: " [-P pid_file] [-p log_socket]");
888: exit(1);
889: }
890:
891: /*
892: * Take a raw input line, decode the message, and print the message
893: * on the appropriate log files.
894: */
895: static void
896: printline(const char *hname, char *msg, int flags)
897: {
898: char *p, *q;
899: long n;
900: int c, pri;
901: char line[MAXLINE + 1];
902:
903: /* test for special codes */
904: p = msg;
905: pri = DEFUPRI;
906: if (*p == '<') {
907: errno = 0;
908: n = strtol(p + 1, &q, 10);
909: if (*q == '>' && n >= 0 && n < INT_MAX && errno == 0) {
910: p = q + 1;
911: pri = n;
912: }
913: }
914: if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
915: pri = DEFUPRI;
916:
917: /*
918: * Don't allow users to log kernel messages.
919: * NOTE: since LOG_KERN == 0 this will also match
920: * messages with no facility specified.
921: */
922: if ((pri & LOG_FACMASK) == LOG_KERN && !KeepKernFac)
923: pri = LOG_MAKEPRI(LOG_USER, LOG_PRI(pri));
924:
925: q = line;
926:
927: while ((c = (unsigned char)*p++) != '\0' &&
928: q < &line[sizeof(line) - 4]) {
929: if (mask_C1 && (c & 0x80) && c < 0xA0) {
930: c &= 0x7F;
931: *q++ = 'M';
932: *q++ = '-';
933: }
934: if (isascii(c) && iscntrl(c)) {
935: if (c == '\n') {
936: *q++ = ' ';
937: } else if (c == '\t') {
938: *q++ = '\t';
939: } else {
940: *q++ = '^';
941: *q++ = c ^ 0100;
942: }
943: } else {
944: *q++ = c;
945: }
946: }
947: *q = '\0';
948:
949: logmsg(pri, line, hname, flags);
950: }
951:
952: /*
953: * Read /dev/klog while data are available, split into lines.
954: */
955: static void
956: readklog(void)
957: {
958: char *p, *q, line[MAXLINE + 1];
959: int len, i;
960:
961: len = 0;
962: for (;;) {
963: i = read(fklog, line + len, MAXLINE - 1 - len);
964: if (i > 0) {
965: line[i + len] = '\0';
966: } else {
967: if (i < 0 && errno != EINTR && errno != EAGAIN) {
968: logerror("klog");
969: fklog = -1;
970: }
971: break;
972: }
973:
974: for (p = line; (q = strchr(p, '\n')) != NULL; p = q + 1) {
975: *q = '\0';
976: printsys(p);
977: }
978: len = strlen(p);
979: if (len >= MAXLINE - 1) {
980: printsys(p);
981: len = 0;
982: }
983: if (len > 0)
984: memmove(line, p, len + 1);
985: }
986: if (len > 0)
987: printsys(line);
988: }
989:
990: /*
991: * Take a raw input line from /dev/klog, format similar to syslog().
992: */
993: static void
994: printsys(char *msg)
995: {
996: char *p, *q;
997: long n;
998: int flags, isprintf, pri;
999:
1000: flags = ISKERNEL | SYNC_FILE | ADDDATE; /* fsync after write */
1001: p = msg;
1002: pri = DEFSPRI;
1003: isprintf = 1;
1004: if (*p == '<') {
1005: errno = 0;
1006: n = strtol(p + 1, &q, 10);
1007: if (*q == '>' && n >= 0 && n < INT_MAX && errno == 0) {
1008: p = q + 1;
1009: pri = n;
1010: isprintf = 0;
1011: }
1012: }
1013: /*
1014: * Kernel printf's and LOG_CONSOLE messages have been displayed
1015: * on the console already.
1016: */
1017: if (isprintf || (pri & LOG_FACMASK) == LOG_CONSOLE)
1018: flags |= IGN_CONS;
1019: if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
1020: pri = DEFSPRI;
1021: logmsg(pri, p, LocalHostName, flags);
1022: }
1023:
1024: static time_t now;
1025:
1026: /*
1027: * Match a program or host name against a specification.
1028: * Return a non-0 value if the message must be ignored
1029: * based on the specification.
1030: */
1031: static int
1032: skip_message(const char *name, const char *spec, int checkcase)
1033: {
1034: const char *s;
1035: char prev, next;
1036: int exclude = 0;
1037: /* Behaviour on explicit match */
1038:
1039: if (spec == NULL)
1040: return 0;
1041: switch (*spec) {
1042: case '-':
1043: exclude = 1;
1044: /*FALLTHROUGH*/
1045: case '+':
1046: spec++;
1047: break;
1048: default:
1049: break;
1050: }
1051: if (checkcase)
1052: s = strstr (spec, name);
1053: else
1054: s = strcasestr (spec, name);
1055:
1056: if (s != NULL) {
1057: prev = (s == spec ? ',' : *(s - 1));
1058: next = *(s + strlen (name));
1059:
1060: if (prev == ',' && (next == '\0' || next == ','))
1061: /* Explicit match: skip iff the spec is an
1062: exclusive one. */
1063: return exclude;
1064: }
1065:
1066: /* No explicit match for this name: skip the message iff
1067: the spec is an inclusive one. */
1068: return !exclude;
1069: }
1070:
1071: /*
1072: * Log a message to the appropriate log files, users, etc. based on
1073: * the priority.
1074: */
1075: static void
1076: logmsg(int pri, const char *msg, const char *from, int flags)
1077: {
1078: struct filed *f;
1079: int i, fac, msglen, omask, prilev;
1080: const char *timestamp;
1081: char prog[NAME_MAX+1];
1082: char buf[MAXLINE+1];
1083:
1084: dprintf("logmsg: pri %o, flags %x, from %s, msg %s\n",
1085: pri, flags, from, msg);
1086:
1087: omask = sigblock(sigmask(SIGHUP)|sigmask(SIGALRM));
1088:
1089: /*
1090: * Check to see if msg looks non-standard.
1091: */
1092: msglen = strlen(msg);
1093: if (msglen < 16 || msg[3] != ' ' || msg[6] != ' ' ||
1094: msg[9] != ':' || msg[12] != ':' || msg[15] != ' ')
1095: flags |= ADDDATE;
1096:
1097: (void)time(&now);
1098: if (flags & ADDDATE) {
1099: timestamp = ctime(&now) + 4;
1100: } else {
1101: timestamp = msg;
1102: msg += 16;
1103: msglen -= 16;
1104: }
1105:
1106: /* skip leading blanks */
1107: while (isspace(*msg)) {
1108: msg++;
1109: msglen--;
1110: }
1111:
1112: /* extract facility and priority level */
1113: if (flags & MARK)
1114: fac = LOG_NFACILITIES;
1115: else
1116: fac = LOG_FAC(pri);
1117:
1118: /* Check maximum facility number. */
1119: if (fac > LOG_NFACILITIES) {
1120: (void)sigsetmask(omask);
1121: return;
1122: }
1123:
1124: prilev = LOG_PRI(pri);
1125:
1126: /* extract program name */
1127: for (i = 0; i < NAME_MAX; i++) {
1128: if (!isprint(msg[i]) || msg[i] == ':' || msg[i] == '[' ||
1129: msg[i] == '/' || isspace(msg[i]))
1130: break;
1131: prog[i] = msg[i];
1132: }
1133: prog[i] = 0;
1134:
1135: /* add kernel prefix for kernel messages */
1136: if (flags & ISKERNEL) {
1137: snprintf(buf, sizeof(buf), "%s: %s",
1138: use_bootfile ? bootfile : "kernel", msg);
1139: msg = buf;
1140: msglen = strlen(buf);
1141: }
1142:
1143: /* log the message to the particular outputs */
1144: if (!Initialized) {
1145: f = &consfile;
1146: /*
1147: * Open in non-blocking mode to avoid hangs during open
1148: * and close(waiting for the port to drain).
1149: */
1150: f->f_file = open(ctty, O_WRONLY | O_NONBLOCK, 0);
1151:
1152: if (f->f_file >= 0) {
1153: (void)strlcpy(f->f_lasttime, timestamp,
1154: sizeof(f->f_lasttime));
1155: fprintlog(f, flags, msg);
1156: (void)close(f->f_file);
1157: }
1158: (void)sigsetmask(omask);
1159: return;
1160: }
1161: for (f = Files; f; f = f->f_next) {
1162: /* skip messages that are incorrect priority */
1163: if (!(((f->f_pcmp[fac] & PRI_EQ) && (f->f_pmask[fac] == prilev))
1164: ||((f->f_pcmp[fac] & PRI_LT) && (f->f_pmask[fac] < prilev))
1165: ||((f->f_pcmp[fac] & PRI_GT) && (f->f_pmask[fac] > prilev))
1166: )
1167: || f->f_pmask[fac] == INTERNAL_NOPRI)
1168: continue;
1169:
1170: /* skip messages with the incorrect hostname */
1171: if (skip_message(from, f->f_host, 0))
1172: continue;
1173:
1174: /* skip messages with the incorrect program name */
1175: if (skip_message(prog, f->f_program, 1))
1176: continue;
1177:
1178: /* skip message to console if it has already been printed */
1179: if (f->f_type == F_CONSOLE && (flags & IGN_CONS))
1180: continue;
1181:
1182: /* don't output marks to recently written files */
1183: if ((flags & MARK) && (now - f->f_time) < MarkInterval / 2)
1184: continue;
1185:
1186: /*
1187: * suppress duplicate lines to this file
1188: */
1189: if (no_compress - (f->f_type != F_PIPE) < 1 &&
1190: (flags & MARK) == 0 && msglen == f->f_prevlen &&
1191: f->f_prevline && !strcmp(msg, f->f_prevline) &&
1192: !strcasecmp(from, f->f_prevhost)) {
1193: (void)strlcpy(f->f_lasttime, timestamp,
1194: sizeof(f->f_lasttime));
1195: f->f_prevcount++;
1196: dprintf("msg repeated %d times, %ld sec of %d\n",
1197: f->f_prevcount, (long)(now - f->f_time),
1198: repeatinterval[f->f_repeatcount]);
1199: /*
1200: * If domark would have logged this by now,
1201: * flush it now (so we don't hold isolated messages),
1202: * but back off so we'll flush less often
1203: * in the future.
1204: */
1205: if (now > REPEATTIME(f)) {
1206: fprintlog(f, flags, (char *)NULL);
1207: BACKOFF(f);
1208: }
1209: } else {
1210: /* new line, save it */
1211: if (f->f_prevcount)
1212: fprintlog(f, 0, (char *)NULL);
1213: f->f_repeatcount = 0;
1214: f->f_prevpri = pri;
1215: (void)strlcpy(f->f_lasttime, timestamp,
1216: sizeof(f->f_lasttime));
1217: (void)strlcpy(f->f_prevhost, from,
1218: sizeof(f->f_prevhost));
1219: if (msglen < MAXSVLINE) {
1220: f->f_prevlen = msglen;
1221: (void)strlcpy(f->f_prevline, msg, sizeof(f->f_prevline));
1222: fprintlog(f, flags, (char *)NULL);
1223: } else {
1224: f->f_prevline[0] = 0;
1225: f->f_prevlen = 0;
1226: fprintlog(f, flags, msg);
1227: }
1228: }
1229: }
1230: (void)sigsetmask(omask);
1231: }
1232:
1233: static void
1234: dofsync(void)
1235: {
1236: struct filed *f;
1237:
1238: for (f = Files; f; f = f->f_next) {
1239: if ((f->f_type == F_FILE) &&
1240: (f->f_flags & FFLAG_NEEDSYNC)) {
1241: f->f_flags &= ~FFLAG_NEEDSYNC;
1242: (void)fsync(f->f_file);
1243: }
1244: }
1245: }
1246:
1247: #define IOV_SIZE 7
1248: static void
1249: fprintlog(struct filed *f, int flags, const char *msg)
1250: {
1251: struct iovec iov[IOV_SIZE];
1252: struct iovec *v;
1253: struct addrinfo *r;
1254: int i, l, lsent = 0;
1255: char line[MAXLINE + 1], repbuf[80], greetings[200], *wmsg = NULL;
1256: char nul[] = "", space[] = " ", lf[] = "\n", crlf[] = "\r\n";
1257: const char *msgret;
1258:
1259: v = iov;
1260: if (f->f_type == F_WALL) {
1261: v->iov_base = greetings;
1262: /* The time displayed is not synchornized with the other log
1263: * destinations (like messages). Following fragment was using
1264: * ctime(&now), which was updating the time every 30 sec.
1265: * With f_lasttime, time is synchronized correctly.
1266: */
1267: v->iov_len = snprintf(greetings, sizeof greetings,
1268: "\r\n\7Message from syslogd@%s at %.24s ...\r\n",
1269: f->f_prevhost, f->f_lasttime);
1270: if (v->iov_len > 0)
1271: v++;
1272: v->iov_base = nul;
1273: v->iov_len = 0;
1274: v++;
1275: } else {
1276: v->iov_base = f->f_lasttime;
1277: v->iov_len = strlen(f->f_lasttime);
1278: v++;
1279: v->iov_base = space;
1280: v->iov_len = 1;
1281: v++;
1282: }
1283:
1284: if (LogFacPri) {
1285: static char fp_buf[30]; /* Hollow laugh */
1286: int fac = f->f_prevpri & LOG_FACMASK;
1287: int pri = LOG_PRI(f->f_prevpri);
1288: const char *f_s = NULL;
1289: char f_n[5]; /* Hollow laugh */
1290: const char *p_s = NULL;
1291: char p_n[5]; /* Hollow laugh */
1292:
1293: if (LogFacPri > 1) {
1294: CODE *c;
1295:
1296: for (c = facilitynames; c->c_name; c++) {
1297: if (c->c_val == fac) {
1298: f_s = c->c_name;
1299: break;
1300: }
1301: }
1302: for (c = prioritynames; c->c_name; c++) {
1303: if (c->c_val == pri) {
1304: p_s = c->c_name;
1305: break;
1306: }
1307: }
1308: }
1309: if (!f_s) {
1310: snprintf(f_n, sizeof f_n, "%d", LOG_FAC(fac));
1311: f_s = f_n;
1312: }
1313: if (!p_s) {
1314: snprintf(p_n, sizeof p_n, "%d", pri);
1315: p_s = p_n;
1316: }
1317: snprintf(fp_buf, sizeof fp_buf, "<%s.%s> ", f_s, p_s);
1318: v->iov_base = fp_buf;
1319: v->iov_len = strlen(fp_buf);
1320: } else {
1321: v->iov_base = nul;
1322: v->iov_len = 0;
1323: }
1324: v++;
1325:
1326: v->iov_base = f->f_prevhost;
1327: v->iov_len = strlen(v->iov_base);
1328: v++;
1329: v->iov_base = space;
1330: v->iov_len = 1;
1331: v++;
1332:
1333: if (msg) {
1334: wmsg = strdup(msg); /* XXX iov_base needs a `const' sibling. */
1335: if (wmsg == NULL) {
1336: logerror("strdup");
1337: exit(1);
1338: }
1339: v->iov_base = wmsg;
1340: v->iov_len = strlen(msg);
1341: } else if (f->f_prevcount > 1) {
1342: v->iov_base = repbuf;
1343: v->iov_len = snprintf(repbuf, sizeof repbuf,
1344: "last message repeated %d times", f->f_prevcount);
1345: } else if (f->f_prevline) {
1346: v->iov_base = f->f_prevline;
1347: v->iov_len = f->f_prevlen;
1348: } else {
1349: return;
1350: }
1351: v++;
1352:
1353: dprintf("Logging to %s", TypeNames[f->f_type]);
1354: f->f_time = now;
1355:
1356: switch (f->f_type) {
1357: int port;
1358: case F_UNUSED:
1359: dprintf("\n");
1360: break;
1361:
1362: case F_FORW:
1363: port = (int)ntohs(((struct sockaddr_in *)
1364: (f->f_un.f_forw.f_addr->ai_addr))->sin_port);
1365: if (port != 514) {
1366: dprintf(" %s:%d\n", f->f_un.f_forw.f_hname, port);
1367: } else {
1368: dprintf(" %s\n", f->f_un.f_forw.f_hname);
1369: }
1370: /* check for local vs remote messages */
1371: if (strcasecmp(f->f_prevhost, LocalHostName))
1372: l = snprintf(line, sizeof line - 1,
1373: "<%d>%.15s Forwarded from %s: %s",
1374: f->f_prevpri, (char *)iov[0].iov_base,
1375: f->f_prevhost, (char *)iov[5].iov_base);
1376: else
1377: l = snprintf(line, sizeof line - 1, "<%d>%.15s %s",
1378: f->f_prevpri, (char *)iov[0].iov_base,
1379: (char *)iov[5].iov_base);
1380: if (l < 0)
1381: l = 0;
1382: else if (l > MAXLINE)
1383: l = MAXLINE;
1384:
1385: if (finet) {
1386: for (r = f->f_un.f_forw.f_addr; r; r = r->ai_next) {
1387: for (i = 0; i < *finet; i++) {
1388: #if 0
1389: /*
1390: * should we check AF first, or just
1391: * trial and error? FWD
1392: */
1393: if (r->ai_family ==
1394: address_family_of(finet[i+1]))
1395: #endif
1396: lsent = sendto(finet[i+1], line, l, 0,
1397: r->ai_addr, r->ai_addrlen);
1398: if (lsent == l)
1399: break;
1400: }
1401: if (lsent == l && !send_to_all)
1402: break;
1403: }
1404: dprintf("lsent/l: %d/%d\n", lsent, l);
1405: if (lsent != l) {
1406: int e = errno;
1407: logerror("sendto");
1408: errno = e;
1409: switch (errno) {
1410: case ENOBUFS:
1411: case ENETDOWN:
1412: case EHOSTUNREACH:
1413: case EHOSTDOWN:
1414: break;
1415: /* case EBADF: */
1416: /* case EACCES: */
1417: /* case ENOTSOCK: */
1418: /* case EFAULT: */
1419: /* case EMSGSIZE: */
1420: /* case EAGAIN: */
1421: /* case ENOBUFS: */
1422: /* case ECONNREFUSED: */
1423: default:
1424: dprintf("removing entry\n");
1425: f->f_type = F_UNUSED;
1426: break;
1427: }
1428: }
1429: }
1430: break;
1431:
1432: case F_FILE:
1433: dprintf(" %s\n", f->f_un.f_fname);
1434: v->iov_base = lf;
1435: v->iov_len = 1;
1436: if (writev(f->f_file, iov, IOV_SIZE) < 0) {
1437: /*
1438: * If writev(2) fails for potentially transient errors
1439: * like the filesystem being full, ignore it.
1440: * Otherwise remove this logfile from the list.
1441: */
1442: if (errno != ENOSPC) {
1443: int e = errno;
1444: (void)close(f->f_file);
1445: f->f_type = F_UNUSED;
1446: errno = e;
1447: logerror(f->f_un.f_fname);
1448: }
1449: } else if ((flags & SYNC_FILE) && (f->f_flags & FFLAG_SYNC)) {
1450: f->f_flags |= FFLAG_NEEDSYNC;
1451: needdofsync = 1;
1452: }
1453: break;
1454:
1455: // CPP:
1456: case F_RING:
1457: dprintf(" %s\n", f->f_un.f_ring.f_rname);
1458: v->iov_base = "\n";
1459: v->iov_len = 1;
1460: if (rbwritev(f, iov, 7) == -1) {
1461: int e = errno;
1462:
1463: munmap(f->f_un.f_ring.f_footer, sizeof(struct clogFooter));
1464: close(f->f_file);
1465: f->f_type = F_UNUSED;
1466: errno = e;
1467: logerror(f->f_un.f_fname);
1468: }
1469: break;
1470: // CPP::
1471:
1472: case F_PIPE:
1473: dprintf(" %s\n", f->f_un.f_pipe.f_pname);
1474: v->iov_base = lf;
1475: v->iov_len = 1;
1476: if (f->f_un.f_pipe.f_pid == 0) {
1477: if ((f->f_file = p_open(f->f_un.f_pipe.f_pname,
1478: &f->f_un.f_pipe.f_pid)) < 0) {
1479: f->f_type = F_UNUSED;
1480: logerror(f->f_un.f_pipe.f_pname);
1481: break;
1482: }
1483: }
1484: if (writev(f->f_file, iov, IOV_SIZE) < 0) {
1485: int e = errno;
1486: (void)close(f->f_file);
1487: if (f->f_un.f_pipe.f_pid > 0)
1488: deadq_enter(f->f_un.f_pipe.f_pid,
1489: f->f_un.f_pipe.f_pname);
1490: f->f_un.f_pipe.f_pid = 0;
1491: errno = e;
1492: logerror(f->f_un.f_pipe.f_pname);
1493: }
1494: break;
1495:
1496: case F_CONSOLE:
1497: if (flags & IGN_CONS) {
1498: dprintf(" (ignored)\n");
1499: break;
1500: }
1501: /* FALLTHROUGH */
1502:
1503: case F_TTY:
1504: dprintf(" %s%s\n", _PATH_DEV, f->f_un.f_fname);
1505: v->iov_base = crlf;
1506: v->iov_len = 2;
1507:
1508: errno = 0; /* ttymsg() only sometimes returns an errno */
1509: if ((msgret = ttymsg(iov, IOV_SIZE, f->f_un.f_fname, 10))) {
1510: f->f_type = F_UNUSED;
1511: logerror(msgret);
1512: }
1513: break;
1514:
1515: case F_USERS:
1516: case F_WALL:
1517: dprintf("\n");
1518: v->iov_base = crlf;
1519: v->iov_len = 2;
1520: wallmsg(f, iov, IOV_SIZE);
1521: break;
1522: }
1523: f->f_prevcount = 0;
1524: free(wmsg);
1525: }
1526:
1527: /*
1528: * WALLMSG -- Write a message to the world at large
1529: *
1530: * Write the specified message to either the entire
1531: * world, or a list of approved users.
1532: */
1533: static void
1534: wallmsg(struct filed *f, struct iovec *iov, const int iovlen)
1535: {
1536: static int reenter; /* avoid calling ourselves */
1537: struct utmpx *ut;
1538: int i;
1539: const char *p;
1540:
1541: if (reenter++)
1542: return;
1543: setutxent();
1544: /* NOSTRICT */
1545: while ((ut = getutxent()) != NULL) {
1546: if (ut->ut_type != USER_PROCESS)
1547: continue;
1548: if (f->f_type == F_WALL) {
1549: if ((p = ttymsg(iov, iovlen, ut->ut_line,
1550: TTYMSGTIME)) != NULL) {
1551: errno = 0; /* already in msg */
1552: logerror(p);
1553: }
1554: continue;
1555: }
1556: /* should we send the message to this user? */
1557: for (i = 0; i < MAXUNAMES; i++) {
1558: if (!f->f_un.f_uname[i][0])
1559: break;
1560: if (!strcmp(f->f_un.f_uname[i], ut->ut_user)) {
1561: if ((p = ttymsg(iov, iovlen, ut->ut_line,
1562: TTYMSGTIME)) != NULL) {
1563: errno = 0; /* already in msg */
1564: logerror(p);
1565: }
1566: break;
1567: }
1568: }
1569: }
1570: endutxent();
1571: reenter = 0;
1572: }
1573:
1574: static void
1575: reapchild(int signo __unused)
1576: {
1577: int status;
1578: pid_t pid;
1579: struct filed *f;
1580:
1581: while ((pid = wait3(&status, WNOHANG, (struct rusage *)NULL)) > 0) {
1582: if (!Initialized)
1583: /* Don't tell while we are initting. */
1584: continue;
1585:
1586: /* First, look if it's a process from the dead queue. */
1587: if (deadq_remove(pid))
1588: goto oncemore;
1589:
1590: /* Now, look in list of active processes. */
1591: for (f = Files; f; f = f->f_next)
1592: if (f->f_type == F_PIPE &&
1593: f->f_un.f_pipe.f_pid == pid) {
1594: (void)close(f->f_file);
1595: f->f_un.f_pipe.f_pid = 0;
1596: log_deadchild(pid, status,
1597: f->f_un.f_pipe.f_pname);
1598: break;
1599: }
1600: oncemore:
1601: continue;
1602: }
1603: }
1604:
1605: /*
1606: * Return a printable representation of a host address.
1607: */
1608: static const char *
1609: cvthname(struct sockaddr *f)
1610: {
1611: int error, hl;
1612: sigset_t omask, nmask;
1613: static char hname[NI_MAXHOST], ip[NI_MAXHOST];
1614:
1615: error = getnameinfo((struct sockaddr *)f,
1616: ((struct sockaddr *)f)->sa_len,
1617: ip, sizeof ip, NULL, 0, NI_NUMERICHOST);
1618: dprintf("cvthname(%s)\n", ip);
1619:
1620: if (error) {
1621: dprintf("Malformed from address %s\n", gai_strerror(error));
1622: return ("???");
1623: }
1624: if (!resolve)
1625: return (ip);
1626:
1627: sigemptyset(&nmask);
1628: sigaddset(&nmask, SIGHUP);
1629: sigprocmask(SIG_BLOCK, &nmask, &omask);
1630: error = getnameinfo((struct sockaddr *)f,
1631: ((struct sockaddr *)f)->sa_len,
1632: hname, sizeof hname, NULL, 0, NI_NAMEREQD);
1633: sigprocmask(SIG_SETMASK, &omask, NULL);
1634: if (error) {
1635: dprintf("Host name for your address (%s) unknown\n", ip);
1636: return (ip);
1637: }
1638: hl = strlen(hname);
1639: if (hl > 0 && hname[hl-1] == '.')
1640: hname[--hl] = '\0';
1641: trimdomain(hname, hl);
1642: return (hname);
1643: }
1644:
1645: static void
1646: dodie(int signo)
1647: {
1648:
1649: WantDie = signo;
1650: }
1651:
1652: static void
1653: domark(int signo __unused)
1654: {
1655:
1656: MarkSet = 1;
1657: }
1658:
1659: /*
1660: * Print syslogd errors some place.
1661: */
1662: static void
1663: logerror(const char *type)
1664: {
1665: char buf[512];
1666: static int recursed = 0;
1667:
1668: /* If there's an error while trying to log an error, give up. */
1669: if (recursed)
1670: return;
1671: recursed++;
1672: if (errno)
1673: (void)snprintf(buf,
1674: sizeof buf, "syslogd: %s: %s", type, strerror(errno));
1675: else
1676: (void)snprintf(buf, sizeof buf, "syslogd: %s", type);
1677: errno = 0;
1678: dprintf("%s\n", buf);
1679: logmsg(LOG_SYSLOG|LOG_ERR, buf, LocalHostName, ADDDATE);
1680: recursed--;
1681: }
1682:
1683: static void
1684: die(int signo)
1685: {
1686: struct filed *f;
1687: struct funix *fx;
1688: int was_initialized;
1689: char buf[100];
1690:
1691: was_initialized = Initialized;
1692: Initialized = 0; /* Don't log SIGCHLDs. */
1693: for (f = Files; f != NULL; f = f->f_next) {
1694: /* flush any pending output */
1695: if (f->f_prevcount)
1696: fprintlog(f, 0, (char *)NULL);
1697: if (f->f_type == F_PIPE && f->f_un.f_pipe.f_pid > 0) {
1698: (void)close(f->f_file);
1699: f->f_un.f_pipe.f_pid = 0;
1700: }
1701: }
1702: Initialized = was_initialized;
1703: if (signo) {
1704: dprintf("syslogd: exiting on signal %d\n", signo);
1705: (void)snprintf(buf, sizeof(buf), "exiting on signal %d", signo);
1706: errno = 0;
1707: logerror(buf);
1708: }
1709: STAILQ_FOREACH(fx, &funixes, next)
1710: (void)unlink(fx->name);
1711: pidfile_remove(pfh);
1712:
1713: exit(1);
1714: }
1715:
1716: /*
1717: * INIT -- Initialize syslogd from configuration table
1718: */
1719: static void
1720: init(int signo)
1721: {
1722: int i;
1723: FILE *cf;
1724: struct filed *f, *next, **nextp;
1725: char *p;
1726: char cline[LINE_MAX];
1727: char prog[NAME_MAX+1];
1728: char host[MAXHOSTNAMELEN];
1729: char oldLocalHostName[MAXHOSTNAMELEN];
1730: char hostMsg[2*MAXHOSTNAMELEN+40];
1731: char bootfileMsg[LINE_MAX];
1732:
1733: dprintf("init\n");
1734:
1735: /*
1736: * Load hostname (may have changed).
1737: */
1738: if (signo != 0)
1739: (void)strlcpy(oldLocalHostName, LocalHostName,
1740: sizeof(oldLocalHostName));
1741: if (gethostname(LocalHostName, sizeof(LocalHostName)))
1742: err(EX_OSERR, "gethostname() failed");
1743: if ((p = strchr(LocalHostName, '.')) != NULL) {
1744: *p++ = '\0';
1745: LocalDomain = p;
1746: } else {
1747: LocalDomain = "";
1748: }
1749:
1750: /*
1751: * Close all open log files.
1752: */
1753: Initialized = 0;
1754: for (f = Files; f != NULL; f = next) {
1755: /* flush any pending output */
1756: if (f->f_prevcount)
1757: fprintlog(f, 0, (char *)NULL);
1758:
1759: switch (f->f_type) {
1760: case F_FILE:
1761: case F_FORW:
1762: case F_CONSOLE:
1763: case F_TTY:
1764: (void)close(f->f_file);
1765: break;
1766: case F_PIPE:
1767: if (f->f_un.f_pipe.f_pid > 0) {
1768: (void)close(f->f_file);
1769: deadq_enter(f->f_un.f_pipe.f_pid,
1770: f->f_un.f_pipe.f_pname);
1771: }
1772: f->f_un.f_pipe.f_pid = 0;
1773: break;
1774: // CPP:
1775: case F_RING:
1776: munmap(f->f_un.f_ring.f_footer, sizeof(struct clogFooter));
1777: close(f->f_file);
1778: break;
1779: // CPP::
1780: }
1781: next = f->f_next;
1782: if (f->f_program) free(f->f_program);
1783: if (f->f_host) free(f->f_host);
1784: free((char *)f);
1785: }
1786: Files = NULL;
1787: nextp = &Files;
1788:
1789: /* open the configuration file */
1790: if ((cf = fopen(ConfFile, "r")) == NULL) {
1791: dprintf("cannot open %s\n", ConfFile);
1792: *nextp = (struct filed *)calloc(1, sizeof(*f));
1793: if (*nextp == NULL) {
1794: logerror("calloc");
1795: exit(1);
1796: }
1797: cfline("*.ERR\t/dev/console", *nextp, "*", "*");
1798: (*nextp)->f_next = (struct filed *)calloc(1, sizeof(*f));
1799: if ((*nextp)->f_next == NULL) {
1800: logerror("calloc");
1801: exit(1);
1802: }
1803: cfline("*.PANIC\t*", (*nextp)->f_next, "*", "*");
1804: Initialized = 1;
1805: return;
1806: }
1807:
1808: /*
1809: * Foreach line in the conf table, open that file.
1810: */
1811: f = NULL;
1812: (void)strlcpy(host, "*", sizeof(host));
1813: (void)strlcpy(prog, "*", sizeof(prog));
1814: while (fgets(cline, sizeof(cline), cf) != NULL) {
1815: /*
1816: * check for end-of-section, comments, strip off trailing
1817: * spaces and newline character. #!prog is treated specially:
1818: * following lines apply only to that program.
1819: */
1820: for (p = cline; isspace(*p); ++p)
1821: continue;
1822: if (*p == 0)
1823: continue;
1824: if (*p == '#') {
1825: p++;
1826: if (*p != '!' && *p != '+' && *p != '-')
1827: continue;
1828: }
1829: if (*p == '+' || *p == '-') {
1830: host[0] = *p++;
1831: while (isspace(*p))
1832: p++;
1833: if ((!*p) || (*p == '*')) {
1834: (void)strlcpy(host, "*", sizeof(host));
1835: continue;
1836: }
1837: if (*p == '@')
1838: p = LocalHostName;
1839: for (i = 1; i < MAXHOSTNAMELEN - 1; i++) {
1840: if (!isalnum(*p) && *p != '.' && *p != '-'
1841: && *p != ',' && *p != ':' && *p != '%')
1842: break;
1843: host[i] = *p++;
1844: }
1845: host[i] = '\0';
1846: continue;
1847: }
1848: if (*p == '!') {
1849: p++;
1850: while (isspace(*p)) p++;
1851: if ((!*p) || (*p == '*')) {
1852: (void)strlcpy(prog, "*", sizeof(prog));
1853: continue;
1854: }
1855: for (i = 0; i < NAME_MAX; i++) {
1856: if (!isprint(p[i]) || isspace(p[i]))
1857: break;
1858: prog[i] = p[i];
1859: }
1860: prog[i] = 0;
1861: continue;
1862: }
1863: for (p = cline + 1; *p != '\0'; p++) {
1864: if (*p != '#')
1865: continue;
1866: if (*(p - 1) == '\\') {
1867: strcpy(p - 1, p);
1868: p--;
1869: continue;
1870: }
1871: *p = '\0';
1872: break;
1873: }
1874: for (i = strlen(cline) - 1; i >= 0 && isspace(cline[i]); i--)
1875: cline[i] = '\0';
1876: f = (struct filed *)calloc(1, sizeof(*f));
1877: if (f == NULL) {
1878: logerror("calloc");
1879: exit(1);
1880: }
1881: *nextp = f;
1882: nextp = &f->f_next;
1883: cfline(cline, f, prog, host);
1884: }
1885:
1886: /* close the configuration file */
1887: (void)fclose(cf);
1888:
1889: Initialized = 1;
1890:
1891: if (Debug) {
1892: int port;
1893: for (f = Files; f; f = f->f_next) {
1894: for (i = 0; i <= LOG_NFACILITIES; i++)
1895: if (f->f_pmask[i] == INTERNAL_NOPRI)
1896: printf("X ");
1897: else
1898: printf("%d ", f->f_pmask[i]);
1899: printf("%s: ", TypeNames[f->f_type]);
1900: switch (f->f_type) {
1901: case F_FILE:
1902: printf("%s", f->f_un.f_fname);
1903: break;
1904:
1905: case F_CONSOLE:
1906: case F_TTY:
1907: printf("%s%s", _PATH_DEV, f->f_un.f_fname);
1908: break;
1909:
1910: case F_FORW:
1911: port = (int)ntohs(((struct sockaddr_in *)
1912: (f->f_un.f_forw.f_addr->ai_addr))->sin_port);
1913: if (port != 514) {
1914: printf("%s:%d",
1915: f->f_un.f_forw.f_hname, port);
1916: } else {
1917: printf("%s", f->f_un.f_forw.f_hname);
1918: }
1919: break;
1920:
1921: // CPP:
1922: case F_RING:
1923: printf("%s", f->f_un.f_ring.f_rname);
1924: break;
1925: // CPP::
1926:
1927: case F_PIPE:
1928: printf("%s", f->f_un.f_pipe.f_pname);
1929: break;
1930:
1931: case F_USERS:
1932: for (i = 0; i < MAXUNAMES && *f->f_un.f_uname[i]; i++)
1933: printf("%s, ", f->f_un.f_uname[i]);
1934: break;
1935: }
1936: if (f->f_program)
1937: printf(" (%s)", f->f_program);
1938: printf("\n");
1939: }
1940: }
1941:
1942: logmsg(LOG_SYSLOG|LOG_INFO, "syslogd: restart", LocalHostName, ADDDATE);
1943: dprintf("syslogd: restarted\n");
1944: /*
1945: * Log a change in hostname, but only on a restart.
1946: */
1947: if (signo != 0 && strcmp(oldLocalHostName, LocalHostName) != 0) {
1948: (void)snprintf(hostMsg, sizeof(hostMsg),
1949: "syslogd: hostname changed, \"%s\" to \"%s\"",
1950: oldLocalHostName, LocalHostName);
1951: logmsg(LOG_SYSLOG|LOG_INFO, hostMsg, LocalHostName, ADDDATE);
1952: dprintf("%s\n", hostMsg);
1953: }
1954: /*
1955: * Log the kernel boot file if we aren't going to use it as
1956: * the prefix, and if this is *not* a restart.
1957: */
1958: if (signo == 0 && !use_bootfile) {
1959: (void)snprintf(bootfileMsg, sizeof(bootfileMsg),
1960: "syslogd: kernel boot file is %s", bootfile);
1961: logmsg(LOG_KERN|LOG_INFO, bootfileMsg, LocalHostName, ADDDATE);
1962: dprintf("%s\n", bootfileMsg);
1963: }
1964: }
1965:
1966: /*
1967: * Crack a configuration file line
1968: */
1969: static void
1970: cfline(const char *line, struct filed *f, const char *prog, const char *host)
1971: {
1972: struct addrinfo hints, *res;
1973: int error, i, pri, syncfile;
1974: const char *p, *q;
1975: char *bp;
1976: char buf[MAXLINE], ebuf[100];
1977: struct stat sb;
1978:
1979: dprintf("cfline(\"%s\", f, \"%s\", \"%s\")\n", line, prog, host);
1980:
1981: errno = 0; /* keep strerror() stuff out of logerror messages */
1982:
1983: /* clear out file entry */
1984: memset(f, 0, sizeof(*f));
1985: for (i = 0; i <= LOG_NFACILITIES; i++)
1986: f->f_pmask[i] = INTERNAL_NOPRI;
1987:
1988: /* save hostname if any */
1989: if (host && *host == '*')
1990: host = NULL;
1991: if (host) {
1992: int hl;
1993:
1994: f->f_host = strdup(host);
1995: if (f->f_host == NULL) {
1996: logerror("strdup");
1997: exit(1);
1998: }
1999: hl = strlen(f->f_host);
2000: if (hl > 0 && f->f_host[hl-1] == '.')
2001: f->f_host[--hl] = '\0';
2002: trimdomain(f->f_host, hl);
2003: }
2004:
2005: /* save program name if any */
2006: if (prog && *prog == '*')
2007: prog = NULL;
2008: if (prog) {
2009: f->f_program = strdup(prog);
2010: if (f->f_program == NULL) {
2011: logerror("strdup");
2012: exit(1);
2013: }
2014: }
2015:
2016: /* scan through the list of selectors */
2017: for (p = line; *p && *p != '\t' && *p != ' ';) {
2018: int pri_done;
2019: int pri_cmp;
2020: int pri_invert;
2021:
2022: /* find the end of this facility name list */
2023: for (q = p; *q && *q != '\t' && *q != ' ' && *q++ != '.'; )
2024: continue;
2025:
2026: /* get the priority comparison */
2027: pri_cmp = 0;
2028: pri_done = 0;
2029: pri_invert = 0;
2030: if (*q == '!') {
2031: pri_invert = 1;
2032: q++;
2033: }
2034: while (!pri_done) {
2035: switch (*q) {
2036: case '<':
2037: pri_cmp |= PRI_LT;
2038: q++;
2039: break;
2040: case '=':
2041: pri_cmp |= PRI_EQ;
2042: q++;
2043: break;
2044: case '>':
2045: pri_cmp |= PRI_GT;
2046: q++;
2047: break;
2048: default:
2049: pri_done++;
2050: break;
2051: }
2052: }
2053:
2054: /* collect priority name */
2055: for (bp = buf; *q && !strchr("\t,; ", *q); )
2056: *bp++ = *q++;
2057: *bp = '\0';
2058:
2059: /* skip cruft */
2060: while (strchr(",;", *q))
2061: q++;
2062:
2063: /* decode priority name */
2064: if (*buf == '*') {
2065: pri = LOG_PRIMASK;
2066: pri_cmp = PRI_LT | PRI_EQ | PRI_GT;
2067: } else {
2068: /* Ignore trailing spaces. */
2069: for (i = strlen(buf) - 1; i >= 0 && buf[i] == ' '; i--)
2070: buf[i] = '\0';
2071:
2072: pri = decode(buf, prioritynames);
2073: if (pri < 0) {
2074: (void)snprintf(ebuf, sizeof ebuf,
2075: "unknown priority name \"%s\"", buf);
2076: logerror(ebuf);
2077: return;
2078: }
2079: }
2080: if (!pri_cmp)
2081: pri_cmp = (UniquePriority)
2082: ? (PRI_EQ)
2083: : (PRI_EQ | PRI_GT)
2084: ;
2085: if (pri_invert)
2086: pri_cmp ^= PRI_LT | PRI_EQ | PRI_GT;
2087:
2088: /* scan facilities */
2089: while (*p && !strchr("\t.; ", *p)) {
2090: for (bp = buf; *p && !strchr("\t,;. ", *p); )
2091: *bp++ = *p++;
2092: *bp = '\0';
2093:
2094: if (*buf == '*') {
2095: for (i = 0; i < LOG_NFACILITIES; i++) {
2096: f->f_pmask[i] = pri;
2097: f->f_pcmp[i] = pri_cmp;
2098: }
2099: } else {
2100: i = decode(buf, facilitynames);
2101: if (i < 0) {
2102: (void)snprintf(ebuf, sizeof ebuf,
2103: "unknown facility name \"%s\"",
2104: buf);
2105: logerror(ebuf);
2106: return;
2107: }
2108: f->f_pmask[i >> 3] = pri;
2109: f->f_pcmp[i >> 3] = pri_cmp;
2110: }
2111: while (*p == ',' || *p == ' ')
2112: p++;
2113: }
2114:
2115: p = q;
2116: }
2117:
2118: /* skip to action part */
2119: while (*p == '\t' || *p == ' ')
2120: p++;
2121:
2122: if (*p == '-') {
2123: syncfile = 0;
2124: p++;
2125: } else
2126: syncfile = 1;
2127:
2128: switch (*p) {
2129: case '@':
2130: {
2131: char *tp;
2132: /*
2133: * scan forward to see if there is a port defined.
2134: * so we can't use strlcpy..
2135: */
2136: i = sizeof(f->f_un.f_forw.f_hname);
2137: tp = f->f_un.f_forw.f_hname;
2138: p++;
2139:
2140: while (*p && (*p != ':') && (i-- > 0)) {
2141: *tp++ = *p++;
2142: }
2143: *tp = '\0';
2144: }
2145: /* See if we copied a domain and have a port */
2146: if (*p == ':')
2147: p++;
2148: else
2149: p = NULL;
2150:
2151: memset(&hints, 0, sizeof(hints));
2152: hints.ai_family = family;
2153: hints.ai_socktype = SOCK_DGRAM;
2154: error = getaddrinfo(f->f_un.f_forw.f_hname,
2155: p ? p : "syslog", &hints, &res);
2156: if (error) {
2157: logerror(gai_strerror(error));
2158: break;
2159: }
2160: f->f_un.f_forw.f_addr = res;
2161: f->f_type = F_FORW;
2162: break;
2163:
2164: case '/':
2165: if ((f->f_file = open(p, logflags, 0600)) < 0) {
2166: f->f_type = F_UNUSED;
2167: logerror(p);
2168: break;
2169: }
2170: if (syncfile)
2171: f->f_flags |= FFLAG_SYNC;
2172: if (isatty(f->f_file)) {
2173: if (strcmp(p, ctty) == 0)
2174: f->f_type = F_CONSOLE;
2175: else
2176: f->f_type = F_TTY;
2177: (void)strlcpy(f->f_un.f_fname, p + sizeof(_PATH_DEV) - 1,
2178: sizeof(f->f_un.f_fname));
2179: } else {
2180: (void)strlcpy(f->f_un.f_fname, p, sizeof(f->f_un.f_fname));
2181: f->f_type = F_FILE;
2182: }
2183: break;
2184:
2185: // CPP:
2186: case '%':
2187: if ((f->f_file = open(p + 1, O_RDWR, 0)) < 0) {
2188: f->f_type = F_UNUSED;
2189: logerror(p + 1);
2190: break;
2191: }
2192: if (fstat(f->f_file, &sb) < 0) {
2193: close(f->f_file);
2194: f->f_type = F_UNUSED;
2195: logerror(p + 1);
2196: break;
2197: }
2198: f->f_un.f_ring.f_footer = mmap(NULL, sizeof(struct clogFooter), PROT_READ | PROT_WRITE, MAP_SHARED,
2199: f->f_file, sb.st_size - sizeof(struct clogFooter));
2200: if (!f->f_un.f_ring.f_footer) {
2201: close(f->f_file);
2202: f->f_type = F_UNUSED;
2203: logerror(p + 1);
2204: break;
2205: }
2206: if (memcmp(&(f->f_un.f_ring.f_footer->cf_magic), MAGIC, 4)) {
2207: munmap(f->f_un.f_ring.f_footer, sizeof(struct clogFooter));
2208: close(f->f_file);
2209: f->f_type = F_UNUSED;
2210: errno = ENODEV;
2211: logerror(p + 1);
2212: break;
2213: }
2214: f->f_un.f_ring.f_size = sb.st_size;
2215: strcpy(f->f_un.f_ring.f_rname, p + 1);
2216: f->f_type = F_RING;
2217: break;
2218: // CPP::
2219:
2220: case '|':
2221: f->f_un.f_pipe.f_pid = 0;
2222: (void)strlcpy(f->f_un.f_pipe.f_pname, p + 1,
2223: sizeof(f->f_un.f_pipe.f_pname));
2224: f->f_type = F_PIPE;
2225: break;
2226:
2227: case '*':
2228: f->f_type = F_WALL;
2229: break;
2230:
2231: default:
2232: for (i = 0; i < MAXUNAMES && *p; i++) {
2233: for (q = p; *q && *q != ','; )
2234: q++;
2235: (void)strncpy(f->f_un.f_uname[i], p, MAXLOGNAME - 1);
2236: if ((q - p) >= MAXLOGNAME)
2237: f->f_un.f_uname[i][MAXLOGNAME - 1] = '\0';
2238: else
2239: f->f_un.f_uname[i][q - p] = '\0';
2240: while (*q == ',' || *q == ' ')
2241: q++;
2242: p = q;
2243: }
2244: f->f_type = F_USERS;
2245: break;
2246: }
2247: }
2248:
2249:
2250: /*
2251: * Decode a symbolic name to a numeric value
2252: */
2253: static int
2254: decode(const char *name, CODE *codetab)
2255: {
2256: CODE *c;
2257: char *p, buf[40];
2258:
2259: if (isdigit(*name))
2260: return (atoi(name));
2261:
2262: for (p = buf; *name && p < &buf[sizeof(buf) - 1]; p++, name++) {
2263: if (isupper(*name))
2264: *p = tolower(*name);
2265: else
2266: *p = *name;
2267: }
2268: *p = '\0';
2269: for (c = codetab; c->c_name; c++)
2270: if (!strcmp(buf, c->c_name))
2271: return (c->c_val);
2272:
2273: return (-1);
2274: }
2275:
2276: static void
2277: markit(void)
2278: {
2279: struct filed *f;
2280: dq_t q, next;
2281:
2282: now = time((time_t *)NULL);
2283: MarkSeq += TIMERINTVL;
2284: if (MarkSeq >= MarkInterval) {
2285: logmsg(LOG_INFO, "-- MARK --",
2286: LocalHostName, ADDDATE|MARK);
2287: MarkSeq = 0;
2288: }
2289:
2290: for (f = Files; f; f = f->f_next) {
2291: if (f->f_prevcount && now >= REPEATTIME(f)) {
2292: dprintf("flush %s: repeated %d times, %d sec.\n",
2293: TypeNames[f->f_type], f->f_prevcount,
2294: repeatinterval[f->f_repeatcount]);
2295: fprintlog(f, 0, (char *)NULL);
2296: BACKOFF(f);
2297: }
2298: }
2299:
2300: /* Walk the dead queue, and see if we should signal somebody. */
2301: for (q = TAILQ_FIRST(&deadq_head); q != NULL; q = next) {
2302: next = TAILQ_NEXT(q, dq_entries);
2303:
2304: switch (q->dq_timeout) {
2305: case 0:
2306: /* Already signalled once, try harder now. */
2307: if (kill(q->dq_pid, SIGKILL) != 0)
2308: (void)deadq_remove(q->dq_pid);
2309: break;
2310:
2311: case 1:
2312: /*
2313: * Timed out on dead queue, send terminate
2314: * signal. Note that we leave the removal
2315: * from the dead queue to reapchild(), which
2316: * will also log the event (unless the process
2317: * didn't even really exist, in case we simply
2318: * drop it from the dead queue).
2319: */
2320: if (kill(q->dq_pid, SIGTERM) != 0)
2321: (void)deadq_remove(q->dq_pid);
2322: /* FALLTHROUGH */
2323:
2324: default:
2325: q->dq_timeout--;
2326: }
2327: }
2328: MarkSet = 0;
2329: (void)alarm(TIMERINTVL);
2330: }
2331:
2332: /*
2333: * fork off and become a daemon, but wait for the child to come online
2334: * before returing to the parent, or we get disk thrashing at boot etc.
2335: * Set a timer so we don't hang forever if it wedges.
2336: */
2337: static int
2338: waitdaemon(int nochdir, int noclose, int maxwait)
2339: {
2340: int fd;
2341: int status;
2342: pid_t pid, childpid;
2343:
2344: switch (childpid = fork()) {
2345: case -1:
2346: return (-1);
2347: case 0:
2348: break;
2349: default:
2350: signal(SIGALRM, timedout);
2351: alarm(maxwait);
2352: while ((pid = wait3(&status, 0, NULL)) != -1) {
2353: if (WIFEXITED(status))
2354: errx(1, "child pid %d exited with return code %d",
2355: pid, WEXITSTATUS(status));
2356: if (WIFSIGNALED(status))
2357: errx(1, "child pid %d exited on signal %d%s",
2358: pid, WTERMSIG(status),
2359: WCOREDUMP(status) ? " (core dumped)" :
2360: "");
2361: if (pid == childpid) /* it's gone... */
2362: break;
2363: }
2364: exit(0);
2365: }
2366:
2367: if (setsid() == -1)
2368: return (-1);
2369:
2370: if (!nochdir)
2371: (void)chdir("/");
2372:
2373: if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
2374: (void)dup2(fd, STDIN_FILENO);
2375: (void)dup2(fd, STDOUT_FILENO);
2376: (void)dup2(fd, STDERR_FILENO);
2377: if (fd > 2)
2378: (void)close (fd);
2379: }
2380: return (getppid());
2381: }
2382:
2383: /*
2384: * We get a SIGALRM from the child when it's running and finished doing it's
2385: * fsync()'s or O_SYNC writes for all the boot messages.
2386: *
2387: * We also get a signal from the kernel if the timer expires, so check to
2388: * see what happened.
2389: */
2390: static void
2391: timedout(int sig __unused)
2392: {
2393: int left;
2394: left = alarm(0);
2395: signal(SIGALRM, SIG_DFL);
2396: if (left == 0)
2397: errx(1, "timed out waiting for child");
2398: else
2399: _exit(0);
2400: }
2401:
2402: /*
2403: * Add `s' to the list of allowable peer addresses to accept messages
2404: * from.
2405: *
2406: * `s' is a string in the form:
2407: *
2408: * [*]domainname[:{servicename|portnumber|*}]
2409: *
2410: * or
2411: *
2412: * netaddr/maskbits[:{servicename|portnumber|*}]
2413: *
2414: * Returns -1 on error, 0 if the argument was valid.
2415: */
2416: static int
2417: allowaddr(char *s)
2418: {
2419: char *cp1, *cp2;
2420: struct allowedpeer ap;
2421: struct servent *se;
2422: int masklen = -1;
2423: struct addrinfo hints, *res;
2424: struct in_addr *addrp, *maskp;
2425: #ifdef INET6
2426: int i;
2427: u_int32_t *addr6p, *mask6p;
2428: #endif
2429: char ip[NI_MAXHOST];
2430:
2431: #ifdef INET6
2432: if (*s != '[' || (cp1 = strchr(s + 1, ']')) == NULL)
2433: #endif
2434: cp1 = s;
2435: if ((cp1 = strrchr(cp1, ':'))) {
2436: /* service/port provided */
2437: *cp1++ = '\0';
2438: if (strlen(cp1) == 1 && *cp1 == '*')
2439: /* any port allowed */
2440: ap.port = 0;
2441: else if ((se = getservbyname(cp1, "udp"))) {
2442: ap.port = ntohs(se->s_port);
2443: } else {
2444: ap.port = strtol(cp1, &cp2, 0);
2445: if (*cp2 != '\0')
2446: return (-1); /* port not numeric */
2447: }
2448: } else {
2449: if ((se = getservbyname("syslog", "udp")))
2450: ap.port = ntohs(se->s_port);
2451: else
2452: /* sanity, should not happen */
2453: ap.port = 514;
2454: }
2455:
2456: if ((cp1 = strchr(s, '/')) != NULL &&
2457: strspn(cp1 + 1, "0123456789") == strlen(cp1 + 1)) {
2458: *cp1 = '\0';
2459: if ((masklen = atoi(cp1 + 1)) < 0)
2460: return (-1);
2461: }
2462: #ifdef INET6
2463: if (*s == '[') {
2464: cp2 = s + strlen(s) - 1;
2465: if (*cp2 == ']') {
2466: ++s;
2467: *cp2 = '\0';
2468: } else {
2469: cp2 = NULL;
2470: }
2471: } else {
2472: cp2 = NULL;
2473: }
2474: #endif
2475: memset(&hints, 0, sizeof(hints));
2476: hints.ai_family = PF_UNSPEC;
2477: hints.ai_socktype = SOCK_DGRAM;
2478: hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
2479: if (getaddrinfo(s, NULL, &hints, &res) == 0) {
2480: ap.isnumeric = 1;
2481: memcpy(&ap.a_addr, res->ai_addr, res->ai_addrlen);
2482: memset(&ap.a_mask, 0, sizeof(ap.a_mask));
2483: ap.a_mask.ss_family = res->ai_family;
2484: if (res->ai_family == AF_INET) {
2485: ap.a_mask.ss_len = sizeof(struct sockaddr_in);
2486: maskp = &((struct sockaddr_in *)&ap.a_mask)->sin_addr;
2487: addrp = &((struct sockaddr_in *)&ap.a_addr)->sin_addr;
2488: if (masklen < 0) {
2489: /* use default netmask */
2490: if (IN_CLASSA(ntohl(addrp->s_addr)))
2491: maskp->s_addr = htonl(IN_CLASSA_NET);
2492: else if (IN_CLASSB(ntohl(addrp->s_addr)))
2493: maskp->s_addr = htonl(IN_CLASSB_NET);
2494: else
2495: maskp->s_addr = htonl(IN_CLASSC_NET);
2496: } else if (masklen <= 32) {
2497: /* convert masklen to netmask */
2498: if (masklen == 0)
2499: maskp->s_addr = 0;
2500: else
2501: maskp->s_addr = htonl(~((1 << (32 - masklen)) - 1));
2502: } else {
2503: freeaddrinfo(res);
2504: return (-1);
2505: }
2506: /* Lose any host bits in the network number. */
2507: addrp->s_addr &= maskp->s_addr;
2508: }
2509: #ifdef INET6
2510: else if (res->ai_family == AF_INET6 && masklen <= 128) {
2511: ap.a_mask.ss_len = sizeof(struct sockaddr_in6);
2512: if (masklen < 0)
2513: masklen = 128;
2514: mask6p = (u_int32_t *)&((struct sockaddr_in6 *)&ap.a_mask)->sin6_addr;
2515: /* convert masklen to netmask */
2516: while (masklen > 0) {
2517: if (masklen < 32) {
2518: *mask6p = htonl(~(0xffffffff >> masklen));
2519: break;
2520: }
2521: *mask6p++ = 0xffffffff;
2522: masklen -= 32;
2523: }
2524: /* Lose any host bits in the network number. */
2525: mask6p = (u_int32_t *)&((struct sockaddr_in6 *)&ap.a_mask)->sin6_addr;
2526: addr6p = (u_int32_t *)&((struct sockaddr_in6 *)&ap.a_addr)->sin6_addr;
2527: for (i = 0; i < 4; i++)
2528: addr6p[i] &= mask6p[i];
2529: }
2530: #endif
2531: else {
2532: freeaddrinfo(res);
2533: return (-1);
2534: }
2535: freeaddrinfo(res);
2536: } else {
2537: /* arg `s' is domain name */
2538: ap.isnumeric = 0;
2539: ap.a_name = s;
2540: if (cp1)
2541: *cp1 = '/';
2542: #ifdef INET6
2543: if (cp2) {
2544: *cp2 = ']';
2545: --s;
2546: }
2547: #endif
2548: }
2549:
2550: if (Debug) {
2551: printf("allowaddr: rule %d: ", NumAllowed);
2552: if (ap.isnumeric) {
2553: printf("numeric, ");
2554: getnameinfo((struct sockaddr *)&ap.a_addr,
2555: ((struct sockaddr *)&ap.a_addr)->sa_len,
2556: ip, sizeof ip, NULL, 0, NI_NUMERICHOST);
2557: printf("addr = %s, ", ip);
2558: getnameinfo((struct sockaddr *)&ap.a_mask,
2559: ((struct sockaddr *)&ap.a_mask)->sa_len,
2560: ip, sizeof ip, NULL, 0, NI_NUMERICHOST);
2561: printf("mask = %s; ", ip);
2562: } else {
2563: printf("domainname = %s; ", ap.a_name);
2564: }
2565: printf("port = %d\n", ap.port);
2566: }
2567:
2568: if ((AllowedPeers = realloc(AllowedPeers,
2569: ++NumAllowed * sizeof(struct allowedpeer)))
2570: == NULL) {
2571: logerror("realloc");
2572: exit(1);
2573: }
2574: memcpy(&AllowedPeers[NumAllowed - 1], &ap, sizeof(struct allowedpeer));
2575: return (0);
2576: }
2577:
2578: /*
2579: * Validate that the remote peer has permission to log to us.
2580: */
2581: static int
2582: validate(struct sockaddr *sa, const char *hname)
2583: {
2584: int i;
2585: size_t l1, l2;
2586: char *cp, name[NI_MAXHOST], ip[NI_MAXHOST], port[NI_MAXSERV];
2587: struct allowedpeer *ap;
2588: struct sockaddr_in *sin4, *a4p = NULL, *m4p = NULL;
2589: #ifdef INET6
2590: int j, reject;
2591: struct sockaddr_in6 *sin6, *a6p = NULL, *m6p = NULL;
2592: #endif
2593: struct addrinfo hints, *res;
2594: u_short sport;
2595:
2596: if (NumAllowed == 0)
2597: /* traditional behaviour, allow everything */
2598: return (1);
2599:
2600: (void)strlcpy(name, hname, sizeof(name));
2601: memset(&hints, 0, sizeof(hints));
2602: hints.ai_family = PF_UNSPEC;
2603: hints.ai_socktype = SOCK_DGRAM;
2604: hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
2605: if (getaddrinfo(name, NULL, &hints, &res) == 0)
2606: freeaddrinfo(res);
2607: else if (strchr(name, '.') == NULL) {
2608: strlcat(name, ".", sizeof name);
2609: strlcat(name, LocalDomain, sizeof name);
2610: }
2611: if (getnameinfo(sa, sa->sa_len, ip, sizeof ip, port, sizeof port,
2612: NI_NUMERICHOST | NI_NUMERICSERV) != 0)
2613: return (0); /* for safety, should not occur */
2614: dprintf("validate: dgram from IP %s, port %s, name %s;\n",
2615: ip, port, name);
2616: sport = atoi(port);
2617:
2618: /* now, walk down the list */
2619: for (i = 0, ap = AllowedPeers; i < NumAllowed; i++, ap++) {
2620: if (ap->port != 0 && ap->port != sport) {
2621: dprintf("rejected in rule %d due to port mismatch.\n", i);
2622: continue;
2623: }
2624:
2625: if (ap->isnumeric) {
2626: if (ap->a_addr.ss_family != sa->sa_family) {
2627: dprintf("rejected in rule %d due to address family mismatch.\n", i);
2628: continue;
2629: }
2630: if (ap->a_addr.ss_family == AF_INET) {
2631: sin4 = (struct sockaddr_in *)sa;
2632: a4p = (struct sockaddr_in *)&ap->a_addr;
2633: m4p = (struct sockaddr_in *)&ap->a_mask;
2634: if ((sin4->sin_addr.s_addr & m4p->sin_addr.s_addr)
2635: != a4p->sin_addr.s_addr) {
2636: dprintf("rejected in rule %d due to IP mismatch.\n", i);
2637: continue;
2638: }
2639: }
2640: #ifdef INET6
2641: else if (ap->a_addr.ss_family == AF_INET6) {
2642: sin6 = (struct sockaddr_in6 *)sa;
2643: a6p = (struct sockaddr_in6 *)&ap->a_addr;
2644: m6p = (struct sockaddr_in6 *)&ap->a_mask;
2645: if (a6p->sin6_scope_id != 0 &&
2646: sin6->sin6_scope_id != a6p->sin6_scope_id) {
2647: dprintf("rejected in rule %d due to scope mismatch.\n", i);
2648: continue;
2649: }
2650: reject = 0;
2651: for (j = 0; j < 16; j += 4) {
2652: if ((*(u_int32_t *)&sin6->sin6_addr.s6_addr[j] & *(u_int32_t *)&m6p->sin6_addr.s6_addr[j])
2653: != *(u_int32_t *)&a6p->sin6_addr.s6_addr[j]) {
2654: ++reject;
2655: break;
2656: }
2657: }
2658: if (reject) {
2659: dprintf("rejected in rule %d due to IP mismatch.\n", i);
2660: continue;
2661: }
2662: }
2663: #endif
2664: else
2665: continue;
2666: } else {
2667: cp = ap->a_name;
2668: l1 = strlen(name);
2669: if (*cp == '*') {
2670: /* allow wildmatch */
2671: cp++;
2672: l2 = strlen(cp);
2673: if (l2 > l1 || memcmp(cp, &name[l1 - l2], l2) != 0) {
2674: dprintf("rejected in rule %d due to name mismatch.\n", i);
2675: continue;
2676: }
2677: } else {
2678: /* exact match */
2679: l2 = strlen(cp);
2680: if (l2 != l1 || memcmp(cp, name, l1) != 0) {
2681: dprintf("rejected in rule %d due to name mismatch.\n", i);
2682: continue;
2683: }
2684: }
2685: }
2686: dprintf("accepted in rule %d.\n", i);
2687: return (1); /* hooray! */
2688: }
2689: return (0);
2690: }
2691:
2692: /*
2693: * Fairly similar to popen(3), but returns an open descriptor, as
2694: * opposed to a FILE *.
2695: */
2696: static int
2697: p_open(const char *prog, pid_t *rpid)
2698: {
2699: int pfd[2], nulldesc, i;
2700: pid_t pid;
2701: sigset_t omask, mask;
2702: char *argv[4]; /* sh -c cmd NULL */
2703: char errmsg[200];
2704:
2705: if (pipe(pfd) == -1)
2706: return (-1);
2707: if ((nulldesc = open(_PATH_DEVNULL, O_RDWR)) == -1)
2708: /* we are royally screwed anyway */
2709: return (-1);
2710:
2711: sigemptyset(&mask);
2712: sigaddset(&mask, SIGALRM);
2713: sigaddset(&mask, SIGHUP);
2714: sigprocmask(SIG_BLOCK, &mask, &omask);
2715: switch ((pid = fork())) {
2716: case -1:
2717: sigprocmask(SIG_SETMASK, &omask, 0);
2718: close(nulldesc);
2719: return (-1);
2720:
2721: case 0:
2722: argv[0] = strdup("sh");
2723: argv[1] = strdup("-c");
2724: argv[2] = strdup(prog);
2725: argv[3] = NULL;
2726: if (argv[0] == NULL || argv[1] == NULL || argv[2] == NULL) {
2727: logerror("strdup");
2728: exit(1);
2729: }
2730:
2731: alarm(0);
2732: (void)setsid(); /* Avoid catching SIGHUPs. */
2733:
2734: /*
2735: * Throw away pending signals, and reset signal
2736: * behaviour to standard values.
2737: */
2738: signal(SIGALRM, SIG_IGN);
2739: signal(SIGHUP, SIG_IGN);
2740: sigprocmask(SIG_SETMASK, &omask, 0);
2741: signal(SIGPIPE, SIG_DFL);
2742: signal(SIGQUIT, SIG_DFL);
2743: signal(SIGALRM, SIG_DFL);
2744: signal(SIGHUP, SIG_DFL);
2745:
2746: dup2(pfd[0], STDIN_FILENO);
2747: dup2(nulldesc, STDOUT_FILENO);
2748: dup2(nulldesc, STDERR_FILENO);
2749: for (i = getdtablesize(); i > 2; i--)
2750: (void)close(i);
2751:
2752: (void)execvp(_PATH_BSHELL, argv);
2753: _exit(255);
2754: }
2755:
2756: sigprocmask(SIG_SETMASK, &omask, 0);
2757: close(nulldesc);
2758: close(pfd[0]);
2759: /*
2760: * Avoid blocking on a hung pipe. With O_NONBLOCK, we are
2761: * supposed to get an EWOULDBLOCK on writev(2), which is
2762: * caught by the logic above anyway, which will in turn close
2763: * the pipe, and fork a new logging subprocess if necessary.
2764: * The stale subprocess will be killed some time later unless
2765: * it terminated itself due to closing its input pipe (so we
2766: * get rid of really dead puppies).
2767: */
2768: if (fcntl(pfd[1], F_SETFL, O_NONBLOCK) == -1) {
2769: /* This is bad. */
2770: (void)snprintf(errmsg, sizeof errmsg,
2771: "Warning: cannot change pipe to PID %d to "
2772: "non-blocking behaviour.",
2773: (int)pid);
2774: logerror(errmsg);
2775: }
2776: *rpid = pid;
2777: return (pfd[1]);
2778: }
2779:
2780: static void
2781: deadq_enter(pid_t pid, const char *name)
2782: {
2783: dq_t p;
2784: int status;
2785:
2786: /*
2787: * Be paranoid, if we can't signal the process, don't enter it
2788: * into the dead queue (perhaps it's already dead). If possible,
2789: * we try to fetch and log the child's status.
2790: */
2791: if (kill(pid, 0) != 0) {
2792: if (waitpid(pid, &status, WNOHANG) > 0)
2793: log_deadchild(pid, status, name);
2794: return;
2795: }
2796:
2797: p = malloc(sizeof(struct deadq_entry));
2798: if (p == NULL) {
2799: logerror("malloc");
2800: exit(1);
2801: }
2802:
2803: p->dq_pid = pid;
2804: p->dq_timeout = DQ_TIMO_INIT;
2805: TAILQ_INSERT_TAIL(&deadq_head, p, dq_entries);
2806: }
2807:
2808: static int
2809: deadq_remove(pid_t pid)
2810: {
2811: dq_t q;
2812:
2813: TAILQ_FOREACH(q, &deadq_head, dq_entries) {
2814: if (q->dq_pid == pid) {
2815: TAILQ_REMOVE(&deadq_head, q, dq_entries);
2816: free(q);
2817: return (1);
2818: }
2819: }
2820:
2821: return (0);
2822: }
2823:
2824: static void
2825: log_deadchild(pid_t pid, int status, const char *name)
2826: {
2827: int code;
2828: char buf[256];
2829: const char *reason;
2830:
2831: errno = 0; /* Keep strerror() stuff out of logerror messages. */
2832: if (WIFSIGNALED(status)) {
2833: reason = "due to signal";
2834: code = WTERMSIG(status);
2835: } else {
2836: reason = "with status";
2837: code = WEXITSTATUS(status);
2838: if (code == 0)
2839: return;
2840: }
2841: (void)snprintf(buf, sizeof buf,
2842: "Logging subprocess %d (%s) exited %s %d.",
2843: pid, name, reason, code);
2844: logerror(buf);
2845: }
2846:
2847: static int *
2848: socksetup(int af, char *bindhostname)
2849: {
2850: struct addrinfo hints, *res, *r;
2851: const char *bindservice;
2852: char *cp;
2853: int error, maxs, *s, *socks;
2854:
2855: /*
2856: * We have to handle this case for backwards compatibility:
2857: * If there are two (or more) colons but no '[' and ']',
2858: * assume this is an inet6 address without a service.
2859: */
2860: bindservice = "syslog";
2861: if (bindhostname != NULL) {
2862: #ifdef INET6
2863: if (*bindhostname == '[' &&
2864: (cp = strchr(bindhostname + 1, ']')) != NULL) {
2865: ++bindhostname;
2866: *cp = '\0';
2867: if (cp[1] == ':' && cp[2] != '\0')
2868: bindservice = cp + 2;
2869: } else {
2870: #endif
2871: cp = strchr(bindhostname, ':');
2872: if (cp != NULL && strchr(cp + 1, ':') == NULL) {
2873: *cp = '\0';
2874: if (cp[1] != '\0')
2875: bindservice = cp + 1;
2876: if (cp == bindhostname)
2877: bindhostname = NULL;
2878: }
2879: #ifdef INET6
2880: }
2881: #endif
2882: }
2883:
2884: memset(&hints, 0, sizeof(hints));
2885: hints.ai_flags = AI_PASSIVE;
2886: hints.ai_family = af;
2887: hints.ai_socktype = SOCK_DGRAM;
2888: error = getaddrinfo(bindhostname, bindservice, &hints, &res);
2889: if (error) {
2890: logerror(gai_strerror(error));
2891: errno = 0;
2892: die(0);
2893: }
2894:
2895: /* Count max number of sockets we may open */
2896: for (maxs = 0, r = res; r; r = r->ai_next, maxs++);
2897: socks = malloc((maxs+1) * sizeof(int));
2898: if (socks == NULL) {
2899: logerror("couldn't allocate memory for sockets");
2900: die(0);
2901: }
2902:
2903: *socks = 0; /* num of sockets counter at start of array */
2904: s = socks + 1;
2905: for (r = res; r; r = r->ai_next) {
2906: int on = 1;
2907: *s = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
2908: if (*s < 0) {
2909: logerror("socket");
2910: continue;
2911: }
2912: if (r->ai_family == AF_INET6) {
2913: if (setsockopt(*s, IPPROTO_IPV6, IPV6_V6ONLY,
2914: (char *)&on, sizeof (on)) < 0) {
2915: logerror("setsockopt");
2916: close(*s);
2917: continue;
2918: }
2919: }
2920: if (setsockopt(*s, SOL_SOCKET, SO_REUSEADDR,
2921: (char *)&on, sizeof (on)) < 0) {
2922: logerror("setsockopt");
2923: close(*s);
2924: continue;
2925: }
2926: if (bind(*s, r->ai_addr, r->ai_addrlen) < 0) {
2927: close(*s);
2928: logerror("bind");
2929: continue;
2930: }
2931:
2932: double_rbuf(*s);
2933:
2934: (*socks)++;
2935: s++;
2936: }
2937:
2938: if (*socks == 0) {
2939: free(socks);
2940: if (Debug)
2941: return (NULL);
2942: else
2943: die(0);
2944: }
2945: if (res)
2946: freeaddrinfo(res);
2947:
2948: return (socks);
2949: }
2950:
2951: static void
2952: double_rbuf(int fd)
2953: {
2954: socklen_t slen, len;
2955:
2956: if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &len, &slen) == 0) {
2957: len *= 2;
2958: setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &len, slen);
2959: }
2960: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>