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 "libcli/resolve/resolve.h"
31 static int numops
= 1000;
34 static int hide_unlock_fails
;
35 static int use_oplocks
;
36 static unsigned int lock_range
= 100;
37 static unsigned int lock_base
= 0;
38 static unsigned int min_length
= 0;
39 static int exact_error_codes
;
42 #define FILENAME "\\locktest.dat"
47 #define RANGE_MULTIPLE 1
49 #define NCONNECTIONS 2
51 #define LOCK_TIMEOUT 0
53 static struct cli_credentials
*servers
[NSERVERS
];
55 enum lock_op
{OP_LOCK
, OP_UNLOCK
, OP_REOPEN
};
59 enum brl_type lock_type
;
69 static struct record preset
[] = {
70 {OP_LOCK
, WRITE_LOCK
, 0, 0, 2, 0, 1},
71 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
72 {OP_LOCK
, WRITE_LOCK
, 0, 0, 3, 0, 1},
73 {OP_UNLOCK
, 0 , 0, 0, 2, 0, 1},
74 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
76 {OP_LOCK
, READ_LOCK
, 0, 0, 2, 0, 1},
77 {OP_LOCK
, READ_LOCK
, 0, 0, 1, 1, 1},
78 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
79 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
81 {OP_LOCK
, READ_LOCK
, 0, 0, 2, 0, 1},
82 {OP_LOCK
, WRITE_LOCK
, 0, 0, 3, 1, 1},
83 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
84 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
86 {OP_LOCK
, READ_LOCK
, 0, 0, 2, 0, 1},
87 {OP_LOCK
, WRITE_LOCK
, 0, 0, 1, 1, 1},
88 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
89 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
91 {OP_LOCK
, WRITE_LOCK
, 0, 0, 2, 0, 1},
92 {OP_LOCK
, READ_LOCK
, 0, 0, 1, 1, 1},
93 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
94 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
96 {OP_LOCK
, WRITE_LOCK
, 0, 0, 2, 0, 1},
97 {OP_LOCK
, READ_LOCK
, 0, 0, 3, 1, 1},
98 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
99 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
104 static struct record
*recorded
;
106 /*****************************************************
107 return a connection to a server
108 *******************************************************/
109 static struct smbcli_state
*connect_one(struct tevent_context
*ev
,
110 struct loadparm_context
*lp_ctx
,
112 char *share
, int snum
, int conn
)
114 struct smbcli_state
*c
;
115 char *server
, *myname
;
118 struct smbcli_options options
;
119 struct smbcli_session_options session_options
;
121 lpcfg_smbcli_options(lp_ctx
, &options
);
122 lpcfg_smbcli_session_options(lp_ctx
, &session_options
);
124 printf("connect_one(%s, %d, %d)\n", share
, snum
, conn
);
126 server
= talloc_strdup(mem_ctx
, share
+2);
127 share
= strchr_m(server
,'\\');
128 if (!share
) return NULL
;
133 char **unc_list
= NULL
;
136 p
= lpcfg_parm_string(lp_ctx
, NULL
, "torture", "unclist");
139 unc_list
= file_lines_load(p
, &num_unc_names
, 0, NULL
);
140 if (!unc_list
|| num_unc_names
<= 0) {
141 printf("Failed to load unc names list from '%s'\n", p
);
145 if (!smbcli_parse_unc(unc_list
[conn
% num_unc_names
],
147 printf("Failed to parse UNC name %s\n",
148 unc_list
[conn
% num_unc_names
]);
151 server
= talloc_strdup(mem_ctx
, h
);
152 share
= talloc_strdup(mem_ctx
, s
);
157 myname
= talloc_asprintf(mem_ctx
, "lock-%d-%d", (int) getpid(), snum
);
158 cli_credentials_set_workstation(servers
[snum
], myname
, CRED_SPECIFIED
);
161 printf("\\\\%s\\%s\n", server
, share
);
162 status
= smbcli_full_connection(NULL
, &c
,
164 lpcfg_smb_ports(lp_ctx
),
166 lpcfg_socket_options(lp_ctx
),
168 lpcfg_resolve_context(lp_ctx
),
169 ev
, &options
, &session_options
,
170 lpcfg_gensec_settings(mem_ctx
, lp_ctx
));
171 if (!NT_STATUS_IS_OK(status
)) {
174 } while (!NT_STATUS_IS_OK(status
) && retries
--);
176 if (!NT_STATUS_IS_OK(status
)) {
184 static void reconnect(struct tevent_context
*ev
,
185 struct loadparm_context
*lp_ctx
,
187 struct smbcli_state
*cli
[NSERVERS
][NCONNECTIONS
], int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
],
188 char *share
[NSERVERS
])
192 for (server
=0;server
<NSERVERS
;server
++)
193 for (conn
=0;conn
<NCONNECTIONS
;conn
++) {
194 if (cli
[server
][conn
]) {
195 for (f
=0;f
<NFILES
;f
++) {
196 if (fnum
[server
][conn
][f
] != -1) {
197 smbcli_close(cli
[server
][conn
]->tree
, fnum
[server
][conn
][f
]);
198 fnum
[server
][conn
][f
] = -1;
201 talloc_free(cli
[server
][conn
]);
203 cli
[server
][conn
] = connect_one(ev
, lp_ctx
, mem_ctx
, share
[server
],
205 if (!cli
[server
][conn
]) {
206 DEBUG(0,("Failed to connect to %s\n", share
[server
]));
214 static bool test_one(struct smbcli_state
*cli
[NSERVERS
][NCONNECTIONS
],
215 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
],
218 unsigned int conn
= rec
->conn
;
219 unsigned int f
= rec
->f
;
220 uint64_t start
= rec
->start
;
221 uint64_t len
= rec
->len
;
222 enum brl_type op
= rec
->lock_type
;
224 /* bool ret[NSERVERS]; */
225 NTSTATUS status
[NSERVERS
];
227 switch (rec
->lock_op
) {
230 for (server
=0;server
<NSERVERS
;server
++) {
232 struct smbcli_tree
*tree
=cli
[server
][conn
]->tree
;
233 int fn
=fnum
[server
][conn
][f
];
235 if (!(tree
->session
->transport
->negotiate
.capabilities
& CAP_LARGE_FILES
)) {
236 res
=smbcli_lock(tree
, fn
, start
, len
, LOCK_TIMEOUT
, (enum brl_type
) rec
->lock_op
);
238 union smb_lock parms
;
240 struct smb_lock_entry lock
[1];
242 parms
.lockx
.level
= RAW_LOCK_LOCKX
;
243 parms
.lockx
.in
.file
.fnum
= fn
;
245 ltype
= (rec
->lock_type
== READ_LOCK
? 1 : 0);
246 ltype
|= LOCKING_ANDX_LARGE_FILES
;
247 parms
.lockx
.in
.mode
= ltype
;
248 parms
.lockx
.in
.timeout
= LOCK_TIMEOUT
;
249 parms
.lockx
.in
.ulock_cnt
= 0;
250 parms
.lockx
.in
.lock_cnt
= 1;
251 lock
[0].pid
= rec
->pid
;
252 lock
[0].offset
= start
;
254 parms
.lockx
.in
.locks
= &lock
[0];
256 res
= smb_raw_lock(tree
, &parms
);
259 /* ret[server] = NT_STATUS_IS_OK(res); */
260 status
[server
] = res
;
261 if (!exact_error_codes
&&
262 NT_STATUS_EQUAL(status
[server
],
263 NT_STATUS_FILE_LOCK_CONFLICT
)) {
264 status
[server
] = NT_STATUS_LOCK_NOT_GRANTED
;
267 if (showall
|| !NT_STATUS_EQUAL(status
[0],status
[1])) {
268 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
270 (double)start
, (double)len
,
271 op
==READ_LOCK
?"READ_LOCK":"WRITE_LOCK",
272 nt_errstr(status
[0]), nt_errstr(status
[1]));
274 if (!NT_STATUS_EQUAL(status
[0],status
[1])) return false;
279 for (server
=0;server
<NSERVERS
;server
++) {
281 struct smbcli_tree
*tree
=cli
[server
][conn
]->tree
;
282 int fn
=fnum
[server
][conn
][f
];
285 if (!(tree
->session
->transport
->negotiate
.capabilities
& CAP_LARGE_FILES
)) {
286 res
=smbcli_unlock(tree
, fn
, start
, len
);
288 union smb_lock parms
;
289 struct smb_lock_entry lock
[1];
291 parms
.lockx
.level
= RAW_LOCK_LOCKX
;
292 parms
.lockx
.in
.file
.fnum
= fn
;
293 parms
.lockx
.in
.mode
= LOCKING_ANDX_LARGE_FILES
;
294 parms
.lockx
.in
.timeout
= 0;
295 parms
.lockx
.in
.ulock_cnt
= 1;
296 parms
.lockx
.in
.lock_cnt
= 0;
297 lock
[0].pid
= rec
->pid
;
299 lock
[0].offset
= start
;
300 parms
.lockx
.in
.locks
= &lock
[0];
302 res
= smb_raw_lock(tree
, &parms
);
305 /* ret[server] = NT_STATUS_IS_OK(res); */
306 status
[server
] = res
;
309 (!hide_unlock_fails
&& !NT_STATUS_EQUAL(status
[0],status
[1]))) {
310 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
312 (double)start
, (double)len
,
313 nt_errstr(status
[0]), nt_errstr(status
[1]));
315 if (!hide_unlock_fails
&& !NT_STATUS_EQUAL(status
[0],status
[1]))
320 /* reopen the file */
321 for (server
=0;server
<NSERVERS
;server
++) {
322 smbcli_close(cli
[server
][conn
]->tree
, fnum
[server
][conn
][f
]);
323 fnum
[server
][conn
][f
] = -1;
325 for (server
=0;server
<NSERVERS
;server
++) {
326 fnum
[server
][conn
][f
] = smbcli_open(cli
[server
][conn
]->tree
, FILENAME
,
329 if (fnum
[server
][conn
][f
] == -1) {
330 printf("failed to reopen on share%d\n", server
);
335 printf("reopen conn=%u f=%u\n",
344 static void close_files(struct smbcli_state
*cli
[NSERVERS
][NCONNECTIONS
],
345 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
])
349 for (server
=0;server
<NSERVERS
;server
++)
350 for (conn
=0;conn
<NCONNECTIONS
;conn
++)
351 for (f
=0;f
<NFILES
;f
++) {
352 if (fnum
[server
][conn
][f
] != -1) {
353 smbcli_close(cli
[server
][conn
]->tree
, fnum
[server
][conn
][f
]);
354 fnum
[server
][conn
][f
] = -1;
357 for (server
=0;server
<NSERVERS
;server
++) {
358 smbcli_unlink(cli
[server
][0]->tree
, FILENAME
);
362 static void open_files(struct smbcli_state
*cli
[NSERVERS
][NCONNECTIONS
],
363 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
])
367 for (server
=0;server
<NSERVERS
;server
++)
368 for (conn
=0;conn
<NCONNECTIONS
;conn
++)
369 for (f
=0;f
<NFILES
;f
++) {
370 fnum
[server
][conn
][f
] = smbcli_open(cli
[server
][conn
]->tree
, FILENAME
,
373 if (fnum
[server
][conn
][f
] == -1) {
374 fprintf(stderr
,"Failed to open fnum[%u][%u][%u]\n",
382 static int retest(struct smbcli_state
*cli
[NSERVERS
][NCONNECTIONS
],
383 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
],
387 printf("Testing %u ...\n", n
);
388 for (i
=0; i
<n
; i
++) {
389 if (i
&& i
% 100 == 0) {
393 if (recorded
[i
].needed
&&
394 !test_one(cli
, fnum
, &recorded
[i
])) return i
;
400 /* each server has two connections open to it. Each connection has two file
401 descriptors open on the file - 8 file descriptors in total
403 we then do random locking ops in tamdem on the 4 fnums from each
404 server and ensure that the results match
406 static int test_locks(struct tevent_context
*ev
,
407 struct loadparm_context
*lp_ctx
,
409 char *share
[NSERVERS
])
411 struct smbcli_state
*cli
[NSERVERS
][NCONNECTIONS
];
412 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
];
413 int n
, i
, n1
, skip
, r1
, r2
;
418 recorded
= malloc_array_p(struct record
, numops
);
420 for (n
=0; n
<numops
; n
++) {
422 if (n
< sizeof(preset
) / sizeof(preset
[0])) {
423 recorded
[n
] = preset
[n
];
426 recorded
[n
].conn
= random() % NCONNECTIONS
;
427 recorded
[n
].f
= random() % NFILES
;
428 recorded
[n
].start
= lock_base
+ ((unsigned int)random() % (lock_range
-1));
429 recorded
[n
].len
= min_length
+
430 random() % (lock_range
-(recorded
[n
].start
-lock_base
));
431 recorded
[n
].start
*= RANGE_MULTIPLE
;
432 recorded
[n
].len
*= RANGE_MULTIPLE
;
433 recorded
[n
].pid
= random()%3;
434 if (recorded
[n
].pid
== 2) {
435 recorded
[n
].pid
= 0xFFFF; /* see if its magic */
440 recorded
[n
].lock_type
= READ_LOCK
;
442 recorded
[n
].lock_type
= WRITE_LOCK
;
445 recorded
[n
].lock_op
= OP_LOCK
;
446 } else if (r2
< UNLOCK_PCT
) {
447 recorded
[n
].lock_op
= OP_UNLOCK
;
449 recorded
[n
].lock_op
= OP_REOPEN
;
451 recorded
[n
].needed
= true;
452 if (!zero_zero
&& recorded
[n
].start
==0 && recorded
[n
].len
==0) {
460 reconnect(ev
, lp_ctx
, mem_ctx
, cli
, fnum
, share
);
461 open_files(cli
, fnum
);
462 n
= retest(cli
, fnum
, numops
);
464 if (n
== numops
|| !analyze
) {
477 close_files(cli
, fnum
);
478 reconnect(ev
, lp_ctx
, mem_ctx
, cli
, fnum
, share
);
479 open_files(cli
, fnum
);
481 for (i
=0;i
<n
-skip
;i
+=skip
) {
483 printf("excluding %d-%d\n", i
, i
+skip
-1);
484 for (j
=i
;j
<i
+skip
;j
++) {
485 recorded
[j
].needed
= false;
488 close_files(cli
, fnum
);
489 open_files(cli
, fnum
);
491 m
= retest(cli
, fnum
, n
);
493 for (j
=i
;j
<i
+skip
;j
++) {
494 recorded
[j
].needed
= true;
497 if (i
+(skip
-1) < m
) {
498 memmove(&recorded
[i
], &recorded
[i
+skip
],
499 (m
-(i
+skip
-1))*sizeof(recorded
[0]));
508 printf("skip=%d\n", skip
);
515 close_files(cli
, fnum
);
516 reconnect(ev
, lp_ctx
, mem_ctx
, cli
, fnum
, share
);
517 open_files(cli
, fnum
);
519 n1
= retest(cli
, fnum
, n
);
521 printf("ERROR - inconsistent result (%u %u)\n", n1
, n
);
523 close_files(cli
, fnum
);
526 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
528 recorded
[i
].lock_type
,
531 (double)recorded
[i
].start
,
532 (double)recorded
[i
].len
,
541 static void usage(poptContext pc
)
543 printf("Usage:\n\tlocktest //server1/share1 //server2/share2 [options..]\n");
544 poptPrintUsage(pc
, stdout
, 0);
547 /****************************************************************************
549 ****************************************************************************/
550 int main(int argc
, const char *argv
[])
552 char *share
[NSERVERS
];
555 int username_count
=0;
556 struct tevent_context
*ev
;
557 struct loadparm_context
*lp_ctx
;
561 enum {OPT_UNCLIST
=1000};
562 struct poptOption long_options
[] = {
564 {"seed", 0, POPT_ARG_INT
, &seed
, 0, "Seed to use for randomizer", NULL
},
565 {"num-ops", 0, POPT_ARG_INT
, &numops
, 0, "num ops", NULL
},
566 {"lockrange", 0, POPT_ARG_INT
, &lock_range
,0, "locking range", NULL
},
567 {"lockbase", 0, POPT_ARG_INT
, &lock_base
, 0, "locking base", NULL
},
568 {"minlength", 0, POPT_ARG_INT
, &min_length
,0, "min lock length", NULL
},
569 {"hidefails", 0, POPT_ARG_NONE
, &hide_unlock_fails
,0,"hide unlock fails", NULL
},
570 {"oplocks", 0, POPT_ARG_NONE
, &use_oplocks
,0, "use oplocks", NULL
},
571 {"showall", 0, POPT_ARG_NONE
, &showall
, 0, "display all operations", NULL
},
572 {"analyse", 0, POPT_ARG_NONE
, &analyze
, 0, "do backtrack analysis", NULL
},
573 {"zerozero", 0, POPT_ARG_NONE
, &zero_zero
, 0, "do zero/zero lock", NULL
},
574 {"exacterrors", 0, POPT_ARG_NONE
, &exact_error_codes
,0,"use exact error codes", NULL
},
575 {"unclist", 0, POPT_ARG_STRING
, NULL
, OPT_UNCLIST
, "unclist", NULL
},
576 { "user", 'U', POPT_ARG_STRING
, NULL
, 'U', "Set the network username", "[DOMAIN/]USERNAME[%PASSWORD]" },
578 POPT_COMMON_CONNECTION
579 POPT_COMMON_CREDENTIALS
583 TALLOC_CTX
*mem_ctx
= NULL
;
589 mem_ctx
= talloc_named_const(NULL
, 0, "locktest_ctx");
590 if (mem_ctx
== NULL
) {
591 printf("Unable to allocate locktest_ctx\n");
595 pc
= poptGetContext("locktest", argc
, argv
, long_options
,
596 POPT_CONTEXT_KEEP_FIRST
);
598 poptSetOtherOptionHelp(pc
, "<unc1> <unc2>");
600 lp_ctx
= cmdline_lp_ctx
;
601 servers
[0] = cli_credentials_init(mem_ctx
);
602 servers
[1] = cli_credentials_init(mem_ctx
);
603 cli_credentials_guess(servers
[0], lp_ctx
);
604 cli_credentials_guess(servers
[1], lp_ctx
);
606 while((opt
= poptGetNextOpt(pc
)) != -1) {
609 lpcfg_set_cmdline(cmdline_lp_ctx
, "torture:unclist", poptGetOptArg(pc
));
612 if (username_count
== 2) {
614 talloc_free(mem_ctx
);
617 cli_credentials_parse_string(servers
[username_count
], poptGetOptArg(pc
), CRED_SPECIFIED
);
623 argv_new
= discard_const_p(char *, poptGetArgs(pc
));
625 for (i
=0; i
<argc
; i
++) {
626 if (argv_new
[i
] == NULL
) {
632 if (!(argc_new
>= 3)) {
637 setup_logging("locktest", DEBUG_STDOUT
);
639 for (server
=0;server
<NSERVERS
;server
++) {
640 share
[server
] = argv_new
[1+server
];
641 all_string_sub(share
[server
],"/","\\",0);
644 lp_ctx
= cmdline_lp_ctx
;
646 if (username_count
== 0) {
651 if (username_count
== 1) {
652 servers
[1] = servers
[0];
655 ev
= s4_event_context_init(mem_ctx
);
659 DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n",
660 seed
, lock_base
, lock_range
, min_length
));
663 ret
= test_locks(ev
, lp_ctx
, NULL
, share
);
665 talloc_free(mem_ctx
);