]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-83-openhook.c
tdb2: allow multiple chain locks.
[ccan] / ccan / tdb2 / test / run-83-openhook.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/check.c>
8 #include <ccan/tdb2/transaction.c>
9 #include <ccan/tap/tap.h>
10 #include <stdlib.h>
11 #include <stdbool.h>
12 #include <stdarg.h>
13 #include <err.h>
14 #include "external-agent.h"
15 #include "logging.h"
16
17 static enum TDB_ERROR clear_if_first(int fd, void *arg)
18 {
19 /* We hold a lock offset 63 always, so we can tell if anyone is holding it. */
20         struct flock fl;
21
22         if (arg != clear_if_first)
23                 return TDB_ERR_CORRUPT;
24
25         fl.l_type = F_WRLCK;
26         fl.l_whence = SEEK_SET;
27         fl.l_start = 63;
28         fl.l_len = 1;
29
30         if (fcntl(fd, F_SETLK, &fl) == 0) {
31                 /* We must be first ones to open it! */
32                 diag("truncating file!");
33                 if (ftruncate(fd, 0) != 0) {
34                         return TDB_ERR_IO;
35                 }
36         }
37         fl.l_type = F_RDLCK;
38         if (fcntl(fd, F_SETLKW, &fl) != 0) {
39                 return TDB_ERR_IO;
40         }
41         return TDB_SUCCESS;
42 }
43
44 int main(int argc, char *argv[])
45 {
46         unsigned int i;
47         struct tdb_context *tdb;
48         struct agent *agent;
49         union tdb_attribute cif;
50         struct tdb_data key = tdb_mkdata("key", 3);
51         int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
52                         TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT };
53
54         cif.openhook.base.attr = TDB_ATTRIBUTE_OPENHOOK;
55         cif.openhook.base.next = &tap_log_attr;
56         cif.openhook.fn = clear_if_first;
57         cif.openhook.data = clear_if_first;
58
59         agent = prepare_external_agent();
60         plan_tests(sizeof(flags) / sizeof(flags[0]) * 13);
61         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
62                 /* Create it */
63                 tdb = tdb_open("run-83-openhook.tdb", flags[i],
64                                O_RDWR|O_CREAT|O_TRUNC, 0600, NULL);
65                 ok1(tdb);
66                 ok1(tdb_store(tdb, key, key, TDB_REPLACE) == 0);
67                 tdb_close(tdb);
68
69                 /* Now, open with CIF, should clear it. */
70                 tdb = tdb_open("run-83-openhook.tdb", flags[i],
71                                O_RDWR, 0, &cif);
72                 ok1(tdb);
73                 ok1(!tdb_exists(tdb, key));
74                 ok1(tdb_store(tdb, key, key, TDB_REPLACE) == 0);
75
76                 /* Agent should not clear it, since it's still open. */
77                 ok1(external_agent_operation(agent, OPEN_WITH_HOOK,
78                                              "run-83-openhook.tdb") == SUCCESS);
79                 ok1(external_agent_operation(agent, FETCH, "key") == SUCCESS);
80                 ok1(external_agent_operation(agent, CLOSE, "") == SUCCESS);
81
82                 /* Still exists for us too. */
83                 ok1(tdb_exists(tdb, key));
84
85                 /* Close it, now agent should clear it. */
86                 tdb_close(tdb);
87
88                 ok1(external_agent_operation(agent, OPEN_WITH_HOOK,
89                                              "run-83-openhook.tdb") == SUCCESS);
90                 ok1(external_agent_operation(agent, FETCH, "key") == FAILED);
91                 ok1(external_agent_operation(agent, CLOSE, "") == SUCCESS);
92
93                 ok1(tap_log_messages == 0);
94         }
95
96         free_external_agent(agent);
97         return exit_status();
98 }
99