Lock file - Unix

This is a discussion on Lock file - Unix ; Hello, I'm trying to lock an file that i'm opening in a c program, but theres no effect in the shell. Example, what i'm doing in the c program is this: int nLockFile=open("/tmp/fileToLock",O_RDWR|O_CREAT,S_IRWXU); flock fLk; fLk.l_type = F_WRLCK|F_RDLCK; fLk.l_whence = ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: Lock file

  1. Lock file

    Hello,

    I'm trying to lock an file that i'm opening in a c program, but theres
    no effect in the shell. Example, what i'm doing in the c program is
    this:
    int nLockFile=open("/tmp/fileToLock",O_RDWR|O_CREAT,S_IRWXU);
    flock fLk;
    fLk.l_type = F_WRLCK|F_RDLCK;
    fLk.l_whence = SEEK_SET;
    fLk.l_start = 0;
    fLk.l_len = 0;
    fcntl(nLockFile, F_SETLK, &fLk);

    With this i think that the file is locked for writing and reading...

    but when i'm testing this in the shell this not works
    cat /tmp/fileToLock <- this works
    like this works too
    echo "teste" >> /tmp/fileToLock
    and this works too
    rm /tmp/fileToLock

    i want to disable all of this three operations on this file... how
    this is done, what i'm doing wrong?

    Nuno


  2. Re: Lock file

    Nuno wrote:
    > I'm trying to lock an file that i'm opening in a c program, but theres
    > no effect in the shell. Example, what i'm doing in the c program is
    > this:
    > int nLockFile=open("/tmp/fileToLock",O_RDWR|O_CREAT,S_IRWXU);
    > flock fLk;
    > fLk.l_type = F_WRLCK|F_RDLCK;
    > fLk.l_whence = SEEK_SET;
    > fLk.l_start = 0;
    > fLk.l_len = 0;
    > fcntl(nLockFile, F_SETLK, &fLk);
    >
    > With this i think that the file is locked for writing and reading...


    It is, but it's an advisory lock, not a mandatory lock.

    That is, others are not forbidden from accessing the file. They
    are just forbidden from getting a lock on the same region.

    - Logan

  3. Re: Lock file

    On 11 Apr., 03:40, Logan Shaw wrote:
    > Nuno wrote:
    > > I'm trying to lock an file that i'm opening in a c program, but theres
    > > no effect in the shell. Example, what i'm doing in the c program is
    > > this:
    > > int nLockFile=open("/tmp/fileToLock",O_RDWR|O_CREAT,S_IRWXU);
    > > flock fLk;
    > > fLk.l_type = F_WRLCK|F_RDLCK;
    > > fLk.l_whence = SEEK_SET;
    > > fLk.l_start = 0;
    > > fLk.l_len = 0;
    > > fcntl(nLockFile, F_SETLK, &fLk);

    >
    > > With this i think that the file is locked for writing and reading...

    >
    > It is, but it's an advisory lock, not a mandatory lock.
    >
    > That is, others are not forbidden from accessing the file. They
    > are just forbidden from getting a lock on the same region.
    >
    > - Logan


    On some systems you can define mandatory locking by setting the SGID
    bit of the access list, but leaving out the write access, for example:

    chmod 2664 /tmp/lockfile (# -rw-r-Sr--)

    On (some) Linux (systems), you have to mount with -o mand to enable
    mandatory locking.

    Hubble.



  4. Re: Lock file

    Nuno wrote:
    > Hello,


    > I'm trying to lock an file that i'm opening in a c program, but theres
    > no effect in the shell. Example, what i'm doing in the c program is
    > this:
    > int nLockFile=open("/tmp/fileToLock",O_RDWR|O_CREAT,S_IRWXU);
    > flock fLk;
    > fLk.l_type = F_WRLCK|F_RDLCK;
    > fLk.l_whence = SEEK_SET;
    > fLk.l_start = 0;
    > fLk.l_len = 0;
    > fcntl(nLockFile, F_SETLK, &fLk);


    > With this i think that the file is locked for writing and reading...


    > but when i'm testing this in the shell this not works
    > cat /tmp/fileToLock <- this works
    > like this works too
    > echo "teste" >> /tmp/fileToLock
    > and this works too
    > rm /tmp/fileToLock


    > i want to disable all of this three operations on this file... how
    > this is done, what i'm doing wrong?


    Because in Unix you typically only get advisory locking. And for advisory
    locking to work each process must know that it must explicitly attempt to
    lock the resource, but the shell never attempts to lock anything since its
    simply obeying the commands you give, and ostensibly you mean what you say.

    The real question should be, why do you want mandatory locking (a la
    Windows)? Advisory locking is usually all you need. More often than not (and
    arguably always), if you feel you need mandatory locking, then your design
    is wrong. An application is more than just a set primitives, but a set of
    contracts obeyed by cooperating processes regarding the semantics of
    operation. If a process doesn't know how to cooperate, mandatory locking
    will just confuse things. This is very often why you end up w/ frozen
    applications in Windows; a programmer attempting to impose a behavior on
    some other unsuspecting process, rather than addressing the real issue,
    which is why some unsuspecting process is involved at all.

    In Unix, rather than mandatory locking, one commen method is to simply
    employ file read/write/execute permissions. You structure your permissions
    such that only applications which are supposed to know about and obey the
    contracts can actually access the resources. _How_ they access the resources
    (whether they lock properly, or whether they scribble random data all over
    the place; is there a WIN32 API to "don't allow bad behavior"?) is in
    another domain.

    For instance, on my server I have a _postgres user, a _mailman user, a _news
    user, etc. These are all users dedicated to particular sub-systems. All
    processes granted the ability to operate under those user IDs all know how
    to play nice together, and sometimes know how to break the rules in case one
    of the players has a bug.


+ Reply to Thread