Remove more event_context_init() uses from function calls within deep down the code.
[bbaumbach/samba-autobuild/.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 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 "system/passwd.h"
22 #include "lib/events/events.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, const char **ports,
141                                         struct smb_options *optionsi,
142                                         struct event_context *ev)
143 {
144         struct smbcli_state *c;
145         char *server_n;
146         fstring server;
147         fstring myname;
148         static int count;
149         NTSTATUS nt_status;
150
151         fstrcpy(server,share+2);
152         share = strchr_m(server,'\\');
153         if (!share) return NULL;
154         *share = 0;
155         share++;
156
157         server_n = server;
158         
159         if (!got_pass) {
160                 char *pass = getpass("Password: ");
161                 if (pass) {
162                         fstrcpy(password, pass);
163                 }
164         }
165
166         slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), count++);
167
168         nt_status = smbcli_full_connection(NULL, 
169                            &c, myname, server_n, ports, share, NULL,
170                            username, lp_workgroup(), password, ev,
171                            options);
172         if (!NT_STATUS_IS_OK(nt_status)) {
173                 DEBUG(0, ("smbcli_full_connection failed with error %s\n", nt_errstr(nt_status)));
174                 return NULL;
175         }
176
177         c->use_oplocks = use_oplocks;
178
179         return c;
180 }
181
182
183 static void reconnect(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
184                       char *nfs[NSERVERS], 
185                       int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
186                       const char **ports,
187                       struct smbcli_options *options,
188                       struct event_context *ev,
189                       char *share1, char *share2)
190 {
191         int server, conn, f, fstype;
192         char *share[2];
193         share[0] = share1;
194         share[1] = share2;
195
196         fstype = FSTYPE_SMB;
197
198         for (server=0;server<NSERVERS;server++)
199         for (conn=0;conn<NCONNECTIONS;conn++) {
200                 if (cli[server][conn]) {
201                         for (f=0;f<NFILES;f++) {
202                                 smbcli_close(cli[server][conn], fnum[server][fstype][conn][f]);
203                         }
204                         smbcli_ulogoff(cli[server][conn]);
205                         talloc_free(cli[server][conn]);
206                 }
207                 cli[server][conn] = connect_one(share[server], ports, options, ev);
208                 if (!cli[server][conn]) {
209                         DEBUG(0,("Failed to connect to %s\n", share[server]));
210                         exit(1);
211                 }
212         }
213 }
214
215
216
217 static bool test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
218                      char *nfs[NSERVERS],
219                      int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
220                      struct record *rec)
221 {
222         uint_t conn = rec->conn;
223         uint_t f = rec->f;
224         uint_t fstype = rec->fstype;
225         uint_t start = rec->start;
226         uint_t len = rec->len;
227         uint_t r1 = rec->r1;
228         uint_t r2 = rec->r2;
229         enum brl_type op;
230         int server;
231         bool ret[NSERVERS];
232
233         if (r1 < READ_PCT) {
234                 op = READ_LOCK; 
235         } else {
236                 op = WRITE_LOCK; 
237         }
238
239         if (r2 < LOCK_PCT) {
240                 /* set a lock */
241                 for (server=0;server<NSERVERS;server++) {
242                         ret[server] = try_lock(cli[server][conn], fstype,
243                                                fnum[server][fstype][conn][f],
244                                                start, len, op);
245                 }
246                 if (showall || ret[0] != ret[1]) {
247                         printf("lock   conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
248                                conn, fstype, f, 
249                                start, start+len-1, len,
250                                op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
251                                ret[0], ret[1]);
252                 }
253                 if (ret[0] != ret[1]) return false;
254         } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
255                 /* unset a lock */
256                 for (server=0;server<NSERVERS;server++) {
257                         ret[server] = try_unlock(cli[server][conn], fstype,
258                                                  fnum[server][fstype][conn][f],
259                                                  start, len);
260                 }
261                 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
262                         printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u)       -> %u:%u\n",
263                                conn, fstype, f, 
264                                start, start+len-1, len,
265                                ret[0], ret[1]);
266                 }
267                 if (!hide_unlock_fails && ret[0] != ret[1]) return false;
268         } else {
269                 /* reopen the file */
270                 for (server=0;server<NSERVERS;server++) {
271                         try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
272                         fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
273                                                                  O_RDWR|O_CREAT);
274                         if (fnum[server][fstype][conn][f] == -1) {
275                                 printf("failed to reopen on share1\n");
276                                 return false;
277                         }
278                 }
279                 if (showall) {
280                         printf("reopen conn=%u fstype=%u f=%u\n",
281                                conn, fstype, f);
282                 }
283         }
284         return true;
285 }
286
287 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
288                         char *nfs[NSERVERS],
289                         int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
290 {
291         int server, conn, f, fstype; 
292
293         for (server=0;server<NSERVERS;server++)
294         for (fstype=0;fstype<NUMFSTYPES;fstype++)
295         for (conn=0;conn<NCONNECTIONS;conn++)
296         for (f=0;f<NFILES;f++) {
297                 if (fnum[server][fstype][conn][f] != -1) {
298                         try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
299                         fnum[server][fstype][conn][f] = -1;
300                 }
301         }
302         for (server=0;server<NSERVERS;server++) {
303                 smbcli_unlink(cli[server][0], FILENAME);
304         }
305 }
306
307 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
308                        char *nfs[NSERVERS],
309                        int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
310 {
311         int server, fstype, conn, f; 
312
313         for (server=0;server<NSERVERS;server++)
314         for (fstype=0;fstype<NUMFSTYPES;fstype++)
315         for (conn=0;conn<NCONNECTIONS;conn++)
316         for (f=0;f<NFILES;f++) {
317                 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
318                                                          O_RDWR|O_CREAT);
319                 if (fnum[server][fstype][conn][f] == -1) {
320                         fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
321                                 server, fstype, conn, f);
322                         exit(1);
323                 }
324         }
325 }
326
327
328 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
329                   char *nfs[NSERVERS],
330                   int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
331                   int n)
332 {
333         int i;
334         printf("testing %u ...\n", n);
335         for (i=0; i<n; i++) {
336                 if (i && i % 100 == 0) {
337                         printf("%u\n", i);
338                 }
339
340                 if (recorded[i].needed &&
341                     !test_one(cli, nfs, fnum, &recorded[i])) return i;
342         }
343         return n;
344 }
345
346
347 /* each server has two connections open to it. Each connection has two file
348    descriptors open on the file - 8 file descriptors in total 
349
350    we then do random locking ops in tamdem on the 4 fnums from each
351    server and ensure that the results match
352  */
353 static void test_locks(char *share1, char *share2,
354                         char *nfspath1, char *nfspath2,
355                         const char **ports,
356                         struct smbcli_options *options,
357                         struct event_context *ev)
358 {
359         struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
360         char *nfs[NSERVERS];
361         int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
362         int n, i, n1; 
363
364         nfs[0] = nfspath1;
365         nfs[1] = nfspath2;
366
367         ZERO_STRUCT(fnum);
368         ZERO_STRUCT(cli);
369
370         recorded = malloc_array_p(struct record, numops);
371
372         for (n=0; n<numops; n++) {
373                 recorded[n].conn = random() % NCONNECTIONS;
374                 recorded[n].fstype = random() % NUMFSTYPES;
375                 recorded[n].f = random() % NFILES;
376                 recorded[n].start = LOCKBASE + ((uint_t)random() % (LOCKRANGE-1));
377                 recorded[n].len = 1 + 
378                         random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
379                 recorded[n].start *= RANGE_MULTIPLE;
380                 recorded[n].len *= RANGE_MULTIPLE;
381                 recorded[n].r1 = random() % 100;
382                 recorded[n].r2 = random() % 100;
383                 recorded[n].needed = true;
384         }
385
386         reconnect(cli, nfs, fnum, ports, options, ev, share1, share2);
387         open_files(cli, nfs, fnum);
388         n = retest(cli, nfs, fnum, numops);
389
390         if (n == numops || !analyze) return;
391         n++;
392
393         while (1) {
394                 n1 = n;
395
396                 close_files(cli, nfs, fnum);
397                 reconnect(cli, nfs, fnum, ports, options, ev, share1, share2);
398                 open_files(cli, nfs, fnum);
399
400                 for (i=0;i<n-1;i++) {
401                         int m;
402                         recorded[i].needed = false;
403
404                         close_files(cli, nfs, fnum);
405                         open_files(cli, nfs, fnum);
406
407                         m = retest(cli, nfs, fnum, n);
408                         if (m == n) {
409                                 recorded[i].needed = true;
410                         } else {
411                                 if (i < m) {
412                                         memmove(&recorded[i], &recorded[i+1],
413                                                 (m-i)*sizeof(recorded[0]));
414                                 }
415                                 n = m;
416                                 i--;
417                         }
418                 }
419
420                 if (n1 == n) break;
421         }
422
423         close_files(cli, nfs, fnum);
424         reconnect(cli, nfs, fnum, ports, options, ev, share1, share2);
425         open_files(cli, nfs, fnum);
426         showall = true;
427         n1 = retest(cli, nfs, fnum, n);
428         if (n1 != n-1) {
429                 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
430         }
431         close_files(cli, nfs, fnum);
432
433         for (i=0;i<n;i++) {
434                 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
435                        recorded[i].r1,
436                        recorded[i].r2,
437                        recorded[i].conn,
438                        recorded[i].fstype,
439                        recorded[i].f,
440                        recorded[i].start,
441                        recorded[i].len,
442                        recorded[i].needed);
443         }       
444 }
445
446
447
448 static void usage(void)
449 {
450         printf(
451 "Usage:\n\
452   locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
453   options:\n\
454         -U user%%pass\n\
455         -s seed\n\
456         -o numops\n\
457         -u          hide unlock fails\n\
458         -a          (show all ops)\n\
459         -O          use oplocks\n\
460 ");
461 }
462
463 /****************************************************************************
464   main program
465 ****************************************************************************/
466  int main(int argc,char *argv[])
467 {
468         char *share1, *share2, *nfspath1, *nfspath2;
469         extern char *optarg;
470         extern int optind;
471         struct smbcli_options options;
472         int opt;
473         char *p;
474         int seed;
475         struct loadparm_context *lp_ctx;
476         struct event_context *ev;
477
478         setlinebuf(stdout);
479
480         dbf = x_stderr;
481
482         if (argc < 5 || argv[1][0] == '-') {
483                 usage();
484                 exit(1);
485         }
486
487         share1 = argv[1];
488         share2 = argv[2];
489         nfspath1 = argv[3];
490         nfspath2 = argv[4];
491
492         all_string_sub(share1,"/","\\",0);
493         all_string_sub(share2,"/","\\",0);
494
495         setup_logging(argv[0], DEBUG_STDOUT);
496
497         argc -= 4;
498         argv += 4;
499
500         lp_ctx = loadparm_init(talloc_autofree_context());
501         lp_load(lp_ctx, dyn_CONFIGFILE);
502
503         if (getenv("USER")) {
504                 fstrcpy(username,getenv("USER"));
505         }
506
507         seed = time(NULL);
508
509         while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
510                 switch (opt) {
511                 case 'U':
512                         fstrcpy(username,optarg);
513                         p = strchr_m(username,'%');
514                         if (p) {
515                                 *p = 0;
516                                 fstrcpy(password, p+1);
517                                 got_pass = 1;
518                         }
519                         break;
520                 case 's':
521                         seed = atoi(optarg);
522                         break;
523                 case 'u':
524                         hide_unlock_fails = true;
525                         break;
526                 case 'o':
527                         numops = atoi(optarg);
528                         break;
529                 case 'O':
530                         use_oplocks = true;
531                         break;
532                 case 'a':
533                         showall = true;
534                         break;
535                 case 'A':
536                         analyze = true;
537                         break;
538                 case 'h':
539                         usage();
540                         exit(1);
541                 default:
542                         printf("Unknown option %c (%d)\n", (char)opt, opt);
543                         exit(1);
544                 }
545         }
546
547         argc -= optind;
548         argv += optind;
549
550         DEBUG(0,("seed=%u\n", seed));
551         srandom(seed);
552
553         ev = event_context_init(talloc_autofree_context());
554
555         locking_init(1);
556         lp_smbcli_options(lp_ctx, &options);
557         test_locks(share1, share2, nfspath1, nfspath2, lp_smb_ports(lp_ctx),
558                    &options, ev);
559
560         return(0);
561 }