Date: Wed, 21 Apr 1999 15:51:24 +0200 From: Graham Wheeler <gram@cdsec.com> To: Ladavac Marino <mladavac@metropolitan.at>, hackers@freebsd.org Subject: Re: File locking problem Message-ID: <371DD7DC.4B5356E@cdsec.com> References: <55586E7391ACD211B9730000C11002761795EF@r-lmh-wi-100.corpnet.at>
next in thread | previous in thread | raw e-mail | index | archive | help
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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?371DD7DC.4B5356E>