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