From owner-freebsd-current@FreeBSD.ORG Wed May 23 07:30:15 2007 Return-Path: X-Original-To: current@FreeBSD.org Delivered-To: freebsd-current@FreeBSD.ORG Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 5F62916A400; Wed, 23 May 2007 07:30:15 +0000 (UTC) (envelope-from des@des.no) Received: from tim.des.no (tim.des.no [194.63.250.121]) by mx1.freebsd.org (Postfix) with ESMTP id 1DB1913C465; Wed, 23 May 2007 07:30:15 +0000 (UTC) (envelope-from des@des.no) Received: from tim.des.no (localhost [127.0.0.1]) by spam.des.no (Postfix) with ESMTP id 4A2262084; Wed, 23 May 2007 09:30:11 +0200 (CEST) X-Spam-Tests: AWL X-Spam-Learn: disabled X-Spam-Score: 0.0/3.0 X-Spam-Checker-Version: SpamAssassin 3.1.7 (2006-10-05) on tim.des.no Received: from dwp.des.no (des.no [80.203.243.180]) by smtp.des.no (Postfix) with ESMTP id C0A3B2083; Wed, 23 May 2007 09:30:10 +0200 (CEST) Received: by dwp.des.no (Postfix, from userid 1001) id A7D3857E3; Wed, 23 May 2007 09:30:10 +0200 (CEST) From: =?utf-8?Q?Dag-Erling_Sm=C3=B8rgrav?= To: Joe Marcus Clarke References: <46534301.10005@FreeBSD.org> <864pm4zkre.fsf@dwp.des.no> <46534EE2.3090107@FreeBSD.org> Date: Wed, 23 May 2007 09:30:10 +0200 In-Reply-To: <46534EE2.3090107@FreeBSD.org> (Joe Marcus Clarke's message of "Tue\, 22 May 2007 16\:13\:22 -0400") Message-ID: <867ir0atfh.fsf@dwp.des.no> User-Agent: Gnus/5.110006 (No Gnus v0.6) Emacs/21.3 (berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Cc: pjd@FreeBSD.org, "current@freebsd.org" Subject: Re: [Fwd: Serious problem with mount(8)] X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 23 May 2007 07:30:15 -0000 Joe Marcus Clarke writes: > Dag-Erling Sm=C3=B8rgrav 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