slapd-mtread.c 19.2 KB
Newer Older
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
1
2
3
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
4
 * Copyright 1999-2020 The OpenLDAP Foundation.
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
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
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* ACKNOWLEDGEMENTS:
 * This work was initially developed by Kurt Spanier for inclusion
 * in OpenLDAP Software.
 */

/*
 * This tool is a MT reader.  It behaves like slapd-read however
 * with one or more threads simultaneously using the same connection.
 * If -M is enabled, then M threads will also perform write operations.
 */

#include "portable.h"

#include <stdio.h>
#include "ldap_pvt_thread.h"

#include "ac/stdlib.h"

#include "ac/ctype.h"
#include "ac/param.h"
#include "ac/socket.h"
#include "ac/string.h"
#include "ac/unistd.h"
#include "ac/wait.h"

#include "ldap.h"
#include "lutil.h"

#include "ldap_pvt.h"

#include "slapd-common.h"

#define MAXCONN	512
#define LOOPS	100
#define RETRIES	0
#define DEFAULT_BASE	"ou=people,dc=example,dc=com"

static void
do_conn( char *uri, char *manager, struct berval *passwd,
	LDAP **ld, int nobind, int maxretries, int conn_num );

static void
do_read( LDAP *ld, char *entry,
	char **attrs, int noattrs, int nobind, int maxloop,
	int maxretries, int delay, int force, int chaserefs, int idx );

static void
do_random( LDAP *ld,
	char *sbase, char *filter, char **attrs, int noattrs, int nobind,
	int innerloop, int maxretries, int delay, int force, int chaserefs,
	int idx );

67
68
69
70
71
72
static void
do_random2( LDAP *ld,
	char *sbase, char *filter, char **attrs, int noattrs, int nobind,
	int innerloop, int maxretries, int delay, int force, int chaserefs,
	int idx );

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
73
74
75
76
77
78
79
static void *
do_onethread( void *arg );

static void *
do_onerwthread( void *arg );

#define MAX_THREAD	1024
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
80
81
82
83
84
85
/* Use same array for readers and writers, offset writers by MAX_THREAD */
int	rt_pass[MAX_THREAD*2];
int	rt_fail[MAX_THREAD*2];
int	*rwt_pass = rt_pass + MAX_THREAD;
int	*rwt_fail = rt_fail + MAX_THREAD;
ldap_pvt_thread_t	rtid[MAX_THREAD*2], *rwtid = rtid + MAX_THREAD;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
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

/*
 * Shared globals (command line args)
 */
LDAP		*ld = NULL;
char		*entry = NULL;
char		*filter  = NULL;
int		loops = LOOPS;
int		outerloops = 1;
int		retries = RETRIES;
int		delay = 0;
int		force = 0;
int		chaserefs = 0;
char		*srchattrs[] = { "1.1", NULL };
char		**attrs = srchattrs;
int		noattrs = 0;
int		nobind = 0;
int		threads = 1;
int		rwthreads = 0;
int		verbose = 0;

int		noconns = 1;
LDAP		**lds = NULL;

static void
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
111
thread_error(int idx, char *string)
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
112
113
114
{
	char		thrstr[BUFSIZ];

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
115
	snprintf(thrstr, BUFSIZ, "error on tidx: %d: %s", idx, string);
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
116
117
118
119
	tester_error( thrstr );
}

static void
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
120
thread_output(int idx, char *string)
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
121
122
123
{
	char		thrstr[BUFSIZ];

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
124
	snprintf(thrstr, BUFSIZ, "tidx: %d says: %s", idx, string);
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
125
126
127
128
	tester_error( thrstr );
}

static void
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
129
thread_verbose(int idx, char *string)
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
130
131
132
133
134
{
	char		thrstr[BUFSIZ];

	if (!verbose)
		return;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
135
	snprintf(thrstr, BUFSIZ, "tidx: %d says: %s", idx, string);
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
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
	tester_error( thrstr );
}

