From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48662) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1en5fd-0001RB-0R for guix-patches@gnu.org; Sat, 17 Feb 2018 11:49:06 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1en5fZ-0006zT-Va for guix-patches@gnu.org; Sat, 17 Feb 2018 11:49:05 -0500 Received: from debbugs.gnu.org ([208.118.235.43]:40633) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1en5fZ-0006zN-RQ for guix-patches@gnu.org; Sat, 17 Feb 2018 11:49:01 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1en5fZ-0000Bn-JS for guix-patches@gnu.org; Sat, 17 Feb 2018 11:49:01 -0500 Subject: [bug#30498] [WIP v2 shepherd] shepherd: If /dev/kmsg is writable, use it for logging. Resent-Message-ID: From: Danny Milosavljevic Date: Sat, 17 Feb 2018 17:48:35 +0100 Message-Id: <20180217164835.1178-1-dannym@scratchpost.org> In-Reply-To: <20180217122035.1443-1-dannym@scratchpost.org> References: <20180217122035.1443-1-dannym@scratchpost.org> 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: 30498@debbugs.gnu.org * modules/shepherd.scm (main): If /dev/kmsg is used, don't log to console again - use only /dev/kmsg. Also redirect stderr to /dev/kmsg in that case. * 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 | 10 +++++++++- modules/shepherd/comm.scm | 20 ++++++++++++-------- modules/shepherd/support.scm | 11 ++++++++++- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/modules/shepherd.scm b/modules/shepherd.scm index 5334657..2893143 100644 --- a/modules/shepherd.scm +++ b/modules/shepherd.scm @@ -141,12 +141,20 @@ ;; 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) + (set-current-error-port + (make-shepherd-output-port (current-error-port) (const #f))) + ;; This _must_ succeed. (We could also put the `catch' around ;; `main', but it is often useful to get the backtrace, and ;; `caught-error' does not do this yet.) diff --git a/modules/shepherd/comm.scm b/modules/shepherd/comm.scm index 0228f63..dc7d740 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,10 +201,16 @@ 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. -(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 +223,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 +235,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 +266,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