From owner-freebsd-hackers Wed Apr 21 6:54:43 1999 Delivered-To: freebsd-hackers@freebsd.org Received: from citadel.cdsec.com (citadel.cdsec.com [192.96.22.18]) by hub.freebsd.org (Postfix) with ESMTP id 4A81015388 for ; Wed, 21 Apr 1999 06:54:31 -0700 (PDT) (envelope-from gram@cdsec.com) Received: (from nobody@localhost) by citadel.cdsec.com (8.9.3/8.9.3) id PAA29679; Wed, 21 Apr 1999 15:51:42 +0200 (SAST) Received: by citadel via recvmail id 29677; Wed Apr 21 15:50:53 1999 Message-ID: <371DD7DC.4B5356E@cdsec.com> Date: Wed, 21 Apr 1999 15:51:24 +0200 From: Graham Wheeler X-Mailer: Mozilla 4.08 [en] (X11; I; FreeBSD 2.2.8-RELEASE i386) MIME-Version: 1.0 To: Ladavac Marino , hackers@freebsd.org Subject: Re: File locking problem References: <55586E7391ACD211B9730000C11002761795EF@r-lmh-wi-100.corpnet.at> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Ladavac Marino wrote: > [ML] Please disregard my second "solution". It won't work (the > second open will create the file if you specify O_CREAT). Talking about > that, the real solution might be to open the file read-write without > O_CREAT flag--the open will fail if the file was unlinked between dir > read and open. That is the solution. I have a routine: FILE *OpenExclusive(const char *path, const char *mode); This routine maps normal fopen() style modes to open() style flags, does the open() with locking, and then an fdopen(). The code that uses it was quite correctly passing "r+" through as the mode, but the OpenExclusive routine was incorrectly setting the O_CREAT flag in this case. After checking Stephens' APitUE fig 5.3 I realised that this was wrong. Removing the O_CREAT solved the problem. I've been using the OpenExclusive routine for a long time, and have never had problems with it before (probably because this is the first time I've used it with the "r+" mode, and also because the problem actually occurs so intermittently and I couldn't reliably replicate it), so I wasted a couple of days poring over the (fairly complex) code of the process that uses it, and couldn't find anything wrong (so I posted the problem). I figured I was misunderstanding file locking in the context of multiple processes. I'm glad that that wasn't the problem, or I would have had to implement my locks using UUCP style `semafiles'. Thanks to everyone for their suggestions. -- Dr Graham Wheeler E-mail: gram@cdsec.com Citadel Data Security Phone: +27(21)423-6065/6/7 Firewalls/Virtual Private Networks Fax: +27(21)24-3656 Internet/Intranet Network Specialists Data Security Products WWW: http://www.cdsec.com/ To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message