blob: 404e2275dbb3427d462fc17b0bc624909f0407c3 [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
20#include <stdio.h>
21#include <stdlib.h>
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000022#include <signal.h> /* for our signal() handlers */
23#include <string.h> /* strncpy() */
24#include <errno.h> /* errno and friends */
Mark Whitley6317c4b2001-03-12 22:51:50 +000025#include <unistd.h>
26#include <ctype.h>
27#include <sys/syslog.h>
Eric Andersen85e5e722003-07-22 08:56:55 +000028#include <sys/klog.h>
Eric Andersene76c3b02001-04-05 03:14:39 +000029
Mark Whitley6317c4b2001-03-12 22:51:50 +000030#include "busybox.h"
31
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000032static void klogd_signal(int sig ATTRIBUTE_UNUSED)
Mark Whitley6317c4b2001-03-12 22:51:50 +000033{
34 klogctl(7, NULL, 0);
35 klogctl(0, 0, 0);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000036 /* logMessage(0, "Kernel log daemon exiting."); */
Eric Andersen36adca82004-06-22 10:07:17 +000037 syslog(LOG_NOTICE, "Kernel log daemon exiting.");
38 exit(EXIT_SUCCESS);
Mark Whitley6317c4b2001-03-12 22:51:50 +000039}
40
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000041#define OPT_LEVEL 1
42#define OPT_FOREGROUND 2
43
44#define KLOGD_LOGBUF_SIZE 4096
45
46int klogd_main(int argc, char **argv)
Mark Whitley6317c4b2001-03-12 22:51:50 +000047{
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000048 RESERVE_CONFIG_BUFFER(log_buffer, KLOGD_LOGBUF_SIZE);
49 int console_log_level = -1;
Mark Whitley6317c4b2001-03-12 22:51:50 +000050 int priority = LOG_INFO;
Mark Whitley6317c4b2001-03-12 22:51:50 +000051 int i, n, lastc;
52 char *start;
53
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000054
55 {
56 unsigned long opt;
57
58 /* do normal option parsing */
59 opt = bb_getopt_ulflags(argc, argv, "c:n", &start);
60
61 if (opt & OPT_LEVEL) {
62 /* Valid levels are between 1 and 8 */
63 console_log_level = bb_xgetlarg(start, 10, 1, 8);
64 }
65
66 if (!(opt & OPT_FOREGROUND)) {
67#ifdef BB_NOMMU
68 vfork_daemon_rexec(0, 1, argc, argv, "-n");
69#else
70 bb_xdaemon(0, 1);
71#endif
72 }
73 }
74
Eric Andersen36adca82004-06-22 10:07:17 +000075 openlog("kernel", 0, LOG_KERN);
76
Mark Whitley6317c4b2001-03-12 22:51:50 +000077 /* Set up sig handlers */
78 signal(SIGINT, klogd_signal);
79 signal(SIGKILL, klogd_signal);
80 signal(SIGTERM, klogd_signal);
81 signal(SIGHUP, SIG_IGN);
82
83 /* "Open the log. Currently a NOP." */
84 klogctl(1, NULL, 0);
85
Glenn L McGrathe1ad6722002-12-01 11:31:58 +000086 /* Set level of kernel console messaging.. */
Eric Andersen0373cf12004-06-25 11:23:03 +000087 if (console_log_level != -1)
Glenn L McGrathe1ad6722002-12-01 11:31:58 +000088 klogctl(8, NULL, console_log_level);
89
"Vladimir N. Oleynik"dd1ccdd2006-02-16 15:40:24 +000090 syslog(LOG_NOTICE, "klogd started: %s", BB_BANNER);
Mark Whitley6317c4b2001-03-12 22:51:50 +000091
92 while (1) {
93 /* Use kernel syscalls */
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000094 memset(log_buffer, '\0', KLOGD_LOGBUF_SIZE);
95 n = klogctl(2, log_buffer, KLOGD_LOGBUF_SIZE);
Mark Whitley6317c4b2001-03-12 22:51:50 +000096 if (n < 0) {
Mark Whitley6317c4b2001-03-12 22:51:50 +000097 if (errno == EINTR)
98 continue;
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000099 syslog(LOG_ERR, "klogd: Error from sys_sycall: %d - %m.\n",
100 errno);
Eric Andersen36adca82004-06-22 10:07:17 +0000101 exit(EXIT_FAILURE);
Mark Whitley6317c4b2001-03-12 22:51:50 +0000102 }
103
104 /* klogctl buffer parsing modelled after code in dmesg.c */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000105 start = &log_buffer[0];
106 lastc = '\0';
107 for (i = 0; i < n; i++) {
Mark Whitley6317c4b2001-03-12 22:51:50 +0000108 if (lastc == '\0' && log_buffer[i] == '<') {
109 priority = 0;
110 i++;
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +0000111 while (log_buffer[i] >= '0' && log_buffer[i] <= '9') {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000112 priority = priority * 10 + (log_buffer[i] - '0');
Mark Whitley6317c4b2001-03-12 22:51:50 +0000113 i++;
114 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000115 if (log_buffer[i] == '>')
116 i++;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000117 start = &log_buffer[i];
118 }
119 if (log_buffer[i] == '\n') {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000120 log_buffer[i] = '\0'; /* zero terminate this message */
Glenn L McGrath12ed3332004-08-06 00:58:53 +0000121 syslog(priority, "%s", start);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000122 start = &log_buffer[i + 1];
Mark Whitley6317c4b2001-03-12 22:51:50 +0000123 priority = LOG_INFO;
124 }
125 lastc = log_buffer[i];
126 }
127 }
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +0000128 if (ENABLE_FEATURE_CLEAN_UP)
129 RELEASE_CONFIG_BUFFER(log_buffer);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000130
Mark Whitley6317c4b2001-03-12 22:51:50 +0000131 return EXIT_SUCCESS;
132}