summaryrefslogtreecommitdiff
path: root/lib/tdb2/test/api-fork-test.c
blob: 934c71cbe8c4a47e059d7e0ef924df0e276e9ec4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* Test forking while holding lock.
 *
 * There are only five ways to do this currently:
 * (1) grab a tdb_chainlock, then fork.
 * (2) grab a tdb_lockall, then fork.
 * (3) grab a tdb_lockall_read, then fork.
 * (4) start a transaction, then fork.
 * (5) fork from inside a tdb_parse() callback.
 *
 * Note that we don't hold a lock across tdb_traverse callbacks, so
 * that doesn't matter.
 */
#include "config.h"
#include "tdb2.h"
#include "tap-interface.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include "logging.h"

static enum TDB_ERROR fork_in_parse(TDB_DATA key, TDB_DATA data,
				    struct tdb_context *tdb)
{
	int status;

	if (fork() == 0) {
		/* We expect this to fail. */
		if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
			exit(1);

		if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
			exit(1);

		if (tap_log_messages != 2)
			exit(2);

		tdb_close(tdb);
		if (tap_log_messages != 2)
			exit(3);
		exit(0);
	}
	wait(&status);
	ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
	return TDB_SUCCESS;
}

int main(int argc, char *argv[])
{
	unsigned int i;
	struct tdb_context *tdb;
	int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
			TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT };
	struct tdb_data key = tdb_mkdata("key", 3);
	struct tdb_data data = tdb_mkdata("data", 4);

	plan_tests(sizeof(flags) / sizeof(flags[0]) * 14);
	for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
		int status;

		tap_log_messages = 0;

		tdb = tdb_open("run-fork-test.tdb", flags[i],
			       O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
		if (!ok1(tdb))
			continue;

		/* Put a record in here. */
		ok1(tdb_store(tdb, key, data, TDB_REPLACE) == TDB_SUCCESS);

		ok1(tdb_chainlock(tdb, key) == TDB_SUCCESS);
		if (fork() == 0) {
			/* We expect this to fail. */
			if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
				return 1;

			if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
				return 1;

			if (tap_log_messages != 2)
				return 2;

			tdb_chainunlock(tdb, key);
			if (tap_log_messages != 3)
				return 3;
			tdb_close(tdb);
			if (tap_log_messages != 3)
				return 4;
			return 0;
		}
		wait(&status);
		ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
		tdb_chainunlock(tdb, key);

		ok1(tdb_lockall(tdb) == TDB_SUCCESS);
		if (fork() == 0) {
			/* We expect this to fail. */
			if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
				return 1;

			if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
				return 1;

			if (tap_log_messages != 2)
				return 2;

			tdb_unlockall(tdb);
			if (tap_log_messages != 2)
				return 3;
			tdb_close(tdb);
			if (tap_log_messages != 2)
				return 4;
			return 0;
		}
		wait(&status);
		ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
		tdb_unlockall(tdb);

		ok1(tdb_lockall_read(tdb) == TDB_SUCCESS);
		if (fork() == 0) {
			/* We expect this to fail. */
			/* This would always fail anyway... */
			if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
				return 1;

			if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
				return 1;

			if (tap_log_messages != 2)
				return 2;

			tdb_unlockall_read(tdb);
			if (tap_log_messages != 2)
				return 3;
			tdb_close(tdb);
			if (tap_log_messages != 2)
				return 4;
			return 0;
		}
		wait(&status);
		ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
		tdb_unlockall_read(tdb);

		ok1(tdb_transaction_start(tdb) == TDB_SUCCESS);
		/* If transactions is empty, noop "commit" succeeds. */
		ok1(tdb_delete(tdb, key) == TDB_SUCCESS);
		if (fork() == 0) {
			/* We expect this to fail. */
			if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
				return 1;

			if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
				return 1;

			if (tap_log_messages != 2)
				return 2;

			if (tdb_transaction_commit(tdb) != TDB_ERR_LOCK)
				return 3;

			tdb_close(tdb);
			if (tap_log_messages < 3)
				return 4;
			return 0;
		}
		wait(&status);
		ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
		tdb_transaction_cancel(tdb);

		ok1(tdb_parse_record(tdb, key, fork_in_parse, tdb)
		    == TDB_SUCCESS);
		tdb_close(tdb);
		ok1(tap_log_messages == 0);
	}
	return exit_status();
}