Subject: bug#30498: closed (Re: [bug#30498] [PATCH 0/3] Log to syslog whenever possible)

From: [email-masked] (GNU bug Tracking System)
Date: Thu, 15 Mar 2018 17:01:02 +0000

Your bug report

#30498: [PATCH shepherd] shepherd: If /dev/kmsg is writable, use it for logging.

which was filed against the guix-patches package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
30498: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=30498
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: [bug#30498] [PATCH 0/3] Log to syslog whenever possible
From: [email-masked] (Ludovic Courtès)
To: [email-masked]
Cc: Danny Milosavljevic <[email-masked]>
Date: Thu, 15 Mar 2018 18:00:11 +0100
List-Id: <debbugs-submit.debbugs.gnu.org>

Ludovic Courtès <[email-masked]> skribis:

>   Turn 'log-output-port' into a parameter.
>   Simplify 'make-shepherd-output-port'.
>   Use syslog for logging when running as root.

Pushed!

For the record, this can be tested on GuixSD by running “make dist” in
the Shepherd and then applying something like the patch below in Guix:

diff --git a/gnu/packages/admin.scm b/gnu/packages/admin.scm
index ad31bc498..04cb69e1b 100644
--- a/gnu/packages/admin.scm
+++ b/gnu/packages/admin.scm
@@ -47,6 +47,7 @@
   #:use-module (guix build-system perl)
   #:use-module (guix build-system python)
   #:use-module (guix build-system trivial)
+  #:use-module (guix gexp)
   #:use-module (gnu packages)
   #:use-module (gnu packages base)
   #:use-module (gnu packages check)
@@ -159,16 +160,8 @@ and provides a \"top-like\" mode (monitoring).")
 (define-public shepherd
   (package
     (name "shepherd")
-    (version "0.3.2")
-    (source (origin
-              (method url-fetch)
-              (uri (string-append "ftp://alpha.gnu.org/gnu/dmd/shepherd-"
-                                  version ".tar.gz"))
-              (sha256
-               (base32
-                "174q1qg7yg6w1hfvlfv720hr6hid4h5xzw15y3ycfpspllzldhcb"))
-              (patches (search-patches "shepherd-close-fds.patch"
-                                       "shepherd-herd-status-sorted.patch"))))
+    (version "0.3.3")
+    (source (local-file "/data/src/shepherd/shepherd-0.3.2.tar.gz"))
     (build-system gnu-build-system)
     (arguments
      '(#:configure-flags '("--localstatedir=/var")))

Ludo’.

Subject: [PATCH shepherd] shepherd: If /dev/kmsg is writable, use it for logging.
From: Danny Milosavljevic <[email-masked]>
To: [email-masked]
Cc: Danny Milosavljevic <[email-masked]>
Date: Sat, 17 Feb 2018 13:20:35 +0100
List-Id: <debbugs-submit.debbugs.gnu.org>

* modules/shepherd.scm (main): If /dev/kmsg is used, don't log to console
again - use only /dev/kmsg.
* modules/shepherd/comm.scm (%current-logfile-date-format): New variable.
(make-shepherd-output-port): Use it.  Export.
* modules/shepherd/support.scm (default-logfile-date-format): New variable.
(default-logfile): Use /dev/kmsg if writable.
(default-logfile-date-format): Drop duplicate timestamp.
---
 modules/shepherd.scm         |  6 +++++-
 modules/shepherd/comm.scm    | 11 ++++++-----
 modules/shepherd/support.scm | 11 ++++++++++-
 3 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/modules/shepherd.scm b/modules/shepherd.scm
index 5334657..2db56a1 100644
--- a/modules/shepherd.scm
+++ b/modules/shepherd.scm
@@ -141,8 +141,12 @@
     ;; Enable logging as first action.
     (start-logging logfile)
 
+    (if (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)))
 
     ;; Start the 'root' service.
     (start root-service)
diff --git a/modules/shepherd/comm.scm b/modules/shepherd/comm.scm
index 0228f63..6cea6f0 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))
 
 
 ;; Command for shepherd.
@@ -200,6 +201,9 @@ on service '~a':")
   ;; Socket of the client currently talking to the daemon.
   (make-parameter #f))
 
+(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.
@@ -228,7 +232,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 +263,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/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