summaryrefslogtreecommitdiffstats
path: root/ldap/c-sdk/libldap/pthreadtest.c
blob: fa913f691909345efc6cee33cad87707b13b4e10 (plain)
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
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
649
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
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
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 * 
 * The contents of this file are subject to the Mozilla Public License Version 
 * 1.1 (the "License"); you may not use this file except in compliance with 
 * the License. You may obtain a copy of the License at 
 * http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 * 
 * The Original Code is Mozilla Communicator client code, released
 * March 31, 1998.
 * 
 * The Initial Developer of the Original Code is
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 1998-1999
 * the Initial Developer. All Rights Reserved.
 * 
 * Contributor(s):
 * 
 * Alternatively, the contents of this file may be used under the terms of
 * either of the GNU General Public License Version 2 or later (the "GPL"),
 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 * 
 * ***** END LICENSE BLOCK ***** */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <values.h>
#include <errno.h>
#include <pthread.h>
#include <synch.h>

#include <ldap.h>

/* Authentication and search information. */
#define NAME		"cn=Directory Manager"
#define PASSWORD	"rtfm11111"
#define BASE		"dc=example,dc=com"
#define SCOPE		LDAP_SCOPE_SUBTREE

static void *modify_thread();
static void *add_thread();
static void *delete_thread();
static void *bind_thread();
static void *compare_thread();
static void *search_thread();
static void *my_mutex_alloc();
static void my_mutex_free();
void *my_sema_alloc( void );
void my_sema_free( void * );
int my_sema_wait( void * );
int my_sema_post( void * );
static void set_ld_error();
static int  get_ld_error();
static void set_errno();
static int  get_errno();
static void tsd_setup();
static void tsd_cleanup();
static int get_random_id( void );
static char *get_id_str( int id );

/* Linked list of LDAPMessage structs for search results. */
typedef struct ldapmsgwrapper {
    LDAPMessage			*lmw_messagep;
    struct ldapmsgwrapper	*lmw_next;
} ldapmsgwrapper;

LDAP		*ld;
pthread_key_t	key;
int		maxid = MAXINT;
int		maxops = 0;		/* zero means no limit */
int		range_filters = 0;	/* if non-zero use >= and >= filters */

main( int argc, char **argv )

