selftest: use a separate var for printing out sub parts of lines with \r
[Samba/ekacnet.git] / source4 / torture / locktest2.c
blob0fe37253859fea62ab9e23471e5d62b1541c5c2a
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 "system/passwd.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 uint_t start, len;
58 char needed;
61 static struct record *recorded;
63 static int try_open(struct smbcli_state *c, char *nfs, int fstype, const char *fname, int flags)
65 pstring path;
67 switch (fstype) {
68 case FSTYPE_SMB:
69 return smbcli_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 smbcli_state *c, int fstype, int fd)
82 switch (fstype) {
83 case FSTYPE_SMB:
84 return smbcli_close(c, fd);
86 case FSTYPE_NFS:
87 return close(fd) == 0;
90 return false;
93 static bool try_lock(struct smbcli_state *c, int fstype,
94 int fd, uint_t start, uint_t len,
95 enum brl_type op)
97 struct flock lock;
99 switch (fstype) {
100 case FSTYPE_SMB:
101 return smbcli_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 smbcli_state *c, int fstype,
116 int fd, uint_t start, uint_t len)
118 struct flock lock;
120 switch (fstype) {
121 case FSTYPE_SMB:
122 return smbcli_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 /*****************************************************
137 return a connection to a server
138 *******************************************************/
139 static struct smbcli_state *connect_one(char *share, const char **ports,
140 struct smb_options *options)
142 struct smbcli_state *c;
143 char *server_n;
144 fstring server;
145 fstring myname;
146 static int count;
147 NTSTATUS nt_status;
149 fstrcpy(server,share+2);
150 share = strchr_m(server,'\\');
151 if (!share) return NULL;
152 *share = 0;
153 share++;
155 server_n = server;
157 if (!got_pass) {
158 char *pass = getpass("Password: ");
159 if (pass) {
160 fstrcpy(password, pass);
164 slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), count++);
166 nt_status = smbcli_full_connection(NULL,
167 &c, myname, server_n, ports, share, NULL,
168 username, lp_workgroup(), password, NULL,
169 options);
170 if (!NT_STATUS_IS_OK(nt_status)) {
171 DEBUG(0, ("smbcli_full_connection failed with error %s\n", nt_errstr(nt_status)));
172 return NULL;
175 c->use_oplocks = use_oplocks;
177 return c;
181 static void reconnect(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
182 char *nfs[NSERVERS],
183 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
184 const char **ports,
185 struct smbcli_options *options,
186 char *share1, char *share2)
188 int server, conn, f, fstype;
189 char *share[2];
190 share[0] = share1;
191 share[1] = share2;
193 fstype = FSTYPE_SMB;
195 for (server=0;server<NSERVERS;server++)
196 for (conn=0;conn<NCONNECTIONS;conn++) {
197 if (cli[server][conn]) {
198 for (f=0;f<NFILES;f++) {
199 smbcli_close(cli[server][conn], fnum[server][fstype][conn][f]);
201 smbcli_ulogoff(cli[server][conn]);
202 talloc_free(cli[server][conn]);
204 cli[server][conn] = connect_one(share[server], ports, options);
205 if (!cli[server][conn]) {
206 DEBUG(0,("Failed to connect to %s\n", share[server]));
207 exit(1);
214 static bool test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
215 char *nfs[NSERVERS],
216 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
217 struct record *rec)
219 uint_t conn = rec->conn;
220 uint_t f = rec->f;
221 uint_t fstype = rec->fstype;
222 uint_t start = rec->start;
223 uint_t len = rec->len;
224 uint_t r1 = rec->r1;
225 uint_t r2 = rec->r2;
226 enum brl_type op;
227 int server;
228 bool ret[NSERVERS];
230 if (r1 < READ_PCT) {
231 op = READ_LOCK;
232 } else {
233 op = WRITE_LOCK;
236 if (r2 < LOCK_PCT) {
237 /* set a lock */
238 for (server=0;server<NSERVERS;server++) {
239 ret[server] = try_lock(cli[server][conn], fstype,
240 fnum[server][fstype][conn][f],
241 start, len, op);
243 if (showall || ret[0] != ret[1]) {
244 printf("lock conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
245 conn, fstype, f,
246 start, start+len-1, len,
247 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
248 ret[0], ret[1]);
250 if (ret[0] != ret[1]) return false;
251 } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
252 /* unset a lock */
253 for (server=0;server<NSERVERS;server++) {
254 ret[server] = try_unlock(cli[server][conn], fstype,
255 fnum[server][fstype][conn][f],
256 start, len);
258 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
259 printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u) -> %u:%u\n",
260 conn, fstype, f,
261 start, start+len-1, len,
262 ret[0], ret[1]);
264 if (!hide_unlock_fails && ret[0] != ret[1]) return false;
265 } else {
266 /* reopen the file */
267 for (server=0;server<NSERVERS;server++) {
268 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
269 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
270 O_RDWR|O_CREAT);
271 if (fnum[server][fstype][conn][f] == -1) {
272 printf("failed to reopen on share1\n");
273 return false;
276 if (showall) {
277 printf("reopen conn=%u fstype=%u f=%u\n",
278 conn, fstype, f);
281 return true;
284 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
285 char *nfs[NSERVERS],
286 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
288 int server, conn, f, fstype;
290 for (server=0;server<NSERVERS;server++)
291 for (fstype=0;fstype<NUMFSTYPES;fstype++)
292 for (conn=0;conn<NCONNECTIONS;conn++)
293 for (f=0;f<NFILES;f++) {
294 if (fnum[server][fstype][conn][f] != -1) {
295 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
296 fnum[server][fstype][conn][f] = -1;
299 for (server=0;server<NSERVERS;server++) {
300 smbcli_unlink(cli[server][0], FILENAME);
304 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
305 char *nfs[NSERVERS],
306 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
308 int server, fstype, conn, f;
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 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
315 O_RDWR|O_CREAT);
316 if (fnum[server][fstype][conn][f] == -1) {
317 fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
318 server, fstype, conn, f);
319 exit(1);
325 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
326 char *nfs[NSERVERS],
327 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
328 int n)
330 int i;
331 printf("testing %u ...\n", n);
332 for (i=0; i<n; i++) {
333 if (i && i % 100 == 0) {
334 printf("%u\n", i);
337 if (recorded[i].needed &&
338 !test_one(cli, nfs, fnum, &recorded[i])) return i;
340 return n;
344 /* each server has two connections open to it. Each connection has two file
345 descriptors open on the file - 8 file descriptors in total
347 we then do random locking ops in tamdem on the 4 fnums from each
348 server and ensure that the results match
350 static void test_locks(char *share1, char *share2, char *nfspath1, char *nfspath2, const char **ports, struct smbcli_options *options)
352 struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
353 char *nfs[NSERVERS];
354 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
355 int n, i, n1;
357 nfs[0] = nfspath1;
358 nfs[1] = nfspath2;
360 ZERO_STRUCT(fnum);
361 ZERO_STRUCT(cli);
363 recorded = malloc_array_p(struct record, numops);
365 for (n=0; n<numops; n++) {
366 recorded[n].conn = random() % NCONNECTIONS;
367 recorded[n].fstype = random() % NUMFSTYPES;
368 recorded[n].f = random() % NFILES;
369 recorded[n].start = LOCKBASE + ((uint_t)random() % (LOCKRANGE-1));
370 recorded[n].len = 1 +
371 random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
372 recorded[n].start *= RANGE_MULTIPLE;
373 recorded[n].len *= RANGE_MULTIPLE;
374 recorded[n].r1 = random() % 100;
375 recorded[n].r2 = random() % 100;
376 recorded[n].needed = true;
379 reconnect(cli, nfs, fnum, ports, options, share1, share2);
380 open_files(cli, nfs, fnum);
381 n = retest(cli, nfs, fnum, numops);
383 if (n == numops || !analyze) return;
384 n++;
386 while (1) {
387 n1 = n;
389 close_files(cli, nfs, fnum);
390 reconnect(cli, nfs, fnum, ports, options, share1, share2);
391 open_files(cli, nfs, fnum);
393 for (i=0;i<n-1;i++) {
394 int m;
395 recorded[i].needed = false;
397 close_files(cli, nfs, fnum);
398 open_files(cli, nfs, fnum);
400 m = retest(cli, nfs, fnum, n);
401 if (m == n) {
402 recorded[i].needed = true;
403 } else {
404 if (i < m) {
405 memmove(&recorded[i], &recorded[i+1],
406 (m-i)*sizeof(recorded[0]));
408 n = m;
409 i--;
413 if (n1 == n) break;
416 close_files(cli, nfs, fnum);
417 reconnect(cli, nfs, fnum, ports, options, share1, share2);
418 open_files(cli, nfs, fnum);
419 showall = true;
420 n1 = retest(cli, nfs, fnum, n);
421 if (n1 != n-1) {
422 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
424 close_files(cli, nfs, fnum);
426 for (i=0;i<n;i++) {
427 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
428 recorded[i].r1,
429 recorded[i].r2,
430 recorded[i].conn,
431 recorded[i].fstype,
432 recorded[i].f,
433 recorded[i].start,
434 recorded[i].len,
435 recorded[i].needed);
441 static void usage(void)
443 printf(
444 "Usage:\n\
445 locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
446 options:\n\
447 -U user%%pass\n\
448 -s seed\n\
449 -o numops\n\
450 -u hide unlock fails\n\
451 -a (show all ops)\n\
452 -O use oplocks\n\
456 /****************************************************************************
457 main program
458 ****************************************************************************/
459 int main(int argc,char *argv[])
461 char *share1, *share2, *nfspath1, *nfspath2;
462 extern char *optarg;
463 extern int optind;
464 struct smbcli_options options;
465 int opt;
466 char *p;
467 int seed;
468 struct loadparm_context *lp_ctx;
470 setlinebuf(stdout);
472 dbf = x_stderr;
474 if (argc < 5 || argv[1][0] == '-') {
475 usage();
476 exit(1);
479 share1 = argv[1];
480 share2 = argv[2];
481 nfspath1 = argv[3];
482 nfspath2 = argv[4];
484 all_string_sub(share1,"/","\\",0);
485 all_string_sub(share2,"/","\\",0);
487 setup_logging(argv[0], DEBUG_STDOUT);
489 argc -= 4;
490 argv += 4;
492 lp_ctx = loadparm_init(talloc_autofree_context());
493 lp_load(lp_ctx, dyn_CONFIGFILE);
495 if (getenv("USER")) {
496 fstrcpy(username,getenv("USER"));
499 seed = time(NULL);
501 while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
502 switch (opt) {
503 case 'U':
504 fstrcpy(username,optarg);
505 p = strchr_m(username,'%');
506 if (p) {
507 *p = 0;
508 fstrcpy(password, p+1);
509 got_pass = 1;
511 break;
512 case 's':
513 seed = atoi(optarg);
514 break;
515 case 'u':
516 hide_unlock_fails = true;
517 break;
518 case 'o':
519 numops = atoi(optarg);
520 break;
521 case 'O':
522 use_oplocks = true;
523 break;
524 case 'a':
525 showall = true;
526 break;
527 case 'A':
528 analyze = true;
529 break;
530 case 'h':
531 usage();
532 exit(1);
533 default:
534 printf("Unknown option %c (%d)\n", (char)opt, opt);
535 exit(1);
539 argc -= optind;
540 argv += optind;
542 DEBUG(0,("seed=%u\n", seed));
543 srandom(seed);
545 locking_init(1);
546 lp_smbcli_options(lp_ctx, &options);
547 test_locks(share1, share2, nfspath1, nfspath2, lp_smb_ports(lp_ctx),
548 &options);
550 return(0);