cafe067d0bb8e1a02ed18200c279d6bcf0c8c739
[ddiss/samba.git] / lib / ntdb / test / api-locktimeout.c
1 #include "config.h"
2 #include "ntdb.h"
3 #include "tap-interface.h"
4 #include "system/wait.h"
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <sys/time.h>
8 #include <fcntl.h>
9 #include <limits.h>
10 #include <errno.h>
11 #include "logging.h"
12 #include "external-agent.h"
13
14 #undef alarm
15 #define alarm fast_alarm
16
17 /* Speed things up by doing things in milliseconds. */
18 static unsigned int fast_alarm(unsigned int milli_seconds)
19 {
20         struct itimerval it;
21
22         it.it_interval.tv_sec = it.it_interval.tv_usec = 0;
23         it.it_value.tv_sec = milli_seconds / 1000;
24         it.it_value.tv_usec = milli_seconds * 1000;
25         setitimer(ITIMER_REAL, &it, NULL);
26         return 0;
27 }
28
29 #define CatchSignal(sig, handler) signal((sig), (handler))
30
31 static void do_nothing(int signum)
32 {
33 }
34
35 /* This example code is taken from SAMBA, so try not to change it. */
36 static struct flock flock_struct;
37
38 /* Return a value which is none of v1, v2 or v3. */
39 static inline short int invalid_value(short int v1, short int v2, short int v3)
40 {
41         short int try = (v1+v2+v3)^((v1+v2+v3) << 16);
42         while (try == v1 || try == v2 || try == v3)
43                 try++;
44         return try;
45 }
46
47 /* We invalidate in as many ways as we can, so the OS rejects it */
48 static void invalidate_flock_struct(int signum)
49 {
50         flock_struct.l_type = invalid_value(F_RDLCK, F_WRLCK, F_UNLCK);
51         flock_struct.l_whence = invalid_value(SEEK_SET, SEEK_CUR, SEEK_END);
52         flock_struct.l_start = -1;
53         /* A large negative. */
54         flock_struct.l_len = (((off_t)1 << (sizeof(off_t)*CHAR_BIT - 1)) + 1);
55 }
56
57 static int timeout_lock(int fd, int rw, off_t off, off_t len, bool waitflag,
58                         void *_timeout)
59 {
60         int ret, saved_errno = errno;
61         unsigned int timeout = *(unsigned int *)_timeout;
62
63         flock_struct.l_type = rw;
64         flock_struct.l_whence = SEEK_SET;
65         flock_struct.l_start = off;
66         flock_struct.l_len = len;
67
68         CatchSignal(SIGALRM, invalidate_flock_struct);
69         alarm(timeout);
70
71         for (;;) {
72                 if (waitflag)
73                         ret = fcntl(fd, F_SETLKW, &flock_struct);
74                 else
75                         ret = fcntl(fd, F_SETLK, &flock_struct);
76
77                 if (ret == 0)
78                         break;
79
80                 /* Not signalled?  Something else went wrong. */
81                 if (flock_struct.l_len == len) {
82                         if (errno == EAGAIN || errno == EINTR)
83                                 continue;
84                         saved_errno = errno;
85                         break;
86                 } else {
87                         saved_errno = EINTR;
88                         break;
89                 }
90         }
91
92         alarm(0);
93         errno = saved_errno;
94         return ret;
95 }
96
97 static int ntdb_chainlock_with_timeout_internal(struct ntdb_context *ntdb,
98                                                NTDB_DATA key,
99                                                unsigned int timeout,
100                                                int rw_type)
101 {
102         union ntdb_attribute locking;
103         enum NTDB_ERROR ecode;
104
105         if (timeout) {
106                 locking.base.attr = NTDB_ATTRIBUTE_FLOCK;
107                 ecode = ntdb_get_attribute(ntdb, &locking);
108                 if (ecode != NTDB_SUCCESS)
109                         return ecode;
110
111                 /* Replace locking function with our own. */
112                 locking.flock.data = &timeout;
113                 locking.flock.lock = timeout_lock;
114
115                 ecode = ntdb_set_attribute(ntdb, &locking);
116                 if (ecode != NTDB_SUCCESS)
117                         return ecode;
118         }
119         if (rw_type == F_RDLCK)
120                 ecode = ntdb_chainlock_read(ntdb, key);
121         else
122                 ecode = ntdb_chainlock(ntdb, key);
123
124         if (timeout) {
125                 ntdb_unset_attribute(ntdb, NTDB_ATTRIBUTE_FLOCK);
126         }
127         return ecode;
128 }
129
130 int main(int argc, char *argv[])
131 {
132         unsigned int i;
133         struct ntdb_context *ntdb;
134         NTDB_DATA key = ntdb_mkdata("hello", 5);
135         int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP,
136                         NTDB_CONVERT, NTDB_NOMMAP|NTDB_CONVERT };
137         struct agent *agent;
138
139         plan_tests(sizeof(flags) / sizeof(flags[0]) * 15);
140
141         agent = prepare_external_agent();
142
143         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
144                 enum NTDB_ERROR ecode;
145                 ntdb = ntdb_open("run-locktimeout.ntdb", flags[i],
146                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
147                 if (!ok1(ntdb))
148                         break;
149
150                 /* Simple cases: should succeed. */
151                 ecode = ntdb_chainlock_with_timeout_internal(ntdb, key, 20,
152                                                             F_RDLCK);
153                 ok1(ecode == NTDB_SUCCESS);
154                 ok1(tap_log_messages == 0);
155
156                 ntdb_chainunlock_read(ntdb, key);
157                 ok1(tap_log_messages == 0);
158
159                 ecode = ntdb_chainlock_with_timeout_internal(ntdb, key, 20,
160                                                             F_WRLCK);
161                 ok1(ecode == NTDB_SUCCESS);
162                 ok1(tap_log_messages == 0);
163
164                 ntdb_chainunlock(ntdb, key);
165                 ok1(tap_log_messages == 0);
166
167                 /* OK, get agent to start transaction, then we should time out. */
168                 ok1(external_agent_operation(agent, OPEN, "run-locktimeout.ntdb")
169                     == SUCCESS);
170                 ok1(external_agent_operation(agent, TRANSACTION_START, "")
171                     == SUCCESS);
172                 ecode = ntdb_chainlock_with_timeout_internal(ntdb, key, 20,
173                                                             F_WRLCK);
174                 ok1(ecode == NTDB_ERR_LOCK);
175                 ok1(tap_log_messages == 0);
176
177                 /* Even if we get a different signal, should be fine. */
178                 CatchSignal(SIGUSR1, do_nothing);
179                 external_agent_operation(agent, SEND_SIGNAL, "");
180                 ecode = ntdb_chainlock_with_timeout_internal(ntdb, key, 20,
181                                                             F_WRLCK);
182                 ok1(ecode == NTDB_ERR_LOCK);
183                 ok1(tap_log_messages == 0);
184
185                 ok1(external_agent_operation(agent, TRANSACTION_COMMIT, "")
186                     == SUCCESS);
187                 ok1(external_agent_operation(agent, CLOSE, "")
188                     == SUCCESS);
189                 ntdb_close(ntdb);
190         }
191         free_external_agent(agent);
192         return exit_status();
193 }