11 #include <ac/unistd.h>
13 #include "ldapconfig.h"
16 #ifdef HAVE_SYS_FILIO_H
17 #include <sys/filio.h>
18 #elif HAVE_SYS_IOCTL_H
19 #include <sys/ioctl.h>
25 int allow_severity = LOG_INFO;
26 int deny_severity = LOG_NOTICE;
27 #endif /* TCP Wrappers */
32 static ldap_pvt_thread_t listener_tid;
33 static volatile sig_atomic_t slapd_shutdown = 0;
36 ldap_pvt_thread_mutex_t sd_mutex;
41 /* In winsock, accept() returns values higher than dtblsize
42 so don't bother with this optimization */
52 * Add a descriptor to daemon control
54 static void slapd_add(int s) {
55 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
57 assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
58 assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
59 assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
61 if (s >= slap_daemon.sd_nfds) {
62 slap_daemon.sd_nfds = s + 1;
65 FD_SET( s, &slap_daemon.sd_actives );
66 FD_SET( s, &slap_daemon.sd_readers );
68 Debug( LDAP_DEBUG_CONNS, "daemon: added %d%s%s\n", s,
69 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
70 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
72 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
76 * Remove the descriptor from daemon control
78 void slapd_remove(int s) {
79 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
81 assert( s < slap_daemon.sd_nfds );
82 assert( FD_ISSET( s, &slap_daemon.sd_actives ));
84 Debug( LDAP_DEBUG_CONNS, "daemon: removing %d%s%s\n", s,
85 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
86 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
88 FD_CLR( s, &slap_daemon.sd_actives );
89 FD_CLR( s, &slap_daemon.sd_readers );
90 FD_CLR( s, &slap_daemon.sd_writers );
92 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
95 void slapd_clr_write(int s, int wake) {
96 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
98 assert( s < slap_daemon.sd_nfds );
99 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
100 assert( FD_ISSET( s, &slap_daemon.sd_writers) );
101 FD_SET( s, &slap_daemon.sd_writers );
103 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
106 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
110 void slapd_set_write(int s, int wake) {
111 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
113 FD_SET( s, &slap_daemon.sd_writers );
115 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
118 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
122 static void slapd_close(int s) {
125 Debug( LDAP_DEBUG_CONNS, "daemon: closing %d\n", s, 0, 0 );
134 struct sockaddr_in *addr = ptr;
138 dtblsize = sysconf( _SC_OPEN_MAX );
139 #elif HAVE_GETDTABLESIZE
140 dtblsize = getdtablesize();
142 dtblsize = FD_SETSIZE
146 if(dtblsize > FD_SETSIZE) {
147 dtblsize = FD_SETSIZE;
149 #endif /* !FD_SETSIZE */
153 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
154 FD_ZERO( &slap_daemon.sd_readers );
155 FD_ZERO( &slap_daemon.sd_writers );
160 if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
161 Debug( LDAP_DEBUG_ANY,
162 "daemon: socket() failed errno %d (%s)", errno,
163 errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
169 if ( tcps >= dtblsize ) {
170 Debug( LDAP_DEBUG_ANY,
171 "daemon: listener descriptor %d is too great",
178 if ( setsockopt( tcps, SOL_SOCKET, SO_REUSEADDR,
179 (char *) &tmp, sizeof(tmp) ) == -1 )
181 Debug( LDAP_DEBUG_ANY,
182 "slapd(%d): setsockopt() failed errno %d (%s)",
184 errno > -1 && errno < sys_nerr
185 ? sys_errlist[errno] : "unknown" );
190 if ( bind( tcps, (struct sockaddr *) addr, sizeof(*addr) ) == -1 ) {
191 Debug( LDAP_DEBUG_ANY, "daemon: bind(%d) failed errno %d (%s)\n",
193 errno > -1 && errno < sys_nerr
194 ? sys_errlist[errno] : "unknown" );
198 if ( listen( tcps, 5 ) == -1 ) {
199 Debug( LDAP_DEBUG_ANY,
200 "daemon: listen(%d, 5) failed errno %d (%s)\n",
202 errno > -1 && errno < sys_nerr
203 ? sys_errlist[errno] : "unknown" );
210 if( connection_init( 0, NULL, NULL ) ) {
211 Debug( LDAP_DEBUG_ANY,
212 "connection_init(%d) failed.\n",
221 while ( !slapd_shutdown ) {
227 struct sockaddr_in from;
235 FD_ZERO( &writefds );
241 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
243 #ifdef FD_SET_MANUAL_COPY
244 for( s = 0; s < nfds; s++ ) {
245 if(FD_ISSET( &slap_sd_writers, s )) {
246 FD_SET( &writefds, s );
248 if(FD_ISSET( &slap_sd_writers, s )) {
249 FD_SET( &writefds, s );
253 memcpy( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
254 memcpy( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
257 FD_SET( tcps, &readfds );
260 nfds = slap_daemon.sd_nfds;
265 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
267 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
268 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
271 tvp = active_threads ? &zero : NULL;
274 Debug( LDAP_DEBUG_CONNS,
275 "daemon: select: tcps=%d active_threads=%d tvp=%s\n",
276 tcps, active_threads,
277 tvp == NULL ? "NULL" : "zero" );
280 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
282 switch(ns = select( nfds, &readfds, &writefds, 0, tvp )) {
283 case -1: /* failure - try again */
284 if( errno != EINTR ) {
285 Debug( LDAP_DEBUG_CONNS,
286 "daemon: select failed (%d): %s\n",
288 errno >= 0 && errno < sys_nerr
289 ? sys_errlist[errno] : "unknown",
297 case 0: /* timeout - let threads run */
298 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
300 ldap_pvt_thread_yield();
303 default: /* something happened - deal with it */
304 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
309 if ( FD_ISSET( tcps, &readfds ) ) {
311 int len = sizeof(from);
314 if ( (s = accept( tcps,
315 (struct sockaddr *) &from, &len )) == -1 )
317 Debug( LDAP_DEBUG_ANY,
318 "daemon: accept(%d) failed errno %d (%s)", errno,
319 tcps, errno >= 0 && errno < sys_nerr ?
320 sys_errlist[errno] : "unknown");
324 assert( !FD_ISSET( 0, &slap_daemon.sd_actives) );
325 assert( !FD_ISSET( 0, &slap_daemon.sd_readers) );
326 assert( !FD_ISSET( 0, &slap_daemon.sd_writers) );
329 /* make sure descriptor number isn't too great */
330 if ( s >= dtblsize ) {
331 Debug( LDAP_DEBUG_ANY,
332 "daemon: %d beyond descriptor table size %d\n",
339 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %d\n",
343 if ( getpeername( s, (struct sockaddr *) &from, &len ) == 0 ) {
344 client_addr = inet_ntoa( from.sin_addr );
346 #if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
347 hp = gethostbyaddr( (char *)
348 &(from.sin_addr.s_addr),
349 sizeof(from.sin_addr.s_addr), AF_INET );
353 client_name = hp->h_name;
355 /* normalize the domain */
356 for ( p = client_name; *p; p++ ) {
357 *p = TOLOWER( (unsigned char) *p );
373 if(!hosts_ctl("slapd",
374 client_name != NULL ? client_name : STRING_UNKNOWN,
375 client_addr != NULL ? client_addr : STRING_UNKNOWN,
379 Statslog( LDAP_DEBUG_ANY,
380 "fd=%d connection from %s (%s) denied.\n",
382 client_name == NULL ? "unknown" : client_name,
383 client_addr == NULL ? "unknown" : client_addr,
389 #endif /* HAVE_TCPD */
391 if( (id = connection_init(s, client_name, client_addr)) < 0 ) {
392 Debug( LDAP_DEBUG_ANY,
393 "daemon: connection_init(%d, %s, %s) failed.\n",
395 client_name == NULL ? "unknown" : client_name,
396 client_addr == NULL ? "unknown" : client_addr);
401 Statslog( LDAP_DEBUG_STATS,
402 "daemon: conn=%d fd=%d connection from %s (%s) accepted.\n",
404 client_name == NULL ? "unknown" : client_name,
405 client_addr == NULL ? "unknown" : client_addr,
413 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
415 for ( i = 0; i < nfds; i++ ) {
418 r = FD_ISSET( i, &readfds );
419 w = FD_ISSET( i, &writefds );
420 if ( i != tcps && (r || w) ) {
421 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
422 r ? "r" : "", w ? "w" : "" );
426 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
429 /* loop through the writers */
430 for ( i = 0; i < nfds; i++ ) {
435 if ( FD_ISSET( i, &writefds ) ) {
436 Debug( LDAP_DEBUG_CONNS,
437 "daemon: signaling write waiter on %d\n", i, 0, 0 );
439 assert( FD_ISSET( 0, &slap_daemon.sd_actives) );
441 /* clear the write flag */
442 slapd_clr_write( i, 0 );
444 if( connection_write( i ) < 0 ) {
445 FD_CLR( i, &readfds );
451 for ( i = 0; i < nfds; i++ ) {
456 if ( FD_ISSET( i, &readfds ) ) {
457 Debug( LDAP_DEBUG_CONNS,
458 "daemon: read activity on %d\n", i, 0, 0 );
460 assert( FD_ISSET( i, &slap_daemon.sd_actives) );
462 if( connection_read( i ) < 0) {
468 ldap_pvt_thread_yield();
471 if( slapd_shutdown > 0 ) {
472 Debug( LDAP_DEBUG_TRACE,
473 "daemon: shutdown requested and initiated.\n",
476 } else if ( slapd_shutdown < 0 ) {
477 Debug( LDAP_DEBUG_TRACE,
478 "daemon: abnormal condition, shutdown initiated.\n",
481 Debug( LDAP_DEBUG_TRACE,
482 "daemon: no active streams, shutdown initiated.\n",
490 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
491 Debug( LDAP_DEBUG_ANY,
492 "slapd shutdown: waiting for %d threads to terminate\n",
493 active_threads, 0, 0 );
494 while ( active_threads > 0 ) {
495 ldap_pvt_thread_cond_wait(&active_threads_cond, &active_threads_mutex);
497 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
502 int slapd_daemon( struct sockaddr_in *addr )
506 status = ldap_pvt_thread_create( &listener_tid, 0,
507 slapd_daemon_task, addr );
510 Debug( LDAP_DEBUG_ANY,
511 "listener ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
514 /* wait for the listener thread to complete */
515 ldap_pvt_thread_join( listener_tid, (void *) NULL );
522 slap_set_shutdown( int sig )
525 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
528 (void) SIGNAL( sig, slap_set_shutdown );
532 slap_do_nothing( int sig )
535 (void) SIGNAL( sig, slap_do_nothing );