{
	pthread_attr_t			attr;
	pthread_t			*threadids;
	void				*status;
	struct ldap_thread_fns		tfns;
	struct ldap_extra_thread_fns	extrafns;
	int		rc, c, errflg, i, inited_attr;
	int		doadd, dodelete, domodify, docompare, dosearch, dobind;
	int		option_extthreads, option_restart;
	int		each_thread_count, thread_count;
	extern int	optind;
	extern char	*optarg;

	doadd = dodelete = domodify = docompare = dobind = dosearch = 0;
	option_extthreads = option_restart = 0;
	inited_attr = 0;
	errflg = 0;
	each_thread_count = 1;	/* how many of each type of thread? */
	rc = LDAP_SUCCESS;	/* optimistic */

	while (( c = getopt( argc, argv, "abcdmrsERi:n:o:S:" )) != EOF ) {
		switch( c ) {
		case 'a':		/* perform add operations */
			++doadd;
			break;
		case 'b':		/* perform bind operations */
			++dobind;
			break;
		case 'c':		/* perform compare operations */
			++docompare;
			break;
		case 'd':		/* perform delete operations */
			++dodelete;
			break;
		case 'm':		/* perform modify operations */
			++domodify;
			break;
		case 'r':		/* use range filters in searches */
			++range_filters;
			break;
		case 's':		/* perform search operations */
			++dosearch;
			break;
		case 'E':		/* use extended thread functions */
			++option_extthreads;
			break;
		case 'R':		/* turn on LDAP_OPT_RESTART */
			++option_restart;
			break;
		case 'i':		/* highest # used for entry names */
			maxid = atoi( optarg );
			break;
		case 'n':		/* # threads for each operation */
			if (( each_thread_count = atoi( optarg )) < 1 ) {
				fprintf( stderr, "thread count must be > 0\n" );
				++errflg;
			}
			break;
		case 'o':		/* operations to perform per thread */
			if (( maxops = atoi( optarg )) < 0 ) {
				fprintf( stderr,
				    "operation limit must be >= 0\n" );
				++errflg;
			}
			break;
		case 'S':		/* random number seed */
			if ( *optarg == 'r' ) {
				int	seed = (int)time( (time_t *)0 );
				srandom( seed );
				printf( "Random seed: %d\n", seed );
			} else {
				srandom( atoi( optarg ));
			}
			break;
		default:
			++errflg;
			break;
		}
	}
	
	/* Check command-line syntax. */
	thread_count = each_thread_count * ( doadd + dodelete + domodify
	    + dobind + docompare + dosearch );
	if ( thread_count < 1 ) {
		fprintf( stderr,
		    "Specify at least one of -a, -b, -c, -d, -m, or -s\n" );
		++errflg;
	}

	if ( errflg || argc - optind != 2 ) {
		fprintf( stderr, "usage: %s [-abcdmrsER] [-i id] [-n thr]"
		    " [-o oplim] [-S sd] host port\n", argv[0] );
		fputs( "\nWhere:\n"
		    "\t\"id\" is the highest entry id (name) to use"
			" (default is MAXINT)\n"
		    "\t\"thr\" is the number of threads for each operation"
			" (default is 1)\n"
		    "\t\"oplim\" is the number of ops done by each thread"
			" (default is infinite)\n"
		    "\t\"sd\" is a random() number seed (default is 1).  Use"
			" the letter r for\n"
			"\t\tsd to seed random() using time of day.\n"
		    "\t\"host\" is the hostname of an LDAP directory server\n"
		    "\t\"port\" is the TCP port of the LDAP directory server\n"
		    , stderr );
		fputs( "\nAnd the single character options are:\n"
		    "\t-a\tstart thread(s) to perform add operations\n"
		    "\t-b\tstart thread(s) to perform bind operations\n"
		    "\t-c\tstart thread(s) to perform compare operations\n"
		    "\t-d\tstart thread(s) to perform delete operations\n"
		    "\t-m\tstart thread(s) to perform modify operations\n"
		    "\t-s\tstart thread(s) to perform search operations\n"
		    "\t-r\tuse range filters in searches\n"
		    "\t-E\tinstall LDAP_OPT_EXTRA_THREAD_FN_PTRS\n"
		    "\t-R\tturn on LDAP_OPT_RESTART\n", stderr );

		return( LDAP_PARAM_ERROR );
	}

	/* Create a key. */
	if ( pthread_key_create( &key, free ) != 0 ) {
		perror( "pthread_key_create" );
	}
	tsd_setup();

	/* Allocate space for thread ids */
	if (( threadids = (pthread_t *)calloc( thread_count,
	    sizeof( pthread_t ))) == NULL ) {
		rc = LDAP_LOCAL_ERROR;
		goto clean_up_and_return;
	}


	/* Initialize the LDAP session. */
	if (( ld = ldap_init( argv[optind], atoi( argv[optind+1] ))) == NULL ) {
		perror( "ldap_init" );
		rc = LDAP_LOCAL_ERROR;
		goto clean_up_and_return;
	}

	/* Set the function pointers for dealing with mutexes
	   and error information. */
	memset( &tfns, '\0', sizeof(struct ldap_thread_fns) );
	tfns.ltf_mutex_alloc = (void *(*)(void)) my_mutex_alloc;
	tfns.ltf_mutex_free = (void (*)(void *)) my_mutex_free;
	tfns.ltf_mutex_lock = (int (*)(void *)) pthread_mutex_lock;
	tfns.ltf_mutex_unlock = (int (*)(void *)) pthread_mutex_unlock;
	tfns.ltf_get_errno = get_errno;
	tfns.ltf_set_errno = set_errno;
	tfns.ltf_get_lderrno = get_ld_error;
	tfns.ltf_set_lderrno = set_ld_error;
	tfns.ltf_lderrno_arg = NULL;

	/* Set up this session to use those function pointers. */

	rc = ldap_set_option( ld, LDAP_OPT_THREAD_FN_PTRS, (void *) &tfns );
	if ( rc < 0 ) {
		rc = ldap_get_lderrno( ld, NULL, NULL );
		fprintf( stderr,
		    "ldap_set_option (LDAP_OPT_THREAD_FN_PTRS): %s\n",
		    ldap_err2string( rc ) );
		goto clean_up_and_return;
	}

	if ( option_extthreads ) {
		/* Set the function pointers for working with semaphores. */

		memset( &extrafns, '\0', sizeof(struct ldap_extra_thread_fns) );
		extrafns.ltf_mutex_trylock =
		    (int (*)(void *)) pthread_mutex_trylock;
		extrafns.ltf_sema_alloc = (void *(*)(void)) my_sema_alloc;
		extrafns.ltf_sema_free = (void (*)(void *)) my_sema_free;
		extrafns.ltf_sema_wait = (int (*)(void *)) my_sema_wait;
		extrafns.ltf_sema_post = (int (*)(void *)) my_sema_post;

		/* Set up this session to use those function pointers. */

		if ( ldap_set_option( ld, LDAP_OPT_EXTRA_THREAD_FN_PTRS,
		    (void *) &extrafns ) != 0 ) {
			rc = ldap_get_lderrno( ld, NULL, NULL );
			ldap_perror( ld, "ldap_set_option"
			    " (LDAP_OPT_EXTRA_THREAD_FN_PTRS)" );
			goto clean_up_and_return;
		}
	}


	if ( option_restart && ldap_set_option( ld, LDAP_OPT_RESTART,
	    LDAP_OPT_ON ) != 0 ) {
		rc = ldap_get_lderrno( ld, NULL, NULL );
		ldap_perror( ld, "ldap_set_option(LDAP_OPT_RESTART)" );
		goto clean_up_and_return;
	}

	/* Attempt to bind to the server. */
	rc = ldap_simple_bind_s( ld, NAME, PASSWORD );
	if ( rc != LDAP_SUCCESS ) {
		fprintf( stderr, "ldap_simple_bind_s: %s\n",
		    ldap_err2string( rc ) );
		goto clean_up_and_return;
	}

	/* Initialize the attribute. */
	if ( pthread_attr_init( &attr ) != 0 ) {
		perror( "pthread_attr_init" );
		rc = LDAP_LOCAL_ERROR;
		goto clean_up_and_return;
	}
	++inited_attr;

	/* Specify that the threads are joinable. */
	pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_JOINABLE );

	/* Create all the requested threads */
	thread_count = 0;
	if ( domodify ) {
		for ( i = 0; i < each_thread_count; ++i ) {
			if ( pthread_create( &threadids[thread_count], &attr,
			    modify_thread, get_id_str(thread_count) ) != 0 ) {
				perror( "pthread_create modify_thread" );
			    rc = LDAP_LOCAL_ERROR;
			    goto clean_up_and_return;
			}
			++thread_count;
		}
	}

	if ( doadd ) {
		for ( i = 0; i < each_thread_count; ++i ) {
			if ( pthread_create( &threadids[thread_count], &attr,
			    add_thread, get_id_str(thread_count) ) != 0 ) {
				perror( "pthread_create add_thread" );
			    rc = LDAP_LOCAL_ERROR;
			    goto clean_up_and_return;
			}
			++thread_count;
		}
	}

	if ( dodelete ) {
		for ( i = 0; i < each_thread_count; ++i ) {
			if ( pthread_create( &threadids[thread_count], &attr,
			    delete_thread, get_id_str(thread_count) ) != 0 ) {
				perror( "pthread_create delete_thread" );
			    rc = LDAP_LOCAL_ERROR;
			    goto clean_up_and_return;
			}
			++thread_count;
		}
	}

	if ( dobind ) {
		for ( i = 0; i < each_thread_count; ++i ) {
			if ( pthread_create( &threadids[thread_count], &attr,
			    bind_thread, get_id_str(thread_count) ) != 0 ) {
				perror( "pthread_create bind_thread" );
			    rc = LDAP_LOCAL_ERROR;
			    goto clean_up_and_return;
			}
			++thread_count;
		}
	}

	if ( docompare ) {
		for ( i = 0; i < each_thread_count; ++i ) {
			if ( pthread_create( &threadids[thread_count], &attr,
			    compare_thread, get_id_str(thread_count) ) != 0 ) {
				perror( "pthread_create compare_thread" );
			    rc = LDAP_LOCAL_ERROR;
			    goto clean_up_and_return;
			}
			++thread_count;
		}
	}

	if ( dosearch ) {
		for ( i = 0; i < each_thread_count; ++i ) {
			if ( pthread_create( &threadids[thread_count], &attr,
			    search_thread, get_id_str(thread_count) ) != 0 ) {
				perror( "pthread_create search_thread" );
			    rc = LDAP_LOCAL_ERROR;
			    goto clean_up_and_return;
			}
			++thread_count;
		}
	}

	/* Wait until these threads exit. */
	for ( i = 0; i < thread_count; ++i ) {
		pthread_join( threadids[i], &status );
	}

