From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp12.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id aCS1NlvGHWLdEwEAgWs5BA (envelope-from ) for ; Tue, 01 Mar 2022 08:08:11 +0100 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp12.migadu.com with LMTPS id cCCCM1vGHWIsaQEAauVa8A (envelope-from ) for ; Tue, 01 Mar 2022 08:08:11 +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 5CA672A826 for ; Tue, 1 Mar 2022 08:08:11 +0100 (CET) Received: from localhost ([::1]:36588 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nOwcD-00059p-OL for larch@yhetil.org; 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 Content-Transfer-Encoding: 8bit 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+larch=yhetil.org@gnu.org Sender: "Guix-patches" 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=1646118491; 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:list-id:list-help: list-unsubscribe:list-subscribe:list-post:dkim-signature; bh=75wmsSlr14mzKkpZBqiq1CEZt/ErTeewRLFbbB19M/o=; b=dNweDuhqDLAiUeEVa+ui9+nj4NQhG+uSsPtbj/gq5+gRTkwaer85BPNjrIxZGReYxt3raF NKRkTDadr8dMqZ4EqkC0+RHa7fpPhoizFgpkjc+JGOvEDXFNky6sYvXmDU3YkRGdZq3Ldp fnzvic1C1Jx06IWvyung8ik6DRJjprufcQr/BCrgdEDkWLjFTh6drDTU6M9TP224YgYp+M IYCClKDXWU7eKFbLHapwDuxcq2W/uAKSV/YEBmFgAN29oZjdzXeesaRAdXGCiWYTwrZXgg xRzLQhsYC56nFuel57fH/gAxUdbJ/k/xBJ+1P4cADCRcdVX9pVsNOM5HbvK/2Q== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1646118491; a=rsa-sha256; cv=none; b=moaBI7QtxRfjb1kH99nAYhCyzzIQNHRCRJ29NfanJX6iExYtVl8qixFBO/qM9G54IzBbS8 vbTYAPEdc8gxZmYxA6Vdfya7JH8bPCD6lmABsWRu8yXeTOdhbYO9shNfSM2M/OAuAXAoNd LOHhsRiG2qffQQVoBj4LjEjuHeprRgeEHe9WwgTvX34X3dwaV2Pf6k5AN5fFGwgwJQXYtX WuHLgU4qZxi7Vo6LILv+68TyCRu/v9LEWdUX18XUwVg0SHpp5dbTm5V9nzYBOp0FJ5ZwhF kZKaGEf6uD2dVkRsBMEeJrSuKLHTulsTNXV/eI1bREqT0KnfhO4VNzvCEAAHkg== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20210112 header.b=mULNaXSO; dmarc=none; 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: 0.91 Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20210112 header.b=mULNaXSO; dmarc=none; 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: 5CA672A826 X-Spam-Score: 0.91 X-Migadu-Scanner: scn0.migadu.com X-TUID: 3MNk9yLexxNL 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) -- 2.34.0