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