Skip to contentSkip to conversation

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

DISCUSSION E. Abbink 3 participants 3 messages Apr 10, 2002 – Apr 11, 2002

lore
Share & tools
lore

E. Abbink

author
#0 lore raw

Hi,

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 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

It's not possible to cat to the file or cp another file over it (as expected). For my application I want a locked file to be completely locked/protected from other processes.

If I retry the same without setting the +s-x bits the cp & cat succeed, so something special is being done in the first case.

According to all docs (i can find) mandatory locks should block both the read and write system calls (i cant find anything in it regarding unlink though...). In my case however it seems as if only write calls are blocked but not read calls?

If anyone could shed any light on this it would be much appreciated.

Esger

system details:

linux 2.4.17 partially suse & xfs patched kernel
filesystem is reiserfs
i386 dual pIII450

test app code:

int main ()

{
    int fd ;


    fd = open ("image.jpg", O_RDWR) ;

    if (fd == -1)
    {
        printf ("error %d while opening file\n", errno) ;
        exit (1) ;
    }

    struct flock lock ;

    lock.l_type = F_WRLCK ;
    lock.l_whence = SEEK_SET ;
    lock.l_start = 0 ;
    lock.l_len = 0 ;
    lock.l_pid = 0 ; // ignored

    int err = fcntl (fd, F_SETLK, &lock) ;

    if (err == -1)
    {
        printf ("error %d while locking file\n", errno) ;
        exit (1) ;
    }
    else
        printf ("file locked\n") ;


    while (1)
        sleep (1) ;
}
Signature · 2 lines
NeoMail - Webmail that doesn't suck... as much.
http://neomail.sourceforge.net
#1 lore raw
Quoted 12 lines · E. Abbink

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 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 7 lines
    lock.l_type = F_WRLCK ;   <================
    lock.l_whence = SEEK_SET ;
    lock.l_start = 0 ;
    lock.l_len = 0 ;
    lock.l_pid = 0 ; // ignored

    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?

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

#2 lore raw
In reply to E. Abbink
Quoted 22 lines · Denis Vlasenko
Quoted 12 lines · E. Abbink

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 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 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?

Quoted 2 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?