From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pve-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
	by lore.proxmox.com (Postfix) with ESMTPS id BDCA21FF168
	for <inbox@lore.proxmox.com>; Tue, 18 Mar 2025 11:28:30 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id C30B11C2AD;
	Tue, 18 Mar 2025 11:28:18 +0100 (CET)
Date: Tue, 18 Mar 2025 11:28:12 +0100
From: Wolfgang Bumiller <w.bumiller@proxmox.com>
To: Christoph Heiss <c.heiss@proxmox.com>
Message-ID: <hwjlbstxqqtzlcn6oq3vp6gctnv2xfubmif36chwnsbl4c3lf7@jg6ewf5iwxfc>
References: <20250317141152.1247324-1-c.heiss@proxmox.com>
 <20250317141152.1247324-5-c.heiss@proxmox.com>
MIME-Version: 1.0
Content-Disposition: inline
In-Reply-To: <20250317141152.1247324-5-c.heiss@proxmox.com>
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.081 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to
 Validity was blocked. See
 https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more
 information.
 RCVD_IN_VALIDITY_RPBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to
 Validity was blocked. See
 https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more
 information.
 RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to
 Validity was blocked. See
 https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more
 information.
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [tools.pm]
Subject: Re: [pve-devel] [PATCH common 04/14] tools: add run_fork_detached()
 for spawning daemons
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
Reply-To: Proxmox VE development discussion <pve-devel@lists.proxmox.com>
Cc: pve-devel@lists.proxmox.com
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: pve-devel-bounces@lists.proxmox.com
Sender: "pve-devel" <pve-devel-bounces@lists.proxmox.com>

On Mon, Mar 17, 2025 at 03:11:41PM +0100, Christoph Heiss wrote:
> This essentially just does a fork() + setsid().
> Needed to e.g. properly spawn background processes.
> 
> Signed-off-by: Christoph Heiss <c.heiss@proxmox.com>
> ---
> Something similar is already used in e.g. pve-storage to spawn fuse
> mounts. If and when this is applied, I'd migrate these sites to this sub
> too.

IIRC there are still outstanding issues with it creating zombie
processes because it only does a single fork instead of properly
daemonizing with a double-fork.

While an alternative to double-forking would be to see if we can add a
general child-reaping mechanic to our daemons (either via a proper
SIGCLD handler, or a signalfd if AnyEvent supports that?), it is
situation dependent on whether the process should be a child process or
a "detached" process as the sub below implies - the question there being
whether the child should be killed if the parent dies.

> 
>  src/PVE/Tools.pm | 30 ++++++++++++++++++++++++++++++
>  1 file changed, 30 insertions(+)
> 
> diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm
> index 0325f53..f5bf24a 100644
> --- a/src/PVE/Tools.pm
> +++ b/src/PVE/Tools.pm
> @@ -1117,6 +1117,36 @@ sub run_fork {
>      return run_fork_with_timeout(undef, $code, $opts);
>  }
>  
> +sub run_fork_detached {
> +    my ($fn) = @_;
> +
> +    pipe(my $rd, my $wr) or die "failed to create pipe: $!\n";
> +
> +    my $pid = fork();
> +    die "fork failed: $!\n" if !defined($pid);
> +
> +    if (!$pid) {
> +	undef $rd;
> +	POSIX::setsid();
> +
> +	eval { $fn->(); };
> +	if (my $err = $@) {
> +	    print {$wr} "ERROR: $err";
> +	}
> +	POSIX::_exit(1);
> +    };
> +    undef $wr;
> +
> +    my $result = do { local $/ = undef; <$rd> };
> +    if ($result =~ /^ERROR: (.*)$/) {
> +	die "$1\n";
> +    }
> +
> +    if (waitpid($pid, POSIX::WNOHANG) == $pid) {
> +	die "failed to spawn process, process exited with status $?\n";
> +    }
> +}
> +
>  # NOTE: NFS syscall can't be interrupted, so alarm does
>  # not work to provide timeouts.
>  # from 'man nfs': "Only SIGKILL can interrupt a pending NFS operation"
> -- 
> 2.48.1


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel