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"
41 #define LOCKBASE (0x40000000 - 50)
47 #define RANGE_MULTIPLE 1
49 #define NCONNECTIONS 2
51 #define LOCK_TIMEOUT 0
53 #define NASTY_POSIX_LOCK_HACK 0
59 SMB_BIG_UINT start, len;
66 static struct record preset[] = {
67 {36, 5, 0, 0, 0, 8, 1},
68 { 2, 6, 0, 1, 0, 1, 1},
69 {53, 92, 0, 0, 0, 0, 1},
70 {99, 11, 0, 0, 7, 1, 1},
74 static struct record *recorded;
76 static void print_brl(SMB_DEV_T dev, SMB_INO_T ino, int pid,
77 enum brl_type lock_type,
78 br_off start, br_off size)
80 #if NASTY_POSIX_LOCK_HACK
83 static SMB_INO_T lastino;
86 slprintf(cmd, sizeof(cmd),
87 "egrep POSIX.*%u /proc/locks", (int)ino);
94 printf("%6d %05x:%05x %s %.0f:%.0f(%.0f)\n",
95 (int)pid, (int)dev, (int)ino,
96 lock_type==READ_LOCK?"R":"W",
97 (double)start, (double)start+size-1,(double)size);
102 static void show_locks(void)
104 brl_forall(print_brl);
105 /* system("cat /proc/locks"); */
109 /*****************************************************
110 return a connection to a server
111 *******************************************************/
112 struct cli_state *connect_one(char *share)
115 struct nmb_name called, calling;
119 extern struct in_addr ipzero;
123 fstrcpy(server,share+2);
124 share = strchr_m(server,'\\');
125 if (!share) return NULL;
133 slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), count++);
135 make_nmb_name(&calling, myname, 0x0);
136 make_nmb_name(&called , server, 0x20);
141 /* have to open a new connection */
142 if (!(c=cli_initialise(NULL)) || (cli_set_port(c, 139) == 0) ||
143 !cli_connect(c, server_n, &ip)) {
144 DEBUG(0,("Connection to %s failed\n", server_n));
148 if (!cli_session_request(c, &calling, &called)) {
149 DEBUG(0,("session request to %s failed\n", called.name));
151 if (strcmp(called.name, "*SMBSERVER")) {
152 make_nmb_name(&called , "*SMBSERVER", 0x20);
158 DEBUG(4,(" session request ok\n"));
160 if (!cli_negprot(c)) {
161 DEBUG(0,("protocol negotiation failed\n"));
167 char *pass = getpass("Password: ");
169 pstrcpy(password, pass);
173 if (!cli_session_setup(c, username,
174 password, strlen(password),
175 password, strlen(password),
177 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
182 * These next two lines are needed to emulate
183 * old client behaviour for people who have
184 * scripts based on client output.
185 * QUESTION ? Do we want to have a 'client compatibility
186 * mode to turn these on/off ? JRA.
189 if (*c->server_domain || *c->server_os || *c->server_type)
190 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
191 c->server_domain,c->server_os,c->server_type));
193 DEBUG(4,(" session setup ok\n"));
195 if (!cli_send_tconX(c, share, "?????",
196 password, strlen(password)+1)) {
197 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
202 DEBUG(4,(" tconx ok\n"));
204 c->use_oplocks = use_oplocks;
210 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
211 char *share[NSERVERS])
215 for (server=0;server<NSERVERS;server++)
216 for (conn=0;conn<NCONNECTIONS;conn++) {
217 if (cli[server][conn]) {
218 for (f=0;f<NFILES;f++) {
219 cli_close(cli[server][conn], fnum[server][conn][f]);
221 cli_ulogoff(cli[server][conn]);
222 cli_shutdown(cli[server][conn]);
223 free(cli[server][conn]);
224 cli[server][conn] = NULL;
226 cli[server][conn] = connect_one(share[server]);
227 if (!cli[server][conn]) {
228 DEBUG(0,("Failed to connect to %s\n", share[server]));
236 static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
237 int fnum[NSERVERS][NCONNECTIONS][NFILES],
240 unsigned conn = rec->conn;
242 SMB_BIG_UINT start = rec->start;
243 SMB_BIG_UINT len = rec->len;
244 unsigned r1 = rec->r1;
245 unsigned r2 = rec->r2;
258 for (server=0;server<NSERVERS;server++) {
259 ret[server] = cli_lock64(cli[server][conn],
260 fnum[server][conn][f],
261 start, len, LOCK_TIMEOUT, op);
263 if (showall || ret[0] != ret[1]) {
264 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %u:%u\n",
266 (double)start, (double)len,
267 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
270 if (showall || ret[0] != ret[1]) show_locks();
271 if (ret[0] != ret[1]) return False;
272 } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
274 for (server=0;server<NSERVERS;server++) {
275 ret[server] = cli_unlock64(cli[server][conn],
276 fnum[server][conn][f],
279 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
280 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %u:%u\n",
282 (double)start, (double)len,
285 if (showall || ret[0] != ret[1]) show_locks();
286 if (!hide_unlock_fails && ret[0] != ret[1]) return False;
288 /* reopen the file */
289 for (server=0;server<NSERVERS;server++) {
290 cli_close(cli[server][conn], fnum[server][conn][f]);
292 for (server=0;server<NSERVERS;server++) {
293 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
296 if (fnum[server][conn][f] == -1) {
297 printf("failed to reopen on share%d\n", server);
302 printf("reopen conn=%u f=%u\n",
310 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
311 int fnum[NSERVERS][NCONNECTIONS][NFILES])
315 for (server=0;server<NSERVERS;server++)
316 for (conn=0;conn<NCONNECTIONS;conn++)
317 for (f=0;f<NFILES;f++) {
318 if (fnum[server][conn][f] != -1) {
319 cli_close(cli[server][conn], fnum[server][conn][f]);
320 fnum[server][conn][f] = -1;
323 for (server=0;server<NSERVERS;server++) {
324 cli_unlink(cli[server][0], FILENAME);
328 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
329 int fnum[NSERVERS][NCONNECTIONS][NFILES])
333 for (server=0;server<NSERVERS;server++)
334 for (conn=0;conn<NCONNECTIONS;conn++)
335 for (f=0;f<NFILES;f++) {
336 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
339 if (fnum[server][conn][f] == -1) {
340 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
348 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
349 int fnum[NSERVERS][NCONNECTIONS][NFILES],
353 printf("testing %u ...\n", n);
354 for (i=0; i<n; i++) {
355 if (i && i % 100 == 0) {
359 if (recorded[i].needed &&
360 !test_one(cli, fnum, &recorded[i])) return i;
366 /* each server has two connections open to it. Each connection has two file
367 descriptors open on the file - 8 file descriptors in total
369 we then do random locking ops in tamdem on the 4 fnums from each
370 server and ensure that the results match
372 static void test_locks(char *share[NSERVERS])
374 struct cli_state *cli[NSERVERS][NCONNECTIONS];
375 int fnum[NSERVERS][NCONNECTIONS][NFILES];
381 recorded = (struct record *)malloc(sizeof(*recorded) * numops);
383 for (n=0; n<numops; n++) {
385 if (n < sizeof(preset) / sizeof(preset[0])) {
386 recorded[n] = preset[n];
389 recorded[n].conn = random() % NCONNECTIONS;
390 recorded[n].f = random() % NFILES;
391 recorded[n].start = LOCKBASE + ((unsigned)random() % (LOCKRANGE-1));
392 recorded[n].len = MINLENGTH +
393 random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
394 recorded[n].start *= RANGE_MULTIPLE;
395 recorded[n].len *= RANGE_MULTIPLE;
396 recorded[n].r1 = random() % 100;
397 recorded[n].r2 = random() % 100;
398 recorded[n].needed = True;
404 reconnect(cli, fnum, share);
405 open_files(cli, fnum);
406 n = retest(cli, fnum, numops);
408 if (n == numops || !analyze) return;
416 close_files(cli, fnum);
417 reconnect(cli, fnum, share);
418 open_files(cli, fnum);
420 for (i=0;i<n-skip;i++) {
422 for (j=i;j<i+skip;j++) {
423 recorded[j].needed = False;
426 close_files(cli, fnum);
427 open_files(cli, fnum);
429 m = retest(cli, fnum, n);
431 for (j=i;j<i+skip;j++) {
432 recorded[j].needed = True;
435 if (i+(skip-1) < m) {
436 memmove(&recorded[i], &recorded[i+skip],
437 (m-(i+skip-1))*sizeof(recorded[0]));
446 printf("skip=%d\n", skip);
453 close_files(cli, fnum);
454 reconnect(cli, fnum, share);
455 open_files(cli, fnum);
457 n1 = retest(cli, fnum, n);
459 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
461 close_files(cli, fnum);
464 printf("{%u, %u, %u, %u, %.0f, %.0f, %u},\n",
469 (double)recorded[i].start,
470 (double)recorded[i].len,
477 static void usage(void)
481 locktest //server1/share1 //server2/share2 [options..]\n\
486 -u hide unlock fails\n\
492 /****************************************************************************
494 ****************************************************************************/
495 int main(int argc,char *argv[])
497 char *share[NSERVERS];
504 static pstring servicesf = CONFIGFILE;
510 if (argc < 3 || argv[1][0] == '-') {
515 setup_logging(argv[0],True);
517 for (server=0;server<NSERVERS;server++) {
518 share[server] = argv[1+server];
519 all_string_sub(share[server],"/","\\",0);
527 lp_load(servicesf,True,False,False);
530 if (getenv("USER")) {
531 pstrcpy(username,getenv("USER"));
536 while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
539 pstrcpy(username,optarg);
540 p = strchr_m(username,'%');
543 pstrcpy(password, p+1);
551 hide_unlock_fails = True;
554 numops = atoi(optarg);
569 printf("Unknown option %c (%d)\n", (char)opt, opt);
577 DEBUG(0,("seed=%u\n", seed));