clean_up_and_return:
	if ( ld != NULL ) {
		set_ld_error( 0, NULL, NULL, NULL );  /* disposes of memory */
		ldap_unbind( ld );
	}
	if ( threadids != NULL ) {
		free( threadids );
	}
	if ( inited_attr ) {
		pthread_attr_destroy( &attr );
	}
	tsd_cleanup();

	return( rc );
}


static void *
modify_thread( char *id )
{
	LDAPMessage	*res;
	LDAPMessage	*e;
	int		i, modentry, num_entries, msgid, parse_rc, finished;
	int		rc, opcount;
	LDAPMod		mod;
	LDAPMod		*mods[2];
	char		*vals[2];
	char		*dn;
	ldapmsgwrapper	*list, *lmwp, *lastlmwp;
	struct timeval	zerotime;
	void		*voidrc = (void *)0;

	zerotime.tv_sec = zerotime.tv_usec = 0L;

	printf( "Starting modify_thread %s.\n", id );
	opcount = 0;
	tsd_setup();

	rc = ldap_search_ext( ld, BASE, SCOPE, "(objectclass=*)",
		NULL, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &msgid );
	if ( rc != LDAP_SUCCESS ) {
		fprintf( stderr, "Thread %s error: Modify thread: "
		"ldap_search_ext: %s\n", id, ldap_err2string( rc ) );
		exit( 1 );
	}
	list = lastlmwp = NULL;
	finished = 0;
	num_entries = 0;
	while ( !finished ) {
		rc = ldap_result( ld, msgid, LDAP_MSG_ONE, &zerotime, &res );
		switch ( rc ) {
		case -1:
			rc = ldap_get_lderrno( ld, NULL, NULL );
			fprintf( stderr, "ldap_result: %s\n", ldap_err2string( rc ) );
			exit( 1 );
			break;
		case 0:
			break;
		/* Keep track of the number of entries found. */
		case LDAP_RES_SEARCH_ENTRY:
			num_entries++;
			if (( lmwp = (ldapmsgwrapper *)
				malloc( sizeof( ldapmsgwrapper ))) == NULL ) {
				fprintf( stderr, "Thread %s: Modify thread: Cannot malloc\n", id );
				exit( 1 );
			}
			lmwp->lmw_messagep = res;
			lmwp->lmw_next = NULL;
			if ( lastlmwp == NULL ) {
				list = lastlmwp = lmwp;
			} else {
				lastlmwp->lmw_next = lmwp;
			}
			lastlmwp = lmwp;
			break;
		case LDAP_RES_SEARCH_REFERENCE:
			break;
		case LDAP_RES_SEARCH_RESULT:
			finished = 1;
			parse_rc = ldap_parse_result( ld, res, &rc, NULL, NULL, NULL, NULL, 1 );
			if ( parse_rc != LDAP_SUCCESS ) {
				fprintf( stderr, "Thread %s error: can't parse result code.\n", id );
				exit( 1 );
			} else {
				if ( rc != LDAP_SUCCESS ) {
					fprintf( stderr, "Thread %s error: ldap_search: %s\n", id, ldap_err2string( rc ) );
				} else {
					printf( "Thread %s: Got %d results.\n", id, num_entries );
				}
			}
			break;
		default:
			break;
		}
	}

	mods[0] = &mod;
	mods[1] = NULL;
	vals[0] = "bar";
	vals[1] = NULL;

	for ( ;; ) {
		modentry = random() % num_entries;
		for ( i = 0, lmwp = list; lmwp != NULL && i < modentry;
		    i++, lmwp = lmwp->lmw_next ) {
			/* NULL */
		}

		if ( lmwp == NULL ) {
			fprintf( stderr,
			    "Thread %s: Modify thread could not find entry %d of %d\n",
			    id, modentry, num_entries );
			continue;
		}

		e = lmwp->lmw_messagep;
		printf( "Thread %s: Modify thread picked entry %d of %d\n", id, i, num_entries );
		dn = ldap_get_dn( ld, e );

		mod.mod_op = LDAP_MOD_REPLACE;
		mod.mod_type = "description";
		mod.mod_values = vals;
		printf( "Thread %s: Modifying (%s)\n", id, dn );

		rc = ldap_modify_ext_s( ld, dn, mods, NULL, NULL );
		if ( rc != LDAP_SUCCESS ) {
			fprintf( stderr, "ldap_modify_ext_s: %s\n",
			    ldap_err2string( rc ) );
			if ( rc == LDAP_SERVER_DOWN ) {
				perror( "ldap_modify_ext_s" );
				voidrc = (void *)1;
				goto modify_cleanup_and_return;
			}
		}
		free( dn );

		++opcount;
		if ( maxops != 0 && opcount >= maxops ) {
			break;
		}
	}

modify_cleanup_and_return:
	printf( "Thread %s: attempted %d modify operations\n", id, opcount );
	set_ld_error( 0, NULL, NULL, NULL );	/* disposes of memory */
	tsd_cleanup();
	free( id );
	return voidrc;
}


