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 ldap_pvt_thread_t listener_tid;
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 */
103 * Add a descriptor to daemon control
105 static void slapd_add(ber_socket_t s) {
106 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
108 assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
109 assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
110 assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
113 if (s >= slap_daemon.sd_nfds) {
114 slap_daemon.sd_nfds = s + 1;
118 FD_SET( s, &slap_daemon.sd_actives );
119 FD_SET( s, &slap_daemon.sd_readers );
121 Debug( LDAP_DEBUG_CONNS, "daemon: added %ld%s%s\n",
123 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
124 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
126 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
130 * Remove the descriptor from daemon control
132 void slapd_remove(ber_socket_t s, int wake) {
133 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
135 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
137 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
138 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
140 FD_CLR( s, &slap_daemon.sd_actives );
141 FD_CLR( s, &slap_daemon.sd_readers );
142 FD_CLR( s, &slap_daemon.sd_writers );
144 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
148 void slapd_clr_write(ber_socket_t s, int wake) {
149 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
151 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
152 FD_CLR( s, &slap_daemon.sd_writers );
154 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
158 void slapd_set_write(ber_socket_t s, int wake) {
159 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
161 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
162 if (!FD_ISSET(s, &slap_daemon.sd_writers))
163 FD_SET( (unsigned) s, &slap_daemon.sd_writers );
165 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
169 void slapd_clr_read(ber_socket_t s, int wake) {
170 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
172 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
173 FD_CLR( s, &slap_daemon.sd_readers );
175 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
179 void slapd_set_read(ber_socket_t s, int wake) {
180 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
182 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
183 if (!FD_ISSET(s, &slap_daemon.sd_readers))
184 FD_SET( s, &slap_daemon.sd_readers );
186 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
190 static void slapd_close(ber_socket_t s) {
191 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
197 static Listener * open_listener( const char* url )
205 #ifdef HAVE_GETADDRINFO
207 struct addrinfo hints, *res, *sai;
211 rc = ldap_url_parse( url, &lud );
213 if( rc != LDAP_URL_SUCCESS ) {
214 Debug( LDAP_DEBUG_ANY,
215 "daemon: listen URL \"%s\" parse error=%d\n",
221 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
222 Debug( LDAP_DEBUG_ANY,
223 "daemon: TLS not supported (%s)\n",
225 ldap_free_urldesc( lud );
229 if(! lud->lud_port ) {
230 lud->lud_port = LDAP_PORT;
234 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
236 if(! lud->lud_port ) {
237 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
241 #ifdef HAVE_GETADDRINFO
242 memset( &hints, '\0', sizeof(hints) );
243 hints.ai_flags = AI_PASSIVE;
244 hints.ai_family = AF_UNSPEC;
245 hints.ai_socktype = SOCK_STREAM;
247 # ifdef LDAP_PF_LOCAL
248 if ( ldap_pvt_url_scheme2proto(lud->lud_scheme) == LDAP_PROTO_IPC ) {
249 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
250 err = getaddrinfo(NULL, LDAPI_SOCK, &hints, &res);
252 unlink( LDAPI_SOCK );
254 err = getaddrinfo(NULL, lud->lud_host, &hints, &res);
256 unlink( lud->lud_host );
259 # endif /* LDAP_PF_LOCAL */
261 snprintf(serv, sizeof serv, "%d", lud->lud_port);
262 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
263 || strcmp(lud->lud_host, "*") == 0 )
265 err = getaddrinfo(NULL, serv, &hints, &res);
267 err = getaddrinfo(lud->lud_host, serv, &hints, &res);
272 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed\n", 0, 0, 0);
273 ldap_free_urldesc( lud );
277 ldap_free_urldesc( lud );
280 if ( (sai->ai_family != AF_INET)
281 # ifdef LDAP_PF_INET6
282 && (sai->ai_family != AF_INET6)
284 # ifdef LDAP_PF_LOCAL
285 && (sai->ai_family != AF_LOCAL)
289 l.sl_sd = socket( sai->ai_family, sai->ai_socktype, sai->ai_protocol);
290 if ( l.sl_sd == AC_SOCKET_INVALID ) {
291 int err = sock_errno();
292 Debug( LDAP_DEBUG_ANY,
293 "daemon: socket() failed errno=%d (%s)\n", err,
294 sock_errstr(err), 0 );
298 if ( sai->ai_family != AF_LOCAL ) {
301 if ( ldap_pvt_url_scheme2proto(lud->lud_scheme) == LDAP_PROTO_IPC ) {
304 (void) memset( (void *)&l.sl_sa.sa_un_addr, '\0', sizeof(l.sl_sa.sa_un_addr) );
306 l.sl_sa.sa_un_addr.sun_family = AF_LOCAL;
308 /* hack: overload the host to be the path */
309 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
310 strcpy( l.sl_sa.sa_un_addr.sun_path, LDAPI_SOCK );
312 if ( strlen(lud->lud_host) > (sizeof(l.sl_sa.sa_un_addr.sun_path) - 1) ) {
313 Debug( LDAP_DEBUG_ANY,
314 "daemon: domain socket path (%s) too long in URL: %s",
315 lud->lud_host, url, 0);
316 ldap_free_urldesc( lud );
319 strcpy( l.sl_sa.sa_un_addr.sun_path, lud->lud_host );
321 unlink( l.sl_sa.sa_un_addr.sun_path );
323 /* I don't think we need to set this. */
324 l.sl_sa.sa_un_addr.sun_len = sizeof( l.sl_sa.sa_un_addr.sun_len ) +
325 sizeof( l.sl_sa.sa_un_addr.sun_family ) +
326 strlen( l.sl_sa.sa_un_addr.sun_path ) + 1;
329 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
331 ldap_free_urldesc( lud );
333 #endif /* LDAP_PF_LOCAL */
336 port = lud->lud_port;
338 (void) memset( (void*) &l.sl_addr, '\0', sizeof(l.sl_addr) );
340 l.sl_addr.sin_family = AF_INET;
341 l.sl_addr.sin_port = htons( (unsigned short) lud->lud_port );
343 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
344 || strcmp(lud->lud_host, "*") == 0 )
346 l.sl_addr.sin_addr.s_addr = htonl(INADDR_ANY);
349 /* host or address was specified */
350 if( !inet_aton( lud->lud_host, &l.sl_addr.sin_addr ) ) {
351 struct hostent *he = gethostbyname( lud->lud_host );
353 Debug( LDAP_DEBUG_ANY,
354 "daemon: invalid host (%s) in URL: %s",
355 lud->lud_host, url, 0);
356 ldap_free_urldesc( lud );
360 AC_MEMCPY( &l.sl_addr.sin_addr, he->h_addr,
361 sizeof( l.sl_addr.sin_addr ) );
366 ldap_free_urldesc( lud );
368 l.sl_sd = socket( l.sl_sa.sa_addr.sa_family, SOCK_STREAM, 0 );
369 if ( l.sl_sd == AC_SOCKET_INVALID ) {
370 int err = sock_errno();
371 Debug( LDAP_DEBUG_ANY,
372 "daemon: socket() failed errno=%d (%s)\n", err,
373 sock_errstr(err), 0 );
378 if ( l.sl_sd >= dtblsize ) {
379 Debug( LDAP_DEBUG_ANY,
380 "daemon: listener descriptor %ld is too great %ld\n",
381 (long) l.sl_sd, (long) dtblsize, 0 );
382 tcp_close( l.sl_sd );
388 /* for IPv4 and IPv6 sockets only */
389 if ( l.sl_sa.sa_addr.sa_family != AF_LOCAL ) {
390 #endif /* LDAP_PF_LOCAL */
391 #endif /* HAVE_GETADDRINFO */
394 /* enable address reuse */
396 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
397 (char *) &tmp, sizeof(tmp) );
398 if ( rc == AC_SOCKET_ERROR ) {
399 int err = sock_errno();
400 Debug( LDAP_DEBUG_ANY,
401 "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
402 (long) l.sl_sd, err, sock_errstr(err) );
406 #ifdef HAVE_GETADDRINFO
407 } /* sai->ai_family != AF_LOCAL */
408 if (!bind(l.sl_sd, sai->ai_addr, sai->ai_addrlen))
411 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
412 (long) l.sl_sd, err, sock_errstr(err) );
413 tcp_close( l.sl_sd );
414 } while ((sai = sai->ai_next) != NULL);
417 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed\n",
418 (long) l.sl_sd, 0, 0 );
422 switch ( sai->ai_family ) {
423 # ifdef LDAP_PF_LOCAL
425 if ( chmod( (char *)sai->ai_addr, S_IRWXU ) < 0 ) {
426 int err = sock_errno();
427 Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
428 (long) l.sl_sd, err, sock_errstr(err) );
429 tcp_close( l.sl_sd );
432 l.sl_name = ch_malloc( strlen((char *)sai->ai_addr) + sizeof("PATH=") );
433 sprintf( l.sl_name, "PATH=%s", sai->ai_addr );
435 # endif /* LDAP_PF_LOCAL */
438 char addr[INET_ADDRSTRLEN];
440 &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
441 addr, sizeof(addr) );
442 l.sl_name = ch_malloc( strlen(addr) + strlen(serv) + sizeof("IP=:") );
443 sprintf( l.sl_name, "IP=%s:%s", addr, serv );
446 # ifdef LDAP_PF_INET6
448 char addr[INET6_ADDRSTRLEN];
450 &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
452 l.sl_name = ch_malloc( strlen(addr) + strlen(serv) + sizeof("IP= ") );
453 sprintf( l.sl_name, "IP=%s %s", addr, serv );
455 # endif /* LDAP_PF_INET6 */
458 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
459 (int) sai->ai_family, 0, 0 );
464 /* close conditional */
466 #endif /* LDAP_PF_LOCAL */
468 switch ( l.sl_sa.sa_addr.sa_family ) {
471 rc = bind( l.sl_sd, (struct sockaddr *)&l.sl_sa,
472 sizeof(l.sl_sa.sa_un_addr) );
477 rc = bind( l.sl_sd, (struct sockaddr *)&l.sl_sa,
478 sizeof(l.sl_sa.sa_in_addr) );
482 rc = AC_SOCKET_ERROR;
487 if ( rc == AC_SOCKET_ERROR ) {
488 int err = sock_errno();
489 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
490 (long) l.sl_sd, err, sock_errstr(err) );
491 tcp_close( l.sl_sd );
495 switch ( l.sl_sa.sa_addr.sa_family ) {
498 if ( chmod( l.sl_sa.sa_un_addr.sun_path, S_IRWXU ) < 0 ) {
499 int err = sock_errno();
500 Debug( LDAP_DEBUG_ANY,
501 "daemon: chmod(%ld) failed errno=%d (%s)",
502 (long) l.sl_sd, err, sock_errstr(err) );
503 tcp_close( l.sl_sd );
507 l.sl_name = ch_malloc( strlen(l.sl_sa.sa_un_addr.sun_path)
509 sprintf( l.sl_name, "PATH=%s", l.sl_sa.sa_un_addr.sun_path );
511 #endif /* LDAP_PF_LOCAL */
514 l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65336") );
515 s = inet_ntoa( l.sl_addr.sin_addr );
516 sprintf( l.sl_name, "IP=%s:%d",
517 s != NULL ? s : "unknown" , port );
521 l.sl_name = ch_strdup( "UNKNOWN" );
525 #endif /* HAVE_GETADDRINFO */
527 l.sl_url = ch_strdup( url );
528 li = ch_malloc( sizeof( Listener ) );
531 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
537 static int sockinit(void);
538 static int sockdestroy(void);
540 int slapd_daemon_init( const char *urls )
545 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
546 urls ? urls : "<null>", 0, 0 );
548 if( (rc = sockinit()) != 0 ) {
553 dtblsize = sysconf( _SC_OPEN_MAX );
554 #elif HAVE_GETDTABLESIZE
555 dtblsize = getdtablesize();
557 dtblsize = FD_SETSIZE;
561 if(dtblsize > FD_SETSIZE) {
562 dtblsize = FD_SETSIZE;
564 #endif /* !FD_SETSIZE */
566 /* open a pipe (or something equivalent connected to itself).
567 * we write a byte on this fd whenever we catch a signal. The main
568 * loop will be select'ing on this socket, and will wake up when
571 if( (rc = lutil_pair( wake_sds )) < 0 ) {
572 Debug( LDAP_DEBUG_ANY,
573 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
577 FD_ZERO( &slap_daemon.sd_readers );
578 FD_ZERO( &slap_daemon.sd_writers );
584 u = str2charray( urls, " " );
586 if( u == NULL || u[0] == NULL ) {
587 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
593 for( i=0; u[i] != NULL; i++ ) {
594 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
599 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
605 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
608 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
610 for(i = 0; u[i] != NULL; i++ ) {
611 slap_listeners[i] = open_listener( u[i] );
613 if( slap_listeners[i] == NULL ) {
618 slap_listeners[i] = NULL;
620 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
624 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
630 slapd_daemon_destroy(void)
632 connections_destroy();
633 tcp_close( wake_sds[1] );
634 tcp_close( wake_sds[0] );
647 time_t last_idle_check = slap_get_time();
650 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
651 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
654 if ( listen( slap_listeners[l]->sl_sd, 5 ) == -1 ) {
655 int err = sock_errno();
656 Debug( LDAP_DEBUG_ANY,
657 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
658 slap_listeners[l]->sl_url, err,
663 slapd_add( slap_listeners[l]->sl_sd );
666 #ifdef HAVE_NT_SERVICE_MANAGER
667 if ( started_event != NULL ) {
668 ldap_pvt_thread_cond_signal( &started_event );
671 /* initialization complete. Here comes the loop. */
673 while ( !slapd_shutdown ) {
678 #define SLAPD_EBADF_LIMIT 16
681 #define SLAPD_IDLE_CHECK_LIMIT 4
682 time_t now = slap_get_time();
689 #if defined(SLAPD_RLOOKUPS)
695 if( global_idletimeout > 0 && difftime(
696 last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT,
699 connections_timeout_idle(now);
702 FD_ZERO( &writefds );
708 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
710 #ifdef FD_SET_MANUAL_COPY
711 for( s = 0; s < nfds; s++ ) {
712 if(FD_ISSET( &slap_sd_readers, s )) {
713 FD_SET( s, &readfds );
715 if(FD_ISSET( &slap_sd_writers, s )) {
716 FD_SET( s, &writefds );
720 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
721 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
723 assert(!FD_ISSET(wake_sds[0], &readfds));
724 FD_SET( wake_sds[0], &readfds );
726 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
727 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
729 if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
730 FD_SET( slap_listeners[l]->sl_sd, &readfds );
734 nfds = slap_daemon.sd_nfds;
739 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
741 at = ldap_pvt_thread_pool_backload(&connection_pool);
743 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
746 tvp = at ? &zero : NULL;
749 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
750 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
753 Debug( LDAP_DEBUG_CONNS,
754 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
755 slap_listeners[l]->sl_sd, at,
756 tvp == NULL ? "NULL" : "zero" );
759 switch(ns = select( nfds, &readfds,
761 /* don't pass empty fd_set */
762 ( writefds.fd_count > 0 ? &writefds : NULL ),
768 case -1: { /* failure - try again */
769 int err = sock_errno();
773 /* you'd think this would be EBADF */
774 || err == WSAENOTSOCK
777 if (++ebadf < SLAPD_EBADF_LIMIT)
782 Debug( LDAP_DEBUG_CONNS,
783 "daemon: select failed (%d): %s\n",
784 err, sock_errstr(err), 0 );
791 case 0: /* timeout - let threads run */
793 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
795 ldap_pvt_thread_yield();
798 default: /* something happened - deal with it */
800 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
805 if( FD_ISSET( wake_sds[0], &readfds ) ) {
807 tcp_read( wake_sds[0], c, sizeof(c) );
814 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
816 socklen_t len = sizeof(from);
824 char peername[MAXPATHLEN + sizeof("PATH=")];
825 #elif defined(LDAP_PF_INET6)
826 char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
828 char peername[sizeof("IP=255.255.255.255:65336")];
829 #endif /* LDAP_PF_LOCAL */
833 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
836 if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
839 if ( (s = accept( slap_listeners[l]->sl_sd,
840 (struct sockaddr *) &from, &len )) == AC_SOCKET_INVALID )
842 int err = sock_errno();
843 Debug( LDAP_DEBUG_ANY,
844 "daemon: accept(%ld) failed errno=%d (%s)\n",
845 (long) slap_listeners[l]->sl_sd, err,
851 /* make sure descriptor number isn't too great */
852 if ( s >= dtblsize ) {
853 Debug( LDAP_DEBUG_ANY,
854 "daemon: %ld beyond descriptor table size %ld\n",
855 (long) s, (long) dtblsize, 0 );
862 /* enable keep alives */
863 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
864 (char *) &tmp, sizeof(tmp) );
865 if ( rc == AC_SOCKET_ERROR ) {
866 int err = sock_errno();
867 Debug( LDAP_DEBUG_ANY,
868 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
869 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
873 /* enable no delay */
875 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
876 (char *)&tmp, sizeof(tmp) );
877 if ( rc == AC_SOCKET_ERROR ) {
878 int err = sock_errno();
879 Debug( LDAP_DEBUG_ANY,
880 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
881 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
887 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
889 /* newly accepted stream should not be in any of the FD SETS */
890 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
891 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
892 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
894 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
897 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
902 if ( getpeername( s, (struct sockaddr *) &from, &len ) != 0 ) {
903 int err = sock_errno();
904 Debug( LDAP_DEBUG_ANY,
905 "daemon: getpeername( %ld ) failed: errno=%d (%s)\n",
906 (long) s, err, sock_errstr(err) );
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 ) );
949 if ( ( from.sa_addr.sa_family == AF_INET )
951 || ( from.sa_addr.sa_family == AF_INET6 )
954 #ifdef SLAPD_RLOOKUPS
955 # ifdef LDAP_PF_INET6
956 if ( from.sa_addr.sa_family == AF_INET6 )
958 (char *)&(from.sa_in6_addr.sin6_addr),
959 sizeof(from.sa_in6_addr.sin6_addr),
962 # endif LDAP_PF_INET6
964 (char *) &(from.sa_in_addr.sin_addr),
965 sizeof(from.sa_in_addr.sin_addr),
967 dnsname = hp ? ldap_pvt_str2lower( hp->h_name ) : NULL;
970 #endif /* SLAPD_RLOOKUPS */
973 if ( !hosts_ctl("slapd",
974 dnsname != NULL ? dnsname : STRING_UNKNOWN,
975 peeraddr != NULL ? peeraddr : STRING_UNKNOWN,
979 Statslog( LDAP_DEBUG_ANY,
980 "fd=%ld host access from %s (%s) denied.\n",
982 dnsname != NULL ? dnsname : "unknown",
983 peeraddr != NULL ? peeraddr : "unknown",
988 #endif /* HAVE_TCPD */
991 id = connection_init(s,
992 slap_listeners[l]->sl_url,
993 dnsname != NULL ? dnsname : "unknown",
995 slap_listeners[l]->sl_name,
997 slap_listeners[l]->sl_is_tls,
1004 if( authid ) ch_free(authid);
1007 Debug( LDAP_DEBUG_ANY,
1008 "daemon: connection_init(%ld, %s, %s) failed.\n",
1011 slap_listeners[l]->sl_name );
1016 Statslog( LDAP_DEBUG_STATS,
1017 "daemon: conn=%ld fd=%ld connection from %s (%s) accepted.\n",
1020 slap_listeners[l]->sl_name,
1028 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1030 for ( i = 0; i < readfds.fd_count; i++ ) {
1031 Debug( LDAP_DEBUG_CONNS, " %d%s",
1032 readfds.fd_array[i], "r", 0 );
1034 for ( i = 0; i < writefds.fd_count; i++ ) {
1035 Debug( LDAP_DEBUG_CONNS, " %d%s",
1036 writefds.fd_array[i], "w", 0 );
1039 for ( i = 0; i < nfds; i++ ) {
1041 int is_listener = 0;
1043 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1044 if ( i == slap_listeners[l]->sl_sd ) {
1049 if ( is_listener ) {
1052 r = FD_ISSET( i, &readfds );
1053 w = FD_ISSET( i, &writefds );
1055 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1056 r ? "r" : "", w ? "w" : "" );
1060 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1063 /* loop through the writers */
1065 for ( i = 0; i < writefds.fd_count; i++ )
1067 for ( i = 0; i < nfds; i++ )
1071 int is_listener = 0;
1073 wd = writefds.fd_array[i];
1075 if( ! FD_ISSET( i, &writefds ) ) {
1081 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1082 if ( i == slap_listeners[l]->sl_sd ) {
1087 if ( is_listener ) {
1090 Debug( LDAP_DEBUG_CONNS,
1091 "daemon: write active on %d\n",
1095 * NOTE: it is possible that the connection was closed
1096 * and that the stream is now inactive.
1097 * connection_write() must valid the stream is still
1101 if ( connection_write( wd ) < 0 ) {
1102 FD_CLR( (unsigned) wd, &readfds );
1108 for ( i = 0; i < readfds.fd_count; i++ )
1110 for ( i = 0; i < nfds; i++ )
1114 int is_listener = 0;
1117 rd = readfds.fd_array[i];
1119 if( ! FD_ISSET( i, &readfds ) ) {
1125 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1126 if ( rd == slap_listeners[l]->sl_sd ) {
1131 if ( is_listener ) {
1135 Debug ( LDAP_DEBUG_CONNS,
1136 "daemon: read activity on %d\n", rd, 0, 0 );
1139 * NOTE: it is possible that the connection was closed
1140 * and that the stream is now inactive.
1141 * connection_read() must valid the stream is still
1145 if ( connection_read( rd ) < 0 ) {
1149 ldap_pvt_thread_yield();
1152 if( slapd_shutdown > 0 ) {
1153 Debug( LDAP_DEBUG_TRACE,
1154 "daemon: shutdown requested and initiated.\n",
1157 } else if ( slapd_shutdown < 0 ) {
1158 #ifdef HAVE_NT_SERVICE_MANAGER
1159 if (slapd_shutdown == -1)
1160 Debug( LDAP_DEBUG_TRACE,
1161 "daemon: shutdown initiated by Service Manager.\n",
1165 Debug( LDAP_DEBUG_TRACE,
1166 "daemon: abnormal condition, shutdown initiated.\n",
1169 Debug( LDAP_DEBUG_TRACE,
1170 "daemon: no active streams, shutdown initiated.\n",
1174 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1175 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1176 #ifdef LDAP_PF_LOCAL
1177 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1178 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1180 #endif /* LDAP_PF_LOCAL */
1181 slapd_close( slap_listeners[l]->sl_sd );
1186 Debug( LDAP_DEBUG_ANY,
1187 "slapd shutdown: waiting for %d threads to terminate\n",
1188 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1190 ldap_pvt_thread_pool_destroy(&connection_pool, 1);
1196 int slapd_daemon( void )
1202 #define SLAPD_LISTENER_THREAD 1
1203 #if defined( SLAPD_LISTENER_THREAD ) || !defined(HAVE_PTHREADS)
1205 /* listener as a separate THREAD */
1206 rc = ldap_pvt_thread_create( &listener_tid,
1207 0, slapd_daemon_task, NULL );
1210 Debug( LDAP_DEBUG_ANY,
1211 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
1215 /* wait for the listener thread to complete */
1216 ldap_pvt_thread_join( listener_tid, (void *) NULL );
1218 /* expermimental code */
1219 listener_tid = pthread_self();
1220 slapd_daemon_task( NULL );
1229 #if defined( HAVE_WINSOCK2 )
1230 WORD wVersionRequested;
1234 wVersionRequested = MAKEWORD( 2, 0 );
1236 err = WSAStartup( wVersionRequested, &wsaData );
1238 /* Tell the user that we couldn't find a usable */
1243 /* Confirm that the WinSock DLL supports 2.0.*/
1244 /* Note that if the DLL supports versions greater */
1245 /* than 2.0 in addition to 2.0, it will still return */
1246 /* 2.0 in wVersion since that is the version we */
1249 if ( LOBYTE( wsaData.wVersion ) != 2 ||
1250 HIBYTE( wsaData.wVersion ) != 0 )
1252 /* Tell the user that we couldn't find a usable */
1258 /* The WinSock DLL is acceptable. Proceed. */
1259 #elif defined( HAVE_WINSOCK )
1261 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
1268 int sockdestroy(void)
1270 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
1277 slap_sig_shutdown( int sig )
1279 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
1282 * If the NT Service Manager is controlling the server, we don't
1283 * want SIGBREAK to kill the server. For some strange reason,
1284 * SIGBREAK is generated when a user logs out.
1287 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
1288 if (is_NT_Service && sig == SIGBREAK)
1289 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: SIGBREAK ignored.\n",
1293 slapd_shutdown = sig;
1297 /* reinstall self */
1298 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
1302 slap_sig_wake( int sig )
1306 /* reinstall self */
1307 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
1311 void slapd_add_internal(ber_socket_t s) {