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
41 #define NCONNECTIONS 2
43 #define LOCK_TIMEOUT 0
45 #define NASTY_POSIX_LOCK_HACK 1
55 static struct record preset[] = {
56 {77, 11, 0, 0, 2432, 480, 1},
57 {13, 11, 0, 0, 2624, 224, 1},
58 {16, 19, 0, 1, 448, 1344, 1},
59 {21, 96, 0, 0, 2144, 640, 1},
60 {53, 5, 1, 1, 2336, 608, 1},
64 {36, 5, 0, 0, 0, 8, 1},
65 { 2, 6, 0, 1, 0, 1, 1},
66 {53, 92, 0, 0, 0, 0, 1},
67 {99, 11, 0, 0, 7, 1, 1},
70 static struct record *recorded;
72 static void print_brl(SMB_DEV_T dev, SMB_INO_T ino, int pid,
73 enum brl_type lock_type,
74 br_off start, br_off size)
76 printf("%6d %05x:%05x %s %9.0f %9.0f\n",
77 (int)pid, (int)dev, (int)ino,
78 lock_type==READ_LOCK?"R":"W",
79 (double)start, (double)size);
81 #if NASTY_POSIX_LOCK_HACK
84 static SMB_INO_T lastino;
87 slprintf(cmd, sizeof(cmd),
88 "egrep POSIX.*%d /proc/locks", (int)ino);
96 /*****************************************************
97 return a connection to a server
98 *******************************************************/
99 struct cli_state *connect_one(char *share)
102 struct nmb_name called, calling;
106 extern struct in_addr ipzero;
110 fstrcpy(server,share+2);
111 share = strchr(server,'\\');
112 if (!share) return NULL;
120 slprintf(myname,sizeof(myname), "lock-%d-%d", getpid(), count++);
122 make_nmb_name(&calling, myname, 0x0);
123 make_nmb_name(&called , server, 0x20);
128 /* have to open a new connection */
129 if (!(c=cli_initialise(NULL)) || (cli_set_port(c, 139) == 0) ||
130 !cli_connect(c, server_n, &ip)) {
131 DEBUG(0,("Connection to %s failed\n", server_n));
135 if (!cli_session_request(c, &calling, &called)) {
136 DEBUG(0,("session request to %s failed\n", called.name));
138 if (strcmp(called.name, "*SMBSERVER")) {
139 make_nmb_name(&called , "*SMBSERVER", 0x20);
145 DEBUG(4,(" session request ok\n"));
147 if (!cli_negprot(c)) {
148 DEBUG(0,("protocol negotiation failed\n"));
154 char *pass = getpass("Password: ");
156 pstrcpy(password, pass);
160 if (!cli_session_setup(c, username,
161 password, strlen(password),
162 password, strlen(password),
164 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
169 * These next two lines are needed to emulate
170 * old client behaviour for people who have
171 * scripts based on client output.
172 * QUESTION ? Do we want to have a 'client compatibility
173 * mode to turn these on/off ? JRA.
176 if (*c->server_domain || *c->server_os || *c->server_type)
177 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
178 c->server_domain,c->server_os,c->server_type));
180 DEBUG(4,(" session setup ok\n"));
182 if (!cli_send_tconX(c, share, "?????",
183 password, strlen(password)+1)) {
184 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
189 DEBUG(4,(" tconx ok\n"));
195 static void reconnect(struct cli_state *cli[2][2], int fnum[2][2][2],
196 char *share1, char *share2)
203 for (server=0;server<2;server++)
204 for (conn=0;conn<NCONNECTIONS;conn++) {
205 if (cli[server][conn]) {
206 for (f=0;f<NFILES;f++) {
207 cli_close(cli[server][conn], fnum[server][conn][f]);
209 cli_ulogoff(cli[server][conn]);
210 cli_shutdown(cli[server][conn]);
211 free(cli[server][conn]);
212 cli[server][conn] = NULL;
214 cli[server][conn] = connect_one(share[server]);
215 if (!cli[server][conn]) {
216 DEBUG(0,("Failed to connect to %s\n", share[server]));
224 static BOOL test_one(struct cli_state *cli[2][2],
228 int conn = rec->conn;
230 int start = rec->start;
245 ret1 = cli_lock(cli[0][conn],
247 start, len, LOCK_TIMEOUT, op);
248 ret2 = cli_lock(cli[1][conn],
250 start, len, LOCK_TIMEOUT, op);
251 if (showall || ret1 != ret2) {
252 printf("lock conn=%d f=%d range=%d:%d op=%s -> %d:%d\n",
253 conn, f, start, len, op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
256 if (showall) brl_forall(print_brl);
257 if (ret1 != ret2) return False;
258 } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
260 ret1 = cli_unlock(cli[0][conn],
263 ret2 = cli_unlock(cli[1][conn],
266 if (showall || ret1 != ret2) {
267 printf("unlock conn=%d f=%d %d:%d -> %d:%d\n",
271 if (showall) brl_forall(print_brl);
272 if (ret1 != ret2) return False;
274 /* reopen the file */
275 cli_close(cli[0][conn], fnum[0][conn][f]);
276 cli_close(cli[1][conn], fnum[1][conn][f]);
277 fnum[0][conn][f] = cli_open(cli[0][conn], FILENAME,
280 fnum[1][conn][f] = cli_open(cli[1][conn], FILENAME,
283 if (fnum[0][conn][f] == -1) {
284 printf("failed to reopen on share1\n");
287 if (fnum[1][conn][f] == -1) {
288 printf("failed to reopen on share2\n");
292 printf("reopen conn=%d f=%d\n",
294 brl_forall(print_brl);
300 static void close_files(struct cli_state *cli[2][2],
305 for (server=0;server<2;server++)
306 for (conn=0;conn<NCONNECTIONS;conn++)
307 for (f=0;f<NFILES;f++) {
308 if (fnum[server][conn][f] != -1) {
309 cli_close(cli[server][conn], fnum[server][conn][f]);
310 fnum[server][conn][f] = -1;
313 cli_unlink(cli[0][0], FILENAME);
314 cli_unlink(cli[1][0], FILENAME);
317 static void open_files(struct cli_state *cli[2][2],
322 for (server=0;server<2;server++)
323 for (conn=0;conn<NCONNECTIONS;conn++)
324 for (f=0;f<NFILES;f++) {
325 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
328 if (fnum[server][conn][f] == -1) {
329 fprintf(stderr,"Failed to open fnum[%d][%d][%d]\n",
337 static int retest(struct cli_state *cli[2][2],
342 printf("testing %d ...\n", n);
343 for (i=0; i<n; i++) {
344 if (i && i % 100 == 0) {
348 if (recorded[i].needed &&
349 !test_one(cli, fnum, &recorded[i])) return i;
355 /* each server has two connections open to it. Each connection has two file
356 descriptors open on the file - 8 file descriptors in total
358 we then do random locking ops in tamdem on the 4 fnums from each
359 server and ensure that the results match
361 static void test_locks(char *share1, char *share2)
363 struct cli_state *cli[2][2];
370 recorded = (struct record *)malloc(sizeof(*recorded) * numops);
372 for (n=0; n<numops; n++) {
373 if (n < sizeof(preset) / sizeof(preset[0])) {
374 recorded[n] = preset[n];
376 recorded[n].conn = random() % NCONNECTIONS;
377 recorded[n].f = random() % NFILES;
378 recorded[n].start = random() % (LOCKRANGE-1);
379 recorded[n].len = 1 + random() % (LOCKRANGE-recorded[n].start);
380 recorded[n].start *= RANGE_MULTIPLE;
381 recorded[n].len *= RANGE_MULTIPLE;
382 recorded[n].r1 = random() % 100;
383 recorded[n].r2 = random() % 100;
384 recorded[n].needed = True;
388 reconnect(cli, fnum, share1, share2);
389 open_files(cli, fnum);
390 n = retest(cli, fnum, numops);
392 if (n == numops || !analyze) return;
398 close_files(cli, fnum);
399 reconnect(cli, fnum, share1, share2);
400 open_files(cli, fnum);
402 for (i=0;i<n-1;i++) {
404 recorded[i].needed = False;
406 close_files(cli, fnum);
407 open_files(cli, fnum);
409 m = retest(cli, fnum, n);
411 recorded[i].needed = True;
414 memmove(&recorded[i], &recorded[i+1],
415 (m-i)*sizeof(recorded[0]));
425 close_files(cli, fnum);
426 reconnect(cli, fnum, share1, share2);
427 open_files(cli, fnum);
429 n1 = retest(cli, fnum, n);
431 printf("ERROR - inconsistent result (%d %d)\n", n1, n);
433 close_files(cli, fnum);
436 printf("{%d, %d, %d, %d, %d, %d, %d},\n",
449 static void usage(void)
453 locktest //server1/share1 //server2/share2 [options..]\n\
462 /****************************************************************************
464 ****************************************************************************/
465 int main(int argc,char *argv[])
467 char *share1, *share2;
474 static pstring servicesf = CONFIGFILE;
480 if (argv[1][0] == '-' || argc < 3) {
488 all_string_sub(share1,"/","\\",0);
489 all_string_sub(share2,"/","\\",0);
491 setup_logging(argv[0],True);
497 charset_initialise();
499 lp_load(servicesf,True,False,False);
502 if (getenv("USER")) {
503 pstrcpy(username,getenv("USER"));
508 while ((opt = getopt(argc, argv, "U:s:ho:aA")) != EOF) {
511 pstrcpy(username,optarg);
512 p = strchr(username,'%');
515 pstrcpy(password, p+1);
523 numops = atoi(optarg);
535 printf("Unknown option %c (%d)\n", (char)opt, opt);
543 DEBUG(0,("seed=%d\n", seed));
547 test_locks(share1, share2);