Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 19 Oct 2009 01:09:35 +0300
From:      Kostik Belousov <kostikbel@gmail.com>
To:        Warren Block <wblock@wonkity.com>
Cc:        current@freebsd.org
Subject:   Re: /etc/rc.d locking devd.pid (was Re: Restarting devd)
Message-ID:  <20091018220935.GR2160@deviant.kiev.zoral.com.ua>
In-Reply-To: <alpine.BSF.2.00.0910181542440.94243@wonkity.com>
References:  <alpine.BSF.2.00.0910171503010.89326@wonkity.com> <alpine.BSF.2.00.0910181542440.94243@wonkity.com>

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

--Thv7PGoFpDPJ7Oar
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Sun, Oct 18, 2009 at 03:54:52PM -0600, Warren Block wrote:
> On Sat, 17 Oct 2009, Warren Block wrote:
>=20
> >Immediately after boot, devd restart fails:
> >
> ># /etc/rc.d/devd restart
> >Stopping devd.
> >Starting devd.
> >devd: devd already running, pid: 398
> >/etc/rc.d/devd: WARNING: failed to start devd
> >
> >And it's right, devd is not running.  Remove the stale pidfile=20
> >/var/run/devd.pid, and '/etc/rc.d/devd start' goes fine.
>=20
> ...and this is due to dhclient, run from /etc/rc.d at startup, locking=20
> /var/run/devd.pid:
>=20
> lightning% lsof /var/run/devd.pid
> COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF  NODE NAME
> devd      400  root    6w  VREG  0,101        3 47124 /var/run/devd.pid
> dhclient  865  root    6w  VREG  0,101        3 47124 /var/run/devd.pid
> dhclient 1024 _dhcp    6w  VREG  0,101        3 47124 /var/run/devd.pid
>=20
> This is a regression from 7-STABLE, where devd.pid is only locked by=20
> devd after startup.

Devd forks to spawn dhclient, it seems, and opened file descriptor for
the lock file is leaking to the child. Since pidfile(3) uses flock(2),
the lock survives devd death.

I think that this is a generic issue with pidfile/fork interaction.
It is not obvious whether setting FD_CLOEXEC flag is right thing to
do there.

Anyway, please test.

diff --git a/lib/libutil/flopen.c b/lib/libutil/flopen.c
index 754c9c0..a5e436e 100644
--- a/lib/libutil/flopen.c
+++ b/lib/libutil/flopen.c
@@ -100,6 +100,12 @@ flopen(const char *path, int flags, ...)
 			errno =3D serrno;
 			return (-1);
 		}
+		if (fcntl(fd, F_SETFD, FD_CLOEXEC) !=3D 0) {
+			serrno =3D errno;
+			(void)close(fd);
+			errno =3D serrno;
+			return (-1);
+		}
 		return (fd);
 	}
 }

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

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

iEYEARECAAYFAkrbkh4ACgkQC3+MBN1Mb4i5rACfT+cKFg6oon1w3jDyWWuGQs8E
INkAnRp1DMbXwZ/UQu6SFl9xbeAYhJa3
=VUQP
-----END PGP SIGNATURE-----

--Thv7PGoFpDPJ7Oar--



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