From 678508409a92f80cacd94a70e1e51ca4a389435a Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Wed, 18 Aug 2021 18:46:40 +0000 Subject: Add s6-socklog Signed-off-by: Laurent Bercot --- doc/index.html | 1 + doc/s6-socklog.html | 139 ++++++++++++++++++ examples/klogd-linux/log/run | 2 +- examples/klogd-linux/run | 2 +- examples/s6-svscanboot | 16 +-- examples/syslogd-linux/README | 9 -- examples/syslogd-linux/log/README | 9 -- examples/syslogd-linux/log/env/LOGSCRIPT | 6 - examples/syslogd-linux/log/notification-fd | 1 - examples/syslogd-linux/log/run | 8 -- examples/syslogd-linux/notification-fd | 1 - examples/syslogd-linux/run | 8 -- examples/syslogd/README | 4 + examples/syslogd/log/README | 9 ++ examples/syslogd/log/env/LOGSCRIPT | 6 + examples/syslogd/log/notification-fd | 1 + examples/syslogd/log/run | 8 ++ examples/syslogd/notification-fd | 1 + examples/syslogd/run | 4 + package/deps.mak | 8 +- package/modes | 1 + package/targets.mak | 1 + src/daemontools-extras/deps-exe/s6-socklog | 4 + src/daemontools-extras/deps-exe/ucspilogd | 1 + src/daemontools-extras/lolsyslog.c | 90 ++++++++++++ src/daemontools-extras/lolsyslog.h | 10 ++ src/daemontools-extras/s6-socklog.c | 220 +++++++++++++++++++++++++++++ src/daemontools-extras/ucspilogd.c | 144 +++---------------- 28 files changed, 536 insertions(+), 178 deletions(-) create mode 100644 doc/s6-socklog.html delete mode 100644 examples/syslogd-linux/README delete mode 100644 examples/syslogd-linux/log/README delete mode 100644 examples/syslogd-linux/log/env/LOGSCRIPT delete mode 100644 examples/syslogd-linux/log/notification-fd delete mode 100755 examples/syslogd-linux/log/run delete mode 100644 examples/syslogd-linux/notification-fd delete mode 100755 examples/syslogd-linux/run create mode 100644 examples/syslogd/README create mode 100644 examples/syslogd/log/README create mode 100644 examples/syslogd/log/env/LOGSCRIPT create mode 100644 examples/syslogd/log/notification-fd create mode 100755 examples/syslogd/log/run create mode 100644 examples/syslogd/notification-fd create mode 100755 examples/syslogd/run create mode 100644 src/daemontools-extras/deps-exe/s6-socklog create mode 100644 src/daemontools-extras/lolsyslog.c create mode 100644 src/daemontools-extras/lolsyslog.h create mode 100644 src/daemontools-extras/s6-socklog.c diff --git a/doc/index.html b/doc/index.html index 7707fe7..d3e7e6f 100644 --- a/doc/index.html +++ b/doc/index.html @@ -259,6 +259,7 @@ synchronization. diff --git a/doc/s6-socklog.html b/doc/s6-socklog.html new file mode 100644 index 0000000..03f8c6b --- /dev/null +++ b/doc/s6-socklog.html @@ -0,0 +1,139 @@ + + + + + + s6: the s6-socklog program + + + + + + +

+s6
+Software
+skarnet.org +

+ +

The s6-socklog program

+ +

+s6-socklog is a minimal syslog daemon. It reads datagrams +from the /dev/log Unix domain socket, or from a Unix domain +or Internet domain socket of the user's choice, converts the encoded +syslog facility and priority names to their human-readable equivalents, +and prints the logs to its stdout. +

+ +

+s6-socklog is a reimplementation of the +socklog program +with a few more features. +

+ +

Interface

+ +
+     s6-socklog [ -d notif ] [ -r ] [ -U | -u uid -g gid -G gidlist ] [ -l linelen ] [ -t lameducktimeout ] [ -x unixsocket | -i ipport ]
+
+ + + +

Exit codes

+ + + +

Signals

+ +

+ s6-socklog reacts to the following signals: +

+ + + +

Options

+ + + +

Typical use

+ +

+ s6-socklog can be paired with s6-log to +implement syslogd functionality. s6-socklog acts as the +frontend: it reads the log lines and processes them, then pipes them +to an s6-log instance that acts as the backend, +i.e. sorts the log lines depending on regular expressions that typically involve +the facility and priority names, then stores them into the filesystem. +

+ +

+ The pipe between s6-socklog and s6-log is +typically a logging pipe automatically provided by +s6-svscan when the s6-log instance is declared as +a logger service for the s6-socklog instance. +

+ +

+ The examples/ subdirectory of the s6 package contains a turnkey +syslogd service that implements this pattern. +

