3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/signal.h>
14 #include <ac/socket.h>
15 #include <ac/string.h>
17 #include <ac/unistd.h>
20 #include "ldap_pvt_thread.h"
27 int allow_severity = LOG_INFO;
28 int deny_severity = LOG_NOTICE;
29 #endif /* TCP Wrappers */
33 #endif /* LDAP_PF_LOCAL */
37 ber_socket_t dtblsize;
39 typedef struct slap_listener {
45 #ifdef LDAP_CONNECTIONLESS
46 int sl_is_udp; /* UDP listener is also data port */
50 #define sl_addr sl_sa.sa_in_addr
53 Listener **slap_listeners = NULL;
55 #define SLAPD_LISTEN 10
57 static ber_socket_t wake_sds[2];
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;
73 static struct slap_daemon {
74 ldap_pvt_thread_mutex_t sd_mutex;
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 = 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 *) realloc( slapd_srvurls[i],
116 sizeof( LDAP_SRVTYPE_PREFIX ) );
117 strcpy( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX );
118 strcat( slapd_srvurls[i], 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 *) realloc( slapd_srvurls[i],
128 sizeof( LDAPS_SRVTYPE_PREFIX ) );
129 strcpy( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX );
130 strcat( slapd_srvurls[i], host );
137 /* open the SLP handle */
138 SLPOpen( "en", 0, &slapd_hslp );
141 void slapd_slp_deinit() {
142 if( slapd_srvurls == NULL ) return;
144 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 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
163 if( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
164 sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
165 strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
166 sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
170 SLP_LIFETIME_MAXIMUM,
180 void slapd_slp_dereg() {
183 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
184 SLPDereg( slapd_hslp,
190 #endif /* HAVE_SLP */
193 * Add a descriptor to daemon control
195 static void slapd_add(ber_socket_t s) {
196 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
198 assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
199 assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
200 assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
203 if (s >= slap_daemon.sd_nfds) {
204 slap_daemon.sd_nfds = s + 1;
208 FD_SET( s, &slap_daemon.sd_actives );
209 FD_SET( s, &slap_daemon.sd_readers );
212 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
213 "slapd_add: added %ld%s%s\n",
215 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
216 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" ));
218 Debug( LDAP_DEBUG_CONNS, "daemon: added %ld%s%s\n",
220 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
221 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
223 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
227 * Remove the descriptor from daemon control
229 void slapd_remove(ber_socket_t s, int wake) {
230 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
233 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
234 "slapd_remove: removing %ld%s%s\n",
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 );
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", LDAP_LEVEL_DETAIL1,
297 "slapd_close: closing %ld\n", (long)s ));
299 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
305 static void slap_free_listener_addresses(struct sockaddr **sal)
307 struct sockaddr **sap;
313 for (sap = sal; *sap != NULL; sap++) {
320 /* port = 0 indicates AF_LOCAL */
321 static int slap_get_listener_addresses(
324 struct sockaddr ***sal)
326 struct sockaddr **sap;
330 *sal = ch_malloc(2 * sizeof(void *));
336 *sap = ch_malloc(sizeof(struct sockaddr_un));
342 (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) ) {
344 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
345 "slap_get_listener_addresses: domain socket path (%s) too long in URL\n",
348 Debug( LDAP_DEBUG_ANY,
349 "daemon: domain socket path (%s) too long in URL",
355 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
356 (*sap)->sa_family = AF_LOCAL;
357 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
361 #ifdef HAVE_GETADDRINFO
362 struct addrinfo hints, *res, *sai;
366 memset( &hints, '\0', sizeof(hints) );
367 hints.ai_flags = AI_PASSIVE;
368 hints.ai_socktype = SOCK_STREAM;
369 hints.ai_family = AF_UNSPEC;
370 snprintf(serv, sizeof serv, "%d", port);
372 if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
374 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
375 "slap_get_listener_addresses: getaddrinfo failed: %s\n",
376 AC_GAI_STRERROR(err) ));
378 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
379 AC_GAI_STRERROR(err), 0, 0);
385 for (n=2; (sai = sai->ai_next) != NULL; n++) {
388 *sal = ch_calloc(n, sizeof(void *));
396 for ( sai=res; sai; sai=sai->ai_next ) {
397 if( sai->ai_addr == NULL ) {
399 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
400 "slap_get_listener_addresses: "
401 "getaddrinfo ai_addr is NULL?\n" ));
403 Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
404 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
410 switch (sai->ai_family) {
411 # ifdef LDAP_PF_INET6
413 *sap = ch_malloc(sizeof(struct sockaddr_in6));
418 *(struct sockaddr_in6 *)*sap =
419 *((struct sockaddr_in6 *)sai->ai_addr);
423 *sap = ch_malloc(sizeof(struct sockaddr_in));
428 *(struct sockaddr_in *)*sap =
429 *((struct sockaddr_in *)sai->ai_addr);
437 (*sap)->sa_family = sai->ai_family;
447 if ( host == NULL ) {
448 in.s_addr = htonl(INADDR_ANY);
450 } else if ( !inet_aton( host, &in ) ) {
451 struct hostent *he = gethostbyname( host );
454 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
455 "slap_get_listener_addresses: invalid host %s\n",
458 Debug( LDAP_DEBUG_ANY,
459 "daemon: invalid host %s", host, 0, 0);
463 AC_MEMCPY( &in, he->h_addr, sizeof( in ) );
466 *sal = ch_malloc(2 * sizeof(void *));
472 *sap = ch_malloc(sizeof(struct sockaddr_in));
478 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_in) );
479 (*sap)->sa_family = AF_INET;
480 ((struct sockaddr_in *)*sap)->sin_port = htons(port);
481 ((struct sockaddr_in *)*sap)->sin_addr = in;
488 slap_free_listener_addresses(*sal);
492 static Listener * slap_open_listener(
500 int err, addrlen = 0;
501 struct sockaddr **sal, **psal;
502 int socktype = SOCK_STREAM; /* default to COTS */
504 rc = ldap_url_parse( url, &lud );
506 if( rc != LDAP_URL_SUCCESS ) {
508 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
509 "slap_open_listener: listen URL \"%s\" parse error %d\n",
512 Debug( LDAP_DEBUG_ANY,
513 "daemon: listen URL \"%s\" parse error=%d\n",
520 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
522 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
523 "slap_open_listener: TLS is not supported (%s)\n",
526 Debug( LDAP_DEBUG_ANY,
527 "daemon: TLS not supported (%s)\n",
530 ldap_free_urldesc( lud );
534 if(! lud->lud_port ) {
535 lud->lud_port = LDAP_PORT;
539 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
541 if(! lud->lud_port ) {
542 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
546 port = (unsigned short) lud->lud_port;
548 tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme);
549 if ( tmp == LDAP_PROTO_IPC ) {
551 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
552 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
554 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
559 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
560 "slap_open_listener: URL scheme is not supported: %s\n",
563 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
566 ldap_free_urldesc( lud );
570 #ifdef LDAP_CONNECTIONLESS
571 l.sl_is_udp = ( tmp == LDAP_PROTO_UDP );
573 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
574 || strcmp(lud->lud_host, "*") == 0 )
576 err = slap_get_listener_addresses(NULL, port, &sal);
578 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
582 ldap_free_urldesc( lud );
588 while ( *sal != NULL ) {
589 switch( (*sal)->sa_family ) {
602 #ifdef LDAP_CONNECTIONLESS
604 socktype = SOCK_DGRAM;
606 l.sl_sd = socket( (*sal)->sa_family, socktype, 0);
607 if ( l.sl_sd == AC_SOCKET_INVALID ) {
608 int err = sock_errno();
610 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
611 "slap_open_listener: socket() failed errno=%d (%s)\n",
612 err, sock_errstr(err) ));
614 Debug( LDAP_DEBUG_ANY,
615 "daemon: socket() failed errno=%d (%s)\n", err,
616 sock_errstr(err), 0 );
622 if ( l.sl_sd >= dtblsize ) {
624 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
625 "slap_open_listener: listener descriptor %ld is too great %ld\n",
626 (long)l.sl_sd, (long)dtblsize ));
628 Debug( LDAP_DEBUG_ANY,
629 "daemon: listener descriptor %ld is too great %ld\n",
630 (long) l.sl_sd, (long) dtblsize, 0 );
632 tcp_close( l.sl_sd );
638 if ( (*sal)->sa_family == AF_LOCAL ) {
639 unlink ( ((struct sockaddr_un *)*sal)->sun_path );
644 /* enable address reuse */
646 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
647 (char *) &tmp, sizeof(tmp) );
648 if ( rc == AC_SOCKET_ERROR ) {
649 int err = sock_errno();
651 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
652 "slap_open_listener: setsockopt( %ld, SO_REUSEADDR ) failed errno %d (%s)\n",
653 (long)l.sl_sd, err, sock_errstr(err) ));
655 Debug( LDAP_DEBUG_ANY,
656 "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
657 (long) l.sl_sd, err, sock_errstr(err) );
663 switch( (*sal)->sa_family ) {
665 addrlen = sizeof(struct sockaddr_in);
669 addrlen = sizeof(struct sockaddr_in6);
674 addrlen = sizeof(struct sockaddr_un);
679 if (!bind(l.sl_sd, *sal, addrlen))
683 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
684 "slap_open_listener: bind(%ld) failed errno=%d (%s)\n",
685 (long)l.sl_sd, err, sock_errstr(err) ));
687 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
688 (long) l.sl_sd, err, sock_errstr(err) );
690 tcp_close( l.sl_sd );
692 } /* while ( *sal != NULL ) */
694 if ( *sal == NULL ) {
696 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
697 "slap_open_listener: bind(%ld) failed.\n", (long)l.sl_sd ));
699 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed\n",
700 (long) l.sl_sd, 0, 0 );
702 slap_free_listener_addresses(psal);
706 switch ( (*sal)->sa_family ) {
709 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
710 if ( chmod( addr, S_IRWXU ) < 0 ) {
711 int err = sock_errno();
713 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
714 "slap_open_listener: fchmod(%ld) failed errno=%d (%s)\n",
715 (long)l.sl_sd, err, sock_errstr(err) ));
717 Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
718 (long) l.sl_sd, err, sock_errstr(err) );
720 tcp_close( l.sl_sd );
721 slap_free_listener_addresses(psal);
724 l.sl_name = ch_malloc( strlen(addr) + sizeof("PATH=") );
725 sprintf( l.sl_name, "PATH=%s", addr );
727 #endif /* LDAP_PF_LOCAL */
731 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
732 char addr[INET_ADDRSTRLEN];
733 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
734 addr, sizeof(addr) );
737 s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr );
739 port = ((struct sockaddr_in *)*sal) ->sin_port;
740 l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65535") );
741 sprintf( l.sl_name, "IP=%s:%d",
742 s != NULL ? s : "unknown" , port );
747 char addr[INET6_ADDRSTRLEN];
748 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
750 port = ((struct sockaddr_in6 *)*sal)->sin6_port;
751 l.sl_name = ch_malloc( strlen(addr) + sizeof("IP= 65535") );
752 sprintf( l.sl_name, "IP=%s %d", addr, port );
754 #endif /* LDAP_PF_INET6 */
758 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
759 "slap_open_listener: unsupported address family (%d)\n",
760 (int)(*sal)->sa_family ));
762 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
763 (int) (*sal)->sa_family, 0, 0 );
768 slap_free_listener_addresses(psal);
770 l.sl_url = ch_strdup( url );
771 li = ch_malloc( sizeof( Listener ) );
775 LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
776 "slap_open_listener: daemon initialzed %s\n", l.sl_url ));
778 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
784 static int sockinit(void);
785 static int sockdestroy(void);
787 int slapd_daemon_init( const char *urls )
793 LDAP_LOG(( "connection", LDAP_LEVEL_ARGS,
794 "slapd_daemon_init: %s\n",
795 urls ? urls : "<null>" ));
797 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
798 urls ? urls : "<null>", 0, 0 );
800 if( (rc = sockinit()) != 0 ) {
805 dtblsize = sysconf( _SC_OPEN_MAX );
806 #elif HAVE_GETDTABLESIZE
807 dtblsize = getdtablesize();
809 dtblsize = FD_SETSIZE;
813 if(dtblsize > FD_SETSIZE) {
814 dtblsize = FD_SETSIZE;
816 #endif /* !FD_SETSIZE */
818 /* open a pipe (or something equivalent connected to itself).
819 * we write a byte on this fd whenever we catch a signal. The main
820 * loop will be select'ing on this socket, and will wake up when
823 if( (rc = lutil_pair( wake_sds )) < 0 ) {
825 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
826 "slap_daemon_init: lutil_pair() failed rc=%d\n", rc ));
828 Debug( LDAP_DEBUG_ANY,
829 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
834 FD_ZERO( &slap_daemon.sd_readers );
835 FD_ZERO( &slap_daemon.sd_writers );
841 u = str2charray( urls, " " );
843 if( u == NULL || u[0] == NULL ) {
845 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
846 "slap_daemon_init: no urls (%s) provided.\n", urls ));
848 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
854 for( i=0; u[i] != NULL; i++ ) {
856 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
857 "slap_daemon_init: listen on %s\n.", u[i] ));
859 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
866 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
867 "slap_daemon_init: no listeners to open (%s)\n", urls ));
869 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
877 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
878 "slap_daemon_init: %d listeners to open...\n", i ));
880 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
883 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
885 for(i = 0; u[i] != NULL; i++ ) {
886 slap_listeners[i] = slap_open_listener( u[i] );
888 if( slap_listeners[i] == NULL ) {
893 slap_listeners[i] = NULL;
896 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
897 "slap_daemon_init: %d listeners opened\n", i ));
899 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
904 slapd_slp_init( urls );
909 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
915 slapd_daemon_destroy(void)
917 connections_destroy();
918 tcp_close( wake_sds[1] );
919 tcp_close( wake_sds[0] );
937 time_t last_idle_check = 0;
940 if ( global_idletimeout > 0 ) {
941 last_idle_check = slap_get_time();
943 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
944 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
946 #ifdef LDAP_CONNECTIONLESS
947 /* Since this is connectionless, the data port is the
948 * listening port. The listen() and accept() calls
951 if ( slap_listeners[l]->sl_is_udp )
953 slapd_add( slap_listeners[l]->sl_sd );
958 if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN ) == -1 ) {
959 int err = sock_errno();
961 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
962 "slapd_daemon_task: listen( %s, 5 ) failed errno=%d (%s)\n",
963 slap_listeners[l]->sl_url, err, sock_errstr(err) ));
965 Debug( LDAP_DEBUG_ANY,
966 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
967 slap_listeners[l]->sl_url, err,
973 slapd_add( slap_listeners[l]->sl_sd );
976 #ifdef HAVE_NT_SERVICE_MANAGER
977 if ( started_event != NULL ) {
978 ldap_pvt_thread_cond_signal( &started_event );
981 /* initialization complete. Here comes the loop. */
983 while ( !slapd_shutdown ) {
988 #define SLAPD_EBADF_LIMIT 16
992 #define SLAPD_IDLE_CHECK_LIMIT 4
1000 #if defined(SLAPD_RLOOKUPS)
1003 struct timeval zero;
1004 struct timeval *tvp;
1007 now = slap_get_time();
1008 connections_timeout_idle( now );
1010 else if ( global_idletimeout > 0 ) {
1011 now = slap_get_time();
1012 if ( difftime( last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 ) {
1013 connections_timeout_idle( now );
1017 FD_ZERO( &writefds );
1018 FD_ZERO( &readfds );
1023 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1025 #ifdef FD_SET_MANUAL_COPY
1026 for( s = 0; s < nfds; s++ ) {
1027 if(FD_ISSET( &slap_sd_readers, s )) {
1028 FD_SET( s, &readfds );
1030 if(FD_ISSET( &slap_sd_writers, s )) {
1031 FD_SET( s, &writefds );
1035 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
1036 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
1038 assert(!FD_ISSET(wake_sds[0], &readfds));
1039 FD_SET( wake_sds[0], &readfds );
1041 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1042 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1044 if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
1045 FD_SET( slap_listeners[l]->sl_sd, &readfds );
1048 #ifndef HAVE_WINSOCK
1049 nfds = slap_daemon.sd_nfds;
1054 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1056 at = ldap_pvt_thread_pool_backload(&connection_pool);
1058 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
1061 tvp = at ? &zero : NULL;
1064 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1065 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1069 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1070 "slapd_daemon_task: select: listen=%d active_threads=%d tvp=%s\n",
1071 slap_listeners[l]->sl_sd, at, tvp == NULL ? "NULL" : "zero" ));
1073 Debug( LDAP_DEBUG_CONNS,
1074 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
1075 slap_listeners[l]->sl_sd, at,
1076 tvp == NULL ? "NULL" : "zero" );
1080 switch(ns = select( nfds, &readfds,
1082 /* don't pass empty fd_set */
1083 ( writefds.fd_count > 0 ? &writefds : NULL ),
1089 case -1: { /* failure - try again */
1090 int err = sock_errno();
1094 /* you'd think this would be EBADF */
1095 || err == WSAENOTSOCK
1098 if (++ebadf < SLAPD_EBADF_LIMIT)
1102 if( err != EINTR ) {
1104 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1105 "slapd_daemon_task: select failed (%d): %s\n",
1106 err, sock_errstr(err) ));
1108 Debug( LDAP_DEBUG_CONNS,
1109 "daemon: select failed (%d): %s\n",
1110 err, sock_errstr(err), 0 );
1112 slapd_shutdown = -1;
1117 case 0: /* timeout - let threads run */
1120 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1121 "slapd_daemon_task: select timeout - yielding\n" ));
1123 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
1126 ldap_pvt_thread_yield();
1129 default: /* something happened - deal with it */
1130 if( slapd_shutdown ) continue;
1134 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1135 "slapd_daemon_task: activity on %d descriptors\n", ns ));
1137 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
1143 if( FD_ISSET( wake_sds[0], &readfds ) ) {
1145 tcp_read( wake_sds[0], c, sizeof(c) );
1152 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1154 socklen_t len = sizeof(from);
1157 char *authid = NULL;
1159 char *dnsname = NULL;
1161 #ifdef LDAP_PF_LOCAL
1162 char peername[MAXPATHLEN + sizeof("PATH=")];
1163 #elif defined(LDAP_PF_INET6)
1164 char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
1166 char peername[sizeof("IP=255.255.255.255:65336")];
1167 #endif /* LDAP_PF_LOCAL */
1171 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1174 if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
1177 #ifdef LDAP_CONNECTIONLESS
1178 if ( slap_listeners[l]->sl_is_udp )
1180 /* The first time we receive a query, we set this
1181 * up as a "connection". It remains open for the life
1184 if ( slap_listeners[l]->sl_is_udp < 2 )
1186 id = connection_init(
1187 slap_listeners[l]->sl_sd,
1188 slap_listeners[l]->sl_url, "", "",
1189 slap_listeners[l]->sl_name,
1191 slap_listeners[l]->sl_is_udp++;
1197 s = accept( slap_listeners[l]->sl_sd,
1198 (struct sockaddr *) &from, &len );
1199 if ( s == AC_SOCKET_INVALID ) {
1200 int err = sock_errno();
1203 if( err == EMFILE ) {
1208 if( err == ENFILE ) {
1218 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1219 "slapd_daemon_task: accept(%ld) failed errno=%d (%s)\n",
1220 (long)slap_listeners[l]->sl_sd, err, sock_errstr(err) ));
1222 Debug( LDAP_DEBUG_ANY,
1223 "daemon: accept(%ld) failed errno=%d (%s)\n",
1224 (long) slap_listeners[l]->sl_sd, err,
1228 /* prevent busy loop */
1230 if( emfile % 4 == 3 ) usleep( 250 );
1232 if( emfile % 8 == 7 ) sleep( 1 );
1236 ldap_pvt_thread_yield();
1241 #ifndef HAVE_WINSOCK
1242 /* make sure descriptor number isn't too great */
1243 if ( s >= dtblsize ) {
1245 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1246 "slapd_daemon_task: %ld beyond descriptor table size %ld\n",
1247 (long)s, (long)dtblsize ));
1249 Debug( LDAP_DEBUG_ANY,
1250 "daemon: %ld beyond descriptor table size %ld\n",
1251 (long) s, (long) dtblsize, 0 );
1255 ldap_pvt_thread_yield();
1261 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1263 /* newly accepted stream should not be in any of the FD SETS */
1264 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
1265 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
1266 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
1268 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1271 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1272 #ifdef LDAP_PF_LOCAL
1273 /* for IPv4 and IPv6 sockets only */
1274 if ( from.sa_addr.sa_family != AF_LOCAL )
1275 #endif /* LDAP_PF_LOCAL */
1280 /* enable keep alives */
1282 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1283 (char *) &tmp, sizeof(tmp) );
1284 if ( rc == AC_SOCKET_ERROR ) {
1285 int err = sock_errno();
1287 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1288 "slapd_daemon_task: setsockopt( %ld, SO_KEEPALIVE) failed errno=%d (%s)\n",
1289 (long)s, err, sock_errstr(err) ));
1291 Debug( LDAP_DEBUG_ANY,
1292 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1293 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1298 /* enable no delay */
1300 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1301 (char *)&tmp, sizeof(tmp) );
1302 if ( rc == AC_SOCKET_ERROR ) {
1303 int err = sock_errno();
1305 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1306 "slapd_daemon_task: setsockopt( %ld, TCP_NODELAY) failed errno=%d (%s)\n",
1307 (long)s, err, sock_errstr(err) ));
1309 Debug( LDAP_DEBUG_ANY,
1310 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1311 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1319 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1320 "slapd_daemon_task: new connection on %ld\n", (long)s ));
1322 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
1325 switch ( from.sa_addr.sa_family ) {
1326 # ifdef LDAP_PF_LOCAL
1328 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1329 ssf = LDAP_PVT_SASL_LOCAL_SSF;
1331 #endif /* LDAP_PF_LOCAL */
1333 # ifdef LDAP_PF_INET6
1335 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1336 peeraddr = inet_ntoa( *((struct in_addr *)
1337 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1338 sprintf( peername, "IP=%s:%d",
1339 peeraddr != NULL ? peeraddr : "unknown",
1340 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1342 char addr[INET6_ADDRSTRLEN];
1343 sprintf( peername, "IP=%s %d",
1344 inet_ntop( AF_INET6,
1345 &from.sa_in6_addr.sin6_addr,
1346 addr, sizeof addr) ? addr : "unknown",
1347 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1350 # endif /* LDAP_PF_INET6 */
1353 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1354 sprintf( peername, "IP=%s:%d",
1355 peeraddr != NULL ? peeraddr : "unknown",
1356 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1364 if ( ( from.sa_addr.sa_family == AF_INET )
1365 #ifdef LDAP_PF_INET6
1366 || ( from.sa_addr.sa_family == AF_INET6 )
1369 #ifdef SLAPD_RLOOKUPS
1370 # ifdef LDAP_PF_INET6
1371 if ( from.sa_addr.sa_family == AF_INET6 )
1373 (char *)&(from.sa_in6_addr.sin6_addr),
1374 sizeof(from.sa_in6_addr.sin6_addr),
1377 # endif /* LDAP_PF_INET6 */
1379 (char *) &(from.sa_in_addr.sin_addr),
1380 sizeof(from.sa_in_addr.sin_addr),
1382 dnsname = hp ? ldap_pvt_str2lower( hp->h_name ) : NULL;
1385 #endif /* SLAPD_RLOOKUPS */
1388 if ( !hosts_ctl("slapd",
1389 dnsname != NULL ? dnsname : STRING_UNKNOWN,
1390 peeraddr != NULL ? peeraddr : STRING_UNKNOWN,
1394 Statslog( LDAP_DEBUG_ANY,
1395 "fd=%ld host access from %s (%s) denied.\n",
1397 dnsname != NULL ? dnsname : "unknown",
1398 peeraddr != NULL ? peeraddr : "unknown",
1403 #endif /* HAVE_TCPD */
1406 id = connection_init(s,
1407 slap_listeners[l]->sl_url,
1408 dnsname != NULL ? dnsname : "unknown",
1410 slap_listeners[l]->sl_name,
1412 slap_listeners[l]->sl_is_tls,
1419 if( authid ) ch_free(authid);
1423 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1424 "slapd_daemon_task: connection_init(%ld, %s, %s) failed.\n",
1425 (long)s, peername, slap_listeners[l]->sl_name ));
1427 Debug( LDAP_DEBUG_ANY,
1428 "daemon: connection_init(%ld, %s, %s) failed.\n",
1431 slap_listeners[l]->sl_name );
1437 Statslog( LDAP_DEBUG_STATS,
1438 "daemon: conn=%ld fd=%ld connection from %s (%s) accepted.\n",
1441 slap_listeners[l]->sl_name,
1450 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1451 "slapd_daemon_task: activity on " ));
1453 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1456 for ( i = 0; i < readfds.fd_count; i++ ) {
1458 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1459 " %d%s", readfds.fd_array[i], "r", 0 ));
1461 Debug( LDAP_DEBUG_CONNS, " %d%s",
1462 readfds.fd_array[i], "r", 0 );
1465 for ( i = 0; i < writefds.fd_count; i++ ) {
1467 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1468 " %d%s", writefds.fd_array[i], "w" ));
1470 Debug( LDAP_DEBUG_CONNS, " %d%s",
1471 writefds.fd_array[i], "w", 0 );
1476 for ( i = 0; i < nfds; i++ ) {
1478 int is_listener = 0;
1480 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1481 if ( i == slap_listeners[l]->sl_sd ) {
1482 #ifdef LDAP_CONNECTIONLESS
1483 /* The listener is the data port. Don't
1486 if (slap_listeners[l]->sl_is_udp)
1493 if ( is_listener ) {
1496 r = FD_ISSET( i, &readfds );
1497 w = FD_ISSET( i, &writefds );
1500 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1502 r ? "r" : "", w ? "w" : "" ));
1504 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1505 r ? "r" : "", w ? "w" : "" );
1511 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2, "\n" ));
1513 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1518 /* loop through the writers */
1520 for ( i = 0; i < writefds.fd_count; i++ )
1522 for ( i = 0; i < nfds; i++ )
1526 int is_listener = 0;
1528 wd = writefds.fd_array[i];
1530 if( ! FD_ISSET( i, &writefds ) ) {
1536 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1537 if ( i == slap_listeners[l]->sl_sd ) {
1538 #ifdef LDAP_CONNECTIONLESS
1539 if (slap_listeners[l]->sl_is_udp)
1546 if ( is_listener ) {
1550 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1551 "slapd_daemon_task: write active on %d\n", wd ));
1553 Debug( LDAP_DEBUG_CONNS,
1554 "daemon: write active on %d\n",
1558 * NOTE: it is possible that the connection was closed
1559 * and that the stream is now inactive.
1560 * connection_write() must valid the stream is still
1564 if ( connection_write( wd ) < 0 ) {
1565 FD_CLR( (unsigned) wd, &readfds );
1571 for ( i = 0; i < readfds.fd_count; i++ )
1573 for ( i = 0; i < nfds; i++ )
1577 int is_listener = 0;
1580 rd = readfds.fd_array[i];
1582 if( ! FD_ISSET( i, &readfds ) ) {
1588 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1589 if ( rd == slap_listeners[l]->sl_sd ) {
1590 #ifdef LDAP_CONNECTIONLESS
1591 if (slap_listeners[l]->sl_is_udp)
1598 if ( is_listener ) {
1603 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1604 "slapd_daemon_task: read activity on %d\n", rd ));
1606 Debug ( LDAP_DEBUG_CONNS,
1607 "daemon: read activity on %d\n", rd, 0, 0 );
1610 * NOTE: it is possible that the connection was closed
1611 * and that the stream is now inactive.
1612 * connection_read() must valid the stream is still
1616 if ( connection_read( rd ) < 0 ) {
1620 ldap_pvt_thread_yield();
1623 if( slapd_shutdown > 0 ) {
1625 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1626 "slapd_daemon_task: shutdown requested and initiated.\n"));
1628 Debug( LDAP_DEBUG_TRACE,
1629 "daemon: shutdown requested and initiated.\n",
1633 } else if ( slapd_shutdown < 0 ) {
1634 #ifdef HAVE_NT_SERVICE_MANAGER
1635 if (slapd_shutdown == -1)
1638 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1639 "slapd_daemon_task: shutdown initiated by Service Manager.\n"));
1641 Debug( LDAP_DEBUG_TRACE,
1642 "daemon: shutdown initiated by Service Manager.\n",
1650 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1651 "slapd_daemon_task: abnormal condition, shutdown initiated.\n" ));
1653 Debug( LDAP_DEBUG_TRACE,
1654 "daemon: abnormal condition, shutdown initiated.\n",
1660 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1661 "slapd_daemon_task: no active streams, shutdown initiated.\n" ));
1663 Debug( LDAP_DEBUG_TRACE,
1664 "daemon: no active streams, shutdown initiated.\n",
1669 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1670 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1671 #ifdef LDAP_PF_LOCAL
1672 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1673 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1675 #endif /* LDAP_PF_LOCAL */
1676 slapd_close( slap_listeners[l]->sl_sd );
1678 if ( slap_listeners[l]->sl_url )
1679 free ( slap_listeners[l]->sl_url );
1680 if ( slap_listeners[l]->sl_name )
1681 free ( slap_listeners[l]->sl_name );
1682 free ( slap_listeners[l] );
1684 free ( slap_listeners );
1685 slap_listeners = NULL;
1688 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1689 "slapd_daemon_task: shutdown waiting for %d threads to terminate.\n",
1690 ldap_pvt_thread_pool_backload(&connection_pool) ));
1692 Debug( LDAP_DEBUG_ANY,
1693 "slapd shutdown: waiting for %d threads to terminate\n",
1694 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1696 ldap_pvt_thread_pool_destroy(&connection_pool, 1);
1702 int slapd_daemon( void )
1708 #define SLAPD_LISTENER_THREAD 1
1709 #if defined( SLAPD_LISTENER_THREAD )
1711 ldap_pvt_thread_t listener_tid;
1713 /* listener as a separate THREAD */
1714 rc = ldap_pvt_thread_create( &listener_tid,
1715 0, slapd_daemon_task, NULL );
1719 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1720 "slapd_daemon: listener ldap_pvt_thread_create failed (%d).\n", rc ));
1722 Debug( LDAP_DEBUG_ANY,
1723 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
1728 /* wait for the listener thread to complete */
1729 ldap_pvt_thread_join( listener_tid, (void *) NULL );
1732 /* experimental code */
1733 slapd_daemon_task( NULL );
1742 #if defined( HAVE_WINSOCK2 )
1743 WORD wVersionRequested;
1747 wVersionRequested = MAKEWORD( 2, 0 );
1749 err = WSAStartup( wVersionRequested, &wsaData );
1751 /* Tell the user that we couldn't find a usable */
1756 /* Confirm that the WinSock DLL supports 2.0.*/
1757 /* Note that if the DLL supports versions greater */
1758 /* than 2.0 in addition to 2.0, it will still return */
1759 /* 2.0 in wVersion since that is the version we */
1762 if ( LOBYTE( wsaData.wVersion ) != 2 ||
1763 HIBYTE( wsaData.wVersion ) != 0 )
1765 /* Tell the user that we couldn't find a usable */
1771 /* The WinSock DLL is acceptable. Proceed. */
1772 #elif defined( HAVE_WINSOCK )
1774 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
1781 int sockdestroy(void)
1783 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
1790 slap_sig_shutdown( int sig )
1793 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1794 "slap_sig_shutdown: signal %d\n", sig ));
1796 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
1800 * If the NT Service Manager is controlling the server, we don't
1801 * want SIGBREAK to kill the server. For some strange reason,
1802 * SIGBREAK is generated when a user logs out.
1805 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
1806 if (is_NT_Service && sig == SIGBREAK)
1808 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1809 "slap_sig_shutdown: SIGBREAK ignored.\n" ));
1811 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: SIGBREAK ignored.\n",
1816 slapd_shutdown = sig;
1820 /* reinstall self */
1821 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
1825 slap_sig_wake( int sig )
1829 /* reinstall self */
1830 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
1834 void slapd_add_internal(ber_socket_t s) {