blob: 5beec14dbeac05fd42cff1b72f3e3249f24a2765 [file] [log] [blame]
Mark Whitley6317c4b2001-03-12 22:51:50 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini klogd implementation for busybox
4 *
Glenn L McGrath6ed77592002-12-12 10:54:48 +00005 * Copyright (C) 2001 by Gennady Feldman <gfeldman@gena01.com>.
Mark Whitley6317c4b2001-03-12 22:51:50 +00006 * Changes: Made this a standalone busybox module which uses standalone
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +00007 * syslog() client interface.
Mark Whitley6317c4b2001-03-12 22:51:50 +00008 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00009 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Mark Whitley6317c4b2001-03-12 22:51:50 +000010 *
11 * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org>
12 *
Glenn L McGrath6ed77592002-12-12 10:54:48 +000013 * "circular buffer" Copyright (C) 2000 by Gennady Feldman <gfeldman@gena01.com>
Mark Whitley6317c4b2001-03-12 22:51:50 +000014 *
Glenn L McGrath6ed77592002-12-12 10:54:48 +000015 * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001
Mark Whitley6bff9cc2001-03-12 23:41:34 +000016 *
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000017 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Mark Whitley6317c4b2001-03-12 22:51:50 +000018 */
19
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000020#include "libbb.h"
Mark Whitley6317c4b2001-03-12 22:51:50 +000021#include <sys/syslog.h>
Eric Andersen85e5e722003-07-22 08:56:55 +000022#include <sys/klog.h>
Eric Andersene76c3b02001-04-05 03:14:39 +000023
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000024static void klogd_signal(int sig ATTRIBUTE_UNUSED)
Mark Whitley6317c4b2001-03-12 22:51:50 +000025{
26 klogctl(7, NULL, 0);
27 klogctl(0, 0, 0);
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000028 syslog(LOG_NOTICE, "Kernel log daemon exiting");
Eric Andersen36adca82004-06-22 10:07:17 +000029 exit(EXIT_SUCCESS);
Mark Whitley6317c4b2001-03-12 22:51:50 +000030}
31
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000032#define OPT_LEVEL 1
33#define OPT_FOREGROUND 2
34
Denis Vlasenkoc84520d2007-02-17 14:12:10 +000035#define KLOGD_LOGBUF_SIZE BUFSIZ
36#define log_buffer bb_common_bufsiz1
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000037
Denis Vlasenko06af2162007-02-03 17:28:39 +000038int klogd_main(int argc, char **argv);
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000039int klogd_main(int argc, char **argv)
Mark Whitley6317c4b2001-03-12 22:51:50 +000040{
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000041 int i = i; /* silence gcc */
Mark Whitley6317c4b2001-03-12 22:51:50 +000042 char *start;
43
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000044 /* do normal option parsing */
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000045 getopt32(argc, argv, "c:n", &start);
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000046
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000047 if (option_mask32 & OPT_LEVEL) {
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000048 /* Valid levels are between 1 and 8 */
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000049 i = xatoul_range(start, 1, 8);
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000050 }
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000051
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000052 if (!(option_mask32 & OPT_FOREGROUND)) {
Denis Vlasenko5a142022007-03-26 13:20:54 +000053 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000054 }
55
Eric Andersen36adca82004-06-22 10:07:17 +000056 openlog("kernel", 0, LOG_KERN);
57
Mark Whitley6317c4b2001-03-12 22:51:50 +000058 /* Set up sig handlers */
59 signal(SIGINT, klogd_signal);
60 signal(SIGKILL, klogd_signal);
61 signal(SIGTERM, klogd_signal);
62 signal(SIGHUP, SIG_IGN);
63
64 /* "Open the log. Currently a NOP." */
65 klogctl(1, NULL, 0);
66
Denis Vlasenkoc84520d2007-02-17 14:12:10 +000067 /* Set level of kernel console messaging. */
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000068 if (option_mask32 & OPT_LEVEL)
69 klogctl(8, NULL, i);
Glenn L McGrathe1ad6722002-12-01 11:31:58 +000070
"Vladimir N. Oleynik"dd1ccdd2006-02-16 15:40:24 +000071 syslog(LOG_NOTICE, "klogd started: %s", BB_BANNER);
Mark Whitley6317c4b2001-03-12 22:51:50 +000072
Denis Vlasenkoc84520d2007-02-17 14:12:10 +000073 /* Note: this code does not detect incomplete messages
74 * (messages not ending with '\n' or just when kernel
75 * generates too many messages for us to keep up)
76 * and will split them in two separate lines */
Mark Whitley6317c4b2001-03-12 22:51:50 +000077 while (1) {
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000078 int n;
79 int priority;
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000080
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000081 n = klogctl(2, log_buffer, KLOGD_LOGBUF_SIZE - 1);
Mark Whitley6317c4b2001-03-12 22:51:50 +000082 if (n < 0) {
Mark Whitley6317c4b2001-03-12 22:51:50 +000083 if (errno == EINTR)
84 continue;
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000085 syslog(LOG_ERR, "klogd: error from klogctl(2): %d - %m",
Denis Vlasenkoc84520d2007-02-17 14:12:10 +000086 errno);
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000087 break;
Mark Whitley6317c4b2001-03-12 22:51:50 +000088 }
Denis Vlasenkoc84520d2007-02-17 14:12:10 +000089 log_buffer[n] = '\n';
90 i = 0;
91 while (i < n) {
92 priority = LOG_INFO;
93 start = &log_buffer[i];
94 if (log_buffer[i] == '<') {
Mark Whitley6317c4b2001-03-12 22:51:50 +000095 i++;
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000096 // kernel never ganerates multi-digit prios
97 //priority = 0;
98 //while (log_buffer[i] >= '0' && log_buffer[i] <= '9') {
99 // priority = priority * 10 + (log_buffer[i] - '0');
100 // i++;
101 //}
102 if (isdigit(log_buffer[i])) {
103 priority = (log_buffer[i] - '0');
Mark Whitley6317c4b2001-03-12 22:51:50 +0000104 i++;
105 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000106 if (log_buffer[i] == '>')
107 i++;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000108 start = &log_buffer[i];
109 }
Denis Vlasenkoc84520d2007-02-17 14:12:10 +0000110 while (log_buffer[i] != '\n')
111 i++;
112 log_buffer[i] = '\0';
113 syslog(priority, "%s", start);
114 i++;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000115 }
116 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000117
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +0000118 return EXIT_FAILURE;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000119}