On Linux I can dd
a file on my hard drive and delete it in Nautilus while the dd is still going on.
Can Linux enforce a mandatory file lock to protect R/W?
On Linux I can dd
a file on my hard drive and delete it in Nautilus while the dd is still going on.
Can Linux enforce a mandatory file lock to protect R/W?
You don't need locking. This is not a bug but a choice, your assumptions are wrong.
The file system uses reference counting and it will mark a file as free when all hard links to the file are removed and all file descriptors are closed.
That approach allows safe operations that Windows, for example, doesn't, as delete, move and rename files in use without needing locking or breaking anything.
Your dd operation is going to succeed despite the file removal.
http://en.wikipedia.org/wiki/Reference_counting#Disk_operating_systems
Linux and Unix OS's can enforce file locks, but it does not do so by default becuase of its multiuser design. Try reading the manual pages for
flock
andfcntl
. That might get you started.To do mandatory locking on Linux, the filesystem must be mounted with the
-o mand
option, and you must setg-x,g+s
permissions on the file. That is, you must disable group execute, and enable setgid. Once this is performed, all access will either block or error withEAGAIN
based on the value ofO_NONBLOCK
on the file descriptor. But beware: "The implementation of mandatory locking in all known versions of Linux is subject to race conditions which render it unreliable... It is therefore inadvisable to rely on mandatory locking." Seefcntl(2)
.