static void
usage( char *name )
{
        fprintf( stderr,
		"usage: %s "
		"-H <uri> | ([-h <host>] -p <port>) "
		"-D <manager> "
		"-w <passwd> "
		"-e <entry> "
		"[-A] "
		"[-C] "
		"[-F] "
		"[-N] "
		"[-v] "
		"[-c connections] "
		"[-f filter] "
		"[-i <ignore>] "
		"[-l <loops>] "
		"[-L <outerloops>] "
		"[-m threads] "
		"[-M threads] "
		"[-r <maxretries>] "
		"[-t <delay>] "
		"[-T <attrs>] "
		"[<attrs>] "
		"\n",
		name );
	exit( EXIT_FAILURE );
}

int
main( int argc, char **argv )
{
	int		i;
	char		*uri = NULL;
	char		*host = "localhost";
	int		port = -1;
	char		*manager = NULL;
	struct berval	passwd = { 0, NULL };
	char		outstr[BUFSIZ];
	int		ptpass;
	int		testfail = 0;


	tester_init( "slapd-mtread", TESTER_READ );

	/* by default, tolerate referrals and no such object */
	tester_ignore_str2errlist( "REFERRAL,NO_SUCH_OBJECT" );

188
	while ( (i = getopt( argc, argv, "ACc:D:e:Ff:H:h:i:L:l:M:m:Np:r:t:T:w:v" )) != EOF ) {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
		switch ( i ) {
		case 'A':
			noattrs++;
			break;

		case 'C':
			chaserefs++;
			break;

		case 'H':		/* the server uri */
			uri = strdup( optarg );
			break;

		case 'h':		/* the servers host */
			host = strdup( optarg );
Howard Chu's avatar
Howard Chu committed
204
			break;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
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

		case 'i':
			tester_ignore_str2errlist( optarg );
			break;

		case 'N':
			nobind++;
			break;

		case 'v':
			verbose++;
			break;

		case 'p':		/* the servers port */
			if ( lutil_atoi( &port, optarg ) != 0 ) {
				usage( argv[0] );
			}
			break;

		case 'D':		/* the servers manager */
			manager = strdup( optarg );
			break;

		case 'w':		/* the server managers password */
			passwd.bv_val = strdup( optarg );
			passwd.bv_len = strlen( optarg );
			memset( optarg, '*', passwd.bv_len );
			break;

		case 'c':		/* the number of connections */
			if ( lutil_atoi( &noconns, optarg ) != 0 ) {
				usage( argv[0] );
			}
			break;

		case 'e':		/* DN to search for */
			entry = strdup( optarg );
			break;

		case 'f':		/* the search request */
			filter = strdup( optarg );
			break;

		case 'F':
			force++;
			break;

		case 'l':		/* the number of loops */
			if ( lutil_atoi( &loops, optarg ) != 0 ) {
				usage( argv[0] );
			}
			break;

		case 'L':		/* the number of outerloops */
			if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
				usage( argv[0] );
			}
			break;

		case 'M':		/* the number of R/W threads */
			if ( lutil_atoi( &rwthreads, optarg ) != 0 ) {
				usage( argv[0] );
			}
			if (rwthreads > MAX_THREAD)
				rwthreads = MAX_THREAD;
			break;

		case 'm':		/* the number of threads */
			if ( lutil_atoi( &threads, optarg ) != 0 ) {
				usage( argv[0] );
			}
			if (threads > MAX_THREAD)
				threads = MAX_THREAD;
			break;

		case 'r':		/* the number of retries */
			if ( lutil_atoi( &retries, optarg ) != 0 ) {
				usage( argv[0] );
			}
			break;

		case 't':		/* delay in seconds */
			if ( lutil_atoi( &delay, optarg ) != 0 ) {
				usage( argv[0] );
			}
			break;

		case 'T':
			attrs = ldap_str2charray( optarg, "," );
			if ( attrs == NULL ) {
				usage( argv[0] );
			}
			break;

		default:
			usage( argv[0] );
			break;
		}
	}

	if (( entry == NULL ) || ( port == -1 && uri == NULL ))
		usage( argv[0] );

	if ( *entry == '\0' ) {
		fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
				argv[0] );
		exit( EXIT_FAILURE );
	}

	if ( argv[optind] != NULL ) {
		attrs = &argv[optind];
	}

	if (noconns < 1)
		noconns = 1;
	if (noconns > MAXCONN)
		noconns = MAXCONN;
	lds = (LDAP **) calloc( sizeof(LDAP *), noconns);
	if (lds == NULL) {
		fprintf( stderr, "%s: Memory error: calloc noconns.\n",
				argv[0] );
		exit( EXIT_FAILURE );
	}

	uri = tester_uri( uri, host, port );
	/* One connection and one connection only */
	do_conn( uri, manager, &passwd, &ld, nobind, retries, 0 );
	lds[0] = ld;
	for(i = 1; i < noconns; i++) {
		do_conn( uri, manager, &passwd, &lds[i], nobind, retries, i );
	}

	ldap_pvt_thread_initialize();

	snprintf(outstr, BUFSIZ, "MT Test Start: conns: %d (%s)", noconns, uri);
	tester_error(outstr);
	snprintf(outstr, BUFSIZ, "Threads: RO: %d RW: %d", threads, rwthreads);
	tester_error(outstr);

	/* Set up read only threads */
	for ( i = 0; i < threads; i++ ) {
		ldap_pvt_thread_create( &rtid[i], 0, do_onethread, &rtid[i]);
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
347
348
		snprintf(outstr, BUFSIZ, "Created RO thread %d", i);
		thread_verbose(-1, outstr);
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
349
350
351
352
	}
	/* Set up read/write threads */
	for ( i = 0; i < rwthreads; i++ ) {
		ldap_pvt_thread_create( &rwtid[i], 0, do_onerwthread, &rwtid[i]);
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
353
354
		snprintf(outstr, BUFSIZ, "Created RW thread %d", i + MAX_THREAD);
		thread_verbose(-1, outstr);
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
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
	}

	ptpass =  outerloops * loops;

	/* wait for read only threads to complete */
	for ( i = 0; i < threads; i++ )
		ldap_pvt_thread_join(rtid[i], NULL);
	/* wait for read/write threads to complete */
	for ( i = 0; i < rwthreads; i++ )
		ldap_pvt_thread_join(rwtid[i], NULL);

	for(i = 0; i < noconns; i++) {
		if ( lds[i] != NULL ) {
			ldap_unbind_ext( lds[i], NULL, NULL );
		}
	}
	free( lds );

	for ( i = 0; i < threads; i++ ) {
		snprintf(outstr, BUFSIZ, "RO thread %d pass=%d fail=%d", i,
			rt_pass[i], rt_fail[i]);
		tester_error(outstr);
		if (rt_fail[i] != 0 || rt_pass[i] != ptpass) {
			snprintf(outstr, BUFSIZ, "FAIL RO thread %d", i);
			tester_error(outstr);
			testfail++;
		}
	}
	for ( i = 0; i < rwthreads; i++ ) {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
384
		snprintf(outstr, BUFSIZ, "RW thread %d pass=%d fail=%d", i + MAX_THREAD,
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
			rwt_pass[i], rwt_fail[i]);
		tester_error(outstr);
		if (rwt_fail[i] != 0 || rwt_pass[i] != ptpass) {
			snprintf(outstr, BUFSIZ, "FAIL RW thread %d", i);
			tester_error(outstr);
			testfail++;
		}
	}
	snprintf(outstr, BUFSIZ, "MT Test complete" );
	tester_error(outstr);

	if (testfail)
		exit( EXIT_FAILURE );
	exit( EXIT_SUCCESS );
}

