3 * Copyright 1998-2002 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 Listener **slap_listeners = NULL;
41 #define SLAPD_LISTEN 10
43 static ber_socket_t wake_sds[2];
47 #define WAKE_LISTENER(w) \
48 ((w && !waking) ? tcp_write( wake_sds[1], "0", 1 ), waking=1 : 0)
50 #define WAKE_LISTENER(w) \
51 do { if (w) tcp_write( wake_sds[1], "0", 1 ); } while(0)
57 volatile sig_atomic_t slapd_shutdown = 0;
59 static struct slap_daemon {
60 ldap_pvt_thread_mutex_t sd_mutex;
65 /* In winsock, accept() returns values higher than dtblsize
66 so don't bother with this optimization */
79 * SLP related functions
83 #define LDAP_SRVTYPE_PREFIX "service:ldap://"
84 #define LDAPS_SRVTYPE_PREFIX "service:ldaps://"
85 static char** slapd_srvurls = NULL;
86 static SLPHandle slapd_hslp = 0;
88 void slapd_slp_init( const char* urls ) {
91 slapd_srvurls = str2charray( urls, " " );
93 if( slapd_srvurls == NULL ) return;
95 /* find and expand INADDR_ANY URLs */
96 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
97 if( strcmp( slapd_srvurls[i], "ldap:///" ) == 0) {
98 char *host = ldap_pvt_get_fqdn( NULL );
100 slapd_srvurls[i] = (char *) realloc( slapd_srvurls[i],
102 sizeof( LDAP_SRVTYPE_PREFIX ) );
103 strcpy( slap_strcopy(slapd_srvurls[i],
104 LDAP_SRVTYPE_PREFIX ), host );
109 } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0) {
110 char *host = ldap_pvt_get_fqdn( NULL );
111 if ( host != NULL ) {
112 slapd_srvurls[i] = (char *) realloc( slapd_srvurls[i],
114 sizeof( LDAPS_SRVTYPE_PREFIX ) );
115 strcpy( slap_strcopy(slapd_srvurls[i],
116 LDAPS_SRVTYPE_PREFIX ), host );
123 /* open the SLP handle */
124 SLPOpen( "en", 0, &slapd_hslp );
127 void slapd_slp_deinit() {
128 if( slapd_srvurls == NULL ) return;
130 charray_free( slapd_srvurls );
131 slapd_srvurls = NULL;
133 /* close the SLP handle */
134 SLPClose( slapd_hslp );
137 void slapd_slp_regreport(
145 void slapd_slp_reg() {
148 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
149 if( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
150 sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
151 strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
152 sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
156 SLP_LIFETIME_MAXIMUM,
166 void slapd_slp_dereg() {
169 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
170 SLPDereg( slapd_hslp,
176 #endif /* HAVE_SLP */
179 * Add a descriptor to daemon control
181 static void slapd_add(ber_socket_t s) {
182 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
184 assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
185 assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
186 assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
189 if (s >= slap_daemon.sd_nfds) {
190 slap_daemon.sd_nfds = s + 1;
194 FD_SET( s, &slap_daemon.sd_actives );
195 FD_SET( s, &slap_daemon.sd_readers );
198 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
199 "slapd_add: added %ld%s%s\n",
201 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
202 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" ));
204 Debug( LDAP_DEBUG_CONNS, "daemon: added %ld%s%s\n",
206 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
207 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
209 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
213 * Remove the descriptor from daemon control
215 void slapd_remove(ber_socket_t s, int wake) {
216 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
219 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
220 "slapd_remove: removing %ld%s%s\n",
222 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
223 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" ));
225 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
227 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
228 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
230 FD_CLR( s, &slap_daemon.sd_actives );
231 FD_CLR( s, &slap_daemon.sd_readers );
232 FD_CLR( s, &slap_daemon.sd_writers );
234 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
238 void slapd_clr_write(ber_socket_t s, int wake) {
239 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
241 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
242 FD_CLR( s, &slap_daemon.sd_writers );
244 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
248 void slapd_set_write(ber_socket_t s, int wake) {
249 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
251 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
252 if (!FD_ISSET(s, &slap_daemon.sd_writers))
253 FD_SET( (unsigned) s, &slap_daemon.sd_writers );
255 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
259 void slapd_clr_read(ber_socket_t s, int wake) {
260 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
262 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
263 FD_CLR( s, &slap_daemon.sd_readers );
265 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
269 void slapd_set_read(ber_socket_t s, int wake) {
270 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
272 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
273 if (!FD_ISSET(s, &slap_daemon.sd_readers))
274 FD_SET( s, &slap_daemon.sd_readers );
276 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
280 static void slapd_close(ber_socket_t s) {
282 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
283 "slapd_close: closing %ld\n", (long)s ));
285 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
291 static void slap_free_listener_addresses(struct sockaddr **sal)
293 struct sockaddr **sap;
299 for (sap = sal; *sap != NULL; sap++) {
306 /* port = 0 indicates AF_LOCAL */
307 static int slap_get_listener_addresses(
310 struct sockaddr ***sal)
312 struct sockaddr **sap;
316 *sal = ch_malloc(2 * sizeof(void *));
322 *sap = ch_malloc(sizeof(struct sockaddr_un));
328 (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) ) {
330 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
331 "slap_get_listener_addresses: domain socket path (%s) too long in URL\n",
334 Debug( LDAP_DEBUG_ANY,
335 "daemon: domain socket path (%s) too long in URL",
341 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
342 (*sap)->sa_family = AF_LOCAL;
343 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
347 #ifdef HAVE_GETADDRINFO
348 struct addrinfo hints, *res, *sai;
352 memset( &hints, '\0', sizeof(hints) );
353 hints.ai_flags = AI_PASSIVE;
354 hints.ai_socktype = SOCK_STREAM;
355 hints.ai_family = AF_UNSPEC;
356 snprintf(serv, sizeof serv, "%d", port);
358 if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
360 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
361 "slap_get_listener_addresses: getaddrinfo failed: %s\n",
362 AC_GAI_STRERROR(err) ));
364 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
365 AC_GAI_STRERROR(err), 0, 0);
371 for (n=2; (sai = sai->ai_next) != NULL; n++) {
374 *sal = ch_calloc(n, sizeof(void *));
382 for ( sai=res; sai; sai=sai->ai_next ) {
383 if( sai->ai_addr == NULL ) {
385 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
386 "slap_get_listener_addresses: "
387 "getaddrinfo ai_addr is NULL?\n" ));
389 Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
390 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
396 switch (sai->ai_family) {
397 # ifdef LDAP_PF_INET6
399 *sap = ch_malloc(sizeof(struct sockaddr_in6));
404 *(struct sockaddr_in6 *)*sap =
405 *((struct sockaddr_in6 *)sai->ai_addr);
409 *sap = ch_malloc(sizeof(struct sockaddr_in));
414 *(struct sockaddr_in *)*sap =
415 *((struct sockaddr_in *)sai->ai_addr);
423 (*sap)->sa_family = sai->ai_family;
433 if ( host == NULL ) {
434 in.s_addr = htonl(INADDR_ANY);
436 } else if ( !inet_aton( host, &in ) ) {
437 struct hostent *he = gethostbyname( host );
440 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
441 "slap_get_listener_addresses: invalid host %s\n",
444 Debug( LDAP_DEBUG_ANY,
445 "daemon: invalid host %s", host, 0, 0);
449 AC_MEMCPY( &in, he->h_addr, sizeof( in ) );
452 *sal = ch_malloc(2 * sizeof(void *));
458 *sap = ch_malloc(sizeof(struct sockaddr_in));
464 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_in) );
465 (*sap)->sa_family = AF_INET;
466 ((struct sockaddr_in *)*sap)->sin_port = htons(port);
467 ((struct sockaddr_in *)*sap)->sin_addr = in;
474 slap_free_listener_addresses(*sal);
478 static Listener * slap_open_listener(
486 int err, addrlen = 0;
487 struct sockaddr **sal, **psal;
488 int socktype = SOCK_STREAM; /* default to COTS */
490 rc = ldap_url_parse( url, &lud );
492 if( rc != LDAP_URL_SUCCESS ) {
494 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
495 "slap_open_listener: listen URL \"%s\" parse error %d\n",
498 Debug( LDAP_DEBUG_ANY,
499 "daemon: listen URL \"%s\" parse error=%d\n",
506 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
508 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
509 "slap_open_listener: TLS is not supported (%s)\n",
512 Debug( LDAP_DEBUG_ANY,
513 "daemon: TLS not supported (%s)\n",
516 ldap_free_urldesc( lud );
520 if(! lud->lud_port ) {
521 lud->lud_port = LDAP_PORT;
525 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
527 if(! lud->lud_port ) {
528 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
532 port = (unsigned short) lud->lud_port;
534 tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme);
535 if ( tmp == LDAP_PROTO_IPC ) {
537 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
538 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
540 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
545 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
546 "slap_open_listener: URL scheme is not supported: %s\n",
549 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
552 ldap_free_urldesc( lud );
556 #ifdef LDAP_CONNECTIONLESS
557 l.sl_is_udp = ( tmp == LDAP_PROTO_UDP );
559 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
560 || strcmp(lud->lud_host, "*") == 0 )
562 err = slap_get_listener_addresses(NULL, port, &sal);
564 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
568 ldap_free_urldesc( lud );
574 while ( *sal != NULL ) {
575 switch( (*sal)->sa_family ) {
588 #ifdef LDAP_CONNECTIONLESS
590 socktype = SOCK_DGRAM;
592 l.sl_sd = socket( (*sal)->sa_family, socktype, 0);
593 if ( l.sl_sd == AC_SOCKET_INVALID ) {
594 int err = sock_errno();
596 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
597 "slap_open_listener: socket() failed errno=%d (%s)\n",
598 err, sock_errstr(err) ));
600 Debug( LDAP_DEBUG_ANY,
601 "daemon: socket() failed errno=%d (%s)\n", err,
602 sock_errstr(err), 0 );
608 if ( l.sl_sd >= dtblsize ) {
610 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
611 "slap_open_listener: listener descriptor %ld is too great %ld\n",
612 (long)l.sl_sd, (long)dtblsize ));
614 Debug( LDAP_DEBUG_ANY,
615 "daemon: listener descriptor %ld is too great %ld\n",
616 (long) l.sl_sd, (long) dtblsize, 0 );
618 tcp_close( l.sl_sd );
624 if ( (*sal)->sa_family == AF_LOCAL ) {
625 unlink ( ((struct sockaddr_un *)*sal)->sun_path );
630 /* enable address reuse */
632 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
633 (char *) &tmp, sizeof(tmp) );
634 if ( rc == AC_SOCKET_ERROR ) {
635 int err = sock_errno();
637 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
638 "slap_open_listener: setsockopt( %ld, SO_REUSEADDR ) failed errno %d (%s)\n",
639 (long)l.sl_sd, err, sock_errstr(err) ));
641 Debug( LDAP_DEBUG_ANY,
642 "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
643 (long) l.sl_sd, err, sock_errstr(err) );
649 switch( (*sal)->sa_family ) {
651 addrlen = sizeof(struct sockaddr_in);
655 addrlen = sizeof(struct sockaddr_in6);
660 addrlen = sizeof(struct sockaddr_un);
665 if (!bind(l.sl_sd, *sal, addrlen))
669 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
670 "slap_open_listener: bind(%ld) failed errno=%d (%s)\n",
671 (long)l.sl_sd, err, sock_errstr(err) ));
673 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
674 (long) l.sl_sd, err, sock_errstr(err) );
676 tcp_close( l.sl_sd );
678 } /* while ( *sal != NULL ) */
680 if ( *sal == NULL ) {
682 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
683 "slap_open_listener: bind(%ld) failed.\n", (long)l.sl_sd ));
685 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed\n",
686 (long) l.sl_sd, 0, 0 );
688 slap_free_listener_addresses(psal);
692 switch ( (*sal)->sa_family ) {
695 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
696 if ( chmod( addr, S_IRWXU ) < 0 ) {
697 int err = sock_errno();
699 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
700 "slap_open_listener: fchmod(%ld) failed errno=%d (%s)\n",
701 (long)l.sl_sd, err, sock_errstr(err) ));
703 Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
704 (long) l.sl_sd, err, sock_errstr(err) );
706 tcp_close( l.sl_sd );
707 slap_free_listener_addresses(psal);
710 l.sl_name = ch_malloc( strlen(addr) + sizeof("PATH=") );
711 sprintf( l.sl_name, "PATH=%s", addr );
713 #endif /* LDAP_PF_LOCAL */
717 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
718 char addr[INET_ADDRSTRLEN];
719 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
720 addr, sizeof(addr) );
723 s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr );
725 port = ((struct sockaddr_in *)*sal) ->sin_port;
726 l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65535") );
727 sprintf( l.sl_name, "IP=%s:%d",
728 s != NULL ? s : "unknown" , port );
733 char addr[INET6_ADDRSTRLEN];
734 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
736 port = ((struct sockaddr_in6 *)*sal)->sin6_port;
737 l.sl_name = ch_malloc( strlen(addr) + sizeof("IP= 65535") );
738 sprintf( l.sl_name, "IP=%s %d", addr, port );
740 #endif /* LDAP_PF_INET6 */
744 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
745 "slap_open_listener: unsupported address family (%d)\n",
746 (int)(*sal)->sa_family ));
748 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
749 (int) (*sal)->sa_family, 0, 0 );
754 slap_free_listener_addresses(psal);
756 l.sl_url = ch_strdup( url );
757 li = ch_malloc( sizeof( Listener ) );
761 LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
762 "slap_open_listener: daemon initialzed %s\n", l.sl_url ));
764 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
770 static int sockinit(void);
771 static int sockdestroy(void);
773 int slapd_daemon_init( const char *urls )
779 LDAP_LOG(( "connection", LDAP_LEVEL_ARGS,
780 "slapd_daemon_init: %s\n",
781 urls ? urls : "<null>" ));
783 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
784 urls ? urls : "<null>", 0, 0 );
786 if( (rc = sockinit()) != 0 ) {
791 dtblsize = sysconf( _SC_OPEN_MAX );
792 #elif HAVE_GETDTABLESIZE
793 dtblsize = getdtablesize();
795 dtblsize = FD_SETSIZE;
799 if(dtblsize > FD_SETSIZE) {
800 dtblsize = FD_SETSIZE;
802 #endif /* !FD_SETSIZE */
804 /* open a pipe (or something equivalent connected to itself).
805 * we write a byte on this fd whenever we catch a signal. The main
806 * loop will be select'ing on this socket, and will wake up when
809 if( (rc = lutil_pair( wake_sds )) < 0 ) {
811 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
812 "slap_daemon_init: lutil_pair() failed rc=%d\n", rc ));
814 Debug( LDAP_DEBUG_ANY,
815 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
820 FD_ZERO( &slap_daemon.sd_readers );
821 FD_ZERO( &slap_daemon.sd_writers );
827 u = str2charray( urls, " " );
829 if( u == NULL || u[0] == NULL ) {
831 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
832 "slap_daemon_init: no urls (%s) provided.\n", urls ));
834 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
840 for( i=0; u[i] != NULL; i++ ) {
842 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
843 "slap_daemon_init: listen on %s\n.", u[i] ));
845 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
852 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
853 "slap_daemon_init: no listeners to open (%s)\n", urls ));
855 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
863 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
864 "slap_daemon_init: %d listeners to open...\n", i ));
866 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
869 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
871 for(i = 0; u[i] != NULL; i++ ) {
872 slap_listeners[i] = slap_open_listener( u[i] );
874 if( slap_listeners[i] == NULL ) {
879 slap_listeners[i] = NULL;
882 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
883 "slap_daemon_init: %d listeners opened\n", i ));
885 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
890 slapd_slp_init( urls );
895 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
901 slapd_daemon_destroy(void)
903 connections_destroy();
904 tcp_close( wake_sds[1] );
905 tcp_close( wake_sds[0] );
923 time_t last_idle_check = 0;
926 if ( global_idletimeout > 0 ) {
927 last_idle_check = slap_get_time();
929 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
930 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
932 #ifdef LDAP_CONNECTIONLESS
933 /* Since this is connectionless, the data port is the
934 * listening port. The listen() and accept() calls
937 if ( slap_listeners[l]->sl_is_udp )
939 slapd_add( slap_listeners[l]->sl_sd );
944 if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN ) == -1 ) {
945 int err = sock_errno();
947 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
948 "slapd_daemon_task: listen( %s, 5 ) failed errno=%d (%s)\n",
949 slap_listeners[l]->sl_url, err, sock_errstr(err) ));
951 Debug( LDAP_DEBUG_ANY,
952 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
953 slap_listeners[l]->sl_url, err,
959 slapd_add( slap_listeners[l]->sl_sd );
962 #ifdef HAVE_NT_SERVICE_MANAGER
963 if ( started_event != NULL ) {
964 ldap_pvt_thread_cond_signal( &started_event );
967 /* initialization complete. Here comes the loop. */
969 while ( !slapd_shutdown ) {
974 #define SLAPD_EBADF_LIMIT 16
978 #define SLAPD_IDLE_CHECK_LIMIT 4
986 #if defined(SLAPD_RLOOKUPS)
993 now = slap_get_time();
994 connections_timeout_idle( now );
996 else if ( global_idletimeout > 0 ) {
997 now = slap_get_time();
998 if ( difftime( last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 ) {
999 connections_timeout_idle( now );
1003 FD_ZERO( &writefds );
1004 FD_ZERO( &readfds );
1009 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1011 #ifdef FD_SET_MANUAL_COPY
1012 for( s = 0; s < nfds; s++ ) {
1013 if(FD_ISSET( &slap_sd_readers, s )) {
1014 FD_SET( s, &readfds );
1016 if(FD_ISSET( &slap_sd_writers, s )) {
1017 FD_SET( s, &writefds );
1021 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
1022 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
1024 assert(!FD_ISSET(wake_sds[0], &readfds));
1025 FD_SET( wake_sds[0], &readfds );
1027 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1028 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1030 if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
1031 FD_SET( slap_listeners[l]->sl_sd, &readfds );
1034 #ifndef HAVE_WINSOCK
1035 nfds = slap_daemon.sd_nfds;
1040 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1042 at = ldap_pvt_thread_pool_backload(&connection_pool);
1044 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
1047 tvp = at ? &zero : NULL;
1050 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1051 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1055 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1056 "slapd_daemon_task: select: listen=%d active_threads=%d tvp=%s\n",
1057 slap_listeners[l]->sl_sd, at, tvp == NULL ? "NULL" : "zero" ));
1059 Debug( LDAP_DEBUG_CONNS,
1060 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
1061 slap_listeners[l]->sl_sd, at,
1062 tvp == NULL ? "NULL" : "zero" );
1066 switch(ns = select( nfds, &readfds,
1068 /* don't pass empty fd_set */
1069 ( writefds.fd_count > 0 ? &writefds : NULL ),
1075 case -1: { /* failure - try again */
1076 int err = sock_errno();
1080 /* you'd think this would be EBADF */
1081 || err == WSAENOTSOCK
1084 if (++ebadf < SLAPD_EBADF_LIMIT)
1088 if( err != EINTR ) {
1090 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1091 "slapd_daemon_task: select failed (%d): %s\n",
1092 err, sock_errstr(err) ));
1094 Debug( LDAP_DEBUG_CONNS,
1095 "daemon: select failed (%d): %s\n",
1096 err, sock_errstr(err), 0 );
1098 slapd_shutdown = -1;
1103 case 0: /* timeout - let threads run */
1106 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1107 "slapd_daemon_task: select timeout - yielding\n" ));
1109 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
1112 ldap_pvt_thread_yield();
1115 default: /* something happened - deal with it */
1116 if( slapd_shutdown ) continue;
1120 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1121 "slapd_daemon_task: activity on %d descriptors\n", ns ));
1123 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
1129 if( FD_ISSET( wake_sds[0], &readfds ) ) {
1131 tcp_read( wake_sds[0], c, sizeof(c) );
1138 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1140 socklen_t len = sizeof(from);
1143 char *authid = NULL;
1145 char *dnsname = NULL;
1147 #ifdef LDAP_PF_LOCAL
1148 char peername[MAXPATHLEN + sizeof("PATH=")];
1149 #elif defined(LDAP_PF_INET6)
1150 char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
1152 char peername[sizeof("IP=255.255.255.255:65336")];
1153 #endif /* LDAP_PF_LOCAL */
1157 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1160 if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
1163 #ifdef LDAP_CONNECTIONLESS
1164 if ( slap_listeners[l]->sl_is_udp )
1166 /* The first time we receive a query, we set this
1167 * up as a "connection". It remains open for the life
1170 if ( slap_listeners[l]->sl_is_udp < 2 )
1172 id = connection_init(
1173 slap_listeners[l]->sl_sd,
1174 slap_listeners[l]->sl_url, "", "",
1175 slap_listeners[l]->sl_name,
1177 slap_listeners[l]->sl_is_udp++;
1183 s = accept( slap_listeners[l]->sl_sd,
1184 (struct sockaddr *) &from, &len );
1185 if ( s == AC_SOCKET_INVALID ) {
1186 int err = sock_errno();
1189 if( err == EMFILE ) {
1194 if( err == ENFILE ) {
1204 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1205 "slapd_daemon_task: accept(%ld) failed errno=%d (%s)\n",
1206 (long)slap_listeners[l]->sl_sd, err, sock_errstr(err) ));
1208 Debug( LDAP_DEBUG_ANY,
1209 "daemon: accept(%ld) failed errno=%d (%s)\n",
1210 (long) slap_listeners[l]->sl_sd, err,
1214 /* prevent busy loop */
1216 if( emfile % 4 == 3 ) usleep( 250 );
1218 if( emfile % 8 == 7 ) sleep( 1 );
1222 ldap_pvt_thread_yield();
1227 #ifndef HAVE_WINSOCK
1228 /* make sure descriptor number isn't too great */
1229 if ( s >= dtblsize ) {
1231 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1232 "slapd_daemon_task: %ld beyond descriptor table size %ld\n",
1233 (long)s, (long)dtblsize ));
1235 Debug( LDAP_DEBUG_ANY,
1236 "daemon: %ld beyond descriptor table size %ld\n",
1237 (long) s, (long) dtblsize, 0 );
1241 ldap_pvt_thread_yield();
1247 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1249 /* newly accepted stream should not be in any of the FD SETS */
1250 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
1251 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
1252 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
1254 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1257 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1258 #ifdef LDAP_PF_LOCAL
1259 /* for IPv4 and IPv6 sockets only */
1260 if ( from.sa_addr.sa_family != AF_LOCAL )
1261 #endif /* LDAP_PF_LOCAL */
1266 /* enable keep alives */
1268 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1269 (char *) &tmp, sizeof(tmp) );
1270 if ( rc == AC_SOCKET_ERROR ) {
1271 int err = sock_errno();
1273 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1274 "slapd_daemon_task: setsockopt( %ld, SO_KEEPALIVE) failed errno=%d (%s)\n",
1275 (long)s, err, sock_errstr(err) ));
1277 Debug( LDAP_DEBUG_ANY,
1278 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1279 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1284 /* enable no delay */
1286 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1287 (char *)&tmp, sizeof(tmp) );
1288 if ( rc == AC_SOCKET_ERROR ) {
1289 int err = sock_errno();
1291 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1292 "slapd_daemon_task: setsockopt( %ld, TCP_NODELAY) failed errno=%d (%s)\n",
1293 (long)s, err, sock_errstr(err) ));
1295 Debug( LDAP_DEBUG_ANY,
1296 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1297 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1305 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1306 "slapd_daemon_task: new connection on %ld\n", (long)s ));
1308 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
1311 switch ( from.sa_addr.sa_family ) {
1312 # ifdef LDAP_PF_LOCAL
1314 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1315 ssf = LDAP_PVT_SASL_LOCAL_SSF;
1317 #endif /* LDAP_PF_LOCAL */
1319 # ifdef LDAP_PF_INET6
1321 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1322 peeraddr = inet_ntoa( *((struct in_addr *)
1323 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1324 sprintf( peername, "IP=%s:%d",
1325 peeraddr != NULL ? peeraddr : "unknown",
1326 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1328 char addr[INET6_ADDRSTRLEN];
1329 sprintf( peername, "IP=%s %d",
1330 inet_ntop( AF_INET6,
1331 &from.sa_in6_addr.sin6_addr,
1332 addr, sizeof addr) ? addr : "unknown",
1333 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1336 # endif /* LDAP_PF_INET6 */
1339 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1340 sprintf( peername, "IP=%s:%d",
1341 peeraddr != NULL ? peeraddr : "unknown",
1342 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1350 if ( ( from.sa_addr.sa_family == AF_INET )
1351 #ifdef LDAP_PF_INET6
1352 || ( from.sa_addr.sa_family == AF_INET6 )
1355 #ifdef SLAPD_RLOOKUPS
1356 # ifdef LDAP_PF_INET6
1357 if ( from.sa_addr.sa_family == AF_INET6 )
1359 (char *)&(from.sa_in6_addr.sin6_addr),
1360 sizeof(from.sa_in6_addr.sin6_addr),
1363 # endif /* LDAP_PF_INET6 */
1365 (char *) &(from.sa_in_addr.sin_addr),
1366 sizeof(from.sa_in_addr.sin_addr),
1368 dnsname = hp ? ldap_pvt_str2lower( hp->h_name ) : NULL;
1371 #endif /* SLAPD_RLOOKUPS */
1374 if ( !hosts_ctl("slapd",
1375 dnsname != NULL ? dnsname : STRING_UNKNOWN,
1376 peeraddr != NULL ? peeraddr : STRING_UNKNOWN,
1380 Statslog( LDAP_DEBUG_ANY,
1381 "fd=%ld host access from %s (%s) denied.\n",
1383 dnsname != NULL ? dnsname : "unknown",
1384 peeraddr != NULL ? peeraddr : "unknown",
1389 #endif /* HAVE_TCPD */
1392 id = connection_init(s,
1393 slap_listeners[l]->sl_url,
1394 dnsname != NULL ? dnsname : "unknown",
1396 slap_listeners[l]->sl_name,
1398 slap_listeners[l]->sl_is_tls,
1405 if( authid ) ch_free(authid);
1409 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1410 "slapd_daemon_task: connection_init(%ld, %s, %s) failed.\n",
1411 (long)s, peername, slap_listeners[l]->sl_name ));
1413 Debug( LDAP_DEBUG_ANY,
1414 "daemon: connection_init(%ld, %s, %s) failed.\n",
1417 slap_listeners[l]->sl_name );
1423 Statslog( LDAP_DEBUG_STATS,
1424 "daemon: conn=%ld fd=%ld connection from %s (%s) accepted.\n",
1427 slap_listeners[l]->sl_name,
1436 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1437 "slapd_daemon_task: activity on " ));
1439 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1442 for ( i = 0; i < readfds.fd_count; i++ ) {
1444 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1445 " %d%s", readfds.fd_array[i], "r", 0 ));
1447 Debug( LDAP_DEBUG_CONNS, " %d%s",
1448 readfds.fd_array[i], "r", 0 );
1451 for ( i = 0; i < writefds.fd_count; i++ ) {
1453 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1454 " %d%s", writefds.fd_array[i], "w" ));
1456 Debug( LDAP_DEBUG_CONNS, " %d%s",
1457 writefds.fd_array[i], "w", 0 );
1462 for ( i = 0; i < nfds; i++ ) {
1464 int is_listener = 0;
1466 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1467 if ( i == slap_listeners[l]->sl_sd ) {
1468 #ifdef LDAP_CONNECTIONLESS
1469 /* The listener is the data port. Don't
1472 if (slap_listeners[l]->sl_is_udp)
1479 if ( is_listener ) {
1482 r = FD_ISSET( i, &readfds );
1483 w = FD_ISSET( i, &writefds );
1486 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1488 r ? "r" : "", w ? "w" : "" ));
1490 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1491 r ? "r" : "", w ? "w" : "" );
1497 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2, "\n" ));
1499 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1504 /* loop through the writers */
1506 for ( i = 0; i < writefds.fd_count; i++ )
1508 for ( i = 0; i < nfds; i++ )
1512 int is_listener = 0;
1514 wd = writefds.fd_array[i];
1516 if( ! FD_ISSET( i, &writefds ) ) {
1522 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1523 if ( i == slap_listeners[l]->sl_sd ) {
1524 #ifdef LDAP_CONNECTIONLESS
1525 if (slap_listeners[l]->sl_is_udp)
1532 if ( is_listener ) {
1536 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1537 "slapd_daemon_task: write active on %d\n", wd ));
1539 Debug( LDAP_DEBUG_CONNS,
1540 "daemon: write active on %d\n",
1544 * NOTE: it is possible that the connection was closed
1545 * and that the stream is now inactive.
1546 * connection_write() must valid the stream is still
1550 if ( connection_write( wd ) < 0 ) {
1551 FD_CLR( (unsigned) wd, &readfds );
1557 for ( i = 0; i < readfds.fd_count; i++ )
1559 for ( i = 0; i < nfds; i++ )
1563 int is_listener = 0;
1566 rd = readfds.fd_array[i];
1568 if( ! FD_ISSET( i, &readfds ) ) {
1574 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1575 if ( rd == slap_listeners[l]->sl_sd ) {
1576 #ifdef LDAP_CONNECTIONLESS
1577 if (slap_listeners[l]->sl_is_udp)
1584 if ( is_listener ) {
1589 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1590 "slapd_daemon_task: read activity on %d\n", rd ));
1592 Debug ( LDAP_DEBUG_CONNS,
1593 "daemon: read activity on %d\n", rd, 0, 0 );
1596 * NOTE: it is possible that the connection was closed
1597 * and that the stream is now inactive.
1598 * connection_read() must valid the stream is still
1602 if ( connection_read( rd ) < 0 ) {
1606 ldap_pvt_thread_yield();
1609 if( slapd_shutdown > 0 ) {
1611 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1612 "slapd_daemon_task: shutdown requested and initiated.\n"));
1614 Debug( LDAP_DEBUG_TRACE,
1615 "daemon: shutdown requested and initiated.\n",
1619 } else if ( slapd_shutdown < 0 ) {
1620 #ifdef HAVE_NT_SERVICE_MANAGER
1621 if (slapd_shutdown == -1)
1624 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1625 "slapd_daemon_task: shutdown initiated by Service Manager.\n"));
1627 Debug( LDAP_DEBUG_TRACE,
1628 "daemon: shutdown initiated by Service Manager.\n",
1636 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1637 "slapd_daemon_task: abnormal condition, shutdown initiated.\n" ));
1639 Debug( LDAP_DEBUG_TRACE,
1640 "daemon: abnormal condition, shutdown initiated.\n",
1646 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1647 "slapd_daemon_task: no active streams, shutdown initiated.\n" ));
1649 Debug( LDAP_DEBUG_TRACE,
1650 "daemon: no active streams, shutdown initiated.\n",
1655 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1656 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1657 #ifdef LDAP_PF_LOCAL
1658 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1659 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1661 #endif /* LDAP_PF_LOCAL */
1662 slapd_close( slap_listeners[l]->sl_sd );
1664 if ( slap_listeners[l]->sl_url )
1665 free ( slap_listeners[l]->sl_url );
1666 if ( slap_listeners[l]->sl_name )
1667 free ( slap_listeners[l]->sl_name );
1668 free ( slap_listeners[l] );
1670 free ( slap_listeners );
1671 slap_listeners = NULL;
1674 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1675 "slapd_daemon_task: shutdown waiting for %d threads to terminate.\n",
1676 ldap_pvt_thread_pool_backload(&connection_pool) ));
1678 Debug( LDAP_DEBUG_ANY,
1679 "slapd shutdown: waiting for %d threads to terminate\n",
1680 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1682 ldap_pvt_thread_pool_destroy(&connection_pool, 1);
1688 int slapd_daemon( void )
1694 #define SLAPD_LISTENER_THREAD 1
1695 #if defined( SLAPD_LISTENER_THREAD )
1697 ldap_pvt_thread_t listener_tid;
1699 /* listener as a separate THREAD */
1700 rc = ldap_pvt_thread_create( &listener_tid,
1701 0, slapd_daemon_task, NULL );
1705 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1706 "slapd_daemon: listener ldap_pvt_thread_create failed (%d).\n", rc ));
1708 Debug( LDAP_DEBUG_ANY,
1709 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
1714 /* wait for the listener thread to complete */
1715 ldap_pvt_thread_join( listener_tid, (void *) NULL );
1718 /* experimental code */
1719 slapd_daemon_task( NULL );
1728 #if defined( HAVE_WINSOCK2 )
1729 WORD wVersionRequested;
1733 wVersionRequested = MAKEWORD( 2, 0 );
1735 err = WSAStartup( wVersionRequested, &wsaData );
1737 /* Tell the user that we couldn't find a usable */
1742 /* Confirm that the WinSock DLL supports 2.0.*/
1743 /* Note that if the DLL supports versions greater */
1744 /* than 2.0 in addition to 2.0, it will still return */
1745 /* 2.0 in wVersion since that is the version we */
1748 if ( LOBYTE( wsaData.wVersion ) != 2 ||
1749 HIBYTE( wsaData.wVersion ) != 0 )
1751 /* Tell the user that we couldn't find a usable */
1757 /* The WinSock DLL is acceptable. Proceed. */
1758 #elif defined( HAVE_WINSOCK )
1760 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
1767 int sockdestroy(void)
1769 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
1776 slap_sig_shutdown( int sig )
1779 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1780 "slap_sig_shutdown: signal %d\n", sig ));
1782 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
1786 * If the NT Service Manager is controlling the server, we don't
1787 * want SIGBREAK to kill the server. For some strange reason,
1788 * SIGBREAK is generated when a user logs out.
1791 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
1792 if (is_NT_Service && sig == SIGBREAK)
1794 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1795 "slap_sig_shutdown: SIGBREAK ignored.\n" ));
1797 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: SIGBREAK ignored.\n",
1802 slapd_shutdown = sig;
1806 /* reinstall self */
1807 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
1811 slap_sig_wake( int sig )
1815 /* reinstall self */
1816 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
1820 void slapd_add_internal(ber_socket_t s) {
1824 Listener ** slapd_get_listeners(void) {
1825 return slap_listeners;