s3:winbindd_reconnect: don't only reconnect on NT_STATUS_UNSUCCESSFUL
[Samba/bb.git] / source4 / torture / locktest2.c
blob40b008588150796a7edd28af8b7fbe624de2f3cd
1 /*
2 Unix SMB/CIFS implementation.
3 byte range lock tester - with local filesystem support
4 Copyright (C) Andrew Tridgell 1999
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.
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.
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/>.
20 #include "includes.h"
21 #include "system/passwd.h"
22 #include "lib/events/events.h"
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;
33 #define FILENAME "\\locktest.dat"
34 #define LOCKRANGE 100
35 #define LOCKBASE 0
38 #define LOCKBASE (0x40000000 - 50)
41 #define READ_PCT 50
42 #define LOCK_PCT 25
43 #define UNLOCK_PCT 65
44 #define RANGE_MULTIPLE 1
46 #define NSERVERS 2
47 #define NCONNECTIONS 2
48 #define NUMFSTYPES 2
49 #define NFILES 2
50 #define LOCK_TIMEOUT 0
52 #define FSTYPE_SMB 0
53 #define FSTYPE_NFS 1
55 struct record {
56 char r1, r2;
57 char conn, f, fstype;
58 uint_t start, len;
59 char needed;
62 static struct record *recorded;
64 static int try_open(struct smbcli_state *c, char *nfs, int fstype, const char *fname, int flags)
66 char *path;
67 int ret;
69 switch (fstype) {
70 case FSTYPE_SMB:
71 return smbcli_open(c, fname, flags, DENY_NONE);
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;
81 return -1;
84 static bool try_close(struct smbcli_state *c, int fstype, int fd)
86 switch (fstype) {
87 case FSTYPE_SMB:
88 return smbcli_close(c, fd);
90 case FSTYPE_NFS:
91 return close(fd) == 0;
94 return false;
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)
101 struct flock lock;
103 switch (fstype) {
104 case FSTYPE_SMB:
105 return smbcli_lock(c, fd, start, len, LOCK_TIMEOUT, op);
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;
116 return false;
119 static bool try_unlock(struct smbcli_state *c, int fstype,
120 int fd, uint_t start, uint_t len)
122 struct flock lock;
124 switch (fstype) {
125 case FSTYPE_SMB:
126 return smbcli_unlock(c, fd, start, len);
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;
137 return false;
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 gensec_settings *gensec_settings,
148 struct tevent_context *ev)
150 struct smbcli_state *c;
151 char *server_n;
152 char *server;
153 char *myname;
154 static int count;
155 NTSTATUS nt_status;
157 server = talloc_strdup(mem_ctx, share+2);
158 share = strchr_m(server,'\\');
159 if (!share) return NULL;
160 *share = 0;
161 share++;
163 server_n = server;
165 if (!got_pass) {
166 char *pass = getpass("Password: ");
167 if (pass) {
168 password = talloc_strdup(mem_ctx, pass);
172 myname = talloc_asprintf(mem_ctx, "lock-%u-%u", getpid(), count++);
174 nt_status = smbcli_full_connection(NULL,
175 &c, myname, server_n, ports, share, NULL,
176 username, lp_workgroup(), password, ev,
177 options, session_options, gensec_settings);
178 if (!NT_STATUS_IS_OK(nt_status)) {
179 DEBUG(0, ("smbcli_full_connection failed with error %s\n", nt_errstr(nt_status)));
180 return NULL;
183 c->use_oplocks = use_oplocks;
185 return c;
189 static void reconnect(TALLOC_CTX *mem_ctx,
190 struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
191 char *nfs[NSERVERS],
192 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
193 const char **ports,
194 struct smbcli_options *options,
195 struct smbcli_session_options *session_options,
196 struct gensec_settings *gensec_settings,
197 struct tevent_context *ev,
198 char *share1, char *share2)
200 int server, conn, f, fstype;
201 char *share[2];
202 share[0] = share1;
203 share[1] = share2;
205 fstype = FSTYPE_SMB;
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 smbcli_close(cli[server][conn], fnum[server][fstype][conn][f]);
213 smbcli_ulogoff(cli[server][conn]);
214 talloc_free(cli[server][conn]);
216 cli[server][conn] = connect_one(mem_ctx, share[server], ports, options, session_options, gensec_settings, ev);
217 if (!cli[server][conn]) {
218 DEBUG(0,("Failed to connect to %s\n", share[server]));
219 exit(1);
226 static bool test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
227 char *nfs[NSERVERS],
228 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
229 struct record *rec)
231 uint_t conn = rec->conn;
232 uint_t f = rec->f;
233 uint_t fstype = rec->fstype;
234 uint_t start = rec->start;
235 uint_t len = rec->len;
236 uint_t r1 = rec->r1;
237 uint_t r2 = rec->r2;
238 enum brl_type op;
239 int server;
240 bool ret[NSERVERS];
242 if (r1 < READ_PCT) {
243 op = READ_LOCK;
244 } else {
245 op = WRITE_LOCK;
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);
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]);
262 if (ret[0] != ret[1]) return false;
263 } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
264 /* unset a lock */
265 for (server=0;server<NSERVERS;server++) {
266 ret[server] = try_unlock(cli[server][conn], fstype,
267 fnum[server][fstype][conn][f],
268 start, len);
270 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
271 printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u) -> %u:%u\n",
272 conn, fstype, f,
273 start, start+len-1, len,
274 ret[0], ret[1]);
276 if (!hide_unlock_fails && ret[0] != ret[1]) return false;
277 } else {
278 /* reopen the file */
279 for (server=0;server<NSERVERS;server++) {
280 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
281 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
282 O_RDWR|O_CREAT);
283 if (fnum[server][fstype][conn][f] == -1) {
284 printf("failed to reopen on share1\n");
285 return false;
288 if (showall) {
289 printf("reopen conn=%u fstype=%u f=%u\n",
290 conn, fstype, f);
293 return true;
296 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
297 char *nfs[NSERVERS],
298 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
300 int server, conn, f, fstype;
302 for (server=0;server<NSERVERS;server++)
303 for (fstype=0;fstype<NUMFSTYPES;fstype++)
304 for (conn=0;conn<NCONNECTIONS;conn++)
305 for (f=0;f<NFILES;f++) {
306 if (fnum[server][fstype][conn][f] != -1) {
307 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
308 fnum[server][fstype][conn][f] = -1;
311 for (server=0;server<NSERVERS;server++) {
312 smbcli_unlink(cli[server][0], FILENAME);
316 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
317 char *nfs[NSERVERS],
318 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
320 int server, fstype, conn, f;
322 for (server=0;server<NSERVERS;server++)
323 for (fstype=0;fstype<NUMFSTYPES;fstype++)
324 for (conn=0;conn<NCONNECTIONS;conn++)
325 for (f=0;f<NFILES;f++) {
326 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
327 O_RDWR|O_CREAT);
328 if (fnum[server][fstype][conn][f] == -1) {
329 fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
330 server, fstype, conn, f);
331 exit(1);
337 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
338 char *nfs[NSERVERS],
339 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
340 int n)
342 int i;
343 printf("testing %u ...\n", n);
344 for (i=0; i<n; i++) {
345 if (i && i % 100 == 0) {
346 printf("%u\n", i);
349 if (recorded[i].needed &&
350 !test_one(cli, nfs, fnum, &recorded[i])) return i;
352 return n;
356 /* each server has two connections open to it. Each connection has two file
357 descriptors open on the file - 8 file descriptors in total
359 we then do random locking ops in tamdem on the 4 fnums from each
360 server and ensure that the results match
362 static void test_locks(TALLOC_CTX *mem_ctx, char *share1, char *share2,
363 char *nfspath1, char *nfspath2,
364 const char **ports,
365 struct smbcli_options *options,
366 struct smbcli_session_options *session_options,
367 struct gensec_settings *gensec_settings,
368 struct tevent_context *ev)
370 struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
371 char *nfs[NSERVERS];
372 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
373 int n, i, n1;
375 nfs[0] = nfspath1;
376 nfs[1] = nfspath2;
378 ZERO_STRUCT(fnum);
379 ZERO_STRUCT(cli);
381 recorded = malloc_array_p(struct record, numops);
383 for (n=0; n<numops; n++) {
384 recorded[n].conn = random() % NCONNECTIONS;
385 recorded[n].fstype = random() % NUMFSTYPES;
386 recorded[n].f = random() % NFILES;
387 recorded[n].start = LOCKBASE + ((uint_t)random() % (LOCKRANGE-1));
388 recorded[n].len = 1 +
389 random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
390 recorded[n].start *= RANGE_MULTIPLE;
391 recorded[n].len *= RANGE_MULTIPLE;
392 recorded[n].r1 = random() % 100;
393 recorded[n].r2 = random() % 100;
394 recorded[n].needed = true;
397 reconnect(mem_ctx, cli, nfs, fnum, ports, options, session_options, gensec_settings, ev, share1, share2);
398 open_files(cli, nfs, fnum);
399 n = retest(cli, nfs, fnum, numops);
401 if (n == numops || !analyze) return;
402 n++;
404 while (1) {
405 n1 = n;
407 close_files(cli, nfs, fnum);
408 reconnect(mem_ctx, cli, nfs, fnum, ports, options, session_options, ev, share1, share2);
409 open_files(cli, nfs, fnum);
411 for (i=0;i<n-1;i++) {
412 int m;
413 recorded[i].needed = false;
415 close_files(cli, nfs, fnum);
416 open_files(cli, nfs, fnum);
418 m = retest(cli, nfs, fnum, n);
419 if (m == n) {
420 recorded[i].needed = true;
421 } else {
422 if (i < m) {
423 memmove(&recorded[i], &recorded[i+1],
424 (m-i)*sizeof(recorded[0]));
426 n = m;
427 i--;
431 if (n1 == n) break;
434 close_files(cli, nfs, fnum);
435 reconnect(mem_ctx, cli, nfs, fnum, ports, options, session_options, gensec_settings, ev, share1, share2);
436 open_files(cli, nfs, fnum);
437 showall = true;
438 n1 = retest(cli, nfs, fnum, n);
439 if (n1 != n-1) {
440 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
442 close_files(cli, nfs, fnum);
444 for (i=0;i<n;i++) {
445 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
446 recorded[i].r1,
447 recorded[i].r2,
448 recorded[i].conn,
449 recorded[i].fstype,
450 recorded[i].f,
451 recorded[i].start,
452 recorded[i].len,
453 recorded[i].needed);
459 static void usage(void)
461 printf(
462 "Usage:\n\
463 locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
464 options:\n\
465 -U user%%pass\n\
466 -s seed\n\
467 -o numops\n\
468 -u hide unlock fails\n\
469 -a (show all ops)\n\
470 -O use oplocks\n\
474 /****************************************************************************
475 main program
476 ****************************************************************************/
477 int main(int argc,char *argv[])
479 char *share1, *share2, *nfspath1, *nfspath2;
480 extern char *optarg;
481 extern int optind;
482 struct smbcli_options options;
483 struct smbcli_session_options session_options;
484 TALLOC_CTX *mem_ctx;
485 int opt;
486 char *p;
487 int seed;
488 struct loadparm_context *lp_ctx;
489 struct tevent_context *ev;
491 mem_ctx = talloc_autofree_context();
493 setlinebuf(stdout);
495 dbf = x_stderr;
497 if (argc < 5 || argv[1][0] == '-') {
498 usage();
499 exit(1);
502 share1 = argv[1];
503 share2 = argv[2];
504 nfspath1 = argv[3];
505 nfspath2 = argv[4];
507 all_string_sub(share1,"/","\\",0);
508 all_string_sub(share2,"/","\\",0);
510 setup_logging(argv[0], DEBUG_STDOUT);
512 argc -= 4;
513 argv += 4;
515 lp_ctx = loadparm_init(mem_ctx);
516 lp_load(lp_ctx, dyn_CONFIGFILE);
518 if (getenv("USER")) {
519 username = talloc_strdup(mem_ctx, getenv("USER"));
522 seed = time(NULL);
524 while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
525 switch (opt) {
526 case 'U':
527 username = talloc_strdup(mem_ctx, optarg);
528 p = strchr_m(username,'%');
529 if (p) {
530 *p = 0;
531 password = talloc_strdup(mem_ctx, p+1);
532 got_pass = 1;
534 break;
535 case 's':
536 seed = atoi(optarg);
537 break;
538 case 'u':
539 hide_unlock_fails = true;
540 break;
541 case 'o':
542 numops = atoi(optarg);
543 break;
544 case 'O':
545 use_oplocks = true;
546 break;
547 case 'a':
548 showall = true;
549 break;
550 case 'A':
551 analyze = true;
552 break;
553 case 'h':
554 usage();
555 exit(1);
556 default:
557 printf("Unknown option %c (%d)\n", (char)opt, opt);
558 exit(1);
562 argc -= optind;
563 argv += optind;
565 DEBUG(0,("seed=%u\n", seed));
566 srandom(seed);
568 ev = s4_event_context_init(mem_ctx);
570 locking_init(1);
571 lp_smbcli_options(lp_ctx, &options);
572 lp_smbcli_session_options(lp_ctx, &session_options);
573 test_locks(mem_ctx, share1, share2, nfspath1, nfspath2,
574 lp_smb_ports(lp_ctx),
575 &options, &session_options, lp_gensec_settings(lp_ctx), ev);
577 return(0);