Bump version to 1.32.0
[oweals/busybox.git] / sysklogd / syslogd.c
index 5630d97fc5d0adb2786e54c937ecf480eaad98cd..ab50f4a28e701daba3e85aed3165781ca4d66b19 100644 (file)
@@ -13,7 +13,7 @@
  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
 //config:config SYSLOGD
-//config:      bool "syslogd (12 kb)"
+//config:      bool "syslogd (13 kb)"
 //config:      default y
 //config:      help
 //config:      The syslogd utility is used to record logs of all the
 //config:      help
 //config:      Supports restricted syslogd config. See docs/syslog.conf.txt
 //config:
+//config:config FEATURE_SYSLOGD_PRECISE_TIMESTAMPS
+//config:      bool "Include milliseconds in timestamps"
+//config:      default n
+//config:      depends on SYSLOGD
+//config:      help
+//config:      Includes milliseconds (HH:MM:SS.mmm) in timestamp when
+//config:      timestamps are added.
+//config:
 //config:config FEATURE_SYSLOGD_READ_BUFFER_SIZE
 //config:      int "Read buffer size in bytes"
 //config:      default 256
@@ -276,7 +284,7 @@ struct globals {
        /* ...then copy to parsebuf, escaping control chars */
        /* (can grow x2 max) */
        char parsebuf[MAX_READ*2];
-       /* ...then sprintf into printbuf, adding timestamp (15 chars),
+       /* ...then sprintf into printbuf, adding timestamp (15 or 19 chars),
         * host (64), fac.prio (20) to the message */
        /* (growth by: 15 + 64 + 20 + delims = ~110) */
        char printbuf[MAX_READ*2 + 128];
@@ -572,12 +580,12 @@ static void ipcsyslog_init(void)
 
        G.shmid = shmget(KEY_ID, G.shm_size, IPC_CREAT | 0644);
        if (G.shmid == -1) {
-               bb_perror_msg_and_die("shmget");
+               bb_simple_perror_msg_and_die("shmget");
        }
 
        G.shbuf = shmat(G.shmid, NULL, 0);
        if (G.shbuf == (void*) -1L) { /* shmat has bizarre error return */
-               bb_perror_msg_and_die("shmat");
+               bb_simple_perror_msg_and_die("shmat");
        }
 
        memset(G.shbuf, 0, G.shm_size);
@@ -592,7 +600,7 @@ static void ipcsyslog_init(void)
                        if (G.s_semid != -1)
                                return;
                }
-               bb_perror_msg_and_die("semget");
+               bb_simple_perror_msg_and_die("semget");
        }
 }
 
@@ -603,7 +611,7 @@ static void log_to_shmem(const char *msg)
        int len;
 
        if (semop(G.s_semid, G.SMwdn, 3) == -1) {
-               bb_perror_msg_and_die("SMwdn");
+               bb_simple_perror_msg_and_die("SMwdn");
        }
 
        /* Circular Buffer Algorithm:
@@ -631,7 +639,7 @@ static void log_to_shmem(const char *msg)
                goto again;
        }
        if (semop(G.s_semid, G.SMwup, 1) == -1) {
-               bb_perror_msg_and_die("SMwup");
+               bb_simple_perror_msg_and_die("SMwup");
        }
        if (DEBUG)
                printf("tail:%d\n", G.shbuf->tail);
@@ -832,12 +840,24 @@ static void timestamp_and_log(int pri, char *msg, int len)
                msg += 16;
        }
 
+#if ENABLE_FEATURE_SYSLOGD_PRECISE_TIMESTAMPS
+       if (!timestamp) {
+               struct timeval tv;
+               gettimeofday(&tv, NULL);
+               now = tv.tv_sec;
+               timestamp = ctime(&now) + 4; /* skip day of week */
+               /* overwrite year by milliseconds, zero terminate */
+               sprintf(timestamp + 15, ".%03u", (unsigned)tv.tv_usec / 1000u);
+       } else {
+               timestamp[15] = '\0';
+       }
+#else
        if (!timestamp) {
                time(&now);
                timestamp = ctime(&now) + 4; /* skip day of week */
        }
-
        timestamp[15] = '\0';
+#endif
 
        if (option_mask32 & OPT_kmsg) {
                log_to_kmsg(pri, msg);
@@ -1098,7 +1118,7 @@ static void do_syslogd(void)
        } /* while (!bb_got_signal) */
 
        timestamp_and_log_internal("syslogd exiting");
-       remove_pidfile(CONFIG_PID_FILE_PATH "/syslogd.pid");
+       remove_pidfile_std_path_and_ext("syslogd");
        ipcsyslog_cleanup();
        if (option_mask32 & OPT_kmsg)
                kmsg_cleanup();
@@ -1164,7 +1184,7 @@ int syslogd_main(int argc UNUSED_PARAM, char **argv)
        }
 
        //umask(0); - why??
-       write_pidfile(CONFIG_PID_FILE_PATH "/syslogd.pid");
+       write_pidfile_std_path_and_ext("syslogd");
 
        do_syslogd();
        /* return EXIT_SUCCESS; */