add xref to fsdb(8)
[unix-history] / usr / src / usr.sbin / syslogd / syslogd.c
index e8b93d0..cda0bdf 100644 (file)
@@ -11,11 +11,9 @@ char copyright[] =
 #endif not lint
 
 #ifndef lint
 #endif not lint
 
 #ifndef lint
-static char sccsid[] = "@(#)syslogd.c  5.3 (Berkeley) %G%";
+static char sccsid[] = "@(#)syslogd.c  5.19 (Berkeley) %G%";
 #endif not lint
 
 #endif not lint
 
-#define COMPAT         /* include 4.3 Alpha compatibility */
-
 /*
  *  syslogd -- log system messages
  *
 /*
  *  syslogd -- log system messages
  *
@@ -36,14 +34,15 @@ static char sccsid[] = "@(#)syslogd.c       5.3 (Berkeley) %G%";
  *
  * Author: Eric Allman
  * extensive changes by Ralph Campbell
  *
  * Author: Eric Allman
  * extensive changes by Ralph Campbell
+ * more extensive changes by Eric Allman (again)
  */
 
 #define        NLOGS           20              /* max number of log files */
 #define        MAXLINE         1024            /* maximum line length */
 #define DEFUPRI                (LOG_USER|LOG_NOTICE)
 #define DEFSPRI                (LOG_KERN|LOG_CRIT)
  */
 
 #define        NLOGS           20              /* max number of log files */
 #define        MAXLINE         1024            /* maximum line length */
 #define DEFUPRI                (LOG_USER|LOG_NOTICE)
 #define DEFSPRI                (LOG_KERN|LOG_CRIT)
+#define MARKCOUNT      10              /* ratio of minor to major marks */
 
 
-#include <syslog.h>
 #include <errno.h>
 #include <stdio.h>
 #include <utmp.h>
 #include <errno.h>
 #include <stdio.h>
 #include <utmp.h>
@@ -52,7 +51,9 @@ static char sccsid[] = "@(#)syslogd.c 5.3 (Berkeley) %G%";
 #include <sysexits.h>
 #include <strings.h>
 
 #include <sysexits.h>
 #include <strings.h>
 
+#include <sys/syslog.h>
 #include <sys/types.h>
 #include <sys/types.h>
+#include <sys/param.h>
 #include <sys/ioctl.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
 #include <sys/ioctl.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
@@ -81,6 +82,7 @@ char  ctty[] = "/dev/console";
 #define MAXFNAME       200     /* max file pathname length */
 
 #define NOPRI          0x10    /* the "no priority" priority */
 #define MAXFNAME       200     /* max file pathname length */
 
 #define NOPRI          0x10    /* the "no priority" priority */
+#define        LOG_MARK        (LOG_NFACILITIES << 3)  /* mark "facility" */
 
 /*
  * Flags to logmsg().
 
 /*
  * Flags to logmsg().
@@ -90,6 +92,7 @@ char  ctty[] = "/dev/console";
 #define SYNC_FILE      0x002   /* do fsync on file after printing */
 #define NOCOPY         0x004   /* don't suppress duplicate messages */
 #define ADDDATE                0x008   /* add a date to the message */
 #define SYNC_FILE      0x002   /* do fsync on file after printing */
 #define NOCOPY         0x004   /* don't suppress duplicate messages */
 #define ADDDATE                0x008   /* add a date to the message */
+#define MARK           0x010   /* this message is a mark */
 
 /*
  * This structure represents the files that will have log
 
 /*
  * This structure represents the files that will have log
@@ -99,16 +102,16 @@ char       ctty[] = "/dev/console";
 struct filed {
        short   f_type;                 /* entry type, see below */
        short   f_file;                 /* file descriptor */
 struct filed {
        short   f_type;                 /* entry type, see below */
        short   f_file;                 /* file descriptor */
-       u_char  f_pmask[LOG_NFACILITIES];       /* priority mask */
+       time_t  f_time;                 /* time this was last written */
+       u_char  f_pmask[LOG_NFACILITIES+1];     /* priority mask */
        union {
                char    f_uname[MAXUNAMES][UNAMESZ+1];
        union {
                char    f_uname[MAXUNAMES][UNAMESZ+1];
-               struct
-               {
-                       char    f_hname[32];
+               struct {
+                       char    f_hname[MAXHOSTNAMELEN+1];
                        struct sockaddr_in      f_addr;
                        struct sockaddr_in      f_addr;
-               }       f_forw;         /* forwarding address */
+               } f_forw;               /* forwarding address */
                char    f_fname[MAXFNAME];
                char    f_fname[MAXFNAME];
-       }       f_un;
+       } f_un;
 };
 
 /* values for f_type */
 };
 
 /* values for f_type */
