Annotation of embedtools/src/syslogd.c, revision 1.1.2.5

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>