s3-netlogon: Add support for LogonGetCapabilities.
[Samba.git] / source3 / torture / locktest2.c
blobaefdcd3ff2588a86fa12414876cd3de91da527e9
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 *pass = getpass("Password: ");
188 if (pass) {
189 fstrcpy(password, pass);
193 slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
195 nt_status = cli_full_connection(&c, myname, server_n, NULL, 0, share, "?????",
196 username, lp_workgroup(), password, 0,
197 SMB_SIGNING_DEFAULT);
199 if (!NT_STATUS_IS_OK(nt_status)) {
200 DEBUG(0, ("cli_full_connection failed with error %s\n", nt_errstr(nt_status)));
201 return NULL;
204 c->use_oplocks = use_oplocks;
206 return c;
210 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS],
211 char *nfs[NSERVERS],
212 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
213 char *share1, char *share2)
215 int server, conn, f, fstype;
216 char *share[2];
217 share[0] = share1;
218 share[1] = share2;
220 fstype = FSTYPE_SMB;
222 for (server=0;server<NSERVERS;server++)
223 for (conn=0;conn<NCONNECTIONS;conn++) {
224 if (cli[server][conn]) {
225 for (f=0;f<NFILES;f++) {
226 cli_close(cli[server][conn], fnum[server][fstype][conn][f]);
228 cli_ulogoff(cli[server][conn]);
229 cli_shutdown(cli[server][conn]);
231 cli[server][conn] = connect_one(share[server]);
232 if (!cli[server][conn]) {
233 DEBUG(0,("Failed to connect to %s\n", share[server]));
234 exit(1);
241 static bool test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
242 char *nfs[NSERVERS],
243 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
244 struct record *rec)
246 unsigned conn = rec->conn;
247 unsigned f = rec->f;
248 unsigned fstype = rec->fstype;
249 unsigned start = rec->start;
250 unsigned len = rec->len;
251 unsigned r1 = rec->r1;
252 unsigned r2 = rec->r2;
253 enum brl_type op;
254 int server;
255 bool ret[NSERVERS];
257 if (r1 < READ_PCT) {
258 op = READ_LOCK;
259 } else {
260 op = WRITE_LOCK;
263 if (r2 < LOCK_PCT) {
264 /* set a lock */
265 for (server=0;server<NSERVERS;server++) {
266 ret[server] = try_lock(cli[server][conn], fstype,
267 fnum[server][fstype][conn][f],
268 start, len, op);
270 if (showall || ret[0] != ret[1]) {
271 printf("lock conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
272 conn, fstype, f,
273 start, start+len-1, len,
274 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
275 ret[0], ret[1]);
277 if (showall) brl_forall(print_brl, NULL);
278 if (ret[0] != ret[1]) return False;
279 } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
280 /* unset a lock */
281 for (server=0;server<NSERVERS;server++) {
282 ret[server] = try_unlock(cli[server][conn], fstype,
283 fnum[server][fstype][conn][f],
284 start, len);
286 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
287 printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u) -> %u:%u\n",
288 conn, fstype, f,
289 start, start+len-1, len,
290 ret[0], ret[1]);
292 if (showall) brl_forall(print_brl, NULL);
293 if (!hide_unlock_fails && ret[0] != ret[1]) return False;
294 } else {
295 /* reopen the file */
296 for (server=0;server<NSERVERS;server++) {
297 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
298 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
299 O_RDWR|O_CREAT);
300 if (fnum[server][fstype][conn][f] == -1) {
301 printf("failed to reopen on share1\n");
302 return False;
305 if (showall) {
306 printf("reopen conn=%u fstype=%u f=%u\n",
307 conn, fstype, f);
308 brl_forall(print_brl, NULL);
311 return True;
314 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
315 char *nfs[NSERVERS],
316 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
318 int server, conn, f, fstype;
320 for (server=0;server<NSERVERS;server++)
321 for (fstype=0;fstype<NUMFSTYPES;fstype++)
322 for (conn=0;conn<NCONNECTIONS;conn++)
323 for (f=0;f<NFILES;f++) {
324 if (fnum[server][fstype][conn][f] != -1) {
325 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
326 fnum[server][fstype][conn][f] = -1;
329 for (server=0;server<NSERVERS;server++) {
330 cli_unlink(cli[server][0], FILENAME, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
334 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
335 char *nfs[NSERVERS],
336 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
338 int server, fstype, conn, f;
340 for (server=0;server<NSERVERS;server++)
341 for (fstype=0;fstype<NUMFSTYPES;fstype++)
342 for (conn=0;conn<NCONNECTIONS;conn++)
343 for (f=0;f<NFILES;f++) {
344 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
345 O_RDWR|O_CREAT);
346 if (fnum[server][fstype][conn][f] == -1) {
347 fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
348 server, fstype, conn, f);
349 exit(1);
355 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
356 char *nfs[NSERVERS],
357 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
358 int n)
360 int i;
361 printf("testing %u ...\n", n);
362 for (i=0; i<n; i++) {
363 if (i && i % 100 == 0) {
364 printf("%u\n", i);
367 if (recorded[i].needed &&
368 !test_one(cli, nfs, fnum, &recorded[i])) return i;
370 return n;
374 /* each server has two connections open to it. Each connection has two file
375 descriptors open on the file - 8 file descriptors in total
377 we then do random locking ops in tamdem on the 4 fnums from each
378 server and ensure that the results match
380 static void test_locks(char *share1, char *share2, char *nfspath1, char *nfspath2)
382 struct cli_state *cli[NSERVERS][NCONNECTIONS];
383 char *nfs[NSERVERS];
384 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
385 int n, i, n1;
387 nfs[0] = nfspath1;
388 nfs[1] = nfspath2;
390 ZERO_STRUCT(fnum);
391 ZERO_STRUCT(cli);
393 recorded = SMB_MALLOC_ARRAY(struct record, numops);
395 for (n=0; n<numops; n++) {
396 recorded[n].conn = random() % NCONNECTIONS;
397 recorded[n].fstype = random() % NUMFSTYPES;
398 recorded[n].f = random() % NFILES;
399 recorded[n].start = LOCKBASE + ((unsigned)random() % (LOCKRANGE-1));
400 recorded[n].len = 1 +
401 random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
402 recorded[n].start *= RANGE_MULTIPLE;
403 recorded[n].len *= RANGE_MULTIPLE;
404 recorded[n].r1 = random() % 100;
405 recorded[n].r2 = random() % 100;
406 recorded[n].needed = True;
409 reconnect(cli, nfs, fnum, share1, share2);
410 open_files(cli, nfs, fnum);
411 n = retest(cli, nfs, fnum, numops);
413 if (n == numops || !analyze) return;
414 n++;
416 while (1) {
417 n1 = n;
419 close_files(cli, nfs, fnum);
420 reconnect(cli, nfs, fnum, share1, share2);
421 open_files(cli, nfs, fnum);
423 for (i=0;i<n-1;i++) {
424 int m;
425 recorded[i].needed = False;
427 close_files(cli, nfs, fnum);
428 open_files(cli, nfs, fnum);
430 m = retest(cli, nfs, fnum, n);
431 if (m == n) {
432 recorded[i].needed = True;
433 } else {
434 if (i < m) {
435 memmove(&recorded[i], &recorded[i+1],
436 (m-i)*sizeof(recorded[0]));
438 n = m;
439 i--;
443 if (n1 == n) break;
446 close_files(cli, nfs, fnum);
447 reconnect(cli, nfs, fnum, share1, share2);
448 open_files(cli, nfs, fnum);
449 showall = True;
450 n1 = retest(cli, nfs, fnum, n);
451 if (n1 != n-1) {
452 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
454 close_files(cli, nfs, fnum);
456 for (i=0;i<n;i++) {
457 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
458 recorded[i].r1,
459 recorded[i].r2,
460 recorded[i].conn,
461 recorded[i].fstype,
462 recorded[i].f,
463 recorded[i].start,
464 recorded[i].len,
465 recorded[i].needed);
471 static void usage(void)
473 printf(
474 "Usage:\n\
475 locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
476 options:\n\
477 -U user%%pass\n\
478 -s seed\n\
479 -o numops\n\
480 -u hide unlock fails\n\
481 -a (show all ops)\n\
482 -O use oplocks\n\
486 /****************************************************************************
487 main program
488 ****************************************************************************/
489 int main(int argc,char *argv[])
491 char *share1, *share2, *nfspath1, *nfspath2;
492 int opt;
493 char *p;
494 int seed;
496 setlinebuf(stdout);
498 if (argc < 5 || argv[1][0] == '-') {
499 usage();
500 exit(1);
503 setup_logging(argv[0], DEBUG_STDOUT);
505 share1 = argv[1];
506 share2 = argv[2];
507 nfspath1 = argv[3];
508 nfspath2 = argv[4];
510 all_string_sub(share1,"/","\\",0);
511 all_string_sub(share2,"/","\\",0);
513 argc -= 4;
514 argv += 4;
516 lp_load_global(get_dyn_CONFIGFILE());
517 load_interfaces();
519 if (getenv("USER")) {
520 fstrcpy(username,getenv("USER"));
523 seed = time(NULL);
525 while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
526 switch (opt) {
527 case 'U':
528 fstrcpy(username,optarg);
529 p = strchr_m(username,'%');
530 if (p) {
531 *p = 0;
532 fstrcpy(password, p+1);
533 got_pass = 1;
535 break;
536 case 's':
537 seed = atoi(optarg);
538 break;
539 case 'u':
540 hide_unlock_fails = True;
541 break;
542 case 'o':
543 numops = atoi(optarg);
544 break;
545 case 'O':
546 use_oplocks = True;
547 break;
548 case 'a':
549 showall = True;
550 break;
551 case 'A':
552 analyze = True;
553 break;
554 case 'h':
555 usage();
556 exit(1);
557 default:
558 printf("Unknown option %c (%d)\n", (char)opt, opt);
559 exit(1);
563 argc -= optind;
564 argv += optind;
566 DEBUG(0,("seed=%u\n", seed));
567 srandom(seed);
569 locking_init_readonly();
570 test_locks(share1, share2, nfspath1, nfspath2);
572 return(0);