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.
22 #include "system/filesys.h"
23 #include "system/time.h"
25 #include "auth/gensec/gensec.h"
26 #include "libcli/libcli.h"
28 static int numops = 1000;
31 static BOOL hide_unlock_fails;
32 static BOOL use_oplocks;
33 static uint_t lock_range = 100;
34 static uint_t lock_base = 0;
35 static uint_t min_length = 0;
36 static BOOL exact_error_codes;
37 static BOOL zero_zero;
39 #define FILENAME "\\locktest.dat"
44 #define RANGE_MULTIPLE 1
46 #define NCONNECTIONS 2
48 #define LOCK_TIMEOUT 0
50 #define NASTY_POSIX_LOCK_HACK 0
52 static struct cli_credentials *servers[NSERVERS];
54 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
58 enum brl_type lock_type;
67 static struct record preset[] = {
68 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
69 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
70 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 0, 1},
71 {OP_UNLOCK, 0 , 0, 0, 2, 0, 1},
72 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
74 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
75 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
76 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
77 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
79 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
80 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 1, 1},
81 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
82 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
84 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
85 {OP_LOCK, WRITE_LOCK, 0, 0, 1, 1, 1},
86 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
87 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
89 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
90 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
91 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
92 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
94 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
95 {OP_LOCK, READ_LOCK, 0, 0, 3, 1, 1},
96 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
97 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
102 static struct record *recorded;
104 /*****************************************************
105 return a connection to a server
106 *******************************************************/
107 static struct smbcli_state *connect_one(char *share, int snum, int conn)
109 struct smbcli_state *c;
110 fstring server, myname;
114 printf("connect_one(%s, %d, %d)\n", share, snum, conn);
116 fstrcpy(server,share+2);
117 share = strchr_m(server,'\\');
118 if (!share) return NULL;
123 char **unc_list = NULL;
126 p = lp_parm_string(-1, "torture", "unclist");
129 unc_list = file_lines_load(p, &num_unc_names, NULL);
130 if (!unc_list || num_unc_names <= 0) {
131 printf("Failed to load unc names list from '%s'\n", p);
135 if (!smbcli_parse_unc(unc_list[conn % num_unc_names],
137 printf("Failed to parse UNC name %s\n",
138 unc_list[conn % num_unc_names]);
147 slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), snum);
148 cli_credentials_set_workstation(servers[snum], myname, CRED_SPECIFIED);
151 printf("\\\\%s\\%s\n", server, share);
152 status = smbcli_full_connection(NULL, &c,
155 servers[snum], NULL);
156 if (!NT_STATUS_IS_OK(status)) {
159 } while (!NT_STATUS_IS_OK(status) && retries--);
161 if (!NT_STATUS_IS_OK(status)) {
169 static void reconnect(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
170 char *share[NSERVERS])
174 for (server=0;server<NSERVERS;server++)
175 for (conn=0;conn<NCONNECTIONS;conn++) {
176 if (cli[server][conn]) {
177 for (f=0;f<NFILES;f++) {
178 if (fnum[server][conn][f] != -1) {
179 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
180 fnum[server][conn][f] = -1;
183 talloc_free(cli[server][conn]);
185 cli[server][conn] = connect_one(share[server], server, conn);
186 if (!cli[server][conn]) {
187 DEBUG(0,("Failed to connect to %s\n", share[server]));
195 static BOOL test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
196 int fnum[NSERVERS][NCONNECTIONS][NFILES],
199 uint_t conn = rec->conn;
201 uint64_t start = rec->start;
202 uint64_t len = rec->len;
203 enum brl_type op = rec->lock_type;
206 NTSTATUS status[NSERVERS];
208 switch (rec->lock_op) {
211 for (server=0;server<NSERVERS;server++) {
212 ret[server] = NT_STATUS_IS_OK(smbcli_lock64(cli[server][conn]->tree,
213 fnum[server][conn][f],
214 start, len, LOCK_TIMEOUT, op));
215 status[server] = smbcli_nt_error(cli[server][conn]->tree);
216 if (!exact_error_codes &&
217 NT_STATUS_EQUAL(status[server],
218 NT_STATUS_FILE_LOCK_CONFLICT)) {
219 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
222 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
223 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
225 (double)start, (double)len,
226 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
227 nt_errstr(status[0]), nt_errstr(status[1]));
229 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
234 for (server=0;server<NSERVERS;server++) {
235 ret[server] = NT_STATUS_IS_OK(smbcli_unlock64(cli[server][conn]->tree,
236 fnum[server][conn][f],
238 status[server] = smbcli_nt_error(cli[server][conn]->tree);
241 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
242 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
244 (double)start, (double)len,
245 nt_errstr(status[0]), nt_errstr(status[1]));
247 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
252 /* reopen the file */
253 for (server=0;server<NSERVERS;server++) {
254 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
255 fnum[server][conn][f] = -1;
257 for (server=0;server<NSERVERS;server++) {
258 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
261 if (fnum[server][conn][f] == -1) {
262 printf("failed to reopen on share%d\n", server);
267 printf("reopen conn=%u f=%u\n",
276 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
277 int fnum[NSERVERS][NCONNECTIONS][NFILES])
281 for (server=0;server<NSERVERS;server++)
282 for (conn=0;conn<NCONNECTIONS;conn++)
283 for (f=0;f<NFILES;f++) {
284 if (fnum[server][conn][f] != -1) {
285 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
286 fnum[server][conn][f] = -1;
289 for (server=0;server<NSERVERS;server++) {
290 smbcli_unlink(cli[server][0]->tree, FILENAME);
294 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
295 int fnum[NSERVERS][NCONNECTIONS][NFILES])
299 for (server=0;server<NSERVERS;server++)
300 for (conn=0;conn<NCONNECTIONS;conn++)
301 for (f=0;f<NFILES;f++) {
302 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
305 if (fnum[server][conn][f] == -1) {
306 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
314 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
315 int fnum[NSERVERS][NCONNECTIONS][NFILES],
319 printf("testing %u ...\n", n);
320 for (i=0; i<n; i++) {
321 if (i && i % 100 == 0) {
325 if (recorded[i].needed &&
326 !test_one(cli, fnum, &recorded[i])) return i;
332 /* each server has two connections open to it. Each connection has two file
333 descriptors open on the file - 8 file descriptors in total
335 we then do random locking ops in tamdem on the 4 fnums from each
336 server and ensure that the results match
338 static void test_locks(char *share[NSERVERS])
340 struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
341 int fnum[NSERVERS][NCONNECTIONS][NFILES];
342 int n, i, n1, skip, r1, r2;
347 recorded = malloc_array_p(struct record, numops);
349 for (n=0; n<numops; n++) {
351 if (n < sizeof(preset) / sizeof(preset[0])) {
352 recorded[n] = preset[n];
355 recorded[n].conn = random() % NCONNECTIONS;
356 recorded[n].f = random() % NFILES;
357 recorded[n].start = lock_base + ((uint_t)random() % (lock_range-1));
358 recorded[n].len = min_length +
359 random() % (lock_range-(recorded[n].start-lock_base));
360 recorded[n].start *= RANGE_MULTIPLE;
361 recorded[n].len *= RANGE_MULTIPLE;
365 recorded[n].lock_type = READ_LOCK;
367 recorded[n].lock_type = WRITE_LOCK;
370 recorded[n].lock_op = OP_LOCK;
371 } else if (r2 < UNLOCK_PCT) {
372 recorded[n].lock_op = OP_UNLOCK;
374 recorded[n].lock_op = OP_REOPEN;
376 recorded[n].needed = True;
377 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
385 reconnect(cli, fnum, share);
386 open_files(cli, fnum);
387 n = retest(cli, fnum, numops);
389 if (n == numops || !analyze) return;
397 close_files(cli, fnum);
398 reconnect(cli, fnum, share);
399 open_files(cli, fnum);
401 for (i=0;i<n-skip;i+=skip) {
403 printf("excluding %d-%d\n", i, i+skip-1);
404 for (j=i;j<i+skip;j++) {
405 recorded[j].needed = False;
408 close_files(cli, fnum);
409 open_files(cli, fnum);
411 m = retest(cli, fnum, n);
413 for (j=i;j<i+skip;j++) {
414 recorded[j].needed = True;
417 if (i+(skip-1) < m) {
418 memmove(&recorded[i], &recorded[i+skip],
419 (m-(i+skip-1))*sizeof(recorded[0]));
428 printf("skip=%d\n", skip);
435 close_files(cli, fnum);
436 reconnect(cli, fnum, share);
437 open_files(cli, fnum);
439 n1 = retest(cli, fnum, n);
441 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
443 close_files(cli, fnum);
446 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
448 recorded[i].lock_type,
451 (double)recorded[i].start,
452 (double)recorded[i].len,
459 static void usage(void)
463 locktest //server1/share1 //server2/share2 [options..]\n\
465 -U user%%pass (may be specified twice)\n\
468 -u hide unlock fails\n\
470 -A analyse for minimal ops\n\
472 -E enable exact error code checking\n\
473 -Z enable the zero/zero lock\n\
474 -R range set lock range\n\
475 -B base set lock base\n\
476 -M min set min lock length\n\
477 -l filename unclist file\n\
481 /****************************************************************************
483 ****************************************************************************/
484 int main(int argc,char *argv[])
486 char *share[NSERVERS];
489 int username_count=0;
493 setup_logging("locktest", DEBUG_STDOUT);
495 if (argc < 3 || argv[1][0] == '-') {
500 setup_logging(argv[0], DEBUG_STDOUT);
502 for (server=0;server<NSERVERS;server++) {
503 share[server] = argv[1+server];
504 all_string_sub(share[server],"/","\\",0);
512 servers[0] = cli_credentials_init(talloc_autofree_context());
513 servers[1] = cli_credentials_init(talloc_autofree_context());
514 cli_credentials_guess(servers[0]);
515 cli_credentials_guess(servers[1]);
519 while ((opt = getopt(argc, argv, "U:s:ho:aAW:OR:B:M:EZW:l:")) != EOF) {
522 if (username_count == 2) {
526 cli_credentials_parse_string(servers[username_count],
527 optarg, CRED_SPECIFIED);
531 lock_range = strtol(optarg, NULL, 0);
534 lock_base = strtol(optarg, NULL, 0);
537 min_length = strtol(optarg, NULL, 0);
543 hide_unlock_fails = True;
546 numops = atoi(optarg);
561 exact_error_codes = True;
564 lp_set_cmdline("torture:unclist", optarg);
567 lp_set_cmdline("workgroup", optarg);
573 printf("Unknown option %c (%d)\n", (char)opt, opt);
578 if (username_count == 0) {
582 if (username_count == 1) {
583 servers[1] = servers[0];
591 DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n",
592 seed, lock_base, lock_range, min_length));