summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@tele2.at>2018-09-01 01:12:46 +0200
committerDavid Oberhollenzer <david.oberhollenzer@tele2.at>2018-09-01 13:03:18 +0200
commitdc30dd7530cee426bbb30425e255868ffa635c13 (patch)
tree5f8c9dce00a58bd82ece93db86c93c783a773c37
parentb0b6c68e3839656c657f83de70badace6c01675a (diff)
Add tiny klogd implementation
Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
-rw-r--r--.gitignore1
-rw-r--r--Makefile.am6
-rw-r--r--configure.ac9
-rw-r--r--docs/defconfig.md4
-rw-r--r--docs/usyslogd.md9
-rw-r--r--services/Makemodule.am6
-rw-r--r--services/klogd6
-rw-r--r--syslogd/Makemodule.am11
-rw-r--r--syslogd/klogd.c191
9 files changed, 238 insertions, 5 deletions
diff --git a/.gitignore b/.gitignore
index 5c14884..ca9c061 100644
--- a/.gitignore
+++ b/.gitignore
@@ -22,6 +22,7 @@ killall5
runsvc
syslog
usyslogd
+klogd
services/sigkill
services/sigterm
diff --git a/Makefile.am b/Makefile.am
index 648631a..063a8e8 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -22,10 +22,7 @@ include cmd/Makemodule.am
include initd/Makemodule.am
include scripts/Makemodule.am
include services/Makemodule.am
-
-if USYSLOGD
include syslogd/Makemodule.am
-endif
install-exec-hook:
(cd $(DESTDIR)$(sbindir); $(LN_S) shutdown reboot)
@@ -57,3 +54,6 @@ install-data-local:
if USYSLOGD
$(LN_S) $(TEMPLATEDIR)/usyslogd $(DESTDIR)$(SVCDIR)/usyslogd
endif
+if KLOGD
+ $(LN_S) $(TEMPLATEDIR)/klogd $(DESTDIR)$(SVCDIR)/klogd
+endif
diff --git a/configure.ac b/configure.ac
index 99e4b2d..01cff9e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -46,6 +46,15 @@ AC_ARG_WITH([usyslogd],
esac],
[AM_CONDITIONAL([USYSLOGD], [true])])
+AC_ARG_WITH([klogd],
+ [AS_HELP_STRING([--without-klogd], [Build without kernel log daemon])],
+ [case "${withval}" in
+ yes) AM_CONDITIONAL([KLOGD], [true]) ;;
+ no) AM_CONDITIONAL([KLOGD], [false]) ;;
+ *) AC_MSG_ERROR([bad value ${withval} for --without-klogd]) ;;
+ esac],
+ [AM_CONDITIONAL([KLOGD], [true])])
+
AC_CONFIG_HEADERS([lib/include/config.h])
AC_DEFINE_DIR(SVCDIR, sysconfdir/init.d, [Startup service directory])
diff --git a/docs/defconfig.md b/docs/defconfig.md
index 10263ba..41afc02 100644
--- a/docs/defconfig.md
+++ b/docs/defconfig.md
@@ -55,8 +55,10 @@ the `vfs` target and *before* the `sysinit` target:
* hostname - reload hostname `/etc/hostname`
* loopback - bring the loopback device up
- * usyslogd - if the `usyslogd` services is compiled with this package, this
+ * usyslogd - if the `usyslogd` service is compiled with this package, this
service is enabled by default and starts `usyslogd`.
+ * klogd - if the `klogd` daemon is compiled with this package, this service
+ is enabled by default and starts after `usyslogd`.
* modules - iterate over the file `/etc/modules` and try to load each module
using modprobe.
* sysctl - restore kernel parameters using `sysctl --system`. See `sysctl(8)`
diff --git a/docs/usyslogd.md b/docs/usyslogd.md
index 96f5755..525c475 100644
--- a/docs/usyslogd.md
+++ b/docs/usyslogd.md
@@ -12,6 +12,15 @@ the log messages into files in the processes working directory (by default
A simple log rotation scheme has been implemented.
+## Kernel Message Logging
+
+An additional small `klogd` daemon is provided that redirects kernel messages
+to the syslog daemon.
+
+It can be enabled or disabled independently of the `usyslogd` daemon and is
+designed to work with any other syslogd implementation.
+
+
## Security Considerations
By default, the daemon switches its working directory to `/var/log`. The
diff --git a/services/Makemodule.am b/services/Makemodule.am
index 0843787..5e38eca 100644
--- a/services/Makemodule.am
+++ b/services/Makemodule.am
@@ -14,8 +14,12 @@ if USYSLOGD
init_DATA += services/usyslogd
endif
+if USYSLOGD
+init_DATA += services/klogd
+endif
+
EXTRA_DIST += services/sysinit services/vfs services/agetty services/hostname
-EXTRA_DIST += services/hwclock services/loopback
+EXTRA_DIST += services/hwclock services/loopback services/klogd
EXTRA_DIST += services/sync services/sysctl services/tmpfs
EXTRA_DIST += services/dhcpcd services/dhcpcdmaster services/unbound
EXTRA_DIST += services/usyslogd services/dnsmasq services/network
diff --git a/services/klogd b/services/klogd
new file mode 100644
index 0000000..aaca59a
--- /dev/null
+++ b/services/klogd
@@ -0,0 +1,6 @@
+description "starting uklogd"
+exec klogd
+type respawn limit 5
+target boot
+after usyslogd
+before sysinit
diff --git a/syslogd/Makemodule.am b/syslogd/Makemodule.am
index 022946b..ab61b87 100644
--- a/syslogd/Makemodule.am
+++ b/syslogd/Makemodule.am
@@ -1,3 +1,4 @@
+if USYSLOGD
usyslogd_SOURCES = syslogd/syslogd.c syslogd/syslogd.h
usyslogd_SOURCES += syslogd/proto.c syslogd/logfile.c
usyslogd_CPPFLAGS = $(AM_CPPFLAGS)
@@ -6,3 +7,13 @@ usyslogd_LDFLAGS = $(AM_LDFLAGS)
usyslogd_LDADD = libinit.a
sbin_PROGRAMS += usyslogd
+endif
+
+if KLOGD
+klogd_SOURCES = syslogd/klogd.c
+klogd_CPPFLAGS = $(AM_CPPFLAGS)
+klogd_CFLAGS = $(AM_CFLAGS)
+klogd_LDFLAGS = $(AM_LDFLAGS)
+
+sbin_PROGRAMS += klogd
+endif
diff --git a/syslogd/klogd.c b/syslogd/klogd.c
new file mode 100644
index 0000000..ca16e4e
--- /dev/null
+++ b/syslogd/klogd.c
@@ -0,0 +1,191 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+/*
+ * Copyright (C) 2018 - David Oberhollenzer
+ *
+ * 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 3 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, see <https://www.gnu.org/licenses/>.
+ */
+#include <sys/klog.h>
+#include <syslog.h>
+#include <signal.h>
+#include <string.h>
+#include <stdlib.h>
+#include <getopt.h>
+#include <stdio.h>
+#include <errno.h>
+
+#include "config.h"
+
+enum {
+ KLOG_CLOSE = 0,
+ KLOG_OPEN = 1,
+ KLOG_READ = 2,
+ KLOG_CONSOLE_OFF = 6,
+ KLOG_CONSOLE_ON = 7,
+ KLOG_CONSOLE_LEVEL = 8,
+};
+
+static char log_buffer[4096];
+static sig_atomic_t running = 1;
+static int level = 0;
+
+static const struct option options[] = {
+ { "help", no_argument, NULL, 'h' },
+ { "version", no_argument, NULL, 'V' },
+ { "level", required_argument, NULL, 'l' },
+ { NULL, 0, NULL, 0 },
+};
+
+static const char *shortopt = "hVl:";
+
+static const char *versiontext =
+"klogd (Pygos init) "PACKAGE_VERSION"\n"
+"Copyright (C) 2018 David Oberhollenzer\n"
+"License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.\n"
+"This is free software: you are free to change and redistribute it.\n"
+"There is NO WARRANTY, to the extent permitted by law.\n";
+
+static const char *helptext =
+"Usage: klogd [OPTION]... \n\n"
+"Collect printk() messages from the kernel and forward them to syslogd.\n"
+"\n"
+"The following OPTIONSs can be used:\n"
+" -l, --level <level> Minimum log level that should be printed to console.\n"
+" If not set, logging to console is turned off.\n"
+" -h, --help Print this help text and exit\n"
+" -V, --version Print version information and exit\n\n";
+
+static void process_options(int argc, char **argv)
+{
+ int c;
+
+ for (;;) {
+ c = getopt_long(argc, argv, shortopt, options, NULL);
+ if (c == -1)
+ break;
+
+ switch (c) {
+ case 'l':
+ level = strtoul(optarg, NULL, 10);
+ break;
+ case 'h':
+ fputs(helptext, stdout);
+ exit(EXIT_SUCCESS);
+ case 'V':
+ fputs(versiontext, stdout);
+ exit(EXIT_SUCCESS);
+ default:
+ fputs("Try `klogd --help' for more information\n",
+ stderr);
+ exit(EXIT_FAILURE);
+ }
+ }
+}
+
+static void sighandler(int signo)
+{
+ if (signo == SIGTERM || signo == SIGINT)
+ running = 0;
+}
+
+static void sigsetup(void)
+{
+ struct sigaction act;
+ sigset_t mask;
+
+ memset(&act, 0, sizeof(act));
+ act.sa_handler = sighandler;
+ sigaction(SIGTERM, &act, NULL);
+ sigaction(SIGINT, &act, NULL);
+
+ sigfillset(&mask);
+ sigdelset(&mask, SIGTERM);
+ sigdelset(&mask, SIGINT);
+ sigprocmask(SIG_SETMASK, &mask, NULL);
+}
+
+static void log_open(void)
+{
+ klogctl(KLOG_OPEN, NULL, 0);
+
+ if (level) {
+ klogctl(KLOG_CONSOLE_LEVEL, NULL, level);
+ } else {
+ klogctl(KLOG_CONSOLE_OFF, NULL, 0);
+ }
+
+ openlog("kernel", 0, LOG_KERN);
+}
+
+static void log_close(void)
+{
+ klogctl(KLOG_CONSOLE_ON, NULL, 0);
+ klogctl(KLOG_CLOSE, NULL, 0);
+ syslog(LOG_NOTICE, "-- klogd terminating --");
+}
+
+int main(int argc, char **argv)
+{
+ int diff, count = 0, priority, ret = EXIT_SUCCESS;
+ char *ptr, *end;
+
+ process_options(argc, argv);
+ sigsetup();
+ log_open();
+
+ /* TODO: seccomp lockdown? */
+
+ while (running) {
+ diff = klogctl(KLOG_READ, log_buffer + count,
+ sizeof(log_buffer) - 1 - count);
+
+ if (diff < 0) {
+ if (errno == EINTR)
+ continue;
+ syslog(LOG_CRIT, "klogctl read: %s", strerror(errno));
+ ret = EXIT_FAILURE;
+ break;
+ }
+
+ count += diff;
+ log_buffer[count] = '\0';
+ ptr = log_buffer;
+
+ for (;;) {
+ end = strchr(ptr, '\n');
+ if (end == NULL) {
+ count = strlen(ptr);
+ memmove(log_buffer, ptr, count);
+ break;
+ }
+
+ *(end++) = '\0';
+ priority = LOG_INFO;
+
+ if (*ptr == '<') {
+ ++ptr;
+ if (*ptr)
+ priority = strtoul(ptr, &ptr, 10);
+ if (*ptr == '>')
+ ++ptr;
+ }
+
+ if (*ptr)
+ syslog(priority, "%s", ptr);
+ ptr = end;
+ }
+ }
+
+ log_close();
+ return ret;
+}