s3-libsmb: let cli_lock64 return NTSTATUS instead of bool
[Samba/gebeck_regimport.git] / source3 / torture / locktest.c
bloba5ccf24c331568b0b0ad53409a2c7bbc09da3dad
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 char *server_n;
168 fstring server;
169 fstring myname;
170 static int count;
171 NTSTATUS status;
173 fstrcpy(server,share+2);
174 share = strchr_m(server,'\\');
175 if (!share) return NULL;
176 *share = 0;
177 share++;
179 server_n = server;
181 slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
183 /* have to open a new connection */
185 status = cli_connect_nb(server_n, NULL, 0, 0x20, myname, Undefined,
186 &c);
187 if (!NT_STATUS_IS_OK(status)) {
188 DEBUG(0, ("Connection to %s failed. Error %s\n", server_n,
189 nt_errstr(status)));
190 return NULL;
193 c->use_kerberos = use_kerberos;
195 status = cli_negprot(c);
196 if (!NT_STATUS_IS_OK(status)) {
197 DEBUG(0, ("protocol negotiation failed: %s\n",
198 nt_errstr(status)));
199 cli_shutdown(c);
200 return NULL;
203 if (!got_pass) {
204 char *pass = getpass("Password: ");
205 if (pass) {
206 fstrcpy(password[0], pass);
207 fstrcpy(password[1], pass);
211 if (got_pass == 1) {
212 strlcpy(password[1], password[0],sizeof(password[1]));
213 strlcpy(username[1], username[0],sizeof(username[1]));
216 status = cli_session_setup(c, username[snum],
217 password[snum], strlen(password[snum]),
218 password[snum], strlen(password[snum]),
219 lp_workgroup());
220 if (!NT_STATUS_IS_OK(status)) {
221 DEBUG(0,("session setup failed: %s\n", nt_errstr(status)));
222 return NULL;
226 * These next two lines are needed to emulate
227 * old client behaviour for people who have
228 * scripts based on client output.
229 * QUESTION ? Do we want to have a 'client compatibility
230 * mode to turn these on/off ? JRA.
233 if (*c->server_domain || *c->server_os || *c->server_type)
234 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
235 c->server_domain,c->server_os,c->server_type));
237 DEBUG(4,(" session setup ok\n"));
239 status = cli_tcon_andx(c, share, "?????", password[snum],
240 strlen(password[snum])+1);
241 if (!NT_STATUS_IS_OK(status)) {
242 DEBUG(0,("tree connect failed: %s\n", nt_errstr(status)));
243 cli_shutdown(c);
244 return NULL;
247 DEBUG(4,(" tconx ok\n"));
249 c->use_oplocks = use_oplocks;
251 return c;
255 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS], uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES],
256 char *share[NSERVERS])
258 int server, conn, f;
260 for (server=0;server<NSERVERS;server++)
261 for (conn=0;conn<NCONNECTIONS;conn++) {
262 if (cli[server][conn]) {
263 for (f=0;f<NFILES;f++) {
264 if (fnum[server][conn][f] != (uint16_t)-1) {
265 cli_close(cli[server][conn], fnum[server][conn][f]);
266 fnum[server][conn][f] = (uint16_t)-1;
269 cli_ulogoff(cli[server][conn]);
270 cli_shutdown(cli[server][conn]);
272 cli[server][conn] = connect_one(share[server], server);
273 if (!cli[server][conn]) {
274 DEBUG(0,("Failed to connect to %s\n", share[server]));
275 exit(1);
282 static bool test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
283 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES],
284 struct record *rec)
286 unsigned conn = rec->conn;
287 unsigned f = rec->f;
288 uint64_t start = rec->start;
289 uint64_t len = rec->len;
290 enum brl_type op = rec->lock_type;
291 int server;
292 NTSTATUS status[NSERVERS];
294 switch (rec->lock_op) {
295 case OP_LOCK:
296 /* set a lock */
297 for (server=0;server<NSERVERS;server++) {
298 status[server] = cli_lock64(cli[server][conn],
299 fnum[server][conn][f],
300 start, len, LOCK_TIMEOUT,
301 op);
302 if (!exact_error_codes &&
303 NT_STATUS_EQUAL(status[server],
304 NT_STATUS_FILE_LOCK_CONFLICT)) {
305 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
308 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
309 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
310 conn, f,
311 (double)start, (double)len,
312 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
313 nt_errstr(status[0]), nt_errstr(status[1]));
315 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
316 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
317 break;
319 case OP_UNLOCK:
320 /* unset a lock */
321 for (server=0;server<NSERVERS;server++) {
322 status[server] = cli_unlock64(cli[server][conn],
323 fnum[server][conn][f],
324 start, len);
326 if (showall ||
327 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
328 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
329 conn, f,
330 (double)start, (double)len,
331 nt_errstr(status[0]), nt_errstr(status[1]));
333 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
334 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
335 return False;
336 break;
338 case OP_REOPEN:
339 /* reopen the file */
340 for (server=0;server<NSERVERS;server++) {
341 cli_close(cli[server][conn], fnum[server][conn][f]);
342 fnum[server][conn][f] = (uint16_t)-1;
344 for (server=0;server<NSERVERS;server++) {
345 fnum[server][conn][f] = (uint16_t)-1;
346 if (!NT_STATUS_IS_OK(cli_open(cli[server][conn], FILENAME,
347 O_RDWR|O_CREAT,
348 DENY_NONE, &fnum[server][conn][f]))) {
349 printf("failed to reopen on share%d\n", server);
350 return False;
353 if (showall) {
354 printf("reopen conn=%u f=%u\n",
355 conn, f);
356 show_locks();
358 break;
361 return True;
364 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
365 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES])
367 int server, conn, f;
369 for (server=0;server<NSERVERS;server++)
370 for (conn=0;conn<NCONNECTIONS;conn++)
371 for (f=0;f<NFILES;f++) {
372 if (fnum[server][conn][f] != (uint16_t)-1) {
373 cli_close(cli[server][conn], fnum[server][conn][f]);
374 fnum[server][conn][f] = (uint16_t)-1;
377 for (server=0;server<NSERVERS;server++) {
378 cli_unlink(cli[server][0], FILENAME, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
382 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
383 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES])
385 int server, conn, f;
387 for (server=0;server<NSERVERS;server++)
388 for (conn=0;conn<NCONNECTIONS;conn++)
389 for (f=0;f<NFILES;f++) {
390 fnum[server][conn][f] = (uint16_t)-1;
391 if (!NT_STATUS_IS_OK(cli_open(cli[server][conn], FILENAME,
392 O_RDWR|O_CREAT,
393 DENY_NONE,
394 &fnum[server][conn][f]))) {
395 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
396 server, conn, f);
397 exit(1);
403 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
404 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES],
405 int n)
407 int i;
408 printf("testing %u ...\n", n);
409 for (i=0; i<n; i++) {
410 if (i && i % 100 == 0) {
411 printf("%u\n", i);
414 if (recorded[i].needed &&
415 !test_one(cli, fnum, &recorded[i])) return i;
417 return n;
421 /* each server has two connections open to it. Each connection has two file
422 descriptors open on the file - 8 file descriptors in total
424 we then do random locking ops in tamdem on the 4 fnums from each
425 server and ensure that the results match
427 static void test_locks(char *share[NSERVERS])
429 struct cli_state *cli[NSERVERS][NCONNECTIONS];
430 uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES];
431 int n, i, n1, skip, r1, r2;
433 ZERO_STRUCT(fnum);
434 ZERO_STRUCT(cli);
436 recorded = SMB_MALLOC_ARRAY(struct record, numops);
438 for (n=0; n<numops; n++) {
439 #if PRESETS
440 if (n < sizeof(preset) / sizeof(preset[0])) {
441 recorded[n] = preset[n];
442 } else {
443 #endif
444 recorded[n].conn = random() % NCONNECTIONS;
445 recorded[n].f = random() % NFILES;
446 recorded[n].start = lock_base + ((unsigned)random() % (lock_range-1));
447 recorded[n].len = min_length +
448 random() % (lock_range-(recorded[n].start-lock_base));
449 recorded[n].start *= RANGE_MULTIPLE;
450 recorded[n].len *= RANGE_MULTIPLE;
451 r1 = random() % 100;
452 r2 = random() % 100;
453 if (r1 < READ_PCT) {
454 recorded[n].lock_type = READ_LOCK;
455 } else {
456 recorded[n].lock_type = WRITE_LOCK;
458 if (r2 < LOCK_PCT) {
459 recorded[n].lock_op = OP_LOCK;
460 } else if (r2 < UNLOCK_PCT) {
461 recorded[n].lock_op = OP_UNLOCK;
462 } else {
463 recorded[n].lock_op = OP_REOPEN;
465 recorded[n].needed = True;
466 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
467 recorded[n].len = 1;
469 #if PRESETS
471 #endif
474 reconnect(cli, fnum, share);
475 open_files(cli, fnum);
476 n = retest(cli, fnum, numops);
478 if (n == numops || !analyze) return;
479 n++;
481 skip = n/2;
483 while (1) {
484 n1 = n;
486 close_files(cli, fnum);
487 reconnect(cli, fnum, share);
488 open_files(cli, fnum);
490 for (i=0;i<n-skip;i+=skip) {
491 int m, j;
492 printf("excluding %d-%d\n", i, i+skip-1);
493 for (j=i;j<i+skip;j++) {
494 recorded[j].needed = False;
497 close_files(cli, fnum);
498 open_files(cli, fnum);
500 m = retest(cli, fnum, n);
501 if (m == n) {
502 for (j=i;j<i+skip;j++) {
503 recorded[j].needed = True;
505 } else {
506 if (i+(skip-1) < m) {
507 memmove(&recorded[i], &recorded[i+skip],
508 (m-(i+skip-1))*sizeof(recorded[0]));
510 n = m-(skip-1);
511 i--;
515 if (skip > 1) {
516 skip = skip/2;
517 printf("skip=%d\n", skip);
518 continue;
521 if (n1 == n) break;
524 close_files(cli, fnum);
525 reconnect(cli, fnum, share);
526 open_files(cli, fnum);
527 showall = True;
528 n1 = retest(cli, fnum, n);
529 if (n1 != n-1) {
530 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
532 close_files(cli, fnum);
534 for (i=0;i<n;i++) {
535 printf("{%s, %s, conn = %u, file = %u, start = %.0f, len = %.0f, %u},\n",
536 lock_op_name(recorded[i].lock_op),
537 lock_op_type(recorded[i].lock_type),
538 recorded[i].conn,
539 recorded[i].f,
540 (double)recorded[i].start,
541 (double)recorded[i].len,
542 recorded[i].needed);
548 static void usage(void)
550 printf(
551 "Usage:\n\
552 locktest //server1/share1 //server2/share2 [options..]\n\
553 options:\n\
554 -U user%%pass (may be specified twice)\n\
555 -k use kerberos\n\
556 -s seed\n\
557 -o numops\n\
558 -u hide unlock fails\n\
559 -a (show all ops)\n\
560 -A analyse for minimal ops\n\
561 -O use oplocks\n\
562 -E enable exact error code checking\n\
563 -Z enable the zero/zero lock\n\
564 -R range set lock range\n\
565 -B base set lock base\n\
566 -M min set min lock length\n\
570 /****************************************************************************
571 main program
572 ****************************************************************************/
573 int main(int argc,char *argv[])
575 char *share[NSERVERS];
576 int opt;
577 char *p;
578 int seed, server;
580 setlinebuf(stdout);
582 load_case_tables();
584 if (argc < 3 || argv[1][0] == '-') {
585 usage();
586 exit(1);
589 setup_logging(argv[0], DEBUG_STDOUT);
591 for (server=0;server<NSERVERS;server++) {
592 share[server] = argv[1+server];
593 all_string_sub(share[server],"/","\\",0);
596 argc -= NSERVERS;
597 argv += NSERVERS;
599 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
600 load_interfaces();
602 if (getenv("USER")) {
603 fstrcpy(username[0],getenv("USER"));
604 fstrcpy(username[1],getenv("USER"));
607 seed = time(NULL);
609 while ((opt = getopt(argc, argv, "U:s:ho:aAW:OkR:B:M:EZ")) != EOF) {
610 switch (opt) {
611 case 'k':
612 #ifdef HAVE_KRB5
613 use_kerberos = True;
614 #else
615 d_printf("No kerberos support compiled in\n");
616 exit(1);
617 #endif
618 break;
619 case 'U':
620 got_user = 1;
621 if (got_pass == 2) {
622 d_printf("Max of 2 usernames\n");
623 exit(1);
625 fstrcpy(username[got_pass],optarg);
626 p = strchr_m(username[got_pass],'%');
627 if (p) {
628 *p = 0;
629 fstrcpy(password[got_pass], p+1);
630 got_pass++;
632 break;
633 case 'R':
634 lock_range = strtol(optarg, NULL, 0);
635 break;
636 case 'B':
637 lock_base = strtol(optarg, NULL, 0);
638 break;
639 case 'M':
640 min_length = strtol(optarg, NULL, 0);
641 break;
642 case 's':
643 seed = atoi(optarg);
644 break;
645 case 'u':
646 hide_unlock_fails = True;
647 break;
648 case 'o':
649 numops = atoi(optarg);
650 break;
651 case 'O':
652 use_oplocks = True;
653 break;
654 case 'a':
655 showall = True;
656 break;
657 case 'A':
658 analyze = True;
659 break;
660 case 'Z':
661 zero_zero = True;
662 break;
663 case 'E':
664 exact_error_codes = True;
665 break;
666 case 'h':
667 usage();
668 exit(1);
669 default:
670 printf("Unknown option %c (%d)\n", (char)opt, opt);
671 exit(1);
675 if(use_kerberos && !got_user) got_pass = True;
677 argc -= optind;
678 argv += optind;
680 DEBUG(0,("seed=%u\n", seed));
681 srandom(seed);
683 test_locks(share);
685 return(0);