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 | * |
| 9 | * Copyright (C) 1999,2000,2001 by Lineo, inc. |
| 10 | * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> |
| 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> |
| 36 | #include <signal.h> /* for our signal() handlers */ |
| 37 | #include <string.h> /* strncpy() */ |
| 38 | #include <errno.h> /* errno and friends */ |
| 39 | #include <unistd.h> |
| 40 | #include <ctype.h> |
| 41 | #include <sys/syslog.h> |
| 42 | |
| 43 | #if ! defined __GLIBC__ && ! defined __UCLIBC__ |
| 44 | #include <sys/syscall.h> |
| 45 | #include <linux/unistd.h> |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 46 | |
| 47 | #ifndef __alpha__ |
| 48 | # define __NR_klogctl __NR_syslog |
| 49 | static inline _syscall3(int, klogctl, int, type, char *, b, int, len); |
| 50 | #else /* __alpha__ */ |
| 51 | #define klogctl syslog |
| 52 | #endif |
| 53 | |
| 54 | #else |
| 55 | # include <sys/klog.h> |
| 56 | #endif |
| 57 | #include "busybox.h" |
| 58 | |
| 59 | static void klogd_signal(int sig) |
| 60 | { |
| 61 | klogctl(7, NULL, 0); |
| 62 | klogctl(0, 0, 0); |
| 63 | //logMessage(0, "Kernel log daemon exiting."); |
| 64 | syslog_msg(LOG_DAEMON, 0, "Kernel log daemon exiting."); |
| 65 | exit(TRUE); |
| 66 | } |
| 67 | |
| 68 | static void doKlogd (void) __attribute__ ((noreturn)); |
| 69 | static void doKlogd (void) |
| 70 | { |
| 71 | int priority = LOG_INFO; |
| 72 | char log_buffer[4096]; |
| 73 | int i, n, lastc; |
| 74 | char *start; |
| 75 | |
| 76 | /* Set up sig handlers */ |
| 77 | signal(SIGINT, klogd_signal); |
| 78 | signal(SIGKILL, klogd_signal); |
| 79 | signal(SIGTERM, klogd_signal); |
| 80 | signal(SIGHUP, SIG_IGN); |
| 81 | |
| 82 | /* "Open the log. Currently a NOP." */ |
| 83 | klogctl(1, NULL, 0); |
| 84 | |
| 85 | syslog_msg(LOG_DAEMON, 0, "klogd started: BusyBox v" BB_VER " (" BB_BT ")"); |
| 86 | |
| 87 | while (1) { |
| 88 | /* Use kernel syscalls */ |
| 89 | memset(log_buffer, '\0', sizeof(log_buffer)); |
| 90 | n = klogctl(2, log_buffer, sizeof(log_buffer)); |
| 91 | if (n < 0) { |
| 92 | char message[80]; |
| 93 | |
| 94 | if (errno == EINTR) |
| 95 | continue; |
| 96 | snprintf(message, 79, "klogd: Error return from sys_sycall: %d - %s.\n", |
| 97 | errno, strerror(errno)); |
| 98 | syslog_msg(LOG_DAEMON, LOG_SYSLOG | LOG_ERR, message); |
| 99 | exit(1); |
| 100 | } |
| 101 | |
| 102 | /* klogctl buffer parsing modelled after code in dmesg.c */ |
| 103 | start=&log_buffer[0]; |
| 104 | lastc='\0'; |
| 105 | for (i=0; i<n; i++) { |
| 106 | if (lastc == '\0' && log_buffer[i] == '<') { |
| 107 | priority = 0; |
| 108 | i++; |
| 109 | while (isdigit(log_buffer[i])) { |
| 110 | priority = priority*10+(log_buffer[i]-'0'); |
| 111 | i++; |
| 112 | } |
| 113 | if (log_buffer[i] == '>') i++; |
| 114 | start = &log_buffer[i]; |
| 115 | } |
| 116 | if (log_buffer[i] == '\n') { |
| 117 | log_buffer[i] = '\0'; /* zero terminate this message */ |
| 118 | syslog_msg(LOG_DAEMON, LOG_KERN | priority, start); |
| 119 | start = &log_buffer[i+1]; |
| 120 | priority = LOG_INFO; |
| 121 | } |
| 122 | lastc = log_buffer[i]; |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 127 | extern int klogd_main(int argc, char **argv) |
| 128 | { |
| 129 | /* no options, no getopt */ |
Eric Andersen | e5c24df | 2001-03-29 21:58:33 +0000 | [diff] [blame] | 130 | int opt; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 131 | int doFork = TRUE; |
| 132 | |
| 133 | /* do normal option parsing */ |
| 134 | while ((opt = getopt(argc, argv, "n")) > 0) { |
| 135 | switch (opt) { |
| 136 | case 'n': |
| 137 | doFork = FALSE; |
| 138 | break; |
| 139 | default: |
| 140 | show_usage(); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | if (doFork == TRUE) { |
Eric Andersen | e5c24df | 2001-03-29 21:58:33 +0000 | [diff] [blame] | 145 | if (daemon(0, 1) < 0) |
| 146 | perror_msg_and_die("daemon"); |
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(); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 149 | |
| 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 | */ |