2 Unix SMB/CIFS implementation.
3 randomised byte range lock tester
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 "lib/cmdline/popt_common.h"
22 #include "lib/events/events.h"
23 #include "system/filesys.h"
24 #include "system/time.h"
25 #include "auth/credentials/credentials.h"
26 #include "auth/gensec/gensec.h"
27 #include "libcli/libcli.h"
28 #include "param/param.h"
29 #include "dynconfig/dynconfig.h"
30 #include "libcli/resolve/resolve.h"
32 static int numops
= 1000;
35 static int hide_unlock_fails
;
36 static int use_oplocks
;
37 static uint_t lock_range
= 100;
38 static uint_t lock_base
= 0;
39 static uint_t min_length
= 0;
40 static int exact_error_codes
;
43 #define FILENAME "\\locktest.dat"
48 #define RANGE_MULTIPLE 1
50 #define NCONNECTIONS 2
52 #define LOCK_TIMEOUT 0
54 static struct cli_credentials
*servers
[NSERVERS
];
56 enum lock_op
{OP_LOCK
, OP_UNLOCK
, OP_REOPEN
};
60 enum brl_type lock_type
;
70 static struct record preset
[] = {
71 {OP_LOCK
, WRITE_LOCK
, 0, 0, 2, 0, 1},
72 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
73 {OP_LOCK
, WRITE_LOCK
, 0, 0, 3, 0, 1},
74 {OP_UNLOCK
, 0 , 0, 0, 2, 0, 1},
75 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
77 {OP_LOCK
, READ_LOCK
, 0, 0, 2, 0, 1},
78 {OP_LOCK
, READ_LOCK
, 0, 0, 1, 1, 1},
79 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
80 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
82 {OP_LOCK
, READ_LOCK
, 0, 0, 2, 0, 1},
83 {OP_LOCK
, WRITE_LOCK
, 0, 0, 3, 1, 1},
84 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
85 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
87 {OP_LOCK
, READ_LOCK
, 0, 0, 2, 0, 1},
88 {OP_LOCK
, WRITE_LOCK
, 0, 0, 1, 1, 1},
89 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
90 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
92 {OP_LOCK
, WRITE_LOCK
, 0, 0, 2, 0, 1},
93 {OP_LOCK
, READ_LOCK
, 0, 0, 1, 1, 1},
94 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
95 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
97 {OP_LOCK
, WRITE_LOCK
, 0, 0, 2, 0, 1},
98 {OP_LOCK
, READ_LOCK
, 0, 0, 3, 1, 1},
99 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
100 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
105 static struct record
*recorded
;
107 /*****************************************************
108 return a connection to a server
109 *******************************************************/
110 static struct smbcli_state
*connect_one(struct tevent_context
*ev
,
111 struct loadparm_context
*lp_ctx
,
113 char *share
, int snum
, int conn
)
115 struct smbcli_state
*c
;
116 char *server
, *myname
;
119 struct smbcli_options options
;
120 struct smbcli_session_options session_options
;
122 lp_smbcli_options(lp_ctx
, &options
);
123 lp_smbcli_session_options(lp_ctx
, &session_options
);
125 printf("connect_one(%s, %d, %d)\n", share
, snum
, conn
);
127 server
= talloc_strdup(mem_ctx
, share
+2);
128 share
= strchr_m(server
,'\\');
129 if (!share
) return NULL
;
134 char **unc_list
= NULL
;
137 p
= lp_parm_string(lp_ctx
, NULL
, "torture", "unclist");
140 unc_list
= file_lines_load(p
, &num_unc_names
, 0, NULL
);
141 if (!unc_list
|| num_unc_names
<= 0) {
142 printf("Failed to load unc names list from '%s'\n", p
);
146 if (!smbcli_parse_unc(unc_list
[conn
% num_unc_names
],
148 printf("Failed to parse UNC name %s\n",
149 unc_list
[conn
% num_unc_names
]);
152 server
= talloc_strdup(mem_ctx
, h
);
153 share
= talloc_strdup(mem_ctx
, s
);
158 myname
= talloc_asprintf(mem_ctx
, "lock-%u-%u", getpid(), snum
);
159 cli_credentials_set_workstation(servers
[snum
], myname
, CRED_SPECIFIED
);
162 printf("\\\\%s\\%s\n", server
, share
);
163 status
= smbcli_full_connection(NULL
, &c
,
165 lp_smb_ports(lp_ctx
),
167 lp_socket_options(lp_ctx
),
169 lp_resolve_context(lp_ctx
),
170 ev
, &options
, &session_options
,
171 lp_iconv_convenience(lp_ctx
),
172 lp_gensec_settings(mem_ctx
, lp_ctx
));
173 if (!NT_STATUS_IS_OK(status
)) {
176 } while (!NT_STATUS_IS_OK(status
) && retries
--);
178 if (!NT_STATUS_IS_OK(status
)) {
186 static void reconnect(struct tevent_context
*ev
,
187 struct loadparm_context
*lp_ctx
,
189 struct smbcli_state
*cli
[NSERVERS
][NCONNECTIONS
], int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
],
190 char *share
[NSERVERS
])
194 for (server
=0;server
<NSERVERS
;server
++)
195 for (conn
=0;conn
<NCONNECTIONS
;conn
++) {
196 if (cli
[server
][conn
]) {
197 for (f
=0;f
<NFILES
;f
++) {
198 if (fnum
[server
][conn
][f
] != -1) {
199 smbcli_close(cli
[server
][conn
]->tree
, fnum
[server
][conn
][f
]);
200 fnum
[server
][conn
][f
] = -1;
203 talloc_free(cli
[server
][conn
]);
205 cli
[server
][conn
] = connect_one(ev
, lp_ctx
, mem_ctx
, share
[server
],
207 if (!cli
[server
][conn
]) {
208 DEBUG(0,("Failed to connect to %s\n", share
[server
]));
216 static bool test_one(struct smbcli_state
*cli
[NSERVERS
][NCONNECTIONS
],
217 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
],
220 uint_t conn
= rec
->conn
;
222 uint64_t start
= rec
->start
;
223 uint64_t len
= rec
->len
;
224 enum brl_type op
= rec
->lock_type
;
227 NTSTATUS status
[NSERVERS
];
229 switch (rec
->lock_op
) {
232 for (server
=0;server
<NSERVERS
;server
++) {
234 struct smbcli_tree
*tree
=cli
[server
][conn
]->tree
;
235 int fn
=fnum
[server
][conn
][f
];
237 if (!(tree
->session
->transport
->negotiate
.capabilities
& CAP_LARGE_FILES
)) {
238 res
=smbcli_lock(tree
, fn
, start
, len
, LOCK_TIMEOUT
, rec
->lock_op
);
240 union smb_lock parms
;
242 struct smb_lock_entry lock
[1];
244 parms
.lockx
.level
= RAW_LOCK_LOCKX
;
245 parms
.lockx
.in
.file
.fnum
= fn
;
247 ltype
= (rec
->lock_op
== READ_LOCK
? 1 : 0);
248 ltype
|= LOCKING_ANDX_LARGE_FILES
;
249 parms
.lockx
.in
.mode
= ltype
;
250 parms
.lockx
.in
.timeout
= LOCK_TIMEOUT
;
251 parms
.lockx
.in
.ulock_cnt
= 0;
252 parms
.lockx
.in
.lock_cnt
= 1;
253 lock
[0].pid
= rec
->pid
;
254 lock
[0].offset
= start
;
256 parms
.lockx
.in
.locks
= &lock
[0];
258 res
= smb_raw_lock(tree
, &parms
);
261 ret
[server
] = NT_STATUS_IS_OK(res
);
262 status
[server
] = smbcli_nt_error(cli
[server
][conn
]->tree
);
263 if (!exact_error_codes
&&
264 NT_STATUS_EQUAL(status
[server
],
265 NT_STATUS_FILE_LOCK_CONFLICT
)) {
266 status
[server
] = NT_STATUS_LOCK_NOT_GRANTED
;
269 if (showall
|| !NT_STATUS_EQUAL(status
[0],status
[1])) {
270 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
272 (double)start
, (double)len
,
273 op
==READ_LOCK
?"READ_LOCK":"WRITE_LOCK",
274 nt_errstr(status
[0]), nt_errstr(status
[1]));
276 if (!NT_STATUS_EQUAL(status
[0],status
[1])) return false;
281 for (server
=0;server
<NSERVERS
;server
++) {
283 struct smbcli_tree
*tree
=cli
[server
][conn
]->tree
;
284 int fn
=fnum
[server
][conn
][f
];
287 if (!(tree
->session
->transport
->negotiate
.capabilities
& CAP_LARGE_FILES
)) {
288 res
=smbcli_unlock(tree
, fn
, start
, len
);
290 union smb_lock parms
;
291 struct smb_lock_entry lock
[1];
293 parms
.lockx
.level
= RAW_LOCK_LOCKX
;
294 parms
.lockx
.in
.file
.fnum
= fn
;
295 parms
.lockx
.in
.mode
= LOCKING_ANDX_LARGE_FILES
;
296 parms
.lockx
.in
.timeout
= 0;
297 parms
.lockx
.in
.ulock_cnt
= 1;
298 parms
.lockx
.in
.lock_cnt
= 0;
299 lock
[0].pid
= rec
->pid
;
301 lock
[0].offset
= start
;
302 parms
.lockx
.in
.locks
= &lock
[0];
304 res
= smb_raw_lock(tree
, &parms
);
307 ret
[server
] = NT_STATUS_IS_OK(res
);
308 status
[server
] = smbcli_nt_error(cli
[server
][conn
]->tree
);
311 (!hide_unlock_fails
&& !NT_STATUS_EQUAL(status
[0],status
[1]))) {
312 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
314 (double)start
, (double)len
,
315 nt_errstr(status
[0]), nt_errstr(status
[1]));
317 if (!hide_unlock_fails
&& !NT_STATUS_EQUAL(status
[0],status
[1]))
322 /* reopen the file */
323 for (server
=0;server
<NSERVERS
;server
++) {
324 smbcli_close(cli
[server
][conn
]->tree
, fnum
[server
][conn
][f
]);
325 fnum
[server
][conn
][f
] = -1;
327 for (server
=0;server
<NSERVERS
;server
++) {
328 fnum
[server
][conn
][f
] = smbcli_open(cli
[server
][conn
]->tree
, FILENAME
,
331 if (fnum
[server
][conn
][f
] == -1) {
332 printf("failed to reopen on share%d\n", server
);
337 printf("reopen conn=%u f=%u\n",
346 static void close_files(struct smbcli_state
*cli
[NSERVERS
][NCONNECTIONS
],
347 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
])
351 for (server
=0;server
<NSERVERS
;server
++)
352 for (conn
=0;conn
<NCONNECTIONS
;conn
++)
353 for (f
=0;f
<NFILES
;f
++) {
354 if (fnum
[server
][conn
][f
] != -1) {
355 smbcli_close(cli
[server
][conn
]->tree
, fnum
[server
][conn
][f
]);
356 fnum
[server
][conn
][f
] = -1;
359 for (server
=0;server
<NSERVERS
;server
++) {
360 smbcli_unlink(cli
[server
][0]->tree
, FILENAME
);
364 static void open_files(struct smbcli_state
*cli
[NSERVERS
][NCONNECTIONS
],
365 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
])
369 for (server
=0;server
<NSERVERS
;server
++)
370 for (conn
=0;conn
<NCONNECTIONS
;conn
++)
371 for (f
=0;f
<NFILES
;f
++) {
372 fnum
[server
][conn
][f
] = smbcli_open(cli
[server
][conn
]->tree
, FILENAME
,
375 if (fnum
[server
][conn
][f
] == -1) {
376 fprintf(stderr
,"Failed to open fnum[%u][%u][%u]\n",
384 static int retest(struct smbcli_state
*cli
[NSERVERS
][NCONNECTIONS
],
385 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
],
389 printf("testing %u ...\n", n
);
390 for (i
=0; i
<n
; i
++) {
391 if (i
&& i
% 100 == 0) {
395 if (recorded
[i
].needed
&&
396 !test_one(cli
, fnum
, &recorded
[i
])) return i
;
402 /* each server has two connections open to it. Each connection has two file
403 descriptors open on the file - 8 file descriptors in total
405 we then do random locking ops in tamdem on the 4 fnums from each
406 server and ensure that the results match
408 static int test_locks(struct tevent_context
*ev
,
409 struct loadparm_context
*lp_ctx
,
411 char *share
[NSERVERS
])
413 struct smbcli_state
*cli
[NSERVERS
][NCONNECTIONS
];
414 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
];
415 int n
, i
, n1
, skip
, r1
, r2
;
420 recorded
= malloc_array_p(struct record
, numops
);
422 for (n
=0; n
<numops
; n
++) {
424 if (n
< sizeof(preset
) / sizeof(preset
[0])) {
425 recorded
[n
] = preset
[n
];
428 recorded
[n
].conn
= random() % NCONNECTIONS
;
429 recorded
[n
].f
= random() % NFILES
;
430 recorded
[n
].start
= lock_base
+ ((uint_t
)random() % (lock_range
-1));
431 recorded
[n
].len
= min_length
+
432 random() % (lock_range
-(recorded
[n
].start
-lock_base
));
433 recorded
[n
].start
*= RANGE_MULTIPLE
;
434 recorded
[n
].len
*= RANGE_MULTIPLE
;
435 recorded
[n
].pid
= random()%3;
436 if (recorded
[n
].pid
== 2) {
437 recorded
[n
].pid
= 0xFFFF; /* see if its magic */
442 recorded
[n
].lock_type
= READ_LOCK
;
444 recorded
[n
].lock_type
= WRITE_LOCK
;
447 recorded
[n
].lock_op
= OP_LOCK
;
448 } else if (r2
< UNLOCK_PCT
) {
449 recorded
[n
].lock_op
= OP_UNLOCK
;
451 recorded
[n
].lock_op
= OP_REOPEN
;
453 recorded
[n
].needed
= true;
454 if (!zero_zero
&& recorded
[n
].start
==0 && recorded
[n
].len
==0) {
462 reconnect(ev
, lp_ctx
, mem_ctx
, cli
, fnum
, share
);
463 open_files(cli
, fnum
);
464 n
= retest(cli
, fnum
, numops
);
466 if (n
== numops
|| !analyze
) {
479 close_files(cli
, fnum
);
480 reconnect(ev
, lp_ctx
, mem_ctx
, cli
, fnum
, share
);
481 open_files(cli
, fnum
);
483 for (i
=0;i
<n
-skip
;i
+=skip
) {
485 printf("excluding %d-%d\n", i
, i
+skip
-1);
486 for (j
=i
;j
<i
+skip
;j
++) {
487 recorded
[j
].needed
= false;
490 close_files(cli
, fnum
);
491 open_files(cli
, fnum
);
493 m
= retest(cli
, fnum
, n
);
495 for (j
=i
;j
<i
+skip
;j
++) {
496 recorded
[j
].needed
= true;
499 if (i
+(skip
-1) < m
) {
500 memmove(&recorded
[i
], &recorded
[i
+skip
],
501 (m
-(i
+skip
-1))*sizeof(recorded
[0]));
510 printf("skip=%d\n", skip
);
517 close_files(cli
, fnum
);
518 reconnect(ev
, lp_ctx
, mem_ctx
, cli
, fnum
, share
);
519 open_files(cli
, fnum
);
521 n1
= retest(cli
, fnum
, n
);
523 printf("ERROR - inconsistent result (%u %u)\n", n1
, n
);
525 close_files(cli
, fnum
);
528 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
530 recorded
[i
].lock_type
,
533 (double)recorded
[i
].start
,
534 (double)recorded
[i
].len
,
543 static void usage(poptContext pc
)
545 printf("Usage:\n\tlocktest //server1/share1 //server2/share2 [options..]\n");
546 poptPrintUsage(pc
, stdout
, 0);
549 /****************************************************************************
551 ****************************************************************************/
552 int main(int argc
,char *argv
[])
554 char *share
[NSERVERS
];
557 int username_count
=0;
558 struct tevent_context
*ev
;
559 struct loadparm_context
*lp_ctx
;
563 enum {OPT_UNCLIST
=1000};
564 struct poptOption long_options
[] = {
566 {"seed", 0, POPT_ARG_INT
, &seed
, 0, "Seed to use for randomizer", NULL
},
567 {"num-ops", 0, POPT_ARG_INT
, &numops
, 0, "num ops", NULL
},
568 {"lockrange", 0, POPT_ARG_INT
, &lock_range
,0, "locking range", NULL
},
569 {"lockbase", 0, POPT_ARG_INT
, &lock_base
, 0, "locking base", NULL
},
570 {"minlength", 0, POPT_ARG_INT
, &min_length
,0, "min lock length", NULL
},
571 {"hidefails", 0, POPT_ARG_NONE
, &hide_unlock_fails
,0,"hide unlock fails", NULL
},
572 {"oplocks", 0, POPT_ARG_NONE
, &use_oplocks
,0, "use oplocks", NULL
},
573 {"showall", 0, POPT_ARG_NONE
, &showall
, 0, "display all operations", NULL
},
574 {"analyse", 0, POPT_ARG_NONE
, &analyze
, 0, "do backtrack analysis", NULL
},
575 {"zerozero", 0, POPT_ARG_NONE
, &zero_zero
, 0, "do zero/zero lock", NULL
},
576 {"exacterrors", 0, POPT_ARG_NONE
, &exact_error_codes
,0,"use exact error codes", NULL
},
577 {"unclist", 0, POPT_ARG_STRING
, NULL
, OPT_UNCLIST
, "unclist", NULL
},
578 { "user", 'U', POPT_ARG_STRING
, NULL
, 'U', "Set the network username", "[DOMAIN/]USERNAME[%PASSWORD]" },
580 POPT_COMMON_CONNECTION
581 POPT_COMMON_CREDENTIALS
589 pc
= poptGetContext("locktest", argc
, (const char **) argv
, long_options
,
590 POPT_CONTEXT_KEEP_FIRST
);
592 poptSetOtherOptionHelp(pc
, "<unc1> <unc2>");
594 lp_ctx
= cmdline_lp_ctx
;
595 servers
[0] = cli_credentials_init(talloc_autofree_context());
596 servers
[1] = cli_credentials_init(talloc_autofree_context());
597 cli_credentials_guess(servers
[0], lp_ctx
);
598 cli_credentials_guess(servers
[1], lp_ctx
);
600 while((opt
= poptGetNextOpt(pc
)) != -1) {
603 lp_set_cmdline(cmdline_lp_ctx
, "torture:unclist", poptGetOptArg(pc
));
606 if (username_count
== 2) {
610 cli_credentials_parse_string(servers
[username_count
], poptGetOptArg(pc
), CRED_SPECIFIED
);
616 argv_new
= discard_const_p(char *, poptGetArgs(pc
));
618 for (i
=0; i
<argc
; i
++) {
619 if (argv_new
[i
] == NULL
) {
625 if (!(argc_new
>= 3)) {
630 setup_logging("locktest", DEBUG_STDOUT
);
632 for (server
=0;server
<NSERVERS
;server
++) {
633 share
[server
] = argv_new
[1+server
];
634 all_string_sub(share
[server
],"/","\\",0);
637 lp_ctx
= cmdline_lp_ctx
;
639 if (username_count
== 0) {
643 if (username_count
== 1) {
644 servers
[1] = servers
[0];
647 ev
= s4_event_context_init(talloc_autofree_context());
651 DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n",
652 seed
, lock_base
, lock_range
, min_length
));
655 return test_locks(ev
, lp_ctx
, NULL
, share
);