r18865: fixed some of the most obvious NTSTATUS/WERROR mixups in Samba3. It
[Samba/nascimento.git] / source3 / torture / locktest.c
blobc3200ca038c7e195c6ff233d4cd607bf460dbdcb
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 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.
21 #include "includes.h"
23 static fstring password[2];
24 static fstring username[2];
25 static int got_user;
26 static int got_pass;
27 static BOOL use_kerberos;
28 static int numops = 1000;
29 static BOOL showall;
30 static BOOL analyze;
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"
41 #define READ_PCT 50
42 #define LOCK_PCT 45
43 #define UNLOCK_PCT 70
44 #define RANGE_MULTIPLE 1
45 #define NSERVERS 2
46 #define NCONNECTIONS 2
47 #define NFILES 2
48 #define LOCK_TIMEOUT 0
50 #define NASTY_POSIX_LOCK_HACK 0
52 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
54 const char *lock_op_type(int op)
56 if (op == WRITE_LOCK) return "write";
57 else if (op == READ_LOCK) return "read";
58 else return "other";
61 const char *lock_op_name(enum lock_op op)
63 if (op == OP_LOCK) return "lock";
64 else if (op == OP_UNLOCK) return "unlock";
65 else return "reopen";
68 struct record {
69 enum lock_op lock_op;
70 enum brl_type lock_type;
71 char conn, f;
72 SMB_BIG_UINT start, len;
73 char needed;
76 #define PRESETS 0
78 #if PRESETS
79 static struct record preset[] = {
80 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
81 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
82 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 0, 1},
83 {OP_UNLOCK, 0 , 0, 0, 2, 0, 1},
84 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
86 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
87 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
88 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
89 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
91 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
92 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 1, 1},
93 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
94 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
96 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
97 {OP_LOCK, WRITE_LOCK, 0, 0, 1, 1, 1},
98 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
99 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
101 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
102 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
103 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
104 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
106 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
107 {OP_LOCK, READ_LOCK, 0, 0, 3, 1, 1},
108 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
109 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
112 #endif
114 static struct record *recorded;
116 static void print_brl(SMB_DEV_T dev,
117 SMB_INO_T ino,
118 struct process_id pid,
119 enum brl_type lock_type,
120 enum brl_flavour lock_flav,
121 br_off start,
122 br_off size)
124 #if NASTY_POSIX_LOCK_HACK
126 pstring cmd;
127 static SMB_INO_T lastino;
129 if (lastino != ino) {
130 slprintf(cmd, sizeof(cmd),
131 "egrep POSIX.*%u /proc/locks", (int)ino);
132 system(cmd);
134 lastino = ino;
136 #endif
138 printf("%s %05x:%05x %s %.0f:%.0f(%.0f)\n",
139 procid_str_static(&pid), (int)dev, (int)ino,
140 lock_type==READ_LOCK?"R":"W",
141 (double)start, (double)start+size-1,(double)size);
146 static void show_locks(void)
148 brl_forall(print_brl);
149 /* system("cat /proc/locks"); */
153 /*****************************************************
154 return a connection to a server
155 *******************************************************/
156 static struct cli_state *connect_one(char *share, int snum)
158 struct cli_state *c;
159 struct nmb_name called, calling;
160 char *server_n;
161 fstring server;
162 struct in_addr ip;
163 fstring myname;
164 static int count;
166 fstrcpy(server,share+2);
167 share = strchr_m(server,'\\');
168 if (!share) return NULL;
169 *share = 0;
170 share++;
172 server_n = server;
174 zero_ip(&ip);
176 slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
178 make_nmb_name(&calling, myname, 0x0);
179 make_nmb_name(&called , server, 0x20);
181 again:
182 zero_ip(&ip);
184 /* have to open a new connection */
185 if (!(c=cli_initialise()) || !cli_connect(c, server_n, &ip)) {
186 DEBUG(0,("Connection to %s failed\n", server_n));
187 return NULL;
190 c->use_kerberos = use_kerberos;
192 if (!cli_session_request(c, &calling, &called)) {
193 DEBUG(0,("session request to %s failed\n", called.name));
194 cli_shutdown(c);
195 if (strcmp(called.name, "*SMBSERVER")) {
196 make_nmb_name(&called , "*SMBSERVER", 0x20);
197 goto again;
199 return NULL;
202 DEBUG(4,(" session request ok\n"));
204 if (!cli_negprot(c)) {
205 DEBUG(0,("protocol negotiation failed\n"));
206 cli_shutdown(c);
207 return NULL;
210 if (!got_pass) {
211 char *pass = getpass("Password: ");
212 if (pass) {
213 fstrcpy(password[0], pass);
214 fstrcpy(password[1], pass);
218 if (got_pass == 1) {
219 fstrcpy(password[1], password[0]);
220 fstrcpy(username[1], username[0]);
223 if (!NT_STATUS_IS_OK(cli_session_setup(c, username[snum],
224 password[snum],
225 strlen(password[snum]),
226 password[snum],
227 strlen(password[snum]),
228 lp_workgroup()))) {
229 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
230 return NULL;
234 * These next two lines are needed to emulate
235 * old client behaviour for people who have
236 * scripts based on client output.
237 * QUESTION ? Do we want to have a 'client compatibility
238 * mode to turn these on/off ? JRA.
241 if (*c->server_domain || *c->server_os || *c->server_type)
242 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
243 c->server_domain,c->server_os,c->server_type));
245 DEBUG(4,(" session setup ok\n"));
247 if (!cli_send_tconX(c, share, "?????",
248 password[snum], strlen(password[snum])+1)) {
249 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
250 cli_shutdown(c);
251 return NULL;
254 DEBUG(4,(" tconx ok\n"));
256 c->use_oplocks = use_oplocks;
258 return c;
262 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
263 char *share[NSERVERS])
265 int server, conn, f;
267 for (server=0;server<NSERVERS;server++)
268 for (conn=0;conn<NCONNECTIONS;conn++) {
269 if (cli[server][conn]) {
270 for (f=0;f<NFILES;f++) {
271 if (fnum[server][conn][f] != -1) {
272 cli_close(cli[server][conn], fnum[server][conn][f]);
273 fnum[server][conn][f] = -1;
276 cli_ulogoff(cli[server][conn]);
277 cli_shutdown(cli[server][conn]);
279 cli[server][conn] = connect_one(share[server], server);
280 if (!cli[server][conn]) {
281 DEBUG(0,("Failed to connect to %s\n", share[server]));
282 exit(1);
289 static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
290 int fnum[NSERVERS][NCONNECTIONS][NFILES],
291 struct record *rec)
293 unsigned conn = rec->conn;
294 unsigned f = rec->f;
295 SMB_BIG_UINT start = rec->start;
296 SMB_BIG_UINT len = rec->len;
297 enum brl_type op = rec->lock_type;
298 int server;
299 BOOL ret[NSERVERS];
300 NTSTATUS status[NSERVERS];
302 switch (rec->lock_op) {
303 case OP_LOCK:
304 /* set a lock */
305 for (server=0;server<NSERVERS;server++) {
306 ret[server] = cli_lock64(cli[server][conn],
307 fnum[server][conn][f],
308 start, len, LOCK_TIMEOUT, op);
309 status[server] = cli_nt_error(cli[server][conn]);
310 if (!exact_error_codes &&
311 NT_STATUS_EQUAL(status[server],
312 NT_STATUS_FILE_LOCK_CONFLICT)) {
313 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
316 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
317 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
318 conn, f,
319 (double)start, (double)len,
320 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
321 nt_errstr(status[0]), nt_errstr(status[1]));
323 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
324 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
325 break;
327 case OP_UNLOCK:
328 /* unset a lock */
329 for (server=0;server<NSERVERS;server++) {
330 ret[server] = cli_unlock64(cli[server][conn],
331 fnum[server][conn][f],
332 start, len);
333 status[server] = cli_nt_error(cli[server][conn]);
335 if (showall ||
336 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
337 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
338 conn, f,
339 (double)start, (double)len,
340 nt_errstr(status[0]), nt_errstr(status[1]));
342 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
343 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
344 return False;
345 break;
347 case OP_REOPEN:
348 /* reopen the file */
349 for (server=0;server<NSERVERS;server++) {
350 cli_close(cli[server][conn], fnum[server][conn][f]);
351 fnum[server][conn][f] = -1;
353 for (server=0;server<NSERVERS;server++) {
354 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
355 O_RDWR|O_CREAT,
356 DENY_NONE);
357 if (fnum[server][conn][f] == -1) {
358 printf("failed to reopen on share%d\n", server);
359 return False;
362 if (showall) {
363 printf("reopen conn=%u f=%u\n",
364 conn, f);
365 show_locks();
367 break;
370 return True;
373 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
374 int fnum[NSERVERS][NCONNECTIONS][NFILES])
376 int server, conn, f;
378 for (server=0;server<NSERVERS;server++)
379 for (conn=0;conn<NCONNECTIONS;conn++)
380 for (f=0;f<NFILES;f++) {
381 if (fnum[server][conn][f] != -1) {
382 cli_close(cli[server][conn], fnum[server][conn][f]);
383 fnum[server][conn][f] = -1;
386 for (server=0;server<NSERVERS;server++) {
387 cli_unlink(cli[server][0], FILENAME);
391 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
392 int fnum[NSERVERS][NCONNECTIONS][NFILES])
394 int server, conn, f;
396 for (server=0;server<NSERVERS;server++)
397 for (conn=0;conn<NCONNECTIONS;conn++)
398 for (f=0;f<NFILES;f++) {
399 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
400 O_RDWR|O_CREAT,
401 DENY_NONE);
402 if (fnum[server][conn][f] == -1) {
403 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
404 server, conn, f);
405 exit(1);
411 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
412 int fnum[NSERVERS][NCONNECTIONS][NFILES],
413 int n)
415 int i;
416 printf("testing %u ...\n", n);
417 for (i=0; i<n; i++) {
418 if (i && i % 100 == 0) {
419 printf("%u\n", i);
422 if (recorded[i].needed &&
423 !test_one(cli, fnum, &recorded[i])) return i;
425 return n;
429 /* each server has two connections open to it. Each connection has two file
430 descriptors open on the file - 8 file descriptors in total
432 we then do random locking ops in tamdem on the 4 fnums from each
433 server and ensure that the results match
435 static void test_locks(char *share[NSERVERS])
437 struct cli_state *cli[NSERVERS][NCONNECTIONS];
438 int fnum[NSERVERS][NCONNECTIONS][NFILES];
439 int n, i, n1, skip, r1, r2;
441 ZERO_STRUCT(fnum);
442 ZERO_STRUCT(cli);
444 recorded = SMB_MALLOC_ARRAY(struct record, numops);
446 for (n=0; n<numops; n++) {
447 #if PRESETS
448 if (n < sizeof(preset) / sizeof(preset[0])) {
449 recorded[n] = preset[n];
450 } else {
451 #endif
452 recorded[n].conn = random() % NCONNECTIONS;
453 recorded[n].f = random() % NFILES;
454 recorded[n].start = lock_base + ((unsigned)random() % (lock_range-1));
455 recorded[n].len = min_length +
456 random() % (lock_range-(recorded[n].start-lock_base));
457 recorded[n].start *= RANGE_MULTIPLE;
458 recorded[n].len *= RANGE_MULTIPLE;
459 r1 = random() % 100;
460 r2 = random() % 100;
461 if (r1 < READ_PCT) {
462 recorded[n].lock_type = READ_LOCK;
463 } else {
464 recorded[n].lock_type = WRITE_LOCK;
466 if (r2 < LOCK_PCT) {
467 recorded[n].lock_op = OP_LOCK;
468 } else if (r2 < UNLOCK_PCT) {
469 recorded[n].lock_op = OP_UNLOCK;
470 } else {
471 recorded[n].lock_op = OP_REOPEN;
473 recorded[n].needed = True;
474 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
475 recorded[n].len = 1;
477 #if PRESETS
479 #endif
482 reconnect(cli, fnum, share);
483 open_files(cli, fnum);
484 n = retest(cli, fnum, numops);
486 if (n == numops || !analyze) return;
487 n++;
489 skip = n/2;
491 while (1) {
492 n1 = n;
494 close_files(cli, fnum);
495 reconnect(cli, fnum, share);
496 open_files(cli, fnum);
498 for (i=0;i<n-skip;i+=skip) {
499 int m, j;
500 printf("excluding %d-%d\n", i, i+skip-1);
501 for (j=i;j<i+skip;j++) {
502 recorded[j].needed = False;
505 close_files(cli, fnum);
506 open_files(cli, fnum);
508 m = retest(cli, fnum, n);
509 if (m == n) {
510 for (j=i;j<i+skip;j++) {
511 recorded[j].needed = True;
513 } else {
514 if (i+(skip-1) < m) {
515 memmove(&recorded[i], &recorded[i+skip],
516 (m-(i+skip-1))*sizeof(recorded[0]));
518 n = m-(skip-1);
519 i--;
523 if (skip > 1) {
524 skip = skip/2;
525 printf("skip=%d\n", skip);
526 continue;
529 if (n1 == n) break;
532 close_files(cli, fnum);
533 reconnect(cli, fnum, share);
534 open_files(cli, fnum);
535 showall = True;
536 n1 = retest(cli, fnum, n);
537 if (n1 != n-1) {
538 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
540 close_files(cli, fnum);
542 for (i=0;i<n;i++) {
543 printf("{%s, %s, conn = %u, file = %u, start = %.0f, len = %.0f, %u},\n",
544 lock_op_name(recorded[i].lock_op),
545 lock_op_type(recorded[i].lock_type),
546 recorded[i].conn,
547 recorded[i].f,
548 (double)recorded[i].start,
549 (double)recorded[i].len,
550 recorded[i].needed);
556 static void usage(void)
558 printf(
559 "Usage:\n\
560 locktest //server1/share1 //server2/share2 [options..]\n\
561 options:\n\
562 -U user%%pass (may be specified twice)\n\
563 -k use kerberos\n\
564 -s seed\n\
565 -o numops\n\
566 -u hide unlock fails\n\
567 -a (show all ops)\n\
568 -A analyse for minimal ops\n\
569 -O use oplocks\n\
570 -E enable exact error code checking\n\
571 -Z enable the zero/zero lock\n\
572 -R range set lock range\n\
573 -B base set lock base\n\
574 -M min set min lock length\n\
578 /****************************************************************************
579 main program
580 ****************************************************************************/
581 int main(int argc,char *argv[])
583 char *share[NSERVERS];
584 extern char *optarg;
585 extern int optind;
586 int opt;
587 char *p;
588 int seed, server;
590 setlinebuf(stdout);
592 load_case_tables();
594 dbf = x_stderr;
596 if (argc < 3 || argv[1][0] == '-') {
597 usage();
598 exit(1);
601 setup_logging(argv[0],True);
603 for (server=0;server<NSERVERS;server++) {
604 share[server] = argv[1+server];
605 all_string_sub(share[server],"/","\\",0);
608 argc -= NSERVERS;
609 argv += NSERVERS;
611 lp_load(dyn_CONFIGFILE,True,False,False,True);
612 load_interfaces();
614 if (getenv("USER")) {
615 fstrcpy(username[0],getenv("USER"));
616 fstrcpy(username[1],getenv("USER"));
619 seed = time(NULL);
621 while ((opt = getopt(argc, argv, "U:s:ho:aAW:OkR:B:M:EZ")) != EOF) {
622 switch (opt) {
623 case 'k':
624 #ifdef HAVE_KRB5
625 use_kerberos = True;
626 #else
627 d_printf("No kerberos support compiled in\n");
628 exit(1);
629 #endif
630 break;
631 case 'U':
632 got_user = 1;
633 if (got_pass == 2) {
634 d_printf("Max of 2 usernames\n");
635 exit(1);
637 fstrcpy(username[got_pass],optarg);
638 p = strchr_m(username[got_pass],'%');
639 if (p) {
640 *p = 0;
641 fstrcpy(password[got_pass], p+1);
642 got_pass++;
644 break;
645 case 'R':
646 lock_range = strtol(optarg, NULL, 0);
647 break;
648 case 'B':
649 lock_base = strtol(optarg, NULL, 0);
650 break;
651 case 'M':
652 min_length = strtol(optarg, NULL, 0);
653 break;
654 case 's':
655 seed = atoi(optarg);
656 break;
657 case 'u':
658 hide_unlock_fails = True;
659 break;
660 case 'o':
661 numops = atoi(optarg);
662 break;
663 case 'O':
664 use_oplocks = True;
665 break;
666 case 'a':
667 showall = True;
668 break;
669 case 'A':
670 analyze = True;
671 break;
672 case 'Z':
673 zero_zero = True;
674 break;
675 case 'E':
676 exact_error_codes = True;
677 break;
678 case 'h':
679 usage();
680 exit(1);
681 default:
682 printf("Unknown option %c (%d)\n", (char)opt, opt);
683 exit(1);
687 if(use_kerberos && !got_user) got_pass = True;
689 argc -= optind;
690 argv += optind;
692 DEBUG(0,("seed=%u\n", seed));
693 srandom(seed);
695 test_locks(share);
697 return(0);