From owner-freebsd-current@FreeBSD.ORG Thu Oct 13 12:51:30 2011 Return-Path: Delivered-To: current@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id DA67F10656D3 for ; Thu, 13 Oct 2011 12:51:30 +0000 (UTC) (envelope-from unixmania@gmail.com) Received: from mail-iy0-f182.google.com (mail-iy0-f182.google.com [209.85.210.182]) by mx1.freebsd.org (Postfix) with ESMTP id A7DAB8FC0A for ; Thu, 13 Oct 2011 12:51:30 +0000 (UTC) Received: by iaky10 with SMTP id y10so1654029iak.13 for ; Thu, 13 Oct 2011 05:51:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=bvBBsPWfXhCibUXrVMWg3wHmVqCJBSZGH7YeU+5GJ1U=; b=uljopDg+MrlKXI5abJ/a8GJR1fkodM98TO+67thSNGDnWjhdjCovx0otArMy5DT/83 JY+ydvGvRKW1Mmbb+FlN7yd5v+JqBBfQMl0GT+N9HtCXNMUUWVOQgMmkCo5tm7IZs+9m OMfLsHdacLDLeYf7ryd8iPPwkz3mrthn0NK+4= MIME-Version: 1.0 Received: by 10.231.83.19 with SMTP id d19mr1645438ibl.7.1318508471553; Thu, 13 Oct 2011 05:21:11 -0700 (PDT) Received: by 10.231.23.66 with HTTP; Thu, 13 Oct 2011 05:21:11 -0700 (PDT) In-Reply-To: <86botlaz41.fsf@ds4.des.no> References: <86pqi1b1qp.fsf@ds4.des.no> <20111013113024.GE1667@garage.freebsd.pl> <86botlaz41.fsf@ds4.des.no> Date: Thu, 13 Oct 2011 09:21:11 -0300 Message-ID: From: "Carlos A. M. dos Santos" To: =?ISO-8859-1?Q?Dag=2DErling_Sm=F8rgrav?= Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: Pawel Jakub Dawidek , current@freebsd.org Subject: Re: incorrect use of pidfile(3) 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: Thu, 13 Oct 2011 12:51:31 -0000 2011/10/13 Dag-Erling Sm=C3=B8rgrav : > Pawel Jakub Dawidek writes: >> Dag-Erling Sm=C3=B8rgrav writes: >> > How do we fix this? =C2=A0My suggestion is to loop until pidfile_open(= ) >> > succeeds or errno !=3D EAGAIN. =C2=A0Does anyone have any objections t= o that >> > approach? >> I think we already do that internally in pidfile_open(). Can you take a = look at >> the source and confirm that this is what you mean? > > No, it doesn't; pidfile_open(3) returns NULL with errno =3D=3D EAGAIN if = the > pidfile is locked but empty, as is the case in the window between a > successful pidfile_open(3) and the first pidfile_write(3). =C2=A0This is > documented in the man page: > > =C2=A0 =C2=A0 [EAGAIN] =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Some process al= ready holds the lock on the given pid=E2=80=90 > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0file, but the file is truncated. =C2=A0Most likely, the > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0existing daemon is writing new PID into the file. > > I have a patch that adds a pidfile to dhclient(8), where I do this: > > =C2=A0 =C2=A0 =C2=A0 =C2=A0for (;;) { > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0pidfile =3D pidfil= e_open(path_dhclient_pidfile, 0600, &otherpid); > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if (pidfile !=3D N= ULL || errno !=3D EAGAIN) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0break; > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0sleep(1); > =C2=A0 =C2=A0 =C2=A0 =C2=A0} > =C2=A0 =C2=A0 =C2=A0 =C2=A0if (pidfile =3D=3D NULL) { > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if (errno =3D=3D E= EXIST) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0error("dhclient already running, pid: %d.", otherpid); > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0warning("Cannot op= en or create pidfile: %m"); > =C2=A0 =C2=A0 =C2=A0 =C2=A0} > > I'm not sure I agree with the common idiom (which I copied here) of > ignoring all other errors than EEXIST, but that's a different story. You are also ignoring the return value of sleep(1), which would tell you if the call was interrupted by a signal handler. This can be fine for dhclient(8) but other utilities might require some guards against such interruptions. --=20 "The flames are all long gone, but the=EF=BB=BF pain lingers on"