From owner-freebsd-hackers@freebsd.org Wed Aug 26 14:40:21 2020 Return-Path: Delivered-To: freebsd-hackers@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 2FDD43D6541 for ; Wed, 26 Aug 2020 14:40:21 +0000 (UTC) (envelope-from kib@freebsd.org) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4Bc7nJ3xGlz4567 for ; Wed, 26 Aug 2020 14:40:20 +0000 (UTC) (envelope-from kib@freebsd.org) Received: from tom.home (kib@localhost [127.0.0.1]) by kib.kiev.ua (8.16.1/8.16.1) with ESMTPS id 07QEeDr5011105 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Wed, 26 Aug 2020 17:40:16 +0300 (EEST) (envelope-from kib@freebsd.org) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua 07QEeDr5011105 Received: (from kostik@localhost) by tom.home (8.16.1/8.16.1/Submit) id 07QEeDpw011103; Wed, 26 Aug 2020 17:40:13 +0300 (EEST) (envelope-from kib@freebsd.org) X-Authentication-Warning: tom.home: kostik set sender to kib@freebsd.org using -f Date: Wed, 26 Aug 2020 17:40:13 +0300 From: Konstantin Belousov To: J David Cc: freebsd-hackers@freebsd.org Subject: Re: pidfile_open() usage in "mount" Message-ID: <20200826144013.GV2551@kib.kiev.ua> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-2.9 required=5.0 tests=ALL_TRUSTED,BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on tom.home X-Rspamd-Queue-Id: 4Bc7nJ3xGlz4567 X-Spamd-Bar: / Authentication-Results: mx1.freebsd.org; none X-Spamd-Result: default: False [0.00 / 15.00]; ASN(0.00)[asn:6939, ipnet:2001:470::/32, country:US]; TAGGED_RCPT(0.00)[]; local_wl_from(0.00)[freebsd.org] X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 26 Aug 2020 14:40:21 -0000 On Tue, Aug 25, 2020 at 10:06:22PM -0400, J David wrote: > It looks like the "mount" program creates /var/run/mountd.pid every > time it runs, if mountd is not itself running. > > This code appears in sbin/mount/mount.c: > > static void > restart_mountd(void) > { > struct pidfh *pfh; > pid_t mountdpid; > > mountdpid = 0; > pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid); > if (pfh != NULL) { > /* Mountd is not running. */ > pidfile_remove(pfh); > return; > } > > pidfile_open(3) *creates* /var/run/mountd.pid if it doesn't already > exist, hence the need to delete it if the call actually succeeds. > This leads to a race condition when multiple mounts occur at the same > time. That case is handled later in the code: > > /* > * Refuse to send broadcast or group signals, this has > * happened due to the bugs in pidfile(3). > */ > if (mountdpid <= 0) { > warnx("mountd pid %d, refusing to send SIGHUP", mountdpid); > return; > } > > "mount" is not "mountd." It seems inappropriate for it to, under any > circumstances, create mountd's pid file. The multiple workarounds for > the problems that causes don't seem like the optimal approach. > > This is something I'd be willing to open a bug and submit a patch for, > but so as not to do work that stands no chance of being accepted, I'd > like to understand first if the preferred approach would be to change > mount.c not to use pidfile library calls at all, or if it would be > better to add a function to the pidfile library similar to > pidfile_open() designed for "consumer" use that would never create the > file, leaving pidfile_open() for "producer" use? I think that a new libutil/pidfile.c function, to open only existing pid file, is the right approach. It is possible that both pidfile_open() and the new function would share some significant amount of code.