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
){
36 aPending
= malloc( sizeof(aPending
[0]) );
38 fprintf(stderr
, "out of memory\n");
41 aPending
[0].lwr
= lwr
;
42 aPending
[0].upr
= upr
;
44 for(nDone
=0; nDone
<nPending
; nDone
++){
45 lwr
= aPending
[nDone
].lwr
;
46 upr
= aPending
[nDone
].upr
;
47 if( lwr
>=upr
) continue;
49 x
.l_whence
= SEEK_SET
;
52 fcntl(fd
, F_GETLK
, &x
);
53 if( x
.l_type
==F_UNLCK
) continue;
54 printf("start: %-12d len: %-5d pid: %-5d type: %s\n",
55 (int)x
.l_start
, (int)x
.l_len
,
56 x
.l_pid
, x
.l_type
==F_WRLCK
? "WRLCK" : "RDLCK");
58 if( nPending
+2 > nAlloc
){
59 nAlloc
= nAlloc
*2 + 2;
60 aPending
= realloc(aPending
, sizeof(aPending
[0])*nAlloc
);
63 fprintf(stderr
, "unable to realloc for %d bytes\n",
64 (int)sizeof(aPending
[0])*(nPending
+2));
68 aPending
[nPending
].lwr
= lwr
;
69 aPending
[nPending
].upr
= x
.l_start
;
72 if( x
.l_start
+x
.l_len
<=upr
){
73 aPending
[nPending
].lwr
= x
.l_start
+ x
.l_len
;
74 aPending
[nPending
].upr
= upr
;
82 int main(int argc
, char **argv
){
87 fprintf(stderr
, "Usage: %s FILENAME\n", argv
[0]);
90 fd
= open(argv
[1], O_RDWR
, 0);
92 fprintf(stderr
, "%s: cannot open %s\n", argv
[0], argv
[1]);
95 cnt
= showLocksInRange(fd
, 0, MX_LCK
);
96 if( cnt
==0 ) printf("no locks\n");