2 ** This file implements a simple command-line utility that shows all of the
3 ** Posix Advisory Locks on a file.
9 ** To compile: gcc -o showlocks showlocks.c
17 /* This utility only looks for locks in the first 2 billion bytes */
18 #define MX_LCK 2147483647
21 ** Print all locks on the inode of "fd" that occur in between
22 ** lwr and upr, inclusive.
24 static int showLocksInRange(int fd
, off_t lwr
, off_t upr
){
29 x
.l_whence
= SEEK_SET
;
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");
39 cnt
+= showLocksInRange(fd
, lwr
, x
.l_start
-1);
41 if( x
.l_start
+x
.l_len
<upr
){
42 cnt
+= showLocksInRange(fd
, x
.l_start
+x
.l_len
+1, upr
);
47 int main(int argc
, char **argv
){
52 fprintf(stderr
, "Usage: %s FILENAME\n", argv
[0]);
55 fd
= open(argv
[1], O_RDWR
, 0);
57 fprintf(stderr
, "%s: cannot open %s\n", argv
[0], argv
[1]);
60 cnt
= showLocksInRange(fd
, 0, MX_LCK
);
61 if( cnt
==0 ) printf("no locks\n");