s3-winbindd: Listen on IRPC and do forwarded DNS updates on an RODC
[Samba/wip.git] / source3 / torture / locktest2.c
blob6ab1ed4fa19e1f444e99c885139949000015f9f7
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/proto.h"
25 static fstring password;
26 static fstring username;
27 static int got_pass;
28 static int numops = 1000;
29 static bool showall;
30 static bool analyze;
31 static bool hide_unlock_fails;
32 static bool use_oplocks;
34 extern char *optarg;
35 extern int optind;
37 #define FILENAME "\\locktest.dat"
38 #define LOCKRANGE 100
39 #define LOCKBASE 0
42 #define LOCKBASE (0x40000000 - 50)
45 #define READ_PCT 50
46 #define LOCK_PCT 25
47 #define UNLOCK_PCT 65
48 #define RANGE_MULTIPLE 1
50 #define NSERVERS 2
51 #define NCONNECTIONS 2
52 #define NUMFSTYPES 2
53 #define NFILES 2
54 #define LOCK_TIMEOUT 0
56 #define FSTYPE_SMB 0
57 #define FSTYPE_NFS 1
59 struct record {
60 char r1, r2;
61 char conn, f, fstype;
62 unsigned start, len;
63 char needed;
66 static struct record *recorded;
68 static int try_open(struct cli_state *c, char *nfs, int fstype, const char *fname, int flags)
70 char *path;
72 switch (fstype) {
73 case FSTYPE_SMB:
75 uint16_t fd;
76 if (!NT_STATUS_IS_OK(cli_openx(c, fname, flags, DENY_NONE, &fd))) {
77 return -1;
79 return fd;
82 case FSTYPE_NFS:
83 if (asprintf(&path, "%s%s", nfs, fname) > 0) {
84 int ret;
85 string_replace(path,'\\', '/');
86 ret = open(path, flags, 0666);
87 SAFE_FREE(path);
88 return ret;
90 break;
93 return -1;
96 static bool try_close(struct cli_state *c, int fstype, int fd)
98 switch (fstype) {
99 case FSTYPE_SMB:
100 return NT_STATUS_IS_OK(cli_close(c, fd));
102 case FSTYPE_NFS:
103 return close(fd) == 0;
106 return False;
109 static bool try_lock(struct cli_state *c, int fstype,
110 int fd, unsigned start, unsigned len,
111 enum brl_type op)
113 struct flock lock;
115 switch (fstype) {
116 case FSTYPE_SMB:
117 return NT_STATUS_IS_OK(cli_lock32(c, fd, start, len,
118 LOCK_TIMEOUT, op));
120 case FSTYPE_NFS:
121 lock.l_type = (op==READ_LOCK) ? F_RDLCK:F_WRLCK;
122 lock.l_whence = SEEK_SET;
123 lock.l_start = start;
124 lock.l_len = len;
125 lock.l_pid = getpid();
126 return fcntl(fd,F_SETLK,&lock) == 0;
129 return False;
132 static bool try_unlock(struct cli_state *c, int fstype,
133 int fd, unsigned start, unsigned len)
135 struct flock lock;
137 switch (fstype) {
138 case FSTYPE_SMB:
139 return NT_STATUS_IS_OK(cli_unlock(c, fd, start, len));
141 case FSTYPE_NFS:
142 lock.l_type = F_UNLCK;
143 lock.l_whence = SEEK_SET;
144 lock.l_start = start;
145 lock.l_len = len;
146 lock.l_pid = getpid();
147 return fcntl(fd,F_SETLK,&lock) == 0;
150 return False;
153 static void print_brl(struct file_id id, struct server_id pid,
154 enum brl_type lock_type,
155 enum brl_flavour lock_flav,
156 br_off start, br_off size,
157 void *private_data)
159 printf("%6d %s %s %.0f:%.0f(%.0f)\n",
160 (int)procid_to_pid(&pid), file_id_string_tos(&id),
161 lock_type==READ_LOCK?"R":"W",
162 (double)start, (double)start+size-1,(double)size);
166 /*****************************************************
167 return a connection to a server
168 *******************************************************/
169 static struct cli_state *connect_one(char *share)
171 struct cli_state *c;
172 char *server_n;
173 fstring server;
174 fstring myname;
175 static int count;
176 NTSTATUS nt_status;
178 fstrcpy(server,share+2);
179 share = strchr_m(server,'\\');
180 if (!share) return NULL;
181 *share = 0;
182 share++;
184 server_n = server;
186 if (!got_pass) {
187 char pwd[256] = {0};
188 int rc;
190 rc = samba_getpass("Password: ", pwd, sizeof(pwd), false, false);
191 if (rc == 0) {
192 fstrcpy(password, pwd);
196 slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
198 nt_status = cli_full_connection(&c, myname, server_n, NULL, 0, share, "?????",
199 username, lp_workgroup(), password, 0,
200 SMB_SIGNING_DEFAULT);
202 if (!NT_STATUS_IS_OK(nt_status)) {
203 DEBUG(0, ("cli_full_connection failed with error %s\n", nt_errstr(nt_status)));
204 return NULL;
207 c->use_oplocks = use_oplocks;
209 return c;
213 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS],
214 char *nfs[NSERVERS],
215 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
216 char *share1, char *share2)
218 int server, conn, f, fstype;
219 char *share[2];
220 share[0] = share1;
221 share[1] = share2;
223 fstype = FSTYPE_SMB;
225 for (server=0;server<NSERVERS;server++)
226 for (conn=0;conn<NCONNECTIONS;conn++) {
227 if (cli[server][conn]) {
228 for (f=0;f<NFILES;f++) {
229 cli_close(cli[server][conn], fnum[server][fstype][conn][f]);
231 cli_ulogoff(cli[server][conn]);
232 cli_shutdown(cli[server][conn]);
234 cli[server][conn] = connect_one(share[server]);
235 if (!cli[server][conn]) {
236 DEBUG(0,("Failed to connect to %s\n", share[server]));
237 exit(1);
244 static bool test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
245 char *nfs[NSERVERS],
246 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
247 struct record *rec)
249 unsigned conn = rec->conn;
250 unsigned f = rec->f;
251 unsigned fstype = rec->fstype;
252 unsigned start = rec->start;
253 unsigned len = rec->len;
254 unsigned r1 = rec->r1;
255 unsigned r2 = rec->r2;
256 enum brl_type op;
257 int server;
258 bool ret[NSERVERS];
260 if (r1 < READ_PCT) {
261 op = READ_LOCK;
262 } else {
263 op = WRITE_LOCK;
266 if (r2 < LOCK_PCT) {
267 /* set a lock */
268 for (server=0;server<NSERVERS;server++) {
269 ret[server] = try_lock(cli[server][conn], fstype,
270 fnum[server][fstype][conn][f],
271 start, len, op);
273 if (showall || ret[0] != ret[1]) {
274 printf("lock conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
275 conn, fstype, f,
276 start, start+len-1, len,
277 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
278 ret[0], ret[1]);
280 if (showall) brl_forall(print_brl, NULL);
281 if (ret[0] != ret[1]) return False;
282 } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
283 /* unset a lock */
284 for (server=0;server<NSERVERS;server++) {
285 ret[server] = try_unlock(cli[server][conn], fstype,
286 fnum[server][fstype][conn][f],
287 start, len);
289 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
290 printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u) -> %u:%u\n",
291 conn, fstype, f,
292 start, start+len-1, len,
293 ret[0], ret[1]);
295 if (showall) brl_forall(print_brl, NULL);
296 if (!hide_unlock_fails && ret[0] != ret[1]) return False;
297 } else {
298 /* reopen the file */
299 for (server=0;server<NSERVERS;server++) {
300 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
301 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
302 O_RDWR|O_CREAT);
303 if (fnum[server][fstype][conn][f] == -1) {
304 printf("failed to reopen on share1\n");
305 return False;
308 if (showall) {
309 printf("reopen conn=%u fstype=%u f=%u\n",
310 conn, fstype, f);
311 brl_forall(print_brl, NULL);
314 return True;
317 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
318 char *nfs[NSERVERS],
319 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
321 int server, conn, f, fstype;
323 for (server=0;server<NSERVERS;server++)
324 for (fstype=0;fstype<NUMFSTYPES;fstype++)
325 for (conn=0;conn<NCONNECTIONS;conn++)
326 for (f=0;f<NFILES;f++) {
327 if (fnum[server][fstype][conn][f] != -1) {
328 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
329 fnum[server][fstype][conn][f] = -1;
332 for (server=0;server<NSERVERS;server++) {
333 cli_unlink(cli[server][0], FILENAME, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
337 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
338 char *nfs[NSERVERS],
339 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
341 int server, fstype, conn, f;
343 for (server=0;server<NSERVERS;server++)
344 for (fstype=0;fstype<NUMFSTYPES;fstype++)
345 for (conn=0;conn<NCONNECTIONS;conn++)
346 for (f=0;f<NFILES;f++) {
347 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
348 O_RDWR|O_CREAT);
349 if (fnum[server][fstype][conn][f] == -1) {
350 fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
351 server, fstype, conn, f);
352 exit(1);
358 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
359 char *nfs[NSERVERS],
360 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
361 int n)
363 int i;
364 printf("testing %u ...\n", n);
365 for (i=0; i<n; i++) {
366 if (i && i % 100 == 0) {
367 printf("%u\n", i);
370 if (recorded[i].needed &&
371 !test_one(cli, nfs, fnum, &recorded[i])) return i;
373 return n;
377 /* each server has two connections open to it. Each connection has two file
378 descriptors open on the file - 8 file descriptors in total
380 we then do random locking ops in tamdem on the 4 fnums from each
381 server and ensure that the results match
383 static void test_locks(char *share1, char *share2, char *nfspath1, char *nfspath2)
385 struct cli_state *cli[NSERVERS][NCONNECTIONS];
386 char *nfs[NSERVERS];
387 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
388 int n, i, n1;
390 nfs[0] = nfspath1;
391 nfs[1] = nfspath2;
393 ZERO_STRUCT(fnum);
394 ZERO_STRUCT(cli);
396 recorded = SMB_MALLOC_ARRAY(struct record, numops);
398 for (n=0; n<numops; n++) {
399 recorded[n].conn = random() % NCONNECTIONS;
400 recorded[n].fstype = random() % NUMFSTYPES;
401 recorded[n].f = random() % NFILES;
402 recorded[n].start = LOCKBASE + ((unsigned)random() % (LOCKRANGE-1));
403 recorded[n].len = 1 +
404 random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
405 recorded[n].start *= RANGE_MULTIPLE;
406 recorded[n].len *= RANGE_MULTIPLE;
407 recorded[n].r1 = random() % 100;
408 recorded[n].r2 = random() % 100;
409 recorded[n].needed = True;
412 reconnect(cli, nfs, fnum, share1, share2);
413 open_files(cli, nfs, fnum);
414 n = retest(cli, nfs, fnum, numops);
416 if (n == numops || !analyze) return;
417 n++;
419 while (1) {
420 n1 = n;
422 close_files(cli, nfs, fnum);
423 reconnect(cli, nfs, fnum, share1, share2);
424 open_files(cli, nfs, fnum);
426 for (i=0;i<n-1;i++) {
427 int m;
428 recorded[i].needed = False;
430 close_files(cli, nfs, fnum);
431 open_files(cli, nfs, fnum);
433 m = retest(cli, nfs, fnum, n);
434 if (m == n) {
435 recorded[i].needed = True;
436 } else {
437 if (i < m) {
438 memmove(&recorded[i], &recorded[i+1],
439 (m-i)*sizeof(recorded[0]));
441 n = m;
442 i--;
446 if (n1 == n) break;
449 close_files(cli, nfs, fnum);
450 reconnect(cli, nfs, fnum, share1, share2);
451 open_files(cli, nfs, fnum);
452 showall = True;
453 n1 = retest(cli, nfs, fnum, n);
454 if (n1 != n-1) {
455 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
457 close_files(cli, nfs, fnum);
459 for (i=0;i<n;i++) {
460 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
461 recorded[i].r1,
462 recorded[i].r2,
463 recorded[i].conn,
464 recorded[i].fstype,
465 recorded[i].f,
466 recorded[i].start,
467 recorded[i].len,
468 recorded[i].needed);
474 static void usage(void)
476 printf(
477 "Usage:\n\
478 locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
479 options:\n\
480 -U user%%pass\n\
481 -s seed\n\
482 -o numops\n\
483 -u hide unlock fails\n\
484 -a (show all ops)\n\
485 -O use oplocks\n\
489 /****************************************************************************
490 main program
491 ****************************************************************************/
492 int main(int argc,char *argv[])
494 char *share1, *share2, *nfspath1, *nfspath2;
495 int opt;
496 char *p;
497 int seed;
499 setlinebuf(stdout);
501 if (argc < 5 || argv[1][0] == '-') {
502 usage();
503 exit(1);
506 setup_logging(argv[0], DEBUG_STDOUT);
508 share1 = argv[1];
509 share2 = argv[2];
510 nfspath1 = argv[3];
511 nfspath2 = argv[4];
513 all_string_sub(share1,"/","\\",0);
514 all_string_sub(share2,"/","\\",0);
516 argc -= 4;
517 argv += 4;
519 lp_load_global(get_dyn_CONFIGFILE());
520 load_interfaces();
522 if (getenv("USER")) {
523 fstrcpy(username,getenv("USER"));
526 seed = time(NULL);
528 while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
529 switch (opt) {
530 case 'U':
531 fstrcpy(username,optarg);
532 p = strchr_m(username,'%');
533 if (p) {
534 *p = 0;
535 fstrcpy(password, p+1);
536 got_pass = 1;
538 break;
539 case 's':
540 seed = atoi(optarg);
541 break;
542 case 'u':
543 hide_unlock_fails = True;
544 break;
545 case 'o':
546 numops = atoi(optarg);
547 break;
548 case 'O':
549 use_oplocks = True;
550 break;
551 case 'a':
552 showall = True;
553 break;
554 case 'A':
555 analyze = True;
556 break;
557 case 'h':
558 usage();
559 exit(1);
560 default:
561 printf("Unknown option %c (%d)\n", (char)opt, opt);
562 exit(1);
566 argc -= optind;
567 argv += optind;
569 DEBUG(0,("seed=%u\n", seed));
570 srandom(seed);
572 locking_init_readonly();
573 test_locks(share1, share2, nfspath1, nfspath2);
575 return(0);