Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 23 Aug 2005 13:26:56 -0700
From:      John-Mark Gurney <gurney_j@resnet.uoregon.edu>
To:        Pawel Jakub Dawidek <pjd@FreeBSD.org>
Cc:        Hajimu UMEMOTO <ume@FreeBSD.org>, FreeBSD-arch <freebsd-arch@FreeBSD.org>
Subject:   Re: New library: libpidfile.
Message-ID:  <20050823202656.GB30465@funkthat.com>
In-Reply-To: <20050823080754.GA47261@garage.freebsd.pl>
References:  <20050822213028.GB4812@garage.freebsd.pl> <ygek6id48nn.wl%ume@mahoroba.org> <20050823080754.GA47261@garage.freebsd.pl>

next in thread | previous in thread | raw e-mail | index | archive | help
Pawel Jakub Dawidek wrote this message on Tue, Aug 23, 2005 at 10:07 +0200:
> On Tue, Aug 23, 2005 at 12:46:52PM +0900, Hajimu UMEMOTO wrote:
> +> Hi,
> +> 
> +> >>>>> On Mon, 22 Aug 2005 23:30:28 +0200
> +> >>>>> Pawel Jakub Dawidek <pjd@FreeBSD.org> said:
> +> 
> +> pjd> I'd like to commit a small library for handling "pidfiles".
> +> 
> +> NetBSD and OpenBSD has similar functions already in libutil.  I think
> +> we alone have a different API is bad idea.  So, it is good to bring
> +> them into FreeBSD from NetBSD or OpenBSD, IMHO.
> 
> I assume you're talking about NetBSD's pidlock(3).
> 
> This is exactly an example of a bad way of doing it, as I understand the
> code.

/me just checked NetBSD and OpenBSD's code at:
http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libutil/pidfile.c?rev=1.7&content-type=text/x-cvsweb-markup
http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libutil/pidfile.c?rev=1.7&content-type=text/x-cvsweb-markup

Neither, of these are safe to prevent multiple daemons from starting
up at the same time...  Both NetBSD and OpenBSD doesn't even check if
a daemon is running.. it just blindly splats the pid into the file..

> It doesn't use flock(2), instead, it reads PID from the file and checks
> if process with this PID is alive. *SOME* process, not necessarily already
> running instance of the daemon, but some process which has the same PID.
> This is most important in case of pkill(1), when we don't want to kill
> some random process.
> 
> This of course is also racy - daamon could be started between checking
> is process is alive and renaming (lock|pid)file.
> 
> It only provides one function which writes the PID of the current process
> into the file. With libpidfile(3), you can open the pidfile before
> fork()ing, so daemon can report if another copy is already running before
> going into the background.
> 
> In general NetBSD's pidlock(3) isn't a complete solution (there is no
> function to remove just remove pidfile on exit, etc.
> 
> In OpenBSD pidfile(3) exists, but it is even worser. It doesn't even check
> if daemon is already running...
> It also doesn't support any pidfile name, you may specify only 'basename'
> and it creates pidfile in a form "/var/run/<basename>.pid", so it won't
> work if most of our daemon where you can specify alternate pidfile location.
> 
> Anyway. There is no one API they share and none of them is a sufficient
> solution.

Just so others know, the method that pjd used was suggested by me
after seeing various issues where a pid file was stale from a previous
boot stopped a daemon from starting because another daemon "got" that
pid...

using a lock has the automatic benifit that if the process dies for any
reason the next time the daemon starts up, since it can lock the file,
it knows that there isn't a daemon running, and it doesn't even care
about the file contents...

Splitting the function into two parts also gives you the ability to
print out an error message saying that another daemon is already
running... if you wait to do the pidfile till you've daemonized, then
you can't send any message to the user unless you have a complicated
pipe system to send back status...

I would like to see us adopt this...  I don't care which library it is
in... It'll make daemon start up more reliable by using these routines..

-- 
  John-Mark Gurney				Voice: +1 415 225 5579

     "All that I will do, has been done, All that I have, has not."



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20050823202656.GB30465>