3 * Copyright 1998-1999 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;
39 typedef union slap_sockaddr {
40 struct sockaddr sa_addr;
41 struct sockaddr_in sa_in_addr;
42 struct sockaddr_un sa_un_addr;
44 #endif /* LDAP_PF_LOCAL */
46 typedef struct slap_listener {
55 #define sl_addr sl_sa.sa_in_addr
57 struct sockaddr_in sl_addr;
58 #endif /* LDAP_PF_LOCAL */
61 Listener **slap_listeners = NULL;
63 static ber_socket_t wake_sds[2];
67 #define WAKE_LISTENER(w) \
68 ((w && !waking) ? tcp_write( wake_sds[1], "0", 1 ), waking=1 : 0)
70 #define WAKE_LISTENER(w) \
71 do { if (w) tcp_write( wake_sds[1], "0", 1 ); } while(0)
74 #ifdef HAVE_NT_SERVICE_MANAGER
76 extern ldap_pvt_thread_cond_t started_event;
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 )
206 rc = ldap_url_parse( url, &lud );
208 if( rc != LDAP_URL_SUCCESS ) {
209 Debug( LDAP_DEBUG_ANY,
210 "daemon: listen URL \"%s\" parse error=%d\n",
216 if( lud->lud_properties & LDAP_URL_USE_SSL ) {
217 Debug( LDAP_DEBUG_ANY,
218 "daemon: TLS not supported (%s)\n",
220 ldap_free_urldesc( lud );
224 if(! lud->lud_port ) {
225 lud->lud_port = LDAP_PORT;
229 l.sl_is_tls = (lud->lud_properties & LDAP_URL_USE_SSL);
231 if(! lud->lud_port ) {
232 lud->lud_port = (lud->lud_properties & LDAP_URL_USE_SSL) ? LDAPS_PORT : LDAP_PORT;
237 if (lud->lud_protocol == LDAP_PROTO_LOCAL) {
239 (void) memset( (void *)&l.sl_sa.sa_un_addr, '\0', sizeof(l.sl_sa.sa_un_addr) );
241 l.sl_sa.sa_un_addr.sun_family = AF_UNIX;
243 /* hack: overload the host to be the path */
244 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
245 strcpy( l.sl_sa.sa_un_addr.sun_path, "/tmp/.ldap-sock" );
247 if ( strlen(lud->lud_host) > (sizeof(l.sl_sa.sa_un_addr.sun_path) - 1) ) {
248 Debug( LDAP_DEBUG_ANY, "domain socket path (%s) too long in URL: %s",
249 lud->lud_host, url, 0);
250 ldap_free_urldesc( lud );
253 strcpy( l.sl_sa.sa_un_addr.sun_path, lud->lud_host );
255 unlink( l.sl_sa.sa_un_addr.sun_path );
257 /* I don't think we need to set this. */
258 l.sl_sa.sa_un_addr.sun_len = sizeof( l.sl_sa.sa_un_addr.sun_len ) +
259 sizeof( l.sl_sa.sa_un_addr.sun_family ) +
260 strlen( l.sl_sa.sa_un_addr.sun_path ) + 1;
263 #endif /* LDAP_PF_LOCAL */
265 port = lud->lud_port;
267 (void) memset( (void*) &l.sl_addr, '\0', sizeof(l.sl_addr) );
269 l.sl_addr.sin_family = AF_INET;
270 l.sl_addr.sin_port = htons( (unsigned short) lud->lud_port );
272 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
273 || strcmp(lud->lud_host, "*") == 0 )
275 l.sl_addr.sin_addr.s_addr = htonl(INADDR_ANY);
278 /* host or address was specified */
279 if( !inet_aton( lud->lud_host, &l.sl_addr.sin_addr ) ) {
280 struct hostent *he = gethostbyname( lud->lud_host );
282 Debug( LDAP_DEBUG_ANY, "invalid host (%s) in URL: %s",
283 lud->lud_host, url, 0);
284 ldap_free_urldesc( lud );
288 memcpy( &l.sl_addr.sin_addr, he->h_addr,
289 sizeof( l.sl_addr.sin_addr ) );
294 #endif /* LDAP_PF_LOCAL */
296 ldap_free_urldesc( lud );
299 l.sl_sd = socket( l.sl_sa.sa_addr.sa_family, SOCK_STREAM, 0 );
300 if ( l.sl_sd == AC_SOCKET_INVALID ) {
302 if ( (l.sl_sd = socket( AF_INET, SOCK_STREAM, 0 )) == AC_SOCKET_INVALID ) {
303 #endif /* LDAP_PF_LOCAL */
304 int err = sock_errno();
305 Debug( LDAP_DEBUG_ANY,
306 "daemon: socket() failed errno=%d (%s)\n", err,
307 sock_errstr(err), 0 );
312 if ( l.sl_sd >= dtblsize ) {
313 Debug( LDAP_DEBUG_ANY,
314 "daemon: listener descriptor %ld is too great %ld\n",
315 (long) l.sl_sd, (long) dtblsize, 0 );
316 tcp_close( l.sl_sd );
322 /* for IP sockets only */
323 if ( l.sl_sa.sa_addr.sa_family == AF_INET ) {
324 #endif /* LDAP_PF_LOCAL */
327 /* enable address reuse */
329 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
330 (char *) &tmp, sizeof(tmp) );
331 if ( rc == AC_SOCKET_ERROR ) {
332 int err = sock_errno();
333 Debug( LDAP_DEBUG_ANY,
334 "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
335 (long) l.sl_sd, err, sock_errstr(err) );
339 /* enable keep alives */
341 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_KEEPALIVE,
342 (char *) &tmp, sizeof(tmp) );
343 if ( rc == AC_SOCKET_ERROR ) {
344 int err = sock_errno();
345 Debug( LDAP_DEBUG_ANY,
346 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed errno=%d (%s)\n",
347 (long) l.sl_sd, err, sock_errstr(err) );
351 /* enable no delay */
353 rc = setsockopt( l.sl_sd, IPPROTO_TCP, TCP_NODELAY,
354 (char *)&tmp, sizeof(tmp) );
355 if ( rc == AC_SOCKET_ERROR ) {
356 int err = sock_errno();
357 Debug( LDAP_DEBUG_ANY,
358 "slapd(%ld): setsockopt(TCP_NODELAY) failed errno=%d (%s)\n",
359 (long) l.sl_sd, err, sock_errstr(err) );
364 /* close conditional */
367 switch ( l.sl_sa.sa_addr.sa_family ) {
369 rc = bind( l.sl_sd, (struct sockaddr *)&l.sl_sa,
370 sizeof(l.sl_sa.sa_un_addr) );
373 rc = bind( l.sl_sd, (struct sockaddr *)&l.sl_sa,
374 sizeof(l.sl_sa.sa_in_addr) );
377 rc = AC_SOCKET_ERROR;
382 rc = bind( l.sl_sd, (struct sockaddr *) &l.sl_addr, sizeof(l.sl_addr) );
383 #endif /* LDAP_PF_LOCAL */
384 if ( rc == AC_SOCKET_ERROR ) {
385 int err = sock_errno();
386 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
387 (long) l.sl_sd, err, sock_errstr(err) );
388 tcp_close( l.sl_sd );
392 if ( l.sl_sa.sa_addr.sa_family == AF_UNIX ) {
393 if ( chmod( l.sl_sa.sa_un_addr.sun_path, S_IRWXU ) < 0 ) {
394 int err = sock_errno();
395 Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
396 (long) l.sl_sd, err, sock_errstr(err) );
397 tcp_close( l.sl_sd );
401 #endif /* LDAP_PF_LOACL */
402 l.sl_url = ch_strdup( url );
404 switch ( l.sl_sa.sa_addr.sa_family ) {
406 l.sl_name = ch_malloc( strlen(l.sl_sa.sa_un_addr.sun_path) + sizeof("PATH=") );
407 sprintf( l.sl_name, "PATH=%s", l.sl_sa.sa_un_addr.sun_path );
410 l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65336") );
411 s = inet_ntoa( l.sl_addr.sin_addr );
412 sprintf( l.sl_name, "IP=%s:%d",
413 s != NULL ? s : "unknown" , port );
416 l.sl_name = ch_strdup( "UNKNOWN" );
420 l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65336") );
421 s = inet_ntoa( l.sl_addr.sin_addr );
422 sprintf( l.sl_name, "IP=%s:%d",
423 s != NULL ? s : "unknown" , port );
424 #endif /* LDAP_PF_LOCAL */
426 li = ch_malloc( sizeof( Listener ) );
429 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
435 static int sockinit(void);
436 static int sockdestroy(void);
438 int slapd_daemon_init( char *urls )
443 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
444 urls ? urls : "<null>", 0, 0 );
446 if( (rc = sockinit()) != 0 ) {
451 dtblsize = sysconf( _SC_OPEN_MAX );
452 #elif HAVE_GETDTABLESIZE
453 dtblsize = getdtablesize();
455 dtblsize = FD_SETSIZE;
459 if(dtblsize > FD_SETSIZE) {
460 dtblsize = FD_SETSIZE;
462 #endif /* !FD_SETSIZE */
464 /* open a pipe (or something equivalent connected to itself).
465 * we write a byte on this fd whenever we catch a signal. The main
466 * loop will be select'ing on this socket, and will wake up when
469 if( (rc = lutil_pair( wake_sds )) < 0 ) {
470 Debug( LDAP_DEBUG_ANY,
471 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
475 FD_ZERO( &slap_daemon.sd_readers );
476 FD_ZERO( &slap_daemon.sd_writers );
482 u = str2charray( urls, " " );
484 if( u == NULL || u[0] == NULL ) {
485 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
491 for( i=0; u[i] != NULL; i++ ) {
492 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
497 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
503 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
506 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
508 for(i = 0; u[i] != NULL; i++ ) {
509 slap_listeners[i] = open_listener( u[i] );
511 if( slap_listeners[i] == NULL ) {
516 slap_listeners[i] = NULL;
518 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
522 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
528 slapd_daemon_destroy(void)
530 connections_destroy();
531 tcp_close( wake_sds[1] );
532 tcp_close( wake_sds[0] );
544 time_t last_idle_check = slap_get_time();
547 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
548 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
551 if ( listen( slap_listeners[l]->sl_sd, 5 ) == -1 ) {
552 int err = sock_errno();
553 Debug( LDAP_DEBUG_ANY,
554 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
555 slap_listeners[l]->sl_url, err,
560 slapd_add( slap_listeners[l]->sl_sd );
563 #ifdef HAVE_NT_SERVICE_MANAGER
564 if ( started_event != NULL ) {
565 ldap_pvt_thread_cond_signal( &started_event );
568 /* initialization complete. Here comes the loop. */
570 while ( !slapd_shutdown ) {
575 #define SLAPD_EBADF_LIMIT 10
578 #define SLAPD_IDLE_CHECK_LIMIT 4
579 time_t now = slap_get_time();
586 /* minimize impact, undefine later. */
587 #define sin_addr sa_in_addr.sin_addr
588 #define sin_port sa_in_addr.sin_port
590 struct sockaddr_in from;
591 #endif /* LDAP_PF_LOCAL */
592 #if defined(SLAPD_RLOOKUPS)
598 if( global_idletimeout > 0 && difftime(
599 last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT,
602 connections_timeout_idle(now);
605 FD_ZERO( &writefds );
611 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
613 #ifdef FD_SET_MANUAL_COPY
614 for( s = 0; s < nfds; s++ ) {
615 if(FD_ISSET( &slap_sd_readers, s )) {
616 FD_SET( s, &readfds );
618 if(FD_ISSET( &slap_sd_writers, s )) {
619 FD_SET( s, &writefds );
623 memcpy( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
624 memcpy( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
626 assert(!FD_ISSET(wake_sds[0], &readfds));
627 FD_SET( wake_sds[0], &readfds );
629 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
630 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
632 if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
633 FD_SET( slap_listeners[l]->sl_sd, &readfds );
637 nfds = slap_daemon.sd_nfds;
642 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
644 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
646 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
648 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
651 tvp = at ? &zero : NULL;
654 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
655 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
658 Debug( LDAP_DEBUG_CONNS,
659 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
660 slap_listeners[l]->sl_sd, at,
661 tvp == NULL ? "NULL" : "zero" );
664 switch(ns = select( nfds, &readfds,
666 /* don't pass empty fd_set */
667 ( writefds.fd_count > 0 ? &writefds : NULL ),
673 case -1: { /* failure - try again */
674 int err = sock_errno();
678 || err == WSAENOTSOCK /* you'd think this would be EBADF */
681 if (++ebadf < SLAPD_EBADF_LIMIT)
686 Debug( LDAP_DEBUG_CONNS,
687 "daemon: select failed (%d): %s\n",
688 err, sock_errstr(err), 0 );
695 case 0: /* timeout - let threads run */
697 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
699 ldap_pvt_thread_yield();
702 default: /* something happened - deal with it */
704 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
709 if( FD_ISSET( wake_sds[0], &readfds ) ) {
711 tcp_read( wake_sds[0], c, sizeof(c) );
718 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
720 socklen_t len = sizeof(from);
726 char peername[MAXPATHLEN + sizeof("PATH=")];
728 char peername[sizeof("IP=255.255.255.255:65336")];
729 #endif /* LDAP_PF_LOCAL */
730 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
733 if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
736 if ( (s = accept( slap_listeners[l]->sl_sd,
737 (struct sockaddr *) &from, &len )) == AC_SOCKET_INVALID )
739 int err = sock_errno();
740 Debug( LDAP_DEBUG_ANY,
741 "daemon: accept(%ld) failed errno=%d (%s)\n",
742 (long) slap_listeners[l]->sl_sd, err,
748 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
750 /* newly accepted stream should not be in any of the FD SETS */
752 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
753 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
754 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
756 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
760 /* make sure descriptor number isn't too great */
761 if ( s >= dtblsize ) {
762 Debug( LDAP_DEBUG_ANY,
763 "daemon: %ld beyond descriptor table size %ld\n",
764 (long) s, (long) dtblsize, 0 );
770 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
775 if ( getpeername( s, (struct sockaddr *) &from, &len ) != 0 ) {
776 int err = sock_errno();
777 Debug( LDAP_DEBUG_ANY,
778 "daemon: getpeername( %ld ) failed: errno=%d (%s)\n",
779 (long) s, err, sock_errstr(err) );
785 switch ( from.sa_addr.sa_family ) {
787 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
790 #endif /* LDAP_PF_LOCAL */
791 peeraddr = inet_ntoa( from.sin_addr );
792 sprintf( peername, "IP=%s:%d",
793 peeraddr != NULL ? peeraddr : "unknown",
794 (unsigned) ntohs( from.sin_port ) );
796 #if defined(SLAPD_RLOOKUPS)
797 hp = gethostbyaddr( (char *)
799 sizeof(from.sin_addr), AF_INET );
802 dnsname = ldap_pvt_str2lower( hp->h_name );
812 if( !hosts_ctl("slapd",
813 dnsname != NULL ? dnsname : STRING_UNKNOWN,
814 peeraddr != NULL ? peeraddr : STRING_UNKNOWN,
818 Statslog( LDAP_DEBUG_ANY,
819 "fd=%ld connection from %s (%s) denied.\n",
821 dnsname != NULL ? dnsname : "unknown",
822 peeraddr != NULL ? peeraddr : "unknown",
828 #endif /* HAVE_TCPD */
837 #endif /* LDAP_PF_LOCAL */
839 if( (id = connection_init(s,
840 slap_listeners[l]->sl_url,
841 dnsname != NULL ? dnsname : "unknown",
843 slap_listeners[l]->sl_name,
845 slap_listeners[l]->sl_is_tls
851 Debug( LDAP_DEBUG_ANY,
852 "daemon: connection_init(%ld, %s, %s) failed.\n",
855 slap_listeners[l]->sl_name );
860 Statslog( LDAP_DEBUG_STATS,
861 "daemon: conn=%ld fd=%ld connection from %s (%s) accepted.\n",
864 slap_listeners[l]->sl_name,
872 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
874 for ( i = 0; i < readfds.fd_count; i++ ) {
875 Debug( LDAP_DEBUG_CONNS, " %d%s",
876 readfds.fd_array[i], "r", 0 );
878 for ( i = 0; i < writefds.fd_count; i++ ) {
879 Debug( LDAP_DEBUG_CONNS, " %d%s",
880 writefds.fd_array[i], "w", 0 );
883 for ( i = 0; i < nfds; i++ ) {
887 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
888 if ( i == slap_listeners[l]->sl_sd ) {
896 r = FD_ISSET( i, &readfds );
897 w = FD_ISSET( i, &writefds );
899 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
900 r ? "r" : "", w ? "w" : "" );
904 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
907 /* loop through the writers */
909 for ( i = 0; i < writefds.fd_count; i++ )
911 for ( i = 0; i < nfds; i++ )
917 wd = writefds.fd_array[i];
919 if( ! FD_ISSET( i, &writefds ) ) {
925 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
926 if ( i == slap_listeners[l]->sl_sd ) {
934 Debug( LDAP_DEBUG_CONNS,
935 "daemon: write active on %d\n",
939 * NOTE: it is possible that the connection was closed
940 * and that the stream is now inactive.
941 * connection_write() must valid the stream is still
945 if ( connection_write( wd ) < 0 ) {
946 FD_CLR( (unsigned) wd, &readfds );
952 for ( i = 0; i < readfds.fd_count; i++ )
954 for ( i = 0; i < nfds; i++ )
961 rd = readfds.fd_array[i];
963 if( ! FD_ISSET( i, &readfds ) ) {
969 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
970 if ( rd == slap_listeners[l]->sl_sd ) {
979 Debug ( LDAP_DEBUG_CONNS,
980 "daemon: read activity on %d\n", rd, 0, 0 );
983 * NOTE: it is possible that the connection was closed
984 * and that the stream is now inactive.
985 * connection_read() must valid the stream is still
989 if ( connection_read( rd ) < 0 ) {
993 ldap_pvt_thread_yield();
996 if( slapd_shutdown > 0 ) {
997 Debug( LDAP_DEBUG_TRACE,
998 "daemon: shutdown requested and initiated.\n",
1001 } else if ( slapd_shutdown < 0 ) {
1002 Debug( LDAP_DEBUG_TRACE,
1003 "daemon: abnormal condition, shutdown initiated.\n",
1006 Debug( LDAP_DEBUG_TRACE,
1007 "daemon: no active streams, shutdown initiated.\n",
1011 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1012 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1013 #ifdef LDAP_PF_LOCAL
1014 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_UNIX ) {
1015 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1017 #endif /* LDAP_PF_LOCAL */
1018 slapd_close( slap_listeners[l]->sl_sd );
1023 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
1024 Debug( LDAP_DEBUG_ANY,
1025 "slapd shutdown: waiting for %d threads to terminate\n",
1026 active_threads, 0, 0 );
1027 while ( active_threads > 0 ) {
1028 ldap_pvt_thread_cond_wait(&active_threads_cond, &active_threads_mutex);
1030 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
1036 int slapd_daemon( void )
1042 #define SLAPD_LISTENER_THREAD 1
1043 #if defined( SLAPD_LISTENER_THREAD ) || !defined(HAVE_PTHREADS)
1045 /* listener as a separate THREAD */
1046 rc = ldap_pvt_thread_create( &listener_tid,
1047 0, slapd_daemon_task, NULL );
1050 Debug( LDAP_DEBUG_ANY,
1051 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
1055 /* wait for the listener thread to complete */
1056 ldap_pvt_thread_join( listener_tid, (void *) NULL );
1058 /* expermimental code */
1059 listener_tid = pthread_self();
1060 slapd_daemon_task( NULL );
1067 #ifdef HAVE_WINSOCK2
1070 WORD wVersionRequested;
1074 wVersionRequested = MAKEWORD( 2, 0 );
1076 err = WSAStartup( wVersionRequested, &wsaData );
1078 /* Tell the user that we couldn't find a usable */
1083 /* Confirm that the WinSock DLL supports 2.0.*/
1084 /* Note that if the DLL supports versions greater */
1085 /* than 2.0 in addition to 2.0, it will still return */
1086 /* 2.0 in wVersion since that is the version we */
1089 if ( LOBYTE( wsaData.wVersion ) != 2 ||
1090 HIBYTE( wsaData.wVersion ) != 0 )
1092 /* Tell the user that we couldn't find a usable */
1098 /* The WinSock DLL is acceptable. Proceed. */
1102 int sockdestroy(void)
1109 static int sockinit(void)
1112 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
1117 static int sockdestroy(void)
1124 static int sockinit(void)
1128 static int sockdestroy(void)
1135 slap_sig_shutdown( int sig )
1137 slapd_shutdown = sig;
1140 /* reinstall self */
1141 (void) SIGNAL( sig, slap_sig_shutdown );
1145 slap_sig_wake( int sig )
1149 /* reinstall self */
1150 (void) SIGNAL( sig, slap_sig_wake );