From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp10.migadu.com ([2001:41d0:403:4789::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms9.migadu.com with LMTPS id 4Ne8F23T32S/4AAASxT56A (envelope-from ) for ; Fri, 18 Aug 2023 22:24:13 +0200 Received: from aspmx1.migadu.com ([2001:41d0:403:4789::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp10.migadu.com with LMTPS id gNCxFm3T32Q0FAEAG6o9tA (envelope-from ) for ; Fri, 18 Aug 2023 22:24:13 +0200 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 24D9B5E34C for ; Fri, 18 Aug 2023 22:24:12 +0200 (CEST) Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=tilde.club header.s=mail header.b=in7GRTY2; 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"; dmarc=fail reason="SPF not aligned (relaxed)" header.from=tilde.club (policy=none) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1692390253; h=from:from:sender:sender: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=49Y46aAwlLLeOv7B6+NtSPy9rl9CS5uoUZaEekowxy4=; b=TdNJZ82StR5Y0Kc1Qk1ac+8tmC4qeKW2jTASbiWaUg+ZlUxondR8/KisGf0Z9hQCp7tmYe enx9XgxRN3nB2d2A8+ci8VobT2rMTetFh9tdic3HuZgq4AnwXVA0wJdHI+hWxKxVQ8i8Ja JO608Ct1CSeEAXCSgPrb+WuxT8kEPsbtwqoJZnuQTFGzT4U+o4M/MM68rMHtdkCOGzj20F M3xu2rwMVZcTppgyQg4JY2CGW5ZmWeQXvksySg3tv59Ij6KbNHLxDck/XkmRJSnDaTKcWU 8oSohSZ1BVU+dbRkwIGQLVRXhKR0cXXZF47ykZJtQvinYcQN+7Uw1sM3FhIxCw== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1692390253; a=rsa-sha256; cv=none; b=D9qn6nrABnU5NmCQiB16+7UfRQMpXr0fCNnqLiV4nUTqX4MIKnZNIt3Gv+Wnp8dE0kkNCf qi7XgmdiCFro/NtgYvPvwwI010mvt3XwUbT5Ma6L9NJQvUR8SLtDUvGj4kRTwm9HOLPAgb sHqEn6I4MzXAOHzL+vFc/bK+4pC/RoU/8ab6dhHpp2bykAizeX0vqhJJJDFXgSluKn4aJA dn5FmLQStoemDx4oxFpEU3nkqcfTuZlf+/xN/hs0t0KQ7BFak8wtWwsLsCKxecFF2lVhtD rraZKoxDK2PBV9cnOkV47HlN4fONxh58eN+qTETVcC0vTBZgAeGwLODHq7mqkw== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=tilde.club header.s=mail header.b=in7GRTY2; 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"; dmarc=fail reason="SPF not aligned (relaxed)" header.from=tilde.club (policy=none) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qX60s-0008T6-00; Fri, 18 Aug 2023 16:24:06 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qX60p-0008RL-3y for guix-patches@gnu.org; Fri, 18 Aug 2023 16:24:03 -0400 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qX60o-0008G3-SG for guix-patches@gnu.org; Fri, 18 Aug 2023 16:24:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qX60p-0006fS-EO for guix-patches@gnu.org; Fri, 18 Aug 2023 16:24:03 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#65221] [PATCH 5/6] service: use RECONFIGURE-FDS for redirecting FDs 0-2. Resent-From: ulfvonbelow Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Fri, 18 Aug 2023 20:24:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 65221 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 65221@debbugs.gnu.org Cc: ulfvonbelow Received: via spool by 65221-submit@debbugs.gnu.org id=B65221.169239021925558 (code B ref 65221); Fri, 18 Aug 2023 20:24:03 +0000 Received: (at 65221) by debbugs.gnu.org; 18 Aug 2023 20:23:39 +0000 Received: from localhost ([127.0.0.1]:48893 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qX60Q-0006eA-TF for submit@debbugs.gnu.org; Fri, 18 Aug 2023 16:23:39 -0400 Received: from tilde.club ([2607:5300:204:4340::114]:43580 ident=postfix) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qX60O-0006e2-MI for 65221@debbugs.gnu.org; Fri, 18 Aug 2023 16:23:37 -0400 Received: by tilde.club (Postfix, from userid 5378) id AD3822250B9BD; Fri, 18 Aug 2023 20:23:35 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 tilde.club AD3822250B9BD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tilde.club; s=mail; t=1692390215; bh=lh+5H7R4WVDYzZjyh6yO9P0SCUKXn1IQbukNEpklb18=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=in7GRTY2fnH2Mt9TX87ssXX89bcnGLRY7wOUV1xdvb3hKTYCbl9l6h6YerBvTPu8c c+nFiygjqIT6u0UYNF1n6xhZDujU+GtFCxrNgWM5cEFhkoie/u0bR/XkHymDwrOYv2 z0I7YHaEBgaGkqBcL/SH6zvpw9DaB4PAknGPsHhk= From: ulfvonbelow Date: Fri, 18 Aug 2023 15:22:38 -0500 Message-Id: <20230818202239.21177-5-striness@tilde.club> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230818202239.21177-1-striness@tilde.club> References: <20230818202239.21177-1-striness@tilde.club> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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-bounces+larch=yhetil.org@gnu.org X-Migadu-Flow: FLOW_IN X-Migadu-Country: US X-Migadu-Spam-Score: -3.60 X-Spam-Score: -3.60 X-Migadu-Queue-Id: 24D9B5E34C X-Migadu-Scanner: mx1.migadu.com X-TUID: 02tJS5orwmI0 There are currently some corner cases in how EXTRA-PORTS works due to it not managing FDs 0, 1, and 2. Specifically, if one were to include a port in EXTRA-PORTS with FD 0, 1, or 2, it would *not* be preserved, and would instead represent the file that EXEC-COMMAND assigned to that file descriptor. To avoid this, it's necessary to call RECONFIGURE-FDS *before* redirecting the input, but this could clobber LOG-PORT or INPUT-PORT, so it would become necessary to include LOG-PORT and INPUT-PORT in the call to RECONFIGURE-FDS, then do the redirection using the new FD assignment, then close them. This complication can be avoided if we simply let RECONFIGURE-FDS itself do the redirection. This also solves other edge cases, like if LOG-PORT has fileno 0 or 1 (previously passing a LOG-PORT of (current-output-port) would cause an error, as the underlying file descriptor would be closed before dup2 was called to copy it), or if INPUT-PORT has fileno 0. To solve this, we have RECONFIGURE-FDS start the range it copies into at 0 instead of 3. We then explicitly pass the desired standard I/O FDs / ports at the front of the list passed to RECONFIGURE-FDS. We also use O_CLOEXEC for opening /dev/null and the log file so that the file descriptors they are originally opened on don't hang around. * modules/shepherd/service.scm (exec-command): use RECONFIGURE-FDS for redirecting FDs 0, 1, and 2. --- modules/shepherd/service.scm | 62 +++++++++++++++++------------------- 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/modules/shepherd/service.scm b/modules/shepherd/service.scm index e816cd1..3008e31 100644 --- a/modules/shepherd/service.scm +++ b/modules/shepherd/service.scm @@ -1561,38 +1561,36 @@ false." (chdir directory) (environ environment-variables) - ;; Redirect stdin. - (catch-system-error (close-fdes 0)) - ;; Make sure file descriptor zero is used, so we don't end up reusing - ;; it for something unrelated, which can confuse some packages. - (dup2 (if input-port - (fileno input-port) - (open-fdes "/dev/null" O_RDONLY)) - 0) - - (when (or log-port log-file) - (catch #t - (lambda () - ;; Redirect stdout and stderr to use LOG-FILE. - (catch-system-error (close-fdes 1)) - (catch-system-error (close-fdes 2)) - (dup2 (if log-file - (open-fdes log-file (logior O_CREAT O_WRONLY O_APPEND) - #o640) - (fileno log-port)) - 1) - (dup2 1 2)) - - (lambda (key . args) - (when log-file - (format (current-error-port) - "failed to open log-file ~s:~%" log-file)) - (print-exception (current-error-port) #f key args) - (primitive-exit 1)))) - - ;; Make EXTRA-PORTS available starting from file descriptor 3. - ;; This clears their FD_CLOEXEC flag. - (reconfigure-fds extra-ports 3) + (let* ( ;; Make sure file descriptor zero is used, so we don't end up reusing + ;; it for something unrelated, which can confuse some packages. + (stdin (or input-port (open-fdes "/dev/null" + (logior O_RDONLY + O_CLOEXEC)))) + (stdout (catch #t + (lambda () + (or log-port + (and log-file + (open-fdes log-file + (logior O_CREAT O_WRONLY O_APPEND + O_CLOEXEC) + #o640)) + 1)) + (lambda (key . args) + (when log-file + (format (current-error-port) + "failed to open log-file ~s:~%" log-file)) + (print-exception (current-error-port) #f key args) + (primitive-exit 1)))) + (stderr (if (or log-port log-file) + stdout + 2)) + (all-fds (+ 3 (length extra-ports)))) + ;; Make EXTRA-PORTS available starting from file descriptor 3. + ;; This clears their FD_CLOEXEC flag. + (reconfigure-fds (cons* stdin + stdout + stderr + extra-ports))) ;; setgid must be done *before* setuid, otherwise the user will ;; likely no longer have permissions to setgid. -- 2.40.1