4e5d630f302ed9b15f70a80ab62a6c59ce2e4d7a
[jpeach/samba.git] / source / torture / locktest.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.0
4    randomised byte range lock tester
5    Copyright (C) Andrew Tridgell 1999
6    
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.
11    
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.
16    
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.
20 */
21
22 #define NO_SYSLOG
23
24 #include "includes.h"
25
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;
34
35 #define FILENAME "\\locktest.dat"
36 #define LOCKRANGE 100
37 #define LOCKBASE 0
38 #define MINLENGTH 0
39
40 #define ZERO_ZERO 0
41
42 /*
43 #define LOCKBASE (0x40000000 - 50)
44 */
45
46 #define READ_PCT 50
47 #define LOCK_PCT 45
48 #define UNLOCK_PCT 70
49 #define RANGE_MULTIPLE 1
50 #define NSERVERS 2
51 #define NCONNECTIONS 2
52 #define NFILES 2
53 #define LOCK_TIMEOUT 0
54
55 #define NASTY_POSIX_LOCK_HACK 0
56
57 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
58
59 struct record {
60         enum lock_op lock_op;
61         enum brl_type lock_type;
62         char conn, f;
63         SMB_BIG_UINT start, len;
64         char needed;
65 };
66
67 #define PRESETS 0
68
69 #if PRESETS
70 static struct record preset[] = {
71 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
72 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
73 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 0, 1},
74 {OP_UNLOCK, 0       , 0, 0, 2, 0, 1},
75 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
76
77 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
78 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
79 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
80 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
81
82 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
83 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 1, 1},
84 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
85 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
86
87 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
88 {OP_LOCK, WRITE_LOCK, 0, 0, 1, 1, 1},
89 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
90 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
91
92 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
93 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
94 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
95 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
96
97 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
98 {OP_LOCK, READ_LOCK, 0, 0, 3, 1, 1},
99 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
100 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
101
102 };
103 #endif
104
105 static struct record *recorded;
106
107 static void print_brl(SMB_DEV_T dev, SMB_INO_T ino, int pid, 
108                       enum brl_type lock_type,
109                       br_off start, br_off size)
110 {
111 #if NASTY_POSIX_LOCK_HACK
112         {
113                 pstring cmd;
114                 static SMB_INO_T lastino;
115
116                 if (lastino != ino) {
117                         slprintf(cmd, sizeof(cmd), 
118                                  "egrep POSIX.*%u /proc/locks", (int)ino);
119                         system(cmd);
120                 }
121                 lastino = ino;
122         }
123 #endif
124
125         printf("%6d   %05x:%05x    %s  %.0f:%.0f(%.0f)\n", 
126                (int)pid, (int)dev, (int)ino, 
127                lock_type==READ_LOCK?"R":"W",
128                (double)start, (double)start+size-1,(double)size);
129
130 }
131
132
133 static void show_locks(void)
134 {
135         brl_forall(print_brl);
136         /* system("cat /proc/locks"); */
137 }
138
139
140 /***************************************************** 
141 return a connection to a server
142 *******************************************************/
143 struct cli_state *connect_one(char *share)
144 {
145         struct cli_state *c;
146         struct nmb_name called, calling;
147         char *server_n;
148         fstring server;
149         struct in_addr ip;
150         fstring myname;
151         static int count;
152
153         fstrcpy(server,share+2);
154         share = strchr_m(server,'\\');
155         if (!share) return NULL;
156         *share = 0;
157         share++;
158
159         server_n = server;
160         
161         zero_ip(&ip);
162
163         slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), count++);
164
165         make_nmb_name(&calling, myname, 0x0);
166         make_nmb_name(&called , server, 0x20);
167
168  again:
169         zero_ip(&ip);
170
171         /* have to open a new connection */
172         if (!(c=cli_initialise(NULL)) || !cli_connect(c, server_n, &ip)) {
173                 DEBUG(0,("Connection to %s failed\n", server_n));
174                 return NULL;
175         }
176
177         if (!cli_session_request(c, &calling, &called)) {
178                 DEBUG(0,("session request to %s failed\n", called.name));
179                 cli_shutdown(c);
180                 if (strcmp(called.name, "*SMBSERVER")) {
181                         make_nmb_name(&called , "*SMBSERVER", 0x20);
182                         goto again;
183                 }
184                 return NULL;
185         }
186
187         DEBUG(4,(" session request ok\n"));
188
189         if (!cli_negprot(c)) {
190                 DEBUG(0,("protocol negotiation failed\n"));
191                 cli_shutdown(c);
192                 return NULL;
193         }
194
195         if (!got_pass) {
196                 char *pass = getpass("Password: ");
197                 if (pass) {
198                         pstrcpy(password, pass);
199                 }
200         }
201
202         if (!cli_session_setup(c, username, 
203                                password, strlen(password),
204                                password, strlen(password),
205                                lp_workgroup())) {
206                 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
207                 return NULL;
208         }
209
210         /*
211          * These next two lines are needed to emulate
212          * old client behaviour for people who have
213          * scripts based on client output.
214          * QUESTION ? Do we want to have a 'client compatibility
215          * mode to turn these on/off ? JRA.
216          */
217
218         if (*c->server_domain || *c->server_os || *c->server_type)
219                 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
220                         c->server_domain,c->server_os,c->server_type));
221         
222         DEBUG(4,(" session setup ok\n"));
223
224         if (!cli_send_tconX(c, share, "?????",
225                             password, strlen(password)+1)) {
226                 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
227                 cli_shutdown(c);
228                 return NULL;
229         }
230
231         DEBUG(4,(" tconx ok\n"));
232
233         c->use_oplocks = use_oplocks;
234
235         return c;
236 }
237
238
239 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
240                       char *share[NSERVERS])
241 {
242         int server, conn, f;
243
244         for (server=0;server<NSERVERS;server++)
245         for (conn=0;conn<NCONNECTIONS;conn++) {
246                 if (cli[server][conn]) {
247                         for (f=0;f<NFILES;f++) {
248                                 cli_close(cli[server][conn], fnum[server][conn][f]);
249                         }
250                         cli_ulogoff(cli[server][conn]);
251                         cli_shutdown(cli[server][conn]);
252                 }
253                 cli[server][conn] = connect_one(share[server]);
254                 if (!cli[server][conn]) {
255                         DEBUG(0,("Failed to connect to %s\n", share[server]));
256                         exit(1);
257                 }
258         }
259 }
260
261
262
263 static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
264                      int fnum[NSERVERS][NCONNECTIONS][NFILES],
265                      struct record *rec)
266 {
267         unsigned conn = rec->conn;
268         unsigned f = rec->f;
269         SMB_BIG_UINT start = rec->start;
270         SMB_BIG_UINT len = rec->len;
271         enum brl_type op = rec->lock_type;
272         int server;
273         BOOL ret[NSERVERS];
274
275         switch (rec->lock_op) {
276         case OP_LOCK:
277                 /* set a lock */
278                 for (server=0;server<NSERVERS;server++) {
279                         ret[server] = cli_lock64(cli[server][conn], 
280                                                  fnum[server][conn][f],
281                                                  start, len, LOCK_TIMEOUT, op);
282                 }
283                 if (showall || ret[0] != ret[1]) {
284                         printf("lock   conn=%u f=%u range=%.0f(%.0f) op=%s -> %u:%u\n",
285                                conn, f, 
286                                (double)start, (double)len,
287                                op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
288                                ret[0], ret[1]);
289                 }
290                 if (showall || ret[0] != ret[1]) show_locks();
291                 if (ret[0] != ret[1]) return False;
292                 break;
293                 
294         case OP_UNLOCK:
295                 /* unset a lock */
296                 for (server=0;server<NSERVERS;server++) {
297                         ret[server] = cli_unlock64(cli[server][conn], 
298                                                    fnum[server][conn][f],
299                                                    start, len);
300                 }
301                 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
302                         printf("unlock conn=%u f=%u range=%.0f(%.0f)       -> %u:%u\n",
303                                conn, f, 
304                                (double)start, (double)len,
305                                ret[0], ret[1]);
306                 }
307                 if (showall || ret[0] != ret[1]) show_locks();
308                 if (!hide_unlock_fails && ret[0] != ret[1]) return False;
309                 break;
310
311         case OP_REOPEN:
312                 /* reopen the file */
313                 for (server=0;server<NSERVERS;server++) {
314                         cli_close(cli[server][conn], fnum[server][conn][f]);
315                 }
316                 for (server=0;server<NSERVERS;server++) {
317                         fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
318                                                          O_RDWR|O_CREAT,
319                                                          DENY_NONE);
320                         if (fnum[server][conn][f] == -1) {
321                                 printf("failed to reopen on share%d\n", server);
322                                 return False;
323                         }
324                 }
325                 if (showall) {
326                         printf("reopen conn=%u f=%u\n",
327                                conn, f);
328                         show_locks();
329                 }
330                 break;
331         }
332
333         return True;
334 }
335
336 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
337                         int fnum[NSERVERS][NCONNECTIONS][NFILES])
338 {
339         int server, conn, f; 
340
341         for (server=0;server<NSERVERS;server++)
342         for (conn=0;conn<NCONNECTIONS;conn++)
343         for (f=0;f<NFILES;f++) {
344                 if (fnum[server][conn][f] != -1) {
345                         cli_close(cli[server][conn], fnum[server][conn][f]);
346                         fnum[server][conn][f] = -1;
347                 }
348         }
349         for (server=0;server<NSERVERS;server++) {
350                 cli_unlink(cli[server][0], FILENAME);
351         }
352 }
353
354 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
355                        int fnum[NSERVERS][NCONNECTIONS][NFILES])
356 {
357         int server, conn, f; 
358
359         for (server=0;server<NSERVERS;server++)
360         for (conn=0;conn<NCONNECTIONS;conn++)
361         for (f=0;f<NFILES;f++) {
362                 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
363                                                  O_RDWR|O_CREAT,
364                                                  DENY_NONE);
365                 if (fnum[server][conn][f] == -1) {
366                         fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
367                                 server, conn, f);
368                         exit(1);
369                 }
370         }
371 }
372
373
374 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
375                    int fnum[NSERVERS][NCONNECTIONS][NFILES],
376                    int n)
377 {
378         int i;
379         printf("testing %u ...\n", n);
380         for (i=0; i<n; i++) {
381                 if (i && i % 100 == 0) {
382                         printf("%u\n", i);
383                 }
384
385                 if (recorded[i].needed &&
386                     !test_one(cli, fnum, &recorded[i])) return i;
387         }
388         return n;
389 }
390
391
392 /* each server has two connections open to it. Each connection has two file
393    descriptors open on the file - 8 file descriptors in total 
394
395    we then do random locking ops in tamdem on the 4 fnums from each
396    server and ensure that the results match
397  */
398 static void test_locks(char *share[NSERVERS])
399 {
400         struct cli_state *cli[NSERVERS][NCONNECTIONS];
401         int fnum[NSERVERS][NCONNECTIONS][NFILES];
402         int n, i, n1, skip, r1, r2; 
403
404         ZERO_STRUCT(fnum);
405         ZERO_STRUCT(cli);
406
407         recorded = (struct record *)malloc(sizeof(*recorded) * numops);
408
409         for (n=0; n<numops; n++) {
410 #if PRESETS
411                 if (n < sizeof(preset) / sizeof(preset[0])) {
412                         recorded[n] = preset[n];
413                 } else {
414 #endif
415                         recorded[n].conn = random() % NCONNECTIONS;
416                         recorded[n].f = random() % NFILES;
417                         recorded[n].start = LOCKBASE + ((unsigned)random() % (LOCKRANGE-1));
418                         recorded[n].len =  MINLENGTH +
419                                 random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
420                         recorded[n].start *= RANGE_MULTIPLE;
421                         recorded[n].len *= RANGE_MULTIPLE;
422                         r1 = random() % 100;
423                         r2 = random() % 100;
424                         if (r1 < READ_PCT) {
425                                 recorded[n].lock_type = READ_LOCK;
426                         } else {
427                                 recorded[n].lock_type = WRITE_LOCK;
428                         }
429                         if (r2 < LOCK_PCT) {
430                                 recorded[n].lock_op = OP_LOCK;
431                         } else if (r2 < UNLOCK_PCT) {
432                                 recorded[n].lock_op = OP_UNLOCK;
433                         } else {
434                                 recorded[n].lock_op = OP_REOPEN;
435                         }
436                         recorded[n].needed = True;
437 #if !ZERO_ZERO
438                         if (recorded[n].start == 0 &&
439                             recorded[n].len == 0) {
440                                 recorded[n].len = 1;
441                         }
442 #endif
443 #if PRESETS
444                 }
445 #endif
446         }
447
448         reconnect(cli, fnum, share);
449         open_files(cli, fnum);
450         n = retest(cli, fnum, numops);
451
452         if (n == numops || !analyze) return;
453         n++;
454
455         skip = n/2;
456
457         while (1) {
458                 n1 = n;
459
460                 close_files(cli, fnum);
461                 reconnect(cli, fnum, share);
462                 open_files(cli, fnum);
463
464                 for (i=0;i<n-skip;i+=skip) {
465                         int m, j;
466                         printf("excluding %d-%d\n", i, i+skip-1);
467                         for (j=i;j<i+skip;j++) {
468                                 recorded[j].needed = False;
469                         }
470
471                         close_files(cli, fnum);
472                         open_files(cli, fnum);
473
474                         m = retest(cli, fnum, n);
475                         if (m == n) {
476                                 for (j=i;j<i+skip;j++) {
477                                         recorded[j].needed = True;
478                                 }
479                         } else {
480                                 if (i+(skip-1) < m) {
481                                         memmove(&recorded[i], &recorded[i+skip],
482                                                 (m-(i+skip-1))*sizeof(recorded[0]));
483                                 }
484                                 n = m-(skip-1);
485                                 i--;
486                         }
487                 }
488
489                 if (skip > 1) {
490                         skip = skip/2;
491                         printf("skip=%d\n", skip);
492                         continue;
493                 }
494
495                 if (n1 == n) break;
496         }
497
498         close_files(cli, fnum);
499         reconnect(cli, fnum, share);
500         open_files(cli, fnum);
501         showall = True;
502         n1 = retest(cli, fnum, n);
503         if (n1 != n-1) {
504                 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
505         }
506         close_files(cli, fnum);
507
508         for (i=0;i<n;i++) {
509                 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
510                        recorded[i].lock_op,
511                        recorded[i].lock_type,
512                        recorded[i].conn,
513                        recorded[i].f,
514                        (double)recorded[i].start,
515                        (double)recorded[i].len,
516                        recorded[i].needed);
517         }       
518 }
519
520
521
522 static void usage(void)
523 {
524         printf(
525 "Usage:\n\
526   locktest //server1/share1 //server2/share2 [options..]\n\
527   options:\n\
528         -U user%%pass\n\
529         -s seed\n\
530         -o numops\n\
531         -u          hide unlock fails\n\
532         -a          (show all ops)\n\
533         -A          analyse for minimal ops\n\
534         -O          use oplocks\n\
535 ");
536 }
537
538 /****************************************************************************
539   main program
540 ****************************************************************************/
541  int main(int argc,char *argv[])
542 {
543         char *share[NSERVERS];
544         extern char *optarg;
545         extern int optind;
546         int opt;
547         char *p;
548         int seed, server;
549
550         setlinebuf(stdout);
551
552         dbf = x_stderr;
553
554         if (argc < 3 || argv[1][0] == '-') {
555                 usage();
556                 exit(1);
557         }
558
559         setup_logging(argv[0],True);
560
561         for (server=0;server<NSERVERS;server++) {
562                 share[server] = argv[1+server];
563                 all_string_sub(share[server],"/","\\",0);
564         }
565
566         argc -= NSERVERS;
567         argv += NSERVERS;
568
569         lp_load(dyn_CONFIGFILE,True,False,False);
570         load_interfaces();
571
572         if (getenv("USER")) {
573                 pstrcpy(username,getenv("USER"));
574         }
575
576         seed = time(NULL);
577
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_m(username,'%');
583                         if (p) {
584                                 *p = 0;
585                                 pstrcpy(password, p+1);
586                                 got_pass = 1;
587                         }
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);
613                 }
614         }
615
616         argc -= optind;
617         argv += optind;
618
619         DEBUG(0,("seed=%u\n", seed));
620         srandom(seed);
621
622         test_locks(share);
623
624         return(0);
625 }