static void *
do_onethread( void *arg )
{
	int		i, j, thisconn;
	LDAP		**mlds;
	char		thrstr[BUFSIZ];
	int		rc, refcnt = 0;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
408
	int		idx = (ldap_pvt_thread_t *)arg - rtid;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
409
410
411

	mlds = (LDAP **) calloc( sizeof(LDAP *), noconns);
	if (mlds == NULL) {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
412
		thread_error( idx, "Memory error: thread calloc for noconns" );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
413
414
415
416
417
418
419
		exit( EXIT_FAILURE );
	}

	for ( j = 0; j < outerloops; j++ ) {
		for(i = 0; i < noconns; i++) {
			mlds[i] = ldap_dup(lds[i]);
			if (mlds[i] == NULL) {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
420
				thread_error( idx, "ldap_dup error" );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
421
422
423
424
			}
		}
		rc = ldap_get_option(mlds[0], LDAP_OPT_SESSION_REFCNT, &refcnt);
		snprintf(thrstr, BUFSIZ,
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
425
426
427
			"RO Thread conns: %d refcnt: %d (rc = %d)",
			noconns, refcnt, rc);
		thread_verbose(idx, thrstr);
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
428

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
429
		thisconn = (idx + j) % noconns;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
430
431
432
		if (thisconn < 0 || thisconn >= noconns)
			thisconn = 0;
		if (mlds[thisconn] == NULL) {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
433
			thread_error( idx, "(failed to dup)");
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
434
435
436
			tester_perror( "ldap_dup", "(failed to dup)" );
			exit( EXIT_FAILURE );
		}
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
437
438
		snprintf(thrstr, BUFSIZ, "Using conn %d", thisconn);
		thread_verbose(idx, thrstr);
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
439
		if ( filter != NULL ) {
440
441
442
443
444
445
446
447
			if (strchr(filter, '['))
				do_random2( mlds[thisconn], entry, filter, attrs,
					noattrs, nobind, loops, retries, delay, force,
					chaserefs, idx );
			else
				do_random( mlds[thisconn], entry, filter, attrs,
					noattrs, nobind, loops, retries, delay, force,
					chaserefs, idx );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
448
449
450
451

		} else {
			do_read( mlds[thisconn], entry, attrs,
				noattrs, nobind, loops, retries, delay, force,
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
452
				chaserefs, idx );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
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
		}
		for(i = 0; i < noconns; i++) {
			(void) ldap_destroy(mlds[i]);
			mlds[i] = NULL;
		}
	}
	free( mlds );
	return( NULL );
}

