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