install correct aliases file
[unix-history] / usr / src / usr.sbin / syslogd / syslogd.c
index bc3af59..5211cfd 100644 (file)
@@ -1,18 +1,29 @@
 /*
 /*
- * Copyright (c) 1983,1988 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * Copyright (c) 1983, 1988 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
 #ifndef lint
 char copyright[] =
  */
 
 #ifndef lint
 char copyright[] =
-"@(#) Copyright (c) 1983,1988 Regents of the University of California.\n\
+"@(#) Copyright (c) 1983, 1988 Regents of the University of California.\n\
  All rights reserved.\n";
  All rights reserved.\n";
-#endif not lint
+#endif /* not lint */
 
 #ifndef lint
 
 #ifndef lint
-static char sccsid[] = "@(#)syslogd.c  5.21 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)syslogd.c  5.27 (Berkeley) %G%";
+#endif /* not lint */
 
 /*
  *  syslogd -- log system messages
 
 /*
  *  syslogd -- log system messages
@@ -37,7 +48,7 @@ static char sccsid[] = "@(#)syslogd.c 5.21 (Berkeley) %G%";
  */
 
 #define        MAXLINE         1024            /* maximum line length */
  */
 
 #define        MAXLINE         1024            /* maximum line length */
-#define        MAXSVLINE       100             /* maximum saved line length */
+#define        MAXSVLINE       120             /* maximum saved line length */
 #define DEFUPRI                (LOG_USER|LOG_NOTICE)
 #define DEFSPRI                (LOG_KERN|LOG_CRIT)
 #define TIMERINTVL     30              /* interval for checking flush, mark */
 #define DEFUPRI                (LOG_USER|LOG_NOTICE)
 #define DEFSPRI                (LOG_KERN|LOG_CRIT)
 #define TIMERINTVL     30              /* interval for checking flush, mark */
@@ -81,7 +92,7 @@ char  ctty[] = CTTY;
 #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" */
+#define        LOG_MARK        LOG_MAKEPRI(LOG_NFACILITIES, 0) /* mark "facility" */
 
 /*
  * Flags to logmsg().
 
 /*
  * Flags to logmsg().
@@ -173,37 +184,31 @@ main(argc, argv)
        struct sockaddr_un sunx, fromunix;
        struct sockaddr_in sin, frominet;
        FILE *fp;
        struct sockaddr_un sunx, fromunix;
        struct sockaddr_in sin, frominet;
        FILE *fp;
+       int ch;
        char line[MSG_BSIZE + 1];
        char line[MSG_BSIZE + 1];
-       extern int die(), domark(), reapchild();
-
-       while (--argc > 0) {
-               p = *++argv;
-               if (p[0] != '-')
-                       usage();
-               switch (p[1]) {
-               case 'f':               /* configuration file */
-                       if (p[2] != '\0')
-                               ConfFile = &p[2];
-                       break;
+       extern int optind, die(), domark(), reapchild();
+       extern char *optarg;
 
 
+       while ((ch = getopt(argc, argv, "df:m:p:")) != EOF)
+               switch((char)ch) {
                case 'd':               /* debug */
                        Debug++;
                        break;
                case 'd':               /* debug */
                        Debug++;
                        break;
-
-               case 'p':               /* path */
-                       if (p[2] != '\0')
-                               LogName = &p[2];
+               case 'f':               /* configuration file */
+                       ConfFile = optarg;
                        break;
                        break;
-
                case 'm':               /* mark interval */
                case 'm':               /* mark interval */
-                       if (p[2] != '\0')
-                               MarkInterval = atoi(&p[2]) * 60;
+                       MarkInterval = atoi(optarg) * 60;
                        break;
                        break;
-
+               case 'p':               /* path */
+                       LogName = optarg;
+                       break;
+               case '?':
                default:
                        usage();
                }
                default:
                        usage();
                }
-       }
+       if (argc -= optind)
+               usage();
 
        if (!Debug) {
                if (fork())
 
        if (!Debug) {
                if (fork())
@@ -337,7 +342,7 @@ main(argc, argv)
 
 usage()
 {
 
 usage()
 {
-       fprintf(stderr, "usage: syslogd [-d] [-mmarkinterval] [-ppath] [-fconffile]\n");
+       fprintf(stderr, "usage: syslogd [-d] [-m markinterval] [-p path] [-f conffile]\n");
        exit(1);
 }
 
        exit(1);
 }
 
@@ -377,24 +382,25 @@ printline(hname, msg)
                        pri = 10 * pri + (*p - '0');
                if (*p == '>')
                        ++p;
                        pri = 10 * pri + (*p - '0');
                if (*p == '>')
                        ++p;
-               if (pri <= 0 || pri >= (LOG_NFACILITIES << 3))
-                       pri = DEFUPRI;
        }
        }
