Victor86B digital multimeter app
[tridge/junkcode.git] / oplock_irix.c
1 #include <fcntl.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <signal.h>
5 #include <sys/capability.h>
6
7 #define FNAME "test.dat"
8
9 static int fd;
10 static int have_lease;
11 static int got_signal;
12
13 static void sig_usr1(int sig)
14 {
15         got_signal = 1;
16 }
17
18 int main(int argc, char *argv[])
19 {
20         int pfd[2];
21         cap_t cap = cap_get_proc();
22
23         cap->cap_effective |= CAP_NETWORK_MGT;
24         if (cap_set_proc(cap) == -1) failed("cap_set_proc");
25         cap_free(cap);
26
27         if (pipe(pfd) != 0) failed("pipe");
28
29         if ((fd = open(FNAME, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0600)) == -1) failed("open");
30
31         if (fcntl(fd, F_SETSIG, SIGUSR1) == -1) failed("setsig");
32
33         signal(SIGUSR1, sig_usr1);
34
35         while (1) {
36                 sleep(1);
37
38                 if (got_signal) {
39                         if (fcntl(fd, F_OPLKACK, OP_REVOKE) == -1) failed("revoke");
40                         got_signal = 0;
41                         have_lease = 0;
42                 }
43
44                 if (!have_lease && fcntl(fd, F_OPLKREG, pfd[1]) == -1) continue;
45                 have_lease = 1;
46         }
47
48         return 0;
49 }
50