blob: d916878bfe66939253b646edaa8aba5928d488e3 [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
35#define KLOGD_LOGBUF_SIZE 4096
36
37int klogd_main(int argc, char **argv)
Mark Whitley6317c4b2001-03-12 22:51:50 +000038{
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000039 RESERVE_CONFIG_BUFFER(log_buffer, KLOGD_LOGBUF_SIZE);
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000040 int i = i; /* silence gcc */
Mark Whitley6317c4b2001-03-12 22:51:50 +000041 char *start;
42
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000043 /* do normal option parsing */
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000044 getopt32(argc, argv, "c:n", &start);
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000045
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000046 if (option_mask32 & OPT_LEVEL) {
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000047 /* Valid levels are between 1 and 8 */
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000048 i = xatoul_range(start, 1, 8);
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000049 }
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000050
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000051 if (!(option_mask32 & OPT_FOREGROUND)) {
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000052#ifdef BB_NOMMU
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000053 vfork_daemon_rexec(0, 1, argc, argv, "-n");
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000054#else
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000055 xdaemon(0, 1);
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000056#endif
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000057 }
58
Eric Andersen36adca82004-06-22 10:07:17 +000059 openlog("kernel", 0, LOG_KERN);
60
Mark Whitley6317c4b2001-03-12 22:51:50 +000061 /* Set up sig handlers */
62 signal(SIGINT, klogd_signal);
63 signal(SIGKILL, klogd_signal);
64 signal(SIGTERM, klogd_signal);
65 signal(SIGHUP, SIG_IGN);
66
67 /* "Open the log. Currently a NOP." */
68 klogctl(1, NULL, 0);
69
Glenn L McGrathe1ad6722002-12-01 11:31:58 +000070 /* Set level of kernel console messaging.. */
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000071 if (option_mask32 & OPT_LEVEL)
72 klogctl(8, NULL, i);
Glenn L McGrathe1ad6722002-12-01 11:31:58 +000073
"Vladimir N. Oleynik"dd1ccdd2006-02-16 15:40:24 +000074 syslog(LOG_NOTICE, "klogd started: %s", BB_BANNER);
Mark Whitley6317c4b2001-03-12 22:51:50 +000075
76 while (1) {
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000077 int n;
78 int priority;
79 char lastc;
80
Mark Whitley6317c4b2001-03-12 22:51:50 +000081 /* Use kernel syscalls */
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000082 memset(log_buffer, '\0', KLOGD_LOGBUF_SIZE);
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000083 /* It will be null-terminted */
84 n = klogctl(2, log_buffer, KLOGD_LOGBUF_SIZE - 1);
Mark Whitley6317c4b2001-03-12 22:51:50 +000085 if (n < 0) {
Mark Whitley6317c4b2001-03-12 22:51:50 +000086 if (errno == EINTR)
87 continue;
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000088 syslog(LOG_ERR, "klogd: error from klogctl(2): %d - %m",
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000089 errno);
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000090 break;
Mark Whitley6317c4b2001-03-12 22:51:50 +000091 }
92
93 /* klogctl buffer parsing modelled after code in dmesg.c */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000094 start = &log_buffer[0];
95 lastc = '\0';
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +000096 priority = LOG_INFO;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000097 for (i = 0; i < n; i++) {
Mark Whitley6317c4b2001-03-12 22:51:50 +000098 if (lastc == '\0' && 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 }
114 if (log_buffer[i] == '\n') {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000115 log_buffer[i] = '\0'; /* zero terminate this message */
Glenn L McGrath12ed3332004-08-06 00:58:53 +0000116 syslog(priority, "%s", start);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000117 start = &log_buffer[i + 1];
Mark Whitley6317c4b2001-03-12 22:51:50 +0000118 priority = LOG_INFO;
119 }
120 lastc = log_buffer[i];
121 }
122 }
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +0000123 if (ENABLE_FEATURE_CLEAN_UP)
124 RELEASE_CONFIG_BUFFER(log_buffer);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000125
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +0000126 return EXIT_FAILURE;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000127}