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 "system/passwd.h"
22 #include "lib/events/events.h"
24 static fstring password;
25 static fstring username;
27 static int numops = 1000;
30 static bool hide_unlock_fails;
31 static bool use_oplocks;
33 #define FILENAME "\\locktest.dat"
38 #define LOCKBASE (0x40000000 - 50)
44 #define RANGE_MULTIPLE 1
47 #define NCONNECTIONS 2
50 #define LOCK_TIMEOUT 0
62 static struct record *recorded;
64 static int try_open(struct smbcli_state *c, char *nfs, int fstype, const char *fname, int flags)
70 return smbcli_open(c, fname, flags, DENY_NONE);
73 slprintf(path, sizeof(path), "%s%s", nfs, fname);
74 pstring_sub(path,"\\", "/");
75 return open(path, flags, 0666);
81 static bool try_close(struct smbcli_state *c, int fstype, int fd)
85 return smbcli_close(c, fd);
88 return close(fd) == 0;
94 static bool try_lock(struct smbcli_state *c, int fstype,
95 int fd, uint_t start, uint_t len,
102 return smbcli_lock(c, fd, start, len, LOCK_TIMEOUT, op);
105 lock.l_type = (op==READ_LOCK) ? F_RDLCK:F_WRLCK;
106 lock.l_whence = SEEK_SET;
107 lock.l_start = start;
109 lock.l_pid = getpid();
110 return fcntl(fd,F_SETLK,&lock) == 0;
116 static bool try_unlock(struct smbcli_state *c, int fstype,
117 int fd, uint_t start, uint_t len)
123 return smbcli_unlock(c, fd, start, len);
126 lock.l_type = F_UNLCK;
127 lock.l_whence = SEEK_SET;
128 lock.l_start = start;
130 lock.l_pid = getpid();
131 return fcntl(fd,F_SETLK,&lock) == 0;
137 /*****************************************************
138 return a connection to a server
139 *******************************************************/
140 static struct smbcli_state *connect_one(char *share, const char **ports,
141 struct smb_options *optionsi,
142 struct event_context *ev)
144 struct smbcli_state *c;
151 fstrcpy(server,share+2);
152 share = strchr_m(server,'\\');
153 if (!share) return NULL;
160 char *pass = getpass("Password: ");
162 fstrcpy(password, pass);
166 slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), count++);
168 nt_status = smbcli_full_connection(NULL,
169 &c, myname, server_n, ports, share, NULL,
170 username, lp_workgroup(), password, ev,
172 if (!NT_STATUS_IS_OK(nt_status)) {
173 DEBUG(0, ("smbcli_full_connection failed with error %s\n", nt_errstr(nt_status)));
177 c->use_oplocks = use_oplocks;
183 static void reconnect(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
185 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
187 struct smbcli_options *options,
188 struct event_context *ev,
189 char *share1, char *share2)
191 int server, conn, f, fstype;
198 for (server=0;server<NSERVERS;server++)
199 for (conn=0;conn<NCONNECTIONS;conn++) {
200 if (cli[server][conn]) {
201 for (f=0;f<NFILES;f++) {
202 smbcli_close(cli[server][conn], fnum[server][fstype][conn][f]);
204 smbcli_ulogoff(cli[server][conn]);
205 talloc_free(cli[server][conn]);
207 cli[server][conn] = connect_one(share[server], ports, options, ev);
208 if (!cli[server][conn]) {
209 DEBUG(0,("Failed to connect to %s\n", share[server]));
217 static bool test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
219 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
222 uint_t conn = rec->conn;
224 uint_t fstype = rec->fstype;
225 uint_t start = rec->start;
226 uint_t len = rec->len;
241 for (server=0;server<NSERVERS;server++) {
242 ret[server] = try_lock(cli[server][conn], fstype,
243 fnum[server][fstype][conn][f],
246 if (showall || ret[0] != ret[1]) {
247 printf("lock conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
249 start, start+len-1, len,
250 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
253 if (ret[0] != ret[1]) return false;
254 } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
256 for (server=0;server<NSERVERS;server++) {
257 ret[server] = try_unlock(cli[server][conn], fstype,
258 fnum[server][fstype][conn][f],
261 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
262 printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u) -> %u:%u\n",
264 start, start+len-1, len,
267 if (!hide_unlock_fails && ret[0] != ret[1]) return false;
269 /* reopen the file */
270 for (server=0;server<NSERVERS;server++) {
271 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
272 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
274 if (fnum[server][fstype][conn][f] == -1) {
275 printf("failed to reopen on share1\n");
280 printf("reopen conn=%u fstype=%u f=%u\n",
287 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
289 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
291 int server, conn, f, fstype;
293 for (server=0;server<NSERVERS;server++)
294 for (fstype=0;fstype<NUMFSTYPES;fstype++)
295 for (conn=0;conn<NCONNECTIONS;conn++)
296 for (f=0;f<NFILES;f++) {
297 if (fnum[server][fstype][conn][f] != -1) {
298 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
299 fnum[server][fstype][conn][f] = -1;
302 for (server=0;server<NSERVERS;server++) {
303 smbcli_unlink(cli[server][0], FILENAME);
307 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
309 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
311 int server, fstype, conn, f;
313 for (server=0;server<NSERVERS;server++)
314 for (fstype=0;fstype<NUMFSTYPES;fstype++)
315 for (conn=0;conn<NCONNECTIONS;conn++)
316 for (f=0;f<NFILES;f++) {
317 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
319 if (fnum[server][fstype][conn][f] == -1) {
320 fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
321 server, fstype, conn, f);
328 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
330 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
334 printf("testing %u ...\n", n);
335 for (i=0; i<n; i++) {
336 if (i && i % 100 == 0) {
340 if (recorded[i].needed &&
341 !test_one(cli, nfs, fnum, &recorded[i])) return i;
347 /* each server has two connections open to it. Each connection has two file
348 descriptors open on the file - 8 file descriptors in total
350 we then do random locking ops in tamdem on the 4 fnums from each
351 server and ensure that the results match
353 static void test_locks(char *share1, char *share2,
354 char *nfspath1, char *nfspath2,
356 struct smbcli_options *options,
357 struct event_context *ev)
359 struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
361 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
370 recorded = malloc_array_p(struct record, numops);
372 for (n=0; n<numops; n++) {
373 recorded[n].conn = random() % NCONNECTIONS;
374 recorded[n].fstype = random() % NUMFSTYPES;
375 recorded[n].f = random() % NFILES;
376 recorded[n].start = LOCKBASE + ((uint_t)random() % (LOCKRANGE-1));
377 recorded[n].len = 1 +
378 random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
379 recorded[n].start *= RANGE_MULTIPLE;
380 recorded[n].len *= RANGE_MULTIPLE;
381 recorded[n].r1 = random() % 100;
382 recorded[n].r2 = random() % 100;
383 recorded[n].needed = true;
386 reconnect(cli, nfs, fnum, ports, options, ev, share1, share2);
387 open_files(cli, nfs, fnum);
388 n = retest(cli, nfs, fnum, numops);
390 if (n == numops || !analyze) return;
396 close_files(cli, nfs, fnum);
397 reconnect(cli, nfs, fnum, ports, options, ev, share1, share2);
398 open_files(cli, nfs, fnum);
400 for (i=0;i<n-1;i++) {
402 recorded[i].needed = false;
404 close_files(cli, nfs, fnum);
405 open_files(cli, nfs, fnum);
407 m = retest(cli, nfs, fnum, n);
409 recorded[i].needed = true;
412 memmove(&recorded[i], &recorded[i+1],
413 (m-i)*sizeof(recorded[0]));
423 close_files(cli, nfs, fnum);
424 reconnect(cli, nfs, fnum, ports, options, ev, share1, share2);
425 open_files(cli, nfs, fnum);
427 n1 = retest(cli, nfs, fnum, n);
429 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
431 close_files(cli, nfs, fnum);
434 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
448 static void usage(void)
452 locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
457 -u hide unlock fails\n\
463 /****************************************************************************
465 ****************************************************************************/
466 int main(int argc,char *argv[])
468 char *share1, *share2, *nfspath1, *nfspath2;
471 struct smbcli_options options;
475 struct loadparm_context *lp_ctx;
476 struct event_context *ev;
482 if (argc < 5 || argv[1][0] == '-') {
492 all_string_sub(share1,"/","\\",0);
493 all_string_sub(share2,"/","\\",0);
495 setup_logging(argv[0], DEBUG_STDOUT);
500 lp_ctx = loadparm_init(talloc_autofree_context());
501 lp_load(lp_ctx, dyn_CONFIGFILE);
503 if (getenv("USER")) {
504 fstrcpy(username,getenv("USER"));
509 while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
512 fstrcpy(username,optarg);
513 p = strchr_m(username,'%');
516 fstrcpy(password, p+1);
524 hide_unlock_fails = true;
527 numops = atoi(optarg);
542 printf("Unknown option %c (%d)\n", (char)opt, opt);
550 DEBUG(0,("seed=%u\n", seed));
553 ev = s4_event_context_init(talloc_autofree_context());
556 lp_smbcli_options(lp_ctx, &options);
557 test_locks(share1, share2, nfspath1, nfspath2, lp_smb_ports(lp_ctx),