blob: fc0ed303769ca835244de37b8c2e015c97d11e15 [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
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000020#include "busybox.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)) {
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000053#ifdef BB_NOMMU
Denis Vlasenko9067f132007-03-24 12:11:17 +000054 if (!re_execed)
55 vfork_daemon_rexec(0, 1, argv);
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000056#else
Denis Vlasenkoc84520d2007-02-17 14:12:10 +000057 bb_daemonize();
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000058#endif
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000059 }
60
Eric Andersen36adca82004-06-22 10:07:17 +000061 openlog("kernel", 0, LOG_KERN);
62
Mark Whitley6317c4b2001-03-12 22:51:50 +000063 /* Set up sig handlers */
64 signal(SIGINT, klogd_signal);
65 signal(SIGKILL, klogd_signal);
66 signal(SIGTERM, klogd_signal);
67 signal(SIGHUP, SIG_IGN);
68
69 /* "Open the log. Currently a NOP." */
70 klogctl(1, NULL, 0);
71
Denis Vlasenkoc84520d2007-02-17 14:12:10 +000072 /* Set level of kernel console messaging. */
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000073 if (option_mask32 & OPT_LEVEL)
74 klogctl(8, NULL, i);
Glenn L McGrathe1ad6722002-12-01 11:31:58 +000075
"Vladimir N. Oleynik"dd1ccdd2006-02-16 15:40:24 +000076 syslog(LOG_NOTICE, "klogd started: %s", BB_BANNER);
Mark Whitley6317c4b2001-03-12 22:51:50 +000077
Denis Vlasenkoc84520d2007-02-17 14:12:10 +000078 /* Note: this code does not detect incomplete messages
79 * (messages not ending with '\n' or just when kernel
80 * generates too many messages for us to keep up)
81 * and will split them in two separate lines */
Mark Whitley6317c4b2001-03-12 22:51:50 +000082 while (1) {
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000083 int n;
84 int priority;
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000085
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000086 n = klogctl(2, log_buffer, KLOGD_LOGBUF_SIZE - 1);
Mark Whitley6317c4b2001-03-12 22:51:50 +000087 if (n < 0) {
Mark Whitley6317c4b2001-03-12 22:51:50 +000088 if (errno == EINTR)
89 continue;
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000090 syslog(LOG_ERR, "klogd: error from klogctl(2): %d - %m",
Denis Vlasenkoc84520d2007-02-17 14:12:10 +000091 errno);
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000092 break;
Mark Whitley6317c4b2001-03-12 22:51:50 +000093 }
Denis Vlasenkoc84520d2007-02-17 14:12:10 +000094 log_buffer[n] = '\n';
95 i = 0;
96 while (i < n) {
97 priority = LOG_INFO;
98 start = &log_buffer[i];
99 if (log_buffer[i] == '<') {
Mark Whitley6317c4b2001-03-12 22:51:50 +0000100 i++;
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +0000101 // kernel never ganerates multi-digit prios
102 //priority = 0;
103 //while (log_buffer[i] >= '0' && log_buffer[i] <= '9') {
104 // priority = priority * 10 + (log_buffer[i] - '0');
105 // i++;
106 //}
107 if (isdigit(log_buffer[i])) {
108 priority = (log_buffer[i] - '0');
Mark Whitley6317c4b2001-03-12 22:51:50 +0000109 i++;
110 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000111 if (log_buffer[i] == '>')
112 i++;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000113 start = &log_buffer[i];
114 }
Denis Vlasenkoc84520d2007-02-17 14:12:10 +0000115 while (log_buffer[i] != '\n')
116 i++;
117 log_buffer[i] = '\0';
118 syslog(priority, "%s", start);
119 i++;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000120 }
121 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000122
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +0000123 return EXIT_FAILURE;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000124}