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