blob: 6dc5457af80dd152069c756c4b3a903a02e90767 [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
32static void klogd_signal(int sig)
33{
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-Fischer86f5c992006-01-22 22:55:11 +000041static void doKlogd(const int console_log_level) ATTRIBUTE_NORETURN;
Eric Andersen0373cf12004-06-25 11:23:03 +000042static void doKlogd(const int console_log_level)
Mark Whitley6317c4b2001-03-12 22:51:50 +000043{
44 int priority = LOG_INFO;
45 char log_buffer[4096];
46 int i, n, lastc;
47 char *start;
48
Eric Andersen36adca82004-06-22 10:07:17 +000049 openlog("kernel", 0, LOG_KERN);
50
Mark Whitley6317c4b2001-03-12 22:51:50 +000051 /* Set up sig handlers */
52 signal(SIGINT, klogd_signal);
53 signal(SIGKILL, klogd_signal);
54 signal(SIGTERM, klogd_signal);
55 signal(SIGHUP, SIG_IGN);
56
57 /* "Open the log. Currently a NOP." */
58 klogctl(1, NULL, 0);
59
Glenn L McGrathe1ad6722002-12-01 11:31:58 +000060 /* Set level of kernel console messaging.. */
Eric Andersen0373cf12004-06-25 11:23:03 +000061 if (console_log_level != -1)
Glenn L McGrathe1ad6722002-12-01 11:31:58 +000062 klogctl(8, NULL, console_log_level);
63
"Vladimir N. Oleynik"dd1ccdd2006-02-16 15:40:24 +000064 syslog(LOG_NOTICE, "klogd started: %s", BB_BANNER);
Mark Whitley6317c4b2001-03-12 22:51:50 +000065
66 while (1) {
67 /* Use kernel syscalls */
68 memset(log_buffer, '\0', sizeof(log_buffer));
69 n = klogctl(2, log_buffer, sizeof(log_buffer));
70 if (n < 0) {
Mark Whitley6317c4b2001-03-12 22:51:50 +000071 if (errno == EINTR)
72 continue;
Eric Andersen0373cf12004-06-25 11:23:03 +000073 syslog(LOG_ERR, "klogd: Error return from sys_sycall: %d - %m.\n", errno);
Eric Andersen36adca82004-06-22 10:07:17 +000074 exit(EXIT_FAILURE);
Mark Whitley6317c4b2001-03-12 22:51:50 +000075 }
76
77 /* klogctl buffer parsing modelled after code in dmesg.c */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000078 start = &log_buffer[0];
79 lastc = '\0';
80 for (i = 0; i < n; i++) {
Mark Whitley6317c4b2001-03-12 22:51:50 +000081 if (lastc == '\0' && log_buffer[i] == '<') {
82 priority = 0;
83 i++;
84 while (isdigit(log_buffer[i])) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000085 priority = priority * 10 + (log_buffer[i] - '0');
Mark Whitley6317c4b2001-03-12 22:51:50 +000086 i++;
87 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000088 if (log_buffer[i] == '>')
89 i++;
Mark Whitley6317c4b2001-03-12 22:51:50 +000090 start = &log_buffer[i];
91 }
92 if (log_buffer[i] == '\n') {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000093 log_buffer[i] = '\0'; /* zero terminate this message */
Glenn L McGrath12ed3332004-08-06 00:58:53 +000094 syslog(priority, "%s", start);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000095 start = &log_buffer[i + 1];
Mark Whitley6317c4b2001-03-12 22:51:50 +000096 priority = LOG_INFO;
97 }
98 lastc = log_buffer[i];
99 }
100 }
101}
102
Rob Landley570f6552005-09-14 15:36:52 +0000103#define OPT_LEVEL 1
104#define OPT_FOREGROUND 2
105
Rob Landleydfba7412006-03-06 20:47:33 +0000106int klogd_main(int argc, char **argv)
Mark Whitley6317c4b2001-03-12 22:51:50 +0000107{
Rob Landley570f6552005-09-14 15:36:52 +0000108 unsigned long opt;
109 char *c_arg;
110 int console_log_level = -1;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000111
112 /* do normal option parsing */
Rob Landley570f6552005-09-14 15:36:52 +0000113 opt = bb_getopt_ulflags (argc, argv, "c:n", &c_arg);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000114
Rob Landley570f6552005-09-14 15:36:52 +0000115 if (opt & OPT_LEVEL) {
116 /* Valid levels are between 1 and 8 */
117 console_log_level = bb_xgetlarg(c_arg, 10, 1, 8);
Mark Whitley6317c4b2001-03-12 22:51:50 +0000118 }
119
Rob Landley570f6552005-09-14 15:36:52 +0000120 if (!(opt & OPT_FOREGROUND)) {
Russ Dilla1fece22003-12-15 21:57:44 +0000121#if defined(__uClinux__)
122 vfork_daemon_rexec(0, 1, argc, argv, "-n");
123#else /* __uClinux__ */
Bernhard Reutner-Fischer2c998512006-04-12 18:09:26 +0000124 bb_xdaemon(0, 1);
Russ Dilla1fece22003-12-15 21:57:44 +0000125#endif /* __uClinux__ */
Mark Whitley6317c4b2001-03-12 22:51:50 +0000126 }
Glenn L McGrathe1ad6722002-12-01 11:31:58 +0000127 doKlogd(console_log_level);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000128
Mark Whitley6317c4b2001-03-12 22:51:50 +0000129 return EXIT_SUCCESS;
130}
131
132/*
133Local Variables
134c-file-style: "linux"
135c-basic-offset: 4
136tab-width: 4
137End:
138*/