]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-search.c
big mutex needs #ifdef HAVE_BERKELEY_DB_THREAD wrapper
[openldap] / tests / progs / slapd-search.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 #include "portable.h"
7
8 #include <stdio.h>
9
10 #include <ac/stdlib.h>
11
12 #include <ac/ctype.h>
13 #include <ac/param.h>
14 #include <ac/socket.h>
15 #include <ac/string.h>
16 #include <ac/unistd.h>
17 #include <ac/wait.h>
18
19
20 #include <ldap.h>
21
22 #define LOOPS   100
23
24 static void
25 do_search( char *host, int port, char *sbase, char *filter, int maxloop );
26
27 static void
28 usage( char *name )
29 {
30         fprintf( stderr, "usage: %s [-h <host>] -p port -b <searchbase> -f <searchfiter> [-l <loops>]\n",
31                         name );
32         exit( EXIT_FAILURE );
33 }
34
35 int
36 main( int argc, char **argv )
37 {
38         int             i;
39         char        *host = "localhost";
40         int                     port = -1;
41         char        *sbase = NULL;
42         char            *filter  = NULL;
43         int                     loops = LOOPS;
44
45         while ( (i = getopt( argc, argv, "h:p:b:f:l:" )) != EOF ) {
46                 switch( i ) {
47                         case 'h':               /* the servers host */
48                                 host = strdup( optarg );
49                         break;
50
51                         case 'p':               /* the servers port */
52                                 port = atoi( optarg );
53                                 break;
54
55                         case 'b':               /* file with search base */
56                                 sbase = strdup( optarg );
57                         break;
58
59                         case 'f':               /* the search request */
60                                 filter = strdup( optarg );
61                                 break;
62
63                         case 'l':               /* number of loops */
64                                 loops = atoi( optarg );
65                                 break;
66
67                         default:
68                                 usage( argv[0] );
69                                 break;
70                 }
71         }
72
73         if (( sbase == NULL ) || ( filter == NULL ) || ( port == -1 ))
74                 usage( argv[0] );
75
76         if ( *filter == '\0' ) {
77
78                 fprintf( stderr, "%s: invalid EMPTY search filter.\n",
79                                 argv[0] );
80                 exit( EXIT_FAILURE );
81
82         }
83
84         do_search( host, port, sbase, filter, ( 4 * loops ));
85
86         exit( EXIT_SUCCESS );
87 }
88
89
90 static void
91 do_search( char *host, int port, char *sbase, char *filter, int maxloop )
92 {
93         LDAP    *ld;
94         int     i;
95         char    *attrs[] = { "cn", "sn", NULL };
96         pid_t   pid = getpid();
97
98         if (( ld = ldap_init( host, port )) == NULL ) {
99                 perror( "ldap_init" );
100                 exit( EXIT_FAILURE );
101         }
102
103         if ( ldap_bind_s( ld, NULL, NULL, LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
104                 ldap_perror( ld, "ldap_bind" );
105                  exit( EXIT_FAILURE );
106         }
107
108
109         fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\".\n",
110                                 (long) pid, maxloop, sbase, filter );
111
112         for ( i = 0; i < maxloop; i++ ) {
113                 LDAPMessage *res;
114                 int         rc;
115
116                 if (( rc = ldap_search_s( ld, sbase, LDAP_SCOPE_SUBTREE,
117                                 filter, attrs, 0, &res )) != LDAP_SUCCESS ) {
118
119                         ldap_perror( ld, "ldap_search" );
120                         if ( rc != LDAP_NO_SUCH_OBJECT ) break;
121                         continue;
122
123                 }
124
125                 ldap_msgfree( res );
126         }
127
128         fprintf( stderr, " PID=%ld - Search done.\n", (long) pid );
129
130         ldap_unbind( ld );
131 }
132
133