klogd: code de-obfuscation with small code size reduction

This commit is contained in:
Denis Vlasenko 2007-02-17 14:12:10 +00:00
parent d818dcc72a
commit c84520d73d
2 changed files with 21 additions and 25 deletions

View File

@ -271,6 +271,7 @@ extern void xdaemon(int nochdir, int noclose);
/* More clever/thorough xdaemon */ /* More clever/thorough xdaemon */
extern void bb_sanitize_stdio_maybe_daemonize(int daemonize); extern void bb_sanitize_stdio_maybe_daemonize(int daemonize);
extern void bb_sanitize_stdio(void); extern void bb_sanitize_stdio(void);
/* NB: be careful: dont open syslog/network sockets before bb_daemonize */
extern void bb_daemonize(void); extern void bb_daemonize(void);
extern void xchdir(const char *path); extern void xchdir(const char *path);
extern void xsetenv(const char *key, const char *value); extern void xsetenv(const char *key, const char *value);

View File

@ -32,12 +32,12 @@ static void klogd_signal(int sig ATTRIBUTE_UNUSED)
#define OPT_LEVEL 1 #define OPT_LEVEL 1
#define OPT_FOREGROUND 2 #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);
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 */ int i = i; /* silence gcc */
char *start; char *start;
@ -53,7 +53,7 @@ int klogd_main(int argc, char **argv)
#ifdef BB_NOMMU #ifdef BB_NOMMU
vfork_daemon_rexec(0, 1, argc, argv, "-n"); vfork_daemon_rexec(0, 1, argc, argv, "-n");
#else #else
xdaemon(0, 1); bb_daemonize();
#endif #endif
} }
@ -68,20 +68,20 @@ int klogd_main(int argc, char **argv)
/* "Open the log. Currently a NOP." */ /* "Open the log. Currently a NOP." */
klogctl(1, NULL, 0); klogctl(1, NULL, 0);
/* Set level of kernel console messaging.. */ /* Set level of kernel console messaging. */
if (option_mask32 & OPT_LEVEL) if (option_mask32 & OPT_LEVEL)
klogctl(8, NULL, i); klogctl(8, NULL, i);
syslog(LOG_NOTICE, "klogd started: %s", BB_BANNER); 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) { while (1) {
int n; int n;
int priority; 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); n = klogctl(2, log_buffer, KLOGD_LOGBUF_SIZE - 1);
if (n < 0) { if (n < 0) {
if (errno == EINTR) if (errno == EINTR)
@ -90,13 +90,12 @@ int klogd_main(int argc, char **argv)
errno); errno);
break; break;
} }
log_buffer[n] = '\n';
/* klogctl buffer parsing modelled after code in dmesg.c */ i = 0;
start = &log_buffer[0]; while (i < n) {
lastc = '\0';
priority = LOG_INFO; priority = LOG_INFO;
for (i = 0; i < n; i++) { start = &log_buffer[i];
if (lastc == '\0' && log_buffer[i] == '<') { if (log_buffer[i] == '<') {
i++; i++;
// kernel never ganerates multi-digit prios // kernel never ganerates multi-digit prios
//priority = 0; //priority = 0;
@ -112,17 +111,13 @@ int klogd_main(int argc, char **argv)
i++; i++;
start = &log_buffer[i]; start = &log_buffer[i];
} }
if (log_buffer[i] == '\n') { while (log_buffer[i] != '\n')
log_buffer[i] = '\0'; /* zero terminate this message */ i++;
log_buffer[i] = '\0';
syslog(priority, "%s", start); syslog(priority, "%s", start);
start = &log_buffer[i + 1]; i++;
priority = LOG_INFO;
}
lastc = log_buffer[i];
} }
} }
if (ENABLE_FEATURE_CLEAN_UP)
RELEASE_CONFIG_BUFFER(log_buffer);
return EXIT_FAILURE; return EXIT_FAILURE;
} }