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"
25 #define SLAP_STRING_UNKNOWN STRING_UNKNOWN
27 int allow_severity = LOG_INFO;
28 int deny_severity = LOG_NOTICE;
29 #else /* ! TCP Wrappers */
30 #define SLAP_STRING_UNKNOWN "unknown"
31 #endif /* ! TCP Wrappers */
35 /* this should go in <ldap.h> as soon as it is accepted */
36 #define LDAPI_MOD_URLEXT "x-mod"
37 #endif /* LDAP_PF_LOCAL */
40 int slap_inet4or6 = AF_UNSPEC;
42 int slap_inet4or6 = AF_INET;
47 ber_socket_t dtblsize;
49 Listener **slap_listeners = NULL;
51 #define SLAPD_LISTEN 10
53 static ber_socket_t wake_sds[2];
55 #if defined(NO_THREADS) || defined(HAVE_GNU_PTH)
57 #define WAKE_LISTENER(w) \
58 ((w && !waking) ? tcp_write( wake_sds[1], "0", 1 ), waking=1 : 0)
60 #define WAKE_LISTENER(w) \
61 do { if (w) tcp_write( wake_sds[1], "0", 1 ); } while(0)
67 volatile sig_atomic_t slapd_shutdown = 0, slapd_gentle_shutdown = 0;
69 static struct slap_daemon {
70 ldap_pvt_thread_mutex_t sd_mutex;
72 ber_socket_t sd_nactives;
75 /* In winsock, accept() returns values higher than dtblsize
76 so don't bother with this optimization */
89 * SLP related functions
93 #define LDAP_SRVTYPE_PREFIX "service:ldap://"
94 #define LDAPS_SRVTYPE_PREFIX "service:ldaps://"
95 static char** slapd_srvurls = NULL;
96 static SLPHandle slapd_hslp = 0;
98 void slapd_slp_init( const char* urls ) {
101 slapd_srvurls = ldap_str2charray( urls, " " );
103 if( slapd_srvurls == NULL ) return;
105 /* find and expand INADDR_ANY URLs */
106 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
107 if( strcmp( slapd_srvurls[i], "ldap:///" ) == 0) {
108 char *host = ldap_pvt_get_fqdn( NULL );
109 if ( host != NULL ) {
110 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
112 sizeof( LDAP_SRVTYPE_PREFIX ) );
113 strcpy( lutil_strcopy(slapd_srvurls[i],
114 LDAP_SRVTYPE_PREFIX ), host );
119 } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0) {
120 char *host = ldap_pvt_get_fqdn( NULL );
121 if ( host != NULL ) {
122 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
124 sizeof( LDAPS_SRVTYPE_PREFIX ) );
125 strcpy( lutil_strcopy(slapd_srvurls[i],
126 LDAPS_SRVTYPE_PREFIX ), host );
133 /* open the SLP handle */
134 SLPOpen( "en", 0, &slapd_hslp );
137 void slapd_slp_deinit() {
138 if( slapd_srvurls == NULL ) return;
140 ldap_charray_free( slapd_srvurls );
141 slapd_srvurls = NULL;
143 /* close the SLP handle */
144 SLPClose( slapd_hslp );
147 void slapd_slp_regreport(
155 void slapd_slp_reg() {
158 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
159 if( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
160 sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
161 strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
162 sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
166 SLP_LIFETIME_MAXIMUM,
176 void slapd_slp_dereg() {
179 if( slapd_srvurls == NULL ) return;
181 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
182 SLPDereg( slapd_hslp,
188 #endif /* HAVE_SLP */
191 * Add a descriptor to daemon control
193 static void slapd_add(ber_socket_t s) {
194 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
196 assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
197 assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
198 assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
201 if (s >= slap_daemon.sd_nfds) {
202 slap_daemon.sd_nfds = s + 1;
206 slap_daemon.sd_nactives++;
208 FD_SET( s, &slap_daemon.sd_actives );
209 FD_SET( s, &slap_daemon.sd_readers );
212 LDAP_LOG( CONNECTION, DETAIL1,
213 "slapd_add: added %ld%s%s\n", (long)s,
214 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
215 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
217 Debug( LDAP_DEBUG_CONNS, "daemon: added %ld%s%s\n",
219 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
220 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
222 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
226 * Remove the descriptor from daemon control
228 void slapd_remove(ber_socket_t s, int wake) {
229 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
231 slap_daemon.sd_nactives--;
234 LDAP_LOG( CONNECTION, DETAIL1,
235 "slapd_remove: removing %ld%s%s\n", (long) s,
236 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
237 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
239 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
241 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
242 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
244 FD_CLR( s, &slap_daemon.sd_actives );
245 FD_CLR( s, &slap_daemon.sd_readers );
246 FD_CLR( s, &slap_daemon.sd_writers );
248 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
249 WAKE_LISTENER(wake || slapd_gentle_shutdown == 2);
252 void slapd_clr_write(ber_socket_t s, int wake) {
253 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
255 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
256 FD_CLR( s, &slap_daemon.sd_writers );
258 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
262 void slapd_set_write(ber_socket_t s, int wake) {
263 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
265 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
266 if (!FD_ISSET(s, &slap_daemon.sd_writers))
267 FD_SET( (unsigned) s, &slap_daemon.sd_writers );
269 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
273 void slapd_clr_read(ber_socket_t s, int wake) {
274 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
276 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
277 FD_CLR( s, &slap_daemon.sd_readers );
279 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
283 void slapd_set_read(ber_socket_t s, int wake) {
284 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
286 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
287 if (!FD_ISSET(s, &slap_daemon.sd_readers))
288 FD_SET( s, &slap_daemon.sd_readers );
290 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
294 static void slapd_close(ber_socket_t s) {
296 LDAP_LOG( CONNECTION, DETAIL1, "slapd_close: closing %ld\n", (long)s, 0, 0);
298 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
304 static void slap_free_listener_addresses(struct sockaddr **sal)
306 struct sockaddr **sap;
312 for (sap = sal; *sap != NULL; sap++) {
319 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
320 static int get_url_perms(
332 for ( i = 0; exts[ i ]; i++ ) {
333 char *type = exts[ i ];
336 if ( type[ 0 ] == '!' ) {
341 if ( strncasecmp( type, LDAPI_MOD_URLEXT "=", sizeof(LDAPI_MOD_URLEXT "=") - 1 ) == 0 ) {
343 + ( sizeof(LDAPI_MOD_URLEXT "=") - 1 );
347 switch (strlen(value)) {
349 /* skip leading '0' */
350 if ( value[ 0 ] != '0' ) {
356 for ( j = 0; j < 3; j++) {
359 v = value[ j ] - '0';
361 if ( v < 0 || v > 7 ) {
370 for ( j = 1; j < 10; j++ ) {
371 static mode_t m[] = { 0,
372 S_IRUSR, S_IWUSR, S_IXUSR,
373 S_IRGRP, S_IWGRP, S_IXGRP,
374 S_IROTH, S_IWOTH, S_IXOTH
376 static char c[] = "-rwxrwxrwx";
378 if ( value[ j ] == c[ j ] ) {
381 } else if ( value[ j ] != '-' ) {
400 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
402 /* port = 0 indicates AF_LOCAL */
403 static int slap_get_listener_addresses(
406 struct sockaddr ***sal)
408 struct sockaddr **sap;
412 *sal = ch_malloc(2 * sizeof(void *));
418 *sap = ch_malloc(sizeof(struct sockaddr_un));
424 (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) ) {
426 LDAP_LOG( CONNECTION, INFO,
427 "slap_get_listener_addresses: domain socket path (%s) "
428 "too long in URL\n", host, 0, 0 );
430 Debug( LDAP_DEBUG_ANY,
431 "daemon: domain socket path (%s) too long in URL",
437 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
438 (*sap)->sa_family = AF_LOCAL;
439 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
443 #ifdef HAVE_GETADDRINFO
444 struct addrinfo hints, *res, *sai;
448 memset( &hints, '\0', sizeof(hints) );
449 hints.ai_flags = AI_PASSIVE;
450 hints.ai_socktype = SOCK_STREAM;
451 hints.ai_family = slap_inet4or6;
452 snprintf(serv, sizeof serv, "%d", port);
454 if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
456 LDAP_LOG( CONNECTION, INFO,
457 "slap_get_listener_addresses: getaddrinfo failed: %s\n",
458 AC_GAI_STRERROR(err), 0, 0 );
460 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
461 AC_GAI_STRERROR(err), 0, 0);
467 for (n=2; (sai = sai->ai_next) != NULL; n++) {
470 *sal = ch_calloc(n, sizeof(void *));
478 for ( sai=res; sai; sai=sai->ai_next ) {
479 if( sai->ai_addr == NULL ) {
481 LDAP_LOG( CONNECTION, INFO,
482 "slap_get_listener_addresses: "
483 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
485 Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
486 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
492 switch (sai->ai_family) {
493 # ifdef LDAP_PF_INET6
495 *sap = ch_malloc(sizeof(struct sockaddr_in6));
500 *(struct sockaddr_in6 *)*sap =
501 *((struct sockaddr_in6 *)sai->ai_addr);
505 *sap = ch_malloc(sizeof(struct sockaddr_in));
510 *(struct sockaddr_in *)*sap =
511 *((struct sockaddr_in *)sai->ai_addr);
519 (*sap)->sa_family = sai->ai_family;
529 struct hostent *he = NULL;
531 if ( host == NULL ) {
532 in.s_addr = htonl(INADDR_ANY);
534 } else if ( !inet_aton( host, &in ) ) {
535 he = gethostbyname( host );
538 LDAP_LOG( CONNECTION, INFO,
539 "slap_get_listener_addresses: invalid host %s\n", host, 0, 0 );
541 Debug( LDAP_DEBUG_ANY,
542 "daemon: invalid host %s", host, 0, 0);
546 for (n = 0; he->h_addr_list[n]; n++) ;
549 *sal = ch_malloc((n+1) * sizeof(void *));
555 for ( i = 0; i<n; i++ ) {
556 sap[i] = ch_malloc(sizeof(struct sockaddr_in));
560 (void)memset( (void *)sap[i], '\0', sizeof(struct sockaddr_in) );
561 sap[i]->sa_family = AF_INET;
562 ((struct sockaddr_in *)sap[i])->sin_port = htons(port);
564 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr, he->h_addr_list[i], sizeof(struct in_addr) );
566 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr, &in, sizeof(struct in_addr) );
576 slap_free_listener_addresses(*sal);
580 static int slap_open_listener(
591 int err, addrlen = 0;
592 struct sockaddr **sal, **psal;
593 int socktype = SOCK_STREAM; /* default to COTS */
595 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
600 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
602 rc = ldap_url_parse( url, &lud );
604 if( rc != LDAP_URL_SUCCESS ) {
606 LDAP_LOG( CONNECTION, ERR,
607 "slap_open_listener: listen URL \"%s\" parse error %d\n",
610 Debug( LDAP_DEBUG_ANY,
611 "daemon: listen URL \"%s\" parse error=%d\n",
617 l.sl_url.bv_val = NULL;
620 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
622 LDAP_LOG( CONNECTION, INFO,
623 "slap_open_listener: TLS is not supported (%s)\n", url, 0, 0 );
625 Debug( LDAP_DEBUG_ANY,
626 "daemon: TLS not supported (%s)\n",
629 ldap_free_urldesc( lud );
633 if(! lud->lud_port ) {
634 lud->lud_port = LDAP_PORT;
638 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
640 if(! lud->lud_port ) {
641 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
645 port = (unsigned short) lud->lud_port;
647 tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme);
648 if ( tmp == LDAP_PROTO_IPC ) {
650 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
651 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
653 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
658 LDAP_LOG( CONNECTION, INFO,
659 "slap_open_listener: URL scheme is not supported: %s\n", url, 0, 0 );
661 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
664 ldap_free_urldesc( lud );
668 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
669 || strcmp(lud->lud_host, "*") == 0 )
671 err = slap_get_listener_addresses(NULL, port, &sal);
673 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
676 #ifdef LDAP_CONNECTIONLESS
677 l.sl_is_udp = ( tmp == LDAP_PROTO_UDP );
680 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
681 if ( lud->lud_exts ) {
682 err = get_url_perms( lud->lud_exts, &l.sl_perms, &crit );
684 l.sl_perms = S_IRWXU | S_IRWXO;
686 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
688 ldap_free_urldesc( lud );
693 /* If we got more than one address returned, we need to make space
694 * for it in the slap_listeners array.
696 for ( num=0; sal[num]; num++ );
699 slap_listeners = ch_realloc( slap_listeners, (*listeners + 1) * sizeof(Listener *) );
703 while ( *sal != NULL ) {
704 switch( (*sal)->sa_family ) {
717 #ifdef LDAP_CONNECTIONLESS
718 if( l.sl_is_udp ) socktype = SOCK_DGRAM;
720 l.sl_sd = socket( (*sal)->sa_family, socktype, 0);
721 if ( l.sl_sd == AC_SOCKET_INVALID ) {
722 int err = sock_errno();
724 LDAP_LOG( CONNECTION, ERR,
725 "slap_open_listener: socket() failed errno=%d (%s)\n",
726 err, sock_errstr(err), 0 );
728 Debug( LDAP_DEBUG_ANY,
729 "daemon: socket() failed errno=%d (%s)\n", err,
730 sock_errstr(err), 0 );
736 if ( l.sl_sd >= dtblsize ) {
738 LDAP_LOG( CONNECTION, ERR,
739 "slap_open_listener: listener descriptor %ld is too "
740 "great %ld\n", (long)l.sl_sd, (long)dtblsize, 0 );
742 Debug( LDAP_DEBUG_ANY,
743 "daemon: listener descriptor %ld is too great %ld\n",
744 (long) l.sl_sd, (long) dtblsize, 0 );
746 tcp_close( l.sl_sd );
752 if ( (*sal)->sa_family == AF_LOCAL ) {
753 unlink ( ((struct sockaddr_un *)*sal)->sun_path );
758 /* enable address reuse */
760 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
761 (char *) &tmp, sizeof(tmp) );
762 if ( rc == AC_SOCKET_ERROR ) {
763 int err = sock_errno();
765 LDAP_LOG( CONNECTION, INFO,
766 "slap_open_listener: setsockopt( %ld, SO_REUSEADDR ) "
767 "failed errno %d (%s)\n", (long)l.sl_sd, err,
770 Debug( LDAP_DEBUG_ANY,
771 "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
772 (long) l.sl_sd, err, sock_errstr(err) );
778 switch( (*sal)->sa_family ) {
780 addrlen = sizeof(struct sockaddr_in);
785 /* Try to use IPv6 sockets for IPv6 only */
787 rc = setsockopt( l.sl_sd, IPPROTO_IPV6, IPV6_V6ONLY,
788 (char *) &tmp, sizeof(tmp) );
789 if ( rc == AC_SOCKET_ERROR ) {
790 int err = sock_errno();
792 LDAP_LOG( CONNECTION, INFO,
793 "slap_open_listener: setsockopt( %ld, IPV6_V6ONLY ) failed errno %d (%s)\n",
794 (long)l.sl_sd, err, sock_errstr(err) );
796 Debug( LDAP_DEBUG_ANY,
797 "slapd(%ld): setsockopt(IPV6_V6ONLY) failed errno=%d (%s)\n",
798 (long) l.sl_sd, err, sock_errstr(err) );
802 addrlen = sizeof(struct sockaddr_in6);
807 addrlen = sizeof(struct sockaddr_un);
812 if (bind(l.sl_sd, *sal, addrlen)) {
815 LDAP_LOG( CONNECTION, INFO,
816 "slap_open_listener: bind(%ld) failed errno=%d (%s)\n",
817 (long)l.sl_sd, err, sock_errstr(err) );
819 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
820 (long) l.sl_sd, err, sock_errstr(err) );
822 tcp_close( l.sl_sd );
827 switch ( (*sal)->sa_family ) {
830 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
831 #if 0 /* don't muck with socket perms */
832 if ( chmod( addr, l.sl_perms ) < 0 && crit ) {
833 int err = sock_errno();
835 LDAP_LOG( CONNECTION, INFO,
836 "slap_open_listener: fchmod(%ld) failed errno=%d (%s)\n",
837 (long)l.sl_sd, err, sock_errstr(err) );
839 Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
840 (long) l.sl_sd, err, sock_errstr(err) );
842 tcp_close( l.sl_sd );
843 slap_free_listener_addresses(psal);
847 l.sl_name.bv_len = strlen(addr) + sizeof("PATH=") - 1;
848 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len + 1 );
849 snprintf( l.sl_name.bv_val, l.sl_name.bv_len + 1,
852 #endif /* LDAP_PF_LOCAL */
856 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
857 char addr[INET_ADDRSTRLEN];
858 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
859 addr, sizeof(addr) );
862 s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr );
864 port = ntohs( ((struct sockaddr_in *)*sal) ->sin_port );
865 l.sl_name.bv_val = ber_memalloc( sizeof("IP=255.255.255.255:65535") );
866 snprintf( l.sl_name.bv_val, sizeof("IP=255.255.255.255:65535"),
868 s != NULL ? s : SLAP_STRING_UNKNOWN, port );
869 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
874 char addr[INET6_ADDRSTRLEN];
875 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
877 port = ntohs( ((struct sockaddr_in6 *)*sal)->sin6_port );
878 l.sl_name.bv_len = strlen(addr) + sizeof("IP= 65535");
879 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len );
880 snprintf( l.sl_name.bv_val, l.sl_name.bv_len, "IP=%s %d",
882 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
884 #endif /* LDAP_PF_INET6 */
888 LDAP_LOG( CONNECTION, INFO,
889 "slap_open_listener: unsupported address family (%d)\n",
890 (int)(*sal)->sa_family, 0, 0 );
892 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
893 (int) (*sal)->sa_family, 0, 0 );
898 AC_MEMCPY(&l.sl_sa, *sal, addrlen);
899 ber_str2bv( url, 0, 1, &l.sl_url);
900 li = ch_malloc( sizeof( Listener ) );
902 slap_listeners[*cur] = li;
906 } /* while ( *sal != NULL ) */
908 slap_free_listener_addresses(psal);
910 if ( l.sl_url.bv_val == NULL )
913 LDAP_LOG( CONNECTION, RESULTS,
914 "slap_open_listener: failed on %s\n", url, 0, 0 );
916 Debug( LDAP_DEBUG_TRACE,
917 "slap_open_listener: failed on %s\n", url, 0, 0 );
923 LDAP_LOG( CONNECTION, RESULTS,
924 "slap_open_listener: daemon initialized %s\n",
925 l.sl_url.bv_val, 0, 0 );
927 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
928 l.sl_url.bv_val, 0, 0 );
933 static int sockinit(void);
934 static int sockdestroy(void);
936 int slapd_daemon_init( const char *urls )
942 LDAP_LOG( CONNECTION, ARGS,
943 "slapd_daemon_init: %s\n", urls ? urls : "<null>", 0, 0 );
945 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
946 urls ? urls : "<null>", 0, 0 );
948 if( (rc = sockinit()) != 0 ) {
953 dtblsize = sysconf( _SC_OPEN_MAX );
954 #elif HAVE_GETDTABLESIZE
955 dtblsize = getdtablesize();
957 dtblsize = FD_SETSIZE;
961 if(dtblsize > FD_SETSIZE) {
962 dtblsize = FD_SETSIZE;
964 #endif /* !FD_SETSIZE */
966 /* open a pipe (or something equivalent connected to itself).
967 * we write a byte on this fd whenever we catch a signal. The main
968 * loop will be select'ing on this socket, and will wake up when
971 if( (rc = lutil_pair( wake_sds )) < 0 ) {
973 LDAP_LOG( CONNECTION, ERR,
974 "slap_daemon_init: lutil_pair() failed rc=%d\n", rc, 0, 0);
976 Debug( LDAP_DEBUG_ANY,
977 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
982 FD_ZERO( &slap_daemon.sd_readers );
983 FD_ZERO( &slap_daemon.sd_writers );
989 u = ldap_str2charray( urls, " " );
991 if( u == NULL || u[0] == NULL ) {
993 LDAP_LOG( CONNECTION, ERR,
994 "slap_daemon_init: no urls (%s) provided.\n", urls, 0, 0 );
996 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
1002 for( i=0; u[i] != NULL; i++ ) {
1004 LDAP_LOG( CONNECTION, DETAIL1,
1005 "slap_daemon_init: listen on %s\n.", u[i], 0, 0 );
1007 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
1014 LDAP_LOG( CONNECTION, INFO,
1015 "slap_daemon_init: no listeners to open (%s)\n", urls, 0, 0 );
1017 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
1020 ldap_charray_free( u );
1025 LDAP_LOG( CONNECTION, INFO,
1026 "slap_daemon_init: %d listeners to open...\n", i, 0, 0 );
1028 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
1031 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
1033 for(n = 0, j = 0; u[n]; n++ ) {
1034 if ( slap_open_listener( u[n], &i, &j ) ) {
1035 ldap_charray_free( u );
1039 slap_listeners[j] = NULL;
1042 LDAP_LOG( CONNECTION, DETAIL1,
1043 "slap_daemon_init: %d listeners opened\n", i, 0, 0 );
1045 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
1050 slapd_slp_init( urls );
1054 ldap_charray_free( u );
1055 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
1061 slapd_daemon_destroy(void)
1063 connections_destroy();
1064 tcp_close( wake_sds[1] );
1065 tcp_close( wake_sds[0] );
1084 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1085 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1087 slapd_remove( slap_listeners[l]->sl_sd, 0 );
1088 #ifdef LDAP_PF_LOCAL
1089 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1090 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1092 #endif /* LDAP_PF_LOCAL */
1093 slapd_close( slap_listeners[l]->sl_sd );
1095 if ( slap_listeners[l]->sl_url.bv_val )
1096 ber_memfree( slap_listeners[l]->sl_url.bv_val );
1097 if ( slap_listeners[l]->sl_name.bv_val )
1098 ber_memfree( slap_listeners[l]->sl_name.bv_val );
1099 free ( slap_listeners[l] );
1100 slap_listeners[l] = NULL;
1111 time_t last_idle_check = 0;
1114 if ( global_idletimeout > 0 ) {
1115 last_idle_check = slap_get_time();
1117 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1118 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1120 #ifdef LDAP_CONNECTIONLESS
1121 /* Since this is connectionless, the data port is the
1122 * listening port. The listen() and accept() calls
1125 if ( slap_listeners[l]->sl_is_udp ) {
1126 slapd_add( slap_listeners[l]->sl_sd );
1131 if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN ) == -1 ) {
1132 int err = sock_errno();
1134 #ifdef LDAP_PF_INET6
1135 /* If error is EADDRINUSE, we are trying to listen to INADDR_ANY and
1136 * we are already listening to in6addr_any, then we want to ignore
1137 * this and continue.
1139 if ( err == EADDRINUSE ) {
1141 struct sockaddr_in sa = slap_listeners[l]->sl_sa.sa_in_addr;
1142 struct sockaddr_in6 sa6;
1144 if ( sa.sin_family == AF_INET &&
1145 sa.sin_addr.s_addr == htonl(INADDR_ANY) ) {
1146 for ( i = 0 ; i < l; i++ ) {
1147 sa6 = slap_listeners[i]->sl_sa.sa_in6_addr;
1148 if ( sa6.sin6_family == AF_INET6 &&
1149 !memcmp( &sa6.sin6_addr, &in6addr_any, sizeof(struct in6_addr) ) )
1154 /* We are already listening to in6addr_any */
1156 LDAP_LOG(CONNECTION, WARNING,
1157 "slapd_daemon_task: Attempt to listen to 0.0.0.0 failed, already listening on ::, assuming IPv4 included\n", 0, 0, 0 );
1159 Debug( LDAP_DEBUG_CONNS,
1160 "daemon: Attempt to listen to 0.0.0.0 failed, already listening on ::, assuming IPv4 included\n",
1163 slapd_close( slap_listeners[l]->sl_sd );
1164 slap_listeners[l]->sl_sd = AC_SOCKET_INVALID;
1171 LDAP_LOG( CONNECTION, ERR,
1172 "slapd_daemon_task: listen( %s, 5 ) failed errno=%d (%s)\n",
1173 slap_listeners[l]->sl_url.bv_val, err, sock_errstr(err) );
1175 Debug( LDAP_DEBUG_ANY,
1176 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
1177 slap_listeners[l]->sl_url.bv_val, err,
1180 return( (void*)-1 );
1183 slapd_add( slap_listeners[l]->sl_sd );
1186 #ifdef HAVE_NT_SERVICE_MANAGER
1187 if ( started_event != NULL ) {
1188 ldap_pvt_thread_cond_signal( &started_event );
1191 /* initialization complete. Here comes the loop. */
1193 while ( !slapd_shutdown ) {
1198 #define SLAPD_EBADF_LIMIT 16
1202 #define SLAPD_IDLE_CHECK_LIMIT 4
1210 struct timeval zero;
1211 struct timeval *tvp;
1214 now = slap_get_time();
1215 connections_timeout_idle( now );
1217 else if ( global_idletimeout > 0 ) {
1218 now = slap_get_time();
1219 if ( difftime( last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 ) {
1220 connections_timeout_idle( now );
1225 if( slapd_gentle_shutdown ) {
1226 ber_socket_t active;
1228 if( slapd_gentle_shutdown == 1 ) {
1229 Debug( LDAP_DEBUG_ANY, "slapd gentle shutdown\n", 0, 0, 0 );
1230 close_listeners( 1 );
1231 global_restrictops |= SLAP_RESTRICT_OP_WRITES;
1232 slapd_gentle_shutdown = 2;
1235 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1236 active = slap_daemon.sd_nactives;
1237 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1245 FD_ZERO( &writefds );
1246 FD_ZERO( &readfds );
1251 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1253 #ifdef FD_SET_MANUAL_COPY
1254 for( s = 0; s < nfds; s++ ) {
1255 if(FD_ISSET( &slap_sd_readers, s )) {
1256 FD_SET( s, &readfds );
1258 if(FD_ISSET( &slap_sd_writers, s )) {
1259 FD_SET( s, &writefds );
1263 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
1264 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
1266 assert(!FD_ISSET(wake_sds[0], &readfds));
1267 FD_SET( wake_sds[0], &readfds );
1269 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1270 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1272 if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
1273 FD_SET( slap_listeners[l]->sl_sd, &readfds );
1276 #ifndef HAVE_WINSOCK
1277 nfds = slap_daemon.sd_nfds;
1282 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1284 at = ldap_pvt_thread_pool_backload(&connection_pool);
1286 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
1289 tvp = at ? &zero : NULL;
1292 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1293 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1297 LDAP_LOG( CONNECTION, DETAIL1,
1298 "slapd_daemon_task: select: listen=%d "
1299 "active_threads=%d tvp=%s\n",
1300 slap_listeners[l]->sl_sd, at, tvp == NULL ? "NULL" : "zero" );
1302 Debug( LDAP_DEBUG_CONNS,
1303 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
1304 slap_listeners[l]->sl_sd, at,
1305 tvp == NULL ? "NULL" : "zero" );
1309 switch(ns = select( nfds, &readfds,
1311 /* don't pass empty fd_set */
1312 ( writefds.fd_count > 0 ? &writefds : NULL ),
1318 case -1: { /* failure - try again */
1319 int err = sock_errno();
1323 /* you'd think this would be EBADF */
1324 || err == WSAENOTSOCK
1327 if (++ebadf < SLAPD_EBADF_LIMIT)
1331 if( err != EINTR ) {
1333 LDAP_LOG( CONNECTION, INFO,
1334 "slapd_daemon_task: select failed (%d): %s\n",
1335 err, sock_errstr(err), 0 );
1337 Debug( LDAP_DEBUG_CONNS,
1338 "daemon: select failed (%d): %s\n",
1339 err, sock_errstr(err), 0 );
1346 case 0: /* timeout - let threads run */
1349 LDAP_LOG( CONNECTION, DETAIL2,
1350 "slapd_daemon_task: select timeout - yielding\n", 0, 0, 0 );
1352 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
1355 ldap_pvt_thread_yield();
1358 default: /* something happened - deal with it */
1359 if( slapd_shutdown ) continue;
1363 LDAP_LOG( CONNECTION, DETAIL2,
1364 "slapd_daemon_task: activity on %d descriptors\n", ns, 0, 0 );
1366 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
1372 if( FD_ISSET( wake_sds[0], &readfds ) ) {
1374 tcp_read( wake_sds[0], c, sizeof(c) );
1375 #if defined(NO_THREADS) || defined(HAVE_GNU_PTH)
1381 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1383 socklen_t len = sizeof(from);
1386 char *authid = NULL;
1387 #ifdef SLAPD_RLOOKUPS
1388 char hbuf[NI_MAXHOST];
1391 char *dnsname = NULL;
1392 char *peeraddr = NULL;
1393 #ifdef LDAP_PF_LOCAL
1394 char peername[MAXPATHLEN + sizeof("PATH=")];
1395 #elif defined(LDAP_PF_INET6)
1396 char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
1398 char peername[sizeof("IP=255.255.255.255:65336")];
1399 #endif /* LDAP_PF_LOCAL */
1403 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1406 if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
1409 #ifdef LDAP_CONNECTIONLESS
1410 if ( slap_listeners[l]->sl_is_udp ) {
1411 /* The first time we receive a query, we set this
1412 * up as a "connection". It remains open for the life
1415 if ( slap_listeners[l]->sl_is_udp < 2 ) {
1416 id = connection_init(
1417 slap_listeners[l]->sl_sd,
1418 slap_listeners[l], "", "",
1420 slap_listeners[l]->sl_is_udp++;
1426 s = accept( slap_listeners[l]->sl_sd,
1427 (struct sockaddr *) &from, &len );
1428 if ( s == AC_SOCKET_INVALID ) {
1429 int err = sock_errno();
1432 if( err == EMFILE ) {
1437 if( err == ENFILE ) {
1447 LDAP_LOG( CONNECTION, ERR,
1448 "slapd_daemon_task: accept(%ld) failed errno=%d (%s)\n",
1449 (long)slap_listeners[l]->sl_sd,
1450 err, sock_errstr(err) );
1452 Debug( LDAP_DEBUG_ANY,
1453 "daemon: accept(%ld) failed errno=%d (%s)\n",
1454 (long) slap_listeners[l]->sl_sd, err,
1458 /* prevent busy loop */
1460 if( emfile % 4 == 3 ) usleep( 250 );
1462 if( emfile % 8 == 7 ) sleep( 1 );
1466 ldap_pvt_thread_yield();
1471 #ifndef HAVE_WINSOCK
1472 /* make sure descriptor number isn't too great */
1473 if ( s >= dtblsize ) {
1475 LDAP_LOG( CONNECTION, ERR,
1476 "slapd_daemon_task: %ld beyond descriptor table size %ld\n",
1477 (long)s, (long)dtblsize, 0 );
1479 Debug( LDAP_DEBUG_ANY,
1480 "daemon: %ld beyond descriptor table size %ld\n",
1481 (long) s, (long) dtblsize, 0 );
1485 ldap_pvt_thread_yield();
1491 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1493 /* newly accepted stream should not be in any of the FD SETS */
1494 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
1495 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
1496 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
1498 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1501 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1502 #ifdef LDAP_PF_LOCAL
1503 /* for IPv4 and IPv6 sockets only */
1504 if ( from.sa_addr.sa_family != AF_LOCAL )
1505 #endif /* LDAP_PF_LOCAL */
1510 /* enable keep alives */
1512 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1513 (char *) &tmp, sizeof(tmp) );
1514 if ( rc == AC_SOCKET_ERROR ) {
1515 int err = sock_errno();
1517 LDAP_LOG( CONNECTION, ERR,
1518 "slapd_daemon_task: setsockopt( %ld, SO_KEEPALIVE)"
1519 " failed errno=%d (%s)\n",
1520 (long)s, err, sock_errstr(err) );
1522 Debug( LDAP_DEBUG_ANY,
1523 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1524 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1529 /* enable no delay */
1531 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1532 (char *)&tmp, sizeof(tmp) );
1533 if ( rc == AC_SOCKET_ERROR ) {
1534 int err = sock_errno();
1536 LDAP_LOG( CONNECTION, ERR,
1537 "slapd_daemon_task: setsockopt( %ld, "
1538 "TCP_NODELAY) failed errno=%d (%s)\n",
1539 (long)s, err, sock_errstr(err) );
1541 Debug( LDAP_DEBUG_ANY,
1542 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1543 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1551 LDAP_LOG( CONNECTION, DETAIL1,
1552 "slapd_daemon_task: new connection on %ld\n", (long)s, 0, 0 );
1554 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
1557 switch ( from.sa_addr.sa_family ) {
1558 # ifdef LDAP_PF_LOCAL
1560 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1561 ssf = LDAP_PVT_SASL_LOCAL_SSF;
1566 if( getpeereid( s, &uid, &gid ) == 0 ) {
1568 sizeof("uidnumber=4294967295+gidnumber=4294967295,"
1569 "cn=peercred,cn=external,cn=auth"));
1570 sprintf(authid, "uidnumber=%d+gidnumber=%d,"
1571 "cn=peercred,cn=external,cn=auth",
1572 (int) uid, (int) gid);
1577 #endif /* LDAP_PF_LOCAL */
1579 # ifdef LDAP_PF_INET6
1581 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1582 peeraddr = inet_ntoa( *((struct in_addr *)
1583 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1584 sprintf( peername, "IP=%s:%d",
1585 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1586 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1588 char addr[INET6_ADDRSTRLEN];
1590 peeraddr = (char *) inet_ntop( AF_INET6,
1591 &from.sa_in6_addr.sin6_addr,
1592 addr, sizeof addr );
1593 sprintf( peername, "IP=%s %d",
1594 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1595 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1598 # endif /* LDAP_PF_INET6 */
1601 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1602 sprintf( peername, "IP=%s:%d",
1603 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1604 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1612 if ( ( from.sa_addr.sa_family == AF_INET )
1613 #ifdef LDAP_PF_INET6
1614 || ( from.sa_addr.sa_family == AF_INET6 )
1617 #ifdef SLAPD_RLOOKUPS
1618 if ( use_reverse_lookup ) {
1620 if (ldap_pvt_get_hname( (const struct sockaddr *)&from, len, hbuf,
1621 sizeof(hbuf), &herr ) == 0) {
1622 ldap_pvt_str2lower( hbuf );
1628 #endif /* SLAPD_RLOOKUPS */
1631 if ( !hosts_ctl("slapd",
1632 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1633 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1634 SLAP_STRING_UNKNOWN ))
1637 Statslog( LDAP_DEBUG_STATS,
1638 "fd=%ld DENIED from %s (%s)",
1640 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1641 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1646 #endif /* HAVE_TCPD */
1649 id = connection_init(s,
1651 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1654 slap_listeners[l]->sl_is_tls,
1661 if( authid ) ch_free(authid);
1665 LDAP_LOG( CONNECTION, INFO,
1666 "slapd_daemon_task: "
1667 "connection_init(%ld, %s, %s) "
1670 slap_listeners[l]->sl_name.bv_val );
1672 Debug( LDAP_DEBUG_ANY,
1673 "daemon: connection_init(%ld, %s, %s) "
1677 slap_listeners[l]->sl_name.bv_val );
1683 Statslog( LDAP_DEBUG_STATS,
1684 "conn=%ld fd=%ld ACCEPT from %s (%s)\n",
1687 slap_listeners[l]->sl_name.bv_val,
1696 LDAP_LOG( CONNECTION, DETAIL2,
1697 "slapd_daemon_task: activity on ", 0, 0, 0 );
1699 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1702 for ( i = 0; i < readfds.fd_count; i++ ) {
1704 LDAP_LOG( CONNECTION, DETAIL2,
1705 " %d%s", readfds.fd_array[i], "r", 0, 0 );
1707 Debug( LDAP_DEBUG_CONNS, " %d%s",
1708 readfds.fd_array[i], "r", 0 );
1711 for ( i = 0; i < writefds.fd_count; i++ ) {
1713 LDAP_LOG( CONNECTION, DETAIL2,
1714 " %d%s", writefds.fd_array[i], "w" , 0 );
1716 Debug( LDAP_DEBUG_CONNS, " %d%s",
1717 writefds.fd_array[i], "w", 0 );
1722 for ( i = 0; i < nfds; i++ ) {
1724 int is_listener = 0;
1726 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1727 if ( i == slap_listeners[l]->sl_sd ) {
1728 #ifdef LDAP_CONNECTIONLESS
1729 /* The listener is the data port. Don't
1732 if (slap_listeners[l]->sl_is_udp) continue;
1738 if ( is_listener ) {
1741 r = FD_ISSET( i, &readfds );
1742 w = FD_ISSET( i, &writefds );
1745 LDAP_LOG( CONNECTION, DETAIL2,
1746 " %d%s%s", i, r ? "r" : "", w ? "w" : "" );
1748 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1749 r ? "r" : "", w ? "w" : "" );
1755 LDAP_LOG( CONNECTION, DETAIL2, "\n", 0, 0, 0 );
1757 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1762 /* loop through the writers */
1764 for ( i = 0; i < writefds.fd_count; i++ )
1766 for ( i = 0; i < nfds; i++ )
1770 int is_listener = 0;
1772 wd = writefds.fd_array[i];
1774 if( ! FD_ISSET( i, &writefds ) ) {
1780 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1781 if ( i == slap_listeners[l]->sl_sd ) {
1782 #ifdef LDAP_CONNECTIONLESS
1783 if (slap_listeners[l]->sl_is_udp) continue;
1789 if ( is_listener ) {
1793 LDAP_LOG( CONNECTION, DETAIL2,
1794 "slapd_daemon_task: write active on %d\n", wd, 0, 0 );
1796 Debug( LDAP_DEBUG_CONNS,
1797 "daemon: write active on %d\n",
1801 * NOTE: it is possible that the connection was closed
1802 * and that the stream is now inactive.
1803 * connection_write() must valid the stream is still
1807 if ( connection_write( wd ) < 0 ) {
1808 FD_CLR( (unsigned) wd, &readfds );
1814 for ( i = 0; i < readfds.fd_count; i++ )
1816 for ( i = 0; i < nfds; i++ )
1820 int is_listener = 0;
1823 rd = readfds.fd_array[i];
1825 if( ! FD_ISSET( i, &readfds ) ) {
1831 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1832 if ( rd == slap_listeners[l]->sl_sd ) {
1833 #ifdef LDAP_CONNECTIONLESS
1834 if (slap_listeners[l]->sl_is_udp) continue;
1840 if ( is_listener ) {
1845 LDAP_LOG( CONNECTION, DETAIL2,
1846 "slapd_daemon_task: read activity on %d\n", rd, 0, 0 );
1848 Debug ( LDAP_DEBUG_CONNS,
1849 "daemon: read activity on %d\n", rd, 0, 0 );
1852 * NOTE: it is possible that the connection was closed
1853 * and that the stream is now inactive.
1854 * connection_read() must valid the stream is still
1858 if ( connection_read( rd ) < 0 ) {
1862 ldap_pvt_thread_yield();
1865 if( slapd_shutdown == 1 ) {
1867 LDAP_LOG( CONNECTION, CRIT,
1868 "slapd_daemon_task: shutdown requested and initiated.\n", 0, 0, 0 );
1870 Debug( LDAP_DEBUG_TRACE,
1871 "daemon: shutdown requested and initiated.\n",
1875 } else if ( slapd_shutdown == 2 ) {
1876 #ifdef HAVE_NT_SERVICE_MANAGER
1878 LDAP_LOG( CONNECTION, CRIT,
1879 "slapd_daemon_task: shutdown initiated by Service Manager.\n",
1882 Debug( LDAP_DEBUG_TRACE,
1883 "daemon: shutdown initiated by Service Manager.\n",
1886 #else /* !HAVE_NT_SERVICE_MANAGER */
1888 LDAP_LOG( CONNECTION, CRIT,
1889 "slapd_daemon_task: abnormal condition, "
1890 "shutdown initiated.\n", 0, 0, 0 );
1892 Debug( LDAP_DEBUG_TRACE,
1893 "daemon: abnormal condition, shutdown initiated.\n",
1896 #endif /* !HAVE_NT_SERVICE_MANAGER */
1899 LDAP_LOG( CONNECTION, CRIT,
1900 "slapd_daemon_task: no active streams, shutdown initiated.\n",
1903 Debug( LDAP_DEBUG_TRACE,
1904 "daemon: no active streams, shutdown initiated.\n",
1909 if( slapd_gentle_shutdown != 2 ) {
1910 close_listeners ( 0 );
1913 free ( slap_listeners );
1914 slap_listeners = NULL;
1916 if( !slapd_gentle_shutdown ) {
1917 connections_shutdown();
1921 LDAP_LOG( CONNECTION, CRIT,
1922 "slapd_daemon_task: shutdown waiting for %d threads to terminate.\n",
1923 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1925 Debug( LDAP_DEBUG_ANY,
1926 "slapd shutdown: waiting for %d threads to terminate\n",
1927 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1929 ldap_pvt_thread_pool_destroy(&connection_pool, 1);
1935 int slapd_daemon( void )
1941 #define SLAPD_LISTENER_THREAD 1
1942 #if defined( SLAPD_LISTENER_THREAD )
1944 ldap_pvt_thread_t listener_tid;
1946 /* listener as a separate THREAD */
1947 rc = ldap_pvt_thread_create( &listener_tid,
1948 0, slapd_daemon_task, NULL );
1952 LDAP_LOG( CONNECTION, ERR,
1953 "slapd_daemon: listener ldap_pvt_thread_create failed (%d).\n",
1956 Debug( LDAP_DEBUG_ANY,
1957 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
1962 /* wait for the listener thread to complete */
1963 ldap_pvt_thread_join( listener_tid, (void *) NULL );
1966 /* experimental code */
1967 slapd_daemon_task( NULL );
1976 #if defined( HAVE_WINSOCK2 )
1977 WORD wVersionRequested;
1981 wVersionRequested = MAKEWORD( 2, 0 );
1983 err = WSAStartup( wVersionRequested, &wsaData );
1985 /* Tell the user that we couldn't find a usable */
1990 /* Confirm that the WinSock DLL supports 2.0.*/
1991 /* Note that if the DLL supports versions greater */
1992 /* than 2.0 in addition to 2.0, it will still return */
1993 /* 2.0 in wVersion since that is the version we */
1996 if ( LOBYTE( wsaData.wVersion ) != 2 ||
1997 HIBYTE( wsaData.wVersion ) != 0 )
1999 /* Tell the user that we couldn't find a usable */
2005 /* The WinSock DLL is acceptable. Proceed. */
2006 #elif defined( HAVE_WINSOCK )
2008 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
2015 int sockdestroy(void)
2017 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
2024 slap_sig_shutdown( int sig )
2027 LDAP_LOG( CONNECTION, CRIT,
2028 "slap_sig_shutdown: signal %d\n", sig, 0, 0 );
2030 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
2034 * If the NT Service Manager is controlling the server, we don't
2035 * want SIGBREAK to kill the server. For some strange reason,
2036 * SIGBREAK is generated when a user logs out.
2039 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
2040 if (is_NT_Service && sig == SIGBREAK)
2042 LDAP_LOG( CONNECTION, CRIT,
2043 "slap_sig_shutdown: SIGBREAK ignored.\n", 0, 0, 0 );
2045 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: SIGBREAK ignored.\n",
2051 if (sig == SIGHUP && global_gentlehup && slapd_gentle_shutdown == 0)
2052 slapd_gentle_shutdown = 1;
2059 /* reinstall self */
2060 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
2064 slap_sig_wake( int sig )
2068 /* reinstall self */
2069 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
2073 void slapd_add_internal(ber_socket_t s) {
2077 Listener ** slapd_get_listeners(void) {
2078 return slap_listeners;