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