tdb: Test for readonly lock upgrade bug
[samba.git] / lib / tdb / test / run-rdlock-upgrade.c
1 #include "../common/io.c"
2 #include "../common/tdb.c"
3 #include "../common/lock.c"
4 #include "../common/freelist.c"
5 #include "../common/traverse.c"
6 #include "../common/transaction.c"
7 #include "../common/error.c"
8 #include "../common/open.c"
9 #include "../common/check.c"
10 #include "../common/hash.c"
11 #include "../common/mutex.c"
12 #include "tap-interface.h"
13 #include <stdlib.h>
14 #include <sys/types.h>
15 #include <sys/wait.h>
16 #include <stdarg.h>
17 #include "logging.h"
18
19 static TDB_DATA key, data;
20
21 static void do_chainlock(const char *name, int tdb_flags, int up, int down)
22 {
23         struct tdb_context *tdb;
24         int ret;
25         ssize_t nread, nwritten;
26         char c = 0;
27
28         tdb = tdb_open_ex(name, 3, tdb_flags,
29                           O_RDWR|O_CREAT, 0755, &taplogctx, NULL);
30         ok(tdb, "tdb_open_ex should succeed");
31
32         ret = tdb_chainlock_read(tdb, key);
33         ok(ret == 0, "tdb_chainlock_read should succeed");
34
35         nwritten = write(up, &c, sizeof(c));
36         ok(nwritten == sizeof(c), "write should succeed");
37
38         nread = read(down, &c, sizeof(c));
39         ok(nread == 0, "read should succeed");
40
41         exit(0);
42 }
43
44 static void do_trylock(const char *name, int tdb_flags, int up, int down)
45 {
46         struct tdb_context *tdb;
47         int ret;
48         ssize_t nread, nwritten;
49         char c = 0;
50
51         tdb = tdb_open_ex(name, 3, tdb_flags,
52                           O_RDWR|O_CREAT, 0755, &taplogctx, NULL);
53         ok(tdb, "tdb_open_ex should succeed");
54
55         /*
56          * tdb used to have a bug where with fcntl locks an upgrade
57          * from a readlock to writelock did not check for the
58          * underlying fcntl lock. Mutexes don't distinguish between
59          * readlocks and writelocks, so that bug does not apply here.
60          */
61
62         ret = tdb_chainlock_read(tdb, key);
63         ok(ret == 0, "tdb_chainlock_read should succeed");
64
65         ret = tdb_chainlock_nonblock(tdb, key);
66         ok(ret == -1, "tdb_chainlock_nonblock should fail");
67
68         nwritten = write(up, &c, sizeof(c));
69         ok(nwritten == sizeof(c), "write should succeed");
70
71         nread = read(down, &c, sizeof(c));
72         ok(nread == 0, "read should succeed");
73
74         exit(0);
75 }
76
77 static int do_tests(const char *name, int tdb_flags)
78 {
79         int ret;
80         pid_t chainlock_child, store_child;
81         int chainlock_down[2];
82         int chainlock_up[2];
83         int store_down[2];
84         int store_up[2];
85         char c;
86         ssize_t nread;
87
88         key.dsize = strlen("hi");
89         key.dptr = discard_const_p(uint8_t, "hi");
90         data.dsize = strlen("world");
91         data.dptr = discard_const_p(uint8_t, "world");
92
93         ret = pipe(chainlock_down);
94         ok(ret == 0, "pipe should succeed");
95
96         ret = pipe(chainlock_up);
97         ok(ret == 0, "pipe should succeed");
98
99         ret = pipe(store_down);
100         ok(ret == 0, "pipe should succeed");
101
102         ret = pipe(store_up);
103         ok(ret == 0, "pipe should succeed");
104
105         chainlock_child = fork();
106         ok(chainlock_child != -1, "fork should succeed");
107
108         if (chainlock_child == 0) {
109                 close(chainlock_up[0]);
110                 close(chainlock_down[1]);
111                 close(store_up[0]);
112                 close(store_up[1]);
113                 close(store_down[0]);
114                 close(store_down[1]);
115                 do_chainlock(name, tdb_flags,
116                              chainlock_up[1], chainlock_down[0]);
117                 exit(0);
118         }
119         close(chainlock_up[1]);
120         close(chainlock_down[0]);
121
122         nread = read(chainlock_up[0], &c, sizeof(c));
123         ok(nread == sizeof(c), "read should succeed");
124
125         /*
126          * Now we have a process holding a chain read lock. Start
127          * another process trying to write lock. This should fail.
128          */
129
130         store_child = fork();
131         ok(store_child != -1, "fork should succeed");
132
133         if (store_child == 0) {
134                 close(chainlock_up[0]);
135                 close(chainlock_down[1]);
136                 close(store_up[0]);
137                 close(store_down[1]);
138                 do_trylock(name, tdb_flags,
139                            store_up[1], store_down[0]);
140                 exit(0);
141         }
142         close(store_up[1]);
143         close(store_down[0]);
144
145         nread = read(store_up[0], &c, sizeof(c));
146         ok(nread == sizeof(c), "read should succeed");
147
148         close(chainlock_up[0]);
149         close(chainlock_down[1]);
150         close(store_up[0]);
151         close(store_down[1]);
152         diag("%s tests done", name);
153         return exit_status();
154 }
155
156 int main(int argc, char *argv[])
157 {
158         int ret;
159
160         ret = do_tests("rdlock-upgrade.tdb",
161                        TDB_CLEAR_IF_FIRST |
162                        TDB_INCOMPATIBLE_HASH);
163         ok(ret == 0, "rdlock-upgrade.tdb tests should succeed");
164
165         return exit_status();
166 }