From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53352) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1esFn4-0007jp-DU for guix-patches@gnu.org; Sat, 03 Mar 2018 17:38:08 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1esFn1-0002Zx-5c for guix-patches@gnu.org; Sat, 03 Mar 2018 17:38:06 -0500 Received: from debbugs.gnu.org ([208.118.235.43]:35391) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1esFn1-0002Zq-1L for guix-patches@gnu.org; Sat, 03 Mar 2018 17:38:03 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1esFn0-0007cr-S1 for guix-patches@gnu.org; Sat, 03 Mar 2018 17:38:02 -0500 Subject: [bug#30498] [WIP v2 shepherd] shepherd: If /dev/kmsg is writable, use it for logging. Resent-Message-ID: Date: Sat, 3 Mar 2018 23:37:15 +0100 From: Danny Milosavljevic Message-ID: <20180303233715.7ec8a08c@scratchpost.org> In-Reply-To: <87bmg46d10.fsf@gnu.org> References: <20180217122035.1443-1-dannym@scratchpost.org> <20180217164835.1178-1-dannym@scratchpost.org> <87muzvtyo3.fsf@gnu.org> <20180226233214.6fe40fa3@scratchpost.org> <87inai6b7w.fsf@gnu.org> <87bmg46d10.fsf@gnu.org> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; boundary="Sig_/EoBWwCLfQr0JvEKz8V5uaAv"; protocol="application/pgp-signature" List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+kyle=kyleam.com@gnu.org Sender: "Guix-patches" To: Ludovic =?UTF-8?Q?Court=C3=A8s?= Cc: 30498@debbugs.gnu.org --Sig_/EoBWwCLfQr0JvEKz8V5uaAv Content-Type: multipart/mixed; boundary="MP_/MmnShyv31=IPi5TGbYGeqiS" --MP_/MmnShyv31=IPi5TGbYGeqiS Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline 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) =20 - (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-p= ort)))) + (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. --MP_/MmnShyv31=IPi5TGbYGeqiS Content-Type: text/x-patch Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename=shepherd-klog.patch 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 possib= le---then it uses @code{/var/log/shepherd.log}), @code{$XDG_CONFIG_HOME/shepherd/shepherd.log} otherwise. =20 @item --pid[=3D@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) =20 + (when (string=3D? 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))) =20 ;; 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)) =20 =0C ;; Command for shepherd. @@ -200,10 +201,18 @@ on service '~a':") ;; Socket of the client currently talking to the daemon. (make-parameter #f)) =20 +;; 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 =20 @@ -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)) =20 ;; 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-form= at) (localtime (current-time)))= )))) (init-line) (for-each log (reverse buffer)) @@ -259,6 +268,3 @@ on service '~a':") =20 ;; 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) =20 - (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-p= ort)))) + (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 @@ =20 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 + na= me." ;; 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"))) =20 +(define default-logfile-date-format + (if (and (zero? (getuid)) (string=3D? 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 --MP_/MmnShyv31=IPi5TGbYGeqiS-- --Sig_/EoBWwCLfQr0JvEKz8V5uaAv Content-Type: application/pgp-signature Content-Description: OpenPGP digital signature -----BEGIN PGP SIGNATURE----- iQEzBAEBCAAdFiEEds7GsXJ0tGXALbPZ5xo1VCwwuqUFAlqbI5sACgkQ5xo1VCww uqWvwgf/WpjVhzjdtyUrbgBy57DT+lHn7YSJbzIlolWnTu8EVKt2e6Ejr78xkaWH hpyH1SxklOeVO38FO5uC+pbn7sagfTuYfOzhFtP76rQzCBHYf/8q1Dqw6VFqdHrq cHfFTACgVzjfJz/DNEZPKdpwQ5lfK4CDHET5EC3oZL0G/kGdm/caLVhBwqaQNrFD EbW3hkdbWqpUB70cdWloVitgI58jlH0nhZChfVGWvFVN9ZoTVEm67S5pOfOT+Pl6 a4A3tldtmiPlRQbxFqqE5yZ0g8twrtSDv6rycnFYduv6uX5JelOZSeNjHZDIPM9X x6gAUzi0lymib7grPhBGAZD+B42JTA== =kF9P -----END PGP SIGNATURE----- --Sig_/EoBWwCLfQr0JvEKz8V5uaAv--