]> git.sur5r.net Git - openldap/blob - tests/slapd-addel.c
More timing for performance testing. Re-introduction of cache.c_mutex.
[openldap] / tests / slapd-addel.c
1 #include "portable.h"
2
3 #include <stdio.h>
4
5 #include <ac/string.h>
6 #include <ac/ctype.h>
7 #include <ac/socket.h>
8 #include <ac/unistd.h>
9 #include <ac/wait.h>
10
11 #include <sys/param.h>
12
13 #include "lber.h"
14 #include "ldap.h"
15
16 #define LOOPS   100
17
18 static char *
19 get_add_entry( char *filename, LDAPMod ***mods );
20
21 static void
22 do_addel( char *host, int port, char *manager, char *passwd,
23         char *dn, LDAPMod **attrs, int maxloop );
24
25 static void
26 usage( char *name )
27 {
28         fprintf( stderr, "usage: %s [-h <host>] -p port -D <managerDN> -w <passwd> -f <addfile> [-l <loops>]\n",
29                         name );
30         exit( 1 );
31 }
32
33 int
34 main( int argc, char **argv )
35 {
36         int             i, j;
37         char        *host = "localhost";
38         int                     port = -1;
39         char            *manager = NULL;
40         char            *passwd = NULL;
41         char            *filename = NULL;
42         char            *entry = NULL;
43         int                     loops = LOOPS;
44         LDAPMod     **attrs = NULL;
45
46         while ( (i = getopt( argc, argv, "h:p:D:w:f:l:" )) != EOF ) {
47                 switch( i ) {
48                         case 'h':               /* the servers host */
49                                 host = strdup( optarg );
50                         break;
51
52                         case 'p':               /* the servers port */
53                                 port = atoi( optarg );
54                                 break;
55
56                         case 'D':               /* the servers manager */
57                                 manager = strdup( optarg );
58                         break;
59
60                         case 'w':               /* the server managers password */
61                                 passwd = strdup( optarg );
62                         break;
63
64                         case 'f':               /* file with entry search request */
65                                 filename = strdup( optarg );
66                                 break;
67
68                         case 'l':               /* the number of loops */
69                                 loops = atoi( optarg );
70                                 break;
71
72                         default:
73                                 usage( argv[0] );
74                                 break;
75                 }
76         }
77
78         if (( filename == NULL ) || ( port == -1 ) ||
79                                 ( manager == NULL ) || ( passwd == NULL ))
80                 usage( argv[0] );
81
82         entry = get_add_entry( filename, &attrs );
83         if (( entry == NULL ) || ( *entry == '\0' )) {
84
85                 fprintf( stderr, "%s: invalid entry DN in file \"%s\".\n",
86                                 argv[0], filename );
87                 exit( 1 );
88
89         }
90
91         if (( attrs == NULL ) || ( *attrs == '\0' )) {
92
93                 fprintf( stderr, "%s: invalid attrs in file \"%s\".\n",
94                                 argv[0], filename );
95                 exit( 1 );
96
97         }
98
99         do_addel( host, port, manager, passwd, entry, attrs, loops );
100
101         exit( 0 );
102 }
103
104
105 #define safe_realloc( ptr, size )   ( ptr == NULL ? malloc( size ) : \
106                                 realloc( ptr, size ))
107
108
109 static void
110 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
111 {
112     LDAPMod             **pmods;
113     int                 i, j;
114     struct berval       *bvp;
115
116     pmods = *pmodsp;
117     modop |= LDAP_MOD_BVALUES;
118
119     i = 0;
120     if ( pmods != NULL ) {
121                 for ( ; pmods[ i ] != NULL; ++i ) {
122                 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
123                         pmods[ i ]->mod_op == modop ) {
124                                 break;
125                 }
126                 }
127     }
128
129     if ( pmods == NULL || pmods[ i ] == NULL ) {
130                 if (( pmods = (LDAPMod **)safe_realloc( pmods, (i + 2) *
131                         sizeof( LDAPMod * ))) == NULL ) {
132                         perror( "safe_realloc" );
133                         exit( 1 );
134                 }
135                 *pmodsp = pmods;
136                 pmods[ i + 1 ] = NULL;
137                 if (( pmods[ i ] = (LDAPMod *)calloc( 1, sizeof( LDAPMod )))
138                         == NULL ) {
139                         perror( "calloc" );
140                         exit( 1 );
141                 }
142                 pmods[ i ]->mod_op = modop;
143                 if (( pmods[ i ]->mod_type = strdup( attr )) == NULL ) {
144                 perror( "strdup" );
145                 exit( 1 );
146                 }
147     }
148
149     if ( value != NULL ) {
150                 j = 0;
151                 if ( pmods[ i ]->mod_bvalues != NULL ) {
152                 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
153                                 ;
154                 }
155                 }
156                 if (( pmods[ i ]->mod_bvalues =
157                         (struct berval **)safe_realloc( pmods[ i ]->mod_bvalues,
158                         (j + 2) * sizeof( struct berval * ))) == NULL ) {
159                         perror( "safe_realloc" );
160                         exit( 1 );
161                 }
162                 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
163                 if (( bvp = (struct berval *)malloc( sizeof( struct berval )))
164                         == NULL ) {
165                         perror( "malloc" );
166                         exit( 1 );
167                 }
168                 pmods[ i ]->mod_bvalues[ j ] = bvp;
169
170             bvp->bv_len = vlen;
171             if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) {
172                         perror( "malloc" );
173                         exit( 1 );
174             }
175             SAFEMEMCPY( bvp->bv_val, value, vlen );
176             bvp->bv_val[ vlen ] = '\0';
177     }
178 }
179
180
181 static char *
182 get_add_entry( char *filename, LDAPMod ***mods )
183 {
184         FILE    *fp;
185         char    *entry = NULL;
186
187         if ( fp = fopen( filename, "r" )) {
188                 char  line[BUFSIZ];
189
190                 if ( fgets( line, BUFSIZ, fp )) {
191                         char *nl;
192
193                         if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
194                                 *nl = '\0';
195                         entry = strdup( line );
196
197                 }
198
199                 while ( fgets( line, BUFSIZ, fp )) {
200                         char    *nl;
201                         char    *value;
202
203                         if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
204                                 *nl = '\0';
205
206                         if ( *line == '\0' ) break;
207                         if ( !( value = strchr( line, ':' ))) break;
208
209                         *value++ = '\0'; 
210                         while ( *value && isspace( *value )) value++;
211
212                         addmodifyop( mods, LDAP_MOD_ADD, line, value, strlen( value ));
213
214                 }
215                 fclose( fp );
216         }
217
218         return( entry );
219 }
220
221
222 static void
223 do_addel(
224         char *host,
225         int port,
226         char *manager,
227         char *passwd,
228         char *entry,
229         LDAPMod **attrs,
230         int maxloop
231 )
232 {
233         LDAP    *ld;
234         int     i;
235         pid_t   pid = getpid();
236
237         if (( ld = ldap_init( host, port )) == NULL ) {
238                 perror( "ldap_init" );
239                 exit( 1 );
240         }
241
242         if ( ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE )
243                                 != LDAP_SUCCESS ) {
244                 ldap_perror( ld, "ldap_bind" );
245                  exit( 1 );
246         }
247
248
249         fprintf( stderr, "PID=%ld - Add/Delete(%d): entry=\"%s\".\n",
250                                         pid, maxloop, entry );
251
252         for ( i = 0; i < maxloop; i++ ) {
253
254                 /* add the entry */
255                 if ( ldap_add_s( ld, entry, attrs ) != LDAP_SUCCESS ) {
256
257                         ldap_perror( ld, "ldap_add" );
258                         break;
259
260                 }
261
262                 /* wait a second for the add to really complete */
263                 sleep( 1 );
264
265                 /* now delete the entry again */
266                 if ( ldap_delete_s( ld, entry ) != LDAP_SUCCESS ) {
267
268                         ldap_perror( ld, "ldap_delete" );
269                         break;
270
271                 }
272
273         }
274
275         fprintf( stderr, " PID=%ld - Add/Delete done.\n", pid );
276
277         ldap_unbind( ld );
278 }
279
280