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 "system/filesys.h"
22 #include "locking/proto.h"
24 static fstring password[2];
25 static fstring username[2];
28 static bool use_kerberos;
29 static int numops = 1000;
32 static bool hide_unlock_fails;
33 static bool use_oplocks;
34 static unsigned lock_range = 100;
35 static unsigned lock_base = 0;
36 static unsigned min_length = 0;
37 static bool exact_error_codes;
38 static bool zero_zero;
43 #define FILENAME "\\locktest.dat"
48 #define RANGE_MULTIPLE 1
50 #define NCONNECTIONS 2
52 #define LOCK_TIMEOUT 0
54 #define NASTY_POSIX_LOCK_HACK 0
56 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
58 static const char *lock_op_type(int op)
60 if (op == WRITE_LOCK) return "write";
61 else if (op == READ_LOCK) return "read";
65 static const char *lock_op_name(enum lock_op op)
67 if (op == OP_LOCK) return "lock";
68 else if (op == OP_UNLOCK) return "unlock";
74 enum brl_type lock_type;
83 static struct record preset[] = {
84 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
85 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
86 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 0, 1},
87 {OP_UNLOCK, 0 , 0, 0, 2, 0, 1},
88 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
90 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
91 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
92 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
93 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
95 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
96 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 1, 1},
97 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
98 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
100 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
101 {OP_LOCK, WRITE_LOCK, 0, 0, 1, 1, 1},
102 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
103 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
105 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
106 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
107 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
108 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
110 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
111 {OP_LOCK, READ_LOCK, 0, 0, 3, 1, 1},
112 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
113 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
118 static struct record *recorded;
120 static void print_brl(struct file_id id,
121 struct server_id pid,
122 enum brl_type lock_type,
123 enum brl_flavour lock_flav,
128 #if NASTY_POSIX_LOCK_HACK
130 static SMB_INO_T lastino;
132 if (lastino != ino) {
135 "egrep POSIX.*%u /proc/locks", (int)ino) > 0) {
144 printf("%s %s %s %.0f:%.0f(%.0f)\n",
145 procid_str_static(&pid), file_id_string_tos(&id),
146 lock_type==READ_LOCK?"R":"W",
147 (double)start, (double)start+size-1,(double)size);
152 static void show_locks(void)
154 brl_forall(print_brl, NULL);
155 /* system("cat /proc/locks"); */
159 /*****************************************************
160 return a connection to a server
161 *******************************************************/
162 static struct cli_state *connect_one(char *share, int snum)
165 struct nmb_name called, calling;
168 struct sockaddr_storage ss;
173 fstrcpy(server,share+2);
174 share = strchr_m(server,'\\');
175 if (!share) return NULL;
183 slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
185 make_nmb_name(&calling, myname, 0x0);
186 make_nmb_name(&called , server, 0x20);
191 /* have to open a new connection */
192 if (!(c=cli_initialise())) {
193 DEBUG(0,("Connection to %s failed\n", server_n));
197 status = cli_connect(c, server_n, &ss);
198 if (!NT_STATUS_IS_OK(status)) {
199 DEBUG(0,("Connection to %s failed. Error %s\n", server_n, nt_errstr(status) ));
203 c->use_kerberos = use_kerberos;
205 if (!cli_session_request(c, &calling, &called)) {
206 DEBUG(0,("session request to %s failed\n", called.name));
208 if (strcmp(called.name, "*SMBSERVER")) {
209 make_nmb_name(&called , "*SMBSERVER", 0x20);
215 DEBUG(4,(" session request ok\n"));
217 status = cli_negprot(c);
218 if (!NT_STATUS_IS_OK(status)) {
219 DEBUG(0, ("protocol negotiation failed: %s\n",
226 char *pass = getpass("Password: ");
228 fstrcpy(password[0], pass);
229 fstrcpy(password[1], pass);
234 fstrcpy(password[1], password[0]);
235 fstrcpy(username[1], username[0]);
238 status = cli_session_setup(c, username[snum],
239 password[snum], strlen(password[snum]),
240 password[snum], strlen(password[snum]),
242 if (!NT_STATUS_IS_OK(status)) {
243 DEBUG(0,("session setup failed: %s\n", nt_errstr(status)));
248 * These next two lines are needed to emulate
249 * old client behaviour for people who have
250 * scripts based on client output.
251 * QUESTION ? Do we want to have a 'client compatibility
252 * mode to turn these on/off ? JRA.
255 if (*c->server_domain || *c->server_os || *c->server_type)
256 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
257 c->server_domain,c->server_os,c->server_type));
259 DEBUG(4,(" session setup ok\n"));
261 status = cli_tcon_andx(c, share, "?????", password[snum],
262 strlen(password[snum])+1);
263 if (!NT_STATUS_IS_OK(status)) {
264 DEBUG(0,("tree connect failed: %s\n", nt_errstr(status)));
269 DEBUG(4,(" tconx ok\n"));
271 c->use_oplocks = use_oplocks;
277 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS], uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES],
278 char *share[NSERVERS])
282 for (server=0;server<NSERVERS;server++)
283 for (conn=0;conn<NCONNECTIONS;conn++) {
284 if (cli[server][conn]) {
285 for (f=0;f<NFILES;f++) {
286 if (fnum[server][conn][f] != (uint16_t)-1) {
287 cli_close(cli[server][conn], fnum[server][conn][f]);
288 fnum[server][conn][f] = (uint16_t)-1;
291 cli_ulogoff(cli[server][conn]);
292 cli_shutdown(cli[server][conn]);
294 cli[server][conn] = connect_one(share[server], server);
295 if (!cli[server][conn]) {
296 DEBUG(0,("Failed to connect to %s\n", share[server]));
304 static bool test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
305 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES],
308 unsigned conn = rec->conn;
310 uint64_t start = rec->start;
311 uint64_t len = rec->len;
312 enum brl_type op = rec->lock_type;
315 NTSTATUS status[NSERVERS];
317 switch (rec->lock_op) {
320 for (server=0;server<NSERVERS;server++) {
321 ret[server] = cli_lock64(cli[server][conn],
322 fnum[server][conn][f],
323 start, len, LOCK_TIMEOUT, op);
324 status[server] = cli_nt_error(cli[server][conn]);
325 if (!exact_error_codes &&
326 NT_STATUS_EQUAL(status[server],
327 NT_STATUS_FILE_LOCK_CONFLICT)) {
328 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
331 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
332 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
334 (double)start, (double)len,
335 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
336 nt_errstr(status[0]), nt_errstr(status[1]));
338 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
339 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
344 for (server=0;server<NSERVERS;server++) {
345 ret[server] = NT_STATUS_IS_OK(cli_unlock64(cli[server][conn],
346 fnum[server][conn][f],
348 status[server] = cli_nt_error(cli[server][conn]);
351 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
352 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
354 (double)start, (double)len,
355 nt_errstr(status[0]), nt_errstr(status[1]));
357 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
358 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
363 /* reopen the file */
364 for (server=0;server<NSERVERS;server++) {
365 cli_close(cli[server][conn], fnum[server][conn][f]);
366 fnum[server][conn][f] = (uint16_t)-1;
368 for (server=0;server<NSERVERS;server++) {
369 fnum[server][conn][f] = (uint16_t)-1;
370 if (!NT_STATUS_IS_OK(cli_open(cli[server][conn], FILENAME,
372 DENY_NONE, &fnum[server][conn][f]))) {
373 printf("failed to reopen on share%d\n", server);
378 printf("reopen conn=%u f=%u\n",
388 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
389 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES])
393 for (server=0;server<NSERVERS;server++)
394 for (conn=0;conn<NCONNECTIONS;conn++)
395 for (f=0;f<NFILES;f++) {
396 if (fnum[server][conn][f] != (uint16_t)-1) {
397 cli_close(cli[server][conn], fnum[server][conn][f]);
398 fnum[server][conn][f] = (uint16_t)-1;
401 for (server=0;server<NSERVERS;server++) {
402 cli_unlink(cli[server][0], FILENAME, aSYSTEM | aHIDDEN);
406 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
407 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES])
411 for (server=0;server<NSERVERS;server++)
412 for (conn=0;conn<NCONNECTIONS;conn++)
413 for (f=0;f<NFILES;f++) {
414 fnum[server][conn][f] = (uint16_t)-1;
415 if (!NT_STATUS_IS_OK(cli_open(cli[server][conn], FILENAME,
418 &fnum[server][conn][f]))) {
419 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
427 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
428 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES],
432 printf("testing %u ...\n", n);
433 for (i=0; i<n; i++) {
434 if (i && i % 100 == 0) {
438 if (recorded[i].needed &&
439 !test_one(cli, fnum, &recorded[i])) return i;
445 /* each server has two connections open to it. Each connection has two file
446 descriptors open on the file - 8 file descriptors in total
448 we then do random locking ops in tamdem on the 4 fnums from each
449 server and ensure that the results match
451 static void test_locks(char *share[NSERVERS])
453 struct cli_state *cli[NSERVERS][NCONNECTIONS];
454 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES];
455 int n, i, n1, skip, r1, r2;
460 recorded = SMB_MALLOC_ARRAY(struct record, numops);
462 for (n=0; n<numops; n++) {
464 if (n < sizeof(preset) / sizeof(preset[0])) {
465 recorded[n] = preset[n];
468 recorded[n].conn = random() % NCONNECTIONS;
469 recorded[n].f = random() % NFILES;
470 recorded[n].start = lock_base + ((unsigned)random() % (lock_range-1));
471 recorded[n].len = min_length +
472 random() % (lock_range-(recorded[n].start-lock_base));
473 recorded[n].start *= RANGE_MULTIPLE;
474 recorded[n].len *= RANGE_MULTIPLE;
478 recorded[n].lock_type = READ_LOCK;
480 recorded[n].lock_type = WRITE_LOCK;
483 recorded[n].lock_op = OP_LOCK;
484 } else if (r2 < UNLOCK_PCT) {
485 recorded[n].lock_op = OP_UNLOCK;
487 recorded[n].lock_op = OP_REOPEN;
489 recorded[n].needed = True;
490 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
498 reconnect(cli, fnum, share);
499 open_files(cli, fnum);
500 n = retest(cli, fnum, numops);
502 if (n == numops || !analyze) return;
510 close_files(cli, fnum);
511 reconnect(cli, fnum, share);
512 open_files(cli, fnum);
514 for (i=0;i<n-skip;i+=skip) {
516 printf("excluding %d-%d\n", i, i+skip-1);
517 for (j=i;j<i+skip;j++) {
518 recorded[j].needed = False;
521 close_files(cli, fnum);
522 open_files(cli, fnum);
524 m = retest(cli, fnum, n);
526 for (j=i;j<i+skip;j++) {
527 recorded[j].needed = True;
530 if (i+(skip-1) < m) {
531 memmove(&recorded[i], &recorded[i+skip],
532 (m-(i+skip-1))*sizeof(recorded[0]));
541 printf("skip=%d\n", skip);
548 close_files(cli, fnum);
549 reconnect(cli, fnum, share);
550 open_files(cli, fnum);
552 n1 = retest(cli, fnum, n);
554 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
556 close_files(cli, fnum);
559 printf("{%s, %s, conn = %u, file = %u, start = %.0f, len = %.0f, %u},\n",
560 lock_op_name(recorded[i].lock_op),
561 lock_op_type(recorded[i].lock_type),
564 (double)recorded[i].start,
565 (double)recorded[i].len,
572 static void usage(void)
576 locktest //server1/share1 //server2/share2 [options..]\n\
578 -U user%%pass (may be specified twice)\n\
582 -u hide unlock fails\n\
584 -A analyse for minimal ops\n\
586 -E enable exact error code checking\n\
587 -Z enable the zero/zero lock\n\
588 -R range set lock range\n\
589 -B base set lock base\n\
590 -M min set min lock length\n\
594 /****************************************************************************
596 ****************************************************************************/
597 int main(int argc,char *argv[])
599 char *share[NSERVERS];
608 if (argc < 3 || argv[1][0] == '-') {
613 setup_logging(argv[0], DEBUG_STDOUT);
615 for (server=0;server<NSERVERS;server++) {
616 share[server] = argv[1+server];
617 all_string_sub(share[server],"/","\\",0);
623 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
626 if (getenv("USER")) {
627 fstrcpy(username[0],getenv("USER"));
628 fstrcpy(username[1],getenv("USER"));
633 while ((opt = getopt(argc, argv, "U:s:ho:aAW:OkR:B:M:EZ")) != EOF) {
639 d_printf("No kerberos support compiled in\n");
646 d_printf("Max of 2 usernames\n");
649 fstrcpy(username[got_pass],optarg);
650 p = strchr_m(username[got_pass],'%');
653 fstrcpy(password[got_pass], p+1);
658 lock_range = strtol(optarg, NULL, 0);
661 lock_base = strtol(optarg, NULL, 0);
664 min_length = strtol(optarg, NULL, 0);
670 hide_unlock_fails = True;
673 numops = atoi(optarg);
688 exact_error_codes = True;
694 printf("Unknown option %c (%d)\n", (char)opt, opt);
699 if(use_kerberos && !got_user) got_pass = True;
704 DEBUG(0,("seed=%u\n", seed));