s3: Update waf build to include missed dependancy on Lion.
[Samba/gebeck_regimport.git] / source3 / torture / locktest.c
blob288ce8fe543a28a406fc77de7b85030628f61360
1 /*
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/>.
20 #include "includes.h"
21 #include "libsmb/libsmb.h"
22 #include "system/filesys.h"
23 #include "locking/proto.h"
24 #include "libsmb/nmblib.h"
26 static fstring password[2];
27 static fstring username[2];
28 static int got_user;
29 static int got_pass;
30 static bool use_kerberos;
31 static int numops = 1000;
32 static bool showall;
33 static bool analyze;
34 static bool hide_unlock_fails;
35 static bool use_oplocks;
36 static unsigned lock_range = 100;
37 static unsigned lock_base = 0;
38 static unsigned min_length = 0;
39 static bool exact_error_codes;
40 static bool zero_zero;
42 extern char *optarg;
43 extern int optind;
45 #define FILENAME "\\locktest.dat"
47 #define READ_PCT 50
48 #define LOCK_PCT 45
49 #define UNLOCK_PCT 70
50 #define RANGE_MULTIPLE 1
51 #define NSERVERS 2
52 #define NCONNECTIONS 2
53 #define NFILES 2
54 #define LOCK_TIMEOUT 0
56 #define NASTY_POSIX_LOCK_HACK 0
58 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
60 static const char *lock_op_type(int op)
62 if (op == WRITE_LOCK) return "write";
63 else if (op == READ_LOCK) return "read";
64 else return "other";
67 static const char *lock_op_name(enum lock_op op)
69 if (op == OP_LOCK) return "lock";
70 else if (op == OP_UNLOCK) return "unlock";
71 else return "reopen";
74 struct record {
75 enum lock_op lock_op;
76 enum brl_type lock_type;
77 char conn, f;
78 uint64_t start, len;
79 char needed;
82 #define PRESETS 0
84 #if PRESETS
85 static struct record preset[] = {
86 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
87 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
88 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 0, 1},
89 {OP_UNLOCK, 0 , 0, 0, 2, 0, 1},
90 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
92 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
93 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
94 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
95 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
97 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
98 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 1, 1},
99 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
100 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
102 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
103 {OP_LOCK, WRITE_LOCK, 0, 0, 1, 1, 1},
104 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
105 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
107 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
108 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
109 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
110 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
112 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
113 {OP_LOCK, READ_LOCK, 0, 0, 3, 1, 1},
114 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
115 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
118 #endif
120 static struct record *recorded;
122 static void print_brl(struct file_id id,
123 struct server_id pid,
124 enum brl_type lock_type,
125 enum brl_flavour lock_flav,
126 br_off start,
127 br_off size,
128 void *private_data)
130 #if NASTY_POSIX_LOCK_HACK
132 static SMB_INO_T lastino;
134 if (lastino != ino) {
135 char *cmd;
136 if (asprintf(&cmd,
137 "egrep POSIX.*%u /proc/locks", (int)ino) > 0) {
138 system(cmd);
139 SAFE_FREE(cmd);
142 lastino = ino;
144 #endif
146 printf("%s %s %s %.0f:%.0f(%.0f)\n",
147 procid_str_static(&pid), file_id_string_tos(&id),
148 lock_type==READ_LOCK?"R":"W",
149 (double)start, (double)start+size-1,(double)size);
154 static void show_locks(void)
156 brl_forall(print_brl, NULL);
157 /* system("cat /proc/locks"); */
161 /*****************************************************
162 return a connection to a server
163 *******************************************************/
164 static struct cli_state *connect_one(char *share, int snum)
166 struct cli_state *c;
167 char *server_n;
168 fstring server;
169 fstring myname;
170 static int count;
171 NTSTATUS status;
172 int flags = 0;
174 fstrcpy(server,share+2);
175 share = strchr_m(server,'\\');
176 if (!share) return NULL;
177 *share = 0;
178 share++;
180 server_n = server;
182 slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
184 /* have to open a new connection */
186 if (use_kerberos) {
187 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
189 if (use_oplocks) {
190 flags |= CLI_FULL_CONNECTION_OPLOCKS;
193 status = cli_connect_nb(server_n, NULL, 0, 0x20, myname,
194 SMB_SIGNING_DEFAULT, flags, &c);
195 if (!NT_STATUS_IS_OK(status)) {
196 DEBUG(0, ("Connection to %s failed. Error %s\n", server_n,
197 nt_errstr(status)));
198 return NULL;
201 status = cli_negprot(c, PROTOCOL_NT1);
202 if (!NT_STATUS_IS_OK(status)) {
203 DEBUG(0, ("protocol negotiation failed: %s\n",
204 nt_errstr(status)));
205 cli_shutdown(c);
206 return NULL;
209 if (!got_pass) {
210 char *pass = getpass("Password: ");
211 if (pass) {
212 fstrcpy(password[0], pass);
213 fstrcpy(password[1], pass);
217 if (got_pass == 1) {
218 strlcpy(password[1], password[0],sizeof(password[1]));
219 strlcpy(username[1], username[0],sizeof(username[1]));
222 status = cli_session_setup(c, username[snum],
223 password[snum], strlen(password[snum]),
224 password[snum], strlen(password[snum]),
225 lp_workgroup());
226 if (!NT_STATUS_IS_OK(status)) {
227 DEBUG(0,("session setup failed: %s\n", nt_errstr(status)));
228 return NULL;
232 * These next two lines are needed to emulate
233 * old client behaviour for people who have
234 * scripts based on client output.
235 * QUESTION ? Do we want to have a 'client compatibility
236 * mode to turn these on/off ? JRA.
239 if (*c->server_domain || *c->server_os || *c->server_type)
240 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
241 c->server_domain,c->server_os,c->server_type));
243 DEBUG(4,(" session setup ok\n"));
245 status = cli_tree_connect(c, share, "?????", password[snum],
246 strlen(password[snum])+1);
247 if (!NT_STATUS_IS_OK(status)) {
248 DEBUG(0,("tree connect failed: %s\n", nt_errstr(status)));
249 cli_shutdown(c);
250 return NULL;
253 DEBUG(4,(" tconx ok\n"));
255 return c;
259 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS], uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES],
260 char *share[NSERVERS])
262 int server, conn, f;
264 for (server=0;server<NSERVERS;server++)
265 for (conn=0;conn<NCONNECTIONS;conn++) {
266 if (cli[server][conn]) {
267 for (f=0;f<NFILES;f++) {
268 if (fnum[server][conn][f] != (uint16_t)-1) {
269 cli_close(cli[server][conn], fnum[server][conn][f]);
270 fnum[server][conn][f] = (uint16_t)-1;
273 cli_ulogoff(cli[server][conn]);
274 cli_shutdown(cli[server][conn]);
276 cli[server][conn] = connect_one(share[server], server);
277 if (!cli[server][conn]) {
278 DEBUG(0,("Failed to connect to %s\n", share[server]));
279 exit(1);
286 static bool test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
287 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES],
288 struct record *rec)
290 unsigned conn = rec->conn;
291 unsigned f = rec->f;
292 uint64_t start = rec->start;
293 uint64_t len = rec->len;
294 enum brl_type op = rec->lock_type;
295 int server;
296 NTSTATUS status[NSERVERS];
298 switch (rec->lock_op) {
299 case OP_LOCK:
300 /* set a lock */
301 for (server=0;server<NSERVERS;server++) {
302 status[server] = cli_lock64(cli[server][conn],
303 fnum[server][conn][f],
304 start, len, LOCK_TIMEOUT,
305 op);
306 if (!exact_error_codes &&
307 NT_STATUS_EQUAL(status[server],
308 NT_STATUS_FILE_LOCK_CONFLICT)) {
309 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
312 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
313 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
314 conn, f,
315 (double)start, (double)len,
316 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
317 nt_errstr(status[0]), nt_errstr(status[1]));
319 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
320 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
321 break;
323 case OP_UNLOCK:
324 /* unset a lock */
325 for (server=0;server<NSERVERS;server++) {
326 status[server] = cli_unlock64(cli[server][conn],
327 fnum[server][conn][f],
328 start, len);
330 if (showall ||
331 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
332 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
333 conn, f,
334 (double)start, (double)len,
335 nt_errstr(status[0]), nt_errstr(status[1]));
337 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
338 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
339 return False;
340 break;
342 case OP_REOPEN:
343 /* reopen the file */
344 for (server=0;server<NSERVERS;server++) {
345 cli_close(cli[server][conn], fnum[server][conn][f]);
346 fnum[server][conn][f] = (uint16_t)-1;
348 for (server=0;server<NSERVERS;server++) {
349 fnum[server][conn][f] = (uint16_t)-1;
350 if (!NT_STATUS_IS_OK(cli_openx(cli[server][conn], FILENAME,
351 O_RDWR|O_CREAT,
352 DENY_NONE, &fnum[server][conn][f]))) {
353 printf("failed to reopen on share%d\n", server);
354 return False;
357 if (showall) {
358 printf("reopen conn=%u f=%u\n",
359 conn, f);
360 show_locks();
362 break;
365 return True;
368 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
369 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES])
371 int server, conn, f;
373 for (server=0;server<NSERVERS;server++)
374 for (conn=0;conn<NCONNECTIONS;conn++)
375 for (f=0;f<NFILES;f++) {
376 if (fnum[server][conn][f] != (uint16_t)-1) {
377 cli_close(cli[server][conn], fnum[server][conn][f]);
378 fnum[server][conn][f] = (uint16_t)-1;
381 for (server=0;server<NSERVERS;server++) {
382 cli_unlink(cli[server][0], FILENAME, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
386 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
387 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES])
389 int server, conn, f;
391 for (server=0;server<NSERVERS;server++)
392 for (conn=0;conn<NCONNECTIONS;conn++)
393 for (f=0;f<NFILES;f++) {
394 fnum[server][conn][f] = (uint16_t)-1;
395 if (!NT_STATUS_IS_OK(cli_openx(cli[server][conn], FILENAME,
396 O_RDWR|O_CREAT,
397 DENY_NONE,
398 &fnum[server][conn][f]))) {
399 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
400 server, conn, f);
401 exit(1);
407 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
408 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES],
409 int n)
411 int i;
412 printf("testing %u ...\n", n);
413 for (i=0; i<n; i++) {
414 if (i && i % 100 == 0) {
415 printf("%u\n", i);
418 if (recorded[i].needed &&
419 !test_one(cli, fnum, &recorded[i])) return i;
421 return n;
425 /* each server has two connections open to it. Each connection has two file
426 descriptors open on the file - 8 file descriptors in total
428 we then do random locking ops in tamdem on the 4 fnums from each
429 server and ensure that the results match
431 static void test_locks(char *share[NSERVERS])
433 struct cli_state *cli[NSERVERS][NCONNECTIONS];
434 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES];
435 int n, i, n1, skip, r1, r2;
437 ZERO_STRUCT(fnum);
438 ZERO_STRUCT(cli);
440 recorded = SMB_MALLOC_ARRAY(struct record, numops);
442 for (n=0; n<numops; n++) {
443 #if PRESETS
444 if (n < sizeof(preset) / sizeof(preset[0])) {
445 recorded[n] = preset[n];
446 } else {
447 #endif
448 recorded[n].conn = random() % NCONNECTIONS;
449 recorded[n].f = random() % NFILES;
450 recorded[n].start = lock_base + ((unsigned)random() % (lock_range-1));
451 recorded[n].len = min_length +
452 random() % (lock_range-(recorded[n].start-lock_base));
453 recorded[n].start *= RANGE_MULTIPLE;
454 recorded[n].len *= RANGE_MULTIPLE;
455 r1 = random() % 100;
456 r2 = random() % 100;
457 if (r1 < READ_PCT) {
458 recorded[n].lock_type = READ_LOCK;
459 } else {
460 recorded[n].lock_type = WRITE_LOCK;
462 if (r2 < LOCK_PCT) {
463 recorded[n].lock_op = OP_LOCK;
464 } else if (r2 < UNLOCK_PCT) {
465 recorded[n].lock_op = OP_UNLOCK;
466 } else {
467 recorded[n].lock_op = OP_REOPEN;
469 recorded[n].needed = True;
470 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
471 recorded[n].len = 1;
473 #if PRESETS
475 #endif
478 reconnect(cli, fnum, share);
479 open_files(cli, fnum);
480 n = retest(cli, fnum, numops);
482 if (n == numops || !analyze) return;
483 n++;
485 skip = n/2;
487 while (1) {
488 n1 = n;
490 close_files(cli, fnum);
491 reconnect(cli, fnum, share);
492 open_files(cli, fnum);
494 for (i=0;i<n-skip;i+=skip) {
495 int m, j;
496 printf("excluding %d-%d\n", i, i+skip-1);
497 for (j=i;j<i+skip;j++) {
498 recorded[j].needed = False;
501 close_files(cli, fnum);
502 open_files(cli, fnum);
504 m = retest(cli, fnum, n);
505 if (m == n) {
506 for (j=i;j<i+skip;j++) {
507 recorded[j].needed = True;
509 } else {
510 if (i+(skip-1) < m) {
511 memmove(&recorded[i], &recorded[i+skip],
512 (m-(i+skip-1))*sizeof(recorded[0]));
514 n = m-(skip-1);
515 i--;
519 if (skip > 1) {
520 skip = skip/2;
521 printf("skip=%d\n", skip);
522 continue;
525 if (n1 == n) break;
528 close_files(cli, fnum);
529 reconnect(cli, fnum, share);
530 open_files(cli, fnum);
531 showall = True;
532 n1 = retest(cli, fnum, n);
533 if (n1 != n-1) {
534 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
536 close_files(cli, fnum);
538 for (i=0;i<n;i++) {
539 printf("{%s, %s, conn = %u, file = %u, start = %.0f, len = %.0f, %u},\n",
540 lock_op_name(recorded[i].lock_op),
541 lock_op_type(recorded[i].lock_type),
542 recorded[i].conn,
543 recorded[i].f,
544 (double)recorded[i].start,
545 (double)recorded[i].len,
546 recorded[i].needed);
552 static void usage(void)
554 printf(
555 "Usage:\n\
556 locktest //server1/share1 //server2/share2 [options..]\n\
557 options:\n\
558 -U user%%pass (may be specified twice)\n\
559 -k use kerberos\n\
560 -s seed\n\
561 -o numops\n\
562 -u hide unlock fails\n\
563 -a (show all ops)\n\
564 -A analyse for minimal ops\n\
565 -O use oplocks\n\
566 -E enable exact error code checking\n\
567 -Z enable the zero/zero lock\n\
568 -R range set lock range\n\
569 -B base set lock base\n\
570 -M min set min lock length\n\
574 /****************************************************************************
575 main program
576 ****************************************************************************/
577 int main(int argc,char *argv[])
579 char *share[NSERVERS];
580 int opt;
581 char *p;
582 int seed, server;
584 setlinebuf(stdout);
586 load_case_tables();
588 if (argc < 3 || argv[1][0] == '-') {
589 usage();
590 exit(1);
593 setup_logging(argv[0], DEBUG_STDOUT);
595 for (server=0;server<NSERVERS;server++) {
596 share[server] = argv[1+server];
597 all_string_sub(share[server],"/","\\",0);
600 argc -= NSERVERS;
601 argv += NSERVERS;
603 lp_load_global(get_dyn_CONFIGFILE());
604 load_interfaces();
606 if (getenv("USER")) {
607 fstrcpy(username[0],getenv("USER"));
608 fstrcpy(username[1],getenv("USER"));
611 seed = time(NULL);
613 while ((opt = getopt(argc, argv, "U:s:ho:aAW:OkR:B:M:EZ")) != EOF) {
614 switch (opt) {
615 case 'k':
616 #ifdef HAVE_KRB5
617 use_kerberos = True;
618 #else
619 d_printf("No kerberos support compiled in\n");
620 exit(1);
621 #endif
622 break;
623 case 'U':
624 got_user = 1;
625 if (got_pass == 2) {
626 d_printf("Max of 2 usernames\n");
627 exit(1);
629 fstrcpy(username[got_pass],optarg);
630 p = strchr_m(username[got_pass],'%');
631 if (p) {
632 *p = 0;
633 fstrcpy(password[got_pass], p+1);
634 got_pass++;
636 break;
637 case 'R':
638 lock_range = strtol(optarg, NULL, 0);
639 break;
640 case 'B':
641 lock_base = strtol(optarg, NULL, 0);
642 break;
643 case 'M':
644 min_length = strtol(optarg, NULL, 0);
645 break;
646 case 's':
647 seed = atoi(optarg);
648 break;
649 case 'u':
650 hide_unlock_fails = True;
651 break;
652 case 'o':
653 numops = atoi(optarg);
654 break;
655 case 'O':
656 use_oplocks = True;
657 break;
658 case 'a':
659 showall = True;
660 break;
661 case 'A':
662 analyze = True;
663 break;
664 case 'Z':
665 zero_zero = True;
666 break;
667 case 'E':
668 exact_error_codes = True;
669 break;
670 case 'h':
671 usage();
672 exit(1);
673 default:
674 printf("Unknown option %c (%d)\n", (char)opt, opt);
675 exit(1);
679 if(use_kerberos && !got_user) got_pass = True;
681 argc -= optind;
682 argv += optind;
684 DEBUG(0,("seed=%u\n", seed));
685 srandom(seed);
687 test_locks(share);
689 return(0);