blob: 97b419104b8ae9218536da32bb954561e1bdf652 [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);
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000040 int console_log_level = console_log_level; /* for gcc */
Mark Whitley6317c4b2001-03-12 22:51:50 +000041 int priority = LOG_INFO;
Mark Whitley6317c4b2001-03-12 22:51:50 +000042 int i, n, lastc;
43 char *start;
44
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000045 /* do normal option parsing */
46 n = getopt32(argc, argv, "c:n", &start);
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000047
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000048 if (n & OPT_LEVEL) {
49 /* Valid levels are between 1 and 8 */
50 console_log_level = xatoul_range(start, 1, 8);
51 }
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000052
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000053 if (!(n & OPT_FOREGROUND)) {
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000054#ifdef BB_NOMMU
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000055 vfork_daemon_rexec(0, 1, argc, argv, "-n");
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000056#else
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000057 xdaemon(0, 1);
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
Glenn L McGrathe1ad6722002-12-01 11:31:58 +000072 /* Set level of kernel console messaging.. */
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000073 if (n & OPT_LEVEL)
Glenn L McGrathe1ad6722002-12-01 11:31:58 +000074 klogctl(8, NULL, console_log_level);
75
"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
78 while (1) {
79 /* Use kernel syscalls */
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000080 memset(log_buffer, '\0', KLOGD_LOGBUF_SIZE);
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000081 /* It will be null-terminted */
82 n = klogctl(2, log_buffer, KLOGD_LOGBUF_SIZE - 1);
Mark Whitley6317c4b2001-03-12 22:51:50 +000083 if (n < 0) {
Mark Whitley6317c4b2001-03-12 22:51:50 +000084 if (errno == EINTR)
85 continue;
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000086 syslog(LOG_ERR, "klogd: error from klogctl(2): %d - %m",
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +000087 errno);
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000088 break;
Mark Whitley6317c4b2001-03-12 22:51:50 +000089 }
90
91 /* klogctl buffer parsing modelled after code in dmesg.c */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000092 start = &log_buffer[0];
93 lastc = '\0';
94 for (i = 0; i < n; i++) {
Mark Whitley6317c4b2001-03-12 22:51:50 +000095 if (lastc == '\0' && log_buffer[i] == '<') {
Mark Whitley6317c4b2001-03-12 22:51:50 +000096 i++;
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +000097 // kernel never ganerates multi-digit prios
98 //priority = 0;
99 //while (log_buffer[i] >= '0' && log_buffer[i] <= '9') {
100 // priority = priority * 10 + (log_buffer[i] - '0');
101 // i++;
102 //}
103 if (isdigit(log_buffer[i])) {
104 priority = (log_buffer[i] - '0');
Mark Whitley6317c4b2001-03-12 22:51:50 +0000105 i++;
106 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000107 if (log_buffer[i] == '>')
108 i++;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000109 start = &log_buffer[i];
110 }
111 if (log_buffer[i] == '\n') {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000112 log_buffer[i] = '\0'; /* zero terminate this message */
Glenn L McGrath12ed3332004-08-06 00:58:53 +0000113 syslog(priority, "%s", start);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000114 start = &log_buffer[i + 1];
Mark Whitley6317c4b2001-03-12 22:51:50 +0000115 priority = LOG_INFO;
116 }
117 lastc = log_buffer[i];
118 }
119 }
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +0000120 if (ENABLE_FEATURE_CLEAN_UP)
121 RELEASE_CONFIG_BUFFER(log_buffer);
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}