Created
September 2, 2011 12:09
-
-
Save jiphex/1188457 to your computer and use it in GitHub Desktop.
Lock Testing Program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// JH Lock Test - 20110902 | |
// I've used this in the past to debug locking issues (NFS) | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
char * filename; | |
int main(int argc, char* argv[]) { | |
int lockrc; | |
int fo; | |
struct flock t_exlock,t_cklock; | |
t_exlock.l_type = F_WRLCK; | |
t_exlock.l_start = 0; | |
t_exlock.l_len = 0; | |
t_exlock.l_whence = SEEK_END; | |
t_cklock.l_type = F_WRLCK; | |
t_cklock.l_start = 0; | |
t_cklock.l_len = 0; | |
t_cklock.l_whence = SEEK_END; | |
filename = argv[1]; | |
printf("lock tester - JH-20110902\n"); | |
if(argc < 2) { | |
fprintf(stderr, "Usage: %s FILENAME\n", argv[0]); | |
exit(22); | |
} | |
fo = open(filename, O_RDWR); | |
printf("Getting lock on [%s] type [%d]...\n", filename, t_cklock.l_type); | |
if(fcntl(fo,F_SETLK,&t_exlock) < 0) { // Try to grab exclusive lock first, if this fails then check the lock. | |
printf("Wasn't able to get an exclusive lock, testing what lock exists...\n"); | |
lockrc = fcntl(fo, F_GETLK, &t_cklock); | |
if(lockrc == 0) { | |
if(t_cklock.l_type == F_UNLCK) { | |
printf("File [%s] is not locked. If this is NFS, is nfslock running?\n", filename); | |
} else { | |
printf("Existing Lock details for %s.\n", filename); | |
printf("\tLock PID: %d\n", t_cklock.l_pid); | |
printf("\tLock Type: %d\n", t_cklock.l_type); | |
} | |
} else { | |
perror("Error testing for lock. Did the file exist?"); | |
} | |
} else { | |
printf("Managed to get exlock on [%s] succesfully. No other lock exists.\n",filename); | |
} | |
printf("fcntl() finished...\n"); | |
close(fo); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment