r25068: Older samba3 DCs will return DCERPC_FAULT_OP_RNG_ERROR for every opcode on the
[Samba.git] / source / lib / tdb / tools / tdbtorture.c
blob14a2b48cdc78d4bf5e4d215b1f2cd354e08312cc
1 /* this tests tdb by doing lots of ops from several simultaneous
2 writers - that stresses the locking code.
3 */
5 #include "replace.h"
6 #include "tdb.h"
7 #include "system/time.h"
8 #include "system/wait.h"
9 #include "system/filesys.h"
11 #ifdef HAVE_GETOPT_H
12 #include <getopt.h>
13 #endif
16 #define REOPEN_PROB 30
17 #define DELETE_PROB 8
18 #define STORE_PROB 4
19 #define APPEND_PROB 6
20 #define TRANSACTION_PROB 10
21 #define LOCKSTORE_PROB 5
22 #define TRAVERSE_PROB 20
23 #define TRAVERSE_READ_PROB 20
24 #define CULL_PROB 100
25 #define KEYLEN 3
26 #define DATALEN 100
28 static struct tdb_context *db;
29 static int in_transaction;
30 static int error_count;
32 #ifdef PRINTF_ATTRIBUTE
33 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
34 #endif
35 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
37 va_list ap;
39 error_count++;
41 va_start(ap, format);
42 vfprintf(stdout, format, ap);
43 va_end(ap);
44 fflush(stdout);
45 #if 0
47 char *ptr;
48 asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
49 system(ptr);
50 free(ptr);
52 #endif
55 static void fatal(const char *why)
57 perror(why);
58 error_count++;
61 static char *randbuf(int len)
63 char *buf;
64 int i;
65 buf = (char *)malloc(len+1);
67 for (i=0;i<len;i++) {
68 buf[i] = 'a' + (rand() % 26);
70 buf[i] = 0;
71 return buf;
74 static int cull_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
75 void *state)
77 #if CULL_PROB
78 if (random() % CULL_PROB == 0) {
79 tdb_delete(tdb, key);
81 #endif
82 return 0;
85 static void addrec_db(void)
87 int klen, dlen;
88 char *k, *d;
89 TDB_DATA key, data;
91 klen = 1 + (rand() % KEYLEN);
92 dlen = 1 + (rand() % DATALEN);
94 k = randbuf(klen);
95 d = randbuf(dlen);
97 key.dptr = (unsigned char *)k;
98 key.dsize = klen+1;
100 data.dptr = (unsigned char *)d;
101 data.dsize = dlen+1;
103 #if TRANSACTION_PROB
104 if (in_transaction == 0 && random() % TRANSACTION_PROB == 0) {
105 if (tdb_transaction_start(db) != 0) {
106 fatal("tdb_transaction_start failed");
108 in_transaction++;
109 goto next;
111 if (in_transaction && random() % TRANSACTION_PROB == 0) {
112 if (tdb_transaction_commit(db) != 0) {
113 fatal("tdb_transaction_commit failed");
115 in_transaction--;
116 goto next;
118 if (in_transaction && random() % TRANSACTION_PROB == 0) {
119 if (tdb_transaction_cancel(db) != 0) {
120 fatal("tdb_transaction_cancel failed");
122 in_transaction--;
123 goto next;
125 #endif
127 #if REOPEN_PROB
128 if (in_transaction == 0 && random() % REOPEN_PROB == 0) {
129 tdb_reopen_all(0);
130 goto next;
132 #endif
134 #if DELETE_PROB
135 if (random() % DELETE_PROB == 0) {
136 tdb_delete(db, key);
137 goto next;
139 #endif
141 #if STORE_PROB
142 if (random() % STORE_PROB == 0) {
143 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
144 fatal("tdb_store failed");
146 goto next;
148 #endif
150 #if APPEND_PROB
151 if (random() % APPEND_PROB == 0) {
152 if (tdb_append(db, key, data) != 0) {
153 fatal("tdb_append failed");
155 goto next;
157 #endif
159 #if LOCKSTORE_PROB
160 if (random() % LOCKSTORE_PROB == 0) {
161 tdb_chainlock(db, key);
162 data = tdb_fetch(db, key);
163 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
164 fatal("tdb_store failed");
166 if (data.dptr) free(data.dptr);
167 tdb_chainunlock(db, key);
168 goto next;
170 #endif
172 #if TRAVERSE_PROB
173 if (random() % TRAVERSE_PROB == 0) {
174 tdb_traverse(db, cull_traverse, NULL);
175 goto next;
177 #endif
179 #if TRAVERSE_READ_PROB
180 if (random() % TRAVERSE_READ_PROB == 0) {
181 tdb_traverse_read(db, NULL, NULL);
182 goto next;
184 #endif
186 data = tdb_fetch(db, key);
187 if (data.dptr) free(data.dptr);
189 next:
190 free(k);
191 free(d);
194 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
195 void *state)
197 tdb_delete(tdb, key);
198 return 0;
201 static void usage(void)
203 printf("Usage: tdbtorture [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-H HASH_SIZE]\n");
204 exit(0);
207 int main(int argc, char * const *argv)
209 int i, seed = -1;
210 int num_procs = 3;
211 int num_loops = 5000;
212 int hash_size = 2;
213 int c;
214 extern char *optarg;
215 pid_t *pids;
217 struct tdb_logging_context log_ctx;
218 log_ctx.log_fn = tdb_log;
220 while ((c = getopt(argc, argv, "n:l:s:H:h")) != -1) {
221 switch (c) {
222 case 'n':
223 num_procs = strtol(optarg, NULL, 0);
224 break;
225 case 'l':
226 num_loops = strtol(optarg, NULL, 0);
227 break;
228 case 'H':
229 hash_size = strtol(optarg, NULL, 0);
230 break;
231 case 's':
232 seed = strtol(optarg, NULL, 0);
233 break;
234 default:
235 usage();
239 unlink("torture.tdb");
241 pids = calloc(sizeof(pid_t), num_procs);
242 pids[0] = getpid();
244 for (i=0;i<num_procs-1;i++) {
245 if ((pids[i+1]=fork()) == 0) break;
248 db = tdb_open_ex("torture.tdb", hash_size, TDB_CLEAR_IF_FIRST,
249 O_RDWR | O_CREAT, 0600, &log_ctx, NULL);
250 if (!db) {
251 fatal("db open failed");
254 if (seed == -1) {
255 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
258 if (i == 0) {
259 printf("testing with %d processes, %d loops, %d hash_size, seed=%d\n",
260 num_procs, num_loops, hash_size, seed);
263 srand(seed + i);
264 srandom(seed + i);
266 for (i=0;i<num_loops && error_count == 0;i++) {
267 addrec_db();
270 if (error_count == 0) {
271 tdb_traverse_read(db, NULL, NULL);
272 tdb_traverse(db, traverse_fn, NULL);
273 tdb_traverse(db, traverse_fn, NULL);
276 tdb_close(db);
278 if (getpid() != pids[0]) {
279 return error_count;
282 for (i=1;i<num_procs;i++) {
283 int status, j;
284 pid_t pid;
285 if (error_count != 0) {
286 /* try and stop the test on any failure */
287 for (j=1;j<num_procs;j++) {
288 if (pids[j] != 0) {
289 kill(pids[j], SIGTERM);
293 pid = waitpid(-1, &status, 0);
294 if (pid == -1) {
295 perror("failed to wait for child\n");
296 exit(1);
298 for (j=1;j<num_procs;j++) {
299 if (pids[j] == pid) break;
301 if (j == num_procs) {
302 printf("unknown child %d exited!?\n", (int)pid);
303 exit(1);
305 if (WEXITSTATUS(status) != 0) {
306 printf("child %d exited with status %d\n",
307 (int)pid, WEXITSTATUS(status));
308 error_count++;
310 pids[j] = 0;
313 if (error_count == 0) {
314 printf("OK\n");
317 return error_count;