2 Unix SMB/CIFS implementation.
3 randomised byte range lock tester
4 Copyright (C) Andrew Tridgell 1999
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 static fstring password
[2];
23 static fstring username
[2];
26 static BOOL use_kerberos
;
27 static int numops
= 1000;
30 static BOOL hide_unlock_fails
;
31 static BOOL use_oplocks
;
32 static unsigned lock_range
= 100;
33 static unsigned lock_base
= 0;
34 static unsigned min_length
= 0;
35 static BOOL exact_error_codes
;
36 static BOOL zero_zero
;
41 #define FILENAME "\\locktest.dat"
46 #define RANGE_MULTIPLE 1
48 #define NCONNECTIONS 2
50 #define LOCK_TIMEOUT 0
52 #define NASTY_POSIX_LOCK_HACK 0
54 enum lock_op
{OP_LOCK
, OP_UNLOCK
, OP_REOPEN
};
56 static const char *lock_op_type(int op
)
58 if (op
== WRITE_LOCK
) return "write";
59 else if (op
== READ_LOCK
) return "read";
63 static const char *lock_op_name(enum lock_op op
)
65 if (op
== OP_LOCK
) return "lock";
66 else if (op
== OP_UNLOCK
) return "unlock";
72 enum brl_type lock_type
;
74 SMB_BIG_UINT start
, len
;
81 static struct record preset
[] = {
82 {OP_LOCK
, WRITE_LOCK
, 0, 0, 2, 0, 1},
83 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
84 {OP_LOCK
, WRITE_LOCK
, 0, 0, 3, 0, 1},
85 {OP_UNLOCK
, 0 , 0, 0, 2, 0, 1},
86 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
88 {OP_LOCK
, READ_LOCK
, 0, 0, 2, 0, 1},
89 {OP_LOCK
, READ_LOCK
, 0, 0, 1, 1, 1},
90 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
91 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
93 {OP_LOCK
, READ_LOCK
, 0, 0, 2, 0, 1},
94 {OP_LOCK
, WRITE_LOCK
, 0, 0, 3, 1, 1},
95 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
96 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
98 {OP_LOCK
, READ_LOCK
, 0, 0, 2, 0, 1},
99 {OP_LOCK
, WRITE_LOCK
, 0, 0, 1, 1, 1},
100 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
101 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
103 {OP_LOCK
, WRITE_LOCK
, 0, 0, 2, 0, 1},
104 {OP_LOCK
, READ_LOCK
, 0, 0, 1, 1, 1},
105 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
106 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
108 {OP_LOCK
, WRITE_LOCK
, 0, 0, 2, 0, 1},
109 {OP_LOCK
, READ_LOCK
, 0, 0, 3, 1, 1},
110 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
111 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
116 static struct record
*recorded
;
118 static void print_brl(struct file_id id
,
119 struct server_id pid
,
120 enum brl_type lock_type
,
121 enum brl_flavour lock_flav
,
126 #if NASTY_POSIX_LOCK_HACK
129 static SMB_INO_T lastino
;
131 if (lastino
!= ino
) {
132 slprintf(cmd
, sizeof(cmd
),
133 "egrep POSIX.*%u /proc/locks", (int)ino
);
140 printf("%s %s %s %.0f:%.0f(%.0f)\n",
141 procid_str_static(&pid
), file_id_static_string(&id
),
142 lock_type
==READ_LOCK
?"R":"W",
143 (double)start
, (double)start
+size
-1,(double)size
);
148 static void show_locks(void)
150 brl_forall(print_brl
, NULL
);
151 /* system("cat /proc/locks"); */
155 /*****************************************************
156 return a connection to a server
157 *******************************************************/
158 static struct cli_state
*connect_one(char *share
, int snum
)
161 struct nmb_name called
, calling
;
169 fstrcpy(server
,share
+2);
170 share
= strchr_m(server
,'\\');
171 if (!share
) return NULL
;
179 slprintf(myname
,sizeof(myname
), "lock-%lu-%u", (unsigned long)getpid(), count
++);
181 make_nmb_name(&calling
, myname
, 0x0);
182 make_nmb_name(&called
, server
, 0x20);
187 /* have to open a new connection */
188 if (!(c
=cli_initialise())) {
189 DEBUG(0,("Connection to %s failed\n", server_n
));
193 status
= cli_connect(c
, server_n
, &ip
);
194 if (!NT_STATUS_IS_OK(status
)) {
195 DEBUG(0,("Connection to %s failed. Error %s\n", server_n
, nt_errstr(status
) ));
199 c
->use_kerberos
= use_kerberos
;
201 if (!cli_session_request(c
, &calling
, &called
)) {
202 DEBUG(0,("session request to %s failed\n", called
.name
));
204 if (strcmp(called
.name
, "*SMBSERVER")) {
205 make_nmb_name(&called
, "*SMBSERVER", 0x20);
211 DEBUG(4,(" session request ok\n"));
213 if (!cli_negprot(c
)) {
214 DEBUG(0,("protocol negotiation failed\n"));
220 char *pass
= getpass("Password: ");
222 fstrcpy(password
[0], pass
);
223 fstrcpy(password
[1], pass
);
228 fstrcpy(password
[1], password
[0]);
229 fstrcpy(username
[1], username
[0]);
232 if (!NT_STATUS_IS_OK(cli_session_setup(c
, username
[snum
],
234 strlen(password
[snum
]),
236 strlen(password
[snum
]),
238 DEBUG(0,("session setup failed: %s\n", cli_errstr(c
)));
243 * These next two lines are needed to emulate
244 * old client behaviour for people who have
245 * scripts based on client output.
246 * QUESTION ? Do we want to have a 'client compatibility
247 * mode to turn these on/off ? JRA.
250 if (*c
->server_domain
|| *c
->server_os
|| *c
->server_type
)
251 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
252 c
->server_domain
,c
->server_os
,c
->server_type
));
254 DEBUG(4,(" session setup ok\n"));
256 if (!cli_send_tconX(c
, share
, "?????",
257 password
[snum
], strlen(password
[snum
])+1)) {
258 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c
)));
263 DEBUG(4,(" tconx ok\n"));
265 c
->use_oplocks
= use_oplocks
;
271 static void reconnect(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
], int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
],
272 char *share
[NSERVERS
])
276 for (server
=0;server
<NSERVERS
;server
++)
277 for (conn
=0;conn
<NCONNECTIONS
;conn
++) {
278 if (cli
[server
][conn
]) {
279 for (f
=0;f
<NFILES
;f
++) {
280 if (fnum
[server
][conn
][f
] != -1) {
281 cli_close(cli
[server
][conn
], fnum
[server
][conn
][f
]);
282 fnum
[server
][conn
][f
] = -1;
285 cli_ulogoff(cli
[server
][conn
]);
286 cli_shutdown(cli
[server
][conn
]);
288 cli
[server
][conn
] = connect_one(share
[server
], server
);
289 if (!cli
[server
][conn
]) {
290 DEBUG(0,("Failed to connect to %s\n", share
[server
]));
298 static BOOL
test_one(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
299 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
],
302 unsigned conn
= rec
->conn
;
304 SMB_BIG_UINT start
= rec
->start
;
305 SMB_BIG_UINT len
= rec
->len
;
306 enum brl_type op
= rec
->lock_type
;
309 NTSTATUS status
[NSERVERS
];
311 switch (rec
->lock_op
) {
314 for (server
=0;server
<NSERVERS
;server
++) {
315 ret
[server
] = cli_lock64(cli
[server
][conn
],
316 fnum
[server
][conn
][f
],
317 start
, len
, LOCK_TIMEOUT
, op
);
318 status
[server
] = cli_nt_error(cli
[server
][conn
]);
319 if (!exact_error_codes
&&
320 NT_STATUS_EQUAL(status
[server
],
321 NT_STATUS_FILE_LOCK_CONFLICT
)) {
322 status
[server
] = NT_STATUS_LOCK_NOT_GRANTED
;
325 if (showall
|| !NT_STATUS_EQUAL(status
[0],status
[1])) {
326 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
328 (double)start
, (double)len
,
329 op
==READ_LOCK
?"READ_LOCK":"WRITE_LOCK",
330 nt_errstr(status
[0]), nt_errstr(status
[1]));
332 if (showall
|| !NT_STATUS_EQUAL(status
[0],status
[1])) show_locks();
333 if (!NT_STATUS_EQUAL(status
[0],status
[1])) return False
;
338 for (server
=0;server
<NSERVERS
;server
++) {
339 ret
[server
] = cli_unlock64(cli
[server
][conn
],
340 fnum
[server
][conn
][f
],
342 status
[server
] = cli_nt_error(cli
[server
][conn
]);
345 (!hide_unlock_fails
&& !NT_STATUS_EQUAL(status
[0],status
[1]))) {
346 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
348 (double)start
, (double)len
,
349 nt_errstr(status
[0]), nt_errstr(status
[1]));
351 if (showall
|| !NT_STATUS_EQUAL(status
[0],status
[1])) show_locks();
352 if (!hide_unlock_fails
&& !NT_STATUS_EQUAL(status
[0],status
[1]))
357 /* reopen the file */
358 for (server
=0;server
<NSERVERS
;server
++) {
359 cli_close(cli
[server
][conn
], fnum
[server
][conn
][f
]);
360 fnum
[server
][conn
][f
] = -1;
362 for (server
=0;server
<NSERVERS
;server
++) {
363 fnum
[server
][conn
][f
] = cli_open(cli
[server
][conn
], FILENAME
,
366 if (fnum
[server
][conn
][f
] == -1) {
367 printf("failed to reopen on share%d\n", server
);
372 printf("reopen conn=%u f=%u\n",
382 static void close_files(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
383 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
])
387 for (server
=0;server
<NSERVERS
;server
++)
388 for (conn
=0;conn
<NCONNECTIONS
;conn
++)
389 for (f
=0;f
<NFILES
;f
++) {
390 if (fnum
[server
][conn
][f
] != -1) {
391 cli_close(cli
[server
][conn
], fnum
[server
][conn
][f
]);
392 fnum
[server
][conn
][f
] = -1;
395 for (server
=0;server
<NSERVERS
;server
++) {
396 cli_unlink(cli
[server
][0], FILENAME
);
400 static void open_files(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
401 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
])
405 for (server
=0;server
<NSERVERS
;server
++)
406 for (conn
=0;conn
<NCONNECTIONS
;conn
++)
407 for (f
=0;f
<NFILES
;f
++) {
408 fnum
[server
][conn
][f
] = cli_open(cli
[server
][conn
], FILENAME
,
411 if (fnum
[server
][conn
][f
] == -1) {
412 fprintf(stderr
,"Failed to open fnum[%u][%u][%u]\n",
420 static int retest(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
421 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
],
425 printf("testing %u ...\n", n
);
426 for (i
=0; i
<n
; i
++) {
427 if (i
&& i
% 100 == 0) {
431 if (recorded
[i
].needed
&&
432 !test_one(cli
, fnum
, &recorded
[i
])) return i
;
438 /* each server has two connections open to it. Each connection has two file
439 descriptors open on the file - 8 file descriptors in total
441 we then do random locking ops in tamdem on the 4 fnums from each
442 server and ensure that the results match
444 static void test_locks(char *share
[NSERVERS
])
446 struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
];
447 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
];
448 int n
, i
, n1
, skip
, r1
, r2
;
453 recorded
= SMB_MALLOC_ARRAY(struct record
, numops
);
455 for (n
=0; n
<numops
; n
++) {
457 if (n
< sizeof(preset
) / sizeof(preset
[0])) {
458 recorded
[n
] = preset
[n
];
461 recorded
[n
].conn
= random() % NCONNECTIONS
;
462 recorded
[n
].f
= random() % NFILES
;
463 recorded
[n
].start
= lock_base
+ ((unsigned)random() % (lock_range
-1));
464 recorded
[n
].len
= min_length
+
465 random() % (lock_range
-(recorded
[n
].start
-lock_base
));
466 recorded
[n
].start
*= RANGE_MULTIPLE
;
467 recorded
[n
].len
*= RANGE_MULTIPLE
;
471 recorded
[n
].lock_type
= READ_LOCK
;
473 recorded
[n
].lock_type
= WRITE_LOCK
;
476 recorded
[n
].lock_op
= OP_LOCK
;
477 } else if (r2
< UNLOCK_PCT
) {
478 recorded
[n
].lock_op
= OP_UNLOCK
;
480 recorded
[n
].lock_op
= OP_REOPEN
;
482 recorded
[n
].needed
= True
;
483 if (!zero_zero
&& recorded
[n
].start
==0 && recorded
[n
].len
==0) {
491 reconnect(cli
, fnum
, share
);
492 open_files(cli
, fnum
);
493 n
= retest(cli
, fnum
, numops
);
495 if (n
== numops
|| !analyze
) return;
503 close_files(cli
, fnum
);
504 reconnect(cli
, fnum
, share
);
505 open_files(cli
, fnum
);
507 for (i
=0;i
<n
-skip
;i
+=skip
) {
509 printf("excluding %d-%d\n", i
, i
+skip
-1);
510 for (j
=i
;j
<i
+skip
;j
++) {
511 recorded
[j
].needed
= False
;
514 close_files(cli
, fnum
);
515 open_files(cli
, fnum
);
517 m
= retest(cli
, fnum
, n
);
519 for (j
=i
;j
<i
+skip
;j
++) {
520 recorded
[j
].needed
= True
;
523 if (i
+(skip
-1) < m
) {
524 memmove(&recorded
[i
], &recorded
[i
+skip
],
525 (m
-(i
+skip
-1))*sizeof(recorded
[0]));
534 printf("skip=%d\n", skip
);
541 close_files(cli
, fnum
);
542 reconnect(cli
, fnum
, share
);
543 open_files(cli
, fnum
);
545 n1
= retest(cli
, fnum
, n
);
547 printf("ERROR - inconsistent result (%u %u)\n", n1
, n
);
549 close_files(cli
, fnum
);
552 printf("{%s, %s, conn = %u, file = %u, start = %.0f, len = %.0f, %u},\n",
553 lock_op_name(recorded
[i
].lock_op
),
554 lock_op_type(recorded
[i
].lock_type
),
557 (double)recorded
[i
].start
,
558 (double)recorded
[i
].len
,
565 static void usage(void)
569 locktest //server1/share1 //server2/share2 [options..]\n\
571 -U user%%pass (may be specified twice)\n\
575 -u hide unlock fails\n\
577 -A analyse for minimal ops\n\
579 -E enable exact error code checking\n\
580 -Z enable the zero/zero lock\n\
581 -R range set lock range\n\
582 -B base set lock base\n\
583 -M min set min lock length\n\
587 /****************************************************************************
589 ****************************************************************************/
590 int main(int argc
,char *argv
[])
592 char *share
[NSERVERS
];
603 if (argc
< 3 || argv
[1][0] == '-') {
608 setup_logging(argv
[0],True
);
610 for (server
=0;server
<NSERVERS
;server
++) {
611 share
[server
] = argv
[1+server
];
612 all_string_sub(share
[server
],"/","\\",0);
618 lp_load(dyn_CONFIGFILE
,True
,False
,False
,True
);
621 if (getenv("USER")) {
622 fstrcpy(username
[0],getenv("USER"));
623 fstrcpy(username
[1],getenv("USER"));
628 while ((opt
= getopt(argc
, argv
, "U:s:ho:aAW:OkR:B:M:EZ")) != EOF
) {
634 d_printf("No kerberos support compiled in\n");
641 d_printf("Max of 2 usernames\n");
644 fstrcpy(username
[got_pass
],optarg
);
645 p
= strchr_m(username
[got_pass
],'%');
648 fstrcpy(password
[got_pass
], p
+1);
653 lock_range
= strtol(optarg
, NULL
, 0);
656 lock_base
= strtol(optarg
, NULL
, 0);
659 min_length
= strtol(optarg
, NULL
, 0);
665 hide_unlock_fails
= True
;
668 numops
= atoi(optarg
);
683 exact_error_codes
= True
;
689 printf("Unknown option %c (%d)\n", (char)opt
, opt
);
694 if(use_kerberos
&& !got_user
) got_pass
= True
;
699 DEBUG(0,("seed=%u\n", seed
));