static void *
add_thread( char *id )
{
	LDAPMod	mod[4];
	LDAPMod	*mods[5];
	char	dn[BUFSIZ], name[40];
	char	*cnvals[2], *snvals[2], *pwdvals[2], *ocvals[3];
	int	i, rc, opcount;
	void	*voidrc = (void *)0;

	printf( "Starting add_thread %s.\n", id );
	opcount = 0;
	tsd_setup();

	for ( i = 0; i < 4; i++ ) {
		mods[i] = &mod[i];
	}

	mods[4] = NULL;

	mod[0].mod_op = 0;
	mod[0].mod_type = "cn";
	mod[0].mod_values = cnvals;
	cnvals[1] = NULL;
	mod[1].mod_op = 0;
	mod[1].mod_type = "sn";
	mod[1].mod_values = snvals;
	snvals[1] = NULL;
	mod[2].mod_op = 0;
	mod[2].mod_type = "objectclass";
	mod[2].mod_values = ocvals;
	ocvals[0] = "top";
	ocvals[1] = "person";
	ocvals[2] = NULL;
	mod[3].mod_op = 0;
	mod[3].mod_type = "userPassword";
	mod[3].mod_values = pwdvals;
	pwdvals[1] = NULL;
	mods[4] = NULL;

	for ( ;; ) {
		sprintf( name, "%d", get_random_id() );
		sprintf( dn, "cn=%s, " BASE, name );
		cnvals[0] = name;
		snvals[0] = name;
		pwdvals[0] = name;

		printf( "Thread %s: Adding entry (%s)\n", id, dn );
		rc = ldap_add_ext_s( ld, dn, mods, NULL, NULL ); 
		if ( rc != LDAP_SUCCESS ) {
			fprintf( stderr, "ldap_add_ext_s: %s\n",
			    ldap_err2string( rc ) );
			if ( rc == LDAP_SERVER_DOWN ) {
				perror( "ldap_add_ext_s" );
				voidrc = (void *)1;
				goto add_cleanup_and_return;
			}
		}

		++opcount;
		if ( maxops != 0 && opcount >= maxops ) {
			break;
		}
	}

add_cleanup_and_return:
	printf( "Thread %s: attempted %d add operations\n", id, opcount );
	set_ld_error( 0, NULL, NULL, NULL );	/* disposes of memory */
	tsd_cleanup();
	free( id );
	return voidrc;
}


