source: branches/samba-3.3.x/source/torture/locktest.c

Last change on this file was 206, checked in by Herwig Bauernfeind, 16 years ago

Import Samba 3.3 branch at 3.0.0 level (psmedley's port)

File size: 16.2 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 randomised byte range lock tester
4 Copyright (C) Andrew Tridgell 1999
5
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.
10
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.
15
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/>.
18*/
19
20#include "includes.h"
21
22static fstring password[2];
23static fstring username[2];
24static int got_user;
25static int got_pass;
26static bool use_kerberos;
27static int numops = 1000;
28static bool showall;
29static bool analyze;
30static bool hide_unlock_fails;
31static bool use_oplocks;
32static unsigned lock_range = 100;
33static unsigned lock_base = 0;
34static unsigned min_length = 0;
35static bool exact_error_codes;
36static bool zero_zero;
37
38extern char *optarg;
39extern int optind;
40
41#define FILENAME "\\locktest.dat"
42
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
51
52#define NASTY_POSIX_LOCK_HACK 0
53
54enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
55
56static const char *lock_op_type(int op)
57{
58 if (op == WRITE_LOCK) return "write";
59 else if (op == READ_LOCK) return "read";
60 else return "other";
61}
62
63static const char *lock_op_name(enum lock_op op)
64{
65 if (op == OP_LOCK) return "lock";
66 else if (op == OP_UNLOCK) return "unlock";
67 else return "reopen";
68}
69
70struct record {
71 enum lock_op lock_op;
72 enum brl_type lock_type;
73 char conn, f;
74 SMB_BIG_UINT start, len;
75 char needed;
76};
77
78#define PRESETS 0
79
80#if PRESETS
81static 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},
87
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},
92
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},
97
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},
102
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},
107
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},
112
113};
114#endif
115
116static struct record *recorded;
117
118static 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)
125{
126#if NASTY_POSIX_LOCK_HACK
127 {
128 static SMB_INO_T lastino;
129
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);
136 }
137 }
138 lastino = ino;
139 }
140#endif
141
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);
146
147}
148
149
150static void show_locks(void)
151{
152 brl_forall(print_brl, NULL);
153 /* system("cat /proc/locks"); */
154}
155
156
157/*****************************************************
158return a connection to a server
159*******************************************************/
160static struct cli_state *connect_one(char *share, int snum)
161{
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;
170
171 fstrcpy(server,share+2);
172 share = strchr_m(server,'\\');
173 if (!share) return NULL;
174 *share = 0;
175 share++;
176
177 server_n = server;
178
179 zero_sockaddr(&ss);
180
181 slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
182
183 make_nmb_name(&calling, myname, 0x0);
184 make_nmb_name(&called , server, 0x20);
185
186 again:
187 zero_sockaddr(&ss);
188
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;
193 }
194
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;
199 }
200
201 c->use_kerberos = use_kerberos;
202
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;
209 }
210 return NULL;
211 }
212
213 DEBUG(4,(" session request ok\n"));
214
215 if (!cli_negprot(c)) {
216 DEBUG(0,("protocol negotiation failed\n"));
217 cli_shutdown(c);
218 return NULL;
219 }
220
221 if (!got_pass) {
222 char *pass = getpass("Password: ");
223 if (pass) {
224 fstrcpy(password[0], pass);
225 fstrcpy(password[1], pass);
226 }
227 }
228
229 if (got_pass == 1) {
230 fstrcpy(password[1], password[0]);
231 fstrcpy(username[1], username[0]);
232 }
233
234 if (!NT_STATUS_IS_OK(cli_session_setup(c, username[snum],
235 password[snum],
236 strlen(password[snum]),
237 password[snum],
238 strlen(password[snum]),
239 lp_workgroup()))) {
240 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
241 return NULL;
242 }
243
244 /*
245 * These next two lines are needed to emulate
246 * old client behaviour for people who have
247 * scripts based on client output.
248 * QUESTION ? Do we want to have a 'client compatibility
249 * mode to turn these on/off ? JRA.
250 */
251
252 if (*c->server_domain || *c->server_os || *c->server_type)
253 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
254 c->server_domain,c->server_os,c->server_type));
255
256 DEBUG(4,(" session setup ok\n"));
257
258 if (!cli_send_tconX(c, share, "?????",
259 password[snum], strlen(password[snum])+1)) {
260 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
261 cli_shutdown(c);
262 return NULL;
263 }
264
265 DEBUG(4,(" tconx ok\n"));
266
267 c->use_oplocks = use_oplocks;
268
269 return c;
270}
271
272
273static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
274 char *share[NSERVERS])
275{
276 int server, conn, f;
277
278 for (server=0;server<NSERVERS;server++)
279 for (conn=0;conn<NCONNECTIONS;conn++) {
280 if (cli[server][conn]) {
281 for (f=0;f<NFILES;f++) {
282 if (fnum[server][conn][f] != -1) {
283 cli_close(cli[server][conn], fnum[server][conn][f]);
284 fnum[server][conn][f] = -1;
285 }
286 }
287 cli_ulogoff(cli[server][conn]);
288 cli_shutdown(cli[server][conn]);
289 }
290 cli[server][conn] = connect_one(share[server], server);
291 if (!cli[server][conn]) {
292 DEBUG(0,("Failed to connect to %s\n", share[server]));
293 exit(1);
294 }
295 }
296}
297
298
299
300static bool test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
301 int fnum[NSERVERS][NCONNECTIONS][NFILES],
302 struct record *rec)
303{
304 unsigned conn = rec->conn;
305 unsigned f = rec->f;
306 SMB_BIG_UINT start = rec->start;
307 SMB_BIG_UINT len = rec->len;
308 enum brl_type op = rec->lock_type;
309 int server;
310 bool ret[NSERVERS];
311 NTSTATUS status[NSERVERS];
312
313 switch (rec->lock_op) {
314 case OP_LOCK:
315 /* set a lock */
316 for (server=0;server<NSERVERS;server++) {
317 ret[server] = cli_lock64(cli[server][conn],
318 fnum[server][conn][f],
319 start, len, LOCK_TIMEOUT, op);
320 status[server] = cli_nt_error(cli[server][conn]);
321 if (!exact_error_codes &&
322 NT_STATUS_EQUAL(status[server],
323 NT_STATUS_FILE_LOCK_CONFLICT)) {
324 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
325 }
326 }
327 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
328 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
329 conn, f,
330 (double)start, (double)len,
331 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
332 nt_errstr(status[0]), nt_errstr(status[1]));
333 }
334 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
335 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
336 break;
337
338 case OP_UNLOCK:
339 /* unset a lock */
340 for (server=0;server<NSERVERS;server++) {
341 ret[server] = cli_unlock64(cli[server][conn],
342 fnum[server][conn][f],
343 start, len);
344 status[server] = cli_nt_error(cli[server][conn]);
345 }
346 if (showall ||
347 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
348 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
349 conn, f,
350 (double)start, (double)len,
351 nt_errstr(status[0]), nt_errstr(status[1]));
352 }
353 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
354 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
355 return False;
356 break;
357
358 case OP_REOPEN:
359 /* reopen the file */
360 for (server=0;server<NSERVERS;server++) {
361 cli_close(cli[server][conn], fnum[server][conn][f]);
362 fnum[server][conn][f] = -1;
363 }
364 for (server=0;server<NSERVERS;server++) {
365 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
366 O_RDWR|O_CREAT,
367 DENY_NONE);
368 if (fnum[server][conn][f] == -1) {
369 printf("failed to reopen on share%d\n", server);
370 return False;
371 }
372 }
373 if (showall) {
374 printf("reopen conn=%u f=%u\n",
375 conn, f);
376 show_locks();
377 }
378 break;
379 }
380
381 return True;
382}
383
384static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
385 int fnum[NSERVERS][NCONNECTIONS][NFILES])
386{
387 int server, conn, f;
388
389 for (server=0;server<NSERVERS;server++)
390 for (conn=0;conn<NCONNECTIONS;conn++)
391 for (f=0;f<NFILES;f++) {
392 if (fnum[server][conn][f] != -1) {
393 cli_close(cli[server][conn], fnum[server][conn][f]);
394 fnum[server][conn][f] = -1;
395 }
396 }
397 for (server=0;server<NSERVERS;server++) {
398 cli_unlink(cli[server][0], FILENAME);
399 }
400}
401
402static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
403 int fnum[NSERVERS][NCONNECTIONS][NFILES])
404{
405 int server, conn, f;
406
407 for (server=0;server<NSERVERS;server++)
408 for (conn=0;conn<NCONNECTIONS;conn++)
409 for (f=0;f<NFILES;f++) {
410 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
411 O_RDWR|O_CREAT,
412 DENY_NONE);
413 if (fnum[server][conn][f] == -1) {
414 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
415 server, conn, f);
416 exit(1);
417 }
418 }
419}
420
421
422static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
423 int fnum[NSERVERS][NCONNECTIONS][NFILES],
424 int n)
425{
426 int i;
427 printf("testing %u ...\n", n);
428 for (i=0; i<n; i++) {
429 if (i && i % 100 == 0) {
430 printf("%u\n", i);
431 }
432
433 if (recorded[i].needed &&
434 !test_one(cli, fnum, &recorded[i])) return i;
435 }
436 return n;
437}
438
439
440/* each server has two connections open to it. Each connection has two file
441 descriptors open on the file - 8 file descriptors in total
442
443 we then do random locking ops in tamdem on the 4 fnums from each
444 server and ensure that the results match
445 */
446static void test_locks(char *share[NSERVERS])
447{
448 struct cli_state *cli[NSERVERS][NCONNECTIONS];
449 int fnum[NSERVERS][NCONNECTIONS][NFILES];
450 int n, i, n1, skip, r1, r2;
451
452 ZERO_STRUCT(fnum);
453 ZERO_STRUCT(cli);
454
455 recorded = SMB_MALLOC_ARRAY(struct record, numops);
456
457 for (n=0; n<numops; n++) {
458#if PRESETS
459 if (n < sizeof(preset) / sizeof(preset[0])) {
460 recorded[n] = preset[n];
461 } else {
462#endif
463 recorded[n].conn = random() % NCONNECTIONS;
464 recorded[n].f = random() % NFILES;
465 recorded[n].start = lock_base + ((unsigned)random() % (lock_range-1));
466 recorded[n].len = min_length +
467 random() % (lock_range-(recorded[n].start-lock_base));
468 recorded[n].start *= RANGE_MULTIPLE;
469 recorded[n].len *= RANGE_MULTIPLE;
470 r1 = random() % 100;
471 r2 = random() % 100;
472 if (r1 < READ_PCT) {
473 recorded[n].lock_type = READ_LOCK;
474 } else {
475 recorded[n].lock_type = WRITE_LOCK;
476 }
477 if (r2 < LOCK_PCT) {
478 recorded[n].lock_op = OP_LOCK;
479 } else if (r2 < UNLOCK_PCT) {
480 recorded[n].lock_op = OP_UNLOCK;
481 } else {
482 recorded[n].lock_op = OP_REOPEN;
483 }
484 recorded[n].needed = True;
485 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
486 recorded[n].len = 1;
487 }
488#if PRESETS
489 }
490#endif
491 }
492
493 reconnect(cli, fnum, share);
494 open_files(cli, fnum);
495 n = retest(cli, fnum, numops);
496
497 if (n == numops || !analyze) return;
498 n++;
499
500 skip = n/2;
501
502 while (1) {
503 n1 = n;
504
505 close_files(cli, fnum);
506 reconnect(cli, fnum, share);
507 open_files(cli, fnum);
508
509 for (i=0;i<n-skip;i+=skip) {
510 int m, j;
511 printf("excluding %d-%d\n", i, i+skip-1);
512 for (j=i;j<i+skip;j++) {
513 recorded[j].needed = False;
514 }
515
516 close_files(cli, fnum);
517 open_files(cli, fnum);
518
519 m = retest(cli, fnum, n);
520 if (m == n) {
521 for (j=i;j<i+skip;j++) {
522 recorded[j].needed = True;
523 }
524 } else {
525 if (i+(skip-1) < m) {
526 memmove(&recorded[i], &recorded[i+skip],
527 (m-(i+skip-1))*sizeof(recorded[0]));
528 }
529 n = m-(skip-1);
530 i--;
531 }
532 }
533
534 if (skip > 1) {
535 skip = skip/2;
536 printf("skip=%d\n", skip);
537 continue;
538 }
539
540 if (n1 == n) break;
541 }
542
543 close_files(cli, fnum);
544 reconnect(cli, fnum, share);
545 open_files(cli, fnum);
546 showall = True;
547 n1 = retest(cli, fnum, n);
548 if (n1 != n-1) {
549 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
550 }
551 close_files(cli, fnum);
552
553 for (i=0;i<n;i++) {
554 printf("{%s, %s, conn = %u, file = %u, start = %.0f, len = %.0f, %u},\n",
555 lock_op_name(recorded[i].lock_op),
556 lock_op_type(recorded[i].lock_type),
557 recorded[i].conn,
558 recorded[i].f,
559 (double)recorded[i].start,
560 (double)recorded[i].len,
561 recorded[i].needed);
562 }
563}
564
565
566
567static void usage(void)
568{
569 printf(
570"Usage:\n\
571 locktest //server1/share1 //server2/share2 [options..]\n\
572 options:\n\
573 -U user%%pass (may be specified twice)\n\
574 -k use kerberos\n\
575 -s seed\n\
576 -o numops\n\
577 -u hide unlock fails\n\
578 -a (show all ops)\n\
579 -A analyse for minimal ops\n\
580 -O use oplocks\n\
581 -E enable exact error code checking\n\
582 -Z enable the zero/zero lock\n\
583 -R range set lock range\n\
584 -B base set lock base\n\
585 -M min set min lock length\n\
586");
587}
588
589/****************************************************************************
590 main program
591****************************************************************************/
592 int main(int argc,char *argv[])
593{
594 char *share[NSERVERS];
595 int opt;
596 char *p;
597 int seed, server;
598
599 setlinebuf(stdout);
600
601 load_case_tables();
602
603 dbf = x_stderr;
604
605 if (argc < 3 || argv[1][0] == '-') {
606 usage();
607 exit(1);
608 }
609
610 setup_logging(argv[0],True);
611
612 for (server=0;server<NSERVERS;server++) {
613 share[server] = argv[1+server];
614 all_string_sub(share[server],"/","\\",0);
615 }
616
617 argc -= NSERVERS;
618 argv += NSERVERS;
619
620 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
621 load_interfaces();
622
623 if (getenv("USER")) {
624 fstrcpy(username[0],getenv("USER"));
625 fstrcpy(username[1],getenv("USER"));
626 }
627
628 seed = time(NULL);
629
630 while ((opt = getopt(argc, argv, "U:s:ho:aAW:OkR:B:M:EZ")) != EOF) {
631 switch (opt) {
632 case 'k':
633#ifdef HAVE_KRB5
634 use_kerberos = True;
635#else
636 d_printf("No kerberos support compiled in\n");
637 exit(1);
638#endif
639 break;
640 case 'U':
641 got_user = 1;
642 if (got_pass == 2) {
643 d_printf("Max of 2 usernames\n");
644 exit(1);
645 }
646 fstrcpy(username[got_pass],optarg);
647 p = strchr_m(username[got_pass],'%');
648 if (p) {
649 *p = 0;
650 fstrcpy(password[got_pass], p+1);
651 got_pass++;
652 }
653 break;
654 case 'R':
655 lock_range = strtol(optarg, NULL, 0);
656 break;
657 case 'B':
658 lock_base = strtol(optarg, NULL, 0);
659 break;
660 case 'M':
661 min_length = strtol(optarg, NULL, 0);
662 break;
663 case 's':
664 seed = atoi(optarg);
665 break;
666 case 'u':
667 hide_unlock_fails = True;
668 break;
669 case 'o':
670 numops = atoi(optarg);
671 break;
672 case 'O':
673 use_oplocks = True;
674 break;
675 case 'a':
676 showall = True;
677 break;
678 case 'A':
679 analyze = True;
680 break;
681 case 'Z':
682 zero_zero = True;
683 break;
684 case 'E':
685 exact_error_codes = True;
686 break;
687 case 'h':
688 usage();
689 exit(1);
690 default:
691 printf("Unknown option %c (%d)\n", (char)opt, opt);
692 exit(1);
693 }
694 }
695
696 if(use_kerberos && !got_user) got_pass = True;
697
698 argc -= optind;
699 argv += optind;
700
701 DEBUG(0,("seed=%u\n", seed));
702 srandom(seed);
703
704 test_locks(share);
705
706 return(0);
707}
Note: See TracBrowser for help on using the repository browser.