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_UNIX */
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;
248 if (lud->lud_protocol == LDAP_PROTO_LOCAL) {
249 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
250 err = getaddrinfo(NULL, "/tmp/.ldap-sock", &hints, &res);
252 unlink( "/tmp/.ldap-sock" );
254 err = getaddrinfo(NULL, lud->lud_host, &hints, &res);
256 unlink( lud->lud_host );
259 # endif /* LDAP_PF_UNIX */
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 l.sl_sd = socket( sai->ai_family, sai->ai_socktype, sai->ai_protocol);
281 if ( l.sl_sd == AC_SOCKET_INVALID ) {
282 Debug( LDAP_DEBUG_ANY,
283 "daemon: socket() failed errno=%d (%s)\n", err,
284 sock_errstr(err), 0 );
288 if ( sai->ai_family != AF_UNIX ) {
291 if ( ldap_pvt_url_scheme2proto(url) == LDAP_PROTO_IPC ) {
294 (void) memset( (void *)&l.sl_sa.sa_un_addr, '\0', sizeof(l.sl_sa.sa_un_addr) );
296 l.sl_sa.sa_un_addr.sun_family = AF_UNIX;
298 /* hack: overload the host to be the path */
299 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
300 strcpy( l.sl_sa.sa_un_addr.sun_path, "/tmp/.ldap-sock" );
302 if ( strlen(lud->lud_host) > (sizeof(l.sl_sa.sa_un_addr.sun_path) - 1) ) {
303 Debug( LDAP_DEBUG_ANY,
304 "daemon: domain socket path (%s) too long in URL: %s",
305 lud->lud_host, url, 0);
306 ldap_free_urldesc( lud );
309 strcpy( l.sl_sa.sa_un_addr.sun_path, lud->lud_host );
311 unlink( l.sl_sa.sa_un_addr.sun_path );
313 /* I don't think we need to set this. */
314 l.sl_sa.sa_un_addr.sun_len = sizeof( l.sl_sa.sa_un_addr.sun_len ) +
315 sizeof( l.sl_sa.sa_un_addr.sun_family ) +
316 strlen( l.sl_sa.sa_un_addr.sun_path ) + 1;
319 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
321 ldap_free_urldesc( lud );
323 #endif /* LDAP_PF_UNIX */
326 port = lud->lud_port;
328 (void) memset( (void*) &l.sl_addr, '\0', sizeof(l.sl_addr) );
330 l.sl_addr.sin_family = AF_INET;
331 l.sl_addr.sin_port = htons( (unsigned short) lud->lud_port );
333 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
334 || strcmp(lud->lud_host, "*") == 0 )
336 l.sl_addr.sin_addr.s_addr = htonl(INADDR_ANY);
339 /* host or address was specified */
340 if( !inet_aton( lud->lud_host, &l.sl_addr.sin_addr ) ) {
341 struct hostent *he = gethostbyname( lud->lud_host );
343 Debug( LDAP_DEBUG_ANY,
344 "daemon: invalid host (%s) in URL: %s",
345 lud->lud_host, url, 0);
346 ldap_free_urldesc( lud );
350 memcpy( &l.sl_addr.sin_addr, he->h_addr,
351 sizeof( l.sl_addr.sin_addr ) );
356 ldap_free_urldesc( lud );
358 l.sl_sd = socket( l.sl_sa.sa_addr.sa_family, SOCK_STREAM, 0 );
359 if ( l.sl_sd == AC_SOCKET_INVALID ) {
360 int err = sock_errno();
361 Debug( LDAP_DEBUG_ANY,
362 "daemon: socket() failed errno=%d (%s)\n", err,
363 sock_errstr(err), 0 );
368 if ( l.sl_sd >= dtblsize ) {
369 Debug( LDAP_DEBUG_ANY,
370 "daemon: listener descriptor %ld is too great %ld\n",
371 (long) l.sl_sd, (long) dtblsize, 0 );
372 tcp_close( l.sl_sd );
378 /* for IP sockets only */
379 if ( l.sl_sa.sa_addr.sa_family == AF_INET ) {
380 #endif /* LDAP_PF_UNIX */
381 #endif /* HAVE_GETADDRINFO */
384 /* enable address reuse */
386 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
387 (char *) &tmp, sizeof(tmp) );
388 if ( rc == AC_SOCKET_ERROR ) {
389 int err = sock_errno();
390 Debug( LDAP_DEBUG_ANY,
391 "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
392 (long) l.sl_sd, err, sock_errstr(err) );
396 /* enable keep alives */
398 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_KEEPALIVE,
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_KEEPALIVE) failed errno=%d (%s)\n",
404 (long) l.sl_sd, err, sock_errstr(err) );
408 /* enable no delay */
410 rc = setsockopt( l.sl_sd, IPPROTO_TCP, TCP_NODELAY,
411 (char *)&tmp, sizeof(tmp) );
412 if ( rc == AC_SOCKET_ERROR ) {
413 int err = sock_errno();
414 Debug( LDAP_DEBUG_ANY,
415 "slapd(%ld): setsockopt(TCP_NODELAY) failed errno=%d (%s)\n",
416 (long) l.sl_sd, err, sock_errstr(err) );
420 #ifdef HAVE_GETADDRINFO
421 } /* sai->ai_family != AF_UNIX */
422 if (!bind(l.sl_sd, sai->ai_addr, sai->ai_addrlen))
425 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
426 (long) l.sl_sd, err, sock_errstr(err) );
427 tcp_close( l.sl_sd );
428 } while ((sai = sai->ai_next) != NULL);
431 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed\n",
432 (long) l.sl_sd, 0, 0 );
436 switch ( sai->ai_family ) {
439 if ( chmod( (char *)sai->ai_addr, S_IRWXU ) < 0 ) {
441 Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
442 (long) l.sl_sd, err, sock_errstr(err) );
443 tcp_close( l.sl_sd );
446 l.sl_name = ch_malloc( strlen((char *)sai->ai_addr) + sizeof("PATH=") );
447 sprintf( l.sl_name, "PATH=%s", sai->ai_addr );
449 # endif /* LDAP_PF_UNIX */
452 char addr[INET_ADDRSTRLEN];
454 &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
455 addr, sizeof(addr) );
456 l.sl_name = ch_malloc( strlen(addr) + strlen(serv) + sizeof("IP=:") );
457 sprintf( l.sl_name, "IP=%s:%s", addr, serv );
460 # ifdef LDAP_PF_INET6
462 char addr[INET6_ADDRSTRLEN];
464 &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
466 l.sl_name = ch_malloc( strlen(addr) + strlen(serv) + sizeof("IP= ") );
467 sprintf( l.sl_name, "IP=%s %s", addr, serv );
469 # endif /* LDAP_PF_INET6 */
472 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
473 (int) sai->ai_family, 0, 0 );
478 /* close conditional */
480 #endif /* LDAP_PF_UNIX */
482 switch ( l.sl_sa.sa_addr.sa_family ) {
485 rc = bind( l.sl_sd, (struct sockaddr *)&l.sl_sa,
486 sizeof(l.sl_sa.sa_un_addr) );
491 rc = bind( l.sl_sd, (struct sockaddr *)&l.sl_sa,
492 sizeof(l.sl_sa.sa_in_addr) );
496 rc = AC_SOCKET_ERROR;
501 if ( rc == AC_SOCKET_ERROR ) {
502 int err = sock_errno();
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 );
509 switch ( l.sl_sa.sa_addr.sa_family ) {
512 if ( chmod( l.sl_sa.sa_un_addr.sun_path, S_IRWXU ) < 0 ) {
513 int err = sock_errno();
514 Debug( LDAP_DEBUG_ANY,
515 "daemon: chmod(%ld) failed errno=%d (%s)",
516 (long) l.sl_sd, err, sock_errstr(err) );
517 tcp_close( l.sl_sd );
521 l.sl_name = ch_malloc( strlen(l.sl_sa.sa_un_addr.sun_path)
523 sprintf( l.sl_name, "PATH=%s", l.sl_sa.sa_un_addr.sun_path );
525 #endif /* LDAP_PF_UNIX */
528 l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65336") );
529 s = inet_ntoa( l.sl_addr.sin_addr );
530 sprintf( l.sl_name, "IP=%s:%d",
531 s != NULL ? s : "unknown" , port );
535 l.sl_name = ch_strdup( "UNKNOWN" );
539 #endif /* HAVE_GETADDRINFO */
541 l.sl_url = ch_strdup( url );
542 li = ch_malloc( sizeof( Listener ) );
545 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
551 static int sockinit(void);
552 static int sockdestroy(void);
554 int slapd_daemon_init( const char *urls )
559 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
560 urls ? urls : "<null>", 0, 0 );
562 if( (rc = sockinit()) != 0 ) {
567 dtblsize = sysconf( _SC_OPEN_MAX );
568 #elif HAVE_GETDTABLESIZE
569 dtblsize = getdtablesize();
571 dtblsize = FD_SETSIZE;
575 if(dtblsize > FD_SETSIZE) {
576 dtblsize = FD_SETSIZE;
578 #endif /* !FD_SETSIZE */
580 /* open a pipe (or something equivalent connected to itself).
581 * we write a byte on this fd whenever we catch a signal. The main
582 * loop will be select'ing on this socket, and will wake up when
585 if( (rc = lutil_pair( wake_sds )) < 0 ) {
586 Debug( LDAP_DEBUG_ANY,
587 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
591 FD_ZERO( &slap_daemon.sd_readers );
592 FD_ZERO( &slap_daemon.sd_writers );
598 u = str2charray( urls, " " );
600 if( u == NULL || u[0] == NULL ) {
601 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
607 for( i=0; u[i] != NULL; i++ ) {
608 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
613 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
619 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
622 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
624 for(i = 0; u[i] != NULL; i++ ) {
625 slap_listeners[i] = open_listener( u[i] );
627 if( slap_listeners[i] == NULL ) {
632 slap_listeners[i] = NULL;
634 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
638 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
644 slapd_daemon_destroy(void)
646 connections_destroy();
647 tcp_close( wake_sds[1] );
648 tcp_close( wake_sds[0] );
660 time_t last_idle_check = slap_get_time();
663 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
664 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
667 if ( listen( slap_listeners[l]->sl_sd, 5 ) == -1 ) {
668 int err = sock_errno();
669 Debug( LDAP_DEBUG_ANY,
670 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
671 slap_listeners[l]->sl_url, err,
676 slapd_add( slap_listeners[l]->sl_sd );
679 #ifdef HAVE_NT_SERVICE_MANAGER
680 if ( started_event != NULL ) {
681 ldap_pvt_thread_cond_signal( &started_event );
684 /* initialization complete. Here comes the loop. */
686 while ( !slapd_shutdown ) {
691 #define SLAPD_EBADF_LIMIT 10
694 #define SLAPD_IDLE_CHECK_LIMIT 4
695 time_t now = slap_get_time();
702 #if defined(SLAPD_RLOOKUPS)
708 if( global_idletimeout > 0 && difftime(
709 last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT,
712 connections_timeout_idle(now);
715 FD_ZERO( &writefds );
721 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
723 #ifdef FD_SET_MANUAL_COPY
724 for( s = 0; s < nfds; s++ ) {
725 if(FD_ISSET( &slap_sd_readers, s )) {
726 FD_SET( s, &readfds );
728 if(FD_ISSET( &slap_sd_writers, s )) {
729 FD_SET( s, &writefds );
733 memcpy( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
734 memcpy( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
736 assert(!FD_ISSET(wake_sds[0], &readfds));
737 FD_SET( wake_sds[0], &readfds );
739 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
740 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
742 if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
743 FD_SET( slap_listeners[l]->sl_sd, &readfds );
747 nfds = slap_daemon.sd_nfds;
752 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
754 at = ldap_pvt_thread_pool_backload(connection_pool);
756 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
759 tvp = at ? &zero : NULL;
762 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
763 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
766 Debug( LDAP_DEBUG_CONNS,
767 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
768 slap_listeners[l]->sl_sd, at,
769 tvp == NULL ? "NULL" : "zero" );
772 switch(ns = select( nfds, &readfds,
774 /* don't pass empty fd_set */
775 ( writefds.fd_count > 0 ? &writefds : NULL ),
781 case -1: { /* failure - try again */
782 int err = sock_errno();
786 || err == WSAENOTSOCK /* you'd think this would be EBADF */
789 if (++ebadf < SLAPD_EBADF_LIMIT)
794 Debug( LDAP_DEBUG_CONNS,
795 "daemon: select failed (%d): %s\n",
796 err, sock_errstr(err), 0 );
803 case 0: /* timeout - let threads run */
805 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
807 ldap_pvt_thread_yield();
810 default: /* something happened - deal with it */
812 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
817 if( FD_ISSET( wake_sds[0], &readfds ) ) {
819 tcp_read( wake_sds[0], c, sizeof(c) );
826 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
828 socklen_t len = sizeof(from);
834 char peername[MAXPATHLEN + sizeof("PATH=")];
835 #elif defined(LDAP_PF_INET6)
836 char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
838 char peername[sizeof("IP=255.255.255.255:65336")];
839 #endif /* LDAP_PF_UNIX */
843 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
846 if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
849 if ( (s = accept( slap_listeners[l]->sl_sd,
850 (struct sockaddr *) &from, &len )) == AC_SOCKET_INVALID )
852 int err = sock_errno();
853 Debug( LDAP_DEBUG_ANY,
854 "daemon: accept(%ld) failed errno=%d (%s)\n",
855 (long) slap_listeners[l]->sl_sd, err,
861 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
863 /* newly accepted stream should not be in any of the FD SETS */
865 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
866 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
867 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
869 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
873 /* make sure descriptor number isn't too great */
874 if ( s >= dtblsize ) {
875 Debug( LDAP_DEBUG_ANY,
876 "daemon: %ld beyond descriptor table size %ld\n",
877 (long) s, (long) dtblsize, 0 );
883 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
888 if ( getpeername( s, (struct sockaddr *) &from, &len ) != 0 ) {
889 int err = sock_errno();
890 Debug( LDAP_DEBUG_ANY,
891 "daemon: getpeername( %ld ) failed: errno=%d (%s)\n",
892 (long) s, err, sock_errstr(err) );
897 switch ( from.sa_addr.sa_family ) {
900 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
902 #endif /* LDAP_PF_UNIX */
904 # ifdef LDAP_PF_INET6
906 char addr[INET6_ADDRSTRLEN];
907 sprintf( peername, "IP=%s %d",
909 &from.sa_in6_addr.sin6_addr,
910 addr, sizeof addr) ? addr : "unknown",
911 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
913 # endif /* LDAP_PF_INET6 */
916 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
917 sprintf( peername, "IP=%s:%d",
918 peeraddr != NULL ? peeraddr : "unknown",
919 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
926 if ( ( from.sa_addr.sa_family == AF_INET )
928 || ( from.sa_addr.sa_family == AF_INET6 )
931 #ifdef SLAPD_RLOOKUPS
932 # ifdef LDAP_PF_INET6
933 if ( from.sa_addr.sa_family == AF_INET6 )
935 (char *)&(from.sa_in6_addr.sin6_addr),
936 sizeof(from.sa_in6_addr.sin6_addr),
939 # endif LDAP_PF_INET6
941 (char *) &(from.sa_in_addr.sin_addr),
942 sizeof(from.sa_in_addr.sin_addr),
944 dnsname = hp ? ldap_pvt_str2lower( hp->h_name ) : NULL;
947 #endif /* SLAPD_RLOOKUPS */
950 if ( !hosts_ctl("slapd",
951 dnsname != NULL ? dnsname : STRING_UNKNOWN,
952 peeraddr != NULL ? peeraddr : STRING_UNKNOWN,
956 Statslog( LDAP_DEBUG_ANY,
957 "fd=%ld connection from %s (%s) denied.\n",
959 dnsname != NULL ? dnsname : "unknown",
960 peeraddr != NULL ? peeraddr : "unknown",
965 #endif /* HAVE_TCPD */
968 if( (id = connection_init(s,
969 slap_listeners[l]->sl_url,
970 dnsname != NULL ? dnsname : "unknown",
972 slap_listeners[l]->sl_name,
974 slap_listeners[l]->sl_is_tls
980 Debug( LDAP_DEBUG_ANY,
981 "daemon: connection_init(%ld, %s, %s) failed.\n",
984 slap_listeners[l]->sl_name );
989 Statslog( LDAP_DEBUG_STATS,
990 "daemon: conn=%ld fd=%ld connection from %s (%s) accepted.\n",
993 slap_listeners[l]->sl_name,
1001 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1003 for ( i = 0; i < readfds.fd_count; i++ ) {
1004 Debug( LDAP_DEBUG_CONNS, " %d%s",
1005 readfds.fd_array[i], "r", 0 );
1007 for ( i = 0; i < writefds.fd_count; i++ ) {
1008 Debug( LDAP_DEBUG_CONNS, " %d%s",
1009 writefds.fd_array[i], "w", 0 );
1012 for ( i = 0; i < nfds; i++ ) {
1014 int is_listener = 0;
1016 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1017 if ( i == slap_listeners[l]->sl_sd ) {
1022 if ( is_listener ) {
1025 r = FD_ISSET( i, &readfds );
1026 w = FD_ISSET( i, &writefds );
1028 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1029 r ? "r" : "", w ? "w" : "" );
1033 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1036 /* loop through the writers */
1038 for ( i = 0; i < writefds.fd_count; i++ )
1040 for ( i = 0; i < nfds; i++ )
1044 int is_listener = 0;
1046 wd = writefds.fd_array[i];
1048 if( ! FD_ISSET( i, &writefds ) ) {
1054 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1055 if ( i == slap_listeners[l]->sl_sd ) {
1060 if ( is_listener ) {
1063 Debug( LDAP_DEBUG_CONNS,
1064 "daemon: write active on %d\n",
1068 * NOTE: it is possible that the connection was closed
1069 * and that the stream is now inactive.
1070 * connection_write() must valid the stream is still
1074 if ( connection_write( wd ) < 0 ) {
1075 FD_CLR( (unsigned) wd, &readfds );
1081 for ( i = 0; i < readfds.fd_count; i++ )
1083 for ( i = 0; i < nfds; i++ )
1087 int is_listener = 0;
1090 rd = readfds.fd_array[i];
1092 if( ! FD_ISSET( i, &readfds ) ) {
1098 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1099 if ( rd == slap_listeners[l]->sl_sd ) {
1104 if ( is_listener ) {
1108 Debug ( LDAP_DEBUG_CONNS,
1109 "daemon: read activity on %d\n", rd, 0, 0 );
1112 * NOTE: it is possible that the connection was closed
1113 * and that the stream is now inactive.
1114 * connection_read() must valid the stream is still
1118 if ( connection_read( rd ) < 0 ) {
1122 ldap_pvt_thread_yield();
1125 if( slapd_shutdown > 0 ) {
1126 Debug( LDAP_DEBUG_TRACE,
1127 "daemon: shutdown requested and initiated.\n",
1130 } else if ( slapd_shutdown < 0 ) {
1131 #ifdef HAVE_NT_SERVICE_MANAGER
1132 if (slapd_shutdown == -1)
1133 Debug( LDAP_DEBUG_TRACE,
1134 "daemon: shutdown initiated by Service Manager.\n",
1138 Debug( LDAP_DEBUG_TRACE,
1139 "daemon: abnormal condition, shutdown initiated.\n",
1142 Debug( LDAP_DEBUG_TRACE,
1143 "daemon: no active streams, shutdown initiated.\n",
1147 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1148 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1150 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_UNIX ) {
1151 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1153 #endif /* LDAP_PF_UNIX */
1154 slapd_close( slap_listeners[l]->sl_sd );
1159 Debug( LDAP_DEBUG_ANY,
1160 "slapd shutdown: waiting for %d threads to terminate\n",
1161 ldap_pvt_thread_pool_backload(connection_pool), 0, 0 );
1163 ldap_pvt_thread_pool_destroy(connection_pool, 1);
1169 int slapd_daemon( void )
1175 #define SLAPD_LISTENER_THREAD 1
1176 #if defined( SLAPD_LISTENER_THREAD ) || !defined(HAVE_PTHREADS)
1178 /* listener as a separate THREAD */
1179 rc = ldap_pvt_thread_create( &listener_tid,
1180 0, slapd_daemon_task, NULL );
1183 Debug( LDAP_DEBUG_ANY,
1184 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
1188 /* wait for the listener thread to complete */
1189 ldap_pvt_thread_join( listener_tid, (void *) NULL );
1191 /* expermimental code */
1192 listener_tid = pthread_self();
1193 slapd_daemon_task( NULL );
1200 #ifdef HAVE_WINSOCK2
1203 WORD wVersionRequested;
1207 wVersionRequested = MAKEWORD( 2, 0 );
1209 err = WSAStartup( wVersionRequested, &wsaData );
1211 /* Tell the user that we couldn't find a usable */
1216 /* Confirm that the WinSock DLL supports 2.0.*/
1217 /* Note that if the DLL supports versions greater */
1218 /* than 2.0 in addition to 2.0, it will still return */
1219 /* 2.0 in wVersion since that is the version we */
1222 if ( LOBYTE( wsaData.wVersion ) != 2 ||
1223 HIBYTE( wsaData.wVersion ) != 0 )
1225 /* Tell the user that we couldn't find a usable */
1231 /* The WinSock DLL is acceptable. Proceed. */
1235 int sockdestroy(void)
1242 static int sockinit(void)
1245 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
1250 static int sockdestroy(void)
1257 static int sockinit(void)
1261 static int sockdestroy(void)
1268 slap_sig_shutdown( int sig )
1270 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
1273 * If the NT Service Manager is controlling the server, we don't
1274 * want SIGBREAK to kill the server. For some strange reason,
1275 * SIGBREAK is generated when a user logs out.
1278 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
1279 if (is_NT_Service && sig == SIGBREAK)
1280 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: SIGBREAK ignored.\n",
1284 slapd_shutdown = sig;
1288 /* reinstall self */
1289 (void) SIGNAL( sig, slap_sig_shutdown );
1293 slap_sig_wake( int sig )
1297 /* reinstall self */
1298 (void) SIGNAL( sig, slap_sig_wake );