r21439: fix compiler warnings
[Samba/ekacnet.git] / source4 / torture / locktest.c
blob608974ec650a6107c0ab2e27d589cd7fe41a1458
1 /*
2 Unix SMB/CIFS implementation.
3 randomised byte range lock tester
4 Copyright (C) Andrew Tridgell 1999
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "system/time.h"
24 #include "pstring.h"
25 #include "auth/credentials/credentials.h"
26 #include "auth/gensec/gensec.h"
27 #include "libcli/libcli.h"
29 static int numops = 1000;
30 static BOOL showall;
31 static BOOL analyze;
32 static BOOL hide_unlock_fails;
33 static BOOL use_oplocks;
34 static uint_t lock_range = 100;
35 static uint_t lock_base = 0;
36 static uint_t min_length = 0;
37 static BOOL exact_error_codes;
38 static BOOL zero_zero;
40 #define FILENAME "\\locktest.dat"
42 #define READ_PCT 50
43 #define LOCK_PCT 45
44 #define UNLOCK_PCT 70
45 #define RANGE_MULTIPLE 1
46 #define NSERVERS 2
47 #define NCONNECTIONS 2
48 #define NFILES 2
49 #define LOCK_TIMEOUT 0
51 static struct cli_credentials *servers[NSERVERS];
53 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
55 struct record {
56 enum lock_op lock_op;
57 enum brl_type lock_type;
58 char conn, f;
59 uint64_t start, len;
60 char needed;
63 #define PRESETS 0
65 #if PRESETS
66 static struct record preset[] = {
67 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
68 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
69 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 0, 1},
70 {OP_UNLOCK, 0 , 0, 0, 2, 0, 1},
71 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
73 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
74 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
75 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
76 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
78 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
79 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 1, 1},
80 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
81 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
83 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
84 {OP_LOCK, WRITE_LOCK, 0, 0, 1, 1, 1},
85 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
86 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
88 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
89 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
90 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
91 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
93 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
94 {OP_LOCK, READ_LOCK, 0, 0, 3, 1, 1},
95 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
96 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
99 #endif
101 static struct record *recorded;
103 /*****************************************************
104 return a connection to a server
105 *******************************************************/
106 static struct smbcli_state *connect_one(char *share, int snum, int conn)
108 struct smbcli_state *c;
109 fstring server, myname;
110 NTSTATUS status;
111 int retries = 10;
113 printf("connect_one(%s, %d, %d)\n", share, snum, conn);
115 fstrcpy(server,share+2);
116 share = strchr_m(server,'\\');
117 if (!share) return NULL;
118 *share = 0;
119 share++;
121 if (snum == 0) {
122 char **unc_list = NULL;
123 int num_unc_names;
124 const char *p;
125 p = lp_parm_string(-1, "torture", "unclist");
126 if (p) {
127 char *h, *s;
128 unc_list = file_lines_load(p, &num_unc_names, NULL);
129 if (!unc_list || num_unc_names <= 0) {
130 printf("Failed to load unc names list from '%s'\n", p);
131 exit(1);
134 if (!smbcli_parse_unc(unc_list[conn % num_unc_names],
135 NULL, &h, &s)) {
136 printf("Failed to parse UNC name %s\n",
137 unc_list[conn % num_unc_names]);
138 exit(1);
140 fstrcpy(server, h);
141 fstrcpy(share, s);
146 slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), snum);
147 cli_credentials_set_workstation(servers[snum], myname, CRED_SPECIFIED);
149 do {
150 printf("\\\\%s\\%s\n", server, share);
151 status = smbcli_full_connection(NULL, &c,
152 server,
153 share, NULL,
154 servers[snum], NULL);
155 if (!NT_STATUS_IS_OK(status)) {
156 sleep(2);
158 } while (!NT_STATUS_IS_OK(status) && retries--);
160 if (!NT_STATUS_IS_OK(status)) {
161 return NULL;
164 return c;
168 static void reconnect(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
169 char *share[NSERVERS])
171 int server, conn, f;
173 for (server=0;server<NSERVERS;server++)
174 for (conn=0;conn<NCONNECTIONS;conn++) {
175 if (cli[server][conn]) {
176 for (f=0;f<NFILES;f++) {
177 if (fnum[server][conn][f] != -1) {
178 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
179 fnum[server][conn][f] = -1;
182 talloc_free(cli[server][conn]);
184 cli[server][conn] = connect_one(share[server], server, conn);
185 if (!cli[server][conn]) {
186 DEBUG(0,("Failed to connect to %s\n", share[server]));
187 exit(1);
194 static BOOL test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
195 int fnum[NSERVERS][NCONNECTIONS][NFILES],
196 struct record *rec)
198 uint_t conn = rec->conn;
199 uint_t f = rec->f;
200 uint64_t start = rec->start;
201 uint64_t len = rec->len;
202 enum brl_type op = rec->lock_type;
203 int server;
204 BOOL ret[NSERVERS];
205 NTSTATUS status[NSERVERS];
207 switch (rec->lock_op) {
208 case OP_LOCK:
209 /* set a lock */
210 for (server=0;server<NSERVERS;server++) {
211 ret[server] = NT_STATUS_IS_OK(smbcli_lock64(cli[server][conn]->tree,
212 fnum[server][conn][f],
213 start, len, LOCK_TIMEOUT, op));
214 status[server] = smbcli_nt_error(cli[server][conn]->tree);
215 if (!exact_error_codes &&
216 NT_STATUS_EQUAL(status[server],
217 NT_STATUS_FILE_LOCK_CONFLICT)) {
218 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
221 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
222 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
223 conn, f,
224 (double)start, (double)len,
225 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
226 nt_errstr(status[0]), nt_errstr(status[1]));
228 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
229 break;
231 case OP_UNLOCK:
232 /* unset a lock */
233 for (server=0;server<NSERVERS;server++) {
234 ret[server] = NT_STATUS_IS_OK(smbcli_unlock64(cli[server][conn]->tree,
235 fnum[server][conn][f],
236 start, len));
237 status[server] = smbcli_nt_error(cli[server][conn]->tree);
239 if (showall ||
240 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
241 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
242 conn, f,
243 (double)start, (double)len,
244 nt_errstr(status[0]), nt_errstr(status[1]));
246 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
247 return False;
248 break;
250 case OP_REOPEN:
251 /* reopen the file */
252 for (server=0;server<NSERVERS;server++) {
253 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
254 fnum[server][conn][f] = -1;
256 for (server=0;server<NSERVERS;server++) {
257 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
258 O_RDWR|O_CREAT,
259 DENY_NONE);
260 if (fnum[server][conn][f] == -1) {
261 printf("failed to reopen on share%d\n", server);
262 return False;
265 if (showall) {
266 printf("reopen conn=%u f=%u\n",
267 conn, f);
269 break;
272 return True;
275 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
276 int fnum[NSERVERS][NCONNECTIONS][NFILES])
278 int server, conn, f;
280 for (server=0;server<NSERVERS;server++)
281 for (conn=0;conn<NCONNECTIONS;conn++)
282 for (f=0;f<NFILES;f++) {
283 if (fnum[server][conn][f] != -1) {
284 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
285 fnum[server][conn][f] = -1;
288 for (server=0;server<NSERVERS;server++) {
289 smbcli_unlink(cli[server][0]->tree, FILENAME);
293 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
294 int fnum[NSERVERS][NCONNECTIONS][NFILES])
296 int server, conn, f;
298 for (server=0;server<NSERVERS;server++)
299 for (conn=0;conn<NCONNECTIONS;conn++)
300 for (f=0;f<NFILES;f++) {
301 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
302 O_RDWR|O_CREAT,
303 DENY_NONE);
304 if (fnum[server][conn][f] == -1) {
305 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
306 server, conn, f);
307 exit(1);
313 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
314 int fnum[NSERVERS][NCONNECTIONS][NFILES],
315 int n)
317 int i;
318 printf("testing %u ...\n", n);
319 for (i=0; i<n; i++) {
320 if (i && i % 100 == 0) {
321 printf("%u\n", i);
324 if (recorded[i].needed &&
325 !test_one(cli, fnum, &recorded[i])) return i;
327 return n;
331 /* each server has two connections open to it. Each connection has two file
332 descriptors open on the file - 8 file descriptors in total
334 we then do random locking ops in tamdem on the 4 fnums from each
335 server and ensure that the results match
337 static void test_locks(char *share[NSERVERS])
339 struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
340 int fnum[NSERVERS][NCONNECTIONS][NFILES];
341 int n, i, n1, skip, r1, r2;
343 ZERO_STRUCT(fnum);
344 ZERO_STRUCT(cli);
346 recorded = malloc_array_p(struct record, numops);
348 for (n=0; n<numops; n++) {
349 #if PRESETS
350 if (n < sizeof(preset) / sizeof(preset[0])) {
351 recorded[n] = preset[n];
352 } else {
353 #endif
354 recorded[n].conn = random() % NCONNECTIONS;
355 recorded[n].f = random() % NFILES;
356 recorded[n].start = lock_base + ((uint_t)random() % (lock_range-1));
357 recorded[n].len = min_length +
358 random() % (lock_range-(recorded[n].start-lock_base));
359 recorded[n].start *= RANGE_MULTIPLE;
360 recorded[n].len *= RANGE_MULTIPLE;
361 r1 = random() % 100;
362 r2 = random() % 100;
363 if (r1 < READ_PCT) {
364 recorded[n].lock_type = READ_LOCK;
365 } else {
366 recorded[n].lock_type = WRITE_LOCK;
368 if (r2 < LOCK_PCT) {
369 recorded[n].lock_op = OP_LOCK;
370 } else if (r2 < UNLOCK_PCT) {
371 recorded[n].lock_op = OP_UNLOCK;
372 } else {
373 recorded[n].lock_op = OP_REOPEN;
375 recorded[n].needed = True;
376 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
377 recorded[n].len = 1;
379 #if PRESETS
381 #endif
384 reconnect(cli, fnum, share);
385 open_files(cli, fnum);
386 n = retest(cli, fnum, numops);
388 if (n == numops || !analyze) return;
389 n++;
391 skip = n/2;
393 while (1) {
394 n1 = n;
396 close_files(cli, fnum);
397 reconnect(cli, fnum, share);
398 open_files(cli, fnum);
400 for (i=0;i<n-skip;i+=skip) {
401 int m, j;
402 printf("excluding %d-%d\n", i, i+skip-1);
403 for (j=i;j<i+skip;j++) {
404 recorded[j].needed = False;
407 close_files(cli, fnum);
408 open_files(cli, fnum);
410 m = retest(cli, fnum, n);
411 if (m == n) {
412 for (j=i;j<i+skip;j++) {
413 recorded[j].needed = True;
415 } else {
416 if (i+(skip-1) < m) {
417 memmove(&recorded[i], &recorded[i+skip],
418 (m-(i+skip-1))*sizeof(recorded[0]));
420 n = m-(skip-1);
421 i--;
425 if (skip > 1) {
426 skip = skip/2;
427 printf("skip=%d\n", skip);
428 continue;
431 if (n1 == n) break;
434 close_files(cli, fnum);
435 reconnect(cli, fnum, share);
436 open_files(cli, fnum);
437 showall = True;
438 n1 = retest(cli, fnum, n);
439 if (n1 != n-1) {
440 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
442 close_files(cli, fnum);
444 for (i=0;i<n;i++) {
445 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
446 recorded[i].lock_op,
447 recorded[i].lock_type,
448 recorded[i].conn,
449 recorded[i].f,
450 (double)recorded[i].start,
451 (double)recorded[i].len,
452 recorded[i].needed);
458 static void usage(void)
460 printf(
461 "Usage:\n\
462 locktest //server1/share1 //server2/share2 [options..]\n\
463 options:\n\
464 -U user%%pass (may be specified twice)\n\
465 -s seed\n\
466 -o numops\n\
467 -u hide unlock fails\n\
468 -a (show all ops)\n\
469 -A analyse for minimal ops\n\
470 -O use oplocks\n\
471 -E enable exact error code checking\n\
472 -Z enable the zero/zero lock\n\
473 -R range set lock range\n\
474 -B base set lock base\n\
475 -M min set min lock length\n\
476 -l filename unclist file\n\
480 /****************************************************************************
481 main program
482 ****************************************************************************/
483 int main(int argc,char *argv[])
485 char *share[NSERVERS];
486 int opt;
487 int seed, server;
488 int username_count=0;
490 setlinebuf(stdout);
492 setup_logging("locktest", DEBUG_STDOUT);
494 if (argc < 3 || argv[1][0] == '-') {
495 usage();
496 exit(1);
499 setup_logging(argv[0], DEBUG_STDOUT);
501 for (server=0;server<NSERVERS;server++) {
502 share[server] = argv[1+server];
503 all_string_sub(share[server],"/","\\",0);
506 argc -= NSERVERS;
507 argv += NSERVERS;
509 lp_load();
511 servers[0] = cli_credentials_init(talloc_autofree_context());
512 servers[1] = cli_credentials_init(talloc_autofree_context());
513 cli_credentials_guess(servers[0]);
514 cli_credentials_guess(servers[1]);
516 seed = time(NULL);
518 while ((opt = getopt(argc, argv, "U:s:ho:aAW:OR:B:M:EZW:l:")) != EOF) {
519 switch (opt) {
520 case 'U':
521 if (username_count == 2) {
522 usage();
523 exit(1);
525 cli_credentials_parse_string(servers[username_count],
526 optarg, CRED_SPECIFIED);
527 username_count++;
528 break;
529 case 'R':
530 lock_range = strtol(optarg, NULL, 0);
531 break;
532 case 'B':
533 lock_base = strtol(optarg, NULL, 0);
534 break;
535 case 'M':
536 min_length = strtol(optarg, NULL, 0);
537 break;
538 case 's':
539 seed = atoi(optarg);
540 break;
541 case 'u':
542 hide_unlock_fails = True;
543 break;
544 case 'o':
545 numops = atoi(optarg);
546 break;
547 case 'O':
548 use_oplocks = True;
549 break;
550 case 'a':
551 showall = True;
552 break;
553 case 'A':
554 analyze = True;
555 break;
556 case 'Z':
557 zero_zero = True;
558 break;
559 case 'E':
560 exact_error_codes = True;
561 break;
562 case 'l':
563 lp_set_cmdline("torture:unclist", optarg);
564 break;
565 case 'W':
566 lp_set_cmdline("workgroup", optarg);
567 break;
568 case 'h':
569 usage();
570 exit(1);
571 default:
572 printf("Unknown option %c (%d)\n", (char)opt, opt);
573 exit(1);
577 if (username_count == 0) {
578 usage();
579 return -1;
581 if (username_count == 1) {
582 servers[1] = servers[0];
585 gensec_init();
587 argc -= optind;
588 argv += optind;
590 DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n",
591 seed, lock_base, lock_range, min_length));
592 srandom(seed);
594 test_locks(share);
596 return(0);