Merged revisions 10129-10142 via svnmerge from
[wvapps.git] / funfs / tests / oplocktest.cc
blob87bb392ee687129854a2e14bd5fa8499b58db154
1 #include <fcntl.h>
2 #include <stdio.h>
3 #include <errno.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <signal.h>
8 #ifndef F_SETLEASE
9 #define F_SETLEASE 1024
10 #endif
12 #ifndef F_GETLEASE
13 #define F_GETLEASE 1025
14 #endif
16 #ifndef RT_SIGNAL_LEASE
17 #define RT_SIGNAL_LEASE 33
18 #endif
20 #ifndef F_SETSIG
21 #define F_SETSIG 10
22 #endif
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)
36 want_to_die = true;
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);
46 //signals_received++;
49 int main(int argc, char *argv[])
51 if (argc < 2)
52 return -1;
54 struct sigaction act;
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");
61 return 20;
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);
69 while (!want_to_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));
79 pause();
81 if (!want_to_die)
82 fprintf(stderr, "Write on fd %d detected!\n", fd_pending);
84 fd_pending = 0;
86 if (fcntl(fd, F_SETLEASE, F_UNLCK) == -1) {
87 fprintf(stderr, "Failed to unlock %s\n", argv[1]);
91 close(fd);
92 fprintf(stderr, "Terminated successfully!\n");