1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
|
/*
Unix SMB/Netbios implementation.
Version 2.0
randomised byte range lock tester
Copyright (C) Andrew Tridgell 1999
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#define NO_SYSLOG
#include "includes.h"
static fstring password;
static fstring username;
static int got_pass;
static int numops = 1000;
static BOOL showall;
static BOOL analyze;
static BOOL hide_unlock_fails;
static BOOL use_oplocks;
#define FILENAME "\\locktest.dat"
#define LOCKRANGE 100
#define LOCKBASE 0
/*
#define LOCKBASE (0x40000000 - 50)
*/
#define READ_PCT 50
#define LOCK_PCT 25
#define UNLOCK_PCT 65
#define RANGE_MULTIPLE 1
#define NCONNECTIONS 2
#define NFILES 2
#define LOCK_TIMEOUT 0
#define NASTY_POSIX_LOCK_HACK 0
struct record {
char r1, r2;
char conn, f;
unsigned start, len;
char needed;
};
static struct record preset[] = {
#if 0
{36, 5, 0, 0, 0, 8, 1},
{ 2, 6, 0, 1, 0, 1, 1},
{53, 92, 0, 0, 0, 0, 1},
{99, 11, 0, 0, 7, 1, 1},
#endif
};
static struct record *recorded;
static void print_brl(SMB_DEV_T dev, SMB_INO_T ino, int pid,
enum brl_type lock_type,
br_off start, br_off size)
{
#if NASTY_POSIX_LOCK_HACK
{
pstring cmd;
static SMB_INO_T lastino;
if (lastino != ino) {
slprintf(cmd, sizeof(cmd),
"egrep POSIX.*%u /proc/locks", (int)ino);
system(cmd);
}
lastino = ino;
}
#endif
printf("%6d %05x:%05x %s %.0f:%.0f(%.0f)\n",
(int)pid, (int)dev, (int)ino,
lock_type==READ_LOCK?"R":"W",
(double)start, (double)start+size-1,(double)size);
}
/*****************************************************
return a connection to a server
*******************************************************/
struct cli_state *connect_one(char *share)
{
struct cli_state *c;
struct nmb_name called, calling;
char *server_n;
fstring server;
struct in_addr ip;
extern struct in_addr ipzero;
fstring myname;
static int count;
fstrcpy(server,share+2);
share = strchr(server,'\\');
if (!share) return NULL;
*share = 0;
share++;
server_n = server;
ip = ipzero;
slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), count++);
make_nmb_name(&calling, myname, 0x0);
make_nmb_name(&called , server, 0x20);
again:
ip = ipzero;
/* have to open a new connection */
if (!(c=cli_initialise(NULL)) || (cli_set_port(c, 139) == 0) ||
!cli_connect(c, server_n, &ip)) {
DEBUG(0,("Connection to %s failed\n", server_n));
return NULL;
}
if (!cli_session_request(c, &calling, &called)) {
DEBUG(0,("session request to %s failed\n", called.name));
cli_shutdown(c);
if (strcmp(called.name, "*SMBSERVER")) {
make_nmb_name(&called , "*SMBSERVER", 0x20);
goto again;
}
return NULL;
}
DEBUG(4,(" session request ok\n"));
if (!cli_negprot(c)) {
DEBUG(0,("protocol negotiation failed\n"));
cli_shutdown(c);
return NULL;
}
if (!got_pass) {
char *pass = getpass("Password: ");
if (pass) {
pstrcpy(password, pass);
}
}
if (!cli_session_setup(c, username,
password, strlen(password),
password, strlen(password),
lp_workgroup())) {
DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
return NULL;
}
/*
* These next two lines are needed to emulate
* old client behaviour for people who have
* scripts based on client output.
* QUESTION ? Do we want to have a 'client compatibility
* mode to turn these on/off ? JRA.
*/
if (*c->server_domain || *c->server_os || *c->server_type)
DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
c->server_domain,c->server_os,c->server_type));
DEBUG(4,(" session setup ok\n"));
if (!cli_send_tconX(c, share, "?????",
password, strlen(password)+1)) {
DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
cli_shutdown(c);
return NULL;
}
DEBUG(4,(" tconx ok\n"));
c->use_oplocks = use_oplocks;
return c;
}
static void reconnect(struct cli_state *cli[2][2], int fnum[2][2][2],
char *share1, char *share2)
{
int server, conn, f;
char *share[2];
share[0] = share1;
share[1] = share2;
for (server=0;server<2;server++)
for (conn=0;conn<NCONNECTIONS;conn++) {
if (cli[server][conn]) {
for (f=0;f<NFILES;f++) {
cli_close(cli[server][conn], fnum[server][conn][f]);
}
cli_ulogoff(cli[server][conn]);
cli_shutdown(cli[server][conn]);
free(cli[server][conn]);
cli[server][conn] = NULL;
}
cli[server][conn] = connect_one(share[server]);
if (!cli[server][conn]) {
DEBUG(0,("Failed to connect to %s\n", share[server]));
exit(1);
}
}
}
static BOOL test_one(struct cli_state *cli[2][2],
int fnum[2][2][2],
struct record *rec)
{
unsigned conn = rec->conn;
unsigned f = rec->f;
unsigned start = rec->start;
unsigned len = rec->len;
unsigned r1 = rec->r1;
unsigned r2 = rec->r2;
unsigned op;
BOOL ret1, ret2;
if (r1 < READ_PCT) {
op = READ_LOCK;
} else {
op = WRITE_LOCK;
}
if (r2 < LOCK_PCT) {
/* set a lock */
ret1 = cli_lock(cli[0][conn],
fnum[0][conn][f],
start, len, LOCK_TIMEOUT, op);
ret2 = cli_lock(cli[1][conn],
fnum[1][conn][f],
start, len, LOCK_TIMEOUT, op);
if (showall || ret1 != ret2) {
printf("lock conn=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
conn, f,
start, start+len-1, len,
op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
ret1, ret2);
}
if (showall) brl_forall(print_brl);
if (ret1 != ret2) return False;
} else if (r2 < LOCK_PCT+UNLOCK_PCT) {
/* unset a lock */
ret1 = cli_unlock(cli[0][conn],
fnum[0][conn][f],
start, len);
ret2 = cli_unlock(cli[1][conn],
fnum[1][conn][f],
start, len);
if (showall || (!hide_unlock_fails && (ret1 != ret2))) {
printf("unlock conn=%u f=%u range=%u:%u(%u) -> %u:%u\n",
conn, f,
start, start+len-1, len,
ret1, ret2);
}
if (showall) brl_forall(print_brl);
if (!hide_unlock_fails && ret1 != ret2) return False;
} else {
/* reopen the file */
cli_close(cli[0][conn], fnum[0][conn][f]);
cli_close(cli[1][conn], fnum[1][conn][f]);
fnum[0][conn][f] = cli_open(cli[0][conn], FILENAME,
O_RDWR|O_CREAT,
DENY_NONE);
fnum[1][conn][f] = cli_open(cli[1][conn], FILENAME,
O_RDWR|O_CREAT,
DENY_NONE);
if (fnum[0][conn][f] == -1) {
printf("failed to reopen on share1\n");
return False;
}
if (fnum[1][conn][f] == -1) {
printf("failed to reopen on share2\n");
return False;
}
if (showall) {
printf("reopen conn=%u f=%u\n",
conn, f);
brl_forall(print_brl);
}
}
return True;
}
static void close_files(struct cli_state *cli[2][2],
int fnum[2][2][2])
{
int server, conn, f;
for (server=0;server<2;server++)
for (conn=0;conn<NCONNECTIONS;conn++)
for (f=0;f<NFILES;f++) {
if (fnum[server][conn][f] != -1) {
cli_close(cli[server][conn], fnum[server][conn][f]);
fnum[server][conn][f] = -1;
}
}
cli_unlink(cli[0][0], FILENAME);
cli_unlink(cli[1][0], FILENAME);
}
static void open_files(struct cli_state *cli[2][2],
int fnum[2][2][2])
{
int server, conn, f;
for (server=0;server<2;server++)
for (conn=0;conn<NCONNECTIONS;conn++)
for (f=0;f<NFILES;f++) {
fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
O_RDWR|O_CREAT,
DENY_NONE);
if (fnum[server][conn][f] == -1) {
fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
server, conn, f);
exit(1);
}
}
}
static int retest(struct cli_state *cli[2][2],
int fnum[2][2][2],
int n)
{
int i;
printf("testing %u ...\n", n);
for (i=0; i<n; i++) {
if (i && i % 100 == 0) {
printf("%u\n", i);
}
if (recorded[i].needed &&
!test_one(cli, fnum, &recorded[i])) return i;
}
return n;
}
/* each server has two connections open to it. Each connection has two file
descriptors open on the file - 8 file descriptors in total
we then do random locking ops in tamdem on the 4 fnums from each
server and ensure that the results match
*/
static void test_locks(char *share1, char *share2)
{
struct cli_state *cli[2][2];
int fnum[2][2][2];
int n, i, n1;
ZERO_STRUCT(fnum);
ZERO_STRUCT(cli);
recorded = (struct record *)malloc(sizeof(*recorded) * numops);
for (n=0; n<numops; n++) {
if (n < sizeof(preset) / sizeof(preset[0])) {
recorded[n] = preset[n];
} else {
recorded[n].conn = random() % NCONNECTIONS;
recorded[n].f = random() % NFILES;
recorded[n].start = LOCKBASE + ((unsigned)random() % (LOCKRANGE-1));
recorded[n].len = 1 +
random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
recorded[n].start *= RANGE_MULTIPLE;
recorded[n].len *= RANGE_MULTIPLE;
recorded[n].r1 = random() % 100;
recorded[n].r2 = random() % 100;
recorded[n].needed = True;
}
}
reconnect(cli, fnum, share1, share2);
open_files(cli, fnum);
n = retest(cli, fnum, numops);
if (n == numops || !analyze) return;
n++;
while (1) {
n1 = n;
close_files(cli, fnum);
reconnect(cli, fnum, share1, share2);
open_files(cli, fnum);
for (i=0;i<n-1;i++) {
int m;
recorded[i].needed = False;
close_files(cli, fnum);
open_files(cli, fnum);
m = retest(cli, fnum, n);
if (m == n) {
recorded[i].needed = True;
} else {
if (i < m) {
memmove(&recorded[i], &recorded[i+1],
(m-i)*sizeof(recorded[0]));
}
n = m;
i--;
}
}
if (n1 == n) break;
}
close_files(cli, fnum);
reconnect(cli, fnum, share1, share2);
open_files(cli, fnum);
showall = True;
n1 = retest(cli, fnum, n);
if (n1 != n-1) {
printf("ERROR - inconsistent result (%u %u)\n", n1, n);
}
close_files(cli, fnum);
for (i=0;i<n;i++) {
printf("{%u, %u, %u, %u, %u, %u, %u},\n",
recorded[i].r1,
recorded[i].r2,
recorded[i].conn,
recorded[i].f,
recorded[i].start,
recorded[i].len,
recorded[i].needed);
}
}
static void usage(void)
{
printf(
"Usage:\n\
locktest //server1/share1 //server2/share2 [options..]\n\
options:\n\
-U user%%pass\n\
-s seed\n\
-o numops\n\
-u hide unlock fails\n\
-a (show all ops)\n\
-O use oplocks\n\
");
}
/****************************************************************************
main program
****************************************************************************/
int main(int argc,char *argv[])
{
char *share1, *share2;
extern char *optarg;
extern int optind;
extern FILE *dbf;
int opt;
char *p;
int seed;
static pstring servicesf = CONFIGFILE;
setlinebuf(stdout);
dbf = stderr;
if (argv[1][0] == '-' || argc < 3) {
usage();
exit(1);
}
share1 = argv[1];
share2 = argv[2];
all_string_sub(share1,"/","\\",0);
all_string_sub(share2,"/","\\",0);
setup_logging(argv[0],True);
argc -= 2;
argv += 2;
TimeInit();
charset_initialise();
codepage_initialise(lp_client_code_page());
lp_load(servicesf,True,False,False);
load_interfaces();
if (getenv("USER")) {
pstrcpy(username,getenv("USER"));
}
seed = time(NULL);
while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
switch (opt) {
case 'U':
pstrcpy(username,optarg);
p = strchr(username,'%');
if (p) {
*p = 0;
pstrcpy(password, p+1);
got_pass = 1;
}
break;
case 's':
seed = atoi(optarg);
break;
case 'u':
hide_unlock_fails = True;
break;
case 'o':
numops = atoi(optarg);
break;
case 'O':
use_oplocks = True;
break;
case 'a':
showall = True;
break;
case 'A':
analyze = True;
break;
case 'h':
usage();
exit(1);
default:
printf("Unknown option %c (%d)\n", (char)opt, opt);
exit(1);
}
}
argc -= optind;
argv += optind;
DEBUG(0,("seed=%u\n", seed));
srandom(seed);
locking_init(1);
test_locks(share1, share2);
return(0);
}
|