summaryrefslogtreecommitdiff
path: root/polux/application/klogd/src/klogd.c
diff options
context:
space:
mode:
Diffstat (limited to 'polux/application/klogd/src/klogd.c')
-rw-r--r--polux/application/klogd/src/klogd.c125
1 files changed, 125 insertions, 0 deletions
diff --git a/polux/application/klogd/src/klogd.c b/polux/application/klogd/src/klogd.c
new file mode 100644
index 0000000000..5c961baa03
--- /dev/null
+++ b/polux/application/klogd/src/klogd.c
@@ -0,0 +1,125 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Mini klogd implementation taken from busybox
+ *
+ * Copyright (C) 2001 by Gennady Feldman <gfeldman@gena01.com>.
+ * Changes: Made this a standalone busybox module which uses standalone
+ * syslog() client interface.
+ *
+ * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
+ *
+ * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org>
+ *
+ * "circular buffer" Copyright (C) 2000 by Gennady Feldman <gfeldman@gena01.com>
+ *
+ * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001
+ *
+ * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <syslog.h>
+#include <sys/klog.h>
+#include <signal.h>
+#include <errno.h>
+#include <ctype.h>
+
+#define START_MESSAGE "miniklogd started"
+#define EXIT_MESSAGE "miniklogd: exiting"
+
+static void klogd_signal(int sig)
+{
+ klogctl(7, NULL, 0);
+ klogctl(0, NULL, 0);
+ syslog(LOG_NOTICE, "%s", EXIT_MESSAGE);
+ exit(sig);
+}
+
+
+int main(int argc, char **argv)
+{
+ int i = 7; //default value
+ int option;
+ char* start;
+ char log_buffer[512];
+ char* list = "nc:";
+
+ /* do normal option parsing */
+ while ((option = getopt(argc, argv, list)) != -1)
+ {
+ switch(option)
+ {
+ case 'n':
+ break;
+ case 'c':
+ sscanf(optarg, "%d", &i);
+ break;
+ }
+ }
+
+ openlog("kernel", 0, LOG_KERN);
+
+ /* Set up sig handlers */
+ signal(SIGINT, klogd_signal);
+ signal(SIGTERM, klogd_signal);
+ signal(SIGHUP, SIG_IGN);
+
+ /* "Open the log. Currently a NOP." */
+ klogctl(1, NULL, 0);
+
+ /* Set level of kernel console messaging. */
+ //if (option_mask32 & OPT_LEVEL)
+ klogctl(8, NULL, i);
+
+ syslog(LOG_NOTICE, "%s", START_MESSAGE);
+
+ /* Note: this code does not detect incomplete messages
+ * (messages not ending with '\n' or just when kernel
+ * generates too many messages for us to keep up)
+ * and will split them in two separate lines */
+ while (1) {
+ int n;
+ int priority;
+
+ n = klogctl(2, log_buffer, sizeof(log_buffer) - 1);
+ if (n < 0) {
+ if (errno == EINTR)
+ continue;
+ syslog(LOG_ERR, "klogd: error from klogctl(2): %d - %m",
+ errno);
+ break;
+ }
+ log_buffer[n] = '\n';
+ i = 0;
+ while (i < n) {
+ priority = LOG_INFO;
+ start = &log_buffer[i];
+ if (log_buffer[i] == '<') {
+ i++;
+ // kernel never ganerates multi-digit prios
+ //priority = 0;
+ //while (log_buffer[i] >= '0' && log_buffer[i] <= '9') {
+ // priority = priority * 10 + (log_buffer[i] - '0');
+ // i++;
+ //}
+ if (isdigit(log_buffer[i])) {
+ priority = (log_buffer[i] - '0');
+ i++;
+ }
+ if (log_buffer[i] == '>')
+ i++;
+ start = &log_buffer[i];
+ }
+ while (log_buffer[i] != '\n')
+ i++;
+ log_buffer[i] = '\0';
+ syslog(priority, "%s", start);
+ i++;
+ }
+ }
+
+ return EXIT_FAILURE;
+}