+ + + diff --git a/examples/klogd-linux/log/run b/examples/klogd-linux/log/run index 458fe27..82a3bb6 100755 --- a/examples/klogd-linux/log/run +++ b/examples/klogd-linux/log/run @@ -1,4 +1,4 @@ -#!/command/execlineb -P +#!/bin/execlineb -P s6-setuidgid klog exec -c s6-log -d3 t s1000000 n20 /var/log/klogd diff --git a/examples/klogd-linux/run b/examples/klogd-linux/run index 453b55b..ae9935a 100755 --- a/examples/klogd-linux/run +++ b/examples/klogd-linux/run @@ -1,4 +1,4 @@ -#!/command/execlineb -P +#!/bin/execlineb -P fdmove -c 2 1 redirfd -r 0 /proc/kmsg exec -c diff --git a/examples/s6-svscanboot b/examples/s6-svscanboot index 6bac3a2..be4bea4 100755 --- a/examples/s6-svscanboot +++ b/examples/s6-svscanboot @@ -1,9 +1,9 @@ -#!/command/execlineb -P +#!/bin/execlineb -P -/command/exec -c -/command/s6-setsid -qb -/command/redirfd -r 0 /dev/null -/command/redirfd -wnb 1 /service/s6-svscan-log/fifo -/command/fdmove -c 2 1 -/command/exec -a s6-svscan -/command/s6-svscan -t0 /service +/bin/exec -c +/bin/s6-setsid -qb +/bin/redirfd -r 0 /dev/null +/bin/redirfd -wnb 1 /service/s6-svscan-log/fifo +/bin/fdmove -c 2 1 +/bin/exec -a s6-svscan +/bin/s6-svscan -t0 /service diff --git a/examples/syslogd-linux/README b/examples/syslogd-linux/README deleted file mode 100644 index 675715b..0000000 --- a/examples/syslogd-linux/README +++ /dev/null @@ -1,9 +0,0 @@ -This is an example of a service directory for process supervision by s6. - -This syslogd emulation works on any Unix where syslog() is -implemented via a connection on the /dev/log Unix-domain socket. -It needs a Unix superserver (see s6-networking, ucspi-unix or -ucspi-ipc) and ucspilogd. -A ucspilogd process is spawned for every syslog() client. It -processes logs and sends them to stderr, i.e. the service's -logger. diff --git a/examples/syslogd-linux/log/README b/examples/syslogd-linux/log/README deleted file mode 100644 index 9d4968e..0000000 --- a/examples/syslogd-linux/log/README +++ /dev/null @@ -1,9 +0,0 @@ - This logger service logs everything the syslogd service receives -into subdirectories of /var/log/syslogd/ logdir. - - The script provided in env/LOGSCRIPT emulates the default -syslogd behaviour on a majority of Linux distributions. - - Note that spaces cannot be used as is in the LOGSCRIPT file, -because they are delimiters for the splitting of $LOGSCRIPT -in the run script. So \s is used instead. diff --git a/examples/syslogd-linux/log/env/LOGSCRIPT b/examples/syslogd-linux/log/env/LOGSCRIPT deleted file mode 100644 index e7a1eb7..0000000 --- a/examples/syslogd-linux/log/env/LOGSCRIPT +++ /dev/null @@ -1,6 +0,0 @@ -- +^[0-9]+:\s[0-9]+:\serror\. t /var/log/syslogd/error -- +^[0-9]+:\s[0-9]+:\sauthpriv\. t /var/log/syslogd/auth -- +^[0-9]+:\s[0-9]+:\suser\. t /var/log/syslogd/user -- +^[0-9]+:\s[0-9]+:\smessages\. t /var/log/syslogd/messages -- +^[0-9]+:\s[0-9]+:\sdaemon\. t /var/log/syslogd/daemon -f t /var/log/syslogd/misc diff --git a/examples/syslogd-linux/log/notification-fd b/examples/syslogd-linux/log/notification-fd deleted file mode 100644 index 00750ed..0000000 --- a/examples/syslogd-linux/log/notification-fd +++ /dev/null @@ -1 +0,0 @@ -3 diff --git a/examples/syslogd-linux/log/run b/examples/syslogd-linux/log/run deleted file mode 100755 index fcb667c..0000000 --- a/examples/syslogd-linux/log/run +++ /dev/null @@ -1,8 +0,0 @@ -#!/command/execlineb -P -s6-setuidgid syslog -s6-envdir -f env -importas -Cs LOGSCRIPT LOGSCRIPT -exec -c -s6-log -d3 -- $LOGSCRIPT - -# Change env/LOGSCRIPT to alter s6-log's behaviour diff --git a/examples/syslogd-linux/notification-fd b/examples/syslogd-linux/notification-fd deleted file mode 100644 index 00750ed..0000000 --- a/examples/syslogd-linux/notification-fd +++ /dev/null @@ -1 +0,0 @@ -3 diff --git a/examples/syslogd-linux/run b/examples/syslogd-linux/run deleted file mode 100755 index de33c2c..0000000 --- a/examples/syslogd-linux/run +++ /dev/null @@ -1,8 +0,0 @@ -#!/command/execlineb -P -fdmove -c 2 1 -exec -c -s6-envuidgid nobody -fdmove 1 3 -s6-ipcserver -U -1 -- /dev/log -fdmove -c 1 2 -ucspilogd IPCREMOTEEUID IPCREMOTEEGID diff --git a/examples/syslogd/README b/examples/syslogd/README new file mode 100644 index 0000000..c115a4e --- /dev/null +++ b/examples/syslogd/README @@ -0,0 +1,4 @@ +This is an example of a service directory for process supervision by s6. + +This syslogd emulation works on any Unix where syslog() is +implemented via a connection on the /dev/log Unix-domain socket. diff --git a/examples/syslogd/log/README b/examples/syslogd/log/README new file mode 100644 index 0000000..9d4968e --- /dev/null +++ b/examples/syslogd/log/README @@ -0,0 +1,9 @@ + This logger service logs everything the syslogd service receives +into subdirectories of /var/log/syslogd/ logdir. + + The script provided in env/LOGSCRIPT emulates the default +syslogd behaviour on a majority of Linux distributions. + + Note that spaces cannot be used as is in the LOGSCRIPT file, +because they are delimiters for the splitting of $LOGSCRIPT +in the run script. So \s is used instead. diff --git a/examples/syslogd/log/env/LOGSCRIPT b/examples/syslogd/log/env/LOGSCRIPT new file mode 100644 index 0000000..35ae234 --- /dev/null +++ b/examples/syslogd/log/env/LOGSCRIPT @@ -0,0 +1,6 @@ +- +^\\serror\\. t /var/log/syslogd/error +- +^\\sauthpriv\\. t /var/log/syslogd/auth +- +^\\suser\\. t /var/log/syslogd/user +- +^\\smessages\\. t /var/log/syslogd/messages +- +^\\sdaemon\\. t /var/log/syslogd/daemon +f t /var/log/syslogd/misc diff --git a/examples/syslogd/log/notification-fd b/examples/syslogd/log/notification-fd new file mode 100644 index 0000000..00750ed --- /dev/null +++ b/examples/syslogd/log/notification-fd @@ -0,0 +1 @@ +3 diff --git a/examples/syslogd/log/run b/examples/syslogd/log/run new file mode 100755 index 0000000..c066f37 --- /dev/null +++ b/examples/syslogd/log/run @@ -0,0 +1,8 @@ +#!/bun/execlineb -P +s6-setuidgid syslog +s6-envdir -f env +importas -Cs LOGSCRIPT LOGSCRIPT +exec -c +s6-log -d3 -- $LOGSCRIPT + +# Change env/LOGSCRIPT to alter s6-log's behaviour diff --git a/examples/syslogd/notification-fd b/examples/syslogd/notification-fd new file mode 100644 index 0000000..00750ed --- /dev/null +++ b/examples/syslogd/notification-fd @@ -0,0 +1 @@ +3 diff --git a/examples/syslogd/run b/examples/syslogd/run new file mode 100755 index 0000000..98a52d9 --- /dev/null +++ b/examples/syslogd/run @@ -0,0 +1,4 @@ +#!/bin/execlineb -P +fdmove -c 2 1 +s6-envuidgid nobody +s6-socklog -d3 -U -t3000 diff --git a/package/deps.mak b/package/deps.mak index 3c051aa..2f21727 100644 --- a/package/deps.mak +++ b/package/deps.mak @@ -19,6 +19,7 @@ src/conn-tools/s6-ipcserverd.o src/conn-tools/s6-ipcserverd.lo: src/conn-tools/s src/conn-tools/s6-sudo.o src/conn-tools/s6-sudo.lo: src/conn-tools/s6-sudo.c src/include/s6/config.h src/conn-tools/s6-sudoc.o src/conn-tools/s6-sudoc.lo: src/conn-tools/s6-sudoc.c src/conn-tools/s6-sudo.h src/conn-tools/s6-sudod.o src/conn-tools/s6-sudod.lo: src/conn-tools/s6-sudod.c src/conn-tools/s6-sudo.h +src/daemontools-extras/lolsyslog.o src/daemontools-extras/lolsyslog.lo: src/daemontools-extras/lolsyslog.c src/daemontools-extras/lolsyslog.h src/daemontools-extras/s6-applyuidgid.o src/daemontools-extras/s6-applyuidgid.lo: src/daemontools-extras/s6-applyuidgid.c src/daemontools-extras/s6-envdir.o src/daemontools-extras/s6-envdir.lo: src/daemontools-extras/s6-envdir.c src/daemontools-extras/s6-envuidgid.o src/daemontools-extras/s6-envuidgid.lo: src/daemontools-extras/s6-envuidgid.c @@ -27,10 +28,11 @@ src/daemontools-extras/s6-log.o src/daemontools-extras/s6-log.lo: src/daemontool src/daemontools-extras/s6-setlock.o src/daemontools-extras/s6-setlock.lo: src/daemontools-extras/s6-setlock.c src/include/s6/config.h src/include-local/s6lockd.h src/daemontools-extras/s6-setsid.o src/daemontools-extras/s6-setsid.lo: src/daemontools-extras/s6-setsid.c src/daemontools-extras/s6-setuidgid.o src/daemontools-extras/s6-setuidgid.lo: src/daemontools-extras/s6-setuidgid.c src/include/s6/config.h +src/daemontools-extras/s6-socklog.o src/daemontools-extras/s6-socklog.lo: src/daemontools-extras/s6-socklog.c src/daemontools-extras/lolsyslog.h src/daemontools-extras/s6-softlimit.o src/daemontools-extras/s6-softlimit.lo: src/daemontools-extras/s6-softlimit.c src/daemontools-extras/s6-tai64n.o src/daemontools-extras/s6-tai64n.lo: src/daemontools-extras/s6-tai64n.c src/daemontools-extras/s6-tai64nlocal.o src/daemontools-extras/s6-tai64nlocal.lo: src/daemontools-extras/s6-tai64nlocal.c -src/daemontools-extras/ucspilogd.o src/daemontools-extras/ucspilogd.lo: src/daemontools-extras/ucspilogd.c +src/daemontools-extras/ucspilogd.o src/daemontools-extras/ucspilogd.lo: src/daemontools-extras/ucspilogd.c src/daemontools-extras/lolsyslog.h src/fdholder/s6-fdholder-daemon.o src/fdholder/s6-fdholder-daemon.lo: src/fdholder/s6-fdholder-daemon.c src/include/s6/config.h src/fdholder/s6-fdholder-delete.o src/fdholder/s6-fdholder-delete.lo: src/fdholder/s6-fdholder-delete.c src/include/s6/s6-fdholder.h src/fdholder/s6-fdholder-getdump.o src/fdholder/s6-fdholder-getdump.lo: src/fdholder/s6-fdholder-getdump.c src/include/s6/s6-fdholder.h @@ -176,6 +178,8 @@ s6-setsid: EXTRA_LIBS := -lskarnet s6-setsid: src/daemontools-extras/s6-setsid.o s6-setuidgid: EXTRA_LIBS := -lskarnet s6-setuidgid: src/daemontools-extras/s6-setuidgid.o +s6-socklog: EXTRA_LIBS := -lskarnet ${SOCKET_LIB} ${SYSCLOCK_LIB} +s6-socklog: src/daemontools-extras/s6-socklog.o src/daemontools-extras/lolsyslog.o s6-softlimit: EXTRA_LIBS := -lskarnet s6-softlimit: src/daemontools-extras/s6-softlimit.o s6-tai64n: EXTRA_LIBS := -lskarnet ${SYSCLOCK_LIB} @@ -183,7 +187,7 @@ s6-tai64n: src/daemontools-extras/s6-tai64n.o s6-tai64nlocal: EXTRA_LIBS := -lskarnet s6-tai64nlocal: src/daemontools-extras/s6-tai64nlocal.o ucspilogd: EXTRA_LIBS := -lskarnet -ucspilogd: src/daemontools-extras/ucspilogd.o +ucspilogd: src/daemontools-extras/ucspilogd.o src/daemontools-extras/lolsyslog.o s6-fdholder-daemon: EXTRA_LIBS := -lskarnet ${SOCKET_LIB} ${SYSCLOCK_LIB} s6-fdholder-daemon: src/fdholder/s6-fdholder-daemon.o ${LIBS6} s6-fdholder-delete: EXTRA_LIBS := -lskarnet ${SOCKET_LIB} ${SYSCLOCK_LIB} diff --git a/package/modes b/package/modes index b457f7d..4678bec 100644 --- a/package/modes +++ b/package/modes @@ -31,6 +31,7 @@ s6-setlock 0755 s6-setsid 0755 s6-setuidgid 0700 s6-softlimit 0755 +s6-socklog 0755 s6-tai64n 0755 s6-tai64nlocal 0755 s6-accessrules-cdb-from-fs 0755 diff --git a/package/targets.mak b/package/targets.mak index 32971b9..0f1ebab 100644 --- a/package/targets.mak +++ b/package/targets.mak @@ -29,6 +29,7 @@ s6-log \ s6-setlock \ s6-setsid \ s6-softlimit \ +s6-socklog \ s6-tai64n \ s6-tai64nlocal \ s6-accessrules-cdb-from-fs \ diff --git a/src/daemontools-extras/deps-exe/s6-socklog b/src/daemontools-extras/deps-exe/s6-socklog new file mode 100644 index 0000000..8e48518 --- /dev/null +++ b/src/daemontools-extras/deps-exe/s6-socklog @@ -0,0 +1,4 @@ +lolsyslog.o +-lskarnet +${SOCKET_LIB} +${SYSCLOCK_LIB} diff --git a/src/daemontools-extras/deps-exe/ucspilogd b/src/daemontools-extras/deps-exe/ucspilogd index e7187fe..3fe140f 100644 --- a/src/daemontools-extras/deps-exe/ucspilogd +++ b/src/daemontools-extras/deps-exe/ucspilogd @@ -1 +1,2 @@ +lolsyslog.o -lskarnet diff --git a/src/daemontools-extras/lolsyslog.c b/src/daemontools-extras/lolsyslog.c new file mode 100644 index 0000000..a397dae --- /dev/null +++ b/src/daemontools-extras/lolsyslog.c @@ -0,0 +1,90 @@ +/* ISC license. */ + +#undef INTERNAL_MARK +#ifndef SYSLOG_NAMES +#define SYSLOG_NAMES +#endif + +#include + +#include +#include + +#include + +#include "lolsyslog.h" + +#ifndef INTERNAL_MARK + +typedef struct CODE_s CODE, *CODE_ref ; +struct CODE_s +{ + char *c_name ; + unsigned int c_val ; +} ; + +#define LOG_PRI(p) ((p) & LOG_PRIMASK) +#define LOG_FAC(p) (((p) & LOG_FACMASK) / (LOG_PRIMASK + 1)) + +static CODE const facilitynames[] = +{ + { "kern", LOG_KERN }, + { "user", LOG_USER }, + { "mail", LOG_MAIL }, + { "news", LOG_NEWS }, + { "uucp", LOG_UUCP }, + { "daemon", LOG_DAEMON }, + { "auth", LOG_AUTH }, + { "cron", LOG_CRON }, + { "lpr", LOG_LPR }, +#ifdef LOG_SYSLOG + { "syslog", LOG_SYSLOG }, +#endif +#ifdef LOG_AUDIT + { "audit", LOG_AUDIT }, +#endif + { "local0", LOG_LOCAL0 }, + { "local1", LOG_LOCAL1 }, + { "local2", LOG_LOCAL2 }, + { "local3", LOG_LOCAL3 }, + { "local4", LOG_LOCAL4 }, + { "local5", LOG_LOCAL5 }, + { "local6", LOG_LOCAL6 }, + { "local7", LOG_LOCAL7 }, + { 0, -1 } +} ; + +static CODE const prioritynames[] = +{ + { "emerg", LOG_EMERG }, + { "alert", LOG_ALERT }, + { "crit", LOG_CRIT }, + { "err", LOG_ERR }, + { "warning", LOG_WARNING }, + { "notice", LOG_NOTICE }, + { "info", LOG_INFO }, + { "debug", LOG_DEBUG }, + { 0, -1 } +} ; + +#endif + +size_t lolsyslog_string (char *out, char const *in) +{ + size_t i ; + unsigned int fpr ; + int fp ; + CODE const *p = facilitynames ; + + if (in[0] != '<' || !(i = uint_scan(in+1, &fpr)) || in[1+i] != '>') return 0 ; + fp = LOG_FAC(fpr) << 3 ; + for (; p->c_name ; p++) if (p->c_val == fp) break ; + out = stpcpy(out, p->c_name ? p->c_name : "unknown") ; + *out++ = '.' ; + + fp = LOG_PRI(fpr) ; + for (p = prioritynames ; p->c_name ; p++) if (p->c_val == fp) break ; + out = stpcpy(out, p->c_name ? p->c_name : "unknown") ; + *out++ = ':' ; *out++ = ' ' ; *out++ = 0 ; + return i+2 ; +} diff --git a/src/daemontools-extras/lolsyslog.h b/src/daemontools-extras/lolsyslog.h new file mode 100644 index 0000000..7364412 --- /dev/null +++ b/src/daemontools-extras/lolsyslog.h @@ -0,0 +1,10 @@ +/* ISC license. */ + +#ifndef LOLSYSLOG_H +#define LOLSYSLOG_H + +#define LOLSYSLOG_STRING 32 + +extern size_t lolsyslog_string (char *, char const *) ; + +#endif diff --git a/src/daemontools-extras/s6-socklog.c b/src/daemontools-extras/s6-socklog.c new file mode 100644 index 0000000..f261916 --- /dev/null +++ b/src/daemontools-extras/s6-socklog.c @@ -0,0 +1,220 @@ +/* ISC license. */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "lolsyslog.h" + +#define USAGE "s6-socklog [ -d notif ] [ -r ] [ -U | -u uid -g gid -G gidlist ] [ -l linelen ] [ -t lameducktimeout ] [ -x socket | -i ip_port ]" +#define dieusage() strerr_dieusage(100, USAGE) + +static tain lameducktto = TAIN_INFINITE_RELATIVE ; +static int cont = 1 ; + +static inline void handle_signals (void) +{ + for (;;) switch (selfpipe_read()) + { + case -1 : strerr_diefu1sys(111, "selfpipe_read()") ; + case 0 : return ; + case SIGTERM : cont = 0 ; break ; + default : break ; + } +} + +int main (int argc, char const *const *argv) +{ + iopause_fd x[3] = { { .events = IOPAUSE_READ }, { .fd = 1 } } ; + int flagraw = 0 ; + char const *usock = "/dev/log" ; + unsigned int linelen = 1024 ; + PROG = "s6-socklog" ; + { + subgetopt l = SUBGETOPT_ZERO ; + unsigned int notif = 0 ; + unsigned int t = 0 ; + uid_t uid = 0 ; + gid_t gid = 0 ; + gid_t gids[NGROUPS_MAX + 1] ; + size_t gidn = (size_t)-1 ; + ip46 ip ; + uint16_t port = 514 ; + for (;;) + { + int opt = subgetopt_r(argc, argv, "rd:l:t:u:g:G:Ux:i:", &l) ; + if (opt == -1) break ; + switch (opt) + { + case 'r' : flagraw = 1 ; break ; + case 'd' : if (!uint0_scan(l.arg, ¬if)) dieusage() ; break ; + case 'l' : if (!uint0_scan(l.arg, &linelen)) dieusage() ; break ; + case 't' : if (!uint0_scan(l.arg, &t)) dieusage() ; break ; + case 'u' : if (!uid0_scan(l.arg, &uid)) dieusage() ; break ; + case 'g' : if (!gid0_scan(l.arg, &gid)) dieusage() ; break ; + case 'G' : if (!gid_scanlist(gids, NGROUPS_MAX, l.arg, &gidn) && *l.arg) dieusage() ; break ; + case 'U' : + { + char const *x = getenv("UID") ; + if (!x) strerr_dienotset(100, "UID") ; + if (!uid0_scan(x, &uid)) strerr_dieinvalid(100, "UID") ; + x = getenv("GID") ; + if (!x) strerr_dienotset(100, "GID") ; + if (!gid0_scan(x, &gid)) strerr_dieinvalid(100, "GID") ; + x = getenv("GIDLIST") ; + if (!x) strerr_dienotset(100, "GIDLIST") ; + if (!gid_scanlist(gids, NGROUPS_MAX+1, x, &gidn) && *x) + strerr_dieinvalid(100, "GIDLIST") ; + break ; + } + case 'x' : usock = l.arg ; break ; + case 'i' : + { + size_t pos = ip46_scan(l.arg, &ip) ; + if (!pos) dieusage() ; + if (l.arg[pos] == '_' || (!ip46_is6(&ip) && l.arg[pos] == ':')) + if (!uint160_scan(l.arg + pos + 1, &port)) dieusage() ; + usock = 0 ; + break ; + } + default : dieusage() ; + } + } + argc -= l.ind ; argv += l.ind ; + + if (linelen < 80) linelen = 80 ; + if (linelen > 1048576) linelen = 1048576 ; + if (t) tain_from_millisecs(&lameducktto, t) ; + else lameducktto = tain_infinite_relative ; + if (notif) + { + if (notif < 3) strerr_dief1x(100, "notification fd must be 3 or more") ; + if (fcntl(notif, F_GETFD) < 0) strerr_dief1sys(100, "invalid notification fd") ; + } + + close(0) ; + if (fcntl(1, F_GETFD) < 0) strerr_dief1sys(100, "invalid stdout") ; + if (usock) + { + x[2].fd = ipc_datagram_nbcoe() ; + if (x[2].fd == -1) strerr_diefu1sys(111, "create socket") ; + if (ipc_bind_reuse_perms(x[2].fd, usock, 0777) == -1) + strerr_diefu2sys(111, "bind socket to ", usock) ; + } + else + { + x[2].fd = socket_udp46_nbcoe(ip46_is6(&ip)) ; + if (x[2].fd == -1) strerr_diefu1sys(111, "create socket") ; + if (socket_bind46_reuse(x[2].fd, &ip, port) == -1) + { + char fmti[IP46_FMT] ; + char fmtp[UINT16_FMT] ; + fmti[ip46_fmt(fmti, &ip)] = 0 ; + fmtp[uint16_fmt(fmtp, port)] = 0 ; + strerr_diefu5sys(111, "bind socket to ", "ip ", fmti, " port ", fmtp) ; + } + } + + if (gidn != (size_t)-1 && setgroups_and_gid(gid ? gid : getegid(), gidn, gids) < 0) + strerr_diefu1sys(111, "set supplementary group list") ; + if (gid && setgid(gid) < 0) + strerr_diefu1sys(111, "setgid") ; + if (uid && setuid(uid) < 0) + strerr_diefu1sys(111, "setuid") ; + + x[0].fd = selfpipe_init() ; + if (x[0].fd == -1) strerr_diefu1sys(111, "init selfpipe") ; + if (!sig_altignore(SIGPIPE)) strerr_diefu1sys(111, "ignore SIGPIPE") ; + if (!selfpipe_trap(SIGTERM)) strerr_diefu1sys(111, "trap signals") ; + + tain_now_set_stopwatch_g() ; + + if (notif) + { + fd_write(notif, "\n", 1) ; + fd_close(notif) ; + } + } + + { + char outbuf[linelen << 2] ; + buffer b1 = BUFFER_INIT(&buffer_write, 1, outbuf, linelen << 2) ; + char line[linelen + 1] ; + while (cont || buffer_len(&b1)) + { + tain deadline = TAIN_INFINITE ; + ssize_t r ; + if (!cont) tain_add_g(&deadline, &lameducktto) ; + x[1].events = buffer_len(&b1) ? IOPAUSE_WRITE : 0 ; + x[2].events = cont && buffer_available(&b1) >= linelen + 80 ? IOPAUSE_READ : 0 ; + r = iopause_g(x, 3, &deadline) ; + if (r == -1) strerr_diefu1sys(111, "iopause") ; + if (!r) return 99 ; + if (x[0].revents & IOPAUSE_READ) handle_signals() ; + if (x[1].events & x[1].revents & IOPAUSE_WRITE) + if (!buffer_flush(&b1) && !error_isagain(errno)) + strerr_diefu1sys(111, "write to stdout") ; + if (x[2].events & x[2].revents & IOPAUSE_READ) + { + if (usock) + { + r = sanitize_read(fd_recv(x[2].fd, line, linelen + 1, 0)) ; + if (r == -1) strerr_diefu1sys(111, "recv") ; + } + else + { + ip46 ip ; + uint16 port ; + r = sanitize_read(socket_recv46(x[2].fd, line, linelen + 1, &ip, &port)) ; + if (r == -1) strerr_diefu1sys(111, "recv") ; + if (r) + { + char fmt[IP46_FMT + UINT16_FMT + 3] ; + size_t m = ip46_fmt(fmt, &ip) ; + fmt[m++] = '_' ; + m += uint16_fmt(fmt, port) ; + fmt[m++] = ':' ; fmt[m++] = ' ' ; + buffer_putnoflush(&b1, fmt, m) ; + } + } + if (r) + { + size_t len = r ; + size_t pos = 0 ; + while (r && (!line[r-1] || line[r-1] == '\n')) r-- ; + for (size_t i = 0 ; i < r ; i++) + if (!line[i] || line[i] == '\n') line[i] = '~' ; + if (!flagraw) + { + char sbuf[LOLSYSLOG_STRING] ; + pos = lolsyslog_string(sbuf, line) ; + if (pos) buffer_putsnoflush(&b1, sbuf) ; + } + buffer_putnoflush(&b1, line + pos, r - pos) ; + if (len == linelen+1) buffer_putnoflush(&b1, "...", 3) ; + buffer_putnoflush(&b1, "\n", 1) ; + } + } + } + } + return 0 ; +} diff --git a/src/daemontools-extras/ucspilogd.c b/src/daemontools-extras/ucspilogd.c index c5694d3..0ab6013 100644 --- a/src/daemontools-extras/ucspilogd.c +++ b/src/daemontools-extras/ucspilogd.c @@ -1,24 +1,18 @@ /* ISC license. */ -#undef INTERNAL_MARK -#ifndef SYSLOG_NAMES -#define SYSLOG_NAMES -#endif - -#include #include #include #include -#include -#include + #include #include #include #include #include -#include #include +#include "lolsyslog.h" + #define USAGE "ucspilogd [ -D default ] [ var... ]" #define dieusage() strerr_dieusage(100, USAGE) @@ -27,110 +21,7 @@ static inline void die (void) strerr_diefu1sys(111, "write to stdout") ; } - - /* - Hack: INTERNAL_MARK is defined by all systems that - use the CODE stuff, and not by others (Solaris). -*/ - -#ifndef INTERNAL_MARK - -typedef struct CODE_s CODE, *CODE_ref ; -struct CODE_s -{ - char *c_name ; - unsigned int c_val ; -} ; - -static CODE const prioritynames[] = -{ - { "emerg", LOG_EMERG }, - { "alert", LOG_ALERT }, - { "crit", LOG_CRIT }, - { "err", LOG_ERR }, - { "warning", LOG_WARNING }, - { "notice", LOG_NOTICE }, - { "info", LOG_INFO }, - { "debug", LOG_DEBUG }, - { 0, -1 } -} ; - -static CODE const facilitynames[] = -{ - { "kern", LOG_KERN }, - { "user", LOG_USER }, - { "mail", LOG_MAIL }, - { "news", LOG_NEWS }, - { "uucp", LOG_UUCP }, - { "daemon", LOG_DAEMON }, - { "auth", LOG_AUTH }, - { "cron", LOG_CRON }, - { "lpr", LOG_LPR }, -#ifdef LOG_SYSLOG - { "syslog", LOG_SYSLOG }, -#endif -#ifdef LOG_AUDIT - { "audit", LOG_AUDIT }, -#endif - { "local0", LOG_LOCAL0 }, - { "local1", LOG_LOCAL1 }, - { "local2", LOG_LOCAL2 }, - { "local3", LOG_LOCAL3 }, - { "local4", LOG_LOCAL4 }, - { "local5", LOG_LOCAL5 }, - { "local6", LOG_LOCAL6 }, - { "local7", LOG_LOCAL7 }, - { 0, -1 } -} ; - -#define LOG_PRI(p) ((p) & LOG_PRIMASK) -#define LOG_FAC(p) (((p) & LOG_FACMASK) / (LOG_PRIMASK + 1)) - -#endif - - -static size_t syslog_names (char const *line) -{ - size_t i ; - unsigned int fpr ; - int fp ; - CODE const *p = facilitynames ; - - if (line[0] != '<') return 0 ; - i = uint_scan(line+1, &fpr) ; - if (!i || (line[i+1] != '>')) return 0 ; - i += 2 ; - - fp = LOG_FAC(fpr) << 3 ; - for (; p->c_name ; p++) if (p->c_val == fp) break ; - if (p->c_name) - { - if ((buffer_puts(buffer_1, p->c_name) < 0) - || (buffer_put(buffer_1, ".", 1) < 1)) die() ; - } - else - { - if (buffer_put(buffer_1, "unknown.", 8) < 8) die() ; - i = 0 ; - } - - fp = LOG_PRI(fpr) ; - for (p = prioritynames ; p->c_name ; p++) if (p->c_val == fp) break ; - if (p->c_name) - { - if ((buffer_puts(buffer_1, p->c_name) < 0) - || (buffer_put(buffer_1, ": ", 2) < 2)) die() ; - } - else - { - if (buffer_put(buffer_1, "unknown: ", 9) < 9) die() ; - i = 0 ; - } - return i ; -} - - -int main (int argc, char const *const *argv, char const *const *envp) +int main (int argc, char const *const *argv) { char const *d = "" ; PROG = "ucspilogd" ; @@ -150,38 +41,43 @@ int main (int argc, char const *const *argv, char const *const *envp) } { - char const *envs[argc] ; unsigned int i = 0 ; + stralloc sa = STRALLOC_ZERO ; + char buf[LOLSYSLOG_STRING] ; + char const *envs[argc] ; for (; i < (unsigned int)argc ; i++) { - envs[i] = env_get2(envp, argv[i]) ; + envs[i] = getenv(argv[i]) ; if (!envs[i]) envs[i] = d ; } for (;;) { size_t pos = 0 ; - satmp.len = 0 ; + size_t j ; + sa.len = 0 ; { - int r = skagetlnsep(buffer_0f1, &satmp, "\n", 2) ; + int r = skagetlnsep(buffer_0f1, &sa, "\n", 2) ; if (r < 0) { - if (errno != EPIPE || !stralloc_0(&satmp)) + if (errno != EPIPE || !stralloc_0(&sa)) strerr_diefu1sys(111, "read from stdin") ; } if (!r) break ; } - if (!satmp.len) continue ; - satmp.s[satmp.len-1] = '\n' ; - if ((satmp.s[0] == '@') && (satmp.len > 26) && (byte_chr(satmp.s, 26, ' ') == 25)) + if (!sa.len) continue ; + sa.s[sa.len-1] = 0 ; + if ((sa.s[0] == '@') && (sa.len > 26) && (byte_chr(sa.s, 26, ' ') == 25)) { - if (buffer_put(buffer_1, satmp.s, 26) < 26) die() ; + if (buffer_put(buffer_1, sa.s, 26) < 26) die() ; pos += 26 ; } for (i = 0 ; i < (unsigned int)argc ; i++) if ((buffer_puts(buffer_1, envs[i]) < 0) || (buffer_put(buffer_1, ": ", 2) < 2)) die() ; - pos += syslog_names(satmp.s + pos) ; - if (buffer_put(buffer_1, satmp.s + pos, satmp.len - pos) < (ssize_t)(satmp.len - pos)) die() ; + j = lolsyslog_string(buf, sa.s + pos) ; pos += j ; + if (j && buffer_puts(buffer_1, buf) < 0) die() ; + sa.s[sa.len-1] = '\n' ; + if (buffer_put(buffer_1, sa.s + pos, sa.len - pos) < 0) die() ; } } return 0 ; -- cgit v1.3.1