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>
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_calloc(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) );
541 s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr );
543 port = ntohs( ((struct sockaddr_in *)*sal) ->sin_port );
544 l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65535") );
545 sprintf( l.sl_name, "IP=%s:%d",
546 s != NULL ? s : "unknown" , port );
551 char addr[INET6_ADDRSTRLEN];
552 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
554 port = ntohs( ((struct sockaddr_in6 *)*sal)->sin6_port );
555 l.sl_name = ch_malloc( strlen(addr) + sizeof("IP= 65535") );
556 sprintf( l.sl_name, "IP=%s %d", addr, port );
558 #endif /* LDAP_PF_INET6 */
561 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
562 (int) (*sal)->sa_family, 0, 0 );
566 slap_free_listener_addresses(psal);
568 l.sl_url = ch_strdup( url );
569 li = ch_malloc( sizeof( Listener ) );
572 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
577 static int sockinit(void);
578 static int sockdestroy(void);
580 int slapd_daemon_init( const char *urls )
585 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
586 urls ? urls : "<null>", 0, 0 );
587 if( (rc = sockinit()) != 0 ) {
592 dtblsize = sysconf( _SC_OPEN_MAX );
593 #elif HAVE_GETDTABLESIZE
594 dtblsize = getdtablesize();
596 dtblsize = FD_SETSIZE;
600 if(dtblsize > FD_SETSIZE) {
601 dtblsize = FD_SETSIZE;
603 #endif /* !FD_SETSIZE */
605 /* open a pipe (or something equivalent connected to itself).
606 * we write a byte on this fd whenever we catch a signal. The main
607 * loop will be select'ing on this socket, and will wake up when
610 if( (rc = lutil_pair( wake_sds )) < 0 ) {
611 Debug( LDAP_DEBUG_ANY,
612 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
616 FD_ZERO( &slap_daemon.sd_readers );
617 FD_ZERO( &slap_daemon.sd_writers );
623 u = str2charray( urls, " " );
625 if( u == NULL || u[0] == NULL ) {
626 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
631 for( i=0; u[i] != NULL; i++ ) {
632 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
637 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
643 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
645 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
647 for(i = 0; u[i] != NULL; i++ ) {
648 slap_listeners[i] = slap_open_listener( u[i] );
650 if( slap_listeners[i] == NULL ) {
655 slap_listeners[i] = NULL;
657 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
662 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
668 slapd_daemon_destroy(void)
670 connections_destroy();
671 tcp_close( wake_sds[1] );
672 tcp_close( wake_sds[0] );
686 time_t last_idle_check = slap_get_time();
689 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
690 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
693 if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN ) == -1 ) {
694 int err = sock_errno();
695 Debug( LDAP_DEBUG_ANY,
696 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
697 slap_listeners[l]->sl_url, err,
702 slapd_add( slap_listeners[l]->sl_sd );
705 #ifdef HAVE_NT_SERVICE_MANAGER
706 if ( started_event != NULL ) {
707 ldap_pvt_thread_cond_signal( &started_event );
710 /* initialization complete. Here comes the loop. */
712 while ( !slapd_shutdown ) {
717 #define SLAPD_EBADF_LIMIT 16
721 #define SLAPD_IDLE_CHECK_LIMIT 4
722 time_t now = slap_get_time();
729 #if defined(SLAPD_RLOOKUPS)
735 if( emfile || ( global_idletimeout > 0 && difftime(
736 last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT,
739 connections_timeout_idle(now);
742 FD_ZERO( &writefds );
748 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
750 #ifdef FD_SET_MANUAL_COPY
751 for( s = 0; s < nfds; s++ ) {
752 if(FD_ISSET( &slap_sd_readers, s )) {
753 FD_SET( s, &readfds );
755 if(FD_ISSET( &slap_sd_writers, s )) {
756 FD_SET( s, &writefds );
760 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
761 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
763 assert(!FD_ISSET(wake_sds[0], &readfds));
764 FD_SET( wake_sds[0], &readfds );
766 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
767 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
769 if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
770 FD_SET( slap_listeners[l]->sl_sd, &readfds );
774 nfds = slap_daemon.sd_nfds;
779 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
781 at = ldap_pvt_thread_pool_backload(&connection_pool);
783 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
786 tvp = at ? &zero : NULL;
789 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
790 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
793 Debug( LDAP_DEBUG_CONNS,
794 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
795 slap_listeners[l]->sl_sd, at,
796 tvp == NULL ? "NULL" : "zero" );
799 switch(ns = select( nfds, &readfds,
801 /* don't pass empty fd_set */
802 ( writefds.fd_count > 0 ? &writefds : NULL ),
808 case -1: { /* failure - try again */
809 int err = sock_errno();
813 /* you'd think this would be EBADF */
814 || err == WSAENOTSOCK
817 if (++ebadf < SLAPD_EBADF_LIMIT)
822 Debug( LDAP_DEBUG_CONNS,
823 "daemon: select failed (%d): %s\n",
824 err, sock_errstr(err), 0 );
830 case 0: /* timeout - let threads run */
832 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
834 ldap_pvt_thread_yield();
837 default: /* something happened - deal with it */
838 if( slapd_shutdown ) continue;
841 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
846 if( FD_ISSET( wake_sds[0], &readfds ) ) {
848 tcp_read( wake_sds[0], c, sizeof(c) );
855 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
857 socklen_t len = sizeof(from);
862 char *dnsname = NULL;
865 char peername[MAXPATHLEN + sizeof("PATH=")];
866 #elif defined(LDAP_PF_INET6)
867 char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
869 char peername[sizeof("IP=255.255.255.255:65336")];
870 #endif /* LDAP_PF_LOCAL */
874 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
877 if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
880 s = accept( slap_listeners[l]->sl_sd,
881 (struct sockaddr *) &from, &len );
882 if ( s == AC_SOCKET_INVALID ) {
883 int err = sock_errno();
886 if( err == EMFILE ) {
891 if( err == ENFILE ) {
900 Debug( LDAP_DEBUG_ANY,
901 "daemon: accept(%ld) failed errno=%d (%s)\n",
902 (long) slap_listeners[l]->sl_sd, err,
905 /* prevent busy loop */
907 if( emfile % 4 == 3 ) usleep( 250 );
909 if( emfile % 8 == 7 ) sleep( 1 );
913 ldap_pvt_thread_yield();
919 /* make sure descriptor number isn't too great */
920 if ( s >= dtblsize ) {
921 Debug( LDAP_DEBUG_ANY,
922 "daemon: %ld beyond descriptor table size %ld\n",
923 (long) s, (long) dtblsize, 0 );
926 ldap_pvt_thread_yield();
932 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
934 /* newly accepted stream should not be in any of the FD SETS */
935 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
936 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
937 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
939 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
942 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
944 /* for IPv4 and IPv6 sockets only */
945 if ( from.sa_addr.sa_family != AF_LOCAL )
946 #endif /* LDAP_PF_LOCAL */
951 /* enable keep alives */
953 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
954 (char *) &tmp, sizeof(tmp) );
955 if ( rc == AC_SOCKET_ERROR ) {
956 int err = sock_errno();
957 Debug( LDAP_DEBUG_ANY,
958 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
959 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
963 /* enable no delay */
965 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
966 (char *)&tmp, sizeof(tmp) );
967 if ( rc == AC_SOCKET_ERROR ) {
968 int err = sock_errno();
969 Debug( LDAP_DEBUG_ANY,
970 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
971 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
977 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
979 switch ( from.sa_addr.sa_family ) {
980 # ifdef LDAP_PF_LOCAL
982 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
983 ssf = LDAP_PVT_SASL_LOCAL_SSF;
985 #endif /* LDAP_PF_LOCAL */
987 # ifdef LDAP_PF_INET6
989 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
990 peeraddr = inet_ntoa( *((struct in_addr *)
991 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
992 sprintf( peername, "IP=%s:%d",
993 peeraddr != NULL ? peeraddr : "unknown",
994 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
996 char addr[INET6_ADDRSTRLEN];
997 sprintf( peername, "IP=%s %d",
999 &from.sa_in6_addr.sin6_addr,
1000 addr, sizeof addr) ? addr : "unknown",
1001 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1004 # endif /* LDAP_PF_INET6 */
1007 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1008 sprintf( peername, "IP=%s:%d",
1009 peeraddr != NULL ? peeraddr : "unknown",
1010 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1018 if ( ( from.sa_addr.sa_family == AF_INET )
1019 #ifdef LDAP_PF_INET6
1020 || ( from.sa_addr.sa_family == AF_INET6 )
1023 #ifdef SLAPD_RLOOKUPS
1024 # ifdef LDAP_PF_INET6
1025 if ( from.sa_addr.sa_family == AF_INET6 )
1027 (char *)&(from.sa_in6_addr.sin6_addr),
1028 sizeof(from.sa_in6_addr.sin6_addr),
1031 # endif /* LDAP_PF_INET6 */
1033 (char *) &(from.sa_in_addr.sin_addr),
1034 sizeof(from.sa_in_addr.sin_addr),
1036 dnsname = hp ? ldap_pvt_str2lower( hp->h_name ) : NULL;
1039 #endif /* SLAPD_RLOOKUPS */
1042 if ( !hosts_ctl("slapd",
1043 dnsname != NULL ? dnsname : STRING_UNKNOWN,
1044 peeraddr != NULL ? peeraddr : STRING_UNKNOWN,
1048 Statslog( LDAP_DEBUG_ANY,
1049 "fd=%ld host access from %s (%s) denied.\n",
1051 dnsname != NULL ? dnsname : "unknown",
1052 peeraddr != NULL ? peeraddr : "unknown",
1057 #endif /* HAVE_TCPD */
1060 id = connection_init(s,
1061 slap_listeners[l]->sl_url,
1062 dnsname != NULL ? dnsname : "unknown",
1064 slap_listeners[l]->sl_name,
1066 slap_listeners[l]->sl_is_tls,
1073 if( authid ) ch_free(authid);
1076 Debug( LDAP_DEBUG_ANY,
1077 "daemon: connection_init(%ld, %s, %s) failed.\n",
1080 slap_listeners[l]->sl_name );
1085 Statslog( LDAP_DEBUG_STATS,
1086 "daemon: conn=%ld fd=%ld connection from %s (%s) accepted.\n",
1089 slap_listeners[l]->sl_name,
1097 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1099 for ( i = 0; i < readfds.fd_count; i++ ) {
1100 Debug( LDAP_DEBUG_CONNS, " %d%s",
1101 readfds.fd_array[i], "r", 0 );
1103 for ( i = 0; i < writefds.fd_count; i++ ) {
1104 Debug( LDAP_DEBUG_CONNS, " %d%s",
1105 writefds.fd_array[i], "w", 0 );
1109 for ( i = 0; i < nfds; i++ ) {
1111 int is_listener = 0;
1113 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1114 if ( i == slap_listeners[l]->sl_sd ) {
1119 if ( is_listener ) {
1122 r = FD_ISSET( i, &readfds );
1123 w = FD_ISSET( i, &writefds );
1125 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1126 r ? "r" : "", w ? "w" : "" );
1130 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1134 /* loop through the writers */
1136 for ( i = 0; i < writefds.fd_count; i++ )
1138 for ( i = 0; i < nfds; i++ )
1142 int is_listener = 0;
1144 wd = writefds.fd_array[i];
1146 if( ! FD_ISSET( i, &writefds ) ) {
1152 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1153 if ( i == slap_listeners[l]->sl_sd ) {
1158 if ( is_listener ) {
1161 Debug( LDAP_DEBUG_CONNS,
1162 "daemon: write active on %d\n",
1165 * NOTE: it is possible that the connection was closed
1166 * and that the stream is now inactive.
1167 * connection_write() must valid the stream is still
1171 if ( connection_write( wd ) < 0 ) {
1172 FD_CLR( (unsigned) wd, &readfds );
1178 for ( i = 0; i < readfds.fd_count; i++ )
1180 for ( i = 0; i < nfds; i++ )
1184 int is_listener = 0;
1187 rd = readfds.fd_array[i];
1189 if( ! FD_ISSET( i, &readfds ) ) {
1195 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1196 if ( rd == slap_listeners[l]->sl_sd ) {
1201 if ( is_listener ) {
1205 Debug ( LDAP_DEBUG_CONNS,
1206 "daemon: read activity on %d\n", rd, 0, 0 );
1208 * NOTE: it is possible that the connection was closed
1209 * and that the stream is now inactive.
1210 * connection_read() must valid the stream is still
1214 if ( connection_read( rd ) < 0 ) {
1218 ldap_pvt_thread_yield();
1221 if( slapd_shutdown > 0 ) {
1222 Debug( LDAP_DEBUG_TRACE,
1223 "daemon: shutdown requested and initiated.\n",
1226 } else if ( slapd_shutdown < 0 ) {
1227 #ifdef HAVE_NT_SERVICE_MANAGER
1228 if (slapd_shutdown == -1)
1230 Debug( LDAP_DEBUG_TRACE,
1231 "daemon: shutdown initiated by Service Manager.\n",
1237 Debug( LDAP_DEBUG_TRACE,
1238 "daemon: abnormal condition, shutdown initiated.\n",
1242 Debug( LDAP_DEBUG_TRACE,
1243 "daemon: no active streams, shutdown initiated.\n",
1247 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1248 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1249 #ifdef LDAP_PF_LOCAL
1250 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1251 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1253 #endif /* LDAP_PF_LOCAL */
1254 slapd_close( slap_listeners[l]->sl_sd );
1259 Debug( LDAP_DEBUG_ANY,
1260 "slapd shutdown: waiting for %d threads to terminate\n",
1261 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1262 ldap_pvt_thread_pool_destroy(&connection_pool, 1);
1268 int slapd_daemon( void )
1274 #define SLAPD_LISTENER_THREAD 1
1275 #if defined( SLAPD_LISTENER_THREAD )
1277 ldap_pvt_thread_t listener_tid;
1279 /* listener as a separate THREAD */
1280 rc = ldap_pvt_thread_create( &listener_tid,
1281 0, slapd_daemon_task, NULL );
1284 Debug( LDAP_DEBUG_ANY,
1285 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
1289 /* wait for the listener thread to complete */
1290 ldap_pvt_thread_join( listener_tid, (void *) NULL );
1293 /* experimental code */
1294 slapd_daemon_task( NULL );
1303 #if defined( HAVE_WINSOCK2 )
1304 WORD wVersionRequested;
1308 wVersionRequested = MAKEWORD( 2, 0 );
1310 err = WSAStartup( wVersionRequested, &wsaData );
1312 /* Tell the user that we couldn't find a usable */
1317 /* Confirm that the WinSock DLL supports 2.0.*/
1318 /* Note that if the DLL supports versions greater */
1319 /* than 2.0 in addition to 2.0, it will still return */
1320 /* 2.0 in wVersion since that is the version we */
1323 if ( LOBYTE( wsaData.wVersion ) != 2 ||
1324 HIBYTE( wsaData.wVersion ) != 0 )
1326 /* Tell the user that we couldn't find a usable */
1332 /* The WinSock DLL is acceptable. Proceed. */
1333 #elif defined( HAVE_WINSOCK )
1335 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
1342 int sockdestroy(void)
1344 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
1351 slap_sig_shutdown( int sig )
1353 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
1356 * If the NT Service Manager is controlling the server, we don't
1357 * want SIGBREAK to kill the server. For some strange reason,
1358 * SIGBREAK is generated when a user logs out.
1361 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
1362 if (is_NT_Service && sig == SIGBREAK)
1363 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: SIGBREAK ignored.\n",
1367 slapd_shutdown = sig;
1371 /* reinstall self */
1372 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
1376 slap_sig_wake( int sig )
1380 /* reinstall self */
1381 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
1385 void slapd_add_internal(ber_socket_t s) {