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 |
Jeremie Koenig | 63c2e7e | 2010-08-01 03:01:44 +0200 | [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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 17 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
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 | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 22 | |
Jeremie Koenig | 63c2e7e | 2010-08-01 03:01:44 +0200 | [diff] [blame] | 23 | |
| 24 | /* The Linux-specific klogctl(3) interface does not rely on the filesystem and |
| 25 | * allows us to change the console loglevel. Alternatively, we read the |
| 26 | * messages from _PATH_KLOG. */ |
| 27 | |
| 28 | #if ENABLE_FEATURE_KLOGD_KLOGCTL |
| 29 | |
| 30 | # include <sys/klog.h> |
| 31 | |
| 32 | static void klogd_open(void) |
| 33 | { |
| 34 | /* "Open the log. Currently a NOP" */ |
| 35 | klogctl(1, NULL, 0); |
| 36 | } |
| 37 | |
| 38 | static void klogd_setloglevel(int lvl) |
| 39 | { |
| 40 | /* "printk() prints a message on the console only if it has a loglevel |
| 41 | * less than console_loglevel". Here we set console_loglevel = lvl. */ |
| 42 | klogctl(8, NULL, lvl); |
| 43 | } |
| 44 | |
| 45 | static int klogd_read(char *bufp, int len) |
| 46 | { |
| 47 | return klogctl(2, bufp, len); |
| 48 | } |
| 49 | # define READ_ERROR "klogctl(2) error" |
| 50 | |
| 51 | static void klogd_close(void) |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 52 | { |
Denis Vlasenko | 7bdf0c8 | 2008-06-06 16:08:04 +0000 | [diff] [blame] | 53 | /* FYI: cmd 7 is equivalent to setting console_loglevel to 7 |
| 54 | * via klogctl(8, NULL, 7). */ |
| 55 | klogctl(7, NULL, 0); /* "7 -- Enable printk's to console" */ |
| 56 | klogctl(0, NULL, 0); /* "0 -- Close the log. Currently a NOP" */ |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Jeremie Koenig | 63c2e7e | 2010-08-01 03:01:44 +0200 | [diff] [blame] | 59 | #else |
| 60 | |
| 61 | # include <paths.h> |
| 62 | # ifndef _PATH_KLOG |
| 63 | # ifdef __GNU__ |
| 64 | # define _PATH_KLOG "/dev/klog" |
| 65 | # else |
| 66 | # error "your system's _PATH_KLOG is unknown" |
| 67 | # endif |
| 68 | # endif |
| 69 | # define PATH_PRINTK "/proc/sys/kernel/printk" |
| 70 | |
| 71 | enum { klogfd = 3 }; |
| 72 | |
| 73 | static void klogd_open(void) |
| 74 | { |
| 75 | int fd = xopen(_PATH_KLOG, O_RDONLY); |
| 76 | xmove_fd(fd, klogfd); |
| 77 | } |
| 78 | |
| 79 | static void klogd_setloglevel(int lvl) |
| 80 | { |
| 81 | FILE *fp = fopen_or_warn(PATH_PRINTK, "w"); |
| 82 | if (fp) { |
| 83 | /* This changes only first value: |
| 84 | * "messages with a higher priority than this |
| 85 | * [that is, with numerically lower value] |
| 86 | * will be printed to the console". |
| 87 | * The other three values in this pseudo-file aren't changed. |
| 88 | */ |
| 89 | fprintf(fp, "%u\n", lvl); |
| 90 | fclose(fp); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | static int klogd_read(char *bufp, int len) |
| 95 | { |
| 96 | return read(klogfd, bufp, len); |
| 97 | } |
| 98 | # define READ_ERROR "read error" |
| 99 | |
| 100 | static void klogd_close(void) |
| 101 | { |
| 102 | klogd_setloglevel(7); |
| 103 | if (ENABLE_FEATURE_CLEAN_UP) |
| 104 | close(klogfd); |
| 105 | } |
| 106 | |
| 107 | #endif |
| 108 | |
Denis Vlasenko | c84520d | 2007-02-17 14:12:10 +0000 | [diff] [blame] | 109 | #define log_buffer bb_common_bufsiz1 |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 110 | enum { |
| 111 | KLOGD_LOGBUF_SIZE = sizeof(log_buffer), |
| 112 | OPT_LEVEL = (1 << 0), |
| 113 | OPT_FOREGROUND = (1 << 1), |
| 114 | }; |
Bernhard Reutner-Fischer | 595159f | 2006-05-31 12:22:13 +0000 | [diff] [blame] | 115 | |
Jeremie Koenig | 63c2e7e | 2010-08-01 03:01:44 +0200 | [diff] [blame] | 116 | /* TODO: glibc openlog(LOG_KERN) reverts to LOG_USER instead, |
| 117 | * because that's how they interpret word "default" |
| 118 | * in the openlog() manpage: |
| 119 | * LOG_USER (default) |
| 120 | * generic user-level messages |
| 121 | * and the fact that LOG_KERN is a constant 0. |
| 122 | * glibc interprets it as "0 in openlog() call means 'use default'". |
| 123 | * I think it means "if openlog wasn't called before syslog() is called, |
| 124 | * use default". |
| 125 | * Convincing glibc maintainers otherwise is, as usual, nearly impossible. |
| 126 | * Should we open-code syslog() here to use correct facility? |
| 127 | */ |
| 128 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 129 | int klogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 130 | int klogd_main(int argc UNUSED_PARAM, char **argv) |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 131 | { |
Denis Vlasenko | 7bdf0c8 | 2008-06-06 16:08:04 +0000 | [diff] [blame] | 132 | int i = 0; |
Denis Vlasenko | 2e7dc5d | 2008-11-19 07:59:49 +0000 | [diff] [blame] | 133 | char *opt_c; |
Denis Vlasenko | 7bdf0c8 | 2008-06-06 16:08:04 +0000 | [diff] [blame] | 134 | int opt; |
Denys Vlasenko | 0016bce | 2010-10-19 23:07:49 +0200 | [diff] [blame] | 135 | int used; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 136 | |
Denis Vlasenko | 2e7dc5d | 2008-11-19 07:59:49 +0000 | [diff] [blame] | 137 | opt = getopt32(argv, "c:n", &opt_c); |
Denis Vlasenko | 7bdf0c8 | 2008-06-06 16:08:04 +0000 | [diff] [blame] | 138 | if (opt & OPT_LEVEL) { |
Denis Vlasenko | e428e9d | 2007-01-04 03:07:57 +0000 | [diff] [blame] | 139 | /* Valid levels are between 1 and 8 */ |
Denis Vlasenko | 2e7dc5d | 2008-11-19 07:59:49 +0000 | [diff] [blame] | 140 | i = xatou_range(opt_c, 1, 8); |
Denis Vlasenko | e428e9d | 2007-01-04 03:07:57 +0000 | [diff] [blame] | 141 | } |
Denis Vlasenko | 7bdf0c8 | 2008-06-06 16:08:04 +0000 | [diff] [blame] | 142 | if (!(opt & OPT_FOREGROUND)) { |
Denis Vlasenko | 5a14202 | 2007-03-26 13:20:54 +0000 | [diff] [blame] | 143 | bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv); |
Bernhard Reutner-Fischer | 595159f | 2006-05-31 12:22:13 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Jeremie Koenig | 63c2e7e | 2010-08-01 03:01:44 +0200 | [diff] [blame] | 146 | logmode = LOGMODE_SYSLOG; |
| 147 | |
| 148 | /* klogd_open() before openlog(), since it might use fixed fd 3, |
| 149 | * and openlog() also may use the same fd 3 if we swap them: |
| 150 | */ |
| 151 | klogd_open(); |
Eric Andersen | 36adca8 | 2004-06-22 10:07:17 +0000 | [diff] [blame] | 152 | openlog("kernel", 0, LOG_KERN); |
Denys Vlasenko | 8b6b472 | 2011-03-07 10:57:26 +0100 | [diff] [blame^] | 153 | /* |
| 154 | * glibc problem: for some reason, glibc changes LOG_KERN to LOG_USER |
| 155 | * above. The logic behind this is that standard |
| 156 | * http://pubs.opengroup.org/onlinepubs/9699919799/functions/syslog.html |
| 157 | * says the following about openlog and syslog: |
| 158 | * "LOG_USER |
| 159 | * Messages generated by arbitrary processes. |
| 160 | * This is the default facility identifier if none is specified." |
| 161 | * |
| 162 | * I believe glibc misinterpreted this text as "if openlog's |
| 163 | * third parameter is 0 (=LOG_KERN), treat it as LOG_USER". |
| 164 | * Whereas it was meant to say "if *syslog* is called with facility |
| 165 | * 0 in its 1st parameter without prior call to openlog, then perform |
| 166 | * implicit openlog(LOG_USER)". |
| 167 | * |
| 168 | * As a result of this, eh, feature, standard klogd was forced |
| 169 | * to open-code its own openlog and syslog implementation (!). |
| 170 | * |
| 171 | * Note that prohibiting openlog(LOG_KERN) on libc level does not |
| 172 | * add any security: any process can open a socket to "/dev/log" |
| 173 | * and write a string "<0>Voila, a LOG_KERN + LOG_EMERG message" |
| 174 | * |
| 175 | * Google code search tells me there is no widespread use of |
| 176 | * openlog("foo", 0, 0), thus fixing glibc won't break userspace. |
| 177 | * |
| 178 | * The bug against glibc was filed: |
| 179 | * bugzilla.redhat.com/show_bug.cgi?id=547000 |
| 180 | */ |
Eric Andersen | 36adca8 | 2004-06-22 10:07:17 +0000 | [diff] [blame] | 181 | |
Denis Vlasenko | 7bdf0c8 | 2008-06-06 16:08:04 +0000 | [diff] [blame] | 182 | if (i) |
Jeremie Koenig | 63c2e7e | 2010-08-01 03:01:44 +0200 | [diff] [blame] | 183 | klogd_setloglevel(i); |
| 184 | |
Jeremie Koenig | 63c2e7e | 2010-08-01 03:01:44 +0200 | [diff] [blame] | 185 | signal(SIGHUP, SIG_IGN); |
Denys Vlasenko | 8b6b472 | 2011-03-07 10:57:26 +0100 | [diff] [blame^] | 186 | /* We want klogd_read to not be restarted, thus _norestart: */ |
| 187 | bb_signals_recursive_norestart(BB_FATAL_SIGS, record_signo); |
Glenn L McGrath | e1ad672 | 2002-12-01 11:31:58 +0000 | [diff] [blame] | 188 | |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 189 | syslog(LOG_NOTICE, "klogd started: %s", bb_banner); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 190 | |
Denys Vlasenko | 0016bce | 2010-10-19 23:07:49 +0200 | [diff] [blame] | 191 | used = 0; |
Jeremie Koenig | 63c2e7e | 2010-08-01 03:01:44 +0200 | [diff] [blame] | 192 | while (!bb_got_signal) { |
Bernhard Reutner-Fischer | 8fc4011 | 2007-01-09 15:46:36 +0000 | [diff] [blame] | 193 | int n; |
| 194 | int priority; |
Denis Vlasenko | 2e7dc5d | 2008-11-19 07:59:49 +0000 | [diff] [blame] | 195 | char *start; |
Bernhard Reutner-Fischer | 8fc4011 | 2007-01-09 15:46:36 +0000 | [diff] [blame] | 196 | |
Denis Vlasenko | 7bdf0c8 | 2008-06-06 16:08:04 +0000 | [diff] [blame] | 197 | /* "2 -- Read from the log." */ |
Denis Vlasenko | 2e7dc5d | 2008-11-19 07:59:49 +0000 | [diff] [blame] | 198 | start = log_buffer + used; |
Jeremie Koenig | 63c2e7e | 2010-08-01 03:01:44 +0200 | [diff] [blame] | 199 | n = klogd_read(start, KLOGD_LOGBUF_SIZE-1 - used); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 200 | if (n < 0) { |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 201 | if (errno == EINTR) |
| 202 | continue; |
Jeremie Koenig | 63c2e7e | 2010-08-01 03:01:44 +0200 | [diff] [blame] | 203 | bb_perror_msg(READ_ERROR); |
Denis Vlasenko | e428e9d | 2007-01-04 03:07:57 +0000 | [diff] [blame] | 204 | break; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 205 | } |
Denis Vlasenko | 2e7dc5d | 2008-11-19 07:59:49 +0000 | [diff] [blame] | 206 | start[n] = '\0'; |
Denis Vlasenko | 93d0776 | 2008-10-04 16:40:17 +0000 | [diff] [blame] | 207 | |
Denis Vlasenko | 93d0776 | 2008-10-04 16:40:17 +0000 | [diff] [blame] | 208 | /* Process each newline-terminated line in the buffer */ |
Denis Vlasenko | 58a8891 | 2008-11-19 09:35:00 +0000 | [diff] [blame] | 209 | start = log_buffer; |
Denis Vlasenko | 93d0776 | 2008-10-04 16:40:17 +0000 | [diff] [blame] | 210 | while (1) { |
Denis Vlasenko | 2e7dc5d | 2008-11-19 07:59:49 +0000 | [diff] [blame] | 211 | char *newline = strchrnul(start, '\n'); |
Denis Vlasenko | 93d0776 | 2008-10-04 16:40:17 +0000 | [diff] [blame] | 212 | |
Denis Vlasenko | 2e7dc5d | 2008-11-19 07:59:49 +0000 | [diff] [blame] | 213 | if (*newline == '\0') { |
Denys Vlasenko | 0016bce | 2010-10-19 23:07:49 +0200 | [diff] [blame] | 214 | /* This line is incomplete */ |
| 215 | |
| 216 | /* move it to the front of the buffer */ |
| 217 | overlapping_strcpy(log_buffer, start); |
| 218 | used = newline - start; |
| 219 | if (used < KLOGD_LOGBUF_SIZE-1) { |
| 220 | /* buffer isn't full */ |
Denis Vlasenko | 93d0776 | 2008-10-04 16:40:17 +0000 | [diff] [blame] | 221 | break; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 222 | } |
Denys Vlasenko | 0016bce | 2010-10-19 23:07:49 +0200 | [diff] [blame] | 223 | /* buffer is full, log it anyway */ |
Denis Vlasenko | 93d0776 | 2008-10-04 16:40:17 +0000 | [diff] [blame] | 224 | used = 0; |
Denis Vlasenko | 2e7dc5d | 2008-11-19 07:59:49 +0000 | [diff] [blame] | 225 | newline = NULL; |
Denis Vlasenko | 93d0776 | 2008-10-04 16:40:17 +0000 | [diff] [blame] | 226 | } else { |
| 227 | *newline++ = '\0'; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 228 | } |
Denis Vlasenko | 93d0776 | 2008-10-04 16:40:17 +0000 | [diff] [blame] | 229 | |
| 230 | /* Extract the priority */ |
| 231 | priority = LOG_INFO; |
| 232 | if (*start == '<') { |
| 233 | start++; |
| 234 | if (*start) { |
| 235 | /* kernel never generates multi-digit prios */ |
| 236 | priority = (*start - '0'); |
| 237 | start++; |
| 238 | } |
Denis Vlasenko | 2e7dc5d | 2008-11-19 07:59:49 +0000 | [diff] [blame] | 239 | if (*start == '>') |
Denis Vlasenko | 93d0776 | 2008-10-04 16:40:17 +0000 | [diff] [blame] | 240 | start++; |
Denis Vlasenko | 93d0776 | 2008-10-04 16:40:17 +0000 | [diff] [blame] | 241 | } |
Denis Vlasenko | 2e7dc5d | 2008-11-19 07:59:49 +0000 | [diff] [blame] | 242 | /* Log (only non-empty lines) */ |
Denis Vlasenko | 93d0776 | 2008-10-04 16:40:17 +0000 | [diff] [blame] | 243 | if (*start) |
| 244 | syslog(priority, "%s", start); |
Denis Vlasenko | 2e7dc5d | 2008-11-19 07:59:49 +0000 | [diff] [blame] | 245 | |
Denis Vlasenko | 93d0776 | 2008-10-04 16:40:17 +0000 | [diff] [blame] | 246 | if (!newline) |
| 247 | break; |
| 248 | start = newline; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 251 | |
Jeremie Koenig | 63c2e7e | 2010-08-01 03:01:44 +0200 | [diff] [blame] | 252 | klogd_close(); |
| 253 | syslog(LOG_NOTICE, "klogd: exiting"); |
| 254 | if (bb_got_signal) |
| 255 | kill_myself_with_sig(bb_got_signal); |
Denis Vlasenko | e428e9d | 2007-01-04 03:07:57 +0000 | [diff] [blame] | 256 | return EXIT_FAILURE; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 257 | } |