OLD | NEW |
| (Empty) |
1 /* | |
2 ** This file implements a simple command-line utility that shows all of the | |
3 ** Posix Advisory Locks on a file. | |
4 ** | |
5 ** Usage: | |
6 ** | |
7 ** showlocks FILENAME | |
8 ** | |
9 ** To compile: gcc -o showlocks showlocks.c | |
10 */ | |
11 #include <stdio.h> | |
12 #include <unistd.h> | |
13 #include <fcntl.h> | |
14 #include <stdlib.h> | |
15 #include <string.h> | |
16 | |
17 /* This utility only looks for locks in the first 2 billion bytes */ | |
18 #define MX_LCK 2147483647 | |
19 | |
20 /* | |
21 ** Print all locks on the inode of "fd" that occur in between | |
22 ** lwr and upr, inclusive. | |
23 */ | |
24 static int showLocksInRange(int fd, off_t lwr, off_t upr){ | |
25 int cnt = 0; | |
26 struct flock x; | |
27 | |
28 x.l_type = F_WRLCK; | |
29 x.l_whence = SEEK_SET; | |
30 x.l_start = lwr; | |
31 x.l_len = upr-lwr; | |
32 fcntl(fd, F_GETLK, &x); | |
33 if( x.l_type==F_UNLCK ) return 0; | |
34 printf("start: %-12d len: %-5d pid: %-5d type: %s\n", | |
35 (int)x.l_start, (int)x.l_len, | |
36 x.l_pid, x.l_type==F_WRLCK ? "WRLCK" : "RDLCK"); | |
37 cnt++; | |
38 if( x.l_start>lwr ){ | |
39 cnt += showLocksInRange(fd, lwr, x.l_start-1); | |
40 } | |
41 if( x.l_start+x.l_len<upr ){ | |
42 cnt += showLocksInRange(fd, x.l_start+x.l_len+1, upr); | |
43 } | |
44 return cnt; | |
45 } | |
46 | |
47 int main(int argc, char **argv){ | |
48 int fd; | |
49 int cnt; | |
50 | |
51 if( argc!=2 ){ | |
52 fprintf(stderr, "Usage: %s FILENAME\n", argv[0]); | |
53 return 1; | |
54 } | |
55 fd = open(argv[1], O_RDWR, 0); | |
56 if( fd<0 ){ | |
57 fprintf(stderr, "%s: cannot open %s\n", argv[0], argv[1]); | |
58 return 1; | |
59 } | |
60 cnt = showLocksInRange(fd, 0, MX_LCK); | |
61 if( cnt==0 ) printf("no locks\n"); | |
62 close(fd); | |
63 return 0; | |
64 } | |
OLD | NEW |