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 "system/filesys.h"
22 #include "locking/proto.h"
23 #include "libsmb/nmblib.h"
25 static fstring password
[2];
26 static fstring username
[2];
29 static bool use_kerberos
;
30 static int numops
= 1000;
33 static bool hide_unlock_fails
;
34 static bool use_oplocks
;
35 static unsigned lock_range
= 100;
36 static unsigned lock_base
= 0;
37 static unsigned min_length
= 0;
38 static bool exact_error_codes
;
39 static bool zero_zero
;
44 #define FILENAME "\\locktest.dat"
49 #define RANGE_MULTIPLE 1
51 #define NCONNECTIONS 2
53 #define LOCK_TIMEOUT 0
55 #define NASTY_POSIX_LOCK_HACK 0
57 enum lock_op
{OP_LOCK
, OP_UNLOCK
, OP_REOPEN
};
59 static const char *lock_op_type(int op
)
61 if (op
== WRITE_LOCK
) return "write";
62 else if (op
== READ_LOCK
) return "read";
66 static const char *lock_op_name(enum lock_op op
)
68 if (op
== OP_LOCK
) return "lock";
69 else if (op
== OP_UNLOCK
) return "unlock";
75 enum brl_type lock_type
;
84 static struct record preset
[] = {
85 {OP_LOCK
, WRITE_LOCK
, 0, 0, 2, 0, 1},
86 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
87 {OP_LOCK
, WRITE_LOCK
, 0, 0, 3, 0, 1},
88 {OP_UNLOCK
, 0 , 0, 0, 2, 0, 1},
89 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
91 {OP_LOCK
, READ_LOCK
, 0, 0, 2, 0, 1},
92 {OP_LOCK
, READ_LOCK
, 0, 0, 1, 1, 1},
93 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
94 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
96 {OP_LOCK
, READ_LOCK
, 0, 0, 2, 0, 1},
97 {OP_LOCK
, WRITE_LOCK
, 0, 0, 3, 1, 1},
98 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
99 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
101 {OP_LOCK
, READ_LOCK
, 0, 0, 2, 0, 1},
102 {OP_LOCK
, WRITE_LOCK
, 0, 0, 1, 1, 1},
103 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
104 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
106 {OP_LOCK
, WRITE_LOCK
, 0, 0, 2, 0, 1},
107 {OP_LOCK
, READ_LOCK
, 0, 0, 1, 1, 1},
108 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
109 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
111 {OP_LOCK
, WRITE_LOCK
, 0, 0, 2, 0, 1},
112 {OP_LOCK
, READ_LOCK
, 0, 0, 3, 1, 1},
113 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
114 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
119 static struct record
*recorded
;
121 static void print_brl(struct file_id id
,
122 struct server_id pid
,
123 enum brl_type lock_type
,
124 enum brl_flavour lock_flav
,
129 #if NASTY_POSIX_LOCK_HACK
131 static SMB_INO_T lastino
;
133 if (lastino
!= ino
) {
136 "egrep POSIX.*%u /proc/locks", (int)ino
) > 0) {
145 printf("%s %s %s %.0f:%.0f(%.0f)\n",
146 procid_str_static(&pid
), file_id_string_tos(&id
),
147 lock_type
==READ_LOCK
?"R":"W",
148 (double)start
, (double)start
+size
-1,(double)size
);
153 static void show_locks(void)
155 brl_forall(print_brl
, NULL
);
156 /* system("cat /proc/locks"); */
160 /*****************************************************
161 return a connection to a server
162 *******************************************************/
163 static struct cli_state
*connect_one(char *share
, int snum
)
166 struct nmb_name called
, calling
;
169 struct sockaddr_storage ss
;
174 fstrcpy(server
,share
+2);
175 share
= strchr_m(server
,'\\');
176 if (!share
) return NULL
;
184 slprintf(myname
,sizeof(myname
), "lock-%lu-%u", (unsigned long)getpid(), count
++);
186 make_nmb_name(&calling
, myname
, 0x0);
187 make_nmb_name(&called
, server
, 0x20);
192 /* have to open a new connection */
193 if (!(c
=cli_initialise())) {
194 DEBUG(0,("Connection to %s failed\n", server_n
));
198 status
= cli_connect(c
, server_n
, &ss
);
199 if (!NT_STATUS_IS_OK(status
)) {
200 DEBUG(0,("Connection to %s failed. Error %s\n", server_n
, nt_errstr(status
) ));
204 c
->use_kerberos
= use_kerberos
;
206 if (!cli_session_request(c
, &calling
, &called
)) {
207 DEBUG(0,("session request to %s failed\n", called
.name
));
209 if (strcmp(called
.name
, "*SMBSERVER")) {
210 make_nmb_name(&called
, "*SMBSERVER", 0x20);
216 DEBUG(4,(" session request ok\n"));
218 status
= cli_negprot(c
);
219 if (!NT_STATUS_IS_OK(status
)) {
220 DEBUG(0, ("protocol negotiation failed: %s\n",
227 char *pass
= getpass("Password: ");
229 fstrcpy(password
[0], pass
);
230 fstrcpy(password
[1], pass
);
235 fstrcpy(password
[1], password
[0]);
236 fstrcpy(username
[1], username
[0]);
239 status
= cli_session_setup(c
, username
[snum
],
240 password
[snum
], strlen(password
[snum
]),
241 password
[snum
], strlen(password
[snum
]),
243 if (!NT_STATUS_IS_OK(status
)) {
244 DEBUG(0,("session setup failed: %s\n", nt_errstr(status
)));
249 * These next two lines are needed to emulate
250 * old client behaviour for people who have
251 * scripts based on client output.
252 * QUESTION ? Do we want to have a 'client compatibility
253 * mode to turn these on/off ? JRA.
256 if (*c
->server_domain
|| *c
->server_os
|| *c
->server_type
)
257 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
258 c
->server_domain
,c
->server_os
,c
->server_type
));
260 DEBUG(4,(" session setup ok\n"));
262 status
= cli_tcon_andx(c
, share
, "?????", password
[snum
],
263 strlen(password
[snum
])+1);
264 if (!NT_STATUS_IS_OK(status
)) {
265 DEBUG(0,("tree connect failed: %s\n", nt_errstr(status
)));
270 DEBUG(4,(" tconx ok\n"));
272 c
->use_oplocks
= use_oplocks
;
278 static void reconnect(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
], uint16_t fnum
[NSERVERS
][NCONNECTIONS
][NFILES
],
279 char *share
[NSERVERS
])
283 for (server
=0;server
<NSERVERS
;server
++)
284 for (conn
=0;conn
<NCONNECTIONS
;conn
++) {
285 if (cli
[server
][conn
]) {
286 for (f
=0;f
<NFILES
;f
++) {
287 if (fnum
[server
][conn
][f
] != (uint16_t)-1) {
288 cli_close(cli
[server
][conn
], fnum
[server
][conn
][f
]);
289 fnum
[server
][conn
][f
] = (uint16_t)-1;
292 cli_ulogoff(cli
[server
][conn
]);
293 cli_shutdown(cli
[server
][conn
]);
295 cli
[server
][conn
] = connect_one(share
[server
], server
);
296 if (!cli
[server
][conn
]) {
297 DEBUG(0,("Failed to connect to %s\n", share
[server
]));
305 static bool test_one(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
306 uint16_t fnum
[NSERVERS
][NCONNECTIONS
][NFILES
],
309 unsigned conn
= rec
->conn
;
311 uint64_t start
= rec
->start
;
312 uint64_t len
= rec
->len
;
313 enum brl_type op
= rec
->lock_type
;
316 NTSTATUS status
[NSERVERS
];
318 switch (rec
->lock_op
) {
321 for (server
=0;server
<NSERVERS
;server
++) {
322 ret
[server
] = cli_lock64(cli
[server
][conn
],
323 fnum
[server
][conn
][f
],
324 start
, len
, LOCK_TIMEOUT
, op
);
325 status
[server
] = cli_nt_error(cli
[server
][conn
]);
326 if (!exact_error_codes
&&
327 NT_STATUS_EQUAL(status
[server
],
328 NT_STATUS_FILE_LOCK_CONFLICT
)) {
329 status
[server
] = NT_STATUS_LOCK_NOT_GRANTED
;
332 if (showall
|| !NT_STATUS_EQUAL(status
[0],status
[1])) {
333 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
335 (double)start
, (double)len
,
336 op
==READ_LOCK
?"READ_LOCK":"WRITE_LOCK",
337 nt_errstr(status
[0]), nt_errstr(status
[1]));
339 if (showall
|| !NT_STATUS_EQUAL(status
[0],status
[1])) show_locks();
340 if (!NT_STATUS_EQUAL(status
[0],status
[1])) return False
;
345 for (server
=0;server
<NSERVERS
;server
++) {
346 ret
[server
] = NT_STATUS_IS_OK(cli_unlock64(cli
[server
][conn
],
347 fnum
[server
][conn
][f
],
349 status
[server
] = cli_nt_error(cli
[server
][conn
]);
352 (!hide_unlock_fails
&& !NT_STATUS_EQUAL(status
[0],status
[1]))) {
353 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
355 (double)start
, (double)len
,
356 nt_errstr(status
[0]), nt_errstr(status
[1]));
358 if (showall
|| !NT_STATUS_EQUAL(status
[0],status
[1])) show_locks();
359 if (!hide_unlock_fails
&& !NT_STATUS_EQUAL(status
[0],status
[1]))
364 /* reopen the file */
365 for (server
=0;server
<NSERVERS
;server
++) {
366 cli_close(cli
[server
][conn
], fnum
[server
][conn
][f
]);
367 fnum
[server
][conn
][f
] = (uint16_t)-1;
369 for (server
=0;server
<NSERVERS
;server
++) {
370 fnum
[server
][conn
][f
] = (uint16_t)-1;
371 if (!NT_STATUS_IS_OK(cli_open(cli
[server
][conn
], FILENAME
,
373 DENY_NONE
, &fnum
[server
][conn
][f
]))) {
374 printf("failed to reopen on share%d\n", server
);
379 printf("reopen conn=%u f=%u\n",
389 static void close_files(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
390 uint16_t fnum
[NSERVERS
][NCONNECTIONS
][NFILES
])
394 for (server
=0;server
<NSERVERS
;server
++)
395 for (conn
=0;conn
<NCONNECTIONS
;conn
++)
396 for (f
=0;f
<NFILES
;f
++) {
397 if (fnum
[server
][conn
][f
] != (uint16_t)-1) {
398 cli_close(cli
[server
][conn
], fnum
[server
][conn
][f
]);
399 fnum
[server
][conn
][f
] = (uint16_t)-1;
402 for (server
=0;server
<NSERVERS
;server
++) {
403 cli_unlink(cli
[server
][0], FILENAME
, aSYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
407 static void open_files(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
408 uint16_t fnum
[NSERVERS
][NCONNECTIONS
][NFILES
])
412 for (server
=0;server
<NSERVERS
;server
++)
413 for (conn
=0;conn
<NCONNECTIONS
;conn
++)
414 for (f
=0;f
<NFILES
;f
++) {
415 fnum
[server
][conn
][f
] = (uint16_t)-1;
416 if (!NT_STATUS_IS_OK(cli_open(cli
[server
][conn
], FILENAME
,
419 &fnum
[server
][conn
][f
]))) {
420 fprintf(stderr
,"Failed to open fnum[%u][%u][%u]\n",
428 static int retest(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
429 uint16_t fnum
[NSERVERS
][NCONNECTIONS
][NFILES
],
433 printf("testing %u ...\n", n
);
434 for (i
=0; i
<n
; i
++) {
435 if (i
&& i
% 100 == 0) {
439 if (recorded
[i
].needed
&&
440 !test_one(cli
, fnum
, &recorded
[i
])) return i
;
446 /* each server has two connections open to it. Each connection has two file
447 descriptors open on the file - 8 file descriptors in total
449 we then do random locking ops in tamdem on the 4 fnums from each
450 server and ensure that the results match
452 static void test_locks(char *share
[NSERVERS
])
454 struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
];
455 uint16_t fnum
[NSERVERS
][NCONNECTIONS
][NFILES
];
456 int n
, i
, n1
, skip
, r1
, r2
;
461 recorded
= SMB_MALLOC_ARRAY(struct record
, numops
);
463 for (n
=0; n
<numops
; n
++) {
465 if (n
< sizeof(preset
) / sizeof(preset
[0])) {
466 recorded
[n
] = preset
[n
];
469 recorded
[n
].conn
= random() % NCONNECTIONS
;
470 recorded
[n
].f
= random() % NFILES
;
471 recorded
[n
].start
= lock_base
+ ((unsigned)random() % (lock_range
-1));
472 recorded
[n
].len
= min_length
+
473 random() % (lock_range
-(recorded
[n
].start
-lock_base
));
474 recorded
[n
].start
*= RANGE_MULTIPLE
;
475 recorded
[n
].len
*= RANGE_MULTIPLE
;
479 recorded
[n
].lock_type
= READ_LOCK
;
481 recorded
[n
].lock_type
= WRITE_LOCK
;
484 recorded
[n
].lock_op
= OP_LOCK
;
485 } else if (r2
< UNLOCK_PCT
) {
486 recorded
[n
].lock_op
= OP_UNLOCK
;
488 recorded
[n
].lock_op
= OP_REOPEN
;
490 recorded
[n
].needed
= True
;
491 if (!zero_zero
&& recorded
[n
].start
==0 && recorded
[n
].len
==0) {
499 reconnect(cli
, fnum
, share
);
500 open_files(cli
, fnum
);
501 n
= retest(cli
, fnum
, numops
);
503 if (n
== numops
|| !analyze
) return;
511 close_files(cli
, fnum
);
512 reconnect(cli
, fnum
, share
);
513 open_files(cli
, fnum
);
515 for (i
=0;i
<n
-skip
;i
+=skip
) {
517 printf("excluding %d-%d\n", i
, i
+skip
-1);
518 for (j
=i
;j
<i
+skip
;j
++) {
519 recorded
[j
].needed
= False
;
522 close_files(cli
, fnum
);
523 open_files(cli
, fnum
);
525 m
= retest(cli
, fnum
, n
);
527 for (j
=i
;j
<i
+skip
;j
++) {
528 recorded
[j
].needed
= True
;
531 if (i
+(skip
-1) < m
) {
532 memmove(&recorded
[i
], &recorded
[i
+skip
],
533 (m
-(i
+skip
-1))*sizeof(recorded
[0]));
542 printf("skip=%d\n", skip
);
549 close_files(cli
, fnum
);
550 reconnect(cli
, fnum
, share
);
551 open_files(cli
, fnum
);
553 n1
= retest(cli
, fnum
, n
);
555 printf("ERROR - inconsistent result (%u %u)\n", n1
, n
);
557 close_files(cli
, fnum
);
560 printf("{%s, %s, conn = %u, file = %u, start = %.0f, len = %.0f, %u},\n",
561 lock_op_name(recorded
[i
].lock_op
),
562 lock_op_type(recorded
[i
].lock_type
),
565 (double)recorded
[i
].start
,
566 (double)recorded
[i
].len
,
573 static void usage(void)
577 locktest //server1/share1 //server2/share2 [options..]\n\
579 -U user%%pass (may be specified twice)\n\
583 -u hide unlock fails\n\
585 -A analyse for minimal ops\n\
587 -E enable exact error code checking\n\
588 -Z enable the zero/zero lock\n\
589 -R range set lock range\n\
590 -B base set lock base\n\
591 -M min set min lock length\n\
595 /****************************************************************************
597 ****************************************************************************/
598 int main(int argc
,char *argv
[])
600 char *share
[NSERVERS
];
609 if (argc
< 3 || argv
[1][0] == '-') {
614 setup_logging(argv
[0], DEBUG_STDOUT
);
616 for (server
=0;server
<NSERVERS
;server
++) {
617 share
[server
] = argv
[1+server
];
618 all_string_sub(share
[server
],"/","\\",0);
624 lp_load(get_dyn_CONFIGFILE(),True
,False
,False
,True
);
627 if (getenv("USER")) {
628 fstrcpy(username
[0],getenv("USER"));
629 fstrcpy(username
[1],getenv("USER"));
634 while ((opt
= getopt(argc
, argv
, "U:s:ho:aAW:OkR:B:M:EZ")) != EOF
) {
640 d_printf("No kerberos support compiled in\n");
647 d_printf("Max of 2 usernames\n");
650 fstrcpy(username
[got_pass
],optarg
);
651 p
= strchr_m(username
[got_pass
],'%');
654 fstrcpy(password
[got_pass
], p
+1);
659 lock_range
= strtol(optarg
, NULL
, 0);
662 lock_base
= strtol(optarg
, NULL
, 0);
665 min_length
= strtol(optarg
, NULL
, 0);
671 hide_unlock_fails
= True
;
674 numops
= atoi(optarg
);
689 exact_error_codes
= True
;
695 printf("Unknown option %c (%d)\n", (char)opt
, opt
);
700 if(use_kerberos
&& !got_user
) got_pass
= True
;
705 DEBUG(0,("seed=%u\n", seed
));