11 #include <ac/unistd.h>
13 #include "ldap_defaults.h"
19 int allow_severity = LOG_INFO;
20 int deny_severity = LOG_NOTICE;
21 #endif /* TCP Wrappers */
24 ber_socket_t dtblsize;
25 static ber_socket_t tcps;
29 extern ldap_pvt_thread_cond_t started_event;
31 /* forward reference */
34 char *WSAGetLastErrorString();
35 static ldap_pvt_thread_t hit_tid;
37 #define WAKE_LISTENER(w) \
40 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );\
45 #define WAKE_LISTENER(w) \
48 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );\
56 volatile sig_atomic_t slapd_shutdown = 0;
58 static int daemon_initialized = 0;
59 static ldap_pvt_thread_t listener_tid;
60 static volatile sig_atomic_t slapd_listener = 0;
64 ldap_pvt_thread_mutex_t sd_mutex;
69 /* In winsock, accept() returns values higher than dtblsize
70 so don't bother with this optimization */
80 * Add a descriptor to daemon control
82 static void slapd_add(ber_socket_t s) {
83 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
85 assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
86 assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
87 assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
90 if (s >= slap_daemon.sd_nfds) {
91 slap_daemon.sd_nfds = s + 1;
95 FD_SET( s, &slap_daemon.sd_actives );
96 FD_SET( s, &slap_daemon.sd_readers );
98 Debug( LDAP_DEBUG_CONNS, "daemon: added %ld%s%s\n",
100 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
101 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
103 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
107 * Remove the descriptor from daemon control
109 void slapd_remove(ber_socket_t s, int wake) {
110 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
113 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
115 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
116 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
118 FD_CLR( s, &slap_daemon.sd_actives );
119 FD_CLR( s, &slap_daemon.sd_readers );
120 FD_CLR( s, &slap_daemon.sd_writers );
122 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
125 void slapd_clr_write(ber_socket_t s, int wake) {
126 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
129 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
130 FD_CLR( s, &slap_daemon.sd_writers );
132 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
135 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
139 void slapd_set_write(ber_socket_t s, int wake) {
140 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
143 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
144 FD_SET( (unsigned) s, &slap_daemon.sd_writers );
146 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
149 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
153 void slapd_clr_read(ber_socket_t s, int wake) {
154 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
157 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
158 FD_CLR( s, &slap_daemon.sd_readers );
160 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
163 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
167 void slapd_set_read(ber_socket_t s, int wake) {
168 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
171 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
172 FD_SET( s, &slap_daemon.sd_readers );
174 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
177 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
181 static void slapd_close(ber_socket_t s) {
182 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
190 set_socket( struct sockaddr_in *addr )
192 ber_socket_t tcps = AC_SOCKET_INVALID;
194 if ( !daemon_initialized ) sockinit();
197 dtblsize = sysconf( _SC_OPEN_MAX );
198 #elif HAVE_GETDTABLESIZE
199 dtblsize = getdtablesize();
201 dtblsize = FD_SETSIZE;
205 if(dtblsize > FD_SETSIZE) {
206 dtblsize = FD_SETSIZE;
208 #endif /* !FD_SETSIZE */
213 if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == AC_SOCKET_INVALID ) {
216 Debug( LDAP_DEBUG_ANY,
217 "daemon: socket() failed errno %d (%s)\n", err,
218 err > -1 && err < sys_nerr ? sys_errlist[err] :
221 Debug( LDAP_DEBUG_ANY,
222 "daemon: socket() failed errno %d (%s)\n",
224 WSAGetLastErrorString(), 0 );
230 if ( tcps >= dtblsize ) {
231 Debug( LDAP_DEBUG_ANY,
232 "daemon: listener descriptor %ld is too great %ld\n",
233 (long) tcps, (long) dtblsize, 0 );
240 if ( setsockopt( tcps, SOL_SOCKET, SO_REUSEADDR,
241 (char *) &tmp, sizeof(tmp) ) == -1 )
244 Debug( LDAP_DEBUG_ANY,
245 "slapd(%ld): setsockopt() failed errno %d (%s)\n",
247 err > -1 && err < sys_nerr
248 ? sys_errlist[err] : "unknown" );
253 if ( setsockopt( tcps, SOL_SOCKET, SO_KEEPALIVE,
254 (char *) &tmp, sizeof(tmp) ) == -1 )
257 Debug( LDAP_DEBUG_ANY,
258 "slapd(%ld): setsockopt(KEEPALIVE) failed errno %d (%s)\n",
260 err > -1 && err < sys_nerr
261 ? sys_errlist[err] : "unknown" );
266 if ( bind( tcps, (struct sockaddr *) addr, sizeof(*addr) ) == -1 ) {
268 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno %d (%s)\n",
270 err > -1 && err < sys_nerr
271 ? sys_errlist[err] : "unknown" );
285 struct slapd_args *args = (struct slapd_args *) ptr;
286 struct sockaddr_in *slapd_addr = args->addr;
290 inetd = ( slapd_addr == NULL);
291 if ( !daemon_initialized ) sockinit();
295 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
296 FD_ZERO( &slap_daemon.sd_readers );
297 FD_ZERO( &slap_daemon.sd_writers );
300 if ( listen( tcps, 5 ) == -1 ) {
302 Debug( LDAP_DEBUG_ANY,
303 "daemon: listen(%ld, 5) failed errno %d (%s)\n",
305 err > -1 && err < sys_nerr
306 ? sys_errlist[err] : "unknown" );
313 if( connection_init( (ber_socket_t) 0, NULL, NULL ) ) {
314 Debug( LDAP_DEBUG_ANY,
315 "connection_init(%d) failed.\n",
324 if ( started_event != NULL )
325 ldap_pvt_thread_cond_signal( &started_event );
327 /* initialization complete. Here comes the loop. */
329 while ( !slapd_shutdown ) {
334 #define SLAPD_EBADF_LIMIT 10
337 #define SLAPD_IDLE_CHECK_LIMIT 4
338 time_t last_idle_check = slap_get_time();
345 struct sockaddr_in from;
346 #if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
355 if( global_idletimeout > 0 && difftime(
356 last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT,
359 connections_timeout_idle(now);
362 FD_ZERO( &writefds );
368 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
370 #ifdef FD_SET_MANUAL_COPY
371 for( s = 0; s < nfds; s++ ) {
372 if(FD_ISSET( &slap_sd_writers, s )) {
373 FD_SET( &writefds, s );
375 if(FD_ISSET( &slap_sd_writers, s )) {
376 FD_SET( &writefds, s );
380 memcpy( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
381 memcpy( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
384 FD_SET( (unsigned) tcps, &readfds );
387 nfds = slap_daemon.sd_nfds;
392 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
394 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
396 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
398 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
401 tvp = at ? &zero : NULL;
404 Debug( LDAP_DEBUG_CONNS,
405 "daemon: select: tcps=%d active_threads=%d tvp=%s\n",
407 tvp == NULL ? "NULL" : "zero" );
411 switch(ns = select( nfds, &readfds,
413 /* don't pass empty fd_set */
414 ( writefds.fd_count > 0 ? &writefds : NULL ),
420 case -1: { /* failure - try again */
422 int err = WSAGetLastError();
427 if( err == EBADF && ++ebadf < SLAPD_EBADF_LIMIT) {
432 Debug( LDAP_DEBUG_CONNS,
433 "daemon: select failed (%d): %s\n",
435 err >= 0 && err < sys_nerr
436 ? sys_errlist[err] : "unknown",
445 case 0: /* timeout - let threads run */
447 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
449 ldap_pvt_thread_yield();
452 default: /* something happened - deal with it */
454 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
459 if ( FD_ISSET( tcps, &readfds ) ) {
461 int len = sizeof(from);
464 if ( (s = accept( tcps,
465 (struct sockaddr *) &from, &len )) == AC_SOCKET_INVALID )
468 Debug( LDAP_DEBUG_ANY,
469 "daemon: accept(%ld) failed errno %d (%s)\n", err,
470 (long) tcps, err >= 0 && err < sys_nerr ?
471 sys_errlist[err] : "unknown");
476 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
478 /* newly accepted stream should not be in any of the FD SETS */
480 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
481 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
482 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
484 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
488 /* make sure descriptor number isn't too great */
489 if ( s >= dtblsize ) {
490 Debug( LDAP_DEBUG_ANY,
491 "daemon: %ld beyond descriptor table size %ld\n",
492 (long) s, (long) dtblsize, 0 );
498 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
502 if ( getpeername( s, (struct sockaddr *) &from, &len ) == 0 ) {
503 client_addr = inet_ntoa( from.sin_addr );
505 #if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
506 hp = gethostbyaddr( (char *)
507 &(from.sin_addr.s_addr),
508 sizeof(from.sin_addr.s_addr), AF_INET );
512 client_name = hp->h_name;
514 /* normalize the domain */
515 for ( p = client_name; *p; p++ ) {
516 *p = TOLOWER( (unsigned char) *p );
532 if(!hosts_ctl("slapd",
533 client_name != NULL ? client_name : STRING_UNKNOWN,
534 client_addr != NULL ? client_addr : STRING_UNKNOWN,
538 Statslog( LDAP_DEBUG_ANY,
539 "fd=%ld connection from %s (%s) denied.\n",
541 client_name == NULL ? "unknown" : client_name,
542 client_addr == NULL ? "unknown" : client_addr,
548 #endif /* HAVE_TCPD */
550 if( (id = connection_init(s, client_name, client_addr)) < 0 ) {
551 Debug( LDAP_DEBUG_ANY,
552 "daemon: connection_init(%ld, %s, %s) failed.\n",
554 client_name == NULL ? "unknown" : client_name,
555 client_addr == NULL ? "unknown" : client_addr);
560 Statslog( LDAP_DEBUG_STATS,
561 "daemon: conn=%d fd=%ld connection from %s (%s) accepted.\n",
563 client_name == NULL ? "unknown" : client_name,
564 client_addr == NULL ? "unknown" : client_addr,
572 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
574 for ( i = 0; i < readfds.fd_count; i++ ) {
575 Debug( LDAP_DEBUG_CONNS, " %d%s",
576 readfds.fd_array[i], "r", 0 );
578 for ( i = 0; i < writefds.fd_count; i++ ) {
579 Debug( LDAP_DEBUG_CONNS, " %d%s",
580 writefds.fd_array[i], "w", 0 );
583 for ( i = 0; i < nfds; i++ ) {
586 r = FD_ISSET( i, &readfds );
587 w = FD_ISSET( i, &writefds );
588 if ( i != tcps && (r || w) ) {
589 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
590 r ? "r" : "", w ? "w" : "" );
594 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
597 /* loop through the writers */
599 for ( i = 0; i < writefds.fd_count; i++ )
601 for ( i = 0; i < nfds; i++ )
607 wd = writefds.fd_array[i];
609 if( ! FD_ISSET( i, &writefds ) ) {
618 Debug( LDAP_DEBUG_CONNS,
619 "daemon: write active on %d\n",
623 * NOTE: it is possible that the connection was closed
624 * and that the stream is now inactive.
625 * connection_write() must valid the stream is still
629 if ( connection_write( wd ) < 0 ) {
630 FD_CLR( (unsigned) wd, &readfds );
636 for ( i = 0; i < readfds.fd_count; i++ )
638 for ( i = 0; i < nfds; i++ )
644 rd = readfds.fd_array[i];
646 if( ! FD_ISSET( i, &readfds ) ) {
656 Debug ( LDAP_DEBUG_CONNS,
657 "daemon: read activity on %d\n", rd, 0, 0 );
660 * NOTE: it is possible that the connection was closed
661 * and that the stream is now inactive.
662 * connection_read() must valid the stream is still
666 if ( connection_read( rd ) < 0 ) {
670 ldap_pvt_thread_yield();
673 if( slapd_shutdown > 0 ) {
674 Debug( LDAP_DEBUG_TRACE,
675 "daemon: shutdown requested and initiated.\n",
678 } else if ( slapd_shutdown < 0 ) {
679 Debug( LDAP_DEBUG_TRACE,
680 "daemon: abnormal condition, shutdown initiated.\n",
683 Debug( LDAP_DEBUG_TRACE,
684 "daemon: no active streams, shutdown initiated.\n",
692 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
693 Debug( LDAP_DEBUG_ANY,
694 "slapd shutdown: waiting for %d threads to terminate\n",
695 active_threads, 0, 0 );
696 while ( active_threads > 0 ) {
697 ldap_pvt_thread_cond_wait(&active_threads_cond, &active_threads_mutex);
699 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
705 int slapd_daemon( struct slapd_args *args )
709 if ( !daemon_initialized ) sockinit();
713 #define SLAPD_LISTENER_THREAD 1
714 #if defined( SLAPD_LISTENER_THREAD ) || !defined(HAVE_PTHREADS)
716 /* listener as a separate THREAD */
717 rc = ldap_pvt_thread_create( &listener_tid,
718 0, slapd_daemon_task, args );
721 Debug( LDAP_DEBUG_ANY,
722 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
726 /* wait for the listener thread to complete */
727 ldap_pvt_thread_join( listener_tid, (void *) NULL );
729 /* expermimental code */
730 listener_tid = pthread_self();
731 slapd_daemon_task( args );
737 connections_destroy();
749 WORD wVersionRequested;
753 wVersionRequested = MAKEWORD( 2, 0 );
755 err = WSAStartup( wVersionRequested, &wsaData );
757 /* Tell the user that we couldn't find a usable */
762 /* Confirm that the WinSock DLL supports 2.0.*/
763 /* Note that if the DLL supports versions greater */
764 /* than 2.0 in addition to 2.0, it will still return */
765 /* 2.0 in wVersion since that is the version we */
768 if ( LOBYTE( wsaData.wVersion ) != 2 ||
769 HIBYTE( wsaData.wVersion ) != 0 )
771 /* Tell the user that we couldn't find a usable */
776 daemon_initialized = 1;
777 } /* The WinSock DLL is acceptable. Proceed. */
783 extern struct sockaddr_in bind_addr;
785 /* throw something at the socket to terminate the select() in the daemon thread. */
786 if (( s = socket( AF_INET, SOCK_STREAM, 0 )) == AC_SOCKET_INVALID )
787 Debug( LDAP_DEBUG_ANY,
788 "slap_set_shutdown: socket failed\n\tWSAGetLastError=%d (%s)\n",
789 WSAGetLastError(), WSAGetLastErrorString(), 0 );
791 if ( ioctlsocket( s, FIONBIO, &on ) == -1 )
792 Debug( LDAP_DEBUG_ANY,
793 "slap_set_shutdown:FIONBIO ioctl on %d faled\n\tWSAGetLastError=%d (%s)\n",
794 s, WSAGetLastError(), WSAGetLastError() );
796 bind_addr.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
798 if ( connect( s, (struct sockaddr *)&bind_addr, sizeof( struct sockaddr_in )) == SOCKET_ERROR ) {
799 Debug( LDAP_DEBUG_ANY,
800 "hit_socket: error on connect: %d\n",
801 WSAGetLastError(), 0, 0 );
802 /* we can probably expect some error to occur here, mostly WSAEWOULDBLOCK */
811 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
814 daemon_initialized = 1;
819 daemon_initialized = 1;
824 slap_set_shutdown( int sig )
826 slapd_shutdown = sig;
829 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
832 Debug( LDAP_DEBUG_TRACE, "Shutdown %d ordered", sig, 0, 0 );
833 /* trying to "hit" the socket seems to always get a */
834 /* EWOULDBLOCK error, so just close the listen socket to */
835 /* break out of the select since we're shutting down anyway */
839 (void) SIGNAL( sig, slap_set_shutdown );
843 slap_do_nothing( int sig )
846 (void) SIGNAL( sig, slap_do_nothing );