Skip to contentSkip to conversation

Problem using mandatory locks (other apps can read/delete etc)

DISCUSSION E. Abbink 1 participant 1 message Apr 12, 2002

lore
Share & tools
lore

E. Abbink

author
#0 lore raw
Quoted 12 lines · Denis Vlasenko
Quoted 9 lines · E. Abbink
Quoted 8 lines

I'm trying to solve a problem using mandatory locks but am having some difficulty in doing so. (if there's a more appropriate place for discussing this please ignore the rest of this post. pointers to that place would be appreciated ;) )

my problem:

when I lock a file with a mandatory write lock (ie. fcntl, +s-x

bits and

Quoted 13 lines
Quoted 13 lines
Quoted 4 lines

mand mount option. for code see below) it is still possible:

- for me to rm the file in question
- for the file to be read by an other process

[snip]

Quoted 6 lines
    lock.l_type = F_WRLCK ;   <================
    lock.l_whence = SEEK_SET ;
    lock.l_start = 0 ;
    lock.l_len = 0 ;
    lock.l_pid = 0 ; // ignored
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
    int err = fcntl (fd, F_SETLK, &lock) ;

I know nothing about file locking in Unix, but it looks like you requested write lock, i.e. forbid writing to a file. Why are you surprised that reads are allowed?

That's advisory write lock. Question is: why open call to read the file succeeded?

Normally it's an advisory lock, but if you do the appropriate actions (mounting the fs with mand and setting the +s-x modebits, see Documentation/mandatory.txt) the file becomes a mandatory lock candidate and fcntl will/should mandatory lock it.

Quoted 9 lines
Quoted 3 lines

Probably someone else would comment on why rm is working, though...

Why not? Apparently he has write permission on the directory which contain the file, and is the owner of that file.

By the way, where are you changing file permissions to +s-x?

by hand before the program is run.

Quoted 3 lines
Signature · 2 lines
NeoMail - Webmail that doesn't suck... as much.
http://neomail.sourceforge.net