]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-read.c
split operations in inner/outer loops
[openldap] / tests / progs / slapd-read.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2006 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
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>.
14  */
15 /* ACKNOWLEDGEMENTS:
16  * This work was initially developed by Kurt Spanier for inclusion
17  * in OpenLDAP Software.
18  */
19
20 #include "portable.h"
21
22 #include <stdio.h>
23
24 #include <ac/stdlib.h>
25
26 #include <ac/ctype.h>
27 #include <ac/param.h>
28 #include <ac/socket.h>
29 #include <ac/string.h>
30 #include <ac/unistd.h>
31 #include <ac/wait.h>
32
33 #include <ldap.h>
34 #include <lutil.h>
35
36 #include "slapd-common.h"
37
38 #define LOOPS   100
39 #define RETRIES 0
40
41 static void
42 do_read( char *uri, char *entry, int maxloop,
43                 int maxretries, int delay );
44
45 static void
46 usage( char *name )
47 {
48         fprintf( stderr,
49                 "usage: %s "
50                 "-H <uri> | ([-h <host>] -p <port>) "
51                 "-e <entry> "
52                 "[-l <loops>] "
53                 "[-L <outerloops>] "
54                 "[-r <maxretries>] "
55                 "[-t <delay>]\n",
56                 name );
57         exit( EXIT_FAILURE );
58 }
59
60 int
61 main( int argc, char **argv )
62 {
63         int             i;
64         char            *uri = NULL;
65         char            *host = "localhost";
66         int             port = -1;
67         char            *entry = NULL;
68         int             loops = LOOPS;
69         int             outerloops = 1;
70         int             retries = RETRIES;
71         int             delay = 0;
72
73         tester_init( "slapd-read" );
74
75         while ( (i = getopt( argc, argv, "H:h:p:e:l:L:r:t:" )) != EOF ) {
76                 switch( i ) {
77                 case 'H':               /* the server uri */
78                         uri = strdup( optarg );
79                         break;
80
81                 case 'h':               /* the servers host */
82                         host = strdup( optarg );
83                         break;
84
85                 case 'p':               /* the servers port */
86                         if ( lutil_atoi( &port, optarg ) != 0 ) {
87                                 usage( argv[0] );
88                         }
89                         break;
90
91                 case 'e':               /* DN to search for */
92                         entry = strdup( optarg );
93                         break;
94
95                 case 'l':               /* the number of loops */
96                         if ( lutil_atoi( &loops, optarg ) != 0 ) {
97                                 usage( argv[0] );
98                         }
99                         break;
100
101                 case 'L':               /* the number of outerloops */
102                         if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
103                                 usage( argv[0] );
104                         }
105                         break;
106
107                 case 'r':               /* the number of retries */
108                         if ( lutil_atoi( &retries, optarg ) != 0 ) {
109                                 usage( argv[0] );
110                         }
111                         break;
112
113                 case 't':               /* delay in seconds */
114                         if ( lutil_atoi( &delay, optarg ) != 0 ) {
115                                 usage( argv[0] );
116                         }
117                         break;
118
119                 default:
120                         usage( argv[0] );
121                         break;
122                 }
123         }
124
125         if (( entry == NULL ) || ( port == -1 && uri == NULL ))
126                 usage( argv[0] );
127
128         if ( *entry == '\0' ) {
129                 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
130                                 argv[0] );
131                 exit( EXIT_FAILURE );
132         }
133
134         uri = tester_uri( uri, host, port );
135
136         for ( i = 0; i < outerloops; i++ ) {
137                 do_read( uri, entry, loops, retries, delay );
138         }
139
140         exit( EXIT_SUCCESS );
141 }
142
143
144 static void
145 do_read( char *uri, char *entry, int maxloop,
146                 int maxretries, int delay )
147 {
148         LDAP    *ld = NULL;
149         int     i = 0, do_retry = maxretries;
150         char    *attrs[] = { "1.1", NULL };
151         pid_t   pid = getpid();
152         int     rc = LDAP_SUCCESS;
153         int     version = LDAP_VERSION3;
154         struct berval   passwd = { 0, "" };
155         
156 retry:;
157         ldap_initialize( &ld, uri );
158         if ( ld == NULL ) {
159                 tester_perror( "ldap_initialize" );
160                 exit( EXIT_FAILURE );
161         }
162
163         (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ); 
164
165         if ( do_retry == maxretries ) {
166                 fprintf( stderr, "PID=%ld - Read(%d): entry=\"%s\".\n",
167                         (long) pid, maxloop, entry );
168         }
169
170         rc = ldap_sasl_bind_s( ld, NULL, LDAP_SASL_SIMPLE, &passwd, NULL, NULL, NULL );
171         if ( rc != LDAP_SUCCESS ) {
172                 tester_ldap_error( ld, "ldap_sasl_bind_s" );
173                 switch ( rc ) {
174                 case LDAP_BUSY:
175                 case LDAP_UNAVAILABLE:
176                         if ( do_retry > 0 ) {
177                                 do_retry--;
178                                 if ( delay > 0 ) {
179                                     sleep( delay );
180                                 }
181                                 goto retry;
182                         }
183                 /* fallthru */
184                 default:
185                         break;
186                 }
187                 exit( EXIT_FAILURE );
188         }
189
190         for ( ; i < maxloop; i++ ) {
191                 LDAPMessage *res;
192
193                 rc = ldap_search_ext_s( ld, entry, LDAP_SCOPE_BASE,
194                                 NULL, attrs, 1, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
195                 if ( rc != LDAP_SUCCESS ) {
196                         tester_ldap_error( ld, "ldap_search_ext_s" );
197                         if ( rc == LDAP_BUSY && do_retry > 0 ) {
198                                 do_retry--;
199                                 goto retry;
200                         }
201                         if ( rc != LDAP_NO_SUCH_OBJECT ) break;
202                         continue;
203
204                 }
205
206                 ldap_msgfree( res );
207         }
208
209         fprintf( stderr, " PID=%ld - Read done (%d).\n", (long) pid, rc );
210
211         ldap_unbind_ext( ld, NULL, NULL );
212 }
213