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