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
58 unsigned int start, len;
62 static struct record *recorded;
64 static int try_open(struct smbcli_state *c, char *nfs, int fstype, const char *fname, int flags)
71 return smbcli_open(c, fname, flags, DENY_NONE);
74 asprintf(&path, "%s%s", nfs, fname);
75 string_replace(path,'\\', '/');
76 ret = open(path, flags, 0666);
84 static bool try_close(struct smbcli_state *c, int fstype, int fd)
88 return smbcli_close(c, fd);
91 return close(fd) == 0;
97 static bool try_lock(struct smbcli_state *c, int fstype,
98 int fd, unsigned int start, unsigned int len,
105 return smbcli_lock(c, fd, start, len, LOCK_TIMEOUT, op);
108 lock.l_type = (op==READ_LOCK) ? F_RDLCK:F_WRLCK;
109 lock.l_whence = SEEK_SET;
110 lock.l_start = start;
112 lock.l_pid = getpid();
113 return fcntl(fd,F_SETLK,&lock) == 0;
119 static bool try_unlock(struct smbcli_state *c, int fstype,
120 int fd, unsigned int start, unsigned int len)
126 return smbcli_unlock(c, fd, start, len);
129 lock.l_type = F_UNLCK;
130 lock.l_whence = SEEK_SET;
131 lock.l_start = start;
133 lock.l_pid = getpid();
134 return fcntl(fd,F_SETLK,&lock) == 0;
140 /*****************************************************
141 return a connection to a server
142 *******************************************************/
143 static struct smbcli_state *connect_one(TALLOC_CTX *mem_ctx,
144 char *share, const char **ports,
145 struct smb_options *options,
146 struct smb_options *session_options,
147 struct gensec_settings *gensec_settings,
148 struct tevent_context *ev)
150 struct smbcli_state *c;
157 server = talloc_strdup(mem_ctx, share+2);
158 share = strchr_m(server,'\\');
159 if (!share) return NULL;
166 char *pass = getpass("Password: ");
168 password = talloc_strdup(mem_ctx, pass);
172 myname = talloc_asprintf(mem_ctx, "lock-%u-%u", getpid(), count++);
174 nt_status = smbcli_full_connection(NULL,
175 &c, myname, server_n, ports, share, NULL,
176 username, lp_workgroup(), password, ev,
177 options, session_options, gensec_settings);
178 if (!NT_STATUS_IS_OK(nt_status)) {
179 DEBUG(0, ("smbcli_full_connection failed with error %s\n", nt_errstr(nt_status)));
183 c->use_oplocks = use_oplocks;
189 static void reconnect(TALLOC_CTX *mem_ctx,
190 struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
192 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
194 struct smbcli_options *options,
195 struct smbcli_session_options *session_options,
196 struct gensec_settings *gensec_settings,
197 struct tevent_context *ev,
198 char *share1, char *share2)
200 int server, conn, f, fstype;
207 for (server=0;server<NSERVERS;server++)
208 for (conn=0;conn<NCONNECTIONS;conn++) {
209 if (cli[server][conn]) {
210 for (f=0;f<NFILES;f++) {
211 smbcli_close(cli[server][conn], fnum[server][fstype][conn][f]);
213 smbcli_ulogoff(cli[server][conn]);
214 talloc_free(cli[server][conn]);
216 cli[server][conn] = connect_one(mem_ctx, share[server], ports, options, session_options, gensec_settings, ev);
217 if (!cli[server][conn]) {
218 DEBUG(0,("Failed to connect to %s\n", share[server]));
226 static bool test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
228 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
231 unsigned int conn = rec->conn;
232 unsigned int f = rec->f;
233 unsigned int fstype = rec->fstype;
234 unsigned int start = rec->start;
235 unsigned int len = rec->len;
236 unsigned int r1 = rec->r1;
237 unsigned int r2 = rec->r2;
250 for (server=0;server<NSERVERS;server++) {
251 ret[server] = try_lock(cli[server][conn], fstype,
252 fnum[server][fstype][conn][f],
255 if (showall || ret[0] != ret[1]) {
256 printf("lock conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
258 start, start+len-1, len,
259 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
262 if (ret[0] != ret[1]) return false;
263 } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
265 for (server=0;server<NSERVERS;server++) {
266 ret[server] = try_unlock(cli[server][conn], fstype,
267 fnum[server][fstype][conn][f],
270 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
271 printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u) -> %u:%u\n",
273 start, start+len-1, len,
276 if (!hide_unlock_fails && ret[0] != ret[1]) return false;
278 /* reopen the file */
279 for (server=0;server<NSERVERS;server++) {
280 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
281 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
283 if (fnum[server][fstype][conn][f] == -1) {
284 printf("failed to reopen on share1\n");
289 printf("reopen conn=%u fstype=%u f=%u\n",
296 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
298 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
300 int server, conn, f, fstype;
302 for (server=0;server<NSERVERS;server++)
303 for (fstype=0;fstype<NUMFSTYPES;fstype++)
304 for (conn=0;conn<NCONNECTIONS;conn++)
305 for (f=0;f<NFILES;f++) {
306 if (fnum[server][fstype][conn][f] != -1) {
307 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
308 fnum[server][fstype][conn][f] = -1;
311 for (server=0;server<NSERVERS;server++) {
312 smbcli_unlink(cli[server][0], FILENAME);
316 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
318 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
320 int server, fstype, conn, f;
322 for (server=0;server<NSERVERS;server++)
323 for (fstype=0;fstype<NUMFSTYPES;fstype++)
324 for (conn=0;conn<NCONNECTIONS;conn++)
325 for (f=0;f<NFILES;f++) {
326 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
328 if (fnum[server][fstype][conn][f] == -1) {
329 fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
330 server, fstype, conn, f);
337 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
339 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
343 printf("testing %u ...\n", n);
344 for (i=0; i<n; i++) {
345 if (i && i % 100 == 0) {
349 if (recorded[i].needed &&
350 !test_one(cli, nfs, fnum, &recorded[i])) return i;
356 /* each server has two connections open to it. Each connection has two file
357 descriptors open on the file - 8 file descriptors in total
359 we then do random locking ops in tamdem on the 4 fnums from each
360 server and ensure that the results match
362 static void test_locks(TALLOC_CTX *mem_ctx, char *share1, char *share2,
363 char *nfspath1, char *nfspath2,
365 struct smbcli_options *options,
366 struct smbcli_session_options *session_options,
367 struct gensec_settings *gensec_settings,
368 struct tevent_context *ev)
370 struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
372 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
381 recorded = malloc_array_p(struct record, numops);
383 for (n=0; n<numops; n++) {
384 recorded[n].conn = random() % NCONNECTIONS;
385 recorded[n].fstype = random() % NUMFSTYPES;
386 recorded[n].f = random() % NFILES;
387 recorded[n].start = LOCKBASE + ((unsigned int)random() % (LOCKRANGE-1));
388 recorded[n].len = 1 +
389 random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
390 recorded[n].start *= RANGE_MULTIPLE;
391 recorded[n].len *= RANGE_MULTIPLE;
392 recorded[n].r1 = random() % 100;
393 recorded[n].r2 = random() % 100;
394 recorded[n].needed = true;
397 reconnect(mem_ctx, cli, nfs, fnum, ports, options, session_options, gensec_settings, ev, share1, share2);
398 open_files(cli, nfs, fnum);
399 n = retest(cli, nfs, fnum, numops);
401 if (n == numops || !analyze) return;
407 close_files(cli, nfs, fnum);
408 reconnect(mem_ctx, cli, nfs, fnum, ports, options, session_options, ev, share1, share2);
409 open_files(cli, nfs, fnum);
411 for (i=0;i<n-1;i++) {
413 recorded[i].needed = false;
415 close_files(cli, nfs, fnum);
416 open_files(cli, nfs, fnum);
418 m = retest(cli, nfs, fnum, n);
420 recorded[i].needed = true;
423 memmove(&recorded[i], &recorded[i+1],
424 (m-i)*sizeof(recorded[0]));
434 close_files(cli, nfs, fnum);
435 reconnect(mem_ctx, cli, nfs, fnum, ports, options, session_options, gensec_settings, ev, share1, share2);
436 open_files(cli, nfs, fnum);
438 n1 = retest(cli, nfs, fnum, n);
440 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
442 close_files(cli, nfs, fnum);
445 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
459 static void usage(void)
463 locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
468 -u hide unlock fails\n\
474 /****************************************************************************
476 ****************************************************************************/
477 int main(int argc,char *argv[])
479 char *share1, *share2, *nfspath1, *nfspath2;
482 struct smbcli_options options;
483 struct smbcli_session_options session_options;
488 struct loadparm_context *lp_ctx;
489 struct tevent_context *ev;
491 mem_ctx = talloc_autofree_context();
497 if (argc < 5 || argv[1][0] == '-') {
507 all_string_sub(share1,"/","\\",0);
508 all_string_sub(share2,"/","\\",0);
510 setup_logging(argv[0], DEBUG_STDOUT);
515 lp_ctx = loadparm_init(mem_ctx);
516 lp_load(lp_ctx, dyn_CONFIGFILE);
518 if (getenv("USER")) {
519 username = talloc_strdup(mem_ctx, getenv("USER"));
524 while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
527 username = talloc_strdup(mem_ctx, optarg);
528 p = strchr_m(username,'%');
531 password = talloc_strdup(mem_ctx, p+1);
539 hide_unlock_fails = true;
542 numops = atoi(optarg);
557 printf("Unknown option %c (%d)\n", (char)opt, opt);
565 DEBUG(0,("seed=%u\n", seed));
568 ev = s4_event_context_init(mem_ctx);
571 lp_smbcli_options(lp_ctx, &options);
572 lp_smbcli_session_options(lp_ctx, &session_options);
573 test_locks(mem_ctx, share1, share2, nfspath1, nfspath2,
574 lp_smb_ports(lp_ctx),
575 &options, &session_options, lp_gensec_settings(lp_ctx), ev);