static void *
delete_thread( char *id )
{
	LDAPMessage	*res;
	char		dn[BUFSIZ], name[40];
	int		num_entries, msgid, rc, parse_rc, finished, opcount;
	struct timeval	zerotime;
	void		*voidrc = (void *)0;

	zerotime.tv_sec = zerotime.tv_usec = 0L;

	printf( "Starting delete_thread %s.\n", id );
	opcount = 0;
	tsd_setup();

	rc = ldap_search_ext( ld, BASE, SCOPE, "(objectclass=*)",
		NULL, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &msgid );
	if ( rc != LDAP_SUCCESS ) {
		fprintf( stderr, "Thread %s error: Delete thread: "
		"ldap_search_ext: %s\n", id, ldap_err2string( rc ) );
		exit( 1 );
	}

	finished = 0;
	num_entries = 0;
	while ( !finished ) {
		rc = ldap_result( ld, msgid, LDAP_MSG_ONE, &zerotime, &res );
		switch ( rc ) {
		case -1:
			rc = ldap_get_lderrno( ld, NULL, NULL );
			fprintf( stderr, "ldap_result: %s\n", ldap_err2string( rc ) );
			exit( 1 );
			break;
		case 0:
			break;
		/* Keep track of the number of entries found. */
		case LDAP_RES_SEARCH_ENTRY:
			num_entries++;
			break;
		case LDAP_RES_SEARCH_REFERENCE:
			break;
		case LDAP_RES_SEARCH_RESULT:
			finished = 1;
			parse_rc = ldap_parse_result( ld, res, &rc, NULL, NULL, NULL, NULL, 1 );
			if ( parse_rc != LDAP_SUCCESS ) {
				fprintf( stderr, "Thread %s error: can't parse result code.\n", id );
				exit( 1 );
			} else {
				if ( rc != LDAP_SUCCESS ) {
					fprintf( stderr, "Thread %s error: ldap_search: %s\n", id, ldap_err2string( rc ) );
				} else {
					printf( "Thread %s: Got %d results.\n", id, num_entries );
				}
			}
			break;
		default:
			break;
		}
	}

	for ( ;; ) {
		sprintf( name, "%d", get_random_id() );
		sprintf( dn, "cn=%s, " BASE, name );
		printf( "Thread %s: Deleting entry (%s)\n", id, dn );

		if (( rc = ldap_delete_ext_s( ld, dn, NULL, NULL ))
		    != LDAP_SUCCESS ) {
			ldap_perror( ld, "ldap_delete_ext_s" );
			if ( rc == LDAP_SERVER_DOWN ) {
				perror( "ldap_delete_ext_s" );
				voidrc = (void *)1;
				goto delete_cleanup_and_return;
			}
		}

		++opcount;
		if ( maxops != 0 && opcount >= maxops ) {
			break;
		}
	}

delete_cleanup_and_return:
	printf( "Thread %s: attempted %d delete operations\n", id, opcount );
	set_ld_error( 0, NULL, NULL, NULL );	/* disposes of memory */
	tsd_cleanup();
	free( id );
	return voidrc;
}


