Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 20 Apr 1999 19:24:22 +0200
From:      Graham Wheeler <gram@cdsec.com>
To:        hackers@freebsd.org
Subject:   File locking problem
Message-ID:  <371CB846.FD0F4422@cdsec.com>

next in thread | raw e-mail | index | archive | help
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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?371CB846.FD0F4422>