BSD 4_3_Tahoe release
[unix-history] / usr / src / lib / libc / gen / syslog.c
index 18940a2..08c3ceb 100644 (file)
@@ -1,12 +1,23 @@
 /*
 /*
- * Copyright (c) 1983 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
-static char sccsid[] = "@(#)syslog.c   5.2 (Berkeley) %G%";
-#endif not lint
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)syslog.c   5.16 (Berkeley) 6/27/88";
+#endif /* LIBC_SCCS and not lint */
 
 /*
  * SYSLOG -- print message on log file
 
 /*
  * SYSLOG -- print message on log file
@@ -28,20 +39,21 @@ static char sccsid[] = "@(#)syslog.c        5.2 (Berkeley) %G%";
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/file.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/file.h>
-#include <syslog.h>
+#include <sys/signal.h>
+#include <sys/syslog.h>
 #include <netdb.h>
 #include <netdb.h>
+#include <strings.h>
 
 #define        MAXLINE 1024                    /* max message size */
 #define NULL   0                       /* manifest */
 
 
 #define        MAXLINE 1024                    /* max message size */
 #define NULL   0                       /* manifest */
 
-#define PRIMASK(p)     (1 << ((p) & LOG_PRIMASK))
-#define PRIFAC(p)      (((p) & LOG_FACMASK) >> 3)
 #define IMPORTANT      LOG_ERR
 
 static char    logname[] = "/dev/log";
 static char    ctty[] = "/dev/console";
 
 static int     LogFile = -1;           /* fd for log */
 #define IMPORTANT      LOG_ERR
 
 static char    logname[] = "/dev/log";
 static char    ctty[] = "/dev/console";
 
 static int     LogFile = -1;           /* fd for log */
+static int     connected;              /* have done connect */
 static int     LogStat = 0;            /* status bits, set by openlog() */
 static char    *LogTag = "syslog";     /* string to tag the entry with */
 static int     LogMask = 0xff;         /* mask of priorities to be logged */
 static int     LogStat = 0;            /* status bits, set by openlog() */
 static char    *LogTag = "syslog";     /* string to tag the entry with */
 static int     LogMask = 0xff;         /* mask of priorities to be logged */
@@ -63,10 +75,12 @@ syslog(pri, fmt, p0, p1, p2, p3, p4)
        int pid, olderrno = errno;
 
        /* see if we should just throw out this message */
        int pid, olderrno = errno;
 
        /* see if we should just throw out this message */
-       if (pri <= 0 || PRIFAC(pri) >= LOG_NFACILITIES || (PRIMASK(pri) & LogMask) == 0)
+       if ((unsigned) LOG_FAC(pri) >= LOG_NFACILITIES ||
+           LOG_MASK(LOG_PRI(pri)) == 0 ||
+           (pri &~ (LOG_PRIMASK|LOG_FACMASK)) != 0)
                return;
                return;
-       if (LogFile < 0)
-               openlog(LogTag, LogStat & ~LOG_ODELAY, 0);
+       if (LogFile < 0 || !connected)
+               openlog(LogTag, LogStat | LOG_NDELAY, 0);
 
        /* set default facility if none specified */
        if ((pri & LOG_FACMASK) == 0)
 
        /* set default facility if none specified */
        if ((pri & LOG_FACMASK) == 0)
@@ -74,17 +88,17 @@ syslog(pri, fmt, p0, p1, p2, p3, p4)
 
        /* build the message */
        o = outline;
 
        /* build the message */
        o = outline;
-       sprintf(o, "<%d>", pri);
+       (void)sprintf(o, "<%d>", pri);
        o += strlen(o);
        time(&now);
        o += strlen(o);
        time(&now);
-       sprintf(o, "%.15s ", ctime(&now) + 4);
+       (void)sprintf(o, "%.15s ", ctime(&now) + 4);
        o += strlen(o);
        if (LogTag) {
                strcpy(o, LogTag);
                o += strlen(o);
        }
        if (LogStat & LOG_PID) {
        o += strlen(o);
        if (LogTag) {
                strcpy(o, LogTag);
                o += strlen(o);
        }
        if (LogStat & LOG_PID) {
-               sprintf(o, "[%d]", getpid());
+               (void)sprintf(o, "[%d]", getpid());
                o += strlen(o);
        }
        if (LogTag) {
                o += strlen(o);
        }
        if (LogTag) {
@@ -105,37 +119,45 @@ syslog(pri, fmt, p0, p1, p2, p3, p4)
                        continue;
                }
                if ((unsigned)olderrno > sys_nerr)
                        continue;
                }
                if ((unsigned)olderrno > sys_nerr)
