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