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