2 Unix SMB/Netbios implementation.
4 byte range lock tester - with local filesystem support
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"
40 #define LOCKBASE (0x40000000 - 50)
46 #define RANGE_MULTIPLE 1
49 #define NCONNECTIONS 2
52 #define LOCK_TIMEOUT 0
64 static struct record
*recorded
;
66 static int try_open(struct cli_state
*c
, char *nfs
, int fstype
, char *fname
, int flags
)
72 return cli_open(c
, fname
, flags
, DENY_NONE
);
75 slprintf(path
, sizeof(path
), "%s%s", nfs
, fname
);
76 pstring_sub(path
,"\\", "/");
77 return open(path
, flags
, 0666);
83 static BOOL
try_close(struct cli_state
*c
, int fstype
, int fd
)
87 return cli_close(c
, fd
);
90 return close(fd
) == 0;
96 static BOOL
try_lock(struct cli_state
*c
, int fstype
,
97 int fd
, unsigned start
, unsigned len
,
104 return cli_lock(c
, fd
, start
, len
, LOCK_TIMEOUT
, op
);
107 lock
.l_type
= (op
==READ_LOCK
) ? F_RDLCK
:F_WRLCK
;
108 lock
.l_whence
= SEEK_SET
;
109 lock
.l_start
= start
;
111 lock
.l_pid
= getpid();
112 return fcntl(fd
,F_SETLK
,&lock
) == 0;
118 static BOOL
try_unlock(struct cli_state
*c
, int fstype
,
119 int fd
, unsigned start
, unsigned len
)
125 return cli_unlock(c
, fd
, start
, len
);
128 lock
.l_type
= F_UNLCK
;
129 lock
.l_whence
= SEEK_SET
;
130 lock
.l_start
= start
;
132 lock
.l_pid
= getpid();
133 return fcntl(fd
,F_SETLK
,&lock
) == 0;
139 static void print_brl(SMB_DEV_T dev
, SMB_INO_T ino
, int pid
,
140 enum brl_type lock_type
,
141 br_off start
, br_off size
)
143 printf("%6d %05x:%05x %s %.0f:%.0f(%.0f)\n",
144 (int)pid
, (int)dev
, (int)ino
,
145 lock_type
==READ_LOCK
?"R":"W",
146 (double)start
, (double)start
+size
-1,(double)size
);
150 /*****************************************************
151 return a connection to a server
152 *******************************************************/
153 struct cli_state
*connect_one(char *share
)
156 struct nmb_name called
, calling
;
160 extern struct in_addr ipzero
;
164 fstrcpy(server
,share
+2);
165 share
= strchr(server
,'\\');
166 if (!share
) return NULL
;
174 slprintf(myname
,sizeof(myname
), "lock-%u-%u", getpid(), count
++);
176 make_nmb_name(&calling
, myname
, 0x0);
177 make_nmb_name(&called
, server
, 0x20);
182 /* have to open a new connection */
183 if (!(c
=cli_initialise(NULL
)) || (cli_set_port(c
, 139) == 0) ||
184 !cli_connect(c
, server_n
, &ip
)) {
185 DEBUG(0,("Connection to %s failed\n", server_n
));
189 if (!cli_session_request(c
, &calling
, &called
)) {
190 DEBUG(0,("session request to %s failed\n", called
.name
));
192 if (strcmp(called
.name
, "*SMBSERVER")) {
193 make_nmb_name(&called
, "*SMBSERVER", 0x20);
199 DEBUG(4,(" session request ok\n"));
201 if (!cli_negprot(c
)) {
202 DEBUG(0,("protocol negotiation failed\n"));
208 char *pass
= getpass("Password: ");
210 pstrcpy(password
, pass
);
214 if (!cli_session_setup(c
, username
,
215 password
, strlen(password
),
216 password
, strlen(password
),
218 DEBUG(0,("session setup failed: %s\n", cli_errstr(c
)));
223 * These next two lines are needed to emulate
224 * old client behaviour for people who have
225 * scripts based on client output.
226 * QUESTION ? Do we want to have a 'client compatibility
227 * mode to turn these on/off ? JRA.
230 if (*c
->server_domain
|| *c
->server_os
|| *c
->server_type
)
231 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
232 c
->server_domain
,c
->server_os
,c
->server_type
));
234 DEBUG(4,(" session setup ok\n"));
236 if (!cli_send_tconX(c
, share
, "?????",
237 password
, strlen(password
)+1)) {
238 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c
)));
243 DEBUG(4,(" tconx ok\n"));
245 c
->use_oplocks
= use_oplocks
;
251 static void reconnect(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
253 int fnum
[NSERVERS
][NUMFSTYPES
][NCONNECTIONS
][NFILES
],
254 char *share1
, char *share2
)
256 int server
, conn
, f
, fstype
;
263 for (server
=0;server
<NSERVERS
;server
++)
264 for (conn
=0;conn
<NCONNECTIONS
;conn
++) {
265 if (cli
[server
][conn
]) {
266 for (f
=0;f
<NFILES
;f
++) {
267 cli_close(cli
[server
][conn
], fnum
[server
][fstype
][conn
][f
]);
269 cli_ulogoff(cli
[server
][conn
]);
270 cli_shutdown(cli
[server
][conn
]);
271 free(cli
[server
][conn
]);
272 cli
[server
][conn
] = NULL
;
274 cli
[server
][conn
] = connect_one(share
[server
]);
275 if (!cli
[server
][conn
]) {
276 DEBUG(0,("Failed to connect to %s\n", share
[server
]));
284 static BOOL
test_one(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
286 int fnum
[NSERVERS
][NUMFSTYPES
][NCONNECTIONS
][NFILES
],
289 unsigned conn
= rec
->conn
;
291 unsigned fstype
= rec
->fstype
;
292 unsigned start
= rec
->start
;
293 unsigned len
= rec
->len
;
294 unsigned r1
= rec
->r1
;
295 unsigned r2
= rec
->r2
;
308 for (server
=0;server
<NSERVERS
;server
++) {
309 ret
[server
] = try_lock(cli
[server
][conn
], fstype
,
310 fnum
[server
][fstype
][conn
][f
],
313 if (showall
|| ret
[0] != ret
[1]) {
314 printf("lock conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
316 start
, start
+len
-1, len
,
317 op
==READ_LOCK
?"READ_LOCK":"WRITE_LOCK",
320 if (showall
) brl_forall(print_brl
);
321 if (ret
[0] != ret
[1]) return False
;
322 } else if (r2
< LOCK_PCT
+UNLOCK_PCT
) {
324 for (server
=0;server
<NSERVERS
;server
++) {
325 ret
[server
] = try_unlock(cli
[server
][conn
], fstype
,
326 fnum
[server
][fstype
][conn
][f
],
329 if (showall
|| (!hide_unlock_fails
&& (ret
[0] != ret
[1]))) {
330 printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u) -> %u:%u\n",
332 start
, start
+len
-1, len
,
335 if (showall
) brl_forall(print_brl
);
336 if (!hide_unlock_fails
&& ret
[0] != ret
[1]) return False
;
338 /* reopen the file */
339 for (server
=0;server
<NSERVERS
;server
++) {
340 try_close(cli
[server
][conn
], fstype
, fnum
[server
][fstype
][conn
][f
]);
341 fnum
[server
][fstype
][conn
][f
] = try_open(cli
[server
][conn
], nfs
[server
], fstype
, FILENAME
,
343 if (fnum
[server
][fstype
][conn
][f
] == -1) {
344 printf("failed to reopen on share1\n");
349 printf("reopen conn=%u fstype=%u f=%u\n",
351 brl_forall(print_brl
);
357 static void close_files(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
359 int fnum
[NSERVERS
][NUMFSTYPES
][NCONNECTIONS
][NFILES
])
361 int server
, conn
, f
, fstype
;
363 for (server
=0;server
<NSERVERS
;server
++)
364 for (fstype
=0;fstype
<NUMFSTYPES
;fstype
++)
365 for (conn
=0;conn
<NCONNECTIONS
;conn
++)
366 for (f
=0;f
<NFILES
;f
++) {
367 if (fnum
[server
][fstype
][conn
][f
] != -1) {
368 try_close(cli
[server
][conn
], fstype
, fnum
[server
][fstype
][conn
][f
]);
369 fnum
[server
][fstype
][conn
][f
] = -1;
372 for (server
=0;server
<NSERVERS
;server
++) {
373 cli_unlink(cli
[server
][0], FILENAME
);
377 static void open_files(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
379 int fnum
[NSERVERS
][NUMFSTYPES
][NCONNECTIONS
][NFILES
])
381 int server
, fstype
, conn
, f
;
383 for (server
=0;server
<NSERVERS
;server
++)
384 for (fstype
=0;fstype
<NUMFSTYPES
;fstype
++)
385 for (conn
=0;conn
<NCONNECTIONS
;conn
++)
386 for (f
=0;f
<NFILES
;f
++) {
387 fnum
[server
][fstype
][conn
][f
] = try_open(cli
[server
][conn
], nfs
[server
], fstype
, FILENAME
,
389 if (fnum
[server
][fstype
][conn
][f
] == -1) {
390 fprintf(stderr
,"Failed to open fnum[%u][%u][%u][%u]\n",
391 server
, fstype
, conn
, f
);
398 static int retest(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
400 int fnum
[NSERVERS
][NUMFSTYPES
][NCONNECTIONS
][NFILES
],
404 printf("testing %u ...\n", n
);
405 for (i
=0; i
<n
; i
++) {
406 if (i
&& i
% 100 == 0) {
410 if (recorded
[i
].needed
&&
411 !test_one(cli
, nfs
, fnum
, &recorded
[i
])) return i
;
417 /* each server has two connections open to it. Each connection has two file
418 descriptors open on the file - 8 file descriptors in total
420 we then do random locking ops in tamdem on the 4 fnums from each
421 server and ensure that the results match
423 static void test_locks(char *share1
, char *share2
, char *nfspath1
, char *nfspath2
)
425 struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
];
427 int fnum
[NSERVERS
][NUMFSTYPES
][NCONNECTIONS
][NFILES
];
436 recorded
= (struct record
*)malloc(sizeof(*recorded
) * numops
);
438 for (n
=0; n
<numops
; n
++) {
439 recorded
[n
].conn
= random() % NCONNECTIONS
;
440 recorded
[n
].fstype
= random() % NUMFSTYPES
;
441 recorded
[n
].f
= random() % NFILES
;
442 recorded
[n
].start
= LOCKBASE
+ ((unsigned)random() % (LOCKRANGE
-1));
443 recorded
[n
].len
= 1 +
444 random() % (LOCKRANGE
-(recorded
[n
].start
-LOCKBASE
));
445 recorded
[n
].start
*= RANGE_MULTIPLE
;
446 recorded
[n
].len
*= RANGE_MULTIPLE
;
447 recorded
[n
].r1
= random() % 100;
448 recorded
[n
].r2
= random() % 100;
449 recorded
[n
].needed
= True
;
452 reconnect(cli
, nfs
, fnum
, share1
, share2
);
453 open_files(cli
, nfs
, fnum
);
454 n
= retest(cli
, nfs
, fnum
, numops
);
456 if (n
== numops
|| !analyze
) return;
462 close_files(cli
, nfs
, fnum
);
463 reconnect(cli
, nfs
, fnum
, share1
, share2
);
464 open_files(cli
, nfs
, fnum
);
466 for (i
=0;i
<n
-1;i
++) {
468 recorded
[i
].needed
= False
;
470 close_files(cli
, nfs
, fnum
);
471 open_files(cli
, nfs
, fnum
);
473 m
= retest(cli
, nfs
, fnum
, n
);
475 recorded
[i
].needed
= True
;
478 memmove(&recorded
[i
], &recorded
[i
+1],
479 (m
-i
)*sizeof(recorded
[0]));
489 close_files(cli
, nfs
, fnum
);
490 reconnect(cli
, nfs
, fnum
, share1
, share2
);
491 open_files(cli
, nfs
, fnum
);
493 n1
= retest(cli
, nfs
, fnum
, n
);
495 printf("ERROR - inconsistent result (%u %u)\n", n1
, n
);
497 close_files(cli
, nfs
, fnum
);
500 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
514 static void usage(void)
518 locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
523 -u hide unlock fails\n\
529 /****************************************************************************
531 ****************************************************************************/
532 int main(int argc
,char *argv
[])
534 char *share1
, *share2
, *nfspath1
, *nfspath2
;
541 static pstring servicesf
= CONFIGFILE
;
547 if (argc
< 5 || argv
[1][0] == '-') {
557 all_string_sub(share1
,"/","\\",0);
558 all_string_sub(share2
,"/","\\",0);
560 setup_logging(argv
[0],True
);
566 charset_initialise();
567 codepage_initialise(lp_client_code_page());
569 lp_load(servicesf
,True
,False
,False
);
572 if (getenv("USER")) {
573 pstrcpy(username
,getenv("USER"));
578 while ((opt
= getopt(argc
, argv
, "U:s:ho:aAW:O")) != EOF
) {
581 pstrcpy(username
,optarg
);
582 p
= strchr(username
,'%');
585 pstrcpy(password
, p
+1);
593 hide_unlock_fails
= True
;
596 numops
= atoi(optarg
);
611 printf("Unknown option %c (%d)\n", (char)opt
, opt
);
619 DEBUG(0,("seed=%u\n", seed
));
623 test_locks(share1
, share2
, nfspath1
, nfspath2
);