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 */
27 /* forward reference */
30 char *WSAGetLastErrorString();
32 #define WAKE_LISTENER \
35 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );\
40 #define WAKE_LISTENER \
43 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );\
48 static int daemon_initialized = 0;
49 static ldap_pvt_thread_t listener_tid;
50 static volatile sig_atomic_t slapd_shutdown = 0;
51 static volatile sig_atomic_t slapd_listener = 0;
55 ldap_pvt_thread_mutex_t sd_mutex;
60 /* In winsock, accept() returns values higher than dtblsize
61 so don't bother with this optimization */
71 * Add a descriptor to daemon control
73 static void slapd_add(int s) {
74 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
76 assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
77 assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
78 assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
81 if (s >= slap_daemon.sd_nfds) {
82 slap_daemon.sd_nfds = s + 1;
86 FD_SET( (unsigned) s, &slap_daemon.sd_actives );
87 FD_SET( (unsigned) s, &slap_daemon.sd_readers );
89 Debug( LDAP_DEBUG_CONNS, "daemon: added %d%s%s\n", s,
90 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
91 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
93 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
97 * Remove the descriptor from daemon control
99 void slapd_remove(int s) {
100 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
102 assert( FD_ISSET( s, &slap_daemon.sd_actives ));
104 Debug( LDAP_DEBUG_CONNS, "daemon: removing %d%s%s\n", s,
105 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
106 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
108 FD_CLR( (unsigned) s, &slap_daemon.sd_actives );
109 FD_CLR( (unsigned) s, &slap_daemon.sd_readers );
110 FD_CLR( (unsigned) s, &slap_daemon.sd_writers );
112 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
115 void slapd_clr_write(int s, int wake) {
116 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
119 assert( FD_ISSET( (unsigned) s, &slap_daemon.sd_actives) );
120 FD_CLR( (unsigned) s, &slap_daemon.sd_writers );
122 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
125 void slapd_set_write(int s, int wake) {
126 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
130 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
131 FD_SET( (unsigned) s, &slap_daemon.sd_writers );
133 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
136 void slapd_clr_read(int s, int wake) {
137 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
140 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
141 FD_CLR( (unsigned) s, &slap_daemon.sd_readers );
143 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
147 void slapd_set_read(int s, int wake) {
148 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
151 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
152 FD_SET( (unsigned) s, &slap_daemon.sd_readers );
154 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
157 static void slapd_close(int s) {
160 Debug( LDAP_DEBUG_CONNS, "daemon: closing %d\n", s, 0, 0 );
165 set_socket( struct sockaddr_in *addr )
168 if ( !daemon_initialized ) sockinit();
171 dtblsize = sysconf( _SC_OPEN_MAX );
172 #elif HAVE_GETDTABLESIZE
173 dtblsize = getdtablesize();
175 dtblsize = FD_SETSIZE;
179 if(dtblsize > FD_SETSIZE) {
180 dtblsize = FD_SETSIZE;
182 #endif /* !FD_SETSIZE */
187 if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
190 Debug( LDAP_DEBUG_ANY,
191 "daemon: socket() failed errno %d (%s)\n", err,
192 err > -1 && err < sys_nerr ? sys_errlist[err] :
195 Debug( LDAP_DEBUG_ANY,
196 "daemon: socket() failed errno %d (%s)\n",
198 WSAGetLastErrorString(), 0 );
204 if ( tcps >= dtblsize ) {
205 Debug( LDAP_DEBUG_ANY,
206 "daemon: listener descriptor %d is too great\n",
214 if ( setsockopt( tcps, SOL_SOCKET, SO_REUSEADDR,
215 (char *) &tmp, sizeof(tmp) ) == -1 )
218 Debug( LDAP_DEBUG_ANY,
219 "slapd(%d): setsockopt() failed errno %d (%s)\n",
221 err > -1 && err < sys_nerr
222 ? sys_errlist[err] : "unknown" );
227 if ( setsockopt( tcps, SOL_SOCKET, SO_KEEPALIVE,
228 (char *) &tmp, sizeof(tmp) ) == -1 )
231 Debug( LDAP_DEBUG_ANY,
232 "slapd(%d): setsockopt(KEEPALIVE) failed errno %d (%s)\n",
234 err > -1 && err < sys_nerr
235 ? sys_errlist[err] : "unknown" );
240 if ( bind( tcps, (struct sockaddr *) addr, sizeof(*addr) ) == -1 ) {
242 Debug( LDAP_DEBUG_ANY, "daemon: bind(%d) failed errno %d (%s)\n",
244 err > -1 && err < sys_nerr
245 ? sys_errlist[err] : "unknown" );
258 int inetd = ((int *)ptr) [0];
259 int tcps = ((int *)ptr) [1];
262 if ( !daemon_initialized ) sockinit();
266 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
267 FD_ZERO( &slap_daemon.sd_readers );
268 FD_ZERO( &slap_daemon.sd_writers );
271 if ( listen( tcps, 5 ) == -1 ) {
273 Debug( LDAP_DEBUG_ANY,
274 "daemon: listen(%d, 5) failed errno %d (%s)\n",
276 err > -1 && err < sys_nerr
277 ? sys_errlist[err] : "unknown" );
284 if( connection_init( 0, NULL, NULL ) ) {
285 Debug( LDAP_DEBUG_ANY,
286 "connection_init(%d) failed.\n",
295 while ( !slapd_shutdown ) {
302 struct sockaddr_in from;
303 #if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
312 FD_ZERO( &writefds );
318 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
320 #ifdef FD_SET_MANUAL_COPY
321 for( s = 0; s < nfds; s++ ) {
322 if(FD_ISSET( &slap_sd_writers, s )) {
323 FD_SET( &writefds, s );
325 if(FD_ISSET( &slap_sd_writers, s )) {
326 FD_SET( &writefds, s );
330 memcpy( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
331 memcpy( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
334 FD_SET( (unsigned) tcps, &readfds );
337 nfds = slap_daemon.sd_nfds;
342 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
344 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
345 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
348 tvp = active_threads ? &zero : NULL;
351 Debug( LDAP_DEBUG_CONNS,
352 "daemon: select: tcps=%d active_threads=%d tvp=%s\n",
353 tcps, active_threads,
354 tvp == NULL ? "NULL" : "zero" );
357 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
359 switch(ns = select( nfds, &readfds, &writefds, 0, tvp )) {
360 case -1: { /* failure - try again */
363 Debug( LDAP_DEBUG_CONNS,
364 "daemon: select failed (%d): %s\n",
366 err >= 0 && err < sys_nerr
367 ? sys_errlist[err] : "unknown",
375 case 0: /* timeout - let threads run */
376 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
378 ldap_pvt_thread_yield();
381 default: /* something happened - deal with it */
382 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
387 if ( FD_ISSET( tcps, &readfds ) ) {
389 int len = sizeof(from);
392 if ( (s = accept( tcps,
393 (struct sockaddr *) &from, &len )) == -1 )
396 Debug( LDAP_DEBUG_ANY,
397 "daemon: accept(%d) failed errno %d (%s)\n", err,
398 tcps, err >= 0 && err < sys_nerr ?
399 sys_errlist[err] : "unknown");
404 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
406 /* newly accepted stream should not be in any of the FD SETS */
408 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
409 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
410 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
412 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
416 /* make sure descriptor number isn't too great */
417 if ( s >= dtblsize ) {
418 Debug( LDAP_DEBUG_ANY,
419 "daemon: %d beyond descriptor table size %d\n",
426 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %d\n",
430 if ( getpeername( s, (struct sockaddr *) &from, &len ) == 0 ) {
431 client_addr = inet_ntoa( from.sin_addr );
433 #if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
434 hp = gethostbyaddr( (char *)
435 &(from.sin_addr.s_addr),
436 sizeof(from.sin_addr.s_addr), AF_INET );
440 client_name = hp->h_name;
442 /* normalize the domain */
443 for ( p = client_name; *p; p++ ) {
444 *p = TOLOWER( (unsigned char) *p );
460 if(!hosts_ctl("slapd",
461 client_name != NULL ? client_name : STRING_UNKNOWN,
462 client_addr != NULL ? client_addr : STRING_UNKNOWN,
466 Statslog( LDAP_DEBUG_ANY,
467 "fd=%d connection from %s (%s) denied.\n",
469 client_name == NULL ? "unknown" : client_name,
470 client_addr == NULL ? "unknown" : client_addr,
476 #endif /* HAVE_TCPD */
479 if( (id = connection_init(s, client_name, client_addr)) < 0 ) {
480 Debug( LDAP_DEBUG_ANY,
481 "daemon: connection_init(%d, %s, %s) failed.\n",
483 client_name == NULL ? "unknown" : client_name,
484 client_addr == NULL ? "unknown" : client_addr);
489 Statslog( LDAP_DEBUG_STATS,
490 "daemon: conn=%d fd=%d connection from %s (%s) accepted.\n",
492 client_name == NULL ? "unknown" : client_name,
493 client_addr == NULL ? "unknown" : client_addr,
501 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
503 for ( i = 0; i < readfds.fd_count; i++ ) {
504 Debug( LDAP_DEBUG_CONNS, " %d%s", readfds.fd_array[i], "r" );
506 for ( i = 0; i < writefds.fd_count; i++ ) {
507 Debug( LDAP_DEBUG_CONNS, " %d%s", writefds.fd_array[i], "w" );
510 for ( i = 0; i < nfds; i++ ) {
513 r = FD_ISSET( i, &readfds );
514 w = FD_ISSET( i, &writefds );
515 if ( i != tcps && (r || w) ) {
516 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
517 r ? "r" : "", w ? "w" : "" );
521 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
524 /* loop through the writers */
526 for ( i = 0; i < writefds.fd_count; i++ ) {
527 int wd = writefds.fd_array[i];
533 Debug( LDAP_DEBUG_CONNS,
534 "daemon: signalling write waiter on %d\n",
537 slapd_clr_write( wd, 0 );
538 if ( connection_write( wd ) < 0 ) {
539 FD_CLR( (unsigned) wd, &readfds );
544 for ( i = 0; i < nfds; i++ ) {
548 if ( FD_ISSET( i, &writefds ) ) {
549 Debug( LDAP_DEBUG_CONNS,
550 "daemon: signaling write waiter on %d\n", i, 0, 0 );
552 /* clear the write flag */
553 slapd_clr_write( i, 0 );
555 if( connection_write( i ) < 0 ) {
556 FD_CLR( i, &readfds );
564 for ( i = 0; i < readfds.fd_count; i++ ) {
565 int rd = readfds.fd_array[i];
569 Debug ( LDAP_DEBUG_CONNS,
570 "daemon: read activity on %d\n", rd, 0, 0 );
572 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
573 assert( FD_ISSET( rd, &slap_daemon.sd_actives) );
574 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
576 if ( connection_read( rd ) < 0 ) {
581 for ( i = 0; i < nfds; i++ ) {
586 if ( FD_ISSET( i, &readfds ) ) {
587 Debug( LDAP_DEBUG_CONNS,
588 "daemon: read activity on %d\n", i, 0, 0 );
590 assert( FD_ISSET( i, &slap_daemon.sd_actives) );
592 if( connection_read( i ) < 0) {
598 ldap_pvt_thread_yield();
601 if( slapd_shutdown > 0 ) {
602 Debug( LDAP_DEBUG_TRACE,
603 "daemon: shutdown requested (%d) and initiated.\n",
604 (int) slapd_shutdown, 0, 0 );
606 } else if ( slapd_shutdown < 0 ) {
607 Debug( LDAP_DEBUG_TRACE,
608 "daemon: abnormal condition (%d), shutdown initiated.\n",
609 (int) slapd_shutdown, 0, 0 );
611 Debug( LDAP_DEBUG_TRACE,
612 "daemon: no active streams, shutdown initiated.\n",
620 /* we only implement "quick" shutdown */
621 connections_shutdown();
623 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
624 Debug( LDAP_DEBUG_ANY,
625 "slapd shutdown: waiting for %d threads to terminate\n",
626 active_threads, 0, 0 );
627 while ( active_threads > 0 ) {
628 ldap_pvt_thread_cond_wait(&active_threads_cond, &active_threads_mutex);
630 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
636 int slapd_daemon( int inetd, int tcps )
639 int *args = ch_malloc( sizeof( int[2] ) );
643 if ( !daemon_initialized ) sockinit();
647 #define SLAPD_LISTENER_THREAD 1
648 #if defined( SLAPD_LISTENER_THREAD ) || !defined(HAVE_PTHREADS)
650 /* listener as a separate THREAD */
651 rc = ldap_pvt_thread_create( &listener_tid,
652 0, slapd_daemon_task, args );
655 Debug( LDAP_DEBUG_ANY,
656 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
660 /* wait for the listener thread to complete */
661 ldap_pvt_thread_join( listener_tid, (void *) NULL );
663 /* expermimental code */
664 listener_tid = pthread_self();
665 slapd_daemon_task( args );
671 connections_destroy();
683 WORD wVersionRequested;
687 wVersionRequested = MAKEWORD( 2, 0 );
689 err = WSAStartup( wVersionRequested, &wsaData );
691 /* Tell the user that we couldn't find a usable */
696 /* Confirm that the WinSock DLL supports 2.0.*/
697 /* Note that if the DLL supports versions greater */
698 /* than 2.0 in addition to 2.0, it will still return */
699 /* 2.0 in wVersion since that is the version we */
702 if ( LOBYTE( wsaData.wVersion ) != 2 ||
703 HIBYTE( wsaData.wVersion ) != 0 )
705 /* Tell the user that we couldn't find a usable */
710 daemon_initialized = 1;
711 } /* The WinSock DLL is acceptable. Proceed. */
713 void hit_socket( void )
716 extern struct sockaddr_in bind_addr;
718 /* throw something at the socket to terminate the select() in the daemon thread. */
719 if (( s = socket( AF_INET, SOCK_STREAM, 0 )) == INVALID_SOCKET )
720 Debug( LDAP_DEBUG_TRACE,
721 "slap_set_shutdown: socket failed\n\tWSAGetLastError=%d (%s)\n",
722 WSAGetLastError(), WSAGetLastErrorString(), 0 );
724 if ( ioctlsocket( s, FIONBIO, &on ) == -1 )
725 Debug( LDAP_DEBUG_TRACE,
726 "slap_set_shutdown:FIONBIO ioctl on %d faled\n\tWSAGetLastError=%d (%s)\n",
727 s, WSAGetLastError(), WSAGetLastError() );
729 bind_addr.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
731 if ( connect( s, (struct sockaddr *)&bind_addr, sizeof( struct sockaddr_in )) == SOCKET_ERROR ) {
732 /* we can probably expect some error to occur here, mostly WSAEWOULDBLOCK */
741 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
744 daemon_initialized = 1;
749 daemon_initialized = 1;
755 slap_set_shutdown( int sig )
757 slapd_shutdown = sig;
760 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
766 (void) SIGNAL( sig, slap_set_shutdown );
770 slap_do_nothing( int sig )
773 (void) SIGNAL( sig, slap_do_nothing );