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};
56 enum brl_type lock_type;
58 SMB_BIG_UINT start, len;
65 static struct record preset[] = {
66 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
67 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
68 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 0, 1},
69 {OP_UNLOCK, 0 , 0, 0, 2, 0, 1},
70 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
72 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
73 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
74 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
75 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
77 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
78 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 1, 1},
79 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
80 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
82 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
83 {OP_LOCK, WRITE_LOCK, 0, 0, 1, 1, 1},
84 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
85 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
87 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
88 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
89 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
90 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
92 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
93 {OP_LOCK, READ_LOCK, 0, 0, 3, 1, 1},
94 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
95 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
100 static struct record *recorded;
102 static void print_brl(SMB_DEV_T dev, SMB_INO_T ino, struct process_id pid,
103 enum brl_type lock_type,
104 br_off start, br_off size)
106 #if NASTY_POSIX_LOCK_HACK
109 static SMB_INO_T lastino;
111 if (lastino != ino) {
112 slprintf(cmd, sizeof(cmd),
113 "egrep POSIX.*%u /proc/locks", (int)ino);
120 printf("%s %05x:%05x %s %.0f:%.0f(%.0f)\n",
121 procid_str_static(&pid), (int)dev, (int)ino,
122 lock_type==READ_LOCK?"R":"W",
123 (double)start, (double)start+size-1,(double)size);
128 static void show_locks(void)
130 brl_forall(print_brl);
131 /* system("cat /proc/locks"); */
135 /*****************************************************
136 return a connection to a server
137 *******************************************************/
138 static struct cli_state *connect_one(char *share, int snum)
141 struct nmb_name called, calling;
148 fstrcpy(server,share+2);
149 share = strchr_m(server,'\\');
150 if (!share) return NULL;
158 slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
160 make_nmb_name(&calling, myname, 0x0);
161 make_nmb_name(&called , server, 0x20);
166 /* have to open a new connection */
167 if (!(c=cli_initialise(NULL)) || !cli_connect(c, server_n, &ip)) {
168 DEBUG(0,("Connection to %s failed\n", server_n));
172 c->use_kerberos = use_kerberos;
174 if (!cli_session_request(c, &calling, &called)) {
175 DEBUG(0,("session request to %s failed\n", called.name));
177 if (strcmp(called.name, "*SMBSERVER")) {
178 make_nmb_name(&called , "*SMBSERVER", 0x20);
184 DEBUG(4,(" session request ok\n"));
186 if (!cli_negprot(c)) {
187 DEBUG(0,("protocol negotiation failed\n"));
193 char *pass = getpass("Password: ");
195 fstrcpy(password[0], pass);
196 fstrcpy(password[1], pass);
201 fstrcpy(password[1], password[0]);
202 fstrcpy(username[1], username[0]);
205 if (!cli_session_setup(c, username[snum],
206 password[snum], strlen(password[snum]),
207 password[snum], strlen(password[snum]),
209 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
214 * These next two lines are needed to emulate
215 * old client behaviour for people who have
216 * scripts based on client output.
217 * QUESTION ? Do we want to have a 'client compatibility
218 * mode to turn these on/off ? JRA.
221 if (*c->server_domain || *c->server_os || *c->server_type)
222 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
223 c->server_domain,c->server_os,c->server_type));
225 DEBUG(4,(" session setup ok\n"));
227 if (!cli_send_tconX(c, share, "?????",
228 password[snum], strlen(password[snum])+1)) {
229 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
234 DEBUG(4,(" tconx ok\n"));
236 c->use_oplocks = use_oplocks;
242 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
243 char *share[NSERVERS])
247 for (server=0;server<NSERVERS;server++)
248 for (conn=0;conn<NCONNECTIONS;conn++) {
249 if (cli[server][conn]) {
250 for (f=0;f<NFILES;f++) {
251 if (fnum[server][conn][f] != -1) {
252 cli_close(cli[server][conn], fnum[server][conn][f]);
253 fnum[server][conn][f] = -1;
256 cli_ulogoff(cli[server][conn]);
257 cli_shutdown(cli[server][conn]);
259 cli[server][conn] = connect_one(share[server], server);
260 if (!cli[server][conn]) {
261 DEBUG(0,("Failed to connect to %s\n", share[server]));
269 static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
270 int fnum[NSERVERS][NCONNECTIONS][NFILES],
273 unsigned conn = rec->conn;
275 SMB_BIG_UINT start = rec->start;
276 SMB_BIG_UINT len = rec->len;
277 enum brl_type op = rec->lock_type;
280 NTSTATUS status[NSERVERS];
282 switch (rec->lock_op) {
285 for (server=0;server<NSERVERS;server++) {
286 ret[server] = cli_lock64(cli[server][conn],
287 fnum[server][conn][f],
288 start, len, LOCK_TIMEOUT, op);
289 status[server] = cli_nt_error(cli[server][conn]);
290 if (!exact_error_codes &&
291 NT_STATUS_EQUAL(status[server],
292 NT_STATUS_FILE_LOCK_CONFLICT)) {
293 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
296 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
297 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
299 (double)start, (double)len,
300 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
301 nt_errstr(status[0]), nt_errstr(status[1]));
303 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
304 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
309 for (server=0;server<NSERVERS;server++) {
310 ret[server] = cli_unlock64(cli[server][conn],
311 fnum[server][conn][f],
313 status[server] = cli_nt_error(cli[server][conn]);
316 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
317 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
319 (double)start, (double)len,
320 nt_errstr(status[0]), nt_errstr(status[1]));
322 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
323 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
328 /* reopen the file */
329 for (server=0;server<NSERVERS;server++) {
330 cli_close(cli[server][conn], fnum[server][conn][f]);
331 fnum[server][conn][f] = -1;
333 for (server=0;server<NSERVERS;server++) {
334 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
337 if (fnum[server][conn][f] == -1) {
338 printf("failed to reopen on share%d\n", server);
343 printf("reopen conn=%u f=%u\n",
353 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
354 int fnum[NSERVERS][NCONNECTIONS][NFILES])
358 for (server=0;server<NSERVERS;server++)
359 for (conn=0;conn<NCONNECTIONS;conn++)
360 for (f=0;f<NFILES;f++) {
361 if (fnum[server][conn][f] != -1) {
362 cli_close(cli[server][conn], fnum[server][conn][f]);
363 fnum[server][conn][f] = -1;
366 for (server=0;server<NSERVERS;server++) {
367 cli_unlink(cli[server][0], FILENAME);
371 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
372 int fnum[NSERVERS][NCONNECTIONS][NFILES])
376 for (server=0;server<NSERVERS;server++)
377 for (conn=0;conn<NCONNECTIONS;conn++)
378 for (f=0;f<NFILES;f++) {
379 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
382 if (fnum[server][conn][f] == -1) {
383 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
391 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
392 int fnum[NSERVERS][NCONNECTIONS][NFILES],
396 printf("testing %u ...\n", n);
397 for (i=0; i<n; i++) {
398 if (i && i % 100 == 0) {
402 if (recorded[i].needed &&
403 !test_one(cli, fnum, &recorded[i])) return i;
409 /* each server has two connections open to it. Each connection has two file
410 descriptors open on the file - 8 file descriptors in total
412 we then do random locking ops in tamdem on the 4 fnums from each
413 server and ensure that the results match
415 static void test_locks(char *share[NSERVERS])
417 struct cli_state *cli[NSERVERS][NCONNECTIONS];
418 int fnum[NSERVERS][NCONNECTIONS][NFILES];
419 int n, i, n1, skip, r1, r2;
424 recorded = SMB_MALLOC_ARRAY(struct record, numops);
426 for (n=0; n<numops; n++) {
428 if (n < sizeof(preset) / sizeof(preset[0])) {
429 recorded[n] = preset[n];
432 recorded[n].conn = random() % NCONNECTIONS;
433 recorded[n].f = random() % NFILES;
434 recorded[n].start = lock_base + ((unsigned)random() % (lock_range-1));
435 recorded[n].len = min_length +
436 random() % (lock_range-(recorded[n].start-lock_base));
437 recorded[n].start *= RANGE_MULTIPLE;
438 recorded[n].len *= RANGE_MULTIPLE;
442 recorded[n].lock_type = READ_LOCK;
444 recorded[n].lock_type = WRITE_LOCK;
447 recorded[n].lock_op = OP_LOCK;
448 } else if (r2 < UNLOCK_PCT) {
449 recorded[n].lock_op = OP_UNLOCK;
451 recorded[n].lock_op = OP_REOPEN;
453 recorded[n].needed = True;
454 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
462 reconnect(cli, fnum, share);
463 open_files(cli, fnum);
464 n = retest(cli, fnum, numops);
466 if (n == numops || !analyze) return;
474 close_files(cli, fnum);
475 reconnect(cli, fnum, share);
476 open_files(cli, fnum);
478 for (i=0;i<n-skip;i+=skip) {
480 printf("excluding %d-%d\n", i, i+skip-1);
481 for (j=i;j<i+skip;j++) {
482 recorded[j].needed = False;
485 close_files(cli, fnum);
486 open_files(cli, fnum);
488 m = retest(cli, fnum, n);
490 for (j=i;j<i+skip;j++) {
491 recorded[j].needed = True;
494 if (i+(skip-1) < m) {
495 memmove(&recorded[i], &recorded[i+skip],
496 (m-(i+skip-1))*sizeof(recorded[0]));
505 printf("skip=%d\n", skip);
512 close_files(cli, fnum);
513 reconnect(cli, fnum, share);
514 open_files(cli, fnum);
516 n1 = retest(cli, fnum, n);
518 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
520 close_files(cli, fnum);
523 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
525 recorded[i].lock_type,
528 (double)recorded[i].start,
529 (double)recorded[i].len,
536 static void usage(void)
540 locktest //server1/share1 //server2/share2 [options..]\n\
542 -U user%%pass (may be specified twice)\n\
546 -u hide unlock fails\n\
548 -A analyse for minimal ops\n\
550 -E enable exact error code checking\n\
551 -Z enable the zero/zero lock\n\
552 -R range set lock range\n\
553 -B base set lock base\n\
554 -M min set min lock length\n\
558 /****************************************************************************
560 ****************************************************************************/
561 int main(int argc,char *argv[])
563 char *share[NSERVERS];
574 if (argc < 3 || argv[1][0] == '-') {
579 setup_logging(argv[0],True);
581 for (server=0;server<NSERVERS;server++) {
582 share[server] = argv[1+server];
583 all_string_sub(share[server],"/","\\",0);
589 lp_load(dyn_CONFIGFILE,True,False,False);
592 if (getenv("USER")) {
593 fstrcpy(username[0],getenv("USER"));
594 fstrcpy(username[1],getenv("USER"));
599 while ((opt = getopt(argc, argv, "U:s:ho:aAW:OkR:B:M:EZ")) != EOF) {
605 d_printf("No kerberos support compiled in\n");
612 d_printf("Max of 2 usernames\n");
615 fstrcpy(username[got_pass],optarg);
616 p = strchr_m(username[got_pass],'%');
619 fstrcpy(password[got_pass], p+1);
624 lock_range = strtol(optarg, NULL, 0);
627 lock_base = strtol(optarg, NULL, 0);
630 min_length = strtol(optarg, NULL, 0);
636 hide_unlock_fails = True;
639 numops = atoi(optarg);
654 exact_error_codes = True;
660 printf("Unknown option %c (%d)\n", (char)opt, opt);
665 if(use_kerberos && !got_user) got_pass = True;
670 DEBUG(0,("seed=%u\n", seed));