From patchwork Tue Mar 1 07:06:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Attila Lendvai X-Patchwork-Id: 37560 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 45E5627BBE9; Tue, 1 Mar 2022 07:08:13 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL, SPF_HELO_PASS autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 09E1627BBEA for ; Tue, 1 Mar 2022 07:08:10 +0000 (GMT) Received: from localhost ([::1]:36588 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nOwcD-00059p-65 for patchwork@mira.cbaines.net; Tue, 01 Mar 2022 02:08:09 -0500 Received: from eggs.gnu.org ([209.51.188.92]:34066) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nOwc6-00059R-9m for guix-patches@gnu.org; Tue, 01 Mar 2022 02:08:02 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:41400) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nOwc6-0003uM-1U for guix-patches@gnu.org; Tue, 01 Mar 2022 02:08:02 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nOwc5-0006yy-TC for guix-patches@gnu.org; Tue, 01 Mar 2022 02:08:01 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#54205] [PATCH Shepherd] Factor out a public CALL-IN-FORK. Resent-From: Attila Lendvai Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 01 Mar 2022 07:08:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 54205 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 54205@debbugs.gnu.org Cc: Attila Lendvai X-Debbugs-Original-To: guix-patches@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.164611847826828 (code B ref -1); Tue, 01 Mar 2022 07:08:01 +0000 Received: (at submit) by debbugs.gnu.org; 1 Mar 2022 07:07:58 +0000 Received: from localhost ([127.0.0.1]:35297 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nOwc2-0006ye-8p for submit@debbugs.gnu.org; Tue, 01 Mar 2022 02:07:58 -0500 Received: from lists.gnu.org ([209.51.188.17]:55250) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nOwbz-0006yU-AO for submit@debbugs.gnu.org; Tue, 01 Mar 2022 02:07:56 -0500 Received: from eggs.gnu.org ([209.51.188.92]:34030) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nOwbz-00056p-4a for guix-patches@gnu.org; Tue, 01 Mar 2022 02:07:55 -0500 Received: from [2a00:1450:4864:20::62f] (port=38718 helo=mail-ej1-x62f.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nOwbm-0003s0-JD for guix-patches@gnu.org; Tue, 01 Mar 2022 02:07:54 -0500 Received: by mail-ej1-x62f.google.com with SMTP id r13so29529168ejd.5 for ; Mon, 28 Feb 2022 23:07:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=75wmsSlr14mzKkpZBqiq1CEZt/ErTeewRLFbbB19M/o=; b=mULNaXSO0k0wHqeplu3K+sTjZhbDwm4JRBSoNVqaooz9/M5dMDYkZUfR7Ca/0KLFnp Dk4HNunPofZeCNkA2HNWkgH/2+14InKr9Z/XzYkYvj84s18K381Z6I/lqrZzv7YHZUBm oGi0Mrqyf1ncE6bYG02MA83RUkgkSUjpVYlx7T8AhXEJa63idvFm20a/vya9OYUCFIXy GVrnk9HWr3/cN2dh3fdS1v43tpLzKpsRDSHeVygB8XIM2SjZQFeB8XEcs0ffY7y0U5V5 8B6DGedA5/Id6YoKHkRtajoH2sdDwbpRsp7snK61M6aPU/NL5OvZiVxcM/+xmj2SK64D nTYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :mime-version:content-transfer-encoding; bh=75wmsSlr14mzKkpZBqiq1CEZt/ErTeewRLFbbB19M/o=; b=HOALzW0lcFPH5sTeyhQTtvXjhEFWE+zF7Mb0RgVAvEZlmO7MTMpTCvCPbJ8pa8vlQx bDhFE3H5tkHWKNDBABk2bXaD6OYOWr7Lrx72qVt6ojHBZWzPVv06aXUxq4wFdzPIQcoq 3TNeCy/ejAoMGwmyfSn8O73GCV16QZNh1EWX3SjBfTo5ZMw0XnzH0DY2ozjnlBw5aJfx G6I/N+CAP5mKsD6bJn4u2Z8KKCVFcobLTO1zjcUveFzK8TS2WhkuyEyleMTVc7z092jR hlPKIo2nkv6ingAfyD0XbQavTfPdfszeOh4wfsrGkJOrtxb5SVbuR7XbWsgqKIzewK3U NpPw== X-Gm-Message-State: AOAM530wvscIx2mZzNBRRjzvqkIKmJUdouoqIGHkgA7WFbnU8zpZ0/sC bbaZy2PHhKB5PX7j9iuAaD3wTg8NyyE= X-Google-Smtp-Source: ABdhPJzkTBx+y0wmz4tHnO2ShykpbKq4ZfLNY6NmuNADwcvvVqcFVfwVwv9ZsuSFbiPCUXIYYw5HAg== X-Received: by 2002:a17:906:a08:b0:6ce:e6d:7a63 with SMTP id w8-20020a1709060a0800b006ce0e6d7a63mr17713309ejf.309.1646118454929; Mon, 28 Feb 2022 23:07:34 -0800 (PST) Received: from lelap.local (catv-89-132-245-188.catv.fixed.vodafone.hu. [89.132.245.188]) by smtp.gmail.com with ESMTPSA id e11-20020aa7d7cb000000b00412a96ba630sm6744987eds.25.2022.02.28.23.07.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Feb 2022 23:07:34 -0800 (PST) From: Attila Lendvai Date: Tue, 1 Mar 2022 08:06:15 +0100 Message-Id: <20220301070615.21028-1-attila@lendvai.name> X-Mailer: git-send-email 2.34.0 MIME-Version: 1.0 X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a00:1450:4864:20::62f (failed) Received-SPF: pass client-ip=2a00:1450:4864:20::62f; envelope-from=attila.lendvai@gmail.com; helo=mail-ej1-x62f.google.com X-Spam_score_int: 0 X-Spam_score: -0.1 X-Spam_bar: / X-Spam_report: (-0.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.249, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action 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+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches This enables service implementations to easily inject code that is run before their service is started. One such example is calling setrlimit from a start action to set NOFILE (the open files limit), before the service is exec'ed and thus inherits this value from the parent process, i.e. from Shepherd. * modules/shepherd/service.scm (fork-and-call): New function. (fork+exec-command): Use the above. --- modules/shepherd/service.scm | 51 ++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/modules/shepherd/service.scm b/modules/shepherd/service.scm index ad8608b..8d5e30f 100644 --- a/modules/shepherd/service.scm +++ b/modules/shepherd/service.scm @@ -79,6 +79,7 @@ make-forkexec-constructor make-kill-destructor exec-command + fork-and-call fork+exec-command default-pid-file-timeout read-pid-file @@ -883,19 +884,8 @@ false." ;; Signals that the shepherd process handles. (list SIGCHLD SIGINT SIGHUP SIGTERM)) -(define* (fork+exec-command command - #:key - (user #f) - (group #f) - (supplementary-groups '()) - (log-file #f) - (directory (default-service-directory)) - (file-creation-mask #f) - (create-session? #t) - (environment-variables - (default-environment-variables))) - "Spawn a process that executed COMMAND as per 'exec-command', and return -its PID." +(define* (fork-and-call thunk) + "Call THUNK in a fork." ;; Install the SIGCHLD handler if this is the first fork+exec-command call. (unless %sigchld-handler-installed? (sigaction SIGCHLD handle-SIGCHLD SA_NOCLDSTOP) @@ -916,17 +906,34 @@ its PID." ;; process. (unblock-signals %precious-signals) - (exec-command command - #:user user - #:group group - #:supplementary-groups supplementary-groups - #:log-file log-file - #:directory directory - #:file-creation-mask file-creation-mask - #:create-session? create-session? - #:environment-variables environment-variables)) + (thunk)) pid)))) +(define* (fork+exec-command command + #:key + (user #f) + (group #f) + (supplementary-groups '()) + (log-file #f) + (directory (default-service-directory)) + (file-creation-mask #f) + (create-session? #t) + (environment-variables + (default-environment-variables))) + "Spawn a process that executed COMMAND as per 'exec-command', and return +its PID." + (fork-and-call + (lambda () + (exec-command command + #:user user + #:group group + #:supplementary-groups supplementary-groups + #:log-file log-file + #:directory directory + #:file-creation-mask file-creation-mask + #:create-session? create-session? + #:environment-variables environment-variables)))) + (define* (make-forkexec-constructor command #:key (user #f)