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); |
Denis Vlasenko | e428e9d | 2007-01-04 03:07:57 +0000 | [diff] [blame] | 28 | syslog(LOG_NOTICE, "Kernel log daemon exiting"); |
Eric Andersen | 36adca8 | 2004-06-22 10:07:17 +0000 | [diff] [blame] | 29 | exit(EXIT_SUCCESS); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Bernhard Reutner-Fischer | 595159f | 2006-05-31 12:22:13 +0000 | [diff] [blame] | 32 | #define OPT_LEVEL 1 |
| 33 | #define OPT_FOREGROUND 2 |
| 34 | |
Denis Vlasenko | c84520d | 2007-02-17 14:12:10 +0000 | [diff] [blame] | 35 | #define KLOGD_LOGBUF_SIZE BUFSIZ |
| 36 | #define log_buffer bb_common_bufsiz1 |
Bernhard Reutner-Fischer | 595159f | 2006-05-31 12:22:13 +0000 | [diff] [blame] | 37 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 38 | int klogd_main(int argc, char **argv); |
Bernhard Reutner-Fischer | 595159f | 2006-05-31 12:22:13 +0000 | [diff] [blame] | 39 | int klogd_main(int argc, char **argv) |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 40 | { |
Bernhard Reutner-Fischer | 8fc4011 | 2007-01-09 15:46:36 +0000 | [diff] [blame] | 41 | int i = i; /* silence gcc */ |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 42 | char *start; |
| 43 | |
Denis Vlasenko | e428e9d | 2007-01-04 03:07:57 +0000 | [diff] [blame] | 44 | /* do normal option parsing */ |
Bernhard Reutner-Fischer | 8fc4011 | 2007-01-09 15:46:36 +0000 | [diff] [blame] | 45 | getopt32(argc, argv, "c:n", &start); |
Bernhard Reutner-Fischer | 595159f | 2006-05-31 12:22:13 +0000 | [diff] [blame] | 46 | |
Bernhard Reutner-Fischer | 8fc4011 | 2007-01-09 15:46:36 +0000 | [diff] [blame] | 47 | if (option_mask32 & OPT_LEVEL) { |
Denis Vlasenko | e428e9d | 2007-01-04 03:07:57 +0000 | [diff] [blame] | 48 | /* Valid levels are between 1 and 8 */ |
Bernhard Reutner-Fischer | 8fc4011 | 2007-01-09 15:46:36 +0000 | [diff] [blame] | 49 | i = xatoul_range(start, 1, 8); |
Denis Vlasenko | e428e9d | 2007-01-04 03:07:57 +0000 | [diff] [blame] | 50 | } |
Bernhard Reutner-Fischer | 595159f | 2006-05-31 12:22:13 +0000 | [diff] [blame] | 51 | |
Bernhard Reutner-Fischer | 8fc4011 | 2007-01-09 15:46:36 +0000 | [diff] [blame] | 52 | if (!(option_mask32 & OPT_FOREGROUND)) { |
Bernhard Reutner-Fischer | 595159f | 2006-05-31 12:22:13 +0000 | [diff] [blame] | 53 | #ifdef BB_NOMMU |
Denis Vlasenko | 9067f13 | 2007-03-24 12:11:17 +0000 | [diff] [blame^] | 54 | if (!re_execed) |
| 55 | vfork_daemon_rexec(0, 1, argv); |
Bernhard Reutner-Fischer | 595159f | 2006-05-31 12:22:13 +0000 | [diff] [blame] | 56 | #else |
Denis Vlasenko | c84520d | 2007-02-17 14:12:10 +0000 | [diff] [blame] | 57 | bb_daemonize(); |
Bernhard Reutner-Fischer | 595159f | 2006-05-31 12:22:13 +0000 | [diff] [blame] | 58 | #endif |
Bernhard Reutner-Fischer | 595159f | 2006-05-31 12:22:13 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Eric Andersen | 36adca8 | 2004-06-22 10:07:17 +0000 | [diff] [blame] | 61 | openlog("kernel", 0, LOG_KERN); |
| 62 | |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 63 | /* Set up sig handlers */ |
| 64 | signal(SIGINT, klogd_signal); |
| 65 | signal(SIGKILL, klogd_signal); |
| 66 | signal(SIGTERM, klogd_signal); |
| 67 | signal(SIGHUP, SIG_IGN); |
| 68 | |
| 69 | /* "Open the log. Currently a NOP." */ |
| 70 | klogctl(1, NULL, 0); |
| 71 | |
Denis Vlasenko | c84520d | 2007-02-17 14:12:10 +0000 | [diff] [blame] | 72 | /* Set level of kernel console messaging. */ |
Bernhard Reutner-Fischer | 8fc4011 | 2007-01-09 15:46:36 +0000 | [diff] [blame] | 73 | if (option_mask32 & OPT_LEVEL) |
| 74 | klogctl(8, NULL, i); |
Glenn L McGrath | e1ad672 | 2002-12-01 11:31:58 +0000 | [diff] [blame] | 75 | |
"Vladimir N. Oleynik" | dd1ccdd | 2006-02-16 15:40:24 +0000 | [diff] [blame] | 76 | syslog(LOG_NOTICE, "klogd started: %s", BB_BANNER); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 77 | |
Denis Vlasenko | c84520d | 2007-02-17 14:12:10 +0000 | [diff] [blame] | 78 | /* Note: this code does not detect incomplete messages |
| 79 | * (messages not ending with '\n' or just when kernel |
| 80 | * generates too many messages for us to keep up) |
| 81 | * and will split them in two separate lines */ |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 82 | while (1) { |
Bernhard Reutner-Fischer | 8fc4011 | 2007-01-09 15:46:36 +0000 | [diff] [blame] | 83 | int n; |
| 84 | int priority; |
Bernhard Reutner-Fischer | 8fc4011 | 2007-01-09 15:46:36 +0000 | [diff] [blame] | 85 | |
Denis Vlasenko | e428e9d | 2007-01-04 03:07:57 +0000 | [diff] [blame] | 86 | n = klogctl(2, log_buffer, KLOGD_LOGBUF_SIZE - 1); |
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; |
Denis Vlasenko | e428e9d | 2007-01-04 03:07:57 +0000 | [diff] [blame] | 90 | syslog(LOG_ERR, "klogd: error from klogctl(2): %d - %m", |
Denis Vlasenko | c84520d | 2007-02-17 14:12:10 +0000 | [diff] [blame] | 91 | errno); |
Denis Vlasenko | e428e9d | 2007-01-04 03:07:57 +0000 | [diff] [blame] | 92 | break; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 93 | } |
Denis Vlasenko | c84520d | 2007-02-17 14:12:10 +0000 | [diff] [blame] | 94 | log_buffer[n] = '\n'; |
| 95 | i = 0; |
| 96 | while (i < n) { |
| 97 | priority = LOG_INFO; |
| 98 | start = &log_buffer[i]; |
| 99 | if (log_buffer[i] == '<') { |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 100 | i++; |
Denis Vlasenko | e428e9d | 2007-01-04 03:07:57 +0000 | [diff] [blame] | 101 | // kernel never ganerates multi-digit prios |
| 102 | //priority = 0; |
| 103 | //while (log_buffer[i] >= '0' && log_buffer[i] <= '9') { |
| 104 | // priority = priority * 10 + (log_buffer[i] - '0'); |
| 105 | // i++; |
| 106 | //} |
| 107 | if (isdigit(log_buffer[i])) { |
| 108 | priority = (log_buffer[i] - '0'); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 109 | i++; |
| 110 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 111 | if (log_buffer[i] == '>') |
| 112 | i++; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 113 | start = &log_buffer[i]; |
| 114 | } |
Denis Vlasenko | c84520d | 2007-02-17 14:12:10 +0000 | [diff] [blame] | 115 | while (log_buffer[i] != '\n') |
| 116 | i++; |
| 117 | log_buffer[i] = '\0'; |
| 118 | syslog(priority, "%s", start); |
| 119 | i++; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 122 | |
Denis Vlasenko | e428e9d | 2007-01-04 03:07:57 +0000 | [diff] [blame] | 123 | return EXIT_FAILURE; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 124 | } |