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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 static fstring password[2];
24 static fstring username[2];
27 static BOOL use_kerberos;
28 static int numops = 1000;
31 static BOOL hide_unlock_fails;
32 static BOOL use_oplocks;
33 static unsigned lock_range = 100;
34 static unsigned lock_base = 0;
35 static unsigned min_length = 0;
36 static BOOL exact_error_codes;
37 static BOOL zero_zero;
39 #define FILENAME "\\locktest.dat"
44 #define RANGE_MULTIPLE 1
46 #define NCONNECTIONS 2
48 #define LOCK_TIMEOUT 0
50 #define NASTY_POSIX_LOCK_HACK 0
52 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
56 enum brl_type lock_type;
58 SMB_BIG_UINT start, len;
65 static struct record preset[] = {
66 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
67 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
68 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 0, 1},
69 {OP_UNLOCK, 0 , 0, 0, 2, 0, 1},
70 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
72 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
73 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
74 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
75 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
77 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
78 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 1, 1},
79 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
80 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
82 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
83 {OP_LOCK, WRITE_LOCK, 0, 0, 1, 1, 1},
84 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
85 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
87 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
88 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
89 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
90 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
92 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
93 {OP_LOCK, READ_LOCK, 0, 0, 3, 1, 1},
94 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
95 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
100 static struct record *recorded;
102 /*****************************************************
103 return a connection to a server
104 *******************************************************/
105 static struct cli_state *connect_one(char *share, int snum)
108 fstring server, myname;
112 fstrcpy(server,share+2);
113 share = strchr_m(server,'\\');
114 if (!share) return NULL;
118 slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), snum);
121 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
123 status = cli_full_connection(&c, myname,
126 username[snum], lp_workgroup(),
127 password[snum], flags, NULL);
129 if (!NT_STATUS_IS_OK(status)) {
137 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
138 char *share[NSERVERS])
142 for (server=0;server<NSERVERS;server++)
143 for (conn=0;conn<NCONNECTIONS;conn++) {
144 if (cli[server][conn]) {
145 for (f=0;f<NFILES;f++) {
146 if (fnum[server][conn][f] != -1) {
147 cli_close(cli[server][conn], fnum[server][conn][f]);
148 fnum[server][conn][f] = -1;
151 cli_shutdown(cli[server][conn]);
153 cli[server][conn] = connect_one(share[server], server);
154 if (!cli[server][conn]) {
155 DEBUG(0,("Failed to connect to %s\n", share[server]));
163 static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
164 int fnum[NSERVERS][NCONNECTIONS][NFILES],
167 unsigned conn = rec->conn;
169 SMB_BIG_UINT start = rec->start;
170 SMB_BIG_UINT len = rec->len;
171 enum brl_type op = rec->lock_type;
174 NTSTATUS status[NSERVERS];
176 switch (rec->lock_op) {
179 for (server=0;server<NSERVERS;server++) {
180 ret[server] = cli_lock64(cli[server][conn],
181 fnum[server][conn][f],
182 start, len, LOCK_TIMEOUT, op);
183 status[server] = cli_nt_error(cli[server][conn]);
184 if (!exact_error_codes &&
185 NT_STATUS_EQUAL(status[server],
186 NT_STATUS_FILE_LOCK_CONFLICT)) {
187 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
190 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
191 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
193 (double)start, (double)len,
194 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
195 nt_errstr(status[0]), nt_errstr(status[1]));
197 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
202 for (server=0;server<NSERVERS;server++) {
203 ret[server] = cli_unlock64(cli[server][conn],
204 fnum[server][conn][f],
206 status[server] = cli_nt_error(cli[server][conn]);
209 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
210 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
212 (double)start, (double)len,
213 nt_errstr(status[0]), nt_errstr(status[1]));
215 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
220 /* reopen the file */
221 for (server=0;server<NSERVERS;server++) {
222 cli_close(cli[server][conn], fnum[server][conn][f]);
223 fnum[server][conn][f] = -1;
225 for (server=0;server<NSERVERS;server++) {
226 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
229 if (fnum[server][conn][f] == -1) {
230 printf("failed to reopen on share%d\n", server);
235 printf("reopen conn=%u f=%u\n",
244 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
245 int fnum[NSERVERS][NCONNECTIONS][NFILES])
249 for (server=0;server<NSERVERS;server++)
250 for (conn=0;conn<NCONNECTIONS;conn++)
251 for (f=0;f<NFILES;f++) {
252 if (fnum[server][conn][f] != -1) {
253 cli_close(cli[server][conn], fnum[server][conn][f]);
254 fnum[server][conn][f] = -1;
257 for (server=0;server<NSERVERS;server++) {
258 cli_unlink(cli[server][0], FILENAME);
262 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
263 int fnum[NSERVERS][NCONNECTIONS][NFILES])
267 for (server=0;server<NSERVERS;server++)
268 for (conn=0;conn<NCONNECTIONS;conn++)
269 for (f=0;f<NFILES;f++) {
270 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
273 if (fnum[server][conn][f] == -1) {
274 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
282 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
283 int fnum[NSERVERS][NCONNECTIONS][NFILES],
287 printf("testing %u ...\n", n);
288 for (i=0; i<n; i++) {
289 if (i && i % 100 == 0) {
293 if (recorded[i].needed &&
294 !test_one(cli, fnum, &recorded[i])) return i;
300 /* each server has two connections open to it. Each connection has two file
301 descriptors open on the file - 8 file descriptors in total
303 we then do random locking ops in tamdem on the 4 fnums from each
304 server and ensure that the results match
306 static void test_locks(char *share[NSERVERS])
308 struct cli_state *cli[NSERVERS][NCONNECTIONS];
309 int fnum[NSERVERS][NCONNECTIONS][NFILES];
310 int n, i, n1, skip, r1, r2;
315 recorded = (struct record *)malloc(sizeof(*recorded) * numops);
317 for (n=0; n<numops; n++) {
319 if (n < sizeof(preset) / sizeof(preset[0])) {
320 recorded[n] = preset[n];
323 recorded[n].conn = random() % NCONNECTIONS;
324 recorded[n].f = random() % NFILES;
325 recorded[n].start = lock_base + ((unsigned)random() % (lock_range-1));
326 recorded[n].len = min_length +
327 random() % (lock_range-(recorded[n].start-lock_base));
328 recorded[n].start *= RANGE_MULTIPLE;
329 recorded[n].len *= RANGE_MULTIPLE;
333 recorded[n].lock_type = READ_LOCK;
335 recorded[n].lock_type = WRITE_LOCK;
338 recorded[n].lock_op = OP_LOCK;
339 } else if (r2 < UNLOCK_PCT) {
340 recorded[n].lock_op = OP_UNLOCK;
342 recorded[n].lock_op = OP_REOPEN;
344 recorded[n].needed = True;
345 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
353 reconnect(cli, fnum, share);
354 open_files(cli, fnum);
355 n = retest(cli, fnum, numops);
357 if (n == numops || !analyze) return;
365 close_files(cli, fnum);
366 reconnect(cli, fnum, share);
367 open_files(cli, fnum);
369 for (i=0;i<n-skip;i+=skip) {
371 printf("excluding %d-%d\n", i, i+skip-1);
372 for (j=i;j<i+skip;j++) {
373 recorded[j].needed = False;
376 close_files(cli, fnum);
377 open_files(cli, fnum);
379 m = retest(cli, fnum, n);
381 for (j=i;j<i+skip;j++) {
382 recorded[j].needed = True;
385 if (i+(skip-1) < m) {
386 memmove(&recorded[i], &recorded[i+skip],
387 (m-(i+skip-1))*sizeof(recorded[0]));
396 printf("skip=%d\n", skip);
403 close_files(cli, fnum);
404 reconnect(cli, fnum, share);
405 open_files(cli, fnum);
407 n1 = retest(cli, fnum, n);
409 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
411 close_files(cli, fnum);
414 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
416 recorded[i].lock_type,
419 (double)recorded[i].start,
420 (double)recorded[i].len,
427 static void usage(void)
431 locktest //server1/share1 //server2/share2 [options..]\n\
433 -U user%%pass (may be specified twice)\n\
437 -u hide unlock fails\n\
439 -A analyse for minimal ops\n\
441 -E enable exact error code checking\n\
442 -Z enable the zero/zero lock\n\
443 -R range set lock range\n\
444 -B base set lock base\n\
445 -M min set min lock length\n\
449 /****************************************************************************
451 ****************************************************************************/
452 int main(int argc,char *argv[])
454 char *share[NSERVERS];
461 setup_logging("locktest", DEBUG_STDOUT);
463 if (argc < 3 || argv[1][0] == '-') {
468 setup_logging(argv[0],True);
470 for (server=0;server<NSERVERS;server++) {
471 share[server] = argv[1+server];
472 all_string_sub(share[server],"/","\\",0);
478 lp_load(dyn_CONFIGFILE,True,False,False);
481 if (getenv("USER")) {
482 fstrcpy(username[0],getenv("USER"));
483 fstrcpy(username[1],getenv("USER"));
488 while ((opt = getopt(argc, argv, "U:s:ho:aAW:OkR:B:M:EZ")) != EOF) {
494 d_printf("No kerberos support compiled in\n");
501 d_printf("Max of 2 usernames\n");
504 fstrcpy(username[got_pass],optarg);
505 p = strchr_m(username[got_pass],'%');
508 fstrcpy(password[got_pass], p+1);
513 lock_range = strtol(optarg, NULL, 0);
516 lock_base = strtol(optarg, NULL, 0);
519 min_length = strtol(optarg, NULL, 0);
525 hide_unlock_fails = True;
528 numops = atoi(optarg);
543 exact_error_codes = True;
549 printf("Unknown option %c (%d)\n", (char)opt, opt);
554 if(use_kerberos && !got_user) got_pass = True;
559 DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n",
560 seed, lock_base, lock_range, min_length));