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/>.
21 #include "libsmb/libsmb.h"
22 #include "system/filesys.h"
23 #include "locking/share_mode_lock.h"
24 #include "locking/proto.h"
25 #include "lib/util/string_wrappers.h"
27 static fstring password
;
28 static fstring username
;
30 static int numops
= 1000;
33 static bool hide_unlock_fails
;
34 static bool use_oplocks
;
39 #define FILENAME "\\locktest.dat"
44 #define LOCKBASE (0x40000000 - 50)
50 #define RANGE_MULTIPLE 1
53 #define NCONNECTIONS 2
56 #define LOCK_TIMEOUT 0
68 static struct record
*recorded
;
70 static int try_open(struct cli_state
*c
, char *nfs
, int fstype
, const char *fname
, int flags
)
78 if (!NT_STATUS_IS_OK(cli_openx(c
, fname
, flags
, DENY_NONE
, &fd
))) {
85 if (asprintf(&path
, "%s%s", nfs
, fname
) > 0) {
87 string_replace(path
,'\\', '/');
88 ret
= open(path
, flags
, 0666);
98 static bool try_close(struct cli_state
*c
, int fstype
, int fd
)
102 return NT_STATUS_IS_OK(cli_close(c
, fd
));
105 return close(fd
) == 0;
111 static bool try_lock(struct cli_state
*c
, int fstype
,
112 int fd
, unsigned start
, unsigned len
,
119 return NT_STATUS_IS_OK(cli_lock32(c
, fd
, start
, len
,
123 lock
.l_type
= (op
==READ_LOCK
) ? F_RDLCK
:F_WRLCK
;
124 lock
.l_whence
= SEEK_SET
;
125 lock
.l_start
= start
;
127 lock
.l_pid
= getpid();
128 return fcntl(fd
,F_SETLK
,&lock
) == 0;
134 static bool try_unlock(struct cli_state
*c
, int fstype
,
135 int fd
, unsigned start
, unsigned len
)
141 return NT_STATUS_IS_OK(cli_unlock(c
, fd
, start
, len
));
144 lock
.l_type
= F_UNLCK
;
145 lock
.l_whence
= SEEK_SET
;
146 lock
.l_start
= start
;
148 lock
.l_pid
= getpid();
149 return fcntl(fd
,F_SETLK
,&lock
) == 0;
155 static void print_brl(struct file_id id
, struct server_id pid
,
156 enum brl_type lock_type
,
157 enum brl_flavour lock_flav
,
158 br_off start
, br_off size
,
161 struct file_id_buf idbuf
;
163 printf("%6d %s %s %.0f:%.0f(%.0f)\n",
164 (int)procid_to_pid(&pid
),
165 file_id_str_buf(id
, &idbuf
),
166 lock_type
==READ_LOCK
?"R":"W",
167 (double)start
, (double)start
+size
-1,(double)size
);
171 /*****************************************************
172 return a connection to a server
173 *******************************************************/
174 static struct cli_state
*connect_one(char *share
)
182 bool use_kerberos
= false;
183 bool fallback_after_kerberos
= false;
184 bool use_ccache
= false;
185 bool pw_nt_hash
= false;
186 struct cli_credentials
*creds
= NULL
;
188 fstrcpy(server
,share
+2);
189 share
= strchr_m(server
,'\\');
190 if (!share
) return NULL
;
200 rc
= samba_getpass("Password: ", pwd
, sizeof(pwd
), false, false);
202 fstrcpy(password
, pwd
);
206 creds
= cli_session_creds_init(NULL
,
209 NULL
, /* realm (use default) */
212 fallback_after_kerberos
,
216 DEBUG(0, ("cli_session_creds_init failed\n"));
220 slprintf(myname
,sizeof(myname
), "lock-%lu-%u", (unsigned long)getpid(), count
++);
222 nt_status
= cli_full_connection_creds(&c
,
232 if (!NT_STATUS_IS_OK(nt_status
)) {
233 DEBUG(0, ("cli_full_connection failed with error %s\n", nt_errstr(nt_status
)));
237 c
->use_oplocks
= use_oplocks
;
243 static void reconnect(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
245 int fnum
[NSERVERS
][NUMFSTYPES
][NCONNECTIONS
][NFILES
],
246 char *share1
, char *share2
)
248 int server
, conn
, f
, fstype
;
255 for (server
=0;server
<NSERVERS
;server
++)
256 for (conn
=0;conn
<NCONNECTIONS
;conn
++) {
257 if (cli
[server
][conn
]) {
258 for (f
=0;f
<NFILES
;f
++) {
259 cli_close(cli
[server
][conn
], fnum
[server
][fstype
][conn
][f
]);
261 cli_ulogoff(cli
[server
][conn
]);
262 cli_shutdown(cli
[server
][conn
]);
264 cli
[server
][conn
] = connect_one(share
[server
]);
265 if (!cli
[server
][conn
]) {
266 DEBUG(0,("Failed to connect to %s\n", share
[server
]));
274 static bool test_one(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
276 int fnum
[NSERVERS
][NUMFSTYPES
][NCONNECTIONS
][NFILES
],
279 unsigned conn
= rec
->conn
;
281 unsigned fstype
= rec
->fstype
;
282 unsigned start
= rec
->start
;
283 unsigned len
= rec
->len
;
284 unsigned r1
= rec
->r1
;
285 unsigned r2
= rec
->r2
;
298 for (server
=0;server
<NSERVERS
;server
++) {
299 ret
[server
] = try_lock(cli
[server
][conn
], fstype
,
300 fnum
[server
][fstype
][conn
][f
],
303 if (showall
|| ret
[0] != ret
[1]) {
304 printf("lock conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
306 start
, start
+len
-1, len
,
307 op
==READ_LOCK
?"READ_LOCK":"WRITE_LOCK",
310 if (showall
) brl_forall(print_brl
, NULL
);
311 if (ret
[0] != ret
[1]) return False
;
312 } else if (r2
< LOCK_PCT
+UNLOCK_PCT
) {
314 for (server
=0;server
<NSERVERS
;server
++) {
315 ret
[server
] = try_unlock(cli
[server
][conn
], fstype
,
316 fnum
[server
][fstype
][conn
][f
],
319 if (showall
|| (!hide_unlock_fails
&& (ret
[0] != ret
[1]))) {
320 printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u) -> %u:%u\n",
322 start
, start
+len
-1, len
,
325 if (showall
) brl_forall(print_brl
, NULL
);
326 if (!hide_unlock_fails
&& ret
[0] != ret
[1]) return False
;
328 /* reopen the file */
329 for (server
=0;server
<NSERVERS
;server
++) {
330 try_close(cli
[server
][conn
], fstype
, fnum
[server
][fstype
][conn
][f
]);
331 fnum
[server
][fstype
][conn
][f
] = try_open(cli
[server
][conn
], nfs
[server
], fstype
, FILENAME
,
333 if (fnum
[server
][fstype
][conn
][f
] == -1) {
334 printf("failed to reopen on share1\n");
339 printf("reopen conn=%u fstype=%u f=%u\n",
341 brl_forall(print_brl
, NULL
);
347 static void close_files(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
349 int fnum
[NSERVERS
][NUMFSTYPES
][NCONNECTIONS
][NFILES
])
351 int server
, conn
, f
, fstype
;
353 for (server
=0;server
<NSERVERS
;server
++)
354 for (fstype
=0;fstype
<NUMFSTYPES
;fstype
++)
355 for (conn
=0;conn
<NCONNECTIONS
;conn
++)
356 for (f
=0;f
<NFILES
;f
++) {
357 if (fnum
[server
][fstype
][conn
][f
] != -1) {
358 try_close(cli
[server
][conn
], fstype
, fnum
[server
][fstype
][conn
][f
]);
359 fnum
[server
][fstype
][conn
][f
] = -1;
362 for (server
=0;server
<NSERVERS
;server
++) {
363 cli_unlink(cli
[server
][0], FILENAME
, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
367 static void open_files(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
369 int fnum
[NSERVERS
][NUMFSTYPES
][NCONNECTIONS
][NFILES
])
371 int server
, fstype
, conn
, f
;
373 for (server
=0;server
<NSERVERS
;server
++)
374 for (fstype
=0;fstype
<NUMFSTYPES
;fstype
++)
375 for (conn
=0;conn
<NCONNECTIONS
;conn
++)
376 for (f
=0;f
<NFILES
;f
++) {
377 fnum
[server
][fstype
][conn
][f
] = try_open(cli
[server
][conn
], nfs
[server
], fstype
, FILENAME
,
379 if (fnum
[server
][fstype
][conn
][f
] == -1) {
380 fprintf(stderr
,"Failed to open fnum[%u][%u][%u][%u]\n",
381 server
, fstype
, conn
, f
);
388 static int retest(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
390 int fnum
[NSERVERS
][NUMFSTYPES
][NCONNECTIONS
][NFILES
],
394 printf("testing %u ...\n", n
);
395 for (i
=0; i
<n
; i
++) {
396 if (i
&& i
% 100 == 0) {
400 if (recorded
[i
].needed
&&
401 !test_one(cli
, nfs
, fnum
, &recorded
[i
])) return i
;
407 /* each server has two connections open to it. Each connection has two file
408 descriptors open on the file - 8 file descriptors in total
410 we then do random locking ops in tamdem on the 4 fnums from each
411 server and ensure that the results match
413 static void test_locks(char *share1
, char *share2
, char *nfspath1
, char *nfspath2
)
415 struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
];
417 int fnum
[NSERVERS
][NUMFSTYPES
][NCONNECTIONS
][NFILES
];
426 recorded
= SMB_MALLOC_ARRAY(struct record
, numops
);
428 for (n
=0; n
<numops
; n
++) {
429 recorded
[n
].conn
= random() % NCONNECTIONS
;
430 recorded
[n
].fstype
= random() % NUMFSTYPES
;
431 recorded
[n
].f
= random() % NFILES
;
432 recorded
[n
].start
= LOCKBASE
+ ((unsigned)random() % (LOCKRANGE
-1));
433 recorded
[n
].len
= 1 +
434 random() % (LOCKRANGE
-(recorded
[n
].start
-LOCKBASE
));
435 recorded
[n
].start
*= RANGE_MULTIPLE
;
436 recorded
[n
].len
*= RANGE_MULTIPLE
;
437 recorded
[n
].r1
= random() % 100;
438 recorded
[n
].r2
= random() % 100;
439 recorded
[n
].needed
= True
;
442 reconnect(cli
, nfs
, fnum
, share1
, share2
);
443 open_files(cli
, nfs
, fnum
);
444 n
= retest(cli
, nfs
, fnum
, numops
);
446 if (n
== numops
|| !analyze
) return;
452 close_files(cli
, nfs
, fnum
);
453 reconnect(cli
, nfs
, fnum
, share1
, share2
);
454 open_files(cli
, nfs
, fnum
);
456 for (i
=0;i
<n
-1;i
++) {
458 recorded
[i
].needed
= False
;
460 close_files(cli
, nfs
, fnum
);
461 open_files(cli
, nfs
, fnum
);
463 m
= retest(cli
, nfs
, fnum
, n
);
465 recorded
[i
].needed
= True
;
468 memmove(&recorded
[i
], &recorded
[i
+1],
469 (m
-i
)*sizeof(recorded
[0]));
479 close_files(cli
, nfs
, fnum
);
480 reconnect(cli
, nfs
, fnum
, share1
, share2
);
481 open_files(cli
, nfs
, fnum
);
483 n1
= retest(cli
, nfs
, fnum
, n
);
485 printf("ERROR - inconsistent result (%u %u)\n", n1
, n
);
487 close_files(cli
, nfs
, fnum
);
490 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
504 static void usage(void)
508 locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
513 -u hide unlock fails\n\
519 /****************************************************************************
521 ****************************************************************************/
522 int main(int argc
,char *argv
[])
524 char *share1
, *share2
, *nfspath1
, *nfspath2
;
531 if (argc
< 5 || argv
[1][0] == '-') {
536 setup_logging(argv
[0], DEBUG_STDOUT
);
543 all_string_sub(share1
,"/","\\",0);
544 all_string_sub(share2
,"/","\\",0);
549 lp_load_global(get_dyn_CONFIGFILE());
552 if (getenv("USER")) {
553 fstrcpy(username
,getenv("USER"));
558 while ((opt
= getopt(argc
, argv
, "U:s:ho:aAW:O")) != EOF
) {
561 fstrcpy(username
,optarg
);
562 p
= strchr_m(username
,'%');
565 fstrcpy(password
, p
+1);
573 hide_unlock_fails
= True
;
576 numops
= atoi(optarg
);
591 printf("Unknown option %c (%d)\n", (char)opt
, opt
);
599 DEBUG(0,("seed=%u\n", seed
));
602 locking_init_readonly();
603 test_locks(share1
, share2
, nfspath1
, nfspath2
);