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>
26 int allow_severity = LOG_INFO;
27 int deny_severity = LOG_NOTICE;
28 #endif /* TCP Wrappers */
32 #endif /* LDAP_PF_LOCAL */
36 ber_socket_t dtblsize;
38 typedef union slap_sockaddr {
39 struct sockaddr sa_addr;
40 struct sockaddr_in sa_in_addr;
42 struct sockaddr_in6 sa_in6_addr;
45 struct sockaddr_un sa_un_addr;
49 typedef struct slap_listener {
57 #define sl_addr sl_sa.sa_in_addr
60 Listener **slap_listeners = NULL;
62 #define SLAPD_LISTEN 10
64 static ber_socket_t wake_sds[2];
68 #define WAKE_LISTENER(w) \
69 ((w && !waking) ? tcp_write( wake_sds[1], "0", 1 ), waking=1 : 0)
71 #define WAKE_LISTENER(w) \
72 do { if (w) tcp_write( wake_sds[1], "0", 1 ); } while(0)
75 #ifdef HAVE_NT_SERVICE_MANAGER
77 extern ldap_pvt_thread_cond_t started_event;
78 extern int is_NT_Service;
84 volatile sig_atomic_t slapd_shutdown = 0;
86 static struct slap_daemon {
87 ldap_pvt_thread_mutex_t sd_mutex;
92 /* In winsock, accept() returns values higher than dtblsize
93 so don't bother with this optimization */
103 * Add a descriptor to daemon control
105 static void slapd_add(ber_socket_t s) {
106 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
108 assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
109 assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
110 assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
113 if (s >= slap_daemon.sd_nfds) {
114 slap_daemon.sd_nfds = s + 1;
118 FD_SET( s, &slap_daemon.sd_actives );
119 FD_SET( s, &slap_daemon.sd_readers );
122 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
123 "slapd_add: added %ld%s%s\n",
125 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
126 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" ));
128 Debug( LDAP_DEBUG_CONNS, "daemon: added %ld%s%s\n",
130 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
131 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
133 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
137 * Remove the descriptor from daemon control
139 void slapd_remove(ber_socket_t s, int wake) {
140 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
143 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
144 "slapd_remove: removing %ld%s%s\n",
146 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
147 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" ));
149 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
151 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
152 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
154 FD_CLR( s, &slap_daemon.sd_actives );
155 FD_CLR( s, &slap_daemon.sd_readers );
156 FD_CLR( s, &slap_daemon.sd_writers );
158 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
162 void slapd_clr_write(ber_socket_t s, int wake) {
163 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
165 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
166 FD_CLR( s, &slap_daemon.sd_writers );
168 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
172 void slapd_set_write(ber_socket_t s, int wake) {
173 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
175 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
176 if (!FD_ISSET(s, &slap_daemon.sd_writers))
177 FD_SET( (unsigned) s, &slap_daemon.sd_writers );
179 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
183 void slapd_clr_read(ber_socket_t s, int wake) {
184 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
186 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
187 FD_CLR( s, &slap_daemon.sd_readers );
189 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
193 void slapd_set_read(ber_socket_t s, int wake) {
194 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
196 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
197 if (!FD_ISSET(s, &slap_daemon.sd_readers))
198 FD_SET( s, &slap_daemon.sd_readers );
200 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
204 static void slapd_close(ber_socket_t s) {
206 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
207 "slapd_close: closing %ld\n", (long)s ));
209 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
215 static void slap_free_listener_addresses(struct sockaddr **sal)
217 struct sockaddr **sap;
223 for (sap = sal; *sap != NULL; sap++) {
230 /* port = 0 indicates AF_LOCAL */
231 static int slap_get_listener_addresses(
234 struct sockaddr ***sal)
236 struct sockaddr **sap;
237 #ifdef HAVE_GETADDRINFO
238 struct addrinfo hints, *res, *sai;
241 memset( &hints, '\0', sizeof(hints) );
242 hints.ai_flags = AI_PASSIVE;
243 hints.ai_socktype = SOCK_STREAM;
244 # ifdef LDAP_PF_LOCAL
246 hints.ai_family = AF_LOCAL;
247 /* host specifies a service in this case */
248 if (err = getaddrinfo(NULL, host, &hints, &res)) {
250 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
251 "slap_get_listener_addresses: getaddrinfo failed: %s\n",
252 AC_GAI_STRERROR(err) ));
254 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
255 AC_GAI_STRERROR(err), 0, 0);
264 snprintf(serv, sizeof serv, "%d", port);
265 hints.ai_family = AF_UNSPEC;
266 if (err = getaddrinfo(host, serv, &hints, &res)) {
268 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
269 "slap_get_listener_addresses: getaddrinfo failed: %s\n",
270 AC_GAI_STRERROR(err) ));
272 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
273 AC_GAI_STRERROR(err), 0, 0);
280 for (n=2; (sai = sai->ai_next) != NULL; n++) {
283 *sal = ch_malloc(n * sizeof(*sal));
291 switch (sai->ai_family) {
292 # ifdef LDAP_PF_LOCAL
294 *sap = ch_malloc(sizeof(struct sockaddr_un));
299 *(struct sockaddr_un *)*sap =
300 *((struct sockaddr_un *)sai->ai_addr);
303 # ifdef LDAP_PF_INET6
305 *sap = ch_malloc(sizeof(struct sockaddr_in6));
310 *(struct sockaddr_in6 *)*sap =
311 *((struct sockaddr_in6 *)sai->ai_addr);
315 *sap = ch_malloc(sizeof(struct sockaddr_in));
320 *(struct sockaddr_in *)*sap =
321 *((struct sockaddr_in *)sai->ai_addr);
328 (*sap)->sa_family = sai->ai_family;
331 } while ((sai = sai->ai_next) != NULL);
336 # ifdef LDAP_PF_LOCAL
338 *sal = ch_malloc(2 * sizeof(*sal));
343 *sap = ch_malloc(sizeof(struct sockaddr_un));
346 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
347 (*sap)->sa_family = AF_LOCAL;
349 (sizeof(((struct sockaddr_un *)*sal)->sun_path) - 1) ) {
351 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
352 "slap_get_listener_addresses: domain socket path (%s) too long in URL\n",
355 Debug( LDAP_DEBUG_ANY,
356 "daemon: domain socket path (%s) too long in URL",
361 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
367 if ( host == NULL ) {
368 in.s_addr = htonl(INADDR_ANY);
370 } else if ( !inet_aton( host, &in ) ) {
371 struct hostent *he = gethostbyname( host );
374 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
375 "slap_get_listener_addresses: invalid host %s\n",
378 Debug( LDAP_DEBUG_ANY,
379 "daemon: invalid host %s", host, 0, 0);
383 AC_MEMCPY( &in, he->h_addr, sizeof( in ) );
386 *sal = ch_malloc(2 * sizeof(*sal));
392 *sap = ch_malloc(sizeof(struct sockaddr_in));
397 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_in) );
398 (*sap)->sa_family = AF_INET;
399 ((struct sockaddr_in *)*sap)->sin_port = htons(port);
400 ((struct sockaddr_in *)*sap)->sin_addr = in;
409 slap_free_listener_addresses(*sal);
413 static Listener * slap_open_listener(
422 struct sockaddr **sal, **psal;
424 rc = ldap_url_parse( url, &lud );
426 if( rc != LDAP_URL_SUCCESS ) {
428 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
429 "slap_open_listener: listen URL \"%s\" parse error %d\n",
432 Debug( LDAP_DEBUG_ANY,
433 "daemon: listen URL \"%s\" parse error=%d\n",
440 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
442 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
443 "slap_open_listener: TLS is not supported (%s)\n",
446 Debug( LDAP_DEBUG_ANY,
447 "daemon: TLS not supported (%s)\n",
450 ldap_free_urldesc( lud );
454 if(! lud->lud_port ) {
455 lud->lud_port = LDAP_PORT;
459 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
461 if(! lud->lud_port ) {
462 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
466 port = (unsigned short) lud->lud_port;
468 if ( ldap_pvt_url_scheme2proto(lud->lud_scheme) == LDAP_PROTO_IPC ) {
470 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
471 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
473 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
478 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
479 "slap_open_listener: URL scheme is not supported: %s\n",
482 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
485 ldap_free_urldesc( lud );
489 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
490 || strcmp(lud->lud_host, "*") == 0 )
492 err = slap_get_listener_addresses(NULL, port, &sal);
494 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
498 ldap_free_urldesc( lud );
504 while ( *sal != NULL ) {
505 switch( (*sal)->sa_family ) {
518 l.sl_sd = socket( (*sal)->sa_family, SOCK_STREAM, 0);
519 if ( l.sl_sd == AC_SOCKET_INVALID ) {
520 int err = sock_errno();
522 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
523 "slap_open_listener: socket() failed errno=%d (%s)\n",
524 err, sock_errstr(err) ));
526 Debug( LDAP_DEBUG_ANY,
527 "daemon: socket() failed errno=%d (%s)\n", err,
528 sock_errstr(err), 0 );
534 if ( l.sl_sd >= dtblsize ) {
536 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
537 "slap_open_listener: listener descriptor %ld is too great %ld\n",
538 (long)l.sl_sd, (long)dtblsize ));
540 Debug( LDAP_DEBUG_ANY,
541 "daemon: listener descriptor %ld is too great %ld\n",
542 (long) l.sl_sd, (long) dtblsize, 0 );
544 tcp_close( l.sl_sd );
550 if ( (*sal)->sa_family == AF_LOCAL ) {
551 unlink ( ((struct sockaddr_un *)*sal)->sun_path );
556 /* enable address reuse */
558 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
559 (char *) &tmp, sizeof(tmp) );
560 if ( rc == AC_SOCKET_ERROR ) {
561 int err = sock_errno();
563 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
564 "slap_open_listener: setsockopt( %ld, SO_REUSEADDR ) failed errno %d (%s)\n",
565 (long)l.sl_sd, err, sock_errstr(err) ));
567 Debug( LDAP_DEBUG_ANY,
568 "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
569 (long) l.sl_sd, err, sock_errstr(err) );
575 switch( (*sal)->sa_family ) {
577 addrlen = sizeof(struct sockaddr_in);
581 addrlen = sizeof(struct sockaddr_in6);
586 addrlen = sizeof(struct sockaddr_un);
591 if (!bind(l.sl_sd, *sal, addrlen))
595 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
596 "slap_open_listener: bind(%ld) failed errno=%d (%s)\n",
597 (long)l.sl_sd, err, sock_errstr(err) ));
599 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
600 (long) l.sl_sd, err, sock_errstr(err) );
602 tcp_close( l.sl_sd );
604 } /* while ( *sal != NULL ) */
606 if ( *sal == NULL ) {
608 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
609 "slap_open_listener: bind(%ld) failed.\n", (long)l.sl_sd ));
611 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed\n",
612 (long) l.sl_sd, 0, 0 );
614 slap_free_listener_addresses(psal);
618 switch ( (*sal)->sa_family ) {
621 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
622 if ( chmod( addr, S_IRWXU ) < 0 ) {
623 int err = sock_errno();
625 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
626 "slap_open_listener: fchmod(%ld) failed errno=%d (%s)\n",
627 (long)l.sl_sd, err, sock_errstr(err) ));
629 Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
630 (long) l.sl_sd, err, sock_errstr(err) );
632 tcp_close( l.sl_sd );
633 slap_free_listener_addresses(psal);
636 l.sl_name = ch_malloc( strlen(addr) + sizeof("PATH=") );
637 sprintf( l.sl_name, "PATH=%s", addr );
639 #endif /* LDAP_PF_LOCAL */
643 #ifdef HAVE_GETADDRINFO
644 char addr[INET_ADDRSTRLEN];
645 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
646 addr, sizeof(addr) );
649 s = inet_ntoa( l.sl_addr.sin_addr );
651 l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65535") );
652 sprintf( l.sl_name, "IP=%s:%d",
653 s != NULL ? s : "unknown" , port );
658 char addr[INET6_ADDRSTRLEN];
659 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
661 l.sl_name = ch_malloc( strlen(addr) + sizeof("IP= 65535") );
662 sprintf( l.sl_name, "IP=%s %d", addr, port );
664 #endif /* LDAP_PF_INET6 */
668 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
669 "slap_open_listener: unsupported address family (%d)\n",
670 (int)(*sal)->sa_family ));
672 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
673 (int) (*sal)->sa_family, 0, 0 );
678 slap_free_listener_addresses(psal);
680 l.sl_url = ch_strdup( url );
681 li = ch_malloc( sizeof( Listener ) );
685 LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
686 "slap_open_listener: daemon initialzed %s\n", l.sl_url ));
688 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
694 static int sockinit(void);
695 static int sockdestroy(void);
697 int slapd_daemon_init( const char *urls )
703 LDAP_LOG(( "connection", LDAP_LEVEL_ARGS,
704 "slapd_daemon_init: %s\n",
705 urls ? urls : "<null>" ));
707 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
708 urls ? urls : "<null>", 0, 0 );
710 if( (rc = sockinit()) != 0 ) {
715 dtblsize = sysconf( _SC_OPEN_MAX );
716 #elif HAVE_GETDTABLESIZE
717 dtblsize = getdtablesize();
719 dtblsize = FD_SETSIZE;
723 if(dtblsize > FD_SETSIZE) {
724 dtblsize = FD_SETSIZE;
726 #endif /* !FD_SETSIZE */
728 /* open a pipe (or something equivalent connected to itself).
729 * we write a byte on this fd whenever we catch a signal. The main
730 * loop will be select'ing on this socket, and will wake up when
733 if( (rc = lutil_pair( wake_sds )) < 0 ) {
735 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
736 "slap_daemon_init: lutil_pair() failed rc=%d\n", rc ));
738 Debug( LDAP_DEBUG_ANY,
739 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
744 FD_ZERO( &slap_daemon.sd_readers );
745 FD_ZERO( &slap_daemon.sd_writers );
751 u = str2charray( urls, " " );
753 if( u == NULL || u[0] == NULL ) {
755 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
756 "slap_daemon_init: no urls (%s) provided.\n", urls ));
758 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
764 for( i=0; u[i] != NULL; i++ ) {
766 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
767 "slap_daemon_init: listen on %s\n.", u[i] ));
769 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
776 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
777 "slap_daemon_init: no listeners to open (%s)\n", urls ));
779 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
787 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
788 "slap_daemon_init: %d listeners to open...\n", i ));
790 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
793 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
795 for(i = 0; u[i] != NULL; i++ ) {
796 slap_listeners[i] = slap_open_listener( u[i] );
798 if( slap_listeners[i] == NULL ) {
803 slap_listeners[i] = NULL;
806 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
807 "slap_daemon_init: %d listeners opened\n", i ));
809 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
813 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
819 slapd_daemon_destroy(void)
821 connections_destroy();
822 tcp_close( wake_sds[1] );
823 tcp_close( wake_sds[0] );
835 time_t last_idle_check = slap_get_time();
838 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
839 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
842 if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN ) == -1 ) {
843 int err = sock_errno();
845 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
846 "slapd_daemon_task: listen( %s, 5 ) failed errno=%d (%s)\n",
847 slap_listeners[l]->sl_url, err, sock_errstr(err) ));
849 Debug( LDAP_DEBUG_ANY,
850 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
851 slap_listeners[l]->sl_url, err,
857 slapd_add( slap_listeners[l]->sl_sd );
860 #ifdef HAVE_NT_SERVICE_MANAGER
861 if ( started_event != NULL ) {
862 ldap_pvt_thread_cond_signal( &started_event );
865 /* initialization complete. Here comes the loop. */
867 while ( !slapd_shutdown ) {
872 #define SLAPD_EBADF_LIMIT 16
875 #define SLAPD_IDLE_CHECK_LIMIT 4
876 time_t now = slap_get_time();
883 #if defined(SLAPD_RLOOKUPS)
889 if( global_idletimeout > 0 && difftime(
890 last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT,
893 connections_timeout_idle(now);
896 FD_ZERO( &writefds );
902 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
904 #ifdef FD_SET_MANUAL_COPY
905 for( s = 0; s < nfds; s++ ) {
906 if(FD_ISSET( &slap_sd_readers, s )) {
907 FD_SET( s, &readfds );
909 if(FD_ISSET( &slap_sd_writers, s )) {
910 FD_SET( s, &writefds );
914 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
915 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
917 assert(!FD_ISSET(wake_sds[0], &readfds));
918 FD_SET( wake_sds[0], &readfds );
920 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
921 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
923 if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
924 FD_SET( slap_listeners[l]->sl_sd, &readfds );
928 nfds = slap_daemon.sd_nfds;
933 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
935 at = ldap_pvt_thread_pool_backload(&connection_pool);
937 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
940 tvp = at ? &zero : NULL;
943 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
944 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
948 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
949 "slapd_daemon_task: select: listen=%d active_threads=%d tvp=%s\n",
950 slap_listeners[l]->sl_sd, at, tvp == NULL ? "NULL" : "zero" ));
952 Debug( LDAP_DEBUG_CONNS,
953 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
954 slap_listeners[l]->sl_sd, at,
955 tvp == NULL ? "NULL" : "zero" );
959 switch(ns = select( nfds, &readfds,
961 /* don't pass empty fd_set */
962 ( writefds.fd_count > 0 ? &writefds : NULL ),
968 case -1: { /* failure - try again */
969 int err = sock_errno();
973 /* you'd think this would be EBADF */
974 || err == WSAENOTSOCK
977 if (++ebadf < SLAPD_EBADF_LIMIT)
983 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
984 "slapd_daemon_task: select failed (%d): %s\n",
985 err, sock_errstr(err) ));
987 Debug( LDAP_DEBUG_CONNS,
988 "daemon: select failed (%d): %s\n",
989 err, sock_errstr(err), 0 );
996 case 0: /* timeout - let threads run */
999 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1000 "slapd_daemon_task: select timeout - yielding\n" ));
1002 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
1005 ldap_pvt_thread_yield();
1008 default: /* something happened - deal with it */
1009 if( slapd_shutdown ) continue;
1013 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1014 "slapd_daemon_task: activity on %d descriptors\n", ns ));
1016 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
1022 if( FD_ISSET( wake_sds[0], &readfds ) ) {
1024 tcp_read( wake_sds[0], c, sizeof(c) );
1031 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1033 socklen_t len = sizeof(from);
1036 char *authid = NULL;
1040 #ifdef LDAP_PF_LOCAL
1041 char peername[MAXPATHLEN + sizeof("PATH=")];
1042 #elif defined(LDAP_PF_INET6)
1043 char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
1045 char peername[sizeof("IP=255.255.255.255:65336")];
1046 #endif /* LDAP_PF_LOCAL */
1050 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1053 if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
1056 s = accept( slap_listeners[l]->sl_sd,
1057 (struct sockaddr *) &from, &len );
1058 if ( s == AC_SOCKET_INVALID ) {
1059 int err = sock_errno();
1061 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1062 "slapd_daemon_task: accept(%ld) failed errno=%d (%s)\n",
1063 (long)slap_listeners[l]->sl_sd, err, sock_errstr(err) ));
1065 Debug( LDAP_DEBUG_ANY,
1066 "daemon: accept(%ld) failed errno=%d (%s)\n",
1067 (long) slap_listeners[l]->sl_sd, err,
1073 #ifndef HAVE_WINSOCK
1074 /* make sure descriptor number isn't too great */
1075 if ( s >= dtblsize ) {
1077 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1078 "slapd_daemon_task: %ld beyond descriptor table size %ld\n",
1079 (long)s, (long)dtblsize ));
1081 Debug( LDAP_DEBUG_ANY,
1082 "daemon: %ld beyond descriptor table size %ld\n",
1083 (long) s, (long) dtblsize, 0 );
1091 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1093 /* newly accepted stream should not be in any of the FD SETS */
1094 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
1095 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
1096 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
1098 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1101 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1102 #ifdef LDAP_PF_LOCAL
1103 /* for IPv4 and IPv6 sockets only */
1104 if ( from.sa_addr.sa_family != AF_LOCAL )
1105 #endif /* LDAP_PF_LOCAL */
1110 /* enable keep alives */
1112 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1113 (char *) &tmp, sizeof(tmp) );
1114 if ( rc == AC_SOCKET_ERROR ) {
1115 int err = sock_errno();
1117 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1118 "slapd_daemon_task: setsockopt( %ld, SO_KEEPALIVE) failed errno=%d (%s)\n",
1119 (long)s, err, sock_errstr(err) ));
1121 Debug( LDAP_DEBUG_ANY,
1122 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1123 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1128 /* enable no delay */
1130 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1131 (char *)&tmp, sizeof(tmp) );
1132 if ( rc == AC_SOCKET_ERROR ) {
1133 int err = sock_errno();
1135 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1136 "slapd_daemon_task: setsockopt( %ld, TCP_NODELAY) failed errno=%d (%s)\n",
1137 (long)s, err, sock_errstr(err) ));
1139 Debug( LDAP_DEBUG_ANY,
1140 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1141 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1149 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1150 "slapd_daemon_task: new connection on %ld\n", (long)s ));
1152 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
1155 switch ( from.sa_addr.sa_family ) {
1156 # ifdef LDAP_PF_LOCAL
1158 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1159 ssf = LDAP_PVT_SASL_LOCAL_SSF;
1161 #endif /* LDAP_PF_LOCAL */
1163 # ifdef LDAP_PF_INET6
1165 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1166 peeraddr = inet_ntoa( *((struct in_addr *)
1167 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1168 sprintf( peername, "IP=%s:%d",
1169 peeraddr != NULL ? peeraddr : "unknown",
1170 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1172 char addr[INET6_ADDRSTRLEN];
1173 sprintf( peername, "IP=%s %d",
1174 inet_ntop( AF_INET6,
1175 &from.sa_in6_addr.sin6_addr,
1176 addr, sizeof addr) ? addr : "unknown",
1177 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1180 # endif /* LDAP_PF_INET6 */
1183 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1184 sprintf( peername, "IP=%s:%d",
1185 peeraddr != NULL ? peeraddr : "unknown",
1186 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1194 if ( ( from.sa_addr.sa_family == AF_INET )
1195 #ifdef LDAP_PF_INET6
1196 || ( from.sa_addr.sa_family == AF_INET6 )
1199 #ifdef SLAPD_RLOOKUPS
1200 # ifdef LDAP_PF_INET6
1201 if ( from.sa_addr.sa_family == AF_INET6 )
1203 (char *)&(from.sa_in6_addr.sin6_addr),
1204 sizeof(from.sa_in6_addr.sin6_addr),
1207 # endif /* LDAP_PF_INET6 */
1209 (char *) &(from.sa_in_addr.sin_addr),
1210 sizeof(from.sa_in_addr.sin_addr),
1212 dnsname = hp ? ldap_pvt_str2lower( hp->h_name ) : NULL;
1215 #endif /* SLAPD_RLOOKUPS */
1218 if ( !hosts_ctl("slapd",
1219 dnsname != NULL ? dnsname : STRING_UNKNOWN,
1220 peeraddr != NULL ? peeraddr : STRING_UNKNOWN,
1224 Statslog( LDAP_DEBUG_ANY,
1225 "fd=%ld host access from %s (%s) denied.\n",
1227 dnsname != NULL ? dnsname : "unknown",
1228 peeraddr != NULL ? peeraddr : "unknown",
1233 #endif /* HAVE_TCPD */
1236 id = connection_init(s,
1237 slap_listeners[l]->sl_url,
1238 dnsname != NULL ? dnsname : "unknown",
1240 slap_listeners[l]->sl_name,
1242 slap_listeners[l]->sl_is_tls,
1249 if( authid ) ch_free(authid);
1253 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1254 "slapd_daemon_task: connection_init(%ld, %s, %s) failed.\n",
1255 (long)s, peername, slap_listeners[l]->sl_name ));
1257 Debug( LDAP_DEBUG_ANY,
1258 "daemon: connection_init(%ld, %s, %s) failed.\n",
1261 slap_listeners[l]->sl_name );
1267 Statslog( LDAP_DEBUG_STATS,
1268 "daemon: conn=%ld fd=%ld connection from %s (%s) accepted.\n",
1271 slap_listeners[l]->sl_name,
1280 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1281 "slapd_daemon_task: activity on " ));
1283 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1286 for ( i = 0; i < readfds.fd_count; i++ ) {
1288 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1289 " %d%s", readfs.fd_array[i], "r", 0 );
1291 Debug( LDAP_DEBUG_CONNS, " %d%s",
1292 readfds.fd_array[i], "r", 0 );
1295 for ( i = 0; i < writefds.fd_count; i++ ) {
1297 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1298 " %d%s", writefds.fd_array[i], "w" ));
1300 Debug( LDAP_DEBUG_CONNS, " %d%s",
1301 writefds.fd_array[i], "w", 0 );
1306 for ( i = 0; i < nfds; i++ ) {
1308 int is_listener = 0;
1310 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1311 if ( i == slap_listeners[l]->sl_sd ) {
1316 if ( is_listener ) {
1319 r = FD_ISSET( i, &readfds );
1320 w = FD_ISSET( i, &writefds );
1323 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1325 r ? "r" : "", w ? "w" : "" ));
1327 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1328 r ? "r" : "", w ? "w" : "" );
1334 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2, "\n" ));
1336 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1341 /* loop through the writers */
1343 for ( i = 0; i < writefds.fd_count; i++ )
1345 for ( i = 0; i < nfds; i++ )
1349 int is_listener = 0;
1351 wd = writefds.fd_array[i];
1353 if( ! FD_ISSET( i, &writefds ) ) {
1359 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1360 if ( i == slap_listeners[l]->sl_sd ) {
1365 if ( is_listener ) {
1369 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1370 "slapd_daemon_task: write active on %d\n", wd ));
1372 Debug( LDAP_DEBUG_CONNS,
1373 "daemon: write active on %d\n",
1377 * NOTE: it is possible that the connection was closed
1378 * and that the stream is now inactive.
1379 * connection_write() must valid the stream is still
1383 if ( connection_write( wd ) < 0 ) {
1384 FD_CLR( (unsigned) wd, &readfds );
1390 for ( i = 0; i < readfds.fd_count; i++ )
1392 for ( i = 0; i < nfds; i++ )
1396 int is_listener = 0;
1399 rd = readfds.fd_array[i];
1401 if( ! FD_ISSET( i, &readfds ) ) {
1407 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1408 if ( rd == slap_listeners[l]->sl_sd ) {
1413 if ( is_listener ) {
1418 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1419 "slapd_daemon_task: read activity on %d\n", rd ));
1421 Debug ( LDAP_DEBUG_CONNS,
1422 "daemon: read activity on %d\n", rd, 0, 0 );
1425 * NOTE: it is possible that the connection was closed
1426 * and that the stream is now inactive.
1427 * connection_read() must valid the stream is still
1431 if ( connection_read( rd ) < 0 ) {
1435 ldap_pvt_thread_yield();
1438 if( slapd_shutdown > 0 ) {
1440 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1441 "slapd_daemon_task: shutdown requested and initiated.\n"));
1443 Debug( LDAP_DEBUG_TRACE,
1444 "daemon: shutdown requested and initiated.\n",
1448 } else if ( slapd_shutdown < 0 ) {
1449 #ifdef HAVE_NT_SERVICE_MANAGER
1450 if (slapd_shutdown == -1)
1452 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1453 "slapd_daemon_task: shutdown initiated by Service Manager.\n"));
1455 Debug( LDAP_DEBUG_TRACE,
1456 "daemon: shutdown initiated by Service Manager.\n",
1462 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1463 "slapd_daemon_task: abnormal condition, shutdown initiated.\n" ));
1465 Debug( LDAP_DEBUG_TRACE,
1466 "daemon: abnormal condition, shutdown initiated.\n",
1471 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1472 "slapd_daemon_task: no active streams, shutdown initiated.\n" ));
1474 Debug( LDAP_DEBUG_TRACE,
1475 "daemon: no active streams, shutdown initiated.\n",
1480 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1481 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1482 #ifdef LDAP_PF_LOCAL
1483 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1484 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1486 #endif /* LDAP_PF_LOCAL */
1487 slapd_close( slap_listeners[l]->sl_sd );
1493 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1494 "slapd_daemon_task: shutdown waiting for %d threads to terminate.\n",
1495 ldap_pvt_thread_pool_backload(&connection_pool) ));
1497 Debug( LDAP_DEBUG_ANY,
1498 "slapd shutdown: waiting for %d threads to terminate\n",
1499 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1501 ldap_pvt_thread_pool_destroy(&connection_pool, 1);
1507 int slapd_daemon( void )
1513 #define SLAPD_LISTENER_THREAD 1
1514 #if defined( SLAPD_LISTENER_THREAD )
1516 ldap_pvt_thread_t listener_tid;
1518 /* listener as a separate THREAD */
1519 rc = ldap_pvt_thread_create( &listener_tid,
1520 0, slapd_daemon_task, NULL );
1524 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1525 "slapd_daemon: listener ldap_pvt_thread_create failed (%d).\n", rc ));
1527 Debug( LDAP_DEBUG_ANY,
1528 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
1533 /* wait for the listener thread to complete */
1534 ldap_pvt_thread_join( listener_tid, (void *) NULL );
1537 /* experimental code */
1538 slapd_daemon_task( NULL );
1547 #if defined( HAVE_WINSOCK2 )
1548 WORD wVersionRequested;
1552 wVersionRequested = MAKEWORD( 2, 0 );
1554 err = WSAStartup( wVersionRequested, &wsaData );
1556 /* Tell the user that we couldn't find a usable */
1561 /* Confirm that the WinSock DLL supports 2.0.*/
1562 /* Note that if the DLL supports versions greater */
1563 /* than 2.0 in addition to 2.0, it will still return */
1564 /* 2.0 in wVersion since that is the version we */
1567 if ( LOBYTE( wsaData.wVersion ) != 2 ||
1568 HIBYTE( wsaData.wVersion ) != 0 )
1570 /* Tell the user that we couldn't find a usable */
1576 /* The WinSock DLL is acceptable. Proceed. */
1577 #elif defined( HAVE_WINSOCK )
1579 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
1586 int sockdestroy(void)
1588 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
1595 slap_sig_shutdown( int sig )
1598 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1599 "slap_sig_shutdown: signal %d\n", sig ));
1601 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
1605 * If the NT Service Manager is controlling the server, we don't
1606 * want SIGBREAK to kill the server. For some strange reason,
1607 * SIGBREAK is generated when a user logs out.
1610 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
1611 if (is_NT_Service && sig == SIGBREAK)
1613 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1614 "slap_sig_shutdown: SIGBREAK ignored.\n" ));
1616 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: SIGBREAK ignored.\n",
1621 slapd_shutdown = sig;
1625 /* reinstall self */
1626 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
1630 slap_sig_wake( int sig )
1634 /* reinstall self */
1635 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
1639 void slapd_add_internal(ber_socket_t s) {