ctdb-server: Remove duplicate logic
[samba4-gss.git] / source3 / torture / locktest2.c
blobcb01648aa587fb0e2209582b823814c051139e20
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 "libsmb/libsmb.h"
22 #include "system/filesys.h"
23 #include "locking/share_mode_lock.h"
24 #include "locking/proto.h"
25 #include "lib/util/string_wrappers.h"
27 static fstring password;
28 static fstring username;
29 static int got_pass;
30 static int numops = 1000;
31 static bool showall;
32 static bool analyze;
33 static bool hide_unlock_fails;
34 static bool use_oplocks;
36 extern char *optarg;
37 extern int optind;
39 #define FILENAME "\\locktest.dat"
40 #define LOCKRANGE 100
41 #define LOCKBASE 0
44 #define LOCKBASE (0x40000000 - 50)
47 #define READ_PCT 50
48 #define LOCK_PCT 25
49 #define UNLOCK_PCT 65
50 #define RANGE_MULTIPLE 1
52 #define NSERVERS 2
53 #define NCONNECTIONS 2
54 #define NUMFSTYPES 2
55 #define NFILES 2
56 #define LOCK_TIMEOUT 0
58 #define FSTYPE_SMB 0
59 #define FSTYPE_NFS 1
61 struct record {
62 char r1, r2;
63 char conn, f, fstype;
64 unsigned start, len;
65 char needed;
68 static struct record *recorded;
70 static int try_open(struct cli_state *c, char *nfs, int fstype, const char *fname, int flags)
72 char *path;
74 switch (fstype) {
75 case FSTYPE_SMB:
77 uint16_t fd;
78 if (!NT_STATUS_IS_OK(cli_openx(c, fname, flags, DENY_NONE, &fd))) {
79 return -1;
81 return fd;
84 case FSTYPE_NFS:
85 if (asprintf(&path, "%s%s", nfs, fname) > 0) {
86 int ret;
87 string_replace(path,'\\', '/');
88 ret = open(path, flags, 0666);
89 SAFE_FREE(path);
90 return ret;
92 break;
95 return -1;
98 static bool try_close(struct cli_state *c, int fstype, int fd)
100 switch (fstype) {
101 case FSTYPE_SMB:
102 return NT_STATUS_IS_OK(cli_close(c, fd));
104 case FSTYPE_NFS:
105 return close(fd) == 0;
108 return False;
111 static bool try_lock(struct cli_state *c, int fstype,
112 int fd, unsigned start, unsigned len,
113 enum brl_type op)
115 struct flock lock;
117 switch (fstype) {
118 case FSTYPE_SMB:
119 return NT_STATUS_IS_OK(cli_lock32(c, fd, start, len,
120 LOCK_TIMEOUT, op));
122 case FSTYPE_NFS:
123 lock.l_type = (op==READ_LOCK) ? F_RDLCK:F_WRLCK;
124 lock.l_whence = SEEK_SET;
125 lock.l_start = start;
126 lock.l_len = len;
127 lock.l_pid = getpid();
128 return fcntl(fd,F_SETLK,&lock) == 0;
131 return False;
134 static bool try_unlock(struct cli_state *c, int fstype,
135 int fd, unsigned start, unsigned len)
137 struct flock lock;
139 switch (fstype) {
140 case FSTYPE_SMB:
141 return NT_STATUS_IS_OK(cli_unlock(c, fd, start, len));
143 case FSTYPE_NFS:
144 lock.l_type = F_UNLCK;
145 lock.l_whence = SEEK_SET;
146 lock.l_start = start;
147 lock.l_len = len;
148 lock.l_pid = getpid();
149 return fcntl(fd,F_SETLK,&lock) == 0;
152 return False;
155 static void print_brl(struct file_id id, struct server_id pid,
156 enum brl_type lock_type,
157 enum brl_flavour lock_flav,
158 br_off start, br_off size,
159 void *private_data)
161 struct file_id_buf idbuf;
163 printf("%6d %s %s %.0f:%.0f(%.0f)\n",
164 (int)procid_to_pid(&pid),
165 file_id_str_buf(id, &idbuf),
166 lock_type==READ_LOCK?"R":"W",
167 (double)start, (double)start+size-1,(double)size);
171 /*****************************************************
172 return a connection to a server
173 *******************************************************/
174 static struct cli_state *connect_one(char *share)
176 struct cli_state *c;
177 char *server_n;
178 fstring server;
179 fstring myname;
180 static int count;
181 NTSTATUS nt_status;
182 struct cli_credentials *creds = NULL;
184 fstrcpy(server,share+2);
185 share = strchr_m(server,'\\');
186 if (!share) return NULL;
187 *share = 0;
188 share++;
190 server_n = server;
192 if (!got_pass) {
193 char pwd[256] = {0};
194 int rc;
196 rc = samba_getpass("Password: ", pwd, sizeof(pwd), false, false);
197 if (rc == 0) {
198 fstrcpy(password, pwd);
202 creds = cli_session_creds_init(NULL,
203 username,
204 lp_workgroup(),
205 NULL, /* realm (use default) */
206 password,
207 false, /* use_kerberos */
208 false, /* fallback_after_kerberos */
209 false, /* use_ccache */
210 false); /* pw_nt_hash */
211 if (creds == NULL) {
212 DEBUG(0, ("cli_session_creds_init failed\n"));
213 return NULL;
216 slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
218 nt_status = cli_full_connection_creds(NULL,
220 myname,
221 server_n,
222 NULL,
224 share,
225 "?????",
226 creds,
228 TALLOC_FREE(creds);
229 if (!NT_STATUS_IS_OK(nt_status)) {
230 DEBUG(0, ("cli_full_connection failed with error %s\n", nt_errstr(nt_status)));
231 return NULL;
234 c->use_oplocks = use_oplocks;
236 return c;
240 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS],
241 char *nfs[NSERVERS],
242 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
243 char *share1, char *share2)
245 int server, conn, f, fstype;
246 char *share[2];
247 share[0] = share1;
248 share[1] = share2;
250 fstype = FSTYPE_SMB;
252 for (server=0;server<NSERVERS;server++)
253 for (conn=0;conn<NCONNECTIONS;conn++) {
254 if (cli[server][conn]) {
255 for (f=0;f<NFILES;f++) {
256 cli_close(cli[server][conn], fnum[server][fstype][conn][f]);
258 cli_ulogoff(cli[server][conn]);
259 cli_shutdown(cli[server][conn]);
261 cli[server][conn] = connect_one(share[server]);
262 if (!cli[server][conn]) {
263 DEBUG(0,("Failed to connect to %s\n", share[server]));
264 exit(1);
271 static bool test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
272 char *nfs[NSERVERS],
273 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
274 struct record *rec)
276 unsigned conn = rec->conn;
277 unsigned f = rec->f;
278 unsigned fstype = rec->fstype;
279 unsigned start = rec->start;
280 unsigned len = rec->len;
281 unsigned r1 = rec->r1;
282 unsigned r2 = rec->r2;
283 enum brl_type op;
284 int server;
285 bool ret[NSERVERS];
287 if (r1 < READ_PCT) {
288 op = READ_LOCK;
289 } else {
290 op = WRITE_LOCK;
293 if (fstype >= NUMFSTYPES) {
294 return false;
297 if (r2 < LOCK_PCT) {
298 /* set a lock */
299 for (server=0;server<NSERVERS;server++) {
300 ret[server] = try_lock(cli[server][conn], fstype,
301 fnum[server][fstype][conn][f],
302 start, len, op);
304 if (showall || ret[0] != ret[1]) {
305 printf("lock conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
306 conn, fstype, f,
307 start, start+len-1, len,
308 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
309 ret[0], ret[1]);
311 if (showall) brl_forall(print_brl, NULL);
312 if (ret[0] != ret[1]) return False;
313 } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
314 /* unset a lock */
315 for (server=0;server<NSERVERS;server++) {
316 ret[server] = try_unlock(cli[server][conn], fstype,
317 fnum[server][fstype][conn][f],
318 start, len);
320 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
321 printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u) -> %u:%u\n",
322 conn, fstype, f,
323 start, start+len-1, len,
324 ret[0], ret[1]);
326 if (showall) brl_forall(print_brl, NULL);
327 if (!hide_unlock_fails && ret[0] != ret[1]) return False;
328 } else {
329 /* reopen the file */
330 for (server=0;server<NSERVERS;server++) {
331 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
332 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
333 O_RDWR|O_CREAT);
334 if (fnum[server][fstype][conn][f] == -1) {
335 printf("failed to reopen on share1\n");
336 return False;
339 if (showall) {
340 printf("reopen conn=%u fstype=%u f=%u\n",
341 conn, fstype, f);
342 brl_forall(print_brl, NULL);
345 return True;
348 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
349 char *nfs[NSERVERS],
350 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
352 int server, conn, f, fstype;
354 for (server=0;server<NSERVERS;server++)
355 for (fstype=0;fstype<NUMFSTYPES;fstype++)
356 for (conn=0;conn<NCONNECTIONS;conn++)
357 for (f=0;f<NFILES;f++) {
358 if (fnum[server][fstype][conn][f] != -1) {
359 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
360 fnum[server][fstype][conn][f] = -1;
363 for (server=0;server<NSERVERS;server++) {
364 cli_unlink(cli[server][0], FILENAME, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
368 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
369 char *nfs[NSERVERS],
370 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
372 int server, fstype, conn, f;
374 for (server=0;server<NSERVERS;server++)
375 for (fstype=0;fstype<NUMFSTYPES;fstype++)
376 for (conn=0;conn<NCONNECTIONS;conn++)
377 for (f=0;f<NFILES;f++) {
378 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
379 O_RDWR|O_CREAT);
380 if (fnum[server][fstype][conn][f] == -1) {
381 fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
382 server, fstype, conn, f);
383 exit(1);
389 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
390 char *nfs[NSERVERS],
391 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
392 int n)
394 int i;
395 printf("testing %u ...\n", n);
396 for (i=0; i<n; i++) {
397 if (i && i % 100 == 0) {
398 printf("%u\n", i);
401 if (recorded[i].needed &&
402 !test_one(cli, nfs, fnum, &recorded[i])) return i;
404 return n;
408 /* each server has two connections open to it. Each connection has two file
409 descriptors open on the file - 8 file descriptors in total
411 we then do random locking ops in tamdem on the 4 fnums from each
412 server and ensure that the results match
414 static void test_locks(char *share1, char *share2, char *nfspath1, char *nfspath2)
416 struct cli_state *cli[NSERVERS][NCONNECTIONS];
417 char *nfs[NSERVERS];
418 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
419 int n, i, n1;
421 nfs[0] = nfspath1;
422 nfs[1] = nfspath2;
424 ZERO_STRUCT(fnum);
425 ZERO_STRUCT(cli);
427 recorded = SMB_MALLOC_ARRAY(struct record, numops);
429 for (n=0; n<numops; n++) {
430 recorded[n].conn = random() % NCONNECTIONS;
431 recorded[n].fstype = random() % NUMFSTYPES;
432 recorded[n].f = random() % NFILES;
433 recorded[n].start = LOCKBASE + ((unsigned)random() % (LOCKRANGE-1));
434 recorded[n].len = 1 +
435 random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
436 recorded[n].start *= RANGE_MULTIPLE;
437 recorded[n].len *= RANGE_MULTIPLE;
438 recorded[n].r1 = random() % 100;
439 recorded[n].r2 = random() % 100;
440 recorded[n].needed = True;
443 reconnect(cli, nfs, fnum, share1, share2);
444 open_files(cli, nfs, fnum);
445 n = retest(cli, nfs, fnum, numops);
447 if (n == numops || !analyze) return;
448 n++;
450 while (1) {
451 n1 = n;
453 close_files(cli, nfs, fnum);
454 reconnect(cli, nfs, fnum, share1, share2);
455 open_files(cli, nfs, fnum);
457 for (i=0;i<n-1;i++) {
458 int m;
459 recorded[i].needed = False;
461 close_files(cli, nfs, fnum);
462 open_files(cli, nfs, fnum);
464 m = retest(cli, nfs, fnum, n);
465 if (m == n) {
466 recorded[i].needed = True;
467 } else {
468 if (i < m) {
469 memmove(&recorded[i], &recorded[i+1],
470 (m-i)*sizeof(recorded[0]));
472 n = m;
473 i--;
477 if (n1 == n) break;
480 close_files(cli, nfs, fnum);
481 reconnect(cli, nfs, fnum, share1, share2);
482 open_files(cli, nfs, fnum);
483 showall = True;
484 n1 = retest(cli, nfs, fnum, n);
485 if (n1 != n-1) {
486 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
488 close_files(cli, nfs, fnum);
490 for (i=0;i<n;i++) {
491 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
492 recorded[i].r1,
493 recorded[i].r2,
494 recorded[i].conn,
495 recorded[i].fstype,
496 recorded[i].f,
497 recorded[i].start,
498 recorded[i].len,
499 recorded[i].needed);
505 static void usage(void)
507 printf(
508 "Usage:\n\
509 locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
510 options:\n\
511 -U user%%pass\n\
512 -s seed\n\
513 -o numops\n\
514 -u hide unlock fails\n\
515 -a (show all ops)\n\
516 -O use oplocks\n\
520 /****************************************************************************
521 main program
522 ****************************************************************************/
523 int main(int argc,char *argv[])
525 char *share1, *share2, *nfspath1, *nfspath2;
526 int opt;
527 char *p;
528 int seed;
530 setlinebuf(stdout);
532 if (argc < 5 || argv[1][0] == '-') {
533 usage();
534 exit(1);
537 setup_logging(argv[0], DEBUG_STDOUT);
539 share1 = argv[1];
540 share2 = argv[2];
541 nfspath1 = argv[3];
542 nfspath2 = argv[4];
544 all_string_sub(share1,"/","\\",0);
545 all_string_sub(share2,"/","\\",0);
547 argc -= 4;
548 argv += 4;
550 lp_load_global(get_dyn_CONFIGFILE());
551 load_interfaces();
553 if (getenv("USER")) {
554 fstrcpy(username,getenv("USER"));
557 seed = time(NULL);
559 while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
560 switch (opt) {
561 case 'U':
562 fstrcpy(username,optarg);
563 p = strchr_m(username,'%');
564 if (p) {
565 *p = 0;
566 fstrcpy(password, p+1);
567 got_pass = 1;
569 break;
570 case 's':
571 seed = atoi(optarg);
572 break;
573 case 'u':
574 hide_unlock_fails = True;
575 break;
576 case 'o':
577 numops = atoi(optarg);
578 break;
579 case 'O':
580 use_oplocks = True;
581 break;
582 case 'a':
583 showall = True;
584 break;
585 case 'A':
586 analyze = True;
587 break;
588 case 'h':
589 usage();
590 exit(1);
591 default:
592 printf("Unknown option %c (%d)\n", (char)opt, opt);
593 exit(1);
597 argc -= optind;
598 argv += optind;
600 DEBUG(0,("seed=%u\n", seed));
601 srandom(seed);
603 locking_init_readonly();
604 test_locks(share1, share2, nfspath1, nfspath2);
606 return(0);