s3:winbindd_reconnect: don't only reconnect on NT_STATUS_UNSUCCESSFUL
[Samba/bb.git] / source4 / torture / locktest.c
blob0a937e9638a1460cca13cc661a62155e79605713
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 3 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, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "lib/cmdline/popt_common.h"
22 #include "lib/events/events.h"
23 #include "system/filesys.h"
24 #include "system/time.h"
25 #include "auth/credentials/credentials.h"
26 #include "auth/gensec/gensec.h"
27 #include "libcli/libcli.h"
28 #include "param/param.h"
29 #include "dynconfig/dynconfig.h"
30 #include "libcli/resolve/resolve.h"
32 static int numops = 1000;
33 static int showall;
34 static int analyze;
35 static int hide_unlock_fails;
36 static int use_oplocks;
37 static uint_t lock_range = 100;
38 static uint_t lock_base = 0;
39 static uint_t min_length = 0;
40 static int exact_error_codes;
41 static int zero_zero;
43 #define FILENAME "\\locktest.dat"
45 #define READ_PCT 50
46 #define LOCK_PCT 45
47 #define UNLOCK_PCT 70
48 #define RANGE_MULTIPLE 1
49 #define NSERVERS 2
50 #define NCONNECTIONS 2
51 #define NFILES 2
52 #define LOCK_TIMEOUT 0
54 static struct cli_credentials *servers[NSERVERS];
56 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
58 struct record {
59 enum lock_op lock_op;
60 enum brl_type lock_type;
61 char conn, f;
62 uint64_t start, len;
63 char needed;
64 uint16_t pid;
67 #define PRESETS 0
69 #if PRESETS
70 static struct record preset[] = {
71 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
72 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
73 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 0, 1},
74 {OP_UNLOCK, 0 , 0, 0, 2, 0, 1},
75 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
77 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
78 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
79 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
80 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
82 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
83 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 1, 1},
84 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
85 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
87 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
88 {OP_LOCK, WRITE_LOCK, 0, 0, 1, 1, 1},
89 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
90 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
92 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
93 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
94 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
95 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
97 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
98 {OP_LOCK, READ_LOCK, 0, 0, 3, 1, 1},
99 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
100 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
103 #endif
105 static struct record *recorded;
107 /*****************************************************
108 return a connection to a server
109 *******************************************************/
110 static struct smbcli_state *connect_one(struct tevent_context *ev,
111 struct loadparm_context *lp_ctx,
112 TALLOC_CTX *mem_ctx,
113 char *share, int snum, int conn)
115 struct smbcli_state *c;
116 char *server, *myname;
117 NTSTATUS status;
118 int retries = 10;
119 struct smbcli_options options;
120 struct smbcli_session_options session_options;
122 lp_smbcli_options(lp_ctx, &options);
123 lp_smbcli_session_options(lp_ctx, &session_options);
125 printf("connect_one(%s, %d, %d)\n", share, snum, conn);
127 server = talloc_strdup(mem_ctx, share+2);
128 share = strchr_m(server,'\\');
129 if (!share) return NULL;
130 *share = 0;
131 share++;
133 if (snum == 0) {
134 char **unc_list = NULL;
135 int num_unc_names;
136 const char *p;
137 p = lp_parm_string(lp_ctx, NULL, "torture", "unclist");
138 if (p) {
139 char *h, *s;
140 unc_list = file_lines_load(p, &num_unc_names, 0, NULL);
141 if (!unc_list || num_unc_names <= 0) {
142 printf("Failed to load unc names list from '%s'\n", p);
143 exit(1);
146 if (!smbcli_parse_unc(unc_list[conn % num_unc_names],
147 NULL, &h, &s)) {
148 printf("Failed to parse UNC name %s\n",
149 unc_list[conn % num_unc_names]);
150 exit(1);
152 server = talloc_strdup(mem_ctx, h);
153 share = talloc_strdup(mem_ctx, s);
158 myname = talloc_asprintf(mem_ctx, "lock-%u-%u", getpid(), snum);
159 cli_credentials_set_workstation(servers[snum], myname, CRED_SPECIFIED);
161 do {
162 printf("\\\\%s\\%s\n", server, share);
163 status = smbcli_full_connection(NULL, &c,
164 server,
165 lp_smb_ports(lp_ctx),
166 share, NULL,
167 lp_socket_options(lp_ctx),
168 servers[snum],
169 lp_resolve_context(lp_ctx),
170 ev, &options, &session_options,
171 lp_iconv_convenience(lp_ctx),
172 lp_gensec_settings(mem_ctx, lp_ctx));
173 if (!NT_STATUS_IS_OK(status)) {
174 sleep(2);
176 } while (!NT_STATUS_IS_OK(status) && retries--);
178 if (!NT_STATUS_IS_OK(status)) {
179 return NULL;
182 return c;
186 static void reconnect(struct tevent_context *ev,
187 struct loadparm_context *lp_ctx,
188 TALLOC_CTX *mem_ctx,
189 struct smbcli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
190 char *share[NSERVERS])
192 int server, conn, f;
194 for (server=0;server<NSERVERS;server++)
195 for (conn=0;conn<NCONNECTIONS;conn++) {
196 if (cli[server][conn]) {
197 for (f=0;f<NFILES;f++) {
198 if (fnum[server][conn][f] != -1) {
199 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
200 fnum[server][conn][f] = -1;
203 talloc_free(cli[server][conn]);
205 cli[server][conn] = connect_one(ev, lp_ctx, mem_ctx, share[server],
206 server, conn);
207 if (!cli[server][conn]) {
208 DEBUG(0,("Failed to connect to %s\n", share[server]));
209 exit(1);
216 static bool test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
217 int fnum[NSERVERS][NCONNECTIONS][NFILES],
218 struct record *rec)
220 uint_t conn = rec->conn;
221 uint_t f = rec->f;
222 uint64_t start = rec->start;
223 uint64_t len = rec->len;
224 enum brl_type op = rec->lock_type;
225 int server;
226 bool ret[NSERVERS];
227 NTSTATUS status[NSERVERS];
229 switch (rec->lock_op) {
230 case OP_LOCK:
231 /* set a lock */
232 for (server=0;server<NSERVERS;server++) {
233 NTSTATUS res;
234 struct smbcli_tree *tree=cli[server][conn]->tree;
235 int fn=fnum[server][conn][f];
237 if (!(tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
238 res=smbcli_lock(tree, fn, start, len, LOCK_TIMEOUT, rec->lock_op);
239 } else {
240 union smb_lock parms;
241 int ltype;
242 struct smb_lock_entry lock[1];
244 parms.lockx.level = RAW_LOCK_LOCKX;
245 parms.lockx.in.file.fnum = fn;
247 ltype = (rec->lock_op == READ_LOCK? 1 : 0);
248 ltype |= LOCKING_ANDX_LARGE_FILES;
249 parms.lockx.in.mode = ltype;
250 parms.lockx.in.timeout = LOCK_TIMEOUT;
251 parms.lockx.in.ulock_cnt = 0;
252 parms.lockx.in.lock_cnt = 1;
253 lock[0].pid = rec->pid;
254 lock[0].offset = start;
255 lock[0].count = len;
256 parms.lockx.in.locks = &lock[0];
258 res = smb_raw_lock(tree, &parms);
261 ret[server] = NT_STATUS_IS_OK(res);
262 status[server] = smbcli_nt_error(cli[server][conn]->tree);
263 if (!exact_error_codes &&
264 NT_STATUS_EQUAL(status[server],
265 NT_STATUS_FILE_LOCK_CONFLICT)) {
266 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
269 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
270 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
271 conn, f,
272 (double)start, (double)len,
273 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
274 nt_errstr(status[0]), nt_errstr(status[1]));
276 if (!NT_STATUS_EQUAL(status[0],status[1])) return false;
277 break;
279 case OP_UNLOCK:
280 /* unset a lock */
281 for (server=0;server<NSERVERS;server++) {
282 NTSTATUS res;
283 struct smbcli_tree *tree=cli[server][conn]->tree;
284 int fn=fnum[server][conn][f];
287 if (!(tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
288 res=smbcli_unlock(tree, fn, start, len);
289 } else {
290 union smb_lock parms;
291 struct smb_lock_entry lock[1];
293 parms.lockx.level = RAW_LOCK_LOCKX;
294 parms.lockx.in.file.fnum = fn;
295 parms.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
296 parms.lockx.in.timeout = 0;
297 parms.lockx.in.ulock_cnt = 1;
298 parms.lockx.in.lock_cnt = 0;
299 lock[0].pid = rec->pid;
300 lock[0].count = len;
301 lock[0].offset = start;
302 parms.lockx.in.locks = &lock[0];
304 res = smb_raw_lock(tree, &parms);
307 ret[server] = NT_STATUS_IS_OK(res);
308 status[server] = smbcli_nt_error(cli[server][conn]->tree);
310 if (showall ||
311 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
312 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
313 conn, f,
314 (double)start, (double)len,
315 nt_errstr(status[0]), nt_errstr(status[1]));
317 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
318 return false;
319 break;
321 case OP_REOPEN:
322 /* reopen the file */
323 for (server=0;server<NSERVERS;server++) {
324 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
325 fnum[server][conn][f] = -1;
327 for (server=0;server<NSERVERS;server++) {
328 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
329 O_RDWR|O_CREAT,
330 DENY_NONE);
331 if (fnum[server][conn][f] == -1) {
332 printf("failed to reopen on share%d\n", server);
333 return false;
336 if (showall) {
337 printf("reopen conn=%u f=%u\n",
338 conn, f);
340 break;
343 return true;
346 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
347 int fnum[NSERVERS][NCONNECTIONS][NFILES])
349 int server, conn, f;
351 for (server=0;server<NSERVERS;server++)
352 for (conn=0;conn<NCONNECTIONS;conn++)
353 for (f=0;f<NFILES;f++) {
354 if (fnum[server][conn][f] != -1) {
355 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
356 fnum[server][conn][f] = -1;
359 for (server=0;server<NSERVERS;server++) {
360 smbcli_unlink(cli[server][0]->tree, FILENAME);
364 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
365 int fnum[NSERVERS][NCONNECTIONS][NFILES])
367 int server, conn, f;
369 for (server=0;server<NSERVERS;server++)
370 for (conn=0;conn<NCONNECTIONS;conn++)
371 for (f=0;f<NFILES;f++) {
372 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
373 O_RDWR|O_CREAT,
374 DENY_NONE);
375 if (fnum[server][conn][f] == -1) {
376 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
377 server, conn, f);
378 exit(1);
384 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
385 int fnum[NSERVERS][NCONNECTIONS][NFILES],
386 int n)
388 int i;
389 printf("testing %u ...\n", n);
390 for (i=0; i<n; i++) {
391 if (i && i % 100 == 0) {
392 printf("%u\n", i);
395 if (recorded[i].needed &&
396 !test_one(cli, fnum, &recorded[i])) return i;
398 return n;
402 /* each server has two connections open to it. Each connection has two file
403 descriptors open on the file - 8 file descriptors in total
405 we then do random locking ops in tamdem on the 4 fnums from each
406 server and ensure that the results match
408 static int test_locks(struct tevent_context *ev,
409 struct loadparm_context *lp_ctx,
410 TALLOC_CTX *mem_ctx,
411 char *share[NSERVERS])
413 struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
414 int fnum[NSERVERS][NCONNECTIONS][NFILES];
415 int n, i, n1, skip, r1, r2;
417 ZERO_STRUCT(fnum);
418 ZERO_STRUCT(cli);
420 recorded = malloc_array_p(struct record, numops);
422 for (n=0; n<numops; n++) {
423 #if PRESETS
424 if (n < sizeof(preset) / sizeof(preset[0])) {
425 recorded[n] = preset[n];
426 } else {
427 #endif
428 recorded[n].conn = random() % NCONNECTIONS;
429 recorded[n].f = random() % NFILES;
430 recorded[n].start = lock_base + ((uint_t)random() % (lock_range-1));
431 recorded[n].len = min_length +
432 random() % (lock_range-(recorded[n].start-lock_base));
433 recorded[n].start *= RANGE_MULTIPLE;
434 recorded[n].len *= RANGE_MULTIPLE;
435 recorded[n].pid = random()%3;
436 if (recorded[n].pid == 2) {
437 recorded[n].pid = 0xFFFF; /* see if its magic */
439 r1 = random() % 100;
440 r2 = random() % 100;
441 if (r1 < READ_PCT) {
442 recorded[n].lock_type = READ_LOCK;
443 } else {
444 recorded[n].lock_type = WRITE_LOCK;
446 if (r2 < LOCK_PCT) {
447 recorded[n].lock_op = OP_LOCK;
448 } else if (r2 < UNLOCK_PCT) {
449 recorded[n].lock_op = OP_UNLOCK;
450 } else {
451 recorded[n].lock_op = OP_REOPEN;
453 recorded[n].needed = true;
454 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
455 recorded[n].len = 1;
457 #if PRESETS
459 #endif
462 reconnect(ev, lp_ctx, mem_ctx, cli, fnum, share);
463 open_files(cli, fnum);
464 n = retest(cli, fnum, numops);
466 if (n == numops || !analyze) {
467 if (n != numops) {
468 return 1;
470 return 0;
472 n++;
474 skip = n/2;
476 while (1) {
477 n1 = n;
479 close_files(cli, fnum);
480 reconnect(ev, lp_ctx, mem_ctx, cli, fnum, share);
481 open_files(cli, fnum);
483 for (i=0;i<n-skip;i+=skip) {
484 int m, j;
485 printf("excluding %d-%d\n", i, i+skip-1);
486 for (j=i;j<i+skip;j++) {
487 recorded[j].needed = false;
490 close_files(cli, fnum);
491 open_files(cli, fnum);
493 m = retest(cli, fnum, n);
494 if (m == n) {
495 for (j=i;j<i+skip;j++) {
496 recorded[j].needed = true;
498 } else {
499 if (i+(skip-1) < m) {
500 memmove(&recorded[i], &recorded[i+skip],
501 (m-(i+skip-1))*sizeof(recorded[0]));
503 n = m-(skip-1);
504 i--;
508 if (skip > 1) {
509 skip = skip/2;
510 printf("skip=%d\n", skip);
511 continue;
514 if (n1 == n) break;
517 close_files(cli, fnum);
518 reconnect(ev, lp_ctx, mem_ctx, cli, fnum, share);
519 open_files(cli, fnum);
520 showall = true;
521 n1 = retest(cli, fnum, n);
522 if (n1 != n-1) {
523 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
525 close_files(cli, fnum);
527 for (i=0;i<n;i++) {
528 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
529 recorded[i].lock_op,
530 recorded[i].lock_type,
531 recorded[i].conn,
532 recorded[i].f,
533 (double)recorded[i].start,
534 (double)recorded[i].len,
535 recorded[i].needed);
538 return 1;
543 static void usage(poptContext pc)
545 printf("Usage:\n\tlocktest //server1/share1 //server2/share2 [options..]\n");
546 poptPrintUsage(pc, stdout, 0);
549 /****************************************************************************
550 main program
551 ****************************************************************************/
552 int main(int argc,char *argv[])
554 char *share[NSERVERS];
555 int opt;
556 int seed, server;
557 int username_count=0;
558 struct tevent_context *ev;
559 struct loadparm_context *lp_ctx;
560 poptContext pc;
561 int argc_new, i;
562 char **argv_new;
563 enum {OPT_UNCLIST=1000};
564 struct poptOption long_options[] = {
565 POPT_AUTOHELP
566 {"seed", 0, POPT_ARG_INT, &seed, 0, "Seed to use for randomizer", NULL},
567 {"num-ops", 0, POPT_ARG_INT, &numops, 0, "num ops", NULL},
568 {"lockrange", 0, POPT_ARG_INT, &lock_range,0, "locking range", NULL},
569 {"lockbase", 0, POPT_ARG_INT, &lock_base, 0, "locking base", NULL},
570 {"minlength", 0, POPT_ARG_INT, &min_length,0, "min lock length", NULL},
571 {"hidefails", 0, POPT_ARG_NONE, &hide_unlock_fails,0,"hide unlock fails", NULL},
572 {"oplocks", 0, POPT_ARG_NONE, &use_oplocks,0, "use oplocks", NULL},
573 {"showall", 0, POPT_ARG_NONE, &showall, 0, "display all operations", NULL},
574 {"analyse", 0, POPT_ARG_NONE, &analyze, 0, "do backtrack analysis", NULL},
575 {"zerozero", 0, POPT_ARG_NONE, &zero_zero, 0, "do zero/zero lock", NULL},
576 {"exacterrors", 0, POPT_ARG_NONE, &exact_error_codes,0,"use exact error codes", NULL},
577 {"unclist", 0, POPT_ARG_STRING, NULL, OPT_UNCLIST, "unclist", NULL},
578 { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "[DOMAIN/]USERNAME[%PASSWORD]" },
579 POPT_COMMON_SAMBA
580 POPT_COMMON_CONNECTION
581 POPT_COMMON_CREDENTIALS
582 POPT_COMMON_VERSION
583 { NULL }
586 setlinebuf(stdout);
587 seed = time(NULL);
589 pc = poptGetContext("locktest", argc, (const char **) argv, long_options,
590 POPT_CONTEXT_KEEP_FIRST);
592 poptSetOtherOptionHelp(pc, "<unc1> <unc2>");
594 lp_ctx = cmdline_lp_ctx;
595 servers[0] = cli_credentials_init(talloc_autofree_context());
596 servers[1] = cli_credentials_init(talloc_autofree_context());
597 cli_credentials_guess(servers[0], lp_ctx);
598 cli_credentials_guess(servers[1], lp_ctx);
600 while((opt = poptGetNextOpt(pc)) != -1) {
601 switch (opt) {
602 case OPT_UNCLIST:
603 lp_set_cmdline(cmdline_lp_ctx, "torture:unclist", poptGetOptArg(pc));
604 break;
605 case 'U':
606 if (username_count == 2) {
607 usage(pc);
608 exit(1);
610 cli_credentials_parse_string(servers[username_count], poptGetOptArg(pc), CRED_SPECIFIED);
611 username_count++;
612 break;
616 argv_new = discard_const_p(char *, poptGetArgs(pc));
617 argc_new = argc;
618 for (i=0; i<argc; i++) {
619 if (argv_new[i] == NULL) {
620 argc_new = i;
621 break;
625 if (!(argc_new >= 3)) {
626 usage(pc);
627 exit(1);
630 setup_logging("locktest", DEBUG_STDOUT);
632 for (server=0;server<NSERVERS;server++) {
633 share[server] = argv_new[1+server];
634 all_string_sub(share[server],"/","\\",0);
637 lp_ctx = cmdline_lp_ctx;
639 if (username_count == 0) {
640 usage(pc);
641 return -1;
643 if (username_count == 1) {
644 servers[1] = servers[0];
647 ev = s4_event_context_init(talloc_autofree_context());
649 gensec_init(lp_ctx);
651 DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n",
652 seed, lock_base, lock_range, min_length));
653 srandom(seed);
655 return test_locks(ev, lp_ctx, NULL, share);