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

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

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