3 #include "tap-interface.h"
11 #include "external-agent.h"
14 static enum TDB_ERROR clear_if_first(int fd, void *arg)
16 /* We hold a lock offset 4 always, so we can tell if anyone is holding it.
17 * (This is compatible with tdb1's TDB_CLEAR_IF_FIRST flag). */
20 if (arg != clear_if_first)
21 return TDB_ERR_CORRUPT;
24 fl.l_whence = SEEK_SET;
28 if (fcntl(fd, F_SETLK, &fl) == 0) {
29 /* We must be first ones to open it! */
30 diag("truncating file!");
31 if (ftruncate(fd, 0) != 0) {
36 if (fcntl(fd, F_SETLKW, &fl) != 0) {
42 int main(int argc, char *argv[])
45 struct tdb_context *tdb;
47 union tdb_attribute cif;
48 struct tdb_data key = tdb_mkdata("key", 3);
49 int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
50 TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT };
52 cif.openhook.base.attr = TDB_ATTRIBUTE_OPENHOOK;
53 cif.openhook.base.next = &tap_log_attr;
54 cif.openhook.fn = clear_if_first;
55 cif.openhook.data = clear_if_first;
57 agent = prepare_external_agent();
58 plan_tests(sizeof(flags) / sizeof(flags[0]) * 13);
59 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
61 tdb = tdb_open("run-83-openhook.tdb", flags[i],
62 O_RDWR|O_CREAT|O_TRUNC, 0600, NULL);
64 ok1(tdb_store(tdb, key, key, TDB_REPLACE) == 0);
67 /* Now, open with CIF, should clear it. */
68 tdb = tdb_open("run-83-openhook.tdb", flags[i],
71 ok1(!tdb_exists(tdb, key));
72 ok1(tdb_store(tdb, key, key, TDB_REPLACE) == 0);
74 /* Agent should not clear it, since it's still open. */
75 ok1(external_agent_operation(agent, OPEN_WITH_HOOK,
76 "run-83-openhook.tdb") == SUCCESS);
77 ok1(external_agent_operation(agent, FETCH, "key") == SUCCESS);
78 ok1(external_agent_operation(agent, CLOSE, "") == SUCCESS);
80 /* Still exists for us too. */
81 ok1(tdb_exists(tdb, key));
83 /* Close it, now agent should clear it. */
86 ok1(external_agent_operation(agent, OPEN_WITH_HOOK,
87 "run-83-openhook.tdb") == SUCCESS);
88 ok1(external_agent_operation(agent, FETCH, "key") == FAILED);
89 ok1(external_agent_operation(agent, CLOSE, "") == SUCCESS);
91 ok1(tap_log_messages == 0);
94 free_external_agent(agent);