2 Unix SMB/Netbios implementation.
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;
28 static fstring workgroup;
30 static int numops = 1000;
34 #define FILENAME "locktest.dat"
40 #define RANGE_MULTIPLE 32
49 static struct record preset[] = {
51 {36, 5, 1, 1, 1, 2, 1},
52 { 2, 6, 0, 1, 0, 2, 1},
53 {53, 92, 1, 1, 0, 0, 1},
54 {99, 11, 1, 1, 2, 1, 1},
56 {36, 5, 1, 1, 1888, 960, 1},
57 {47, 23, 0, 1, 0, 2176, 1},
58 {84, 95, 1, 1, 3072, 96, 1},
59 {65, 14, 0, 0, 2752, 352, 1},
62 static struct record *recorded;
64 static void print_brl(SMB_DEV_T dev, SMB_INO_T ino, int pid,
65 enum brl_type lock_type,
66 br_off start, br_off size)
68 printf("%6d %05x:%05x %s %9.0f %9.0f\n",
69 (int)pid, (int)dev, (int)ino,
70 lock_type==READ_LOCK?"R":"W",
71 (double)start, (double)size);
74 /*****************************************************
75 return a connection to a server
76 *******************************************************/
77 struct cli_state *connect_one(char *share)
80 struct nmb_name called, calling;
84 extern struct in_addr ipzero;
88 fstrcpy(server,share+2);
89 share = strchr(server,'\\');
90 if (!share) return NULL;
98 slprintf(myname,sizeof(myname), "lock-%d-%d", getpid(), count++);
100 make_nmb_name(&calling, myname, 0x0);
101 make_nmb_name(&called , server, 0x20);
106 /* have to open a new connection */
107 if (!(c=cli_initialise(NULL)) || (cli_set_port(c, 139) == 0) ||
108 !cli_connect(c, server_n, &ip)) {
109 DEBUG(0,("Connection to %s failed\n", server_n));
113 if (!cli_session_request(c, &calling, &called)) {
114 DEBUG(0,("session request to %s failed\n", called.name));
116 if (strcmp(called.name, "*SMBSERVER")) {
117 make_nmb_name(&called , "*SMBSERVER", 0x20);
123 DEBUG(4,(" session request ok\n"));
125 if (!cli_negprot(c)) {
126 DEBUG(0,("protocol negotiation failed\n"));
132 char *pass = getpass("Password: ");
134 pstrcpy(password, pass);
138 if (!cli_session_setup(c, username,
139 password, strlen(password),
140 password, strlen(password),
142 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
147 * These next two lines are needed to emulate
148 * old client behaviour for people who have
149 * scripts based on client output.
150 * QUESTION ? Do we want to have a 'client compatibility
151 * mode to turn these on/off ? JRA.
154 if (*c->server_domain || *c->server_os || *c->server_type)
155 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
156 c->server_domain,c->server_os,c->server_type));
158 DEBUG(4,(" session setup ok\n"));
160 if (!cli_send_tconX(c, share, "?????",
161 password, strlen(password)+1)) {
162 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
167 DEBUG(4,(" tconx ok\n"));
173 static void reconnect(struct cli_state *cli[2][2], int fnum[2][2][2],
174 char *share1, char *share2)
183 for (server=0;server<2;server++)
184 for (conn=0;conn<2;conn++) {
185 if (cli[server][conn]) {
187 cli_close(cli[server][conn], fnum[server][conn][f]);
189 cli_ulogoff(cli[server][conn]);
190 cli_shutdown(cli[server][conn]);
191 free(cli[server][conn]);
192 cli[server][conn] = NULL;
194 cli[server][conn] = connect_one(share[server]);
195 if (!cli[server][conn]) {
196 DEBUG(0,("Failed to connect to %s\n", share[server]));
204 static BOOL test_one(struct cli_state *cli[2][2],
208 int conn = rec->conn;
210 int start = rec->start;
225 ret1 = cli_lock(cli[0][conn],
228 ret2 = cli_lock(cli[1][conn],
231 if (showall || ret1 != ret2) {
232 printf("lock conn=%d f=%d range=%d:%d op=%s -> %d:%d\n",
233 conn, f, start, len, op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
236 if (showall) brl_forall(print_brl);
237 if (ret1 != ret2) return False;
238 } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
240 ret1 = cli_unlock(cli[0][conn],
243 ret2 = cli_unlock(cli[1][conn],
246 if (showall || ret1 != ret2) {
247 printf("unlock conn=%d f=%d %d:%d -> %d:%d\n",
251 if (showall) brl_forall(print_brl);
252 if (ret1 != ret2) return False;
254 /* reopen the file */
255 cli_close(cli[0][conn], fnum[0][conn][f]);
256 cli_close(cli[1][conn], fnum[1][conn][f]);
257 fnum[0][conn][f] = cli_open(cli[0][conn], FILENAME,
260 fnum[1][conn][f] = cli_open(cli[1][conn], FILENAME,
263 if (fnum[0][conn][f] == -1) {
264 printf("failed to reopen on share1\n");
267 if (fnum[1][conn][f] == -1) {
268 printf("failed to reopen on share2\n");
272 printf("reopen conn=%d f=%d\n",
274 brl_forall(print_brl);
280 static void close_files(struct cli_state *cli[2][2],
285 for (server=0;server<2;server++)
286 for (conn=0;conn<2;conn++)
288 if (fnum[server][conn][f] != -1) {
289 cli_close(cli[server][conn], fnum[server][conn][f]);
290 fnum[server][conn][f] = -1;
293 cli_unlink(cli[0][0], FILENAME);
294 cli_unlink(cli[1][0], FILENAME);
297 static void open_files(struct cli_state *cli[2][2],
302 for (server=0;server<2;server++)
303 for (conn=0;conn<2;conn++)
305 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
308 if (fnum[server][conn][f] == -1) {
309 fprintf(stderr,"Failed to open fnum[%d][%d][%d]\n",
317 static int retest(struct cli_state *cli[2][2],
322 printf("testing %d ...\n", n);
323 for (i=0; i<n; i++) {
324 if (i && i % 100 == 0) {
328 if (recorded[i].needed &&
329 !test_one(cli, fnum, &recorded[i])) return i;
335 /* each server has two connections open to it. Each connection has two file
336 descriptors open on the file - 8 file descriptors in total
338 we then do random locking ops in tamdem on the 4 fnums from each
339 server and ensure that the results match
341 static void test_locks(char *share1, char *share2)
343 struct cli_state *cli[2][2];
350 recorded = (struct record *)malloc(sizeof(*recorded) * numops);
352 for (n=0; n<numops; n++) {
353 if (n < sizeof(preset) / sizeof(preset[0])) {
354 recorded[n] = preset[n];
356 recorded[n].conn = random() % 2;
357 recorded[n].f = random() % 2;
358 recorded[n].start = random() % (LOCKRANGE-1);
359 recorded[n].len = 1 + random() % (LOCKRANGE-recorded[n].start);
360 recorded[n].start *= RANGE_MULTIPLE;
361 recorded[n].len *= RANGE_MULTIPLE;
362 recorded[n].r1 = random() % 100;
363 recorded[n].r2 = random() % 100;
364 recorded[n].needed = True;
368 reconnect(cli, fnum, share1, share2);
369 open_files(cli, fnum);
370 n = retest(cli, fnum, numops);
372 if (n == numops || !analyze) return;
378 close_files(cli, fnum);
379 reconnect(cli, fnum, share1, share2);
380 open_files(cli, fnum);
382 for (i=0;i<n-1;i++) {
384 recorded[i].needed = False;
386 close_files(cli, fnum);
387 open_files(cli, fnum);
389 m = retest(cli, fnum, n);
391 recorded[i].needed = True;
394 memmove(&recorded[i], &recorded[i+1],
395 (m-i)*sizeof(recorded[0]));
405 close_files(cli, fnum);
406 reconnect(cli, fnum, share1, share2);
407 open_files(cli, fnum);
409 n1 = retest(cli, fnum, n);
411 printf("ERROR - inconsistent result (%d %d)\n", n1, n);
413 close_files(cli, fnum);
416 printf("{%d, %d, %d, %d, %d, %d, %d},\n",
429 static void usage(void)
433 locktest //server1/share1 //server2/share2 [options..]\n\
442 /****************************************************************************
444 ****************************************************************************/
445 int main(int argc,char *argv[])
447 char *share1, *share2;
454 static pstring servicesf = CONFIGFILE;
460 if (argv[1][0] == '-' || argc < 3) {
468 all_string_sub(share1,"/","\\",0);
469 all_string_sub(share2,"/","\\",0);
471 setup_logging(argv[0],True);
477 charset_initialise();
479 lp_load(servicesf,True,False,False);
482 if (getenv("USER")) {
483 pstrcpy(username,getenv("USER"));
488 while ((opt = getopt(argc, argv, "U:s:ho:aA")) != EOF) {
491 pstrcpy(username,optarg);
492 p = strchr(username,'%');
495 pstrcpy(password, p+1);
503 numops = atoi(optarg);
515 printf("Unknown option %c (%d)\n", (char)opt, opt);
523 DEBUG(0,("seed=%d\n", seed));
527 test_locks(share1, share2);