r18865: fixed some of the most obvious NTSTATUS/WERROR mixups in Samba3. It
[Samba/nascimento.git] / source3 / torture / locktest2.c
blob5f2f2499acd3ce8350fe9e7a1a6c344dc88c3c24
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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
23 static fstring password;
24 static fstring username;
25 static int got_pass;
26 static int numops = 1000;
27 static BOOL showall;
28 static BOOL analyze;
29 static BOOL hide_unlock_fails;
30 static BOOL use_oplocks;
32 #define FILENAME "\\locktest.dat"
33 #define LOCKRANGE 100
34 #define LOCKBASE 0
37 #define LOCKBASE (0x40000000 - 50)
40 #define READ_PCT 50
41 #define LOCK_PCT 25
42 #define UNLOCK_PCT 65
43 #define RANGE_MULTIPLE 1
45 #define NSERVERS 2
46 #define NCONNECTIONS 2
47 #define NUMFSTYPES 2
48 #define NFILES 2
49 #define LOCK_TIMEOUT 0
51 #define FSTYPE_SMB 0
52 #define FSTYPE_NFS 1
54 struct record {
55 char r1, r2;
56 char conn, f, fstype;
57 unsigned start, len;
58 char needed;
61 static struct record *recorded;
63 static int try_open(struct cli_state *c, char *nfs, int fstype, const char *fname, int flags)
65 pstring path;
67 switch (fstype) {
68 case FSTYPE_SMB:
69 return cli_open(c, fname, flags, DENY_NONE);
71 case FSTYPE_NFS:
72 slprintf(path, sizeof(path), "%s%s", nfs, fname);
73 pstring_sub(path,"\\", "/");
74 return open(path, flags, 0666);
77 return -1;
80 static BOOL try_close(struct cli_state *c, int fstype, int fd)
82 switch (fstype) {
83 case FSTYPE_SMB:
84 return cli_close(c, fd);
86 case FSTYPE_NFS:
87 return close(fd) == 0;
90 return False;
93 static BOOL try_lock(struct cli_state *c, int fstype,
94 int fd, unsigned start, unsigned len,
95 enum brl_type op)
97 struct flock lock;
99 switch (fstype) {
100 case FSTYPE_SMB:
101 return cli_lock(c, fd, start, len, LOCK_TIMEOUT, op);
103 case FSTYPE_NFS:
104 lock.l_type = (op==READ_LOCK) ? F_RDLCK:F_WRLCK;
105 lock.l_whence = SEEK_SET;
106 lock.l_start = start;
107 lock.l_len = len;
108 lock.l_pid = getpid();
109 return fcntl(fd,F_SETLK,&lock) == 0;
112 return False;
115 static BOOL try_unlock(struct cli_state *c, int fstype,
116 int fd, unsigned start, unsigned len)
118 struct flock lock;
120 switch (fstype) {
121 case FSTYPE_SMB:
122 return cli_unlock(c, fd, start, len);
124 case FSTYPE_NFS:
125 lock.l_type = F_UNLCK;
126 lock.l_whence = SEEK_SET;
127 lock.l_start = start;
128 lock.l_len = len;
129 lock.l_pid = getpid();
130 return fcntl(fd,F_SETLK,&lock) == 0;
133 return False;
136 static void print_brl(SMB_DEV_T dev, SMB_INO_T ino, struct process_id pid,
137 enum brl_type lock_type,
138 enum brl_flavour lock_flav,
139 br_off start, br_off size)
141 printf("%6d %05x:%05x %s %.0f:%.0f(%.0f)\n",
142 (int)procid_to_pid(&pid), (int)dev, (int)ino,
143 lock_type==READ_LOCK?"R":"W",
144 (double)start, (double)start+size-1,(double)size);
148 /*****************************************************
149 return a connection to a server
150 *******************************************************/
151 static struct cli_state *connect_one(char *share)
153 struct cli_state *c;
154 char *server_n;
155 fstring server;
156 fstring myname;
157 static int count;
158 NTSTATUS nt_status;
160 fstrcpy(server,share+2);
161 share = strchr_m(server,'\\');
162 if (!share) return NULL;
163 *share = 0;
164 share++;
166 server_n = server;
168 if (!got_pass) {
169 char *pass = getpass("Password: ");
170 if (pass) {
171 fstrcpy(password, pass);
175 slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
177 nt_status = cli_full_connection(&c, myname, server_n, NULL, 0, share, "?????",
178 username, lp_workgroup(), password, 0,
179 Undefined, NULL);
181 if (!NT_STATUS_IS_OK(nt_status)) {
182 DEBUG(0, ("cli_full_connection failed with error %s\n", nt_errstr(nt_status)));
183 return NULL;
186 c->use_oplocks = use_oplocks;
188 return c;
192 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS],
193 char *nfs[NSERVERS],
194 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
195 char *share1, char *share2)
197 int server, conn, f, fstype;
198 char *share[2];
199 share[0] = share1;
200 share[1] = share2;
202 fstype = FSTYPE_SMB;
204 for (server=0;server<NSERVERS;server++)
205 for (conn=0;conn<NCONNECTIONS;conn++) {
206 if (cli[server][conn]) {
207 for (f=0;f<NFILES;f++) {
208 cli_close(cli[server][conn], fnum[server][fstype][conn][f]);
210 cli_ulogoff(cli[server][conn]);
211 cli_shutdown(cli[server][conn]);
213 cli[server][conn] = connect_one(share[server]);
214 if (!cli[server][conn]) {
215 DEBUG(0,("Failed to connect to %s\n", share[server]));
216 exit(1);
223 static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
224 char *nfs[NSERVERS],
225 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
226 struct record *rec)
228 unsigned conn = rec->conn;
229 unsigned f = rec->f;
230 unsigned fstype = rec->fstype;
231 unsigned start = rec->start;
232 unsigned len = rec->len;
233 unsigned r1 = rec->r1;
234 unsigned r2 = rec->r2;
235 enum brl_type op;
236 int server;
237 BOOL ret[NSERVERS];
239 if (r1 < READ_PCT) {
240 op = READ_LOCK;
241 } else {
242 op = WRITE_LOCK;
245 if (r2 < LOCK_PCT) {
246 /* set a lock */
247 for (server=0;server<NSERVERS;server++) {
248 ret[server] = try_lock(cli[server][conn], fstype,
249 fnum[server][fstype][conn][f],
250 start, len, op);
252 if (showall || ret[0] != ret[1]) {
253 printf("lock conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
254 conn, fstype, f,
255 start, start+len-1, len,
256 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
257 ret[0], ret[1]);
259 if (showall) brl_forall(print_brl);
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);
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]);
274 if (showall) brl_forall(print_brl);
275 if (!hide_unlock_fails && ret[0] != ret[1]) return False;
276 } else {
277 /* reopen the file */
278 for (server=0;server<NSERVERS;server++) {
279 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
280 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
281 O_RDWR|O_CREAT);
282 if (fnum[server][fstype][conn][f] == -1) {
283 printf("failed to reopen on share1\n");
284 return False;
287 if (showall) {
288 printf("reopen conn=%u fstype=%u f=%u\n",
289 conn, fstype, f);
290 brl_forall(print_brl);
293 return True;
296 static void close_files(struct cli_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 cli_unlink(cli[server][0], FILENAME);
316 static void open_files(struct cli_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 cli_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(char *share1, char *share2, char *nfspath1, char *nfspath2)
364 struct cli_state *cli[NSERVERS][NCONNECTIONS];
365 char *nfs[NSERVERS];
366 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
367 int n, i, n1;
369 nfs[0] = nfspath1;
370 nfs[1] = nfspath2;
372 ZERO_STRUCT(fnum);
373 ZERO_STRUCT(cli);
375 recorded = SMB_MALLOC_ARRAY(struct record, numops);
377 for (n=0; n<numops; n++) {
378 recorded[n].conn = random() % NCONNECTIONS;
379 recorded[n].fstype = random() % NUMFSTYPES;
380 recorded[n].f = random() % NFILES;
381 recorded[n].start = LOCKBASE + ((unsigned)random() % (LOCKRANGE-1));
382 recorded[n].len = 1 +
383 random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
384 recorded[n].start *= RANGE_MULTIPLE;
385 recorded[n].len *= RANGE_MULTIPLE;
386 recorded[n].r1 = random() % 100;
387 recorded[n].r2 = random() % 100;
388 recorded[n].needed = True;
391 reconnect(cli, nfs, fnum, share1, share2);
392 open_files(cli, nfs, fnum);
393 n = retest(cli, nfs, fnum, numops);
395 if (n == numops || !analyze) return;
396 n++;
398 while (1) {
399 n1 = n;
401 close_files(cli, nfs, fnum);
402 reconnect(cli, nfs, fnum, share1, share2);
403 open_files(cli, nfs, fnum);
405 for (i=0;i<n-1;i++) {
406 int m;
407 recorded[i].needed = False;
409 close_files(cli, nfs, fnum);
410 open_files(cli, nfs, fnum);
412 m = retest(cli, nfs, fnum, n);
413 if (m == n) {
414 recorded[i].needed = True;
415 } else {
416 if (i < m) {
417 memmove(&recorded[i], &recorded[i+1],
418 (m-i)*sizeof(recorded[0]));
420 n = m;
421 i--;
425 if (n1 == n) break;
428 close_files(cli, nfs, fnum);
429 reconnect(cli, nfs, fnum, share1, share2);
430 open_files(cli, nfs, fnum);
431 showall = True;
432 n1 = retest(cli, nfs, fnum, n);
433 if (n1 != n-1) {
434 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
436 close_files(cli, nfs, fnum);
438 for (i=0;i<n;i++) {
439 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
440 recorded[i].r1,
441 recorded[i].r2,
442 recorded[i].conn,
443 recorded[i].fstype,
444 recorded[i].f,
445 recorded[i].start,
446 recorded[i].len,
447 recorded[i].needed);
453 static void usage(void)
455 printf(
456 "Usage:\n\
457 locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
458 options:\n\
459 -U user%%pass\n\
460 -s seed\n\
461 -o numops\n\
462 -u hide unlock fails\n\
463 -a (show all ops)\n\
464 -O use oplocks\n\
468 /****************************************************************************
469 main program
470 ****************************************************************************/
471 int main(int argc,char *argv[])
473 char *share1, *share2, *nfspath1, *nfspath2;
474 extern char *optarg;
475 extern int optind;
476 int opt;
477 char *p;
478 int seed;
480 setlinebuf(stdout);
482 dbf = x_stderr;
484 if (argc < 5 || argv[1][0] == '-') {
485 usage();
486 exit(1);
489 share1 = argv[1];
490 share2 = argv[2];
491 nfspath1 = argv[3];
492 nfspath2 = argv[4];
494 all_string_sub(share1,"/","\\",0);
495 all_string_sub(share2,"/","\\",0);
497 setup_logging(argv[0],True);
499 argc -= 4;
500 argv += 4;
502 lp_load(dyn_CONFIGFILE,True,False,False,True);
503 load_interfaces();
505 if (getenv("USER")) {
506 fstrcpy(username,getenv("USER"));
509 seed = time(NULL);
511 while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
512 switch (opt) {
513 case 'U':
514 fstrcpy(username,optarg);
515 p = strchr_m(username,'%');
516 if (p) {
517 *p = 0;
518 fstrcpy(password, p+1);
519 got_pass = 1;
521 break;
522 case 's':
523 seed = atoi(optarg);
524 break;
525 case 'u':
526 hide_unlock_fails = True;
527 break;
528 case 'o':
529 numops = atoi(optarg);
530 break;
531 case 'O':
532 use_oplocks = True;
533 break;
534 case 'a':
535 showall = True;
536 break;
537 case 'A':
538 analyze = True;
539 break;
540 case 'h':
541 usage();
542 exit(1);
543 default:
544 printf("Unknown option %c (%d)\n", (char)opt, opt);
545 exit(1);
549 argc -= optind;
550 argv += optind;
552 DEBUG(0,("seed=%u\n", seed));
553 srandom(seed);
555 locking_init(1);
556 test_locks(share1, share2, nfspath1, nfspath2);
558 return(0);