blob: f735d9fc1ad2234c7c3062ca2769890b11516870 [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);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000028 /* logMessage(0, "Kernel log daemon exiting."); */
Eric Andersen36adca82004-06-22 10:07:17 +000029 syslog(LOG_NOTICE, "Kernel log daemon exiting.");
30 exit(EXIT_SUCCESS);
Mark Whitley6317c4b2001-03-12 22:51:50 +000031}
32
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000033#define OPT_LEVEL 1
34#define OPT_FOREGROUND 2
35
36#define KLOGD_LOGBUF_SIZE 4096
37
38int klogd_main(int argc, char **argv)
Mark Whitley6317c4b2001-03-12 22:51:50 +000039{
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000040 RESERVE_CONFIG_BUFFER(log_buffer, KLOGD_LOGBUF_SIZE);
41 int console_log_level = -1;
Mark Whitley6317c4b2001-03-12 22:51:50 +000042 int priority = LOG_INFO;
Mark Whitley6317c4b2001-03-12 22:51:50 +000043 int i, n, lastc;
44 char *start;
45
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000046 {
Denis Vlasenko67b23e62006-10-03 21:00:06 +000047 unsigned opt;
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000048
49 /* do normal option parsing */
Denis Vlasenko67b23e62006-10-03 21:00:06 +000050 opt = getopt32(argc, argv, "c:n", &start);
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000051
52 if (opt & OPT_LEVEL) {
53 /* Valid levels are between 1 and 8 */
Denis Vlasenko13858992006-10-08 12:49:22 +000054 console_log_level = xatoul_range(start, 1, 8);
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000055 }
56
57 if (!(opt & OPT_FOREGROUND)) {
58#ifdef BB_NOMMU
59 vfork_daemon_rexec(0, 1, argc, argv, "-n");
60#else
Rob Landleyd921b2e2006-08-03 15:41:12 +000061 xdaemon(0, 1);
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000062#endif
63 }
64 }
65
Eric Andersen36adca82004-06-22 10:07:17 +000066 openlog("kernel", 0, LOG_KERN);
67
Mark Whitley6317c4b2001-03-12 22:51:50 +000068 /* Set up sig handlers */
69 signal(SIGINT, klogd_signal);
70 signal(SIGKILL, klogd_signal);
71 signal(SIGTERM, klogd_signal);
72 signal(SIGHUP, SIG_IGN);
73
74 /* "Open the log. Currently a NOP." */
75 klogctl(1, NULL, 0);
76
Glenn L McGrathe1ad6722002-12-01 11:31:58 +000077 /* Set level of kernel console messaging.. */
Eric Andersen0373cf12004-06-25 11:23:03 +000078 if (console_log_level != -1)
Glenn L McGrathe1ad6722002-12-01 11:31:58 +000079 klogctl(8, NULL, console_log_level);
80
"Vladimir N. Oleynik"dd1ccdd2006-02-16 15:40:24 +000081 syslog(LOG_NOTICE, "klogd started: %s", BB_BANNER);
Mark Whitley6317c4b2001-03-12 22:51:50 +000082
83 while (1) {
84 /* Use kernel syscalls */
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000085 memset(log_buffer, '\0', KLOGD_LOGBUF_SIZE);
86 n = klogctl(2, log_buffer, KLOGD_LOGBUF_SIZE);
Mark Whitley6317c4b2001-03-12 22:51:50 +000087 if (n < 0) {
Mark Whitley6317c4b2001-03-12 22:51:50 +000088 if (errno == EINTR)
89 continue;
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000090 syslog(LOG_ERR, "klogd: Error from sys_sycall: %d - %m.\n",
91 errno);
Eric Andersen36adca82004-06-22 10:07:17 +000092 exit(EXIT_FAILURE);
Mark Whitley6317c4b2001-03-12 22:51:50 +000093 }
94
95 /* klogctl buffer parsing modelled after code in dmesg.c */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000096 start = &log_buffer[0];
97 lastc = '\0';
98 for (i = 0; i < n; i++) {
Mark Whitley6317c4b2001-03-12 22:51:50 +000099 if (lastc == '\0' && log_buffer[i] == '<') {
100 priority = 0;
101 i++;
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +0000102 while (log_buffer[i] >= '0' && log_buffer[i] <= '9') {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000103 priority = priority * 10 + (log_buffer[i] - '0');
Mark Whitley6317c4b2001-03-12 22:51:50 +0000104 i++;
105 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000106 if (log_buffer[i] == '>')
107 i++;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000108 start = &log_buffer[i];
109 }
110 if (log_buffer[i] == '\n') {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000111 log_buffer[i] = '\0'; /* zero terminate this message */
Glenn L McGrath12ed3332004-08-06 00:58:53 +0000112 syslog(priority, "%s", start);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000113 start = &log_buffer[i + 1];
Mark Whitley6317c4b2001-03-12 22:51:50 +0000114 priority = LOG_INFO;
115 }
116 lastc = log_buffer[i];
117 }
118 }
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +0000119 if (ENABLE_FEATURE_CLEAN_UP)
120 RELEASE_CONFIG_BUFFER(log_buffer);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000121
Mark Whitley6317c4b2001-03-12 22:51:50 +0000122 return EXIT_SUCCESS;
123}