2 Unix SMB/Netbios implementation.
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.
26 static fstring password;
27 static fstring username;
29 static int numops = 1000;
32 static BOOL hide_unlock_fails;
33 static BOOL use_oplocks;
35 #define FILENAME "\\locktest.dat"
40 #define LOCKBASE (0x40000000 - 50)
46 #define RANGE_MULTIPLE 1
49 #define NCONNECTIONS 2
52 #define LOCK_TIMEOUT 0
64 static struct record *recorded;
66 static int try_open(struct cli_state *c, char *nfs, int fstype, char *fname, int flags)
72 return cli_open(c, fname, flags, DENY_NONE);
75 slprintf(path, sizeof(path), "%s%s", nfs, fname);
76 pstring_sub(path,"\\", "/");
77 return open(path, flags, 0666);
83 static BOOL try_close(struct cli_state *c, int fstype, int fd)
87 return cli_close(c, fd);
90 return close(fd) == 0;
96 static BOOL try_lock(struct cli_state *c, int fstype,
97 int fd, unsigned start, unsigned len,
104 return cli_lock(c, fd, start, len, LOCK_TIMEOUT, op);
107 lock.l_type = (op==READ_LOCK) ? F_RDLCK:F_WRLCK;
108 lock.l_whence = SEEK_SET;
109 lock.l_start = start;
111 lock.l_pid = getpid();
112 return fcntl(fd,F_SETLK,&lock) == 0;
118 static BOOL try_unlock(struct cli_state *c, int fstype,
119 int fd, unsigned start, unsigned len)
125 return cli_unlock(c, fd, start, len);
128 lock.l_type = F_UNLCK;
129 lock.l_whence = SEEK_SET;
130 lock.l_start = start;
132 lock.l_pid = getpid();
133 return fcntl(fd,F_SETLK,&lock) == 0;
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)
156 struct nmb_name called, calling;
160 extern struct in_addr ipzero;
164 fstrcpy(server,share+2);
165 share = strchr_m(server,'\\');
166 if (!share) return NULL;
174 slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), count++);
176 make_nmb_name(&calling, myname, 0x0);
177 make_nmb_name(&called , server, 0x20);
182 /* have to open a new connection */
183 if (!(c=cli_initialise(NULL)) || !cli_connect(c, server_n, &ip)) {
184 DEBUG(0,("Connection to %s failed\n", server_n));
188 if (!cli_session_request(c, &calling, &called)) {
189 DEBUG(0,("session request to %s failed\n", called.name));
191 if (strcmp(called.name, "*SMBSERVER")) {
192 make_nmb_name(&called , "*SMBSERVER", 0x20);
198 DEBUG(4,(" session request ok\n"));
200 if (!cli_negprot(c)) {
201 DEBUG(0,("protocol negotiation failed\n"));
207 char *pass = getpass("Password: ");
209 pstrcpy(password, pass);
213 if (!cli_session_setup(c, username,
214 password, strlen(password),
215 password, strlen(password),
217 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
222 * These next two lines are needed to emulate
223 * old client behaviour for people who have
224 * scripts based on client output.
225 * QUESTION ? Do we want to have a 'client compatibility
226 * mode to turn these on/off ? JRA.
229 if (*c->server_domain || *c->server_os || *c->server_type)
230 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
231 c->server_domain,c->server_os,c->server_type));
233 DEBUG(4,(" session setup ok\n"));
235 if (!cli_send_tconX(c, share, "?????",
236 password, strlen(password)+1)) {
237 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
242 DEBUG(4,(" tconx ok\n"));
244 c->use_oplocks = use_oplocks;
250 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS],
252 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
253 char *share1, char *share2)
255 int server, conn, f, fstype;
262 for (server=0;server<NSERVERS;server++)
263 for (conn=0;conn<NCONNECTIONS;conn++) {
264 if (cli[server][conn]) {
265 for (f=0;f<NFILES;f++) {
266 cli_close(cli[server][conn], fnum[server][fstype][conn][f]);
268 cli_ulogoff(cli[server][conn]);
269 cli_shutdown(cli[server][conn]);
270 free(cli[server][conn]);
271 cli[server][conn] = NULL;
273 cli[server][conn] = connect_one(share[server]);
274 if (!cli[server][conn]) {
275 DEBUG(0,("Failed to connect to %s\n", share[server]));
283 static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
285 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
288 unsigned conn = rec->conn;
290 unsigned fstype = rec->fstype;
291 unsigned start = rec->start;
292 unsigned len = rec->len;
293 unsigned r1 = rec->r1;
294 unsigned r2 = rec->r2;
307 for (server=0;server<NSERVERS;server++) {
308 ret[server] = try_lock(cli[server][conn], fstype,
309 fnum[server][fstype][conn][f],
312 if (showall || ret[0] != ret[1]) {
313 printf("lock conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
315 start, start+len-1, len,
316 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
319 if (showall) brl_forall(print_brl);
320 if (ret[0] != ret[1]) return False;
321 } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
323 for (server=0;server<NSERVERS;server++) {
324 ret[server] = try_unlock(cli[server][conn], fstype,
325 fnum[server][fstype][conn][f],
328 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
329 printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u) -> %u:%u\n",
331 start, start+len-1, len,
334 if (showall) brl_forall(print_brl);
335 if (!hide_unlock_fails && ret[0] != ret[1]) return False;
337 /* reopen the file */
338 for (server=0;server<NSERVERS;server++) {
339 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
340 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
342 if (fnum[server][fstype][conn][f] == -1) {
343 printf("failed to reopen on share1\n");
348 printf("reopen conn=%u fstype=%u f=%u\n",
350 brl_forall(print_brl);
356 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
358 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
360 int server, conn, f, fstype;
362 for (server=0;server<NSERVERS;server++)
363 for (fstype=0;fstype<NUMFSTYPES;fstype++)
364 for (conn=0;conn<NCONNECTIONS;conn++)
365 for (f=0;f<NFILES;f++) {
366 if (fnum[server][fstype][conn][f] != -1) {
367 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
368 fnum[server][fstype][conn][f] = -1;
371 for (server=0;server<NSERVERS;server++) {
372 cli_unlink(cli[server][0], FILENAME);
376 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
378 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
380 int server, fstype, conn, f;
382 for (server=0;server<NSERVERS;server++)
383 for (fstype=0;fstype<NUMFSTYPES;fstype++)
384 for (conn=0;conn<NCONNECTIONS;conn++)
385 for (f=0;f<NFILES;f++) {
386 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
388 if (fnum[server][fstype][conn][f] == -1) {
389 fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
390 server, fstype, conn, f);
397 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
399 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
403 printf("testing %u ...\n", n);
404 for (i=0; i<n; i++) {
405 if (i && i % 100 == 0) {
409 if (recorded[i].needed &&
410 !test_one(cli, nfs, fnum, &recorded[i])) return i;
416 /* each server has two connections open to it. Each connection has two file
417 descriptors open on the file - 8 file descriptors in total
419 we then do random locking ops in tamdem on the 4 fnums from each
420 server and ensure that the results match
422 static void test_locks(char *share1, char *share2, char *nfspath1, char *nfspath2)
424 struct cli_state *cli[NSERVERS][NCONNECTIONS];
426 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
435 recorded = (struct record *)malloc(sizeof(*recorded) * numops);
437 for (n=0; n<numops; n++) {
438 recorded[n].conn = random() % NCONNECTIONS;
439 recorded[n].fstype = random() % NUMFSTYPES;
440 recorded[n].f = random() % NFILES;
441 recorded[n].start = LOCKBASE + ((unsigned)random() % (LOCKRANGE-1));
442 recorded[n].len = 1 +
443 random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
444 recorded[n].start *= RANGE_MULTIPLE;
445 recorded[n].len *= RANGE_MULTIPLE;
446 recorded[n].r1 = random() % 100;
447 recorded[n].r2 = random() % 100;
448 recorded[n].needed = True;
451 reconnect(cli, nfs, fnum, share1, share2);
452 open_files(cli, nfs, fnum);
453 n = retest(cli, nfs, fnum, numops);
455 if (n == numops || !analyze) return;
461 close_files(cli, nfs, fnum);
462 reconnect(cli, nfs, fnum, share1, share2);
463 open_files(cli, nfs, fnum);
465 for (i=0;i<n-1;i++) {
467 recorded[i].needed = False;
469 close_files(cli, nfs, fnum);
470 open_files(cli, nfs, fnum);
472 m = retest(cli, nfs, fnum, n);
474 recorded[i].needed = True;
477 memmove(&recorded[i], &recorded[i+1],
478 (m-i)*sizeof(recorded[0]));
488 close_files(cli, nfs, fnum);
489 reconnect(cli, nfs, fnum, share1, share2);
490 open_files(cli, nfs, fnum);
492 n1 = retest(cli, nfs, fnum, n);
494 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
496 close_files(cli, nfs, fnum);
499 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
513 static void usage(void)
517 locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
522 -u hide unlock fails\n\
528 /****************************************************************************
530 ****************************************************************************/
531 int main(int argc,char *argv[])
533 char *share1, *share2, *nfspath1, *nfspath2;
539 static pstring servicesf = CONFIGFILE;
545 if (argc < 5 || argv[1][0] == '-') {
555 all_string_sub(share1,"/","\\",0);
556 all_string_sub(share2,"/","\\",0);
558 setup_logging(argv[0],True);
565 lp_load(servicesf,True,False,False);
568 if (getenv("USER")) {
569 pstrcpy(username,getenv("USER"));
574 while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
577 pstrcpy(username,optarg);
578 p = strchr_m(username,'%');
581 pstrcpy(password, p+1);
589 hide_unlock_fails = True;
592 numops = atoi(optarg);
607 printf("Unknown option %c (%d)\n", (char)opt, opt);
615 DEBUG(0,("seed=%u\n", seed));
619 test_locks(share1, share2, nfspath1, nfspath2);