These messages can be .rodata, so make them even more const.
[oweals/busybox.git] / syslogd.c
index e5ddd3b8202e96eaf0cd1db509826eda28693eee..9f5fc93b1f21745bc3130f6b8c57c4d0b1cc4c03 100644 (file)
--- a/syslogd.c
+++ b/syslogd.c
@@ -119,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]");
 }
 
@@ -155,6 +155,8 @@ void ipcsyslog_init(void){
                    perror_msg_and_die("semget");
                }else
                        perror_msg_and_die("semget");
+           } else {
+                   sem_up(s_semid);
            }
        }else{
                printf("Buffer already allocated just grab the semaphore?");
@@ -393,7 +395,9 @@ static int serveConnection (int conn)
        RESERVE_BB_BUFFER(tmpbuf, BUFSIZE + 1);
        int    n_read;
 
-       while ((n_read = read (conn, tmpbuf, BUFSIZE )) > 0) {
+       n_read = read (conn, tmpbuf, BUFSIZE );
+
+       if (n_read > 0) {
 
                int           pri = (LOG_USER | LOG_NOTICE);
                char          line[ BUFSIZE + 1 ];
@@ -427,7 +431,7 @@ static int serveConnection (int conn)
                /* Now log it */
                logMessage (pri, line);
        }
-       return (0);
+       return n_read;
 }
 
 
@@ -439,7 +443,7 @@ static void init_RemoteLog (void){
   int len = sizeof(remoteaddr);
 
   bzero(&remoteaddr, len);
-  
+
   remotefd = socket(AF_INET, SOCK_DGRAM, 0);
 
   if (remotefd < 0) {
@@ -512,13 +516,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 (0, "syslogd started: " BB_BANNER);
 
        for (;;) {
 
@@ -548,29 +558,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 +595,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 +627,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;
 }