klogd: code de-obfuscation with small code size reduction
authorDenis Vlasenko <vda.linux@googlemail.com>
Sat, 17 Feb 2007 14:12:10 +0000 (14:12 -0000)
committerDenis Vlasenko <vda.linux@googlemail.com>
Sat, 17 Feb 2007 14:12:10 +0000 (14:12 -0000)
include/libbb.h
sysklogd/klogd.c

index 077b6584047485312cac466ec972a20ce91e5692..024d960bfb8e89f2ac9d8a54d82f248b459c98d4 100644 (file)
@@ -271,6 +271,7 @@ extern void xdaemon(int nochdir, int noclose);
 /* More clever/thorough xdaemon */
 extern void bb_sanitize_stdio_maybe_daemonize(int daemonize);
 extern void bb_sanitize_stdio(void);
+/* NB: be careful: dont open syslog/network sockets before bb_daemonize */
 extern void bb_daemonize(void);
 extern void xchdir(const char *path);
 extern void xsetenv(const char *key, const char *value);
index 47c2c72523467a3bebe335dc1bcd80020d17ae0b..cca6f56711672e77f369283e533b0dcfd7a6b081 100644 (file)
@@ -32,12 +32,12 @@ static void klogd_signal(int sig ATTRIBUTE_UNUSED)
 #define OPT_LEVEL        1
 #define OPT_FOREGROUND   2
 
-#define KLOGD_LOGBUF_SIZE 4096
+#define KLOGD_LOGBUF_SIZE BUFSIZ
+#define log_buffer bb_common_bufsiz1
 
 int klogd_main(int argc, char **argv);
 int klogd_main(int argc, char **argv)
 {
-       RESERVE_CONFIG_BUFFER(log_buffer, KLOGD_LOGBUF_SIZE);
        int i = i; /* silence gcc */
        char *start;
 
@@ -53,7 +53,7 @@ int klogd_main(int argc, char **argv)
 #ifdef BB_NOMMU
                vfork_daemon_rexec(0, 1, argc, argv, "-n");
 #else
-               xdaemon(0, 1);
+               bb_daemonize();
 #endif
        }
 
@@ -68,35 +68,34 @@ int klogd_main(int argc, char **argv)
        /* "Open the log. Currently a NOP." */
        klogctl(1, NULL, 0);
 
-       /* Set level of kernel console messaging.. */
+       /* Set level of kernel console messaging. */
        if (option_mask32 & OPT_LEVEL)
                klogctl(8, NULL, i);
 
        syslog(LOG_NOTICE, "klogd started: %s", BB_BANNER);
 
+       /* Note: this code does not detect incomplete messages
+        * (messages not ending with '\n' or just when kernel
+        * generates too many messages for us to keep up)
+        * and will split them in two separate lines */
        while (1) {
                int n;
                int priority;
-               char lastc;
 
-               /* Use kernel syscalls */
-               memset(log_buffer, '\0', KLOGD_LOGBUF_SIZE);
-               /* It will be null-terminted */
                n = klogctl(2, log_buffer, KLOGD_LOGBUF_SIZE - 1);
                if (n < 0) {
                        if (errno == EINTR)
                                continue;
                        syslog(LOG_ERR, "klogd: error from klogctl(2): %d - %m",
-                                  errno);
+                                       errno);
                        break;
                }
-
-               /* klogctl buffer parsing modelled after code in dmesg.c */
-               start = &log_buffer[0];
-               lastc = '\0';
-               priority = LOG_INFO;
-               for (i = 0; i < n; i++) {
-                       if (lastc == '\0' && log_buffer[i] == '<') {
+               log_buffer[n] = '\n';
+               i = 0;
+               while (i < n) {
+                       priority = LOG_INFO;
+                       start = &log_buffer[i];
+                       if (log_buffer[i] == '<') {
                                i++;
                                // kernel never ganerates multi-digit prios
                                //priority = 0;
@@ -112,17 +111,13 @@ int klogd_main(int argc, char **argv)
                                        i++;
                                start = &log_buffer[i];
                        }
-                       if (log_buffer[i] == '\n') {
-                               log_buffer[i] = '\0';   /* zero terminate this message */
-                               syslog(priority, "%s", start);
-                               start = &log_buffer[i + 1];
-                               priority = LOG_INFO;
-                       }
-                       lastc = log_buffer[i];
+                       while (log_buffer[i] != '\n')
+                               i++;
+                       log_buffer[i] = '\0';
+                       syslog(priority, "%s", start);
+                       i++;
                }
        }
-       if (ENABLE_FEATURE_CLEAN_UP)
-               RELEASE_CONFIG_BUFFER(log_buffer);
 
        return EXIT_FAILURE;
 }