blob: 72f3b559ca928c712d6b16cf61cd45eac438ec43 [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);
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000027 klogctl(0, NULL, 0);
28 syslog(LOG_NOTICE, "klogd: exiting");
Eric Andersen36adca82004-06-22 10:07:17 +000029 exit(EXIT_SUCCESS);
Mark Whitley6317c4b2001-03-12 22:51:50 +000030}
31
Denis Vlasenkoc84520d2007-02-17 14:12:10 +000032#define log_buffer bb_common_bufsiz1
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000033enum {
34 KLOGD_LOGBUF_SIZE = sizeof(log_buffer),
35 OPT_LEVEL = (1 << 0),
36 OPT_FOREGROUND = (1 << 1),
37};
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000038
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000039int klogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000040int klogd_main(int argc, char **argv)
Mark Whitley6317c4b2001-03-12 22:51:50 +000041{
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000042 int i = i; /* silence gcc */
Mark Whitley6317c4b2001-03-12 22:51:50 +000043 char *start;
44
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000045 /* do normal option parsing */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000046 getopt32(argv, "c:n", &start);
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000047
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000048 if (option_mask32 & OPT_LEVEL) {
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000049 /* Valid levels are between 1 and 8 */
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000050 i = xatoul_range(start, 1, 8);
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000051 }
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000052
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000053 if (!(option_mask32 & OPT_FOREGROUND)) {
Denis Vlasenko5a142022007-03-26 13:20:54 +000054 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000055 }
56
Eric Andersen36adca82004-06-22 10:07:17 +000057 openlog("kernel", 0, LOG_KERN);
58
Mark Whitley6317c4b2001-03-12 22:51:50 +000059 /* Set up sig handlers */
60 signal(SIGINT, klogd_signal);
61 signal(SIGKILL, klogd_signal);
62 signal(SIGTERM, klogd_signal);
63 signal(SIGHUP, SIG_IGN);
64
65 /* "Open the log. Currently a NOP." */
66 klogctl(1, NULL, 0);
67
Denis Vlasenkoc84520d2007-02-17 14:12:10 +000068 /* Set level of kernel console messaging. */
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000069 if (option_mask32 & OPT_LEVEL)
70 klogctl(8, NULL, i);
Glenn L McGrathe1ad6722002-12-01 11:31:58 +000071
Denis Vlasenkoca525b42007-06-13 12:27:17 +000072 syslog(LOG_NOTICE, "klogd started: %s", bb_banner);
Mark Whitley6317c4b2001-03-12 22:51:50 +000073
Denis Vlasenkoc84520d2007-02-17 14:12:10 +000074 /* Note: this code does not detect incomplete messages
75 * (messages not ending with '\n' or just when kernel
76 * generates too many messages for us to keep up)
77 * and will split them in two separate lines */
Mark Whitley6317c4b2001-03-12 22:51:50 +000078 while (1) {
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000079 int n;
80 int priority;
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000081
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000082 n = klogctl(2, log_buffer, KLOGD_LOGBUF_SIZE - 1);
Mark Whitley6317c4b2001-03-12 22:51:50 +000083 if (n < 0) {
Mark Whitley6317c4b2001-03-12 22:51:50 +000084 if (errno == EINTR)
85 continue;
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000086 syslog(LOG_ERR, "klogd: error from klogctl(2): %d - %m",
Denis Vlasenkoc84520d2007-02-17 14:12:10 +000087 errno);
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000088 break;
Mark Whitley6317c4b2001-03-12 22:51:50 +000089 }
Denis Vlasenkoc84520d2007-02-17 14:12:10 +000090 log_buffer[n] = '\n';
91 i = 0;
92 while (i < n) {
93 priority = LOG_INFO;
94 start = &log_buffer[i];
95 if (log_buffer[i] == '<') {
Mark Whitley6317c4b2001-03-12 22:51:50 +000096 i++;
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000097 // kernel never ganerates multi-digit prios
98 //priority = 0;
99 //while (log_buffer[i] >= '0' && log_buffer[i] <= '9') {
100 // priority = priority * 10 + (log_buffer[i] - '0');
101 // i++;
102 //}
103 if (isdigit(log_buffer[i])) {
104 priority = (log_buffer[i] - '0');
Mark Whitley6317c4b2001-03-12 22:51:50 +0000105 i++;
106 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000107 if (log_buffer[i] == '>')
108 i++;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000109 start = &log_buffer[i];
110 }
Denis Vlasenkoc84520d2007-02-17 14:12:10 +0000111 while (log_buffer[i] != '\n')
112 i++;
113 log_buffer[i] = '\0';
114 syslog(priority, "%s", start);
115 i++;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000116 }
117 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000118
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +0000119 return EXIT_FAILURE;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000120}