+       if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
+               pri = DEFUPRI;
 
        /* don't allow users to log kernel messages */
 
        /* don't allow users to log kernel messages */
-       if ((pri & LOG_PRIMASK) == LOG_KERN)
-               pri |= LOG_USER;
+       if (LOG_FAC(pri) == LOG_KERN)
+               pri = LOG_MAKEPRI(LOG_USER, LOG_PRI(pri));
 
        q = line;
 
 
        q = line;
 
-       while ((c = *p++ & 0177) != '\0' && c != '\n' &&
-           q < &line[sizeof(line) - 1]) {
-               if (iscntrl(c)) {
+       while ((c = *p++ & 0177) != '\0' &&
+           q < &line[sizeof(line) - 1])
+               if (c == '\n')
+                       *q++ = ' ';
+               else if (iscntrl(c)) {
                        *q++ = '^';
                        *q++ = c ^ 0100;
                } else
                        *q++ = c;
                        *q++ = '^';
                        *q++ = c ^ 0100;
                } else
                        *q++ = c;
-       }
        *q = '\0';
 
        logmsg(pri, line, hname, 0);
        *q = '\0';
 
        logmsg(pri, line, hname, 0);
@@ -424,12 +430,12 @@ printsys(msg)
                                pri = 10 * pri + (*p - '0');
                        if (*p == '>')
                                ++p;
                                pri = 10 * pri + (*p - '0');
                        if (*p == '>')
                                ++p;
-                       if (pri <= 0 || pri >= (LOG_NFACILITIES << 3))
-                               pri = DEFSPRI;
                } else {
                        /* kernel printf's come out on console */
                        flags |= IGN_CONS;
                }
                } else {
                        /* kernel printf's come out on console */
                        flags |= IGN_CONS;
                }
+               if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
+                       pri = DEFSPRI;
                q = lp;
                while (*p != '\0' && (c = *p++) != '\n' &&
                    q < &line[MAXLINE])
                q = lp;
                while (*p != '\0' && (c = *p++) != '\n' &&
                    q < &line[MAXLINE])
@@ -478,10 +484,11 @@ logmsg(pri, msg, from, flags)
        }
 
        /* extract facility and priority level */
        }
 
        /* extract facility and priority level */
-       fac = (pri & LOG_FACMASK) >> 3;
        if (flags & MARK)
                fac = LOG_NFACILITIES;
        if (flags & MARK)
                fac = LOG_NFACILITIES;
