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