From owner-freebsd-hackers Tue Apr 20 10:27: 5 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 C833914ED7 for ; Tue, 20 Apr 1999 10:27:00 -0700 (PDT) (envelope-from gram@cdsec.com) Received: (from nobody@localhost) by citadel.cdsec.com (8.9.3/8.9.3) id TAA18438 for ; Tue, 20 Apr 1999 19:24:28 +0200 (SAST) Received: by citadel via recvmail id 18377; Tue Apr 20 19:23:57 1999 Message-ID: <371CB846.FD0F4422@cdsec.com> Date: Tue, 20 Apr 1999 19:24:22 +0200 From: Graham Wheeler X-Mailer: Mozilla 4.08 [en] (X11; I; FreeBSD 2.2.8-RELEASE i386) MIME-Version: 1.0 To: hackers@freebsd.org Subject: File locking problem Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Hi all I'm having some problems with file locking; perhaps someone can clarify this as the flock man page isn't very illuminating. I have a process which obtains an exclusive lock on a file. It then forks off a child that execs another process, after dup-ing the file descriptor so that the exec'ed process sees the file as standard input. From the behaviour I am seeing, it appears as if when the execed process completes the file gets unlocked, even though the parent still has it open. What exactly happens when a process obtains an exclusive lock on a file and then forks? Does the lock get removed only after both processes have closed the file, or after either one of them closes it? And if the latter, is there a way I can get around this? In my code I actually have three processes: grandparent: locks the file, forks off parent, closes file, and loops around looking for more files to process parent: forks off child and waits to see if it exits with value 0 if so it unlinks the file in either case it closes the file and exits child: dups the file descriptor to be fd 0 and execs another program to handle the input The problem is that when the child exits the grandparent is looping around and relocking the file (which should have been unlinked), and forking off another parent - which then complains because the file is empty (fgets fails and feof is set). So it appears the lock is being removed by the child upon termination, and the grandparent is obtaining another lock before the unlink takes effect. -- 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