1/* vi: set sw=4 ts=4: */ 2/* 3 * Mini klogd implementation for busybox 4 * 5 * Copyright (C) 2001 by Gennady Feldman <gfeldman@gena01.com>. 6 * Changes: Made this a standalone busybox module which uses standalone 7 * syslog() client interface. 8 * 9 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> 10 * 11 * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org> 12 * 13 * "circular buffer" Copyright (C) 2000 by Gennady Feldman <gfeldman@gena01.com> 14 * 15 * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001 16 * 17 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. 18 */ 19 20#include "libbb.h" 21#include <sys/syslog.h> 22#include <sys/klog.h> 23 24static void klogd_signal(int sig ATTRIBUTE_UNUSED) 25{ 26 klogctl(7, NULL, 0); 27 klogctl(0, 0, 0); 28 syslog(LOG_NOTICE, "Kernel log daemon exiting"); 29 exit(EXIT_SUCCESS); 30} 31 32#define OPT_LEVEL 1 33#define OPT_FOREGROUND 2 34 35#define KLOGD_LOGBUF_SIZE BUFSIZ 36#define log_buffer bb_common_bufsiz1 37 38int klogd_main(int argc, char **argv); 39int klogd_main(int argc, char **argv) 40{ 41 int i = i; /* silence gcc */ 42 char *start; 43 44 /* do normal option parsing */ 45 getopt32(argv, "c:n", &start); 46 47 if (option_mask32 & OPT_LEVEL) { 48 /* Valid levels are between 1 and 8 */ 49 i = xatoul_range(start, 1, 8); 50 } 51 52 if (!(option_mask32 & OPT_FOREGROUND)) { 53 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv); 54 } 55 56 openlog("kernel", 0, LOG_KERN); 57 58 /* Set up sig handlers */ 59 signal(SIGINT, klogd_signal); 60 signal(SIGKILL, klogd_signal); 61 signal(SIGTERM, klogd_signal); 62 signal(SIGHUP, SIG_IGN); 63 64 /* "Open the log. Currently a NOP." */ 65 klogctl(1, NULL, 0); 66 67 /* Set level of kernel console messaging. */ 68 if (option_mask32 & OPT_LEVEL) 69 klogctl(8, NULL, i); 70 71 syslog(LOG_NOTICE, "klogd started: %s", bb_banner); 72 73 /* Note: this code does not detect incomplete messages 74 * (messages not ending with '\n' or just when kernel 75 * generates too many messages for us to keep up) 76 * and will split them in two separate lines */ 77 while (1) { 78 int n; 79 int priority; 80 81 n = klogctl(2, log_buffer, KLOGD_LOGBUF_SIZE - 1); 82 if (n < 0) { 83 if (errno == EINTR) 84 continue; 85 syslog(LOG_ERR, "klogd: error from klogctl(2): %d - %m", 86 errno); 87 break; 88 } 89 log_buffer[n] = '\n'; 90 i = 0; 91 while (i < n) { 92 priority = LOG_INFO; 93 start = &log_buffer[i]; 94 if (log_buffer[i] == '<') { 95 i++; 96 // kernel never ganerates multi-digit prios 97 //priority = 0; 98 //while (log_buffer[i] >= '0' && log_buffer[i] <= '9') { 99 // priority = priority * 10 + (log_buffer[i] - '0'); 100 // i++; 101 //} 102 if (isdigit(log_buffer[i])) { 103 priority = (log_buffer[i] - '0'); 104 i++; 105 } 106 if (log_buffer[i] == '>') 107 i++; 108 start = &log_buffer[i]; 109 } 110 while (log_buffer[i] != '\n') 111 i++; 112 log_buffer[i] = '\0'; 113 syslog(priority, "%s", start); 114 i++; 115 } 116 } 117 118 return EXIT_FAILURE; 119} 120