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