9 #define F_SETLEASE 1024
13 #define F_GETLEASE 1025
16 #ifndef RT_SIGNAL_LEASE
17 #define RT_SIGNAL_LEASE 33
25 static volatile sig_atomic_t fd_pending
;
27 static volatile bool want_to_die
= false;;
29 void sighandler_ignore(int signum
)
31 fprintf(stderr
, "Ignoring signal %d\n", signum
);
34 void sighandler_die(int signum
)
39 static void signal_handler(int sig
, siginfo_t
*info
, void *unused
)
41 //BlockSignals(True, sig);
42 fd_pending
= (sig_atomic_t)info
->si_fd
;
44 fprintf(stderr
, "Got signal %d about fd %d\n", sig
, fd_pending
);
49 int main(int argc
, char *argv
[])
56 act
.sa_handler
= NULL
;
57 act
.sa_sigaction
= signal_handler
;
58 act
.sa_flags
= SA_SIGINFO
;
59 if (sigaction(RT_SIGNAL_LEASE
, &act
, NULL
) != 0) {
60 fprintf(stderr
, "Failed to set up the signal hangler.\n");
64 int fd
= open(argv
[1], O_RDWR
);
65 fprintf(stderr
, "Opening '%s' for fd %d\n", argv
[1], fd
);
67 signal(SIGINT
, sighandler_die
);
71 if (fcntl(fd
, F_SETSIG
, RT_SIGNAL_LEASE
) == -1) {
72 fprintf(stderr
, "Failed to set signal handler for kernel lease\n");
74 if (fcntl(fd
, F_SETLEASE
, F_RDLCK
) == -1) {
75 fprintf(stderr
, "Failed to get a write lock on %s: %s\n",
76 argv
[1], strerror(errno
));
82 fprintf(stderr
, "Write on fd %d detected!\n", fd_pending
);
86 if (fcntl(fd
, F_SETLEASE
, F_UNLCK
) == -1) {
87 fprintf(stderr
, "Failed to unlock %s\n", argv
[1]);
92 fprintf(stderr
, "Terminated successfully!\n");