PArtial Changelog update. I'm still on vacation (I'm at a campground
[oweals/busybox.git] / syslogd.c
index a7f982a3512afc8843ca0429f569afda69247445..14219eb541f2b8710d91951abc3362966841f4cc 100644 (file)
--- a/syslogd.c
+++ b/syslogd.c
@@ -9,6 +9,8 @@
  *
  * "circular buffer" Copyright (C) 2001 by Gennady Feldman <gfeldman@cachier.com>
  *
+ * Maintainer: Gennady Feldman <gena01@cachier.com> as of Mar 12, 2001
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
@@ -53,7 +55,7 @@
 #define __LOG_FILE "/var/log/messages"
 
 /* Path to the unix socket */
-char lfile[BUFSIZ] = "";
+static char lfile[BUFSIZ];
 
 static char *logFilePath = __LOG_FILE;
 
@@ -117,7 +119,7 @@ static inline void sem_up(int semid)
  */
 static inline void sem_down(int semid)
 {
-       if ( semop(semid, SMwdn, 2) == -1 )
+       if ( semop(semid, SMwdn, 3) == -1 )
                perror_msg_and_die("semop[SMwdn]");
 }
 
@@ -344,7 +346,7 @@ static const int IOV_COUNT = 2;
                struct iovec iov[IOV_COUNT];
                struct iovec *v = iov;
 
-               bzero(&res, sizeof(res));
+               memset(&res, 0, sizeof(res));
                snprintf(res, sizeof(res), "<%d>", pri);
                v->iov_base = res ;
                v->iov_len = strlen(res);          
@@ -368,7 +370,7 @@ static const int IOV_COUNT = 2;
 
 static void quit_signal(int sig)
 {
-       logMessage(0, "System log daemon exiting.");
+       logMessage(LOG_SYSLOG | LOG_INFO, "System log daemon exiting.");
        unlink(lfile);
 #ifdef BB_FEATURE_IPC_SYSLOG
        ipcsyslog_cleanup();
@@ -385,21 +387,25 @@ static void domark(int sig)
        }
 }
 
