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 */
106 * Add a descriptor to daemon control
108 static void slapd_add(ber_socket_t s) {
109 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
111 assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
112 assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
113 assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
116 if (s >= slap_daemon.sd_nfds) {
117 slap_daemon.sd_nfds = s + 1;
121 FD_SET( s, &slap_daemon.sd_actives );
122 FD_SET( s, &slap_daemon.sd_readers );
124 Debug( LDAP_DEBUG_CONNS, "daemon: added %ld%s%s\n",
126 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
127 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
128 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
132 * Remove the descriptor from daemon control
134 void slapd_remove(ber_socket_t s, int wake) {
135 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
137 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
139 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
140 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
141 FD_CLR( s, &slap_daemon.sd_actives );
142 FD_CLR( s, &slap_daemon.sd_readers );
143 FD_CLR( s, &slap_daemon.sd_writers );
145 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
149 void slapd_clr_write(ber_socket_t s, int wake) {
150 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
152 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
153 FD_CLR( s, &slap_daemon.sd_writers );
155 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
159 void slapd_set_write(ber_socket_t s, int wake) {
160 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
162 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
163 if (!FD_ISSET(s, &slap_daemon.sd_writers))
164 FD_SET( (unsigned) s, &slap_daemon.sd_writers );
166 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
170 void slapd_clr_read(ber_socket_t s, int wake) {
171 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
173 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
174 FD_CLR( s, &slap_daemon.sd_readers );
176 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
180 void slapd_set_read(ber_socket_t s, int wake) {
181 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
183 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
184 if (!FD_ISSET(s, &slap_daemon.sd_readers))
185 FD_SET( s, &slap_daemon.sd_readers );
187 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
191 static void slapd_close(ber_socket_t s) {
192 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
197 static void slap_free_listener_addresses(struct sockaddr **sal)
199 struct sockaddr **sap;
205 for (sap = sal; *sap != NULL; sap++) {
212 /* port = 0 indicates AF_LOCAL */
213 static int slap_get_listener_addresses(
216 struct sockaddr ***sal)
218 struct sockaddr **sap;
222 *sal = ch_malloc(2 * sizeof(void *));
228 *sap = ch_malloc(sizeof(struct sockaddr_un));
234 (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) ) {
235 Debug( LDAP_DEBUG_ANY,
236 "daemon: domain socket path (%s) too long in URL",
241 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
242 (*sap)->sa_family = AF_LOCAL;
243 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
247 #ifdef HAVE_GETADDRINFO
248 struct addrinfo hints, *res, *sai;
252 memset( &hints, '\0', sizeof(hints) );
253 hints.ai_flags = AI_PASSIVE;
254 hints.ai_socktype = SOCK_STREAM;
255 hints.ai_family = AF_UNSPEC;
256 snprintf(serv, sizeof serv, "%d", port);
258 if (err = getaddrinfo(host, serv, &hints, &res)) {
259 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
260 AC_GAI_STRERROR(err), 0, 0);
265 for (n=2; (sai = sai->ai_next) != NULL; n++) {
268 *sal = ch_malloc(n * sizeof(void *));
275 for ( sai=res; sai; sai=sai->ai_next ) {
276 if( sai->ai_addr == NULL ) {
277 Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
278 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
283 switch (sai->ai_family) {
284 # ifdef LDAP_PF_INET6
286 *sap = ch_malloc(sizeof(struct sockaddr_in6));
291 *(struct sockaddr_in6 *)*sap =
292 *((struct sockaddr_in6 *)sai->ai_addr);
296 *sap = ch_malloc(sizeof(struct sockaddr_in));
301 *(struct sockaddr_in *)*sap =
302 *((struct sockaddr_in *)sai->ai_addr);
309 (*sap)->sa_family = sai->ai_family;
319 if ( host == NULL ) {
320 in.s_addr = htonl(INADDR_ANY);
322 } else if ( !inet_aton( host, &in ) ) {
323 struct hostent *he = gethostbyname( host );
325 Debug( LDAP_DEBUG_ANY,
326 "daemon: invalid host %s", host, 0, 0);
329 AC_MEMCPY( &in, he->h_addr, sizeof( in ) );
332 *sal = ch_malloc(2 * sizeof(void *));
338 *sap = ch_malloc(sizeof(struct sockaddr_in));
344 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_in) );
345 (*sap)->sa_family = AF_INET;
346 ((struct sockaddr_in *)*sap)->sin_port = htons(port);
347 ((struct sockaddr_in *)*sap)->sin_addr = in;
354 slap_free_listener_addresses(*sal);
358 static Listener * slap_open_listener(
367 struct sockaddr **sal, **psal;
369 rc = ldap_url_parse( url, &lud );
371 if( rc != LDAP_URL_SUCCESS ) {
372 Debug( LDAP_DEBUG_ANY,
373 "daemon: listen URL \"%s\" parse error=%d\n",
379 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
380 Debug( LDAP_DEBUG_ANY,
381 "daemon: TLS not supported (%s)\n",
383 ldap_free_urldesc( lud );
387 if(! lud->lud_port ) {
388 lud->lud_port = LDAP_PORT;
392 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
394 if(! lud->lud_port ) {
395 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
399 port = (unsigned short) lud->lud_port;
401 if ( ldap_pvt_url_scheme2proto(lud->lud_scheme) == LDAP_PROTO_IPC ) {
403 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
404 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
406 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
410 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
412 ldap_free_urldesc( lud );
416 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
417 || strcmp(lud->lud_host, "*") == 0 )
419 err = slap_get_listener_addresses(NULL, port, &sal);
421 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
425 ldap_free_urldesc( lud );
431 while ( *sal != NULL ) {
432 switch( (*sal)->sa_family ) {
445 l.sl_sd = socket( (*sal)->sa_family, SOCK_STREAM, 0);
446 if ( l.sl_sd == AC_SOCKET_INVALID ) {
447 int err = sock_errno();
448 Debug( LDAP_DEBUG_ANY,
449 "daemon: socket() failed errno=%d (%s)\n", err,
450 sock_errstr(err), 0 );
455 if ( l.sl_sd >= dtblsize ) {
456 Debug( LDAP_DEBUG_ANY,
457 "daemon: listener descriptor %ld is too great %ld\n",
458 (long) l.sl_sd, (long) dtblsize, 0 );
459 tcp_close( l.sl_sd );
465 if ( (*sal)->sa_family == AF_LOCAL ) {
466 unlink ( ((struct sockaddr_un *)*sal)->sun_path );
471 /* enable address reuse */
473 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
474 (char *) &tmp, sizeof(tmp) );
475 if ( rc == AC_SOCKET_ERROR ) {
476 int err = sock_errno();
477 Debug( LDAP_DEBUG_ANY,
478 "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
479 (long) l.sl_sd, err, sock_errstr(err) );
484 switch( (*sal)->sa_family ) {
486 addrlen = sizeof(struct sockaddr_in);
490 addrlen = sizeof(struct sockaddr_in6);
495 addrlen = sizeof(struct sockaddr_un);
500 if (!bind(l.sl_sd, *sal, addrlen))
503 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
504 (long) l.sl_sd, err, sock_errstr(err) );
505 tcp_close( l.sl_sd );
507 } /* while ( *sal != NULL ) */
509 if ( *sal == NULL ) {
510 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed\n",
511 (long) l.sl_sd, 0, 0 );
512 slap_free_listener_addresses(psal);
516 switch ( (*sal)->sa_family ) {
519 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
520 if ( chmod( addr, S_IRWXU ) < 0 ) {
521 int err = sock_errno();
522 Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
523 (long) l.sl_sd, err, sock_errstr(err) );
524 tcp_close( l.sl_sd );
525 slap_free_listener_addresses(psal);
528 l.sl_name = ch_malloc( strlen(addr) + sizeof("PATH=") );
529 sprintf( l.sl_name, "PATH=%s", addr );
531 #endif /* LDAP_PF_LOCAL */
535 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
536 char addr[INET_ADDRSTRLEN];
537 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
538 addr, sizeof(addr) );
540 port = ((struct sockaddr_in *)*sal) ->sin_port;
542 s = inet_ntoa( l.sl_addr.sin_addr );
543 port = l.sl_addr.sin_port;
545 l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65535") );
546 sprintf( l.sl_name, "IP=%s:%d",
547 s != NULL ? s : "unknown" , port );
552 char addr[INET6_ADDRSTRLEN];
553 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
555 port = ((struct sockaddr_in6 *)*sal)->sin6_port;
556 l.sl_name = ch_malloc( strlen(addr) + sizeof("IP= 65535") );
557 sprintf( l.sl_name, "IP=%s %d", addr, port );
559 #endif /* LDAP_PF_INET6 */
562 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
563 (int) (*sal)->sa_family, 0, 0 );
567 slap_free_listener_addresses(psal);
569 l.sl_url = ch_strdup( url );
570 li = ch_malloc( sizeof( Listener ) );
573 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
578 static int sockinit(void);
579 static int sockdestroy(void);
581 int slapd_daemon_init( const char *urls )
586 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
587 urls ? urls : "<null>", 0, 0 );
588 if( (rc = sockinit()) != 0 ) {
593 dtblsize = sysconf( _SC_OPEN_MAX );
594 #elif HAVE_GETDTABLESIZE
595 dtblsize = getdtablesize();
597 dtblsize = FD_SETSIZE;
601 if(dtblsize > FD_SETSIZE) {
602 dtblsize = FD_SETSIZE;
604 #endif /* !FD_SETSIZE */
606 /* open a pipe (or something equivalent connected to itself).
607 * we write a byte on this fd whenever we catch a signal. The main
608 * loop will be select'ing on this socket, and will wake up when
611 if( (rc = lutil_pair( wake_sds )) < 0 ) {
612 Debug( LDAP_DEBUG_ANY,
613 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
617 FD_ZERO( &slap_daemon.sd_readers );
618 FD_ZERO( &slap_daemon.sd_writers );
624 u = str2charray( urls, " " );
626 if( u == NULL || u[0] == NULL ) {
627 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
632 for( i=0; u[i] != NULL; i++ ) {
633 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
638 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
644 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
646 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
648 for(i = 0; u[i] != NULL; i++ ) {
649 slap_listeners[i] = slap_open_listener( u[i] );
651 if( slap_listeners[i] == NULL ) {
656 slap_listeners[i] = NULL;
658 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
663 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
669 slapd_daemon_destroy(void)
671 connections_destroy();
672 tcp_close( wake_sds[1] );
673 tcp_close( wake_sds[0] );
687 time_t last_idle_check = slap_get_time();
690 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
691 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
694 if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN ) == -1 ) {
695 int err = sock_errno();
696 Debug( LDAP_DEBUG_ANY,
697 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
698 slap_listeners[l]->sl_url, err,
703 slapd_add( slap_listeners[l]->sl_sd );
706 #ifdef HAVE_NT_SERVICE_MANAGER
707 if ( started_event != NULL ) {
708 ldap_pvt_thread_cond_signal( &started_event );
711 /* initialization complete. Here comes the loop. */
713 while ( !slapd_shutdown ) {
718 #define SLAPD_EBADF_LIMIT 16
722 #define SLAPD_IDLE_CHECK_LIMIT 4
723 time_t now = slap_get_time();
730 #if defined(SLAPD_RLOOKUPS)
736 if( emfile || ( global_idletimeout > 0 && difftime(
737 last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT,
740 connections_timeout_idle(now);
743 FD_ZERO( &writefds );
749 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
751 #ifdef FD_SET_MANUAL_COPY
752 for( s = 0; s < nfds; s++ ) {
753 if(FD_ISSET( &slap_sd_readers, s )) {
754 FD_SET( s, &readfds );
756 if(FD_ISSET( &slap_sd_writers, s )) {
757 FD_SET( s, &writefds );
761 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
762 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
764 assert(!FD_ISSET(wake_sds[0], &readfds));
765 FD_SET( wake_sds[0], &readfds );
767 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
768 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
770 if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
771 FD_SET( slap_listeners[l]->sl_sd, &readfds );
775 nfds = slap_daemon.sd_nfds;
780 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
782 at = ldap_pvt_thread_pool_backload(&connection_pool);
784 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
787 tvp = at ? &zero : NULL;
790 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
791 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
794 Debug( LDAP_DEBUG_CONNS,
795 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
796 slap_listeners[l]->sl_sd, at,
797 tvp == NULL ? "NULL" : "zero" );
800 switch(ns = select( nfds, &readfds,
802 /* don't pass empty fd_set */
803 ( writefds.fd_count > 0 ? &writefds : NULL ),
809 case -1: { /* failure - try again */
810 int err = sock_errno();
814 /* you'd think this would be EBADF */
815 || err == WSAENOTSOCK
818 if (++ebadf < SLAPD_EBADF_LIMIT)
823 Debug( LDAP_DEBUG_CONNS,
824 "daemon: select failed (%d): %s\n",
825 err, sock_errstr(err), 0 );
831 case 0: /* timeout - let threads run */
833 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
835 ldap_pvt_thread_yield();
838 default: /* something happened - deal with it */
839 if( slapd_shutdown ) continue;
842 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
847 if( FD_ISSET( wake_sds[0], &readfds ) ) {
849 tcp_read( wake_sds[0], c, sizeof(c) );
856 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
858 socklen_t len = sizeof(from);
863 char *dnsname = NULL;
866 char peername[MAXPATHLEN + sizeof("PATH=")];
867 #elif defined(LDAP_PF_INET6)
868 char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
870 char peername[sizeof("IP=255.255.255.255:65336")];
871 #endif /* LDAP_PF_LOCAL */
875 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
878 if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
881 s = accept( slap_listeners[l]->sl_sd,
882 (struct sockaddr *) &from, &len );
883 if ( s == AC_SOCKET_INVALID ) {
884 int err = sock_errno();
887 if( err == EMFILE ) {
892 if( err == ENFILE ) {
901 Debug( LDAP_DEBUG_ANY,
902 "daemon: accept(%ld) failed errno=%d (%s)\n",
903 (long) slap_listeners[l]->sl_sd, err,
906 /* prevent busy loop */
908 if( emfile % 4 == 3 ) usleep( 250 );
910 if( emfile % 8 == 7 ) sleep( 1 );
914 ldap_pvt_thread_yield();
920 /* make sure descriptor number isn't too great */
921 if ( s >= dtblsize ) {
922 Debug( LDAP_DEBUG_ANY,
923 "daemon: %ld beyond descriptor table size %ld\n",
924 (long) s, (long) dtblsize, 0 );
927 ldap_pvt_thread_yield();
933 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
935 /* newly accepted stream should not be in any of the FD SETS */
936 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
937 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
938 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
940 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
943 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
945 /* for IPv4 and IPv6 sockets only */
946 if ( from.sa_addr.sa_family != AF_LOCAL )
947 #endif /* LDAP_PF_LOCAL */
952 /* enable keep alives */
954 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
955 (char *) &tmp, sizeof(tmp) );
956 if ( rc == AC_SOCKET_ERROR ) {
957 int err = sock_errno();
958 Debug( LDAP_DEBUG_ANY,
959 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
960 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
964 /* enable no delay */
966 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
967 (char *)&tmp, sizeof(tmp) );
968 if ( rc == AC_SOCKET_ERROR ) {
969 int err = sock_errno();
970 Debug( LDAP_DEBUG_ANY,
971 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
972 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
978 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
980 switch ( from.sa_addr.sa_family ) {
981 # ifdef LDAP_PF_LOCAL
983 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
984 ssf = LDAP_PVT_SASL_LOCAL_SSF;
986 #endif /* LDAP_PF_LOCAL */
988 # ifdef LDAP_PF_INET6
990 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
991 peeraddr = inet_ntoa( *((struct in_addr *)
992 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
993 sprintf( peername, "IP=%s:%d",
994 peeraddr != NULL ? peeraddr : "unknown",
995 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
997 char addr[INET6_ADDRSTRLEN];
998 sprintf( peername, "IP=%s %d",
1000 &from.sa_in6_addr.sin6_addr,
1001 addr, sizeof addr) ? addr : "unknown",
1002 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1005 # endif /* LDAP_PF_INET6 */
1008 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1009 sprintf( peername, "IP=%s:%d",
1010 peeraddr != NULL ? peeraddr : "unknown",
1011 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1019 if ( ( from.sa_addr.sa_family == AF_INET )
1020 #ifdef LDAP_PF_INET6
1021 || ( from.sa_addr.sa_family == AF_INET6 )
1024 #ifdef SLAPD_RLOOKUPS
1025 # ifdef LDAP_PF_INET6
1026 if ( from.sa_addr.sa_family == AF_INET6 )
1028 (char *)&(from.sa_in6_addr.sin6_addr),
1029 sizeof(from.sa_in6_addr.sin6_addr),
1032 # endif /* LDAP_PF_INET6 */
1034 (char *) &(from.sa_in_addr.sin_addr),
1035 sizeof(from.sa_in_addr.sin_addr),
1037 dnsname = hp ? ldap_pvt_str2lower( hp->h_name ) : NULL;
1040 #endif /* SLAPD_RLOOKUPS */
1043 if ( !hosts_ctl("slapd",
1044 dnsname != NULL ? dnsname : STRING_UNKNOWN,
1045 peeraddr != NULL ? peeraddr : STRING_UNKNOWN,
1049 Statslog( LDAP_DEBUG_ANY,
1050 "fd=%ld host access from %s (%s) denied.\n",
1052 dnsname != NULL ? dnsname : "unknown",
1053 peeraddr != NULL ? peeraddr : "unknown",
1058 #endif /* HAVE_TCPD */
1061 id = connection_init(s,
1062 slap_listeners[l]->sl_url,
1063 dnsname != NULL ? dnsname : "unknown",
1065 slap_listeners[l]->sl_name,
1067 slap_listeners[l]->sl_is_tls,
1074 if( authid ) ch_free(authid);
1077 Debug( LDAP_DEBUG_ANY,
1078 "daemon: connection_init(%ld, %s, %s) failed.\n",
1081 slap_listeners[l]->sl_name );
1086 Statslog( LDAP_DEBUG_STATS,
1087 "daemon: conn=%ld fd=%ld connection from %s (%s) accepted.\n",
1090 slap_listeners[l]->sl_name,
1098 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1100 for ( i = 0; i < readfds.fd_count; i++ ) {
1101 Debug( LDAP_DEBUG_CONNS, " %d%s",
1102 readfds.fd_array[i], "r", 0 );
1104 for ( i = 0; i < writefds.fd_count; i++ ) {
1105 Debug( LDAP_DEBUG_CONNS, " %d%s",
1106 writefds.fd_array[i], "w", 0 );
1110 for ( i = 0; i < nfds; i++ ) {
1112 int is_listener = 0;
1114 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1115 if ( i == slap_listeners[l]->sl_sd ) {
1120 if ( is_listener ) {
1123 r = FD_ISSET( i, &readfds );
1124 w = FD_ISSET( i, &writefds );
1126 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1127 r ? "r" : "", w ? "w" : "" );
1131 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1135 /* loop through the writers */
1137 for ( i = 0; i < writefds.fd_count; i++ )
1139 for ( i = 0; i < nfds; i++ )
1143 int is_listener = 0;
1145 wd = writefds.fd_array[i];
1147 if( ! FD_ISSET( i, &writefds ) ) {
1153 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1154 if ( i == slap_listeners[l]->sl_sd ) {
1159 if ( is_listener ) {
1162 Debug( LDAP_DEBUG_CONNS,
1163 "daemon: write active on %d\n",
1166 * NOTE: it is possible that the connection was closed
1167 * and that the stream is now inactive.
1168 * connection_write() must valid the stream is still
1172 if ( connection_write( wd ) < 0 ) {
1173 FD_CLR( (unsigned) wd, &readfds );
1179 for ( i = 0; i < readfds.fd_count; i++ )
1181 for ( i = 0; i < nfds; i++ )
1185 int is_listener = 0;
1188 rd = readfds.fd_array[i];
1190 if( ! FD_ISSET( i, &readfds ) ) {
1196 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1197 if ( rd == slap_listeners[l]->sl_sd ) {
1202 if ( is_listener ) {
1206 Debug ( LDAP_DEBUG_CONNS,
1207 "daemon: read activity on %d\n", rd, 0, 0 );
1209 * NOTE: it is possible that the connection was closed
1210 * and that the stream is now inactive.
1211 * connection_read() must valid the stream is still
1215 if ( connection_read( rd ) < 0 ) {
1219 ldap_pvt_thread_yield();
1222 if( slapd_shutdown > 0 ) {
1223 Debug( LDAP_DEBUG_TRACE,
1224 "daemon: shutdown requested and initiated.\n",
1227 } else if ( slapd_shutdown < 0 ) {
1228 #ifdef HAVE_NT_SERVICE_MANAGER
1229 if (slapd_shutdown == -1)
1231 Debug( LDAP_DEBUG_TRACE,
1232 "daemon: shutdown initiated by Service Manager.\n",
1238 Debug( LDAP_DEBUG_TRACE,
1239 "daemon: abnormal condition, shutdown initiated.\n",
1243 Debug( LDAP_DEBUG_TRACE,
1244 "daemon: no active streams, shutdown initiated.\n",
1248 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1249 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1250 #ifdef LDAP_PF_LOCAL
1251 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1252 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1254 #endif /* LDAP_PF_LOCAL */
1255 slapd_close( slap_listeners[l]->sl_sd );
1260 Debug( LDAP_DEBUG_ANY,
1261 "slapd shutdown: waiting for %d threads to terminate\n",
1262 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1263 ldap_pvt_thread_pool_destroy(&connection_pool, 1);
1269 int slapd_daemon( void )
1275 #define SLAPD_LISTENER_THREAD 1
1276 #if defined( SLAPD_LISTENER_THREAD )
1278 ldap_pvt_thread_t listener_tid;
1280 /* listener as a separate THREAD */
1281 rc = ldap_pvt_thread_create( &listener_tid,
1282 0, slapd_daemon_task, NULL );
1285 Debug( LDAP_DEBUG_ANY,
1286 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
1290 /* wait for the listener thread to complete */
1291 ldap_pvt_thread_join( listener_tid, (void *) NULL );
1294 /* experimental code */
1295 slapd_daemon_task( NULL );
1304 #if defined( HAVE_WINSOCK2 )
1305 WORD wVersionRequested;
1309 wVersionRequested = MAKEWORD( 2, 0 );
1311 err = WSAStartup( wVersionRequested, &wsaData );
1313 /* Tell the user that we couldn't find a usable */
1318 /* Confirm that the WinSock DLL supports 2.0.*/
1319 /* Note that if the DLL supports versions greater */
1320 /* than 2.0 in addition to 2.0, it will still return */
1321 /* 2.0 in wVersion since that is the version we */
1324 if ( LOBYTE( wsaData.wVersion ) != 2 ||
1325 HIBYTE( wsaData.wVersion ) != 0 )
1327 /* Tell the user that we couldn't find a usable */
1333 /* The WinSock DLL is acceptable. Proceed. */
1334 #elif defined( HAVE_WINSOCK )
1336 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
1343 int sockdestroy(void)
1345 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
1352 slap_sig_shutdown( int sig )
1354 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
1357 * If the NT Service Manager is controlling the server, we don't
1358 * want SIGBREAK to kill the server. For some strange reason,
1359 * SIGBREAK is generated when a user logs out.
1362 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
1363 if (is_NT_Service && sig == SIGBREAK)
1364 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: SIGBREAK ignored.\n",
1368 slapd_shutdown = sig;
1372 /* reinstall self */
1373 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
1377 slap_sig_wake( int sig )
1381 /* reinstall self */
1382 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
1386 void slapd_add_internal(ber_socket_t s) {