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\n", 0, 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 */
801 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
806 if( FD_ISSET( wake_sds[0], &readfds ) ) {
808 tcp_read( wake_sds[0], c, sizeof(c) );
815 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
817 socklen_t len = sizeof(from);
825 char peername[MAXPATHLEN + sizeof("PATH=")];
826 #elif defined(LDAP_PF_INET6)
827 char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
829 char peername[sizeof("IP=255.255.255.255:65336")];
830 #endif /* LDAP_PF_LOCAL */
834 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
837 if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
840 if ( (s = accept( slap_listeners[l]->sl_sd,
841 (struct sockaddr *) &from, &len )) == AC_SOCKET_INVALID )
843 int err = sock_errno();
844 Debug( LDAP_DEBUG_ANY,
845 "daemon: accept(%ld) failed errno=%d (%s)\n",
846 (long) slap_listeners[l]->sl_sd, err,
852 /* make sure descriptor number isn't too great */
853 if ( s >= dtblsize ) {
854 Debug( LDAP_DEBUG_ANY,
855 "daemon: %ld beyond descriptor table size %ld\n",
856 (long) s, (long) dtblsize, 0 );
863 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
865 /* newly accepted stream should not be in any of the FD SETS */
866 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
867 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
868 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
870 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
873 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
875 /* for IPv4 and IPv6 sockets only */
876 if ( from.sa_addr.sa_family != AF_LOCAL )
877 #endif /* LDAP_PF_LOCAL */
882 /* enable keep alives */
884 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
885 (char *) &tmp, sizeof(tmp) );
886 if ( rc == AC_SOCKET_ERROR ) {
887 int err = sock_errno();
888 Debug( LDAP_DEBUG_ANY,
889 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
890 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
894 /* enable no delay */
896 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
897 (char *)&tmp, sizeof(tmp) );
898 if ( rc == AC_SOCKET_ERROR ) {
899 int err = sock_errno();
900 Debug( LDAP_DEBUG_ANY,
901 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
902 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
908 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
911 switch ( from.sa_addr.sa_family ) {
912 # ifdef LDAP_PF_LOCAL
914 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
915 ssf = LDAP_PVT_SASL_LOCAL_SSF;
917 #endif /* LDAP_PF_LOCAL */
919 # ifdef LDAP_PF_INET6
921 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
922 peeraddr = inet_ntoa( *((struct in_addr *)
923 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
924 sprintf( peername, "IP=%s:%d",
925 peeraddr != NULL ? peeraddr : "unknown",
926 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
928 char addr[INET6_ADDRSTRLEN];
929 sprintf( peername, "IP=%s %d",
931 &from.sa_in6_addr.sin6_addr,
932 addr, sizeof addr) ? addr : "unknown",
933 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
936 # endif /* LDAP_PF_INET6 */
939 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
940 sprintf( peername, "IP=%s:%d",
941 peeraddr != NULL ? peeraddr : "unknown",
942 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
950 if ( ( from.sa_addr.sa_family == AF_INET )
952 || ( from.sa_addr.sa_family == AF_INET6 )
955 #ifdef SLAPD_RLOOKUPS
956 # ifdef LDAP_PF_INET6
957 if ( from.sa_addr.sa_family == AF_INET6 )
959 (char *)&(from.sa_in6_addr.sin6_addr),
960 sizeof(from.sa_in6_addr.sin6_addr),
963 # endif LDAP_PF_INET6
965 (char *) &(from.sa_in_addr.sin_addr),
966 sizeof(from.sa_in_addr.sin_addr),
968 dnsname = hp ? ldap_pvt_str2lower( hp->h_name ) : NULL;
971 #endif /* SLAPD_RLOOKUPS */
974 if ( !hosts_ctl("slapd",
975 dnsname != NULL ? dnsname : STRING_UNKNOWN,
976 peeraddr != NULL ? peeraddr : STRING_UNKNOWN,
980 Statslog( LDAP_DEBUG_ANY,
981 "fd=%ld host access from %s (%s) denied.\n",
983 dnsname != NULL ? dnsname : "unknown",
984 peeraddr != NULL ? peeraddr : "unknown",
989 #endif /* HAVE_TCPD */
992 id = connection_init(s,
993 slap_listeners[l]->sl_url,
994 dnsname != NULL ? dnsname : "unknown",
996 slap_listeners[l]->sl_name,
998 slap_listeners[l]->sl_is_tls,
1005 if( authid ) ch_free(authid);
1008 Debug( LDAP_DEBUG_ANY,
1009 "daemon: connection_init(%ld, %s, %s) failed.\n",
1012 slap_listeners[l]->sl_name );
1017 Statslog( LDAP_DEBUG_STATS,
1018 "daemon: conn=%ld fd=%ld connection from %s (%s) accepted.\n",
1021 slap_listeners[l]->sl_name,
1029 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1031 for ( i = 0; i < readfds.fd_count; i++ ) {
1032 Debug( LDAP_DEBUG_CONNS, " %d%s",
1033 readfds.fd_array[i], "r", 0 );
1035 for ( i = 0; i < writefds.fd_count; i++ ) {
1036 Debug( LDAP_DEBUG_CONNS, " %d%s",
1037 writefds.fd_array[i], "w", 0 );
1040 for ( i = 0; i < nfds; i++ ) {
1042 int is_listener = 0;
1044 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1045 if ( i == slap_listeners[l]->sl_sd ) {
1050 if ( is_listener ) {
1053 r = FD_ISSET( i, &readfds );
1054 w = FD_ISSET( i, &writefds );
1056 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1057 r ? "r" : "", w ? "w" : "" );
1061 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1064 /* loop through the writers */
1066 for ( i = 0; i < writefds.fd_count; i++ )
1068 for ( i = 0; i < nfds; i++ )
1072 int is_listener = 0;
1074 wd = writefds.fd_array[i];
1076 if( ! FD_ISSET( i, &writefds ) ) {
1082 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1083 if ( i == slap_listeners[l]->sl_sd ) {
1088 if ( is_listener ) {
1091 Debug( LDAP_DEBUG_CONNS,
1092 "daemon: write active on %d\n",
1096 * NOTE: it is possible that the connection was closed
1097 * and that the stream is now inactive.
1098 * connection_write() must valid the stream is still
1102 if ( connection_write( wd ) < 0 ) {
1103 FD_CLR( (unsigned) wd, &readfds );
1109 for ( i = 0; i < readfds.fd_count; i++ )
1111 for ( i = 0; i < nfds; i++ )
1115 int is_listener = 0;
1118 rd = readfds.fd_array[i];
1120 if( ! FD_ISSET( i, &readfds ) ) {
1126 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1127 if ( rd == slap_listeners[l]->sl_sd ) {
1132 if ( is_listener ) {
1136 Debug ( LDAP_DEBUG_CONNS,
1137 "daemon: read activity on %d\n", rd, 0, 0 );
1140 * NOTE: it is possible that the connection was closed
1141 * and that the stream is now inactive.
1142 * connection_read() must valid the stream is still
1146 if ( connection_read( rd ) < 0 ) {
1150 ldap_pvt_thread_yield();
1153 if( slapd_shutdown > 0 ) {
1154 Debug( LDAP_DEBUG_TRACE,
1155 "daemon: shutdown requested and initiated.\n",
1158 } else if ( slapd_shutdown < 0 ) {
1159 #ifdef HAVE_NT_SERVICE_MANAGER
1160 if (slapd_shutdown == -1)
1161 Debug( LDAP_DEBUG_TRACE,
1162 "daemon: shutdown initiated by Service Manager.\n",
1166 Debug( LDAP_DEBUG_TRACE,
1167 "daemon: abnormal condition, shutdown initiated.\n",
1170 Debug( LDAP_DEBUG_TRACE,
1171 "daemon: no active streams, shutdown initiated.\n",
1175 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1176 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1177 #ifdef LDAP_PF_LOCAL
1178 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1179 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1181 #endif /* LDAP_PF_LOCAL */
1182 slapd_close( slap_listeners[l]->sl_sd );
1187 Debug( LDAP_DEBUG_ANY,
1188 "slapd shutdown: waiting for %d threads to terminate\n",
1189 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1191 ldap_pvt_thread_pool_destroy(&connection_pool, 1);
1197 int slapd_daemon( void )
1203 #define SLAPD_LISTENER_THREAD 1
1204 #if defined( SLAPD_LISTENER_THREAD )
1206 ldap_pvt_thread_t listener_tid;
1208 /* listener as a separate THREAD */
1209 rc = ldap_pvt_thread_create( &listener_tid,
1210 0, slapd_daemon_task, NULL );
1213 Debug( LDAP_DEBUG_ANY,
1214 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
1218 /* wait for the listener thread to complete */
1219 ldap_pvt_thread_join( listener_tid, (void *) NULL );
1222 /* experimental code */
1223 slapd_daemon_task( NULL );
1232 #if defined( HAVE_WINSOCK2 )
1233 WORD wVersionRequested;
1237 wVersionRequested = MAKEWORD( 2, 0 );
1239 err = WSAStartup( wVersionRequested, &wsaData );
1241 /* Tell the user that we couldn't find a usable */
1246 /* Confirm that the WinSock DLL supports 2.0.*/
1247 /* Note that if the DLL supports versions greater */
1248 /* than 2.0 in addition to 2.0, it will still return */
1249 /* 2.0 in wVersion since that is the version we */
1252 if ( LOBYTE( wsaData.wVersion ) != 2 ||
1253 HIBYTE( wsaData.wVersion ) != 0 )
1255 /* Tell the user that we couldn't find a usable */
1261 /* The WinSock DLL is acceptable. Proceed. */
1262 #elif defined( HAVE_WINSOCK )
1264 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
1271 int sockdestroy(void)
1273 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
1280 slap_sig_shutdown( int sig )
1282 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
1285 * If the NT Service Manager is controlling the server, we don't
1286 * want SIGBREAK to kill the server. For some strange reason,
1287 * SIGBREAK is generated when a user logs out.
1290 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
1291 if (is_NT_Service && sig == SIGBREAK)
1292 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: SIGBREAK ignored.\n",
1296 slapd_shutdown = sig;
1300 /* reinstall self */
1301 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
1305 slap_sig_wake( int sig )
1309 /* reinstall self */
1310 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
1314 void slapd_add_internal(ber_socket_t s) {