fixed error check which caused domain logons to fail
[Samba.git] / source / utils / locktest2.c
blob0391aa29426771fbac1ceee1a58173c0c23de0ce
1 /*
2 Unix SMB/Netbios implementation.
3 Version 2.0
4 byte range lock tester - with local filesystem support
5 Copyright (C) Andrew Tridgell 1999
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #define NO_SYSLOG
24 #include "includes.h"
26 static fstring password;
27 static fstring username;
28 static int got_pass;
29 static int numops = 1000;
30 static BOOL showall;
31 static BOOL analyze;
32 static BOOL hide_unlock_fails;
33 static BOOL use_oplocks;
35 #define FILENAME "\\locktest.dat"
36 #define LOCKRANGE 100
37 #define LOCKBASE 0
40 #define LOCKBASE (0x40000000 - 50)
43 #define READ_PCT 50
44 #define LOCK_PCT 25
45 #define UNLOCK_PCT 65
46 #define RANGE_MULTIPLE 1
48 #define NSERVERS 2
49 #define NCONNECTIONS 2
50 #define NUMFSTYPES 2
51 #define NFILES 2
52 #define LOCK_TIMEOUT 0
54 #define FSTYPE_SMB 0
55 #define FSTYPE_NFS 1
57 struct record {
58 char r1, r2;
59 char conn, f, fstype;
60 unsigned start, len;
61 char needed;
64 static struct record *recorded;
66 static int try_open(struct cli_state *c, char *nfs, int fstype, char *fname, int flags)
68 pstring path;
70 switch (fstype) {
71 case FSTYPE_SMB:
72 return cli_open(c, fname, flags, DENY_NONE);
74 case FSTYPE_NFS:
75 slprintf(path, sizeof(path), "%s%s", nfs, fname);
76 pstring_sub(path,"\\", "/");
77 return open(path, flags, 0666);
80 return -1;
83 static BOOL try_close(struct cli_state *c, int fstype, int fd)
85 switch (fstype) {
86 case FSTYPE_SMB:
87 return cli_close(c, fd);
89 case FSTYPE_NFS:
90 return close(fd) == 0;
93 return False;
96 static BOOL try_lock(struct cli_state *c, int fstype,
97 int fd, unsigned start, unsigned len,
98 int op)
100 struct flock lock;
102 switch (fstype) {
103 case FSTYPE_SMB:
104 return cli_lock(c, fd, start, len, LOCK_TIMEOUT, op);
106 case FSTYPE_NFS:
107 lock.l_type = (op==READ_LOCK) ? F_RDLCK:F_WRLCK;
108 lock.l_whence = SEEK_SET;
109 lock.l_start = start;
110 lock.l_len = len;
111 lock.l_pid = getpid();
112 return fcntl(fd,F_SETLK,&lock) == 0;
115 return False;
118 static BOOL try_unlock(struct cli_state *c, int fstype,
119 int fd, unsigned start, unsigned len)
121 struct flock lock;
123 switch (fstype) {
124 case FSTYPE_SMB:
125 return cli_unlock(c, fd, start, len);
127 case FSTYPE_NFS:
128 lock.l_type = F_UNLCK;
129 lock.l_whence = SEEK_SET;
130 lock.l_start = start;
131 lock.l_len = len;
132 lock.l_pid = getpid();
133 return fcntl(fd,F_SETLK,&lock) == 0;
136 return False;
139 static void print_brl(SMB_DEV_T dev, SMB_INO_T ino, int pid,
140 enum brl_type lock_type,
141 br_off start, br_off size)
143 printf("%6d %05x:%05x %s %.0f:%.0f(%.0f)\n",
144 (int)pid, (int)dev, (int)ino,
145 lock_type==READ_LOCK?"R":"W",
146 (double)start, (double)start+size-1,(double)size);
150 /*****************************************************
151 return a connection to a server
152 *******************************************************/
153 struct cli_state *connect_one(char *share)
155 struct cli_state *c;
156 struct nmb_name called, calling;
157 char *server_n;
158 fstring server;
159 struct in_addr ip;
160 extern struct in_addr ipzero;
161 fstring myname;
162 static int count;
164 fstrcpy(server,share+2);
165 share = strchr(server,'\\');
166 if (!share) return NULL;
167 *share = 0;
168 share++;
170 server_n = server;
172 ip = ipzero;
174 slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), count++);
176 make_nmb_name(&calling, myname, 0x0);
177 make_nmb_name(&called , server, 0x20);
179 again:
180 ip = ipzero;
182 /* have to open a new connection */
183 if (!(c=cli_initialise(NULL)) || (cli_set_port(c, 139) == 0) ||
184 !cli_connect(c, server_n, &ip)) {
185 DEBUG(0,("Connection to %s failed\n", server_n));
186 return NULL;
189 if (!cli_session_request(c, &calling, &called)) {
190 DEBUG(0,("session request to %s failed\n", called.name));
191 cli_shutdown(c);
192 if (strcmp(called.name, "*SMBSERVER")) {
193 make_nmb_name(&called , "*SMBSERVER", 0x20);
194 goto again;
196 return NULL;
199 DEBUG(4,(" session request ok\n"));
201 if (!cli_negprot(c)) {
202 DEBUG(0,("protocol negotiation failed\n"));
203 cli_shutdown(c);
204 return NULL;
207 if (!got_pass) {
208 char *pass = getpass("Password: ");
209 if (pass) {
210 pstrcpy(password, pass);
214 if (!cli_session_setup(c, username,
215 password, strlen(password),
216 password, strlen(password),
217 lp_workgroup())) {
218 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
219 return NULL;
223 * These next two lines are needed to emulate
224 * old client behaviour for people who have
225 * scripts based on client output.
226 * QUESTION ? Do we want to have a 'client compatibility
227 * mode to turn these on/off ? JRA.
230 if (*c->server_domain || *c->server_os || *c->server_type)
231 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
232 c->server_domain,c->server_os,c->server_type));
234 DEBUG(4,(" session setup ok\n"));
236 if (!cli_send_tconX(c, share, "?????",
237 password, strlen(password)+1)) {
238 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
239 cli_shutdown(c);
240 return NULL;
243 DEBUG(4,(" tconx ok\n"));
245 c->use_oplocks = use_oplocks;
247 return c;
251 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS],
252 char *nfs[NSERVERS],
253 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
254 char *share1, char *share2)
256 int server, conn, f, fstype;
257 char *share[2];
258 share[0] = share1;
259 share[1] = share2;
261 fstype = FSTYPE_SMB;
263 for (server=0;server<NSERVERS;server++)
264 for (conn=0;conn<NCONNECTIONS;conn++) {
265 if (cli[server][conn]) {
266 for (f=0;f<NFILES;f++) {
267 cli_close(cli[server][conn], fnum[server][fstype][conn][f]);
269 cli_ulogoff(cli[server][conn]);
270 cli_shutdown(cli[server][conn]);
271 free(cli[server][conn]);
272 cli[server][conn] = NULL;
274 cli[server][conn] = connect_one(share[server]);
275 if (!cli[server][conn]) {
276 DEBUG(0,("Failed to connect to %s\n", share[server]));
277 exit(1);
284 static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
285 char *nfs[NSERVERS],
286 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
287 struct record *rec)
289 unsigned conn = rec->conn;
290 unsigned f = rec->f;
291 unsigned fstype = rec->fstype;
292 unsigned start = rec->start;
293 unsigned len = rec->len;
294 unsigned r1 = rec->r1;
295 unsigned r2 = rec->r2;
296 unsigned op;
297 int server;
298 BOOL ret[NSERVERS];
300 if (r1 < READ_PCT) {
301 op = READ_LOCK;
302 } else {
303 op = WRITE_LOCK;
306 if (r2 < LOCK_PCT) {
307 /* set a lock */
308 for (server=0;server<NSERVERS;server++) {
309 ret[server] = try_lock(cli[server][conn], fstype,
310 fnum[server][fstype][conn][f],
311 start, len, op);
313 if (showall || ret[0] != ret[1]) {
314 printf("lock conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
315 conn, fstype, f,
316 start, start+len-1, len,
317 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
318 ret[0], ret[1]);
320 if (showall) brl_forall(print_brl);
321 if (ret[0] != ret[1]) return False;
322 } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
323 /* unset a lock */
324 for (server=0;server<NSERVERS;server++) {
325 ret[server] = try_unlock(cli[server][conn], fstype,
326 fnum[server][fstype][conn][f],
327 start, len);
329 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
330 printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u) -> %u:%u\n",
331 conn, fstype, f,
332 start, start+len-1, len,
333 ret[0], ret[1]);
335 if (showall) brl_forall(print_brl);
336 if (!hide_unlock_fails && ret[0] != ret[1]) return False;
337 } else {
338 /* reopen the file */
339 for (server=0;server<NSERVERS;server++) {
340 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
341 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
342 O_RDWR|O_CREAT);
343 if (fnum[server][fstype][conn][f] == -1) {
344 printf("failed to reopen on share1\n");
345 return False;
348 if (showall) {
349 printf("reopen conn=%u fstype=%u f=%u\n",
350 conn, fstype, f);
351 brl_forall(print_brl);
354 return True;
357 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
358 char *nfs[NSERVERS],
359 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
361 int server, conn, f, fstype;
363 for (server=0;server<NSERVERS;server++)
364 for (fstype=0;fstype<NUMFSTYPES;fstype++)
365 for (conn=0;conn<NCONNECTIONS;conn++)
366 for (f=0;f<NFILES;f++) {
367 if (fnum[server][fstype][conn][f] != -1) {
368 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
369 fnum[server][fstype][conn][f] = -1;
372 for (server=0;server<NSERVERS;server++) {
373 cli_unlink(cli[server][0], FILENAME);
377 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
378 char *nfs[NSERVERS],
379 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
381 int server, fstype, conn, f;
383 for (server=0;server<NSERVERS;server++)
384 for (fstype=0;fstype<NUMFSTYPES;fstype++)
385 for (conn=0;conn<NCONNECTIONS;conn++)
386 for (f=0;f<NFILES;f++) {
387 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
388 O_RDWR|O_CREAT);
389 if (fnum[server][fstype][conn][f] == -1) {
390 fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
391 server, fstype, conn, f);
392 exit(1);
398 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
399 char *nfs[NSERVERS],
400 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
401 int n)
403 int i;
404 printf("testing %u ...\n", n);
405 for (i=0; i<n; i++) {
406 if (i && i % 100 == 0) {
407 printf("%u\n", i);
410 if (recorded[i].needed &&
411 !test_one(cli, nfs, fnum, &recorded[i])) return i;
413 return n;
417 /* each server has two connections open to it. Each connection has two file
418 descriptors open on the file - 8 file descriptors in total
420 we then do random locking ops in tamdem on the 4 fnums from each
421 server and ensure that the results match
423 static void test_locks(char *share1, char *share2, char *nfspath1, char *nfspath2)
425 struct cli_state *cli[NSERVERS][NCONNECTIONS];
426 char *nfs[NSERVERS];
427 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
428 int n, i, n1;
430 nfs[0] = nfspath1;
431 nfs[1] = nfspath2;
433 ZERO_STRUCT(fnum);
434 ZERO_STRUCT(cli);
436 recorded = (struct record *)malloc(sizeof(*recorded) * numops);
438 for (n=0; n<numops; n++) {
439 recorded[n].conn = random() % NCONNECTIONS;
440 recorded[n].fstype = random() % NUMFSTYPES;
441 recorded[n].f = random() % NFILES;
442 recorded[n].start = LOCKBASE + ((unsigned)random() % (LOCKRANGE-1));
443 recorded[n].len = 1 +
444 random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
445 recorded[n].start *= RANGE_MULTIPLE;
446 recorded[n].len *= RANGE_MULTIPLE;
447 recorded[n].r1 = random() % 100;
448 recorded[n].r2 = random() % 100;
449 recorded[n].needed = True;
452 reconnect(cli, nfs, fnum, share1, share2);
453 open_files(cli, nfs, fnum);
454 n = retest(cli, nfs, fnum, numops);
456 if (n == numops || !analyze) return;
457 n++;
459 while (1) {
460 n1 = n;
462 close_files(cli, nfs, fnum);
463 reconnect(cli, nfs, fnum, share1, share2);
464 open_files(cli, nfs, fnum);
466 for (i=0;i<n-1;i++) {
467 int m;
468 recorded[i].needed = False;
470 close_files(cli, nfs, fnum);
471 open_files(cli, nfs, fnum);
473 m = retest(cli, nfs, fnum, n);
474 if (m == n) {
475 recorded[i].needed = True;
476 } else {
477 if (i < m) {
478 memmove(&recorded[i], &recorded[i+1],
479 (m-i)*sizeof(recorded[0]));
481 n = m;
482 i--;
486 if (n1 == n) break;
489 close_files(cli, nfs, fnum);
490 reconnect(cli, nfs, fnum, share1, share2);
491 open_files(cli, nfs, fnum);
492 showall = True;
493 n1 = retest(cli, nfs, fnum, n);
494 if (n1 != n-1) {
495 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
497 close_files(cli, nfs, fnum);
499 for (i=0;i<n;i++) {
500 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
501 recorded[i].r1,
502 recorded[i].r2,
503 recorded[i].conn,
504 recorded[i].fstype,
505 recorded[i].f,
506 recorded[i].start,
507 recorded[i].len,
508 recorded[i].needed);
514 static void usage(void)
516 printf(
517 "Usage:\n\
518 locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
519 options:\n\
520 -U user%%pass\n\
521 -s seed\n\
522 -o numops\n\
523 -u hide unlock fails\n\
524 -a (show all ops)\n\
525 -O use oplocks\n\
529 /****************************************************************************
530 main program
531 ****************************************************************************/
532 int main(int argc,char *argv[])
534 char *share1, *share2, *nfspath1, *nfspath2;
535 extern char *optarg;
536 extern int optind;
537 extern FILE *dbf;
538 int opt;
539 char *p;
540 int seed;
541 static pstring servicesf = CONFIGFILE;
543 setlinebuf(stdout);
545 dbf = stderr;
547 if (argc < 5 || argv[1][0] == '-') {
548 usage();
549 exit(1);
552 share1 = argv[1];
553 share2 = argv[2];
554 nfspath1 = argv[3];
555 nfspath2 = argv[4];
557 all_string_sub(share1,"/","\\",0);
558 all_string_sub(share2,"/","\\",0);
560 setup_logging(argv[0],True);
562 argc -= 4;
563 argv += 4;
565 TimeInit();
566 charset_initialise();
567 codepage_initialise(lp_client_code_page());
569 lp_load(servicesf,True,False,False);
570 load_interfaces();
572 if (getenv("USER")) {
573 pstrcpy(username,getenv("USER"));
576 seed = time(NULL);
578 while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
579 switch (opt) {
580 case 'U':
581 pstrcpy(username,optarg);
582 p = strchr(username,'%');
583 if (p) {
584 *p = 0;
585 pstrcpy(password, p+1);
586 got_pass = 1;
588 break;
589 case 's':
590 seed = atoi(optarg);
591 break;
592 case 'u':
593 hide_unlock_fails = True;
594 break;
595 case 'o':
596 numops = atoi(optarg);
597 break;
598 case 'O':
599 use_oplocks = True;
600 break;
601 case 'a':
602 showall = True;
603 break;
604 case 'A':
605 analyze = True;
606 break;
607 case 'h':
608 usage();
609 exit(1);
610 default:
611 printf("Unknown option %c (%d)\n", (char)opt, opt);
612 exit(1);
616 argc -= optind;
617 argv += optind;
619 DEBUG(0,("seed=%u\n", seed));
620 srandom(seed);
622 locking_init(1);
623 test_locks(share1, share2, nfspath1, nfspath2);
625 return(0);