-static const int BUFSIZE = 1023;
+/* This must be a #define, since when DODEBUG and BUFFERS_GO_IN_BSS are
+ * enabled, we otherwise get a "storage size isn't constant error. */
+#define BUFSIZE 1023
 static int serveConnection (int conn)
 {
-       RESERVE_BB_BUFFER(buf, BUFSIZE + 1);
+       RESERVE_BB_BUFFER(tmpbuf, BUFSIZE + 1);
        int    n_read;
 
-       while ((n_read = read (conn, buf, BUFSIZE )) > 0) {
+       n_read = read (conn, tmpbuf, BUFSIZE );
+
+       if (n_read > 0) {
 
                int           pri = (LOG_USER | LOG_NOTICE);
                char          line[ BUFSIZE + 1 ];
                unsigned char c;
 
-               char *p = buf, *q = line;
+               char *p = tmpbuf, *q = line;
 
-               buf[ n_read - 1 ] = '\0';
+               tmpbuf[ n_read - 1 ] = '\0';
 
                while (p && (c = *p) && q < &line[ sizeof (line) - 1 ]) {
                        if (c == '<') {
@@ -425,7 +431,8 @@ static int serveConnection (int conn)
                /* Now log it */
                logMessage (pri, line);
        }
-       return (0);
+       RELEASE_BB_BUFFER (tmpbuf);
+       return n_read;
 }
 
 
@@ -436,19 +443,15 @@ static void init_RemoteLog (void){
   struct hostent *hostinfo;
   int len = sizeof(remoteaddr);
 
-  bzero(&remoteaddr, len);
-  
+  memset(&remoteaddr, 0, len);
+
   remotefd = socket(AF_INET, SOCK_DGRAM, 0);
 
   if (remotefd < 0) {
     error_msg_and_die("syslogd: cannot create socket");
   }
 
-  hostinfo = (struct hostent *) gethostbyname(RemoteHost);
-
-  if (!hostinfo) {
-    error_msg_and_die("syslogd: cannot resolve remote host name [%s]", RemoteHost);
-  }
+  hostinfo = xgethostbyname(RemoteHost);
 
   remoteaddr.sin_family = AF_INET;
   remoteaddr.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list;
@@ -475,8 +478,6 @@ static void doSyslogd (void)
        int sock_fd;
        fd_set fds;
 
-       RESERVE_BB_BUFFER(lfile, BUFSIZ);
-
        /* Set up signal handlers. */
        signal (SIGINT,  quit_signal);
        signal (SIGTERM, quit_signal);
@@ -490,17 +491,14 @@ static void doSyslogd (void)
        alarm (MarkInterval);
 
        /* Create the syslog file so realpath() can work. */
-       close (open (_PATH_LOG, O_RDWR | O_CREAT, 0644));
-       if (realpath (_PATH_LOG, lfile) == NULL)
-               perror_msg_and_die ("Could not resolve path to " _PATH_LOG);
-
-       unlink (lfile);
+       if (realpath (_PATH_LOG, lfile) != NULL)
+               unlink (lfile);
 
        memset (&sunx, 0, sizeof (sunx));
        sunx.sun_family = AF_UNIX;
        strncpy (sunx.sun_path, lfile, sizeof (sunx.sun_path));
        if ((sock_fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
-               perror_msg_and_die ("Couldn't obtain descriptor for socket " _PATH_LOG);
+               perror_msg_and_die ("Couldn't get file descriptor for socket " _PATH_LOG);
 
        addrLength = sizeof (sunx.sun_family) + strlen (sunx.sun_path);
        if ((bind (sock_fd, (struct sockaddr *) &sunx, addrLength)) || (listen (sock_fd, 5)))
@@ -512,13 +510,19 @@ static void doSyslogd (void)
        FD_ZERO (&fds);
        FD_SET (sock_fd, &fds);
 
+#ifdef BB_FEATURE_IPC_SYSLOG
+       if (circular_logging == TRUE ){
+          ipcsyslog_init();
+       }
+#endif
+
         #ifdef BB_FEATURE_REMOTE_LOG
         if (doRemoteLog == TRUE){
           init_RemoteLog();
         }
         #endif
 
-       logMessage (0, "syslogd started: BusyBox v" BB_VER " (" BB_BT ")");
+       logMessage (LOG_SYSLOG | LOG_INFO, "syslogd started: " BB_BANNER);
 
        for (;;) {
 
@@ -548,29 +552,21 @@ static void doSyslogd (void)
 
                                        FD_SET(conn, &fds);
                                        //printf("conn: %i, set_size: %i\n",conn,FD_SETSIZE);
-                               } else {                
+                               } else {
                                        //printf("Serving connection: %i\n",fd);
-                                       serveConnection (fd);
-                                       close (fd);
-                                       FD_CLR(fd, &fds);
+                                         if ( serveConnection(fd) <= 0 ) {
+                                           close (fd);
+                                           FD_CLR(fd, &fds);
+            }
                                } /* fd == sock_fd */
                        }/* FD_ISSET() */
                }/* for */
        } /* for main loop */
 }
 
-static void daemon_init (char **argv, char *dz, void fn (void))
-{
-       setsid();
-       chdir ("/");
-       strncpy(argv[0], dz, strlen(argv[0]));
-       fn();
-       exit(0);
-}
-
 extern int syslogd_main(int argc, char **argv)
 {
-       int opt, pid;
+       int opt;
        int doFork = TRUE;
 
        char *p;
@@ -593,7 +589,7 @@ extern int syslogd_main(int argc, char **argv)
                                if ( (p = strchr(RemoteHost, ':'))){
                                        RemotePort = atoi(p+1);
                                        *p = '\0';
-                               }          
+                               }
                                doRemoteLog = TRUE;
                                break;
                        case 'L':
@@ -625,22 +621,11 @@ extern int syslogd_main(int argc, char **argv)
 
        umask(0);
 
-#ifdef BB_FEATURE_IPC_SYSLOG
-       if (circular_logging == TRUE ){
-          ipcsyslog_init();
-       }
-#endif
-
        if (doFork == TRUE) {
-               pid = fork();
-               if (pid < 0)
-                       exit(pid);
-               else if (pid == 0) {
-                       daemon_init (argv, "syslogd", doSyslogd);
-               }
-       } else {
-               doSyslogd();
+               if (daemon(0, 1) < 0)
+                       perror_msg_and_die("daemon");
        }
+       doSyslogd();
 
        return EXIT_SUCCESS;
 }