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 )
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_is_ldaps_url( url ) ) {
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_is_ldaps_url( url );
236 if(! lud->lud_port ) {
237 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
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 (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_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 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 (lud->lud_protocol == LDAP_PROTO_LOCAL) {
293 (void) memset( (void *)&l.sl_sa.sa_un_addr, '\0', sizeof(l.sl_sa.sa_un_addr) );
295 l.sl_sa.sa_un_addr.sun_family = AF_UNIX;
297 /* hack: overload the host to be the path */
298 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
299 strcpy( l.sl_sa.sa_un_addr.sun_path, "/tmp/.ldap-sock" );
301 if ( strlen(lud->lud_host) > (sizeof(l.sl_sa.sa_un_addr.sun_path) - 1) ) {
302 Debug( LDAP_DEBUG_ANY,
303 "daemon: domain socket path (%s) too long in URL: %s",
304 lud->lud_host, url, 0);
305 ldap_free_urldesc( lud );
308 strcpy( l.sl_sa.sa_un_addr.sun_path, lud->lud_host );
310 unlink( l.sl_sa.sa_un_addr.sun_path );
312 /* I don't think we need to set this. */
313 l.sl_sa.sa_un_addr.sun_len = sizeof( l.sl_sa.sa_un_addr.sun_len ) +
314 sizeof( l.sl_sa.sa_un_addr.sun_family ) +
315 strlen( l.sl_sa.sa_un_addr.sun_path ) + 1;
318 #endif /* LDAP_PF_LOCAL */
321 port = lud->lud_port;
323 (void) memset( (void*) &l.sl_addr, '\0', sizeof(l.sl_addr) );
325 l.sl_addr.sin_family = AF_INET;
326 l.sl_addr.sin_port = htons( (unsigned short) lud->lud_port );
328 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
329 || strcmp(lud->lud_host, "*") == 0 )
331 l.sl_addr.sin_addr.s_addr = htonl(INADDR_ANY);
334 /* host or address was specified */
335 if( !inet_aton( lud->lud_host, &l.sl_addr.sin_addr ) ) {
336 struct hostent *he = gethostbyname( lud->lud_host );
338 Debug( LDAP_DEBUG_ANY,
339 "daemon: invalid host (%s) in URL: %s",
340 lud->lud_host, url, 0);
341 ldap_free_urldesc( lud );
345 memcpy( &l.sl_addr.sin_addr, he->h_addr,
346 sizeof( l.sl_addr.sin_addr ) );
351 ldap_free_urldesc( lud );
353 l.sl_sd = socket( l.sl_sa.sa_addr.sa_family, SOCK_STREAM, 0 );
354 if ( l.sl_sd == AC_SOCKET_INVALID ) {
355 int err = sock_errno();
356 Debug( LDAP_DEBUG_ANY,
357 "daemon: socket() failed errno=%d (%s)\n", err,
358 sock_errstr(err), 0 );
363 if ( l.sl_sd >= dtblsize ) {
364 Debug( LDAP_DEBUG_ANY,
365 "daemon: listener descriptor %ld is too great %ld\n",
366 (long) l.sl_sd, (long) dtblsize, 0 );
367 tcp_close( l.sl_sd );
373 /* for IP sockets only */
374 if ( l.sl_sa.sa_addr.sa_family == AF_INET ) {
375 #endif /* LDAP_PF_LOCAL */
376 #endif /* LDAP_INET6 */
379 /* enable address reuse */
381 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
382 (char *) &tmp, sizeof(tmp) );
383 if ( rc == AC_SOCKET_ERROR ) {
384 int err = sock_errno();
385 Debug( LDAP_DEBUG_ANY,
386 "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
387 (long) l.sl_sd, err, sock_errstr(err) );
391 /* enable keep alives */
393 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_KEEPALIVE,
394 (char *) &tmp, sizeof(tmp) );
395 if ( rc == AC_SOCKET_ERROR ) {
396 int err = sock_errno();
397 Debug( LDAP_DEBUG_ANY,
398 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed errno=%d (%s)\n",
399 (long) l.sl_sd, err, sock_errstr(err) );
403 /* enable no delay */
405 rc = setsockopt( l.sl_sd, IPPROTO_TCP, TCP_NODELAY,
406 (char *)&tmp, sizeof(tmp) );
407 if ( rc == AC_SOCKET_ERROR ) {
408 int err = sock_errno();
409 Debug( LDAP_DEBUG_ANY,
410 "slapd(%ld): setsockopt(TCP_NODELAY) failed errno=%d (%s)\n",
411 (long) l.sl_sd, err, sock_errstr(err) );
416 } /* sai->ai_family != AF_UNIX */
417 if (!bind(l.sl_sd, sai->ai_addr, sai->ai_addrlen))
420 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
421 (long) l.sl_sd, err, sock_errstr(err) );
422 tcp_close( l.sl_sd );
423 } while ((sai = sai->ai_next) != NULL);
426 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed\n",
427 (long) l.sl_sd, 0, 0 );
431 switch ( sai->ai_family ) {
434 if ( chmod( (char *)sai->ai_addr, S_IRWXU ) < 0 ) {
436 Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
437 (long) l.sl_sd, err, sock_errstr(err) );
438 tcp_close( l.sl_sd );
441 l.sl_name = ch_malloc( strlen((char *)sai->ai_addr) + sizeof("PATH=") );
442 sprintf( l.sl_name, "PATH=%s", sai->ai_addr );
444 # endif /* LDAP_PF_LOCAL */
447 char addr[INET_ADDRSTRLEN];
449 &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
450 addr, sizeof(addr) );
451 l.sl_name = ch_malloc( strlen(addr) + strlen(serv) + sizeof("IP=:") );
452 sprintf( l.sl_name, "IP=%s:%s", addr, serv );
457 char addr[INET6_ADDRSTRLEN];
459 &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
461 l.sl_name = ch_malloc( strlen(addr) + strlen(serv) + sizeof("IP= ") );
462 sprintf( l.sl_name, "IP=%s %s", addr, serv );
467 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
468 (int) sai->ai_family, 0, 0 );
473 /* close conditional */
475 #endif /* LDAP_PF_LOCAL */
477 switch ( l.sl_sa.sa_addr.sa_family ) {
480 rc = bind( l.sl_sd, (struct sockaddr *)&l.sl_sa,
481 sizeof(l.sl_sa.sa_un_addr) );
486 rc = bind( l.sl_sd, (struct sockaddr *)&l.sl_sa,
487 sizeof(l.sl_sa.sa_in_addr) );
491 rc = AC_SOCKET_ERROR;
496 if ( rc == AC_SOCKET_ERROR ) {
497 int err = sock_errno();
498 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
499 (long) l.sl_sd, err, sock_errstr(err) );
500 tcp_close( l.sl_sd );
504 switch ( l.sl_sa.sa_addr.sa_family ) {
507 if ( chmod( l.sl_sa.sa_un_addr.sun_path, S_IRWXU ) < 0 ) {
508 int err = sock_errno();
509 Debug( LDAP_DEBUG_ANY,
510 "daemon: chmod(%ld) failed errno=%d (%s)",
511 (long) l.sl_sd, err, sock_errstr(err) );
512 tcp_close( l.sl_sd );
516 l.sl_name = ch_malloc( strlen(l.sl_sa.sa_un_addr.sun_path)
518 sprintf( l.sl_name, "PATH=%s", l.sl_sa.sa_un_addr.sun_path );
520 # endif /* LDAP_PF_LOCAL */
523 l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65336") );
524 s = inet_ntoa( l.sl_addr.sin_addr );
525 sprintf( l.sl_name, "IP=%s:%d",
526 s != NULL ? s : "unknown" , port );
530 l.sl_name = ch_strdup( "UNKNOWN" );
534 #endif /* LDAP_INET6 */
536 l.sl_url = ch_strdup( url );
537 li = ch_malloc( sizeof( Listener ) );
540 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
546 static int sockinit(void);
547 static int sockdestroy(void);
549 int slapd_daemon_init( const char *urls )
554 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
555 urls ? urls : "<null>", 0, 0 );
557 if( (rc = sockinit()) != 0 ) {
562 dtblsize = sysconf( _SC_OPEN_MAX );
563 #elif HAVE_GETDTABLESIZE
564 dtblsize = getdtablesize();
566 dtblsize = FD_SETSIZE;
570 if(dtblsize > FD_SETSIZE) {
571 dtblsize = FD_SETSIZE;
573 #endif /* !FD_SETSIZE */
575 /* open a pipe (or something equivalent connected to itself).
576 * we write a byte on this fd whenever we catch a signal. The main
577 * loop will be select'ing on this socket, and will wake up when
580 if( (rc = lutil_pair( wake_sds )) < 0 ) {
581 Debug( LDAP_DEBUG_ANY,
582 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
586 FD_ZERO( &slap_daemon.sd_readers );
587 FD_ZERO( &slap_daemon.sd_writers );
593 u = str2charray( urls, " " );
595 if( u == NULL || u[0] == NULL ) {
596 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
602 for( i=0; u[i] != NULL; i++ ) {
603 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
608 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
614 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
617 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
619 for(i = 0; u[i] != NULL; i++ ) {
620 slap_listeners[i] = open_listener( u[i] );
622 if( slap_listeners[i] == NULL ) {
627 slap_listeners[i] = NULL;
629 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
633 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
639 slapd_daemon_destroy(void)
641 connections_destroy();
642 tcp_close( wake_sds[1] );
643 tcp_close( wake_sds[0] );
655 time_t last_idle_check = slap_get_time();
658 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
659 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
662 if ( listen( slap_listeners[l]->sl_sd, 5 ) == -1 ) {
663 int err = sock_errno();
664 Debug( LDAP_DEBUG_ANY,
665 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
666 slap_listeners[l]->sl_url, err,
671 slapd_add( slap_listeners[l]->sl_sd );
674 #ifdef HAVE_NT_SERVICE_MANAGER
675 if ( started_event != NULL ) {
676 ldap_pvt_thread_cond_signal( &started_event );
679 /* initialization complete. Here comes the loop. */
681 while ( !slapd_shutdown ) {
686 #define SLAPD_EBADF_LIMIT 10
689 #define SLAPD_IDLE_CHECK_LIMIT 4
690 time_t now = slap_get_time();
695 #if defined( LDAP_PF_LOCAL ) || defined( LDAP_INET6 )
697 /* minimize impact, undefine later. */
698 #define sin_addr sa_in_addr.sin_addr
699 #define sin_port sa_in_addr.sin_port
701 struct sockaddr_in from;
702 #endif /* LDAP_PF_LOCAL || LDAP_INET6 */
703 #if defined(SLAPD_RLOOKUPS)
709 if( global_idletimeout > 0 && difftime(
710 last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT,
713 connections_timeout_idle(now);
716 FD_ZERO( &writefds );
722 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
724 #ifdef FD_SET_MANUAL_COPY
725 for( s = 0; s < nfds; s++ ) {
726 if(FD_ISSET( &slap_sd_readers, s )) {
727 FD_SET( s, &readfds );
729 if(FD_ISSET( &slap_sd_writers, s )) {
730 FD_SET( s, &writefds );
734 memcpy( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
735 memcpy( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
737 assert(!FD_ISSET(wake_sds[0], &readfds));
738 FD_SET( wake_sds[0], &readfds );
740 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
741 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
743 if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
744 FD_SET( slap_listeners[l]->sl_sd, &readfds );
748 nfds = slap_daemon.sd_nfds;
753 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
755 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
757 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
759 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
762 tvp = at ? &zero : NULL;
765 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
766 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
769 Debug( LDAP_DEBUG_CONNS,
770 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
771 slap_listeners[l]->sl_sd, at,
772 tvp == NULL ? "NULL" : "zero" );
775 switch(ns = select( nfds, &readfds,
777 /* don't pass empty fd_set */
778 ( writefds.fd_count > 0 ? &writefds : NULL ),
784 case -1: { /* failure - try again */
785 int err = sock_errno();
789 || err == WSAENOTSOCK /* you'd think this would be EBADF */
792 if (++ebadf < SLAPD_EBADF_LIMIT)
797 Debug( LDAP_DEBUG_CONNS,
798 "daemon: select failed (%d): %s\n",
799 err, sock_errstr(err), 0 );
806 case 0: /* timeout - let threads run */
808 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
810 ldap_pvt_thread_yield();
813 default: /* something happened - deal with it */
815 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
820 if( FD_ISSET( wake_sds[0], &readfds ) ) {
822 tcp_read( wake_sds[0], c, sizeof(c) );
829 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
831 socklen_t len = sizeof(from);
837 char peername[MAXPATHLEN + sizeof("PATH=")];
840 char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
842 char peername[sizeof("IP=255.255.255.255:65336")];
844 #endif /* LDAP_PF_LOCAL */
845 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
848 if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
851 if ( (s = accept( slap_listeners[l]->sl_sd,
852 (struct sockaddr *) &from, &len )) == AC_SOCKET_INVALID )
854 int err = sock_errno();
855 Debug( LDAP_DEBUG_ANY,
856 "daemon: accept(%ld) failed errno=%d (%s)\n",
857 (long) slap_listeners[l]->sl_sd, err,
863 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
865 /* newly accepted stream should not be in any of the FD SETS */
867 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
868 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
869 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
871 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
875 /* make sure descriptor number isn't too great */
876 if ( s >= dtblsize ) {
877 Debug( LDAP_DEBUG_ANY,
878 "daemon: %ld beyond descriptor table size %ld\n",
879 (long) s, (long) dtblsize, 0 );
885 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
890 if ( getpeername( s, (struct sockaddr *) &from, &len ) != 0 ) {
891 int err = sock_errno();
892 Debug( LDAP_DEBUG_ANY,
893 "daemon: getpeername( %ld ) failed: errno=%d (%s)\n",
894 (long) s, err, sock_errstr(err) );
899 #if defined( LDAP_PF_LOCAL ) || defined( LDAP_INET6 )
900 switch ( from.sa_addr.sa_family ) {
901 # ifdef LDAP_PF_LOCAL
903 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
905 #endif /* LDAP_PF_LOCAL */
909 char addr[INET6_ADDRSTRLEN];
910 sprintf( peername, "IP=%s %d",
912 &from.sa_in6_addr.sin6_addr,
913 addr, sizeof addr) ? addr : "unknown",
914 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
916 # endif /* LDAP_INET6 */
919 #endif /* LDAP_PF_LOCAL || LDAP_INET6 */
920 peeraddr = inet_ntoa( from.sin_addr );
921 sprintf( peername, "IP=%s:%d",
922 peeraddr != NULL ? peeraddr : "unknown",
923 (unsigned) ntohs( from.sin_port ) );
924 #if defined( LDAP_PF_LOCAL ) || defined( LDAP_INET6 )
931 if ( from.sa_addr.sa_family != AF_UNIX ) {
932 #endif /* LDAP_PF_LOCAL || LDAP_INET6 */
933 #ifdef SLAPD_RLOOKUPS
935 if ( from.sa_addr.sa_family == AF_INET6 )
936 hp = gethostbyaddr( (char *)
937 &(from.sa_in6_addr.sin6_addr),
938 sizeof(from.sa_in6_addr.sin6_addr),
942 hp = gethostbyaddr( (char *) &(from.sin_addr),
943 sizeof(from.sin_addr), AF_INET );
945 dnsname = hp ? ldap_pvt_str2lower( hp->h_name ) : NULL;
948 #endif /* SLAPD_RLOOKUPS */
951 if( !hosts_ctl("slapd",
952 dnsname != NULL ? dnsname : STRING_UNKNOWN,
953 peeraddr != NULL ? peeraddr : STRING_UNKNOWN,
957 Statslog( LDAP_DEBUG_ANY,
958 "fd=%ld connection from %s (%s) denied.\n",
960 dnsname != NULL ? dnsname : "unknown",
961 peeraddr != NULL ? peeraddr : "unknown",
967 #endif /* HAVE_TCPD */
968 #if defined( LDAP_PF_LOCAL ) || defined( LDAP_INET6 )
972 #endif /* LDAP_PF_LOCAL || LDAP_INET6 */
974 if( (id = connection_init(s,
975 slap_listeners[l]->sl_url,
976 dnsname != NULL ? dnsname : "unknown",
978 slap_listeners[l]->sl_name,
980 slap_listeners[l]->sl_is_tls
986 Debug( LDAP_DEBUG_ANY,
987 "daemon: connection_init(%ld, %s, %s) failed.\n",
990 slap_listeners[l]->sl_name );
995 Statslog( LDAP_DEBUG_STATS,
996 "daemon: conn=%ld fd=%ld connection from %s (%s) accepted.\n",
999 slap_listeners[l]->sl_name,
1007 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1009 for ( i = 0; i < readfds.fd_count; i++ ) {
1010 Debug( LDAP_DEBUG_CONNS, " %d%s",
1011 readfds.fd_array[i], "r", 0 );
1013 for ( i = 0; i < writefds.fd_count; i++ ) {
1014 Debug( LDAP_DEBUG_CONNS, " %d%s",
1015 writefds.fd_array[i], "w", 0 );
1018 for ( i = 0; i < nfds; i++ ) {
1020 int is_listener = 0;
1022 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1023 if ( i == slap_listeners[l]->sl_sd ) {
1028 if ( is_listener ) {
1031 r = FD_ISSET( i, &readfds );
1032 w = FD_ISSET( i, &writefds );
1034 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1035 r ? "r" : "", w ? "w" : "" );
1039 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1042 /* loop through the writers */
1044 for ( i = 0; i < writefds.fd_count; i++ )
1046 for ( i = 0; i < nfds; i++ )
1050 int is_listener = 0;
1052 wd = writefds.fd_array[i];
1054 if( ! FD_ISSET( i, &writefds ) ) {
1060 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1061 if ( i == slap_listeners[l]->sl_sd ) {
1066 if ( is_listener ) {
1069 Debug( LDAP_DEBUG_CONNS,
1070 "daemon: write active on %d\n",
1074 * NOTE: it is possible that the connection was closed
1075 * and that the stream is now inactive.
1076 * connection_write() must valid the stream is still
1080 if ( connection_write( wd ) < 0 ) {
1081 FD_CLR( (unsigned) wd, &readfds );
1087 for ( i = 0; i < readfds.fd_count; i++ )
1089 for ( i = 0; i < nfds; i++ )
1093 int is_listener = 0;
1096 rd = readfds.fd_array[i];
1098 if( ! FD_ISSET( i, &readfds ) ) {
1104 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1105 if ( rd == slap_listeners[l]->sl_sd ) {
1110 if ( is_listener ) {
1114 Debug ( LDAP_DEBUG_CONNS,
1115 "daemon: read activity on %d\n", rd, 0, 0 );
1118 * NOTE: it is possible that the connection was closed
1119 * and that the stream is now inactive.
1120 * connection_read() must valid the stream is still
1124 if ( connection_read( rd ) < 0 ) {
1128 ldap_pvt_thread_yield();
1131 if( slapd_shutdown > 0 ) {
1132 Debug( LDAP_DEBUG_TRACE,
1133 "daemon: shutdown requested and initiated.\n",
1136 } else if ( slapd_shutdown < 0 ) {
1137 #ifdef HAVE_NT_SERVICE_MANAGER
1138 if (slapd_shutdown == -1)
1139 Debug( LDAP_DEBUG_TRACE,
1140 "daemon: shutdown initiated by Service Manager.\n",
1144 Debug( LDAP_DEBUG_TRACE,
1145 "daemon: abnormal condition, shutdown initiated.\n",
1148 Debug( LDAP_DEBUG_TRACE,
1149 "daemon: no active streams, shutdown initiated.\n",
1153 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1154 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1155 #ifdef LDAP_PF_LOCAL
1156 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_UNIX ) {
1157 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1159 #endif /* LDAP_PF_LOCAL */
1160 slapd_close( slap_listeners[l]->sl_sd );
1165 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
1166 Debug( LDAP_DEBUG_ANY,
1167 "slapd shutdown: waiting for %d threads to terminate\n",
1168 active_threads, 0, 0 );
1169 while ( active_threads > 0 ) {
1170 ldap_pvt_thread_cond_wait(&active_threads_cond, &active_threads_mutex);
1172 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
1178 int slapd_daemon( void )
1184 #define SLAPD_LISTENER_THREAD 1
1185 #if defined( SLAPD_LISTENER_THREAD ) || !defined(HAVE_PTHREADS)
1187 /* listener as a separate THREAD */
1188 rc = ldap_pvt_thread_create( &listener_tid,
1189 0, slapd_daemon_task, NULL );
1192 Debug( LDAP_DEBUG_ANY,
1193 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
1197 /* wait for the listener thread to complete */
1198 ldap_pvt_thread_join( listener_tid, (void *) NULL );
1200 /* expermimental code */
1201 listener_tid = pthread_self();
1202 slapd_daemon_task( NULL );
1209 #ifdef HAVE_WINSOCK2
1212 WORD wVersionRequested;
1216 wVersionRequested = MAKEWORD( 2, 0 );
1218 err = WSAStartup( wVersionRequested, &wsaData );
1220 /* Tell the user that we couldn't find a usable */
1225 /* Confirm that the WinSock DLL supports 2.0.*/
1226 /* Note that if the DLL supports versions greater */
1227 /* than 2.0 in addition to 2.0, it will still return */
1228 /* 2.0 in wVersion since that is the version we */
1231 if ( LOBYTE( wsaData.wVersion ) != 2 ||
1232 HIBYTE( wsaData.wVersion ) != 0 )
1234 /* Tell the user that we couldn't find a usable */
1240 /* The WinSock DLL is acceptable. Proceed. */
1244 int sockdestroy(void)
1251 static int sockinit(void)
1254 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
1259 static int sockdestroy(void)
1266 static int sockinit(void)
1270 static int sockdestroy(void)
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( sig, slap_sig_shutdown );
1302 slap_sig_wake( int sig )
1306 /* reinstall self */
1307 (void) SIGNAL( sig, slap_sig_wake );