dmesg,klogd: make code more readable

libbb: explain why we declare klogctl
libbb: move defs around so that order makes more sense
This commit is contained in:
Denis Vlasenko 2007-11-16 20:18:54 +00:00
parent 7c2db5c198
commit 7e3a5f58bc
3 changed files with 79 additions and 71 deletions

View File

@ -26,7 +26,6 @@
#include <stdarg.h>
#include <stddef.h>
#include <string.h>
/* #include <strings.h> - said to be obsolete */
#include <sys/poll.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
@ -40,6 +39,12 @@
#include <time.h>
#include <unistd.h>
#include <utime.h>
/* Try to pull in PATH_MAX */
#include <limits.h>
#include <sys/param.h>
#ifndef PATH_MAX
#define PATH_MAX 256
#endif
#if ENABLE_SELINUX
#include <selinux/selinux.h>
@ -62,14 +67,37 @@
#include "shadow_.h"
#endif
/* Try to pull in PATH_MAX */
#include <limits.h>
#include <sys/param.h>
#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 <libgen.h> 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 <libgen.h> 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);

View File

@ -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)

View File

@ -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;
return EXIT_SUCCESS;
}
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");
if (len == 0)
return EXIT_SUCCESS;
// Skip <#> at the start of lines, and make sure we end with a newline.
/* Skip <#> at the start of lines, and make sure we end with a newline. */
if (ENABLE_FEATURE_DMESG_PRETTY) {
int last = '\n';
int in;
int in = 0;
for (in = 0; in<len;) {
if (last == '\n' && buf[in] == '<') in += 3;
else bb_putchar(last = buf[in++]);
do {
if (last == '\n' && buf[in] == '<')
in += 3;
else {
last = buf[in++];
bb_putchar(last);
}
if (last != '\n') bb_putchar('\n');
} while (in < len);
if (last != '\n')
bb_putchar('\n');
} else {
write(1,buf,len);
if (len && buf[len-1]!='\n') bb_putchar('\n');
full_write(STDOUT_FILENO, buf, len);
if (buf[len-1] != '\n')
bb_putchar('\n');
}
if (ENABLE_FEATURE_CLEAN_UP) free(buf);
}
return 0;
return EXIT_SUCCESS;
}