static void *
do_onerwthread( void *arg )
{
	int		i, j, thisconn;
	LDAP		**mlds, *ld;
	char		thrstr[BUFSIZ];
	char		dn[256], uids[32], cns[32], *base;
	LDAPMod		*attrp[5], attrs[4];
	char		*oc_vals[] = { "top", "OpenLDAPperson", NULL };
	char		*cn_vals[] = { NULL, NULL };
	char		*sn_vals[] = { NULL, NULL };
	char		*uid_vals[] = { NULL, NULL };
	int		ret;
	int		adds = 0;
	int		dels = 0;
	int		rc, refcnt = 0;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
479
	int		idx = (ldap_pvt_thread_t *)arg - rtid;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
480
481
482

	mlds = (LDAP **) calloc( sizeof(LDAP *), noconns);
	if (mlds == NULL) {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
483
		thread_error( idx, "Memory error: thread calloc for noconns" );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
484
485
486
		exit( EXIT_FAILURE );
	}

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
487
488
	snprintf(uids, sizeof(uids), "rwtest%04d", idx);
	snprintf(cns, sizeof(cns), "rwtest%04d", idx);
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
	/* add setup */
	for (i = 0; i < 4; i++) {
		attrp[i] = &attrs[i];
		attrs[i].mod_op = 0;
	}
	attrp[4] = NULL;
	attrs[0].mod_type = "objectClass";
	attrs[0].mod_values = oc_vals;
	attrs[1].mod_type = "cn";
	attrs[1].mod_values = cn_vals;
	cn_vals[0] = &cns[0];
	attrs[2].mod_type = "sn";
	attrs[2].mod_values = sn_vals;
	sn_vals[0] = &cns[0];
	attrs[3].mod_type = "uid";
	attrs[3].mod_values = uid_vals;
	uid_vals[0] = &uids[0];

	for ( j = 0; j < outerloops; j++ ) {
		for(i = 0; i < noconns; i++) {
			mlds[i] = ldap_dup(lds[i]);
			if (mlds[i] == NULL) {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
511
				thread_error( idx, "ldap_dup error" );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
512
513
514
515
			}
		}
		rc = ldap_get_option(mlds[0], LDAP_OPT_SESSION_REFCNT, &refcnt);
		snprintf(thrstr, BUFSIZ,
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
516
517
518
			"RW Thread conns: %d refcnt: %d (rc = %d)",
			noconns, refcnt, rc);
		thread_verbose(idx, thrstr);
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
519

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
520
		thisconn = (idx + j) % noconns;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
521
522
523
		if (thisconn < 0 || thisconn >= noconns)
			thisconn = 0;
		if (mlds[thisconn] == NULL) {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
524
			thread_error( idx, "(failed to dup)");
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
525
526
527
			tester_perror( "ldap_dup", "(failed to dup)" );
			exit( EXIT_FAILURE );
		}
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
528
529
		snprintf(thrstr, BUFSIZ, "START RW Thread using conn %d", thisconn);
		thread_verbose(idx, thrstr);
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546

		ld = mlds[thisconn];
		if (entry != NULL)
			base = entry;
		else
			base = DEFAULT_BASE;
		snprintf(dn, 256, "cn=%s,%s", cns, base);

		adds = 0;
		dels = 0;
		for (i = 0; i < loops; i++) {
			ret = ldap_add_ext_s(ld, dn, &attrp[0], NULL, NULL);
			if (ret == LDAP_SUCCESS) {
				adds++;
				ret = ldap_delete_ext_s(ld, dn, NULL, NULL);
				if (ret == LDAP_SUCCESS) {
					dels++;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
547
					rt_pass[idx]++;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
548
				} else {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
549
550
					thread_output(idx, ldap_err2string(ret));
					rt_fail[idx]++;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
551
552
				}
			} else {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
553
554
				thread_output(idx, ldap_err2string(ret));
				rt_fail[idx]++;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
555
556
557
558
			}
		}

		snprintf(thrstr, BUFSIZ,
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
559
560
561
			"INNER STOP RW Thread using conn %d (%d/%d)",
			thisconn, adds, dels);
		thread_verbose(idx, thrstr);
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578

		for(i = 0; i < noconns; i++) {
			(void) ldap_destroy(mlds[i]);
			mlds[i] = NULL;
		}
	}

	free( mlds );
	return( NULL );
}

