]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-read.c
add "retry" switch (currently needed to stress-test back-meta); defaults to old behavior
[openldap] / tests / progs / slapd-read.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2005 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 #define LDAP_DEPRECATED 1
34 #include <ldap.h>
35
36 #define LOOPS   100
37 #define RETRIES 0
38
39 static void
40 do_read( char *uri, char *host, int port, char *entry, int maxloop,
41                 int maxretries );
42
43 static void
44 usage( char *name )
45 {
46         fprintf( stderr, "usage: %s [-h <host>] -p port -e <entry> [-l <loops>]\n",
47                         name );
48         exit( EXIT_FAILURE );
49 }
50
51 int
52 main( int argc, char **argv )
53 {
54         int             i;
55         char            *uri = NULL;
56         char            *host = "localhost";
57         int             port = -1;
58         char            *entry = NULL;
59         int             loops = LOOPS;
60         int             retries = RETRIES;
61
62         while ( (i = getopt( argc, argv, "H:h:p:e:l:r:" )) != EOF ) {
63                 switch( i ) {
64                 case 'H':               /* the server uri */
65                         uri = strdup( optarg );
66                         break;
67
68                 case 'h':               /* the servers host */
69                         host = strdup( optarg );
70                         break;
71
72                 case 'p':               /* the servers port */
73                         port = atoi( optarg );
74                         break;
75
76                 case 'e':               /* DN to search for */
77                         entry = strdup( optarg );
78                         break;
79
80                 case 'l':               /* the number of loops */
81                         loops = atoi( optarg );
82                         break;
83
84                 case 'r':               /* the number of retries */
85                         retries = atoi( optarg );
86                         break;
87
88                 default:
89                         usage( argv[0] );
90                         break;
91                 }
92         }
93
94         if (( entry == NULL ) || ( port == -1 && uri == NULL ))
95                 usage( argv[0] );
96
97         if ( *entry == '\0' ) {
98                 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
99                                 argv[0] );
100                 exit( EXIT_FAILURE );
101         }
102
103         do_read( uri, host, port, entry, ( 20 * loops ), retries );
104         exit( EXIT_SUCCESS );
105 }
106
107
108 static void
109 do_read( char *uri, char *host, int port, char *entry, int maxloop,
110                 int maxretries )
111 {
112         LDAP    *ld = NULL;
113         int     i = 0, do_retry = maxretries;
114         char    *attrs[] = { "1.1", NULL };
115         pid_t   pid = getpid();
116         int     rc = LDAP_SUCCESS;
117         
118 retry:;
119         if ( uri ) {
120                 ldap_initialize( &ld, uri );
121         } else {
122                 ld = ldap_init( host, port );
123         }
124         if ( ld == NULL ) {
125                 perror( "ldap_init" );
126                 exit( EXIT_FAILURE );
127         }
128
129         {
130                 int version = LDAP_VERSION3;
131                 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
132                         &version ); 
133         }
134
135         if ( do_retry == maxretries ) {
136                 fprintf( stderr, "PID=%ld - Read(%d): entry=\"%s\".\n",
137                         (long) pid, maxloop, entry );
138         }
139
140         rc = ldap_bind_s( ld, NULL, NULL, LDAP_AUTH_SIMPLE );
141         if ( rc != LDAP_SUCCESS ) {
142                 ldap_perror( ld, "ldap_bind" );
143                 if ( rc == LDAP_BUSY && do_retry > 0 ) {
144                         do_retry--;
145                         goto retry;
146                 }
147                 exit( EXIT_FAILURE );
148         }
149
150         for ( ; i < maxloop; i++ ) {
151                 LDAPMessage *res;
152
153                 rc = ldap_search_s( ld, entry, LDAP_SCOPE_BASE,
154                                 NULL, attrs, 1, &res );
155                 if ( rc != LDAP_SUCCESS ) {
156                         ldap_perror( ld, "ldap_read" );
157                         if ( rc == LDAP_BUSY && do_retry > 0 ) {
158                                 do_retry--;
159                                 goto retry;
160                         }
161                         if ( rc != LDAP_NO_SUCH_OBJECT ) break;
162                         continue;
163
164                 }
165
166                 ldap_msgfree( res );
167         }
168
169         fprintf( stderr, " PID=%ld - Read done (%d).\n", (long) pid, rc );
170
171         ldap_unbind( ld );
172 }
173