Shepherd redirect stdout/stderr to syslog

Message ID CAL1_immoRNBh9OL2eN5sLN9c_w8NfiK5NTdAkG6VUgtTmspVSw@mail.gmail.com
State New
Headers

Commit Message

David Craven Sept. 5, 2016, 1:44 p.m. UTC
  > Is redirecting stdout/stderr to syslog something that
> make-forkexec-constructor could/should do?

I looked into what would be involved. I included a diff that I didn't
test and don't expect to work.

The reason why I don't expect this to work is that running echo
"hello" > /dev/log errors. stracing logger "hello" shows that it makes
use of the socket and sendmsg syscalls instead of the usual open and
write syscalls. I don't understand why though, since what's the point
of everything being a file if they don't share the same interface?
  

Comments

Ludovic Courtès Sept. 5, 2016, 9:14 p.m. UTC | #1
David Craven <david@craven.ch> skribis:

>> Is redirecting stdout/stderr to syslog something that
>> make-forkexec-constructor could/should do?
>
> I looked into what would be involved. I included a diff that I didn't
> test and don't expect to work.
>
> The reason why I don't expect this to work is that running echo
> "hello" > /dev/log errors. stracing logger "hello" shows that it makes
> use of the socket and sendmsg syscalls instead of the usual open and
> write syscalls. I don't understand why though, since what's the point
> of everything being a file if they don't share the same interface?

The important thing is the special format, which includes the date:

  sendto(3, "<13>Sep  5 23:09:51 ludo: foo", 29, 0, NULL, 0) = 29

misc/syslog.c:193 in glibc implements this.

So we would need to pipe each daemon’s stdout/stderr to the Shepherd
itself, which would select(2) on all these, prepend the right prefix,
and write that to /dev/log, I think.

Ludo’.
  

Patch

diff --git a/modules/shepherd/service.scm b/modules/shepherd/service.scm
index 49f6e8b..69c1cc2 100644
--- a/modules/shepherd/service.scm
+++ b/modules/shepherd/service.scm
@@ -712,12 +712,18 @@  false."

      ;; Close all the file descriptors except stdout and stderr.
      (let ((max-fd (max-file-descriptors)))
+       ;; Redirect stdin to use /dev/null
        (catch-system-error (close-fdes 0))
-
        ;; Make sure file descriptor zero is used, so we don't end up reusing
        ;; it for something unrelated, which can confuse some packages.
        (dup2 (open-fdes "/dev/null" O_RDONLY) 0)

+       ;; Redirect stout and stderr to use /dev/log
+       (catch-system-error (close-fdes 1))
+       (catch-system-error (close-fdes 2))
+       (dup2 (open-fdes "/dev/log" O_WRONLY) 1)
+       (dup2 (open-fdes "/dev/log" O_WRONLY) 2)
+
        (let loop ((i 3))
          (when (< i max-fd)
            (catch-system-error (close-fdes i))