r11420: Fix issue pointed out by Dina Fine <dina@exanet.com>. We can
[ira/wip.git] / source3 / torture / locktest.c
1 /* 
2    Unix SMB/CIFS implementation.
3    randomised byte range lock tester
4    Copyright (C) Andrew Tridgell 1999
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 static fstring password[2];
24 static fstring username[2];
25 static int got_user;
26 static int got_pass;
27 static BOOL use_kerberos;
28 static int numops = 1000;
29 static BOOL showall;
30 static BOOL analyze;
31 static BOOL hide_unlock_fails;
32 static BOOL use_oplocks;
33 static unsigned lock_range = 100;
34 static unsigned lock_base = 0;
35 static unsigned min_length = 0;
36 static BOOL exact_error_codes;
37 static BOOL zero_zero;
38
39 #define FILENAME "\\locktest.dat"
40
41 #define READ_PCT 50
42 #define LOCK_PCT 45
43 #define UNLOCK_PCT 70
44 #define RANGE_MULTIPLE 1
45 #define NSERVERS 2
46 #define NCONNECTIONS 2
47 #define NFILES 2
48 #define LOCK_TIMEOUT 0
49
50 #define NASTY_POSIX_LOCK_HACK 0
51
52 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
53
54 struct record {
55         enum lock_op lock_op;
56         enum brl_type lock_type;
57         char conn, f;
58         SMB_BIG_UINT start, len;
59         char needed;
60 };
61
62 #define PRESETS 0
63
64 #if PRESETS
65 static struct record preset[] = {
66 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
67 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
68 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 0, 1},
69 {OP_UNLOCK, 0       , 0, 0, 2, 0, 1},
70 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
71
72 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
73 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
74 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 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, WRITE_LOCK, 0, 0, 3, 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, 1, 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, WRITE_LOCK, 0, 0, 2, 0, 1},
88 {OP_LOCK, READ_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, 3, 1, 1},
94 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
95 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
96
97 };
98 #endif
99
100 static struct record *recorded;
101
102 static void print_brl(SMB_DEV_T dev, SMB_INO_T ino, int pid, 
103                       enum brl_type lock_type,
104                       br_off start, br_off size)
105 {
106 #if NASTY_POSIX_LOCK_HACK
107         {
108                 pstring cmd;
109                 static SMB_INO_T lastino;
110
111                 if (lastino != ino) {
112                         slprintf(cmd, sizeof(cmd), 
113                                  "egrep POSIX.*%u /proc/locks", (int)ino);
114                         system(cmd);
115                 }
116                 lastino = ino;
117         }
118 #endif
119
120         printf("%6d   %05x:%05x    %s  %.0f:%.0f(%.0f)\n", 
121                (int)pid, (int)dev, (int)ino, 
122                lock_type==READ_LOCK?"R":"W",
123                (double)start, (double)start+size-1,(double)size);
124
125 }
126
127
128 static void show_locks(void)
129 {
130         brl_forall(print_brl);
131         /* system("cat /proc/locks"); */
132 }
133
134
135 /***************************************************** 
136 return a connection to a server
137 *******************************************************/
138 static struct cli_state *connect_one(char *share, int snum)
139 {
140         struct cli_state *c;
141         struct nmb_name called, calling;
142         char *server_n;
143         fstring server;
144         struct in_addr ip;
145         fstring myname;
146         static int count;
147
148         fstrcpy(server,share+2);
149         share = strchr_m(server,'\\');
150         if (!share) return NULL;
151         *share = 0;
152         share++;
153
154         server_n = server;
155         
156         zero_ip(&ip);
157
158         slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
159
160         make_nmb_name(&calling, myname, 0x0);
161         make_nmb_name(&called , server, 0x20);
162
163  again:
164         zero_ip(&ip);
165
166         /* have to open a new connection */
167         if (!(c=cli_initialise(NULL)) || !cli_connect(c, server_n, &ip)) {
168                 DEBUG(0,("Connection to %s failed\n", server_n));
169                 return NULL;
170         }
171
172         c->use_kerberos = use_kerberos;
173
174         if (!cli_session_request(c, &calling, &called)) {
175                 DEBUG(0,("session request to %s failed\n", called.name));
176                 cli_shutdown(c);
177                 if (strcmp(called.name, "*SMBSERVER")) {
178                         make_nmb_name(&called , "*SMBSERVER", 0x20);
179                         goto again;
180                 }
181                 return NULL;
182         }
183
184         DEBUG(4,(" session request ok\n"));
185
186         if (!cli_negprot(c)) {
187                 DEBUG(0,("protocol negotiation failed\n"));
188                 cli_shutdown(c);
189                 return NULL;
190         }
191
192         if (!got_pass) {
193                 char *pass = getpass("Password: ");
194                 if (pass) {
195                         fstrcpy(password[0], pass);
196                         fstrcpy(password[1], pass);
197                 }
198         }
199
200         if (got_pass == 1) {
201                 fstrcpy(password[1], password[0]);
202                 fstrcpy(username[1], username[0]);
203         }
204
205         if (!cli_session_setup(c, username[snum], 
206                                password[snum], strlen(password[snum]),
207                                password[snum], strlen(password[snum]),
208                                lp_workgroup())) {
209                 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
210                 return NULL;
211         }
212
213         /*
214          * These next two lines are needed to emulate
215          * old client behaviour for people who have
216          * scripts based on client output.
217          * QUESTION ? Do we want to have a 'client compatibility
218          * mode to turn these on/off ? JRA.
219          */
220
221         if (*c->server_domain || *c->server_os || *c->server_type)
222                 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
223                         c->server_domain,c->server_os,c->server_type));
224         
225         DEBUG(4,(" session setup ok\n"));
226
227         if (!cli_send_tconX(c, share, "?????",
228                             password[snum], strlen(password[snum])+1)) {
229                 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
230                 cli_shutdown(c);
231                 return NULL;
232         }
233
234         DEBUG(4,(" tconx ok\n"));
235
236         c->use_oplocks = use_oplocks;
237
238         return c;
239 }
240
241
242 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
243                       char *share[NSERVERS])
244 {
245         int server, conn, f;
246
247         for (server=0;server<NSERVERS;server++)
248         for (conn=0;conn<NCONNECTIONS;conn++) {
249                 if (cli[server][conn]) {
250                         for (f=0;f<NFILES;f++) {
251                                 if (fnum[server][conn][f] != -1) {
252                                         cli_close(cli[server][conn], fnum[server][conn][f]);
253                                         fnum[server][conn][f] = -1;
254                                 }
255                         }
256                         cli_ulogoff(cli[server][conn]);
257                         cli_shutdown(cli[server][conn]);
258                 }
259                 cli[server][conn] = connect_one(share[server], server);
260                 if (!cli[server][conn]) {
261                         DEBUG(0,("Failed to connect to %s\n", share[server]));
262                         exit(1);
263                 }
264         }
265 }
266
267
268
269 static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
270                      int fnum[NSERVERS][NCONNECTIONS][NFILES],
271                      struct record *rec)
272 {
273         unsigned conn = rec->conn;
274         unsigned f = rec->f;
275         SMB_BIG_UINT start = rec->start;
276         SMB_BIG_UINT len = rec->len;
277         enum brl_type op = rec->lock_type;
278         int server;
279         BOOL ret[NSERVERS];
280         NTSTATUS status[NSERVERS];
281
282         switch (rec->lock_op) {
283         case OP_LOCK:
284                 /* set a lock */
285                 for (server=0;server<NSERVERS;server++) {
286                         ret[server] = cli_lock64(cli[server][conn], 
287                                                  fnum[server][conn][f],
288                                                  start, len, LOCK_TIMEOUT, op);
289                         status[server] = cli_nt_error(cli[server][conn]);
290                         if (!exact_error_codes && 
291                             NT_STATUS_EQUAL(status[server], 
292                                             NT_STATUS_FILE_LOCK_CONFLICT)) {
293                                 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
294                         }
295                 }
296                 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
297                         printf("lock   conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
298                                conn, f, 
299                                (double)start, (double)len,
300                                op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
301                                nt_errstr(status[0]), nt_errstr(status[1]));
302                 }
303                 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
304                 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
305                 break;
306                 
307         case OP_UNLOCK:
308                 /* unset a lock */
309                 for (server=0;server<NSERVERS;server++) {
310                         ret[server] = cli_unlock64(cli[server][conn], 
311                                                    fnum[server][conn][f],
312                                                    start, len);
313                         status[server] = cli_nt_error(cli[server][conn]);
314                 }
315                 if (showall || 
316                     (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
317                         printf("unlock conn=%u f=%u range=%.0f(%.0f)       -> %s:%s\n",
318                                conn, f, 
319                                (double)start, (double)len,
320                                nt_errstr(status[0]), nt_errstr(status[1]));
321                 }
322                 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
323                 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1])) 
324                         return False;
325                 break;
326
327         case OP_REOPEN:
328                 /* reopen the file */
329                 for (server=0;server<NSERVERS;server++) {
330                         cli_close(cli[server][conn], fnum[server][conn][f]);
331                         fnum[server][conn][f] = -1;
332                 }
333                 for (server=0;server<NSERVERS;server++) {
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                                 printf("failed to reopen on share%d\n", server);
339                                 return False;
340                         }
341                 }
342                 if (showall) {
343                         printf("reopen conn=%u f=%u\n",
344                                conn, f);
345                         show_locks();
346                 }
347                 break;
348         }
349
350         return True;
351 }
352
353 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
354                         int fnum[NSERVERS][NCONNECTIONS][NFILES])
355 {
356         int server, conn, f; 
357
358         for (server=0;server<NSERVERS;server++)
359         for (conn=0;conn<NCONNECTIONS;conn++)
360         for (f=0;f<NFILES;f++) {
361                 if (fnum[server][conn][f] != -1) {
362                         cli_close(cli[server][conn], fnum[server][conn][f]);
363                         fnum[server][conn][f] = -1;
364                 }
365         }
366         for (server=0;server<NSERVERS;server++) {
367                 cli_unlink(cli[server][0], FILENAME);
368         }
369 }
370
371 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
372                        int fnum[NSERVERS][NCONNECTIONS][NFILES])
373 {
374         int server, conn, f; 
375
376         for (server=0;server<NSERVERS;server++)
377         for (conn=0;conn<NCONNECTIONS;conn++)
378         for (f=0;f<NFILES;f++) {
379                 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
380                                                  O_RDWR|O_CREAT,
381                                                  DENY_NONE);
382                 if (fnum[server][conn][f] == -1) {
383                         fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
384                                 server, conn, f);
385                         exit(1);
386                 }
387         }
388 }
389
390
391 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
392                    int fnum[NSERVERS][NCONNECTIONS][NFILES],
393                    int n)
394 {
395         int i;
396         printf("testing %u ...\n", n);
397         for (i=0; i<n; i++) {
398                 if (i && i % 100 == 0) {
399                         printf("%u\n", i);
400                 }
401
402                 if (recorded[i].needed &&
403                     !test_one(cli, fnum, &recorded[i])) return i;
404         }
405         return n;
406 }
407
408
409 /* each server has two connections open to it. Each connection has two file
410    descriptors open on the file - 8 file descriptors in total 
411
412    we then do random locking ops in tamdem on the 4 fnums from each
413    server and ensure that the results match
414  */
415 static void test_locks(char *share[NSERVERS])
416 {
417         struct cli_state *cli[NSERVERS][NCONNECTIONS];
418         int fnum[NSERVERS][NCONNECTIONS][NFILES];
419         int n, i, n1, skip, r1, r2; 
420
421         ZERO_STRUCT(fnum);
422         ZERO_STRUCT(cli);
423
424         recorded = SMB_MALLOC_ARRAY(struct record, numops);
425
426         for (n=0; n<numops; n++) {
427 #if PRESETS
428                 if (n < sizeof(preset) / sizeof(preset[0])) {
429                         recorded[n] = preset[n];
430                 } else {
431 #endif
432                         recorded[n].conn = random() % NCONNECTIONS;
433                         recorded[n].f = random() % NFILES;
434                         recorded[n].start = lock_base + ((unsigned)random() % (lock_range-1));
435                         recorded[n].len =  min_length +
436                                 random() % (lock_range-(recorded[n].start-lock_base));
437                         recorded[n].start *= RANGE_MULTIPLE;
438                         recorded[n].len *= RANGE_MULTIPLE;
439                         r1 = random() % 100;
440                         r2 = random() % 100;
441                         if (r1 < READ_PCT) {
442                                 recorded[n].lock_type = READ_LOCK;
443                         } else {
444                                 recorded[n].lock_type = WRITE_LOCK;
445                         }
446                         if (r2 < LOCK_PCT) {
447                                 recorded[n].lock_op = OP_LOCK;
448                         } else if (r2 < UNLOCK_PCT) {
449                                 recorded[n].lock_op = OP_UNLOCK;
450                         } else {
451                                 recorded[n].lock_op = OP_REOPEN;
452                         }
453                         recorded[n].needed = True;
454                         if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
455                                 recorded[n].len = 1;
456                         }
457 #if PRESETS
458                 }
459 #endif
460         }
461
462         reconnect(cli, fnum, share);
463         open_files(cli, fnum);
464         n = retest(cli, fnum, numops);
465
466         if (n == numops || !analyze) return;
467         n++;
468
469         skip = n/2;
470
471         while (1) {
472                 n1 = n;
473
474                 close_files(cli, fnum);
475                 reconnect(cli, fnum, share);
476                 open_files(cli, fnum);
477
478                 for (i=0;i<n-skip;i+=skip) {
479                         int m, j;
480                         printf("excluding %d-%d\n", i, i+skip-1);
481                         for (j=i;j<i+skip;j++) {
482                                 recorded[j].needed = False;
483                         }
484
485                         close_files(cli, fnum);
486                         open_files(cli, fnum);
487
488                         m = retest(cli, fnum, n);
489                         if (m == n) {
490                                 for (j=i;j<i+skip;j++) {
491                                         recorded[j].needed = True;
492                                 }
493                         } else {
494                                 if (i+(skip-1) < m) {
495                                         memmove(&recorded[i], &recorded[i+skip],
496                                                 (m-(i+skip-1))*sizeof(recorded[0]));
497                                 }
498                                 n = m-(skip-1);
499                                 i--;
500                         }
501                 }
502
503                 if (skip > 1) {
504                         skip = skip/2;
505                         printf("skip=%d\n", skip);
506                         continue;
507                 }
508
509                 if (n1 == n) break;
510         }
511
512         close_files(cli, fnum);
513         reconnect(cli, fnum, share);
514         open_files(cli, fnum);
515         showall = True;
516         n1 = retest(cli, fnum, n);
517         if (n1 != n-1) {
518                 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
519         }
520         close_files(cli, fnum);
521
522         for (i=0;i<n;i++) {
523                 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
524                        recorded[i].lock_op,
525                        recorded[i].lock_type,
526                        recorded[i].conn,
527                        recorded[i].f,
528                        (double)recorded[i].start,
529                        (double)recorded[i].len,
530                        recorded[i].needed);
531         }       
532 }
533
534
535
536 static void usage(void)
537 {
538         printf(
539 "Usage:\n\
540   locktest //server1/share1 //server2/share2 [options..]\n\
541   options:\n\
542         -U user%%pass        (may be specified twice)\n\
543         -k               use kerberos\n\
544         -s seed\n\
545         -o numops\n\
546         -u          hide unlock fails\n\
547         -a          (show all ops)\n\
548         -A          analyse for minimal ops\n\
549         -O          use oplocks\n\
550         -E          enable exact error code checking\n\
551         -Z          enable the zero/zero lock\n\
552         -R range    set lock range\n\
553         -B base     set lock base\n\
554         -M min      set min lock length\n\
555 ");
556 }
557
558 /****************************************************************************
559   main program
560 ****************************************************************************/
561  int main(int argc,char *argv[])
562 {
563         char *share[NSERVERS];
564         extern char *optarg;
565         extern int optind;
566         int opt;
567         char *p;
568         int seed, server;
569
570         setlinebuf(stdout);
571
572         dbf = x_stderr;
573
574         if (argc < 3 || argv[1][0] == '-') {
575                 usage();
576                 exit(1);
577         }
578
579         setup_logging(argv[0],True);
580
581         for (server=0;server<NSERVERS;server++) {
582                 share[server] = argv[1+server];
583                 all_string_sub(share[server],"/","\\",0);
584         }
585
586         argc -= NSERVERS;
587         argv += NSERVERS;
588
589         lp_load(dyn_CONFIGFILE,True,False,False);
590         load_interfaces();
591
592         if (getenv("USER")) {
593                 fstrcpy(username[0],getenv("USER"));
594                 fstrcpy(username[1],getenv("USER"));
595         }
596
597         seed = time(NULL);
598
599         while ((opt = getopt(argc, argv, "U:s:ho:aAW:OkR:B:M:EZ")) != EOF) {
600                 switch (opt) {
601                 case 'k':
602 #ifdef HAVE_KRB5
603                         use_kerberos = True;
604 #else
605                         d_printf("No kerberos support compiled in\n");
606                         exit(1);
607 #endif
608                         break;
609                 case 'U':
610                         got_user = 1;
611                         if (got_pass == 2) {
612                                 d_printf("Max of 2 usernames\n");
613                                 exit(1);
614                         }
615                         fstrcpy(username[got_pass],optarg);
616                         p = strchr_m(username[got_pass],'%');
617                         if (p) {
618                                 *p = 0;
619                                 fstrcpy(password[got_pass], p+1);
620                                 got_pass++;
621                         }
622                         break;
623                 case 'R':
624                         lock_range = strtol(optarg, NULL, 0);
625                         break;
626                 case 'B':
627                         lock_base = strtol(optarg, NULL, 0);
628                         break;
629                 case 'M':
630                         min_length = strtol(optarg, NULL, 0);
631                         break;
632                 case 's':
633                         seed = atoi(optarg);
634                         break;
635                 case 'u':
636                         hide_unlock_fails = True;
637                         break;
638                 case 'o':
639                         numops = atoi(optarg);
640                         break;
641                 case 'O':
642                         use_oplocks = True;
643                         break;
644                 case 'a':
645                         showall = True;
646                         break;
647                 case 'A':
648                         analyze = True;
649                         break;
650                 case 'Z':
651                         zero_zero = True;
652                         break;
653                 case 'E':
654                         exact_error_codes = True;
655                         break;
656                 case 'h':
657                         usage();
658                         exit(1);
659                 default:
660                         printf("Unknown option %c (%d)\n", (char)opt, opt);
661                         exit(1);
662                 }
663         }
664
665         if(use_kerberos && !got_user) got_pass = True;
666
667         argc -= optind;
668         argv += optind;
669
670         DEBUG(0,("seed=%u\n", seed));
671         srandom(seed);
672
673         test_locks(share);
674
675         return(0);
676 }