libndr: Avoid assigning duplicate versions to symbols
[amitay/samba.git] / source3 / torture / locktest2.c
1 /* 
2    Unix SMB/CIFS implementation.
3    byte range lock tester - with local filesystem support
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 "lib/util/string_wrappers.h"
25
26 static fstring password;
27 static fstring username;
28 static int got_pass;
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
35 extern char *optarg;
36 extern int optind;
37
38 #define FILENAME "\\locktest.dat"
39 #define LOCKRANGE 100
40 #define LOCKBASE 0
41
42 /*
43 #define LOCKBASE (0x40000000 - 50)
44 */
45
46 #define READ_PCT 50
47 #define LOCK_PCT 25
48 #define UNLOCK_PCT 65
49 #define RANGE_MULTIPLE 1
50
51 #define NSERVERS 2
52 #define NCONNECTIONS 2
53 #define NUMFSTYPES 2
54 #define NFILES 2
55 #define LOCK_TIMEOUT 0
56
57 #define FSTYPE_SMB 0
58 #define FSTYPE_NFS 1
59
60 struct record {
61         char r1, r2;
62         char conn, f, fstype;
63         unsigned start, len;
64         char needed;
65 };
66
67 static struct record *recorded;
68
69 static int try_open(struct cli_state *c, char *nfs, int fstype, const char *fname, int flags)
70 {
71         char *path;
72
73         switch (fstype) {
74         case FSTYPE_SMB:
75                 {
76                         uint16_t fd;
77                         if (!NT_STATUS_IS_OK(cli_openx(c, fname, flags, DENY_NONE, &fd))) {
78                                 return -1;
79                         }
80                         return fd;
81                 }
82
83         case FSTYPE_NFS:
84                 if (asprintf(&path, "%s%s", nfs, fname) > 0) {
85                         int ret;
86                         string_replace(path,'\\', '/');
87                         ret = open(path, flags, 0666);
88                         SAFE_FREE(path);
89                         return ret;
90                 }
91                 break;
92         }
93
94         return -1;
95 }
96
97 static bool try_close(struct cli_state *c, int fstype, int fd)
98 {
99         switch (fstype) {
100         case FSTYPE_SMB:
101                 return NT_STATUS_IS_OK(cli_close(c, fd));
102
103         case FSTYPE_NFS:
104                 return close(fd) == 0;
105         }
106
107         return False;
108 }
109
110 static bool try_lock(struct cli_state *c, int fstype, 
111                      int fd, unsigned start, unsigned len,
112                      enum brl_type op)
113 {
114         struct flock lock;
115
116         switch (fstype) {
117         case FSTYPE_SMB:
118                 return NT_STATUS_IS_OK(cli_lock32(c, fd, start, len,
119                                        LOCK_TIMEOUT, op));
120
121         case FSTYPE_NFS:
122                 lock.l_type = (op==READ_LOCK) ? F_RDLCK:F_WRLCK;
123                 lock.l_whence = SEEK_SET;
124                 lock.l_start = start;
125                 lock.l_len = len;
126                 lock.l_pid = getpid();
127                 return fcntl(fd,F_SETLK,&lock) == 0;
128         }
129
130         return False;
131 }
132
133 static bool try_unlock(struct cli_state *c, int fstype, 
134                        int fd, unsigned start, unsigned len)
135 {
136         struct flock lock;
137
138         switch (fstype) {
139         case FSTYPE_SMB:
140                 return NT_STATUS_IS_OK(cli_unlock(c, fd, start, len));
141
142         case FSTYPE_NFS:
143                 lock.l_type = F_UNLCK;
144                 lock.l_whence = SEEK_SET;
145                 lock.l_start = start;
146                 lock.l_len = len;
147                 lock.l_pid = getpid();
148                 return fcntl(fd,F_SETLK,&lock) == 0;
149         }
150
151         return False;
152 }       
153
154 static void print_brl(struct file_id id, struct server_id pid, 
155                       enum brl_type lock_type,
156                       enum brl_flavour lock_flav,
157                       br_off start, br_off size,
158                       void *private_data)
159 {
160         struct file_id_buf idbuf;
161
162         printf("%6d   %s    %s  %.0f:%.0f(%.0f)\n", 
163                (int)procid_to_pid(&pid),
164                file_id_str_buf(id, &idbuf),
165                lock_type==READ_LOCK?"R":"W",
166                (double)start, (double)start+size-1,(double)size);
167
168 }
169
170 /***************************************************** 
171 return a connection to a server
172 *******************************************************/
173 static struct cli_state *connect_one(char *share)
174 {
175         struct cli_state *c;
176         char *server_n;
177         fstring server;
178         fstring myname;
179         static int count;
180         NTSTATUS nt_status;
181         bool use_kerberos = false;
182         bool fallback_after_kerberos = false;
183         bool use_ccache = false;
184         bool pw_nt_hash = false;
185         struct cli_credentials *creds = NULL;
186
187         fstrcpy(server,share+2);
188         share = strchr_m(server,'\\');
189         if (!share) return NULL;
190         *share = 0;
191         share++;
192
193         server_n = server;
194         
195         if (!got_pass) {
196                 char pwd[256] = {0};
197                 int rc;
198
199                 rc = samba_getpass("Password: ", pwd, sizeof(pwd), false, false);
200                 if (rc == 0) {
201                         fstrcpy(password, pwd);
202                 }
203         }
204
205         creds = cli_session_creds_init(NULL,
206                                        username,
207                                        lp_workgroup(),
208                                        NULL, /* realm (use default) */
209                                        password,
210                                        use_kerberos,
211                                        fallback_after_kerberos,
212                                        use_ccache,
213                                        pw_nt_hash);
214         if (creds == NULL) {
215                 DEBUG(0, ("cli_session_creds_init failed\n"));
216                 return NULL;
217         }
218
219         slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
220
221         nt_status = cli_full_connection_creds(&c,
222                                               myname,
223                                               server_n,
224                                               NULL,
225                                               0,
226                                               share,
227                                               "?????",
228                                               creds,
229                                               0);
230         TALLOC_FREE(creds);
231         if (!NT_STATUS_IS_OK(nt_status)) {
232                 DEBUG(0, ("cli_full_connection failed with error %s\n", nt_errstr(nt_status)));
233                 return NULL;
234         }
235
236         c->use_oplocks = use_oplocks;
237
238         return c;
239 }
240
241
242 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
243                       char *nfs[NSERVERS], 
244                       int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
245                       char *share1, char *share2)
246 {
247         int server, conn, f, fstype;
248         char *share[2];
249         share[0] = share1;
250         share[1] = share2;
251
252         fstype = FSTYPE_SMB;
253
254         for (server=0;server<NSERVERS;server++)
255         for (conn=0;conn<NCONNECTIONS;conn++) {
256                 if (cli[server][conn]) {
257                         for (f=0;f<NFILES;f++) {
258                                 cli_close(cli[server][conn], fnum[server][fstype][conn][f]);
259                         }
260                         cli_ulogoff(cli[server][conn]);
261                         cli_shutdown(cli[server][conn]);
262                 }
263                 cli[server][conn] = connect_one(share[server]);
264                 if (!cli[server][conn]) {
265                         DEBUG(0,("Failed to connect to %s\n", share[server]));
266                         exit(1);
267                 }
268         }
269 }
270
271
272
273 static bool test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
274                      char *nfs[NSERVERS],
275                      int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
276                      struct record *rec)
277 {
278         unsigned conn = rec->conn;
279         unsigned f = rec->f;
280         unsigned fstype = rec->fstype;
281         unsigned start = rec->start;
282         unsigned len = rec->len;
283         unsigned r1 = rec->r1;
284         unsigned r2 = rec->r2;
285         enum brl_type op;
286         int server;
287         bool ret[NSERVERS];
288
289         if (r1 < READ_PCT) {
290                 op = READ_LOCK; 
291         } else {
292                 op = WRITE_LOCK; 
293         }
294
295         if (r2 < LOCK_PCT) {
296                 /* set a lock */
297                 for (server=0;server<NSERVERS;server++) {
298                         ret[server] = try_lock(cli[server][conn], fstype,
299                                                fnum[server][fstype][conn][f],
300                                                start, len, op);
301                 }
302                 if (showall || ret[0] != ret[1]) {
303                         printf("lock   conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
304                                conn, fstype, f, 
305                                start, start+len-1, len,
306                                op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
307                                ret[0], ret[1]);
308                 }
309                 if (showall) brl_forall(print_brl, NULL);
310                 if (ret[0] != ret[1]) return False;
311         } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
312                 /* unset a lock */
313                 for (server=0;server<NSERVERS;server++) {
314                         ret[server] = try_unlock(cli[server][conn], fstype,
315                                                  fnum[server][fstype][conn][f],
316                                                  start, len);
317                 }
318                 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
319                         printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u)       -> %u:%u\n",
320                                conn, fstype, f, 
321                                start, start+len-1, len,
322                                ret[0], ret[1]);
323                 }
324                 if (showall) brl_forall(print_brl, NULL);
325                 if (!hide_unlock_fails && ret[0] != ret[1]) return False;
326         } else {
327                 /* reopen the file */
328                 for (server=0;server<NSERVERS;server++) {
329                         try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
330                         fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
331                                                                  O_RDWR|O_CREAT);
332                         if (fnum[server][fstype][conn][f] == -1) {
333                                 printf("failed to reopen on share1\n");
334                                 return False;
335                         }
336                 }
337                 if (showall) {
338                         printf("reopen conn=%u fstype=%u f=%u\n",
339                                conn, fstype, f);
340                         brl_forall(print_brl, NULL);
341                 }
342         }
343         return True;
344 }
345
346 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
347                         char *nfs[NSERVERS],
348                         int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
349 {
350         int server, conn, f, fstype; 
351
352         for (server=0;server<NSERVERS;server++)
353         for (fstype=0;fstype<NUMFSTYPES;fstype++)
354         for (conn=0;conn<NCONNECTIONS;conn++)
355         for (f=0;f<NFILES;f++) {
356                 if (fnum[server][fstype][conn][f] != -1) {
357                         try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
358                         fnum[server][fstype][conn][f] = -1;
359                 }
360         }
361         for (server=0;server<NSERVERS;server++) {
362                 cli_unlink(cli[server][0], FILENAME, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
363         }
364 }
365
366 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
367                        char *nfs[NSERVERS],
368                        int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
369 {
370         int server, fstype, conn, f; 
371
372         for (server=0;server<NSERVERS;server++)
373         for (fstype=0;fstype<NUMFSTYPES;fstype++)
374         for (conn=0;conn<NCONNECTIONS;conn++)
375         for (f=0;f<NFILES;f++) {
376                 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
377                                                          O_RDWR|O_CREAT);
378                 if (fnum[server][fstype][conn][f] == -1) {
379                         fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
380                                 server, fstype, conn, f);
381                         exit(1);
382                 }
383         }
384 }
385
386
387 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
388                   char *nfs[NSERVERS],
389                   int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
390                   int n)
391 {
392         int i;
393         printf("testing %u ...\n", n);
394         for (i=0; i<n; i++) {
395                 if (i && i % 100 == 0) {
396                         printf("%u\n", i);
397                 }
398
399                 if (recorded[i].needed &&
400                     !test_one(cli, nfs, fnum, &recorded[i])) return i;
401         }
402         return n;
403 }
404
405
406 /* each server has two connections open to it. Each connection has two file
407    descriptors open on the file - 8 file descriptors in total 
408
409    we then do random locking ops in tamdem on the 4 fnums from each
410    server and ensure that the results match
411  */
412 static void test_locks(char *share1, char *share2, char *nfspath1, char *nfspath2)
413 {
414         struct cli_state *cli[NSERVERS][NCONNECTIONS];
415         char *nfs[NSERVERS];
416         int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
417         int n, i, n1; 
418
419         nfs[0] = nfspath1;
420         nfs[1] = nfspath2;
421
422         ZERO_STRUCT(fnum);
423         ZERO_STRUCT(cli);
424
425         recorded = SMB_MALLOC_ARRAY(struct record, numops);
426
427         for (n=0; n<numops; n++) {
428                 recorded[n].conn = random() % NCONNECTIONS;
429                 recorded[n].fstype = random() % NUMFSTYPES;
430                 recorded[n].f = random() % NFILES;
431                 recorded[n].start = LOCKBASE + ((unsigned)random() % (LOCKRANGE-1));
432                 recorded[n].len = 1 + 
433                         random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
434                 recorded[n].start *= RANGE_MULTIPLE;
435                 recorded[n].len *= RANGE_MULTIPLE;
436                 recorded[n].r1 = random() % 100;
437                 recorded[n].r2 = random() % 100;
438                 recorded[n].needed = True;
439         }
440
441         reconnect(cli, nfs, fnum, share1, share2);
442         open_files(cli, nfs, fnum);
443         n = retest(cli, nfs, fnum, numops);
444
445         if (n == numops || !analyze) return;
446         n++;
447
448         while (1) {
449                 n1 = n;
450
451                 close_files(cli, nfs, fnum);
452                 reconnect(cli, nfs, fnum, share1, share2);
453                 open_files(cli, nfs, fnum);
454
455                 for (i=0;i<n-1;i++) {
456                         int m;
457                         recorded[i].needed = False;
458
459                         close_files(cli, nfs, fnum);
460                         open_files(cli, nfs, fnum);
461
462                         m = retest(cli, nfs, fnum, n);
463                         if (m == n) {
464                                 recorded[i].needed = True;
465                         } else {
466                                 if (i < m) {
467                                         memmove(&recorded[i], &recorded[i+1],
468                                                 (m-i)*sizeof(recorded[0]));
469                                 }
470                                 n = m;
471                                 i--;
472                         }
473                 }
474
475                 if (n1 == n) break;
476         }
477
478         close_files(cli, nfs, fnum);
479         reconnect(cli, nfs, fnum, share1, share2);
480         open_files(cli, nfs, fnum);
481         showall = True;
482         n1 = retest(cli, nfs, fnum, n);
483         if (n1 != n-1) {
484                 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
485         }
486         close_files(cli, nfs, fnum);
487
488         for (i=0;i<n;i++) {
489                 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
490                        recorded[i].r1,
491                        recorded[i].r2,
492                        recorded[i].conn,
493                        recorded[i].fstype,
494                        recorded[i].f,
495                        recorded[i].start,
496                        recorded[i].len,
497                        recorded[i].needed);
498         }       
499 }
500
501
502
503 static void usage(void)
504 {
505         printf(
506 "Usage:\n\
507   locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
508   options:\n\
509         -U user%%pass\n\
510         -s seed\n\
511         -o numops\n\
512         -u          hide unlock fails\n\
513         -a          (show all ops)\n\
514         -O          use oplocks\n\
515 ");
516 }
517
518 /****************************************************************************
519   main program
520 ****************************************************************************/
521  int main(int argc,char *argv[])
522 {
523         char *share1, *share2, *nfspath1, *nfspath2;
524         int opt;
525         char *p;
526         int seed;
527
528         setlinebuf(stdout);
529
530         if (argc < 5 || argv[1][0] == '-') {
531                 usage();
532                 exit(1);
533         }
534
535         setup_logging(argv[0], DEBUG_STDOUT);
536
537         share1 = argv[1];
538         share2 = argv[2];
539         nfspath1 = argv[3];
540         nfspath2 = argv[4];
541
542         all_string_sub(share1,"/","\\",0);
543         all_string_sub(share2,"/","\\",0);
544
545         argc -= 4;
546         argv += 4;
547
548         lp_load_global(get_dyn_CONFIGFILE());
549         load_interfaces();
550
551         if (getenv("USER")) {
552                 fstrcpy(username,getenv("USER"));
553         }
554
555         seed = time(NULL);
556
557         while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
558                 switch (opt) {
559                 case 'U':
560                         fstrcpy(username,optarg);
561                         p = strchr_m(username,'%');
562                         if (p) {
563                                 *p = 0;
564                                 fstrcpy(password, p+1);
565                                 got_pass = 1;
566                         }
567                         break;
568                 case 's':
569                         seed = atoi(optarg);
570                         break;
571                 case 'u':
572                         hide_unlock_fails = True;
573                         break;
574                 case 'o':
575                         numops = atoi(optarg);
576                         break;
577                 case 'O':
578                         use_oplocks = True;
579                         break;
580                 case 'a':
581                         showall = True;
582                         break;
583                 case 'A':
584                         analyze = True;
585                         break;
586                 case 'h':
587                         usage();
588                         exit(1);
589                 default:
590                         printf("Unknown option %c (%d)\n", (char)opt, opt);
591                         exit(1);
592                 }
593         }
594
595         argc -= optind;
596         argv += optind;
597
598         DEBUG(0,("seed=%u\n", seed));
599         srandom(seed);
600
601         locking_init_readonly();
602         test_locks(share1, share2, nfspath1, nfspath2);
603
604         return(0);
605 }