static void *
bind_thread( char *id )
{
	char		dn[BUFSIZ], name[40];
	int		rc, opcount;
	void		*voidrc = (void *)0;

	printf( "Starting bind_thread %s.\n", id );
	opcount = 0;
	tsd_setup();

	for ( ;; ) {
		sprintf( name, "%d", get_random_id() );
		sprintf( dn, "cn=%s, " BASE, name );
		printf( "Thread %s: Binding as entry (%s)\n", id, dn );

		if (( rc = ldap_simple_bind_s( ld, dn, name ))
		    != LDAP_SUCCESS ) {
			ldap_perror( ld, "ldap_simple_bind_s" );
			if ( rc == LDAP_SERVER_DOWN ) {
				perror( "ldap_simple_bind_s" );
				voidrc = (void *)1;
				goto bind_cleanup_and_return;
			}
		} else {
			printf( "Thread %s: bound as entry (%s)\n", id, dn );
		}

		++opcount;
		if ( maxops != 0 && opcount >= maxops ) {
			break;
		}
	}

bind_cleanup_and_return:
	printf( "Thread %s: attempted %d bind operations\n", id, opcount );
	set_ld_error( 0, NULL, NULL, NULL );	/* disposes of memory */
	tsd_cleanup();
	free( id );
	return voidrc;
}