@@ -128,63 +131,23 @@ char      *TypeNames[7] = {
 struct filed   Files[NLOGS];
 
 int    Debug;                  /* debug flag */
 struct filed   Files[NLOGS];
 
 int    Debug;                  /* debug flag */
-char   LocalHostName[32];      /* our hostname */
+char   LocalHostName[MAXHOSTNAMELEN+1];        /* our hostname */
+char   *LocalDomain;           /* our local domain name */
 int    InetInuse = 0;          /* non-zero if INET sockets are being used */
 int    LogPort;                /* port number for INET connections */
 char   PrevLine[MAXLINE + 1];  /* copy of last line to supress repeats */
 int    InetInuse = 0;          /* non-zero if INET sockets are being used */
 int    LogPort;                /* port number for INET connections */
 char   PrevLine[MAXLINE + 1];  /* copy of last line to supress repeats */
-char   PrevHost[32];           /* previous host */
+char   PrevHost[MAXHOSTNAMELEN+1];             /* previous host */
 int    PrevFlags;
 int    PrevPri;
 int    PrevCount = 0;          /* number of times seen */
 int    Initialized = 0;        /* set when we have initialized ourselves */
 int    PrevFlags;
 int    PrevPri;
 int    PrevCount = 0;          /* number of times seen */
 int    Initialized = 0;        /* set when we have initialized ourselves */
+int    MarkInterval = 20;      /* interval between marks in minutes */
+int    MarkSeq = 0;            /* mark sequence number */
 
 extern int errno, sys_nerr;
 extern char *sys_errlist[];
 extern char *ctime(), *index();
 
 
 extern int errno, sys_nerr;
 extern char *sys_errlist[];
 extern char *ctime(), *index();
 
-#ifdef COMPAT
-int    CompatMode = 0;         /* run in compatibility mode */
-int    CompatCodes[32] = {
-               LOG_USER|LOG_ALERT,     /* 0 -- undefined */
-                       /* kernel priorities */
-               LOG_KERN|LOG_EMERG,     /* KERN_EMERG */
-               LOG_KERN|LOG_ALERT,     /* KERN_ALERT */
-               LOG_KERN|LOG_CRIT,      /* KERN_ERR */
-               LOG_KERN|LOG_ERR,       /* KERN_FAIL */
-               LOG_KERN|LOG_WARNING,   /* KERN_RECOV */
-               LOG_KERN|LOG_INFO,      /* KERN_INFO */
-                       /* user abnormal conditions priorities */
-               LOG_USER|LOG_EMERG,     /* LOG_EMERG */
-               LOG_USER|LOG_ALERT,     /* LOG_ALERT */
-               LOG_USER|LOG_CRIT,      /* LOG_CRIT */
-               LOG_USER|LOG_ERR,       /* LOG_ERR */
-               LOG_USER|LOG_ERR,       /* LOG_ERR */
-               LOG_USER|LOG_WARNING,   /* LOG_WARNING */
-                       /* user priorities */
-               LOG_USER|LOG_ALERT,     /* LOG_SALERT */
-               LOG_AUTH|LOG_NOTICE,    /* LOG_SECURITY */
-               LOG_USER|LOG_INFO,      /* LOG_FIXED */
-               LOG_MAIL|LOG_ERR,       /* LOG_MAIL */
-               LOG_DAEMON|LOG_ERR,     /* LOG_REJECT */
-               LOG_USER|LOG_NOTICE,    /* LOG_NOTICE */
-                       /* user information priorities */
-               LOG_USER|LOG_INFO,      /* LOG_INFO */
-               LOG_LOCAL1|LOG_INFO,    /* LOG_INFO1 */
-               LOG_LOCAL2|LOG_INFO,    /* LOG_INFO2 */
-               LOG_LOCAL3|LOG_INFO,    /* LOG_INFO3 */
-               LOG_LOCAL4|LOG_INFO,    /* LOG_INFO4 */
-               LOG_LOCAL5|LOG_INFO,    /* LOG_INFO5 */
-                       /* user debug/local priorities */
-               LOG_USER|LOG_DEBUG,     /* LOG_DEBUG */
-               LOG_LOCAL1|LOG_DEBUG,   /* LOG_LOCAL1 */
-               LOG_LOCAL2|LOG_DEBUG,   /* LOG_LOCAL2 */
-               LOG_LOCAL3|LOG_DEBUG,   /* LOG_LOCAL3 */
-               LOG_LOCAL4|LOG_DEBUG,   /* LOG_LOCAL4 */
-               LOG_LOCAL5|LOG_DEBUG,   /* LOG_LOCAL5 */
-               LOG_LOCAL6|LOG_DEBUG,   /* LOG_LOCAL6 */
-};
-#endif COMPAT
-
 main(argc, argv)
        int argc;
        char **argv;
 main(argc, argv)
        int argc;
        char **argv;
@@ -192,7 +155,7 @@ main(argc, argv)
        register int i;
        register char *p;
        int funix, finet, inetm, fklog, klogm, len;
        register int i;
        register char *p;
        int funix, finet, inetm, fklog, klogm, len;
-       struct sockaddr_un sun, fromunix;
+       struct sockaddr_un sunx, fromunix;
        struct sockaddr_in sin, frominet;
        FILE *fp;
        char line[MSG_BSIZE + 1];
        struct sockaddr_in sin, frominet;
        FILE *fp;
        char line[MSG_BSIZE + 1];
@@ -212,17 +175,16 @@ main(argc, argv)
                        Debug++;
                        break;
 
                        Debug++;
                        break;
 
-#ifdef COMPAT
-               case 'C':               /* run in compat mode */
-                       CompatMode++;
-                       break;
-#endif COMPAT
-
                case 'p':               /* path */
                        if (p[2] != '\0')
                                LogName = &p[2];
                        break;
 
                case 'p':               /* path */
                        if (p[2] != '\0')
                                LogName = &p[2];
                        break;
 
+               case 'm':               /* mark interval */
+                       if (p[2] != '\0')
+                               MarkInterval = atoi(&p[2]);
+                       break;
+
                default:
                        usage();
                }
                default:
                        usage();
                }
