2001-03-13 04:21:50 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Mini klogd implementation for busybox
|
|
|
|
*
|
2002-12-12 16:24:48 +05:30
|
|
|
* Copyright (C) 2001 by Gennady Feldman <gfeldman@gena01.com>.
|
2001-03-13 04:21:50 +05:30
|
|
|
* Changes: Made this a standalone busybox module which uses standalone
|
|
|
|
* syslog() client interface.
|
|
|
|
*
|
2003-07-15 02:51:08 +05:30
|
|
|
* Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org>
|
2001-03-13 04:21:50 +05:30
|
|
|
*
|
|
|
|
* Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org>
|
|
|
|
*
|
2002-12-12 16:24:48 +05:30
|
|
|
* "circular buffer" Copyright (C) 2000 by Gennady Feldman <gfeldman@gena01.com>
|
2001-03-13 04:21:50 +05:30
|
|
|
*
|
2002-12-12 16:24:48 +05:30
|
|
|
* Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001
|
2001-03-13 05:11:34 +05:30
|
|
|
*
|
2001-03-13 04:21:50 +05:30
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2002-08-23 00:11:20 +05:30
|
|
|
#include <signal.h> /* for our signal() handlers */
|
|
|
|
#include <string.h> /* strncpy() */
|
|
|
|
#include <errno.h> /* errno and friends */
|
2001-03-13 04:21:50 +05:30
|
|
|
#include <unistd.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <sys/syslog.h>
|
2003-07-22 14:26:55 +05:30
|
|
|
#include <sys/klog.h>
|
2001-04-05 08:44:39 +05:30
|
|
|
|
2001-03-13 04:21:50 +05:30
|
|
|
#include "busybox.h"
|
|
|
|
|
|
|
|
static void klogd_signal(int sig)
|
|
|
|
{
|
|
|
|
klogctl(7, NULL, 0);
|
|
|
|
klogctl(0, 0, 0);
|
2002-08-23 00:11:20 +05:30
|
|
|
/* logMessage(0, "Kernel log daemon exiting."); */
|
2002-09-18 19:53:06 +05:30
|
|
|
syslog_msg(LOG_SYSLOG, LOG_NOTICE, "Kernel log daemon exiting.");
|
2001-03-13 04:21:50 +05:30
|
|
|
exit(TRUE);
|
|
|
|
}
|
|
|
|
|
2002-12-01 17:01:58 +05:30
|
|
|
static void doKlogd(const char console_log_level) __attribute__ ((noreturn));
|
|
|
|
static void doKlogd(const char console_log_level)
|
2001-03-13 04:21:50 +05:30
|
|
|
{
|
|
|
|
int priority = LOG_INFO;
|
|
|
|
char log_buffer[4096];
|
|
|
|
int i, n, lastc;
|
|
|
|
char *start;
|
|
|
|
|
|
|
|
/* Set up sig handlers */
|
|
|
|
signal(SIGINT, klogd_signal);
|
|
|
|
signal(SIGKILL, klogd_signal);
|
|
|
|
signal(SIGTERM, klogd_signal);
|
|
|
|
signal(SIGHUP, SIG_IGN);
|
|
|
|
|
|
|
|
/* "Open the log. Currently a NOP." */
|
|
|
|
klogctl(1, NULL, 0);
|
|
|
|
|
2002-12-01 17:01:58 +05:30
|
|
|
/* Set level of kernel console messaging.. */
|
|
|
|
if (console_log_level)
|
|
|
|
klogctl(8, NULL, console_log_level);
|
|
|
|
|
2002-09-18 19:53:06 +05:30
|
|
|
syslog_msg(LOG_SYSLOG, LOG_NOTICE, "klogd started: " BB_BANNER);
|
2001-03-13 04:21:50 +05:30
|
|
|
|
|
|
|
while (1) {
|
|
|
|
/* Use kernel syscalls */
|
|
|
|
memset(log_buffer, '\0', sizeof(log_buffer));
|
|
|
|
n = klogctl(2, log_buffer, sizeof(log_buffer));
|
|
|
|
if (n < 0) {
|
|
|
|
char message[80];
|
|
|
|
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
2002-08-23 00:11:20 +05:30
|
|
|
snprintf(message, 79,
|
|
|
|
"klogd: Error return from sys_sycall: %d - %s.\n", errno,
|
|
|
|
strerror(errno));
|
2002-09-18 19:53:06 +05:30
|
|
|
syslog_msg(LOG_SYSLOG, LOG_ERR, message);
|
2001-03-13 04:21:50 +05:30
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* klogctl buffer parsing modelled after code in dmesg.c */
|
2002-08-23 00:11:20 +05:30
|
|
|
start = &log_buffer[0];
|
|
|
|
lastc = '\0';
|
|
|
|
for (i = 0; i < n; i++) {
|
2001-03-13 04:21:50 +05:30
|
|
|
if (lastc == '\0' && log_buffer[i] == '<') {
|
|
|
|
priority = 0;
|
|
|
|
i++;
|
|
|
|
while (isdigit(log_buffer[i])) {
|
2002-08-23 00:11:20 +05:30
|
|
|
priority = priority * 10 + (log_buffer[i] - '0');
|
2001-03-13 04:21:50 +05:30
|
|
|
i++;
|
|
|
|
}
|
2002-08-23 00:11:20 +05:30
|
|
|
if (log_buffer[i] == '>')
|
|
|
|
i++;
|
2001-03-13 04:21:50 +05:30
|
|
|
start = &log_buffer[i];
|
|
|
|
}
|
|
|
|
if (log_buffer[i] == '\n') {
|
2002-08-23 00:11:20 +05:30
|
|
|
log_buffer[i] = '\0'; /* zero terminate this message */
|
2002-09-18 19:53:06 +05:30
|
|
|
syslog_msg(LOG_KERN, priority, start);
|
2002-08-23 00:11:20 +05:30
|
|
|
start = &log_buffer[i + 1];
|
2001-03-13 04:21:50 +05:30
|
|
|
priority = LOG_INFO;
|
|
|
|
}
|
|
|
|
lastc = log_buffer[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern int klogd_main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
/* no options, no getopt */
|
2001-03-30 03:28:33 +05:30
|
|
|
int opt;
|
2001-03-13 04:21:50 +05:30
|
|
|
int doFork = TRUE;
|
2002-12-01 17:01:58 +05:30
|
|
|
unsigned char console_log_level = 7;
|
2001-03-13 04:21:50 +05:30
|
|
|
|
|
|
|
/* do normal option parsing */
|
2002-12-01 17:01:58 +05:30
|
|
|
while ((opt = getopt(argc, argv, "c:n")) > 0) {
|
2001-03-13 04:21:50 +05:30
|
|
|
switch (opt) {
|
2002-12-01 17:01:58 +05:30
|
|
|
case 'c':
|
|
|
|
if ((optarg == NULL) || (optarg[1] != '\0')) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_show_usage();
|
2002-12-01 17:01:58 +05:30
|
|
|
}
|
|
|
|
/* Valid levels are between 1 and 8 */
|
|
|
|
console_log_level = *optarg - '1';
|
|
|
|
if (console_log_level > 7) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_show_usage();
|
2002-12-01 17:01:58 +05:30
|
|
|
}
|
|
|
|
console_log_level++;
|
|
|
|
|
|
|
|
break;
|
2002-08-23 00:11:20 +05:30
|
|
|
case 'n':
|
|
|
|
doFork = FALSE;
|
|
|
|
break;
|
|
|
|
default:
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_show_usage();
|
2001-03-13 04:21:50 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-21 04:43:26 +05:30
|
|
|
if (doFork) {
|
2003-12-16 03:27:44 +05:30
|
|
|
#if defined(__uClinux__)
|
|
|
|
vfork_daemon_rexec(0, 1, argc, argv, "-n");
|
|
|
|
#else /* __uClinux__ */
|
2001-03-30 03:28:33 +05:30
|
|
|
if (daemon(0, 1) < 0)
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_perror_msg_and_die("daemon");
|
2003-12-16 03:27:44 +05:30
|
|
|
#endif /* __uClinux__ */
|
2001-03-13 04:21:50 +05:30
|
|
|
}
|
2002-12-01 17:01:58 +05:30
|
|
|
doKlogd(console_log_level);
|
2002-08-23 00:11:20 +05:30
|
|
|
|
2001-03-13 04:21:50 +05:30
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Local Variables
|
|
|
|
c-file-style: "linux"
|
|
|
|
c-basic-offset: 4
|
|
|
|
tab-width: 4
|
|
|
|
End:
|
|
|
|
*/
|