unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
From: Danny Milosavljevic <dannym@scratchpost.org>
To: "Ludovic Courtès" <ludo@gnu.org>
Cc: 30498@debbugs.gnu.org
Subject: [bug#30498] [WIP v2 shepherd] shepherd: If /dev/kmsg is writable, use it for logging.
Date: Sat, 3 Mar 2018 23:37:15 +0100	[thread overview]
Message-ID: <20180303233715.7ec8a08c@scratchpost.org> (raw)
In-Reply-To: <87bmg46d10.fsf@gnu.org>


[-- Attachment #1.1: Type: text/plain, Size: 2939 bytes --]

I think that just something like this is missing:

diff --git a/modules/shepherd/service.scm b/modules/shepherd/service.scm
index 83600e4..481203d 100644
--- a/modules/shepherd/service.scm
+++ b/modules/shepherd/service.scm
@@ -728,20 +728,37 @@ false."
        ;; it for something unrelated, which can confuse some packages.
        (dup2 (open-fdes "/dev/null" O_RDONLY) 0)
 
-       (when log-file
-         (catch #t
-           (lambda ()
-             ;; Redirect stout and stderr to use LOG-FILE.
-             (catch-system-error (close-fdes 1))
-             (catch-system-error (close-fdes 2))
-             (dup2 (open-fdes log-file (logior O_CREAT O_WRONLY)) 1)
-             (dup2 (open-fdes log-file (logior O_CREAT O_WRONLY)) 2))
-           (lambda (key . args)
-             (format (current-error-port)
-                     "failed to open log-file ~s:~%" log-file)
-             (print-exception (current-error-port) #f key args)
-             (primitive-exit 1))))
-
+       (if log-file
+           (catch #t
+             (lambda ()
+               ;; Redirect stout and stderr to use LOG-FILE.
+               (catch-system-error (close-fdes 1))
+               (catch-system-error (close-fdes 2))
+               (dup2 (open-fdes log-file (logior O_CREAT O_WRONLY)) 1)
+               (dup2 (open-fdes log-file (logior O_CREAT O_WRONLY)) 2))
+             (lambda (key . args)
+               (format (current-error-port)
+                       "failed to open log-file ~s:~%" log-file)
+               (print-exception (current-error-port) #f key args)
+               (primitive-exit 1)))
+           (catch #t
+             (lambda ()
+               ;; Make sure the child has stdout/stderr that can be used.
+               ;; We sometimes set current-error-port to a softport.
+               ;; libguile would then autoconnect /dev/null -
+               ;; which we don't want.
+               ;; Also, cryptsetup interactively asks for a password,
+               ;; so we don't want /dev/kmsg either.
+               ;; In a user shepherd all this is not necessary -
+               ;; but then, port->fdes will not fail.
+               (when (not (false-if-exception (port->fdes (current-output-port))))
+                 (dup2 (open-fdes "/dev/console" (logior O_WRONLY)) 1))
+               (when (not (false-if-exception (port->fdes (current-error-port))))
+                 (dup2 (open-fdes "/dev/console" (logior O_WRONLY)) 2)))
+             (lambda (key . args)
+               (format (current-error-port) "failed to open stdout/stderr\n")
+               (print-exception (current-error-port) #f key args)
+               (primitive-exit 1))))
        (let loop ((i 3))
          (when (< i max-fd)
            ;; First try to close any ports associated with file descriptor I.

Patch starting from master attached for convenience.

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.2: shepherd-klog.patch --]
[-- Type: text/x-patch, Size: 7946 bytes --]

diff --git a/doc/shepherd.texi b/doc/shepherd.texi
index 810336c..88b39e3 100644
--- a/doc/shepherd.texi
+++ b/doc/shepherd.texi
@@ -405,7 +405,7 @@ permissions are not as expected.
 @cindex logging
 @cindex log file
 Log output into @var{file}, or if @var{file} is not given,
-@code{/var/log/shepherd.log} when running as superuser,
+@code{/dev/kmsg} when running as superuser (except when this is not possible---then it uses @code{/var/log/shepherd.log}),
 @code{$XDG_CONFIG_HOME/shepherd/shepherd.log} otherwise.
 
 @item --pid[=@var{file}]
diff --git a/modules/shepherd.scm b/modules/shepherd.scm
index 5334657..6acd41e 100644
--- a/modules/shepherd.scm
+++ b/modules/shepherd.scm
@@ -141,8 +141,17 @@
     ;; Enable logging as first action.
     (start-logging logfile)
 
+    (when (string=? logfile "/dev/kmsg")
+      ;; Prevent duplicate messages.
+      (set-current-output-port (%make-void-port "w")))
+
     ;; Send output to log and clients.
-    (set-current-output-port shepherd-output-port)
+    (set-current-output-port
+     (make-shepherd-output-port (current-output-port)))
+
+    ;; Send errors to log.
+    (set-current-error-port
+     (make-shepherd-output-port (current-error-port) (const #f)))
 
     ;; Start the 'root' service.
     (start root-service)
diff --git a/modules/shepherd/comm.scm b/modules/shepherd/comm.scm
index 0228f63..99b8d04 100644
--- a/modules/shepherd/comm.scm
+++ b/modules/shepherd/comm.scm
@@ -51,7 +51,8 @@
             start-logging
             stop-logging
             %current-client-socket
-            shepherd-output-port))
+            %current-logfile-date-format
+            make-shepherd-output-port))
 
 \f
 ;; Command for shepherd.
@@ -200,10 +201,18 @@ on service '~a':")
   ;; Socket of the client currently talking to the daemon.
   (make-parameter #f))
 
+;; Every entry in the logfile is prefixed with
+;; (strftime %current-logfile-date-format).
+(define %current-logfile-date-format
+  (make-parameter default-logfile-date-format))
+
 ;; We provide our own output mechanism, because we have certain
 ;; special needs; most importantly, we want to send output to herd
 ;; sometimes.
-(define (make-shepherd-output-port original-output-port)
+(define* (make-shepherd-output-port original-output-port
+                                   #:optional
+                                   (current-client-socket-thunk
+                                    %current-client-socket))
   (make-soft-port
    (vector
 
@@ -216,9 +225,9 @@ on service '~a':")
       (lambda (str)
         ;; When herd is connected, send it the output; otherwise, in the
         ;; unlikely case nobody is listening, send to the standard output.
-        (if (%current-client-socket)
+        (if (current-client-socket-thunk)
             (catch-system-error
-             (display str (%current-client-socket)))
+             (display str (current-client-socket-thunk)))
             (display str original-output-port))
 
         ;; Logfile, buffer line-wise and output time for each
@@ -228,7 +237,7 @@ on service '~a':")
             (let* ((log (lambda (x)
                           (display x log-output-port)))
                    (init-line (lambda ()
-                                (log (strftime "%Y-%m-%d %H:%M:%S "
+                                (log (strftime (%current-logfile-date-format)
                                                (localtime (current-time)))))))
               (init-line)
               (for-each log (reverse buffer))
@@ -259,6 +268,3 @@ on service '~a':")
 
    ;; It's an output-only port.
    "w"))
-
-(define shepherd-output-port
-  (make-shepherd-output-port (current-output-port)))
diff --git a/modules/shepherd/service.scm b/modules/shepherd/service.scm
index 83600e4..481203d 100644
--- a/modules/shepherd/service.scm
+++ b/modules/shepherd/service.scm
@@ -728,20 +728,37 @@ false."
        ;; it for something unrelated, which can confuse some packages.
        (dup2 (open-fdes "/dev/null" O_RDONLY) 0)
 
-       (when log-file
-         (catch #t
-           (lambda ()
-             ;; Redirect stout and stderr to use LOG-FILE.
-             (catch-system-error (close-fdes 1))
-             (catch-system-error (close-fdes 2))
-             (dup2 (open-fdes log-file (logior O_CREAT O_WRONLY)) 1)
-             (dup2 (open-fdes log-file (logior O_CREAT O_WRONLY)) 2))
-           (lambda (key . args)
-             (format (current-error-port)
-                     "failed to open log-file ~s:~%" log-file)
-             (print-exception (current-error-port) #f key args)
-             (primitive-exit 1))))
-
+       (if log-file
+           (catch #t
+             (lambda ()
+               ;; Redirect stout and stderr to use LOG-FILE.
+               (catch-system-error (close-fdes 1))
+               (catch-system-error (close-fdes 2))
+               (dup2 (open-fdes log-file (logior O_CREAT O_WRONLY)) 1)
+               (dup2 (open-fdes log-file (logior O_CREAT O_WRONLY)) 2))
+             (lambda (key . args)
+               (format (current-error-port)
+                       "failed to open log-file ~s:~%" log-file)
+               (print-exception (current-error-port) #f key args)
+               (primitive-exit 1)))
+           (catch #t
+             (lambda ()
+               ;; Make sure the child has stdout/stderr that can be used.
+               ;; We sometimes set current-error-port to a softport.
+               ;; libguile would then autoconnect /dev/null -
+               ;; which we don't want.
+               ;; Also, cryptsetup interactively asks for a password,
+               ;; so we don't want /dev/kmsg either.
+               ;; In a user shepherd all this is not necessary -
+               ;; but then, port->fdes will not fail.
+               (when (not (false-if-exception (port->fdes (current-output-port))))
+                 (dup2 (open-fdes "/dev/console" (logior O_WRONLY)) 1))
+               (when (not (false-if-exception (port->fdes (current-error-port))))
+                 (dup2 (open-fdes "/dev/console" (logior O_WRONLY)) 2)))
+             (lambda (key . args)
+               (format (current-error-port) "failed to open stdout/stderr\n")
+               (print-exception (current-error-port) #f key args)
+               (primitive-exit 1))))
        (let loop ((i 3))
          (when (< i max-fd)
            ;; First try to close any ports associated with file descriptor I.
diff --git a/modules/shepherd/support.scm b/modules/shepherd/support.scm
index bb01edc..585aef9 100644
--- a/modules/shepherd/support.scm
+++ b/modules/shepherd/support.scm
@@ -22,6 +22,7 @@
 (define-module (shepherd support)
   #:use-module (shepherd config)
   #:use-module (ice-9 match)
+  #:use-module (ice-9 format)
   #:export (call/ec
             caught-error
             assert
@@ -43,6 +44,7 @@
 
             user-homedir
             default-logfile
+            default-logfile-date-format
             default-config-file
             default-socket-dir
             default-socket-file
@@ -282,9 +284,16 @@ TARGET should be a string representing a filepath + name."
 ;; Logfile.
 (define default-logfile
   (if (zero? (getuid))
-      (string-append %localstatedir "/log/shepherd.log")
+      (if (access? "/dev/kmsg" W_OK)
+          "/dev/kmsg"
+          (string-append %localstatedir "/log/shepherd.log"))
       (string-append %user-config-dir "/shepherd.log")))
 
+(define default-logfile-date-format
+   (if (and (zero? (getuid)) (string=? default-logfile "/dev/kmsg"))
+       (format #f "shepherd[~d]: " (getpid))
+       "%Y-%m-%d %H:%M:%S "))
+
 ;; Configuration file.
 (define (default-config-file)
   "Return the default configuration file---either the user's file, or the

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

  reply	other threads:[~2018-03-03 22:38 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-17 12:20 [bug#30498] [PATCH shepherd] shepherd: If /dev/kmsg is writable, use it for logging Danny Milosavljevic
2018-02-17 12:25 ` Danny Milosavljevic
2018-02-17 16:48 ` [bug#30498] [WIP v2 " Danny Milosavljevic
2018-02-17 16:49   ` Danny Milosavljevic
2018-02-26 18:04   ` Ludovic Courtès
2018-02-26 21:51     ` Danny Milosavljevic
2018-02-27  9:22       ` Ludovic Courtès
2018-02-26 22:32     ` Danny Milosavljevic
2018-02-27  9:19       ` Ludovic Courtès
2018-03-03 21:54         ` Ludovic Courtès
2018-03-03 22:37           ` Danny Milosavljevic [this message]
2018-03-05 16:51             ` Ludovic Courtès
2018-03-06  8:09               ` Danny Milosavljevic
2018-03-07 12:46                 ` Ludovic Courtès
2018-03-07 11:04               ` [bug#30498] [PATCH 0/3] Log to syslog whenever possible Ludovic Courtès
2018-03-07 11:04                 ` [bug#30498] [PATCH 1/3] Turn 'log-output-port' into a parameter Ludovic Courtès
2018-03-07 11:04                 ` [bug#30498] [PATCH 2/3] Simplify 'make-shepherd-output-port' Ludovic Courtès
2018-03-07 11:04                 ` [bug#30498] [PATCH 3/3] Use syslog for logging when running as root Ludovic Courtès
2018-03-07 15:25                 ` [bug#30498] [PATCH 0/3] Log to syslog whenever possible Ludovic Courtès
2018-03-15 17:00                 ` bug#30498: " Ludovic Courtès

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://guix.gnu.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180303233715.7ec8a08c@scratchpost.org \
    --to=dannym@scratchpost.org \
    --cc=30498@debbugs.gnu.org \
    --cc=ludo@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).