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>
28 #include <ac/socket.h>
29 #include <ac/string.h>
30 #include <ac/unistd.h>
36 #include "slapd-common.h"
42 do_read( char *uri, char *entry, int maxloop,
43 int maxretries, int delay );
50 "-H <uri> | ([-h <host>] -p <port>) "
60 main( int argc, char **argv )
64 char *host = "localhost";
68 int retries = RETRIES;
71 tester_init( "slapd-read" );
73 while ( (i = getopt( argc, argv, "H:h:p:e:l:r:t:" )) != EOF ) {
75 case 'H': /* the server uri */
76 uri = strdup( optarg );
79 case 'h': /* the servers host */
80 host = strdup( optarg );
83 case 'p': /* the servers port */
84 if ( lutil_atoi( &port, optarg ) != 0 ) {
89 case 'e': /* DN to search for */
90 entry = strdup( optarg );
93 case 'l': /* the number of loops */
94 if ( lutil_atoi( &loops, optarg ) != 0 ) {
99 case 'r': /* the number of retries */
100 if ( lutil_atoi( &retries, optarg ) != 0 ) {
105 case 't': /* delay in seconds */
106 if ( lutil_atoi( &delay, optarg ) != 0 ) {
117 if (( entry == NULL ) || ( port == -1 && uri == NULL ))
120 if ( *entry == '\0' ) {
121 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
123 exit( EXIT_FAILURE );
126 uri = tester_uri( uri, host, port );
128 do_read( uri, entry, ( 20 * loops ), retries, delay );
129 exit( EXIT_SUCCESS );
134 do_read( char *uri, char *entry, int maxloop,
135 int maxretries, int delay )
138 int i = 0, do_retry = maxretries;
139 char *attrs[] = { "1.1", NULL };
140 pid_t pid = getpid();
141 int rc = LDAP_SUCCESS;
142 int version = LDAP_VERSION3;
143 struct berval passwd = { 0, "" };
146 ldap_initialize( &ld, uri );
148 tester_perror( "ldap_initialize" );
149 exit( EXIT_FAILURE );
152 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
154 if ( do_retry == maxretries ) {
155 fprintf( stderr, "PID=%ld - Read(%d): entry=\"%s\".\n",
156 (long) pid, maxloop, entry );
159 rc = ldap_sasl_bind_s( ld, NULL, LDAP_SASL_SIMPLE, &passwd, NULL, NULL, NULL );
160 if ( rc != LDAP_SUCCESS ) {
161 tester_ldap_error( ld, "ldap_sasl_bind_s" );
164 case LDAP_UNAVAILABLE:
165 if ( do_retry > 0 ) {
176 exit( EXIT_FAILURE );
179 for ( ; i < maxloop; i++ ) {
182 rc = ldap_search_ext_s( ld, entry, LDAP_SCOPE_BASE,
183 NULL, attrs, 1, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
184 if ( rc != LDAP_SUCCESS ) {
185 tester_ldap_error( ld, "ldap_search_ext_s" );
186 if ( rc == LDAP_BUSY && do_retry > 0 ) {
190 if ( rc != LDAP_NO_SUCH_OBJECT ) break;
198 fprintf( stderr, " PID=%ld - Read done (%d).\n", (long) pid, rc );
200 ldap_unbind_ext( ld, NULL, NULL );