04928f20ac85084dfef760310ddbbf0d981ccda0
[kai/samba.git] / source3 / 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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "libsmb/libsmb.h"
22 #include "system/filesys.h"
23 #include "locking/proto.h"
24 #include "libsmb/nmblib.h"
25 #include "../libcli/smb/smbXcli_base.h"
26
27 static fstring password[2];
28 static fstring username[2];
29 static int got_user;
30 static int got_pass;
31 static bool use_kerberos;
32 static int numops = 1000;
33 static bool showall;
34 static bool analyze;
35 static bool hide_unlock_fails;
36 static bool use_oplocks;
37 static unsigned lock_range = 100;
38 static unsigned lock_base = 0;
39 static unsigned min_length = 0;
40 static bool exact_error_codes;
41 static bool zero_zero;
42
43 extern char *optarg;
44 extern int optind;
45
46 #define FILENAME "\\locktest.dat"
47
48 #define READ_PCT 50
49 #define LOCK_PCT 45
50 #define UNLOCK_PCT 70
51 #define RANGE_MULTIPLE 1
52 #define NSERVERS 2
53 #define NCONNECTIONS 2
54 #define NFILES 2
55 #define LOCK_TIMEOUT 0
56
57 #define NASTY_POSIX_LOCK_HACK 0
58
59 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
60
61 static const char *lock_op_type(int op)
62 {
63         if (op == WRITE_LOCK) return "write";
64         else if (op == READ_LOCK) return "read";
65         else return "other";
66 }
67
68 static const char *lock_op_name(enum lock_op op)
69 {
70         if (op == OP_LOCK) return "lock";
71         else if (op == OP_UNLOCK) return "unlock";
72         else return "reopen";
73 }
74
75 struct record {
76         enum lock_op lock_op;
77         enum brl_type lock_type;
78         char conn, f;
79         uint64_t start, len;
80         char needed;
81 };
82
83 #define PRESETS 0
84
85 #if PRESETS
86 static struct record preset[] = {
87 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
88 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
89 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 0, 1},
90 {OP_UNLOCK, 0       , 0, 0, 2, 0, 1},
91 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
92
93 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
94 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
95 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
96 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
97
98 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
99 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 1, 1},
100 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
101 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
102
103 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
104 {OP_LOCK, WRITE_LOCK, 0, 0, 1, 1, 1},
105 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
106 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
107
108 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
109 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
110 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
111 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
112
113 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
114 {OP_LOCK, READ_LOCK, 0, 0, 3, 1, 1},
115 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
116 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
117
118 };
119 #endif
120
121 static struct record *recorded;
122
123 static void print_brl(struct file_id id,
124                         struct server_id pid, 
125                         enum brl_type lock_type,
126                         enum brl_flavour lock_flav,
127                         br_off start,
128                         br_off size,
129                         void *private_data)
130 {
131 #if NASTY_POSIX_LOCK_HACK
132         {
133                 static SMB_INO_T lastino;
134
135                 if (lastino != ino) {
136                         char *cmd;
137                         if (asprintf(&cmd,
138                                  "egrep POSIX.*%u /proc/locks", (int)ino) > 0) {
139                                 system(cmd);
140                                 SAFE_FREE(cmd);
141                         }
142                 }
143                 lastino = ino;
144         }
145 #endif
146
147         printf("%s   %s    %s  %.0f:%.0f(%.0f)\n", 
148                procid_str_static(&pid), file_id_string_tos(&id),
149                lock_type==READ_LOCK?"R":"W",
150                (double)start, (double)start+size-1,(double)size);
151
152 }
153
154
155 static void show_locks(void)
156 {
157         brl_forall(print_brl, NULL);
158         /* system("cat /proc/locks"); */
159 }
160
161
162 /***************************************************** 
163 return a connection to a server
164 *******************************************************/
165 static struct cli_state *connect_one(char *share, int snum)
166 {
167         struct cli_state *c;
168         char *server_n;
169         fstring server;
170         fstring myname;
171         static int count;
172         NTSTATUS status;
173         int flags = 0;
174
175         fstrcpy(server,share+2);
176         share = strchr_m(server,'\\');
177         if (!share) return NULL;
178         *share = 0;
179         share++;
180
181         server_n = server;
182
183         slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
184
185         /* have to open a new connection */
186
187         if (use_kerberos) {
188                 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
189         }
190         if (use_oplocks) {
191                 flags |= CLI_FULL_CONNECTION_OPLOCKS;
192         }
193
194         status = cli_connect_nb(server_n, NULL, 0, 0x20, myname,
195                                 SMB_SIGNING_DEFAULT, flags, &c);
196         if (!NT_STATUS_IS_OK(status)) {
197                 DEBUG(0, ("Connection to %s failed. Error %s\n", server_n,
198                           nt_errstr(status)));
199                 return NULL;
200         }
201
202         status = smbXcli_negprot(c->conn, c->timeout, PROTOCOL_CORE,
203                                  PROTOCOL_NT1);
204         if (!NT_STATUS_IS_OK(status)) {
205                 DEBUG(0, ("protocol negotiation failed: %s\n",
206                           nt_errstr(status)));
207                 cli_shutdown(c);
208                 return NULL;
209         }
210
211         if (!got_pass) {
212                 char *pass = getpass("Password: ");
213                 if (pass) {
214                         fstrcpy(password[0], pass);
215                         fstrcpy(password[1], pass);
216                 }
217         }
218
219         if (got_pass == 1) {
220                 strlcpy(password[1], password[0],sizeof(password[1]));
221                 strlcpy(username[1], username[0],sizeof(username[1]));
222         }
223
224         status = cli_session_setup(c, username[snum],
225                                    password[snum], strlen(password[snum]),
226                                    password[snum], strlen(password[snum]),
227                                    lp_workgroup());
228         if (!NT_STATUS_IS_OK(status)) {
229                 DEBUG(0,("session setup failed: %s\n", nt_errstr(status)));
230                 return NULL;
231         }
232
233         /*
234          * These next two lines are needed to emulate
235          * old client behaviour for people who have
236          * scripts based on client output.
237          * QUESTION ? Do we want to have a 'client compatibility
238          * mode to turn these on/off ? JRA.
239          */
240
241         if (*c->server_domain || *c->server_os || *c->server_type)
242                 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
243                         c->server_domain,c->server_os,c->server_type));
244         
245         DEBUG(4,(" session setup ok\n"));
246
247         status = cli_tree_connect(c, share, "?????", password[snum],
248                                   strlen(password[snum])+1);
249         if (!NT_STATUS_IS_OK(status)) {
250                 DEBUG(0,("tree connect failed: %s\n", nt_errstr(status)));
251                 cli_shutdown(c);
252                 return NULL;
253         }
254
255         DEBUG(4,(" tconx ok\n"));
256
257         return c;
258 }
259
260
261 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS], uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES],
262                       char *share[NSERVERS])
263 {
264         int server, conn, f;
265
266         for (server=0;server<NSERVERS;server++)
267         for (conn=0;conn<NCONNECTIONS;conn++) {
268                 if (cli[server][conn]) {
269                         for (f=0;f<NFILES;f++) {
270                                 if (fnum[server][conn][f] != (uint16_t)-1) {
271                                         cli_close(cli[server][conn], fnum[server][conn][f]);
272                                         fnum[server][conn][f] = (uint16_t)-1;
273                                 }
274                         }
275                         cli_ulogoff(cli[server][conn]);
276                         cli_shutdown(cli[server][conn]);
277                 }
278                 cli[server][conn] = connect_one(share[server], server);
279                 if (!cli[server][conn]) {
280                         DEBUG(0,("Failed to connect to %s\n", share[server]));
281                         exit(1);
282                 }
283         }
284 }
285
286
287
288 static bool test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
289                      uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES],
290                      struct record *rec)
291 {
292         unsigned conn = rec->conn;
293         unsigned f = rec->f;
294         uint64_t start = rec->start;
295         uint64_t len = rec->len;
296         enum brl_type op = rec->lock_type;
297         int server;
298         NTSTATUS status[NSERVERS];
299
300         switch (rec->lock_op) {
301         case OP_LOCK:
302                 /* set a lock */
303                 for (server=0;server<NSERVERS;server++) {
304                         status[server] = cli_lock64(cli[server][conn],
305                                                     fnum[server][conn][f],
306                                                     start, len, LOCK_TIMEOUT,
307                                                     op);
308                         if (!exact_error_codes && 
309                             NT_STATUS_EQUAL(status[server], 
310                                             NT_STATUS_FILE_LOCK_CONFLICT)) {
311                                 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
312                         }
313                 }
314                 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
315                         printf("lock   conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
316                                conn, f, 
317                                (double)start, (double)len,
318                                op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
319                                nt_errstr(status[0]), nt_errstr(status[1]));
320                 }
321                 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
322                 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
323                 break;
324                 
325         case OP_UNLOCK:
326                 /* unset a lock */
327                 for (server=0;server<NSERVERS;server++) {
328                         status[server] = cli_unlock64(cli[server][conn],
329                                                       fnum[server][conn][f],
330                                                       start, len);
331                 }
332                 if (showall || 
333                     (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
334                         printf("unlock conn=%u f=%u range=%.0f(%.0f)       -> %s:%s\n",
335                                conn, f, 
336                                (double)start, (double)len,
337                                nt_errstr(status[0]), nt_errstr(status[1]));
338                 }
339                 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) show_locks();
340                 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1])) 
341                         return False;
342                 break;
343
344         case OP_REOPEN:
345                 /* reopen the file */
346                 for (server=0;server<NSERVERS;server++) {
347                         cli_close(cli[server][conn], fnum[server][conn][f]);
348                         fnum[server][conn][f] = (uint16_t)-1;
349                 }
350                 for (server=0;server<NSERVERS;server++) {
351                         fnum[server][conn][f] = (uint16_t)-1;
352                         if (!NT_STATUS_IS_OK(cli_openx(cli[server][conn], FILENAME,
353                                                          O_RDWR|O_CREAT,
354                                                          DENY_NONE, &fnum[server][conn][f]))) {
355                                 printf("failed to reopen on share%d\n", server);
356                                 return False;
357                         }
358                 }
359                 if (showall) {
360                         printf("reopen conn=%u f=%u\n",
361                                conn, f);
362                         show_locks();
363                 }
364                 break;
365         }
366
367         return True;
368 }
369
370 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
371                         uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES])
372 {
373         int server, conn, f; 
374
375         for (server=0;server<NSERVERS;server++)
376         for (conn=0;conn<NCONNECTIONS;conn++)
377         for (f=0;f<NFILES;f++) {
378                 if (fnum[server][conn][f] != (uint16_t)-1) {
379                         cli_close(cli[server][conn], fnum[server][conn][f]);
380                         fnum[server][conn][f] = (uint16_t)-1;
381                 }
382         }
383         for (server=0;server<NSERVERS;server++) {
384                 cli_unlink(cli[server][0], FILENAME, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
385         }
386 }
387
388 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
389                        uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES])
390 {
391         int server, conn, f; 
392
393         for (server=0;server<NSERVERS;server++)
394         for (conn=0;conn<NCONNECTIONS;conn++)
395         for (f=0;f<NFILES;f++) {
396                 fnum[server][conn][f] = (uint16_t)-1;
397                 if (!NT_STATUS_IS_OK(cli_openx(cli[server][conn], FILENAME,
398                                                  O_RDWR|O_CREAT,
399                                                  DENY_NONE,
400                                                  &fnum[server][conn][f]))) {
401                         fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
402                                 server, conn, f);
403                         exit(1);
404                 }
405         }
406 }
407
408
409 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
410                    uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES],
411                    int n)
412 {
413         int i;
414         printf("testing %u ...\n", n);
415         for (i=0; i<n; i++) {
416                 if (i && i % 100 == 0) {
417                         printf("%u\n", i);
418                 }
419
420                 if (recorded[i].needed &&
421                     !test_one(cli, fnum, &recorded[i])) return i;
422         }
423         return n;
424 }
425
426
427 /* each server has two connections open to it. Each connection has two file
428    descriptors open on the file - 8 file descriptors in total 
429
430    we then do random locking ops in tamdem on the 4 fnums from each
431    server and ensure that the results match
432  */
433 static void test_locks(char *share[NSERVERS])
434 {
435         struct cli_state *cli[NSERVERS][NCONNECTIONS];
436         uint16_t fnum[NSERVERS][NCONNECTIONS][NFILES];
437         int n, i, n1, skip, r1, r2; 
438
439         ZERO_STRUCT(fnum);
440         ZERO_STRUCT(cli);
441
442         recorded = SMB_MALLOC_ARRAY(struct record, numops);
443
444         for (n=0; n<numops; n++) {
445 #if PRESETS
446                 if (n < sizeof(preset) / sizeof(preset[0])) {
447                         recorded[n] = preset[n];
448                 } else {
449 #endif
450                         recorded[n].conn = random() % NCONNECTIONS;
451                         recorded[n].f = random() % NFILES;
452                         recorded[n].start = lock_base + ((unsigned)random() % (lock_range-1));
453                         recorded[n].len =  min_length +
454                                 random() % (lock_range-(recorded[n].start-lock_base));
455                         recorded[n].start *= RANGE_MULTIPLE;
456                         recorded[n].len *= RANGE_MULTIPLE;
457                         r1 = random() % 100;
458                         r2 = random() % 100;
459                         if (r1 < READ_PCT) {
460                                 recorded[n].lock_type = READ_LOCK;
461                         } else {
462                                 recorded[n].lock_type = WRITE_LOCK;
463                         }
464                         if (r2 < LOCK_PCT) {
465                                 recorded[n].lock_op = OP_LOCK;
466                         } else if (r2 < UNLOCK_PCT) {
467                                 recorded[n].lock_op = OP_UNLOCK;
468                         } else {
469                                 recorded[n].lock_op = OP_REOPEN;
470                         }
471                         recorded[n].needed = True;
472                         if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
473                                 recorded[n].len = 1;
474                         }
475 #if PRESETS
476                 }
477 #endif
478         }
479
480         reconnect(cli, fnum, share);
481         open_files(cli, fnum);
482         n = retest(cli, fnum, numops);
483
484         if (n == numops || !analyze) return;
485         n++;
486
487         skip = n/2;
488
489         while (1) {
490                 n1 = n;
491
492                 close_files(cli, fnum);
493                 reconnect(cli, fnum, share);
494                 open_files(cli, fnum);
495
496                 for (i=0;i<n-skip;i+=skip) {
497                         int m, j;
498                         printf("excluding %d-%d\n", i, i+skip-1);
499                         for (j=i;j<i+skip;j++) {
500                                 recorded[j].needed = False;
501                         }
502
503                         close_files(cli, fnum);
504                         open_files(cli, fnum);
505
506                         m = retest(cli, fnum, n);
507                         if (m == n) {
508                                 for (j=i;j<i+skip;j++) {
509                                         recorded[j].needed = True;
510                                 }
511                         } else {
512                                 if (i+(skip-1) < m) {
513                                         memmove(&recorded[i], &recorded[i+skip],
514                                                 (m-(i+skip-1))*sizeof(recorded[0]));
515                                 }
516                                 n = m-(skip-1);
517                                 i--;
518                         }
519                 }
520
521                 if (skip > 1) {
522                         skip = skip/2;
523                         printf("skip=%d\n", skip);
524                         continue;
525                 }
526
527                 if (n1 == n) break;
528         }
529
530         close_files(cli, fnum);
531         reconnect(cli, fnum, share);
532         open_files(cli, fnum);
533         showall = True;
534         n1 = retest(cli, fnum, n);
535         if (n1 != n-1) {
536                 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
537         }
538         close_files(cli, fnum);
539
540         for (i=0;i<n;i++) {
541                 printf("{%s, %s, conn = %u, file = %u, start = %.0f, len = %.0f, %u},\n",
542                        lock_op_name(recorded[i].lock_op),
543                        lock_op_type(recorded[i].lock_type),
544                        recorded[i].conn,
545                        recorded[i].f,
546                        (double)recorded[i].start,
547                        (double)recorded[i].len,
548                        recorded[i].needed);
549         }       
550 }
551
552
553
554 static void usage(void)
555 {
556         printf(
557 "Usage:\n\
558   locktest //server1/share1 //server2/share2 [options..]\n\
559   options:\n\
560         -U user%%pass        (may be specified twice)\n\
561         -k               use kerberos\n\
562         -s seed\n\
563         -o numops\n\
564         -u          hide unlock fails\n\
565         -a          (show all ops)\n\
566         -A          analyse for minimal ops\n\
567         -O          use oplocks\n\
568         -E          enable exact error code checking\n\
569         -Z          enable the zero/zero lock\n\
570         -R range    set lock range\n\
571         -B base     set lock base\n\
572         -M min      set min lock length\n\
573 ");
574 }
575
576 /****************************************************************************
577   main program
578 ****************************************************************************/
579  int main(int argc,char *argv[])
580 {
581         char *share[NSERVERS];
582         int opt;
583         char *p;
584         int seed, server;
585
586         setlinebuf(stdout);
587
588         load_case_tables();
589
590         if (argc < 3 || argv[1][0] == '-') {
591                 usage();
592                 exit(1);
593         }
594
595         setup_logging(argv[0], DEBUG_STDOUT);
596
597         for (server=0;server<NSERVERS;server++) {
598                 share[server] = argv[1+server];
599                 all_string_sub(share[server],"/","\\",0);
600         }
601
602         argc -= NSERVERS;
603         argv += NSERVERS;
604
605         lp_load_global(get_dyn_CONFIGFILE());
606         load_interfaces();
607
608         if (getenv("USER")) {
609                 fstrcpy(username[0],getenv("USER"));
610                 fstrcpy(username[1],getenv("USER"));
611         }
612
613         seed = time(NULL);
614
615         while ((opt = getopt(argc, argv, "U:s:ho:aAW:OkR:B:M:EZ")) != EOF) {
616                 switch (opt) {
617                 case 'k':
618 #ifdef HAVE_KRB5
619                         use_kerberos = True;
620 #else
621                         d_printf("No kerberos support compiled in\n");
622                         exit(1);
623 #endif
624                         break;
625                 case 'U':
626                         got_user = 1;
627                         if (got_pass == 2) {
628                                 d_printf("Max of 2 usernames\n");
629                                 exit(1);
630                         }
631                         fstrcpy(username[got_pass],optarg);
632                         p = strchr_m(username[got_pass],'%');
633                         if (p) {
634                                 *p = 0;
635                                 fstrcpy(password[got_pass], p+1);
636                                 got_pass++;
637                         }
638                         break;
639                 case 'R':
640                         lock_range = strtol(optarg, NULL, 0);
641                         break;
642                 case 'B':
643                         lock_base = strtol(optarg, NULL, 0);
644                         break;
645                 case 'M':
646                         min_length = strtol(optarg, NULL, 0);
647                         break;
648                 case 's':
649                         seed = atoi(optarg);
650                         break;
651                 case 'u':
652                         hide_unlock_fails = True;
653                         break;
654                 case 'o':
655                         numops = atoi(optarg);
656                         break;
657                 case 'O':
658                         use_oplocks = True;
659                         break;
660                 case 'a':
661                         showall = True;
662                         break;
663                 case 'A':
664                         analyze = True;
665                         break;
666                 case 'Z':
667                         zero_zero = True;
668                         break;
669                 case 'E':
670                         exact_error_codes = True;
671                         break;
672                 case 'h':
673                         usage();
674                         exit(1);
675                 default:
676                         printf("Unknown option %c (%d)\n", (char)opt, opt);
677                         exit(1);
678                 }
679         }
680
681         if(use_kerberos && !got_user) got_pass = True;
682
683         argc -= optind;
684         argv += optind;
685
686         DEBUG(0,("seed=%u\n", seed));
687         srandom(seed);
688
689         test_locks(share);
690
691         return(0);
692 }