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/util/util_file.h"
22 #include "lib/cmdline/cmdline.h"
23 #include "lib/events/events.h"
24 #include "system/filesys.h"
25 #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 "libcli/resolve/resolve.h"
32 static int numops
= 1000;
35 static int hide_unlock_fails
;
36 static int use_oplocks
;
37 static unsigned int lock_range
= 100;
38 static unsigned int lock_base
= 0;
39 static unsigned int 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 tevent_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 lpcfg_smbcli_options(lp_ctx
, &options
);
123 lpcfg_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
= lpcfg_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-%d-%d", (int) 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 lpcfg_smb_ports(lp_ctx
),
167 lpcfg_socket_options(lp_ctx
),
169 lpcfg_resolve_context(lp_ctx
),
170 ev
, &options
, &session_options
,
171 lpcfg_gensec_settings(mem_ctx
, lp_ctx
));
172 if (!NT_STATUS_IS_OK(status
)) {
175 } while (!NT_STATUS_IS_OK(status
) && retries
--);
177 if (!NT_STATUS_IS_OK(status
)) {
185 static void reconnect(struct tevent_context
*ev
,
186 struct loadparm_context
*lp_ctx
,
188 struct smbcli_state
*cli
[NSERVERS
][NCONNECTIONS
], int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
],
189 char *share
[NSERVERS
])
193 for (server
=0;server
<NSERVERS
;server
++)
194 for (conn
=0;conn
<NCONNECTIONS
;conn
++) {
195 if (cli
[server
][conn
]) {
196 for (f
=0;f
<NFILES
;f
++) {
197 if (fnum
[server
][conn
][f
] != -1) {
198 smbcli_close(cli
[server
][conn
]->tree
, fnum
[server
][conn
][f
]);
199 fnum
[server
][conn
][f
] = -1;
202 talloc_free(cli
[server
][conn
]);
204 cli
[server
][conn
] = connect_one(ev
, lp_ctx
, mem_ctx
, share
[server
],
206 if (!cli
[server
][conn
]) {
207 DEBUG(0,("Failed to connect to %s\n", share
[server
]));
215 static bool test_one(struct smbcli_state
*cli
[NSERVERS
][NCONNECTIONS
],
216 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
],
219 unsigned int conn
= rec
->conn
;
220 unsigned int f
= rec
->f
;
221 uint64_t start
= rec
->start
;
222 uint64_t len
= rec
->len
;
223 enum brl_type op
= rec
->lock_type
;
225 /* bool ret[NSERVERS]; */
226 NTSTATUS status
[NSERVERS
];
228 switch (rec
->lock_op
) {
231 for (server
=0;server
<NSERVERS
;server
++) {
233 struct smbcli_tree
*tree
=cli
[server
][conn
]->tree
;
234 int fn
=fnum
[server
][conn
][f
];
236 if (!(tree
->session
->transport
->negotiate
.capabilities
& CAP_LARGE_FILES
)) {
237 res
=smbcli_lock(tree
, fn
, start
, len
, LOCK_TIMEOUT
, (enum brl_type
) rec
->lock_op
);
239 union smb_lock parms
;
241 struct smb_lock_entry lock
[1];
243 parms
.lockx
.level
= RAW_LOCK_LOCKX
;
244 parms
.lockx
.in
.file
.fnum
= fn
;
246 ltype
= (rec
->lock_type
== READ_LOCK
? 1 : 0);
247 ltype
|= LOCKING_ANDX_LARGE_FILES
;
248 parms
.lockx
.in
.mode
= ltype
;
249 parms
.lockx
.in
.timeout
= LOCK_TIMEOUT
;
250 parms
.lockx
.in
.ulock_cnt
= 0;
251 parms
.lockx
.in
.lock_cnt
= 1;
252 lock
[0].pid
= rec
->pid
;
253 lock
[0].offset
= start
;
255 parms
.lockx
.in
.locks
= &lock
[0];
257 res
= smb_raw_lock(tree
, &parms
);
260 /* ret[server] = NT_STATUS_IS_OK(res); */
261 status
[server
] = res
;
262 if (!exact_error_codes
&&
263 NT_STATUS_EQUAL(status
[server
],
264 NT_STATUS_FILE_LOCK_CONFLICT
)) {
265 status
[server
] = NT_STATUS_LOCK_NOT_GRANTED
;
268 if (showall
|| !NT_STATUS_EQUAL(status
[0],status
[1])) {
269 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
271 (double)start
, (double)len
,
272 op
==READ_LOCK
?"READ_LOCK":"WRITE_LOCK",
273 nt_errstr(status
[0]), nt_errstr(status
[1]));
275 if (!NT_STATUS_EQUAL(status
[0],status
[1])) return false;
280 for (server
=0;server
<NSERVERS
;server
++) {
282 struct smbcli_tree
*tree
=cli
[server
][conn
]->tree
;
283 int fn
=fnum
[server
][conn
][f
];
286 if (!(tree
->session
->transport
->negotiate
.capabilities
& CAP_LARGE_FILES
)) {
287 res
=smbcli_unlock(tree
, fn
, start
, len
);
289 union smb_lock parms
;
290 struct smb_lock_entry lock
[1];
292 parms
.lockx
.level
= RAW_LOCK_LOCKX
;
293 parms
.lockx
.in
.file
.fnum
= fn
;
294 parms
.lockx
.in
.mode
= LOCKING_ANDX_LARGE_FILES
;
295 parms
.lockx
.in
.timeout
= 0;
296 parms
.lockx
.in
.ulock_cnt
= 1;
297 parms
.lockx
.in
.lock_cnt
= 0;
298 lock
[0].pid
= rec
->pid
;
300 lock
[0].offset
= start
;
301 parms
.lockx
.in
.locks
= &lock
[0];
303 res
= smb_raw_lock(tree
, &parms
);
306 /* ret[server] = NT_STATUS_IS_OK(res); */
307 status
[server
] = res
;
310 (!hide_unlock_fails
&& !NT_STATUS_EQUAL(status
[0],status
[1]))) {
311 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
313 (double)start
, (double)len
,
314 nt_errstr(status
[0]), nt_errstr(status
[1]));
316 if (!hide_unlock_fails
&& !NT_STATUS_EQUAL(status
[0],status
[1]))
321 /* reopen the file */
322 for (server
=0;server
<NSERVERS
;server
++) {
323 smbcli_close(cli
[server
][conn
]->tree
, fnum
[server
][conn
][f
]);
324 fnum
[server
][conn
][f
] = -1;
326 for (server
=0;server
<NSERVERS
;server
++) {
327 fnum
[server
][conn
][f
] = smbcli_open(cli
[server
][conn
]->tree
, FILENAME
,
330 if (fnum
[server
][conn
][f
] == -1) {
331 printf("failed to reopen on share%d\n", server
);
336 printf("reopen conn=%u f=%u\n",
345 static void close_files(struct smbcli_state
*cli
[NSERVERS
][NCONNECTIONS
],
346 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
])
350 for (server
=0;server
<NSERVERS
;server
++)
351 for (conn
=0;conn
<NCONNECTIONS
;conn
++)
352 for (f
=0;f
<NFILES
;f
++) {
353 if (fnum
[server
][conn
][f
] != -1) {
354 smbcli_close(cli
[server
][conn
]->tree
, fnum
[server
][conn
][f
]);
355 fnum
[server
][conn
][f
] = -1;
358 for (server
=0;server
<NSERVERS
;server
++) {
359 smbcli_unlink(cli
[server
][0]->tree
, FILENAME
);
363 static void open_files(struct smbcli_state
*cli
[NSERVERS
][NCONNECTIONS
],
364 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
])
368 for (server
=0;server
<NSERVERS
;server
++)
369 for (conn
=0;conn
<NCONNECTIONS
;conn
++)
370 for (f
=0;f
<NFILES
;f
++) {
371 fnum
[server
][conn
][f
] = smbcli_open(cli
[server
][conn
]->tree
, FILENAME
,
374 if (fnum
[server
][conn
][f
] == -1) {
375 fprintf(stderr
,"Failed to open fnum[%u][%u][%u]\n",
383 static int retest(struct smbcli_state
*cli
[NSERVERS
][NCONNECTIONS
],
384 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
],
388 printf("Testing %u ...\n", n
);
389 for (i
=0; i
<n
; i
++) {
390 if (i
&& i
% 100 == 0) {
394 if (recorded
[i
].needed
&&
395 !test_one(cli
, fnum
, &recorded
[i
])) return i
;
401 /* each server has two connections open to it. Each connection has two file
402 descriptors open on the file - 8 file descriptors in total
404 we then do random locking ops in tamdem on the 4 fnums from each
405 server and ensure that the results match
407 static int test_locks(struct tevent_context
*ev
,
408 struct loadparm_context
*lp_ctx
,
410 char *share
[NSERVERS
])
412 struct smbcli_state
*cli
[NSERVERS
][NCONNECTIONS
];
413 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
];
414 int n
, i
, n1
, skip
, r1
, r2
;
419 recorded
= malloc_array_p(struct record
, numops
);
421 for (n
=0; n
<numops
; n
++) {
423 if (n
< sizeof(preset
) / sizeof(preset
[0])) {
424 recorded
[n
] = preset
[n
];
427 recorded
[n
].conn
= random() % NCONNECTIONS
;
428 recorded
[n
].f
= random() % NFILES
;
429 recorded
[n
].start
= lock_base
+ ((unsigned int)random() % (lock_range
-1));
430 recorded
[n
].len
= min_length
+
431 random() % (lock_range
-(recorded
[n
].start
-lock_base
));
432 recorded
[n
].start
*= RANGE_MULTIPLE
;
433 recorded
[n
].len
*= RANGE_MULTIPLE
;
434 recorded
[n
].pid
= random()%3;
435 if (recorded
[n
].pid
== 2) {
436 recorded
[n
].pid
= 0xFFFF; /* see if its magic */
441 recorded
[n
].lock_type
= READ_LOCK
;
443 recorded
[n
].lock_type
= WRITE_LOCK
;
446 recorded
[n
].lock_op
= OP_LOCK
;
447 } else if (r2
< UNLOCK_PCT
) {
448 recorded
[n
].lock_op
= OP_UNLOCK
;
450 recorded
[n
].lock_op
= OP_REOPEN
;
452 recorded
[n
].needed
= true;
453 if (!zero_zero
&& recorded
[n
].start
==0 && recorded
[n
].len
==0) {
461 reconnect(ev
, lp_ctx
, mem_ctx
, cli
, fnum
, share
);
462 open_files(cli
, fnum
);
463 n
= retest(cli
, fnum
, numops
);
465 if (n
== numops
|| !analyze
) {
478 close_files(cli
, fnum
);
479 reconnect(ev
, lp_ctx
, mem_ctx
, cli
, fnum
, share
);
480 open_files(cli
, fnum
);
482 for (i
=0;i
<n
-skip
;i
+=skip
) {
484 printf("excluding %d-%d\n", i
, i
+skip
-1);
485 for (j
=i
;j
<i
+skip
;j
++) {
486 recorded
[j
].needed
= false;
489 close_files(cli
, fnum
);
490 open_files(cli
, fnum
);
492 m
= retest(cli
, fnum
, n
);
494 for (j
=i
;j
<i
+skip
;j
++) {
495 recorded
[j
].needed
= true;
498 if (i
+(skip
-1) < m
) {
499 memmove(&recorded
[i
], &recorded
[i
+skip
],
500 (m
-(i
+skip
-1))*sizeof(recorded
[0]));
509 printf("skip=%d\n", skip
);
516 close_files(cli
, fnum
);
517 reconnect(ev
, lp_ctx
, mem_ctx
, cli
, fnum
, share
);
518 open_files(cli
, fnum
);
520 n1
= retest(cli
, fnum
, n
);
522 printf("ERROR - inconsistent result (%u %u)\n", n1
, n
);
524 close_files(cli
, fnum
);
527 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
529 recorded
[i
].lock_type
,
532 (double)recorded
[i
].start
,
533 (double)recorded
[i
].len
,
542 static void usage(poptContext pc
)
544 printf("Usage:\n\tlocktest //server1/share1 //server2/share2 [options..]\n");
545 poptPrintUsage(pc
, stdout
, 0);
548 /****************************************************************************
550 ****************************************************************************/
551 int main(int argc
, const char *argv
[])
553 char *share
[NSERVERS
];
556 int username_count
=0;
557 struct tevent_context
*ev
;
558 struct loadparm_context
*lp_ctx
;
567 struct poptOption long_options
[] = {
569 {"seed", 0, POPT_ARG_INT
, &seed
, 0, "Seed to use for randomizer", NULL
},
570 {"num-ops", 0, POPT_ARG_INT
, &numops
, 0, "num ops", NULL
},
571 {"lockrange", 0, POPT_ARG_INT
, &lock_range
,0, "locking range", NULL
},
572 {"lockbase", 0, POPT_ARG_INT
, &lock_base
, 0, "locking base", NULL
},
573 {"minlength", 0, POPT_ARG_INT
, &min_length
,0, "min lock length", NULL
},
574 {"hidefails", 0, POPT_ARG_NONE
, &hide_unlock_fails
,0,"hide unlock fails", NULL
},
575 {"oplocks", 0, POPT_ARG_NONE
, &use_oplocks
,0, "use oplocks", NULL
},
576 {"showall", 0, POPT_ARG_NONE
, &showall
, 0, "display all operations", NULL
},
577 {"analyse", 0, POPT_ARG_NONE
, &analyze
, 0, "do backtrack analysis", NULL
},
578 {"zerozero", 0, POPT_ARG_NONE
, &zero_zero
, 0, "do zero/zero lock", NULL
},
579 {"exacterrors", 0, POPT_ARG_NONE
, &exact_error_codes
,0,"use exact error codes", NULL
},
580 {"unclist", 0, POPT_ARG_STRING
, NULL
, OPT_UNCLIST
, "unclist", NULL
},
581 {"user1", 0, POPT_ARG_STRING
, NULL
, OPT_USER1
, "Set first network username", "[DOMAIN/]USERNAME[%PASSWORD]" },
582 {"user2", 0, POPT_ARG_STRING
, NULL
, OPT_USER2
, "Set second network username", "[DOMAIN/]USERNAME[%PASSWORD]" },
584 POPT_COMMON_CONNECTION
585 POPT_COMMON_CREDENTIALS
590 TALLOC_CTX
*mem_ctx
= NULL
;
597 mem_ctx
= talloc_named_const(NULL
, 0, "locktest_ctx");
598 if (mem_ctx
== NULL
) {
599 printf("Unable to allocate locktest_ctx\n");
603 ok
= samba_cmdline_init(mem_ctx
,
604 SAMBA_CMDLINE_CONFIG_CLIENT
,
605 false /* require_smbconf */);
607 DBG_ERR("Failed to init cmdline parser!\n");
608 TALLOC_FREE(mem_ctx
);
613 pc
= samba_popt_get_context("locktest",
617 POPT_CONTEXT_KEEP_FIRST
);
619 DBG_ERR("Failed to setup popt context!\n");
620 TALLOC_FREE(mem_ctx
);
624 poptSetOtherOptionHelp(pc
, "<unc1> <unc2>");
626 lp_ctx
= samba_cmdline_get_lp_ctx();
628 servers
[0] = cli_credentials_init(mem_ctx
);
629 servers
[1] = cli_credentials_init(mem_ctx
);
630 cli_credentials_guess(servers
[0], lp_ctx
);
631 cli_credentials_guess(servers
[1], lp_ctx
);
633 while((opt
= poptGetNextOpt(pc
)) != -1) {
636 lpcfg_set_cmdline(lp_ctx
, "torture:unclist", poptGetOptArg(pc
));
639 cli_credentials_parse_string(servers
[0],
645 cli_credentials_parse_string(servers
[1],
650 case POPT_ERROR_BADOPT
:
651 fprintf(stderr
, "\nInvalid option %s: %s\n\n",
652 poptBadOption(pc
, 0), poptStrerror(opt
));
653 poptPrintUsage(pc
, stderr
, 0);
658 argv_new
= discard_const_p(char *, poptGetArgs(pc
));
660 for (i
=0; i
<argc
; i
++) {
661 if (argv_new
[i
] == NULL
) {
667 if (!(argc_new
>= 3)) {
672 setup_logging("locktest", DEBUG_STDOUT
);
674 for (server
=0;server
<NSERVERS
;server
++) {
675 share
[server
] = argv_new
[1+server
];
676 all_string_sub(share
[server
],"/","\\",0);
679 if (username_count
== 0) {
684 if (username_count
== 1) {
685 servers
[1] = servers
[0];
688 ev
= s4_event_context_init(mem_ctx
);
692 DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n",
693 seed
, lock_base
, lock_range
, min_length
));
696 ret
= test_locks(ev
, lp_ctx
, NULL
, share
);
698 talloc_free(mem_ctx
);