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 static fstring password
[2];
26 static fstring username
[2];
29 static BOOL use_kerberos
;
30 static int numops
= 1000;
33 static BOOL hide_unlock_fails
;
34 static BOOL use_oplocks
;
35 static unsigned lock_range
= 100;
36 static unsigned lock_base
= 0;
37 static unsigned min_length
= 0;
38 static BOOL exact_error_codes
;
39 static BOOL zero_zero
;
41 #define FILENAME "\\locktest.dat"
46 #define RANGE_MULTIPLE 1
48 #define NCONNECTIONS 2
50 #define LOCK_TIMEOUT 0
52 #define NASTY_POSIX_LOCK_HACK 0
54 enum lock_op
{OP_LOCK
, OP_UNLOCK
, OP_REOPEN
};
58 enum brl_type lock_type
;
60 SMB_BIG_UINT start
, len
;
67 static struct record preset
[] = {
68 {OP_LOCK
, WRITE_LOCK
, 0, 0, 2, 0, 1},
69 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
70 {OP_LOCK
, WRITE_LOCK
, 0, 0, 3, 0, 1},
71 {OP_UNLOCK
, 0 , 0, 0, 2, 0, 1},
72 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
74 {OP_LOCK
, READ_LOCK
, 0, 0, 2, 0, 1},
75 {OP_LOCK
, READ_LOCK
, 0, 0, 1, 1, 1},
76 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
77 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
79 {OP_LOCK
, READ_LOCK
, 0, 0, 2, 0, 1},
80 {OP_LOCK
, WRITE_LOCK
, 0, 0, 3, 1, 1},
81 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
82 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
84 {OP_LOCK
, READ_LOCK
, 0, 0, 2, 0, 1},
85 {OP_LOCK
, WRITE_LOCK
, 0, 0, 1, 1, 1},
86 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
87 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
89 {OP_LOCK
, WRITE_LOCK
, 0, 0, 2, 0, 1},
90 {OP_LOCK
, READ_LOCK
, 0, 0, 1, 1, 1},
91 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
92 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
94 {OP_LOCK
, WRITE_LOCK
, 0, 0, 2, 0, 1},
95 {OP_LOCK
, READ_LOCK
, 0, 0, 3, 1, 1},
96 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
97 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
102 static struct record
*recorded
;
104 static void print_brl(SMB_DEV_T dev
, SMB_INO_T ino
, int pid
,
105 enum brl_type lock_type
,
106 br_off start
, br_off size
)
108 #if NASTY_POSIX_LOCK_HACK
111 static SMB_INO_T lastino
;
113 if (lastino
!= ino
) {
114 slprintf(cmd
, sizeof(cmd
),
115 "egrep POSIX.*%u /proc/locks", (int)ino
);
122 printf("%6d %05x:%05x %s %.0f:%.0f(%.0f)\n",
123 (int)pid
, (int)dev
, (int)ino
,
124 lock_type
==READ_LOCK
?"R":"W",
125 (double)start
, (double)start
+size
-1,(double)size
);
130 static void show_locks(void)
132 brl_forall(print_brl
);
133 /* system("cat /proc/locks"); */
137 /*****************************************************
138 return a connection to a server
139 *******************************************************/
140 static struct cli_state
*connect_one(char *share
, int snum
)
143 struct nmb_name called
, calling
;
150 fstrcpy(server
,share
+2);
151 share
= strchr_m(server
,'\\');
152 if (!share
) return NULL
;
160 slprintf(myname
,sizeof(myname
), "lock-%lu-%u", (unsigned long)getpid(), count
++);
162 make_nmb_name(&calling
, myname
, 0x0);
163 make_nmb_name(&called
, server
, 0x20);
168 /* have to open a new connection */
169 if (!(c
=cli_initialise(NULL
)) || !cli_connect(c
, server_n
, &ip
)) {
170 DEBUG(0,("Connection to %s failed\n", server_n
));
174 c
->use_kerberos
= use_kerberos
;
176 if (!cli_session_request(c
, &calling
, &called
)) {
177 DEBUG(0,("session request to %s failed\n", called
.name
));
179 if (strcmp(called
.name
, "*SMBSERVER")) {
180 make_nmb_name(&called
, "*SMBSERVER", 0x20);
186 DEBUG(4,(" session request ok\n"));
188 if (!cli_negprot(c
)) {
189 DEBUG(0,("protocol negotiation failed\n"));
195 char *pass
= getpass("Password: ");
197 fstrcpy(password
[0], pass
);
198 fstrcpy(password
[1], pass
);
203 fstrcpy(password
[1], password
[0]);
204 fstrcpy(username
[1], username
[0]);
207 if (!cli_session_setup(c
, username
[snum
],
208 password
[snum
], strlen(password
[snum
]),
209 password
[snum
], strlen(password
[snum
]),
211 DEBUG(0,("session setup failed: %s\n", cli_errstr(c
)));
216 * These next two lines are needed to emulate
217 * old client behaviour for people who have
218 * scripts based on client output.
219 * QUESTION ? Do we want to have a 'client compatibility
220 * mode to turn these on/off ? JRA.
223 if (*c
->server_domain
|| *c
->server_os
|| *c
->server_type
)
224 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
225 c
->server_domain
,c
->server_os
,c
->server_type
));
227 DEBUG(4,(" session setup ok\n"));
229 if (!cli_send_tconX(c
, share
, "?????",
230 password
[snum
], strlen(password
[snum
])+1)) {
231 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c
)));
236 DEBUG(4,(" tconx ok\n"));
238 c
->use_oplocks
= use_oplocks
;
244 static void reconnect(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
], int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
],
245 char *share
[NSERVERS
])
249 for (server
=0;server
<NSERVERS
;server
++)
250 for (conn
=0;conn
<NCONNECTIONS
;conn
++) {
251 if (cli
[server
][conn
]) {
252 for (f
=0;f
<NFILES
;f
++) {
253 if (fnum
[server
][conn
][f
] != -1) {
254 cli_close(cli
[server
][conn
], fnum
[server
][conn
][f
]);
255 fnum
[server
][conn
][f
] = -1;
258 cli_ulogoff(cli
[server
][conn
]);
259 cli_shutdown(cli
[server
][conn
]);
261 cli
[server
][conn
] = connect_one(share
[server
], server
);
262 if (!cli
[server
][conn
]) {
263 DEBUG(0,("Failed to connect to %s\n", share
[server
]));
271 static BOOL
test_one(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
272 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
],
275 unsigned conn
= rec
->conn
;
277 SMB_BIG_UINT start
= rec
->start
;
278 SMB_BIG_UINT len
= rec
->len
;
279 enum brl_type op
= rec
->lock_type
;
282 NTSTATUS status
[NSERVERS
];
284 switch (rec
->lock_op
) {
287 for (server
=0;server
<NSERVERS
;server
++) {
288 ret
[server
] = cli_lock64(cli
[server
][conn
],
289 fnum
[server
][conn
][f
],
290 start
, len
, LOCK_TIMEOUT
, op
);
291 status
[server
] = cli_nt_error(cli
[server
][conn
]);
292 if (!exact_error_codes
&&
293 NT_STATUS_EQUAL(status
[server
],
294 NT_STATUS_FILE_LOCK_CONFLICT
)) {
295 status
[server
] = NT_STATUS_LOCK_NOT_GRANTED
;
298 if (showall
|| !NT_STATUS_EQUAL(status
[0],status
[1])) {
299 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
301 (double)start
, (double)len
,
302 op
==READ_LOCK
?"READ_LOCK":"WRITE_LOCK",
303 nt_errstr(status
[0]), nt_errstr(status
[1]));
305 if (showall
|| !NT_STATUS_EQUAL(status
[0],status
[1])) show_locks();
306 if (!NT_STATUS_EQUAL(status
[0],status
[1])) return False
;
311 for (server
=0;server
<NSERVERS
;server
++) {
312 ret
[server
] = cli_unlock64(cli
[server
][conn
],
313 fnum
[server
][conn
][f
],
315 status
[server
] = cli_nt_error(cli
[server
][conn
]);
318 (!hide_unlock_fails
&& !NT_STATUS_EQUAL(status
[0],status
[1]))) {
319 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
321 (double)start
, (double)len
,
322 nt_errstr(status
[0]), nt_errstr(status
[1]));
324 if (showall
|| !NT_STATUS_EQUAL(status
[0],status
[1])) show_locks();
325 if (!hide_unlock_fails
&& !NT_STATUS_EQUAL(status
[0],status
[1]))
330 /* reopen the file */
331 for (server
=0;server
<NSERVERS
;server
++) {
332 cli_close(cli
[server
][conn
], fnum
[server
][conn
][f
]);
333 fnum
[server
][conn
][f
] = -1;
335 for (server
=0;server
<NSERVERS
;server
++) {
336 fnum
[server
][conn
][f
] = cli_open(cli
[server
][conn
], FILENAME
,
339 if (fnum
[server
][conn
][f
] == -1) {
340 printf("failed to reopen on share%d\n", server
);
345 printf("reopen conn=%u f=%u\n",
355 static void close_files(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
356 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
])
360 for (server
=0;server
<NSERVERS
;server
++)
361 for (conn
=0;conn
<NCONNECTIONS
;conn
++)
362 for (f
=0;f
<NFILES
;f
++) {
363 if (fnum
[server
][conn
][f
] != -1) {
364 cli_close(cli
[server
][conn
], fnum
[server
][conn
][f
]);
365 fnum
[server
][conn
][f
] = -1;
368 for (server
=0;server
<NSERVERS
;server
++) {
369 cli_unlink(cli
[server
][0], FILENAME
);
373 static void open_files(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
374 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
])
378 for (server
=0;server
<NSERVERS
;server
++)
379 for (conn
=0;conn
<NCONNECTIONS
;conn
++)
380 for (f
=0;f
<NFILES
;f
++) {
381 fnum
[server
][conn
][f
] = cli_open(cli
[server
][conn
], FILENAME
,
384 if (fnum
[server
][conn
][f
] == -1) {
385 fprintf(stderr
,"Failed to open fnum[%u][%u][%u]\n",
393 static int retest(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
394 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
],
398 printf("testing %u ...\n", n
);
399 for (i
=0; i
<n
; i
++) {
400 if (i
&& i
% 100 == 0) {
404 if (recorded
[i
].needed
&&
405 !test_one(cli
, fnum
, &recorded
[i
])) return i
;
411 /* each server has two connections open to it. Each connection has two file
412 descriptors open on the file - 8 file descriptors in total
414 we then do random locking ops in tamdem on the 4 fnums from each
415 server and ensure that the results match
417 static void test_locks(char *share
[NSERVERS
])
419 struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
];
420 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
];
421 int n
, i
, n1
, skip
, r1
, r2
;
426 recorded
= SMB_MALLOC_ARRAY(struct record
, numops
);
428 for (n
=0; n
<numops
; n
++) {
430 if (n
< sizeof(preset
) / sizeof(preset
[0])) {
431 recorded
[n
] = preset
[n
];
434 recorded
[n
].conn
= random() % NCONNECTIONS
;
435 recorded
[n
].f
= random() % NFILES
;
436 recorded
[n
].start
= lock_base
+ ((unsigned)random() % (lock_range
-1));
437 recorded
[n
].len
= min_length
+
438 random() % (lock_range
-(recorded
[n
].start
-lock_base
));
439 recorded
[n
].start
*= RANGE_MULTIPLE
;
440 recorded
[n
].len
*= RANGE_MULTIPLE
;
444 recorded
[n
].lock_type
= READ_LOCK
;
446 recorded
[n
].lock_type
= WRITE_LOCK
;
449 recorded
[n
].lock_op
= OP_LOCK
;
450 } else if (r2
< UNLOCK_PCT
) {
451 recorded
[n
].lock_op
= OP_UNLOCK
;
453 recorded
[n
].lock_op
= OP_REOPEN
;
455 recorded
[n
].needed
= True
;
456 if (!zero_zero
&& recorded
[n
].start
==0 && recorded
[n
].len
==0) {
464 reconnect(cli
, fnum
, share
);
465 open_files(cli
, fnum
);
466 n
= retest(cli
, fnum
, numops
);
468 if (n
== numops
|| !analyze
) return;
476 close_files(cli
, fnum
);
477 reconnect(cli
, fnum
, share
);
478 open_files(cli
, fnum
);
480 for (i
=0;i
<n
-skip
;i
+=skip
) {
482 printf("excluding %d-%d\n", i
, i
+skip
-1);
483 for (j
=i
;j
<i
+skip
;j
++) {
484 recorded
[j
].needed
= False
;
487 close_files(cli
, fnum
);
488 open_files(cli
, fnum
);
490 m
= retest(cli
, fnum
, n
);
492 for (j
=i
;j
<i
+skip
;j
++) {
493 recorded
[j
].needed
= True
;
496 if (i
+(skip
-1) < m
) {
497 memmove(&recorded
[i
], &recorded
[i
+skip
],
498 (m
-(i
+skip
-1))*sizeof(recorded
[0]));
507 printf("skip=%d\n", skip
);
514 close_files(cli
, fnum
);
515 reconnect(cli
, fnum
, share
);
516 open_files(cli
, fnum
);
518 n1
= retest(cli
, fnum
, n
);
520 printf("ERROR - inconsistent result (%u %u)\n", n1
, n
);
522 close_files(cli
, fnum
);
525 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
527 recorded
[i
].lock_type
,
530 (double)recorded
[i
].start
,
531 (double)recorded
[i
].len
,
538 static void usage(void)
542 locktest //server1/share1 //server2/share2 [options..]\n\
544 -U user%%pass (may be specified twice)\n\
548 -u hide unlock fails\n\
550 -A analyse for minimal ops\n\
552 -E enable exact error code checking\n\
553 -Z enable the zero/zero lock\n\
554 -R range set lock range\n\
555 -B base set lock base\n\
556 -M min set min lock length\n\
560 /****************************************************************************
562 ****************************************************************************/
563 int main(int argc
,char *argv
[])
565 char *share
[NSERVERS
];
576 if (argc
< 3 || argv
[1][0] == '-') {
581 setup_logging(argv
[0],True
);
583 for (server
=0;server
<NSERVERS
;server
++) {
584 share
[server
] = argv
[1+server
];
585 all_string_sub(share
[server
],"/","\\",0);
591 lp_load(dyn_CONFIGFILE
,True
,False
,False
);
594 if (getenv("USER")) {
595 fstrcpy(username
[0],getenv("USER"));
596 fstrcpy(username
[1],getenv("USER"));
601 while ((opt
= getopt(argc
, argv
, "U:s:ho:aAW:OkR:B:M:EZ")) != EOF
) {
607 d_printf("No kerberos support compiled in\n");
614 d_printf("Max of 2 usernames\n");
617 fstrcpy(username
[got_pass
],optarg
);
618 p
= strchr_m(username
[got_pass
],'%');
621 fstrcpy(password
[got_pass
], p
+1);
626 lock_range
= strtol(optarg
, NULL
, 0);
629 lock_base
= strtol(optarg
, NULL
, 0);
632 min_length
= strtol(optarg
, NULL
, 0);
638 hide_unlock_fails
= True
;
641 numops
= atoi(optarg
);
656 exact_error_codes
= True
;
662 printf("Unknown option %c (%d)\n", (char)opt
, opt
);
667 if(use_kerberos
&& !got_user
) got_pass
= True
;
672 DEBUG(0,("seed=%u\n", seed
));