3 #include "lock-tracking.h"
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
);
9 #define pwrite pwrite_check
10 #define write write_check
11 #define fcntl fcntl_with_lockcheck
12 #define ftruncate ftruncate_check
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>
27 #include "external-agent.h"
30 static struct agent
*agent
;
32 static int errors
= 0;
33 #define TEST_DBNAME "run-56-open-during-transaction.tdb"
40 static bool is_same(const char *snapshot
, const char *latest
, off_t len
)
44 for (i
= 0; i
< len
; i
++) {
45 if (snapshot
[i
] != latest
[i
])
51 static bool compare_file(int fd
, const char *snapshot
, off_t snapshot_len
)
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
);
64 static void check_file_intact(int fd
)
66 enum agent_return ret
;
71 contents
= malloc(st
.st_size
);
72 if (pread(fd
, contents
, st
.st_size
, 0) != st
.st_size
) {
78 /* Ask agent to open file. */
79 ret
= external_agent_operation(agent
, OPEN
, TEST_DBNAME
);
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
));
89 ret
= external_agent_operation(agent
, CLOSE
, NULL
);
91 diag("Agent failed to close tdb: %s",
92 agent_return_name(ret
));
95 } else if (ret
!= WOULD_HAVE_BLOCKED
) {
96 diag("Agent opening file gave %s",
97 agent_return_name(ret
));
104 static void after_unlock(int fd
)
107 check_file_intact(fd
);
110 static ssize_t
pwrite_check(int fd
,
111 const void *buf
, size_t count
, off_t offset
)
114 check_file_intact(fd
);
116 return pwrite(fd
, buf
, count
, offset
);
119 static ssize_t
write_check(int fd
, const void *buf
, size_t count
)
122 check_file_intact(fd
);
124 return write(fd
, buf
, count
);
127 static int ftruncate_check(int fd
, off_t length
)
130 check_file_intact(fd
);
132 return ftruncate(fd
, length
);
136 int main(int argc
, char *argv
[])
138 const int flags
[] = { TDB_DEFAULT
,
141 TDB_CONVERT
| TDB_NOMMAP
};
143 struct tdb_context
*tdb
;
147 agent
= prepare_external_agent();
149 err(1, "preparing agent");
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");
157 tdb
= tdb_open(TEST_DBNAME
, flags
[i
],
158 O_RDWR
|O_CREAT
|O_TRUNC
, 0600, &tap_log_attr
);
162 ok1(tdb_transaction_start(tdb
) == 0);
163 key
= tdb_mkdata("hi", strlen("hi"));
164 data
= tdb_mkdata("world", strlen("world"));
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
);
174 return exit_status();