static void *
compare_thread( char *id )
{
	char		dn[BUFSIZ], name[40], cmpval[40];
	int		rc, randval, opcount;
	struct berval	bv;
	void		*voidrc = (void *)0;

	printf( "Starting compare_thread %s.\n", id );
	opcount = 0;
	tsd_setup();

	for ( ;; ) {
		randval = get_random_id();
		sprintf( name, "%d", randval );
		sprintf( dn, "cn=%s, " BASE, name );
		sprintf( cmpval, "%d", randval + random() % 3 );
		bv.bv_val = cmpval;
		bv.bv_len = strlen( cmpval );

		printf( "Thread %s: Comparing cn in entry (%s) with %s\n",
		    id, dn, cmpval );

		rc = ldap_compare_ext_s( ld, dn, "cn", &bv, NULL, NULL );
		switch ( rc ) {
		case LDAP_COMPARE_TRUE:
			printf( "Thread %s: entry (%s) contains cn %s\n",
			    id, dn, cmpval );
			break;
		case LDAP_COMPARE_FALSE:
			printf( "Thread %s: entry (%s) doesn't contain cn %s\n",
			    id, dn, cmpval );
			break;
		default:
			ldap_perror( ld, "ldap_compare_ext_s" );
			if ( rc == LDAP_SERVER_DOWN ) {
				perror( "ldap_compare_ext_s" );
				voidrc = (void *)1;
				goto compare_cleanup_and_return;
			}
		}

		++opcount;
		if ( maxops != 0 && opcount >= maxops ) {
			break;
		}
	}

compare_cleanup_and_return:
	printf( "Thread %s: attempted %d compare operations\n", id, opcount );
	set_ld_error( 0, NULL, NULL, NULL );	/* disposes of memory */
	tsd_cleanup();
	free( id );
	return voidrc;
}


static void *
search_thread( char *id )
{
	LDAPMessage	*res, *entry;
	char		*dn, filter[40];
	int		rc, opcount;
	void		*voidrc = (void *)0;

	printf( "Starting search_thread %s.\n", id );
	opcount = 0;
	tsd_setup();

	for ( ;; ) {
		if ( range_filters ) {
			switch( get_random_id() % 3 ) {
			case 0:
				sprintf( filter, "(cn>=%d)", get_random_id());
				break;
			case 1:
				sprintf( filter, "(cn<=%d)", get_random_id());
				break;
			case 2:
				sprintf( filter, "(&(cn>=%d)(cn<=%d))",
				    get_random_id(), get_random_id() );
				break;
			}
		} else {
			sprintf( filter, "cn=%d", get_random_id() );
		}

		printf( "Thread %s: Searching for entry (%s)\n", id, filter );

		res = NULL;
		if (( rc = ldap_search_ext_s( ld, BASE, SCOPE, filter, NULL, 0,
		    NULL, NULL, NULL, 0, &res )) != LDAP_SUCCESS ) {
			ldap_perror( ld, "ldap_search_ext_s" );
			if ( rc == LDAP_SERVER_DOWN ) {
				perror( "ldap_search_ext_s" );
				voidrc = (void *)1;
				goto search_cleanup_and_return;
			}
		}
		if ( res != NULL ) {
			entry = ldap_first_entry( ld, res );
			if ( entry == NULL ) {
				printf( "Thread %s: found no entries\n", id );
			} else {
				dn = ldap_get_dn( ld, entry );
				printf(
				    "Thread %s: found entry (%s); %d total\n",
				    id, dn == NULL ? "(Null)" : dn,
				    ldap_count_entries( ld, res ));
				ldap_memfree( dn );
			}
			ldap_msgfree( res );
		}

		++opcount;
		if ( maxops != 0 && opcount >= maxops ) {
			break;
		}
	}

search_cleanup_and_return:
	printf( "Thread %s: attempted %d search operations\n", id, opcount );
	set_ld_error( 0, NULL, NULL, NULL );	/* disposes of memory */
	tsd_cleanup();
	free( id );
	return voidrc;
}


