r14156: Fix coverity #114: free storage alloc'ed by sstring_sub()
[Samba/nascimento.git] / source3 / torture / locktest2.c
blob519acebe8e81413621b1b66a69139aa78ce6d00e
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 br_off start, br_off size)
140 printf("%6d %05x:%05x %s %.0f:%.0f(%.0f)\n",
141 (int)procid_to_pid(&pid), (int)dev, (int)ino,
142 lock_type==READ_LOCK?"R":"W",
143 (double)start, (double)start+size-1,(double)size);
147 /*****************************************************
148 return a connection to a server
149 *******************************************************/
150 static struct cli_state *connect_one(char *share)
152 struct cli_state *c;
153 char *server_n;
154 fstring server;
155 fstring myname;
156 static int count;
157 NTSTATUS nt_status;
159 fstrcpy(server,share+2);
160 share = strchr_m(server,'\\');
161 if (!share) return NULL;
162 *share = 0;
163 share++;
165 server_n = server;
167 if (!got_pass) {
168 char *pass = getpass("Password: ");
169 if (pass) {
170 fstrcpy(password, pass);
174 slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
176 nt_status = cli_full_connection(&c, myname, server_n, NULL, 0, share, "?????",
177 username, lp_workgroup(), password, 0,
178 Undefined, NULL);
180 if (!NT_STATUS_IS_OK(nt_status)) {
181 DEBUG(0, ("cli_full_connection failed with error %s\n", nt_errstr(nt_status)));
182 return NULL;
185 c->use_oplocks = use_oplocks;
187 return c;
191 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS],
192 char *nfs[NSERVERS],
193 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
194 char *share1, char *share2)
196 int server, conn, f, fstype;
197 char *share[2];
198 share[0] = share1;
199 share[1] = share2;
201 fstype = FSTYPE_SMB;
203 for (server=0;server<NSERVERS;server++)
204 for (conn=0;conn<NCONNECTIONS;conn++) {
205 if (cli[server][conn]) {
206 for (f=0;f<NFILES;f++) {
207 cli_close(cli[server][conn], fnum[server][fstype][conn][f]);
209 cli_ulogoff(cli[server][conn]);
210 cli_shutdown(cli[server][conn]);
212 cli[server][conn] = connect_one(share[server]);
213 if (!cli[server][conn]) {
214 DEBUG(0,("Failed to connect to %s\n", share[server]));
215 exit(1);
222 static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
223 char *nfs[NSERVERS],
224 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
225 struct record *rec)
227 unsigned conn = rec->conn;
228 unsigned f = rec->f;
229 unsigned fstype = rec->fstype;
230 unsigned start = rec->start;
231 unsigned len = rec->len;
232 unsigned r1 = rec->r1;
233 unsigned r2 = rec->r2;
234 enum brl_type op;
235 int server;
236 BOOL ret[NSERVERS];
238 if (r1 < READ_PCT) {
239 op = READ_LOCK;
240 } else {
241 op = WRITE_LOCK;
244 if (r2 < LOCK_PCT) {
245 /* set a lock */
246 for (server=0;server<NSERVERS;server++) {
247 ret[server] = try_lock(cli[server][conn], fstype,
248 fnum[server][fstype][conn][f],
249 start, len, op);
251 if (showall || ret[0] != ret[1]) {
252 printf("lock conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
253 conn, fstype, f,
254 start, start+len-1, len,
255 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
256 ret[0], ret[1]);
258 if (showall) brl_forall(print_brl);
259 if (ret[0] != ret[1]) return False;
260 } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
261 /* unset a lock */
262 for (server=0;server<NSERVERS;server++) {
263 ret[server] = try_unlock(cli[server][conn], fstype,
264 fnum[server][fstype][conn][f],
265 start, len);
267 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
268 printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u) -> %u:%u\n",
269 conn, fstype, f,
270 start, start+len-1, len,
271 ret[0], ret[1]);
273 if (showall) brl_forall(print_brl);
274 if (!hide_unlock_fails && ret[0] != ret[1]) return False;
275 } else {
276 /* reopen the file */
277 for (server=0;server<NSERVERS;server++) {
278 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
279 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
280 O_RDWR|O_CREAT);
281 if (fnum[server][fstype][conn][f] == -1) {
282 printf("failed to reopen on share1\n");
283 return False;
286 if (showall) {
287 printf("reopen conn=%u fstype=%u f=%u\n",
288 conn, fstype, f);
289 brl_forall(print_brl);
292 return True;
295 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
296 char *nfs[NSERVERS],
297 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
299 int server, conn, f, fstype;
301 for (server=0;server<NSERVERS;server++)
302 for (fstype=0;fstype<NUMFSTYPES;fstype++)
303 for (conn=0;conn<NCONNECTIONS;conn++)
304 for (f=0;f<NFILES;f++) {
305 if (fnum[server][fstype][conn][f] != -1) {
306 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
307 fnum[server][fstype][conn][f] = -1;
310 for (server=0;server<NSERVERS;server++) {
311 cli_unlink(cli[server][0], FILENAME);
315 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
316 char *nfs[NSERVERS],
317 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
319 int server, fstype, conn, f;
321 for (server=0;server<NSERVERS;server++)
322 for (fstype=0;fstype<NUMFSTYPES;fstype++)
323 for (conn=0;conn<NCONNECTIONS;conn++)
324 for (f=0;f<NFILES;f++) {
325 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
326 O_RDWR|O_CREAT);
327 if (fnum[server][fstype][conn][f] == -1) {
328 fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
329 server, fstype, conn, f);
330 exit(1);
336 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
337 char *nfs[NSERVERS],
338 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
339 int n)
341 int i;
342 printf("testing %u ...\n", n);
343 for (i=0; i<n; i++) {
344 if (i && i % 100 == 0) {
345 printf("%u\n", i);
348 if (recorded[i].needed &&
349 !test_one(cli, nfs, fnum, &recorded[i])) return i;
351 return n;
355 /* each server has two connections open to it. Each connection has two file
356 descriptors open on the file - 8 file descriptors in total
358 we then do random locking ops in tamdem on the 4 fnums from each
359 server and ensure that the results match
361 static void test_locks(char *share1, char *share2, char *nfspath1, char *nfspath2)
363 struct cli_state *cli[NSERVERS][NCONNECTIONS];
364 char *nfs[NSERVERS];
365 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
366 int n, i, n1;
368 nfs[0] = nfspath1;
369 nfs[1] = nfspath2;
371 ZERO_STRUCT(fnum);
372 ZERO_STRUCT(cli);
374 recorded = SMB_MALLOC_ARRAY(struct record, numops);
376 for (n=0; n<numops; n++) {
377 recorded[n].conn = random() % NCONNECTIONS;
378 recorded[n].fstype = random() % NUMFSTYPES;
379 recorded[n].f = random() % NFILES;
380 recorded[n].start = LOCKBASE + ((unsigned)random() % (LOCKRANGE-1));
381 recorded[n].len = 1 +
382 random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
383 recorded[n].start *= RANGE_MULTIPLE;
384 recorded[n].len *= RANGE_MULTIPLE;
385 recorded[n].r1 = random() % 100;
386 recorded[n].r2 = random() % 100;
387 recorded[n].needed = True;
390 reconnect(cli, nfs, fnum, share1, share2);
391 open_files(cli, nfs, fnum);
392 n = retest(cli, nfs, fnum, numops);
394 if (n == numops || !analyze) return;
395 n++;
397 while (1) {
398 n1 = n;
400 close_files(cli, nfs, fnum);
401 reconnect(cli, nfs, fnum, share1, share2);
402 open_files(cli, nfs, fnum);
404 for (i=0;i<n-1;i++) {
405 int m;
406 recorded[i].needed = False;
408 close_files(cli, nfs, fnum);
409 open_files(cli, nfs, fnum);
411 m = retest(cli, nfs, fnum, n);
412 if (m == n) {
413 recorded[i].needed = True;
414 } else {
415 if (i < m) {
416 memmove(&recorded[i], &recorded[i+1],
417 (m-i)*sizeof(recorded[0]));
419 n = m;
420 i--;
424 if (n1 == n) break;
427 close_files(cli, nfs, fnum);
428 reconnect(cli, nfs, fnum, share1, share2);
429 open_files(cli, nfs, fnum);
430 showall = True;
431 n1 = retest(cli, nfs, fnum, n);
432 if (n1 != n-1) {
433 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
435 close_files(cli, nfs, fnum);
437 for (i=0;i<n;i++) {
438 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
439 recorded[i].r1,
440 recorded[i].r2,
441 recorded[i].conn,
442 recorded[i].fstype,
443 recorded[i].f,
444 recorded[i].start,
445 recorded[i].len,
446 recorded[i].needed);
452 static void usage(void)
454 printf(
455 "Usage:\n\
456 locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
457 options:\n\
458 -U user%%pass\n\
459 -s seed\n\
460 -o numops\n\
461 -u hide unlock fails\n\
462 -a (show all ops)\n\
463 -O use oplocks\n\
467 /****************************************************************************
468 main program
469 ****************************************************************************/
470 int main(int argc,char *argv[])
472 char *share1, *share2, *nfspath1, *nfspath2;
473 extern char *optarg;
474 extern int optind;
475 int opt;
476 char *p;
477 int seed;
479 setlinebuf(stdout);
481 dbf = x_stderr;
483 if (argc < 5 || argv[1][0] == '-') {
484 usage();
485 exit(1);
488 share1 = argv[1];
489 share2 = argv[2];
490 nfspath1 = argv[3];
491 nfspath2 = argv[4];
493 all_string_sub(share1,"/","\\",0);
494 all_string_sub(share2,"/","\\",0);
496 setup_logging(argv[0],True);
498 argc -= 4;
499 argv += 4;
501 lp_load(dyn_CONFIGFILE,True,False,False,True);
502 load_interfaces();
504 if (getenv("USER")) {
505 fstrcpy(username,getenv("USER"));
508 seed = time(NULL);
510 while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
511 switch (opt) {
512 case 'U':
513 fstrcpy(username,optarg);
514 p = strchr_m(username,'%');
515 if (p) {
516 *p = 0;
517 fstrcpy(password, p+1);
518 got_pass = 1;
520 break;
521 case 's':
522 seed = atoi(optarg);
523 break;
524 case 'u':
525 hide_unlock_fails = True;
526 break;
527 case 'o':
528 numops = atoi(optarg);
529 break;
530 case 'O':
531 use_oplocks = True;
532 break;
533 case 'a':
534 showall = True;
535 break;
536 case 'A':
537 analyze = True;
538 break;
539 case 'h':
540 usage();
541 exit(1);
542 default:
543 printf("Unknown option %c (%d)\n", (char)opt, opt);
544 exit(1);
548 argc -= optind;
549 argv += optind;
551 DEBUG(0,("seed=%u\n", seed));
552 srandom(seed);
554 locking_init(1);
555 test_locks(share1, share2, nfspath1, nfspath2);
557 return(0);