Opened 6 years ago

Last modified 6 years ago

#13795 closed bug

flock may be used multiple times from the same process — at Version 1

Reported by: pulkomandy Owned by: nobody
Priority: normal Milestone: Unscheduled
Component: System/libroot.so Version: R1/Development
Keywords: Cc:
Blocked By: Blocking:
Platform: All

Description (last modified by pulkomandy)

This is supposed to work (ie. the second lock should fail because the first one is held):

#include <fcntl.h>
#include <sys/file.h>
#include <sys/stat.h>

int main(void)
{
        int fd = open("flock", O_TRUNC | O_CREAT | O_WRONLY, 0666);
        int fd2 = open("flock", O_TRUNC | O_CREAT | O_WRONLY, 0666);

        if (flock(fd, LOCK_EX | LOCK_NB) != 0) {
                puts("Failed to grab first lock");
                return -1;
        }

        if (flock(fd2, LOCK_EX | LOCK_NB) == 0) {
                puts("Managed to grab second lock");
                return -1;
        }

        return 0;
}

Basically, flock is not owned by the team, but by the file descriptor. This also explains the behavior with regard to fork().

We are currently using a session_id (pid_t) as an identifier for locks. Could we switch to using the file descriptor number instead, or would that break something? (this would mean the process should keep the fd opened until the lock is released, and should release using the same fd used for locking).

Change History (1)

comment:1 by pulkomandy, 6 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.