2 Unix SMB/Netbios implementation.
4 randomised byte range lock tester
5 Copyright (C) Andrew Tridgell 1999
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 static fstring password;
27 static fstring username;
29 static int numops = 1000;
32 static BOOL hide_unlock_fails;
33 static BOOL use_oplocks;
35 #define FILENAME "\\locktest.dat"
43 #define LOCKBASE (0x40000000 - 50)
49 #define RANGE_MULTIPLE 1
51 #define NCONNECTIONS 2
53 #define LOCK_TIMEOUT 0
55 #define NASTY_POSIX_LOCK_HACK 0
57 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
61 enum brl_type lock_type;
63 SMB_BIG_UINT start, len;
70 static struct record preset[] = {
71 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
72 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
73 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 0, 1},
74 {OP_UNLOCK, 0 , 0, 0, 2, 0, 1},
75 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
77 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
78 {OP_LOCK, READ_LOCK, 0, 0, 1, 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, 3, 1, 1},
84 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
85 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
87 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
88 {OP_LOCK, WRITE_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, 1, 1, 1},
94 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
95 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
97 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
98 {OP_LOCK, READ_LOCK, 0, 0, 3, 1, 1},
99 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
100 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
105 static struct record *recorded;
107 static void print_brl(SMB_DEV_T dev, SMB_INO_T ino, int pid,
108 enum brl_type lock_type,
109 br_off start, br_off size)
111 #if NASTY_POSIX_LOCK_HACK
114 static SMB_INO_T lastino;
116 if (lastino != ino) {
117 slprintf(cmd, sizeof(cmd),
118 "egrep POSIX.*%u /proc/locks", (int)ino);
125 printf("%6d %05x:%05x %s %.0f:%.0f(%.0f)\n",
126 (int)pid, (int)dev, (int)ino,
127 lock_type==READ_LOCK?"R":"W",
128 (double)start, (double)start+size-1,(double)size);
133 static void show_locks(void)
135 brl_forall(print_brl);
136 /* system("cat /proc/locks"); */
140 /*****************************************************
141 return a connection to a server
142 *******************************************************/
143 struct cli_state *connect_one(char *share)
146 struct nmb_name called, calling;
150 extern struct in_addr ipzero;
154 fstrcpy(server,share+2);
155 share = strchr_m(server,'\\');
156 if (!share) return NULL;
164 slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), count++);
166 make_nmb_name(&calling, myname, 0x0);
167 make_nmb_name(&called , server, 0x20);
172 /* have to open a new connection */
173 if (!(c=cli_initialise(NULL)) || !cli_connect(c, server_n, &ip)) {
174 DEBUG(0,("Connection to %s failed\n", server_n));
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 pstrcpy(password, pass);
203 if (!cli_session_setup(c, username,
204 password, strlen(password),
205 password, strlen(password),
207 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
212 * These next two lines are needed to emulate
213 * old client behaviour for people who have
214 * scripts based on client output.
215 * QUESTION ? Do we want to have a 'client compatibility
216 * mode to turn these on/off ? JRA.
219 if (*c->server_domain || *c->server_os || *c->server_type)
220 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
221 c->server_domain,c->server_os,c->server_type));
223 DEBUG(4,(" session setup ok\n"));
225 if (!cli_send_tconX(c, share, "?????",
226 password, strlen(password)+1)) {
227 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
232 DEBUG(4,(" tconx ok\n"));
234 c->use_oplocks = use_oplocks;
240 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
241 char *share[NSERVERS])
245 for (server=0;server<NSERVERS;server++)
246 for (conn=0;conn<NCONNECTIONS;conn++) {
247 if (cli[server][conn]) {
248 for (f=0;f<NFILES;f++) {
249 cli_close(cli[server][conn], fnum[server][conn][f]);
251 cli_ulogoff(cli[server][conn]);
252 cli_shutdown(cli[server][conn]);
253 SAFE_FREE(cli[server][conn]);
255 cli[server][conn] = connect_one(share[server]);
256 if (!cli[server][conn]) {
257 DEBUG(0,("Failed to connect to %s\n", share[server]));
265 static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
266 int fnum[NSERVERS][NCONNECTIONS][NFILES],
269 unsigned conn = rec->conn;
271 SMB_BIG_UINT start = rec->start;
272 SMB_BIG_UINT len = rec->len;
273 enum brl_type op = rec->lock_type;
277 switch (rec->lock_op) {
280 for (server=0;server<NSERVERS;server++) {
281 ret[server] = cli_lock64(cli[server][conn],
282 fnum[server][conn][f],
283 start, len, LOCK_TIMEOUT, op);
285 if (showall || ret[0] != ret[1]) {
286 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %u:%u\n",
288 (double)start, (double)len,
289 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
292 if (showall || ret[0] != ret[1]) show_locks();
293 if (ret[0] != ret[1]) return False;
298 for (server=0;server<NSERVERS;server++) {
299 ret[server] = cli_unlock64(cli[server][conn],
300 fnum[server][conn][f],
303 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
304 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %u:%u\n",
306 (double)start, (double)len,
309 if (showall || ret[0] != ret[1]) show_locks();
310 if (!hide_unlock_fails && ret[0] != ret[1]) return False;
314 /* reopen the file */
315 for (server=0;server<NSERVERS;server++) {
316 cli_close(cli[server][conn], fnum[server][conn][f]);
318 for (server=0;server<NSERVERS;server++) {
319 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
322 if (fnum[server][conn][f] == -1) {
323 printf("failed to reopen on share%d\n", server);
328 printf("reopen conn=%u f=%u\n",
338 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
339 int fnum[NSERVERS][NCONNECTIONS][NFILES])
343 for (server=0;server<NSERVERS;server++)
344 for (conn=0;conn<NCONNECTIONS;conn++)
345 for (f=0;f<NFILES;f++) {
346 if (fnum[server][conn][f] != -1) {
347 cli_close(cli[server][conn], fnum[server][conn][f]);
348 fnum[server][conn][f] = -1;
351 for (server=0;server<NSERVERS;server++) {
352 cli_unlink(cli[server][0], FILENAME);
356 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
357 int fnum[NSERVERS][NCONNECTIONS][NFILES])
361 for (server=0;server<NSERVERS;server++)
362 for (conn=0;conn<NCONNECTIONS;conn++)
363 for (f=0;f<NFILES;f++) {
364 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
367 if (fnum[server][conn][f] == -1) {
368 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
376 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
377 int fnum[NSERVERS][NCONNECTIONS][NFILES],
381 printf("testing %u ...\n", n);
382 for (i=0; i<n; i++) {
383 if (i && i % 100 == 0) {
387 if (recorded[i].needed &&
388 !test_one(cli, fnum, &recorded[i])) return i;
394 /* each server has two connections open to it. Each connection has two file
395 descriptors open on the file - 8 file descriptors in total
397 we then do random locking ops in tamdem on the 4 fnums from each
398 server and ensure that the results match
400 static void test_locks(char *share[NSERVERS])
402 struct cli_state *cli[NSERVERS][NCONNECTIONS];
403 int fnum[NSERVERS][NCONNECTIONS][NFILES];
404 int n, i, n1, skip, r1, r2;
409 recorded = (struct record *)malloc(sizeof(*recorded) * numops);
411 for (n=0; n<numops; n++) {
413 if (n < sizeof(preset) / sizeof(preset[0])) {
414 recorded[n] = preset[n];
417 recorded[n].conn = random() % NCONNECTIONS;
418 recorded[n].f = random() % NFILES;
419 recorded[n].start = LOCKBASE + ((unsigned)random() % (LOCKRANGE-1));
420 recorded[n].len = MINLENGTH +
421 random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
422 recorded[n].start *= RANGE_MULTIPLE;
423 recorded[n].len *= RANGE_MULTIPLE;
427 recorded[n].lock_type = READ_LOCK;
429 recorded[n].lock_type = WRITE_LOCK;
432 recorded[n].lock_op = OP_LOCK;
433 } else if (r2 < UNLOCK_PCT) {
434 recorded[n].lock_op = OP_UNLOCK;
436 recorded[n].lock_op = OP_REOPEN;
438 recorded[n].needed = True;
440 if (recorded[n].start == 0 &&
441 recorded[n].len == 0) {
450 reconnect(cli, fnum, share);
451 open_files(cli, fnum);
452 n = retest(cli, fnum, numops);
454 if (n == numops || !analyze) return;
462 close_files(cli, fnum);
463 reconnect(cli, fnum, share);
464 open_files(cli, fnum);
466 for (i=0;i<n-skip;i+=skip) {
468 printf("excluding %d-%d\n", i, i+skip-1);
469 for (j=i;j<i+skip;j++) {
470 recorded[j].needed = False;
473 close_files(cli, fnum);
474 open_files(cli, fnum);
476 m = retest(cli, fnum, n);
478 for (j=i;j<i+skip;j++) {
479 recorded[j].needed = True;
482 if (i+(skip-1) < m) {
483 memmove(&recorded[i], &recorded[i+skip],
484 (m-(i+skip-1))*sizeof(recorded[0]));
493 printf("skip=%d\n", skip);
500 close_files(cli, fnum);
501 reconnect(cli, fnum, share);
502 open_files(cli, fnum);
504 n1 = retest(cli, fnum, n);
506 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
508 close_files(cli, fnum);
511 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
513 recorded[i].lock_type,
516 (double)recorded[i].start,
517 (double)recorded[i].len,
524 static void usage(void)
528 locktest //server1/share1 //server2/share2 [options..]\n\
533 -u hide unlock fails\n\
539 /****************************************************************************
541 ****************************************************************************/
542 int main(int argc,char *argv[])
544 char *share[NSERVERS];
550 static pstring servicesf = CONFIGFILE;
556 if (argc < 3 || argv[1][0] == '-') {
561 setup_logging(argv[0],True);
563 for (server=0;server<NSERVERS;server++) {
564 share[server] = argv[1+server];
565 all_string_sub(share[server],"/","\\",0);
573 lp_load(servicesf,True,False,False);
576 if (getenv("USER")) {
577 pstrcpy(username,getenv("USER"));
582 while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
585 pstrcpy(username,optarg);
586 p = strchr_m(username,'%');
589 pstrcpy(password, p+1);
597 hide_unlock_fails = True;
600 numops = atoi(optarg);
615 printf("Unknown option %c (%d)\n", (char)opt, opt);
623 DEBUG(0,("seed=%u\n", seed));