Revision 22525,
1.1 KB
checked in by rbasch, 18 years ago
(diff) |
Open the lock file read-only, since we are not setting a lock.
Also remove the extraneous mode parameter in the open().
|
Line | |
---|
1 | #include <stdio.h> |
---|
2 | #include <sys/types.h> |
---|
3 | #include <sys/stat.h> |
---|
4 | #include <unistd.h> |
---|
5 | #include <fcntl.h> |
---|
6 | #include <string.h> |
---|
7 | #include <errno.h> |
---|
8 | |
---|
9 | int main(int argc, char **argv) |
---|
10 | { |
---|
11 | char *progname, *filename; |
---|
12 | int fd; |
---|
13 | struct flock lock; |
---|
14 | |
---|
15 | progname = argv[0]; |
---|
16 | if (argc != 2) |
---|
17 | { |
---|
18 | fprintf(stderr, "%s: Usage: %s <lock_file>\n", progname, progname); |
---|
19 | return 4; |
---|
20 | } |
---|
21 | filename = argv[1]; |
---|
22 | |
---|
23 | /* Open the file. */ |
---|
24 | fd = open(filename, O_RDONLY); |
---|
25 | if (fd == -1) |
---|
26 | { |
---|
27 | fprintf(stderr, "%s: Cannot open %s: %s\n", progname, filename, |
---|
28 | strerror(errno)); |
---|
29 | return 3; |
---|
30 | } |
---|
31 | |
---|
32 | /* See if there is a write lock on the file. */ |
---|
33 | lock.l_type = F_WRLCK; |
---|
34 | lock.l_whence = SEEK_SET; |
---|
35 | lock.l_start = 0; |
---|
36 | lock.l_len = 0; |
---|
37 | if (fcntl(fd, F_GETLK, &lock) == -1) |
---|
38 | { |
---|
39 | fprintf(stderr, "%s: Cannot get the lock for %s: %s\n", progname, |
---|
40 | filename, strerror(errno)); |
---|
41 | return 3; |
---|
42 | } |
---|
43 | close(fd); |
---|
44 | if (lock.l_type != F_UNLCK) |
---|
45 | { |
---|
46 | /* The file is locked; print the PID of the locking process. */ |
---|
47 | printf("%ld\n", (long) lock.l_pid); |
---|
48 | return 2; |
---|
49 | } |
---|
50 | return 0; |
---|
51 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.