e7cbf13c74eb4a4ef1c2c43427ff121bdd24a44d
[gd/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         char *path;
67         int ret;
68
69         switch (fstype) {
70         case FSTYPE_SMB:
71                 return smbcli_open(c, fname, flags, DENY_NONE);
72
73         case FSTYPE_NFS:
74                 asprintf(&path, "%s%s", nfs, fname);
75                 string_replace(path,'\\', '/');
76                 ret = open(path, flags, 0666);
77                 SAFE_FREE(Path);
78                 return ret;
79         }
80
81         return -1;
82 }
83
84 static bool try_close(struct smbcli_state *c, int fstype, int fd)
85 {
86         switch (fstype) {
87         case FSTYPE_SMB:
88                 return smbcli_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 smbcli_state *c, int fstype, 
98                      int fd, uint_t start, uint_t len,
99                      enum brl_type op)
100 {
101         struct flock lock;
102
103         switch (fstype) {
104         case FSTYPE_SMB:
105                 return smbcli_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 smbcli_state *c, int fstype, 
120                        int fd, uint_t start, uint_t len)
121 {
122         struct flock lock;
123
124         switch (fstype) {
125         case FSTYPE_SMB:
126                 return smbcli_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 /***************************************************** 
141 return a connection to a server
142 *******************************************************/
143 static struct smbcli_state *connect_one(TALLOC_CTX *mem_ctx,
144                                                                                 char *share, const char **ports,
145                                         struct smb_options *options,
146                                         struct smb_options *session_options,
147                                         struct event_context *ev)
148 {
149         struct smbcli_state *c;
150         char *server_n;
151         char *server;
152         char *myname;
153         static int count;
154         NTSTATUS nt_status;
155
156         server = talloc_strdup(mem_ctx, share+2);
157         share = strchr_m(server,'\\');
158         if (!share) return NULL;
159         *share = 0;
160         share++;
161
162         server_n = server;
163         
164         if (!got_pass) {
165                 char *pass = getpass("Password: ");
166                 if (pass) {
167                         password = talloc_strdup(mem_ctx, pass);
168                 }
169         }
170
171         myname = talloc_asprintf(mem_ctx, "lock-%u-%u", getpid(), count++);
172
173         nt_status = smbcli_full_connection(NULL, 
174                            &c, myname, server_n, ports, share, NULL,
175                            username, lp_workgroup(), password, ev,
176                            options, session_options);
177         if (!NT_STATUS_IS_OK(nt_status)) {
178                 DEBUG(0, ("smbcli_full_connection failed with error %s\n", nt_errstr(nt_status)));
179                 return NULL;
180         }
181
182         c->use_oplocks = use_oplocks;
183
184         return c;
185 }
186
187
188 static void reconnect(TALLOC_CTX *mem_ctx, 
189                                           struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
190                       char *nfs[NSERVERS], 
191                       int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
192                       const char **ports,
193                       struct smbcli_options *options,
194                       struct smbcli_session_options *session_options,
195                       struct event_context *ev,
196                       char *share1, char *share2)
197 {
198         int server, conn, f, fstype;
199         char *share[2];
200         share[0] = share1;
201         share[1] = share2;
202
203         fstype = FSTYPE_SMB;
204
205         for (server=0;server<NSERVERS;server++)
206         for (conn=0;conn<NCONNECTIONS;conn++) {
207                 if (cli[server][conn]) {
208                         for (f=0;f<NFILES;f++) {
209                                 smbcli_close(cli[server][conn], fnum[server][fstype][conn][f]);
210                         }
211                         smbcli_ulogoff(cli[server][conn]);
212                         talloc_free(cli[server][conn]);
213                 }
214                 cli[server][conn] = connect_one(mem_ctx, share[server], ports, options, session_options, ev);
215                 if (!cli[server][conn]) {
216                         DEBUG(0,("Failed to connect to %s\n", share[server]));
217                         exit(1);
218                 }
219         }
220 }
221
222
223
224 static bool test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
225                      char *nfs[NSERVERS],
226                      int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
227                      struct record *rec)
228 {
229         uint_t conn = rec->conn;
230         uint_t f = rec->f;
231         uint_t fstype = rec->fstype;
232         uint_t start = rec->start;
233         uint_t len = rec->len;
234         uint_t r1 = rec->r1;
235         uint_t r2 = rec->r2;
236         enum brl_type op;
237         int server;
238         bool ret[NSERVERS];
239
240         if (r1 < READ_PCT) {
241                 op = READ_LOCK; 
242         } else {
243                 op = WRITE_LOCK; 
244         }
245
246         if (r2 < LOCK_PCT) {
247                 /* set a lock */
248                 for (server=0;server<NSERVERS;server++) {
249                         ret[server] = try_lock(cli[server][conn], fstype,
250                                                fnum[server][fstype][conn][f],
251                                                start, len, op);
252                 }
253                 if (showall || ret[0] != ret[1]) {
254                         printf("lock   conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
255                                conn, fstype, f, 
256                                start, start+len-1, len,
257                                op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
258                                ret[0], ret[1]);
259                 }
260                 if (ret[0] != ret[1]) return false;
261         } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
262                 /* unset a lock */
263                 for (server=0;server<NSERVERS;server++) {
264                         ret[server] = try_unlock(cli[server][conn], fstype,
265                                                  fnum[server][fstype][conn][f],
266                                                  start, len);
267                 }
268                 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
269                         printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u)       -> %u:%u\n",
270                                conn, fstype, f, 
271                                start, start+len-1, len,
272                                ret[0], ret[1]);
273                 }
274                 if (!hide_unlock_fails && ret[0] != ret[1]) return false;
275         } else {
276                 /* reopen the file */
277                 for (server=0;server<NSERVERS;server++) {
278                         try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
279                         fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
280                                                                  O_RDWR|O_CREAT);
281                         if (fnum[server][fstype][conn][f] == -1) {
282                                 printf("failed to reopen on share1\n");
283                                 return false;
284                         }
285                 }
286                 if (showall) {
287                         printf("reopen conn=%u fstype=%u f=%u\n",
288                                conn, fstype, f);
289                 }
290         }
291         return true;
292 }
293
294 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
295                         char *nfs[NSERVERS],
296                         int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
297 {
298         int server, conn, f, fstype; 
299
300         for (server=0;server<NSERVERS;server++)
301         for (fstype=0;fstype<NUMFSTYPES;fstype++)
302         for (conn=0;conn<NCONNECTIONS;conn++)
303         for (f=0;f<NFILES;f++) {
304                 if (fnum[server][fstype][conn][f] != -1) {
305                         try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
306                         fnum[server][fstype][conn][f] = -1;
307                 }
308         }
309         for (server=0;server<NSERVERS;server++) {
310                 smbcli_unlink(cli[server][0], FILENAME);
311         }
312 }
313
314 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
315                        char *nfs[NSERVERS],
316                        int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
317 {
318         int server, fstype, conn, f; 
319
320         for (server=0;server<NSERVERS;server++)
321         for (fstype=0;fstype<NUMFSTYPES;fstype++)
322         for (conn=0;conn<NCONNECTIONS;conn++)
323         for (f=0;f<NFILES;f++) {
324                 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
325                                                          O_RDWR|O_CREAT);
326                 if (fnum[server][fstype][conn][f] == -1) {
327                         fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
328                                 server, fstype, conn, f);
329                         exit(1);
330                 }
331         }
332 }
333
334
335 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
336                   char *nfs[NSERVERS],
337                   int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
338                   int n)
339 {
340         int i;
341         printf("testing %u ...\n", n);
342         for (i=0; i<n; i++) {
343                 if (i && i % 100 == 0) {
344                         printf("%u\n", i);
345                 }
346
347                 if (recorded[i].needed &&
348                     !test_one(cli, nfs, fnum, &recorded[i])) return i;
349         }
350         return n;
351 }
352
353
354 /* each server has two connections open to it. Each connection has two file
355    descriptors open on the file - 8 file descriptors in total 
356
357    we then do random locking ops in tamdem on the 4 fnums from each
358    server and ensure that the results match
359  */
360 static void test_locks(TALLOC_CTX *mem_ctx, char *share1, char *share2,
361                         char *nfspath1, char *nfspath2,
362                         const char **ports,
363                         struct smbcli_options *options,
364                         struct smbcli_session_options *session_options,
365                         struct event_context *ev)
366 {
367         struct smbcli_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 = malloc_array_p(struct record, 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 + ((uint_t)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(mem_ctx, cli, nfs, fnum, ports, options, session_options, ev, 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(mem_ctx, cli, nfs, fnum, ports, options, session_options, ev, 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(mem_ctx, cli, nfs, fnum, ports, options, session_options, ev, 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         struct smbcli_options options;
480         struct smbcli_session_options session_options;
481         TALLOC_CTX *mem_ctx;
482         int opt;
483         char *p;
484         int seed;
485         struct loadparm_context *lp_ctx;
486         struct event_context *ev;
487
488         mem_ctx = talloc_autofree_context();
489
490         setlinebuf(stdout);
491
492         dbf = x_stderr;
493
494         if (argc < 5 || argv[1][0] == '-') {
495                 usage();
496                 exit(1);
497         }
498
499         share1 = argv[1];
500         share2 = argv[2];
501         nfspath1 = argv[3];
502         nfspath2 = argv[4];
503
504         all_string_sub(share1,"/","\\",0);
505         all_string_sub(share2,"/","\\",0);
506
507         setup_logging(argv[0], DEBUG_STDOUT);
508
509         argc -= 4;
510         argv += 4;
511
512         lp_ctx = loadparm_init(mem_ctx);
513         lp_load(lp_ctx, dyn_CONFIGFILE);
514
515         if (getenv("USER")) {
516                 username = talloc_strdup(mem_ctx, getenv("USER"));
517         }
518
519         seed = time(NULL);
520
521         while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
522                 switch (opt) {
523                 case 'U':
524                         username = talloc_strdup(mem_ctx, optarg);
525                         p = strchr_m(username,'%');
526                         if (p) {
527                                 *p = 0;
528                                 password = talloc_strdup(mem_ctx, p+1);
529                                 got_pass = 1;
530                         }
531                         break;
532                 case 's':
533                         seed = atoi(optarg);
534                         break;
535                 case 'u':
536                         hide_unlock_fails = true;
537                         break;
538                 case 'o':
539                         numops = atoi(optarg);
540                         break;
541                 case 'O':
542                         use_oplocks = true;
543                         break;
544                 case 'a':
545                         showall = true;
546                         break;
547                 case 'A':
548                         analyze = true;
549                         break;
550                 case 'h':
551                         usage();
552                         exit(1);
553                 default:
554                         printf("Unknown option %c (%d)\n", (char)opt, opt);
555                         exit(1);
556                 }
557         }
558
559         argc -= optind;
560         argv += optind;
561
562         DEBUG(0,("seed=%u\n", seed));
563         srandom(seed);
564
565         ev = s4_event_context_init(mem_ctx);
566
567         locking_init(1);
568         lp_smbcli_options(lp_ctx, &options);
569         lp_smbcli_session_options(lp_ctx, &session_options);
570         test_locks(mem_ctx, share1, share2, nfspath1, nfspath2, lp_smb_ports(lp_ctx),
571                    &options, &session_options, ev);
572
573         return(0);
574 }