00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "includes.h"
00022
00023 static fstring password[2];
00024 static fstring username[2];
00025 static int got_user;
00026 static int got_pass;
00027 static BOOL use_kerberos;
00028 static int numops = 1000;
00029 static BOOL showall;
00030 static BOOL analyze;
00031 static BOOL hide_unlock_fails;
00032 static BOOL use_oplocks;
00033 static unsigned lock_range = 100;
00034 static unsigned lock_base = 0;
00035 static unsigned min_length = 0;
00036 static BOOL exact_error_codes;
00037 static BOOL zero_zero;
00038
00039 extern char *optarg;
00040 extern int optind;
00041
00042 #define FILENAME "\\locktest.dat"
00043
00044 #define READ_PCT 50
00045 #define LOCK_PCT 45
00046 #define UNLOCK_PCT 70
00047 #define RANGE_MULTIPLE 1
00048 #define NSERVERS 2
00049 #define NCONNECTIONS 2
00050 #define NFILES 2
00051 #define LOCK_TIMEOUT 0
00052
00053 #define NASTY_POSIX_LOCK_HACK 0
00054
00055 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
00056
00057 static const char *lock_op_type(int op)
00058 {
00059 if (op == WRITE_LOCK) return "write";
00060 else if (op == READ_LOCK) return "read";
00061 else return "other";
00062 }
00063
00064 static const char *lock_op_name(enum lock_op op)
00065 {
00066 if (op == OP_LOCK) return "lock";
00067 else if (op == OP_UNLOCK) return "unlock";
00068 else return "reopen";
00069 }
00070
00071 struct record {
00072 enum lock_op lock_op;
00073 enum brl_type lock_type;
00074 char conn, f;
00075 SMB_BIG_UINT start, len;
00076 char needed;
00077 };
00078
00079 #define PRESETS 0
00080
00081 #if PRESETS
00082 static struct record preset[] = {
00083 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
00084 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
00085 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 0, 1},
00086 {OP_UNLOCK, 0 , 0, 0, 2, 0, 1},
00087 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
00088
00089 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
00090 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
00091 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
00092 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
00093
00094 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
00095 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 1, 1},
00096 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
00097 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
00098
00099 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
00100 {OP_LOCK, WRITE_LOCK, 0, 0, 1, 1, 1},
00101 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
00102 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
00103
00104 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
00105 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
00106 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
00107 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
00108
00109 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
00110 {OP_LOCK, READ_LOCK, 0, 0, 3, 1, 1},
00111 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
00112 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
00113
00114 };
00115 #endif
00116
00117 static struct record *recorded;
00118
00119 static void print_brl(SMB_DEV_T dev,
00120 SMB_INO_T ino,
00121 struct process_id pid,
00122 enum brl_type lock_type,
00123 enum brl_flavour lock_flav,
00124 br_off start,
00125 br_off size)
00126 {
00127 #if NASTY_POSIX_LOCK_HACK
00128 {
00129 pstring cmd;
00130 static SMB_INO_T lastino;
00131
00132 if (lastino != ino) {
00133 slprintf(cmd, sizeof(cmd),
00134 "egrep POSIX.*%u /proc/locks", (int)ino);
00135 system(cmd);
00136 }
00137 lastino = ino;
00138 }
00139 #endif
00140
00141 printf("%s %05x:%05x %s %.0f:%.0f(%.0f)\n",
00142 procid_str_static(&pid), (int)dev, (int)ino,
00143 lock_type==READ_LOCK?"R":"W",
00144 (double)start, (double)start+size-1,(double)size);
00145
00146 }
00147
00148
00149 static void show_locks(void)
00150 {
00151 brl_forall(print_brl);
00152
00153 }
00154
00155
00156
00157
00158
00159 static struct cli_state *connect_one(char *share, int snum)
00160 {
00161 struct cli_state *c;
00162 struct nmb_name called, calling;
00163 char *server_n;
00164 fstring server;
00165 struct in_addr ip;
00166 fstring myname;
00167 static int count;
00168 NTSTATUS status;
00169
00170 fstrcpy(server,share+2);
00171 share = strchr_m(server,'\\');
00172 if (!share) return NULL;
00173 *share = 0;
00174 share++;
00175
00176 server_n = server;
00177
00178 zero_ip(&ip);
00179
00180 slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
00181
00182 make_nmb_name(&calling, myname, 0x0);
00183 make_nmb_name(&called , server, 0x20);
00184
00185 again:
00186 zero_ip(&ip);
00187
00188
00189 if (!(c=cli_initialise())) {
00190 DEBUG(0,("Connection to %s failed\n", server_n));
00191 return NULL;
00192 }
00193
00194 status = cli_connect(c, server_n, &ip);
00195 if (!NT_STATUS_IS_OK(status)) {
00196 DEBUG(0,("Connection to %s failed. Error %s\n", server_n, nt_errstr(status) ));
00197 return NULL;
00198 }
00199
00200 c->use_kerberos = use_kerberos;
00201
00202 if (!cli_session_request(c, &calling, &called)) {
00203 DEBUG(0,("session request to %s failed\n", called.name));
00204 cli_shutdown(c);
00205 if (strcmp(called.name, "*SMBSERVER")) {
00206 make_nmb_name(&called , "*SMBSERVER", 0x20);
00207 goto again;
00208 }
00209 return NULL;
00210 }
00211
00212 DEBUG(4,(" session request ok\n"));
00213
00214 if (!cli_negprot(c)) {
00215 DEBUG(0,("protocol negotiation failed\n"));
00216 cli_shutdown(c);
00217 return NULL;
00218 }
00219
00220 if (!got_pass) {
00221 char *pass = getpass("Password: ");
00222 if (pass) {
00223 fstrcpy(password[0], pass);
00224 fstrcpy(password[1], pass);
00225 }
00226 }
00227
00228 if (got_pass == 1) {
00229 fstrcpy(password[1], password[0]);
00230 fstrcpy(username[1], username[0]);
00231 }
00232
00233 if (!NT_STATUS_IS_OK(cli_session_setup(c, username[snum],
00234 password[snum],
00235 strlen(password[snum]),
00236 password[snum],
00237 strlen(password[snum]),
00238 lp_workgroup()))) {
00239 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
00240 return NULL;
00241 }
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251 if (*c->server_domain || *c->server_os || *c->server_type)
00252 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
00253 c->server_domain,c->server_os,c->server_type));
00254
00255 DEBUG(4,(" session setup ok\n"));
00256
00257 if (!cli_send_tconX(c, share, "?????",
00258 password[snum], strlen(password[snum])+1)) {
00259 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
00260 cli_shutdown(c);
00261 return NULL;
00262 }
00263
00264 DEBUG(4,(" tconx ok\n"));
00265
00266 c->use_oplocks = use_oplocks;
00267
00268 return c;
00269 }
00270
00271
00272 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
00273 char *share[NSERVERS])
00274 {
00275 int server, conn, f;
00276
00277 for (server=0;server<NSERVERS;server++)
00278 for (conn=0;conn<NCONNECTIONS;conn++) {
00279 if (cli[server][conn]) {
00280 for (f=0;f<NFILES;f++) {
00281 if (fnum[server][conn][f] != -1) {
00282 cli_close(cli[server][conn], fnum[server][conn][f]);
00283 fnum[server][conn][f] = -1;
00284 }
00285 }
00286 cli_ulogoff(cli[server][conn]);
00287 cli_shutdown(cli[server][conn]);
00288 }
00289 cli[server][conn] = connect_one(share[server], server);
00290 if (!cli[server][conn]) {
00291 DEBUG(0,("Failed to connect to %s\n", share[server]));
00292 exit(1);
00293 }
00294 }
00295 }
00296
00297
00298
00299 static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
00300 int fnum[NSERVERS][NCONNECTIONS][NFILES],
00301 struct record *rec)
00302 {
00303 unsigned conn = rec->conn;
00304 unsigned f = rec->f;
00305 SMB_BIG_UINT start = rec->start;
00306 SMB_BIG_UINT len = rec->len;
00307 enum brl_type op = rec->lock_type;
00308 int server;
00309 BOOL ret[NSERVERS];
00310 NTSTATUS status[NSERVERS];
00311
00312 switch (rec->lock_op) {
00313 case OP_LOCK:
00314
00315 for (server=0;server<NSERVERS;server++) {
00316 ret[server] = cli_lock64(cli[server][conn],
00317 fnum[server][conn][f],
00318 start, len, LOCK_TIMEOUT, op);
00319 status[server] = cli_nt_error(cli[server][conn]);
00320 if (!exact_error_codes &&
00321 NT_STATUS_EQUAL(status[server],
00322 NT_STATUS_FILE_LOCK_CONFLICT)) {
00323 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
00324 }
00325 }
00326 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
00327 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
00328 conn, f,
00329 (double)start, (double)len,
00330 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
00331 nt_errstr(status[0]), nt_errstr(status[1]));
00332 }
00333 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
00334 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
00335 break;
00336
00337 case OP_UNLOCK:
00338
00339 for (server=0;server<NSERVERS;server++) {
00340 ret[server] = cli_unlock64(cli[server][conn],
00341 fnum[server][conn][f],
00342 start, len);
00343 status[server] = cli_nt_error(cli[server][conn]);
00344 }
00345 if (showall ||
00346 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
00347 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
00348 conn, f,
00349 (double)start, (double)len,
00350 nt_errstr(status[0]), nt_errstr(status[1]));
00351 }
00352 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
00353 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
00354 return False;
00355 break;
00356
00357 case OP_REOPEN:
00358
00359 for (server=0;server<NSERVERS;server++) {
00360 cli_close(cli[server][conn], fnum[server][conn][f]);
00361 fnum[server][conn][f] = -1;
00362 }
00363 for (server=0;server<NSERVERS;server++) {
00364 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
00365 O_RDWR|O_CREAT,
00366 DENY_NONE);
00367 if (fnum[server][conn][f] == -1) {
00368 printf("failed to reopen on share%d\n", server);
00369 return False;
00370 }
00371 }
00372 if (showall) {
00373 printf("reopen conn=%u f=%u\n",
00374 conn, f);
00375 show_locks();
00376 }
00377 break;
00378 }
00379
00380 return True;
00381 }
00382
00383 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
00384 int fnum[NSERVERS][NCONNECTIONS][NFILES])
00385 {
00386 int server, conn, f;
00387
00388 for (server=0;server<NSERVERS;server++)
00389 for (conn=0;conn<NCONNECTIONS;conn++)
00390 for (f=0;f<NFILES;f++) {
00391 if (fnum[server][conn][f] != -1) {
00392 cli_close(cli[server][conn], fnum[server][conn][f]);
00393 fnum[server][conn][f] = -1;
00394 }
00395 }
00396 for (server=0;server<NSERVERS;server++) {
00397 cli_unlink(cli[server][0], FILENAME);
00398 }
00399 }
00400
00401 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
00402 int fnum[NSERVERS][NCONNECTIONS][NFILES])
00403 {
00404 int server, conn, f;
00405
00406 for (server=0;server<NSERVERS;server++)
00407 for (conn=0;conn<NCONNECTIONS;conn++)
00408 for (f=0;f<NFILES;f++) {
00409 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
00410 O_RDWR|O_CREAT,
00411 DENY_NONE);
00412 if (fnum[server][conn][f] == -1) {
00413 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
00414 server, conn, f);
00415 exit(1);
00416 }
00417 }
00418 }
00419
00420
00421 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
00422 int fnum[NSERVERS][NCONNECTIONS][NFILES],
00423 int n)
00424 {
00425 int i;
00426 printf("testing %u ...\n", n);
00427 for (i=0; i<n; i++) {
00428 if (i && i % 100 == 0) {
00429 printf("%u\n", i);
00430 }
00431
00432 if (recorded[i].needed &&
00433 !test_one(cli, fnum, &recorded[i])) return i;
00434 }
00435 return n;
00436 }
00437
00438
00439
00440
00441
00442
00443
00444
00445 static void test_locks(char *share[NSERVERS])
00446 {
00447 struct cli_state *cli[NSERVERS][NCONNECTIONS];
00448 int fnum[NSERVERS][NCONNECTIONS][NFILES];
00449 int n, i, n1, skip, r1, r2;
00450
00451 ZERO_STRUCT(fnum);
00452 ZERO_STRUCT(cli);
00453
00454 recorded = SMB_MALLOC_ARRAY(struct record, numops);
00455
00456 for (n=0; n<numops; n++) {
00457 #if PRESETS
00458 if (n < sizeof(preset) / sizeof(preset[0])) {
00459 recorded[n] = preset[n];
00460 } else {
00461 #endif
00462 recorded[n].conn = random() % NCONNECTIONS;
00463 recorded[n].f = random() % NFILES;
00464 recorded[n].start = lock_base + ((unsigned)random() % (lock_range-1));
00465 recorded[n].len = min_length +
00466 random() % (lock_range-(recorded[n].start-lock_base));
00467 recorded[n].start *= RANGE_MULTIPLE;
00468 recorded[n].len *= RANGE_MULTIPLE;
00469 r1 = random() % 100;
00470 r2 = random() % 100;
00471 if (r1 < READ_PCT) {
00472 recorded[n].lock_type = READ_LOCK;
00473 } else {
00474 recorded[n].lock_type = WRITE_LOCK;
00475 }
00476 if (r2 < LOCK_PCT) {
00477 recorded[n].lock_op = OP_LOCK;
00478 } else if (r2 < UNLOCK_PCT) {
00479 recorded[n].lock_op = OP_UNLOCK;
00480 } else {
00481 recorded[n].lock_op = OP_REOPEN;
00482 }
00483 recorded[n].needed = True;
00484 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
00485 recorded[n].len = 1;
00486 }
00487 #if PRESETS
00488 }
00489 #endif
00490 }
00491
00492 reconnect(cli, fnum, share);
00493 open_files(cli, fnum);
00494 n = retest(cli, fnum, numops);
00495
00496 if (n == numops || !analyze) return;
00497 n++;
00498
00499 skip = n/2;
00500
00501 while (1) {
00502 n1 = n;
00503
00504 close_files(cli, fnum);
00505 reconnect(cli, fnum, share);
00506 open_files(cli, fnum);
00507
00508 for (i=0;i<n-skip;i+=skip) {
00509 int m, j;
00510 printf("excluding %d-%d\n", i, i+skip-1);
00511 for (j=i;j<i+skip;j++) {
00512 recorded[j].needed = False;
00513 }
00514
00515 close_files(cli, fnum);
00516 open_files(cli, fnum);
00517
00518 m = retest(cli, fnum, n);
00519 if (m == n) {
00520 for (j=i;j<i+skip;j++) {
00521 recorded[j].needed = True;
00522 }
00523 } else {
00524 if (i+(skip-1) < m) {
00525 memmove(&recorded[i], &recorded[i+skip],
00526 (m-(i+skip-1))*sizeof(recorded[0]));
00527 }
00528 n = m-(skip-1);
00529 i--;
00530 }
00531 }
00532
00533 if (skip > 1) {
00534 skip = skip/2;
00535 printf("skip=%d\n", skip);
00536 continue;
00537 }
00538
00539 if (n1 == n) break;
00540 }
00541
00542 close_files(cli, fnum);
00543 reconnect(cli, fnum, share);
00544 open_files(cli, fnum);
00545 showall = True;
00546 n1 = retest(cli, fnum, n);
00547 if (n1 != n-1) {
00548 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
00549 }
00550 close_files(cli, fnum);
00551
00552 for (i=0;i<n;i++) {
00553 printf("{%s, %s, conn = %u, file = %u, start = %.0f, len = %.0f, %u},\n",
00554 lock_op_name(recorded[i].lock_op),
00555 lock_op_type(recorded[i].lock_type),
00556 recorded[i].conn,
00557 recorded[i].f,
00558 (double)recorded[i].start,
00559 (double)recorded[i].len,
00560 recorded[i].needed);
00561 }
00562 }
00563
00564
00565
00566 static void usage(void)
00567 {
00568 printf(
00569 "Usage:\n\
00570 locktest //server1/share1 //server2/share2 [options..]\n\
00571 options:\n\
00572 -U user%%pass (may be specified twice)\n\
00573 -k use kerberos\n\
00574 -s seed\n\
00575 -o numops\n\
00576 -u hide unlock fails\n\
00577 -a (show all ops)\n\
00578 -A analyse for minimal ops\n\
00579 -O use oplocks\n\
00580 -E enable exact error code checking\n\
00581 -Z enable the zero/zero lock\n\
00582 -R range set lock range\n\
00583 -B base set lock base\n\
00584 -M min set min lock length\n\
00585 ");
00586 }
00587
00588
00589
00590
00591 int main(int argc,char *argv[])
00592 {
00593 char *share[NSERVERS];
00594 int opt;
00595 char *p;
00596 int seed, server;
00597
00598 setlinebuf(stdout);
00599
00600 load_case_tables();
00601
00602 dbf = x_stderr;
00603
00604 if (argc < 3 || argv[1][0] == '-') {
00605 usage();
00606 exit(1);
00607 }
00608
00609 setup_logging(argv[0],True);
00610
00611 for (server=0;server<NSERVERS;server++) {
00612 share[server] = argv[1+server];
00613 all_string_sub(share[server],"/","\\",0);
00614 }
00615
00616 argc -= NSERVERS;
00617 argv += NSERVERS;
00618
00619 lp_load(dyn_CONFIGFILE,True,False,False,True);
00620 load_interfaces();
00621
00622 if (getenv("USER")) {
00623 fstrcpy(username[0],getenv("USER"));
00624 fstrcpy(username[1],getenv("USER"));
00625 }
00626
00627 seed = time(NULL);
00628
00629 while ((opt = getopt(argc, argv, "U:s:ho:aAW:OkR:B:M:EZ")) != EOF) {
00630 switch (opt) {
00631 case 'k':
00632 #ifdef HAVE_KRB5
00633 use_kerberos = True;
00634 #else
00635 d_printf("No kerberos support compiled in\n");
00636 exit(1);
00637 #endif
00638 break;
00639 case 'U':
00640 got_user = 1;
00641 if (got_pass == 2) {
00642 d_printf("Max of 2 usernames\n");
00643 exit(1);
00644 }
00645 fstrcpy(username[got_pass],optarg);
00646 p = strchr_m(username[got_pass],'%');
00647 if (p) {
00648 *p = 0;
00649 fstrcpy(password[got_pass], p+1);
00650 got_pass++;
00651 }
00652 break;
00653 case 'R':
00654 lock_range = strtol(optarg, NULL, 0);
00655 break;
00656 case 'B':
00657 lock_base = strtol(optarg, NULL, 0);
00658 break;
00659 case 'M':
00660 min_length = strtol(optarg, NULL, 0);
00661 break;
00662 case 's':
00663 seed = atoi(optarg);
00664 break;
00665 case 'u':
00666 hide_unlock_fails = True;
00667 break;
00668 case 'o':
00669 numops = atoi(optarg);
00670 break;
00671 case 'O':
00672 use_oplocks = True;
00673 break;
00674 case 'a':
00675 showall = True;
00676 break;
00677 case 'A':
00678 analyze = True;
00679 break;
00680 case 'Z':
00681 zero_zero = True;
00682 break;
00683 case 'E':
00684 exact_error_codes = True;
00685 break;
00686 case 'h':
00687 usage();
00688 exit(1);
00689 default:
00690 printf("Unknown option %c (%d)\n", (char)opt, opt);
00691 exit(1);
00692 }
00693 }
00694
00695 if(use_kerberos && !got_user) got_pass = True;
00696
00697 argc -= optind;
00698 argv += optind;
00699
00700 DEBUG(0,("seed=%u\n", seed));
00701 srandom(seed);
00702
00703 test_locks(share);
00704
00705 return(0);
00706 }