added hooks for sort
[oweals/busybox.git] / syslogd.c
index 736adf7d189478f84195658725f2bfdc471cb04d..43e83b19175cdbf51dacfa0b4c714d86fcc0ea43 100644 (file)
--- a/syslogd.c
+++ b/syslogd.c
@@ -22,6 +22,7 @@
 
 #include "internal.h"
 #include <stdio.h>
+#include <stdarg.h>
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <unistd.h>
 #include <signal.h>
 #include <ctype.h>
 #include <netdb.h>
+#include <sys/klog.h>
+#include <errno.h>
+#include <paths.h>
+
+#define ksyslog klogctl
+extern int ksyslog(int type, char *buf, int len);
 
 
 /* SYSLOG_NAMES defined to pull some extra junk from syslog.h */
@@ -40,8 +47,6 @@
 
 /* Path for the file where all log messages are written */
 #define __LOG_FILE             "/var/log/messages"
-/* Path to the current console device */
-#define __DEV_CONSOLE          "/dev/console"
 
 
 static char* logFilePath = __LOG_FILE;
@@ -52,14 +57,17 @@ static char LocalHostName[32];
 
 static const char syslogd_usage[] =
     "syslogd [OPTION]...\n\n"
-    "Linux system logging utility.\n\n"
+    "Linux system and kernel (provides klogd) logging utility.\n"
+    "Note that this version of syslogd/klogd ignores /etc/syslog.conf.\n\n"
     "Options:\n"
     "\t-m\tChange the mark timestamp interval. default=20min. 0=off\n"
     "\t-n\tDo not fork into the background (for when run by init)\n"
+#ifdef BB_KLOGD
+    "\t-K\tDo not start up the klogd process (by default syslogd spawns klogd).\n"
+#endif
     "\t-O\tSpecify an alternate log file.  default=/var/log/messages\n";
 
 
-
 /* try to open up the specified device */
 static int device_open(char *device, int mode)
 {
@@ -92,7 +100,7 @@ static void message(char *fmt, ...)
        close(fd);
     } else {
        /* Always send console messages to /dev/console so people will see them. */
-       if ((fd = device_open(__DEV_CONSOLE, O_WRONLY|O_NOCTTY|O_NONBLOCK)) >= 0) {
+       if ((fd = device_open(_PATH_CONSOLE, O_WRONLY|O_NOCTTY|O_NONBLOCK)) >= 0) {
            va_start(arguments, fmt);
            vdprintf(fd, fmt, arguments);
            va_end(arguments);
@@ -141,7 +149,8 @@ static void logMessage( int pri, char* msg)
 
 static void quit_signal(int sig)
 {
-    logMessage(LOG_SYSLOG|LOG_INFO, "syslogd exiting");
+    logMessage(LOG_SYSLOG|LOG_INFO, "System log daemon exiting.");
+    unlink( _PATH_LOG);
     exit( TRUE);
 }
 
@@ -155,7 +164,6 @@ static void domark(int sig)
 {
     if (MarkInterval > 0) {
        logMessage(LOG_SYSLOG|LOG_INFO, "-- MARK --");
-       signal(SIGALRM, domark);
        alarm(MarkInterval);
     }
 }
@@ -180,32 +188,27 @@ static void doSyslogd(void)
     signal(SIGALRM, domark);
     alarm(MarkInterval);
 
+
+    unlink( _PATH_LOG);
     memset(&sunx, 0, sizeof(sunx));
     sunx.sun_family = AF_UNIX; /* Unix domain socket */
     strncpy(sunx.sun_path, _PATH_LOG, sizeof(sunx.sun_path));
-    if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0 ) {
+    if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0 ) {
         perror("Couldn't obtain descriptor for socket " _PATH_LOG);
        exit( FALSE);
     }
 
     addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path);
     if ( (bind(fd, (struct sockaddr *) &sunx, addrLength)) ||
-           (chmod(_PATH_LOG, 0666) < 0) ||
-           (listen(fd, 5)) ) 
+           (fchmod(fd, 0666) < 0) || (listen(fd, 5)) ) 
     {
        perror("Could not connect to socket " _PATH_LOG);
        exit( FALSE);
     }
     
     
