3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/signal.h>
14 #include <ac/socket.h>
15 #include <ac/string.h>
17 #include <ac/unistd.h>
26 int allow_severity = LOG_INFO;
27 int deny_severity = LOG_NOTICE;
28 #endif /* TCP Wrappers */
32 #endif /* LDAP_PF_LOCAL */
36 ber_socket_t dtblsize;
38 typedef union slap_sockaddr {
39 struct sockaddr sa_addr;
40 struct sockaddr_in sa_in_addr;
42 struct sockaddr_in6 sa_in6_addr;
45 struct sockaddr_un sa_un_addr;
49 typedef struct slap_listener {
57 #define sl_addr sl_sa.sa_in_addr
60 Listener **slap_listeners = NULL;
62 #define SLAPD_LISTEN 10
64 static ber_socket_t wake_sds[2];
68 #define WAKE_LISTENER(w) \
69 ((w && !waking) ? tcp_write( wake_sds[1], "0", 1 ), waking=1 : 0)
71 #define WAKE_LISTENER(w) \
72 do { if (w) tcp_write( wake_sds[1], "0", 1 ); } while(0)
75 #ifdef HAVE_NT_SERVICE_MANAGER
77 extern ldap_pvt_thread_cond_t started_event;
78 extern int is_NT_Service;
84 volatile sig_atomic_t slapd_shutdown = 0;
86 static struct slap_daemon {
87 ldap_pvt_thread_mutex_t sd_mutex;
92 /* In winsock, accept() returns values higher than dtblsize
93 so don't bother with this optimization */
106 * SLP related functions
110 #define MAX_HOSTNAME_LEN 256
111 #define LDAP_SRVTYPE_PREFIX "service:ldap://"
112 static char** slapd_srvurls = 0;
113 static SLPHandle slapd_hslp = 0;
115 void slapd_slp_init( const char* urls ) {
118 char hn[MAX_HOSTNAME_LEN];
120 slapd_srvurls = str2charray( urls, " " );
122 for( i=0; slapd_srvurls[i]!=NULL; i++ ) {
123 if( strcmp( slapd_srvurls[i], "ldap:///" ) == 0) {
124 /* INADDR_ANY urls should be marked up with host.domainname */
125 if ( gethostname( hn, MAX_HOSTNAME_LEN ) == 0) {
126 he = gethostbyname( hn );
128 slapd_srvurls[i] = (char *) realloc( slapd_srvurls[i],
129 strlen( he->h_name ) +
130 strlen( LDAP_SRVTYPE_PREFIX ) + 1);
131 strcpy( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX );
132 strcat( slapd_srvurls[i], he->h_name );
138 /* open the SLP handle */
139 SLPOpen("en", 0, &slapd_hslp);
142 void slapd_slp_deinit() {
143 if ( slapd_srvurls ) {
144 charray_free( slapd_srvurls );
147 /* close the SLP handle */
148 SLPClose( slapd_hslp );
151 void slapd_slp_regreport(
159 void slapd_slp_reg() {
162 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
165 SLP_LIFETIME_MAXIMUM,
174 void slapd_slp_dereg() {
177 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
178 SLPDereg( slapd_hslp,
184 #endif /* HAVE_SLP */
187 * Add a descriptor to daemon control
189 static void slapd_add(ber_socket_t s) {
190 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
192 assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
193 assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
194 assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
197 if (s >= slap_daemon.sd_nfds) {
198 slap_daemon.sd_nfds = s + 1;
202 FD_SET( s, &slap_daemon.sd_actives );
203 FD_SET( s, &slap_daemon.sd_readers );
206 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
207 "slapd_add: added %ld%s%s\n",
209 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
210 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" ));
212 Debug( LDAP_DEBUG_CONNS, "daemon: added %ld%s%s\n",
214 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
215 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
217 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
221 * Remove the descriptor from daemon control
223 void slapd_remove(ber_socket_t s, int wake) {
224 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
227 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
228 "slapd_remove: removing %ld%s%s\n",
230 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
231 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" ));
233 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
235 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
236 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
238 FD_CLR( s, &slap_daemon.sd_actives );
239 FD_CLR( s, &slap_daemon.sd_readers );
240 FD_CLR( s, &slap_daemon.sd_writers );
242 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
246 void slapd_clr_write(ber_socket_t s, int wake) {
247 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
249 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
250 FD_CLR( s, &slap_daemon.sd_writers );
252 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
256 void slapd_set_write(ber_socket_t s, int wake) {
257 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
259 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
260 if (!FD_ISSET(s, &slap_daemon.sd_writers))
261 FD_SET( (unsigned) s, &slap_daemon.sd_writers );
263 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
267 void slapd_clr_read(ber_socket_t s, int wake) {
268 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
270 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
271 FD_CLR( s, &slap_daemon.sd_readers );
273 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
277 void slapd_set_read(ber_socket_t s, int wake) {
278 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
280 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
281 if (!FD_ISSET(s, &slap_daemon.sd_readers))
282 FD_SET( s, &slap_daemon.sd_readers );
284 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
288 static void slapd_close(ber_socket_t s) {
290 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
291 "slapd_close: closing %ld\n", (long)s ));
293 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
299 static void slap_free_listener_addresses(struct sockaddr **sal)
301 struct sockaddr **sap;
307 for (sap = sal; *sap != NULL; sap++) {
314 /* port = 0 indicates AF_LOCAL */
315 static int slap_get_listener_addresses(
318 struct sockaddr ***sal)
320 struct sockaddr **sap;
321 #ifdef HAVE_GETADDRINFO
322 struct addrinfo hints, *res, *sai;
325 memset( &hints, '\0', sizeof(hints) );
326 hints.ai_flags = AI_PASSIVE;
327 hints.ai_socktype = SOCK_STREAM;
328 # ifdef LDAP_PF_LOCAL
330 hints.ai_family = AF_LOCAL;
331 /* host specifies a service in this case */
332 if (err = getaddrinfo(NULL, host, &hints, &res)) {
334 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
335 "slap_get_listener_addresses: getaddrinfo failed: %s\n",
336 AC_GAI_STRERROR(err) ));
338 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
339 AC_GAI_STRERROR(err), 0, 0);
348 snprintf(serv, sizeof serv, "%d", port);
349 hints.ai_family = AF_UNSPEC;
350 if (err = getaddrinfo(host, serv, &hints, &res)) {
352 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
353 "slap_get_listener_addresses: getaddrinfo failed: %s\n",
354 AC_GAI_STRERROR(err) ));
356 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
357 AC_GAI_STRERROR(err), 0, 0);
364 for (n=2; (sai = sai->ai_next) != NULL; n++) {
367 *sal = ch_malloc(n * sizeof(*sal));
375 switch (sai->ai_family) {
376 # ifdef LDAP_PF_LOCAL
378 *sap = ch_malloc(sizeof(struct sockaddr_un));
383 *(struct sockaddr_un *)*sap =
384 *((struct sockaddr_un *)sai->ai_addr);
387 # ifdef LDAP_PF_INET6
389 *sap = ch_malloc(sizeof(struct sockaddr_in6));
394 *(struct sockaddr_in6 *)*sap =
395 *((struct sockaddr_in6 *)sai->ai_addr);
399 *sap = ch_malloc(sizeof(struct sockaddr_in));
404 *(struct sockaddr_in *)*sap =
405 *((struct sockaddr_in *)sai->ai_addr);
412 (*sap)->sa_family = sai->ai_family;
415 } while ((sai = sai->ai_next) != NULL);
420 # ifdef LDAP_PF_LOCAL
422 *sal = ch_malloc(2 * sizeof(*sal));
427 *sap = ch_malloc(sizeof(struct sockaddr_un));
430 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
431 (*sap)->sa_family = AF_LOCAL;
433 (sizeof(((struct sockaddr_un *)*sal)->sun_path) - 1) ) {
435 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
436 "slap_get_listener_addresses: domain socket path (%s) too long in URL\n",
439 Debug( LDAP_DEBUG_ANY,
440 "daemon: domain socket path (%s) too long in URL",
445 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
451 if ( host == NULL ) {
452 in.s_addr = htonl(INADDR_ANY);
454 } else if ( !inet_aton( host, &in ) ) {
455 struct hostent *he = gethostbyname( host );
458 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
459 "slap_get_listener_addresses: invalid host %s\n",
462 Debug( LDAP_DEBUG_ANY,
463 "daemon: invalid host %s", host, 0, 0);
467 AC_MEMCPY( &in, he->h_addr, sizeof( in ) );
470 *sal = ch_malloc(2 * sizeof(*sal));
476 *sap = ch_malloc(sizeof(struct sockaddr_in));
481 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_in) );
482 (*sap)->sa_family = AF_INET;
483 ((struct sockaddr_in *)*sap)->sin_port = htons(port);
484 ((struct sockaddr_in *)*sap)->sin_addr = in;
493 slap_free_listener_addresses(*sal);
497 static Listener * slap_open_listener(
506 struct sockaddr **sal, **psal;
508 rc = ldap_url_parse( url, &lud );
510 if( rc != LDAP_URL_SUCCESS ) {
512 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
513 "slap_open_listener: listen URL \"%s\" parse error %d\n",
516 Debug( LDAP_DEBUG_ANY,
517 "daemon: listen URL \"%s\" parse error=%d\n",
524 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
526 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
527 "slap_open_listener: TLS is not supported (%s)\n",
530 Debug( LDAP_DEBUG_ANY,
531 "daemon: TLS not supported (%s)\n",
534 ldap_free_urldesc( lud );
538 if(! lud->lud_port ) {
539 lud->lud_port = LDAP_PORT;
543 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
545 if(! lud->lud_port ) {
546 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
550 port = (unsigned short) lud->lud_port;
552 if ( ldap_pvt_url_scheme2proto(lud->lud_scheme) == LDAP_PROTO_IPC ) {
554 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
555 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
557 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
562 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
563 "slap_open_listener: URL scheme is not supported: %s\n",
566 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
569 ldap_free_urldesc( lud );
573 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
574 || strcmp(lud->lud_host, "*") == 0 )
576 err = slap_get_listener_addresses(NULL, port, &sal);
578 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
582 ldap_free_urldesc( lud );
588 while ( *sal != NULL ) {
589 switch( (*sal)->sa_family ) {
602 l.sl_sd = socket( (*sal)->sa_family, SOCK_STREAM, 0);
603 if ( l.sl_sd == AC_SOCKET_INVALID ) {
604 int err = sock_errno();
606 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
607 "slap_open_listener: socket() failed errno=%d (%s)\n",
608 err, sock_errstr(err) ));
610 Debug( LDAP_DEBUG_ANY,
611 "daemon: socket() failed errno=%d (%s)\n", err,
612 sock_errstr(err), 0 );
618 if ( l.sl_sd >= dtblsize ) {
620 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
621 "slap_open_listener: listener descriptor %ld is too great %ld\n",
622 (long)l.sl_sd, (long)dtblsize ));
624 Debug( LDAP_DEBUG_ANY,
625 "daemon: listener descriptor %ld is too great %ld\n",
626 (long) l.sl_sd, (long) dtblsize, 0 );
628 tcp_close( l.sl_sd );
634 if ( (*sal)->sa_family == AF_LOCAL ) {
635 unlink ( ((struct sockaddr_un *)*sal)->sun_path );
640 /* enable address reuse */
642 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
643 (char *) &tmp, sizeof(tmp) );
644 if ( rc == AC_SOCKET_ERROR ) {
645 int err = sock_errno();
647 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
648 "slap_open_listener: setsockopt( %ld, SO_REUSEADDR ) failed errno %d (%s)\n",
649 (long)l.sl_sd, err, sock_errstr(err) ));
651 Debug( LDAP_DEBUG_ANY,
652 "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
653 (long) l.sl_sd, err, sock_errstr(err) );
659 switch( (*sal)->sa_family ) {
661 addrlen = sizeof(struct sockaddr_in);
665 addrlen = sizeof(struct sockaddr_in6);
670 addrlen = sizeof(struct sockaddr_un);
675 if (!bind(l.sl_sd, *sal, addrlen))
679 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
680 "slap_open_listener: bind(%ld) failed errno=%d (%s)\n",
681 (long)l.sl_sd, err, sock_errstr(err) ));
683 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
684 (long) l.sl_sd, err, sock_errstr(err) );
686 tcp_close( l.sl_sd );
688 } /* while ( *sal != NULL ) */
690 if ( *sal == NULL ) {
692 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
693 "slap_open_listener: bind(%ld) failed.\n", (long)l.sl_sd ));
695 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed\n",
696 (long) l.sl_sd, 0, 0 );
698 slap_free_listener_addresses(psal);
702 switch ( (*sal)->sa_family ) {
705 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
706 if ( chmod( addr, S_IRWXU ) < 0 ) {
707 int err = sock_errno();
709 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
710 "slap_open_listener: fchmod(%ld) failed errno=%d (%s)\n",
711 (long)l.sl_sd, err, sock_errstr(err) ));
713 Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
714 (long) l.sl_sd, err, sock_errstr(err) );
716 tcp_close( l.sl_sd );
717 slap_free_listener_addresses(psal);
720 l.sl_name = ch_malloc( strlen(addr) + sizeof("PATH=") );
721 sprintf( l.sl_name, "PATH=%s", addr );
723 #endif /* LDAP_PF_LOCAL */
727 #ifdef HAVE_GETADDRINFO
728 char addr[INET_ADDRSTRLEN];
729 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
730 addr, sizeof(addr) );
733 s = inet_ntoa( l.sl_addr.sin_addr );
735 l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65535") );
736 sprintf( l.sl_name, "IP=%s:%d",
737 s != NULL ? s : "unknown" , port );
742 char addr[INET6_ADDRSTRLEN];
743 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
745 l.sl_name = ch_malloc( strlen(addr) + sizeof("IP= 65535") );
746 sprintf( l.sl_name, "IP=%s %d", addr, port );
748 #endif /* LDAP_PF_INET6 */
752 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
753 "slap_open_listener: unsupported address family (%d)\n",
754 (int)(*sal)->sa_family ));
756 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
757 (int) (*sal)->sa_family, 0, 0 );
762 slap_free_listener_addresses(psal);
764 l.sl_url = ch_strdup( url );
765 li = ch_malloc( sizeof( Listener ) );
769 LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
770 "slap_open_listener: daemon initialzed %s\n", l.sl_url ));
772 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
778 static int sockinit(void);
779 static int sockdestroy(void);
781 int slapd_daemon_init( const char *urls )
787 LDAP_LOG(( "connection", LDAP_LEVEL_ARGS,
788 "slapd_daemon_init: %s\n",
789 urls ? urls : "<null>" ));
791 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
792 urls ? urls : "<null>", 0, 0 );
794 if( (rc = sockinit()) != 0 ) {
799 dtblsize = sysconf( _SC_OPEN_MAX );
800 #elif HAVE_GETDTABLESIZE
801 dtblsize = getdtablesize();
803 dtblsize = FD_SETSIZE;
807 if(dtblsize > FD_SETSIZE) {
808 dtblsize = FD_SETSIZE;
810 #endif /* !FD_SETSIZE */
812 /* open a pipe (or something equivalent connected to itself).
813 * we write a byte on this fd whenever we catch a signal. The main
814 * loop will be select'ing on this socket, and will wake up when
817 if( (rc = lutil_pair( wake_sds )) < 0 ) {
819 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
820 "slap_daemon_init: lutil_pair() failed rc=%d\n", rc ));
822 Debug( LDAP_DEBUG_ANY,
823 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
828 FD_ZERO( &slap_daemon.sd_readers );
829 FD_ZERO( &slap_daemon.sd_writers );
835 u = str2charray( urls, " " );
837 if( u == NULL || u[0] == NULL ) {
839 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
840 "slap_daemon_init: no urls (%s) provided.\n", urls ));
842 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
848 for( i=0; u[i] != NULL; i++ ) {
850 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
851 "slap_daemon_init: listen on %s\n.", u[i] ));
853 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
860 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
861 "slap_daemon_init: no listeners to open (%s)\n", urls ));
863 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
871 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
872 "slap_daemon_init: %d listeners to open...\n", i ));
874 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
877 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
879 for(i = 0; u[i] != NULL; i++ ) {
880 slap_listeners[i] = slap_open_listener( u[i] );
882 if( slap_listeners[i] == NULL ) {
887 slap_listeners[i] = NULL;
890 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
891 "slap_daemon_init: %d listeners opened\n", i ));
893 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
898 slapd_slp_init( urls );
903 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
909 slapd_daemon_destroy(void)
911 connections_destroy();
912 tcp_close( wake_sds[1] );
913 tcp_close( wake_sds[0] );
931 time_t last_idle_check = slap_get_time();
934 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
935 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
938 if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN ) == -1 ) {
939 int err = sock_errno();
941 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
942 "slapd_daemon_task: listen( %s, 5 ) failed errno=%d (%s)\n",
943 slap_listeners[l]->sl_url, err, sock_errstr(err) ));
945 Debug( LDAP_DEBUG_ANY,
946 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
947 slap_listeners[l]->sl_url, err,
953 slapd_add( slap_listeners[l]->sl_sd );
956 #ifdef HAVE_NT_SERVICE_MANAGER
957 if ( started_event != NULL ) {
958 ldap_pvt_thread_cond_signal( &started_event );
961 /* initialization complete. Here comes the loop. */
963 while ( !slapd_shutdown ) {
968 #define SLAPD_EBADF_LIMIT 16
971 #define SLAPD_IDLE_CHECK_LIMIT 4
972 time_t now = slap_get_time();
979 #if defined(SLAPD_RLOOKUPS)
985 if( global_idletimeout > 0 && difftime(
986 last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT,
989 connections_timeout_idle(now);
992 FD_ZERO( &writefds );
998 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1000 #ifdef FD_SET_MANUAL_COPY
1001 for( s = 0; s < nfds; s++ ) {
1002 if(FD_ISSET( &slap_sd_readers, s )) {
1003 FD_SET( s, &readfds );
1005 if(FD_ISSET( &slap_sd_writers, s )) {
1006 FD_SET( s, &writefds );
1010 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
1011 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
1013 assert(!FD_ISSET(wake_sds[0], &readfds));
1014 FD_SET( wake_sds[0], &readfds );
1016 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1017 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1019 if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
1020 FD_SET( slap_listeners[l]->sl_sd, &readfds );
1023 #ifndef HAVE_WINSOCK
1024 nfds = slap_daemon.sd_nfds;
1029 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1031 at = ldap_pvt_thread_pool_backload(&connection_pool);
1033 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
1036 tvp = at ? &zero : NULL;
1039 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1040 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1044 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1045 "slapd_daemon_task: select: listen=%d active_threads=%d tvp=%s\n",
1046 slap_listeners[l]->sl_sd, at, tvp == NULL ? "NULL" : "zero" ));
1048 Debug( LDAP_DEBUG_CONNS,
1049 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
1050 slap_listeners[l]->sl_sd, at,
1051 tvp == NULL ? "NULL" : "zero" );
1055 switch(ns = select( nfds, &readfds,
1057 /* don't pass empty fd_set */
1058 ( writefds.fd_count > 0 ? &writefds : NULL ),
1064 case -1: { /* failure - try again */
1065 int err = sock_errno();
1069 /* you'd think this would be EBADF */
1070 || err == WSAENOTSOCK
1073 if (++ebadf < SLAPD_EBADF_LIMIT)
1077 if( err != EINTR ) {
1079 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1080 "slapd_daemon_task: select failed (%d): %s\n",
1081 err, sock_errstr(err) ));
1083 Debug( LDAP_DEBUG_CONNS,
1084 "daemon: select failed (%d): %s\n",
1085 err, sock_errstr(err), 0 );
1087 slapd_shutdown = -1;
1092 case 0: /* timeout - let threads run */
1095 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1096 "slapd_daemon_task: select timeout - yielding\n" ));
1098 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
1101 ldap_pvt_thread_yield();
1104 default: /* something happened - deal with it */
1105 if( slapd_shutdown ) continue;
1109 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1110 "slapd_daemon_task: activity on %d descriptors\n", ns ));
1112 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
1118 if( FD_ISSET( wake_sds[0], &readfds ) ) {
1120 tcp_read( wake_sds[0], c, sizeof(c) );
1127 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1129 socklen_t len = sizeof(from);
1132 char *authid = NULL;
1136 #ifdef LDAP_PF_LOCAL
1137 char peername[MAXPATHLEN + sizeof("PATH=")];
1138 #elif defined(LDAP_PF_INET6)
1139 char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
1141 char peername[sizeof("IP=255.255.255.255:65336")];
1142 #endif /* LDAP_PF_LOCAL */
1146 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1149 if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
1152 s = accept( slap_listeners[l]->sl_sd,
1153 (struct sockaddr *) &from, &len );
1154 if ( s == AC_SOCKET_INVALID ) {
1155 int err = sock_errno();
1157 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1158 "slapd_daemon_task: accept(%ld) failed errno=%d (%s)\n",
1159 (long)slap_listeners[l]->sl_sd, err, sock_errstr(err) ));
1161 Debug( LDAP_DEBUG_ANY,
1162 "daemon: accept(%ld) failed errno=%d (%s)\n",
1163 (long) slap_listeners[l]->sl_sd, err,
1169 #ifndef HAVE_WINSOCK
1170 /* make sure descriptor number isn't too great */
1171 if ( s >= dtblsize ) {
1173 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1174 "slapd_daemon_task: %ld beyond descriptor table size %ld\n",
1175 (long)s, (long)dtblsize ));
1177 Debug( LDAP_DEBUG_ANY,
1178 "daemon: %ld beyond descriptor table size %ld\n",
1179 (long) s, (long) dtblsize, 0 );
1187 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1189 /* newly accepted stream should not be in any of the FD SETS */
1190 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
1191 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
1192 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
1194 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1197 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1198 #ifdef LDAP_PF_LOCAL
1199 /* for IPv4 and IPv6 sockets only */
1200 if ( from.sa_addr.sa_family != AF_LOCAL )
1201 #endif /* LDAP_PF_LOCAL */
1206 /* enable keep alives */
1208 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1209 (char *) &tmp, sizeof(tmp) );
1210 if ( rc == AC_SOCKET_ERROR ) {
1211 int err = sock_errno();
1213 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1214 "slapd_daemon_task: setsockopt( %ld, SO_KEEPALIVE) failed errno=%d (%s)\n",
1215 (long)s, err, sock_errstr(err) ));
1217 Debug( LDAP_DEBUG_ANY,
1218 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1219 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1224 /* enable no delay */
1226 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1227 (char *)&tmp, sizeof(tmp) );
1228 if ( rc == AC_SOCKET_ERROR ) {
1229 int err = sock_errno();
1231 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1232 "slapd_daemon_task: setsockopt( %ld, TCP_NODELAY) failed errno=%d (%s)\n",
1233 (long)s, err, sock_errstr(err) ));
1235 Debug( LDAP_DEBUG_ANY,
1236 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1237 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1245 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1246 "slapd_daemon_task: new connection on %ld\n", (long)s ));
1248 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
1251 switch ( from.sa_addr.sa_family ) {
1252 # ifdef LDAP_PF_LOCAL
1254 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1255 ssf = LDAP_PVT_SASL_LOCAL_SSF;
1257 #endif /* LDAP_PF_LOCAL */
1259 # ifdef LDAP_PF_INET6
1261 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1262 peeraddr = inet_ntoa( *((struct in_addr *)
1263 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1264 sprintf( peername, "IP=%s:%d",
1265 peeraddr != NULL ? peeraddr : "unknown",
1266 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1268 char addr[INET6_ADDRSTRLEN];
1269 sprintf( peername, "IP=%s %d",
1270 inet_ntop( AF_INET6,
1271 &from.sa_in6_addr.sin6_addr,
1272 addr, sizeof addr) ? addr : "unknown",
1273 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1276 # endif /* LDAP_PF_INET6 */
1279 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1280 sprintf( peername, "IP=%s:%d",
1281 peeraddr != NULL ? peeraddr : "unknown",
1282 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1290 if ( ( from.sa_addr.sa_family == AF_INET )
1291 #ifdef LDAP_PF_INET6
1292 || ( from.sa_addr.sa_family == AF_INET6 )
1295 #ifdef SLAPD_RLOOKUPS
1296 # ifdef LDAP_PF_INET6
1297 if ( from.sa_addr.sa_family == AF_INET6 )
1299 (char *)&(from.sa_in6_addr.sin6_addr),
1300 sizeof(from.sa_in6_addr.sin6_addr),
1303 # endif /* LDAP_PF_INET6 */
1305 (char *) &(from.sa_in_addr.sin_addr),
1306 sizeof(from.sa_in_addr.sin_addr),
1308 dnsname = hp ? ldap_pvt_str2lower( hp->h_name ) : NULL;
1311 #endif /* SLAPD_RLOOKUPS */
1314 if ( !hosts_ctl("slapd",
1315 dnsname != NULL ? dnsname : STRING_UNKNOWN,
1316 peeraddr != NULL ? peeraddr : STRING_UNKNOWN,
1320 Statslog( LDAP_DEBUG_ANY,
1321 "fd=%ld host access from %s (%s) denied.\n",
1323 dnsname != NULL ? dnsname : "unknown",
1324 peeraddr != NULL ? peeraddr : "unknown",
1329 #endif /* HAVE_TCPD */
1332 id = connection_init(s,
1333 slap_listeners[l]->sl_url,
1334 dnsname != NULL ? dnsname : "unknown",
1336 slap_listeners[l]->sl_name,
1338 slap_listeners[l]->sl_is_tls,
1345 if( authid ) ch_free(authid);
1349 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1350 "slapd_daemon_task: connection_init(%ld, %s, %s) failed.\n",
1351 (long)s, peername, slap_listeners[l]->sl_name ));
1353 Debug( LDAP_DEBUG_ANY,
1354 "daemon: connection_init(%ld, %s, %s) failed.\n",
1357 slap_listeners[l]->sl_name );
1363 Statslog( LDAP_DEBUG_STATS,
1364 "daemon: conn=%ld fd=%ld connection from %s (%s) accepted.\n",
1367 slap_listeners[l]->sl_name,
1376 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1377 "slapd_daemon_task: activity on " ));
1379 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1382 for ( i = 0; i < readfds.fd_count; i++ ) {
1384 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1385 " %d%s", readfs.fd_array[i], "r", 0 );
1387 Debug( LDAP_DEBUG_CONNS, " %d%s",
1388 readfds.fd_array[i], "r", 0 );
1391 for ( i = 0; i < writefds.fd_count; i++ ) {
1393 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1394 " %d%s", writefds.fd_array[i], "w" ));
1396 Debug( LDAP_DEBUG_CONNS, " %d%s",
1397 writefds.fd_array[i], "w", 0 );
1402 for ( i = 0; i < nfds; i++ ) {
1404 int is_listener = 0;
1406 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1407 if ( i == slap_listeners[l]->sl_sd ) {
1412 if ( is_listener ) {
1415 r = FD_ISSET( i, &readfds );
1416 w = FD_ISSET( i, &writefds );
1419 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1421 r ? "r" : "", w ? "w" : "" ));
1423 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1424 r ? "r" : "", w ? "w" : "" );
1430 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2, "\n" ));
1432 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1437 /* loop through the writers */
1439 for ( i = 0; i < writefds.fd_count; i++ )
1441 for ( i = 0; i < nfds; i++ )
1445 int is_listener = 0;
1447 wd = writefds.fd_array[i];
1449 if( ! FD_ISSET( i, &writefds ) ) {
1455 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1456 if ( i == slap_listeners[l]->sl_sd ) {
1461 if ( is_listener ) {
1465 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1466 "slapd_daemon_task: write active on %d\n", wd ));
1468 Debug( LDAP_DEBUG_CONNS,
1469 "daemon: write active on %d\n",
1473 * NOTE: it is possible that the connection was closed
1474 * and that the stream is now inactive.
1475 * connection_write() must valid the stream is still
1479 if ( connection_write( wd ) < 0 ) {
1480 FD_CLR( (unsigned) wd, &readfds );
1486 for ( i = 0; i < readfds.fd_count; i++ )
1488 for ( i = 0; i < nfds; i++ )
1492 int is_listener = 0;
1495 rd = readfds.fd_array[i];
1497 if( ! FD_ISSET( i, &readfds ) ) {
1503 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1504 if ( rd == slap_listeners[l]->sl_sd ) {
1509 if ( is_listener ) {
1514 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1515 "slapd_daemon_task: read activity on %d\n", rd ));
1517 Debug ( LDAP_DEBUG_CONNS,
1518 "daemon: read activity on %d\n", rd, 0, 0 );
1521 * NOTE: it is possible that the connection was closed
1522 * and that the stream is now inactive.
1523 * connection_read() must valid the stream is still
1527 if ( connection_read( rd ) < 0 ) {
1531 ldap_pvt_thread_yield();
1534 if( slapd_shutdown > 0 ) {
1536 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1537 "slapd_daemon_task: shutdown requested and initiated.\n"));
1539 Debug( LDAP_DEBUG_TRACE,
1540 "daemon: shutdown requested and initiated.\n",
1544 } else if ( slapd_shutdown < 0 ) {
1545 #ifdef HAVE_NT_SERVICE_MANAGER
1546 if (slapd_shutdown == -1)
1548 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1549 "slapd_daemon_task: shutdown initiated by Service Manager.\n"));
1551 Debug( LDAP_DEBUG_TRACE,
1552 "daemon: shutdown initiated by Service Manager.\n",
1558 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1559 "slapd_daemon_task: abnormal condition, shutdown initiated.\n" ));
1561 Debug( LDAP_DEBUG_TRACE,
1562 "daemon: abnormal condition, shutdown initiated.\n",
1567 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1568 "slapd_daemon_task: no active streams, shutdown initiated.\n" ));
1570 Debug( LDAP_DEBUG_TRACE,
1571 "daemon: no active streams, shutdown initiated.\n",
1576 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1577 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1578 #ifdef LDAP_PF_LOCAL
1579 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1580 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1582 #endif /* LDAP_PF_LOCAL */
1583 slapd_close( slap_listeners[l]->sl_sd );
1589 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1590 "slapd_daemon_task: shutdown waiting for %d threads to terminate.\n",
1591 ldap_pvt_thread_pool_backload(&connection_pool) ));
1593 Debug( LDAP_DEBUG_ANY,
1594 "slapd shutdown: waiting for %d threads to terminate\n",
1595 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1597 ldap_pvt_thread_pool_destroy(&connection_pool, 1);
1603 int slapd_daemon( void )
1609 #define SLAPD_LISTENER_THREAD 1
1610 #if defined( SLAPD_LISTENER_THREAD )
1612 ldap_pvt_thread_t listener_tid;
1614 /* listener as a separate THREAD */
1615 rc = ldap_pvt_thread_create( &listener_tid,
1616 0, slapd_daemon_task, NULL );
1620 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1621 "slapd_daemon: listener ldap_pvt_thread_create failed (%d).\n", rc ));
1623 Debug( LDAP_DEBUG_ANY,
1624 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
1629 /* wait for the listener thread to complete */
1630 ldap_pvt_thread_join( listener_tid, (void *) NULL );
1633 /* experimental code */
1634 slapd_daemon_task( NULL );
1643 #if defined( HAVE_WINSOCK2 )
1644 WORD wVersionRequested;
1648 wVersionRequested = MAKEWORD( 2, 0 );
1650 err = WSAStartup( wVersionRequested, &wsaData );
1652 /* Tell the user that we couldn't find a usable */
1657 /* Confirm that the WinSock DLL supports 2.0.*/
1658 /* Note that if the DLL supports versions greater */
1659 /* than 2.0 in addition to 2.0, it will still return */
1660 /* 2.0 in wVersion since that is the version we */
1663 if ( LOBYTE( wsaData.wVersion ) != 2 ||
1664 HIBYTE( wsaData.wVersion ) != 0 )
1666 /* Tell the user that we couldn't find a usable */
1672 /* The WinSock DLL is acceptable. Proceed. */
1673 #elif defined( HAVE_WINSOCK )
1675 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
1682 int sockdestroy(void)
1684 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
1691 slap_sig_shutdown( int sig )
1694 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1695 "slap_sig_shutdown: signal %d\n", sig ));
1697 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
1701 * If the NT Service Manager is controlling the server, we don't
1702 * want SIGBREAK to kill the server. For some strange reason,
1703 * SIGBREAK is generated when a user logs out.
1706 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
1707 if (is_NT_Service && sig == SIGBREAK)
1709 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1710 "slap_sig_shutdown: SIGBREAK ignored.\n" ));
1712 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: SIGBREAK ignored.\n",
1717 slapd_shutdown = sig;
1721 /* reinstall self */
1722 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
1726 slap_sig_wake( int sig )
1730 /* reinstall self */
1731 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
1735 void slapd_add_internal(ber_socket_t s) {