jeudi 1 septembre 2016

fcntl() give IO when locking file on NFS share

I was trying to lock a file in NFS so did I tried the following,first I've gone with CLI

exec 80>lock.file
echo "testwrite" > &80
flock -s 80 [IO error]

Then I've found flock is not much in case of locking file in NFS so I planned to go lockf and I have written the following code

  void write_lock(string path)
    {
        int fd;
        int error;

        fd = open(path.c_str(), O_CREAT | O_RDWR, 0664);
        if (fd < 0) {
            perror("open");
            exit(EXIT_FAILURE);
        }

        // if len is zero, lock all bytes
        error = lockf(fd, F_LOCK, 0);
        if (error == 0){
            printf("%#x: lock succeeds!\n", getpid());
            cout<<"locked"<<endl;

            }
        else
        {
            perror("lockf");

        }

        sleep(1);
        lockf(fd, F_ULOCK, 0);
        cout<<"unlocked"<<endl;
        close(fd);
  }

nothing wrong with the code it compiles ans lock the file when i ran it locally,but when I run in the mounted path 'a.out "filename"' I end up in same IO error.and I fired tshark to sniff the reply message, It says the below when I did apply lock(shared/exclusive)

NFS 190 V3 ACCESS Reply (Call In 2092), [Access Denied: XE], [Allowed: RD MD XT] //Access Denied for even newly created file already exist one and for every lock it says the same

Can anyone tell me what went wrong I stuck here, if I found where it cause the problem I will write the code in c++11 to get through it..yet I have a hard time with it

Aucun commentaire:

Enregistrer un commentaire