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