This is an automated email from the git hooks/post-receive script.

civodul pushed a commit to branch main
in repository shepherd.

The following commit(s) were added to refs/heads/main by this push:
     new a54a503  system-log: Start even if #:kernel-log-file cannot be opened.
a54a503 is described below

commit a54a503b01f9da4f6fb49d736dcf90da0fe51f1b
Author: Ludovic Courtès <l...@gnu.org>
AuthorDate: Wed May 7 00:06:15 2025 +0200

    system-log: Start even if #:kernel-log-file cannot be opened.
    
    Partly fixes <https://issues.guix.gnu.org/77578>.
    
    Previously ‘system-log’ would fail to start if it could not
    open #:kernel-log-file.  This is unwise because in practice it would
    prevent most other system services from starting.  Furthermore,
    on Linux /proc/kmsg is typically inaccessible in containers, so
    gracefully handling it makes more sense.
    
    * modules/shepherd/service/system-log.scm (system-log-service)
    [maybe-kernel-log-port]: New procedure.
    Use it.
    * tests/services/system-log.sh: Add test where #:kernel-log-file is
    inaccessible.
    * NEWS: Update.
    
    Reported-by: Oleg Pykhalov <go.wig...@gmail.com>
---
 NEWS                                    | 11 +++++++++
 modules/shepherd/service/system-log.scm | 44 +++++++++++++++++++++------------
 tests/services/system-log.sh            | 25 +++++++++++++++++++
 3 files changed, 64 insertions(+), 16 deletions(-)

diff --git a/NEWS b/NEWS
index cfc27aa..1022b7e 100644
--- a/NEWS
+++ b/NEWS
@@ -18,6 +18,17 @@ Please send Shepherd bug reports to bug-g...@gnu.org.
 When using the ‘system-log’ service, ‘herd status system-log’ now shows the
 socket(s) it is listening to and the kernel log file it is reading (if any).
 
+** ‘system-log’ starts even if #:kernel-log-file is inaccessible
+   (<https://issues.guix.gnu.org/77578>)
+
+It used to be that ‘system-log’ would fail to start if the file specified as
+#:kernel-log-file was inaccessible.  This was unwise because that would then
+typically prevent most system services from starting; also, on Linux, the
+default #:kernel-log-file is /proc/kmsg, and that is inaccessible within
+containers.
+
+Failure to open #:kernel-log-file is now logged but is non-fatal.
+
 ** ‘system-log’ service reads /dev/klog on the Hurd
    (<https://issues.guix.gnu.org/77634>)
 
diff --git a/modules/shepherd/service/system-log.scm 
b/modules/shepherd/service/system-log.scm
index 890a0f5..435949c 100644
--- a/modules/shepherd/service/system-log.scm
+++ b/modules/shepherd/service/system-log.scm
@@ -481,31 +481,43 @@ for a format identical to that of traditional syslogd 
implementations.
 
 Keep up to @var{history-size} messages in memory for the purposes of allowing
 users to view recent messages without opening various files."
+  (define (maybe-kernel-log-port)
+    (catch 'system-error
+      (lambda ()
+        (open kernel-log-file
+              (logior O_RDONLY O_NONBLOCK O_CLOEXEC)))
+      (lambda args
+        ;; If KERNEL-LOG-FILE cannot be opened, dismiss it instead of failing
+        ;; to start since that could prevent many system services from
+        ;; starting (useful for example in Linux containers where /proc/kmsg
+        ;; is not accessible).
+        (local-output (l10n "Dismissing kernel log '~a': ~a.")
+                      kernel-log-file
+                      (strerror (system-error-errno args)))
+        #f)))
+
   (define this-system-log
     (service provision
              #:requirement requirement
              #:start (lambda ()
-                       (let ((channel (make-channel))
-                             (ports (append (open-sockets sources)
-                                            (if kernel-log-file
-                                                (list (open kernel-log-file
-                                                            (logior O_RDONLY
-                                                                    O_NONBLOCK
-                                                                    
O_CLOEXEC)))
-                                                '())))
-                             (dispatcher (spawn-log-dispatcher 
message-destination
-                                                               
#:max-silent-time
-                                                               max-silent-time
-                                                               #:history-size
-                                                               history-size
-                                                               #:date-format
-                                                               date-format)))
+                       (let* ((channel (make-channel))
+                              (klog (and kernel-log-file
+                                         (maybe-kernel-log-port)))
+                              (ports (append (open-sockets sources)
+                                             (if klog (list klog) '())))
+                              (dispatcher (spawn-log-dispatcher 
message-destination
+                                                                
#:max-silent-time
+                                                                max-silent-time
+                                                                #:history-size
+                                                                history-size
+                                                                #:date-format
+                                                                date-format)))
                          (register-service-logger this-system-log dispatcher)
                          (spawn-fiber
                           (lambda ()
                             (run-system-log channel ports dispatcher)))
                          (system-log channel ports dispatcher
-                                     sources kernel-log-file)))
+                                     sources (and klog kernel-log-file))))
              #:stop (lambda (system-log)
                       (let ((reply (make-channel)))
                         (put-message (system-log-channel system-log)
diff --git a/tests/services/system-log.sh b/tests/services/system-log.sh
index 047b9dd..5bc8116 100644
--- a/tests/services/system-log.sh
+++ b/tests/services/system-log.sh
@@ -248,6 +248,31 @@ do
     test -f "$file"
 done
 
+$herd stop system-log
+
+# Check that an inaccessible #:kernel-log-file does not prevent 'system-log'
+# from starting.
+
+cat > "$conf" <<EOF
+(use-modules (shepherd service system-log)
+             (shepherd endpoints)
+             (srfi srfi-19))
+
+(define %endpoints
+  (list (endpoint (make-socket-address AF_UNIX "$syslog_socket")
+                  #:style SOCK_DGRAM)))
+
+(register-services
+  (list (system-log-service %endpoints
+                            #:kernel-log-file "/does/not/exist")))
+EOF
+
+$herd load root "$conf"
+$herd start system-log
+$herd status system-log | grep "$syslog_socket"
+if $herd status system-log | grep "/does/not/exist"; then false; else true; fi
+grep "Dismissing kernel log.*/does/not/exist" "$log"
+
 $herd stop root
 
 # Local Variables:

Reply via email to