static void *
my_mutex_alloc( void )
{
	pthread_mutex_t	*mutexp;

	if ( (mutexp = malloc( sizeof(pthread_mutex_t) )) != NULL ) {
		pthread_mutex_init( mutexp, NULL );
	}
	return( mutexp );
}


void *
my_sema_alloc( void )
{
	sema_t *semptr;

	if( (semptr = malloc( sizeof(sema_t) ) ) != NULL ) {
		sema_init( semptr, 0, USYNC_THREAD, NULL );
	}
	return ( semptr );
}


static void
my_mutex_free( void *mutexp )
{
	pthread_mutex_destroy( (pthread_mutex_t *) mutexp );
	free( mutexp );
}


void
my_sema_free( void *semptr )
{
	sema_destroy( (sema_t *) semptr );
	free( semptr );
}


int
my_sema_wait( void *semptr )
{
	if( semptr != NULL )
		return( sema_wait( (sema_t *) semptr ) );
	else
		return( -1 );
}


int
my_sema_post( void *semptr )
{
	if( semptr != NULL )
		return( sema_post( (sema_t *) semptr ) );
	else
		return( -1 );
}


struct ldap_error {
	int	le_errno;
	char	*le_matched;
	char	*le_errmsg;
};


static void
tsd_setup()
{
	void	*tsd;
	tsd = pthread_getspecific( key );
	if ( tsd != NULL ) {
		fprintf( stderr, "tsd non-null!\n" );
		pthread_exit( NULL );
	}

	tsd = (void *) calloc( 1, sizeof(struct ldap_error) );
	pthread_setspecific( key, tsd );
}


static void
tsd_cleanup()
{
	void	*tsd;

	if (( tsd = pthread_getspecific( key )) != NULL ) {
		pthread_setspecific( key, NULL );
		free( tsd );
	}
}


static void
set_ld_error( int err, char *matched, char *errmsg, void *dummy )
{
	struct ldap_error *le;

	le = pthread_getspecific( key );

	le->le_errno = err;

	if ( le->le_matched != NULL ) {
		ldap_memfree( le->le_matched );
	}
	le->le_matched = matched;

	if ( le->le_errmsg != NULL ) {
		ldap_memfree( le->le_errmsg );
	}
	le->le_errmsg = errmsg;
}


static int
get_ld_error( char **matchedp, char **errmsgp, void *dummy )
{
	struct ldap_error *le;

	le = pthread_getspecific( key );
	if ( matchedp != NULL ) {
		*matchedp = le->le_matched;
	}
	if ( errmsgp != NULL ) {
		*errmsgp = le->le_errmsg;
	}
	return( le->le_errno );
}


static void
set_errno( int err )
{
	errno = err;
}


static int
get_errno( void )
{
	return( errno );
}


static int
get_random_id()
{
	return( random() % maxid );
}


static char *
get_id_str( int id )
{
	char	idstr[ 10 ];

	sprintf( idstr, "%d", id );
	return( strdup( idstr ));
}