The "standard" locking snippet I've seen goes something like…
(
flock -n 200 || exit 1;
# do stuff
) 200>program.lock
Is it safe (testing seems to say so) to use exec
at that point? Will the subprocess retain the lock?
(
flock -n 200 || exit 1;
exec /usr/bin/python vendors-notcoolstuff.py
) 200>program.lock
I vaguely remember exec'd processes retain open file descriptors and since flock uses file descriptors it should work. But I cannot find any documentation that makes that definitive and clear.
For the record, this is specific to Linux.
Best Answer
Yes, locks are preserved across
exec
. Locks are preserved across the underlying system callexecve
, as long as the file descriptor remains open. File descriptors remain open acrossexecve
unless they have been configured to be closed on exec, and file descriptors created by shell redirection are not marked as close-on-exec.