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/>.
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;
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;
43 #define FILENAME "\\locktest.dat"
48 #define RANGE_MULTIPLE 1
50 #define NCONNECTIONS 2
52 #define LOCK_TIMEOUT 0
54 static struct cli_credentials *servers[NSERVERS];
56 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
60 enum brl_type lock_type;
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},
105 static struct record *recorded;
107 /*****************************************************
108 return a connection to a server
109 *******************************************************/
110 static struct smbcli_state *connect_one(struct event_context *ev,
111 struct loadparm_context *lp_ctx,
113 char *share, int snum, int conn)
115 struct smbcli_state *c;
116 char *server, *myname;
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;
134 char **unc_list = NULL;
137 p = lp_parm_string(lp_ctx, NULL, "torture", "unclist");
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);
146 if (!smbcli_parse_unc(unc_list[conn % num_unc_names],
148 printf("Failed to parse UNC name %s\n",
149 unc_list[conn % num_unc_names]);
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);
162 printf("\\\\%s\\%s\n", server, share);
163 status = smbcli_full_connection(NULL, &c,
165 lp_smb_ports(lp_ctx),
168 lp_resolve_context(lp_ctx),
169 ev, &options, &session_options);
170 if (!NT_STATUS_IS_OK(status)) {
173 } while (!NT_STATUS_IS_OK(status) && retries--);
175 if (!NT_STATUS_IS_OK(status)) {
183 static void reconnect(struct event_context *ev,
184 struct loadparm_context *lp_ctx,
186 struct smbcli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
187 char *share[NSERVERS])
191 for (server=0;server<NSERVERS;server++)
192 for (conn=0;conn<NCONNECTIONS;conn++) {
193 if (cli[server][conn]) {
194 for (f=0;f<NFILES;f++) {
195 if (fnum[server][conn][f] != -1) {
196 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
197 fnum[server][conn][f] = -1;
200 talloc_free(cli[server][conn]);
202 cli[server][conn] = connect_one(ev, lp_ctx, mem_ctx, share[server],
204 if (!cli[server][conn]) {
205 DEBUG(0,("Failed to connect to %s\n", share[server]));
213 static bool test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
214 int fnum[NSERVERS][NCONNECTIONS][NFILES],
217 uint_t conn = rec->conn;
219 uint64_t start = rec->start;
220 uint64_t len = rec->len;
221 enum brl_type op = rec->lock_type;
224 NTSTATUS status[NSERVERS];
226 switch (rec->lock_op) {
229 for (server=0;server<NSERVERS;server++) {
231 struct smbcli_tree *tree=cli[server][conn]->tree;
232 int fn=fnum[server][conn][f];
234 if (!(tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
235 res=smbcli_lock(tree, fn, start, len, LOCK_TIMEOUT, rec->lock_op);
237 union smb_lock parms;
239 struct smb_lock_entry lock[1];
241 parms.lockx.level = RAW_LOCK_LOCKX;
242 parms.lockx.in.file.fnum = fn;
244 ltype = (rec->lock_op == READ_LOCK? 1 : 0);
245 ltype |= LOCKING_ANDX_LARGE_FILES;
246 parms.lockx.in.mode = ltype;
247 parms.lockx.in.timeout = LOCK_TIMEOUT;
248 parms.lockx.in.ulock_cnt = 0;
249 parms.lockx.in.lock_cnt = 1;
250 lock[0].pid = rec->pid;
251 lock[0].offset = start;
253 parms.lockx.in.locks = &lock[0];
255 res = smb_raw_lock(tree, &parms);
258 ret[server] = NT_STATUS_IS_OK(res);
259 status[server] = smbcli_nt_error(cli[server][conn]->tree);
260 if (!exact_error_codes &&
261 NT_STATUS_EQUAL(status[server],
262 NT_STATUS_FILE_LOCK_CONFLICT)) {
263 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
266 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
267 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
269 (double)start, (double)len,
270 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
271 nt_errstr(status[0]), nt_errstr(status[1]));
273 if (!NT_STATUS_EQUAL(status[0],status[1])) return false;
278 for (server=0;server<NSERVERS;server++) {
280 struct smbcli_tree *tree=cli[server][conn]->tree;
281 int fn=fnum[server][conn][f];
284 if (!(tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
285 res=smbcli_unlock(tree, fn, start, len);
287 union smb_lock parms;
288 struct smb_lock_entry lock[1];
290 parms.lockx.level = RAW_LOCK_LOCKX;
291 parms.lockx.in.file.fnum = fn;
292 parms.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
293 parms.lockx.in.timeout = 0;
294 parms.lockx.in.ulock_cnt = 1;
295 parms.lockx.in.lock_cnt = 0;
296 lock[0].pid = rec->pid;
298 lock[0].offset = start;
299 parms.lockx.in.locks = &lock[0];
301 res = smb_raw_lock(tree, &parms);
304 ret[server] = NT_STATUS_IS_OK(res);
305 status[server] = smbcli_nt_error(cli[server][conn]->tree);
308 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
309 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
311 (double)start, (double)len,
312 nt_errstr(status[0]), nt_errstr(status[1]));
314 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
319 /* reopen the file */
320 for (server=0;server<NSERVERS;server++) {
321 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
322 fnum[server][conn][f] = -1;
324 for (server=0;server<NSERVERS;server++) {
325 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
328 if (fnum[server][conn][f] == -1) {
329 printf("failed to reopen on share%d\n", server);
334 printf("reopen conn=%u f=%u\n",
343 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
344 int fnum[NSERVERS][NCONNECTIONS][NFILES])
348 for (server=0;server<NSERVERS;server++)
349 for (conn=0;conn<NCONNECTIONS;conn++)
350 for (f=0;f<NFILES;f++) {
351 if (fnum[server][conn][f] != -1) {
352 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
353 fnum[server][conn][f] = -1;
356 for (server=0;server<NSERVERS;server++) {
357 smbcli_unlink(cli[server][0]->tree, FILENAME);
361 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
362 int fnum[NSERVERS][NCONNECTIONS][NFILES])
366 for (server=0;server<NSERVERS;server++)
367 for (conn=0;conn<NCONNECTIONS;conn++)
368 for (f=0;f<NFILES;f++) {
369 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
372 if (fnum[server][conn][f] == -1) {
373 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
381 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
382 int fnum[NSERVERS][NCONNECTIONS][NFILES],
386 printf("testing %u ...\n", n);
387 for (i=0; i<n; i++) {
388 if (i && i % 100 == 0) {
392 if (recorded[i].needed &&
393 !test_one(cli, fnum, &recorded[i])) return i;
399 /* each server has two connections open to it. Each connection has two file
400 descriptors open on the file - 8 file descriptors in total
402 we then do random locking ops in tamdem on the 4 fnums from each
403 server and ensure that the results match
405 static int test_locks(struct event_context *ev,
406 struct loadparm_context *lp_ctx,
408 char *share[NSERVERS])
410 struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
411 int fnum[NSERVERS][NCONNECTIONS][NFILES];
412 int n, i, n1, skip, r1, r2;
417 recorded = malloc_array_p(struct record, numops);
419 for (n=0; n<numops; n++) {
421 if (n < sizeof(preset) / sizeof(preset[0])) {
422 recorded[n] = preset[n];
425 recorded[n].conn = random() % NCONNECTIONS;
426 recorded[n].f = random() % NFILES;
427 recorded[n].start = lock_base + ((uint_t)random() % (lock_range-1));
428 recorded[n].len = min_length +
429 random() % (lock_range-(recorded[n].start-lock_base));
430 recorded[n].start *= RANGE_MULTIPLE;
431 recorded[n].len *= RANGE_MULTIPLE;
432 recorded[n].pid = random()%3;
433 if (recorded[n].pid == 2) {
434 recorded[n].pid = 0xFFFF; /* see if its magic */
439 recorded[n].lock_type = READ_LOCK;
441 recorded[n].lock_type = WRITE_LOCK;
444 recorded[n].lock_op = OP_LOCK;
445 } else if (r2 < UNLOCK_PCT) {
446 recorded[n].lock_op = OP_UNLOCK;
448 recorded[n].lock_op = OP_REOPEN;
450 recorded[n].needed = true;
451 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
459 reconnect(ev, lp_ctx, mem_ctx, cli, fnum, share);
460 open_files(cli, fnum);
461 n = retest(cli, fnum, numops);
463 if (n == numops || !analyze) {
476 close_files(cli, fnum);
477 reconnect(ev, lp_ctx, mem_ctx, cli, fnum, share);
478 open_files(cli, fnum);
480 for (i=0;i<n-skip;i+=skip) {
482 printf("excluding %d-%d\n", i, i+skip-1);
483 for (j=i;j<i+skip;j++) {
484 recorded[j].needed = false;
487 close_files(cli, fnum);
488 open_files(cli, fnum);
490 m = retest(cli, fnum, n);
492 for (j=i;j<i+skip;j++) {
493 recorded[j].needed = true;
496 if (i+(skip-1) < m) {
497 memmove(&recorded[i], &recorded[i+skip],
498 (m-(i+skip-1))*sizeof(recorded[0]));
507 printf("skip=%d\n", skip);
514 close_files(cli, fnum);
515 reconnect(ev, lp_ctx, mem_ctx, cli, fnum, share);
516 open_files(cli, fnum);
518 n1 = retest(cli, fnum, n);
520 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
522 close_files(cli, fnum);
525 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
527 recorded[i].lock_type,
530 (double)recorded[i].start,
531 (double)recorded[i].len,
540 static void usage(poptContext pc)
542 printf("Usage:\n\tlocktest //server1/share1 //server2/share2 [options..]\n");
543 poptPrintUsage(pc, stdout, 0);
546 /****************************************************************************
548 ****************************************************************************/
549 int main(int argc,char *argv[])
551 char *share[NSERVERS];
554 int username_count=0;
555 struct event_context *ev;
556 struct loadparm_context *lp_ctx;
560 enum {OPT_UNCLIST=1000};
561 struct poptOption long_options[] = {
563 {"seed", 0, POPT_ARG_INT, &seed, 0, "Seed to use for randomizer", NULL},
564 {"num-ops", 0, POPT_ARG_INT, &numops, 0, "num ops", NULL},
565 {"lockrange", 0, POPT_ARG_INT, &lock_range,0, "locking range", NULL},
566 {"lockbase", 0, POPT_ARG_INT, &lock_base, 0, "locking base", NULL},
567 {"minlength", 0, POPT_ARG_INT, &min_length,0, "min lock length", NULL},
568 {"hidefails", 0, POPT_ARG_NONE, &hide_unlock_fails,0,"hide unlock fails", NULL},
569 {"oplocks", 0, POPT_ARG_NONE, &use_oplocks,0, "use oplocks", NULL},
570 {"showall", 0, POPT_ARG_NONE, &showall, 0, "display all operations", NULL},
571 {"analyse", 0, POPT_ARG_NONE, &analyze, 0, "do backtrack analysis", NULL},
572 {"zerozero", 0, POPT_ARG_NONE, &zero_zero, 0, "do zero/zero lock", NULL},
573 {"exacterrors", 0, POPT_ARG_NONE, &exact_error_codes,0,"use exact error codes", NULL},
574 {"unclist", 0, POPT_ARG_STRING, NULL, OPT_UNCLIST, "unclist", NULL},
575 { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "[DOMAIN/]USERNAME[%PASSWORD]" },
577 POPT_COMMON_CONNECTION
578 POPT_COMMON_CREDENTIALS
586 pc = poptGetContext("locktest", argc, (const char **) argv, long_options,
587 POPT_CONTEXT_KEEP_FIRST);
589 poptSetOtherOptionHelp(pc, "<unc1> <unc2>");
591 lp_ctx = cmdline_lp_ctx;
592 servers[0] = cli_credentials_init(talloc_autofree_context());
593 servers[1] = cli_credentials_init(talloc_autofree_context());
594 cli_credentials_guess(servers[0], lp_ctx);
595 cli_credentials_guess(servers[1], lp_ctx);
597 while((opt = poptGetNextOpt(pc)) != -1) {
600 lp_set_cmdline(cmdline_lp_ctx, "torture:unclist", poptGetOptArg(pc));
603 if (username_count == 2) {
607 cli_credentials_parse_string(servers[username_count], poptGetOptArg(pc), CRED_SPECIFIED);
613 argv_new = discard_const_p(char *, poptGetArgs(pc));
615 for (i=0; i<argc; i++) {
616 if (argv_new[i] == NULL) {
622 if (!(argc_new >= 3)) {
627 setup_logging("locktest", DEBUG_STDOUT);
629 for (server=0;server<NSERVERS;server++) {
630 share[server] = argv_new[1+server];
631 all_string_sub(share[server],"/","\\",0);
634 lp_ctx = cmdline_lp_ctx;
636 if (username_count == 0) {
640 if (username_count == 1) {
641 servers[1] = servers[0];
644 ev = s4_event_context_init(talloc_autofree_context());
648 DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n",
649 seed, lock_base, lock_range, min_length));
652 return test_locks(ev, lp_ctx, NULL, share);