11 #include <ac/unistd.h>
13 #include "ldapconfig.h"
19 int allow_severity = LOG_INFO;
20 int deny_severity = LOG_NOTICE;
21 #endif /* TCP Wrappers */
26 static ldap_pvt_thread_t listener_tid;
27 static volatile sig_atomic_t slapd_shutdown = 0;
28 static volatile sig_atomic_t slapd_listener = 0;
31 ldap_pvt_thread_mutex_t sd_mutex;
36 /* In winsock, accept() returns values higher than dtblsize
37 so don't bother with this optimization */
47 * Add a descriptor to daemon control
49 static void slapd_add(int s) {
50 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
52 assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
53 assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
54 assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
57 if (s >= slap_daemon.sd_nfds) {
58 slap_daemon.sd_nfds = s + 1;
62 FD_SET( s, &slap_daemon.sd_actives );
63 FD_SET( s, &slap_daemon.sd_readers );
65 Debug( LDAP_DEBUG_CONNS, "daemon: added %d%s%s\n", s,
66 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
67 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
69 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
73 * Remove the descriptor from daemon control
75 void slapd_remove(int s) {
76 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
78 assert( FD_ISSET( s, &slap_daemon.sd_actives ));
80 Debug( LDAP_DEBUG_CONNS, "daemon: removing %d%s%s\n", s,
81 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
82 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
84 FD_CLR( s, &slap_daemon.sd_actives );
85 FD_CLR( s, &slap_daemon.sd_readers );
86 FD_CLR( s, &slap_daemon.sd_writers );
88 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
91 void slapd_clr_write(int s, int wake) {
92 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
94 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
95 FD_CLR( s, &slap_daemon.sd_writers );
97 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
100 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
104 void slapd_set_write(int s, int wake) {
105 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
107 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
108 FD_SET( s, &slap_daemon.sd_writers );
110 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
113 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
117 void slapd_clr_read(int s, int wake) {
118 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
120 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
121 FD_CLR( s, &slap_daemon.sd_readers );
123 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
126 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
130 void slapd_set_read(int s, int wake) {
131 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
133 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
134 FD_SET( s, &slap_daemon.sd_readers );
136 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
139 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
143 static void slapd_close(int s) {
146 Debug( LDAP_DEBUG_CONNS, "daemon: closing %d\n", s, 0, 0 );
151 set_socket( struct sockaddr_in *addr )
156 dtblsize = sysconf( _SC_OPEN_MAX );
157 #elif HAVE_GETDTABLESIZE
158 dtblsize = getdtablesize();
160 dtblsize = FD_SETSIZE;
164 if(dtblsize > FD_SETSIZE) {
165 dtblsize = FD_SETSIZE;
167 #endif /* !FD_SETSIZE */
171 WORD vers = MAKEWORD( 2, 0);
174 err = WSAStartup( vers, &wsaData );
181 if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
183 Debug( LDAP_DEBUG_ANY,
184 "daemon: socket() failed errno %d (%s)\n", err,
185 err > -1 && err < sys_nerr ? sys_errlist[err] :
191 if ( tcps >= dtblsize ) {
192 Debug( LDAP_DEBUG_ANY,
193 "daemon: listener descriptor %d is too great\n",
200 if ( setsockopt( tcps, SOL_SOCKET, SO_REUSEADDR,
201 (char *) &tmp, sizeof(tmp) ) == -1 )
204 Debug( LDAP_DEBUG_ANY,
205 "slapd(%d): setsockopt() failed errno %d (%s)\n",
207 err > -1 && err < sys_nerr
208 ? sys_errlist[err] : "unknown" );
211 if ( bind( tcps, (struct sockaddr *) addr, sizeof(*addr) ) == -1 ) {
213 Debug( LDAP_DEBUG_ANY, "daemon: bind(%d) failed errno %d (%s)\n",
215 err > -1 && err < sys_nerr
216 ? sys_errlist[err] : "unknown" );
229 int inetd = ((int *)ptr) [0];
230 int tcps = ((int *)ptr) [1];
237 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
238 FD_ZERO( &slap_daemon.sd_readers );
239 FD_ZERO( &slap_daemon.sd_writers );
242 if ( listen( tcps, 5 ) == -1 ) {
244 Debug( LDAP_DEBUG_ANY,
245 "daemon: listen(%d, 5) failed errno %d (%s)\n",
247 err > -1 && err < sys_nerr
248 ? sys_errlist[err] : "unknown" );
255 if( connection_init( 0, NULL, NULL ) ) {
256 Debug( LDAP_DEBUG_ANY,
257 "connection_init(%d) failed.\n",
266 while ( !slapd_shutdown ) {
273 struct sockaddr_in from;
274 #if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
283 FD_ZERO( &writefds );
289 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
291 #ifdef FD_SET_MANUAL_COPY
292 for( s = 0; s < nfds; s++ ) {
293 if(FD_ISSET( &slap_sd_writers, s )) {
294 FD_SET( &writefds, s );
296 if(FD_ISSET( &slap_sd_writers, s )) {
297 FD_SET( &writefds, s );
301 memcpy( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
302 memcpy( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
305 FD_SET( tcps, &readfds );
308 nfds = slap_daemon.sd_nfds;
313 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
315 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
316 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
319 tvp = active_threads ? &zero : NULL;
322 Debug( LDAP_DEBUG_CONNS,
323 "daemon: select: tcps=%d active_threads=%d tvp=%s\n",
324 tcps, active_threads,
325 tvp == NULL ? "NULL" : "zero" );
328 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
330 switch(ns = select( nfds, &readfds, &writefds, 0, tvp )) {
331 case -1: { /* failure - try again */
334 Debug( LDAP_DEBUG_CONNS,
335 "daemon: select failed (%d): %s\n",
337 err >= 0 && err < sys_nerr
338 ? sys_errlist[err] : "unknown",
346 case 0: /* timeout - let threads run */
347 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
349 ldap_pvt_thread_yield();
352 default: /* something happened - deal with it */
353 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
358 if ( FD_ISSET( tcps, &readfds ) ) {
360 int len = sizeof(from);
363 if ( (s = accept( tcps,
364 (struct sockaddr *) &from, &len )) == -1 )
367 Debug( LDAP_DEBUG_ANY,
368 "daemon: accept(%d) failed errno %d (%s)\n", err,
369 tcps, err >= 0 && err < sys_nerr ?
370 sys_errlist[err] : "unknown");
374 assert( !FD_ISSET( 0, &slap_daemon.sd_actives) );
375 assert( !FD_ISSET( 0, &slap_daemon.sd_readers) );
376 assert( !FD_ISSET( 0, &slap_daemon.sd_writers) );
379 /* make sure descriptor number isn't too great */
380 if ( s >= dtblsize ) {
381 Debug( LDAP_DEBUG_ANY,
382 "daemon: %d beyond descriptor table size %d\n",
389 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %d\n",
393 if ( getpeername( s, (struct sockaddr *) &from, &len ) == 0 ) {
394 client_addr = inet_ntoa( from.sin_addr );
396 #if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
397 hp = gethostbyaddr( (char *)
398 &(from.sin_addr.s_addr),
399 sizeof(from.sin_addr.s_addr), AF_INET );
403 client_name = hp->h_name;
405 /* normalize the domain */
406 for ( p = client_name; *p; p++ ) {
407 *p = TOLOWER( (unsigned char) *p );
423 if(!hosts_ctl("slapd",
424 client_name != NULL ? client_name : STRING_UNKNOWN,
425 client_addr != NULL ? client_addr : STRING_UNKNOWN,
429 Statslog( LDAP_DEBUG_ANY,
430 "fd=%d connection from %s (%s) denied.\n",
432 client_name == NULL ? "unknown" : client_name,
433 client_addr == NULL ? "unknown" : client_addr,
439 #endif /* HAVE_TCPD */
441 if( (id = connection_init(s, client_name, client_addr)) < 0 ) {
442 Debug( LDAP_DEBUG_ANY,
443 "daemon: connection_init(%d, %s, %s) failed.\n",
445 client_name == NULL ? "unknown" : client_name,
446 client_addr == NULL ? "unknown" : client_addr);
451 Statslog( LDAP_DEBUG_STATS,
452 "daemon: conn=%d fd=%d connection from %s (%s) accepted.\n",
454 client_name == NULL ? "unknown" : client_name,
455 client_addr == NULL ? "unknown" : client_addr,
463 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
465 for ( i = 0; i < readfds.fd_count; i++ )
467 Debug( LDAP_DEBUG_CONNS, " %d%s", readfds.fd_array[i], "r" );
469 for ( i = 0; i < writefds.fd_count; i++ )
471 Debug( LDAP_DEBUG_CONNS, " %d%s", writefds.fd_array[i], "w" );
474 for ( i = 0; i < nfds; i++ ) {
477 r = FD_ISSET( i, &readfds );
478 w = FD_ISSET( i, &writefds );
479 if ( i != tcps && (r || w) ) {
480 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
481 r ? "r" : "", w ? "w" : "" );
485 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
488 /* loop through the writers */
490 for ( i = 0; i < writefds.fd_count; i++ ) {
491 int wd = writefds.fd_array[i];
497 Debug( LDAP_DEBUG_CONNS,
498 "daemon: signalling write waiter on %d\n",
501 assert( FD_ISSET( wd, &slap_daemon.sd_actives) );
503 slapd_clr_write( wd, 0 );
504 if ( connection_write( wd ) < 0 ) {
505 FD_CLR( wd, &readfds );
510 for ( i = 0; i < nfds; i++ ) {
514 if ( FD_ISSET( i, &writefds ) ) {
515 Debug( LDAP_DEBUG_CONNS,
516 "daemon: signaling write waiter on %d\n", i, 0, 0 );
518 assert( FD_ISSET( i, &slap_daemon.sd_actives) );
520 /* clear the write flag */
521 slapd_clr_write( i, 0 );
523 if( connection_write( i ) < 0 ) {
524 FD_CLR( i, &readfds );
532 for ( i = 0; i < readfds.fd_count; i++ ) {
533 int rd = readfds.fd_array[i];
537 Debug ( LDAP_DEBUG_CONNS,
538 "daemon: read activity on %d\n", rd, 0, 0 );
539 assert( FD_ISSET( rd, &slap_daemon.sd_actives) );
541 if ( connection_read( rd ) < 0 ) {
546 for ( i = 0; i < nfds; i++ ) {
551 if ( FD_ISSET( i, &readfds ) ) {
552 Debug( LDAP_DEBUG_CONNS,
553 "daemon: read activity on %d\n", i, 0, 0 );
555 assert( FD_ISSET( i, &slap_daemon.sd_actives) );
557 if( connection_read( i ) < 0) {
563 ldap_pvt_thread_yield();
566 if( slapd_shutdown > 0 ) {
567 Debug( LDAP_DEBUG_TRACE,
568 "daemon: shutdown requested (%d) and initiated.\n",
569 (int) slapd_shutdown, 0, 0 );
571 } else if ( slapd_shutdown < 0 ) {
572 Debug( LDAP_DEBUG_TRACE,
573 "daemon: abnormal condition (%d), shutdown initiated.\n",
574 (int) slapd_shutdown, 0, 0 );
576 Debug( LDAP_DEBUG_TRACE,
577 "daemon: no active streams, shutdown initiated.\n",
585 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
586 Debug( LDAP_DEBUG_ANY,
587 "slapd shutdown: waiting for %d threads to terminate\n",
588 active_threads, 0, 0 );
589 while ( active_threads > 0 ) {
590 ldap_pvt_thread_cond_wait(&active_threads_cond, &active_threads_mutex);
592 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
598 int slapd_daemon( int inetd, int tcps )
601 int *args = ch_malloc( sizeof( int[2] ) );
605 #define SLAPD_LISTENER_THREAD 1
606 #if SLAPD_LISTENER_THREAD
607 /* listener as a separate THREAD */
608 status = ldap_pvt_thread_create( &listener_tid,
609 0, slapd_daemon_task, args );
612 Debug( LDAP_DEBUG_ANY,
613 "listener ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
617 /* wait for the listener thread to complete */
618 ldap_pvt_thread_join( listener_tid, (void *) NULL );
620 /* expermimental code */
621 listener_tid = pthread_self();
622 slapd_daemon_task( args );
629 slap_set_shutdown( int sig )
631 slapd_shutdown = sig;
634 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
638 (void) SIGNAL( sig, slap_set_shutdown );
642 slap_do_nothing( int sig )
645 (void) SIGNAL( sig, slap_do_nothing );