add xref to fsdb(8)
[unix-history] / usr / src / usr.sbin / syslogd / syslogd.c
index 072dbf0..cda0bdf 100644 (file)
@@ -11,7 +11,7 @@ char copyright[] =
 #endif not lint
 
 #ifndef lint
 #endif not lint
 
 #ifndef lint
-static char sccsid[] = "@(#)syslogd.c  5.11 (Berkeley) %G%";
+static char sccsid[] = "@(#)syslogd.c  5.19 (Berkeley) %G%";
 #endif not lint
 
 /*
 #endif not lint
 
 /*
@@ -34,6 +34,7 @@ static char sccsid[] = "@(#)syslogd.c 5.11 (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        NLOGS           20              /* max number of log files */
@@ -42,7 +43,6 @@ static char sccsid[] = "@(#)syslogd.c 5.11 (Berkeley) %G%";
 #define DEFSPRI                (LOG_KERN|LOG_CRIT)
 #define MARKCOUNT      10              /* ratio of minor to major marks */
 
 #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>
@@ -51,6 +51,7 @@ static char sccsid[] = "@(#)syslogd.c 5.11 (Berkeley) %G%";
 #include <sysexits.h>
 #include <strings.h>
 
 #include <sysexits.h>
 #include <strings.h>
 
+#include <sys/syslog.h>
 #include <sys/types.h>
 #include <sys/param.h>
 #include <sys/ioctl.h>
 #include <sys/types.h>
 #include <sys/param.h>
 #include <sys/ioctl.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().
@@ -101,16 +103,15 @@ struct filed {
        short   f_type;                 /* entry type, see below */
        short   f_file;                 /* file descriptor */
        time_t  f_time;                 /* time this was last written */
        short   f_type;                 /* entry type, see below */
        short   f_file;                 /* file descriptor */
        time_t  f_time;                 /* time this was last written */
-       u_char  f_pmask[LOG_NFACILITIES];       /* priority mask */
+       u_char  f_pmask[LOG_NFACILITIES+1];     /* priority mask */
        union {
                char    f_uname[MAXUNAMES][UNAMESZ+1];
        union {
                char    f_uname[MAXUNAMES][UNAMESZ+1];
-               struct
-               {
+               struct {
                        char    f_hname[MAXHOSTNAMELEN+1];
                        struct sockaddr_in      f_addr;
                        char    f_hname[MAXHOSTNAMELEN+1];
                        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 */
@@ -154,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];
@@ -216,11 +217,11 @@ main(argc, argv)
        (void) alarm(MarkInterval * 60 / MARKCOUNT);
        (void) unlink(LogName);
 
        (void) alarm(MarkInterval * 60 / MARKCOUNT);
        (void) unlink(LogName);
 
-       sun.sun_family = AF_UNIX;
-       (void) strncpy(sun.sun_path, LogName, sizeof sun.sun_path);
+       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);
@@ -271,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);
@@ -497,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 */
@@ -531,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;
@@ -575,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;
@@ -657,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
@@ -673,8 +677,13 @@ wallmsg(f, iov)
                        (void) alarm(30);
                        /* open the terminal */
                        ttyf = open(p, O_WRONLY);
                        (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);
                }
        }
@@ -724,7 +733,7 @@ domark()
        int pri;
 
        if ((++MarkSeq % MARKCOUNT) == 0)
        int pri;
 
        if ((++MarkSeq % MARKCOUNT) == 0)
-               logmsg(LOG_SYSLOG|LOG_INFO, "-- MARK --", LocalHostName, ADDDATE|MARK);
+               logmsg(LOG_INFO, "-- MARK --", LocalHostName, ADDDATE|MARK);
        else
                flushmsg();
        alarm(MarkInterval * 60 / MARKCOUNT);
        else
                flushmsg();
        alarm(MarkInterval * 60 / MARKCOUNT);
@@ -768,6 +777,7 @@ die(sig)
                dprintf("syslogd: going down on signal %d\n", sig);
                flushmsg();
                (void) sprintf(buf, "going down on signal %d", 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);
                logerror(buf);
        }
        (void) unlink(LogName);
@@ -794,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 */
@@ -832,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
@@ -894,9 +911,10 @@ struct code        FacNames[] = {
        "daemon",       LOG_DAEMON,
        "auth",         LOG_AUTH,
        "security",     LOG_AUTH,
        "daemon",       LOG_DAEMON,
        "auth",         LOG_AUTH,
        "security",     LOG_AUTH,
-       "mark",         LOG_SYSLOG,
+       "mark",         LOG_MARK,
        "syslog",       LOG_SYSLOG,
        "lpr",          LOG_LPR,
        "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,
@@ -922,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 */