source: trunk/server/source3/torture/locktest2.c

Last change on this file was 745, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.0

File size: 13.6 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 byte range lock tester - with local filesystem support
4 Copyright (C) Andrew Tridgell 1999
5
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.
10
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.
15
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/>.
18*/
19
20#include "includes.h"
21#include "libsmb/libsmb.h"
22#include "system/filesys.h"
23#include "locking/proto.h"
24
25static fstring password;
26static fstring username;
27static int got_pass;
28static int numops = 1000;
29static bool showall;
30static bool analyze;
31static bool hide_unlock_fails;
32static bool use_oplocks;
33
34extern char *optarg;
35extern int optind;
36
37#define FILENAME "\\locktest.dat"
38#define LOCKRANGE 100
39#define LOCKBASE 0
40
41/*
42#define LOCKBASE (0x40000000 - 50)
43*/
44
45#define READ_PCT 50
46#define LOCK_PCT 25
47#define UNLOCK_PCT 65
48#define RANGE_MULTIPLE 1
49
50#define NSERVERS 2
51#define NCONNECTIONS 2
52#define NUMFSTYPES 2
53#define NFILES 2
54#define LOCK_TIMEOUT 0
55
56#define FSTYPE_SMB 0
57#define FSTYPE_NFS 1
58
59struct record {
60 char r1, r2;
61 char conn, f, fstype;
62 unsigned start, len;
63 char needed;
64};
65
66static struct record *recorded;
67
68static int try_open(struct cli_state *c, char *nfs, int fstype, const char *fname, int flags)
69{
70 char *path;
71
72 switch (fstype) {
73 case FSTYPE_SMB:
74 {
75 uint16_t fd;
76 if (!NT_STATUS_IS_OK(cli_open(c, fname, flags, DENY_NONE, &fd))) {
77 return -1;
78 }
79 return fd;
80 }
81
82 case FSTYPE_NFS:
83 if (asprintf(&path, "%s%s", nfs, fname) > 0) {
84 int ret;
85 string_replace(path,'\\', '/');
86 ret = open(path, flags, 0666);
87 SAFE_FREE(path);
88 return ret;
89 }
90 break;
91 }
92
93 return -1;
94}
95
96static bool try_close(struct cli_state *c, int fstype, int fd)
97{
98 switch (fstype) {
99 case FSTYPE_SMB:
100 return NT_STATUS_IS_OK(cli_close(c, fd));
101
102 case FSTYPE_NFS:
103 return close(fd) == 0;
104 }
105
106 return False;
107}
108
109static bool try_lock(struct cli_state *c, int fstype,
110 int fd, unsigned start, unsigned len,
111 enum brl_type op)
112{
113 struct flock lock;
114
115 switch (fstype) {
116 case FSTYPE_SMB:
117 return cli_lock(c, fd, start, len, LOCK_TIMEOUT, op);
118
119 case FSTYPE_NFS:
120 lock.l_type = (op==READ_LOCK) ? F_RDLCK:F_WRLCK;
121 lock.l_whence = SEEK_SET;
122 lock.l_start = start;
123 lock.l_len = len;
124 lock.l_pid = getpid();
125 return fcntl(fd,F_SETLK,&lock) == 0;
126 }
127
128 return False;
129}
130
131static bool try_unlock(struct cli_state *c, int fstype,
132 int fd, unsigned start, unsigned len)
133{
134 struct flock lock;
135
136 switch (fstype) {
137 case FSTYPE_SMB:
138 return NT_STATUS_IS_OK(cli_unlock(c, fd, start, len));
139
140 case FSTYPE_NFS:
141 lock.l_type = F_UNLCK;
142 lock.l_whence = SEEK_SET;
143 lock.l_start = start;
144 lock.l_len = len;
145 lock.l_pid = getpid();
146 return fcntl(fd,F_SETLK,&lock) == 0;
147 }
148
149 return False;
150}
151
152static void print_brl(struct file_id id, struct server_id pid,
153 enum brl_type lock_type,
154 enum brl_flavour lock_flav,
155 br_off start, br_off size,
156 void *private_data)
157{
158 printf("%6d %s %s %.0f:%.0f(%.0f)\n",
159 (int)procid_to_pid(&pid), file_id_string_tos(&id),
160 lock_type==READ_LOCK?"R":"W",
161 (double)start, (double)start+size-1,(double)size);
162
163}
164
165/*****************************************************
166return a connection to a server
167*******************************************************/
168static struct cli_state *connect_one(char *share)
169{
170 struct cli_state *c;
171 char *server_n;
172 fstring server;
173 fstring myname;
174 static int count;
175 NTSTATUS nt_status;
176
177 fstrcpy(server,share+2);
178 share = strchr_m(server,'\\');
179 if (!share) return NULL;
180 *share = 0;
181 share++;
182
183 server_n = server;
184
185 if (!got_pass) {
186 char *pass = getpass("Password: ");
187 if (pass) {
188 fstrcpy(password, pass);
189 }
190 }
191
192 slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
193
194 nt_status = cli_full_connection(&c, myname, server_n, NULL, 0, share, "?????",
195 username, lp_workgroup(), password, 0,
196 Undefined);
197
198 if (!NT_STATUS_IS_OK(nt_status)) {
199 DEBUG(0, ("cli_full_connection failed with error %s\n", nt_errstr(nt_status)));
200 return NULL;
201 }
202
203 c->use_oplocks = use_oplocks;
204
205 return c;
206}
207
208
209static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS],
210 char *nfs[NSERVERS],
211 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
212 char *share1, char *share2)
213{
214 int server, conn, f, fstype;
215 char *share[2];
216 share[0] = share1;
217 share[1] = share2;
218
219 fstype = FSTYPE_SMB;
220
221 for (server=0;server<NSERVERS;server++)
222 for (conn=0;conn<NCONNECTIONS;conn++) {
223 if (cli[server][conn]) {
224 for (f=0;f<NFILES;f++) {
225 cli_close(cli[server][conn], fnum[server][fstype][conn][f]);
226 }
227 cli_ulogoff(cli[server][conn]);
228 cli_shutdown(cli[server][conn]);
229 }
230 cli[server][conn] = connect_one(share[server]);
231 if (!cli[server][conn]) {
232 DEBUG(0,("Failed to connect to %s\n", share[server]));
233 exit(1);
234 }
235 }
236}
237
238
239
240static bool test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
241 char *nfs[NSERVERS],
242 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
243 struct record *rec)
244{
245 unsigned conn = rec->conn;
246 unsigned f = rec->f;
247 unsigned fstype = rec->fstype;
248 unsigned start = rec->start;
249 unsigned len = rec->len;
250 unsigned r1 = rec->r1;
251 unsigned r2 = rec->r2;
252 enum brl_type op;
253 int server;
254 bool ret[NSERVERS];
255
256 if (r1 < READ_PCT) {
257 op = READ_LOCK;
258 } else {
259 op = WRITE_LOCK;
260 }
261
262 if (r2 < LOCK_PCT) {
263 /* set a lock */
264 for (server=0;server<NSERVERS;server++) {
265 ret[server] = try_lock(cli[server][conn], fstype,
266 fnum[server][fstype][conn][f],
267 start, len, op);
268 }
269 if (showall || ret[0] != ret[1]) {
270 printf("lock conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
271 conn, fstype, f,
272 start, start+len-1, len,
273 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
274 ret[0], ret[1]);
275 }
276 if (showall) brl_forall(print_brl, NULL);
277 if (ret[0] != ret[1]) return False;
278 } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
279 /* unset a lock */
280 for (server=0;server<NSERVERS;server++) {
281 ret[server] = try_unlock(cli[server][conn], fstype,
282 fnum[server][fstype][conn][f],
283 start, len);
284 }
285 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
286 printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u) -> %u:%u\n",
287 conn, fstype, f,
288 start, start+len-1, len,
289 ret[0], ret[1]);
290 }
291 if (showall) brl_forall(print_brl, NULL);
292 if (!hide_unlock_fails && ret[0] != ret[1]) return False;
293 } else {
294 /* reopen the file */
295 for (server=0;server<NSERVERS;server++) {
296 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
297 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
298 O_RDWR|O_CREAT);
299 if (fnum[server][fstype][conn][f] == -1) {
300 printf("failed to reopen on share1\n");
301 return False;
302 }
303 }
304 if (showall) {
305 printf("reopen conn=%u fstype=%u f=%u\n",
306 conn, fstype, f);
307 brl_forall(print_brl, NULL);
308 }
309 }
310 return True;
311}
312
313static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
314 char *nfs[NSERVERS],
315 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
316{
317 int server, conn, f, fstype;
318
319 for (server=0;server<NSERVERS;server++)
320 for (fstype=0;fstype<NUMFSTYPES;fstype++)
321 for (conn=0;conn<NCONNECTIONS;conn++)
322 for (f=0;f<NFILES;f++) {
323 if (fnum[server][fstype][conn][f] != -1) {
324 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
325 fnum[server][fstype][conn][f] = -1;
326 }
327 }
328 for (server=0;server<NSERVERS;server++) {
329 cli_unlink(cli[server][0], FILENAME, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
330 }
331}
332
333static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
334 char *nfs[NSERVERS],
335 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
336{
337 int server, fstype, conn, f;
338
339 for (server=0;server<NSERVERS;server++)
340 for (fstype=0;fstype<NUMFSTYPES;fstype++)
341 for (conn=0;conn<NCONNECTIONS;conn++)
342 for (f=0;f<NFILES;f++) {
343 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
344 O_RDWR|O_CREAT);
345 if (fnum[server][fstype][conn][f] == -1) {
346 fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
347 server, fstype, conn, f);
348 exit(1);
349 }
350 }
351}
352
353
354static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
355 char *nfs[NSERVERS],
356 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
357 int n)
358{
359 int i;
360 printf("testing %u ...\n", n);
361 for (i=0; i<n; i++) {
362 if (i && i % 100 == 0) {
363 printf("%u\n", i);
364 }
365
366 if (recorded[i].needed &&
367 !test_one(cli, nfs, fnum, &recorded[i])) return i;
368 }
369 return n;
370}
371
372
373/* each server has two connections open to it. Each connection has two file
374 descriptors open on the file - 8 file descriptors in total
375
376 we then do random locking ops in tamdem on the 4 fnums from each
377 server and ensure that the results match
378 */
379static void test_locks(char *share1, char *share2, char *nfspath1, char *nfspath2)
380{
381 struct cli_state *cli[NSERVERS][NCONNECTIONS];
382 char *nfs[NSERVERS];
383 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
384 int n, i, n1;
385
386 nfs[0] = nfspath1;
387 nfs[1] = nfspath2;
388
389 ZERO_STRUCT(fnum);
390 ZERO_STRUCT(cli);
391
392 recorded = SMB_MALLOC_ARRAY(struct record, numops);
393
394 for (n=0; n<numops; n++) {
395 recorded[n].conn = random() % NCONNECTIONS;
396 recorded[n].fstype = random() % NUMFSTYPES;
397 recorded[n].f = random() % NFILES;
398 recorded[n].start = LOCKBASE + ((unsigned)random() % (LOCKRANGE-1));
399 recorded[n].len = 1 +
400 random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
401 recorded[n].start *= RANGE_MULTIPLE;
402 recorded[n].len *= RANGE_MULTIPLE;
403 recorded[n].r1 = random() % 100;
404 recorded[n].r2 = random() % 100;
405 recorded[n].needed = True;
406 }
407
408 reconnect(cli, nfs, fnum, share1, share2);
409 open_files(cli, nfs, fnum);
410 n = retest(cli, nfs, fnum, numops);
411
412 if (n == numops || !analyze) return;
413 n++;
414
415 while (1) {
416 n1 = n;
417
418 close_files(cli, nfs, fnum);
419 reconnect(cli, nfs, fnum, share1, share2);
420 open_files(cli, nfs, fnum);
421
422 for (i=0;i<n-1;i++) {
423 int m;
424 recorded[i].needed = False;
425
426 close_files(cli, nfs, fnum);
427 open_files(cli, nfs, fnum);
428
429 m = retest(cli, nfs, fnum, n);
430 if (m == n) {
431 recorded[i].needed = True;
432 } else {
433 if (i < m) {
434 memmove(&recorded[i], &recorded[i+1],
435 (m-i)*sizeof(recorded[0]));
436 }
437 n = m;
438 i--;
439 }
440 }
441
442 if (n1 == n) break;
443 }
444
445 close_files(cli, nfs, fnum);
446 reconnect(cli, nfs, fnum, share1, share2);
447 open_files(cli, nfs, fnum);
448 showall = True;
449 n1 = retest(cli, nfs, fnum, n);
450 if (n1 != n-1) {
451 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
452 }
453 close_files(cli, nfs, fnum);
454
455 for (i=0;i<n;i++) {
456 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
457 recorded[i].r1,
458 recorded[i].r2,
459 recorded[i].conn,
460 recorded[i].fstype,
461 recorded[i].f,
462 recorded[i].start,
463 recorded[i].len,
464 recorded[i].needed);
465 }
466}
467
468
469
470static void usage(void)
471{
472 printf(
473"Usage:\n\
474 locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
475 options:\n\
476 -U user%%pass\n\
477 -s seed\n\
478 -o numops\n\
479 -u hide unlock fails\n\
480 -a (show all ops)\n\
481 -O use oplocks\n\
482");
483}
484
485/****************************************************************************
486 main program
487****************************************************************************/
488 int main(int argc,char *argv[])
489{
490 char *share1, *share2, *nfspath1, *nfspath2;
491 int opt;
492 char *p;
493 int seed;
494
495 setlinebuf(stdout);
496
497 if (argc < 5 || argv[1][0] == '-') {
498 usage();
499 exit(1);
500 }
501
502 setup_logging(argv[0], DEBUG_STDOUT);
503
504 share1 = argv[1];
505 share2 = argv[2];
506 nfspath1 = argv[3];
507 nfspath2 = argv[4];
508
509 all_string_sub(share1,"/","\\",0);
510 all_string_sub(share2,"/","\\",0);
511
512 argc -= 4;
513 argv += 4;
514
515 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
516 load_interfaces();
517
518 if (getenv("USER")) {
519 fstrcpy(username,getenv("USER"));
520 }
521
522 seed = time(NULL);
523
524 while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
525 switch (opt) {
526 case 'U':
527 fstrcpy(username,optarg);
528 p = strchr_m(username,'%');
529 if (p) {
530 *p = 0;
531 fstrcpy(password, p+1);
532 got_pass = 1;
533 }
534 break;
535 case 's':
536 seed = atoi(optarg);
537 break;
538 case 'u':
539 hide_unlock_fails = True;
540 break;
541 case 'o':
542 numops = atoi(optarg);
543 break;
544 case 'O':
545 use_oplocks = True;
546 break;
547 case 'a':
548 showall = True;
549 break;
550 case 'A':
551 analyze = True;
552 break;
553 case 'h':
554 usage();
555 exit(1);
556 default:
557 printf("Unknown option %c (%d)\n", (char)opt, opt);
558 exit(1);
559 }
560 }
561
562 argc -= optind;
563 argv += optind;
564
565 DEBUG(0,("seed=%u\n", seed));
566 srandom(seed);
567
568 locking_init_readonly();
569 test_locks(share1, share2, nfspath1, nfspath2);
570
571 return(0);
572}
Note: See TracBrowser for help on using the repository browser.