-                       sprintf(b, "error %d", olderrno);
+                       (void)sprintf(b, "error %d", olderrno);
                else
                        strcpy(b, sys_errlist[olderrno]);
                b += strlen(b);
        }
        *b++ = '\n';
        *b = '\0';
                else
                        strcpy(b, sys_errlist[olderrno]);
                b += strlen(b);
        }
        *b++ = '\n';
        *b = '\0';
-       sprintf(o, buf, p0, p1, p2, p3, p4);
+       (void)sprintf(o, buf, p0, p1, p2, p3, p4);
        c = strlen(outline);
        if (c > MAXLINE)
                c = MAXLINE;
 
        /* output the message to the local logger */
        c = strlen(outline);
        if (c > MAXLINE)
                c = MAXLINE;
 
        /* output the message to the local logger */
-       if (sendto(LogFile, outline, c, 0, &SyslogAddr, sizeof SyslogAddr) >= 0)
+       if (send(LogFile, outline, c, 0) >= 0)
                return;
                return;
-       if (!(LogStat & LOG_CONS) && (pri & LOG_PRIMASK) <= LOG_ERR)
+       if (!(LogStat & LOG_CONS))
                return;
 
        /* output the message to the console */
                return;
 
        /* output the message to the console */
-       pid = fork();
+       pid = vfork();
        if (pid == -1)
                return;
        if (pid == 0) {
        if (pid == -1)
                return;
        if (pid == 0) {
-               LogFile = open(ctty, O_WRONLY);
+               int fd;
+
+               signal(SIGALRM, SIG_DFL);
+               sigsetmask(sigblock(0L) & ~sigmask(SIGALRM));
+               alarm(5);
+               fd = open(ctty, O_WRONLY);
+               alarm(0);
                strcat(o, "\r");
                strcat(o, "\r");
-               write(LogFile, outline, c+1);
-               close(LogFile);
-               exit(0);
+               o = index(outline, '>') + 1;
+               write(fd, o, c + 1 - (o - outline));
+               close(fd);
+               _exit(0);
        }
        }
-       while ((c = wait((int *)0)) > 0 && c != pid)
-               ;
+       if (!(LogStat & LOG_NOWAIT))
+               while ((c = wait((int *)0)) > 0 && c != pid)
+                       ;
 }
 
 /*
 }
 
 /*
@@ -149,26 +171,31 @@ openlog(ident, logstat, logfac)
        if (ident != NULL)
                LogTag = ident;
        LogStat = logstat;
        if (ident != NULL)
                LogTag = ident;
        LogStat = logstat;
-       if (logfac != 0)
-               LogFacility = logfac & LOG_FACMASK;
-       if (LogFile >= 0)
-               return;
-       SyslogAddr.sa_family = AF_UNIX;
-       strncpy(SyslogAddr.sa_data, logname, sizeof SyslogAddr.sa_data);
-       if (!(LogStat & LOG_ODELAY)) {
-               LogFile = socket(AF_UNIX, SOCK_DGRAM, 0);
-               fcntl(LogFile, F_SETFD, 1);
+       if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
+               LogFacility = logfac;
+       if (LogFile == -1) {
+               SyslogAddr.sa_family = AF_UNIX;
+               strncpy(SyslogAddr.sa_data, logname, sizeof SyslogAddr.sa_data);
+               if (LogStat & LOG_NDELAY) {
+                       LogFile = socket(AF_UNIX, SOCK_DGRAM, 0);
+                       fcntl(LogFile, F_SETFD, 1);
+               }
        }
        }
+       if (LogFile != -1 && !connected &&
+           connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) != -1)
+               connected = 1;
 }
 
 /*
  * CLOSELOG -- close the system log
  */
 }
 
 /*
  * CLOSELOG -- close the system log
  */
+
 closelog()
 {
 
        (void) close(LogFile);
        LogFile = -1;
 closelog()
 {
 
        (void) close(LogFile);
        LogFile = -1;
+       connected = 0;
 }
 
 /*
 }
 
 /*