static void
do_conn( char *uri, char *manager, struct berval *passwd,
	LDAP **ldp, int nobind, int maxretries, int conn_num )
{
	LDAP	*ld = NULL;
	int	version = LDAP_VERSION3;
579
	int  	do_retry = maxretries;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
	int     rc = LDAP_SUCCESS;
	char	thrstr[BUFSIZ];

retry:;
	ldap_initialize( &ld, uri );
	if ( ld == NULL ) {
		snprintf( thrstr, BUFSIZ, "connection: %d", conn_num );
		tester_error( thrstr );
		tester_perror( "ldap_initialize", NULL );
		exit( EXIT_FAILURE );
	}

	(void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ); 
	(void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
		chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );

	if ( do_retry == maxretries ) {
		snprintf( thrstr, BUFSIZ, "do_conn #%d\n", conn_num );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
598
		thread_verbose( -1, thrstr );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
	}

	if ( nobind == 0 ) {
		rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
		if ( rc != LDAP_SUCCESS ) {
			snprintf( thrstr, BUFSIZ, "connection: %d", conn_num );
			tester_error( thrstr );
			tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
			switch ( rc ) {
			case LDAP_BUSY:
			case LDAP_UNAVAILABLE:
				if ( do_retry > 0 ) {
					ldap_unbind_ext( ld, NULL, NULL );
					ld = NULL;
					do_retry--;
					if ( delay != 0 ) {
					    sleep( delay );
					}
					goto retry;
				}
			/* fallthru */
			default:
				break;
			}
			exit( EXIT_FAILURE );
		}
	}
	*ldp = ld;
}

