Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 22 Aug 2005 23:30:28 +0200
From:      Pawel Jakub Dawidek <pjd@FreeBSD.org>
To:        FreeBSD-arch <freebsd-arch@FreeBSD.org>
Subject:   New library: libpidfile.
Message-ID:  <20050822213028.GB4812@garage.freebsd.pl>

next in thread | raw e-mail | index | archive | help

--GID0FwUMdk1T2AWN
Content-Type: text/plain; charset=iso-8859-2
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Hi.

I'd like to commit a small library for handling "pidfiles".

It is something that is used by quite every daemon, but most of them don't
do it right.

Most important thing about this library, is that created pidfile is
flock(2)ed, so we can easly detect is daemon is already running.

Another interesting thing is that I've also modified pkill(1)'s '-F' option
to take advantage of libpidfile and only kill process from the given
pidfile if we cannot flock it. This ensures, that we don't kill some random
process when daemon is already dead.

Libpidfile has four functions to offer, but it will be easiest to just show
an example of use (this is from manual page):

	pid_t otherpid, childpid;

	if (pidfile_open("/var/run/daemon.pid", 0644, &otherpid) =3D=3D -1) {
		if (errno =3D=3D EEXIST) {
			errx(EXIT_FAILURE, "Daemon already running, pid: %d.",
			    otherpid);
		}
		/* If we cannot create pidfile from other reasons, only warn. */
		warn("Cannot open or create pidfile");
	}

	if (daemon(0, 0) =3D=3D -1) {
		warn("Cannot daemonize");
		pidfile_remove(1);
		exit(EXIT_FAILURE);
	}

	pidfile_write();

	for (;;) {
		/* Do work. */
		childpid =3D fork();
		switch (childpid) {
		case -1:
			syslog(LOG_ERR, "Cannot fork(): %s.", strerror(errno));
			break;
		case 0:
			pidfile_close();
			/* Do child work. */
			break;
		default:
			syslog(LOG_INFO, "Child %d started.", childpid);
			break;
		}
	}

	pidfile_remove(0); /* Not really needed. */
	exit(EXIT_SUCCESS);

As you can see, it easy to use. Note that return values from functions other
than pidfile_open() can be ignored, as they were implemented to handle
previous pidfile_open() failure.

The work is finished. It was implemented in FreeBSD's perforce in
pjd_pidfile branch.
I also converted few daemons:
	- devd(8),
	- cron(8),
	- syslogd(8),
	- moused(8),
	- powerd(8).
Reimplementation of pkill(1)'s '-F' option is also there.

The source code is availabe here:

	http://perforce.freebsd.org/depotTreeBrowser.cgi?FSPC=3D//depot/user/pjd/p=
idfile/lib/libpidfile&HIDEDEL=3DNO

--=20
Pawel Jakub Dawidek                       http://www.wheel.pl
pjd@FreeBSD.org                           http://www.FreeBSD.org
FreeBSD committer                         Am I Evil? Yes, I Am!

--GID0FwUMdk1T2AWN
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (FreeBSD)

iD8DBQFDCkP0ForvXbEpPzQRAv2zAKCohm/SOykgxcL6sbio5irUEG4HHwCfVBFV
sBKLUPC4qYYJ3WLH6Xb0FKc=
=iCAd
-----END PGP SIGNATURE-----

--GID0FwUMdk1T2AWN--



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