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