96107d637e4472b7026b587f26c031191b1c9631
[ccan] / ccan / tdb2 / test / run-56-open-during-transaction.c
1 #include "config.h"
2 #include <unistd.h>
3 #include "lock-tracking.h"
4
5 static ssize_t pwrite_check(int fd, const void *buf, size_t count, off_t offset);
6 static ssize_t write_check(int fd, const void *buf, size_t count);
7 static int ftruncate_check(int fd, off_t length);
8
9 #define pwrite pwrite_check
10 #define write write_check
11 #define fcntl fcntl_with_lockcheck
12 #define ftruncate ftruncate_check
13
14 #include <ccan/tdb2/tdb.c>
15 #include <ccan/tdb2/open.c>
16 #include <ccan/tdb2/free.c>
17 #include <ccan/tdb2/lock.c>
18 #include <ccan/tdb2/io.c>
19 #include <ccan/tdb2/hash.c>
20 #include <ccan/tdb2/check.c>
21 #include <ccan/tdb2/transaction.c>
22 #include <ccan/tap/tap.h>
23 #include <stdlib.h>
24 #include <stdbool.h>
25 #include <stdarg.h>
26 #include <err.h>
27 #include "external-agent.h"
28 #include "logging.h"
29
30 static struct agent *agent;
31 static bool opened;
32 static int errors = 0;
33 #define TEST_DBNAME "run-56-open-during-transaction.tdb"
34
35 #undef write
36 #undef pwrite
37 #undef fcntl
38 #undef ftruncate
39
40 static bool is_same(const char *snapshot, const char *latest, off_t len)
41 {
42         unsigned i;
43
44         for (i = 0; i < len; i++) {
45                 if (snapshot[i] != latest[i])
46                         return false;
47         }
48         return true;
49 }
50
51 static bool compare_file(int fd, const char *snapshot, off_t snapshot_len)
52 {
53         char *contents;
54         bool same;
55
56         /* over-length read serves as length check. */
57         contents = malloc(snapshot_len+1);
58         same = pread(fd, contents, snapshot_len+1, 0) == snapshot_len
59                 && is_same(snapshot, contents, snapshot_len);
60         free(contents);
61         return same;
62 }
63
64 static void check_file_intact(int fd)
65 {
66         enum agent_return ret;
67         struct stat st;
68         char *contents;
69
70         fstat(fd, &st);
71         contents = malloc(st.st_size);
72         if (pread(fd, contents, st.st_size, 0) != st.st_size) {
73                 diag("Read fail");
74                 errors++;
75                 return;
76         }
77
78         /* Ask agent to open file. */
79         ret = external_agent_operation(agent, OPEN, TEST_DBNAME);
80
81         /* It's OK to open it, but it must not have changed! */
82         if (!compare_file(fd, contents, st.st_size)) {
83                 diag("Agent changed file after opening %s",
84                      agent_return_name(ret));
85                 errors++;
86         }
87
88         if (ret == SUCCESS) {
89                 ret = external_agent_operation(agent, CLOSE, NULL);
90                 if (ret != SUCCESS) {
91                         diag("Agent failed to close tdb: %s",
92                              agent_return_name(ret));
93                         errors++;
94                 }
95         } else if (ret != WOULD_HAVE_BLOCKED) {
96                 diag("Agent opening file gave %s",
97                      agent_return_name(ret));
98                 errors++;
99         }
100
101         free(contents);
102 }
103
104 static void after_unlock(int fd)
105 {
106         if (opened)
107                 check_file_intact(fd);
108 }
109
110 static ssize_t pwrite_check(int fd,
111                             const void *buf, size_t count, off_t offset)
112 {
113         if (opened)
114                 check_file_intact(fd);
115
116         return pwrite(fd, buf, count, offset);
117 }
118
119 static ssize_t write_check(int fd, const void *buf, size_t count)
120 {
121         if (opened)
122                 check_file_intact(fd);
123
124         return write(fd, buf, count);
125 }
126
127 static int ftruncate_check(int fd, off_t length)
128 {
129         if (opened)
130                 check_file_intact(fd);
131
132         return ftruncate(fd, length);
133
134 }
135
136 int main(int argc, char *argv[])
137 {
138         const int flags[] = { TDB_DEFAULT,
139                               TDB_NOMMAP,
140                               TDB_CONVERT,
141                               TDB_CONVERT | TDB_NOMMAP };
142         int i;
143         struct tdb_context *tdb;
144         TDB_DATA key, data;
145
146         plan_tests(20);
147         agent = prepare_external_agent();
148         if (!agent)
149                 err(1, "preparing agent");
150
151         unlock_callback = after_unlock;
152         for (i = 0; i < sizeof(flags)/sizeof(flags[0]); i++) {
153                 diag("Test with %s and %s\n",
154                      (flags[i] & TDB_CONVERT) ? "CONVERT" : "DEFAULT",
155                      (flags[i] & TDB_NOMMAP) ? "no mmap" : "mmap");
156                 unlink(TEST_DBNAME);
157                 tdb = tdb_open(TEST_DBNAME, flags[i],
158                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
159                 ok1(tdb);
160
161                 opened = true;
162                 ok1(tdb_transaction_start(tdb) == 0);
163                 key = tdb_mkdata("hi", strlen("hi"));
164                 data = tdb_mkdata("world", strlen("world"));
165
166                 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
167                 ok1(tdb_transaction_commit(tdb) == 0);
168                 ok(!errors, "We had %u open errors", errors);
169
170                 opened = false;
171                 tdb_close(tdb);
172         }
173
174         return exit_status();
175 }