lpd: avoid SEGVing on immediate EOF from peer
[oweals/busybox.git] / sysklogd / klogd.c
index 845c49a5ea6844db01608011b60e8780a57ef57f..17b6ca23543d70c743a4197d85ddcbb5b68cb4ad 100644 (file)
  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
 //config:config KLOGD
-//config:      bool "klogd"
+//config:      bool "klogd (5.7 kb)"
 //config:      default y
 //config:      help
-//config:        klogd is a utility which intercepts and logs all
-//config:        messages from the Linux kernel and sends the messages
-//config:        out to the 'syslogd' utility so they can be logged. If
-//config:        you wish to record the messages produced by the kernel,
-//config:        you should enable this option.
+//config:      klogd is a utility which intercepts and logs all
+//config:      messages from the Linux kernel and sends the messages
+//config:      out to the 'syslogd' utility so they can be logged. If
+//config:      you wish to record the messages produced by the kernel,
+//config:      you should enable this option.
 //config:
 //config:comment "klogd should not be used together with syslog to kernel printk buffer"
 //config:      depends on KLOGD && FEATURE_KMSG_SYSLOG
 //config:      depends on KLOGD
 //config:      select PLATFORM_LINUX
 //config:      help
-//config:        The klogd applet supports two interfaces for reading
-//config:        kernel messages. Linux provides the klogctl() interface
-//config:        which allows reading messages from the kernel ring buffer
-//config:        independently from the file system.
+//config:      The klogd applet supports two interfaces for reading
+//config:      kernel messages. Linux provides the klogctl() interface
+//config:      which allows reading messages from the kernel ring buffer
+//config:      independently from the file system.
 //config:
-//config:        If you answer 'N' here, klogd will use the more portable
-//config:        approach of reading them from /proc or a device node.
-//config:        However, this method requires the file to be available.
+//config:      If you answer 'N' here, klogd will use the more portable
+//config:      approach of reading them from /proc or a device node.
+//config:      However, this method requires the file to be available.
 //config:
-//config:        If in doubt, say 'Y'.
+//config:      If in doubt, say 'Y'.
 
 //applet:IF_KLOGD(APPLET(klogd, BB_DIR_SBIN, BB_SUID_DROP))
 
@@ -53,7 +53,7 @@
 //usage:#define klogd_trivial_usage
 //usage:       "[-c N] [-n]"
 //usage:#define klogd_full_usage "\n\n"
-//usage:       "Kernel logger\n"
+//usage:       "Log kernel messages to syslog\n"
 //usage:     "\n       -c N    Print to console messages more urgent than prio N (1-8)"
 //usage:     "\n       -n      Run in foreground"
 
@@ -85,6 +85,7 @@ static void klogd_setloglevel(int lvl)
 
 static int klogd_read(char *bufp, int len)
 {
+       /* "2 -- Read from the log." */
        return klogctl(2, bufp, len);
 }
 # define READ_ERROR "klogctl(2) error"
@@ -146,10 +147,9 @@ static void klogd_close(void)
 
 #endif
 
-#define        log_buffer bb_common_bufsiz1
-#define sizeof_log_buffer COMMON_BUFSIZE
+#define log_buffer bb_common_bufsiz1
 enum {
-       KLOGD_LOGBUF_SIZE = sizeof_log_buffer,
+       KLOGD_LOGBUF_SIZE = COMMON_BUFSIZE,
        OPT_LEVEL      = (1 << 0),
        OPT_FOREGROUND = (1 << 1),
 };
@@ -175,6 +175,8 @@ int klogd_main(int argc UNUSED_PARAM, char **argv)
        int opt;
        int used;
 
+       setup_common_bufsiz();
+
        opt = getopt32(argv, "c:n", &opt_c);
        if (opt & OPT_LEVEL) {
                /* Valid levels are between 1 and 8 */
@@ -229,7 +231,7 @@ int klogd_main(int argc UNUSED_PARAM, char **argv)
 
        syslog(LOG_NOTICE, "klogd started: %s", bb_banner);
 
-       write_pidfile(CONFIG_PID_FILE_PATH "/klogd.pid");
+       write_pidfile_std_path_and_ext("klogd");
 
        used = 0;
        while (!bb_got_signal) {
@@ -237,7 +239,6 @@ int klogd_main(int argc UNUSED_PARAM, char **argv)
                int priority;
                char *start;
 
-               /* "2 -- Read from the log." */
                start = log_buffer + used;
                n = klogd_read(start, KLOGD_LOGBUF_SIZE-1 - used);
                if (n < 0) {
@@ -274,10 +275,13 @@ int klogd_main(int argc UNUSED_PARAM, char **argv)
                        priority = LOG_INFO;
                        if (*start == '<') {
                                start++;
-                               if (*start)
-                                       priority = strtoul(start, &start, 10);
-                               if (*start == '>')
-                                       start++;
+                               if (*start) {
+                                       char *end;
+                                       priority = strtoul(start, &end, 10);
+                                       if (*end == '>')
+                                               end++;
+                                       start = end;
+                               }
                        }
                        /* Log (only non-empty lines) */
                        if (*start)
@@ -291,7 +295,7 @@ int klogd_main(int argc UNUSED_PARAM, char **argv)
 
        klogd_close();
        syslog(LOG_NOTICE, "klogd: exiting");
-       remove_pidfile(CONFIG_PID_FILE_PATH "/klogd.pid");
+       remove_pidfile_std_path_and_ext("klogd");
        if (bb_got_signal)
                kill_myself_with_sig(bb_got_signal);
        return EXIT_FAILURE;