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