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