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