Patch from metze and me that adds dummy smb_register_*() functions
[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 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #define NO_SYSLOG
22
23 #include "includes.h"
24
25 #include "module_dummy.h"
26
27 static fstring password;
28 static fstring username;
29 static int got_pass;
30 static int numops = 1000;
31 static BOOL showall;
32 static BOOL analyze;
33 static BOOL hide_unlock_fails;
34 static BOOL use_oplocks;
35
36 #define FILENAME "\\locktest.dat"
37 #define LOCKRANGE 100
38 #define LOCKBASE 0
39
40 /*
41 #define LOCKBASE (0x40000000 - 50)
42 */
43
44 #define READ_PCT 50
45 #define LOCK_PCT 25
46 #define UNLOCK_PCT 65
47 #define RANGE_MULTIPLE 1
48
49 #define NSERVERS 2
50 #define NCONNECTIONS 2
51 #define NUMFSTYPES 2
52 #define NFILES 2
53 #define LOCK_TIMEOUT 0
54
55 #define FSTYPE_SMB 0
56 #define FSTYPE_NFS 1
57
58 struct record {
59         char r1, r2;
60         char conn, f, fstype;
61         unsigned start, len;
62         char needed;
63 };
64
65 static struct record *recorded;
66
67 static int try_open(struct cli_state *c, char *nfs, int fstype, const char *fname, int flags)
68 {
69         pstring path;
70
71         switch (fstype) {
72         case FSTYPE_SMB:
73                 return cli_open(c, fname, flags, DENY_NONE);
74
75         case FSTYPE_NFS:
76                 slprintf(path, sizeof(path), "%s%s", nfs, fname);
77                 pstring_sub(path,"\\", "/");
78                 return open(path, flags, 0666);
79         }
80
81         return -1;
82 }
83
84 static BOOL try_close(struct cli_state *c, int fstype, int fd)
85 {
86         switch (fstype) {
87         case FSTYPE_SMB:
88                 return cli_close(c, fd);
89
90         case FSTYPE_NFS:
91                 return close(fd) == 0;
92         }
93
94         return False;
95 }
96
97 static BOOL try_lock(struct cli_state *c, int fstype, 
98                      int fd, unsigned start, unsigned len,
99                      enum brl_type op)
100 {
101         struct flock lock;
102
103         switch (fstype) {
104         case FSTYPE_SMB:
105                 return cli_lock(c, fd, start, len, LOCK_TIMEOUT, op);
106
107         case FSTYPE_NFS:
108                 lock.l_type = (op==READ_LOCK) ? F_RDLCK:F_WRLCK;
109                 lock.l_whence = SEEK_SET;
110                 lock.l_start = start;
111                 lock.l_len = len;
112                 lock.l_pid = getpid();
113                 return fcntl(fd,F_SETLK,&lock) == 0;
114         }
115
116         return False;
117 }
118
119 static BOOL try_unlock(struct cli_state *c, int fstype, 
120                        int fd, unsigned start, unsigned len)
121 {
122         struct flock lock;
123
124         switch (fstype) {
125         case FSTYPE_SMB:
126                 return cli_unlock(c, fd, start, len);
127
128         case FSTYPE_NFS:
129                 lock.l_type = F_UNLCK;
130                 lock.l_whence = SEEK_SET;
131                 lock.l_start = start;
132                 lock.l_len = len;
133                 lock.l_pid = getpid();
134                 return fcntl(fd,F_SETLK,&lock) == 0;
135         }
136
137         return False;
138 }       
139
140 static void print_brl(SMB_DEV_T dev, SMB_INO_T ino, int pid, 
141                       enum brl_type lock_type,
142                       br_off start, br_off size)
143 {
144         printf("%6d   %05x:%05x    %s  %.0f:%.0f(%.0f)\n", 
145                (int)pid, (int)dev, (int)ino, 
146                lock_type==READ_LOCK?"R":"W",
147                (double)start, (double)start+size-1,(double)size);
148
149 }
150
151 /***************************************************** 
152 return a connection to a server
153 *******************************************************/
154 static struct cli_state *connect_one(char *share)
155 {
156         struct cli_state *c;
157         char *server_n;
158         fstring server;
159         fstring myname;
160         static int count;
161         NTSTATUS nt_status;
162
163         fstrcpy(server,share+2);
164         share = strchr_m(server,'\\');
165         if (!share) return NULL;
166         *share = 0;
167         share++;
168
169         server_n = server;
170         
171         if (!got_pass) {
172                 char *pass = getpass("Password: ");
173                 if (pass) {
174                         fstrcpy(password, pass);
175                 }
176         }
177
178         slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), count++);
179
180         nt_status = cli_full_connection(&c, myname, server_n, NULL, 0, share, "?????", 
181                                         username, lp_workgroup(), password, 0,
182                                         NULL);
183
184         if (!NT_STATUS_IS_OK(nt_status)) {
185                 DEBUG(0, ("cli_full_connection failed with error %s\n", nt_errstr(nt_status)));
186                 return NULL;
187         }
188
189         c->use_oplocks = use_oplocks;
190
191         return c;
192 }
193
194
195 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
196                       char *nfs[NSERVERS], 
197                       int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
198                       char *share1, char *share2)
199 {
200         int server, conn, f, fstype;
201         char *share[2];
202         share[0] = share1;
203         share[1] = share2;
204
205         fstype = FSTYPE_SMB;
206
207         for (server=0;server<NSERVERS;server++)
208         for (conn=0;conn<NCONNECTIONS;conn++) {
209                 if (cli[server][conn]) {
210                         for (f=0;f<NFILES;f++) {
211                                 cli_close(cli[server][conn], fnum[server][fstype][conn][f]);
212                         }
213                         cli_ulogoff(cli[server][conn]);
214                         cli_shutdown(cli[server][conn]);
215                 }
216                 cli[server][conn] = connect_one(share[server]);
217                 if (!cli[server][conn]) {
218                         DEBUG(0,("Failed to connect to %s\n", share[server]));
219                         exit(1);
220                 }
221         }
222 }
223
224
225
226 static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
227                      char *nfs[NSERVERS],
228                      int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
229                      struct record *rec)
230 {
231         unsigned conn = rec->conn;
232         unsigned f = rec->f;
233         unsigned fstype = rec->fstype;
234         unsigned start = rec->start;
235         unsigned len = rec->len;
236         unsigned r1 = rec->r1;
237         unsigned r2 = rec->r2;
238         enum brl_type op;
239         int server;
240         BOOL ret[NSERVERS];
241
242         if (r1 < READ_PCT) {
243                 op = READ_LOCK; 
244         } else {
245                 op = WRITE_LOCK; 
246         }
247
248         if (r2 < LOCK_PCT) {
249                 /* set a lock */
250                 for (server=0;server<NSERVERS;server++) {
251                         ret[server] = try_lock(cli[server][conn], fstype,
252                                                fnum[server][fstype][conn][f],
253                                                start, len, op);
254                 }
255                 if (showall || ret[0] != ret[1]) {
256                         printf("lock   conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
257                                conn, fstype, f, 
258                                start, start+len-1, len,
259                                op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
260                                ret[0], ret[1]);
261                 }
262                 if (showall) brl_forall(print_brl);
263                 if (ret[0] != ret[1]) return False;
264         } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
265                 /* unset a lock */
266                 for (server=0;server<NSERVERS;server++) {
267                         ret[server] = try_unlock(cli[server][conn], fstype,
268                                                  fnum[server][fstype][conn][f],
269                                                  start, len);
270                 }
271                 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
272                         printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u)       -> %u:%u\n",
273                                conn, fstype, f, 
274                                start, start+len-1, len,
275                                ret[0], ret[1]);
276                 }
277                 if (showall) brl_forall(print_brl);
278                 if (!hide_unlock_fails && ret[0] != ret[1]) return False;
279         } else {
280                 /* reopen the file */
281                 for (server=0;server<NSERVERS;server++) {
282                         try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
283                         fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
284                                                                  O_RDWR|O_CREAT);
285                         if (fnum[server][fstype][conn][f] == -1) {
286                                 printf("failed to reopen on share1\n");
287                                 return False;
288                         }
289                 }
290                 if (showall) {
291                         printf("reopen conn=%u fstype=%u f=%u\n",
292                                conn, fstype, f);
293                         brl_forall(print_brl);
294                 }
295         }
296         return True;
297 }
298
299 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
300                         char *nfs[NSERVERS],
301                         int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
302 {
303         int server, conn, f, fstype; 
304
305         for (server=0;server<NSERVERS;server++)
306         for (fstype=0;fstype<NUMFSTYPES;fstype++)
307         for (conn=0;conn<NCONNECTIONS;conn++)
308         for (f=0;f<NFILES;f++) {
309                 if (fnum[server][fstype][conn][f] != -1) {
310                         try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
311                         fnum[server][fstype][conn][f] = -1;
312                 }
313         }
314         for (server=0;server<NSERVERS;server++) {
315                 cli_unlink(cli[server][0], FILENAME);
316         }
317 }
318
319 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
320                        char *nfs[NSERVERS],
321                        int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
322 {
323         int server, fstype, conn, f; 
324
325         for (server=0;server<NSERVERS;server++)
326         for (fstype=0;fstype<NUMFSTYPES;fstype++)
327         for (conn=0;conn<NCONNECTIONS;conn++)
328         for (f=0;f<NFILES;f++) {
329                 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
330                                                          O_RDWR|O_CREAT);
331                 if (fnum[server][fstype][conn][f] == -1) {
332                         fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
333                                 server, fstype, conn, f);
334                         exit(1);
335                 }
336         }
337 }
338
339
340 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
341                   char *nfs[NSERVERS],
342                   int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
343                   int n)
344 {
345         int i;
346         printf("testing %u ...\n", n);
347         for (i=0; i<n; i++) {
348                 if (i && i % 100 == 0) {
349                         printf("%u\n", i);
350                 }
351
352                 if (recorded[i].needed &&
353                     !test_one(cli, nfs, fnum, &recorded[i])) return i;
354         }
355         return n;
356 }
357
358
359 /* each server has two connections open to it. Each connection has two file
360    descriptors open on the file - 8 file descriptors in total 
361
362    we then do random locking ops in tamdem on the 4 fnums from each
363    server and ensure that the results match
364  */
365 static void test_locks(char *share1, char *share2, char *nfspath1, char *nfspath2)
366 {
367         struct cli_state *cli[NSERVERS][NCONNECTIONS];
368         char *nfs[NSERVERS];
369         int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
370         int n, i, n1; 
371
372         nfs[0] = nfspath1;
373         nfs[1] = nfspath2;
374
375         ZERO_STRUCT(fnum);
376         ZERO_STRUCT(cli);
377
378         recorded = (struct record *)malloc(sizeof(*recorded) * numops);
379
380         for (n=0; n<numops; n++) {
381                 recorded[n].conn = random() % NCONNECTIONS;
382                 recorded[n].fstype = random() % NUMFSTYPES;
383                 recorded[n].f = random() % NFILES;
384                 recorded[n].start = LOCKBASE + ((unsigned)random() % (LOCKRANGE-1));
385                 recorded[n].len = 1 + 
386                         random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
387                 recorded[n].start *= RANGE_MULTIPLE;
388                 recorded[n].len *= RANGE_MULTIPLE;
389                 recorded[n].r1 = random() % 100;
390                 recorded[n].r2 = random() % 100;
391                 recorded[n].needed = True;
392         }
393
394         reconnect(cli, nfs, fnum, share1, share2);
395         open_files(cli, nfs, fnum);
396         n = retest(cli, nfs, fnum, numops);
397
398         if (n == numops || !analyze) return;
399         n++;
400
401         while (1) {
402                 n1 = n;
403
404                 close_files(cli, nfs, fnum);
405                 reconnect(cli, nfs, fnum, share1, share2);
406                 open_files(cli, nfs, fnum);
407
408                 for (i=0;i<n-1;i++) {
409                         int m;
410                         recorded[i].needed = False;
411
412                         close_files(cli, nfs, fnum);
413                         open_files(cli, nfs, fnum);
414
415                         m = retest(cli, nfs, fnum, n);
416                         if (m == n) {
417                                 recorded[i].needed = True;
418                         } else {
419                                 if (i < m) {
420                                         memmove(&recorded[i], &recorded[i+1],
421                                                 (m-i)*sizeof(recorded[0]));
422                                 }
423                                 n = m;
424                                 i--;
425                         }
426                 }
427
428                 if (n1 == n) break;
429         }
430
431         close_files(cli, nfs, fnum);
432         reconnect(cli, nfs, fnum, share1, share2);
433         open_files(cli, nfs, fnum);
434         showall = True;
435         n1 = retest(cli, nfs, fnum, n);
436         if (n1 != n-1) {
437                 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
438         }
439         close_files(cli, nfs, fnum);
440
441         for (i=0;i<n;i++) {
442                 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
443                        recorded[i].r1,
444                        recorded[i].r2,
445                        recorded[i].conn,
446                        recorded[i].fstype,
447                        recorded[i].f,
448                        recorded[i].start,
449                        recorded[i].len,
450                        recorded[i].needed);
451         }       
452 }
453
454
455
456 static void usage(void)
457 {
458         printf(
459 "Usage:\n\
460   locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
461   options:\n\
462         -U user%%pass\n\
463         -s seed\n\
464         -o numops\n\
465         -u          hide unlock fails\n\
466         -a          (show all ops)\n\
467         -O          use oplocks\n\
468 ");
469 }
470
471 /****************************************************************************
472   main program
473 ****************************************************************************/
474  int main(int argc,char *argv[])
475 {
476         char *share1, *share2, *nfspath1, *nfspath2;
477         extern char *optarg;
478         extern int optind;
479         int opt;
480         char *p;
481         int seed;
482
483         setlinebuf(stdout);
484
485         dbf = x_stderr;
486
487         if (argc < 5 || argv[1][0] == '-') {
488                 usage();
489                 exit(1);
490         }
491
492         share1 = argv[1];
493         share2 = argv[2];
494         nfspath1 = argv[3];
495         nfspath2 = argv[4];
496
497         all_string_sub(share1,"/","\\",0);
498         all_string_sub(share2,"/","\\",0);
499
500         setup_logging(argv[0],True);
501
502         argc -= 4;
503         argv += 4;
504
505         lp_load(dyn_CONFIGFILE,True,False,False);
506         load_interfaces();
507
508         if (getenv("USER")) {
509                 fstrcpy(username,getenv("USER"));
510         }
511
512         seed = time(NULL);
513
514         while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
515                 switch (opt) {
516                 case 'U':
517                         fstrcpy(username,optarg);
518                         p = strchr_m(username,'%');
519                         if (p) {
520                                 *p = 0;
521                                 fstrcpy(password, p+1);
522                                 got_pass = 1;
523                         }
524                         break;
525                 case 's':
526                         seed = atoi(optarg);
527                         break;
528                 case 'u':
529                         hide_unlock_fails = True;
530                         break;
531                 case 'o':
532                         numops = atoi(optarg);
533                         break;
534                 case 'O':
535                         use_oplocks = True;
536                         break;
537                 case 'a':
538                         showall = True;
539                         break;
540                 case 'A':
541                         analyze = True;
542                         break;
543                 case 'h':
544                         usage();
545                         exit(1);
546                 default:
547                         printf("Unknown option %c (%d)\n", (char)opt, opt);
548                         exit(1);
549                 }
550         }
551
552         argc -= optind;
553         argv += optind;
554
555         DEBUG(0,("seed=%u\n", seed));
556         srandom(seed);
557
558         locking_init(1);
559         test_locks(share1, share2, nfspath1, nfspath2);
560
561         return(0);
562 }