-       prilev = pri & LOG_PRIMASK;
+       else
+               fac = LOG_FAC(pri);
+       prilev = LOG_PRI(pri);
 
        /* log the message to the particular outputs */
        if (!Initialized) {
 
        /* log the message to the particular outputs */
        if (!Initialized) {
@@ -490,7 +497,7 @@ logmsg(pri, msg, from, flags)
 
                if (f->f_file >= 0) {
                        untty();
 
                if (f->f_file >= 0) {
                        untty();
-                       fprintlog(f, flags);
+                       fprintlog(f, flags, msg);
                        (void) close(f->f_file);
                }
                (void) sigsetmask(omask);
                        (void) close(f->f_file);
                }
                (void) sigsetmask(omask);
@@ -501,6 +508,9 @@ 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;
 
+               if (f->f_type == F_CONSOLE && (flags & IGN_CONS))
+                       continue;
+
                /* don't output marks to recently written files */
                if ((flags & MARK) && (now - f->f_time) < MarkInterval / 2)
                        continue;
                /* don't output marks to recently written files */
                if ((flags & MARK) && (now - f->f_time) < MarkInterval / 2)
                        continue;
@@ -513,7 +523,7 @@ logmsg(pri, msg, from, flags)
                    !strcmp(from, f->f_prevhost)) {
                        (void) strncpy(f->f_lasttime, timestamp, 15);
                        f->f_prevcount++;
                    !strcmp(from, f->f_prevhost)) {
                        (void) strncpy(f->f_lasttime, timestamp, 15);
                        f->f_prevcount++;
-                       dprintf("msg repeated %d times, %d sec of %d\n",
+                       dprintf("msg repeated %d times, %ld sec of %d\n",
                            f->f_prevcount, now - f->f_time,
                            repeatinterval[f->f_repeatcount]);
                        /*
                            f->f_prevcount, now - f->f_time,
                            repeatinterval[f->f_repeatcount]);
                        /*
@@ -523,34 +533,36 @@ logmsg(pri, msg, from, flags)
                         * in the future.
                         */
                        if (now > REPEATTIME(f)) {
                         * in the future.
                         */
                        if (now > REPEATTIME(f)) {
-                               fprintlog(f, flags);
+                               fprintlog(f, flags, (char *)NULL);
                                BACKOFF(f);
                        }
                } else {
                        /* new line, save it */
                                BACKOFF(f);
                        }
                } else {
                        /* new line, save it */
-                       if (f->f_prevcount) {
-                               fprintlog(f, 0);
-                               f->f_repeatcount = 0;
-                       }
+                       if (f->f_prevcount)
+                               fprintlog(f, 0, (char *)NULL);
+                       f->f_repeatcount = 0;
                        (void) strncpy(f->f_lasttime, timestamp, 15);
                        (void) strncpy(f->f_lasttime, timestamp, 15);
-                       if (msglen < MAXSVLINE && (f->f_type != F_CONSOLE ||
-                           (flags & IGN_CONS) == 0)) {
+                       (void) strncpy(f->f_prevhost, from,
+                                       sizeof(f->f_prevhost));
+                       if (msglen < MAXSVLINE) {
                                f->f_prevlen = msglen;
                                f->f_prevpri = pri;
                                (void) strcpy(f->f_prevline, msg);
                                f->f_prevlen = msglen;
                                f->f_prevpri = pri;
                                (void) strcpy(f->f_prevline, msg);
-                               (void) strncpy(f->f_prevhost, from,
-                                       sizeof(f->f_prevhost));
-                       } else
+                               fprintlog(f, flags, (char *)NULL);
+                       } else {
                                f->f_prevline[0] = 0;
                                f->f_prevline[0] = 0;
-                       fprintlog(f, flags);
+                               f->f_prevlen = 0;
+                               fprintlog(f, flags, msg);
+                       }
                }
        }
        (void) sigsetmask(omask);
 }
 
                }
        }
        (void) sigsetmask(omask);
 }
 
-fprintlog(f, flags)
+fprintlog(f, flags, msg)
        register struct filed *f;
        int flags;
        register struct filed *f;
        int flags;
+       char *msg;
 {
        struct iovec iov[6];
        register struct iovec *v = iov;
 {
        struct iovec iov[6];
        register struct iovec *v = iov;
@@ -570,7 +582,10 @@ fprintlog(f, flags)
        v->iov_base = " ";
        v->iov_len = 1;
        v++;
        v->iov_base = " ";
        v->iov_len = 1;
        v++;
-       if (f->f_prevcount > 1) {
+       if (msg) {
+               v->iov_base = msg;
+               v->iov_len = strlen(msg);
+       } else if (f->f_prevcount > 1) {
                (void) sprintf(repbuf, "last message repeated %d times",
                    f->f_prevcount);
                v->iov_base = repbuf;
                (void) sprintf(repbuf, "last message repeated %d times",
                    f->f_prevcount);
                v->iov_base = repbuf;
@@ -815,7 +830,7 @@ domark()
                        dprintf("flush %s: repeated %d times, %d sec.\n",
                            TypeNames[f->f_type], f->f_prevcount,
                            repeatinterval[f->f_repeatcount]);
                        dprintf("flush %s: repeated %d times, %d sec.\n",
                            TypeNames[f->f_type], f->f_prevcount,
                            repeatinterval[f->f_repeatcount]);
-                       fprintlog(f, 0);
+                       fprintlog(f, 0, (char *)NULL);
                        BACKOFF(f);
                }
        }
                        BACKOFF(f);
                }
        }
