itCbitC:
Code:
int main() {
char *fn = "/tmp/test.txt"; //the test file
FILE *F = fopen(fn,"w"); //this simulates another process writing to a file but not closing it yet
//We now attempt to detect that the file is still open
int fd = open(fn,O_RDONLY);
struct flock fl;
fl.l_type = F_WRLCK; /* F_RDLCK, F_WRLCK, F_UNLCK */
fl.l_whence = SEEK_SET; /* SEEK_SET, SEEK_CUR, SEEK_END */
fl.l_start = 0; /* Offset from l_whence */
fl.l_len = 0; /* length, 0 = to EOF */
fl.l_pid = getpid();
printf("%d %d %d %d %d\n",fl.l_type,fl.l_whence,fl.l_start,fl.l_len,fl.l_pid);
//1 0 0 0 19865
int ret = fcntl(fd,F_GETLK,&fl);
printf("%d %d %d %d %d\n",fl.l_type,fl.l_whence,fl.l_start,fl.l_len,fl.l_pid);
//2 0 0 0 19865
sleep(3); //this gives me time to manually run fuser/lsof to observe that the file is indeed open
//finalize
fclose(F);
return 0;
}
According to the fcntl documentation, if the lock is not detected, it will keep the struct the same but replace the mode with UNLCK, which is precisely what it does here.
Zlatko, are you referring to this?
fuser cannot report on any processes that it doesn’t have permission to
look at the file descriptor table for. The most common time this prob-
lem occurs is when looking for TCP or UDP sockets when running fuser as
a non-root user. In this case fuser will report no access
Because "man 2 fuser" doesn't contain anything: "No entry for fuser in section 2 of the manual"
Regardless, when I try to run it without root, I get the following:
$ fuser
bash: fuser: command not found
$