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",
195 static Listener * open_listener( const char* url )
203 #ifdef HAVE_GETADDRINFO
205 struct addrinfo hints, *res, *sai;
209 rc = ldap_url_parse( url, &lud );
211 if( rc != LDAP_URL_SUCCESS ) {
212 Debug( LDAP_DEBUG_ANY,
213 "daemon: listen URL \"%s\" parse error=%d\n",
219 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
220 Debug( LDAP_DEBUG_ANY,
221 "daemon: TLS not supported (%s)\n",
223 ldap_free_urldesc( lud );
227 if(! lud->lud_port ) {
228 lud->lud_port = LDAP_PORT;
232 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
234 if(! lud->lud_port ) {
235 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
239 #ifdef HAVE_GETADDRINFO
240 memset( &hints, '\0', sizeof(hints) );
241 hints.ai_flags = AI_PASSIVE;
242 hints.ai_family = AF_UNSPEC;
243 hints.ai_socktype = SOCK_STREAM;
245 # ifdef LDAP_PF_LOCAL
246 if ( ldap_pvt_url_scheme2proto(lud->lud_scheme) == LDAP_PROTO_IPC ) {
247 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
248 err = getaddrinfo(NULL, LDAPI_SOCK, &hints, &res);
250 unlink( LDAPI_SOCK );
252 err = getaddrinfo(NULL, lud->lud_host, &hints, &res);
254 unlink( lud->lud_host );
257 # endif /* LDAP_PF_LOCAL */
259 snprintf(serv, sizeof serv, "%d", lud->lud_port);
260 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
261 || strcmp(lud->lud_host, "*") == 0 )
263 err = getaddrinfo(NULL, serv, &hints, &res);
265 err = getaddrinfo(lud->lud_host, serv, &hints, &res);
270 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n", gai_strerror(err), 0, 0);
271 ldap_free_urldesc( lud );
275 ldap_free_urldesc( lud );
278 switch( sai->ai_family ) {
280 # ifdef LDAP_PF_INET6
283 # ifdef LDAP_PF_LOCAL
291 l.sl_sd = socket( sai->ai_family, sai->ai_socktype, sai->ai_protocol);
292 if ( l.sl_sd == AC_SOCKET_INVALID ) {
293 int err = sock_errno();
294 Debug( LDAP_DEBUG_ANY,
295 "daemon: socket() failed errno=%d (%s)\n", err,
296 sock_errstr(err), 0 );
300 if ( sai->ai_family != AF_LOCAL ) {
303 if ( ldap_pvt_url_scheme2proto(lud->lud_scheme) == LDAP_PROTO_IPC ) {
306 (void) memset( (void *)&l.sl_sa.sa_un_addr, '\0', sizeof(l.sl_sa.sa_un_addr) );
308 l.sl_sa.sa_un_addr.sun_family = AF_LOCAL;
310 /* hack: overload the host to be the path */
311 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
312 strcpy( l.sl_sa.sa_un_addr.sun_path, LDAPI_SOCK );
314 if ( strlen(lud->lud_host) > (sizeof(l.sl_sa.sa_un_addr.sun_path) - 1) ) {
315 Debug( LDAP_DEBUG_ANY,
316 "daemon: domain socket path (%s) too long in URL: %s",
317 lud->lud_host, url, 0);
318 ldap_free_urldesc( lud );
321 strcpy( l.sl_sa.sa_un_addr.sun_path, lud->lud_host );
323 unlink( l.sl_sa.sa_un_addr.sun_path );
325 /* I don't think we need to set this. */
326 l.sl_sa.sa_un_addr.sun_len = sizeof( l.sl_sa.sa_un_addr.sun_len ) +
327 sizeof( l.sl_sa.sa_un_addr.sun_family ) +
328 strlen( l.sl_sa.sa_un_addr.sun_path ) + 1;
331 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
333 ldap_free_urldesc( lud );
335 #endif /* LDAP_PF_LOCAL */
338 port = lud->lud_port;
340 (void) memset( (void*) &l.sl_addr, '\0', sizeof(l.sl_addr) );
342 l.sl_addr.sin_family = AF_INET;
343 l.sl_addr.sin_port = htons( (unsigned short) lud->lud_port );
345 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
346 || strcmp(lud->lud_host, "*") == 0 )
348 l.sl_addr.sin_addr.s_addr = htonl(INADDR_ANY);
351 /* host or address was specified */
352 if( !inet_aton( lud->lud_host, &l.sl_addr.sin_addr ) ) {
353 struct hostent *he = gethostbyname( lud->lud_host );
355 Debug( LDAP_DEBUG_ANY,
356 "daemon: invalid host (%s) in URL: %s",
357 lud->lud_host, url, 0);
358 ldap_free_urldesc( lud );
362 AC_MEMCPY( &l.sl_addr.sin_addr, he->h_addr,
363 sizeof( l.sl_addr.sin_addr ) );
368 ldap_free_urldesc( lud );
370 l.sl_sd = socket( l.sl_sa.sa_addr.sa_family, SOCK_STREAM, 0 );
371 if ( l.sl_sd == AC_SOCKET_INVALID ) {
372 int err = sock_errno();
373 Debug( LDAP_DEBUG_ANY,
374 "daemon: socket() failed errno=%d (%s)\n", err,
375 sock_errstr(err), 0 );
380 if ( l.sl_sd >= dtblsize ) {
381 Debug( LDAP_DEBUG_ANY,
382 "daemon: listener descriptor %ld is too great %ld\n",
383 (long) l.sl_sd, (long) dtblsize, 0 );
384 tcp_close( l.sl_sd );
390 /* for IPv4 and IPv6 sockets only */
391 if ( l.sl_sa.sa_addr.sa_family != AF_LOCAL ) {
392 #endif /* LDAP_PF_LOCAL */
393 #endif /* HAVE_GETADDRINFO */
396 /* enable address reuse */
398 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
399 (char *) &tmp, sizeof(tmp) );
400 if ( rc == AC_SOCKET_ERROR ) {
401 int err = sock_errno();
402 Debug( LDAP_DEBUG_ANY,
403 "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
404 (long) l.sl_sd, err, sock_errstr(err) );
408 #ifdef HAVE_GETADDRINFO
409 } /* sai->ai_family != AF_LOCAL */
410 if (!bind(l.sl_sd, sai->ai_addr, sai->ai_addrlen))
413 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
414 (long) l.sl_sd, err, sock_errstr(err) );
415 tcp_close( l.sl_sd );
416 } while ((sai = sai->ai_next) != NULL);
419 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed\n",
420 (long) l.sl_sd, 0, 0 );
424 switch ( sai->ai_family ) {
425 # ifdef LDAP_PF_LOCAL
427 if ( chmod( (char *)sai->ai_addr, S_IRWXU ) < 0 ) {
428 int err = sock_errno();
429 Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
430 (long) l.sl_sd, err, sock_errstr(err) );
431 tcp_close( l.sl_sd );
434 l.sl_name = ch_malloc( strlen((char *)sai->ai_addr) + sizeof("PATH=") );
435 sprintf( l.sl_name, "PATH=%s", sai->ai_addr );
437 # endif /* LDAP_PF_LOCAL */
440 char addr[INET_ADDRSTRLEN];
442 &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
443 addr, sizeof(addr) );
444 l.sl_name = ch_malloc( strlen(addr) + strlen(serv) + sizeof("IP=:") );
445 sprintf( l.sl_name, "IP=%s:%s", addr, serv );
448 # ifdef LDAP_PF_INET6
450 char addr[INET6_ADDRSTRLEN];
452 &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
454 l.sl_name = ch_malloc( strlen(addr) + strlen(serv) + sizeof("IP= ") );
455 sprintf( l.sl_name, "IP=%s %s", addr, serv );
457 # endif /* LDAP_PF_INET6 */
460 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
461 (int) sai->ai_family, 0, 0 );
466 /* close conditional */
468 #endif /* LDAP_PF_LOCAL */
470 switch ( l.sl_sa.sa_addr.sa_family ) {
473 rc = bind( l.sl_sd, (struct sockaddr *)&l.sl_sa,
474 sizeof(l.sl_sa.sa_un_addr) );
479 rc = bind( l.sl_sd, (struct sockaddr *)&l.sl_sa,
480 sizeof(l.sl_sa.sa_in_addr) );
484 rc = AC_SOCKET_ERROR;
489 if ( rc == AC_SOCKET_ERROR ) {
490 int err = sock_errno();
491 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
492 (long) l.sl_sd, err, sock_errstr(err) );
493 tcp_close( l.sl_sd );
497 switch ( l.sl_sa.sa_addr.sa_family ) {
500 if ( chmod( l.sl_sa.sa_un_addr.sun_path, S_IRWXU ) < 0 ) {
501 int err = sock_errno();
502 Debug( LDAP_DEBUG_ANY,
503 "daemon: chmod(%ld) failed errno=%d (%s)",
504 (long) l.sl_sd, err, sock_errstr(err) );
505 tcp_close( l.sl_sd );
509 l.sl_name = ch_malloc( strlen(l.sl_sa.sa_un_addr.sun_path)
511 sprintf( l.sl_name, "PATH=%s", l.sl_sa.sa_un_addr.sun_path );
513 #endif /* LDAP_PF_LOCAL */
516 l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65336") );
517 s = inet_ntoa( l.sl_addr.sin_addr );
518 sprintf( l.sl_name, "IP=%s:%d",
519 s != NULL ? s : "unknown" , port );
523 l.sl_name = ch_strdup( "UNKNOWN" );
527 #endif /* HAVE_GETADDRINFO */
529 l.sl_url = ch_strdup( url );
530 li = ch_malloc( sizeof( Listener ) );
533 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
539 static int sockinit(void);
540 static int sockdestroy(void);
542 int slapd_daemon_init( const char *urls )
547 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
548 urls ? urls : "<null>", 0, 0 );
550 if( (rc = sockinit()) != 0 ) {
555 dtblsize = sysconf( _SC_OPEN_MAX );
556 #elif HAVE_GETDTABLESIZE
557 dtblsize = getdtablesize();
559 dtblsize = FD_SETSIZE;
563 if(dtblsize > FD_SETSIZE) {
564 dtblsize = FD_SETSIZE;
566 #endif /* !FD_SETSIZE */
568 /* open a pipe (or something equivalent connected to itself).
569 * we write a byte on this fd whenever we catch a signal. The main
570 * loop will be select'ing on this socket, and will wake up when
573 if( (rc = lutil_pair( wake_sds )) < 0 ) {
574 Debug( LDAP_DEBUG_ANY,
575 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
579 FD_ZERO( &slap_daemon.sd_readers );
580 FD_ZERO( &slap_daemon.sd_writers );
586 u = str2charray( urls, " " );
588 if( u == NULL || u[0] == NULL ) {
589 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
595 for( i=0; u[i] != NULL; i++ ) {
596 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
601 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
607 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
610 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
612 for(i = 0; u[i] != NULL; i++ ) {
613 slap_listeners[i] = open_listener( u[i] );
615 if( slap_listeners[i] == NULL ) {
620 slap_listeners[i] = NULL;
622 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
626 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
632 slapd_daemon_destroy(void)
634 connections_destroy();
635 tcp_close( wake_sds[1] );
636 tcp_close( wake_sds[0] );
648 time_t last_idle_check = slap_get_time();
651 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
652 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
655 if ( listen( slap_listeners[l]->sl_sd, 5 ) == -1 ) {
656 int err = sock_errno();
657 Debug( LDAP_DEBUG_ANY,
658 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
659 slap_listeners[l]->sl_url, err,
664 slapd_add( slap_listeners[l]->sl_sd );
667 #ifdef HAVE_NT_SERVICE_MANAGER
668 if ( started_event != NULL ) {
669 ldap_pvt_thread_cond_signal( &started_event );
672 /* initialization complete. Here comes the loop. */
674 while ( !slapd_shutdown ) {
679 #define SLAPD_EBADF_LIMIT 16
682 #define SLAPD_IDLE_CHECK_LIMIT 4
683 time_t now = slap_get_time();
690 #if defined(SLAPD_RLOOKUPS)
696 if( global_idletimeout > 0 && difftime(
697 last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT,
700 connections_timeout_idle(now);
703 FD_ZERO( &writefds );
709 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
711 #ifdef FD_SET_MANUAL_COPY
712 for( s = 0; s < nfds; s++ ) {
713 if(FD_ISSET( &slap_sd_readers, s )) {
714 FD_SET( s, &readfds );
716 if(FD_ISSET( &slap_sd_writers, s )) {
717 FD_SET( s, &writefds );
721 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
722 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
724 assert(!FD_ISSET(wake_sds[0], &readfds));
725 FD_SET( wake_sds[0], &readfds );
727 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
728 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
730 if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
731 FD_SET( slap_listeners[l]->sl_sd, &readfds );
735 nfds = slap_daemon.sd_nfds;
740 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
742 at = ldap_pvt_thread_pool_backload(&connection_pool);
744 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
747 tvp = at ? &zero : NULL;
750 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
751 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
754 Debug( LDAP_DEBUG_CONNS,
755 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
756 slap_listeners[l]->sl_sd, at,
757 tvp == NULL ? "NULL" : "zero" );
760 switch(ns = select( nfds, &readfds,
762 /* don't pass empty fd_set */
763 ( writefds.fd_count > 0 ? &writefds : NULL ),
769 case -1: { /* failure - try again */
770 int err = sock_errno();
774 /* you'd think this would be EBADF */
775 || err == WSAENOTSOCK
778 if (++ebadf < SLAPD_EBADF_LIMIT)
783 Debug( LDAP_DEBUG_CONNS,
784 "daemon: select failed (%d): %s\n",
785 err, sock_errstr(err), 0 );
792 case 0: /* timeout - let threads run */
794 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
796 ldap_pvt_thread_yield();
799 default: /* something happened - deal with it */
800 if( slapd_shutdown ) continue;
803 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
808 if( FD_ISSET( wake_sds[0], &readfds ) ) {
810 tcp_read( wake_sds[0], c, sizeof(c) );
817 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
819 socklen_t len = sizeof(from);
827 char peername[MAXPATHLEN + sizeof("PATH=")];
828 #elif defined(LDAP_PF_INET6)
829 char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
831 char peername[sizeof("IP=255.255.255.255:65336")];
832 #endif /* LDAP_PF_LOCAL */
836 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
839 if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
842 s = accept( slap_listeners[l]->sl_sd,
843 (struct sockaddr *) &from, &len );
844 if ( s == AC_SOCKET_INVALID ) {
845 int err = sock_errno();
846 Debug( LDAP_DEBUG_ANY,
847 "daemon: accept(%ld) failed errno=%d (%s)\n",
848 (long) slap_listeners[l]->sl_sd, err,
854 /* make sure descriptor number isn't too great */
855 if ( s >= dtblsize ) {
856 Debug( LDAP_DEBUG_ANY,
857 "daemon: %ld beyond descriptor table size %ld\n",
858 (long) s, (long) dtblsize, 0 );
865 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
867 /* newly accepted stream should not be in any of the FD SETS */
868 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
869 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
870 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
872 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
875 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
877 /* for IPv4 and IPv6 sockets only */
878 if ( from.sa_addr.sa_family != AF_LOCAL )
879 #endif /* LDAP_PF_LOCAL */
884 /* enable keep alives */
886 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
887 (char *) &tmp, sizeof(tmp) );
888 if ( rc == AC_SOCKET_ERROR ) {
889 int err = sock_errno();
890 Debug( LDAP_DEBUG_ANY,
891 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
892 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
896 /* enable no delay */
898 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
899 (char *)&tmp, sizeof(tmp) );
900 if ( rc == AC_SOCKET_ERROR ) {
901 int err = sock_errno();
902 Debug( LDAP_DEBUG_ANY,
903 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
904 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
910 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
913 switch ( from.sa_addr.sa_family ) {
914 # ifdef LDAP_PF_LOCAL
916 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
917 ssf = LDAP_PVT_SASL_LOCAL_SSF;
919 #endif /* LDAP_PF_LOCAL */
921 # ifdef LDAP_PF_INET6
923 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
924 peeraddr = inet_ntoa( *((struct in_addr *)
925 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
926 sprintf( peername, "IP=%s:%d",
927 peeraddr != NULL ? peeraddr : "unknown",
928 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
930 char addr[INET6_ADDRSTRLEN];
931 sprintf( peername, "IP=%s %d",
933 &from.sa_in6_addr.sin6_addr,
934 addr, sizeof addr) ? addr : "unknown",
935 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
938 # endif /* LDAP_PF_INET6 */
941 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
942 sprintf( peername, "IP=%s:%d",
943 peeraddr != NULL ? peeraddr : "unknown",
944 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
952 if ( ( from.sa_addr.sa_family == AF_INET )
954 || ( from.sa_addr.sa_family == AF_INET6 )
957 #ifdef SLAPD_RLOOKUPS
958 # ifdef LDAP_PF_INET6
959 if ( from.sa_addr.sa_family == AF_INET6 )
961 (char *)&(from.sa_in6_addr.sin6_addr),
962 sizeof(from.sa_in6_addr.sin6_addr),
965 # endif LDAP_PF_INET6
967 (char *) &(from.sa_in_addr.sin_addr),
968 sizeof(from.sa_in_addr.sin_addr),
970 dnsname = hp ? ldap_pvt_str2lower( hp->h_name ) : NULL;
973 #endif /* SLAPD_RLOOKUPS */
976 if ( !hosts_ctl("slapd",
977 dnsname != NULL ? dnsname : STRING_UNKNOWN,
978 peeraddr != NULL ? peeraddr : STRING_UNKNOWN,
982 Statslog( LDAP_DEBUG_ANY,
983 "fd=%ld host access from %s (%s) denied.\n",
985 dnsname != NULL ? dnsname : "unknown",
986 peeraddr != NULL ? peeraddr : "unknown",
991 #endif /* HAVE_TCPD */
994 id = connection_init(s,
995 slap_listeners[l]->sl_url,
996 dnsname != NULL ? dnsname : "unknown",
998 slap_listeners[l]->sl_name,
1000 slap_listeners[l]->sl_is_tls,
1007 if( authid ) ch_free(authid);
1010 Debug( LDAP_DEBUG_ANY,
1011 "daemon: connection_init(%ld, %s, %s) failed.\n",
1014 slap_listeners[l]->sl_name );
1019 Statslog( LDAP_DEBUG_STATS,
1020 "daemon: conn=%ld fd=%ld connection from %s (%s) accepted.\n",
1023 slap_listeners[l]->sl_name,
1031 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1033 for ( i = 0; i < readfds.fd_count; i++ ) {
1034 Debug( LDAP_DEBUG_CONNS, " %d%s",
1035 readfds.fd_array[i], "r", 0 );
1037 for ( i = 0; i < writefds.fd_count; i++ ) {
1038 Debug( LDAP_DEBUG_CONNS, " %d%s",
1039 writefds.fd_array[i], "w", 0 );
1042 for ( i = 0; i < nfds; i++ ) {
1044 int is_listener = 0;
1046 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1047 if ( i == slap_listeners[l]->sl_sd ) {
1052 if ( is_listener ) {
1055 r = FD_ISSET( i, &readfds );
1056 w = FD_ISSET( i, &writefds );
1058 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1059 r ? "r" : "", w ? "w" : "" );
1063 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1066 /* loop through the writers */
1068 for ( i = 0; i < writefds.fd_count; i++ )
1070 for ( i = 0; i < nfds; i++ )
1074 int is_listener = 0;
1076 wd = writefds.fd_array[i];
1078 if( ! FD_ISSET( i, &writefds ) ) {
1084 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1085 if ( i == slap_listeners[l]->sl_sd ) {
1090 if ( is_listener ) {
1093 Debug( LDAP_DEBUG_CONNS,
1094 "daemon: write active on %d\n",
1098 * NOTE: it is possible that the connection was closed
1099 * and that the stream is now inactive.
1100 * connection_write() must valid the stream is still
1104 if ( connection_write( wd ) < 0 ) {
1105 FD_CLR( (unsigned) wd, &readfds );
1111 for ( i = 0; i < readfds.fd_count; i++ )
1113 for ( i = 0; i < nfds; i++ )
1117 int is_listener = 0;
1120 rd = readfds.fd_array[i];
1122 if( ! FD_ISSET( i, &readfds ) ) {
1128 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1129 if ( rd == slap_listeners[l]->sl_sd ) {
1134 if ( is_listener ) {
1138 Debug ( LDAP_DEBUG_CONNS,
1139 "daemon: read activity on %d\n", rd, 0, 0 );
1142 * NOTE: it is possible that the connection was closed
1143 * and that the stream is now inactive.
1144 * connection_read() must valid the stream is still
1148 if ( connection_read( rd ) < 0 ) {
1152 ldap_pvt_thread_yield();
1155 if( slapd_shutdown > 0 ) {
1156 Debug( LDAP_DEBUG_TRACE,
1157 "daemon: shutdown requested and initiated.\n",
1160 } else if ( slapd_shutdown < 0 ) {
1161 #ifdef HAVE_NT_SERVICE_MANAGER
1162 if (slapd_shutdown == -1)
1163 Debug( LDAP_DEBUG_TRACE,
1164 "daemon: shutdown initiated by Service Manager.\n",
1168 Debug( LDAP_DEBUG_TRACE,
1169 "daemon: abnormal condition, shutdown initiated.\n",
1172 Debug( LDAP_DEBUG_TRACE,
1173 "daemon: no active streams, shutdown initiated.\n",
1177 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1178 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1179 #ifdef LDAP_PF_LOCAL
1180 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1181 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1183 #endif /* LDAP_PF_LOCAL */
1184 slapd_close( slap_listeners[l]->sl_sd );
1189 Debug( LDAP_DEBUG_ANY,
1190 "slapd shutdown: waiting for %d threads to terminate\n",
1191 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1193 ldap_pvt_thread_pool_destroy(&connection_pool, 1);
1199 int slapd_daemon( void )
1205 #define SLAPD_LISTENER_THREAD 1
1206 #if defined( SLAPD_LISTENER_THREAD )
1208 ldap_pvt_thread_t listener_tid;
1210 /* listener as a separate THREAD */
1211 rc = ldap_pvt_thread_create( &listener_tid,
1212 0, slapd_daemon_task, NULL );
1215 Debug( LDAP_DEBUG_ANY,
1216 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
1220 /* wait for the listener thread to complete */
1221 ldap_pvt_thread_join( listener_tid, (void *) NULL );
1224 /* experimental code */
1225 slapd_daemon_task( NULL );
1234 #if defined( HAVE_WINSOCK2 )
1235 WORD wVersionRequested;
1239 wVersionRequested = MAKEWORD( 2, 0 );
1241 err = WSAStartup( wVersionRequested, &wsaData );
1243 /* Tell the user that we couldn't find a usable */
1248 /* Confirm that the WinSock DLL supports 2.0.*/
1249 /* Note that if the DLL supports versions greater */
1250 /* than 2.0 in addition to 2.0, it will still return */
1251 /* 2.0 in wVersion since that is the version we */
1254 if ( LOBYTE( wsaData.wVersion ) != 2 ||
1255 HIBYTE( wsaData.wVersion ) != 0 )
1257 /* Tell the user that we couldn't find a usable */
1263 /* The WinSock DLL is acceptable. Proceed. */
1264 #elif defined( HAVE_WINSOCK )
1266 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
1273 int sockdestroy(void)
1275 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
1282 slap_sig_shutdown( int sig )
1284 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
1287 * If the NT Service Manager is controlling the server, we don't
1288 * want SIGBREAK to kill the server. For some strange reason,
1289 * SIGBREAK is generated when a user logs out.
1292 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
1293 if (is_NT_Service && sig == SIGBREAK)
1294 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: SIGBREAK ignored.\n",
1298 slapd_shutdown = sig;
1302 /* reinstall self */
1303 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
1307 slap_sig_wake( int sig )
1311 /* reinstall self */
1312 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
1316 void slapd_add_internal(ber_socket_t s) {