Fix bug #10097 - MacOSX 10.9 will not follow path-based DFS referrals handed out...
[Samba.git] / source3 / torture / locktest.c
blob41bfc225ac1b04f042ede0ad6b47a6a8984b2969
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 struct nmb_name called, calling;
168 char *server_n;
169 fstring server;
170 struct sockaddr_storage ss;
171 fstring myname;
172 static int count;
173 NTSTATUS status;
175 fstrcpy(server,share+2);
176 share = strchr_m(server,'\\');
177 if (!share) return NULL;
178 *share = 0;
179 share++;
181 server_n = server;
183 zero_sockaddr(&ss);
185 slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
187 make_nmb_name(&calling, myname, 0x0);
188 make_nmb_name(&called , server, 0x20);
190 again:
191 zero_sockaddr(&ss);
193 /* have to open a new connection */
194 if (!(c=cli_initialise())) {
195 DEBUG(0,("Connection to %s failed\n", server_n));
196 return NULL;
199 status = cli_connect(c, server_n, &ss);
200 if (!NT_STATUS_IS_OK(status)) {
201 DEBUG(0,("Connection to %s failed. Error %s\n", server_n, nt_errstr(status) ));
202 return NULL;
205 c->use_kerberos = use_kerberos;
207 if (!cli_session_request(c, &calling, &called)) {
208 DEBUG(0,("session request to %s failed\n", called.name));
209 cli_shutdown(c);
210 if (strcmp(called.name, "*SMBSERVER")) {
211 make_nmb_name(&called , "*SMBSERVER", 0x20);
212 goto again;
214 return NULL;
217 DEBUG(4,(" session request ok\n"));
219 status = cli_negprot(c);
220 if (!NT_STATUS_IS_OK(status)) {
221 DEBUG(0, ("protocol negotiation failed: %s\n",
222 nt_errstr(status)));
223 cli_shutdown(c);
224 return NULL;
227 if (!got_pass) {
228 char *pass = getpass("Password: ");
229 if (pass) {
230 fstrcpy(password[0], pass);
231 fstrcpy(password[1], pass);
235 if (got_pass == 1) {
236 fstrcpy(password[1], password[0]);
237 fstrcpy(username[1], username[0]);
240 status = cli_session_setup(c, username[snum],
241 password[snum], strlen(password[snum]),
242 password[snum], strlen(password[snum]),
243 lp_workgroup());
244 if (!NT_STATUS_IS_OK(status)) {
245 DEBUG(0,("session setup failed: %s\n", nt_errstr(status)));
246 return NULL;
250 * These next two lines are needed to emulate
251 * old client behaviour for people who have
252 * scripts based on client output.
253 * QUESTION ? Do we want to have a 'client compatibility
254 * mode to turn these on/off ? JRA.
257 if (*c->server_domain || *c->server_os || *c->server_type)
258 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
259 c->server_domain,c->server_os,c->server_type));
261 DEBUG(4,(" session setup ok\n"));
263 status = cli_tcon_andx(c, share, "?????", password[snum],
264 strlen(password[snum])+1);
265 if (!NT_STATUS_IS_OK(status)) {
266 DEBUG(0,("tree connect failed: %s\n", nt_errstr(status)));
267 cli_shutdown(c);
268 return NULL;
271 DEBUG(4,(" tconx ok\n"));
273 c->use_oplocks = use_oplocks;
275 return c;
279 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS], uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES],
280 char *share[NSERVERS])
282 int server, conn, f;
284 for (server=0;server<NSERVERS;server++)
285 for (conn=0;conn<NCONNECTIONS;conn++) {
286 if (cli[server][conn]) {
287 for (f=0;f<NFILES;f++) {
288 if (fnum[server][conn][f] != (uint16_t)-1) {
289 cli_close(cli[server][conn], fnum[server][conn][f]);
290 fnum[server][conn][f] = (uint16_t)-1;
293 cli_ulogoff(cli[server][conn]);
294 cli_shutdown(cli[server][conn]);
296 cli[server][conn] = connect_one(share[server], server);
297 if (!cli[server][conn]) {
298 DEBUG(0,("Failed to connect to %s\n", share[server]));
299 exit(1);
306 static bool test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
307 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES],
308 struct record *rec)
310 unsigned conn = rec->conn;
311 unsigned f = rec->f;
312 uint64_t start = rec->start;
313 uint64_t len = rec->len;
314 enum brl_type op = rec->lock_type;
315 int server;
316 bool ret[NSERVERS];
317 NTSTATUS status[NSERVERS];
319 switch (rec->lock_op) {
320 case OP_LOCK:
321 /* set a lock */
322 for (server=0;server<NSERVERS;server++) {
323 ret[server] = cli_lock64(cli[server][conn],
324 fnum[server][conn][f],
325 start, len, LOCK_TIMEOUT, op);
326 status[server] = cli_nt_error(cli[server][conn]);
327 if (!exact_error_codes &&
328 NT_STATUS_EQUAL(status[server],
329 NT_STATUS_FILE_LOCK_CONFLICT)) {
330 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
333 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
334 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
335 conn, f,
336 (double)start, (double)len,
337 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
338 nt_errstr(status[0]), nt_errstr(status[1]));
340 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
341 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
342 break;
344 case OP_UNLOCK:
345 /* unset a lock */
346 for (server=0;server<NSERVERS;server++) {
347 ret[server] = NT_STATUS_IS_OK(cli_unlock64(cli[server][conn],
348 fnum[server][conn][f],
349 start, len));
350 status[server] = cli_nt_error(cli[server][conn]);
352 if (showall ||
353 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
354 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
355 conn, f,
356 (double)start, (double)len,
357 nt_errstr(status[0]), nt_errstr(status[1]));
359 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
360 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
361 return False;
362 break;
364 case OP_REOPEN:
365 /* reopen the file */
366 for (server=0;server<NSERVERS;server++) {
367 cli_close(cli[server][conn], fnum[server][conn][f]);
368 fnum[server][conn][f] = (uint16_t)-1;
370 for (server=0;server<NSERVERS;server++) {
371 fnum[server][conn][f] = (uint16_t)-1;
372 if (!NT_STATUS_IS_OK(cli_open(cli[server][conn], FILENAME,
373 O_RDWR|O_CREAT,
374 DENY_NONE, &fnum[server][conn][f]))) {
375 printf("failed to reopen on share%d\n", server);
376 return False;
379 if (showall) {
380 printf("reopen conn=%u f=%u\n",
381 conn, f);
382 show_locks();
384 break;
387 return True;
390 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
391 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES])
393 int server, conn, f;
395 for (server=0;server<NSERVERS;server++)
396 for (conn=0;conn<NCONNECTIONS;conn++)
397 for (f=0;f<NFILES;f++) {
398 if (fnum[server][conn][f] != (uint16_t)-1) {
399 cli_close(cli[server][conn], fnum[server][conn][f]);
400 fnum[server][conn][f] = (uint16_t)-1;
403 for (server=0;server<NSERVERS;server++) {
404 cli_unlink(cli[server][0], FILENAME, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
408 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
409 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES])
411 int server, conn, f;
413 for (server=0;server<NSERVERS;server++)
414 for (conn=0;conn<NCONNECTIONS;conn++)
415 for (f=0;f<NFILES;f++) {
416 fnum[server][conn][f] = (uint16_t)-1;
417 if (!NT_STATUS_IS_OK(cli_open(cli[server][conn], FILENAME,
418 O_RDWR|O_CREAT,
419 DENY_NONE,
420 &fnum[server][conn][f]))) {
421 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
422 server, conn, f);
423 exit(1);
429 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
430 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES],
431 int n)
433 int i;
434 printf("testing %u ...\n", n);
435 for (i=0; i<n; i++) {
436 if (i && i % 100 == 0) {
437 printf("%u\n", i);
440 if (recorded[i].needed &&
441 !test_one(cli, fnum, &recorded[i])) return i;
443 return n;
447 /* each server has two connections open to it. Each connection has two file
448 descriptors open on the file - 8 file descriptors in total
450 we then do random locking ops in tamdem on the 4 fnums from each
451 server and ensure that the results match
453 static void test_locks(char *share[NSERVERS])
455 struct cli_state *cli[NSERVERS][NCONNECTIONS];
456 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES];
457 int n, i, n1, skip, r1, r2;
459 ZERO_STRUCT(fnum);
460 ZERO_STRUCT(cli);
462 recorded = SMB_MALLOC_ARRAY(struct record, numops);
464 for (n=0; n<numops; n++) {
465 #if PRESETS
466 if (n < sizeof(preset) / sizeof(preset[0])) {
467 recorded[n] = preset[n];
468 } else {
469 #endif
470 recorded[n].conn = random() % NCONNECTIONS;
471 recorded[n].f = random() % NFILES;
472 recorded[n].start = lock_base + ((unsigned)random() % (lock_range-1));
473 recorded[n].len = min_length +
474 random() % (lock_range-(recorded[n].start-lock_base));
475 recorded[n].start *= RANGE_MULTIPLE;
476 recorded[n].len *= RANGE_MULTIPLE;
477 r1 = random() % 100;
478 r2 = random() % 100;
479 if (r1 < READ_PCT) {
480 recorded[n].lock_type = READ_LOCK;
481 } else {
482 recorded[n].lock_type = WRITE_LOCK;
484 if (r2 < LOCK_PCT) {
485 recorded[n].lock_op = OP_LOCK;
486 } else if (r2 < UNLOCK_PCT) {
487 recorded[n].lock_op = OP_UNLOCK;
488 } else {
489 recorded[n].lock_op = OP_REOPEN;
491 recorded[n].needed = True;
492 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
493 recorded[n].len = 1;
495 #if PRESETS
497 #endif
500 reconnect(cli, fnum, share);
501 open_files(cli, fnum);
502 n = retest(cli, fnum, numops);
504 if (n == numops || !analyze) return;
505 n++;
507 skip = n/2;
509 while (1) {
510 n1 = n;
512 close_files(cli, fnum);
513 reconnect(cli, fnum, share);
514 open_files(cli, fnum);
516 for (i=0;i<n-skip;i+=skip) {
517 int m, j;
518 printf("excluding %d-%d\n", i, i+skip-1);
519 for (j=i;j<i+skip;j++) {
520 recorded[j].needed = False;
523 close_files(cli, fnum);
524 open_files(cli, fnum);
526 m = retest(cli, fnum, n);
527 if (m == n) {
528 for (j=i;j<i+skip;j++) {
529 recorded[j].needed = True;
531 } else {
532 if (i+(skip-1) < m) {
533 memmove(&recorded[i], &recorded[i+skip],
534 (m-(i+skip-1))*sizeof(recorded[0]));
536 n = m-(skip-1);
537 i--;
541 if (skip > 1) {
542 skip = skip/2;
543 printf("skip=%d\n", skip);
544 continue;
547 if (n1 == n) break;
550 close_files(cli, fnum);
551 reconnect(cli, fnum, share);
552 open_files(cli, fnum);
553 showall = True;
554 n1 = retest(cli, fnum, n);
555 if (n1 != n-1) {
556 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
558 close_files(cli, fnum);
560 for (i=0;i<n;i++) {
561 printf("{%s, %s, conn = %u, file = %u, start = %.0f, len = %.0f, %u},\n",
562 lock_op_name(recorded[i].lock_op),
563 lock_op_type(recorded[i].lock_type),
564 recorded[i].conn,
565 recorded[i].f,
566 (double)recorded[i].start,
567 (double)recorded[i].len,
568 recorded[i].needed);
574 static void usage(void)
576 printf(
577 "Usage:\n\
578 locktest //server1/share1 //server2/share2 [options..]\n\
579 options:\n\
580 -U user%%pass (may be specified twice)\n\
581 -k use kerberos\n\
582 -s seed\n\
583 -o numops\n\
584 -u hide unlock fails\n\
585 -a (show all ops)\n\
586 -A analyse for minimal ops\n\
587 -O use oplocks\n\
588 -E enable exact error code checking\n\
589 -Z enable the zero/zero lock\n\
590 -R range set lock range\n\
591 -B base set lock base\n\
592 -M min set min lock length\n\
596 /****************************************************************************
597 main program
598 ****************************************************************************/
599 int main(int argc,char *argv[])
601 char *share[NSERVERS];
602 int opt;
603 char *p;
604 int seed, server;
606 setlinebuf(stdout);
608 load_case_tables();
610 if (argc < 3 || argv[1][0] == '-') {
611 usage();
612 exit(1);
615 setup_logging(argv[0], DEBUG_STDOUT);
617 for (server=0;server<NSERVERS;server++) {
618 share[server] = argv[1+server];
619 all_string_sub(share[server],"/","\\",0);
622 argc -= NSERVERS;
623 argv += NSERVERS;
625 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
626 load_interfaces();
628 if (getenv("USER")) {
629 fstrcpy(username[0],getenv("USER"));
630 fstrcpy(username[1],getenv("USER"));
633 seed = time(NULL);
635 while ((opt = getopt(argc, argv, "U:s:ho:aAW:OkR:B:M:EZ")) != EOF) {
636 switch (opt) {
637 case 'k':
638 #ifdef HAVE_KRB5
639 use_kerberos = True;
640 #else
641 d_printf("No kerberos support compiled in\n");
642 exit(1);
643 #endif
644 break;
645 case 'U':
646 got_user = 1;
647 if (got_pass == 2) {
648 d_printf("Max of 2 usernames\n");
649 exit(1);
651 fstrcpy(username[got_pass],optarg);
652 p = strchr_m(username[got_pass],'%');
653 if (p) {
654 *p = 0;
655 fstrcpy(password[got_pass], p+1);
656 got_pass++;
658 break;
659 case 'R':
660 lock_range = strtol(optarg, NULL, 0);
661 break;
662 case 'B':
663 lock_base = strtol(optarg, NULL, 0);
664 break;
665 case 'M':
666 min_length = strtol(optarg, NULL, 0);
667 break;
668 case 's':
669 seed = atoi(optarg);
670 break;
671 case 'u':
672 hide_unlock_fails = True;
673 break;
674 case 'o':
675 numops = atoi(optarg);
676 break;
677 case 'O':
678 use_oplocks = True;
679 break;
680 case 'a':
681 showall = True;
682 break;
683 case 'A':
684 analyze = True;
685 break;
686 case 'Z':
687 zero_zero = True;
688 break;
689 case 'E':
690 exact_error_codes = True;
691 break;
692 case 'h':
693 usage();
694 exit(1);
695 default:
696 printf("Unknown option %c (%d)\n", (char)opt, opt);
697 exit(1);
701 if(use_kerberos && !got_user) got_pass = True;
703 argc -= optind;
704 argv += optind;
706 DEBUG(0,("seed=%u\n", seed));
707 srandom(seed);
709 test_locks(share);
711 return(0);