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 static ber_socket_t wake_sds[2];
66 #define WAKE_LISTENER(w) \
67 ((w && !waking) ? tcp_write( wake_sds[1], "0", 1 ), waking=1 : 0)
69 #define WAKE_LISTENER(w) \
70 do { if (w) tcp_write( wake_sds[1], "0", 1 ); } while(0)
73 #ifdef HAVE_NT_SERVICE_MANAGER
75 extern ldap_pvt_thread_cond_t started_event;
76 extern int is_NT_Service;
82 volatile sig_atomic_t slapd_shutdown = 0;
84 static struct slap_daemon {
85 ldap_pvt_thread_mutex_t sd_mutex;
90 /* In winsock, accept() returns values higher than dtblsize
91 so don't bother with this optimization */
101 * Add a descriptor to daemon control
103 static void slapd_add(ber_socket_t s) {
104 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
106 assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
107 assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
108 assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
111 if (s >= slap_daemon.sd_nfds) {
112 slap_daemon.sd_nfds = s + 1;
116 FD_SET( s, &slap_daemon.sd_actives );
117 FD_SET( s, &slap_daemon.sd_readers );
119 Debug( LDAP_DEBUG_CONNS, "daemon: added %ld%s%s\n",
121 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
122 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
124 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
128 * Remove the descriptor from daemon control
130 void slapd_remove(ber_socket_t s, int wake) {
131 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
133 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
135 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
136 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
138 FD_CLR( s, &slap_daemon.sd_actives );
139 FD_CLR( s, &slap_daemon.sd_readers );
140 FD_CLR( s, &slap_daemon.sd_writers );
142 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
146 void slapd_clr_write(ber_socket_t s, int wake) {
147 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
149 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
150 FD_CLR( s, &slap_daemon.sd_writers );
152 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
156 void slapd_set_write(ber_socket_t s, int wake) {
157 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
159 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
160 if (!FD_ISSET(s, &slap_daemon.sd_writers))
161 FD_SET( (unsigned) s, &slap_daemon.sd_writers );
163 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
167 void slapd_clr_read(ber_socket_t s, int wake) {
168 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
170 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
171 FD_CLR( s, &slap_daemon.sd_readers );
173 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
177 void slapd_set_read(ber_socket_t s, int wake) {
178 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
180 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
181 if (!FD_ISSET(s, &slap_daemon.sd_readers))
182 FD_SET( s, &slap_daemon.sd_readers );
184 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
188 static void slapd_close(ber_socket_t s) {
189 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
194 static slap_free_listener_addresses(struct sockaddr **sal)
196 struct sockaddr **sap;
202 for (sap = sal; *sap != NULL; sap++) {
209 /* port = 0 indicates AF_LOCAL */
210 static int slap_get_listener_addresses(
213 struct sockaddr ***sal)
215 struct sockaddr **sap;
216 #ifdef HAVE_GETADDRINFO
217 struct addrinfo hints, *res, *sai;
220 memset( &hints, '\0', sizeof(hints) );
221 hints.ai_flags = AI_PASSIVE;
222 hints.ai_socktype = SOCK_STREAM;
223 # ifdef LDAP_PF_LOCAL
225 hints.ai_family = AF_LOCAL;
226 /* host specifies a service in this case */
227 if (err = getaddrinfo(NULL, host, &hints, &res)) {
228 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
229 gai_strerror(err), 0, 0);
237 snprintf(serv, sizeof serv, "%d", port);
238 hints.ai_family = AF_UNSPEC;
239 if (err = getaddrinfo(host, serv, &hints, &res)) {
240 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
241 gai_strerror(err), 0, 0);
247 for (n=2; (sai = sai->ai_next) != NULL; n++) {
250 *sal = ch_malloc(n * sizeof(*sal));
258 switch (sai->ai_family) {
259 # ifdef LDAP_PF_LOCAL
261 *sap = ch_malloc(sizeof(struct sockaddr_un));
266 *(struct sockaddr_un *)*sap =
267 *((struct sockaddr_un *)sai->ai_addr);
270 # ifdef LDAP_PF_INET6
272 *sap = ch_malloc(sizeof(struct sockaddr_in6));
277 *(struct sockaddr_in6 *)*sap =
278 *((struct sockaddr_in6 *)sai->ai_addr);
282 *sap = ch_malloc(sizeof(struct sockaddr_in));
287 *(struct sockaddr_in *)*sap =
288 *((struct sockaddr_in *)sai->ai_addr);
295 (*sap)->sa_family = sai->ai_family;
296 (*sap)->sa_len = sai->ai_addrlen;
299 } while ((sai = sai->ai_next) != NULL);
304 # ifdef LDAP_PF_LOCAL
306 *sal = ch_malloc(2 * sizeof(*sal));
311 *sap = ch_malloc(sizeof(struct sockaddr_un));
314 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
315 (*sap)->sa_family = AF_LOCAL;
317 (sizeof(((struct sockaddr_un *)*sal)->sun_path) - 1) ) {
318 Debug( LDAP_DEBUG_ANY,
319 "daemon: domain socket path (%s) too long in URL",
323 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
329 if ( host == NULL ) {
330 in.s_addr = htonl(INADDR_ANY);
332 } else if ( !inet_aton( host, &in ) ) {
333 struct hostent *he = gethostbyname( host );
335 Debug( LDAP_DEBUG_ANY,
336 "daemon: invalid host %s", host, 0, 0);
339 AC_MEMCPY( &in, he->h_addr, sizeof( in ) );
342 *sal = ch_malloc(2 * sizeof(*sal));
348 *sap = ch_malloc(sizeof(struct sockaddr_in));
353 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_in) );
354 (*sap)->sa_family = AF_INET;
355 ((struct sockaddr_in *)*sap)->sin_port = htons(port);
356 ((struct sockaddr_in *)*sap)->sin_addr = in;
365 slap_free_listener_addresses(*sal);
369 static Listener * slap_open_listener(
378 struct sockaddr **sal, **psal;
380 rc = ldap_url_parse( url, &lud );
382 if( rc != LDAP_URL_SUCCESS ) {
383 Debug( LDAP_DEBUG_ANY,
384 "daemon: listen URL \"%s\" parse error=%d\n",
390 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
391 Debug( LDAP_DEBUG_ANY,
392 "daemon: TLS not supported (%s)\n",
394 ldap_free_urldesc( lud );
398 if(! lud->lud_port ) {
399 lud->lud_port = LDAP_PORT;
403 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
405 if(! lud->lud_port ) {
406 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
410 port = (unsigned short) lud->lud_port;
412 if ( ldap_pvt_url_scheme2proto(lud->lud_scheme) == LDAP_PROTO_IPC ) {
414 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
415 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
417 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
420 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
422 ldap_free_urldesc( lud );
426 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
427 || strcmp(lud->lud_host, "*") == 0 )
429 err = slap_get_listener_addresses(NULL, port, &sal);
431 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
435 ldap_free_urldesc( lud );
441 while ( *sal != NULL ) {
442 switch( (*sal)->sa_family ) {
455 l.sl_sd = socket( (*sal)->sa_family, SOCK_STREAM, 0);
456 if ( l.sl_sd == AC_SOCKET_INVALID ) {
457 int err = sock_errno();
458 Debug( LDAP_DEBUG_ANY,
459 "daemon: socket() failed errno=%d (%s)\n", err,
460 sock_errstr(err), 0 );
465 if ( l.sl_sd >= dtblsize ) {
466 Debug( LDAP_DEBUG_ANY,
467 "daemon: listener descriptor %ld is too great %ld\n",
468 (long) l.sl_sd, (long) dtblsize, 0 );
469 tcp_close( l.sl_sd );
475 if ( (*sal)->sa_family == AF_LOCAL ) {
476 unlink ( ((struct sockaddr_un *)*sal)->sun_path );
481 /* enable address reuse */
483 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
484 (char *) &tmp, sizeof(tmp) );
485 if ( rc == AC_SOCKET_ERROR ) {
486 int err = sock_errno();
487 Debug( LDAP_DEBUG_ANY,
488 "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
489 (long) l.sl_sd, err, sock_errstr(err) );
494 switch( (*sal)->sa_family ) {
496 addrlen = sizeof(struct sockaddr_in);
500 addrlen = sizeof(struct sockaddr_in6);
505 addrlen = sizeof(struct sockaddr_un);
510 if (!bind(l.sl_sd, *sal, addrlen))
513 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
514 (long) l.sl_sd, err, sock_errstr(err) );
515 tcp_close( l.sl_sd );
517 } /* while ( *sal != NULL ) */
519 if ( *sal == NULL ) {
520 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed\n",
521 (long) l.sl_sd, 0, 0 );
522 slap_free_listener_addresses(psal);
526 switch ( (*sal)->sa_family ) {
529 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
530 if ( chmod( addr, S_IRWXU ) < 0 ) {
531 int err = sock_errno();
532 Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
533 (long) l.sl_sd, err, sock_errstr(err) );
534 tcp_close( l.sl_sd );
535 slap_free_listener_addresses(psal);
538 l.sl_name = ch_malloc( strlen(addr) + sizeof("PATH=") );
539 sprintf( l.sl_name, "PATH=%s", addr );
541 #endif /* LDAP_PF_LOCAL */
545 #ifdef HAVE_GETADDRINFO
546 char addr[INET_ADDRSTRLEN];
547 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
548 addr, sizeof(addr) );
551 s = inet_ntoa( l.sl_addr.sin_addr );
553 l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65535") );
554 sprintf( l.sl_name, "IP=%s:%d",
555 s != NULL ? s : "unknown" , port );
560 char addr[INET6_ADDRSTRLEN];
561 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
563 l.sl_name = ch_malloc( strlen(addr) + sizeof("IP= 65535") );
564 sprintf( l.sl_name, "IP=%s %d", addr, port );
566 #endif /* LDAP_PF_INET6 */
569 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
570 (int) (*sal)->sa_family, 0, 0 );
574 slap_free_listener_addresses(psal);
576 l.sl_url = ch_strdup( url );
577 li = ch_malloc( sizeof( Listener ) );
580 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
586 static int sockinit(void);
587 static int sockdestroy(void);
589 int slapd_daemon_init( const char *urls )
594 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
595 urls ? urls : "<null>", 0, 0 );
597 if( (rc = sockinit()) != 0 ) {
602 dtblsize = sysconf( _SC_OPEN_MAX );
603 #elif HAVE_GETDTABLESIZE
604 dtblsize = getdtablesize();
606 dtblsize = FD_SETSIZE;
610 if(dtblsize > FD_SETSIZE) {
611 dtblsize = FD_SETSIZE;
613 #endif /* !FD_SETSIZE */
615 /* open a pipe (or something equivalent connected to itself).
616 * we write a byte on this fd whenever we catch a signal. The main
617 * loop will be select'ing on this socket, and will wake up when
620 if( (rc = lutil_pair( wake_sds )) < 0 ) {
621 Debug( LDAP_DEBUG_ANY,
622 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
626 FD_ZERO( &slap_daemon.sd_readers );
627 FD_ZERO( &slap_daemon.sd_writers );
633 u = str2charray( urls, " " );
635 if( u == NULL || u[0] == NULL ) {
636 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
642 for( i=0; u[i] != NULL; i++ ) {
643 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
648 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
654 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
657 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
659 for(i = 0; u[i] != NULL; i++ ) {
660 slap_listeners[i] = slap_open_listener( u[i] );
662 if( slap_listeners[i] == NULL ) {
667 slap_listeners[i] = NULL;
669 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
673 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
679 slapd_daemon_destroy(void)
681 connections_destroy();
682 tcp_close( wake_sds[1] );
683 tcp_close( wake_sds[0] );
695 time_t last_idle_check = slap_get_time();
698 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
699 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
702 if ( listen( slap_listeners[l]->sl_sd, 5 ) == -1 ) {
703 int err = sock_errno();
704 Debug( LDAP_DEBUG_ANY,
705 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
706 slap_listeners[l]->sl_url, err,
711 slapd_add( slap_listeners[l]->sl_sd );
714 #ifdef HAVE_NT_SERVICE_MANAGER
715 if ( started_event != NULL ) {
716 ldap_pvt_thread_cond_signal( &started_event );
719 /* initialization complete. Here comes the loop. */
721 while ( !slapd_shutdown ) {
726 #define SLAPD_EBADF_LIMIT 16
729 #define SLAPD_IDLE_CHECK_LIMIT 4
730 time_t now = slap_get_time();
737 #if defined(SLAPD_RLOOKUPS)
743 if( global_idletimeout > 0 && difftime(
744 last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT,
747 connections_timeout_idle(now);
750 FD_ZERO( &writefds );
756 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
758 #ifdef FD_SET_MANUAL_COPY
759 for( s = 0; s < nfds; s++ ) {
760 if(FD_ISSET( &slap_sd_readers, s )) {
761 FD_SET( s, &readfds );
763 if(FD_ISSET( &slap_sd_writers, s )) {
764 FD_SET( s, &writefds );
768 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
769 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
771 assert(!FD_ISSET(wake_sds[0], &readfds));
772 FD_SET( wake_sds[0], &readfds );
774 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
775 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
777 if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
778 FD_SET( slap_listeners[l]->sl_sd, &readfds );
782 nfds = slap_daemon.sd_nfds;
787 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
789 at = ldap_pvt_thread_pool_backload(&connection_pool);
791 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
794 tvp = at ? &zero : NULL;
797 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
798 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
801 Debug( LDAP_DEBUG_CONNS,
802 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
803 slap_listeners[l]->sl_sd, at,
804 tvp == NULL ? "NULL" : "zero" );
807 switch(ns = select( nfds, &readfds,
809 /* don't pass empty fd_set */
810 ( writefds.fd_count > 0 ? &writefds : NULL ),
816 case -1: { /* failure - try again */
817 int err = sock_errno();
821 /* you'd think this would be EBADF */
822 || err == WSAENOTSOCK
825 if (++ebadf < SLAPD_EBADF_LIMIT)
830 Debug( LDAP_DEBUG_CONNS,
831 "daemon: select failed (%d): %s\n",
832 err, sock_errstr(err), 0 );
839 case 0: /* timeout - let threads run */
841 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
843 ldap_pvt_thread_yield();
846 default: /* something happened - deal with it */
847 if( slapd_shutdown ) continue;
850 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
855 if( FD_ISSET( wake_sds[0], &readfds ) ) {
857 tcp_read( wake_sds[0], c, sizeof(c) );
864 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
866 socklen_t len = sizeof(from);
874 char peername[MAXPATHLEN + sizeof("PATH=")];
875 #elif defined(LDAP_PF_INET6)
876 char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
878 char peername[sizeof("IP=255.255.255.255:65336")];
879 #endif /* LDAP_PF_LOCAL */
883 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
886 if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
889 s = accept( slap_listeners[l]->sl_sd,
890 (struct sockaddr *) &from, &len );
891 if ( s == AC_SOCKET_INVALID ) {
892 int err = sock_errno();
893 Debug( LDAP_DEBUG_ANY,
894 "daemon: accept(%ld) failed errno=%d (%s)\n",
895 (long) slap_listeners[l]->sl_sd, err,
901 /* make sure descriptor number isn't too great */
902 if ( s >= dtblsize ) {
903 Debug( LDAP_DEBUG_ANY,
904 "daemon: %ld beyond descriptor table size %ld\n",
905 (long) s, (long) dtblsize, 0 );
912 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
914 /* newly accepted stream should not be in any of the FD SETS */
915 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
916 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
917 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
919 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
922 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
924 /* for IPv4 and IPv6 sockets only */
925 if ( from.sa_addr.sa_family != AF_LOCAL )
926 #endif /* LDAP_PF_LOCAL */
931 /* enable keep alives */
933 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
934 (char *) &tmp, sizeof(tmp) );
935 if ( rc == AC_SOCKET_ERROR ) {
936 int err = sock_errno();
937 Debug( LDAP_DEBUG_ANY,
938 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
939 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
943 /* enable no delay */
945 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
946 (char *)&tmp, sizeof(tmp) );
947 if ( rc == AC_SOCKET_ERROR ) {
948 int err = sock_errno();
949 Debug( LDAP_DEBUG_ANY,
950 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
951 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
957 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
960 switch ( from.sa_addr.sa_family ) {
961 # ifdef LDAP_PF_LOCAL
963 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
964 ssf = LDAP_PVT_SASL_LOCAL_SSF;
966 #endif /* LDAP_PF_LOCAL */
968 # ifdef LDAP_PF_INET6
970 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
971 peeraddr = inet_ntoa( *((struct in_addr *)
972 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
973 sprintf( peername, "IP=%s:%d",
974 peeraddr != NULL ? peeraddr : "unknown",
975 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
977 char addr[INET6_ADDRSTRLEN];
978 sprintf( peername, "IP=%s %d",
980 &from.sa_in6_addr.sin6_addr,
981 addr, sizeof addr) ? addr : "unknown",
982 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
985 # endif /* LDAP_PF_INET6 */
988 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
989 sprintf( peername, "IP=%s:%d",
990 peeraddr != NULL ? peeraddr : "unknown",
991 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
999 if ( ( from.sa_addr.sa_family == AF_INET )
1000 #ifdef LDAP_PF_INET6
1001 || ( from.sa_addr.sa_family == AF_INET6 )
1004 #ifdef SLAPD_RLOOKUPS
1005 # ifdef LDAP_PF_INET6
1006 if ( from.sa_addr.sa_family == AF_INET6 )
1008 (char *)&(from.sa_in6_addr.sin6_addr),
1009 sizeof(from.sa_in6_addr.sin6_addr),
1012 # endif /* LDAP_PF_INET6 */
1014 (char *) &(from.sa_in_addr.sin_addr),
1015 sizeof(from.sa_in_addr.sin_addr),
1017 dnsname = hp ? ldap_pvt_str2lower( hp->h_name ) : NULL;
1020 #endif /* SLAPD_RLOOKUPS */
1023 if ( !hosts_ctl("slapd",
1024 dnsname != NULL ? dnsname : STRING_UNKNOWN,
1025 peeraddr != NULL ? peeraddr : STRING_UNKNOWN,
1029 Statslog( LDAP_DEBUG_ANY,
1030 "fd=%ld host access from %s (%s) denied.\n",
1032 dnsname != NULL ? dnsname : "unknown",
1033 peeraddr != NULL ? peeraddr : "unknown",
1038 #endif /* HAVE_TCPD */
1041 id = connection_init(s,
1042 slap_listeners[l]->sl_url,
1043 dnsname != NULL ? dnsname : "unknown",
1045 slap_listeners[l]->sl_name,
1047 slap_listeners[l]->sl_is_tls,
1054 if( authid ) ch_free(authid);
1057 Debug( LDAP_DEBUG_ANY,
1058 "daemon: connection_init(%ld, %s, %s) failed.\n",
1061 slap_listeners[l]->sl_name );
1066 Statslog( LDAP_DEBUG_STATS,
1067 "daemon: conn=%ld fd=%ld connection from %s (%s) accepted.\n",
1070 slap_listeners[l]->sl_name,
1078 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1080 for ( i = 0; i < readfds.fd_count; i++ ) {
1081 Debug( LDAP_DEBUG_CONNS, " %d%s",
1082 readfds.fd_array[i], "r", 0 );
1084 for ( i = 0; i < writefds.fd_count; i++ ) {
1085 Debug( LDAP_DEBUG_CONNS, " %d%s",
1086 writefds.fd_array[i], "w", 0 );
1089 for ( i = 0; i < nfds; i++ ) {
1091 int is_listener = 0;
1093 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1094 if ( i == slap_listeners[l]->sl_sd ) {
1099 if ( is_listener ) {
1102 r = FD_ISSET( i, &readfds );
1103 w = FD_ISSET( i, &writefds );
1105 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1106 r ? "r" : "", w ? "w" : "" );
1110 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1113 /* loop through the writers */
1115 for ( i = 0; i < writefds.fd_count; i++ )
1117 for ( i = 0; i < nfds; i++ )
1121 int is_listener = 0;
1123 wd = writefds.fd_array[i];
1125 if( ! FD_ISSET( i, &writefds ) ) {
1131 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1132 if ( i == slap_listeners[l]->sl_sd ) {
1137 if ( is_listener ) {
1140 Debug( LDAP_DEBUG_CONNS,
1141 "daemon: write active on %d\n",
1145 * NOTE: it is possible that the connection was closed
1146 * and that the stream is now inactive.
1147 * connection_write() must valid the stream is still
1151 if ( connection_write( wd ) < 0 ) {
1152 FD_CLR( (unsigned) wd, &readfds );
1158 for ( i = 0; i < readfds.fd_count; i++ )
1160 for ( i = 0; i < nfds; i++ )
1164 int is_listener = 0;
1167 rd = readfds.fd_array[i];
1169 if( ! FD_ISSET( i, &readfds ) ) {
1175 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1176 if ( rd == slap_listeners[l]->sl_sd ) {
1181 if ( is_listener ) {
1185 Debug ( LDAP_DEBUG_CONNS,
1186 "daemon: read activity on %d\n", rd, 0, 0 );
1189 * NOTE: it is possible that the connection was closed
1190 * and that the stream is now inactive.
1191 * connection_read() must valid the stream is still
1195 if ( connection_read( rd ) < 0 ) {
1199 ldap_pvt_thread_yield();
1202 if( slapd_shutdown > 0 ) {
1203 Debug( LDAP_DEBUG_TRACE,
1204 "daemon: shutdown requested and initiated.\n",
1207 } else if ( slapd_shutdown < 0 ) {
1208 #ifdef HAVE_NT_SERVICE_MANAGER
1209 if (slapd_shutdown == -1)
1210 Debug( LDAP_DEBUG_TRACE,
1211 "daemon: shutdown initiated by Service Manager.\n",
1215 Debug( LDAP_DEBUG_TRACE,
1216 "daemon: abnormal condition, shutdown initiated.\n",
1219 Debug( LDAP_DEBUG_TRACE,
1220 "daemon: no active streams, shutdown initiated.\n",
1224 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1225 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1226 #ifdef LDAP_PF_LOCAL
1227 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1228 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1230 #endif /* LDAP_PF_LOCAL */
1231 slapd_close( slap_listeners[l]->sl_sd );
1236 Debug( LDAP_DEBUG_ANY,
1237 "slapd shutdown: waiting for %d threads to terminate\n",
1238 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1240 ldap_pvt_thread_pool_destroy(&connection_pool, 1);
1246 int slapd_daemon( void )
1252 #define SLAPD_LISTENER_THREAD 1
1253 #if defined( SLAPD_LISTENER_THREAD )
1255 ldap_pvt_thread_t listener_tid;
1257 /* listener as a separate THREAD */
1258 rc = ldap_pvt_thread_create( &listener_tid,
1259 0, slapd_daemon_task, NULL );
1262 Debug( LDAP_DEBUG_ANY,
1263 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
1267 /* wait for the listener thread to complete */
1268 ldap_pvt_thread_join( listener_tid, (void *) NULL );
1271 /* experimental code */
1272 slapd_daemon_task( NULL );
1281 #if defined( HAVE_WINSOCK2 )
1282 WORD wVersionRequested;
1286 wVersionRequested = MAKEWORD( 2, 0 );
1288 err = WSAStartup( wVersionRequested, &wsaData );
1290 /* Tell the user that we couldn't find a usable */
1295 /* Confirm that the WinSock DLL supports 2.0.*/
1296 /* Note that if the DLL supports versions greater */
1297 /* than 2.0 in addition to 2.0, it will still return */
1298 /* 2.0 in wVersion since that is the version we */
1301 if ( LOBYTE( wsaData.wVersion ) != 2 ||
1302 HIBYTE( wsaData.wVersion ) != 0 )
1304 /* Tell the user that we couldn't find a usable */
1310 /* The WinSock DLL is acceptable. Proceed. */
1311 #elif defined( HAVE_WINSOCK )
1313 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
1320 int sockdestroy(void)
1322 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
1329 slap_sig_shutdown( int sig )
1331 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
1334 * If the NT Service Manager is controlling the server, we don't
1335 * want SIGBREAK to kill the server. For some strange reason,
1336 * SIGBREAK is generated when a user logs out.
1339 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
1340 if (is_NT_Service && sig == SIGBREAK)
1341 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: SIGBREAK ignored.\n",
1345 slapd_shutdown = sig;
1349 /* reinstall self */
1350 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
1354 slap_sig_wake( int sig )
1358 /* reinstall self */
1359 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
1363 void slapd_add_internal(ber_socket_t s) {