2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2006 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
16 * This work was initially developed by Kurt Spanier for inclusion
17 * in OpenLDAP Software.
24 #include <ac/stdlib.h>
27 #include <ac/dirent.h>
29 #include <ac/socket.h>
30 #include <ac/string.h>
31 #include <ac/unistd.h>
35 #include "ldap_defaults.h"
39 #include "slapd-common.h"
41 #define SEARCHCMD "slapd-search"
42 #define READCMD "slapd-read"
43 #define ADDCMD "slapd-addel"
44 #define MODRDNCMD "slapd-modrdn"
45 #define MODIFYCMD "slapd-modify"
46 #define BINDCMD "slapd-bind"
50 #define OUTERLOOPS "1"
53 #define TSEARCHFILE "do_search.0"
54 #define TREADFILE "do_read.0"
55 #define TADDFILE "do_add."
56 #define TMODRDNFILE "do_modrdn.0"
57 #define TMODIFYFILE "do_modify.0"
58 #define TBINDFILE "do_bind.0"
60 static char *get_file_name( char *dirname, char *filename );
61 static int get_search_filters( char *filename, char *filters[], char *bases[] );
62 static int get_read_entries( char *filename, char *entries[] );
63 static void fork_child( char *prog, char **args );
64 static void wait4kids( int nkidval );
66 static int maxkids = 20;
70 static HANDLE *children;
71 static char argbuf[BUFSIZ];
72 #define ArgDup(x) strdup(strcat(strcat(strcpy(argbuf,"\""),x),"\""))
74 #define ArgDup(x) strdup(x)
82 "-H <uri> | ([-h <host>] -p <port>) "
98 main( int argc, char **argv )
102 char *host = "localhost";
104 char *manager = NULL;
106 char *dirname = NULL;
107 char *progdir = NULL;
109 char *outerloops = OUTERLOOPS;
110 char *retries = RETRIES;
117 char *sreqs[MAXREQS];
118 char *sbase[MAXREQS];
120 char *sargs[MAXARGS];
122 char scmd[MAXPATHLEN];
123 char sloops[] = "18446744073709551615UL";
126 char *rreqs[MAXREQS];
128 char *rargs[MAXARGS];
130 char rcmd[MAXPATHLEN];
131 char rloops[] = "18446744073709551615UL";
133 char *afiles[MAXREQS];
135 char *aargs[MAXARGS];
137 char acmd[MAXPATHLEN];
138 char aloops[] = "18446744073709551615UL";
141 char *mreqs[MAXREQS];
143 char *margs[MAXARGS];
145 char mcmd[MAXPATHLEN];
146 char mloops[] = "18446744073709551615UL";
148 char *modfile = NULL;
149 char *modreqs[MAXREQS];
150 char *moddn[MAXREQS];
152 char *modargs[MAXARGS];
154 char modcmd[MAXPATHLEN];
155 char modloops[] = "18446744073709551615UL";
158 char *breqs[MAXREQS];
159 char *bcreds[MAXREQS];
161 char *bargs[MAXARGS];
163 char bcmd[MAXPATHLEN];
164 char bloops[] = "18446744073709551615UL";
166 char *friendlyOpt = NULL;
168 tester_init( "slapd-tester" );
170 while ( (i = getopt( argc, argv, "D:d:FH:h:j:l:L:P:p:r:t:w:" )) != EOF ) {
172 case 'D': /* slapd manager */
173 manager = ArgDup( optarg );
176 case 'd': /* data directory */
177 dirname = strdup( optarg );
184 case 'H': /* slapd uri */
185 uri = strdup( optarg );
188 case 'h': /* slapd host */
189 host = strdup( optarg );
192 case 'j': /* the number of parallel clients */
193 if ( lutil_atoi( &maxkids, optarg ) != 0 ) {
198 case 'l': /* the number of loops per client */
199 if ( lutil_atoi( &loops, optarg ) != 0 ) {
204 case 'L': /* the number of outerloops per client */
205 outerloops = strdup( optarg );
208 case 'P': /* prog directory */
209 progdir = strdup( optarg );
212 case 'p': /* the servers port number */
213 port = strdup( optarg );
216 case 'r': /* the number of retries in case of error */
217 retries = strdup( optarg );
220 case 't': /* the delay in seconds between each retry */
221 delay = strdup( optarg );
224 case 'w': /* the managers passwd */
225 passwd = ArgDup( optarg );
234 if (( dirname == NULL ) || ( port == NULL && uri == NULL ) ||
235 ( manager == NULL ) || ( passwd == NULL ) || ( progdir == NULL ))
239 children = malloc( maxkids * sizeof(HANDLE) );
241 /* get the file list */
242 if ( ( datadir = opendir( dirname )) == NULL ) {
244 fprintf( stderr, "%s: couldn't open data directory \"%s\".\n",
246 exit( EXIT_FAILURE );
250 /* look for search, read, modrdn, and add/delete files */
251 for ( file = readdir( datadir ); file; file = readdir( datadir )) {
253 if ( !strcasecmp( file->d_name, TSEARCHFILE )) {
254 sfile = get_file_name( dirname, file->d_name );
256 } else if ( !strcasecmp( file->d_name, TREADFILE )) {
257 rfile = get_file_name( dirname, file->d_name );
259 } else if ( !strcasecmp( file->d_name, TMODRDNFILE )) {
260 mfile = get_file_name( dirname, file->d_name );
262 } else if ( !strcasecmp( file->d_name, TMODIFYFILE )) {
263 modfile = get_file_name( dirname, file->d_name );
265 } else if ( !strncasecmp( file->d_name, TADDFILE, strlen( TADDFILE ))
266 && ( anum < MAXREQS )) {
267 afiles[anum++] = get_file_name( dirname, file->d_name );
269 } else if ( !strcasecmp( file->d_name, TBINDFILE )) {
270 bfile = get_file_name( dirname, file->d_name );
277 /* look for search requests */
279 snum = get_search_filters( sfile, sreqs, sbase );
282 /* look for read requests */
284 rnum = get_read_entries( rfile, rreqs );
287 /* look for modrdn requests */
289 mnum = get_read_entries( mfile, mreqs );
292 /* look for modify requests */
294 modnum = get_search_filters( modfile, modreqs, moddn );
297 /* look for bind requests */
299 bnum = get_search_filters( bfile, bcreds, breqs );
302 /* setup friendly option */
304 switch ( friendly ) {
313 /* NOTE: right now we don't need it more than twice */
319 snprintf( sloops, sizeof( sloops ), "%d", 10 * loops );
320 snprintf( rloops, sizeof( rloops ), "%d", 20 * loops );
321 snprintf( aloops, sizeof( aloops ), "%d", loops );
322 snprintf( mloops, sizeof( mloops ), "%d", loops );
323 snprintf( modloops, sizeof( modloops ), "%d", loops );
324 snprintf( bloops, sizeof( bloops ), "%d", 20 * loops );
327 * generate the search clients
331 snprintf( scmd, sizeof scmd, "%s" LDAP_DIRSEP SEARCHCMD,
333 sargs[sanum++] = scmd;
335 sargs[sanum++] = "-H";
336 sargs[sanum++] = uri;
338 sargs[sanum++] = "-h";
339 sargs[sanum++] = host;
340 sargs[sanum++] = "-p";
341 sargs[sanum++] = port;
343 sargs[sanum++] = "-D";
344 sargs[sanum++] = manager;
345 sargs[sanum++] = "-w";
346 sargs[sanum++] = passwd;
347 sargs[sanum++] = "-l";
348 sargs[sanum++] = sloops;
349 sargs[sanum++] = "-L";
350 sargs[sanum++] = outerloops;
351 sargs[sanum++] = "-r";
352 sargs[sanum++] = retries;
353 sargs[sanum++] = "-t";
354 sargs[sanum++] = delay;
355 sargs[sanum++] = "-b";
356 sargs[sanum++] = NULL; /* will hold the search base */
357 sargs[sanum++] = "-f";
358 sargs[sanum++] = NULL; /* will hold the search request */
359 sargs[sanum++] = NULL;
362 * generate the read clients
366 snprintf( rcmd, sizeof rcmd, "%s" LDAP_DIRSEP READCMD,
368 rargs[ranum++] = rcmd;
370 rargs[ranum++] = "-H";
371 rargs[ranum++] = uri;
373 rargs[ranum++] = "-h";
374 rargs[ranum++] = host;
375 rargs[ranum++] = "-p";
376 rargs[ranum++] = port;
378 rargs[ranum++] = "-l";
379 rargs[ranum++] = rloops;
380 rargs[ranum++] = "-L";
381 rargs[ranum++] = outerloops;
382 rargs[ranum++] = "-r";
383 rargs[ranum++] = retries;
384 rargs[ranum++] = "-t";
385 rargs[ranum++] = delay;
386 rargs[ranum++] = "-e";
387 rargs[ranum++] = NULL; /* will hold the read entry */
388 rargs[ranum++] = NULL;
391 * generate the modrdn clients
395 snprintf( mcmd, sizeof mcmd, "%s" LDAP_DIRSEP MODRDNCMD,
397 margs[manum++] = mcmd;
399 margs[manum++] = "-H";
400 margs[manum++] = uri;
402 margs[manum++] = "-h";
403 margs[manum++] = host;
404 margs[manum++] = "-p";
405 margs[manum++] = port;
407 margs[manum++] = "-D";
408 margs[manum++] = manager;
409 margs[manum++] = "-w";
410 margs[manum++] = passwd;
411 margs[manum++] = "-l";
412 margs[manum++] = mloops;
413 margs[manum++] = "-L";
414 margs[manum++] = outerloops;
415 margs[manum++] = "-r";
416 margs[manum++] = retries;
417 margs[manum++] = "-t";
418 margs[manum++] = delay;
420 margs[manum++] = friendlyOpt;
422 margs[manum++] = "-e";
423 margs[manum++] = NULL; /* will hold the modrdn entry */
424 margs[manum++] = NULL;
427 * generate the modify clients
431 snprintf( modcmd, sizeof modcmd, "%s" LDAP_DIRSEP MODIFYCMD,
433 modargs[modanum++] = modcmd;
435 modargs[modanum++] = "-H";
436 modargs[modanum++] = uri;
438 modargs[modanum++] = "-h";
439 modargs[modanum++] = host;
440 modargs[modanum++] = "-p";
441 modargs[modanum++] = port;
443 modargs[modanum++] = "-D";
444 modargs[modanum++] = manager;
445 modargs[modanum++] = "-w";
446 modargs[modanum++] = passwd;
447 modargs[modanum++] = "-l";
448 modargs[modanum++] = modloops;
449 modargs[modanum++] = "-L";
450 modargs[modanum++] = outerloops;
451 modargs[modanum++] = "-r";
452 modargs[modanum++] = retries;
453 modargs[modanum++] = "-t";
454 modargs[modanum++] = delay;
456 modargs[modanum++] = friendlyOpt;
458 modargs[modanum++] = "-e";
459 modargs[modanum++] = NULL; /* will hold the modify entry */
460 modargs[modanum++] = "-a";;
461 modargs[modanum++] = NULL; /* will hold the ava */
462 modargs[modanum++] = NULL;
465 * generate the add/delete clients
469 snprintf( acmd, sizeof acmd, "%s" LDAP_DIRSEP ADDCMD,
471 aargs[aanum++] = acmd;
473 aargs[aanum++] = "-H";
474 aargs[aanum++] = uri;
476 aargs[aanum++] = "-h";
477 aargs[aanum++] = host;
478 aargs[aanum++] = "-p";
479 aargs[aanum++] = port;
481 aargs[aanum++] = "-D";
482 aargs[aanum++] = manager;
483 aargs[aanum++] = "-w";
484 aargs[aanum++] = passwd;
485 aargs[aanum++] = "-l";
486 aargs[aanum++] = aloops;
487 aargs[aanum++] = "-L";
488 aargs[aanum++] = outerloops;
489 aargs[aanum++] = "-r";
490 aargs[aanum++] = retries;
491 aargs[aanum++] = "-t";
492 aargs[aanum++] = delay;
494 aargs[aanum++] = friendlyOpt;
496 aargs[aanum++] = "-f";
497 aargs[aanum++] = NULL; /* will hold the add data file */
498 aargs[aanum++] = NULL;
501 * generate the bind clients
505 snprintf( bcmd, sizeof bcmd, "%s" LDAP_DIRSEP BINDCMD,
507 bargs[banum++] = bcmd;
509 bargs[banum++] = "-H";
510 bargs[banum++] = uri;
512 bargs[banum++] = "-h";
513 bargs[banum++] = host;
514 bargs[banum++] = "-p";
515 bargs[banum++] = port;
517 bargs[banum++] = "-l";
518 bargs[banum++] = bloops;
519 bargs[banum++] = "-L";
520 bargs[banum++] = outerloops;
522 bargs[banum++] = "-r";
523 bargs[banum++] = retries;
524 bargs[banum++] = "-t";
525 bargs[banum++] = delay;
528 bargs[banum++] = friendlyOpt;
530 bargs[banum++] = "-D";
531 bargs[banum++] = NULL;
532 bargs[banum++] = "-w";
533 bargs[banum++] = NULL;
534 bargs[banum++] = NULL;
536 for ( j = 0; j < MAXREQS; j++ ) {
539 sargs[sanum - 2] = sreqs[j];
540 sargs[sanum - 4] = sbase[j];
541 fork_child( scmd, sargs );
547 rargs[ranum - 2] = rreqs[j];
548 fork_child( rcmd, rargs );
554 margs[manum - 2] = mreqs[j];
555 fork_child( mcmd, margs );
560 modargs[modanum - 4] = moddn[j];
561 modargs[modanum - 2] = modreqs[j];
562 fork_child( modcmd, modargs );
568 aargs[aanum - 2] = afiles[j];
569 fork_child( acmd, aargs );
575 bargs[banum - 4] = breqs[j];
576 bargs[banum - 2] = bcreds[j];
577 fork_child( bcmd, bargs );
585 exit( EXIT_SUCCESS );
589 get_file_name( char *dirname, char *filename )
591 char buf[MAXPATHLEN];
593 snprintf( buf, sizeof buf, "%s" LDAP_DIRSEP "%s",
595 return( strdup( buf ));
600 get_search_filters( char *filename, char *filters[], char *bases[] )
605 if ( (fp = fopen( filename, "r" )) != NULL ) {
608 while (( filter < MAXREQS ) && ( fgets( line, BUFSIZ, fp ))) {
611 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
613 bases[filter] = ArgDup( line );
614 fgets( line, BUFSIZ, fp );
615 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
618 filters[filter++] = ArgDup( line );
629 get_read_entries( char *filename, char *entries[] )
634 if ( (fp = fopen( filename, "r" )) != NULL ) {
637 while (( entry < MAXREQS ) && ( fgets( line, BUFSIZ, fp ))) {
640 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
642 entries[entry++] = ArgDup( line );
653 fork_child( char *prog, char **args )
657 wait4kids( maxkids );
659 switch ( pid = fork() ) {
662 /* The __LIBASCII execvp only handles ASCII "prog",
663 * we still need to translate the arg vec ourselves.
665 { char *arg2[MAXREQS];
668 for (i=0; args[i]; i++) {
669 arg2[i] = ArgDup(args[i]);
675 execvp( prog, args );
676 tester_perror( "execvp" );
677 exit( EXIT_FAILURE );
680 case -1: /* trouble */
681 tester_perror( "fork" );
684 default: /* parent */
691 wait4kids( int nkidval )
695 while ( nkids >= nkidval ) {
698 if ( WIFSTOPPED(status) ) {
700 "stopping: child stopped with signal %d\n",
701 (int) WSTOPSIG(status) );
703 } else if ( WIFSIGNALED(status) ) {
705 "stopping: child terminated with signal %d%s\n",
706 (int) WTERMSIG(status),
708 WCOREDUMP(status) ? ", core dumped" : ""
713 exit( WEXITSTATUS(status) );
715 } else if ( WEXITSTATUS(status) != 0 ) {
717 "stopping: child exited with status %d\n",
718 (int) WEXITSTATUS(status) );
719 exit( WEXITSTATUS(status) );
729 wait4kids( int nkidval )
733 while ( nkids >= nkidval ) {
734 rc = WaitForMultipleObjects( nkids, children, FALSE, INFINITE );
735 for ( i=rc - WAIT_OBJECT_0; i<nkids-1; i++)
736 children[i] = children[i+1];
742 fork_child( char *prog, char **args )
746 wait4kids( maxkids );
748 rc = _spawnvp( _P_NOWAIT, prog, args );
751 tester_perror( "_spawnvp" );
753 children[nkids++] = (HANDLE)rc;