From owner-freebsd-stable Sat Jun 22 19:48:19 2002 Delivered-To: freebsd-stable@freebsd.org Received: from hardtime.linuxman.net (hardtime.linuxman.net [66.147.26.65]) by hub.freebsd.org (Postfix) with ESMTP id 7875B37B400 for ; Sat, 22 Jun 2002 19:48:14 -0700 (PDT) Received: from mortis.over-yonder.net (localhost [127.0.0.1]) by hardtime.linuxman.net (8.11.6/8.11.6) with ESMTP id g5N1qoB10513; Sat, 22 Jun 2002 20:52:50 -0500 Received: by mortis.over-yonder.net (Postfix, from userid 100) id 732F01F02; Sat, 22 Jun 2002 21:48:04 -0500 (CDT) Date: Sat, 22 Jun 2002 21:48:04 -0500 From: "Matthew D. Fuller" To: "Geoffrey C. Speicher" Cc: freebsd-stable@freebsd.org, Matt Simerson , Paul Herman Subject: Re: bug in pw, -STABLE [patch] Message-ID: <20020623024804.GB95458@over-yonder.net> References: <20020622071722.GA57065@over-yonder.net> <20020622180011.V80651-100000@sea-incorporated.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20020622180011.V80651-100000@sea-incorporated.com> User-Agent: Mutt/1.4i X-Editor: vi X-OS: FreeBSD Sender: owner-freebsd-stable@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG On Sat, Jun 22, 2002 at 08:17:54PM -0400 I heard the voice of Geoffrey C. Speicher, and lo! it spake thus: > > I think that B1 is already taken care of by init(8) and other > solutions. That leaves A1 and A2 (or B2 if you prefer), and what > I'm thinking is that it should probably just be implemented as a > library whose API consists of two functions that look something > like this: Hrm. I looked at this, and there was something that I couldn't make work (mentally, that is) with library functions that I could with a daemon, but I've totally lost it. OK, let's see... > SYNOPSIS > int > pid_begin(const char* path, int flags=0); > > int > pid_end(const char* path); Good, good... libutil, you think? > pid_begin() will check for existence of the pid file named > _path_, and if it exists, determine whether the process whose > pid is contained therein is still running. > > If the file does not exist, or the contained pid is no > longer valid, then pid_begin() will create the file, write > the current pid, and return 0 (success). Now, this is a problem. There's a race condition here. It's a very small window, to be sure, but I'm not quite sure how to close it. After all, neither creating, nor reading (and then checking PID) are atomic. Perhaps using flock() to lock accesses to the file, and requiring that you hold the lock on it to remove it? I'll have to fiddle a bit to see if there's any edge cases on that... We might also want to put in logic of the type "If PID file is less than X old (5 seconds? 1 minute?), don't bother testing PID" as an optimization as well as as a bit of insurance against the common cases where the race would come up. > What do you think about that? Unless I can remember why I dismissed library functions in the first place, it looks like the way to go; at least, once we address the race possibilities. -- Matthew Fuller (MF4839) | fullermd@over-yonder.net Systems/Network Administrator | http://www.over-yonder.net/~fullermd/ "The only reason I'm burning my candle at both ends, is because I haven't figured out how to light the middle yet" To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-stable" in the body of the message