-    /* Get localhost's name */
-    gethostname(LocalHostName, sizeof(LocalHostName));
-    if ( (p = strchr(LocalHostName, '.')) ) {
-       *p++ = '\0';
-    }
-    
     logMessage(LOG_SYSLOG|LOG_INFO, "syslogd started: "
-           "BusyBox v" BB_VER " (" BB_BT ") multi-call binary");
+           "BusyBox v" BB_VER " (" BB_BT ")");
 
 
     while ((conn = accept(fd, (struct sockaddr *) &sunx, 
@@ -250,46 +253,151 @@ static void doSyslogd(void)
     close(fd);
 }
 
+#ifdef BB_KLOGD
+
+static void klogd_signal(int sig)
+{
+    ksyslog(7, NULL, 0);
+    ksyslog(0, 0, 0);
+    logMessage(LOG_SYSLOG|LOG_INFO, "Kernel log daemon exiting.");
+    exit( TRUE);
+}
+
+static void doKlogd(void)
+{
+    int priority=LOG_INFO;
+    char log_buffer[4096];
+    char *logp;
+
+    /* Set up sig handlers */
+    signal(SIGINT,  klogd_signal);
+    signal(SIGKILL, klogd_signal);
+    signal(SIGTERM, klogd_signal);
+    signal(SIGHUP,  klogd_signal);
+    logMessage(LOG_SYSLOG|LOG_INFO, "klogd started: "
+           "BusyBox v" BB_VER " (" BB_BT ")");
+
+    ksyslog(1, NULL, 0);
+
+    while (1) {
+       /* Use kernel syscalls */
+       memset(log_buffer, '\0', sizeof(log_buffer));
+       if ( ksyslog(2, log_buffer, sizeof(log_buffer)) < 0 ) {
+           char message[80];
+           if ( errno == EINTR )
+               continue;
+           snprintf(message, 79, "klogd: Error return from sys_sycall: " \
+                   "%d - %s.\n", errno, strerror(errno));
+           logMessage(LOG_SYSLOG|LOG_ERR, message);
+           exit(1);
+       }
+       logp=log_buffer;
+        if ( *log_buffer == '<' )
+        {
+           switch ( *(log_buffer+1) )
+           {
+               case '0':
+                   priority = LOG_EMERG;
+                   break;
+               case '1':
+                   priority = LOG_ALERT;
+                   break;
+               case '2':
+                   priority = LOG_CRIT;
+                   break;
+               case '3':
+                   priority = LOG_ERR;
+                   break;
+               case '4':
+                   priority = LOG_WARNING;
+                   break;
+               case '5':
+                   priority = LOG_NOTICE;
+                   break;
+               case '6':
+                   priority = LOG_INFO;
+                   break;
+               case '7':
+               default:
+                   priority = LOG_DEBUG;
+           }
+           logp+=3;
+        }
+       logMessage(LOG_KERN|priority, logp);
+    }
+
+}
+
+#endif
 
 extern int syslogd_main(int argc, char **argv)
 {
-    int        pid;
+    int        pid, klogd_pid;
     int doFork = TRUE;
-
-    while (--argc > 0 && **(++argv) == '-') {
-       while (*(++(*argv))) {
-           switch (**argv) {
+#ifdef BB_KLOGD
+    int startKlogd = TRUE;
+#endif
+    char *p;
+    char **argv1=argv;
+    
+    while (--argc > 0 && **(++argv1) == '-') {
+       while (*(++(*argv1))) {
+           switch (**argv1) {
            case 'm':
                if (--argc == 0) {
                    usage(syslogd_usage);
                }
-               MarkInterval = atoi(*(++argv))*60;
+               MarkInterval = atoi(*(++argv1))*60;
                break;
            case 'n':
                doFork = FALSE;
                break;
+#ifdef BB_KLOGD
+           case 'K':
+               startKlogd = FALSE;
+               break;
+#endif
            case 'O':
                if (--argc == 0) {
                    usage(syslogd_usage);
                }
-               logFilePath = *(++argv);
+               logFilePath = *(++argv1);
                break;
            default:
                usage(syslogd_usage);
            }
        }
     }
+    
+    /* Store away localhost's name before the fork */
+    gethostname(LocalHostName, sizeof(LocalHostName));
+    if ( (p = strchr(LocalHostName, '.')) ) {
+       *p++ = '\0';
+    }
 
     if (doFork == TRUE) {
        pid = fork();
        if ( pid < 0 )
            exit( pid);
        else if ( pid == 0 ) {
+           strncpy(argv[0], "syslogd",strlen(argv[0]));
            doSyslogd();
        }
     } else {
        doSyslogd();
     }
+
+#ifdef BB_KLOGD
+    /* Start up the klogd process */
+    if (startKlogd == TRUE) {
+       klogd_pid = fork();
+       if (klogd_pid == 0 ) {
+               strncpy(argv[0], "klogd", strlen(argv[0]));
+               doKlogd();
+       }
+    }
+#endif
+
     exit( TRUE);
 }