I don't know why this code is not working under NFS4, using NFS3 works perfectly. The idea is to avoid the file being written while a process is still reading it.
I would like to debug, but our sys-admin is not able to. Which could be reason. Under our NFS4-Installation I always get into this condition
if ( flock(fp,LOCK_EX) == -1)
printf("Error: file %s is already locked\n", fileName);
the whole program is :
#include <sys/file.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv){
if (argc<2){
printf("Usage:\n a.out fileName\n");
return 1;
}
char *fileName=argv[1];
int fp;
/* block the file, I know a process can write
rendering my information useless*/
fp=open(fileName,O_RDONLY);
if ( flock(fp,LOCK_EX) == -1){
printf("Error: file %s is already locked\n", fileName);
}
else{
printf("OK: file %s was locked\n",fileName );
}
/* read and parse the fileName
another process should not be able to write or
modify the fileName while I am reading it
*/
return 0;
}
Edit:
I would like to clarify. This is an fragment of the code I am using. fileName is supposed to be a valid existing file
I am reading a fileName and making a copy, editing some few parts. I know, while I am doing this, an external process can update the fileName. I would like to use a semaphore to avoid modifications on this file until I am done with it. This program was working perfectly until stop doing so. The only difference, is the file system where the fileNames are located. It was updated form NFS3 to NFS4. Even the OS (SLE15.2) is the same with kernel 5.3.18, and using strerror(errno) produces a seg fault on NFS4. The only hint when I do print("%d",error) is 9 which should be "wrong file descriptor"
Thanks for your help
Julia