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 |
| 7 | * syslog() client interface. |
| 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 | * |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 17 | * This program is free software; you can redistribute it and/or modify |
| 18 | * it under the terms of the GNU General Public License as published by |
| 19 | * the Free Software Foundation; either version 2 of the License, or |
| 20 | * (at your option) any later version. |
| 21 | * |
| 22 | * This program is distributed in the hope that it will be useful, |
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 25 | * General Public License for more details. |
| 26 | * |
| 27 | * You should have received a copy of the GNU General Public License |
| 28 | * along with this program; if not, write to the Free Software |
| 29 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 30 | * |
| 31 | */ |
| 32 | |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 35 | #include <signal.h> /* for our signal() handlers */ |
| 36 | #include <string.h> /* strncpy() */ |
| 37 | #include <errno.h> /* errno and friends */ |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 38 | #include <unistd.h> |
| 39 | #include <ctype.h> |
| 40 | #include <sys/syslog.h> |
Eric Andersen | 85e5e72 | 2003-07-22 08:56:55 +0000 | [diff] [blame] | 41 | #include <sys/klog.h> |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 42 | |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 43 | #include "busybox.h" |
| 44 | |
| 45 | static void klogd_signal(int sig) |
| 46 | { |
| 47 | klogctl(7, NULL, 0); |
| 48 | klogctl(0, 0, 0); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 49 | /* logMessage(0, "Kernel log daemon exiting."); */ |
Eric Andersen | 36adca8 | 2004-06-22 10:07:17 +0000 | [diff] [blame] | 50 | syslog(LOG_NOTICE, "Kernel log daemon exiting."); |
| 51 | exit(EXIT_SUCCESS); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Eric Andersen | 0373cf1 | 2004-06-25 11:23:03 +0000 | [diff] [blame] | 54 | static void doKlogd(const int console_log_level) __attribute__ ((noreturn)); |
| 55 | static void doKlogd(const int console_log_level) |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 56 | { |
| 57 | int priority = LOG_INFO; |
| 58 | char log_buffer[4096]; |
| 59 | int i, n, lastc; |
| 60 | char *start; |
| 61 | |
Eric Andersen | 36adca8 | 2004-06-22 10:07:17 +0000 | [diff] [blame] | 62 | openlog("kernel", 0, LOG_KERN); |
| 63 | |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 64 | /* Set up sig handlers */ |
| 65 | signal(SIGINT, klogd_signal); |
| 66 | signal(SIGKILL, klogd_signal); |
| 67 | signal(SIGTERM, klogd_signal); |
| 68 | signal(SIGHUP, SIG_IGN); |
| 69 | |
| 70 | /* "Open the log. Currently a NOP." */ |
| 71 | klogctl(1, NULL, 0); |
| 72 | |
Glenn L McGrath | e1ad672 | 2002-12-01 11:31:58 +0000 | [diff] [blame] | 73 | /* Set level of kernel console messaging.. */ |
Eric Andersen | 0373cf1 | 2004-06-25 11:23:03 +0000 | [diff] [blame] | 74 | if (console_log_level != -1) |
Glenn L McGrath | e1ad672 | 2002-12-01 11:31:58 +0000 | [diff] [blame] | 75 | klogctl(8, NULL, console_log_level); |
| 76 | |
Eric Andersen | 36adca8 | 2004-06-22 10:07:17 +0000 | [diff] [blame] | 77 | syslog(LOG_NOTICE, "klogd started: " BB_BANNER); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 78 | |
| 79 | while (1) { |
| 80 | /* Use kernel syscalls */ |
| 81 | memset(log_buffer, '\0', sizeof(log_buffer)); |
| 82 | n = klogctl(2, log_buffer, sizeof(log_buffer)); |
| 83 | if (n < 0) { |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 84 | if (errno == EINTR) |
| 85 | continue; |
Eric Andersen | 0373cf1 | 2004-06-25 11:23:03 +0000 | [diff] [blame] | 86 | syslog(LOG_ERR, "klogd: Error return from sys_sycall: %d - %m.\n", errno); |
Eric Andersen | 36adca8 | 2004-06-22 10:07:17 +0000 | [diff] [blame] | 87 | exit(EXIT_FAILURE); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /* klogctl buffer parsing modelled after code in dmesg.c */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 91 | start = &log_buffer[0]; |
| 92 | lastc = '\0'; |
| 93 | for (i = 0; i < n; i++) { |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 94 | if (lastc == '\0' && log_buffer[i] == '<') { |
| 95 | priority = 0; |
| 96 | i++; |
| 97 | while (isdigit(log_buffer[i])) { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 98 | priority = priority * 10 + (log_buffer[i] - '0'); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 99 | i++; |
| 100 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 101 | if (log_buffer[i] == '>') |
| 102 | i++; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 103 | start = &log_buffer[i]; |
| 104 | } |
| 105 | if (log_buffer[i] == '\n') { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 106 | log_buffer[i] = '\0'; /* zero terminate this message */ |
Glenn L McGrath | 12ed333 | 2004-08-06 00:58:53 +0000 | [diff] [blame] | 107 | syslog(priority, "%s", start); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 108 | start = &log_buffer[i + 1]; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 109 | priority = LOG_INFO; |
| 110 | } |
| 111 | lastc = log_buffer[i]; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
Rob Landley | 570f655 | 2005-09-14 15:36:52 +0000 | [diff] [blame] | 116 | #define OPT_LEVEL 1 |
| 117 | #define OPT_FOREGROUND 2 |
| 118 | |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 119 | extern int klogd_main(int argc, char **argv) |
| 120 | { |
Rob Landley | 570f655 | 2005-09-14 15:36:52 +0000 | [diff] [blame] | 121 | unsigned long opt; |
| 122 | char *c_arg; |
| 123 | int console_log_level = -1; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 124 | |
| 125 | /* do normal option parsing */ |
Rob Landley | 570f655 | 2005-09-14 15:36:52 +0000 | [diff] [blame] | 126 | opt = bb_getopt_ulflags (argc, argv, "c:n", &c_arg); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 127 | |
Rob Landley | 570f655 | 2005-09-14 15:36:52 +0000 | [diff] [blame] | 128 | if (opt & OPT_LEVEL) { |
| 129 | /* Valid levels are between 1 and 8 */ |
| 130 | console_log_level = bb_xgetlarg(c_arg, 10, 1, 8); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Rob Landley | 570f655 | 2005-09-14 15:36:52 +0000 | [diff] [blame] | 133 | if (!(opt & OPT_FOREGROUND)) { |
Russ Dill | a1fece2 | 2003-12-15 21:57:44 +0000 | [diff] [blame] | 134 | #if defined(__uClinux__) |
| 135 | vfork_daemon_rexec(0, 1, argc, argv, "-n"); |
| 136 | #else /* __uClinux__ */ |
Eric Andersen | e5c24df | 2001-03-29 21:58:33 +0000 | [diff] [blame] | 137 | if (daemon(0, 1) < 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 138 | bb_perror_msg_and_die("daemon"); |
Russ Dill | a1fece2 | 2003-12-15 21:57:44 +0000 | [diff] [blame] | 139 | #endif /* __uClinux__ */ |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 140 | } |
Glenn L McGrath | e1ad672 | 2002-12-01 11:31:58 +0000 | [diff] [blame] | 141 | doKlogd(console_log_level); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 142 | |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 143 | return EXIT_SUCCESS; |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | Local Variables |
| 148 | c-file-style: "linux" |
| 149 | c-basic-offset: 4 |
| 150 | tab-width: 4 |
| 151 | End: |
| 152 | */ |