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"
36 #define LOCKRANGE 1000
40 #define LOCKBASE (0x40000000 - 50)
46 #define RANGE_MULTIPLE 1
48 #define NCONNECTIONS 2
50 #define LOCK_TIMEOUT 0
52 #define NASTY_POSIX_LOCK_HACK 0
58 SMB_BIG_UINT start, len;
65 static struct record preset[] = {
66 {36, 5, 0, 0, 0, 8, 1},
67 { 2, 6, 0, 1, 0, 1, 1},
68 {53, 92, 0, 0, 0, 0, 1},
69 {99, 11, 0, 0, 7, 1, 1},
73 static struct record *recorded;
75 static void print_brl(SMB_DEV_T dev, SMB_INO_T ino, int pid,
76 enum brl_type lock_type,
77 br_off start, br_off size)
79 #if NASTY_POSIX_LOCK_HACK
82 static SMB_INO_T lastino;
85 slprintf(cmd, sizeof(cmd),
86 "egrep POSIX.*%u /proc/locks", (int)ino);
93 printf("%6d %05x:%05x %s %.0f:%.0f(%.0f)\n",
94 (int)pid, (int)dev, (int)ino,
95 lock_type==READ_LOCK?"R":"W",
96 (double)start, (double)start+size-1,(double)size);
101 static void show_locks(void)
103 brl_forall(print_brl);
104 /* system("cat /proc/locks"); */
108 /*****************************************************
109 return a connection to a server
110 *******************************************************/
111 struct cli_state *connect_one(char *share)
114 struct nmb_name called, calling;
118 extern struct in_addr ipzero;
122 fstrcpy(server,share+2);
123 share = strchr_m(server,'\\');
124 if (!share) return NULL;
132 slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), count++);
134 make_nmb_name(&calling, myname, 0x0);
135 make_nmb_name(&called , server, 0x20);
140 /* have to open a new connection */
141 if (!(c=cli_initialise(NULL)) || (cli_set_port(c, 139) == 0) ||
142 !cli_connect(c, server_n, &ip)) {
143 DEBUG(0,("Connection to %s failed\n", server_n));
147 if (!cli_session_request(c, &calling, &called)) {
148 DEBUG(0,("session request to %s failed\n", called.name));
150 if (strcmp(called.name, "*SMBSERVER")) {
151 make_nmb_name(&called , "*SMBSERVER", 0x20);
157 DEBUG(4,(" session request ok\n"));
159 if (!cli_negprot(c)) {
160 DEBUG(0,("protocol negotiation failed\n"));
166 char *pass = getpass("Password: ");
168 pstrcpy(password, pass);
172 if (!cli_session_setup(c, username,
173 password, strlen(password),
174 password, strlen(password),
176 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
181 * These next two lines are needed to emulate
182 * old client behaviour for people who have
183 * scripts based on client output.
184 * QUESTION ? Do we want to have a 'client compatibility
185 * mode to turn these on/off ? JRA.
188 if (*c->server_domain || *c->server_os || *c->server_type)
189 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
190 c->server_domain,c->server_os,c->server_type));
192 DEBUG(4,(" session setup ok\n"));
194 if (!cli_send_tconX(c, share, "?????",
195 password, strlen(password)+1)) {
196 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
201 DEBUG(4,(" tconx ok\n"));
203 c->use_oplocks = use_oplocks;
209 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
210 char *share[NSERVERS])
214 for (server=0;server<NSERVERS;server++)
215 for (conn=0;conn<NCONNECTIONS;conn++) {
216 if (cli[server][conn]) {
217 for (f=0;f<NFILES;f++) {
218 cli_close(cli[server][conn], fnum[server][conn][f]);
220 cli_ulogoff(cli[server][conn]);
221 cli_shutdown(cli[server][conn]);
222 free(cli[server][conn]);
223 cli[server][conn] = NULL;
225 cli[server][conn] = connect_one(share[server]);
226 if (!cli[server][conn]) {
227 DEBUG(0,("Failed to connect to %s\n", share[server]));
235 static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
236 int fnum[NSERVERS][NCONNECTIONS][NFILES],
239 unsigned conn = rec->conn;
241 SMB_BIG_UINT start = rec->start;
242 SMB_BIG_UINT len = rec->len;
243 unsigned r1 = rec->r1;
244 unsigned r2 = rec->r2;
257 for (server=0;server<NSERVERS;server++) {
258 ret[server] = cli_lock64(cli[server][conn],
259 fnum[server][conn][f],
260 start, len, LOCK_TIMEOUT, op);
262 if (showall || ret[0] != ret[1]) {
263 printf("lock conn=%u f=%u range=%.0f:%.0f(%.0f) op=%s -> %u:%u\n",
265 (double)start, (double)start+len-1, (double)len,
266 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
269 if (showall || ret[0] != ret[1]) show_locks();
270 if (ret[0] != ret[1]) return False;
271 } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
273 for (server=0;server<NSERVERS;server++) {
274 ret[server] = cli_unlock64(cli[server][conn],
275 fnum[server][conn][f],
278 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
279 printf("unlock conn=%u f=%u range=%.0f:%.0f(%.0f) -> %u:%u\n",
281 (double)start, (double)start+len-1, (double)len,
284 if (showall || ret[0] != ret[1]) show_locks();
285 if (!hide_unlock_fails && ret[0] != ret[1]) return False;
287 /* reopen the file */
288 for (server=0;server<NSERVERS;server++) {
289 cli_close(cli[server][conn], fnum[server][conn][f]);
291 for (server=0;server<NSERVERS;server++) {
292 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
295 if (fnum[server][conn][f] == -1) {
296 printf("failed to reopen on share%d\n", server);
301 printf("reopen conn=%u f=%u\n",
309 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
310 int fnum[NSERVERS][NCONNECTIONS][NFILES])
314 for (server=0;server<NSERVERS;server++)
315 for (conn=0;conn<NCONNECTIONS;conn++)
316 for (f=0;f<NFILES;f++) {
317 if (fnum[server][conn][f] != -1) {
318 cli_close(cli[server][conn], fnum[server][conn][f]);
319 fnum[server][conn][f] = -1;
322 for (server=0;server<NSERVERS;server++) {
323 cli_unlink(cli[server][0], FILENAME);
327 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
328 int fnum[NSERVERS][NCONNECTIONS][NFILES])
332 for (server=0;server<NSERVERS;server++)
333 for (conn=0;conn<NCONNECTIONS;conn++)
334 for (f=0;f<NFILES;f++) {
335 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
338 if (fnum[server][conn][f] == -1) {
339 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
347 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
348 int fnum[NSERVERS][NCONNECTIONS][NFILES],
352 printf("testing %u ...\n", n);
353 for (i=0; i<n; i++) {
354 if (i && i % 100 == 0) {
358 if (recorded[i].needed &&
359 !test_one(cli, fnum, &recorded[i])) return i;
365 /* each server has two connections open to it. Each connection has two file
366 descriptors open on the file - 8 file descriptors in total
368 we then do random locking ops in tamdem on the 4 fnums from each
369 server and ensure that the results match
371 static void test_locks(char *share[NSERVERS])
373 struct cli_state *cli[NSERVERS][NCONNECTIONS];
374 int fnum[NSERVERS][NCONNECTIONS][NFILES];
380 recorded = (struct record *)malloc(sizeof(*recorded) * numops);
382 for (n=0; n<numops; n++) {
384 if (n < sizeof(preset) / sizeof(preset[0])) {
385 recorded[n] = preset[n];
388 recorded[n].conn = random() % NCONNECTIONS;
389 recorded[n].f = random() % NFILES;
390 recorded[n].start = LOCKBASE + ((unsigned)random() % (LOCKRANGE-1));
392 random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
393 recorded[n].start *= RANGE_MULTIPLE;
394 recorded[n].len *= RANGE_MULTIPLE;
395 recorded[n].r1 = random() % 100;
396 recorded[n].r2 = random() % 100;
397 recorded[n].needed = True;
403 reconnect(cli, fnum, share);
404 open_files(cli, fnum);
405 n = retest(cli, fnum, numops);
407 if (n == numops || !analyze) return;
413 close_files(cli, fnum);
414 reconnect(cli, fnum, share);
415 open_files(cli, fnum);
417 for (i=0;i<n-1;i++) {
419 recorded[i].needed = False;
421 close_files(cli, fnum);
422 open_files(cli, fnum);
424 m = retest(cli, fnum, n);
426 recorded[i].needed = True;
429 memmove(&recorded[i], &recorded[i+1],
430 (m-i)*sizeof(recorded[0]));
440 close_files(cli, fnum);
441 reconnect(cli, fnum, share);
442 open_files(cli, fnum);
444 n1 = retest(cli, fnum, n);
446 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
448 close_files(cli, fnum);
451 printf("{%u, %u, %u, %u, %.0f, %.0f, %u},\n",
456 (double)recorded[i].start,
457 (double)recorded[i].len,
464 static void usage(void)
468 locktest //server1/share1 //server2/share2 [options..]\n\
473 -u hide unlock fails\n\
479 /****************************************************************************
481 ****************************************************************************/
482 int main(int argc,char *argv[])
484 char *share[NSERVERS];
491 static pstring servicesf = CONFIGFILE;
497 if (argc < 3 || argv[1][0] == '-') {
502 setup_logging(argv[0],True);
504 for (server=0;server<NSERVERS;server++) {
505 share[server] = argv[1+server];
506 all_string_sub(share[server],"/","\\",0);
514 lp_load(servicesf,True,False,False);
517 if (getenv("USER")) {
518 pstrcpy(username,getenv("USER"));
523 while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
526 pstrcpy(username,optarg);
527 p = strchr_m(username,'%');
530 pstrcpy(password, p+1);
538 hide_unlock_fails = True;
541 numops = atoi(optarg);
556 printf("Unknown option %c (%d)\n", (char)opt, opt);
564 DEBUG(0,("seed=%u\n", seed));