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