static void
do_random( LDAP *ld,
	char *sbase, char *filter, char **srchattrs, int noattrs, int nobind,
	int innerloop, int maxretries, int delay, int force, int chaserefs,
	int idx )
{
	int  	i = 0, do_retry = maxretries;
	char	*attrs[ 2 ];
	int     rc = LDAP_SUCCESS;
	int	nvalues = 0;
	char	**values = NULL;
	LDAPMessage *res = NULL, *e = NULL;
	char	thrstr[BUFSIZ];

	attrs[ 0 ] = LDAP_NO_ATTRS;
	attrs[ 1 ] = NULL;

	snprintf( thrstr, BUFSIZ,
			"Read(%d): base=\"%s\", filter=\"%s\".\n",
			innerloop, sbase, filter );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
649
	thread_verbose( idx, thrstr );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677

	rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
		filter, attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
	switch ( rc ) {
	case LDAP_SIZELIMIT_EXCEEDED:
	case LDAP_TIMELIMIT_EXCEEDED:
	case LDAP_SUCCESS:
		nvalues = ldap_count_entries( ld, res );
		if ( nvalues == 0 ) {
			if ( rc ) {
				tester_ldap_error( ld, "ldap_search_ext_s", NULL );
			}
			break;
		}

		values = malloc( ( nvalues + 1 ) * sizeof( char * ) );
		for ( i = 0, e = ldap_first_entry( ld, res ); e != NULL; i++, e = ldap_next_entry( ld, e ) )
		{
			values[ i ] = ldap_get_dn( ld, e );
		}
		values[ i ] = NULL;

		ldap_msgfree( res );

		if ( do_retry == maxretries ) {
			snprintf( thrstr, BUFSIZ,
				"Read base=\"%s\" filter=\"%s\" got %d values.\n",
				sbase, filter, nvalues );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
678
			thread_verbose( idx, thrstr );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
		}

		for ( i = 0; i < innerloop; i++ ) {
			int	r = ((double)nvalues)*rand()/(RAND_MAX + 1.0);

			do_read( ld, values[ r ],
				srchattrs, noattrs, nobind, 1, maxretries,
				delay, force, chaserefs, idx );
		}
		for( i = 0; i < nvalues; i++) {
			if (values[i] != NULL)
				ldap_memfree( values[i] );
		}
		free( values );
		break;

	default:
		tester_ldap_error( ld, "ldap_search_ext_s", NULL );
		break;
	}

	snprintf( thrstr, BUFSIZ, "Search done (%d).\n", rc );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
701
	thread_verbose( idx, thrstr );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
702
703
}

