blob: 6a675b8b3fdb5faa5c18e7896de2a26344420652 [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"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000021#include <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 */
Denis Vlasenko25591c32008-02-16 22:58:56 +000060 bb_signals(0
61 + (1 << SIGINT)
62 + (1 << SIGTERM)
63 , klogd_signal);
Mark Whitley6317c4b2001-03-12 22:51:50 +000064 signal(SIGHUP, SIG_IGN);
65
66 /* "Open the log. Currently a NOP." */
67 klogctl(1, NULL, 0);
68
Denis Vlasenkoc84520d2007-02-17 14:12:10 +000069 /* Set level of kernel console messaging. */
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000070 if (option_mask32 & OPT_LEVEL)
71 klogctl(8, NULL, i);
Glenn L McGrathe1ad6722002-12-01 11:31:58 +000072
Denis Vlasenkoca525b42007-06-13 12:27:17 +000073 syslog(LOG_NOTICE, "klogd started: %s", bb_banner);
Mark Whitley6317c4b2001-03-12 22:51:50 +000074
Denis Vlasenkoc84520d2007-02-17 14:12:10 +000075 /* Note: this code does not detect incomplete messages
76 * (messages not ending with '\n' or just when kernel
77 * generates too many messages for us to keep up)
78 * and will split them in two separate lines */
Mark Whitley6317c4b2001-03-12 22:51:50 +000079 while (1) {
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000080 int n;
81 int priority;
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000082
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000083 n = klogctl(2, log_buffer, KLOGD_LOGBUF_SIZE - 1);
Mark Whitley6317c4b2001-03-12 22:51:50 +000084 if (n < 0) {
Mark Whitley6317c4b2001-03-12 22:51:50 +000085 if (errno == EINTR)
86 continue;
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000087 syslog(LOG_ERR, "klogd: error from klogctl(2): %d - %m",
Denis Vlasenkoc84520d2007-02-17 14:12:10 +000088 errno);
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000089 break;
Mark Whitley6317c4b2001-03-12 22:51:50 +000090 }
Denis Vlasenkoc84520d2007-02-17 14:12:10 +000091 log_buffer[n] = '\n';
92 i = 0;
93 while (i < n) {
94 priority = LOG_INFO;
95 start = &log_buffer[i];
96 if (log_buffer[i] == '<') {
Mark Whitley6317c4b2001-03-12 22:51:50 +000097 i++;
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000098 // kernel never ganerates multi-digit prios
99 //priority = 0;
100 //while (log_buffer[i] >= '0' && log_buffer[i] <= '9') {
101 // priority = priority * 10 + (log_buffer[i] - '0');
102 // i++;
103 //}
104 if (isdigit(log_buffer[i])) {
105 priority = (log_buffer[i] - '0');
Mark Whitley6317c4b2001-03-12 22:51:50 +0000106 i++;
107 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000108 if (log_buffer[i] == '>')
109 i++;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000110 start = &log_buffer[i];
111 }
Denis Vlasenkoc84520d2007-02-17 14:12:10 +0000112 while (log_buffer[i] != '\n')
113 i++;
114 log_buffer[i] = '\0';
115 syslog(priority, "%s", start);
116 i++;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000117 }
118 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000119
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +0000120 return EXIT_FAILURE;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000121}