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 "libsmb/libsmb.h"
22 #include "system/filesys.h"
23 #include "locking/proto.h"
24 #include "libsmb/nmblib.h"
25 #include "../libcli/smb/smbXcli_base.h"
27 static fstring password
[2];
28 static fstring username
[2];
31 static bool use_kerberos
;
32 static int numops
= 1000;
35 static bool hide_unlock_fails
;
36 static bool use_oplocks
;
37 static unsigned lock_range
= 100;
38 static unsigned lock_base
= 0;
39 static unsigned min_length
= 0;
40 static bool exact_error_codes
;
41 static bool zero_zero
;
46 #define FILENAME "\\locktest.dat"
51 #define RANGE_MULTIPLE 1
53 #define NCONNECTIONS 2
55 #define LOCK_TIMEOUT 0
57 #define NASTY_POSIX_LOCK_HACK 0
59 enum lock_op
{OP_LOCK
, OP_UNLOCK
, OP_REOPEN
};
61 static const char *lock_op_type(int op
)
63 if (op
== WRITE_LOCK
) return "write";
64 else if (op
== READ_LOCK
) return "read";
68 static const char *lock_op_name(enum lock_op op
)
70 if (op
== OP_LOCK
) return "lock";
71 else if (op
== OP_UNLOCK
) return "unlock";
77 enum brl_type lock_type
;
86 static struct record preset
[] = {
87 {OP_LOCK
, WRITE_LOCK
, 0, 0, 2, 0, 1},
88 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
89 {OP_LOCK
, WRITE_LOCK
, 0, 0, 3, 0, 1},
90 {OP_UNLOCK
, 0 , 0, 0, 2, 0, 1},
91 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
93 {OP_LOCK
, READ_LOCK
, 0, 0, 2, 0, 1},
94 {OP_LOCK
, READ_LOCK
, 0, 0, 1, 1, 1},
95 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
96 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
98 {OP_LOCK
, READ_LOCK
, 0, 0, 2, 0, 1},
99 {OP_LOCK
, WRITE_LOCK
, 0, 0, 3, 1, 1},
100 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
101 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
103 {OP_LOCK
, READ_LOCK
, 0, 0, 2, 0, 1},
104 {OP_LOCK
, WRITE_LOCK
, 0, 0, 1, 1, 1},
105 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
106 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
108 {OP_LOCK
, WRITE_LOCK
, 0, 0, 2, 0, 1},
109 {OP_LOCK
, READ_LOCK
, 0, 0, 1, 1, 1},
110 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
111 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
113 {OP_LOCK
, WRITE_LOCK
, 0, 0, 2, 0, 1},
114 {OP_LOCK
, READ_LOCK
, 0, 0, 3, 1, 1},
115 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
116 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
121 static struct record
*recorded
;
123 static void print_brl(struct file_id id
,
124 struct server_id pid
,
125 enum brl_type lock_type
,
126 enum brl_flavour lock_flav
,
131 #if NASTY_POSIX_LOCK_HACK
133 static SMB_INO_T lastino
;
135 if (lastino
!= ino
) {
138 "egrep POSIX.*%u /proc/locks", (int)ino
) > 0) {
147 printf("%s %s %s %.0f:%.0f(%.0f)\n",
148 procid_str_static(&pid
), file_id_string_tos(&id
),
149 lock_type
==READ_LOCK
?"R":"W",
150 (double)start
, (double)start
+size
-1,(double)size
);
155 static void show_locks(void)
157 brl_forall(print_brl
, NULL
);
158 /* system("cat /proc/locks"); */
162 /*****************************************************
163 return a connection to a server
164 *******************************************************/
165 static struct cli_state
*connect_one(char *share
, int snum
)
175 fstrcpy(server
,share
+2);
176 share
= strchr_m(server
,'\\');
177 if (!share
) return NULL
;
183 slprintf(myname
,sizeof(myname
), "lock-%lu-%u", (unsigned long)getpid(), count
++);
185 /* have to open a new connection */
188 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
191 flags
|= CLI_FULL_CONNECTION_OPLOCKS
;
194 status
= cli_connect_nb(server_n
, NULL
, 0, 0x20, myname
,
195 SMB_SIGNING_DEFAULT
, flags
, &c
);
196 if (!NT_STATUS_IS_OK(status
)) {
197 DEBUG(0, ("Connection to %s failed. Error %s\n", server_n
,
202 status
= smbXcli_negprot(c
->conn
, c
->timeout
, PROTOCOL_CORE
,
204 if (!NT_STATUS_IS_OK(status
)) {
205 DEBUG(0, ("protocol negotiation failed: %s\n",
212 char *pass
= getpass("Password: ");
214 fstrcpy(password
[0], pass
);
215 fstrcpy(password
[1], pass
);
220 strlcpy(password
[1], password
[0],sizeof(password
[1]));
221 strlcpy(username
[1], username
[0],sizeof(username
[1]));
224 status
= cli_session_setup(c
, username
[snum
],
225 password
[snum
], strlen(password
[snum
]),
226 password
[snum
], strlen(password
[snum
]),
228 if (!NT_STATUS_IS_OK(status
)) {
229 DEBUG(0,("session setup failed: %s\n", nt_errstr(status
)));
234 * These next two lines are needed to emulate
235 * old client behaviour for people who have
236 * scripts based on client output.
237 * QUESTION ? Do we want to have a 'client compatibility
238 * mode to turn these on/off ? JRA.
241 if (*c
->server_domain
|| *c
->server_os
|| *c
->server_type
)
242 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
243 c
->server_domain
,c
->server_os
,c
->server_type
));
245 DEBUG(4,(" session setup ok\n"));
247 status
= cli_tree_connect(c
, share
, "?????", password
[snum
],
248 strlen(password
[snum
])+1);
249 if (!NT_STATUS_IS_OK(status
)) {
250 DEBUG(0,("tree connect failed: %s\n", nt_errstr(status
)));
255 DEBUG(4,(" tconx ok\n"));
261 static void reconnect(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
], uint16_t fnum
[NSERVERS
][NCONNECTIONS
][NFILES
],
262 char *share
[NSERVERS
])
266 for (server
=0;server
<NSERVERS
;server
++)
267 for (conn
=0;conn
<NCONNECTIONS
;conn
++) {
268 if (cli
[server
][conn
]) {
269 for (f
=0;f
<NFILES
;f
++) {
270 if (fnum
[server
][conn
][f
] != (uint16_t)-1) {
271 cli_close(cli
[server
][conn
], fnum
[server
][conn
][f
]);
272 fnum
[server
][conn
][f
] = (uint16_t)-1;
275 cli_ulogoff(cli
[server
][conn
]);
276 cli_shutdown(cli
[server
][conn
]);
278 cli
[server
][conn
] = connect_one(share
[server
], server
);
279 if (!cli
[server
][conn
]) {
280 DEBUG(0,("Failed to connect to %s\n", share
[server
]));
288 static bool test_one(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
289 uint16_t fnum
[NSERVERS
][NCONNECTIONS
][NFILES
],
292 unsigned conn
= rec
->conn
;
294 uint64_t start
= rec
->start
;
295 uint64_t len
= rec
->len
;
296 enum brl_type op
= rec
->lock_type
;
298 NTSTATUS status
[NSERVERS
];
300 switch (rec
->lock_op
) {
303 for (server
=0;server
<NSERVERS
;server
++) {
304 status
[server
] = cli_lock64(cli
[server
][conn
],
305 fnum
[server
][conn
][f
],
306 start
, len
, LOCK_TIMEOUT
,
308 if (!exact_error_codes
&&
309 NT_STATUS_EQUAL(status
[server
],
310 NT_STATUS_FILE_LOCK_CONFLICT
)) {
311 status
[server
] = NT_STATUS_LOCK_NOT_GRANTED
;
314 if (showall
|| !NT_STATUS_EQUAL(status
[0],status
[1])) {
315 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
317 (double)start
, (double)len
,
318 op
==READ_LOCK
?"READ_LOCK":"WRITE_LOCK",
319 nt_errstr(status
[0]), nt_errstr(status
[1]));
321 if (showall
|| !NT_STATUS_EQUAL(status
[0],status
[1])) show_locks();
322 if (!NT_STATUS_EQUAL(status
[0],status
[1])) return False
;
327 for (server
=0;server
<NSERVERS
;server
++) {
328 status
[server
] = cli_unlock64(cli
[server
][conn
],
329 fnum
[server
][conn
][f
],
333 (!hide_unlock_fails
&& !NT_STATUS_EQUAL(status
[0],status
[1]))) {
334 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
336 (double)start
, (double)len
,
337 nt_errstr(status
[0]), nt_errstr(status
[1]));
339 if (showall
|| !NT_STATUS_EQUAL(status
[0],status
[1])) show_locks();
340 if (!hide_unlock_fails
&& !NT_STATUS_EQUAL(status
[0],status
[1]))
345 /* reopen the file */
346 for (server
=0;server
<NSERVERS
;server
++) {
347 cli_close(cli
[server
][conn
], fnum
[server
][conn
][f
]);
348 fnum
[server
][conn
][f
] = (uint16_t)-1;
350 for (server
=0;server
<NSERVERS
;server
++) {
351 fnum
[server
][conn
][f
] = (uint16_t)-1;
352 if (!NT_STATUS_IS_OK(cli_openx(cli
[server
][conn
], FILENAME
,
354 DENY_NONE
, &fnum
[server
][conn
][f
]))) {
355 printf("failed to reopen on share%d\n", server
);
360 printf("reopen conn=%u f=%u\n",
370 static void close_files(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
371 uint16_t fnum
[NSERVERS
][NCONNECTIONS
][NFILES
])
375 for (server
=0;server
<NSERVERS
;server
++)
376 for (conn
=0;conn
<NCONNECTIONS
;conn
++)
377 for (f
=0;f
<NFILES
;f
++) {
378 if (fnum
[server
][conn
][f
] != (uint16_t)-1) {
379 cli_close(cli
[server
][conn
], fnum
[server
][conn
][f
]);
380 fnum
[server
][conn
][f
] = (uint16_t)-1;
383 for (server
=0;server
<NSERVERS
;server
++) {
384 cli_unlink(cli
[server
][0], FILENAME
, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
388 static void open_files(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
389 uint16_t fnum
[NSERVERS
][NCONNECTIONS
][NFILES
])
393 for (server
=0;server
<NSERVERS
;server
++)
394 for (conn
=0;conn
<NCONNECTIONS
;conn
++)
395 for (f
=0;f
<NFILES
;f
++) {
396 fnum
[server
][conn
][f
] = (uint16_t)-1;
397 if (!NT_STATUS_IS_OK(cli_openx(cli
[server
][conn
], FILENAME
,
400 &fnum
[server
][conn
][f
]))) {
401 fprintf(stderr
,"Failed to open fnum[%u][%u][%u]\n",
409 static int retest(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
410 uint16_t fnum
[NSERVERS
][NCONNECTIONS
][NFILES
],
414 printf("testing %u ...\n", n
);
415 for (i
=0; i
<n
; i
++) {
416 if (i
&& i
% 100 == 0) {
420 if (recorded
[i
].needed
&&
421 !test_one(cli
, fnum
, &recorded
[i
])) return i
;
427 /* each server has two connections open to it. Each connection has two file
428 descriptors open on the file - 8 file descriptors in total
430 we then do random locking ops in tamdem on the 4 fnums from each
431 server and ensure that the results match
433 static void test_locks(char *share
[NSERVERS
])
435 struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
];
436 uint16_t fnum
[NSERVERS
][NCONNECTIONS
][NFILES
];
437 int n
, i
, n1
, skip
, r1
, r2
;
442 recorded
= SMB_MALLOC_ARRAY(struct record
, numops
);
444 for (n
=0; n
<numops
; n
++) {
446 if (n
< sizeof(preset
) / sizeof(preset
[0])) {
447 recorded
[n
] = preset
[n
];
450 recorded
[n
].conn
= random() % NCONNECTIONS
;
451 recorded
[n
].f
= random() % NFILES
;
452 recorded
[n
].start
= lock_base
+ ((unsigned)random() % (lock_range
-1));
453 recorded
[n
].len
= min_length
+
454 random() % (lock_range
-(recorded
[n
].start
-lock_base
));
455 recorded
[n
].start
*= RANGE_MULTIPLE
;
456 recorded
[n
].len
*= RANGE_MULTIPLE
;
460 recorded
[n
].lock_type
= READ_LOCK
;
462 recorded
[n
].lock_type
= WRITE_LOCK
;
465 recorded
[n
].lock_op
= OP_LOCK
;
466 } else if (r2
< UNLOCK_PCT
) {
467 recorded
[n
].lock_op
= OP_UNLOCK
;
469 recorded
[n
].lock_op
= OP_REOPEN
;
471 recorded
[n
].needed
= True
;
472 if (!zero_zero
&& recorded
[n
].start
==0 && recorded
[n
].len
==0) {
480 reconnect(cli
, fnum
, share
);
481 open_files(cli
, fnum
);
482 n
= retest(cli
, fnum
, numops
);
484 if (n
== numops
|| !analyze
) return;
492 close_files(cli
, fnum
);
493 reconnect(cli
, fnum
, share
);
494 open_files(cli
, fnum
);
496 for (i
=0;i
<n
-skip
;i
+=skip
) {
498 printf("excluding %d-%d\n", i
, i
+skip
-1);
499 for (j
=i
;j
<i
+skip
;j
++) {
500 recorded
[j
].needed
= False
;
503 close_files(cli
, fnum
);
504 open_files(cli
, fnum
);
506 m
= retest(cli
, fnum
, n
);
508 for (j
=i
;j
<i
+skip
;j
++) {
509 recorded
[j
].needed
= True
;
512 if (i
+(skip
-1) < m
) {
513 memmove(&recorded
[i
], &recorded
[i
+skip
],
514 (m
-(i
+skip
-1))*sizeof(recorded
[0]));
523 printf("skip=%d\n", skip
);
530 close_files(cli
, fnum
);
531 reconnect(cli
, fnum
, share
);
532 open_files(cli
, fnum
);
534 n1
= retest(cli
, fnum
, n
);
536 printf("ERROR - inconsistent result (%u %u)\n", n1
, n
);
538 close_files(cli
, fnum
);
541 printf("{%s, %s, conn = %u, file = %u, start = %.0f, len = %.0f, %u},\n",
542 lock_op_name(recorded
[i
].lock_op
),
543 lock_op_type(recorded
[i
].lock_type
),
546 (double)recorded
[i
].start
,
547 (double)recorded
[i
].len
,
554 static void usage(void)
558 locktest //server1/share1 //server2/share2 [options..]\n\
560 -U user%%pass (may be specified twice)\n\
564 -u hide unlock fails\n\
566 -A analyse for minimal ops\n\
568 -E enable exact error code checking\n\
569 -Z enable the zero/zero lock\n\
570 -R range set lock range\n\
571 -B base set lock base\n\
572 -M min set min lock length\n\
576 /****************************************************************************
578 ****************************************************************************/
579 int main(int argc
,char *argv
[])
581 char *share
[NSERVERS
];
590 if (argc
< 3 || argv
[1][0] == '-') {
595 setup_logging(argv
[0], DEBUG_STDOUT
);
597 for (server
=0;server
<NSERVERS
;server
++) {
598 share
[server
] = argv
[1+server
];
599 all_string_sub(share
[server
],"/","\\",0);
605 lp_load_global(get_dyn_CONFIGFILE());
608 if (getenv("USER")) {
609 fstrcpy(username
[0],getenv("USER"));
610 fstrcpy(username
[1],getenv("USER"));
615 while ((opt
= getopt(argc
, argv
, "U:s:ho:aAW:OkR:B:M:EZ")) != EOF
) {
621 d_printf("No kerberos support compiled in\n");
628 d_printf("Max of 2 usernames\n");
631 fstrcpy(username
[got_pass
],optarg
);
632 p
= strchr_m(username
[got_pass
],'%');
635 fstrcpy(password
[got_pass
], p
+1);
640 lock_range
= strtol(optarg
, NULL
, 0);
643 lock_base
= strtol(optarg
, NULL
, 0);
646 min_length
= strtol(optarg
, NULL
, 0);
652 hide_unlock_fails
= True
;
655 numops
= atoi(optarg
);
670 exact_error_codes
= True
;
676 printf("Unknown option %c (%d)\n", (char)opt
, opt
);
681 if(use_kerberos
&& !got_user
) got_pass
= True
;
686 DEBUG(0,("seed=%u\n", seed
));