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