@@ -240,17 +202,26 @@ main(argc, argv)
        } else
                setlinebuf(stdout);
 
        } else
                setlinebuf(stdout);
 
+       (void) gethostname(LocalHostName, sizeof LocalHostName);
+       if (p = index(LocalHostName, '.')) {
+               *p++ = '\0';
+               LocalDomain = p;
+       }
+       else
+               LocalDomain = "";
        (void) signal(SIGTERM, die);
        (void) signal(SIGTERM, die);
-       (void) signal(SIGINT, die);
+       (void) signal(SIGINT, Debug ? die : SIG_IGN);
+       (void) signal(SIGQUIT, Debug ? die : SIG_IGN);
        (void) signal(SIGCHLD, reapchild);
        (void) signal(SIGCHLD, reapchild);
+       (void) signal(SIGALRM, domark);
+       (void) alarm(MarkInterval * 60 / MARKCOUNT);
        (void) unlink(LogName);
 
        (void) unlink(LogName);
 
-       sun.sun_family = AF_UNIX;
-       (void) strncpy(sun.sun_path, LogName, sizeof sun.sun_path);
-       (void) gethostname(LocalHostName, sizeof LocalHostName);
+       sunx.sun_family = AF_UNIX;
+       (void) strncpy(sunx.sun_path, LogName, sizeof sunx.sun_path);
        funix = socket(AF_UNIX, SOCK_DGRAM, 0);
        funix = socket(AF_UNIX, SOCK_DGRAM, 0);
