WHATSNEW: Start release notes for 3.6.0pre3.
[Samba.git] / source3 / torture / locktest.c
blobd52a8de4605351a3ca29d97caee6e408c223b8f1
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 "system/filesys.h"
22 #include "locking/proto.h"
23 #include "libsmb/nmblib.h"
25 static fstring password[2];
26 static fstring username[2];
27 static int got_user;
28 static int got_pass;
29 static bool use_kerberos;
30 static int numops = 1000;
31 static bool showall;
32 static bool analyze;
33 static bool hide_unlock_fails;
34 static bool use_oplocks;
35 static unsigned lock_range = 100;
36 static unsigned lock_base = 0;
37 static unsigned min_length = 0;
38 static bool exact_error_codes;
39 static bool zero_zero;
41 extern char *optarg;
42 extern int optind;
44 #define FILENAME "\\locktest.dat"
46 #define READ_PCT 50
47 #define LOCK_PCT 45
48 #define UNLOCK_PCT 70
49 #define RANGE_MULTIPLE 1
50 #define NSERVERS 2
51 #define NCONNECTIONS 2
52 #define NFILES 2
53 #define LOCK_TIMEOUT 0
55 #define NASTY_POSIX_LOCK_HACK 0
57 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
59 static const char *lock_op_type(int op)
61 if (op == WRITE_LOCK) return "write";
62 else if (op == READ_LOCK) return "read";
63 else return "other";
66 static const char *lock_op_name(enum lock_op op)
68 if (op == OP_LOCK) return "lock";
69 else if (op == OP_UNLOCK) return "unlock";
70 else return "reopen";
73 struct record {
74 enum lock_op lock_op;
75 enum brl_type lock_type;
76 char conn, f;
77 uint64_t start, len;
78 char needed;
81 #define PRESETS 0
83 #if PRESETS
84 static struct record preset[] = {
85 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
86 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
87 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 0, 1},
88 {OP_UNLOCK, 0 , 0, 0, 2, 0, 1},
89 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
91 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
92 {OP_LOCK, READ_LOCK, 0, 0, 1, 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, 3, 1, 1},
98 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
99 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
101 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
102 {OP_LOCK, WRITE_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, 1, 1, 1},
108 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
109 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
111 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
112 {OP_LOCK, READ_LOCK, 0, 0, 3, 1, 1},
113 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
114 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
117 #endif
119 static struct record *recorded;
121 static void print_brl(struct file_id id,
122 struct server_id pid,
123 enum brl_type lock_type,
124 enum brl_flavour lock_flav,
125 br_off start,
126 br_off size,
127 void *private_data)
129 #if NASTY_POSIX_LOCK_HACK
131 static SMB_INO_T lastino;
133 if (lastino != ino) {
134 char *cmd;
135 if (asprintf(&cmd,
136 "egrep POSIX.*%u /proc/locks", (int)ino) > 0) {
137 system(cmd);
138 SAFE_FREE(cmd);
141 lastino = ino;
143 #endif
145 printf("%s %s %s %.0f:%.0f(%.0f)\n",
146 procid_str_static(&pid), file_id_string_tos(&id),
147 lock_type==READ_LOCK?"R":"W",
148 (double)start, (double)start+size-1,(double)size);
153 static void show_locks(void)
155 brl_forall(print_brl, NULL);
156 /* system("cat /proc/locks"); */
160 /*****************************************************
161 return a connection to a server
162 *******************************************************/
163 static struct cli_state *connect_one(char *share, int snum)
165 struct cli_state *c;
166 struct nmb_name called, calling;
167 char *server_n;
168 fstring server;
169 struct sockaddr_storage ss;
170 fstring myname;
171 static int count;
172 NTSTATUS status;
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 zero_sockaddr(&ss);
184 slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
186 make_nmb_name(&calling, myname, 0x0);
187 make_nmb_name(&called , server, 0x20);
189 again:
190 zero_sockaddr(&ss);
192 /* have to open a new connection */
193 if (!(c=cli_initialise())) {
194 DEBUG(0,("Connection to %s failed\n", server_n));
195 return NULL;
198 status = cli_connect(c, server_n, &ss);
199 if (!NT_STATUS_IS_OK(status)) {
200 DEBUG(0,("Connection to %s failed. Error %s\n", server_n, nt_errstr(status) ));
201 return NULL;
204 c->use_kerberos = use_kerberos;
206 if (!cli_session_request(c, &calling, &called)) {
207 DEBUG(0,("session request to %s failed\n", called.name));
208 cli_shutdown(c);
209 if (strcmp(called.name, "*SMBSERVER")) {
210 make_nmb_name(&called , "*SMBSERVER", 0x20);
211 goto again;
213 return NULL;
216 DEBUG(4,(" session request ok\n"));
218 status = cli_negprot(c);
219 if (!NT_STATUS_IS_OK(status)) {
220 DEBUG(0, ("protocol negotiation failed: %s\n",
221 nt_errstr(status)));
222 cli_shutdown(c);
223 return NULL;
226 if (!got_pass) {
227 char *pass = getpass("Password: ");
228 if (pass) {
229 fstrcpy(password[0], pass);
230 fstrcpy(password[1], pass);
234 if (got_pass == 1) {
235 fstrcpy(password[1], password[0]);
236 fstrcpy(username[1], username[0]);
239 status = cli_session_setup(c, username[snum],
240 password[snum], strlen(password[snum]),
241 password[snum], strlen(password[snum]),
242 lp_workgroup());
243 if (!NT_STATUS_IS_OK(status)) {
244 DEBUG(0,("session setup failed: %s\n", nt_errstr(status)));
245 return NULL;
249 * These next two lines are needed to emulate
250 * old client behaviour for people who have
251 * scripts based on client output.
252 * QUESTION ? Do we want to have a 'client compatibility
253 * mode to turn these on/off ? JRA.
256 if (*c->server_domain || *c->server_os || *c->server_type)
257 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
258 c->server_domain,c->server_os,c->server_type));
260 DEBUG(4,(" session setup ok\n"));
262 status = cli_tcon_andx(c, share, "?????", password[snum],
263 strlen(password[snum])+1);
264 if (!NT_STATUS_IS_OK(status)) {
265 DEBUG(0,("tree connect failed: %s\n", nt_errstr(status)));
266 cli_shutdown(c);
267 return NULL;
270 DEBUG(4,(" tconx ok\n"));
272 c->use_oplocks = use_oplocks;
274 return c;
278 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS], uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES],
279 char *share[NSERVERS])
281 int server, conn, f;
283 for (server=0;server<NSERVERS;server++)
284 for (conn=0;conn<NCONNECTIONS;conn++) {
285 if (cli[server][conn]) {
286 for (f=0;f<NFILES;f++) {
287 if (fnum[server][conn][f] != (uint16_t)-1) {
288 cli_close(cli[server][conn], fnum[server][conn][f]);
289 fnum[server][conn][f] = (uint16_t)-1;
292 cli_ulogoff(cli[server][conn]);
293 cli_shutdown(cli[server][conn]);
295 cli[server][conn] = connect_one(share[server], server);
296 if (!cli[server][conn]) {
297 DEBUG(0,("Failed to connect to %s\n", share[server]));
298 exit(1);
305 static bool test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
306 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES],
307 struct record *rec)
309 unsigned conn = rec->conn;
310 unsigned f = rec->f;
311 uint64_t start = rec->start;
312 uint64_t len = rec->len;
313 enum brl_type op = rec->lock_type;
314 int server;
315 bool ret[NSERVERS];
316 NTSTATUS status[NSERVERS];
318 switch (rec->lock_op) {
319 case OP_LOCK:
320 /* set a lock */
321 for (server=0;server<NSERVERS;server++) {
322 ret[server] = cli_lock64(cli[server][conn],
323 fnum[server][conn][f],
324 start, len, LOCK_TIMEOUT, op);
325 status[server] = cli_nt_error(cli[server][conn]);
326 if (!exact_error_codes &&
327 NT_STATUS_EQUAL(status[server],
328 NT_STATUS_FILE_LOCK_CONFLICT)) {
329 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
332 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
333 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
334 conn, f,
335 (double)start, (double)len,
336 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
337 nt_errstr(status[0]), nt_errstr(status[1]));
339 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
340 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
341 break;
343 case OP_UNLOCK:
344 /* unset a lock */
345 for (server=0;server<NSERVERS;server++) {
346 ret[server] = NT_STATUS_IS_OK(cli_unlock64(cli[server][conn],
347 fnum[server][conn][f],
348 start, len));
349 status[server] = cli_nt_error(cli[server][conn]);
351 if (showall ||
352 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
353 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
354 conn, f,
355 (double)start, (double)len,
356 nt_errstr(status[0]), nt_errstr(status[1]));
358 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
359 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
360 return False;
361 break;
363 case OP_REOPEN:
364 /* reopen the file */
365 for (server=0;server<NSERVERS;server++) {
366 cli_close(cli[server][conn], fnum[server][conn][f]);
367 fnum[server][conn][f] = (uint16_t)-1;
369 for (server=0;server<NSERVERS;server++) {
370 fnum[server][conn][f] = (uint16_t)-1;
371 if (!NT_STATUS_IS_OK(cli_open(cli[server][conn], FILENAME,
372 O_RDWR|O_CREAT,
373 DENY_NONE, &fnum[server][conn][f]))) {
374 printf("failed to reopen on share%d\n", server);
375 return False;
378 if (showall) {
379 printf("reopen conn=%u f=%u\n",
380 conn, f);
381 show_locks();
383 break;
386 return True;
389 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
390 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES])
392 int server, conn, f;
394 for (server=0;server<NSERVERS;server++)
395 for (conn=0;conn<NCONNECTIONS;conn++)
396 for (f=0;f<NFILES;f++) {
397 if (fnum[server][conn][f] != (uint16_t)-1) {
398 cli_close(cli[server][conn], fnum[server][conn][f]);
399 fnum[server][conn][f] = (uint16_t)-1;
402 for (server=0;server<NSERVERS;server++) {
403 cli_unlink(cli[server][0], FILENAME, aSYSTEM | aHIDDEN);
407 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
408 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES])
410 int server, conn, f;
412 for (server=0;server<NSERVERS;server++)
413 for (conn=0;conn<NCONNECTIONS;conn++)
414 for (f=0;f<NFILES;f++) {
415 fnum[server][conn][f] = (uint16_t)-1;
416 if (!NT_STATUS_IS_OK(cli_open(cli[server][conn], FILENAME,
417 O_RDWR|O_CREAT,
418 DENY_NONE,
419 &fnum[server][conn][f]))) {
420 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
421 server, conn, f);
422 exit(1);
428 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
429 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES],
430 int n)
432 int i;
433 printf("testing %u ...\n", n);
434 for (i=0; i<n; i++) {
435 if (i && i % 100 == 0) {
436 printf("%u\n", i);
439 if (recorded[i].needed &&
440 !test_one(cli, fnum, &recorded[i])) return i;
442 return n;
446 /* each server has two connections open to it. Each connection has two file
447 descriptors open on the file - 8 file descriptors in total
449 we then do random locking ops in tamdem on the 4 fnums from each
450 server and ensure that the results match
452 static void test_locks(char *share[NSERVERS])
454 struct cli_state *cli[NSERVERS][NCONNECTIONS];
455 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES];
456 int n, i, n1, skip, r1, r2;
458 ZERO_STRUCT(fnum);
459 ZERO_STRUCT(cli);
461 recorded = SMB_MALLOC_ARRAY(struct record, numops);
463 for (n=0; n<numops; n++) {
464 #if PRESETS
465 if (n < sizeof(preset) / sizeof(preset[0])) {
466 recorded[n] = preset[n];
467 } else {
468 #endif
469 recorded[n].conn = random() % NCONNECTIONS;
470 recorded[n].f = random() % NFILES;
471 recorded[n].start = lock_base + ((unsigned)random() % (lock_range-1));
472 recorded[n].len = min_length +
473 random() % (lock_range-(recorded[n].start-lock_base));
474 recorded[n].start *= RANGE_MULTIPLE;
475 recorded[n].len *= RANGE_MULTIPLE;
476 r1 = random() % 100;
477 r2 = random() % 100;
478 if (r1 < READ_PCT) {
479 recorded[n].lock_type = READ_LOCK;
480 } else {
481 recorded[n].lock_type = WRITE_LOCK;
483 if (r2 < LOCK_PCT) {
484 recorded[n].lock_op = OP_LOCK;
485 } else if (r2 < UNLOCK_PCT) {
486 recorded[n].lock_op = OP_UNLOCK;
487 } else {
488 recorded[n].lock_op = OP_REOPEN;
490 recorded[n].needed = True;
491 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
492 recorded[n].len = 1;
494 #if PRESETS
496 #endif
499 reconnect(cli, fnum, share);
500 open_files(cli, fnum);
501 n = retest(cli, fnum, numops);
503 if (n == numops || !analyze) return;
504 n++;
506 skip = n/2;
508 while (1) {
509 n1 = n;
511 close_files(cli, fnum);
512 reconnect(cli, fnum, share);
513 open_files(cli, fnum);
515 for (i=0;i<n-skip;i+=skip) {
516 int m, j;
517 printf("excluding %d-%d\n", i, i+skip-1);
518 for (j=i;j<i+skip;j++) {
519 recorded[j].needed = False;
522 close_files(cli, fnum);
523 open_files(cli, fnum);
525 m = retest(cli, fnum, n);
526 if (m == n) {
527 for (j=i;j<i+skip;j++) {
528 recorded[j].needed = True;
530 } else {
531 if (i+(skip-1) < m) {
532 memmove(&recorded[i], &recorded[i+skip],
533 (m-(i+skip-1))*sizeof(recorded[0]));
535 n = m-(skip-1);
536 i--;
540 if (skip > 1) {
541 skip = skip/2;
542 printf("skip=%d\n", skip);
543 continue;
546 if (n1 == n) break;
549 close_files(cli, fnum);
550 reconnect(cli, fnum, share);
551 open_files(cli, fnum);
552 showall = True;
553 n1 = retest(cli, fnum, n);
554 if (n1 != n-1) {
555 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
557 close_files(cli, fnum);
559 for (i=0;i<n;i++) {
560 printf("{%s, %s, conn = %u, file = %u, start = %.0f, len = %.0f, %u},\n",
561 lock_op_name(recorded[i].lock_op),
562 lock_op_type(recorded[i].lock_type),
563 recorded[i].conn,
564 recorded[i].f,
565 (double)recorded[i].start,
566 (double)recorded[i].len,
567 recorded[i].needed);
573 static void usage(void)
575 printf(
576 "Usage:\n\
577 locktest //server1/share1 //server2/share2 [options..]\n\
578 options:\n\
579 -U user%%pass (may be specified twice)\n\
580 -k use kerberos\n\
581 -s seed\n\
582 -o numops\n\
583 -u hide unlock fails\n\
584 -a (show all ops)\n\
585 -A analyse for minimal ops\n\
586 -O use oplocks\n\
587 -E enable exact error code checking\n\
588 -Z enable the zero/zero lock\n\
589 -R range set lock range\n\
590 -B base set lock base\n\
591 -M min set min lock length\n\
595 /****************************************************************************
596 main program
597 ****************************************************************************/
598 int main(int argc,char *argv[])
600 char *share[NSERVERS];
601 int opt;
602 char *p;
603 int seed, server;
605 setlinebuf(stdout);
607 load_case_tables();
609 if (argc < 3 || argv[1][0] == '-') {
610 usage();
611 exit(1);
614 setup_logging(argv[0], DEBUG_STDOUT);
616 for (server=0;server<NSERVERS;server++) {
617 share[server] = argv[1+server];
618 all_string_sub(share[server],"/","\\",0);
621 argc -= NSERVERS;
622 argv += NSERVERS;
624 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
625 load_interfaces();
627 if (getenv("USER")) {
628 fstrcpy(username[0],getenv("USER"));
629 fstrcpy(username[1],getenv("USER"));
632 seed = time(NULL);
634 while ((opt = getopt(argc, argv, "U:s:ho:aAW:OkR:B:M:EZ")) != EOF) {
635 switch (opt) {
636 case 'k':
637 #ifdef HAVE_KRB5
638 use_kerberos = True;
639 #else
640 d_printf("No kerberos support compiled in\n");
641 exit(1);
642 #endif
643 break;
644 case 'U':
645 got_user = 1;
646 if (got_pass == 2) {
647 d_printf("Max of 2 usernames\n");
648 exit(1);
650 fstrcpy(username[got_pass],optarg);
651 p = strchr_m(username[got_pass],'%');
652 if (p) {
653 *p = 0;
654 fstrcpy(password[got_pass], p+1);
655 got_pass++;
657 break;
658 case 'R':
659 lock_range = strtol(optarg, NULL, 0);
660 break;
661 case 'B':
662 lock_base = strtol(optarg, NULL, 0);
663 break;
664 case 'M':
665 min_length = strtol(optarg, NULL, 0);
666 break;
667 case 's':
668 seed = atoi(optarg);
669 break;
670 case 'u':
671 hide_unlock_fails = True;
672 break;
673 case 'o':
674 numops = atoi(optarg);
675 break;
676 case 'O':
677 use_oplocks = True;
678 break;
679 case 'a':
680 showall = True;
681 break;
682 case 'A':
683 analyze = True;
684 break;
685 case 'Z':
686 zero_zero = True;
687 break;
688 case 'E':
689 exact_error_codes = True;
690 break;
691 case 'h':
692 usage();
693 exit(1);
694 default:
695 printf("Unknown option %c (%d)\n", (char)opt, opt);
696 exit(1);
700 if(use_kerberos && !got_user) got_pass = True;
702 argc -= optind;
703 argv += optind;
705 DEBUG(0,("seed=%u\n", seed));
706 srandom(seed);
708 test_locks(share);
710 return(0);