be friendlier in smb2_deltree to some of the SMB2 implementations that
[Samba.git] / source3 / torture / locktest2.c
blob68e18439e841a834fbcf96213bcc774f0e743393
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"
22 static fstring password;
23 static fstring username;
24 static int got_pass;
25 static int numops = 1000;
26 static bool showall;
27 static bool analyze;
28 static bool hide_unlock_fails;
29 static bool use_oplocks;
31 extern char *optarg;
32 extern int optind;
34 #define FILENAME "\\locktest.dat"
35 #define LOCKRANGE 100
36 #define LOCKBASE 0
39 #define LOCKBASE (0x40000000 - 50)
42 #define READ_PCT 50
43 #define LOCK_PCT 25
44 #define UNLOCK_PCT 65
45 #define RANGE_MULTIPLE 1
47 #define NSERVERS 2
48 #define NCONNECTIONS 2
49 #define NUMFSTYPES 2
50 #define NFILES 2
51 #define LOCK_TIMEOUT 0
53 #define FSTYPE_SMB 0
54 #define FSTYPE_NFS 1
56 struct record {
57 char r1, r2;
58 char conn, f, fstype;
59 unsigned start, len;
60 char needed;
63 static struct record *recorded;
65 static int try_open(struct cli_state *c, char *nfs, int fstype, const char *fname, int flags)
67 char *path;
69 switch (fstype) {
70 case FSTYPE_SMB:
71 return cli_open(c, fname, flags, DENY_NONE);
73 case FSTYPE_NFS:
74 if (asprintf(&path, "%s%s", nfs, fname) > 0) {
75 int ret;
76 string_replace(path,'\\', '/');
77 ret = open(path, flags, 0666);
78 SAFE_FREE(path);
79 return ret;
81 break;
84 return -1;
87 static bool try_close(struct cli_state *c, int fstype, int fd)
89 switch (fstype) {
90 case FSTYPE_SMB:
91 return cli_close(c, fd);
93 case FSTYPE_NFS:
94 return close(fd) == 0;
97 return False;
100 static bool try_lock(struct cli_state *c, int fstype,
101 int fd, unsigned start, unsigned len,
102 enum brl_type op)
104 struct flock lock;
106 switch (fstype) {
107 case FSTYPE_SMB:
108 return cli_lock(c, fd, start, len, LOCK_TIMEOUT, op);
110 case FSTYPE_NFS:
111 lock.l_type = (op==READ_LOCK) ? F_RDLCK:F_WRLCK;
112 lock.l_whence = SEEK_SET;
113 lock.l_start = start;
114 lock.l_len = len;
115 lock.l_pid = getpid();
116 return fcntl(fd,F_SETLK,&lock) == 0;
119 return False;
122 static bool try_unlock(struct cli_state *c, int fstype,
123 int fd, unsigned start, unsigned len)
125 struct flock lock;
127 switch (fstype) {
128 case FSTYPE_SMB:
129 return cli_unlock(c, fd, start, len);
131 case FSTYPE_NFS:
132 lock.l_type = F_UNLCK;
133 lock.l_whence = SEEK_SET;
134 lock.l_start = start;
135 lock.l_len = len;
136 lock.l_pid = getpid();
137 return fcntl(fd,F_SETLK,&lock) == 0;
140 return False;
143 static void print_brl(struct file_id id, struct server_id pid,
144 enum brl_type lock_type,
145 enum brl_flavour lock_flav,
146 br_off start, br_off size,
147 void *private_data)
149 printf("%6d %s %s %.0f:%.0f(%.0f)\n",
150 (int)procid_to_pid(&pid), file_id_string_tos(&id),
151 lock_type==READ_LOCK?"R":"W",
152 (double)start, (double)start+size-1,(double)size);
156 /*****************************************************
157 return a connection to a server
158 *******************************************************/
159 static struct cli_state *connect_one(char *share)
161 struct cli_state *c;
162 char *server_n;
163 fstring server;
164 fstring myname;
165 static int count;
166 NTSTATUS nt_status;
168 fstrcpy(server,share+2);
169 share = strchr_m(server,'\\');
170 if (!share) return NULL;
171 *share = 0;
172 share++;
174 server_n = server;
176 if (!got_pass) {
177 char *pass = getpass("Password: ");
178 if (pass) {
179 fstrcpy(password, pass);
183 slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
185 nt_status = cli_full_connection(&c, myname, server_n, NULL, 0, share, "?????",
186 username, lp_workgroup(), password, 0,
187 Undefined, NULL);
189 if (!NT_STATUS_IS_OK(nt_status)) {
190 DEBUG(0, ("cli_full_connection failed with error %s\n", nt_errstr(nt_status)));
191 return NULL;
194 c->use_oplocks = use_oplocks;
196 return c;
200 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS],
201 char *nfs[NSERVERS],
202 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
203 char *share1, char *share2)
205 int server, conn, f, fstype;
206 char *share[2];
207 share[0] = share1;
208 share[1] = share2;
210 fstype = FSTYPE_SMB;
212 for (server=0;server<NSERVERS;server++)
213 for (conn=0;conn<NCONNECTIONS;conn++) {
214 if (cli[server][conn]) {
215 for (f=0;f<NFILES;f++) {
216 cli_close(cli[server][conn], fnum[server][fstype][conn][f]);
218 cli_ulogoff(cli[server][conn]);
219 cli_shutdown(cli[server][conn]);
221 cli[server][conn] = connect_one(share[server]);
222 if (!cli[server][conn]) {
223 DEBUG(0,("Failed to connect to %s\n", share[server]));
224 exit(1);
231 static bool test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
232 char *nfs[NSERVERS],
233 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
234 struct record *rec)
236 unsigned conn = rec->conn;
237 unsigned f = rec->f;
238 unsigned fstype = rec->fstype;
239 unsigned start = rec->start;
240 unsigned len = rec->len;
241 unsigned r1 = rec->r1;
242 unsigned r2 = rec->r2;
243 enum brl_type op;
244 int server;
245 bool ret[NSERVERS];
247 if (r1 < READ_PCT) {
248 op = READ_LOCK;
249 } else {
250 op = WRITE_LOCK;
253 if (r2 < LOCK_PCT) {
254 /* set a lock */
255 for (server=0;server<NSERVERS;server++) {
256 ret[server] = try_lock(cli[server][conn], fstype,
257 fnum[server][fstype][conn][f],
258 start, len, op);
260 if (showall || ret[0] != ret[1]) {
261 printf("lock conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
262 conn, fstype, f,
263 start, start+len-1, len,
264 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
265 ret[0], ret[1]);
267 if (showall) brl_forall(print_brl, NULL);
268 if (ret[0] != ret[1]) return False;
269 } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
270 /* unset a lock */
271 for (server=0;server<NSERVERS;server++) {
272 ret[server] = try_unlock(cli[server][conn], fstype,
273 fnum[server][fstype][conn][f],
274 start, len);
276 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
277 printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u) -> %u:%u\n",
278 conn, fstype, f,
279 start, start+len-1, len,
280 ret[0], ret[1]);
282 if (showall) brl_forall(print_brl, NULL);
283 if (!hide_unlock_fails && ret[0] != ret[1]) return False;
284 } else {
285 /* reopen the file */
286 for (server=0;server<NSERVERS;server++) {
287 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
288 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
289 O_RDWR|O_CREAT);
290 if (fnum[server][fstype][conn][f] == -1) {
291 printf("failed to reopen on share1\n");
292 return False;
295 if (showall) {
296 printf("reopen conn=%u fstype=%u f=%u\n",
297 conn, fstype, f);
298 brl_forall(print_brl, NULL);
301 return True;
304 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
305 char *nfs[NSERVERS],
306 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
308 int server, conn, f, fstype;
310 for (server=0;server<NSERVERS;server++)
311 for (fstype=0;fstype<NUMFSTYPES;fstype++)
312 for (conn=0;conn<NCONNECTIONS;conn++)
313 for (f=0;f<NFILES;f++) {
314 if (fnum[server][fstype][conn][f] != -1) {
315 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
316 fnum[server][fstype][conn][f] = -1;
319 for (server=0;server<NSERVERS;server++) {
320 cli_unlink(cli[server][0], FILENAME);
324 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
325 char *nfs[NSERVERS],
326 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
328 int server, fstype, conn, f;
330 for (server=0;server<NSERVERS;server++)
331 for (fstype=0;fstype<NUMFSTYPES;fstype++)
332 for (conn=0;conn<NCONNECTIONS;conn++)
333 for (f=0;f<NFILES;f++) {
334 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
335 O_RDWR|O_CREAT);
336 if (fnum[server][fstype][conn][f] == -1) {
337 fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
338 server, fstype, conn, f);
339 exit(1);
345 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
346 char *nfs[NSERVERS],
347 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
348 int n)
350 int i;
351 printf("testing %u ...\n", n);
352 for (i=0; i<n; i++) {
353 if (i && i % 100 == 0) {
354 printf("%u\n", i);
357 if (recorded[i].needed &&
358 !test_one(cli, nfs, fnum, &recorded[i])) return i;
360 return n;
364 /* each server has two connections open to it. Each connection has two file
365 descriptors open on the file - 8 file descriptors in total
367 we then do random locking ops in tamdem on the 4 fnums from each
368 server and ensure that the results match
370 static void test_locks(char *share1, char *share2, char *nfspath1, char *nfspath2)
372 struct cli_state *cli[NSERVERS][NCONNECTIONS];
373 char *nfs[NSERVERS];
374 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
375 int n, i, n1;
377 nfs[0] = nfspath1;
378 nfs[1] = nfspath2;
380 ZERO_STRUCT(fnum);
381 ZERO_STRUCT(cli);
383 recorded = SMB_MALLOC_ARRAY(struct record, numops);
385 for (n=0; n<numops; n++) {
386 recorded[n].conn = random() % NCONNECTIONS;
387 recorded[n].fstype = random() % NUMFSTYPES;
388 recorded[n].f = random() % NFILES;
389 recorded[n].start = LOCKBASE + ((unsigned)random() % (LOCKRANGE-1));
390 recorded[n].len = 1 +
391 random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
392 recorded[n].start *= RANGE_MULTIPLE;
393 recorded[n].len *= RANGE_MULTIPLE;
394 recorded[n].r1 = random() % 100;
395 recorded[n].r2 = random() % 100;
396 recorded[n].needed = True;
399 reconnect(cli, nfs, fnum, share1, share2);
400 open_files(cli, nfs, fnum);
401 n = retest(cli, nfs, fnum, numops);
403 if (n == numops || !analyze) return;
404 n++;
406 while (1) {
407 n1 = n;
409 close_files(cli, nfs, fnum);
410 reconnect(cli, nfs, fnum, share1, share2);
411 open_files(cli, nfs, fnum);
413 for (i=0;i<n-1;i++) {
414 int m;
415 recorded[i].needed = False;
417 close_files(cli, nfs, fnum);
418 open_files(cli, nfs, fnum);
420 m = retest(cli, nfs, fnum, n);
421 if (m == n) {
422 recorded[i].needed = True;
423 } else {
424 if (i < m) {
425 memmove(&recorded[i], &recorded[i+1],
426 (m-i)*sizeof(recorded[0]));
428 n = m;
429 i--;
433 if (n1 == n) break;
436 close_files(cli, nfs, fnum);
437 reconnect(cli, nfs, fnum, share1, share2);
438 open_files(cli, nfs, fnum);
439 showall = True;
440 n1 = retest(cli, nfs, fnum, n);
441 if (n1 != n-1) {
442 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
444 close_files(cli, nfs, fnum);
446 for (i=0;i<n;i++) {
447 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
448 recorded[i].r1,
449 recorded[i].r2,
450 recorded[i].conn,
451 recorded[i].fstype,
452 recorded[i].f,
453 recorded[i].start,
454 recorded[i].len,
455 recorded[i].needed);
461 static void usage(void)
463 printf(
464 "Usage:\n\
465 locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
466 options:\n\
467 -U user%%pass\n\
468 -s seed\n\
469 -o numops\n\
470 -u hide unlock fails\n\
471 -a (show all ops)\n\
472 -O use oplocks\n\
476 /****************************************************************************
477 main program
478 ****************************************************************************/
479 int main(int argc,char *argv[])
481 char *share1, *share2, *nfspath1, *nfspath2;
482 int opt;
483 char *p;
484 int seed;
486 setlinebuf(stdout);
488 dbf = x_stderr;
490 if (argc < 5 || argv[1][0] == '-') {
491 usage();
492 exit(1);
495 share1 = argv[1];
496 share2 = argv[2];
497 nfspath1 = argv[3];
498 nfspath2 = argv[4];
500 all_string_sub(share1,"/","\\",0);
501 all_string_sub(share2,"/","\\",0);
503 setup_logging(argv[0],True);
505 argc -= 4;
506 argv += 4;
508 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
509 load_interfaces();
511 if (getenv("USER")) {
512 fstrcpy(username,getenv("USER"));
515 seed = time(NULL);
517 while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
518 switch (opt) {
519 case 'U':
520 fstrcpy(username,optarg);
521 p = strchr_m(username,'%');
522 if (p) {
523 *p = 0;
524 fstrcpy(password, p+1);
525 got_pass = 1;
527 break;
528 case 's':
529 seed = atoi(optarg);
530 break;
531 case 'u':
532 hide_unlock_fails = True;
533 break;
534 case 'o':
535 numops = atoi(optarg);
536 break;
537 case 'O':
538 use_oplocks = True;
539 break;
540 case 'a':
541 showall = True;
542 break;
543 case 'A':
544 analyze = True;
545 break;
546 case 'h':
547 usage();
548 exit(1);
549 default:
550 printf("Unknown option %c (%d)\n", (char)opt, opt);
551 exit(1);
555 argc -= optind;
556 argv += optind;
558 DEBUG(0,("seed=%u\n", seed));
559 srandom(seed);
561 locking_init_readonly();
562 test_locks(share1, share2, nfspath1, nfspath2);
564 return(0);