3 * Copyright 1998-2003 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"
29 #define SLAP_STRING_UNKNOWN STRING_UNKNOWN
31 int allow_severity = LOG_INFO;
32 int deny_severity = LOG_NOTICE;
33 #else /* ! TCP Wrappers */
34 #define SLAP_STRING_UNKNOWN "unknown"
35 #endif /* ! TCP Wrappers */
39 /* this should go in <ldap.h> as soon as it is accepted */
40 #define LDAPI_MOD_URLEXT "x-mod"
41 #endif /* LDAP_PF_LOCAL */
44 int slap_inet4or6 = AF_UNSPEC;
46 int slap_inet4or6 = AF_INET;
51 ber_socket_t dtblsize;
53 Listener **slap_listeners = NULL;
55 #define SLAPD_LISTEN 10
57 static ber_socket_t wake_sds[2];
59 #if defined(NO_THREADS) || defined(HAVE_GNU_PTH)
61 #define WAKE_LISTENER(w) \
62 ((w && !waking) ? tcp_write( wake_sds[1], "0", 1 ), waking=1 : 0)
64 #define WAKE_LISTENER(w) \
65 do { if (w) tcp_write( wake_sds[1], "0", 1 ); } while(0)
71 volatile sig_atomic_t slapd_shutdown = 0, slapd_gentle_shutdown = 0;
73 static struct slap_daemon {
74 ldap_pvt_thread_mutex_t sd_mutex;
76 ber_socket_t sd_nactives;
79 /* In winsock, accept() returns values higher than dtblsize
80 so don't bother with this optimization */
93 * SLP related functions
97 #define LDAP_SRVTYPE_PREFIX "service:ldap://"
98 #define LDAPS_SRVTYPE_PREFIX "service:ldaps://"
99 static char** slapd_srvurls = NULL;
100 static SLPHandle slapd_hslp = 0;
102 void slapd_slp_init( const char* urls ) {
105 slapd_srvurls = ldap_str2charray( urls, " " );
107 if( slapd_srvurls == NULL ) return;
109 /* find and expand INADDR_ANY URLs */
110 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
111 if( strcmp( slapd_srvurls[i], "ldap:///" ) == 0) {
112 char *host = ldap_pvt_get_fqdn( NULL );
113 if ( host != NULL ) {
114 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
116 sizeof( LDAP_SRVTYPE_PREFIX ) );
117 strcpy( lutil_strcopy(slapd_srvurls[i],
118 LDAP_SRVTYPE_PREFIX ), host );
123 } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0) {
124 char *host = ldap_pvt_get_fqdn( NULL );
125 if ( host != NULL ) {
126 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
128 sizeof( LDAPS_SRVTYPE_PREFIX ) );
129 strcpy( lutil_strcopy(slapd_srvurls[i],
130 LDAPS_SRVTYPE_PREFIX ), host );
137 /* open the SLP handle */
138 SLPOpen( "en", 0, &slapd_hslp );
141 void slapd_slp_deinit() {
142 if( slapd_srvurls == NULL ) return;
144 ldap_charray_free( slapd_srvurls );
145 slapd_srvurls = NULL;
147 /* close the SLP handle */
148 SLPClose( slapd_hslp );
151 void slapd_slp_regreport(
159 void slapd_slp_reg() {
162 if( slapd_srvurls == NULL ) return;
164 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
165 if( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
166 sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
167 strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
168 sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
172 SLP_LIFETIME_MAXIMUM,
182 void slapd_slp_dereg() {
185 if( slapd_srvurls == NULL ) return;
187 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
188 SLPDereg( slapd_hslp,
194 #endif /* HAVE_SLP */
197 * Add a descriptor to daemon control
199 static void slapd_add(ber_socket_t s) {
200 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
202 assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
203 assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
204 assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
207 if (s >= slap_daemon.sd_nfds) {
208 slap_daemon.sd_nfds = s + 1;
212 slap_daemon.sd_nactives++;
214 FD_SET( s, &slap_daemon.sd_actives );
215 FD_SET( s, &slap_daemon.sd_readers );
218 LDAP_LOG( CONNECTION, DETAIL1,
219 "slapd_add: added %ld%s%s\n", (long)s,
220 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
221 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
223 Debug( LDAP_DEBUG_CONNS, "daemon: added %ld%s%s\n",
225 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
226 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
228 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
232 * Remove the descriptor from daemon control
234 void slapd_remove(ber_socket_t s, int wake) {
235 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
237 slap_daemon.sd_nactives--;
240 LDAP_LOG( CONNECTION, DETAIL1,
241 "slapd_remove: removing %ld%s%s\n", (long) s,
242 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
243 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
245 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
247 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
248 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
250 FD_CLR( s, &slap_daemon.sd_actives );
251 FD_CLR( s, &slap_daemon.sd_readers );
252 FD_CLR( s, &slap_daemon.sd_writers );
254 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
255 WAKE_LISTENER(wake || slapd_gentle_shutdown == 2);
258 void slapd_clr_write(ber_socket_t s, int wake) {
259 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
261 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
262 FD_CLR( s, &slap_daemon.sd_writers );
264 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
268 void slapd_set_write(ber_socket_t s, int wake) {
269 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
271 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
272 if (!FD_ISSET(s, &slap_daemon.sd_writers))
273 FD_SET( (unsigned) s, &slap_daemon.sd_writers );
275 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
279 void slapd_clr_read(ber_socket_t s, int wake) {
280 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
282 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
283 FD_CLR( s, &slap_daemon.sd_readers );
285 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
289 void slapd_set_read(ber_socket_t s, int wake) {
290 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
292 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
293 if (!FD_ISSET(s, &slap_daemon.sd_readers))
294 FD_SET( s, &slap_daemon.sd_readers );
296 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
300 static void slapd_close(ber_socket_t s) {
302 LDAP_LOG( CONNECTION, DETAIL1, "slapd_close: closing %ld\n", (long)s, 0, 0);
304 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
310 static void slap_free_listener_addresses(struct sockaddr **sal)
312 struct sockaddr **sap;
318 for (sap = sal; *sap != NULL; sap++) {
325 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
326 static int get_url_perms(
338 for ( i = 0; exts[ i ]; i++ ) {
339 char *type = exts[ i ];
342 if ( type[ 0 ] == '!' ) {
347 if ( strncasecmp( type, LDAPI_MOD_URLEXT "=", sizeof(LDAPI_MOD_URLEXT "=") - 1 ) == 0 ) {
349 + ( sizeof(LDAPI_MOD_URLEXT "=") - 1 );
353 switch (strlen(value)) {
355 /* skip leading '0' */
356 if ( value[ 0 ] != '0' ) {
362 for ( j = 0; j < 3; j++) {
365 v = value[ j ] - '0';
367 if ( v < 0 || v > 7 ) {
376 for ( j = 1; j < 10; j++ ) {
377 static mode_t m[] = { 0,
378 S_IRUSR, S_IWUSR, S_IXUSR,
379 S_IRGRP, S_IWGRP, S_IXGRP,
380 S_IROTH, S_IWOTH, S_IXOTH
382 static char c[] = "-rwxrwxrwx";
384 if ( value[ j ] == c[ j ] ) {
387 } else if ( value[ j ] != '-' ) {
406 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
408 /* port = 0 indicates AF_LOCAL */
409 static int slap_get_listener_addresses(
412 struct sockaddr ***sal)
414 struct sockaddr **sap;
418 *sal = ch_malloc(2 * sizeof(void *));
424 *sap = ch_malloc(sizeof(struct sockaddr_un));
430 (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) ) {
432 LDAP_LOG( CONNECTION, INFO,
433 "slap_get_listener_addresses: domain socket path (%s) "
434 "too long in URL\n", host, 0, 0 );
436 Debug( LDAP_DEBUG_ANY,
437 "daemon: domain socket path (%s) too long in URL",
443 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
444 (*sap)->sa_family = AF_LOCAL;
445 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
449 #ifdef HAVE_GETADDRINFO
450 struct addrinfo hints, *res, *sai;
454 memset( &hints, '\0', sizeof(hints) );
455 hints.ai_flags = AI_PASSIVE;
456 hints.ai_socktype = SOCK_STREAM;
457 hints.ai_family = slap_inet4or6;
458 snprintf(serv, sizeof serv, "%d", port);
460 if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
462 LDAP_LOG( CONNECTION, INFO,
463 "slap_get_listener_addresses: getaddrinfo failed: %s\n",
464 AC_GAI_STRERROR(err), 0, 0 );
466 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
467 AC_GAI_STRERROR(err), 0, 0);
473 for (n=2; (sai = sai->ai_next) != NULL; n++) {
476 *sal = ch_calloc(n, sizeof(void *));
484 for ( sai=res; sai; sai=sai->ai_next ) {
485 if( sai->ai_addr == NULL ) {
487 LDAP_LOG( CONNECTION, INFO,
488 "slap_get_listener_addresses: "
489 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
491 Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
492 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
498 switch (sai->ai_family) {
499 # ifdef LDAP_PF_INET6
501 *sap = ch_malloc(sizeof(struct sockaddr_in6));
506 *(struct sockaddr_in6 *)*sap =
507 *((struct sockaddr_in6 *)sai->ai_addr);
511 *sap = ch_malloc(sizeof(struct sockaddr_in));
516 *(struct sockaddr_in *)*sap =
517 *((struct sockaddr_in *)sai->ai_addr);
525 (*sap)->sa_family = sai->ai_family;
535 struct hostent *he = NULL;
537 if ( host == NULL ) {
538 in.s_addr = htonl(INADDR_ANY);
540 } else if ( !inet_aton( host, &in ) ) {
541 he = gethostbyname( host );
544 LDAP_LOG( CONNECTION, INFO,
545 "slap_get_listener_addresses: invalid host %s\n", host, 0, 0 );
547 Debug( LDAP_DEBUG_ANY,
548 "daemon: invalid host %s", host, 0, 0);
552 for (n = 0; he->h_addr_list[n]; n++) ;
555 *sal = ch_malloc((n+1) * sizeof(void *));
561 for ( i = 0; i<n; i++ ) {
562 sap[i] = ch_malloc(sizeof(struct sockaddr_in));
566 (void)memset( (void *)sap[i], '\0', sizeof(struct sockaddr_in) );
567 sap[i]->sa_family = AF_INET;
568 ((struct sockaddr_in *)sap[i])->sin_port = htons(port);
570 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr, he->h_addr_list[i], sizeof(struct in_addr) );
572 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr, &in, sizeof(struct in_addr) );
582 slap_free_listener_addresses(*sal);
586 static int slap_open_listener(
597 int err, addrlen = 0;
598 struct sockaddr **sal, **psal;
599 int socktype = SOCK_STREAM; /* default to COTS */
601 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
606 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
608 rc = ldap_url_parse( url, &lud );
610 if( rc != LDAP_URL_SUCCESS ) {
612 LDAP_LOG( CONNECTION, ERR,
613 "slap_open_listener: listen URL \"%s\" parse error %d\n",
616 Debug( LDAP_DEBUG_ANY,
617 "daemon: listen URL \"%s\" parse error=%d\n",
623 l.sl_url.bv_val = NULL;
626 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
628 LDAP_LOG( CONNECTION, INFO,
629 "slap_open_listener: TLS is not supported (%s)\n", url, 0, 0 );
631 Debug( LDAP_DEBUG_ANY,
632 "daemon: TLS not supported (%s)\n",
635 ldap_free_urldesc( lud );
639 if(! lud->lud_port ) {
640 lud->lud_port = LDAP_PORT;
644 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
646 if(! lud->lud_port ) {
647 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
651 port = (unsigned short) lud->lud_port;
653 tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme);
654 if ( tmp == LDAP_PROTO_IPC ) {
656 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
657 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
659 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
664 LDAP_LOG( CONNECTION, INFO,
665 "slap_open_listener: URL scheme is not supported: %s\n", url, 0, 0 );
667 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
670 ldap_free_urldesc( lud );
674 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
675 || strcmp(lud->lud_host, "*") == 0 )
677 err = slap_get_listener_addresses(NULL, port, &sal);
679 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
682 #ifdef LDAP_CONNECTIONLESS
683 l.sl_is_udp = ( tmp == LDAP_PROTO_UDP );
686 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
687 if ( lud->lud_exts ) {
688 err = get_url_perms( lud->lud_exts, &l.sl_perms, &crit );
690 l.sl_perms = S_IRWXU | S_IRWXO;
692 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
694 ldap_free_urldesc( lud );
699 /* If we got more than one address returned, we need to make space
700 * for it in the slap_listeners array.
702 for ( num=0; sal[num]; num++ );
705 slap_listeners = ch_realloc( slap_listeners, (*listeners + 1) * sizeof(Listener *) );
709 while ( *sal != NULL ) {
710 switch( (*sal)->sa_family ) {
723 #ifdef LDAP_CONNECTIONLESS
724 if( l.sl_is_udp ) socktype = SOCK_DGRAM;
726 l.sl_sd = socket( (*sal)->sa_family, socktype, 0);
727 if ( l.sl_sd == AC_SOCKET_INVALID ) {
728 int err = sock_errno();
730 LDAP_LOG( CONNECTION, ERR,
731 "slap_open_listener: socket() failed errno=%d (%s)\n",
732 err, sock_errstr(err), 0 );
734 Debug( LDAP_DEBUG_ANY,
735 "daemon: socket() failed errno=%d (%s)\n", err,
736 sock_errstr(err), 0 );
742 if ( l.sl_sd >= dtblsize ) {
744 LDAP_LOG( CONNECTION, ERR,
745 "slap_open_listener: listener descriptor %ld is too "
746 "great %ld\n", (long)l.sl_sd, (long)dtblsize, 0 );
748 Debug( LDAP_DEBUG_ANY,
749 "daemon: listener descriptor %ld is too great %ld\n",
750 (long) l.sl_sd, (long) dtblsize, 0 );
752 tcp_close( l.sl_sd );
758 if ( (*sal)->sa_family == AF_LOCAL ) {
759 unlink ( ((struct sockaddr_un *)*sal)->sun_path );
764 /* enable address reuse */
766 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
767 (char *) &tmp, sizeof(tmp) );
768 if ( rc == AC_SOCKET_ERROR ) {
769 int err = sock_errno();
771 LDAP_LOG( CONNECTION, INFO,
772 "slap_open_listener: setsockopt( %ld, SO_REUSEADDR ) "
773 "failed errno %d (%s)\n", (long)l.sl_sd, err,
776 Debug( LDAP_DEBUG_ANY,
777 "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
778 (long) l.sl_sd, err, sock_errstr(err) );
784 switch( (*sal)->sa_family ) {
786 addrlen = sizeof(struct sockaddr_in);
791 /* Try to use IPv6 sockets for IPv6 only */
793 rc = setsockopt( l.sl_sd, IPPROTO_IPV6, IPV6_V6ONLY,
794 (char *) &tmp, sizeof(tmp) );
795 if ( rc == AC_SOCKET_ERROR ) {
796 int err = sock_errno();
798 LDAP_LOG( CONNECTION, INFO,
799 "slap_open_listener: setsockopt( %ld, IPV6_V6ONLY ) failed errno %d (%s)\n",
800 (long)l.sl_sd, err, sock_errstr(err) );
802 Debug( LDAP_DEBUG_ANY,
803 "slapd(%ld): setsockopt(IPV6_V6ONLY) failed errno=%d (%s)\n",
804 (long) l.sl_sd, err, sock_errstr(err) );
808 addrlen = sizeof(struct sockaddr_in6);
813 addrlen = sizeof(struct sockaddr_un);
818 if (bind(l.sl_sd, *sal, addrlen)) {
821 LDAP_LOG( CONNECTION, INFO,
822 "slap_open_listener: bind(%ld) failed errno=%d (%s)\n",
823 (long)l.sl_sd, err, sock_errstr(err) );
825 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
826 (long) l.sl_sd, err, sock_errstr(err) );
828 tcp_close( l.sl_sd );
833 switch ( (*sal)->sa_family ) {
836 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
837 #if 0 /* don't muck with socket perms */
838 if ( chmod( addr, l.sl_perms ) < 0 && crit ) {
839 int err = sock_errno();
841 LDAP_LOG( CONNECTION, INFO,
842 "slap_open_listener: fchmod(%ld) failed errno=%d (%s)\n",
843 (long)l.sl_sd, err, sock_errstr(err) );
845 Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
846 (long) l.sl_sd, err, sock_errstr(err) );
848 tcp_close( l.sl_sd );
849 slap_free_listener_addresses(psal);
853 l.sl_name.bv_len = strlen(addr) + sizeof("PATH=") - 1;
854 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len + 1 );
855 snprintf( l.sl_name.bv_val, l.sl_name.bv_len + 1,
858 #endif /* LDAP_PF_LOCAL */
862 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
863 char addr[INET_ADDRSTRLEN];
864 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
865 addr, sizeof(addr) );
868 s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr );
870 port = ntohs( ((struct sockaddr_in *)*sal) ->sin_port );
871 l.sl_name.bv_val = ber_memalloc( sizeof("IP=255.255.255.255:65535") );
872 snprintf( l.sl_name.bv_val, sizeof("IP=255.255.255.255:65535"),
874 s != NULL ? s : SLAP_STRING_UNKNOWN, port );
875 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
880 char addr[INET6_ADDRSTRLEN];
881 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
883 port = ntohs( ((struct sockaddr_in6 *)*sal)->sin6_port );
884 l.sl_name.bv_len = strlen(addr) + sizeof("IP= 65535");
885 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len );
886 snprintf( l.sl_name.bv_val, l.sl_name.bv_len, "IP=%s %d",
888 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
890 #endif /* LDAP_PF_INET6 */
894 LDAP_LOG( CONNECTION, INFO,
895 "slap_open_listener: unsupported address family (%d)\n",
896 (int)(*sal)->sa_family, 0, 0 );
898 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
899 (int) (*sal)->sa_family, 0, 0 );
904 AC_MEMCPY(&l.sl_sa, *sal, addrlen);
905 ber_str2bv( url, 0, 1, &l.sl_url);
906 li = ch_malloc( sizeof( Listener ) );
908 slap_listeners[*cur] = li;
912 } /* while ( *sal != NULL ) */
914 slap_free_listener_addresses(psal);
916 if ( l.sl_url.bv_val == NULL )
919 LDAP_LOG( CONNECTION, RESULTS,
920 "slap_open_listener: failed on %s\n", url, 0, 0 );
922 Debug( LDAP_DEBUG_TRACE,
923 "slap_open_listener: failed on %s\n", url, 0, 0 );
929 LDAP_LOG( CONNECTION, RESULTS,
930 "slap_open_listener: daemon initialized %s\n",
931 l.sl_url.bv_val, 0, 0 );
933 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
934 l.sl_url.bv_val, 0, 0 );
939 static int sockinit(void);
940 static int sockdestroy(void);
942 int slapd_daemon_init( const char *urls )
948 LDAP_LOG( CONNECTION, ARGS,
949 "slapd_daemon_init: %s\n", urls ? urls : "<null>", 0, 0 );
951 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
952 urls ? urls : "<null>", 0, 0 );
954 if( (rc = sockinit()) != 0 ) {
959 dtblsize = sysconf( _SC_OPEN_MAX );
960 #elif HAVE_GETDTABLESIZE
961 dtblsize = getdtablesize();
963 dtblsize = FD_SETSIZE;
967 if(dtblsize > FD_SETSIZE) {
968 dtblsize = FD_SETSIZE;
970 #endif /* !FD_SETSIZE */
972 /* open a pipe (or something equivalent connected to itself).
973 * we write a byte on this fd whenever we catch a signal. The main
974 * loop will be select'ing on this socket, and will wake up when
977 if( (rc = lutil_pair( wake_sds )) < 0 ) {
979 LDAP_LOG( CONNECTION, ERR,
980 "slap_daemon_init: lutil_pair() failed rc=%d\n", rc, 0, 0);
982 Debug( LDAP_DEBUG_ANY,
983 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
988 FD_ZERO( &slap_daemon.sd_readers );
989 FD_ZERO( &slap_daemon.sd_writers );
995 u = ldap_str2charray( urls, " " );
997 if( u == NULL || u[0] == NULL ) {
999 LDAP_LOG( CONNECTION, ERR,
1000 "slap_daemon_init: no urls (%s) provided.\n", urls, 0, 0 );
1002 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
1008 for( i=0; u[i] != NULL; i++ ) {
1010 LDAP_LOG( CONNECTION, DETAIL1,
1011 "slap_daemon_init: listen on %s\n.", u[i], 0, 0 );
1013 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
1020 LDAP_LOG( CONNECTION, INFO,
1021 "slap_daemon_init: no listeners to open (%s)\n", urls, 0, 0 );
1023 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
1026 ldap_charray_free( u );
1031 LDAP_LOG( CONNECTION, INFO,
1032 "slap_daemon_init: %d listeners to open...\n", i, 0, 0 );
1034 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
1037 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
1039 for(n = 0, j = 0; u[n]; n++ ) {
1040 if ( slap_open_listener( u[n], &i, &j ) ) {
1041 ldap_charray_free( u );
1045 slap_listeners[j] = NULL;
1048 LDAP_LOG( CONNECTION, DETAIL1,
1049 "slap_daemon_init: %d listeners opened\n", i, 0, 0 );
1051 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
1056 slapd_slp_init( urls );
1060 ldap_charray_free( u );
1061 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
1067 slapd_daemon_destroy(void)
1069 connections_destroy();
1070 tcp_close( wake_sds[1] );
1071 tcp_close( wake_sds[0] );
1090 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1091 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1093 slapd_remove( slap_listeners[l]->sl_sd, 0 );
1094 #ifdef LDAP_PF_LOCAL
1095 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1096 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1098 #endif /* LDAP_PF_LOCAL */
1099 slapd_close( slap_listeners[l]->sl_sd );
1101 if ( slap_listeners[l]->sl_url.bv_val )
1102 ber_memfree( slap_listeners[l]->sl_url.bv_val );
1103 if ( slap_listeners[l]->sl_name.bv_val )
1104 ber_memfree( slap_listeners[l]->sl_name.bv_val );
1105 free ( slap_listeners[l] );
1106 slap_listeners[l] = NULL;
1117 time_t last_idle_check = 0;
1120 if ( global_idletimeout > 0 ) {
1121 last_idle_check = slap_get_time();
1123 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1124 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1126 #ifdef LDAP_CONNECTIONLESS
1127 /* Since this is connectionless, the data port is the
1128 * listening port. The listen() and accept() calls
1131 if ( slap_listeners[l]->sl_is_udp ) {
1132 slapd_add( slap_listeners[l]->sl_sd );
1137 if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN ) == -1 ) {
1138 int err = sock_errno();
1140 #ifdef LDAP_PF_INET6
1141 /* If error is EADDRINUSE, we are trying to listen to INADDR_ANY and
1142 * we are already listening to in6addr_any, then we want to ignore
1143 * this and continue.
1145 if ( err == EADDRINUSE ) {
1147 struct sockaddr_in sa = slap_listeners[l]->sl_sa.sa_in_addr;
1148 struct sockaddr_in6 sa6;
1150 if ( sa.sin_family == AF_INET &&
1151 sa.sin_addr.s_addr == htonl(INADDR_ANY) ) {
1152 for ( i = 0 ; i < l; i++ ) {
1153 sa6 = slap_listeners[i]->sl_sa.sa_in6_addr;
1154 if ( sa6.sin6_family == AF_INET6 &&
1155 !memcmp( &sa6.sin6_addr, &in6addr_any, sizeof(struct in6_addr) ) )
1160 /* We are already listening to in6addr_any */
1162 LDAP_LOG(CONNECTION, WARNING,
1163 "slapd_daemon_task: Attempt to listen to 0.0.0.0 failed, already listening on ::, assuming IPv4 included\n", 0, 0, 0 );
1165 Debug( LDAP_DEBUG_CONNS,
1166 "daemon: Attempt to listen to 0.0.0.0 failed, already listening on ::, assuming IPv4 included\n",
1169 slapd_close( slap_listeners[l]->sl_sd );
1170 slap_listeners[l]->sl_sd = AC_SOCKET_INVALID;
1177 LDAP_LOG( CONNECTION, ERR,
1178 "slapd_daemon_task: listen( %s, 5 ) failed errno=%d (%s)\n",
1179 slap_listeners[l]->sl_url.bv_val, err, sock_errstr(err) );
1181 Debug( LDAP_DEBUG_ANY,
1182 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
1183 slap_listeners[l]->sl_url.bv_val, err,
1186 return( (void*)-1 );
1189 slapd_add( slap_listeners[l]->sl_sd );
1192 #ifdef HAVE_NT_SERVICE_MANAGER
1193 if ( started_event != NULL ) {
1194 ldap_pvt_thread_cond_signal( &started_event );
1197 /* initialization complete. Here comes the loop. */
1199 while ( !slapd_shutdown ) {
1204 #define SLAPD_EBADF_LIMIT 16
1208 #define SLAPD_IDLE_CHECK_LIMIT 4
1216 struct timeval zero;
1217 struct timeval *tvp;
1219 #ifdef LDAP_SYNCREPL
1220 struct timeval diff;
1221 struct timeval *cat;
1226 now = slap_get_time();
1229 connections_timeout_idle( now );
1231 } else if ( global_idletimeout > 0 ) {
1232 if ( difftime( last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 ) {
1233 connections_timeout_idle( now );
1238 if( slapd_gentle_shutdown ) {
1239 ber_socket_t active;
1241 if( slapd_gentle_shutdown == 1 ) {
1242 Debug( LDAP_DEBUG_ANY, "slapd gentle shutdown\n", 0, 0, 0 );
1243 close_listeners( 1 );
1244 global_restrictops |= SLAP_RESTRICT_OP_WRITES;
1245 slapd_gentle_shutdown = 2;
1248 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1249 active = slap_daemon.sd_nactives;
1250 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1258 FD_ZERO( &writefds );
1259 FD_ZERO( &readfds );
1264 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1266 #ifdef FD_SET_MANUAL_COPY
1267 for( s = 0; s < nfds; s++ ) {
1268 if(FD_ISSET( &slap_sd_readers, s )) {
1269 FD_SET( s, &readfds );
1271 if(FD_ISSET( &slap_sd_writers, s )) {
1272 FD_SET( s, &writefds );
1276 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
1277 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
1279 assert(!FD_ISSET(wake_sds[0], &readfds));
1280 FD_SET( wake_sds[0], &readfds );
1282 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1283 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1285 if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
1286 FD_SET( slap_listeners[l]->sl_sd, &readfds );
1289 #ifndef HAVE_WINSOCK
1290 nfds = slap_daemon.sd_nfds;
1295 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1297 at = ldap_pvt_thread_pool_backload(&connection_pool);
1299 #ifdef LDAP_SYNCREPL
1300 /* cat is of struct timeval containing the earliest schedule */
1301 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
1302 rtask = ldap_pvt_runqueue_next_sched( &syncrepl_rq, &cat );
1303 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
1304 if ( cat != NULL ) {
1305 diff.tv_sec = cat->tv_sec - slap_get_time();
1306 if ( diff.tv_sec == 0 )
1307 diff.tv_sec = tdelta;
1311 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
1314 tvp = at ? &zero : NULL;
1317 #ifdef LDAP_SYNCREPL
1318 if ( cat != NULL ) {
1319 if ( tvp == NULL ) {
1325 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1326 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1330 LDAP_LOG( CONNECTION, DETAIL1,
1331 "slapd_daemon_task: select: listen=%d "
1332 "active_threads=%d tvp=%s\n",
1333 slap_listeners[l]->sl_sd, at, tvp == NULL ? "NULL" : "zero" );
1335 Debug( LDAP_DEBUG_CONNS,
1336 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
1337 slap_listeners[l]->sl_sd, at,
1338 tvp == NULL ? "NULL" : "zero" );
1342 switch(ns = select( nfds, &readfds,
1344 /* don't pass empty fd_set */
1345 ( writefds.fd_count > 0 ? &writefds : NULL ),
1351 case -1: { /* failure - try again */
1352 int err = sock_errno();
1356 /* you'd think this would be EBADF */
1357 || err == WSAENOTSOCK
1360 if (++ebadf < SLAPD_EBADF_LIMIT)
1364 if( err != EINTR ) {
1366 LDAP_LOG( CONNECTION, INFO,
1367 "slapd_daemon_task: select failed (%d): %s\n",
1368 err, sock_errstr(err), 0 );
1370 Debug( LDAP_DEBUG_CONNS,
1371 "daemon: select failed (%d): %s\n",
1372 err, sock_errstr(err), 0 );
1379 case 0: /* timeout - let threads run */
1382 LDAP_LOG( CONNECTION, DETAIL2,
1383 "slapd_daemon_task: select timeout - yielding\n", 0, 0, 0 );
1385 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
1389 #ifdef LDAP_SYNCREPL
1390 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
1391 rtask = ldap_pvt_runqueue_next_sched( &syncrepl_rq, &cat );
1392 if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
1393 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask );
1394 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
1395 } else if ( cat && cat->tv_sec && cat->tv_sec <= slap_get_time() ) {
1396 ldap_pvt_runqueue_runtask( &syncrepl_rq, rtask );
1397 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask );
1398 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
1399 ldap_pvt_thread_pool_submit( &connection_pool,
1400 do_syncrepl, (void *) rtask );
1402 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
1405 ldap_pvt_thread_yield();
1408 default: /* something happened - deal with it */
1409 if( slapd_shutdown ) continue;
1413 LDAP_LOG( CONNECTION, DETAIL2,
1414 "slapd_daemon_task: activity on %d descriptors\n", ns, 0, 0 );
1416 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
1422 if( FD_ISSET( wake_sds[0], &readfds ) ) {
1424 tcp_read( wake_sds[0], c, sizeof(c) );
1425 #if defined(NO_THREADS) || defined(HAVE_GNU_PTH)
1431 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1433 socklen_t len = sizeof(from);
1436 char *authid = NULL;
1437 #ifdef SLAPD_RLOOKUPS
1438 char hbuf[NI_MAXHOST];
1441 char *dnsname = NULL;
1442 char *peeraddr = NULL;
1443 #ifdef LDAP_PF_LOCAL
1444 char peername[MAXPATHLEN + sizeof("PATH=")];
1445 #elif defined(LDAP_PF_INET6)
1446 char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
1448 char peername[sizeof("IP=255.255.255.255:65336")];
1449 #endif /* LDAP_PF_LOCAL */
1453 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1456 if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
1459 #ifdef LDAP_CONNECTIONLESS
1460 if ( slap_listeners[l]->sl_is_udp ) {
1461 /* The first time we receive a query, we set this
1462 * up as a "connection". It remains open for the life
1465 if ( slap_listeners[l]->sl_is_udp < 2 ) {
1466 id = connection_init(
1467 slap_listeners[l]->sl_sd,
1468 slap_listeners[l], "", "",
1470 slap_listeners[l]->sl_is_udp++;
1476 s = accept( slap_listeners[l]->sl_sd,
1477 (struct sockaddr *) &from, &len );
1478 if ( s == AC_SOCKET_INVALID ) {
1479 int err = sock_errno();
1482 if( err == EMFILE ) {
1487 if( err == ENFILE ) {
1497 LDAP_LOG( CONNECTION, ERR,
1498 "slapd_daemon_task: accept(%ld) failed errno=%d (%s)\n",
1499 (long)slap_listeners[l]->sl_sd,
1500 err, sock_errstr(err) );
1502 Debug( LDAP_DEBUG_ANY,
1503 "daemon: accept(%ld) failed errno=%d (%s)\n",
1504 (long) slap_listeners[l]->sl_sd, err,
1508 /* prevent busy loop */
1510 if( emfile % 4 == 3 ) usleep( 250 );
1512 if( emfile % 8 == 7 ) sleep( 1 );
1516 ldap_pvt_thread_yield();
1521 #ifndef HAVE_WINSOCK
1522 /* make sure descriptor number isn't too great */
1523 if ( s >= dtblsize ) {
1525 LDAP_LOG( CONNECTION, ERR,
1526 "slapd_daemon_task: %ld beyond descriptor table size %ld\n",
1527 (long)s, (long)dtblsize, 0 );
1529 Debug( LDAP_DEBUG_ANY,
1530 "daemon: %ld beyond descriptor table size %ld\n",
1531 (long) s, (long) dtblsize, 0 );
1535 ldap_pvt_thread_yield();
1541 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1543 /* newly accepted stream should not be in any of the FD SETS */
1544 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
1545 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
1546 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
1548 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1551 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1552 #ifdef LDAP_PF_LOCAL
1553 /* for IPv4 and IPv6 sockets only */
1554 if ( from.sa_addr.sa_family != AF_LOCAL )
1555 #endif /* LDAP_PF_LOCAL */
1560 /* enable keep alives */
1562 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1563 (char *) &tmp, sizeof(tmp) );
1564 if ( rc == AC_SOCKET_ERROR ) {
1565 int err = sock_errno();
1567 LDAP_LOG( CONNECTION, ERR,
1568 "slapd_daemon_task: setsockopt( %ld, SO_KEEPALIVE)"
1569 " failed errno=%d (%s)\n",
1570 (long)s, err, sock_errstr(err) );
1572 Debug( LDAP_DEBUG_ANY,
1573 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1574 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1579 /* enable no delay */
1581 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1582 (char *)&tmp, sizeof(tmp) );
1583 if ( rc == AC_SOCKET_ERROR ) {
1584 int err = sock_errno();
1586 LDAP_LOG( CONNECTION, ERR,
1587 "slapd_daemon_task: setsockopt( %ld, "
1588 "TCP_NODELAY) failed errno=%d (%s)\n",
1589 (long)s, err, sock_errstr(err) );
1591 Debug( LDAP_DEBUG_ANY,
1592 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1593 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1601 LDAP_LOG( CONNECTION, DETAIL1,
1602 "slapd_daemon_task: new connection on %ld\n", (long)s, 0, 0 );
1604 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
1607 switch ( from.sa_addr.sa_family ) {
1608 # ifdef LDAP_PF_LOCAL
1610 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1611 ssf = LDAP_PVT_SASL_LOCAL_SSF;
1616 if( getpeereid( s, &uid, &gid ) == 0 ) {
1618 sizeof("uidnumber=4294967295+gidnumber=4294967295,"
1619 "cn=peercred,cn=external,cn=auth"));
1620 sprintf(authid, "uidnumber=%d+gidnumber=%d,"
1621 "cn=peercred,cn=external,cn=auth",
1622 (int) uid, (int) gid);
1627 #endif /* LDAP_PF_LOCAL */
1629 # ifdef LDAP_PF_INET6
1631 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1632 peeraddr = inet_ntoa( *((struct in_addr *)
1633 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1634 sprintf( peername, "IP=%s:%d",
1635 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1636 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1638 char addr[INET6_ADDRSTRLEN];
1640 peeraddr = (char *) inet_ntop( AF_INET6,
1641 &from.sa_in6_addr.sin6_addr,
1642 addr, sizeof addr );
1643 sprintf( peername, "IP=%s %d",
1644 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1645 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1648 # endif /* LDAP_PF_INET6 */
1651 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1652 sprintf( peername, "IP=%s:%d",
1653 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1654 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1662 if ( ( from.sa_addr.sa_family == AF_INET )
1663 #ifdef LDAP_PF_INET6
1664 || ( from.sa_addr.sa_family == AF_INET6 )
1667 #ifdef SLAPD_RLOOKUPS
1668 if ( use_reverse_lookup ) {
1670 if (ldap_pvt_get_hname( (const struct sockaddr *)&from, len, hbuf,
1671 sizeof(hbuf), &herr ) == 0) {
1672 ldap_pvt_str2lower( hbuf );
1678 #endif /* SLAPD_RLOOKUPS */
1681 if ( !hosts_ctl("slapd",
1682 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1683 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1684 SLAP_STRING_UNKNOWN ))
1687 Statslog( LDAP_DEBUG_STATS,
1688 "fd=%ld DENIED from %s (%s)",
1690 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1691 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1696 #endif /* HAVE_TCPD */
1699 id = connection_init(s,
1701 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1704 slap_listeners[l]->sl_is_tls,
1711 if( authid ) ch_free(authid);
1715 LDAP_LOG( CONNECTION, INFO,
1716 "slapd_daemon_task: "
1717 "connection_init(%ld, %s, %s) "
1720 slap_listeners[l]->sl_name.bv_val );
1722 Debug( LDAP_DEBUG_ANY,
1723 "daemon: connection_init(%ld, %s, %s) "
1727 slap_listeners[l]->sl_name.bv_val );
1733 Statslog( LDAP_DEBUG_STATS,
1734 "conn=%ld fd=%ld ACCEPT from %s (%s)\n",
1737 slap_listeners[l]->sl_name.bv_val,
1746 LDAP_LOG( CONNECTION, DETAIL2,
1747 "slapd_daemon_task: activity on ", 0, 0, 0 );
1749 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1752 for ( i = 0; i < readfds.fd_count; i++ ) {
1754 LDAP_LOG( CONNECTION, DETAIL2,
1755 " %d%s", readfds.fd_array[i], "r", 0, 0 );
1757 Debug( LDAP_DEBUG_CONNS, " %d%s",
1758 readfds.fd_array[i], "r", 0 );
1761 for ( i = 0; i < writefds.fd_count; i++ ) {
1763 LDAP_LOG( CONNECTION, DETAIL2,
1764 " %d%s", writefds.fd_array[i], "w" , 0 );
1766 Debug( LDAP_DEBUG_CONNS, " %d%s",
1767 writefds.fd_array[i], "w", 0 );
1772 for ( i = 0; i < nfds; i++ ) {
1774 int is_listener = 0;
1776 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1777 if ( i == slap_listeners[l]->sl_sd ) {
1778 #ifdef LDAP_CONNECTIONLESS
1779 /* The listener is the data port. Don't
1782 if (slap_listeners[l]->sl_is_udp) continue;
1788 if ( is_listener ) {
1791 r = FD_ISSET( i, &readfds );
1792 w = FD_ISSET( i, &writefds );
1795 LDAP_LOG( CONNECTION, DETAIL2,
1796 " %d%s%s", i, r ? "r" : "", w ? "w" : "" );
1798 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1799 r ? "r" : "", w ? "w" : "" );
1805 LDAP_LOG( CONNECTION, DETAIL2, "\n", 0, 0, 0 );
1807 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1812 /* loop through the writers */
1814 for ( i = 0; i < writefds.fd_count; i++ )
1816 for ( i = 0; i < nfds; i++ )
1820 int is_listener = 0;
1822 wd = writefds.fd_array[i];
1824 if( ! FD_ISSET( i, &writefds ) ) {
1830 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1831 if ( i == slap_listeners[l]->sl_sd ) {
1832 #ifdef LDAP_CONNECTIONLESS
1833 if (slap_listeners[l]->sl_is_udp) continue;
1839 if ( is_listener ) {
1843 LDAP_LOG( CONNECTION, DETAIL2,
1844 "slapd_daemon_task: write active on %d\n", wd, 0, 0 );
1846 Debug( LDAP_DEBUG_CONNS,
1847 "daemon: write active on %d\n",
1851 * NOTE: it is possible that the connection was closed
1852 * and that the stream is now inactive.
1853 * connection_write() must valid the stream is still
1857 if ( connection_write( wd ) < 0 ) {
1858 FD_CLR( (unsigned) wd, &readfds );
1864 for ( i = 0; i < readfds.fd_count; i++ )
1866 for ( i = 0; i < nfds; i++ )
1870 int is_listener = 0;
1873 rd = readfds.fd_array[i];
1875 if( ! FD_ISSET( i, &readfds ) ) {
1881 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1882 if ( rd == slap_listeners[l]->sl_sd ) {
1883 #ifdef LDAP_CONNECTIONLESS
1884 if (slap_listeners[l]->sl_is_udp) continue;
1890 if ( is_listener ) {
1895 LDAP_LOG( CONNECTION, DETAIL2,
1896 "slapd_daemon_task: read activity on %d\n", rd, 0, 0 );
1898 Debug ( LDAP_DEBUG_CONNS,
1899 "daemon: read activity on %d\n", rd, 0, 0 );
1902 * NOTE: it is possible that the connection was closed
1903 * and that the stream is now inactive.
1904 * connection_read() must valid the stream is still
1908 if ( connection_read( rd ) < 0 ) {
1912 ldap_pvt_thread_yield();
1915 if( slapd_shutdown == 1 ) {
1917 LDAP_LOG( CONNECTION, CRIT,
1918 "slapd_daemon_task: shutdown requested and initiated.\n", 0, 0, 0 );
1920 Debug( LDAP_DEBUG_TRACE,
1921 "daemon: shutdown requested and initiated.\n",
1925 } else if ( slapd_shutdown == 2 ) {
1926 #ifdef HAVE_NT_SERVICE_MANAGER
1928 LDAP_LOG( CONNECTION, CRIT,
1929 "slapd_daemon_task: shutdown initiated by Service Manager.\n",
1932 Debug( LDAP_DEBUG_TRACE,
1933 "daemon: shutdown initiated by Service Manager.\n",
1936 #else /* !HAVE_NT_SERVICE_MANAGER */
1938 LDAP_LOG( CONNECTION, CRIT,
1939 "slapd_daemon_task: abnormal condition, "
1940 "shutdown initiated.\n", 0, 0, 0 );
1942 Debug( LDAP_DEBUG_TRACE,
1943 "daemon: abnormal condition, shutdown initiated.\n",
1946 #endif /* !HAVE_NT_SERVICE_MANAGER */
1949 LDAP_LOG( CONNECTION, CRIT,
1950 "slapd_daemon_task: no active streams, shutdown initiated.\n",
1953 Debug( LDAP_DEBUG_TRACE,
1954 "daemon: no active streams, shutdown initiated.\n",
1959 if( slapd_gentle_shutdown != 2 ) {
1960 close_listeners ( 0 );
1963 free ( slap_listeners );
1964 slap_listeners = NULL;
1966 if( !slapd_gentle_shutdown ) {
1967 connections_shutdown();
1971 LDAP_LOG( CONNECTION, CRIT,
1972 "slapd_daemon_task: shutdown waiting for %d threads to terminate.\n",
1973 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1975 Debug( LDAP_DEBUG_ANY,
1976 "slapd shutdown: waiting for %d threads to terminate\n",
1977 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1979 ldap_pvt_thread_pool_destroy(&connection_pool, 1);
1985 int slapd_daemon( void )
1991 #define SLAPD_LISTENER_THREAD 1
1992 #if defined( SLAPD_LISTENER_THREAD )
1994 ldap_pvt_thread_t listener_tid;
1996 /* listener as a separate THREAD */
1997 rc = ldap_pvt_thread_create( &listener_tid,
1998 0, slapd_daemon_task, NULL );
2002 LDAP_LOG( CONNECTION, ERR,
2003 "slapd_daemon: listener ldap_pvt_thread_create failed (%d).\n",
2006 Debug( LDAP_DEBUG_ANY,
2007 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
2012 /* wait for the listener thread to complete */
2013 ldap_pvt_thread_join( listener_tid, (void *) NULL );
2016 /* experimental code */
2017 slapd_daemon_task( NULL );
2026 #if defined( HAVE_WINSOCK2 )
2027 WORD wVersionRequested;
2031 wVersionRequested = MAKEWORD( 2, 0 );
2033 err = WSAStartup( wVersionRequested, &wsaData );
2035 /* Tell the user that we couldn't find a usable */
2040 /* Confirm that the WinSock DLL supports 2.0.*/
2041 /* Note that if the DLL supports versions greater */
2042 /* than 2.0 in addition to 2.0, it will still return */
2043 /* 2.0 in wVersion since that is the version we */
2046 if ( LOBYTE( wsaData.wVersion ) != 2 ||
2047 HIBYTE( wsaData.wVersion ) != 0 )
2049 /* Tell the user that we couldn't find a usable */
2055 /* The WinSock DLL is acceptable. Proceed. */
2056 #elif defined( HAVE_WINSOCK )
2058 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
2065 int sockdestroy(void)
2067 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
2074 slap_sig_shutdown( int sig )
2077 LDAP_LOG( CONNECTION, CRIT,
2078 "slap_sig_shutdown: signal %d\n", sig, 0, 0 );
2080 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
2084 * If the NT Service Manager is controlling the server, we don't
2085 * want SIGBREAK to kill the server. For some strange reason,
2086 * SIGBREAK is generated when a user logs out.
2089 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
2090 if (is_NT_Service && sig == SIGBREAK)
2092 LDAP_LOG( CONNECTION, CRIT,
2093 "slap_sig_shutdown: SIGBREAK ignored.\n", 0, 0, 0 );
2095 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: SIGBREAK ignored.\n",
2101 if (sig == SIGHUP && global_gentlehup && slapd_gentle_shutdown == 0)
2102 slapd_gentle_shutdown = 1;
2109 /* reinstall self */
2110 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
2114 slap_sig_wake( int sig )
2118 /* reinstall self */
2119 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
2123 void slapd_add_internal(ber_socket_t s) {
2127 Listener ** slapd_get_listeners(void) {
2128 return slap_listeners;