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
;
121 fstrcpy(server
,share
+2);
122 share
= strchr(server
,'\\');
123 if (!share
) return NULL
;
131 slprintf(myname
,sizeof(myname
), "lock-%u-%u", getpid(), count
++);
133 make_nmb_name(&calling
, myname
, 0x0);
134 make_nmb_name(&called
, server
, 0x20);
139 /* have to open a new connection */
140 if (!(c
=cli_initialise(NULL
)) || (cli_set_port(c
, 139) == 0) ||
141 !cli_connect(c
, server_n
, &ip
)) {
142 DEBUG(0,("Connection to %s failed\n", server_n
));
146 if (!cli_session_request(c
, &calling
, &called
)) {
147 DEBUG(0,("session request to %s failed\n", called
.name
));
149 if (strcmp(called
.name
, "*SMBSERVER")) {
150 make_nmb_name(&called
, "*SMBSERVER", 0x20);
156 DEBUG(4,(" session request ok\n"));
158 if (!cli_negprot(c
)) {
159 DEBUG(0,("protocol negotiation failed\n"));
165 char *pass
= getpass("Password: ");
167 pstrcpy(password
, pass
);
171 if (!cli_session_setup(c
, username
,
172 password
, strlen(password
),
173 password
, strlen(password
),
175 DEBUG(0,("session setup failed: %s\n", cli_errstr(c
)));
180 * These next two lines are needed to emulate
181 * old client behaviour for people who have
182 * scripts based on client output.
183 * QUESTION ? Do we want to have a 'client compatibility
184 * mode to turn these on/off ? JRA.
187 if (*c
->server_domain
|| *c
->server_os
|| *c
->server_type
)
188 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
189 c
->server_domain
,c
->server_os
,c
->server_type
));
191 DEBUG(4,(" session setup ok\n"));
193 if (!cli_send_tconX(c
, share
, "?????",
194 password
, strlen(password
)+1)) {
195 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c
)));
200 DEBUG(4,(" tconx ok\n"));
202 c
->use_oplocks
= use_oplocks
;
208 static void reconnect(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
], int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
],
209 char *share
[NSERVERS
])
213 for (server
=0;server
<NSERVERS
;server
++)
214 for (conn
=0;conn
<NCONNECTIONS
;conn
++) {
215 if (cli
[server
][conn
]) {
216 for (f
=0;f
<NFILES
;f
++) {
217 cli_close(cli
[server
][conn
], fnum
[server
][conn
][f
]);
219 cli_ulogoff(cli
[server
][conn
]);
220 cli_shutdown(cli
[server
][conn
]);
221 SAFE_FREE(cli
[server
][conn
]);
222 cli
[server
][conn
] = NULL
;
224 cli
[server
][conn
] = connect_one(share
[server
]);
225 if (!cli
[server
][conn
]) {
226 DEBUG(0,("Failed to connect to %s\n", share
[server
]));
234 static BOOL
test_one(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
235 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
],
238 unsigned conn
= rec
->conn
;
240 SMB_BIG_UINT start
= rec
->start
;
241 SMB_BIG_UINT len
= rec
->len
;
242 unsigned r1
= rec
->r1
;
243 unsigned r2
= rec
->r2
;
256 for (server
=0;server
<NSERVERS
;server
++) {
257 ret
[server
] = cli_lock64(cli
[server
][conn
],
258 fnum
[server
][conn
][f
],
259 start
, len
, LOCK_TIMEOUT
, op
);
261 if (showall
|| ret
[0] != ret
[1]) {
262 printf("lock conn=%u f=%u range=%.0f:%.0f(%.0f) op=%s -> %u:%u\n",
264 (double)start
, (double)start
+len
-1, (double)len
,
265 op
==READ_LOCK
?"READ_LOCK":"WRITE_LOCK",
268 if (showall
|| ret
[0] != ret
[1]) show_locks();
269 if (ret
[0] != ret
[1]) return False
;
270 } else if (r2
< LOCK_PCT
+UNLOCK_PCT
) {
272 for (server
=0;server
<NSERVERS
;server
++) {
273 ret
[server
] = cli_unlock64(cli
[server
][conn
],
274 fnum
[server
][conn
][f
],
277 if (showall
|| (!hide_unlock_fails
&& (ret
[0] != ret
[1]))) {
278 printf("unlock conn=%u f=%u range=%.0f:%.0f(%.0f) -> %u:%u\n",
280 (double)start
, (double)start
+len
-1, (double)len
,
283 if (showall
|| ret
[0] != ret
[1]) show_locks();
284 if (!hide_unlock_fails
&& ret
[0] != ret
[1]) return False
;
286 /* reopen the file */
287 for (server
=0;server
<NSERVERS
;server
++) {
288 cli_close(cli
[server
][conn
], fnum
[server
][conn
][f
]);
290 for (server
=0;server
<NSERVERS
;server
++) {
291 fnum
[server
][conn
][f
] = cli_open(cli
[server
][conn
], FILENAME
,
294 if (fnum
[server
][conn
][f
] == -1) {
295 printf("failed to reopen on share%d\n", server
);
300 printf("reopen conn=%u f=%u\n",
308 static void close_files(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
309 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
])
313 for (server
=0;server
<NSERVERS
;server
++)
314 for (conn
=0;conn
<NCONNECTIONS
;conn
++)
315 for (f
=0;f
<NFILES
;f
++) {
316 if (fnum
[server
][conn
][f
] != -1) {
317 cli_close(cli
[server
][conn
], fnum
[server
][conn
][f
]);
318 fnum
[server
][conn
][f
] = -1;
321 for (server
=0;server
<NSERVERS
;server
++) {
322 cli_unlink(cli
[server
][0], FILENAME
);
326 static void open_files(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
327 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
])
331 for (server
=0;server
<NSERVERS
;server
++)
332 for (conn
=0;conn
<NCONNECTIONS
;conn
++)
333 for (f
=0;f
<NFILES
;f
++) {
334 fnum
[server
][conn
][f
] = cli_open(cli
[server
][conn
], FILENAME
,
337 if (fnum
[server
][conn
][f
] == -1) {
338 fprintf(stderr
,"Failed to open fnum[%u][%u][%u]\n",
346 static int retest(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
347 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
],
351 printf("testing %u ...\n", n
);
352 for (i
=0; i
<n
; i
++) {
353 if (i
&& i
% 100 == 0) {
357 if (recorded
[i
].needed
&&
358 !test_one(cli
, fnum
, &recorded
[i
])) return i
;
364 /* each server has two connections open to it. Each connection has two file
365 descriptors open on the file - 8 file descriptors in total
367 we then do random locking ops in tamdem on the 4 fnums from each
368 server and ensure that the results match
370 static void test_locks(char *share
[NSERVERS
])
372 struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
];
373 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
];
379 recorded
= (struct record
*)malloc(sizeof(*recorded
) * numops
);
381 for (n
=0; n
<numops
; n
++) {
383 if (n
< sizeof(preset
) / sizeof(preset
[0])) {
384 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));
391 recorded
[n
].len
= 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
;
401 reconnect(cli
, fnum
, share
);
402 open_files(cli
, fnum
);
403 n
= retest(cli
, fnum
, numops
);
405 if (n
== numops
|| !analyze
) return;
411 close_files(cli
, fnum
);
412 reconnect(cli
, fnum
, share
);
413 open_files(cli
, fnum
);
415 for (i
=0;i
<n
-1;i
++) {
417 recorded
[i
].needed
= False
;
419 close_files(cli
, fnum
);
420 open_files(cli
, fnum
);
422 m
= retest(cli
, fnum
, n
);
424 recorded
[i
].needed
= True
;
427 memmove(&recorded
[i
], &recorded
[i
+1],
428 (m
-i
)*sizeof(recorded
[0]));
438 close_files(cli
, fnum
);
439 reconnect(cli
, fnum
, share
);
440 open_files(cli
, fnum
);
442 n1
= retest(cli
, fnum
, n
);
444 printf("ERROR - inconsistent result (%u %u)\n", n1
, n
);
446 close_files(cli
, fnum
);
449 printf("{%u, %u, %u, %u, %.0f, %.0f, %u},\n",
454 (double)recorded
[i
].start
,
455 (double)recorded
[i
].len
,
462 static void usage(void)
466 locktest //server1/share1 //server2/share2 [options..]\n\
471 -u hide unlock fails\n\
477 /****************************************************************************
479 ****************************************************************************/
480 int main(int argc
,char *argv
[])
482 char *share
[NSERVERS
];
489 static pstring servicesf
= CONFIGFILE
;
495 if (argc
< 3 || argv
[1][0] == '-') {
500 setup_logging(argv
[0],True
);
502 for (server
=0;server
<NSERVERS
;server
++) {
503 share
[server
] = argv
[1+server
];
504 all_string_sub(share
[server
],"/","\\",0);
511 charset_initialise();
512 codepage_initialise(lp_client_code_page());
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(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
));