-       if (funix < 0 || bind(funix, (struct sockaddr *) &sun,
-           sizeof(sun.sun_family)+strlen(sun.sun_path)) < 0 ||
+       if (funix < 0 || bind(funix, (struct sockaddr *) &sunx,
+           sizeof(sunx.sun_family)+strlen(sunx.sun_path)) < 0 ||
            chmod(LogName, 0666) < 0) {
                (void) sprintf(line, "cannot create %s", LogName);
                logerror(line);
            chmod(LogName, 0666) < 0) {
                (void) sprintf(line, "cannot create %s", LogName);
                logerror(line);
@@ -301,7 +272,7 @@ main(argc, argv)
                int nfds, readfds = FDMASK(funix) | inetm | klogm;
 
                errno = 0;
                int nfds, readfds = FDMASK(funix) | inetm | klogm;
 
                errno = 0;
-               dprintf("readfds = %#x\n", readfds, funix, finet, fklog);
+               dprintf("readfds = %#x\n", readfds);
                nfds = select(20, (fd_set *) &readfds, (fd_set *) NULL,
                                  (fd_set *) NULL, (struct timeval *) NULL);
                dprintf("got a message (%d, %#x)\n", nfds, readfds);
                nfds = select(20, (fd_set *) &readfds, (fd_set *) NULL,
                                  (fd_set *) NULL, (struct timeval *) NULL);
                dprintf("got a message (%d, %#x)\n", nfds, readfds);
@@ -349,7 +320,7 @@ main(argc, argv)
 
 usage()
 {
 
 usage()
 {
-       fprintf(stderr, "usage: syslogd [-d] [-ppath] [-fconffile]\n");
+       fprintf(stderr, "usage: syslogd [-d] [-mmarkinterval] [-ppath] [-fconffile]\n");
        exit(1);
 }
 
        exit(1);
 }
 
@@ -398,25 +369,6 @@ printline(hname, msg)
                pri |= LOG_USER;
 
        q = line;
                pri |= LOG_USER;
 
        q = line;
-#ifdef COMPAT
-       if (CompatMode) {
-               register char *lp = index(p, ':');
-
-               if (lp && lp[1] == ' ' && lp[17] == '-' && lp[18] == '-') {
-                       /*
-                        * Old format message
-                        */
-                       dprintf("mapping <%d> to <%d>\n", pri, CompatCodes[pri]);
-                       pri = CompatCodes[pri];
-                       (void) strncpy(q, lp + 2, 15);
-                       q += 15;
-                       *q++ = ' ';
-                       (void) strncpy(q, p, lp - p + 1);
-                       q += lp - p + 1;
-                       p = lp + 19;
-               }
-       }
-#endif COMPAT
 
        while ((c = *p++ & 0177) != '\0' && c != '\n' &&
            q < &line[sizeof(line) - 1]) {
 
        while ((c = *p++ & 0177) != '\0' && c != '\n' &&
            q < &line[sizeof(line) - 1]) {
@@ -459,12 +411,6 @@ printsys(msg)
                                ++p;
                        if (pri <= 0 || pri >= (LOG_NFACILITIES << 3))
                                pri = DEFSPRI;
                                ++p;
                        if (pri <= 0 || pri >= (LOG_NFACILITIES << 3))
                                pri = DEFSPRI;
-#ifdef COMPAT
-                       else if (CompatMode) {
-                               dprintf("mapping <%d> to <%d>\n", pri, CompatCodes[pri]);
-                               pri = CompatCodes[pri];
-                       }
-#endif COMPAT
                } else {
                        /* kernel printf's come out on console */
                        flags |= IGN_CONS;
                } else {
                        /* kernel printf's come out on console */
                        flags |= IGN_CONS;
@@ -488,28 +434,28 @@ logmsg(pri, msg, from, flags)
        char *msg, *from;
        int flags;
 {
        char *msg, *from;
        int flags;
 {
-       char line[MAXLINE + 1];
        register struct filed *f;
        register int l;
        int fac, prilev;
        register struct filed *f;
        register int l;
        int fac, prilev;
+       time_t now;
+       int omask;
        struct iovec iov[6];
        register struct iovec *v = iov;
        struct iovec iov[6];
        register struct iovec *v = iov;
-       int omask;
+       char line[MAXLINE + 1];
 
        dprintf("logmsg: pri %o, flags %x, from %s, msg %s\n", pri, flags, from, msg);
 
 
        dprintf("logmsg: pri %o, flags %x, from %s, msg %s\n", pri, flags, from, msg);
 
-       omask = sigblock(sigmask(SIGALRM)|sigmask(SIGHUP));
+       omask = sigblock(sigmask(SIGHUP)|sigmask(SIGALRM));
 
        /*
         * Check to see if msg looks non-standard.
         */
 
        /*
         * Check to see if msg looks non-standard.
         */
-       if (!(flags & ADDDATE) && (strlen(msg) < 16 ||
-           msg[3] != ' ' || msg[6] != ' ' ||
-           msg[9] != ':' || msg[12] != ':' || msg[15] != ' '))
+       if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' ||
+           msg[9] != ':' || msg[12] != ':' || msg[15] != ' ')
                flags |= ADDDATE;
 
                flags |= ADDDATE;
 
-       if ((flags & NOCOPY) == 0) {
-               if (flags & ADDDATE)
+       if (!(flags & NOCOPY)) {
+               if (flags & (ADDDATE|MARK))
                        flushmsg();
                else if (!strcmp(msg + 16, PrevLine + 16)) {
                        /* we found a match, update the time */
                        flushmsg();
                else if (!strcmp(msg + 16, PrevLine + 16)) {
                        /* we found a match, update the time */
@@ -527,12 +473,10 @@ logmsg(pri, msg, from, flags)
                }
        }
 
                }
        }
 
-       if (flags & ADDDATE) {
-               time_t now;
-
-               (void) time(&now);
+       (void) time(&now);
+       if (flags & ADDDATE)
                v->iov_base = ctime(&now) + 4;
                v->iov_base = ctime(&now) + 4;
-       else
+       else
                v->iov_base = msg;
        v->iov_len = 15;
        v++;
                v->iov_base = msg;
        v->iov_len = 15;
        v++;
@@ -554,6 +498,8 @@ logmsg(pri, msg, from, flags)
 
        /* extract facility and priority level */
        fac = (pri & LOG_FACMASK) >> 3;
 
        /* extract facility and priority level */
        fac = (pri & LOG_FACMASK) >> 3;
+       if (flags & MARK)
+               fac = LOG_NFACILITIES;
        prilev = pri & LOG_PRIMASK;
 
        /* log the message to the particular outputs */
        prilev = pri & LOG_PRIMASK;
 
        /* log the message to the particular outputs */
@@ -566,6 +512,8 @@ logmsg(pri, msg, from, flags)
                        (void) writev(cfd, iov, 6);
                        (void) close(cfd);
                }
                        (void) writev(cfd, iov, 6);
                        (void) close(cfd);
                }
+               untty();
+               (void) sigsetmask(omask);
                return;
        }
        for (f = Files; f < &Files[NLOGS]; f++) {
                return;
        }
        for (f = Files; f < &Files[NLOGS]; f++) {
@@ -573,7 +521,12 @@ logmsg(pri, msg, from, flags)
                if (f->f_pmask[fac] < prilev || f->f_pmask[fac] == NOPRI)
                        continue;
 
                if (f->f_pmask[fac] < prilev || f->f_pmask[fac] == NOPRI)
                        continue;
 
+               /* don't output marks to recently written files */
+               if ((flags & MARK) && (now - f->f_time) < (MarkInterval * 60 / 2))
+                       continue;
+
                dprintf("Logging to %s", TypeNames[f->f_type]);
                dprintf("Logging to %s", TypeNames[f->f_type]);
+               f->f_time = now;
                switch (f->f_type) {
                case F_UNUSED:
                        dprintf("\n");
                switch (f->f_type) {
                case F_UNUSED:
                        dprintf("\n");
@@ -581,8 +534,8 @@ logmsg(pri, msg, from, flags)
 
                case F_FORW:
                        dprintf(" %s\n", f->f_un.f_forw.f_hname);
 
                case F_FORW:
                        dprintf(" %s\n", f->f_un.f_forw.f_hname);
-                       (void) sprintf(line, "<%d>%.15s %s", pri, v[0].iov_base,
-                               v[4].iov_base);
+                       (void) sprintf(line, "<%d>%.15s %s", pri,
+                               iov[0].iov_base, iov[4].iov_base);
                        l = strlen(line);
                        if (l > MAXLINE)
                                l = MAXLINE;
                        l = strlen(line);
                        if (l > MAXLINE)
                                l = MAXLINE;
@@ -625,6 +578,7 @@ logmsg(pri, msg, from, flags)
                                                f->f_type = F_UNUSED;
                                                logerror(f->f_un.f_fname);
                                        }
                                                f->f_type = F_UNUSED;
                                                logerror(f->f_un.f_fname);
                                        }
+                                       untty();
                                } else {
                                        f->f_type = F_UNUSED;
                                        errno = e;
                                } else {
                                        f->f_type = F_UNUSED;
                                        errno = e;
@@ -707,7 +661,7 @@ wallmsg(f, iov)
 
                /* compute the device name */
                p = "/dev/12345678";
 
                /* compute the device name */
                p = "/dev/12345678";
-               strcpyn(&p[5], ut.ut_line, UNAMESZ);
+               strncpy(&p[5], ut.ut_line, UNAMESZ);
 
                /*
                 * Might as well fork instead of using nonblocking I/O
 
                /*
                 * Might as well fork instead of using nonblocking I/O
@@ -717,17 +671,21 @@ wallmsg(f, iov)
                        if (f->f_type == F_WALL) {
                                iov[0].iov_base = greetings;
                                iov[0].iov_len = len;
                        if (f->f_type == F_WALL) {
                                iov[0].iov_base = greetings;
                                iov[0].iov_len = len;
+                               iov[1].iov_len = 0;
                        }
                        (void) signal(SIGALRM, SIG_DFL);
                        (void) alarm(30);
                        /* open the terminal */
                        ttyf = open(p, O_WRONLY);
                        }
                        (void) signal(SIGALRM, SIG_DFL);
                        (void) alarm(30);
                        /* open the terminal */
                        ttyf = open(p, O_WRONLY);
-                       if (ttyf >= 0)
-                               (void) writev(ttyf, iov, 6);
+                       if (ttyf >= 0) {
+                               struct stat statb;
+
+                               if (fstat(ttyf, &statb) == 0 &&
+                                   (statb.st_mode & S_IWRITE))
+                                       (void) writev(ttyf, iov, 6);
+                       }
                        exit(0);
                }
                        exit(0);
                }
-               /* avoid having them all pile up at once */
-               sleep(1);
        }
        /* close the user login file */
        (void) fclose(uf);
        }
        /* close the user login file */
        (void) fclose(uf);