@@ -849,7 +864,7 @@ die(sig)
        for (f = Files; f != NULL; f = f->f_next) {
                /* flush any pending output */
                if (f->f_prevcount)
        for (f = Files; f != NULL; f = f->f_next) {
                /* flush any pending output */
                if (f->f_prevcount)
-                       fprintlog(f, 0);
+                       fprintlog(f, 0, (char *)NULL);
        }
        if (sig) {
                dprintf("syslogd: exiting on signal %d\n", sig);
        }
        if (sig) {
                dprintf("syslogd: exiting on signal %d\n", sig);
@@ -882,7 +897,7 @@ init()
        for (f = Files; f != NULL; f = next) {
                /* flush any pending output */
                if (f->f_prevcount)
        for (f = Files; f != NULL; f = next) {
                /* flush any pending output */
                if (f->f_prevcount)
-                       fprintlog(f, 0);
+                       fprintlog(f, 0, (char *)NULL);
 
                switch (f->f_type) {
                  case F_FILE:
 
                switch (f->f_type) {
                  case F_FILE:
@@ -913,18 +928,18 @@ init()
         */
        f = NULL;
        while (fgets(cline, sizeof cline, cf) != NULL) {
         */
        f = NULL;
        while (fgets(cline, sizeof cline, cf) != NULL) {
-               /* check for end-of-section */
-               if (cline[0] == '\n' || cline[0] == '#')
+               /*
+                * check for end-of-section, comments, strip off trailing
+                * spaces and newline character.
+                */
+               for (p = cline; isspace(*p); ++p);
+               if (*p == NULL || *p == '#')
                        continue;
                        continue;
-
+               for (p = index(cline, '\0'); isspace(*--p););
+               *++p = '\0';
                f = (struct filed *)calloc(1, sizeof(*f));
                *nextp = f;
                nextp = &f->f_next;
                f = (struct filed *)calloc(1, sizeof(*f));
                *nextp = f;
                nextp = &f->f_next;
-               /* strip off newline character */
-               p = index(cline, '\n');
-               if (p)
-                       *p = '\0';
-
                cfline(cline, f);
        }
 
                cfline(cline, f);
        }
 
@@ -1001,6 +1016,7 @@ struct code       FacNames[] = {
        "syslog",       LOG_SYSLOG,
        "lpr",          LOG_LPR,
        "news",         LOG_NEWS,
        "syslog",       LOG_SYSLOG,
        "lpr",          LOG_LPR,
        "news",         LOG_NEWS,
+       "uucp",         LOG_UUCP,
        "local0",       LOG_LOCAL0,
        "local1",       LOG_LOCAL1,
        "local2",       LOG_LOCAL2,
        "local0",       LOG_LOCAL0,
        "local1",       LOG_LOCAL1,
        "local2",       LOG_LOCAL2,
@@ -1026,7 +1042,7 @@ cfline(line, f)
 
        dprintf("cfline(%s)\n", line);
 
 
        dprintf("cfline(%s)\n", line);
 
-       errno = 0;      /* keep sys_errlist stuff out of logerror messages */
+       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);
@@ -1061,8 +1077,6 @@ cfline(line, f)
 
                /* scan facilities */
                while (*p && !index("\t.;", *p)) {
 
                /* scan facilities */
                while (*p && !index("\t.;", *p)) {
-                       int i;
-
                        for (bp = buf; *p && !index("\t,;.", *p); )
                                *bp++ = *p++;
                        *bp = '\0';
                        for (bp = buf; *p && !index("\t,;.", *p); )
                                *bp++ = *p++;
                        *bp = '\0';
@@ -1099,11 +1113,11 @@ cfline(line, f)
                (void) strcpy(f->f_un.f_forw.f_hname, ++p);
                hp = gethostbyname(p);
                if (hp == NULL) {
                (void) strcpy(f->f_un.f_forw.f_hname, ++p);
                hp = gethostbyname(p);
                if (hp == NULL) {
-                       char buf[100];
+                       extern int h_errno, h_nerr;
+                       extern char **h_errlist;
 
 
-                       (void) sprintf(buf, "unknown host %s", p);
-                       errno = 0;
-                       logerror(buf);
+                       logerror((u_int)h_errno < h_nerr ?
+                           h_errlist[h_errno] : "Unknown error");
                        break;
                }
                bzero((char *) &f->f_un.f_forw.f_addr,
                        break;
                }
                bzero((char *) &f->f_un.f_forw.f_addr,
@@ -1117,6 +1131,7 @@ cfline(line, f)
        case '/':
                (void) strcpy(f->f_un.f_fname, p);
                if ((f->f_file = open(p, O_WRONLY|O_APPEND)) < 0) {
        case '/':
                (void) strcpy(f->f_un.f_fname, p);
                if ((f->f_file = open(p, O_WRONLY|O_APPEND)) < 0) {
+                       f->f_file = F_UNUSED;
                        logerror(p);
                        break;
                }
                        logerror(p);
                        break;
                }