Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini klogd implementation for busybox |
| 4 | * |
Glenn L McGrath | 6ed7759 | 2002-12-12 10:54:48 +0000 | [diff] [blame] | 5 | * Copyright (C) 2001 by Gennady Feldman <gfeldman@gena01.com>. |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 6 | * Changes: Made this a standalone busybox module which uses standalone |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 7 | * syslog() client interface. |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 8 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 9 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 10 | * |
| 11 | * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org> |
| 12 | * |
Glenn L McGrath | 6ed7759 | 2002-12-12 10:54:48 +0000 | [diff] [blame] | 13 | * "circular buffer" Copyright (C) 2000 by Gennady Feldman <gfeldman@gena01.com> |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 14 | * |
Glenn L McGrath | 6ed7759 | 2002-12-12 10:54:48 +0000 | [diff] [blame] | 15 | * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001 |
Mark Whitley | 6bff9cc | 2001-03-12 23:41:34 +0000 | [diff] [blame] | 16 | * |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 17 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 20 | #include "busybox.h" |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 21 | #include <sys/syslog.h> |
Eric Andersen | 85e5e72 | 2003-07-22 08:56:55 +0000 | [diff] [blame] | 22 | #include <sys/klog.h> |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 23 | |
Bernhard Reutner-Fischer | 595159f | 2006-05-31 12:22:13 +0000 | [diff] [blame] | 24 | static void klogd_signal(int sig ATTRIBUTE_UNUSED) |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 25 | { |
| 26 | klogctl(7, NULL, 0); |
| 27 | klogctl(0, 0, 0); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 28 | /* logMessage(0, "Kernel log daemon exiting."); */ |
Eric Andersen | 36adca8 | 2004-06-22 10:07:17 +0000 | [diff] [blame] | 29 | syslog(LOG_NOTICE, "Kernel log daemon exiting."); |
| 30 | exit(EXIT_SUCCESS); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Bernhard Reutner-Fischer | 595159f | 2006-05-31 12:22:13 +0000 | [diff] [blame] | 33 | #define OPT_LEVEL 1 |
| 34 | #define OPT_FOREGROUND 2 |
| 35 | |
| 36 | #define KLOGD_LOGBUF_SIZE 4096 |
| 37 | |
| 38 | int klogd_main(int argc, char **argv) |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 39 | { |
Bernhard Reutner-Fischer | 595159f | 2006-05-31 12:22:13 +0000 | [diff] [blame] | 40 | RESERVE_CONFIG_BUFFER(log_buffer, KLOGD_LOGBUF_SIZE); |
| 41 | int console_log_level = -1; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 42 | int priority = LOG_INFO; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 43 | int i, n, lastc; |
| 44 | char *start; |
| 45 | |
Bernhard Reutner-Fischer | 595159f | 2006-05-31 12:22:13 +0000 | [diff] [blame] | 46 | { |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 47 | unsigned opt; |
Bernhard Reutner-Fischer | 595159f | 2006-05-31 12:22:13 +0000 | [diff] [blame] | 48 | |
| 49 | /* do normal option parsing */ |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 50 | opt = getopt32(argc, argv, "c:n", &start); |
Bernhard Reutner-Fischer | 595159f | 2006-05-31 12:22:13 +0000 | [diff] [blame] | 51 | |
| 52 | if (opt & OPT_LEVEL) { |
| 53 | /* Valid levels are between 1 and 8 */ |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 54 | console_log_level = xatoul_range(start, 1, 8); |
Bernhard Reutner-Fischer | 595159f | 2006-05-31 12:22:13 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | if (!(opt & OPT_FOREGROUND)) { |
| 58 | #ifdef BB_NOMMU |
| 59 | vfork_daemon_rexec(0, 1, argc, argv, "-n"); |
| 60 | #else |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 61 | xdaemon(0, 1); |
Bernhard Reutner-Fischer | 595159f | 2006-05-31 12:22:13 +0000 | [diff] [blame] | 62 | #endif |
| 63 | } |
| 64 | } |
| 65 | |
Eric Andersen | 36adca8 | 2004-06-22 10:07:17 +0000 | [diff] [blame] | 66 | openlog("kernel", 0, LOG_KERN); |
| 67 | |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 68 | /* Set up sig handlers */ |
| 69 | signal(SIGINT, klogd_signal); |
| 70 | signal(SIGKILL, klogd_signal); |
| 71 | signal(SIGTERM, klogd_signal); |
| 72 | signal(SIGHUP, SIG_IGN); |
| 73 | |
| 74 | /* "Open the log. Currently a NOP." */ |
| 75 | klogctl(1, NULL, 0); |
| 76 | |
Glenn L McGrath | e1ad672 | 2002-12-01 11:31:58 +0000 | [diff] [blame] | 77 | /* Set level of kernel console messaging.. */ |
Eric Andersen | 0373cf1 | 2004-06-25 11:23:03 +0000 | [diff] [blame] | 78 | if (console_log_level != -1) |
Glenn L McGrath | e1ad672 | 2002-12-01 11:31:58 +0000 | [diff] [blame] | 79 | klogctl(8, NULL, console_log_level); |
| 80 | |
"Vladimir N. Oleynik" | dd1ccdd | 2006-02-16 15:40:24 +0000 | [diff] [blame] | 81 | syslog(LOG_NOTICE, "klogd started: %s", BB_BANNER); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 82 | |
| 83 | while (1) { |
| 84 | /* Use kernel syscalls */ |
Bernhard Reutner-Fischer | 595159f | 2006-05-31 12:22:13 +0000 | [diff] [blame] | 85 | memset(log_buffer, '\0', KLOGD_LOGBUF_SIZE); |
| 86 | n = klogctl(2, log_buffer, KLOGD_LOGBUF_SIZE); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 87 | if (n < 0) { |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 88 | if (errno == EINTR) |
| 89 | continue; |
Bernhard Reutner-Fischer | 595159f | 2006-05-31 12:22:13 +0000 | [diff] [blame] | 90 | syslog(LOG_ERR, "klogd: Error from sys_sycall: %d - %m.\n", |
| 91 | errno); |
Eric Andersen | 36adca8 | 2004-06-22 10:07:17 +0000 | [diff] [blame] | 92 | exit(EXIT_FAILURE); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /* klogctl buffer parsing modelled after code in dmesg.c */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 96 | start = &log_buffer[0]; |
| 97 | lastc = '\0'; |
| 98 | for (i = 0; i < n; i++) { |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 99 | if (lastc == '\0' && log_buffer[i] == '<') { |
| 100 | priority = 0; |
| 101 | i++; |
Bernhard Reutner-Fischer | 595159f | 2006-05-31 12:22:13 +0000 | [diff] [blame] | 102 | while (log_buffer[i] >= '0' && log_buffer[i] <= '9') { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 103 | priority = priority * 10 + (log_buffer[i] - '0'); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 104 | i++; |
| 105 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 106 | if (log_buffer[i] == '>') |
| 107 | i++; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 108 | start = &log_buffer[i]; |
| 109 | } |
| 110 | if (log_buffer[i] == '\n') { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 111 | log_buffer[i] = '\0'; /* zero terminate this message */ |
Glenn L McGrath | 12ed333 | 2004-08-06 00:58:53 +0000 | [diff] [blame] | 112 | syslog(priority, "%s", start); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 113 | start = &log_buffer[i + 1]; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 114 | priority = LOG_INFO; |
| 115 | } |
| 116 | lastc = log_buffer[i]; |
| 117 | } |
| 118 | } |
Bernhard Reutner-Fischer | 595159f | 2006-05-31 12:22:13 +0000 | [diff] [blame] | 119 | if (ENABLE_FEATURE_CLEAN_UP) |
| 120 | RELEASE_CONFIG_BUFFER(log_buffer); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 121 | |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 122 | return EXIT_SUCCESS; |
| 123 | } |