@@ -750,6 +708,7 @@ cvthname(f)
        struct sockaddr_in *f;
 {
        struct hostent *hp;
        struct sockaddr_in *f;
 {
        struct hostent *hp;
+       register char *p;
        extern char *inet_ntoa();
 
        dprintf("cvthname(%s)\n", inet_ntoa(f->sin_addr));
        extern char *inet_ntoa();
 
        dprintf("cvthname(%s)\n", inet_ntoa(f->sin_addr));
@@ -764,9 +723,22 @@ cvthname(f)
                        inet_ntoa(f->sin_addr));
                return (inet_ntoa(f->sin_addr));
        }
                        inet_ntoa(f->sin_addr));
                return (inet_ntoa(f->sin_addr));
        }
+       if ((p = index(hp->h_name, '.')) && strcmp(p + 1, LocalDomain) == 0)
+               *p = '\0';
        return (hp->h_name);
 }
 
        return (hp->h_name);
 }
 
+domark()
+{
+       int pri;
+
+       if ((++MarkSeq % MARKCOUNT) == 0)
+               logmsg(LOG_INFO, "-- MARK --", LocalHostName, ADDDATE|MARK);
+       else
+               flushmsg();
+       alarm(MarkInterval * 60 / MARKCOUNT);
+}
+
 flushmsg()
 {
        if (PrevCount == 0)
 flushmsg()
 {
        if (PrevCount == 0)
@@ -794,17 +766,20 @@ logerror(type)
                (void) sprintf(buf, "syslogd: %s: %s", type, sys_errlist[errno]);
        errno = 0;
        dprintf("%s\n", buf);
                (void) sprintf(buf, "syslogd: %s: %s", type, sys_errlist[errno]);
        errno = 0;
        dprintf("%s\n", buf);
-       logmsg(LOG_DAEMON|LOG_ERR, buf, LocalHostName, ADDDATE);
+       logmsg(LOG_SYSLOG|LOG_ERR, buf, LocalHostName, ADDDATE);
 }
 
 die(sig)
 {
        char buf[100];
 
 }
 
 die(sig)
 {
        char buf[100];
 
-       dprintf("syslogd: going down on signal %d\n", sig);
-       flushmsg();
-       (void) sprintf(buf, "going down on signal %d", sig);
-       logerror(buf);
+       if (sig) {
+               dprintf("syslogd: going down on signal %d\n", sig);
+               flushmsg();
+               (void) sprintf(buf, "going down on signal %d", sig);
+               errno = 0;
+               logerror(buf);
+       }
        (void) unlink(LogName);
        exit(0);
 }
        (void) unlink(LogName);
        exit(0);
 }
