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"
26 #include "auth/credentials/credentials.h"
27 #include "auth/gensec/gensec.h"
28 #include "libcli/libcli.h"
29 #include "param/param.h"
30 #include "dynconfig.h"
31 #include "libcli/resolve/resolve.h"
33 static int numops = 1000;
36 static int hide_unlock_fails;
37 static int use_oplocks;
38 static uint_t lock_range = 100;
39 static uint_t lock_base = 0;
40 static uint_t min_length = 0;
41 static int exact_error_codes;
44 #define FILENAME "\\locktest.dat"
49 #define RANGE_MULTIPLE 1
51 #define NCONNECTIONS 2
53 #define LOCK_TIMEOUT 0
55 static struct cli_credentials *servers[NSERVERS];
57 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
61 enum brl_type lock_type;
71 static struct record preset[] = {
72 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
73 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
74 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 0, 1},
75 {OP_UNLOCK, 0 , 0, 0, 2, 0, 1},
76 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
78 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
79 {OP_LOCK, READ_LOCK, 0, 0, 1, 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, 3, 1, 1},
85 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
86 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
88 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
89 {OP_LOCK, WRITE_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, 1, 1, 1},
95 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
96 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
98 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
99 {OP_LOCK, READ_LOCK, 0, 0, 3, 1, 1},
100 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
101 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
106 static struct record *recorded;
108 /*****************************************************
109 return a connection to a server
110 *******************************************************/
111 static struct smbcli_state *connect_one(struct event_context *ev,
112 struct loadparm_context *lp_ctx,
113 char *share, int snum, int conn)
115 struct smbcli_state *c;
116 fstring server, myname;
119 struct smbcli_options options;
121 lp_smbcli_options(lp_ctx, &options);
123 printf("connect_one(%s, %d, %d)\n", share, snum, conn);
125 fstrcpy(server,share+2);
126 share = strchr_m(server,'\\');
127 if (!share) return NULL;
132 char **unc_list = NULL;
135 p = lp_parm_string(lp_ctx, NULL, "torture", "unclist");
138 unc_list = file_lines_load(p, &num_unc_names, NULL);
139 if (!unc_list || num_unc_names <= 0) {
140 printf("Failed to load unc names list from '%s'\n", p);
144 if (!smbcli_parse_unc(unc_list[conn % num_unc_names],
146 printf("Failed to parse UNC name %s\n",
147 unc_list[conn % num_unc_names]);
156 slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), snum);
157 cli_credentials_set_workstation(servers[snum], myname, CRED_SPECIFIED);
160 printf("\\\\%s\\%s\n", server, share);
161 status = smbcli_full_connection(NULL, &c,
163 lp_smb_ports(lp_ctx),
166 lp_resolve_context(lp_ctx),
168 if (!NT_STATUS_IS_OK(status)) {
171 } while (!NT_STATUS_IS_OK(status) && retries--);
173 if (!NT_STATUS_IS_OK(status)) {
181 static void reconnect(struct event_context *ev,
182 struct loadparm_context *lp_ctx,
183 struct smbcli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
184 char *share[NSERVERS])
188 for (server=0;server<NSERVERS;server++)
189 for (conn=0;conn<NCONNECTIONS;conn++) {
190 if (cli[server][conn]) {
191 for (f=0;f<NFILES;f++) {
192 if (fnum[server][conn][f] != -1) {
193 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
194 fnum[server][conn][f] = -1;
197 talloc_free(cli[server][conn]);
199 cli[server][conn] = connect_one(ev, lp_ctx, share[server],
201 if (!cli[server][conn]) {
202 DEBUG(0,("Failed to connect to %s\n", share[server]));
210 static bool test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
211 int fnum[NSERVERS][NCONNECTIONS][NFILES],
214 uint_t conn = rec->conn;
216 uint64_t start = rec->start;
217 uint64_t len = rec->len;
218 enum brl_type op = rec->lock_type;
221 NTSTATUS status[NSERVERS];
223 switch (rec->lock_op) {
226 for (server=0;server<NSERVERS;server++) {
228 struct smbcli_tree *tree=cli[server][conn]->tree;
229 int fn=fnum[server][conn][f];
231 if (!(tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
232 res=smbcli_lock(tree, fn, start, len, LOCK_TIMEOUT, rec->lock_op);
234 union smb_lock parms;
236 struct smb_lock_entry lock[1];
238 parms.lockx.level = RAW_LOCK_LOCKX;
239 parms.lockx.in.file.fnum = fn;
241 ltype = (rec->lock_op == READ_LOCK? 1 : 0);
242 ltype |= LOCKING_ANDX_LARGE_FILES;
243 parms.lockx.in.mode = ltype;
244 parms.lockx.in.timeout = LOCK_TIMEOUT;
245 parms.lockx.in.ulock_cnt = 0;
246 parms.lockx.in.lock_cnt = 1;
247 lock[0].pid = rec->pid;
248 lock[0].offset = start;
250 parms.lockx.in.locks = &lock[0];
252 res = smb_raw_lock(tree, &parms);
255 ret[server] = NT_STATUS_IS_OK(res);
256 status[server] = smbcli_nt_error(cli[server][conn]->tree);
257 if (!exact_error_codes &&
258 NT_STATUS_EQUAL(status[server],
259 NT_STATUS_FILE_LOCK_CONFLICT)) {
260 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
263 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
264 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
266 (double)start, (double)len,
267 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
268 nt_errstr(status[0]), nt_errstr(status[1]));
270 if (!NT_STATUS_EQUAL(status[0],status[1])) return false;
275 for (server=0;server<NSERVERS;server++) {
277 struct smbcli_tree *tree=cli[server][conn]->tree;
278 int fn=fnum[server][conn][f];
281 if (!(tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
282 res=smbcli_unlock(tree, fn, start, len);
284 union smb_lock parms;
285 struct smb_lock_entry lock[1];
287 parms.lockx.level = RAW_LOCK_LOCKX;
288 parms.lockx.in.file.fnum = fn;
289 parms.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
290 parms.lockx.in.timeout = 0;
291 parms.lockx.in.ulock_cnt = 1;
292 parms.lockx.in.lock_cnt = 0;
293 lock[0].pid = rec->pid;
295 lock[0].offset = start;
296 parms.lockx.in.locks = &lock[0];
298 res = smb_raw_lock(tree, &parms);
301 ret[server] = NT_STATUS_IS_OK(res);
302 status[server] = smbcli_nt_error(cli[server][conn]->tree);
305 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
306 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
308 (double)start, (double)len,
309 nt_errstr(status[0]), nt_errstr(status[1]));
311 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
316 /* reopen the file */
317 for (server=0;server<NSERVERS;server++) {
318 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
319 fnum[server][conn][f] = -1;
321 for (server=0;server<NSERVERS;server++) {
322 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
325 if (fnum[server][conn][f] == -1) {
326 printf("failed to reopen on share%d\n", server);
331 printf("reopen conn=%u f=%u\n",
340 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
341 int fnum[NSERVERS][NCONNECTIONS][NFILES])
345 for (server=0;server<NSERVERS;server++)
346 for (conn=0;conn<NCONNECTIONS;conn++)
347 for (f=0;f<NFILES;f++) {
348 if (fnum[server][conn][f] != -1) {
349 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
350 fnum[server][conn][f] = -1;
353 for (server=0;server<NSERVERS;server++) {
354 smbcli_unlink(cli[server][0]->tree, FILENAME);
358 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
359 int fnum[NSERVERS][NCONNECTIONS][NFILES])
363 for (server=0;server<NSERVERS;server++)
364 for (conn=0;conn<NCONNECTIONS;conn++)
365 for (f=0;f<NFILES;f++) {
366 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
369 if (fnum[server][conn][f] == -1) {
370 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
378 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
379 int fnum[NSERVERS][NCONNECTIONS][NFILES],
383 printf("testing %u ...\n", n);
384 for (i=0; i<n; i++) {
385 if (i && i % 100 == 0) {
389 if (recorded[i].needed &&
390 !test_one(cli, fnum, &recorded[i])) return i;
396 /* each server has two connections open to it. Each connection has two file
397 descriptors open on the file - 8 file descriptors in total
399 we then do random locking ops in tamdem on the 4 fnums from each
400 server and ensure that the results match
402 static int test_locks(struct event_context *ev,
403 struct loadparm_context *lp_ctx,
404 char *share[NSERVERS])
406 struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
407 int fnum[NSERVERS][NCONNECTIONS][NFILES];
408 int n, i, n1, skip, r1, r2;
413 recorded = malloc_array_p(struct record, numops);
415 for (n=0; n<numops; n++) {
417 if (n < sizeof(preset) / sizeof(preset[0])) {
418 recorded[n] = preset[n];
421 recorded[n].conn = random() % NCONNECTIONS;
422 recorded[n].f = random() % NFILES;
423 recorded[n].start = lock_base + ((uint_t)random() % (lock_range-1));
424 recorded[n].len = min_length +
425 random() % (lock_range-(recorded[n].start-lock_base));
426 recorded[n].start *= RANGE_MULTIPLE;
427 recorded[n].len *= RANGE_MULTIPLE;
428 recorded[n].pid = random()%3;
429 if (recorded[n].pid == 2) {
430 recorded[n].pid = 0xFFFF; /* see if its magic */
435 recorded[n].lock_type = READ_LOCK;
437 recorded[n].lock_type = WRITE_LOCK;
440 recorded[n].lock_op = OP_LOCK;
441 } else if (r2 < UNLOCK_PCT) {
442 recorded[n].lock_op = OP_UNLOCK;
444 recorded[n].lock_op = OP_REOPEN;
446 recorded[n].needed = true;
447 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
455 reconnect(ev, lp_ctx, cli, fnum, share);
456 open_files(cli, fnum);
457 n = retest(cli, fnum, numops);
459 if (n == numops || !analyze) {
472 close_files(cli, fnum);
473 reconnect(ev, lp_ctx, cli, fnum, share);
474 open_files(cli, fnum);
476 for (i=0;i<n-skip;i+=skip) {
478 printf("excluding %d-%d\n", i, i+skip-1);
479 for (j=i;j<i+skip;j++) {
480 recorded[j].needed = false;
483 close_files(cli, fnum);
484 open_files(cli, fnum);
486 m = retest(cli, fnum, n);
488 for (j=i;j<i+skip;j++) {
489 recorded[j].needed = true;
492 if (i+(skip-1) < m) {
493 memmove(&recorded[i], &recorded[i+skip],
494 (m-(i+skip-1))*sizeof(recorded[0]));
503 printf("skip=%d\n", skip);
510 close_files(cli, fnum);
511 reconnect(ev, lp_ctx, cli, fnum, share);
512 open_files(cli, fnum);
514 n1 = retest(cli, fnum, n);
516 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
518 close_files(cli, fnum);
521 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
523 recorded[i].lock_type,
526 (double)recorded[i].start,
527 (double)recorded[i].len,
536 static void usage(poptContext pc)
538 printf("Usage:\n\tlocktest //server1/share1 //server2/share2 [options..]\n");
539 poptPrintUsage(pc, stdout, 0);
542 /****************************************************************************
544 ****************************************************************************/
545 int main(int argc,char *argv[])
547 char *share[NSERVERS];
550 int username_count=0;
551 struct event_context *ev;
552 struct loadparm_context *lp_ctx;
556 enum {OPT_UNCLIST=1000};
557 struct poptOption long_options[] = {
559 {"seed", 0, POPT_ARG_INT, &seed, 0, "Seed to use for randomizer", NULL},
560 {"num-ops", 0, POPT_ARG_INT, &numops, 0, "num ops", NULL},
561 {"lockrange", 0, POPT_ARG_INT, &lock_range,0, "locking range", NULL},
562 {"lockbase", 0, POPT_ARG_INT, &lock_base, 0, "locking base", NULL},
563 {"minlength", 0, POPT_ARG_INT, &min_length,0, "min lock length", NULL},
564 {"hidefails", 0, POPT_ARG_NONE, &hide_unlock_fails,0,"hide unlock fails", NULL},
565 {"oplocks", 0, POPT_ARG_NONE, &use_oplocks,0, "use oplocks", NULL},
566 {"showall", 0, POPT_ARG_NONE, &showall, 0, "display all operations", NULL},
567 {"analyse", 0, POPT_ARG_NONE, &analyze, 0, "do backtrack analysis", NULL},
568 {"zerozero", 0, POPT_ARG_NONE, &zero_zero, 0, "do zero/zero lock", NULL},
569 {"exacterrors", 0, POPT_ARG_NONE, &exact_error_codes,0,"use exact error codes", NULL},
570 {"unclist", 0, POPT_ARG_STRING, NULL, OPT_UNCLIST, "unclist", NULL},
571 { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "[DOMAIN/]USERNAME[%PASSWORD]" },
573 POPT_COMMON_CONNECTION
574 POPT_COMMON_CREDENTIALS
582 pc = poptGetContext("locktest", argc, (const char **) argv, long_options,
583 POPT_CONTEXT_KEEP_FIRST);
585 poptSetOtherOptionHelp(pc, "<unc1> <unc2>");
587 lp_ctx = cmdline_lp_ctx;
588 servers[0] = cli_credentials_init(talloc_autofree_context());
589 servers[1] = cli_credentials_init(talloc_autofree_context());
590 cli_credentials_guess(servers[0], lp_ctx);
591 cli_credentials_guess(servers[1], lp_ctx);
593 while((opt = poptGetNextOpt(pc)) != -1) {
596 lp_set_cmdline(cmdline_lp_ctx, "torture:unclist", poptGetOptArg(pc));
599 if (username_count == 2) {
603 cli_credentials_parse_string(servers[username_count], poptGetOptArg(pc), CRED_SPECIFIED);
609 argv_new = discard_const_p(char *, poptGetArgs(pc));
611 for (i=0; i<argc; i++) {
612 if (argv_new[i] == NULL) {
618 if (!(argc_new >= 3)) {
623 setup_logging("locktest", DEBUG_STDOUT);
625 for (server=0;server<NSERVERS;server++) {
626 share[server] = argv_new[1+server];
627 all_string_sub(share[server],"/","\\",0);
630 lp_ctx = cmdline_lp_ctx;
632 if (username_count == 0) {
636 if (username_count == 1) {
637 servers[1] = servers[0];
640 ev = event_context_init(talloc_autofree_context());
644 DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n",
645 seed, lock_base, lock_range, min_length));
648 return test_locks(ev, lp_ctx, share);