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