@@ -829,10 +804,17 @@ init()
        /*
         *  Close all open log files.
         */
        /*
         *  Close all open log files.
         */
+       Initialized = 0;
        for (f = Files; f < &Files[NLOGS]; f++) {
        for (f = Files; f < &Files[NLOGS]; f++) {
-               if (f->f_type == F_FILE || f->f_type == F_TTY)
+               switch (f->f_type) {
+                 case F_FILE:
+                 case F_TTY:
+                 case F_FORW:
+                 case F_CONSOLE:
                        (void) close(f->f_file);
                        (void) close(f->f_file);
-               f->f_type = F_UNUSED;
+                       f->f_type = F_UNUSED;
+                       break;
+               }
        }
 
        /* open the configuration file */
        }
 
        /* open the configuration file */
@@ -867,7 +849,7 @@ init()
 
        if (Debug) {
                for (f = Files; f < &Files[NLOGS]; f++) {
 
        if (Debug) {
                for (f = Files; f < &Files[NLOGS]; f++) {
-                       for (i = 0; i < LOG_NFACILITIES; i++)
+                       for (i = 0; i <= LOG_NFACILITIES; i++)
                                if (f->f_pmask[i] == NOPRI)
                                        printf("X ");
                                else
                                if (f->f_pmask[i] == NOPRI)
                                        printf("X ");
                                else
@@ -893,7 +875,7 @@ init()
                }
        }
 
                }
        }
 
