summary refs log tree commit diff
path: root/nanoklogd.c
diff options
context:
space:
mode:
authorChristian Neukirchen <chneukirchen@gmail.com>2014-07-18 23:25:01 +0200
committerChristian Neukirchen <chneukirchen@gmail.com>2014-07-18 23:25:01 +0200
commitb0368aad76ce3b67f6050855f9a43cc7bb2d1dc3 (patch)
treec4d545445aa4f83440d2a31a9cd4bb347c375765 /nanoklogd.c
downloadsocklog-void-b0368aad76ce3b67f6050855f9a43cc7bb2d1dc3.tar.gz
socklog-void-b0368aad76ce3b67f6050855f9a43cc7bb2d1dc3.tar.xz
socklog-void-b0368aad76ce3b67f6050855f9a43cc7bb2d1dc3.zip
Initial import of socklog-void
Diffstat (limited to 'nanoklogd.c')
-rw-r--r--nanoklogd.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/nanoklogd.c b/nanoklogd.c
new file mode 100644
index 0000000..2dd32a0
--- /dev/null
+++ b/nanoklogd.c
@@ -0,0 +1,58 @@
+/* nanoklogd - forward kernel messages to /dev/log.  */
+
+/* Written by Christian Neukirchen <chneukirchen@gmail.com>
+   To the extent possible under law, the creator of this work has waived
+   all copyright and related or neighboring rights to this work.  */
+
+/* This uses /dev/log directly because glibc syslog(3) does
+   forward LOG_KERN as LOG_USER.  */
+
+#include <sys/klog.h>
+#include <sys/socket.h>
+#include <errno.h>
+#include <unistd.h>
+
+static const struct {
+        short sun_family;
+        char sun_path[9];
+} log_addr = {
+        AF_UNIX,
+        "/dev/log"
+};
+
+int
+main(int argc, char *argv[]) {
+	char buf[16384];
+	int f, l;
+
+	f = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
+	if (f < 0)
+		return 111;
+
+	if (connect(f, (void *)&log_addr, sizeof log_addr) < 0)
+		return 112;
+
+	while (1) {
+		l = klogctl(2, buf, sizeof buf);
+		if (l < 0)
+			return 113;
+
+		while (send(f, buf, l, 0) != l)
+			switch (errno) {
+			case ECONNREFUSED:
+			case ECONNRESET:
+			case ENOTCONN:
+				/* syslogd went down, retry until up.  */
+				close(f);
+				sleep(1);
+				f = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
+				if (f < 0)
+					return 111;
+				connect(f, (void *)&log_addr, sizeof log_addr);
+				break;
+			default:
+				return 114;
+			}
+	}
+	return 0;
+}