From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp11.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id iPnLOQHS4mE0xgAAgWs5BA (envelope-from ) for ; Sat, 15 Jan 2022 14:54:09 +0100 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp11.migadu.com with LMTPS id cHAzNwHS4mGfowAA9RJhRA (envelope-from ) for ; Sat, 15 Jan 2022 14:54:09 +0100 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 7C0343D3BB for ; Sat, 15 Jan 2022 14:54:09 +0100 (CET) Received: from localhost ([::1]:43854 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n8jVQ-0000ei-NA for larch@yhetil.org; Sat, 15 Jan 2022 08:54:08 -0500 Received: from eggs.gnu.org ([209.51.188.92]:37786) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n8jST-0005EC-98 for guix-patches@gnu.org; Sat, 15 Jan 2022 08:51:05 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:46623) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1n8jSS-0004FM-W5 for guix-patches@gnu.org; Sat, 15 Jan 2022 08:51:05 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1n8jSS-0001rC-VO for guix-patches@gnu.org; Sat, 15 Jan 2022 08:51:04 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#53063] [PATCH v2 wip-harden-installer 07/18] installer: Capture external commands output. Resent-From: Josselin Poiret Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sat, 15 Jan 2022 13:51:04 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 53063 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: Mathieu Othacehe Cc: 53063@debbugs.gnu.org, ludo@gnu.org, Josselin Poiret Received: via spool by 53063-submit@debbugs.gnu.org id=B53063.16422546427015 (code B ref 53063); Sat, 15 Jan 2022 13:51:04 +0000 Received: (at 53063) by debbugs.gnu.org; 15 Jan 2022 13:50:42 +0000 Received: from localhost ([127.0.0.1]:39506 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1n8jS5-0001oy-Mn for submit@debbugs.gnu.org; Sat, 15 Jan 2022 08:50:42 -0500 Received: from jpoiret.xyz ([206.189.101.64]:48758) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1n8jRt-0001ma-Nn for 53063@debbugs.gnu.org; Sat, 15 Jan 2022 08:50:30 -0500 Received: from authenticated-user (jpoiret.xyz [206.189.101.64]) by jpoiret.xyz (Postfix) with ESMTPA id 178BD18506C; Sat, 15 Jan 2022 13:50:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jpoiret.xyz; s=dkim; t=1642254629; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3eREbZa2+KugnerobItWN+D8awUsc0i5/edHw9BlSyo=; b=LUQsO1yV0w2lyzdYvg9/SjXTi0oO1dcph3QhWzrwpZUoul+D1zEIcEk+fb1sn2mreRl0ht 13GmtIv2IWiVjpJiTpTk8X+o48XHuSqdZ6anryIfFYDACyu7GXU4iugYBul379Q8/XnUp7 AqR16FoaPS3O3V4PvNcHuPFikgWRQTqDE/7vQK3HuwHDSJh9SHltasdOBaomErMTU4YbFj 41mURoZy+hPK318IjeL4kYRbfO+cHjnkc6GMyqLVepMDo5ifMVvO1VZonQFqJp45VJKCkT d8Mogl6k4RSo/jbI/H2iSBtATSrO7bMBoW4h/NE7DoVHP3f4I1kSgxufALisVQ== Date: Sat, 15 Jan 2022 14:50:00 +0100 Message-Id: <20220115135011.5817-8-dev@jpoiret.xyz> In-Reply-To: <20220115135011.5817-1-dev@jpoiret.xyz> References: <8735lz4xsv.fsf@gnu.org> <20220115135011.5817-1-dev@jpoiret.xyz> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spamd-Bar: / X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: "Guix-patches" Reply-to: Josselin Poiret X-ACL-Warn: , Josselin Poiret via Guix-patches From: Josselin Poiret via Guix-patches via X-Migadu-Flow: FLOW_IN X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1642254849; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=3eREbZa2+KugnerobItWN+D8awUsc0i5/edHw9BlSyo=; b=ONipVe1jvf3rnPwAowHotam0knxG+xjbYJaeNH3+o3y+wxLc0NZFDZt3vv6BbIjI1lxF4s A9gpZxr+onl27iVVkJJPgvKukN7V8+QDvGq+Twhfv/jF3puOLvFB3wBe6qNNys+rEFgmuz Kscg/0GIFrRn4BJMuQKLz04hCspwi9jhEt+nfb27u+HQlauOCNck9Cxkuwey624733slii jiD9JUKWUZEfFQklEsjm3PmYpVveCUPOrscLM9BNQ8X9vYHZzT4yP6uqcXUgo8r9BX7T0O /4G+7ifHbPd8PFXq7KAexYyil7TYY76lP2VBYAcCpFpVqpChH1wWGkoUiTUfMQ== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1642254849; a=rsa-sha256; cv=none; b=nqL1cR6hq4g5Zd91VM3vLvJIo0KXFa2m9stJzGVUjf+xl3ZuP0D2B6s5VIyeE1czrQ+c0l 64oJyEZlpaxaO9aiR723zfc96uFpQ0dZDhZve7ifWipcGC+QqTOlA9RcZOwKv/gtNUygBW vQORkUG9ZsCoQQLnYRbqkAXHFzggNUFmAQCdOzp7BT175shl7yIj8pr169spDOGaa0anv+ mOSqNoanxMd0IGqAmm3NElRzSXLnvhwYpouOVezqKDLHMNaoPyT7SxmHlP6VnhiUzcI+m0 2JKBY+7QLgmFQPDCEu51MrVu0dreRlFAtNpoVfi5uxSFdNNEWleO4mKGE1Ttqg== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=jpoiret.xyz header.s=dkim header.b=LUQsO1yV; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Spam-Score: -3.93 Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=jpoiret.xyz header.s=dkim header.b=LUQsO1yV; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Queue-Id: 7C0343D3BB X-Spam-Score: -3.93 X-Migadu-Scanner: scn0.migadu.com X-TUID: Um4RlqKy1B/m * gnu/installer/utils.scm (run-external-command-with-handler, run-external-command-with-line-hooks): New variables. (run-command): Use run-external-command-with-line-hooks. --- gnu/installer/utils.scm | 97 ++++++++++++++++++++++++++++++++--------- 1 file changed, 77 insertions(+), 20 deletions(-) diff --git a/gnu/installer/utils.scm b/gnu/installer/utils.scm index 1bff1e1229..9cfff0054b 100644 --- a/gnu/installer/utils.scm +++ b/gnu/installer/utils.scm @@ -25,7 +25,9 @@ (define-module (gnu installer utils) #:use-module (srfi srfi-1) #:use-module (srfi srfi-19) #:use-module (srfi srfi-34) + #:use-module (ice-9 control) #:use-module (ice-9 match) + #:use-module (ice-9 popen) #:use-module (ice-9 rdelim) #:use-module (ice-9 regex) #:use-module (ice-9 format) @@ -34,6 +36,8 @@ (define-module (gnu installer utils) read-all nearest-exact-integer read-percentage + run-external-command-with-handler + run-external-command-with-line-hooks run-command syslog-port @@ -78,37 +82,90 @@ (define (read-percentage percentage) (and result (string->number (match:substring result 1))))) +(define* (run-external-command-with-handler handler command) + "Run command specified by the list COMMAND in a child with output handler +HANDLER. HANDLER is a procedure taking an input port, to which the command +will write its standard output and error. Returns the integer status value of +the child process as returned by waitpid." + (match-let (((input . output) (pipe))) + ;; Hack to work around Guile bug 52835 + (define dup-output (duplicate-port output "w")) + ;; Void pipe, but holds the pid for close-pipe. + (define dummy-pipe + (with-input-from-file "/dev/null" + (lambda () + (with-output-to-port output + (lambda () + (with-error-to-port dup-output + (lambda () + (apply open-pipe* (cons "" command))))))))) + (close-port output) + (close-port dup-output) + (handler input) + (close-port input) + (close-pipe dummy-pipe))) + +(define (run-external-command-with-line-hooks line-hooks command) + "Run command specified by ARGS in a child, processing each output line with +the procedures in LINE-HOOKS. Returns the integer status value of +the child process as returned by waitpid." + (define (handler input) + (and (and=> (get-line input) + (lambda (line) + (if (eof-object? line) + #f + (begin (for-each (lambda (f) (f line)) + (append line-hooks + %default-installer-line-hooks)) + #t)))) + (handler input))) + (run-external-command-with-handler handler command)) + (define* (run-command command) "Run COMMAND, a list of strings. Return true if COMMAND exited successfully, #f otherwise." - (define env (environ)) - (define (pause) (format #t (G_ "Press Enter to continue.~%")) (send-to-clients '(pause)) - (environ env) ;restore environment variables (match (select (cons (current-input-port) (current-clients)) '() '()) (((port _ ...) _ _) (read-line port)))) - (setenv "PATH" "/run/current-system/profile/bin") - - (guard (c ((invoke-error? c) - (newline) - (format (current-error-port) - (G_ "Command failed with exit code ~a.~%") - (invoke-error-exit-status c)) - (installer-log-line "command ~s failed with exit code ~a" - command (invoke-error-exit-status c)) - (pause) - #f)) - (installer-log-line "running command ~s" command) - (apply invoke command) - (installer-log-line "command ~s succeeded" command) - (newline) - (pause) - #t)) + (installer-log-line "running command ~s" command) + (define result (run-external-command-with-line-hooks + (list %display-line-hook) + command)) + (define exit-val (status:exit-val result)) + (define term-sig (status:term-sig result)) + (define stop-sig (status:stop-sig result)) + (define succeeded? + (cond + ((and exit-val (not (zero? exit-val))) + (installer-log-line "command ~s exited with value ~a" + command exit-val) + (format #t (G_ "Command ~s exited with value ~a") + command exit-val) + #f) + (term-sig + (installer-log-line "command ~s killed by signal ~a" + command term-sig) + (format #t (G_ "Command ~s killed by signal ~a") + command term-sig) + #f) + (stop-sig + (installer-log-line "command ~s stopped by signal ~a" + command stop-sig) + (format #t (G_ "Command ~s stopped by signal ~a") + command stop-sig) + #f) + (else + (installer-log-line "command ~s succeeded" command) + (format #t (G_ "Command ~s succeeded") command) + #t))) + (newline) + (pause) + succeeded?) ;;; -- 2.34.0