-       logmsg(LOG_DAEMON|LOG_INFO, "syslogd: restart", LocalHostName, ADDDATE);
+       logmsg(LOG_SYSLOG|LOG_INFO, "syslogd: restart", LocalHostName, ADDDATE);
        dprintf("syslogd: restarted\n");
 }
 
        dprintf("syslogd: restarted\n");
 }
 
@@ -918,6 +900,7 @@ struct code PriNames[] = {
        "notice",       LOG_NOTICE,
        "info",         LOG_INFO,
        "debug",        LOG_DEBUG,
        "notice",       LOG_NOTICE,
        "info",         LOG_INFO,
        "debug",        LOG_DEBUG,
+       "none",         NOPRI,
        NULL,           -1
 };
 
        NULL,           -1
 };
 
@@ -925,8 +908,13 @@ struct code        FacNames[] = {
        "kern",         LOG_KERN,
        "user",         LOG_USER,
        "mail",         LOG_MAIL,
        "kern",         LOG_KERN,
        "user",         LOG_USER,
        "mail",         LOG_MAIL,
+       "daemon",       LOG_DAEMON,
        "auth",         LOG_AUTH,
        "security",     LOG_AUTH,
        "auth",         LOG_AUTH,
        "security",     LOG_AUTH,
+       "mark",         LOG_MARK,
+       "syslog",       LOG_SYSLOG,
+       "lpr",          LOG_LPR,
+       "news",         LOG_NEWS,
        "local0",       LOG_LOCAL0,
        "local1",       LOG_LOCAL1,
        "local2",       LOG_LOCAL2,
        "local0",       LOG_LOCAL0,
        "local1",       LOG_LOCAL1,
        "local2",       LOG_LOCAL2,
@@ -952,9 +940,11 @@ cfline(line, f)
 
        dprintf("cfline(%s)\n", line);
 
 
        dprintf("cfline(%s)\n", line);
 
+       errno = 0;      /* keep sys_errlist stuff out of logerror messages */
+
        /* clear out file entry */
        bzero((char *) f, sizeof *f);
        /* clear out file entry */
        bzero((char *) f, sizeof *f);
-       for (i = 0; i < LOG_NFACILITIES; i++)
+       for (i = 0; i <= LOG_NFACILITIES; i++)
                f->f_pmask[i] = NOPRI;
 
        /* scan through the list of selectors */
                f->f_pmask[i] = NOPRI;
 
        /* scan through the list of selectors */