selftest: Make sure format-subunit and filter-subunit can find testtools/subunit.
[Samba/eduardoll.git] / source3 / torture / locktest.c
bloba90c2e2dfee02227c7b30bf0e619e5014f1e5b76
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"
22 static fstring password[2];
23 static fstring username[2];
24 static int got_user;
25 static int got_pass;
26 static bool use_kerberos;
27 static int numops = 1000;
28 static bool showall;
29 static bool analyze;
30 static bool hide_unlock_fails;
31 static bool use_oplocks;
32 static unsigned lock_range = 100;
33 static unsigned lock_base = 0;
34 static unsigned min_length = 0;
35 static bool exact_error_codes;
36 static bool zero_zero;
38 extern char *optarg;
39 extern int optind;
41 #define FILENAME "\\locktest.dat"
43 #define READ_PCT 50
44 #define LOCK_PCT 45
45 #define UNLOCK_PCT 70
46 #define RANGE_MULTIPLE 1
47 #define NSERVERS 2
48 #define NCONNECTIONS 2
49 #define NFILES 2
50 #define LOCK_TIMEOUT 0
52 #define NASTY_POSIX_LOCK_HACK 0
54 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
56 static const char *lock_op_type(int op)
58 if (op == WRITE_LOCK) return "write";
59 else if (op == READ_LOCK) return "read";
60 else return "other";
63 static const char *lock_op_name(enum lock_op op)
65 if (op == OP_LOCK) return "lock";
66 else if (op == OP_UNLOCK) return "unlock";
67 else return "reopen";
70 struct record {
71 enum lock_op lock_op;
72 enum brl_type lock_type;
73 char conn, f;
74 uint64_t start, len;
75 char needed;
78 #define PRESETS 0
80 #if PRESETS
81 static struct record preset[] = {
82 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
83 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
84 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 0, 1},
85 {OP_UNLOCK, 0 , 0, 0, 2, 0, 1},
86 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
88 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
89 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
90 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
91 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
93 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
94 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 1, 1},
95 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
96 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
98 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
99 {OP_LOCK, WRITE_LOCK, 0, 0, 1, 1, 1},
100 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
101 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
103 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
104 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
105 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
106 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
108 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
109 {OP_LOCK, READ_LOCK, 0, 0, 3, 1, 1},
110 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
111 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
114 #endif
116 static struct record *recorded;
118 static void print_brl(struct file_id id,
119 struct server_id pid,
120 enum brl_type lock_type,
121 enum brl_flavour lock_flav,
122 br_off start,
123 br_off size,
124 void *private_data)
126 #if NASTY_POSIX_LOCK_HACK
128 static SMB_INO_T lastino;
130 if (lastino != ino) {
131 char *cmd;
132 if (asprintf(&cmd,
133 "egrep POSIX.*%u /proc/locks", (int)ino) > 0) {
134 system(cmd);
135 SAFE_FREE(cmd);
138 lastino = ino;
140 #endif
142 printf("%s %s %s %.0f:%.0f(%.0f)\n",
143 procid_str_static(&pid), file_id_string_tos(&id),
144 lock_type==READ_LOCK?"R":"W",
145 (double)start, (double)start+size-1,(double)size);
150 static void show_locks(void)
152 brl_forall(print_brl, NULL);
153 /* system("cat /proc/locks"); */
157 /*****************************************************
158 return a connection to a server
159 *******************************************************/
160 static struct cli_state *connect_one(char *share, int snum)
162 struct cli_state *c;
163 struct nmb_name called, calling;
164 char *server_n;
165 fstring server;
166 struct sockaddr_storage ss;
167 fstring myname;
168 static int count;
169 NTSTATUS status;
171 fstrcpy(server,share+2);
172 share = strchr_m(server,'\\');
173 if (!share) return NULL;
174 *share = 0;
175 share++;
177 server_n = server;
179 zero_sockaddr(&ss);
181 slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
183 make_nmb_name(&calling, myname, 0x0);
184 make_nmb_name(&called , server, 0x20);
186 again:
187 zero_sockaddr(&ss);
189 /* have to open a new connection */
190 if (!(c=cli_initialise())) {
191 DEBUG(0,("Connection to %s failed\n", server_n));
192 return NULL;
195 status = cli_connect(c, server_n, &ss);
196 if (!NT_STATUS_IS_OK(status)) {
197 DEBUG(0,("Connection to %s failed. Error %s\n", server_n, nt_errstr(status) ));
198 return NULL;
201 c->use_kerberos = use_kerberos;
203 if (!cli_session_request(c, &calling, &called)) {
204 DEBUG(0,("session request to %s failed\n", called.name));
205 cli_shutdown(c);
206 if (strcmp(called.name, "*SMBSERVER")) {
207 make_nmb_name(&called , "*SMBSERVER", 0x20);
208 goto again;
210 return NULL;
213 DEBUG(4,(" session request ok\n"));
215 status = cli_negprot(c);
216 if (!NT_STATUS_IS_OK(status)) {
217 DEBUG(0, ("protocol negotiation failed: %s\n",
218 nt_errstr(status)));
219 cli_shutdown(c);
220 return NULL;
223 if (!got_pass) {
224 char *pass = getpass("Password: ");
225 if (pass) {
226 fstrcpy(password[0], pass);
227 fstrcpy(password[1], pass);
231 if (got_pass == 1) {
232 fstrcpy(password[1], password[0]);
233 fstrcpy(username[1], username[0]);
236 if (!NT_STATUS_IS_OK(cli_session_setup(c, username[snum],
237 password[snum],
238 strlen(password[snum]),
239 password[snum],
240 strlen(password[snum]),
241 lp_workgroup()))) {
242 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
243 return NULL;
247 * These next two lines are needed to emulate
248 * old client behaviour for people who have
249 * scripts based on client output.
250 * QUESTION ? Do we want to have a 'client compatibility
251 * mode to turn these on/off ? JRA.
254 if (*c->server_domain || *c->server_os || *c->server_type)
255 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
256 c->server_domain,c->server_os,c->server_type));
258 DEBUG(4,(" session setup ok\n"));
260 status = cli_tcon_andx(c, share, "?????", password[snum],
261 strlen(password[snum])+1);
262 if (!NT_STATUS_IS_OK(status)) {
263 DEBUG(0,("tree connect failed: %s\n", nt_errstr(status)));
264 cli_shutdown(c);
265 return NULL;
268 DEBUG(4,(" tconx ok\n"));
270 c->use_oplocks = use_oplocks;
272 return c;
276 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS], uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES],
277 char *share[NSERVERS])
279 int server, conn, f;
281 for (server=0;server<NSERVERS;server++)
282 for (conn=0;conn<NCONNECTIONS;conn++) {
283 if (cli[server][conn]) {
284 for (f=0;f<NFILES;f++) {
285 if (fnum[server][conn][f] != (uint16_t)-1) {
286 cli_close(cli[server][conn], fnum[server][conn][f]);
287 fnum[server][conn][f] = (uint16_t)-1;
290 cli_ulogoff(cli[server][conn]);
291 cli_shutdown(cli[server][conn]);
293 cli[server][conn] = connect_one(share[server], server);
294 if (!cli[server][conn]) {
295 DEBUG(0,("Failed to connect to %s\n", share[server]));
296 exit(1);
303 static bool test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
304 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES],
305 struct record *rec)
307 unsigned conn = rec->conn;
308 unsigned f = rec->f;
309 uint64_t start = rec->start;
310 uint64_t len = rec->len;
311 enum brl_type op = rec->lock_type;
312 int server;
313 bool ret[NSERVERS];
314 NTSTATUS status[NSERVERS];
316 switch (rec->lock_op) {
317 case OP_LOCK:
318 /* set a lock */
319 for (server=0;server<NSERVERS;server++) {
320 ret[server] = cli_lock64(cli[server][conn],
321 fnum[server][conn][f],
322 start, len, LOCK_TIMEOUT, op);
323 status[server] = cli_nt_error(cli[server][conn]);
324 if (!exact_error_codes &&
325 NT_STATUS_EQUAL(status[server],
326 NT_STATUS_FILE_LOCK_CONFLICT)) {
327 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
330 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
331 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
332 conn, f,
333 (double)start, (double)len,
334 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
335 nt_errstr(status[0]), nt_errstr(status[1]));
337 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
338 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
339 break;
341 case OP_UNLOCK:
342 /* unset a lock */
343 for (server=0;server<NSERVERS;server++) {
344 ret[server] = NT_STATUS_IS_OK(cli_unlock64(cli[server][conn],
345 fnum[server][conn][f],
346 start, len));
347 status[server] = cli_nt_error(cli[server][conn]);
349 if (showall ||
350 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
351 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
352 conn, f,
353 (double)start, (double)len,
354 nt_errstr(status[0]), nt_errstr(status[1]));
356 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
357 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
358 return False;
359 break;
361 case OP_REOPEN:
362 /* reopen the file */
363 for (server=0;server<NSERVERS;server++) {
364 cli_close(cli[server][conn], fnum[server][conn][f]);
365 fnum[server][conn][f] = (uint16_t)-1;
367 for (server=0;server<NSERVERS;server++) {
368 fnum[server][conn][f] = (uint16_t)-1;
369 if (!NT_STATUS_IS_OK(cli_open(cli[server][conn], FILENAME,
370 O_RDWR|O_CREAT,
371 DENY_NONE, &fnum[server][conn][f]))) {
372 printf("failed to reopen on share%d\n", server);
373 return False;
376 if (showall) {
377 printf("reopen conn=%u f=%u\n",
378 conn, f);
379 show_locks();
381 break;
384 return True;
387 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
388 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES])
390 int server, conn, f;
392 for (server=0;server<NSERVERS;server++)
393 for (conn=0;conn<NCONNECTIONS;conn++)
394 for (f=0;f<NFILES;f++) {
395 if (fnum[server][conn][f] != (uint16_t)-1) {
396 cli_close(cli[server][conn], fnum[server][conn][f]);
397 fnum[server][conn][f] = (uint16_t)-1;
400 for (server=0;server<NSERVERS;server++) {
401 cli_unlink(cli[server][0], FILENAME, aSYSTEM | aHIDDEN);
405 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
406 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES])
408 int server, conn, f;
410 for (server=0;server<NSERVERS;server++)
411 for (conn=0;conn<NCONNECTIONS;conn++)
412 for (f=0;f<NFILES;f++) {
413 fnum[server][conn][f] = (uint16_t)-1;
414 if (!NT_STATUS_IS_OK(cli_open(cli[server][conn], FILENAME,
415 O_RDWR|O_CREAT,
416 DENY_NONE,
417 &fnum[server][conn][f]))) {
418 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
419 server, conn, f);
420 exit(1);
426 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
427 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES],
428 int n)
430 int i;
431 printf("testing %u ...\n", n);
432 for (i=0; i<n; i++) {
433 if (i && i % 100 == 0) {
434 printf("%u\n", i);
437 if (recorded[i].needed &&
438 !test_one(cli, fnum, &recorded[i])) return i;
440 return n;
444 /* each server has two connections open to it. Each connection has two file
445 descriptors open on the file - 8 file descriptors in total
447 we then do random locking ops in tamdem on the 4 fnums from each
448 server and ensure that the results match
450 static void test_locks(char *share[NSERVERS])
452 struct cli_state *cli[NSERVERS][NCONNECTIONS];
453 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES];
454 int n, i, n1, skip, r1, r2;
456 ZERO_STRUCT(fnum);
457 ZERO_STRUCT(cli);
459 recorded = SMB_MALLOC_ARRAY(struct record, numops);
461 for (n=0; n<numops; n++) {
462 #if PRESETS
463 if (n < sizeof(preset) / sizeof(preset[0])) {
464 recorded[n] = preset[n];
465 } else {
466 #endif
467 recorded[n].conn = random() % NCONNECTIONS;
468 recorded[n].f = random() % NFILES;
469 recorded[n].start = lock_base + ((unsigned)random() % (lock_range-1));
470 recorded[n].len = min_length +
471 random() % (lock_range-(recorded[n].start-lock_base));
472 recorded[n].start *= RANGE_MULTIPLE;
473 recorded[n].len *= RANGE_MULTIPLE;
474 r1 = random() % 100;
475 r2 = random() % 100;
476 if (r1 < READ_PCT) {
477 recorded[n].lock_type = READ_LOCK;
478 } else {
479 recorded[n].lock_type = WRITE_LOCK;
481 if (r2 < LOCK_PCT) {
482 recorded[n].lock_op = OP_LOCK;
483 } else if (r2 < UNLOCK_PCT) {
484 recorded[n].lock_op = OP_UNLOCK;
485 } else {
486 recorded[n].lock_op = OP_REOPEN;
488 recorded[n].needed = True;
489 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
490 recorded[n].len = 1;
492 #if PRESETS
494 #endif
497 reconnect(cli, fnum, share);
498 open_files(cli, fnum);
499 n = retest(cli, fnum, numops);
501 if (n == numops || !analyze) return;
502 n++;
504 skip = n/2;
506 while (1) {
507 n1 = n;
509 close_files(cli, fnum);
510 reconnect(cli, fnum, share);
511 open_files(cli, fnum);
513 for (i=0;i<n-skip;i+=skip) {
514 int m, j;
515 printf("excluding %d-%d\n", i, i+skip-1);
516 for (j=i;j<i+skip;j++) {
517 recorded[j].needed = False;
520 close_files(cli, fnum);
521 open_files(cli, fnum);
523 m = retest(cli, fnum, n);
524 if (m == n) {
525 for (j=i;j<i+skip;j++) {
526 recorded[j].needed = True;
528 } else {
529 if (i+(skip-1) < m) {
530 memmove(&recorded[i], &recorded[i+skip],
531 (m-(i+skip-1))*sizeof(recorded[0]));
533 n = m-(skip-1);
534 i--;
538 if (skip > 1) {
539 skip = skip/2;
540 printf("skip=%d\n", skip);
541 continue;
544 if (n1 == n) break;
547 close_files(cli, fnum);
548 reconnect(cli, fnum, share);
549 open_files(cli, fnum);
550 showall = True;
551 n1 = retest(cli, fnum, n);
552 if (n1 != n-1) {
553 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
555 close_files(cli, fnum);
557 for (i=0;i<n;i++) {
558 printf("{%s, %s, conn = %u, file = %u, start = %.0f, len = %.0f, %u},\n",
559 lock_op_name(recorded[i].lock_op),
560 lock_op_type(recorded[i].lock_type),
561 recorded[i].conn,
562 recorded[i].f,
563 (double)recorded[i].start,
564 (double)recorded[i].len,
565 recorded[i].needed);
571 static void usage(void)
573 printf(
574 "Usage:\n\
575 locktest //server1/share1 //server2/share2 [options..]\n\
576 options:\n\
577 -U user%%pass (may be specified twice)\n\
578 -k use kerberos\n\
579 -s seed\n\
580 -o numops\n\
581 -u hide unlock fails\n\
582 -a (show all ops)\n\
583 -A analyse for minimal ops\n\
584 -O use oplocks\n\
585 -E enable exact error code checking\n\
586 -Z enable the zero/zero lock\n\
587 -R range set lock range\n\
588 -B base set lock base\n\
589 -M min set min lock length\n\
593 /****************************************************************************
594 main program
595 ****************************************************************************/
596 int main(int argc,char *argv[])
598 char *share[NSERVERS];
599 int opt;
600 char *p;
601 int seed, server;
603 setlinebuf(stdout);
605 load_case_tables();
607 dbf = x_stderr;
609 if (argc < 3 || argv[1][0] == '-') {
610 usage();
611 exit(1);
614 setup_logging(argv[0],True);
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);