added one more comment
[Samba.git] / source / utils / locktest.c
blobf34e12885be828004333f6a3acc0d56169aa352a
1 /*
2 Unix SMB/Netbios implementation.
3 Version 2.0
4 randomised byte range lock tester
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 1000
37 #define LOCKBASE 0
40 #define LOCKBASE (0x40000000 - 50)
43 #define READ_PCT 50
44 #define LOCK_PCT 35
45 #define UNLOCK_PCT 55
46 #define RANGE_MULTIPLE 1
47 #define NSERVERS 2
48 #define NCONNECTIONS 2
49 #define NFILES 2
50 #define LOCK_TIMEOUT 0
52 #define NASTY_POSIX_LOCK_HACK 0
55 struct record {
56 char r1, r2;
57 char conn, f;
58 SMB_BIG_UINT start, len;
59 char needed;
62 #define PRESETS 0
64 #if PRESETS
65 static struct record preset[] = {
66 {36, 5, 0, 0, 0, 8, 1},
67 { 2, 6, 0, 1, 0, 1, 1},
68 {53, 92, 0, 0, 0, 0, 1},
69 {99, 11, 0, 0, 7, 1, 1},
71 #endif
73 static struct record *recorded;
75 static void print_brl(SMB_DEV_T dev, SMB_INO_T ino, int pid,
76 enum brl_type lock_type,
77 br_off start, br_off size)
79 #if NASTY_POSIX_LOCK_HACK
81 pstring cmd;
82 static SMB_INO_T lastino;
84 if (lastino != ino) {
85 slprintf(cmd, sizeof(cmd),
86 "egrep POSIX.*%u /proc/locks", (int)ino);
87 system(cmd);
89 lastino = ino;
91 #endif
93 printf("%6d %05x:%05x %s %.0f:%.0f(%.0f)\n",
94 (int)pid, (int)dev, (int)ino,
95 lock_type==READ_LOCK?"R":"W",
96 (double)start, (double)start+size-1,(double)size);
101 static void show_locks(void)
103 brl_forall(print_brl);
104 /* system("cat /proc/locks"); */
108 /*****************************************************
109 return a connection to a server
110 *******************************************************/
111 struct cli_state *connect_one(char *share)
113 struct cli_state *c;
114 struct nmb_name called, calling;
115 char *server_n;
116 fstring server;
117 struct in_addr ip;
118 fstring myname;
119 static int count;
121 fstrcpy(server,share+2);
122 share = strchr(server,'\\');
123 if (!share) return NULL;
124 *share = 0;
125 share++;
127 server_n = server;
129 zero_ip(&ip);
131 slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), count++);
133 make_nmb_name(&calling, myname, 0x0);
134 make_nmb_name(&called , server, 0x20);
136 again:
137 zero_ip(&ip);
139 /* have to open a new connection */
140 if (!(c=cli_initialise(NULL)) || (cli_set_port(c, 139) == 0) ||
141 !cli_connect(c, server_n, &ip)) {
142 DEBUG(0,("Connection to %s failed\n", server_n));
143 return NULL;
146 if (!cli_session_request(c, &calling, &called)) {
147 DEBUG(0,("session request to %s failed\n", called.name));
148 cli_shutdown(c);
149 if (strcmp(called.name, "*SMBSERVER")) {
150 make_nmb_name(&called , "*SMBSERVER", 0x20);
151 goto again;
153 return NULL;
156 DEBUG(4,(" session request ok\n"));
158 if (!cli_negprot(c)) {
159 DEBUG(0,("protocol negotiation failed\n"));
160 cli_shutdown(c);
161 return NULL;
164 if (!got_pass) {
165 char *pass = getpass("Password: ");
166 if (pass) {
167 pstrcpy(password, pass);
171 if (!cli_session_setup(c, username,
172 password, strlen(password),
173 password, strlen(password),
174 lp_workgroup())) {
175 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
176 return NULL;
180 * These next two lines are needed to emulate
181 * old client behaviour for people who have
182 * scripts based on client output.
183 * QUESTION ? Do we want to have a 'client compatibility
184 * mode to turn these on/off ? JRA.
187 if (*c->server_domain || *c->server_os || *c->server_type)
188 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
189 c->server_domain,c->server_os,c->server_type));
191 DEBUG(4,(" session setup ok\n"));
193 if (!cli_send_tconX(c, share, "?????",
194 password, strlen(password)+1)) {
195 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
196 cli_shutdown(c);
197 return NULL;
200 DEBUG(4,(" tconx ok\n"));
202 c->use_oplocks = use_oplocks;
204 return c;
208 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
209 char *share[NSERVERS])
211 int server, conn, f;
213 for (server=0;server<NSERVERS;server++)
214 for (conn=0;conn<NCONNECTIONS;conn++) {
215 if (cli[server][conn]) {
216 for (f=0;f<NFILES;f++) {
217 cli_close(cli[server][conn], fnum[server][conn][f]);
219 cli_ulogoff(cli[server][conn]);
220 cli_shutdown(cli[server][conn]);
221 SAFE_FREE(cli[server][conn]);
222 cli[server][conn] = NULL;
224 cli[server][conn] = connect_one(share[server]);
225 if (!cli[server][conn]) {
226 DEBUG(0,("Failed to connect to %s\n", share[server]));
227 exit(1);
234 static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
235 int fnum[NSERVERS][NCONNECTIONS][NFILES],
236 struct record *rec)
238 unsigned conn = rec->conn;
239 unsigned f = rec->f;
240 SMB_BIG_UINT start = rec->start;
241 SMB_BIG_UINT len = rec->len;
242 unsigned r1 = rec->r1;
243 unsigned r2 = rec->r2;
244 unsigned op;
245 int server;
246 BOOL ret[NSERVERS];
248 if (r1 < READ_PCT) {
249 op = READ_LOCK;
250 } else {
251 op = WRITE_LOCK;
254 if (r2 < LOCK_PCT) {
255 /* set a lock */
256 for (server=0;server<NSERVERS;server++) {
257 ret[server] = cli_lock64(cli[server][conn],
258 fnum[server][conn][f],
259 start, len, LOCK_TIMEOUT, op);
261 if (showall || ret[0] != ret[1]) {
262 printf("lock conn=%u f=%u range=%.0f:%.0f(%.0f) op=%s -> %u:%u\n",
263 conn, f,
264 (double)start, (double)start+len-1, (double)len,
265 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
266 ret[0], ret[1]);
268 if (showall || ret[0] != ret[1]) show_locks();
269 if (ret[0] != ret[1]) return False;
270 } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
271 /* unset a lock */
272 for (server=0;server<NSERVERS;server++) {
273 ret[server] = cli_unlock64(cli[server][conn],
274 fnum[server][conn][f],
275 start, len);
277 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
278 printf("unlock conn=%u f=%u range=%.0f:%.0f(%.0f) -> %u:%u\n",
279 conn, f,
280 (double)start, (double)start+len-1, (double)len,
281 ret[0], ret[1]);
283 if (showall || ret[0] != ret[1]) show_locks();
284 if (!hide_unlock_fails && ret[0] != ret[1]) return False;
285 } else {
286 /* reopen the file */
287 for (server=0;server<NSERVERS;server++) {
288 cli_close(cli[server][conn], fnum[server][conn][f]);
290 for (server=0;server<NSERVERS;server++) {
291 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
292 O_RDWR|O_CREAT,
293 DENY_NONE);
294 if (fnum[server][conn][f] == -1) {
295 printf("failed to reopen on share%d\n", server);
296 return False;
299 if (showall) {
300 printf("reopen conn=%u f=%u\n",
301 conn, f);
302 show_locks();
305 return True;
308 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
309 int fnum[NSERVERS][NCONNECTIONS][NFILES])
311 int server, conn, f;
313 for (server=0;server<NSERVERS;server++)
314 for (conn=0;conn<NCONNECTIONS;conn++)
315 for (f=0;f<NFILES;f++) {
316 if (fnum[server][conn][f] != -1) {
317 cli_close(cli[server][conn], fnum[server][conn][f]);
318 fnum[server][conn][f] = -1;
321 for (server=0;server<NSERVERS;server++) {
322 cli_unlink(cli[server][0], FILENAME);
326 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
327 int fnum[NSERVERS][NCONNECTIONS][NFILES])
329 int server, conn, f;
331 for (server=0;server<NSERVERS;server++)
332 for (conn=0;conn<NCONNECTIONS;conn++)
333 for (f=0;f<NFILES;f++) {
334 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
335 O_RDWR|O_CREAT,
336 DENY_NONE);
337 if (fnum[server][conn][f] == -1) {
338 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
339 server, conn, f);
340 exit(1);
346 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
347 int fnum[NSERVERS][NCONNECTIONS][NFILES],
348 int n)
350 int i;
351 printf("testing %u ...\n", n);
352 for (i=0; i<n; i++) {
353 if (i && i % 100 == 0) {
354 printf("%u\n", i);
357 if (recorded[i].needed &&
358 !test_one(cli, fnum, &recorded[i])) return i;
360 return n;
364 /* each server has two connections open to it. Each connection has two file
365 descriptors open on the file - 8 file descriptors in total
367 we then do random locking ops in tamdem on the 4 fnums from each
368 server and ensure that the results match
370 static void test_locks(char *share[NSERVERS])
372 struct cli_state *cli[NSERVERS][NCONNECTIONS];
373 int fnum[NSERVERS][NCONNECTIONS][NFILES];
374 int n, i, n1;
376 ZERO_STRUCT(fnum);
377 ZERO_STRUCT(cli);
379 recorded = (struct record *)malloc(sizeof(*recorded) * numops);
381 for (n=0; n<numops; n++) {
382 #if PRESETS
383 if (n < sizeof(preset) / sizeof(preset[0])) {
384 recorded[n] = preset[n];
385 } else
386 #endif
388 recorded[n].conn = random() % NCONNECTIONS;
389 recorded[n].f = random() % NFILES;
390 recorded[n].start = LOCKBASE + ((unsigned)random() % (LOCKRANGE-1));
391 recorded[n].len = 1 +
392 random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
393 recorded[n].start *= RANGE_MULTIPLE;
394 recorded[n].len *= RANGE_MULTIPLE;
395 recorded[n].r1 = random() % 100;
396 recorded[n].r2 = random() % 100;
397 recorded[n].needed = True;
401 reconnect(cli, fnum, share);
402 open_files(cli, fnum);
403 n = retest(cli, fnum, numops);
405 if (n == numops || !analyze) return;
406 n++;
408 while (1) {
409 n1 = n;
411 close_files(cli, fnum);
412 reconnect(cli, fnum, share);
413 open_files(cli, fnum);
415 for (i=0;i<n-1;i++) {
416 int m;
417 recorded[i].needed = False;
419 close_files(cli, fnum);
420 open_files(cli, fnum);
422 m = retest(cli, fnum, n);
423 if (m == n) {
424 recorded[i].needed = True;
425 } else {
426 if (i < m) {
427 memmove(&recorded[i], &recorded[i+1],
428 (m-i)*sizeof(recorded[0]));
430 n = m;
431 i--;
435 if (n1 == n) break;
438 close_files(cli, fnum);
439 reconnect(cli, fnum, share);
440 open_files(cli, fnum);
441 showall = True;
442 n1 = retest(cli, fnum, n);
443 if (n1 != n-1) {
444 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
446 close_files(cli, fnum);
448 for (i=0;i<n;i++) {
449 printf("{%u, %u, %u, %u, %.0f, %.0f, %u},\n",
450 recorded[i].r1,
451 recorded[i].r2,
452 recorded[i].conn,
453 recorded[i].f,
454 (double)recorded[i].start,
455 (double)recorded[i].len,
456 recorded[i].needed);
462 static void usage(void)
464 printf(
465 "Usage:\n\
466 locktest //server1/share1 //server2/share2 [options..]\n\
467 options:\n\
468 -U user%%pass\n\
469 -s seed\n\
470 -o numops\n\
471 -u hide unlock fails\n\
472 -a (show all ops)\n\
473 -O use oplocks\n\
477 /****************************************************************************
478 main program
479 ****************************************************************************/
480 int main(int argc,char *argv[])
482 char *share[NSERVERS];
483 extern char *optarg;
484 extern int optind;
485 extern FILE *dbf;
486 int opt;
487 char *p;
488 int seed, server;
489 static pstring servicesf = CONFIGFILE;
491 setlinebuf(stdout);
493 dbf = stderr;
495 if (argc < 3 || argv[1][0] == '-') {
496 usage();
497 exit(1);
500 setup_logging(argv[0],True);
502 for (server=0;server<NSERVERS;server++) {
503 share[server] = argv[1+server];
504 all_string_sub(share[server],"/","\\",0);
507 argc -= NSERVERS;
508 argv += NSERVERS;
510 TimeInit();
511 charset_initialise();
512 codepage_initialise(lp_client_code_page());
514 lp_load(servicesf,True,False,False);
515 load_interfaces();
517 if (getenv("USER")) {
518 pstrcpy(username,getenv("USER"));
521 seed = time(NULL);
523 while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
524 switch (opt) {
525 case 'U':
526 pstrcpy(username,optarg);
527 p = strchr(username,'%');
528 if (p) {
529 *p = 0;
530 pstrcpy(password, p+1);
531 got_pass = 1;
533 break;
534 case 's':
535 seed = atoi(optarg);
536 break;
537 case 'u':
538 hide_unlock_fails = True;
539 break;
540 case 'o':
541 numops = atoi(optarg);
542 break;
543 case 'O':
544 use_oplocks = True;
545 break;
546 case 'a':
547 showall = True;
548 break;
549 case 'A':
550 analyze = True;
551 break;
552 case 'h':
553 usage();
554 exit(1);
555 default:
556 printf("Unknown option %c (%d)\n", (char)opt, opt);
557 exit(1);
561 argc -= optind;
562 argv += optind;
564 DEBUG(0,("seed=%u\n", seed));
565 srandom(seed);
567 locking_init(1);
568 test_locks(share);
570 return(0);