From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8DF24C4332F for ; Mon, 5 Dec 2022 15:40:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230293AbiLEPko (ORCPT ); Mon, 5 Dec 2022 10:40:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48290 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230050AbiLEPkm (ORCPT ); Mon, 5 Dec 2022 10:40:42 -0500 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3D6E1BA8 for ; Mon, 5 Dec 2022 07:40:40 -0800 (PST) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 29FDC1F747; Mon, 5 Dec 2022 15:40:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1670254839; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=af80sJB23JE+1RCXzxLADGzxcTl1jqHmsSi3AWSU52M=; b=hhbGHQZgkrQ6m8h6Ro26CDXXdpxQj1iD2T7L2g7Ncg0WOxSi+onbiMX0JPJm0br16MZE5y Z1OLiQ0AKnD9D5JMdWHunvPab2G4FkuSN2S+fstwJe5dz4wOo6C4cCYyBBKldhqk0K/gSW 1e1zapXKdOxhjqt7pGTvKXAj2mV9T1g= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1670254839; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=af80sJB23JE+1RCXzxLADGzxcTl1jqHmsSi3AWSU52M=; b=4I07ydAWwMSRr7/qlMlBEgg8vksxw9c7xr7IMdzBNwimO87zo8yhg47h1gL47fDMXWBXrq f/S/NsbwTHgPHnDQ== Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap1.suse-dmz.suse.de (Postfix) with ESMTPS id 1C07A13326; Mon, 5 Dec 2022 15:40:39 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap1.suse-dmz.suse.de with ESMTPSA id VW/OBvcQjmOGOwAAGKfGzw (envelope-from ); Mon, 05 Dec 2022 15:40:39 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id 9EA74A0720; Mon, 5 Dec 2022 16:40:38 +0100 (CET) Date: Mon, 5 Dec 2022 16:40:38 +0100 From: Jan Kara To: "lihaoxiang (F)" Cc: jack@suse.cz, linux-ext4@vger.kernel.org, Zhiqiang Liu , linfeilong@huawei.com, louhongxiang@huawei.com, "lijinlin (A)" Subject: Re: [PATCH] quota-nld: fix open PID file failed when systemd read it Message-ID: <20221205154038.esy7qsjajmfoxfo5@quack3> References: <29fb4747-5162-4e50-2771-570dc8776c26@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <29fb4747-5162-4e50-2771-570dc8776c26@huawei.com> Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org On Thu 01-12-22 12:10:49, lihaoxiang (F) wrote: > Running quota_nld by systemd might cause the problem that systemd > couldn't open the PID file generated by quota_nld. In fact, the PID > file hasn't existed yet because it originates from the child process > of quota_nld which is a daemon process. As the main process exit, > systemd try to access the PID file but the daemon hadn't create it > that time. > > In this situation, we move the procedure of creating PID file into the > parent process to ensure the PID file must existed when quota_nld exit. > After that, the above problem would never occur again. > > Signed-off-by: lihaoxiang Thanks for the fix! In your patch the SIGTERM handling is actually wrong (as it now happens in the parent process instead of in the child). Also some format strings needed to be fixed up. I've done these changes and pushed the patch to my tree. Honza > --- > quota_nld.c | 43 +++++++++++++++++++++++++++++++++---------- > 1 file changed, 33 insertions(+), 10 deletions(-) > > diff --git a/quota_nld.c b/quota_nld.c > index 09c4775..4a02eb1 100644 > --- a/quota_nld.c > +++ b/quota_nld.c > @@ -413,7 +413,7 @@ static char *build_pid_file_name(void) > } > > /* Store daemon's PID to file */ > -static int store_pid(void) > +static int store_pid(pid_t pid) > { > FILE *pid_file; > char *pid_name; > @@ -429,7 +429,7 @@ static int store_pid(void) > free(pid_name); > return -1; > } > - if (fprintf(pid_file, "%jd\n", (intmax_t)getpid()) < 0) { > + if (fprintf(pid_file, "%jd\n", pid) < 0) { > errstr(_("Could not write daemon's PID into '%s'.\n"), > pid_name); > fclose(pid_file); > @@ -460,7 +460,7 @@ static void remove_pid(int signal) > } > > /* Store daemon's PID into file and register its removal on SIGTERM */ > -static void use_pid_file(void) > +static void use_pid_file(pid_t pid) > { > struct sigaction term_action; > > @@ -468,8 +468,35 @@ static void use_pid_file(void) > term_action.sa_flags = 0; > if (sigemptyset(&term_action.sa_mask) || sigaction(SIGTERM, &term_action, NULL)) > errstr(_("Could not register PID file removal on SIGTERM.\n")); > - if (store_pid()) > - errstr(_("Could not store my PID %jd.\n"), (intmax_t )getpid()); > + if (store_pid(pid)) > + errstr(_("Could not store my PID %jd.\n"), pid); > +} > + > +static void fork_daemon() > +{ > + pid_t pid = fork(); > + if (pid < 0) { > + errstr(_("Failed to daemonize: fork error with %s\n"), strerror(errno)); > + exit(1); > + } else if (pid != 0) { > + use_pid_file(pid); > + exit(0); > + } > + > + if (setsid() == -1) { > + errstr(_("Failed to daemonize: setsid error with %s\n"), strerror(errno)); > + exit(1); > + } > + if (chdir("/")) > + errstr(_("Failed to chdir in daemonize \n")); > + int fd = open("/dev/null", O_RDWR, 0); > + if (fd >= 0) { > + (void)dup2(fd, STDIN_FILENO); > + (void)dup2(fd, STDOUT_FILENO); > + (void)dup2(fd, STDERR_FILENO); > + > + (void)close(fd); > + } > } > > int main(int argc, char **argv) > @@ -485,11 +512,7 @@ int main(int argc, char **argv) > dhandle = init_dbus(); > if (!(flags & FL_NODAEMON)) { > use_syslog(); > - if (daemon(0, 0)) { > - errstr(_("Failed to daemonize: %s\n"), strerror(errno)); > - exit(1); > - }; > - use_pid_file(); > + fork_daemon(); > } > run(nsock); > return 0; > -- > 2.37.0.windows.1 -- Jan Kara SUSE Labs, CR