first public release of samba4 code
[ira/wip.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 #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 struct record {
55         enum lock_op lock_op;
56         enum brl_type lock_type;
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 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
67 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
68 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 0, 1},
69 {OP_UNLOCK, 0       , 0, 0, 2, 0, 1},
70 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
71
72 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
73 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
74 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 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, WRITE_LOCK, 0, 0, 3, 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, 1, 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, WRITE_LOCK, 0, 0, 2, 0, 1},
88 {OP_LOCK, READ_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, 3, 1, 1},
94 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
95 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
96
97 };
98 #endif
99
100 static struct record *recorded;
101
102 /***************************************************** 
103 return a connection to a server
104 *******************************************************/
105 static struct cli_state *connect_one(char *share, int snum)
106 {
107         struct cli_state *c;
108         fstring server, myname;
109         uint_t flags = 0;
110         NTSTATUS status;
111
112         fstrcpy(server,share+2);
113         share = strchr_m(server,'\\');
114         if (!share) return NULL;
115         *share = 0;
116         share++;
117
118         slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), snum);
119
120         if (use_kerberos)
121                 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
122         
123         status = cli_full_connection(&c, myname,
124                                      server, NULL,  
125                                      share, "?????", 
126                                      username[snum], lp_workgroup(), 
127                                      password[snum], flags, NULL);
128
129         if (!NT_STATUS_IS_OK(status)) {
130                 return NULL;
131         }
132
133         return c;
134 }
135
136
137 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
138                       char *share[NSERVERS])
139 {
140         int server, conn, f;
141
142         for (server=0;server<NSERVERS;server++)
143         for (conn=0;conn<NCONNECTIONS;conn++) {
144                 if (cli[server][conn]) {
145                         for (f=0;f<NFILES;f++) {
146                                 if (fnum[server][conn][f] != -1) {
147                                         cli_close(cli[server][conn], fnum[server][conn][f]);
148                                         fnum[server][conn][f] = -1;
149                                 }
150                         }
151                         cli_shutdown(cli[server][conn]);
152                 }
153                 cli[server][conn] = connect_one(share[server], server);
154                 if (!cli[server][conn]) {
155                         DEBUG(0,("Failed to connect to %s\n", share[server]));
156                         exit(1);
157                 }
158         }
159 }
160
161
162
163 static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
164                      int fnum[NSERVERS][NCONNECTIONS][NFILES],
165                      struct record *rec)
166 {
167         unsigned conn = rec->conn;
168         unsigned f = rec->f;
169         SMB_BIG_UINT start = rec->start;
170         SMB_BIG_UINT len = rec->len;
171         enum brl_type op = rec->lock_type;
172         int server;
173         BOOL ret[NSERVERS];
174         NTSTATUS status[NSERVERS];
175
176         switch (rec->lock_op) {
177         case OP_LOCK:
178                 /* set a lock */
179                 for (server=0;server<NSERVERS;server++) {
180                         ret[server] = cli_lock64(cli[server][conn], 
181                                                  fnum[server][conn][f],
182                                                  start, len, LOCK_TIMEOUT, op);
183                         status[server] = cli_nt_error(cli[server][conn]);
184                         if (!exact_error_codes && 
185                             NT_STATUS_EQUAL(status[server], 
186                                             NT_STATUS_FILE_LOCK_CONFLICT)) {
187                                 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
188                         }
189                 }
190                 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
191                         printf("lock   conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
192                                conn, f, 
193                                (double)start, (double)len,
194                                op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
195                                nt_errstr(status[0]), nt_errstr(status[1]));
196                 }
197                 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
198                 break;
199                 
200         case OP_UNLOCK:
201                 /* unset a lock */
202                 for (server=0;server<NSERVERS;server++) {
203                         ret[server] = cli_unlock64(cli[server][conn], 
204                                                    fnum[server][conn][f],
205                                                    start, len);
206                         status[server] = cli_nt_error(cli[server][conn]);
207                 }
208                 if (showall || 
209                     (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
210                         printf("unlock conn=%u f=%u range=%.0f(%.0f)       -> %s:%s\n",
211                                conn, f, 
212                                (double)start, (double)len,
213                                nt_errstr(status[0]), nt_errstr(status[1]));
214                 }
215                 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1])) 
216                         return False;
217                 break;
218
219         case OP_REOPEN:
220                 /* reopen the file */
221                 for (server=0;server<NSERVERS;server++) {
222                         cli_close(cli[server][conn], fnum[server][conn][f]);
223                         fnum[server][conn][f] = -1;
224                 }
225                 for (server=0;server<NSERVERS;server++) {
226                         fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
227                                                          O_RDWR|O_CREAT,
228                                                          DENY_NONE);
229                         if (fnum[server][conn][f] == -1) {
230                                 printf("failed to reopen on share%d\n", server);
231                                 return False;
232                         }
233                 }
234                 if (showall) {
235                         printf("reopen conn=%u f=%u\n",
236                                conn, f);
237                 }
238                 break;
239         }
240
241         return True;
242 }
243
244 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
245                         int fnum[NSERVERS][NCONNECTIONS][NFILES])
246 {
247         int server, conn, f; 
248
249         for (server=0;server<NSERVERS;server++)
250         for (conn=0;conn<NCONNECTIONS;conn++)
251         for (f=0;f<NFILES;f++) {
252                 if (fnum[server][conn][f] != -1) {
253                         cli_close(cli[server][conn], fnum[server][conn][f]);
254                         fnum[server][conn][f] = -1;
255                 }
256         }
257         for (server=0;server<NSERVERS;server++) {
258                 cli_unlink(cli[server][0], FILENAME);
259         }
260 }
261
262 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
263                        int fnum[NSERVERS][NCONNECTIONS][NFILES])
264 {
265         int server, conn, f; 
266
267         for (server=0;server<NSERVERS;server++)
268         for (conn=0;conn<NCONNECTIONS;conn++)
269         for (f=0;f<NFILES;f++) {
270                 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
271                                                  O_RDWR|O_CREAT,
272                                                  DENY_NONE);
273                 if (fnum[server][conn][f] == -1) {
274                         fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
275                                 server, conn, f);
276                         exit(1);
277                 }
278         }
279 }
280
281
282 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
283                    int fnum[NSERVERS][NCONNECTIONS][NFILES],
284                    int n)
285 {
286         int i;
287         printf("testing %u ...\n", n);
288         for (i=0; i<n; i++) {
289                 if (i && i % 100 == 0) {
290                         printf("%u\n", i);
291                 }
292
293                 if (recorded[i].needed &&
294                     !test_one(cli, fnum, &recorded[i])) return i;
295         }
296         return n;
297 }
298
299
300 /* each server has two connections open to it. Each connection has two file
301    descriptors open on the file - 8 file descriptors in total 
302
303    we then do random locking ops in tamdem on the 4 fnums from each
304    server and ensure that the results match
305  */
306 static void test_locks(char *share[NSERVERS])
307 {
308         struct cli_state *cli[NSERVERS][NCONNECTIONS];
309         int fnum[NSERVERS][NCONNECTIONS][NFILES];
310         int n, i, n1, skip, r1, r2; 
311
312         ZERO_STRUCT(fnum);
313         ZERO_STRUCT(cli);
314
315         recorded = (struct record *)malloc(sizeof(*recorded) * numops);
316
317         for (n=0; n<numops; n++) {
318 #if PRESETS
319                 if (n < sizeof(preset) / sizeof(preset[0])) {
320                         recorded[n] = preset[n];
321                 } else {
322 #endif
323                         recorded[n].conn = random() % NCONNECTIONS;
324                         recorded[n].f = random() % NFILES;
325                         recorded[n].start = lock_base + ((unsigned)random() % (lock_range-1));
326                         recorded[n].len =  min_length +
327                                 random() % (lock_range-(recorded[n].start-lock_base));
328                         recorded[n].start *= RANGE_MULTIPLE;
329                         recorded[n].len *= RANGE_MULTIPLE;
330                         r1 = random() % 100;
331                         r2 = random() % 100;
332                         if (r1 < READ_PCT) {
333                                 recorded[n].lock_type = READ_LOCK;
334                         } else {
335                                 recorded[n].lock_type = WRITE_LOCK;
336                         }
337                         if (r2 < LOCK_PCT) {
338                                 recorded[n].lock_op = OP_LOCK;
339                         } else if (r2 < UNLOCK_PCT) {
340                                 recorded[n].lock_op = OP_UNLOCK;
341                         } else {
342                                 recorded[n].lock_op = OP_REOPEN;
343                         }
344                         recorded[n].needed = True;
345                         if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
346                                 recorded[n].len = 1;
347                         }
348 #if PRESETS
349                 }
350 #endif
351         }
352
353         reconnect(cli, fnum, share);
354         open_files(cli, fnum);
355         n = retest(cli, fnum, numops);
356
357         if (n == numops || !analyze) return;
358         n++;
359
360         skip = n/2;
361
362         while (1) {
363                 n1 = n;
364
365                 close_files(cli, fnum);
366                 reconnect(cli, fnum, share);
367                 open_files(cli, fnum);
368
369                 for (i=0;i<n-skip;i+=skip) {
370                         int m, j;
371                         printf("excluding %d-%d\n", i, i+skip-1);
372                         for (j=i;j<i+skip;j++) {
373                                 recorded[j].needed = False;
374                         }
375
376                         close_files(cli, fnum);
377                         open_files(cli, fnum);
378
379                         m = retest(cli, fnum, n);
380                         if (m == n) {
381                                 for (j=i;j<i+skip;j++) {
382                                         recorded[j].needed = True;
383                                 }
384                         } else {
385                                 if (i+(skip-1) < m) {
386                                         memmove(&recorded[i], &recorded[i+skip],
387                                                 (m-(i+skip-1))*sizeof(recorded[0]));
388                                 }
389                                 n = m-(skip-1);
390                                 i--;
391                         }
392                 }
393
394                 if (skip > 1) {
395                         skip = skip/2;
396                         printf("skip=%d\n", skip);
397                         continue;
398                 }
399
400                 if (n1 == n) break;
401         }
402
403         close_files(cli, fnum);
404         reconnect(cli, fnum, share);
405         open_files(cli, fnum);
406         showall = True;
407         n1 = retest(cli, fnum, n);
408         if (n1 != n-1) {
409                 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
410         }
411         close_files(cli, fnum);
412
413         for (i=0;i<n;i++) {
414                 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
415                        recorded[i].lock_op,
416                        recorded[i].lock_type,
417                        recorded[i].conn,
418                        recorded[i].f,
419                        (double)recorded[i].start,
420                        (double)recorded[i].len,
421                        recorded[i].needed);
422         }       
423 }
424
425
426
427 static void usage(void)
428 {
429         printf(
430 "Usage:\n\
431   locktest //server1/share1 //server2/share2 [options..]\n\
432   options:\n\
433         -U user%%pass        (may be specified twice)\n\
434         -k               use kerberos\n\
435         -s seed\n\
436         -o numops\n\
437         -u          hide unlock fails\n\
438         -a          (show all ops)\n\
439         -A          analyse for minimal ops\n\
440         -O          use oplocks\n\
441         -E          enable exact error code checking\n\
442         -Z          enable the zero/zero lock\n\
443         -R range    set lock range\n\
444         -B base     set lock base\n\
445         -M min      set min lock length\n\
446 ");
447 }
448
449 /****************************************************************************
450   main program
451 ****************************************************************************/
452  int main(int argc,char *argv[])
453 {
454         char *share[NSERVERS];
455         int opt;
456         char *p;
457         int seed, server;
458
459         setlinebuf(stdout);
460
461         setup_logging("locktest", DEBUG_STDOUT);
462
463         if (argc < 3 || argv[1][0] == '-') {
464                 usage();
465                 exit(1);
466         }
467
468         setup_logging(argv[0],True);
469
470         for (server=0;server<NSERVERS;server++) {
471                 share[server] = argv[1+server];
472                 all_string_sub(share[server],"/","\\",0);
473         }
474
475         argc -= NSERVERS;
476         argv += NSERVERS;
477
478         lp_load(dyn_CONFIGFILE,True,False,False);
479         load_interfaces();
480
481         if (getenv("USER")) {
482                 fstrcpy(username[0],getenv("USER"));
483                 fstrcpy(username[1],getenv("USER"));
484         }
485
486         seed = time(NULL);
487
488         while ((opt = getopt(argc, argv, "U:s:ho:aAW:OkR:B:M:EZ")) != EOF) {
489                 switch (opt) {
490                 case 'k':
491 #ifdef HAVE_KRB5
492                         use_kerberos = True;
493 #else
494                         d_printf("No kerberos support compiled in\n");
495                         exit(1);
496 #endif
497                         break;
498                 case 'U':
499                         got_user = 1;
500                         if (got_pass == 2) {
501                                 d_printf("Max of 2 usernames\n");
502                                 exit(1);
503                         }
504                         fstrcpy(username[got_pass],optarg);
505                         p = strchr_m(username[got_pass],'%');
506                         if (p) {
507                                 *p = 0;
508                                 fstrcpy(password[got_pass], p+1);
509                                 got_pass++;
510                         }
511                         break;
512                 case 'R':
513                         lock_range = strtol(optarg, NULL, 0);
514                         break;
515                 case 'B':
516                         lock_base = strtol(optarg, NULL, 0);
517                         break;
518                 case 'M':
519                         min_length = strtol(optarg, NULL, 0);
520                         break;
521                 case 's':
522                         seed = atoi(optarg);
523                         break;
524                 case 'u':
525                         hide_unlock_fails = True;
526                         break;
527                 case 'o':
528                         numops = atoi(optarg);
529                         break;
530                 case 'O':
531                         use_oplocks = True;
532                         break;
533                 case 'a':
534                         showall = True;
535                         break;
536                 case 'A':
537                         analyze = True;
538                         break;
539                 case 'Z':
540                         zero_zero = True;
541                         break;
542                 case 'E':
543                         exact_error_codes = True;
544                         break;
545                 case 'h':
546                         usage();
547                         exit(1);
548                 default:
549                         printf("Unknown option %c (%d)\n", (char)opt, opt);
550                         exit(1);
551                 }
552         }
553
554         if(use_kerberos && !got_user) got_pass = True;
555
556         argc -= optind;
557         argv += optind;
558
559         DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n", 
560                  seed, lock_base, lock_range, min_length));
561         srandom(seed);
562
563         test_locks(share);
564
565         return(0);
566 }