Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 23 May 2007 09:30:10 +0200
From:      =?utf-8?Q?Dag-Erling_Sm=C3=B8rgrav?= <des@des.no>
To:        Joe Marcus Clarke <marcus@FreeBSD.org>
Cc:        pjd@FreeBSD.org, "current@freebsd.org" <current@FreeBSD.org>
Subject:   Re: [Fwd: Serious problem with mount(8)]
Message-ID:  <867ir0atfh.fsf@dwp.des.no>
In-Reply-To: <46534EE2.3090107@FreeBSD.org> (Joe Marcus Clarke's message of "Tue\, 22 May 2007 16\:13\:22 -0400")
References:  <46534301.10005@FreeBSD.org> <864pm4zkre.fsf@dwp.des.no> <46534EE2.3090107@FreeBSD.org>

next in thread | previous in thread | raw e-mail | index | archive | help
Joe Marcus Clarke <marcus@FreeBSD.org> writes:
> Dag-Erling Sm=C3=B8rgrav <des@des.no> writes:
> > Better yet, extend the pidfile API with a function which reads the
> > contents of a PID file and also checks whether it's locked.
> I'd be happy to do this.  Is my approach with this code sound (i.e. can
> I simply port this to pidfile(3)), or should I take another approach?
> Thanks.

You expose yourself to all sorts of race conditions by opening the file
twice...  what you should do is something like (off the top of my head):

        fd =3D open(pidfile, O_RDONLY);
        fcntl(fd, F_GETLK, &fl)
        if (fl.l_type =3D=3D F_UNLCK)
                return (-1)
        fstat(fd, &fsb);
        read(fd, buf, fsb.st_len);
        if (atoi(buf) !=3D fl.l_pid)
                return (-1);
#ifdef OPTIONAL
        stat(pidfile, &sb);
        if (sb.st_dev !=3D fsb.st_dev || sb.st_ino !=3D fsb.st_ino)
                return (-1);
#endif
        return (fl.l_pid);

with appropriate error checking, of course.

DES
--=20
Dag-Erling Sm=C3=B8rgrav - des@des.no



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