From 7e3a5f58bcd2c40745ed7eaaa8952b29c3da4447 Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Fri, 16 Nov 2007 20:18:54 +0000 Subject: [PATCH] dmesg,klogd: make code more readable libbb: explain why we declare klogctl libbb: move defs around so that order makes more sense --- include/libbb.h | 73 +++++++++++++++++++++++----------------------- sysklogd/klogd.c | 13 +++++---- util-linux/dmesg.c | 60 ++++++++++++++++++++----------------- 3 files changed, 77 insertions(+), 69 deletions(-) diff --git a/include/libbb.h b/include/libbb.h index 1adac8443..2593c3ad2 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -26,7 +26,6 @@ #include #include #include -/* #include - said to be obsolete */ #include #include #include @@ -40,6 +39,12 @@ #include #include #include +/* Try to pull in PATH_MAX */ +#include +#include +#ifndef PATH_MAX +#define PATH_MAX 256 +#endif #if ENABLE_SELINUX #include @@ -62,14 +67,37 @@ #include "shadow_.h" #endif -/* Try to pull in PATH_MAX */ -#include -#include -#ifndef PATH_MAX -#define PATH_MAX 256 +#if defined(__GLIBC__) && __GLIBC__ < 2 +int vdprintf(int d, const char *format, va_list ap); #endif +/* klogctl is in libc's klog.h, but we cheat and not #include that */ +int klogctl(int type, char *b, int len); +/* This is declared here rather than #including in order to avoid + * confusing the two versions of basename. See the dirname/basename man page + * for details. */ +char *dirname(char *path); +/* Include our own copy of struct sysinfo to avoid binary compatibility + * problems with Linux 2.4, which changed things. Grumble, grumble. */ +struct sysinfo { + long uptime; /* Seconds since boot */ + unsigned long loads[3]; /* 1, 5, and 15 minute load averages */ + unsigned long totalram; /* Total usable main memory size */ + unsigned long freeram; /* Available memory size */ + unsigned long sharedram; /* Amount of shared memory */ + unsigned long bufferram; /* Memory used by buffers */ + unsigned long totalswap; /* Total swap space size */ + unsigned long freeswap; /* swap space still available */ + unsigned short procs; /* Number of current processes */ + unsigned short pad; /* Padding needed for m68k */ + unsigned long totalhigh; /* Total high memory size */ + unsigned long freehigh; /* Available high memory size */ + unsigned int mem_unit; /* Memory unit size in bytes */ + char _f[20 - 2*sizeof(long) - sizeof(int)]; /* Padding: libc5 uses this.. */ +}; +int sysinfo(struct sysinfo* info); + -/* Tested to work correctly (IIRC :]) */ +/* Tested to work correctly with all int types (IIRC :]) */ #define MAXINT(T) (T)( \ ((T)-1) > 0 \ ? (T)-1 \ @@ -83,7 +111,7 @@ ) /* Large file support */ -/* Note that CONFIG_LFS forces bbox to be built with all common ops +/* Note that CONFIG_LFS=y forces bbox to be built with all common ops * (stat, lseek etc) mapped to "largefile" variants by libc. * Practically it means that open() automatically has O_LARGEFILE added * and all filesize/file_offset parameters and struct members are "large" @@ -167,7 +195,6 @@ #endif #endif - #if defined(__GLIBC__) /* glibc uses __errno_location() to get a ptr to errno */ /* We can just memorize it once - no multithreading in busybox :) */ @@ -176,33 +203,6 @@ extern int *const bb_errno; #define errno (*bb_errno) #endif -#if defined(__GLIBC__) && __GLIBC__ < 2 -int vdprintf(int d, const char *format, va_list ap); -#endif -// This is declared here rather than #including in order to avoid -// confusing the two versions of basename. See the dirname/basename man page -// for details. -char *dirname(char *path); -/* Include our own copy of struct sysinfo to avoid binary compatibility - * problems with Linux 2.4, which changed things. Grumble, grumble. */ -struct sysinfo { - long uptime; /* Seconds since boot */ - unsigned long loads[3]; /* 1, 5, and 15 minute load averages */ - unsigned long totalram; /* Total usable main memory size */ - unsigned long freeram; /* Available memory size */ - unsigned long sharedram; /* Amount of shared memory */ - unsigned long bufferram; /* Memory used by buffers */ - unsigned long totalswap; /* Total swap space size */ - unsigned long freeswap; /* swap space still available */ - unsigned short procs; /* Number of current processes */ - unsigned short pad; /* Padding needed for m68k */ - unsigned long totalhigh; /* Total high memory size */ - unsigned long freehigh; /* Available high memory size */ - unsigned int mem_unit; /* Memory unit size in bytes */ - char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */ -}; -int sysinfo(struct sysinfo* info); - unsigned long long monotonic_us(void); unsigned monotonic_sec(void); @@ -788,7 +788,6 @@ extern int set_loop(char **devname, const char *file, unsigned long long offset) //TODO: pass buf pointer or return allocated buf (avoid statics)? char *bb_askpass(int timeout, const char * prompt); int bb_ask_confirmation(void); -int klogctl(int type, char * b, int len); extern int bb_parse_mode(const char* s, mode_t* theMode); diff --git a/sysklogd/klogd.c b/sysklogd/klogd.c index 11642461d..72f3b559c 100644 --- a/sysklogd/klogd.c +++ b/sysklogd/klogd.c @@ -24,16 +24,17 @@ static void klogd_signal(int sig ATTRIBUTE_UNUSED) { klogctl(7, NULL, 0); - klogctl(0, 0, 0); - syslog(LOG_NOTICE, "Kernel log daemon exiting"); + klogctl(0, NULL, 0); + syslog(LOG_NOTICE, "klogd: exiting"); exit(EXIT_SUCCESS); } -#define OPT_LEVEL 1 -#define OPT_FOREGROUND 2 - -#define KLOGD_LOGBUF_SIZE BUFSIZ #define log_buffer bb_common_bufsiz1 +enum { + KLOGD_LOGBUF_SIZE = sizeof(log_buffer), + OPT_LEVEL = (1 << 0), + OPT_FOREGROUND = (1 << 1), +}; int klogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int klogd_main(int argc, char **argv) diff --git a/util-linux/dmesg.c b/util-linux/dmesg.c index 90b327b4c..cdd385ede 100644 --- a/util-linux/dmesg.c +++ b/util-linux/dmesg.c @@ -15,40 +15,48 @@ int dmesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int dmesg_main(int argc, char **argv) { + int len; + char *buf; char *size, *level; int flags = getopt32(argv, "cs:n:", &size, &level); if (flags & 4) { if (klogctl(8, NULL, xatoul_range(level, 0, 10))) bb_perror_msg_and_die("klogctl"); - } else { - int len; - char *buf; - - len = (flags & 2) ? xatoul_range(size, 2, INT_MAX) : 16384; - buf = xmalloc(len); - len = klogctl(3 + (flags & 1), buf, len); - if (len < 0) - bb_perror_msg_and_die("klogctl"); - - // Skip <#> at the start of lines, and make sure we end with a newline. - - if (ENABLE_FEATURE_DMESG_PRETTY) { - int last = '\n'; - int in; + return EXIT_SUCCESS; + } - for (in = 0; in at the start of lines, and make sure we end with a newline. */ + + if (ENABLE_FEATURE_DMESG_PRETTY) { + int last = '\n'; + int in = 0; + + do { + if (last == '\n' && buf[in] == '<') + in += 3; + else { + last = buf[in++]; + bb_putchar(last); } - if (last != '\n') bb_putchar('\n'); - } else { - write(1,buf,len); - if (len && buf[len-1]!='\n') bb_putchar('\n'); - } - - if (ENABLE_FEATURE_CLEAN_UP) free(buf); + } while (in < len); + if (last != '\n') + bb_putchar('\n'); + } else { + full_write(STDOUT_FILENO, buf, len); + if (buf[len-1] != '\n') + bb_putchar('\n'); } - return 0; + if (ENABLE_FEATURE_CLEAN_UP) free(buf); + + return EXIT_SUCCESS; } -- 2.25.1