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