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.
23 static fstring password[2];
24 static fstring username[2];
27 static BOOL use_kerberos;
28 static int numops = 1000;
31 static BOOL hide_unlock_fails;
32 static BOOL use_oplocks;
33 static unsigned lock_range = 100;
34 static unsigned lock_base = 0;
35 static unsigned 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 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
54 const char *lock_op_type(int op)
56 if (op == WRITE_LOCK) return "write";
57 else if (op == READ_LOCK) return "read";
61 const char *lock_op_name(enum lock_op op)
63 if (op == OP_LOCK) return "lock";
64 else if (op == OP_UNLOCK) return "unlock";
70 enum brl_type lock_type;
72 SMB_BIG_UINT start, len;
79 static struct record preset[] = {
80 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
81 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
82 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 0, 1},
83 {OP_UNLOCK, 0 , 0, 0, 2, 0, 1},
84 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
86 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
87 {OP_LOCK, READ_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, READ_LOCK, 0, 0, 2, 0, 1},
92 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 1, 1},
93 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
94 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
96 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
97 {OP_LOCK, WRITE_LOCK, 0, 0, 1, 1, 1},
98 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
99 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
101 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
102 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
103 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
104 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
106 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
107 {OP_LOCK, READ_LOCK, 0, 0, 3, 1, 1},
108 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
109 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
114 static struct record *recorded;
116 static void print_brl(SMB_DEV_T dev, SMB_INO_T ino, struct process_id pid,
117 enum brl_type lock_type,
118 br_off start, br_off size)
120 #if NASTY_POSIX_LOCK_HACK
123 static SMB_INO_T lastino;
125 if (lastino != ino) {
126 slprintf(cmd, sizeof(cmd),
127 "egrep POSIX.*%u /proc/locks", (int)ino);
134 printf("%s %05x:%05x %s %.0f:%.0f(%.0f)\n",
135 procid_str_static(&pid), (int)dev, (int)ino,
136 lock_type==READ_LOCK?"R":"W",
137 (double)start, (double)start+size-1,(double)size);
142 static void show_locks(void)
144 brl_forall(print_brl);
145 /* system("cat /proc/locks"); */
149 /*****************************************************
150 return a connection to a server
151 *******************************************************/
152 static struct cli_state *connect_one(char *share, int snum)
155 struct nmb_name called, calling;
162 fstrcpy(server,share+2);
163 share = strchr_m(server,'\\');
164 if (!share) return NULL;
172 slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
174 make_nmb_name(&calling, myname, 0x0);
175 make_nmb_name(&called , server, 0x20);
180 /* have to open a new connection */
181 if (!(c=cli_initialise(NULL)) || !cli_connect(c, server_n, &ip)) {
182 DEBUG(0,("Connection to %s failed\n", server_n));
186 c->use_kerberos = use_kerberos;
188 if (!cli_session_request(c, &calling, &called)) {
189 DEBUG(0,("session request to %s failed\n", called.name));
191 if (strcmp(called.name, "*SMBSERVER")) {
192 make_nmb_name(&called , "*SMBSERVER", 0x20);
198 DEBUG(4,(" session request ok\n"));
200 if (!cli_negprot(c)) {
201 DEBUG(0,("protocol negotiation failed\n"));
207 char *pass = getpass("Password: ");
209 fstrcpy(password[0], pass);
210 fstrcpy(password[1], pass);
215 fstrcpy(password[1], password[0]);
216 fstrcpy(username[1], username[0]);
219 if (!cli_session_setup(c, username[snum],
220 password[snum], strlen(password[snum]),
221 password[snum], strlen(password[snum]),
223 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
228 * These next two lines are needed to emulate
229 * old client behaviour for people who have
230 * scripts based on client output.
231 * QUESTION ? Do we want to have a 'client compatibility
232 * mode to turn these on/off ? JRA.
235 if (*c->server_domain || *c->server_os || *c->server_type)
236 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
237 c->server_domain,c->server_os,c->server_type));
239 DEBUG(4,(" session setup ok\n"));
241 if (!cli_send_tconX(c, share, "?????",
242 password[snum], strlen(password[snum])+1)) {
243 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
248 DEBUG(4,(" tconx ok\n"));
250 c->use_oplocks = use_oplocks;
256 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
257 char *share[NSERVERS])
261 for (server=0;server<NSERVERS;server++)
262 for (conn=0;conn<NCONNECTIONS;conn++) {
263 if (cli[server][conn]) {
264 for (f=0;f<NFILES;f++) {
265 if (fnum[server][conn][f] != -1) {
266 cli_close(cli[server][conn], fnum[server][conn][f]);
267 fnum[server][conn][f] = -1;
270 cli_ulogoff(cli[server][conn]);
271 cli_shutdown(cli[server][conn]);
273 cli[server][conn] = connect_one(share[server], server);
274 if (!cli[server][conn]) {
275 DEBUG(0,("Failed to connect to %s\n", share[server]));
283 static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
284 int fnum[NSERVERS][NCONNECTIONS][NFILES],
287 unsigned conn = rec->conn;
289 SMB_BIG_UINT start = rec->start;
290 SMB_BIG_UINT len = rec->len;
291 enum brl_type op = rec->lock_type;
294 NTSTATUS status[NSERVERS];
296 switch (rec->lock_op) {
299 for (server=0;server<NSERVERS;server++) {
300 ret[server] = cli_lock64(cli[server][conn],
301 fnum[server][conn][f],
302 start, len, LOCK_TIMEOUT, op);
303 status[server] = cli_nt_error(cli[server][conn]);
304 if (!exact_error_codes &&
305 NT_STATUS_EQUAL(status[server],
306 NT_STATUS_FILE_LOCK_CONFLICT)) {
307 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
310 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
311 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
313 (double)start, (double)len,
314 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
315 nt_errstr(status[0]), nt_errstr(status[1]));
317 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
318 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
323 for (server=0;server<NSERVERS;server++) {
324 ret[server] = cli_unlock64(cli[server][conn],
325 fnum[server][conn][f],
327 status[server] = cli_nt_error(cli[server][conn]);
330 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
331 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
333 (double)start, (double)len,
334 nt_errstr(status[0]), nt_errstr(status[1]));
336 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
337 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
342 /* reopen the file */
343 for (server=0;server<NSERVERS;server++) {
344 cli_close(cli[server][conn], fnum[server][conn][f]);
345 fnum[server][conn][f] = -1;
347 for (server=0;server<NSERVERS;server++) {
348 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
351 if (fnum[server][conn][f] == -1) {
352 printf("failed to reopen on share%d\n", server);
357 printf("reopen conn=%u f=%u\n",
367 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
368 int fnum[NSERVERS][NCONNECTIONS][NFILES])
372 for (server=0;server<NSERVERS;server++)
373 for (conn=0;conn<NCONNECTIONS;conn++)
374 for (f=0;f<NFILES;f++) {
375 if (fnum[server][conn][f] != -1) {
376 cli_close(cli[server][conn], fnum[server][conn][f]);
377 fnum[server][conn][f] = -1;
380 for (server=0;server<NSERVERS;server++) {
381 cli_unlink(cli[server][0], FILENAME);
385 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
386 int fnum[NSERVERS][NCONNECTIONS][NFILES])
390 for (server=0;server<NSERVERS;server++)
391 for (conn=0;conn<NCONNECTIONS;conn++)
392 for (f=0;f<NFILES;f++) {
393 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
396 if (fnum[server][conn][f] == -1) {
397 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
405 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
406 int fnum[NSERVERS][NCONNECTIONS][NFILES],
410 printf("testing %u ...\n", n);
411 for (i=0; i<n; i++) {
412 if (i && i % 100 == 0) {
416 if (recorded[i].needed &&
417 !test_one(cli, fnum, &recorded[i])) return i;
423 /* each server has two connections open to it. Each connection has two file
424 descriptors open on the file - 8 file descriptors in total
426 we then do random locking ops in tamdem on the 4 fnums from each
427 server and ensure that the results match
429 static void test_locks(char *share[NSERVERS])
431 struct cli_state *cli[NSERVERS][NCONNECTIONS];
432 int fnum[NSERVERS][NCONNECTIONS][NFILES];
433 int n, i, n1, skip, r1, r2;
438 recorded = SMB_MALLOC_ARRAY(struct record, numops);
440 for (n=0; n<numops; n++) {
442 if (n < sizeof(preset) / sizeof(preset[0])) {
443 recorded[n] = preset[n];
446 recorded[n].conn = random() % NCONNECTIONS;
447 recorded[n].f = random() % NFILES;
448 recorded[n].start = lock_base + ((unsigned)random() % (lock_range-1));
449 recorded[n].len = min_length +
450 random() % (lock_range-(recorded[n].start-lock_base));
451 recorded[n].start *= RANGE_MULTIPLE;
452 recorded[n].len *= RANGE_MULTIPLE;
456 recorded[n].lock_type = READ_LOCK;
458 recorded[n].lock_type = WRITE_LOCK;
461 recorded[n].lock_op = OP_LOCK;
462 } else if (r2 < UNLOCK_PCT) {
463 recorded[n].lock_op = OP_UNLOCK;
465 recorded[n].lock_op = OP_REOPEN;
467 recorded[n].needed = True;
468 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
476 reconnect(cli, fnum, share);
477 open_files(cli, fnum);
478 n = retest(cli, fnum, numops);
480 if (n == numops || !analyze) return;
488 close_files(cli, fnum);
489 reconnect(cli, fnum, share);
490 open_files(cli, fnum);
492 for (i=0;i<n-skip;i+=skip) {
494 printf("excluding %d-%d\n", i, i+skip-1);
495 for (j=i;j<i+skip;j++) {
496 recorded[j].needed = False;
499 close_files(cli, fnum);
500 open_files(cli, fnum);
502 m = retest(cli, fnum, n);
504 for (j=i;j<i+skip;j++) {
505 recorded[j].needed = True;
508 if (i+(skip-1) < m) {
509 memmove(&recorded[i], &recorded[i+skip],
510 (m-(i+skip-1))*sizeof(recorded[0]));
519 printf("skip=%d\n", skip);
526 close_files(cli, fnum);
527 reconnect(cli, fnum, share);
528 open_files(cli, fnum);
530 n1 = retest(cli, fnum, n);
532 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
534 close_files(cli, fnum);
537 printf("{%s, %s, conn = %u, file = %u, start = %.0f, len = %.0f, %u},\n",
538 lock_op_name(recorded[i].lock_op),
539 lock_op_type(recorded[i].lock_type),
542 (double)recorded[i].start,
543 (double)recorded[i].len,
550 static void usage(void)
554 locktest //server1/share1 //server2/share2 [options..]\n\
556 -U user%%pass (may be specified twice)\n\
560 -u hide unlock fails\n\
562 -A analyse for minimal ops\n\
564 -E enable exact error code checking\n\
565 -Z enable the zero/zero lock\n\
566 -R range set lock range\n\
567 -B base set lock base\n\
568 -M min set min lock length\n\
572 /****************************************************************************
574 ****************************************************************************/
575 int main(int argc,char *argv[])
577 char *share[NSERVERS];
590 if (argc < 3 || argv[1][0] == '-') {
595 setup_logging(argv[0],True);
597 for (server=0;server<NSERVERS;server++) {
598 share[server] = argv[1+server];
599 all_string_sub(share[server],"/","\\",0);
605 lp_load(dyn_CONFIGFILE,True,False,False,True);
608 if (getenv("USER")) {
609 fstrcpy(username[0],getenv("USER"));
610 fstrcpy(username[1],getenv("USER"));
615 while ((opt = getopt(argc, argv, "U:s:ho:aAW:OkR:B:M:EZ")) != EOF) {
621 d_printf("No kerberos support compiled in\n");
628 d_printf("Max of 2 usernames\n");
631 fstrcpy(username[got_pass],optarg);
632 p = strchr_m(username[got_pass],'%');
635 fstrcpy(password[got_pass], p+1);
640 lock_range = strtol(optarg, NULL, 0);
643 lock_base = strtol(optarg, NULL, 0);
646 min_length = strtol(optarg, NULL, 0);
652 hide_unlock_fails = True;
655 numops = atoi(optarg);
670 exact_error_codes = True;
676 printf("Unknown option %c (%d)\n", (char)opt, opt);
681 if(use_kerberos && !got_user) got_pass = True;
686 DEBUG(0,("seed=%u\n", seed));