704
705
706
707
708
709
710
711
712
713
714
/* substitute a generated int into the filter */
static void
do_random2( LDAP *ld,
	char *sbase, char *filter, char **srchattrs, int noattrs, int nobind,
	int innerloop, int maxretries, int delay, int force, int chaserefs,
	int idx )
{
	int  	i = 0, do_retry = maxretries;
	int     rc = LDAP_SUCCESS;
	int		lo, hi, range;
	int	flen;
715
	LDAPMessage *res = NULL;
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
	char	*ptr, *ftail;
	char	thrstr[BUFSIZ];
	char	fbuf[BUFSIZ];

	snprintf( thrstr, BUFSIZ,
			"Read(%d): base=\"%s\", filter=\"%s\".\n",
			innerloop, sbase, filter );
	thread_verbose( idx, thrstr );

	ptr = strchr(filter, '[');
	if (!ptr)
		return;
	ftail = strchr(filter, ']');
	if (!ftail || ftail < ptr)
		return;

	sscanf(ptr, "[%d-%d]", &lo, &hi);
	range = hi - lo + 1;

	flen = ptr - filter;
	ftail++;

	for ( i = 0; i < innerloop; i++ ) {
		int	r = ((double)range)*rand()/(RAND_MAX + 1.0);
		sprintf(fbuf, "%.*s%d%s", flen, filter, r, ftail);

		rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
				fbuf, srchattrs, noattrs, NULL, NULL, NULL,
				LDAP_NO_LIMIT, &res );
		if ( res != NULL ) {
			ldap_msgfree( res );
		}
		if ( rc == 0 ) {
			rt_pass[idx]++;
		} else {
			int		first = tester_ignore_err( rc );
			char		buf[ BUFSIZ ];

			rt_fail[idx]++;
			snprintf( buf, sizeof( buf ), "ldap_search_ext_s(%s)", entry );

			/* if ignore.. */
			if ( first ) {
				/* only log if first occurrence */
				if ( ( force < 2 && first > 0 ) || abs(first) == 1 ) {
					tester_ldap_error( ld, buf, NULL );
				}
				continue;
			}

			/* busy needs special handling */
			tester_ldap_error( ld, buf, NULL );
			if ( rc == LDAP_BUSY && do_retry > 0 ) {
				do_retry--;
				continue;
			}
			break;
		}
	}

	snprintf( thrstr, BUFSIZ, "Search done (%d).\n", rc );
	thread_verbose( idx, thrstr );
}

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
780
781
782
783
784
785
786
787
788
789
790
791
792
static void
do_read( LDAP *ld, char *entry,
	char **attrs, int noattrs, int nobind, int maxloop,
	int maxretries, int delay, int force, int chaserefs, int idx )
{
	int  	i = 0, do_retry = maxretries;
	int     rc = LDAP_SUCCESS;
	char	thrstr[BUFSIZ];

retry:;
	if ( do_retry == maxretries ) {
		snprintf( thrstr, BUFSIZ, "Read(%d): entry=\"%s\".\n",
			maxloop, entry );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
793
		thread_verbose( idx, thrstr );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
794
795
	}

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
796
797
798
	snprintf(thrstr, BUFSIZ, "LD %p cnt: %d (retried %d) (%s)", \
		 (void *) ld, maxloop, (do_retry - maxretries), entry);
	thread_verbose( idx, thrstr );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837

	for ( ; i < maxloop; i++ ) {
		LDAPMessage *res = NULL;

		rc = ldap_search_ext_s( ld, entry, LDAP_SCOPE_BASE,
				NULL, attrs, noattrs, NULL, NULL, NULL,
				LDAP_NO_LIMIT, &res );
		if ( res != NULL ) {
			ldap_msgfree( res );
		}

		if ( rc == 0 ) {
			rt_pass[idx]++;
		} else {
			int		first = tester_ignore_err( rc );
			char		buf[ BUFSIZ ];

			rt_fail[idx]++;
			snprintf( buf, sizeof( buf ), "ldap_search_ext_s(%s)", entry );

			/* if ignore.. */
			if ( first ) {
				/* only log if first occurrence */
				if ( ( force < 2 && first > 0 ) || abs(first) == 1 ) {
					tester_ldap_error( ld, buf, NULL );
				}
				continue;
			}

			/* busy needs special handling */
			tester_ldap_error( ld, buf, NULL );
			if ( rc == LDAP_BUSY && do_retry > 0 ) {
				do_retry--;
				goto retry;
			}
			break;
		}
	}
}