20d0aaafa05e1b282595093ee2c21674e5f348aa
[ccan] / ccan / tdb2 / test / run-82-lockattr.c
1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include "logging.h"
4
5 static int mylock(int fd, int rw, off_t off, off_t len, bool waitflag,
6                   void *_err)
7 {
8         int *lock_err = _err;
9         struct flock fl;
10         int ret;
11
12         if (*lock_err) {
13                 errno = *lock_err;
14                 return -1;
15         }
16
17         do {
18                 fl.l_type = rw;
19                 fl.l_whence = SEEK_SET;
20                 fl.l_start = off;
21                 fl.l_len = len;
22
23                 if (waitflag)
24                         ret = fcntl(fd, F_SETLKW, &fl);
25                 else
26                         ret = fcntl(fd, F_SETLK, &fl);
27         } while (ret != 0 && errno == EINTR);
28
29         return ret;
30 }
31
32 static int myunlock(int fd, int rw, off_t off, off_t len, void *_err)
33 {
34         int *lock_err = _err;
35         struct flock fl;
36         int ret;
37
38         if (*lock_err) {
39                 errno = *lock_err;
40                 return -1;
41         }
42
43         do {
44                 fl.l_type = F_UNLCK;
45                 fl.l_whence = SEEK_SET;
46                 fl.l_start = off;
47                 fl.l_len = len;
48
49                 ret = fcntl(fd, F_SETLKW, &fl);
50         } while (ret != 0 && errno == EINTR);
51
52         return ret;
53 }
54
55 static int trav_err;
56 static int trav(struct tdb_context *tdb, TDB_DATA k, TDB_DATA d, int *err)
57 {
58         *err = trav_err;
59         return 0;
60 }
61
62 int main(int argc, char *argv[])
63 {
64         unsigned int i;
65         struct tdb_context *tdb;
66         int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
67                         TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT };
68         union tdb_attribute lock_attr;
69         struct tdb_data key = tdb_mkdata("key", 3);
70         struct tdb_data data = tdb_mkdata("data", 4);
71         int lock_err;
72
73         lock_attr.base.attr = TDB_ATTRIBUTE_FLOCK;
74         lock_attr.base.next = &tap_log_attr;
75         lock_attr.flock.lock = mylock;
76         lock_attr.flock.unlock = myunlock;
77         lock_attr.flock.data = &lock_err;
78
79         plan_tests(sizeof(flags) / sizeof(flags[0]) * 80);
80
81         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
82                 struct tdb_data d;
83
84                 /* Nonblocking open; expect no error message. */
85                 lock_err = EAGAIN;
86                 tdb = tdb_open("run-82-lockattr.tdb", flags[i],
87                                O_RDWR|O_CREAT|O_TRUNC, 0600, &lock_attr);
88                 ok(errno == lock_err, "Errno is %u", errno);
89                 ok1(!tdb);
90                 ok1(tap_log_messages == 0);
91
92                 lock_err = EINTR;
93                 tdb = tdb_open("run-82-lockattr.tdb", flags[i],
94                                O_RDWR|O_CREAT|O_TRUNC, 0600, &lock_attr);
95                 ok(errno == lock_err, "Errno is %u", errno);
96                 ok1(!tdb);
97                 ok1(tap_log_messages == 0);
98
99                 /* Forced fail open. */
100                 lock_err = ENOMEM;
101                 tdb = tdb_open("run-82-lockattr.tdb", flags[i],
102                                O_RDWR|O_CREAT|O_TRUNC, 0600, &lock_attr);
103                 ok1(errno == lock_err);
104                 ok1(!tdb);
105                 ok1(tap_log_messages == 1);
106                 tap_log_messages = 0;
107
108                 lock_err = 0;
109                 tdb = tdb_open("run-82-lockattr.tdb", flags[i],
110                                O_RDWR|O_CREAT|O_TRUNC, 0600, &lock_attr);
111                 if (!ok1(tdb))
112                         continue;
113                 ok1(tap_log_messages == 0);
114
115                 /* Nonblocking store. */
116                 lock_err = EAGAIN;
117                 ok1(tdb_store(tdb, key, data, TDB_REPLACE) == TDB_ERR_LOCK);
118                 ok1(tap_log_messages == 0);
119                 lock_err = EINTR;
120                 ok1(tdb_store(tdb, key, data, TDB_REPLACE) == TDB_ERR_LOCK);
121                 ok1(tap_log_messages == 0);
122                 lock_err = ENOMEM;
123                 ok1(tdb_store(tdb, key, data, TDB_REPLACE) == TDB_ERR_LOCK);
124                 ok1(tap_log_messages == 1);
125                 tap_log_messages = 0;
126
127                 /* Nonblocking fetch. */
128                 lock_err = EAGAIN;
129                 ok1(!tdb_exists(tdb, key));
130                 ok1(tap_log_messages == 0);
131                 lock_err = EINTR;
132                 ok1(!tdb_exists(tdb, key));
133                 ok1(tap_log_messages == 0);
134                 lock_err = ENOMEM;
135                 ok1(!tdb_exists(tdb, key));
136                 ok1(tap_log_messages == 1);
137                 tap_log_messages = 0;
138
139                 lock_err = EAGAIN;
140                 ok1(tdb_fetch(tdb, key, &d) == TDB_ERR_LOCK);
141                 ok1(tap_log_messages == 0);
142                 lock_err = EINTR;
143                 ok1(tdb_fetch(tdb, key, &d) == TDB_ERR_LOCK);
144                 ok1(tap_log_messages == 0);
145                 lock_err = ENOMEM;
146                 ok1(tdb_fetch(tdb, key, &d) == TDB_ERR_LOCK);
147                 ok1(tap_log_messages == 1);
148                 tap_log_messages = 0;
149
150                 /* Nonblocking delete. */
151                 lock_err = EAGAIN;
152                 ok1(tdb_delete(tdb, key) == TDB_ERR_LOCK);
153                 ok1(tap_log_messages == 0);
154                 lock_err = EINTR;
155                 ok1(tdb_delete(tdb, key) == TDB_ERR_LOCK);
156                 ok1(tap_log_messages == 0);
157                 lock_err = ENOMEM;
158                 ok1(tdb_delete(tdb, key) == TDB_ERR_LOCK);
159                 ok1(tap_log_messages == 1);
160                 tap_log_messages = 0;
161
162                 /* Nonblocking locks. */
163                 lock_err = EAGAIN;
164                 ok1(tdb_chainlock(tdb, key) == TDB_ERR_LOCK);
165                 ok1(tap_log_messages == 0);
166                 lock_err = EINTR;
167                 ok1(tdb_chainlock(tdb, key) == TDB_ERR_LOCK);
168                 ok1(tap_log_messages == 0);
169                 lock_err = ENOMEM;
170                 ok1(tdb_chainlock(tdb, key) == TDB_ERR_LOCK);
171                 ok1(tap_log_messages == 1);
172                 tap_log_messages = 0;
173
174                 lock_err = EAGAIN;
175                 ok1(tdb_chainlock_read(tdb, key) == TDB_ERR_LOCK);
176                 ok1(tap_log_messages == 0);
177                 lock_err = EINTR;
178                 ok1(tdb_chainlock_read(tdb, key) == TDB_ERR_LOCK);
179                 ok1(tap_log_messages == 0);
180                 lock_err = ENOMEM;
181                 ok1(tdb_chainlock_read(tdb, key) == TDB_ERR_LOCK);
182                 ok1(tap_log_messages == 1);
183                 tap_log_messages = 0;
184
185                 lock_err = EAGAIN;
186                 ok1(tdb_lockall(tdb) == TDB_ERR_LOCK);
187                 ok1(tap_log_messages == 0);
188                 lock_err = EINTR;
189                 ok1(tdb_lockall(tdb) == TDB_ERR_LOCK);
190                 ok1(tap_log_messages == 0);
191                 lock_err = ENOMEM;
192                 ok1(tdb_lockall(tdb) == TDB_ERR_LOCK);
193                 /* This actually does divide and conquer. */
194                 ok1(tap_log_messages > 0);
195                 tap_log_messages = 0;
196
197                 lock_err = EAGAIN;
198                 ok1(tdb_lockall_read(tdb) == TDB_ERR_LOCK);
199                 ok1(tap_log_messages == 0);
200                 lock_err = EINTR;
201                 ok1(tdb_lockall_read(tdb) == TDB_ERR_LOCK);
202                 ok1(tap_log_messages == 0);
203                 lock_err = ENOMEM;
204                 ok1(tdb_lockall_read(tdb) == TDB_ERR_LOCK);
205                 ok1(tap_log_messages > 0);
206                 tap_log_messages = 0;
207
208                 /* Nonblocking traverse; go nonblock partway through. */
209                 lock_err = 0;
210                 ok1(tdb_store(tdb, key, data, TDB_REPLACE) == 0);
211                 trav_err = EAGAIN;
212                 ok1(tdb_traverse(tdb, trav, &lock_err) == TDB_ERR_LOCK);
213                 ok1(tap_log_messages == 0);
214                 trav_err = EINTR;
215                 lock_err = 0;
216                 ok1(tdb_traverse(tdb, trav, &lock_err) == TDB_ERR_LOCK);
217                 ok1(tap_log_messages == 0);
218                 trav_err = ENOMEM;
219                 lock_err = 0;
220                 ok1(tdb_traverse(tdb, trav, &lock_err) == TDB_ERR_LOCK);
221                 ok1(tap_log_messages == 1);
222                 tap_log_messages = 0;
223
224                 /* Nonblocking transactions. */
225                 lock_err = EAGAIN;
226                 ok1(tdb_transaction_start(tdb) == TDB_ERR_LOCK);
227                 ok1(tap_log_messages == 0);
228                 lock_err = EINTR;
229                 ok1(tdb_transaction_start(tdb) == TDB_ERR_LOCK);
230                 ok1(tap_log_messages == 0);
231                 lock_err = ENOMEM;
232                 ok1(tdb_transaction_start(tdb) == TDB_ERR_LOCK);
233                 ok1(tap_log_messages == 1);
234                 tap_log_messages = 0;
235
236                 /* Nonblocking transaction prepare. */
237                 lock_err = 0;
238                 ok1(tdb_transaction_start(tdb) == 0);
239                 ok1(tdb_delete(tdb, key) == 0);
240
241                 lock_err = EAGAIN;
242                 ok1(tdb_transaction_prepare_commit(tdb) == TDB_ERR_LOCK);
243                 ok1(tap_log_messages == 0);
244
245                 lock_err = 0;
246                 ok1(tdb_transaction_prepare_commit(tdb) == 0);
247                 ok1(tdb_transaction_commit(tdb) == 0);
248
249                 /* And the transaction was committed, right? */
250                 ok1(!tdb_exists(tdb, key));
251                 tdb_close(tdb);
252                 ok1(tap_log_messages == 0);
253         }
254         return exit_status();
255 }