r14170: Paranioa fix for sesssetup.
[Samba/nascimento.git] / source3 / torture / locktest.c
blobb946e48666ebb1019e3d54281bdeb37a9f254183
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, SMB_INO_T ino, struct process_id pid,
117 enum brl_type lock_type,
118 br_off start, br_off size)
120 #if NASTY_POSIX_LOCK_HACK
122 pstring cmd;
123 static SMB_INO_T lastino;
125 if (lastino != ino) {
126 slprintf(cmd, sizeof(cmd),
127 "egrep POSIX.*%u /proc/locks", (int)ino);
128 system(cmd);
130 lastino = ino;
132 #endif
134 printf("%s %05x:%05x %s %.0f:%.0f(%.0f)\n",
135 procid_str_static(&pid), (int)dev, (int)ino,
136 lock_type==READ_LOCK?"R":"W",
137 (double)start, (double)start+size-1,(double)size);
142 static void show_locks(void)
144 brl_forall(print_brl);
145 /* system("cat /proc/locks"); */
149 /*****************************************************
150 return a connection to a server
151 *******************************************************/
152 static struct cli_state *connect_one(char *share, int snum)
154 struct cli_state *c;
155 struct nmb_name called, calling;
156 char *server_n;
157 fstring server;
158 struct in_addr ip;
159 fstring myname;
160 static int count;
162 fstrcpy(server,share+2);
163 share = strchr_m(server,'\\');
164 if (!share) return NULL;
165 *share = 0;
166 share++;
168 server_n = server;
170 zero_ip(&ip);
172 slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
174 make_nmb_name(&calling, myname, 0x0);
175 make_nmb_name(&called , server, 0x20);
177 again:
178 zero_ip(&ip);
180 /* have to open a new connection */
181 if (!(c=cli_initialise(NULL)) || !cli_connect(c, server_n, &ip)) {
182 DEBUG(0,("Connection to %s failed\n", server_n));
183 return NULL;
186 c->use_kerberos = use_kerberos;
188 if (!cli_session_request(c, &calling, &called)) {
189 DEBUG(0,("session request to %s failed\n", called.name));
190 cli_shutdown(c);
191 if (strcmp(called.name, "*SMBSERVER")) {
192 make_nmb_name(&called , "*SMBSERVER", 0x20);
193 goto again;
195 return NULL;
198 DEBUG(4,(" session request ok\n"));
200 if (!cli_negprot(c)) {
201 DEBUG(0,("protocol negotiation failed\n"));
202 cli_shutdown(c);
203 return NULL;
206 if (!got_pass) {
207 char *pass = getpass("Password: ");
208 if (pass) {
209 fstrcpy(password[0], pass);
210 fstrcpy(password[1], pass);
214 if (got_pass == 1) {
215 fstrcpy(password[1], password[0]);
216 fstrcpy(username[1], username[0]);
219 if (!cli_session_setup(c, username[snum],
220 password[snum], strlen(password[snum]),
221 password[snum], strlen(password[snum]),
222 lp_workgroup())) {
223 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
224 return NULL;
228 * These next two lines are needed to emulate
229 * old client behaviour for people who have
230 * scripts based on client output.
231 * QUESTION ? Do we want to have a 'client compatibility
232 * mode to turn these on/off ? JRA.
235 if (*c->server_domain || *c->server_os || *c->server_type)
236 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
237 c->server_domain,c->server_os,c->server_type));
239 DEBUG(4,(" session setup ok\n"));
241 if (!cli_send_tconX(c, share, "?????",
242 password[snum], strlen(password[snum])+1)) {
243 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
244 cli_shutdown(c);
245 return NULL;
248 DEBUG(4,(" tconx ok\n"));
250 c->use_oplocks = use_oplocks;
252 return c;
256 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
257 char *share[NSERVERS])
259 int server, conn, f;
261 for (server=0;server<NSERVERS;server++)
262 for (conn=0;conn<NCONNECTIONS;conn++) {
263 if (cli[server][conn]) {
264 for (f=0;f<NFILES;f++) {
265 if (fnum[server][conn][f] != -1) {
266 cli_close(cli[server][conn], fnum[server][conn][f]);
267 fnum[server][conn][f] = -1;
270 cli_ulogoff(cli[server][conn]);
271 cli_shutdown(cli[server][conn]);
273 cli[server][conn] = connect_one(share[server], server);
274 if (!cli[server][conn]) {
275 DEBUG(0,("Failed to connect to %s\n", share[server]));
276 exit(1);
283 static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
284 int fnum[NSERVERS][NCONNECTIONS][NFILES],
285 struct record *rec)
287 unsigned conn = rec->conn;
288 unsigned f = rec->f;
289 SMB_BIG_UINT start = rec->start;
290 SMB_BIG_UINT len = rec->len;
291 enum brl_type op = rec->lock_type;
292 int server;
293 BOOL ret[NSERVERS];
294 NTSTATUS status[NSERVERS];
296 switch (rec->lock_op) {
297 case OP_LOCK:
298 /* set a lock */
299 for (server=0;server<NSERVERS;server++) {
300 ret[server] = cli_lock64(cli[server][conn],
301 fnum[server][conn][f],
302 start, len, LOCK_TIMEOUT, op);
303 status[server] = cli_nt_error(cli[server][conn]);
304 if (!exact_error_codes &&
305 NT_STATUS_EQUAL(status[server],
306 NT_STATUS_FILE_LOCK_CONFLICT)) {
307 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
310 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
311 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
312 conn, f,
313 (double)start, (double)len,
314 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
315 nt_errstr(status[0]), nt_errstr(status[1]));
317 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
318 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
319 break;
321 case OP_UNLOCK:
322 /* unset a lock */
323 for (server=0;server<NSERVERS;server++) {
324 ret[server] = cli_unlock64(cli[server][conn],
325 fnum[server][conn][f],
326 start, len);
327 status[server] = cli_nt_error(cli[server][conn]);
329 if (showall ||
330 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
331 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
332 conn, f,
333 (double)start, (double)len,
334 nt_errstr(status[0]), nt_errstr(status[1]));
336 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
337 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
338 return False;
339 break;
341 case OP_REOPEN:
342 /* reopen the file */
343 for (server=0;server<NSERVERS;server++) {
344 cli_close(cli[server][conn], fnum[server][conn][f]);
345 fnum[server][conn][f] = -1;
347 for (server=0;server<NSERVERS;server++) {
348 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
349 O_RDWR|O_CREAT,
350 DENY_NONE);
351 if (fnum[server][conn][f] == -1) {
352 printf("failed to reopen on share%d\n", server);
353 return False;
356 if (showall) {
357 printf("reopen conn=%u f=%u\n",
358 conn, f);
359 show_locks();
361 break;
364 return True;
367 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
368 int fnum[NSERVERS][NCONNECTIONS][NFILES])
370 int server, conn, f;
372 for (server=0;server<NSERVERS;server++)
373 for (conn=0;conn<NCONNECTIONS;conn++)
374 for (f=0;f<NFILES;f++) {
375 if (fnum[server][conn][f] != -1) {
376 cli_close(cli[server][conn], fnum[server][conn][f]);
377 fnum[server][conn][f] = -1;
380 for (server=0;server<NSERVERS;server++) {
381 cli_unlink(cli[server][0], FILENAME);
385 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
386 int fnum[NSERVERS][NCONNECTIONS][NFILES])
388 int server, conn, f;
390 for (server=0;server<NSERVERS;server++)
391 for (conn=0;conn<NCONNECTIONS;conn++)
392 for (f=0;f<NFILES;f++) {
393 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
394 O_RDWR|O_CREAT,
395 DENY_NONE);
396 if (fnum[server][conn][f] == -1) {
397 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
398 server, conn, f);
399 exit(1);
405 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
406 int fnum[NSERVERS][NCONNECTIONS][NFILES],
407 int n)
409 int i;
410 printf("testing %u ...\n", n);
411 for (i=0; i<n; i++) {
412 if (i && i % 100 == 0) {
413 printf("%u\n", i);
416 if (recorded[i].needed &&
417 !test_one(cli, fnum, &recorded[i])) return i;
419 return n;
423 /* each server has two connections open to it. Each connection has two file
424 descriptors open on the file - 8 file descriptors in total
426 we then do random locking ops in tamdem on the 4 fnums from each
427 server and ensure that the results match
429 static void test_locks(char *share[NSERVERS])
431 struct cli_state *cli[NSERVERS][NCONNECTIONS];
432 int fnum[NSERVERS][NCONNECTIONS][NFILES];
433 int n, i, n1, skip, r1, r2;
435 ZERO_STRUCT(fnum);
436 ZERO_STRUCT(cli);
438 recorded = SMB_MALLOC_ARRAY(struct record, numops);
440 for (n=0; n<numops; n++) {
441 #if PRESETS
442 if (n < sizeof(preset) / sizeof(preset[0])) {
443 recorded[n] = preset[n];
444 } else {
445 #endif
446 recorded[n].conn = random() % NCONNECTIONS;
447 recorded[n].f = random() % NFILES;
448 recorded[n].start = lock_base + ((unsigned)random() % (lock_range-1));
449 recorded[n].len = min_length +
450 random() % (lock_range-(recorded[n].start-lock_base));
451 recorded[n].start *= RANGE_MULTIPLE;
452 recorded[n].len *= RANGE_MULTIPLE;
453 r1 = random() % 100;
454 r2 = random() % 100;
455 if (r1 < READ_PCT) {
456 recorded[n].lock_type = READ_LOCK;
457 } else {
458 recorded[n].lock_type = WRITE_LOCK;
460 if (r2 < LOCK_PCT) {
461 recorded[n].lock_op = OP_LOCK;
462 } else if (r2 < UNLOCK_PCT) {
463 recorded[n].lock_op = OP_UNLOCK;
464 } else {
465 recorded[n].lock_op = OP_REOPEN;
467 recorded[n].needed = True;
468 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
469 recorded[n].len = 1;
471 #if PRESETS
473 #endif
476 reconnect(cli, fnum, share);
477 open_files(cli, fnum);
478 n = retest(cli, fnum, numops);
480 if (n == numops || !analyze) return;
481 n++;
483 skip = n/2;
485 while (1) {
486 n1 = n;
488 close_files(cli, fnum);
489 reconnect(cli, fnum, share);
490 open_files(cli, fnum);
492 for (i=0;i<n-skip;i+=skip) {
493 int m, j;
494 printf("excluding %d-%d\n", i, i+skip-1);
495 for (j=i;j<i+skip;j++) {
496 recorded[j].needed = False;
499 close_files(cli, fnum);
500 open_files(cli, fnum);
502 m = retest(cli, fnum, n);
503 if (m == n) {
504 for (j=i;j<i+skip;j++) {
505 recorded[j].needed = True;
507 } else {
508 if (i+(skip-1) < m) {
509 memmove(&recorded[i], &recorded[i+skip],
510 (m-(i+skip-1))*sizeof(recorded[0]));
512 n = m-(skip-1);
513 i--;
517 if (skip > 1) {
518 skip = skip/2;
519 printf("skip=%d\n", skip);
520 continue;
523 if (n1 == n) break;
526 close_files(cli, fnum);
527 reconnect(cli, fnum, share);
528 open_files(cli, fnum);
529 showall = True;
530 n1 = retest(cli, fnum, n);
531 if (n1 != n-1) {
532 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
534 close_files(cli, fnum);
536 for (i=0;i<n;i++) {
537 printf("{%s, %s, conn = %u, file = %u, start = %.0f, len = %.0f, %u},\n",
538 lock_op_name(recorded[i].lock_op),
539 lock_op_type(recorded[i].lock_type),
540 recorded[i].conn,
541 recorded[i].f,
542 (double)recorded[i].start,
543 (double)recorded[i].len,
544 recorded[i].needed);
550 static void usage(void)
552 printf(
553 "Usage:\n\
554 locktest //server1/share1 //server2/share2 [options..]\n\
555 options:\n\
556 -U user%%pass (may be specified twice)\n\
557 -k use kerberos\n\
558 -s seed\n\
559 -o numops\n\
560 -u hide unlock fails\n\
561 -a (show all ops)\n\
562 -A analyse for minimal ops\n\
563 -O use oplocks\n\
564 -E enable exact error code checking\n\
565 -Z enable the zero/zero lock\n\
566 -R range set lock range\n\
567 -B base set lock base\n\
568 -M min set min lock length\n\
572 /****************************************************************************
573 main program
574 ****************************************************************************/
575 int main(int argc,char *argv[])
577 char *share[NSERVERS];
578 extern char *optarg;
579 extern int optind;
580 int opt;
581 char *p;
582 int seed, server;
584 setlinebuf(stdout);
586 load_case_tables();
588 dbf = x_stderr;
590 if (argc < 3 || argv[1][0] == '-') {
591 usage();
592 exit(1);
595 setup_logging(argv[0],True);
597 for (server=0;server<NSERVERS;server++) {
598 share[server] = argv[1+server];
599 all_string_sub(share[server],"/","\\",0);
602 argc -= NSERVERS;
603 argv += NSERVERS;
605 lp_load(dyn_CONFIGFILE,True,False,False,True);
606 load_interfaces();
608 if (getenv("USER")) {
609 fstrcpy(username[0],getenv("USER"));
610 fstrcpy(username[1],getenv("USER"));
613 seed = time(NULL);
615 while ((opt = getopt(argc, argv, "U:s:ho:aAW:OkR:B:M:EZ")) != EOF) {
616 switch (opt) {
617 case 'k':
618 #ifdef HAVE_KRB5
619 use_kerberos = True;
620 #else
621 d_printf("No kerberos support compiled in\n");
622 exit(1);
623 #endif
624 break;
625 case 'U':
626 got_user = 1;
627 if (got_pass == 2) {
628 d_printf("Max of 2 usernames\n");
629 exit(1);
631 fstrcpy(username[got_pass],optarg);
632 p = strchr_m(username[got_pass],'%');
633 if (p) {
634 *p = 0;
635 fstrcpy(password[got_pass], p+1);
636 got_pass++;
638 break;
639 case 'R':
640 lock_range = strtol(optarg, NULL, 0);
641 break;
642 case 'B':
643 lock_base = strtol(optarg, NULL, 0);
644 break;
645 case 'M':
646 min_length = strtol(optarg, NULL, 0);
647 break;
648 case 's':
649 seed = atoi(optarg);
650 break;
651 case 'u':
652 hide_unlock_fails = True;
653 break;
654 case 'o':
655 numops = atoi(optarg);
656 break;
657 case 'O':
658 use_oplocks = True;
659 break;
660 case 'a':
661 showall = True;
662 break;
663 case 'A':
664 analyze = True;
665 break;
666 case 'Z':
667 zero_zero = True;
668 break;
669 case 'E':
670 exact_error_codes = True;
671 break;
672 case 'h':
673 usage();
674 exit(1);
675 default:
676 printf("Unknown option %c (%d)\n", (char)opt, opt);
677 exit(1);
681 if(use_kerberos && !got_user) got_pass = True;
683 argc -= optind;
684 argv += optind;
686 DEBUG(0,("seed=%u\n", seed));
687 srandom(seed);
689 test_locks(share);
691 return(0);