blob: cca6f56711672e77f369283e533b0dcfd7a6b081 [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 Vlasenkoe428e9d2007-01-04 03:07:57 +000054 vfork_daemon_rexec(0, 1, argc, argv, "-n");
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000055#else
Denis Vlasenkoc84520d2007-02-17 14:12:10 +000056 bb_daemonize();
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000057#endif
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000058 }
59
Eric Andersen36adca82004-06-22 10:07:17 +000060 openlog("kernel", 0, LOG_KERN);
61
Mark Whitley6317c4b2001-03-12 22:51:50 +000062 /* Set up sig handlers */
63 signal(SIGINT, klogd_signal);
64 signal(SIGKILL, klogd_signal);
65 signal(SIGTERM, klogd_signal);
66 signal(SIGHUP, SIG_IGN);
67
68 /* "Open the log. Currently a NOP." */
69 klogctl(1, NULL, 0);
70
Denis Vlasenkoc84520d2007-02-17 14:12:10 +000071 /* Set level of kernel console messaging. */
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000072 if (option_mask32 & OPT_LEVEL)
73 klogctl(8, NULL, i);
Glenn L McGrathe1ad6722002-12-01 11:31:58 +000074
"Vladimir N. Oleynik"dd1ccdd2006-02-16 15:40:24 +000075 syslog(LOG_NOTICE, "klogd started: %s", BB_BANNER);
Mark Whitley6317c4b2001-03-12 22:51:50 +000076
Denis Vlasenkoc84520d2007-02-17 14:12:10 +000077 /* Note: this code does not detect incomplete messages
78 * (messages not ending with '\n' or just when kernel
79 * generates too many messages for us to keep up)
80 * and will split them in two separate lines */
Mark Whitley6317c4b2001-03-12 22:51:50 +000081 while (1) {
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000082 int n;
83 int priority;
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000084
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000085 n = klogctl(2, log_buffer, KLOGD_LOGBUF_SIZE - 1);
Mark Whitley6317c4b2001-03-12 22:51:50 +000086 if (n < 0) {
Mark Whitley6317c4b2001-03-12 22:51:50 +000087 if (errno == EINTR)
88 continue;
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000089 syslog(LOG_ERR, "klogd: error from klogctl(2): %d - %m",
Denis Vlasenkoc84520d2007-02-17 14:12:10 +000090 errno);
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000091 break;
Mark Whitley6317c4b2001-03-12 22:51:50 +000092 }
Denis Vlasenkoc84520d2007-02-17 14:12:10 +000093 log_buffer[n] = '\n';
94 i = 0;
95 while (i < n) {
96 priority = LOG_INFO;
97 start = &log_buffer[i];
98 if (log_buffer[i] == '<') {
Mark Whitley6317c4b2001-03-12 22:51:50 +000099 i++;
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +0000100 // kernel never ganerates multi-digit prios
101 //priority = 0;
102 //while (log_buffer[i] >= '0' && log_buffer[i] <= '9') {
103 // priority = priority * 10 + (log_buffer[i] - '0');
104 // i++;
105 //}
106 if (isdigit(log_buffer[i])) {
107 priority = (log_buffer[i] - '0');
Mark Whitley6317c4b2001-03-12 22:51:50 +0000108 i++;
109 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000110 if (log_buffer[i] == '>')
111 i++;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000112 start = &log_buffer[i];
113 }
Denis Vlasenkoc84520d2007-02-17 14:12:10 +0000114 while (log_buffer[i] != '\n')
115 i++;
116 log_buffer[i] = '\0';
117 syslog(priority, "%s", start);
118 i++;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000119 }
120 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000121
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +0000122 return EXIT_FAILURE;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000123}