]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-read.c
Update lutil_lockf (aka: ldap_lockf) to hide implementation in
[openldap] / tests / progs / slapd-read.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_read( char *host, int port, char *entry, int maxloop );
21
22 static void
23 usage( char *name )
24 {
25         fprintf( stderr, "usage: %s [-h <host>] -p port -e <entry> [-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            *entry = NULL;
37         int                     loops = LOOPS;
38
39         while ( (i = getopt( argc, argv, "h:p:e:l:" )) != EOF ) {
40                 switch( i ) {
41                         case 'h':               /* the servers host */
42                                 host = strdup( optarg );
43                         break;
44
45                         case 'p':               /* the servers port */
46                                 port = atoi( optarg );
47                                 break;
48
49                         case 'e':               /* file with entry search request */
50                                 entry = strdup( optarg );
51                                 break;
52
53                         case 'l':               /* the number of loops */
54                                 loops = atoi( optarg );
55                                 break;
56
57                         default:
58                                 usage( argv[0] );
59                                 break;
60                 }
61         }
62
63         if (( entry == NULL ) || ( port == -1 ))
64                 usage( argv[0] );
65
66         if ( *entry == '\0' ) {
67
68                 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
69                                 argv[0] );
70                 exit( 1 );
71
72         }
73
74         do_read( host, port, entry, ( 4 * loops ));
75
76         exit( 0 );
77 }
78
79
80 static void
81 do_read( char *host, int port, char *entry, int maxloop )
82 {
83         LDAP    *ld;
84         int     i;
85         char    *attrs[] = { "cn", "sn", NULL };
86         char    *filter = "(objectclass=*)";
87         pid_t   pid = getpid();
88
89         if (( ld = ldap_init( host, port )) == NULL ) {
90                 perror( "ldap_init" );
91                 exit( 1 );
92         }
93
94         if ( ldap_bind_s( ld, NULL, NULL, LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
95                 ldap_perror( ld, "ldap_bind" );
96                  exit( 1 );
97         }
98
99
100         fprintf( stderr, "PID=%ld - Read(%d): entry=\"%s\".\n",
101                  (long) pid, maxloop, entry );
102
103         for ( i = 0; i < maxloop; i++ ) {
104                 LDAPMessage *res;
105                 int         rc;
106
107                 if (( rc = ldap_search_s( ld, entry, LDAP_SCOPE_BASE,
108                                 filter, attrs, 0, &res )) != LDAP_SUCCESS ) {
109
110                         ldap_perror( ld, "ldap_read" );
111                         if ( rc != LDAP_NO_SUCH_OBJECT ) break;
112                         continue;
113
114                 }
115
116                 ldap_msgfree( res );
117         }
118
119         fprintf( stderr, " PID=%ld - Read done.\n", (long) pid );
120
121         ldap_unbind( ld );
122 }
123
124