3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/socket.h>
14 #include <ac/string.h>
16 #include <ac/unistd.h>
19 #include "ldap_pvt_thread.h"
26 int allow_severity = LOG_INFO;
27 int deny_severity = LOG_NOTICE;
28 #endif /* TCP Wrappers */
32 /* this should go in <ldap.h> as soon as it is accepted */
33 #define LDAPI_MOD_URLEXT "x-mod"
34 #endif /* LDAP_PF_LOCAL */
36 int slap_inet4or6 = AF_UNSPEC;
40 ber_socket_t dtblsize;
42 Listener **slap_listeners = NULL;
44 #define SLAPD_LISTEN 10
46 static ber_socket_t wake_sds[2];
50 #define WAKE_LISTENER(w) \
51 ((w && !waking) ? tcp_write( wake_sds[1], "0", 1 ), waking=1 : 0)
53 #define WAKE_LISTENER(w) \
54 do { if (w) tcp_write( wake_sds[1], "0", 1 ); } while(0)
60 volatile sig_atomic_t slapd_shutdown = 0, slapd_gentle_shutdown = 0;
62 static struct slap_daemon {
63 ldap_pvt_thread_mutex_t sd_mutex;
65 ber_socket_t sd_nactives;
68 /* In winsock, accept() returns values higher than dtblsize
69 so don't bother with this optimization */
82 * SLP related functions
86 #define LDAP_SRVTYPE_PREFIX "service:ldap://"
87 #define LDAPS_SRVTYPE_PREFIX "service:ldaps://"
88 static char** slapd_srvurls = NULL;
89 static SLPHandle slapd_hslp = 0;
91 void slapd_slp_init( const char* urls ) {
94 slapd_srvurls = str2charray( urls, " " );
96 if( slapd_srvurls == NULL ) return;
98 /* find and expand INADDR_ANY URLs */
99 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
100 if( strcmp( slapd_srvurls[i], "ldap:///" ) == 0) {
101 char *host = ldap_pvt_get_fqdn( NULL );
102 if ( host != NULL ) {
103 slapd_srvurls[i] = (char *) realloc( slapd_srvurls[i],
105 sizeof( LDAP_SRVTYPE_PREFIX ) );
106 strcpy( slap_strcopy(slapd_srvurls[i],
107 LDAP_SRVTYPE_PREFIX ), host );
112 } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0) {
113 char *host = ldap_pvt_get_fqdn( NULL );
114 if ( host != NULL ) {
115 slapd_srvurls[i] = (char *) realloc( slapd_srvurls[i],
117 sizeof( LDAPS_SRVTYPE_PREFIX ) );
118 strcpy( slap_strcopy(slapd_srvurls[i],
119 LDAPS_SRVTYPE_PREFIX ), host );
126 /* open the SLP handle */
127 SLPOpen( "en", 0, &slapd_hslp );
130 void slapd_slp_deinit() {
131 if( slapd_srvurls == NULL ) return;
133 charray_free( slapd_srvurls );
134 slapd_srvurls = NULL;
136 /* close the SLP handle */
137 SLPClose( slapd_hslp );
140 void slapd_slp_regreport(
148 void slapd_slp_reg() {
151 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
152 if( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
153 sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
154 strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
155 sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
159 SLP_LIFETIME_MAXIMUM,
169 void slapd_slp_dereg() {
172 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
173 SLPDereg( slapd_hslp,
179 #endif /* HAVE_SLP */
182 * Add a descriptor to daemon control
184 static void slapd_add(ber_socket_t s) {
185 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
187 assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
188 assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
189 assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
192 if (s >= slap_daemon.sd_nfds) {
193 slap_daemon.sd_nfds = s + 1;
197 slap_daemon.sd_nactives++;
199 FD_SET( s, &slap_daemon.sd_actives );
200 FD_SET( s, &slap_daemon.sd_readers );
203 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
204 "slapd_add: added %ld%s%s\n",
206 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
207 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" ));
209 Debug( LDAP_DEBUG_CONNS, "daemon: added %ld%s%s\n",
211 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
212 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
214 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
218 * Remove the descriptor from daemon control
220 void slapd_remove(ber_socket_t s, int wake) {
221 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
223 slap_daemon.sd_nactives--;
226 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
227 "slapd_remove: removing %ld%s%s\n",
229 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
230 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" ));
232 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
234 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
235 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
237 FD_CLR( s, &slap_daemon.sd_actives );
238 FD_CLR( s, &slap_daemon.sd_readers );
239 FD_CLR( s, &slap_daemon.sd_writers );
241 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
242 WAKE_LISTENER(wake || slapd_gentle_shutdown == 2);
245 void slapd_clr_write(ber_socket_t s, int wake) {
246 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
248 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
249 FD_CLR( s, &slap_daemon.sd_writers );
251 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
255 void slapd_set_write(ber_socket_t s, int wake) {
256 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
258 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
259 if (!FD_ISSET(s, &slap_daemon.sd_writers))
260 FD_SET( (unsigned) s, &slap_daemon.sd_writers );
262 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
266 void slapd_clr_read(ber_socket_t s, int wake) {
267 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
269 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
270 FD_CLR( s, &slap_daemon.sd_readers );
272 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
276 void slapd_set_read(ber_socket_t s, int wake) {
277 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
279 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
280 if (!FD_ISSET(s, &slap_daemon.sd_readers))
281 FD_SET( s, &slap_daemon.sd_readers );
283 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
287 static void slapd_close(ber_socket_t s) {
289 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
290 "slapd_close: closing %ld\n", (long)s ));
292 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
298 static void slap_free_listener_addresses(struct sockaddr **sal)
300 struct sockaddr **sap;
306 for (sap = sal; *sap != NULL; sap++) {
314 static int get_url_perms(
326 for ( i = 0; exts[ i ]; i++ ) {
327 char *type = exts[ i ];
330 if ( type[ 0 ] == '!' ) {
335 if ( strncasecmp( type, LDAPI_MOD_URLEXT "=", sizeof(LDAPI_MOD_URLEXT "=") - 1 ) == 0 ) {
336 char *value = type + sizeof(LDAPI_MOD_URLEXT "=") - 1;
340 if ( strlen(value) != 3 ) {
344 for ( j = 0; j < 3; j++ ) {
346 = { S_IRWXU, S_IRWXG, S_IRWXO };
348 switch ( value[ j ] ) {
368 #endif /* LDAP_PF_LOCAL */
370 /* port = 0 indicates AF_LOCAL */
371 static int slap_get_listener_addresses(
374 struct sockaddr ***sal)
376 struct sockaddr **sap;
380 *sal = ch_malloc(2 * sizeof(void *));
386 *sap = ch_malloc(sizeof(struct sockaddr_un));
392 (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) ) {
394 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
395 "slap_get_listener_addresses: domain socket path (%s) too long in URL\n",
398 Debug( LDAP_DEBUG_ANY,
399 "daemon: domain socket path (%s) too long in URL",
405 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
406 (*sap)->sa_family = AF_LOCAL;
407 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
411 #ifdef HAVE_GETADDRINFO
412 struct addrinfo hints, *res, *sai;
416 memset( &hints, '\0', sizeof(hints) );
417 hints.ai_flags = AI_PASSIVE;
418 hints.ai_socktype = SOCK_STREAM;
419 hints.ai_family = slap_inet4or6;
420 snprintf(serv, sizeof serv, "%d", port);
422 if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
424 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
425 "slap_get_listener_addresses: getaddrinfo failed: %s\n",
426 AC_GAI_STRERROR(err) ));
428 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
429 AC_GAI_STRERROR(err), 0, 0);
435 for (n=2; (sai = sai->ai_next) != NULL; n++) {
438 *sal = ch_calloc(n, sizeof(void *));
446 for ( sai=res; sai; sai=sai->ai_next ) {
447 if( sai->ai_addr == NULL ) {
449 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
450 "slap_get_listener_addresses: "
451 "getaddrinfo ai_addr is NULL?\n" ));
453 Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
454 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
460 switch (sai->ai_family) {
461 # ifdef LDAP_PF_INET6
463 *sap = ch_malloc(sizeof(struct sockaddr_in6));
468 *(struct sockaddr_in6 *)*sap =
469 *((struct sockaddr_in6 *)sai->ai_addr);
473 *sap = ch_malloc(sizeof(struct sockaddr_in));
478 *(struct sockaddr_in *)*sap =
479 *((struct sockaddr_in *)sai->ai_addr);
487 (*sap)->sa_family = sai->ai_family;
497 struct hostent *he = NULL;
499 if ( host == NULL ) {
500 in.s_addr = htonl(INADDR_ANY);
502 } else if ( !inet_aton( host, &in ) ) {
503 he = gethostbyname( host );
506 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
507 "slap_get_listener_addresses: invalid host %s\n",
510 Debug( LDAP_DEBUG_ANY,
511 "daemon: invalid host %s", host, 0, 0);
515 for (n = 0; he->h_addr_list[n]; n++) ;
518 *sal = ch_malloc((n+1) * sizeof(void *));
524 for ( i = 0; i<n; i++ ) {
525 sap[i] = ch_malloc(sizeof(struct sockaddr_in));
529 (void)memset( (void *)sap[i], '\0', sizeof(struct sockaddr_in) );
530 sap[i]->sa_family = AF_INET;
531 ((struct sockaddr_in *)sap[i])->sin_port = htons(port);
533 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr, he->h_addr_list[i], sizeof(struct in_addr) );
535 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr, &in, sizeof(struct in_addr) );
545 slap_free_listener_addresses(*sal);
549 static int slap_open_listener(
560 int err, addrlen = 0;
561 struct sockaddr **sal, **psal;
562 int socktype = SOCK_STREAM; /* default to COTS */
564 mode_t perms = S_IRWXU;
568 rc = ldap_url_parse( url, &lud );
570 if( rc != LDAP_URL_SUCCESS ) {
572 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
573 "slap_open_listener: listen URL \"%s\" parse error %d\n",
576 Debug( LDAP_DEBUG_ANY,
577 "daemon: listen URL \"%s\" parse error=%d\n",
584 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
586 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
587 "slap_open_listener: TLS is not supported (%s)\n",
590 Debug( LDAP_DEBUG_ANY,
591 "daemon: TLS not supported (%s)\n",
594 ldap_free_urldesc( lud );
598 if(! lud->lud_port ) {
599 lud->lud_port = LDAP_PORT;
603 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
605 if(! lud->lud_port ) {
606 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
610 port = (unsigned short) lud->lud_port;
612 tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme);
613 if ( tmp == LDAP_PROTO_IPC ) {
615 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
616 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
618 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
621 if ( lud->lud_exts ) {
622 err = get_url_perms( lud->lud_exts, &perms, &crit );
627 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
628 "slap_open_listener: URL scheme is not supported: %s\n",
631 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
634 ldap_free_urldesc( lud );
638 #ifdef LDAP_CONNECTIONLESS
639 l.sl_is_udp = ( tmp == LDAP_PROTO_UDP );
641 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
642 || strcmp(lud->lud_host, "*") == 0 )
644 err = slap_get_listener_addresses(NULL, port, &sal);
646 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
650 ldap_free_urldesc( lud );
655 /* If we got more than one address returned, we need to make space
656 * for it in the slap_listeners array.
658 for ( num=0; sal[num]; num++ );
661 slap_listeners = ch_realloc( slap_listeners, (*listeners + 1) * sizeof(Listener *) );
665 while ( *sal != NULL ) {
666 switch( (*sal)->sa_family ) {
679 #ifdef LDAP_CONNECTIONLESS
681 socktype = SOCK_DGRAM;
683 l.sl_sd = socket( (*sal)->sa_family, socktype, 0);
684 if ( l.sl_sd == AC_SOCKET_INVALID ) {
685 int err = sock_errno();
687 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
688 "slap_open_listener: socket() failed errno=%d (%s)\n",
689 err, sock_errstr(err) ));
691 Debug( LDAP_DEBUG_ANY,
692 "daemon: socket() failed errno=%d (%s)\n", err,
693 sock_errstr(err), 0 );
699 if ( l.sl_sd >= dtblsize ) {
701 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
702 "slap_open_listener: listener descriptor %ld is too great %ld\n",
703 (long)l.sl_sd, (long)dtblsize ));
705 Debug( LDAP_DEBUG_ANY,
706 "daemon: listener descriptor %ld is too great %ld\n",
707 (long) l.sl_sd, (long) dtblsize, 0 );
709 tcp_close( l.sl_sd );
715 if ( (*sal)->sa_family == AF_LOCAL ) {
716 unlink ( ((struct sockaddr_un *)*sal)->sun_path );
721 /* enable address reuse */
723 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
724 (char *) &tmp, sizeof(tmp) );
725 if ( rc == AC_SOCKET_ERROR ) {
726 int err = sock_errno();
728 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
729 "slap_open_listener: setsockopt( %ld, SO_REUSEADDR ) failed errno %d (%s)\n",
730 (long)l.sl_sd, err, sock_errstr(err) ));
732 Debug( LDAP_DEBUG_ANY,
733 "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
734 (long) l.sl_sd, err, sock_errstr(err) );
740 switch( (*sal)->sa_family ) {
742 addrlen = sizeof(struct sockaddr_in);
747 /* Try to use IPv6 sockets for IPv6 only */
749 rc = setsockopt( l.sl_sd, IPPROTO_IPV6, IPV6_V6ONLY,
750 (char *) &tmp, sizeof(tmp) );
751 if ( rc == AC_SOCKET_ERROR ) {
752 int err = sock_errno();
754 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
755 "slap_open_listener: setsockopt( %ld, IPV6_V6ONLY ) failed errno %d (%s)\n",
756 (long)l.sl_sd, err, sock_errstr(err) ));
758 Debug( LDAP_DEBUG_ANY,
759 "slapd(%ld): setsockopt(IPV6_V6ONLY) failed errno=%d (%s)\n",
760 (long) l.sl_sd, err, sock_errstr(err) );
764 addrlen = sizeof(struct sockaddr_in6);
769 addrlen = sizeof(struct sockaddr_un);
774 if (bind(l.sl_sd, *sal, addrlen)) {
777 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
778 "slap_open_listener: bind(%ld) failed errno=%d (%s)\n",
779 (long)l.sl_sd, err, sock_errstr(err) ));
781 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
782 (long) l.sl_sd, err, sock_errstr(err) );
784 tcp_close( l.sl_sd );
789 switch ( (*sal)->sa_family ) {
792 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
793 if ( chmod( addr, perms ) < 0 && crit ) {
794 int err = sock_errno();
796 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
797 "slap_open_listener: fchmod(%ld) failed errno=%d (%s)\n",
798 (long)l.sl_sd, err, sock_errstr(err) ));
800 Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
801 (long) l.sl_sd, err, sock_errstr(err) );
803 tcp_close( l.sl_sd );
804 slap_free_listener_addresses(psal);
807 l.sl_name = ch_malloc( strlen(addr) + sizeof("PATH=") );
808 sprintf( l.sl_name, "PATH=%s", addr );
810 #endif /* LDAP_PF_LOCAL */
814 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
815 char addr[INET_ADDRSTRLEN];
816 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
817 addr, sizeof(addr) );
820 s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr );
822 port = ntohs( ((struct sockaddr_in *)*sal) ->sin_port );
823 l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65535") );
824 sprintf( l.sl_name, "IP=%s:%d",
825 s != NULL ? s : "unknown" , port );
830 char addr[INET6_ADDRSTRLEN];
831 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
833 port = ntohs( ((struct sockaddr_in6 *)*sal)->sin6_port );
834 l.sl_name = ch_malloc( strlen(addr) + sizeof("IP= 65535") );
835 sprintf( l.sl_name, "IP=%s %d", addr, port );
837 #endif /* LDAP_PF_INET6 */
841 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
842 "slap_open_listener: unsupported address family (%d)\n",
843 (int)(*sal)->sa_family ));
845 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
846 (int) (*sal)->sa_family, 0, 0 );
851 AC_MEMCPY(&l.sl_sa, *sal, addrlen);
852 l.sl_url = ch_strdup( url );
853 li = ch_malloc( sizeof( Listener ) );
855 slap_listeners[*cur] = li;
859 } /* while ( *sal != NULL ) */
861 slap_free_listener_addresses(psal);
865 LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
866 "slap_open_listener: daemon initialized %s\n", l.sl_url ));
868 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
874 static int sockinit(void);
875 static int sockdestroy(void);
877 int slapd_daemon_init( const char *urls )
883 LDAP_LOG(( "connection", LDAP_LEVEL_ARGS,
884 "slapd_daemon_init: %s\n",
885 urls ? urls : "<null>" ));
887 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
888 urls ? urls : "<null>", 0, 0 );
890 if( (rc = sockinit()) != 0 ) {
895 dtblsize = sysconf( _SC_OPEN_MAX );
896 #elif HAVE_GETDTABLESIZE
897 dtblsize = getdtablesize();
899 dtblsize = FD_SETSIZE;
903 if(dtblsize > FD_SETSIZE) {
904 dtblsize = FD_SETSIZE;
906 #endif /* !FD_SETSIZE */
908 /* open a pipe (or something equivalent connected to itself).
909 * we write a byte on this fd whenever we catch a signal. The main
910 * loop will be select'ing on this socket, and will wake up when
913 if( (rc = lutil_pair( wake_sds )) < 0 ) {
915 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
916 "slap_daemon_init: lutil_pair() failed rc=%d\n", rc ));
918 Debug( LDAP_DEBUG_ANY,
919 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
924 FD_ZERO( &slap_daemon.sd_readers );
925 FD_ZERO( &slap_daemon.sd_writers );
931 u = str2charray( urls, " " );
933 if( u == NULL || u[0] == NULL ) {
935 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
936 "slap_daemon_init: no urls (%s) provided.\n", urls ));
938 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
944 for( i=0; u[i] != NULL; i++ ) {
946 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
947 "slap_daemon_init: listen on %s\n.", u[i] ));
949 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
956 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
957 "slap_daemon_init: no listeners to open (%s)\n", urls ));
959 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
967 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
968 "slap_daemon_init: %d listeners to open...\n", i ));
970 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
973 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
975 for(n = 0, j = 0; u[n]; n++ ) {
976 if ( slap_open_listener( u[n], &i, &j ) ) {
981 slap_listeners[j] = NULL;
984 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
985 "slap_daemon_init: %d listeners opened\n", i ));
987 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
992 slapd_slp_init( urls );
997 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
1003 slapd_daemon_destroy(void)
1005 connections_destroy();
1006 tcp_close( wake_sds[1] );
1007 tcp_close( wake_sds[0] );
1026 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1027 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1029 slapd_remove( slap_listeners[l]->sl_sd, 0 );
1030 #ifdef LDAP_PF_LOCAL
1031 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1032 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1034 #endif /* LDAP_PF_LOCAL */
1035 slapd_close( slap_listeners[l]->sl_sd );
1037 if ( slap_listeners[l]->sl_url )
1038 free ( slap_listeners[l]->sl_url );
1039 if ( slap_listeners[l]->sl_name )
1040 free ( slap_listeners[l]->sl_name );
1041 free ( slap_listeners[l] );
1042 slap_listeners[l] = NULL;
1053 time_t last_idle_check = 0;
1056 if ( global_idletimeout > 0 ) {
1057 last_idle_check = slap_get_time();
1059 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1060 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1062 #ifdef LDAP_CONNECTIONLESS
1063 /* Since this is connectionless, the data port is the
1064 * listening port. The listen() and accept() calls
1067 if ( slap_listeners[l]->sl_is_udp )
1069 slapd_add( slap_listeners[l]->sl_sd );
1074 if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN ) == -1 ) {
1075 int err = sock_errno();
1077 #ifdef LDAP_PF_INET6
1078 /* If error is EADDRINUSE, we are trying to listen to INADDR_ANY and
1079 * we are already listening to in6addr_any, then we want to ignore
1080 * this and continue.
1082 if ( err == EADDRINUSE ) {
1084 struct sockaddr_in sa = slap_listeners[l]->sl_sa.sa_in_addr;
1085 struct sockaddr_in6 sa6;
1087 if ( sa.sin_family == AF_INET &&
1088 sa.sin_addr.s_addr == htonl(INADDR_ANY) ) {
1089 for ( i = 0 ; i < l; i++ ) {
1090 sa6 = slap_listeners[i]->sl_sa.sa_in6_addr;
1091 if ( sa6.sin6_family == AF_INET6 &&
1092 !memcmp( &sa6.sin6_addr, &in6addr_any, sizeof(struct in6_addr) ) )
1097 /* We are already listening to in6addr_any */
1099 LDAP_LOG(( "connection", LDAP_LEVEL_WARNING,
1100 "slapd_daemon_task: Attempt to listen to 0.0.0.0 failed, already listening on ::, assuming IPv4 included\n" ));
1102 Debug( LDAP_DEBUG_CONNS,
1103 "daemon: Attempt to listen to 0.0.0.0 failed, already listening on ::, assuming IPv4 included\n",
1106 slapd_close( slap_listeners[l]->sl_sd );
1107 slap_listeners[l]->sl_sd = AC_SOCKET_INVALID;
1114 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1115 "slapd_daemon_task: listen( %s, 5 ) failed errno=%d (%s)\n",
1116 slap_listeners[l]->sl_url, err, sock_errstr(err) ));
1118 Debug( LDAP_DEBUG_ANY,
1119 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
1120 slap_listeners[l]->sl_url, err,
1123 return( (void*)-1 );
1126 slapd_add( slap_listeners[l]->sl_sd );
1129 #ifdef HAVE_NT_SERVICE_MANAGER
1130 if ( started_event != NULL ) {
1131 ldap_pvt_thread_cond_signal( &started_event );
1134 /* initialization complete. Here comes the loop. */
1136 while ( !slapd_shutdown ) {
1141 #define SLAPD_EBADF_LIMIT 16
1145 #define SLAPD_IDLE_CHECK_LIMIT 4
1153 #if defined(SLAPD_RLOOKUPS)
1156 struct timeval zero;
1157 struct timeval *tvp;
1160 now = slap_get_time();
1161 connections_timeout_idle( now );
1163 else if ( global_idletimeout > 0 ) {
1164 now = slap_get_time();
1165 if ( difftime( last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 ) {
1166 connections_timeout_idle( now );
1171 if( slapd_gentle_shutdown ) {
1172 ber_socket_t active;
1174 if( slapd_gentle_shutdown == 1 ) {
1175 Debug( LDAP_DEBUG_ANY, "slapd gentle shutdown\n", 0, 0, 0 );
1176 close_listeners( 1 );
1177 global_restrictops |= SLAP_RESTRICT_OP_WRITES;
1178 slapd_gentle_shutdown = 2;
1181 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1182 active = slap_daemon.sd_nactives;
1183 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1191 FD_ZERO( &writefds );
1192 FD_ZERO( &readfds );
1197 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1199 #ifdef FD_SET_MANUAL_COPY
1200 for( s = 0; s < nfds; s++ ) {
1201 if(FD_ISSET( &slap_sd_readers, s )) {
1202 FD_SET( s, &readfds );
1204 if(FD_ISSET( &slap_sd_writers, s )) {
1205 FD_SET( s, &writefds );
1209 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
1210 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
1212 assert(!FD_ISSET(wake_sds[0], &readfds));
1213 FD_SET( wake_sds[0], &readfds );
1215 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1216 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1218 if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
1219 FD_SET( slap_listeners[l]->sl_sd, &readfds );
1222 #ifndef HAVE_WINSOCK
1223 nfds = slap_daemon.sd_nfds;
1228 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1230 at = ldap_pvt_thread_pool_backload(&connection_pool);
1232 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
1235 tvp = at ? &zero : NULL;
1238 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1239 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1243 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1244 "slapd_daemon_task: select: listen=%d active_threads=%d tvp=%s\n",
1245 slap_listeners[l]->sl_sd, at, tvp == NULL ? "NULL" : "zero" ));
1247 Debug( LDAP_DEBUG_CONNS,
1248 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
1249 slap_listeners[l]->sl_sd, at,
1250 tvp == NULL ? "NULL" : "zero" );
1254 switch(ns = select( nfds, &readfds,
1256 /* don't pass empty fd_set */
1257 ( writefds.fd_count > 0 ? &writefds : NULL ),
1263 case -1: { /* failure - try again */
1264 int err = sock_errno();
1268 /* you'd think this would be EBADF */
1269 || err == WSAENOTSOCK
1272 if (++ebadf < SLAPD_EBADF_LIMIT)
1276 if( err != EINTR ) {
1278 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1279 "slapd_daemon_task: select failed (%d): %s\n",
1280 err, sock_errstr(err) ));
1282 Debug( LDAP_DEBUG_CONNS,
1283 "daemon: select failed (%d): %s\n",
1284 err, sock_errstr(err), 0 );
1291 case 0: /* timeout - let threads run */
1294 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1295 "slapd_daemon_task: select timeout - yielding\n" ));
1297 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
1300 ldap_pvt_thread_yield();
1303 default: /* something happened - deal with it */
1304 if( slapd_shutdown ) continue;
1308 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1309 "slapd_daemon_task: activity on %d descriptors\n", ns ));
1311 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
1317 if( FD_ISSET( wake_sds[0], &readfds ) ) {
1319 tcp_read( wake_sds[0], c, sizeof(c) );
1326 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1328 socklen_t len = sizeof(from);
1331 char *authid = NULL;
1333 char *dnsname = NULL;
1335 #ifdef LDAP_PF_LOCAL
1336 char peername[MAXPATHLEN + sizeof("PATH=")];
1337 #elif defined(LDAP_PF_INET6)
1338 char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
1340 char peername[sizeof("IP=255.255.255.255:65336")];
1341 #endif /* LDAP_PF_LOCAL */
1345 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1348 if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
1351 #ifdef LDAP_CONNECTIONLESS
1352 if ( slap_listeners[l]->sl_is_udp )
1354 /* The first time we receive a query, we set this
1355 * up as a "connection". It remains open for the life
1358 if ( slap_listeners[l]->sl_is_udp < 2 )
1360 id = connection_init(
1361 slap_listeners[l]->sl_sd,
1362 slap_listeners[l]->sl_url, "", "",
1363 slap_listeners[l]->sl_name,
1365 slap_listeners[l]->sl_is_udp++;
1371 s = accept( slap_listeners[l]->sl_sd,
1372 (struct sockaddr *) &from, &len );
1373 if ( s == AC_SOCKET_INVALID ) {
1374 int err = sock_errno();
1377 if( err == EMFILE ) {
1382 if( err == ENFILE ) {
1392 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1393 "slapd_daemon_task: accept(%ld) failed errno=%d (%s)\n",
1394 (long)slap_listeners[l]->sl_sd, err, sock_errstr(err) ));
1396 Debug( LDAP_DEBUG_ANY,
1397 "daemon: accept(%ld) failed errno=%d (%s)\n",
1398 (long) slap_listeners[l]->sl_sd, err,
1402 /* prevent busy loop */
1404 if( emfile % 4 == 3 ) usleep( 250 );
1406 if( emfile % 8 == 7 ) sleep( 1 );
1410 ldap_pvt_thread_yield();
1415 #ifndef HAVE_WINSOCK
1416 /* make sure descriptor number isn't too great */
1417 if ( s >= dtblsize ) {
1419 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1420 "slapd_daemon_task: %ld beyond descriptor table size %ld\n",
1421 (long)s, (long)dtblsize ));
1423 Debug( LDAP_DEBUG_ANY,
1424 "daemon: %ld beyond descriptor table size %ld\n",
1425 (long) s, (long) dtblsize, 0 );
1429 ldap_pvt_thread_yield();
1435 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1437 /* newly accepted stream should not be in any of the FD SETS */
1438 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
1439 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
1440 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
1442 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1445 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1446 #ifdef LDAP_PF_LOCAL
1447 /* for IPv4 and IPv6 sockets only */
1448 if ( from.sa_addr.sa_family != AF_LOCAL )
1449 #endif /* LDAP_PF_LOCAL */
1454 /* enable keep alives */
1456 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1457 (char *) &tmp, sizeof(tmp) );
1458 if ( rc == AC_SOCKET_ERROR ) {
1459 int err = sock_errno();
1461 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1462 "slapd_daemon_task: setsockopt( %ld, SO_KEEPALIVE) failed errno=%d (%s)\n",
1463 (long)s, err, sock_errstr(err) ));
1465 Debug( LDAP_DEBUG_ANY,
1466 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1467 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1472 /* enable no delay */
1474 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1475 (char *)&tmp, sizeof(tmp) );
1476 if ( rc == AC_SOCKET_ERROR ) {
1477 int err = sock_errno();
1479 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1480 "slapd_daemon_task: setsockopt( %ld, TCP_NODELAY) failed errno=%d (%s)\n",
1481 (long)s, err, sock_errstr(err) ));
1483 Debug( LDAP_DEBUG_ANY,
1484 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1485 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1493 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1494 "slapd_daemon_task: new connection on %ld\n", (long)s ));
1496 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
1499 switch ( from.sa_addr.sa_family ) {
1500 # ifdef LDAP_PF_LOCAL
1502 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1503 ssf = LDAP_PVT_SASL_LOCAL_SSF;
1506 #endif /* LDAP_PF_LOCAL */
1508 # ifdef LDAP_PF_INET6
1510 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1511 peeraddr = inet_ntoa( *((struct in_addr *)
1512 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1513 sprintf( peername, "IP=%s:%d",
1514 peeraddr != NULL ? peeraddr : "unknown",
1515 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1517 char addr[INET6_ADDRSTRLEN];
1519 peeraddr = (char *) inet_ntop( AF_INET6,
1520 &from.sa_in6_addr.sin6_addr,
1521 addr, sizeof addr );
1522 sprintf( peername, "IP=%s %d",
1523 peeraddr != NULL ? peeraddr : "unknown",
1524 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1527 # endif /* LDAP_PF_INET6 */
1530 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1531 sprintf( peername, "IP=%s:%d",
1532 peeraddr != NULL ? peeraddr : "unknown",
1533 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1541 if ( ( from.sa_addr.sa_family == AF_INET )
1542 #ifdef LDAP_PF_INET6
1543 || ( from.sa_addr.sa_family == AF_INET6 )
1546 #ifdef SLAPD_RLOOKUPS
1547 if ( use_reverse_lookup ) {
1548 # ifdef LDAP_PF_INET6
1549 if ( from.sa_addr.sa_family == AF_INET6 )
1551 (char *)&(from.sa_in6_addr.sin6_addr),
1552 sizeof(from.sa_in6_addr.sin6_addr),
1555 # endif /* LDAP_PF_INET6 */
1557 (char *) &(from.sa_in_addr.sin_addr),
1558 sizeof(from.sa_in_addr.sin_addr),
1560 dnsname = hp ? ldap_pvt_str2lower( hp->h_name ) : NULL;
1564 #endif /* SLAPD_RLOOKUPS */
1567 if ( !hosts_ctl("slapd",
1568 dnsname != NULL ? dnsname : STRING_UNKNOWN,
1569 peeraddr != NULL ? peeraddr : STRING_UNKNOWN,
1573 Statslog( LDAP_DEBUG_ANY,
1574 "fd=%ld host access from %s (%s) denied.\n",
1576 dnsname != NULL ? dnsname : "unknown",
1577 peeraddr != NULL ? peeraddr : "unknown",
1582 #endif /* HAVE_TCPD */
1585 id = connection_init(s,
1586 slap_listeners[l]->sl_url,
1587 dnsname != NULL ? dnsname : "unknown",
1589 slap_listeners[l]->sl_name,
1591 slap_listeners[l]->sl_is_tls,
1598 if( authid ) ch_free(authid);
1602 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1603 "slapd_daemon_task: connection_init(%ld, %s, %s) failed.\n",
1604 (long)s, peername, slap_listeners[l]->sl_name ));
1606 Debug( LDAP_DEBUG_ANY,
1607 "daemon: connection_init(%ld, %s, %s) failed.\n",
1610 slap_listeners[l]->sl_name );
1616 Statslog( LDAP_DEBUG_STATS,
1617 "daemon: conn=%ld fd=%ld connection from %s (%s) accepted.\n",
1620 slap_listeners[l]->sl_name,
1629 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1630 "slapd_daemon_task: activity on " ));
1632 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1635 for ( i = 0; i < readfds.fd_count; i++ ) {
1637 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1638 " %d%s", readfds.fd_array[i], "r", 0 ));
1640 Debug( LDAP_DEBUG_CONNS, " %d%s",
1641 readfds.fd_array[i], "r", 0 );
1644 for ( i = 0; i < writefds.fd_count; i++ ) {
1646 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1647 " %d%s", writefds.fd_array[i], "w" ));
1649 Debug( LDAP_DEBUG_CONNS, " %d%s",
1650 writefds.fd_array[i], "w", 0 );
1655 for ( i = 0; i < nfds; i++ ) {
1657 int is_listener = 0;
1659 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1660 if ( i == slap_listeners[l]->sl_sd ) {
1661 #ifdef LDAP_CONNECTIONLESS
1662 /* The listener is the data port. Don't
1665 if (slap_listeners[l]->sl_is_udp)
1672 if ( is_listener ) {
1675 r = FD_ISSET( i, &readfds );
1676 w = FD_ISSET( i, &writefds );
1679 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1681 r ? "r" : "", w ? "w" : "" ));
1683 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1684 r ? "r" : "", w ? "w" : "" );
1690 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2, "\n" ));
1692 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1697 /* loop through the writers */
1699 for ( i = 0; i < writefds.fd_count; i++ )
1701 for ( i = 0; i < nfds; i++ )
1705 int is_listener = 0;
1707 wd = writefds.fd_array[i];
1709 if( ! FD_ISSET( i, &writefds ) ) {
1715 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1716 if ( i == slap_listeners[l]->sl_sd ) {
1717 #ifdef LDAP_CONNECTIONLESS
1718 if (slap_listeners[l]->sl_is_udp)
1725 if ( is_listener ) {
1729 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1730 "slapd_daemon_task: write active on %d\n", wd ));
1732 Debug( LDAP_DEBUG_CONNS,
1733 "daemon: write active on %d\n",
1737 * NOTE: it is possible that the connection was closed
1738 * and that the stream is now inactive.
1739 * connection_write() must valid the stream is still
1743 if ( connection_write( wd ) < 0 ) {
1744 FD_CLR( (unsigned) wd, &readfds );
1750 for ( i = 0; i < readfds.fd_count; i++ )
1752 for ( i = 0; i < nfds; i++ )
1756 int is_listener = 0;
1759 rd = readfds.fd_array[i];
1761 if( ! FD_ISSET( i, &readfds ) ) {
1767 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1768 if ( rd == slap_listeners[l]->sl_sd ) {
1769 #ifdef LDAP_CONNECTIONLESS
1770 if (slap_listeners[l]->sl_is_udp)
1777 if ( is_listener ) {
1782 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1783 "slapd_daemon_task: read activity on %d\n", rd ));
1785 Debug ( LDAP_DEBUG_CONNS,
1786 "daemon: read activity on %d\n", rd, 0, 0 );
1789 * NOTE: it is possible that the connection was closed
1790 * and that the stream is now inactive.
1791 * connection_read() must valid the stream is still
1795 if ( connection_read( rd ) < 0 ) {
1799 ldap_pvt_thread_yield();
1802 if( slapd_shutdown == 1 ) {
1804 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1805 "slapd_daemon_task: shutdown requested and initiated.\n"));
1807 Debug( LDAP_DEBUG_TRACE,
1808 "daemon: shutdown requested and initiated.\n",
1812 } else if ( slapd_shutdown == 2 ) {
1813 #ifdef HAVE_NT_SERVICE_MANAGER
1815 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1816 "slapd_daemon_task: shutdown initiated by Service Manager.\n"));
1818 Debug( LDAP_DEBUG_TRACE,
1819 "daemon: shutdown initiated by Service Manager.\n",
1822 #else /* !HAVE_NT_SERVICE_MANAGER */
1824 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1825 "slapd_daemon_task: abnormal condition, shutdown initiated.\n" ));
1827 Debug( LDAP_DEBUG_TRACE,
1828 "daemon: abnormal condition, shutdown initiated.\n",
1831 #endif /* !HAVE_NT_SERVICE_MANAGER */
1834 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1835 "slapd_daemon_task: no active streams, shutdown initiated.\n" ));
1837 Debug( LDAP_DEBUG_TRACE,
1838 "daemon: no active streams, shutdown initiated.\n",
1843 if( slapd_gentle_shutdown != 2 )
1844 close_listeners ( 0 );
1845 free ( slap_listeners );
1846 slap_listeners = NULL;
1849 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1850 "slapd_daemon_task: shutdown waiting for %d threads to terminate.\n",
1851 ldap_pvt_thread_pool_backload(&connection_pool) ));
1853 Debug( LDAP_DEBUG_ANY,
1854 "slapd shutdown: waiting for %d threads to terminate\n",
1855 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1857 ldap_pvt_thread_pool_destroy(&connection_pool, 1);
1863 int slapd_daemon( void )
1869 #define SLAPD_LISTENER_THREAD 1
1870 #if defined( SLAPD_LISTENER_THREAD )
1872 ldap_pvt_thread_t listener_tid;
1874 /* listener as a separate THREAD */
1875 rc = ldap_pvt_thread_create( &listener_tid,
1876 0, slapd_daemon_task, NULL );
1880 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1881 "slapd_daemon: listener ldap_pvt_thread_create failed (%d).\n", rc ));
1883 Debug( LDAP_DEBUG_ANY,
1884 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
1889 /* wait for the listener thread to complete */
1890 ldap_pvt_thread_join( listener_tid, (void *) NULL );
1893 /* experimental code */
1894 slapd_daemon_task( NULL );
1903 #if defined( HAVE_WINSOCK2 )
1904 WORD wVersionRequested;
1908 wVersionRequested = MAKEWORD( 2, 0 );
1910 err = WSAStartup( wVersionRequested, &wsaData );
1912 /* Tell the user that we couldn't find a usable */
1917 /* Confirm that the WinSock DLL supports 2.0.*/
1918 /* Note that if the DLL supports versions greater */
1919 /* than 2.0 in addition to 2.0, it will still return */
1920 /* 2.0 in wVersion since that is the version we */
1923 if ( LOBYTE( wsaData.wVersion ) != 2 ||
1924 HIBYTE( wsaData.wVersion ) != 0 )
1926 /* Tell the user that we couldn't find a usable */
1932 /* The WinSock DLL is acceptable. Proceed. */
1933 #elif defined( HAVE_WINSOCK )
1935 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
1942 int sockdestroy(void)
1944 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
1951 slap_sig_shutdown( int sig )
1954 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1955 "slap_sig_shutdown: signal %d\n", sig ));
1957 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
1961 * If the NT Service Manager is controlling the server, we don't
1962 * want SIGBREAK to kill the server. For some strange reason,
1963 * SIGBREAK is generated when a user logs out.
1966 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
1967 if (is_NT_Service && sig == SIGBREAK)
1969 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1970 "slap_sig_shutdown: SIGBREAK ignored.\n" ));
1972 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: SIGBREAK ignored.\n",
1978 if (sig == SIGHUP && global_gentlehup && slapd_gentle_shutdown == 0)
1979 slapd_gentle_shutdown = 1;
1986 /* reinstall self */
1987 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
1991 slap_sig_wake( int sig )
1995 /* reinstall self */
1996 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
2000 void slapd_add_internal(ber_socket_t s) {
2004 Listener ** slapd_get_listeners(void) {
2005 return slap_listeners;