10 #include "ldapconfig.h"
12 #include "lutil.h" /* Get lutil_detach() */
16 * read-only global variables or variables only written by the listener
17 * thread (after they are initialized) - no need to protect them with a mutex.
21 int ldap_syslog = LDAP_DEBUG_STATS;
25 int ldap_syslog_level = LOG_DEBUG;
28 char *default_referral;
31 pthread_t listener_tid;
35 * global variables that need mutex protection
38 pthread_mutex_t currenttime_mutex;
40 pthread_mutex_t active_threads_mutex;
41 pthread_mutex_t new_conn_mutex;
45 pthread_mutex_t ops_mutex;
46 long num_entries_sent;
48 pthread_mutex_t num_sent_mutex;
50 * these mutexes must be used when calling the entry2str()
51 * routine since it returns a pointer to static data.
53 pthread_mutex_t entry2str_mutex;
54 pthread_mutex_t replog_mutex;
59 fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]\n", name );
63 main( int argc, char **argv )
72 configfile = SLAPD_DEFAULT_CONFIGFILE;
77 while ( (i = getopt( argc, argv, "d:f:ip:s:u" )) != EOF ) {
80 case 'd': /* turn on debugging */
81 if ( optarg[0] == '?' ) {
82 printf( "Debug levels:\n" );
83 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
85 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
87 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
89 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
91 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
93 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
95 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
97 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
99 printf( "\tLDAP_DEBUG_STATS\t%d\n",
101 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
103 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
105 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
107 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
111 ldap_debug |= atoi( optarg );
112 lber_debug = (ldap_debug & LDAP_DEBUG_BER);
116 case 'd': /* turn on debugging */
118 "must compile with LDAP_DEBUG for debugging\n" );
122 case 'f': /* read config file */
123 configfile = ch_strdup( optarg );
126 case 'i': /* run from inetd */
130 case 'p': /* port on which to listen */
131 port = atoi( optarg );
134 case 's': /* set syslog level */
135 ldap_syslog = atoi( optarg );
138 case 'u': /* do udp */
148 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
150 if ( (myname = strrchr( argv[0], '/' )) == NULL ) {
151 myname = ch_strdup( argv[0] );
153 myname = ch_strdup( myname + 1 );
157 /* pre-open config file before detach in case it is a relative path */
158 fp = fopen( configfile, "r" );
160 lutil_detach( ldap_debug, 0 );
162 lutil_detach( 0, 0 );
166 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL4 );
168 openlog( myname, OPENLOG_OPTIONS );
172 read_config( configfile, &be, fp );
179 pthread_attr_init( &attr );
180 #ifdef DETACH_LISTENER_THREAD
181 /* we should detach it if we're going to join with it */
182 pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
185 #if !defined(HAVE_PTHREADS_D4)
186 /* POSIX_THREADS or compatible
187 * This is a draft 10 or standard pthreads implementation
189 if ( pthread_create( &listener_tid, &attr, slapd_daemon,
190 (void *) port ) != 0 ) {
191 Debug( LDAP_DEBUG_ANY,
192 "listener pthread_create failed\n", 0, 0, 0 );
197 * This is a draft 4 or earlier pthreads implementation
199 if ( pthread_create( &listener_tid, attr, slapd_daemon,
200 (void *) port ) != 0 ) {
201 Debug( LDAP_DEBUG_ANY,
202 "listener pthread_create failed\n", 0, 0, 0 );
206 pthread_attr_destroy( &attr );
207 #ifdef HAVE_PHREADS_FINAL
208 pthread_join( listener_tid, (void *) NULL );
210 pthread_join( listener_tid, (void *) &status );
217 unsigned long len, tag;
220 struct sockaddr_in from;
226 c.c_sb.sb_options = 0;
227 c.c_sb.sb_naddr = udp ? 1 : 0;
228 c.c_sb.sb_ber.ber_buf = NULL;
229 c.c_sb.sb_ber.ber_ptr = NULL;
230 c.c_sb.sb_ber.ber_end = NULL;
231 pthread_mutex_init( &c.c_dnmutex, pthread_mutexattr_default );
232 pthread_mutex_init( &c.c_opsmutex, pthread_mutexattr_default );
233 pthread_mutex_init( &c.c_pdumutex, pthread_mutexattr_default );
235 c.c_sb.sb_addrs = (void **) saddrlist;
236 c.c_sb.sb_fromaddr = &faddr;
237 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
240 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
241 #ifdef SLAPD_RLOOKUPS
242 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
243 sizeof(from.sin_addr.s_addr), AF_INET );
248 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
249 hp == NULL ? "unknown" : hp->h_name,
250 inet_ntoa( from.sin_addr ), 0 );
252 c.c_addr = inet_ntoa( from.sin_addr );
253 c.c_domain = ch_strdup( hp == NULL ? "" : hp->h_name );
255 Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
260 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
261 == LDAP_TAG_MESSAGE ) {
262 pthread_mutex_lock( ¤ttime_mutex );
263 time( ¤ttime );
264 pthread_mutex_unlock( ¤ttime_mutex );
266 if ( (tag = ber_get_int( &ber, &msgid ))
267 != LDAP_TAG_MSGID ) {
268 /* log and send error */
269 Debug( LDAP_DEBUG_ANY,
270 "ber_get_int returns 0x%lx\n", tag, 0, 0 );
275 if ( (tag = ber_peek_tag( &ber, &len ))
277 /* log, close and send error */
278 Debug( LDAP_DEBUG_ANY,
279 "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
281 close( c.c_sb.sb_sd );
286 connection_activity( &c );