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 | * |
| 5 | * Copyright (C) 2001 by Gennady Feldman <gfeldman@cachier.com>. |
| 6 | * Changes: Made this a standalone busybox module which uses standalone |
| 7 | * syslog() client interface. |
| 8 | * |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 9 | * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen |
| 10 | * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org> |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 11 | * |
| 12 | * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org> |
| 13 | * |
| 14 | * "circular buffer" Copyright (C) 2000 by Gennady Feldman <gfeldman@mail.com> |
| 15 | * |
Mark Whitley | 6bff9cc | 2001-03-12 23:41:34 +0000 | [diff] [blame] | 16 | * Maintainer: Gennady Feldman <gena01@cachier.com> as of Mar 12, 2001 |
| 17 | * |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 18 | * This program is free software; you can redistribute it and/or modify |
| 19 | * it under the terms of the GNU General Public License as published by |
| 20 | * the Free Software Foundation; either version 2 of the License, or |
| 21 | * (at your option) any later version. |
| 22 | * |
| 23 | * This program is distributed in the hope that it will be useful, |
| 24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 26 | * General Public License for more details. |
| 27 | * |
| 28 | * You should have received a copy of the GNU General Public License |
| 29 | * along with this program; if not, write to the Free Software |
| 30 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include <stdio.h> |
| 35 | #include <stdlib.h> |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame^] | 36 | #include <signal.h> /* for our signal() handlers */ |
| 37 | #include <string.h> /* strncpy() */ |
| 38 | #include <errno.h> /* errno and friends */ |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 39 | #include <unistd.h> |
| 40 | #include <ctype.h> |
| 41 | #include <sys/syslog.h> |
| 42 | |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 43 | #if __GNU_LIBRARY__ < 5 |
| 44 | # ifdef __alpha__ |
| 45 | # define klogctl syslog |
| 46 | # endif |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 47 | #else |
| 48 | # include <sys/klog.h> |
| 49 | #endif |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 50 | |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 51 | #include "busybox.h" |
| 52 | |
| 53 | static void klogd_signal(int sig) |
| 54 | { |
| 55 | klogctl(7, NULL, 0); |
| 56 | klogctl(0, 0, 0); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame^] | 57 | /* logMessage(0, "Kernel log daemon exiting."); */ |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 58 | syslog_msg(LOG_DAEMON, 0, "Kernel log daemon exiting."); |
| 59 | exit(TRUE); |
| 60 | } |
| 61 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame^] | 62 | static void doKlogd(void) __attribute__ ((noreturn)); |
| 63 | static void doKlogd(void) |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 64 | { |
| 65 | int priority = LOG_INFO; |
| 66 | char log_buffer[4096]; |
| 67 | int i, n, lastc; |
| 68 | char *start; |
| 69 | |
| 70 | /* Set up sig handlers */ |
| 71 | signal(SIGINT, klogd_signal); |
| 72 | signal(SIGKILL, klogd_signal); |
| 73 | signal(SIGTERM, klogd_signal); |
| 74 | signal(SIGHUP, SIG_IGN); |
| 75 | |
| 76 | /* "Open the log. Currently a NOP." */ |
| 77 | klogctl(1, NULL, 0); |
| 78 | |
Matt Kraai | 6ba1a80 | 2001-04-12 20:11:55 +0000 | [diff] [blame] | 79 | syslog_msg(LOG_DAEMON, 0, "klogd started: " BB_BANNER); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 80 | |
| 81 | while (1) { |
| 82 | /* Use kernel syscalls */ |
| 83 | memset(log_buffer, '\0', sizeof(log_buffer)); |
| 84 | n = klogctl(2, log_buffer, sizeof(log_buffer)); |
| 85 | if (n < 0) { |
| 86 | char message[80]; |
| 87 | |
| 88 | if (errno == EINTR) |
| 89 | continue; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame^] | 90 | snprintf(message, 79, |
| 91 | "klogd: Error return from sys_sycall: %d - %s.\n", errno, |
| 92 | strerror(errno)); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 93 | syslog_msg(LOG_DAEMON, LOG_SYSLOG | LOG_ERR, message); |
| 94 | exit(1); |
| 95 | } |
| 96 | |
| 97 | /* klogctl buffer parsing modelled after code in dmesg.c */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame^] | 98 | start = &log_buffer[0]; |
| 99 | lastc = '\0'; |
| 100 | for (i = 0; i < n; i++) { |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 101 | if (lastc == '\0' && log_buffer[i] == '<') { |
| 102 | priority = 0; |
| 103 | i++; |
| 104 | while (isdigit(log_buffer[i])) { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame^] | 105 | priority = priority * 10 + (log_buffer[i] - '0'); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 106 | i++; |
| 107 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame^] | 108 | if (log_buffer[i] == '>') |
| 109 | i++; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 110 | start = &log_buffer[i]; |
| 111 | } |
| 112 | if (log_buffer[i] == '\n') { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame^] | 113 | log_buffer[i] = '\0'; /* zero terminate this message */ |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 114 | syslog_msg(LOG_DAEMON, LOG_KERN | priority, start); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame^] | 115 | start = &log_buffer[i + 1]; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 116 | priority = LOG_INFO; |
| 117 | } |
| 118 | lastc = log_buffer[i]; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 123 | extern int klogd_main(int argc, char **argv) |
| 124 | { |
| 125 | /* no options, no getopt */ |
Eric Andersen | e5c24df | 2001-03-29 21:58:33 +0000 | [diff] [blame] | 126 | int opt; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 127 | int doFork = TRUE; |
| 128 | |
| 129 | /* do normal option parsing */ |
| 130 | while ((opt = getopt(argc, argv, "n")) > 0) { |
| 131 | switch (opt) { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame^] | 132 | case 'n': |
| 133 | doFork = FALSE; |
| 134 | break; |
| 135 | default: |
| 136 | show_usage(); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 140 | if (doFork) { |
Eric Andersen | 72f9a42 | 2001-10-28 05:12:20 +0000 | [diff] [blame] | 141 | #if !defined(__UCLIBC__) || defined(__UCLIBC_HAS_MMU__) |
Eric Andersen | e5c24df | 2001-03-29 21:58:33 +0000 | [diff] [blame] | 142 | if (daemon(0, 1) < 0) |
| 143 | perror_msg_and_die("daemon"); |
Eric Andersen | 72f9a42 | 2001-10-28 05:12:20 +0000 | [diff] [blame] | 144 | #else |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame^] | 145 | error_msg_and_die("daemon not supported"); |
Eric Andersen | 72f9a42 | 2001-10-28 05:12:20 +0000 | [diff] [blame] | 146 | #endif |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 147 | } |
Eric Andersen | e5c24df | 2001-03-29 21:58:33 +0000 | [diff] [blame] | 148 | doKlogd(); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame^] | 149 | |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 150 | return EXIT_SUCCESS; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | Local Variables |
| 155 | c-file-style: "linux" |
| 156 | c-basic-offset: 4 |
| 157 | tab-width: 4 |
| 158 | End: |
| 159 | */ |