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.
25 #include "module_dummy.h"
27 static fstring password[2];
28 static fstring username[2];
31 static BOOL use_kerberos;
32 static int numops = 1000;
35 static BOOL hide_unlock_fails;
36 static BOOL use_oplocks;
37 static unsigned lock_range = 100;
38 static unsigned lock_base = 0;
39 static unsigned min_length = 0;
40 static BOOL exact_error_codes;
41 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};
60 enum brl_type lock_type;
62 SMB_BIG_UINT start, len;
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 static void print_brl(SMB_DEV_T dev, SMB_INO_T ino, int pid,
107 enum brl_type lock_type,
108 br_off start, br_off size)
110 #if NASTY_POSIX_LOCK_HACK
113 static SMB_INO_T lastino;
115 if (lastino != ino) {
116 slprintf(cmd, sizeof(cmd),
117 "egrep POSIX.*%u /proc/locks", (int)ino);
124 printf("%6d %05x:%05x %s %.0f:%.0f(%.0f)\n",
125 (int)pid, (int)dev, (int)ino,
126 lock_type==READ_LOCK?"R":"W",
127 (double)start, (double)start+size-1,(double)size);
132 static void show_locks(void)
134 brl_forall(print_brl);
135 /* system("cat /proc/locks"); */
139 /*****************************************************
140 return a connection to a server
141 *******************************************************/
142 static struct cli_state *connect_one(char *share, int snum)
145 struct nmb_name called, calling;
152 fstrcpy(server,share+2);
153 share = strchr_m(server,'\\');
154 if (!share) return NULL;
162 slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), count++);
164 make_nmb_name(&calling, myname, 0x0);
165 make_nmb_name(&called , server, 0x20);
170 /* have to open a new connection */
171 if (!(c=cli_initialise(NULL)) || !cli_connect(c, server_n, &ip)) {
172 DEBUG(0,("Connection to %s failed\n", server_n));
176 c->use_kerberos = use_kerberos;
178 if (!cli_session_request(c, &calling, &called)) {
179 DEBUG(0,("session request to %s failed\n", called.name));
181 if (strcmp(called.name, "*SMBSERVER")) {
182 make_nmb_name(&called , "*SMBSERVER", 0x20);
188 DEBUG(4,(" session request ok\n"));
190 if (!cli_negprot(c)) {
191 DEBUG(0,("protocol negotiation failed\n"));
197 char *pass = getpass("Password: ");
199 fstrcpy(password[0], pass);
200 fstrcpy(password[1], pass);
205 fstrcpy(password[1], password[0]);
206 fstrcpy(username[1], username[0]);
209 if (!cli_session_setup(c, username[snum],
210 password[snum], strlen(password[snum]),
211 password[snum], strlen(password[snum]),
213 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
218 * These next two lines are needed to emulate
219 * old client behaviour for people who have
220 * scripts based on client output.
221 * QUESTION ? Do we want to have a 'client compatibility
222 * mode to turn these on/off ? JRA.
225 if (*c->server_domain || *c->server_os || *c->server_type)
226 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
227 c->server_domain,c->server_os,c->server_type));
229 DEBUG(4,(" session setup ok\n"));
231 if (!cli_send_tconX(c, share, "?????",
232 password[snum], strlen(password[snum])+1)) {
233 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
238 DEBUG(4,(" tconx ok\n"));
240 c->use_oplocks = use_oplocks;
246 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
247 char *share[NSERVERS])
251 for (server=0;server<NSERVERS;server++)
252 for (conn=0;conn<NCONNECTIONS;conn++) {
253 if (cli[server][conn]) {
254 for (f=0;f<NFILES;f++) {
255 if (fnum[server][conn][f] != -1) {
256 cli_close(cli[server][conn], fnum[server][conn][f]);
257 fnum[server][conn][f] = -1;
260 cli_ulogoff(cli[server][conn]);
261 cli_shutdown(cli[server][conn]);
263 cli[server][conn] = connect_one(share[server], server);
264 if (!cli[server][conn]) {
265 DEBUG(0,("Failed to connect to %s\n", share[server]));
273 static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
274 int fnum[NSERVERS][NCONNECTIONS][NFILES],
277 unsigned conn = rec->conn;
279 SMB_BIG_UINT start = rec->start;
280 SMB_BIG_UINT len = rec->len;
281 enum brl_type op = rec->lock_type;
284 NTSTATUS status[NSERVERS];
286 switch (rec->lock_op) {
289 for (server=0;server<NSERVERS;server++) {
290 ret[server] = cli_lock64(cli[server][conn],
291 fnum[server][conn][f],
292 start, len, LOCK_TIMEOUT, op);
293 status[server] = cli_nt_error(cli[server][conn]);
294 if (!exact_error_codes &&
295 NT_STATUS_EQUAL(status[server],
296 NT_STATUS_FILE_LOCK_CONFLICT)) {
297 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
300 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
301 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
303 (double)start, (double)len,
304 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
305 nt_errstr(status[0]), nt_errstr(status[1]));
307 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
308 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
313 for (server=0;server<NSERVERS;server++) {
314 ret[server] = cli_unlock64(cli[server][conn],
315 fnum[server][conn][f],
317 status[server] = cli_nt_error(cli[server][conn]);
320 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
321 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
323 (double)start, (double)len,
324 nt_errstr(status[0]), nt_errstr(status[1]));
326 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
327 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
332 /* reopen the file */
333 for (server=0;server<NSERVERS;server++) {
334 cli_close(cli[server][conn], fnum[server][conn][f]);
335 fnum[server][conn][f] = -1;
337 for (server=0;server<NSERVERS;server++) {
338 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
341 if (fnum[server][conn][f] == -1) {
342 printf("failed to reopen on share%d\n", server);
347 printf("reopen conn=%u f=%u\n",
357 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
358 int fnum[NSERVERS][NCONNECTIONS][NFILES])
362 for (server=0;server<NSERVERS;server++)
363 for (conn=0;conn<NCONNECTIONS;conn++)
364 for (f=0;f<NFILES;f++) {
365 if (fnum[server][conn][f] != -1) {
366 cli_close(cli[server][conn], fnum[server][conn][f]);
367 fnum[server][conn][f] = -1;
370 for (server=0;server<NSERVERS;server++) {
371 cli_unlink(cli[server][0], FILENAME);
375 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
376 int fnum[NSERVERS][NCONNECTIONS][NFILES])
380 for (server=0;server<NSERVERS;server++)
381 for (conn=0;conn<NCONNECTIONS;conn++)
382 for (f=0;f<NFILES;f++) {
383 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
386 if (fnum[server][conn][f] == -1) {
387 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
395 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
396 int fnum[NSERVERS][NCONNECTIONS][NFILES],
400 printf("testing %u ...\n", n);
401 for (i=0; i<n; i++) {
402 if (i && i % 100 == 0) {
406 if (recorded[i].needed &&
407 !test_one(cli, fnum, &recorded[i])) return i;
413 /* each server has two connections open to it. Each connection has two file
414 descriptors open on the file - 8 file descriptors in total
416 we then do random locking ops in tamdem on the 4 fnums from each
417 server and ensure that the results match
419 static void test_locks(char *share[NSERVERS])
421 struct cli_state *cli[NSERVERS][NCONNECTIONS];
422 int fnum[NSERVERS][NCONNECTIONS][NFILES];
423 int n, i, n1, skip, r1, r2;
428 recorded = (struct record *)malloc(sizeof(*recorded) * numops);
430 for (n=0; n<numops; n++) {
432 if (n < sizeof(preset) / sizeof(preset[0])) {
433 recorded[n] = preset[n];
436 recorded[n].conn = random() % NCONNECTIONS;
437 recorded[n].f = random() % NFILES;
438 recorded[n].start = lock_base + ((unsigned)random() % (lock_range-1));
439 recorded[n].len = min_length +
440 random() % (lock_range-(recorded[n].start-lock_base));
441 recorded[n].start *= RANGE_MULTIPLE;
442 recorded[n].len *= RANGE_MULTIPLE;
446 recorded[n].lock_type = READ_LOCK;
448 recorded[n].lock_type = WRITE_LOCK;
451 recorded[n].lock_op = OP_LOCK;
452 } else if (r2 < UNLOCK_PCT) {
453 recorded[n].lock_op = OP_UNLOCK;
455 recorded[n].lock_op = OP_REOPEN;
457 recorded[n].needed = True;
458 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
466 reconnect(cli, fnum, share);
467 open_files(cli, fnum);
468 n = retest(cli, fnum, numops);
470 if (n == numops || !analyze) return;
478 close_files(cli, fnum);
479 reconnect(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(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,
540 static void usage(void)
544 locktest //server1/share1 //server2/share2 [options..]\n\
546 -U user%%pass (may be specified twice)\n\
550 -u hide unlock fails\n\
552 -A analyse for minimal ops\n\
554 -E enable exact error code checking\n\
555 -Z enable the zero/zero lock\n\
556 -R range set lock range\n\
557 -B base set lock base\n\
558 -M min set min lock length\n\
562 /****************************************************************************
564 ****************************************************************************/
565 int main(int argc,char *argv[])
567 char *share[NSERVERS];
578 if (argc < 3 || argv[1][0] == '-') {
583 setup_logging(argv[0],True);
585 for (server=0;server<NSERVERS;server++) {
586 share[server] = argv[1+server];
587 all_string_sub(share[server],"/","\\",0);
593 lp_load(dyn_CONFIGFILE,True,False,False);
596 if (getenv("USER")) {
597 fstrcpy(username[0],getenv("USER"));
598 fstrcpy(username[1],getenv("USER"));
603 while ((opt = getopt(argc, argv, "U:s:ho:aAW:OkR:B:M:EZ")) != EOF) {
609 d_printf("No kerberos support compiled in\n");
616 d_printf("Max of 2 usernames\n");
619 fstrcpy(username[got_pass],optarg);
620 p = strchr_m(username[got_pass],'%');
623 fstrcpy(password[got_pass], p+1);
628 lock_range = strtol(optarg, NULL, 0);
631 lock_base = strtol(optarg, NULL, 0);
634 min_length = strtol(optarg, NULL, 0);
640 hide_unlock_fails = True;
643 numops = atoi(optarg);
658 exact_error_codes = True;
664 printf("Unknown option %c (%d)\n", (char)opt, opt);
669 if(use_kerberos && !got_user) got_pass = True;
674 DEBUG(0,("seed=%u\n", seed));