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 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 20 | #include "libbb.h" |
Bernhard Reutner-Fischer | f470196 | 2008-01-27 12:50:12 +0000 | [diff] [blame] | 21 | #include <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 | |
Denis Vlasenko | 7bdf0c8 | 2008-06-06 16:08:04 +0000 | [diff] [blame] | 24 | static void klogd_signal(int sig) |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 25 | { |
Denis Vlasenko | 7bdf0c8 | 2008-06-06 16:08:04 +0000 | [diff] [blame] | 26 | /* FYI: cmd 7 is equivalent to setting console_loglevel to 7 |
| 27 | * via klogctl(8, NULL, 7). */ |
| 28 | klogctl(7, NULL, 0); /* "7 -- Enable printk's to console" */ |
| 29 | klogctl(0, NULL, 0); /* "0 -- Close the log. Currently a NOP" */ |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 30 | syslog(LOG_NOTICE, "klogd: exiting"); |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 31 | kill_myself_with_sig(sig); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Denis Vlasenko | c84520d | 2007-02-17 14:12:10 +0000 | [diff] [blame] | 34 | #define log_buffer bb_common_bufsiz1 |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 35 | enum { |
| 36 | KLOGD_LOGBUF_SIZE = sizeof(log_buffer), |
| 37 | OPT_LEVEL = (1 << 0), |
| 38 | OPT_FOREGROUND = (1 << 1), |
| 39 | }; |
Bernhard Reutner-Fischer | 595159f | 2006-05-31 12:22:13 +0000 | [diff] [blame] | 40 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 41 | int klogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 42 | int klogd_main(int argc UNUSED_PARAM, char **argv) |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 43 | { |
Denis Vlasenko | 7bdf0c8 | 2008-06-06 16:08:04 +0000 | [diff] [blame] | 44 | int i = 0; |
Denis Vlasenko | 2e7dc5d | 2008-11-19 07:59:49 +0000 | [diff] [blame] | 45 | char *opt_c; |
Denis Vlasenko | 7bdf0c8 | 2008-06-06 16:08:04 +0000 | [diff] [blame] | 46 | int opt; |
Denis Vlasenko | 93d0776 | 2008-10-04 16:40:17 +0000 | [diff] [blame] | 47 | int used = 0; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 48 | |
Denis Vlasenko | 2e7dc5d | 2008-11-19 07:59:49 +0000 | [diff] [blame] | 49 | opt = getopt32(argv, "c:n", &opt_c); |
Denis Vlasenko | 7bdf0c8 | 2008-06-06 16:08:04 +0000 | [diff] [blame] | 50 | if (opt & OPT_LEVEL) { |
Denis Vlasenko | e428e9d | 2007-01-04 03:07:57 +0000 | [diff] [blame] | 51 | /* Valid levels are between 1 and 8 */ |
Denis Vlasenko | 2e7dc5d | 2008-11-19 07:59:49 +0000 | [diff] [blame] | 52 | i = xatou_range(opt_c, 1, 8); |
Denis Vlasenko | e428e9d | 2007-01-04 03:07:57 +0000 | [diff] [blame] | 53 | } |
Denis Vlasenko | 7bdf0c8 | 2008-06-06 16:08:04 +0000 | [diff] [blame] | 54 | if (!(opt & OPT_FOREGROUND)) { |
Denis Vlasenko | 5a14202 | 2007-03-26 13:20:54 +0000 | [diff] [blame] | 55 | bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv); |
Bernhard Reutner-Fischer | 595159f | 2006-05-31 12:22:13 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Eric Andersen | 36adca8 | 2004-06-22 10:07:17 +0000 | [diff] [blame] | 58 | openlog("kernel", 0, LOG_KERN); |
| 59 | |
Denis Vlasenko | 2e7dc5d | 2008-11-19 07:59:49 +0000 | [diff] [blame] | 60 | bb_signals(BB_FATAL_SIGS, klogd_signal); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 61 | signal(SIGHUP, SIG_IGN); |
| 62 | |
Denis Vlasenko | 7bdf0c8 | 2008-06-06 16:08:04 +0000 | [diff] [blame] | 63 | /* "Open the log. Currently a NOP" */ |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 64 | klogctl(1, NULL, 0); |
| 65 | |
Denis Vlasenko | 7bdf0c8 | 2008-06-06 16:08:04 +0000 | [diff] [blame] | 66 | /* "printk() prints a message on the console only if it has a loglevel |
| 67 | * less than console_loglevel". Here we set console_loglevel = i. */ |
| 68 | if (i) |
Bernhard Reutner-Fischer | 8fc4011 | 2007-01-09 15:46:36 +0000 | [diff] [blame] | 69 | klogctl(8, NULL, i); |
Glenn L McGrath | e1ad672 | 2002-12-01 11:31:58 +0000 | [diff] [blame] | 70 | |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 71 | syslog(LOG_NOTICE, "klogd started: %s", bb_banner); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 72 | |
| 73 | while (1) { |
Bernhard Reutner-Fischer | 8fc4011 | 2007-01-09 15:46:36 +0000 | [diff] [blame] | 74 | int n; |
| 75 | int priority; |
Denis Vlasenko | 2e7dc5d | 2008-11-19 07:59:49 +0000 | [diff] [blame] | 76 | char *start; |
Bernhard Reutner-Fischer | 8fc4011 | 2007-01-09 15:46:36 +0000 | [diff] [blame] | 77 | |
Denis Vlasenko | 7bdf0c8 | 2008-06-06 16:08:04 +0000 | [diff] [blame] | 78 | /* "2 -- Read from the log." */ |
Denis Vlasenko | 2e7dc5d | 2008-11-19 07:59:49 +0000 | [diff] [blame] | 79 | start = log_buffer + used; |
| 80 | n = klogctl(2, start, KLOGD_LOGBUF_SIZE-1 - used); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 81 | if (n < 0) { |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 82 | if (errno == EINTR) |
| 83 | continue; |
Denis Vlasenko | 7bdf0c8 | 2008-06-06 16:08:04 +0000 | [diff] [blame] | 84 | syslog(LOG_ERR, "klogd: error %d in klogctl(2): %m", |
Denis Vlasenko | c84520d | 2007-02-17 14:12:10 +0000 | [diff] [blame] | 85 | errno); |
Denis Vlasenko | e428e9d | 2007-01-04 03:07:57 +0000 | [diff] [blame] | 86 | break; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 87 | } |
Denis Vlasenko | 2e7dc5d | 2008-11-19 07:59:49 +0000 | [diff] [blame] | 88 | start[n] = '\0'; |
Denis Vlasenko | 93d0776 | 2008-10-04 16:40:17 +0000 | [diff] [blame] | 89 | |
| 90 | /* klogctl buffer parsing modelled after code in dmesg.c */ |
Denis Vlasenko | 93d0776 | 2008-10-04 16:40:17 +0000 | [diff] [blame] | 91 | /* Process each newline-terminated line in the buffer */ |
Denis Vlasenko | 58a8891 | 2008-11-19 09:35:00 +0000 | [diff] [blame^] | 92 | start = log_buffer; |
Denis Vlasenko | 93d0776 | 2008-10-04 16:40:17 +0000 | [diff] [blame] | 93 | while (1) { |
Denis Vlasenko | 2e7dc5d | 2008-11-19 07:59:49 +0000 | [diff] [blame] | 94 | char *newline = strchrnul(start, '\n'); |
Denis Vlasenko | 93d0776 | 2008-10-04 16:40:17 +0000 | [diff] [blame] | 95 | |
Denis Vlasenko | 2e7dc5d | 2008-11-19 07:59:49 +0000 | [diff] [blame] | 96 | if (*newline == '\0') { |
Denis Vlasenko | 93d0776 | 2008-10-04 16:40:17 +0000 | [diff] [blame] | 97 | /* This line is incomplete... */ |
| 98 | if (start != log_buffer) { |
| 99 | /* move it to the front of the buffer */ |
Denis Vlasenko | 2e7dc5d | 2008-11-19 07:59:49 +0000 | [diff] [blame] | 100 | overlapping_strcpy(log_buffer, start); |
| 101 | used = newline - start; |
Denis Vlasenko | 93d0776 | 2008-10-04 16:40:17 +0000 | [diff] [blame] | 102 | /* don't log it yet */ |
Denis Vlasenko | 93d0776 | 2008-10-04 16:40:17 +0000 | [diff] [blame] | 103 | break; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 104 | } |
Denis Vlasenko | 2e7dc5d | 2008-11-19 07:59:49 +0000 | [diff] [blame] | 105 | /* ...but if buffer is full, log it anyway */ |
Denis Vlasenko | 93d0776 | 2008-10-04 16:40:17 +0000 | [diff] [blame] | 106 | used = 0; |
Denis Vlasenko | 2e7dc5d | 2008-11-19 07:59:49 +0000 | [diff] [blame] | 107 | newline = NULL; |
Denis Vlasenko | 93d0776 | 2008-10-04 16:40:17 +0000 | [diff] [blame] | 108 | } else { |
| 109 | *newline++ = '\0'; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 110 | } |
Denis Vlasenko | 93d0776 | 2008-10-04 16:40:17 +0000 | [diff] [blame] | 111 | |
| 112 | /* Extract the priority */ |
| 113 | priority = LOG_INFO; |
| 114 | if (*start == '<') { |
| 115 | start++; |
| 116 | if (*start) { |
| 117 | /* kernel never generates multi-digit prios */ |
| 118 | priority = (*start - '0'); |
| 119 | start++; |
| 120 | } |
Denis Vlasenko | 2e7dc5d | 2008-11-19 07:59:49 +0000 | [diff] [blame] | 121 | if (*start == '>') |
Denis Vlasenko | 93d0776 | 2008-10-04 16:40:17 +0000 | [diff] [blame] | 122 | start++; |
Denis Vlasenko | 93d0776 | 2008-10-04 16:40:17 +0000 | [diff] [blame] | 123 | } |
Denis Vlasenko | 2e7dc5d | 2008-11-19 07:59:49 +0000 | [diff] [blame] | 124 | /* Log (only non-empty lines) */ |
Denis Vlasenko | 93d0776 | 2008-10-04 16:40:17 +0000 | [diff] [blame] | 125 | if (*start) |
| 126 | syslog(priority, "%s", start); |
Denis Vlasenko | 2e7dc5d | 2008-11-19 07:59:49 +0000 | [diff] [blame] | 127 | |
Denis Vlasenko | 93d0776 | 2008-10-04 16:40:17 +0000 | [diff] [blame] | 128 | if (!newline) |
| 129 | break; |
| 130 | start = newline; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 133 | |
Denis Vlasenko | e428e9d | 2007-01-04 03:07:57 +0000 | [diff] [blame] | 134 | return EXIT_FAILURE; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 135 | } |