2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/signal.h>
13 #include <ac/socket.h>
14 #include <ac/string.h>
16 #include <ac/unistd.h>
19 #include "ldap_defaults.h"
25 int allow_severity = LOG_INFO;
26 int deny_severity = LOG_NOTICE;
27 #endif /* TCP Wrappers */
31 ber_socket_t dtblsize;
33 typedef struct slap_listener {
40 struct sockaddr_in sl_addr;
43 Listener **slap_listeners = NULL;
47 extern ldap_pvt_thread_cond_t started_event;
49 /* forward reference */
50 static void hit_socket(void);
52 char *WSAGetLastErrorString();
53 static ldap_pvt_thread_t hit_tid;
55 #define WAKE_LISTENER(w) \
58 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );\
63 #define WAKE_LISTENER(w) \
66 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );\
74 volatile sig_atomic_t slapd_shutdown = 0;
75 static int sel_exit_fd;
77 static ldap_pvt_thread_t listener_tid;
78 static volatile sig_atomic_t slapd_listener = 0;
80 static struct slap_daemon {
81 ldap_pvt_thread_mutex_t sd_mutex;
86 /* In winsock, accept() returns values higher than dtblsize
87 so don't bother with this optimization */
97 * Add a descriptor to daemon control
99 static void slapd_add(ber_socket_t s) {
100 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
102 assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
103 assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
104 assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
107 if (s >= slap_daemon.sd_nfds) {
108 slap_daemon.sd_nfds = s + 1;
112 FD_SET( s, &slap_daemon.sd_actives );
113 FD_SET( s, &slap_daemon.sd_readers );
115 Debug( LDAP_DEBUG_CONNS, "daemon: added %ld%s%s\n",
117 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
118 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
120 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
124 * Remove the descriptor from daemon control
126 void slapd_remove(ber_socket_t s, int wake) {
127 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
130 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
132 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
133 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
135 FD_CLR( s, &slap_daemon.sd_actives );
136 FD_CLR( s, &slap_daemon.sd_readers );
137 FD_CLR( s, &slap_daemon.sd_writers );
139 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
142 void slapd_clr_write(ber_socket_t s, int wake) {
143 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
146 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
147 FD_CLR( s, &slap_daemon.sd_writers );
149 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
152 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
156 void slapd_set_write(ber_socket_t s, int wake) {
157 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
160 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
161 FD_SET( (unsigned) s, &slap_daemon.sd_writers );
163 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
166 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
170 void slapd_clr_read(ber_socket_t s, int wake) {
171 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
174 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
175 FD_CLR( s, &slap_daemon.sd_readers );
177 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
180 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
184 void slapd_set_read(ber_socket_t s, int wake) {
185 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
188 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
189 FD_SET( s, &slap_daemon.sd_readers );
191 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
194 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
198 static void slapd_close(ber_socket_t s) {
199 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
219 rc = ldap_url_parse( url, &lud );
221 if( rc != LDAP_URL_SUCCESS ) {
222 Debug( LDAP_DEBUG_ANY,
223 "daemon: listen URL \"%s\" parse error=%d\n",
229 if( lud->lud_ldaps ) {
230 Debug( LDAP_DEBUG_ANY,
231 "daemon: TLS not supported (%s)\n",
233 ldap_free_urldesc( lud );
237 if(! lud->lud_port ) {
238 lud->lud_port = port;
242 l.sl_is_tls = lud->lud_ldaps;
244 if(! lud->lud_port ) {
245 lud->lud_port = lud->lud_ldaps ? tls_port : port;
249 port = lud->lud_port;
251 (void) memset( (void*) &l.sl_addr, '\0', sizeof(l.sl_addr) );
253 l.sl_addr.sin_family = AF_INET;
254 l.sl_addr.sin_port = htons( (unsigned short) lud->lud_port );
256 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
257 || strcmp(lud->lud_host, "*") == 0 )
259 l.sl_addr.sin_addr.s_addr = htonl(INADDR_ANY);
262 /* host or address was specified */
263 if( !inet_aton( lud->lud_host, &l.sl_addr.sin_addr ) ) {
264 struct hostent *he = gethostbyname( lud->lud_host );
266 Debug( LDAP_DEBUG_ANY, "invalid host (%s) in URL: %s",
267 lud->lud_host, url, 0);
268 ldap_free_urldesc( lud );
272 memcpy( &l.sl_addr.sin_addr, he->h_addr,
273 sizeof( l.sl_addr.sin_addr ) );
277 ldap_free_urldesc( lud );
280 if ( (l.sl_sd = socket( AF_INET, SOCK_STREAM, 0 )) == AC_SOCKET_INVALID ) {
281 SOCK_ERR(err, errstr);
282 Debug( LDAP_DEBUG_ANY,
283 "daemon: socket() failed errno=%d (%s)\n", err,
289 if ( l.sl_sd >= dtblsize ) {
290 Debug( LDAP_DEBUG_ANY,
291 "daemon: listener descriptor %ld is too great %ld\n",
292 (long) l.sl_sd, (long) dtblsize, 0 );
293 tcp_close( l.sl_sd );
299 /* enable address reuse */
301 if ( setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
302 (char *) &tmp, sizeof(tmp) ) == -1 )
304 SOCK_ERR(err, errstr);
305 Debug( LDAP_DEBUG_ANY,
306 "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
307 (long) l.sl_sd, err, errstr );
311 /* enable keep alives */
313 if ( setsockopt( l.sl_sd, SOL_SOCKET, SO_KEEPALIVE,
314 (char *) &tmp, sizeof(tmp) ) == -1 )
316 SOCK_ERR(err, errstr);
317 Debug( LDAP_DEBUG_ANY,
318 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed errno=%d (%s)\n",
319 (long) l.sl_sd, err, errstr );
323 /* enable no delay */
325 if ( setsockopt( l.sl_sd, IPPROTO_TCP, TCP_NODELAY,
326 (char *)&tmp, sizeof(tmp) ) )
328 SOCK_ERR(err, errstr);
329 Debug( LDAP_DEBUG_ANY,
330 "slapd(%ld): setsockopt(TCP_NODELAY) failed errno=%d (%s)\n",
331 (long) l.sl_sd, err, errstr );
335 if ( bind( l.sl_sd, (struct sockaddr *) &l.sl_addr, sizeof(l.sl_addr) ) == -1 ) {
336 SOCK_ERR(err, errstr);
337 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
338 (long) l.sl_sd, err, errstr );
339 tcp_close( l.sl_sd );
343 l.sl_url = ch_strdup( url );
345 l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65336") );
346 s = inet_ntoa( l.sl_addr.sin_addr );
347 sprintf( l.sl_name, "IP=%s:%d",
348 s != NULL ? s : "unknown" , port );
350 li = ch_malloc( sizeof( Listener ) );
353 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
359 static int sockinit(void);
360 static int sockdestroy(void);
362 int slapd_daemon_init(char *urls, int port, int tls_port )
370 assert( tls_port == 0 );
373 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s (%d/%d)\n",
374 urls ? urls : "<null>", port, tls_port );
376 if( rc = sockinit() ) {
381 dtblsize = sysconf( _SC_OPEN_MAX );
382 #elif HAVE_GETDTABLESIZE
383 dtblsize = getdtablesize();
385 dtblsize = FD_SETSIZE;
389 if(dtblsize > FD_SETSIZE) {
390 dtblsize = FD_SETSIZE;
392 #endif /* !FD_SETSIZE */
394 /* set up a datagram socket connected to itself. we write a byte
395 * on this socket whenever we catch a signal. The main loop will
396 * be select'ing on this socket, and will wake up when this byte
399 if( (sel_exit_fd = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 )
401 SOCK_ERR(err, errstr);
402 Debug( LDAP_DEBUG_ANY,
403 "daemon: socket() failed errno=%d (%s)\n", err,
407 struct sockaddr_in si;
408 int len = sizeof(si);
410 (void) memset( (void*) &si, 0, len );
412 si.sin_family = AF_INET;
414 si.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
416 if( rc = bind( sel_exit_fd, (struct sockaddr *)&si, len ) )
418 SOCK_ERR(err, errstr);
419 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
420 (long) sel_exit_fd, err, errstr );
421 tcp_close( sel_exit_fd );
424 if( rc = getsockname( sel_exit_fd, (struct sockaddr *)&si, &len))
426 SOCK_ERR(err, errstr);
427 Debug( LDAP_DEBUG_ANY, "daemon: getsockname(%ld) failed errno=%d (%s)\n",
428 (long) sel_exit_fd, err, errstr );
429 tcp_close( sel_exit_fd );
432 if( rc = connect( sel_exit_fd, (struct sockaddr *)&si, len))
434 SOCK_ERR(err, errstr);
435 Debug( LDAP_DEBUG_ANY, "daemon: connect(%ld) failed errno=%d (%s)\n",
436 (long) sel_exit_fd, err, errstr );
437 tcp_close( sel_exit_fd );
442 FD_ZERO( &slap_daemon.sd_readers );
443 FD_ZERO( &slap_daemon.sd_writers );
449 u = str2charray( urls, " " );
451 if( u == NULL || u[0] == NULL ) {
452 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
458 for( i=0; u[i] != NULL; i++ ) {
459 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
464 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
470 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
473 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
475 for(i = 0; u[i] != NULL; i++ ) {
476 slap_listeners[i] = open_listener( u[i], port, tls_port );
478 if( slap_listeners[i] == NULL ) {
483 slap_listeners[i] = NULL;
485 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
489 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
495 slapd_daemon_destroy(void)
497 connections_destroy();
514 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
515 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
518 if ( listen( slap_listeners[l]->sl_sd, 5 ) == -1 ) {
519 SOCK_ERR(err, errstr);
520 Debug( LDAP_DEBUG_ANY,
521 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
522 (long) slap_listeners[l]->sl_url, err,
527 slapd_add( slap_listeners[l]->sl_sd );
531 if ( started_event != NULL ) {
532 ldap_pvt_thread_cond_signal( &started_event );
535 /* initialization complete. Here comes the loop. */
537 while ( !slapd_shutdown ) {
542 #define SLAPD_EBADF_LIMIT 10
545 #define SLAPD_IDLE_CHECK_LIMIT 4
546 time_t last_idle_check = slap_get_time();
553 struct sockaddr_in from;
554 #if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
560 if( global_idletimeout > 0 && difftime(
561 last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT,
564 connections_timeout_idle(now);
567 FD_ZERO( &writefds );
573 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
575 #ifdef FD_SET_MANUAL_COPY
576 for( s = 0; s < nfds; s++ ) {
577 if(FD_ISSET( &slap_sd_readers, s )) {
578 FD_SET( s, &readfds );
580 if(FD_ISSET( &slap_sd_writers, s )) {
581 FD_SET( s, &writefds );
585 memcpy( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
586 memcpy( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
588 FD_SET( sel_exit_fd, &readfds );
590 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
591 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
593 FD_SET( slap_listeners[l]->sl_sd, &readfds );
597 nfds = slap_daemon.sd_nfds;
602 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
604 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
606 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
608 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
611 tvp = at ? &zero : NULL;
614 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
615 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
618 Debug( LDAP_DEBUG_CONNS,
619 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
620 slap_listeners[l]->sl_sd, at,
621 tvp == NULL ? "NULL" : "zero" );
624 switch(ns = select( nfds, &readfds,
626 /* don't pass empty fd_set */
627 ( writefds.fd_count > 0 ? &writefds : NULL ),
633 case -1: { /* failure - try again */
634 SOCK_ERR(err, errstr);
635 if( err == EBADF && ++ebadf < SLAPD_EBADF_LIMIT) {
640 Debug( LDAP_DEBUG_CONNS,
641 "daemon: select failed (%d): %s\n",
649 case 0: /* timeout - let threads run */
651 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
653 ldap_pvt_thread_yield();
656 default: /* something happened - deal with it */
658 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
663 if( FD_ISSET( sel_exit_fd, &readfds ) )
666 tcp_read( sel_exit_fd, &c, 1 );
669 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
671 socklen_t len = sizeof(from);
677 char peername[sizeof("IP=255.255.255.255:65336")];
679 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
682 if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
685 if ( (s = accept( slap_listeners[l]->sl_sd,
686 (struct sockaddr *) &from, &len )) == AC_SOCKET_INVALID )
688 SOCK_ERR(err, errstr);
689 Debug( LDAP_DEBUG_ANY,
690 "daemon: accept(%ld) failed errno=%d (%s)\n",
691 (long) slap_listeners[l]->sl_sd, err,
697 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
699 /* newly accepted stream should not be in any of the FD SETS */
701 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
702 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
703 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
705 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
709 /* make sure descriptor number isn't too great */
710 if ( s >= dtblsize ) {
711 Debug( LDAP_DEBUG_ANY,
712 "daemon: %ld beyond descriptor table size %ld\n",
713 (long) s, (long) dtblsize, 0 );
719 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
724 if ( getpeername( s, (struct sockaddr *) &from, &len ) != 0 ) {
725 SOCK_ERR(err, errstr);
726 Debug( LDAP_DEBUG_ANY,
727 "daemon: getpeername( %ld ) failed: errno=%d (%s)\n",
728 (long) s, err, errstr );
733 peeraddr = inet_ntoa( from.sin_addr );
734 sprintf( peername, "IP=%s:%d",
735 peeraddr != NULL ? peeraddr : "unknown",
736 (unsigned) ntohs( from.sin_port ) );
738 #if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
739 hp = gethostbyaddr( (char *)
740 &(from.sin_addr.s_addr),
741 sizeof(from.sin_addr.s_addr), AF_INET );
744 dnsname = ldap_pvt_str2lower( hp->h_name );
754 if( !hosts_ctl("slapd",
755 dnsname != NULL ? dnsname : STRING_UNKNOWN,
756 peeraddr != NULL ? peeraddr : STRING_UNKNOWN,
760 Statslog( LDAP_DEBUG_ANY,
761 "fd=%ld connection from %s (%s) denied.\n",
763 dnsname != NULL ? dnsname : "unknown",
764 peeraddr != NULL ? peeraddr : "unknown",
770 #endif /* HAVE_TCPD */
772 if( (id = connection_init(s,
773 slap_listeners[l]->sl_url,
774 dnsname != NULL ? dnsname : "unknown",
776 slap_listeners[l]->sl_name,
778 slap_listeners[l]->sl_is_tls
784 Debug( LDAP_DEBUG_ANY,
785 "daemon: connection_init(%ld, %s, %s) failed.\n",
788 slap_listeners[l]->sl_name );
793 Statslog( LDAP_DEBUG_STATS,
794 "daemon: conn=%ld fd=%ld connection from %s (%s) accepted.\n",
797 slap_listeners[l]->sl_name,
805 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
807 for ( i = 0; i < readfds.fd_count; i++ ) {
808 Debug( LDAP_DEBUG_CONNS, " %d%s",
809 readfds.fd_array[i], "r", 0 );
811 for ( i = 0; i < writefds.fd_count; i++ ) {
812 Debug( LDAP_DEBUG_CONNS, " %d%s",
813 writefds.fd_array[i], "w", 0 );
816 for ( i = 0; i < nfds; i++ ) {
820 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
821 if ( i == slap_listeners[l]->sl_sd ) {
829 r = FD_ISSET( i, &readfds );
830 w = FD_ISSET( i, &writefds );
832 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
833 r ? "r" : "", w ? "w" : "" );
837 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
840 /* loop through the writers */
842 for ( i = 0; i < writefds.fd_count; i++ )
844 for ( i = 0; i < nfds; i++ )
850 wd = writefds.fd_array[i];
852 if( ! FD_ISSET( i, &writefds ) ) {
858 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
859 if ( i == slap_listeners[l]->sl_sd ) {
867 Debug( LDAP_DEBUG_CONNS,
868 "daemon: write active on %d\n",
872 * NOTE: it is possible that the connection was closed
873 * and that the stream is now inactive.
874 * connection_write() must valid the stream is still
878 if ( connection_write( wd ) < 0 ) {
879 FD_CLR( (unsigned) wd, &readfds );
885 for ( i = 0; i < readfds.fd_count; i++ )
887 for ( i = 0; i < nfds; i++ )
894 rd = readfds.fd_array[i];
896 if( ! FD_ISSET( i, &readfds ) ) {
902 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
903 if ( rd == slap_listeners[l]->sl_sd ) {
912 Debug ( LDAP_DEBUG_CONNS,
913 "daemon: read activity on %d\n", rd, 0, 0 );
916 * NOTE: it is possible that the connection was closed
917 * and that the stream is now inactive.
918 * connection_read() must valid the stream is still
922 if ( connection_read( rd ) < 0 ) {
926 ldap_pvt_thread_yield();
929 if( slapd_shutdown > 0 ) {
930 Debug( LDAP_DEBUG_TRACE,
931 "daemon: shutdown requested and initiated.\n",
934 } else if ( slapd_shutdown < 0 ) {
935 Debug( LDAP_DEBUG_TRACE,
936 "daemon: abnormal condition, shutdown initiated.\n",
939 Debug( LDAP_DEBUG_TRACE,
940 "daemon: no active streams, shutdown initiated.\n",
944 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
945 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
946 slapd_close( slap_listeners[l]->sl_sd );
951 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
952 Debug( LDAP_DEBUG_ANY,
953 "slapd shutdown: waiting for %d threads to terminate\n",
954 active_threads, 0, 0 );
955 while ( active_threads > 0 ) {
956 ldap_pvt_thread_cond_wait(&active_threads_cond, &active_threads_mutex);
958 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
964 int slapd_daemon( void )
970 #define SLAPD_LISTENER_THREAD 1
971 #if defined( SLAPD_LISTENER_THREAD ) || !defined(HAVE_PTHREADS)
973 /* listener as a separate THREAD */
974 rc = ldap_pvt_thread_create( &listener_tid,
975 0, slapd_daemon_task, NULL );
978 Debug( LDAP_DEBUG_ANY,
979 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
983 /* wait for the listener thread to complete */
984 ldap_pvt_thread_join( listener_tid, (void *) NULL );
986 /* expermimental code */
987 listener_tid = pthread_self();
988 slapd_daemon_task( NULL );
998 WORD wVersionRequested;
1002 wVersionRequested = MAKEWORD( 2, 0 );
1004 err = WSAStartup( wVersionRequested, &wsaData );
1006 /* Tell the user that we couldn't find a usable */
1011 /* Confirm that the WinSock DLL supports 2.0.*/
1012 /* Note that if the DLL supports versions greater */
1013 /* than 2.0 in addition to 2.0, it will still return */
1014 /* 2.0 in wVersion since that is the version we */
1017 if ( LOBYTE( wsaData.wVersion ) != 2 ||
1018 HIBYTE( wsaData.wVersion ) != 0 )
1020 /* Tell the user that we couldn't find a usable */
1026 /* The WinSock DLL is acceptable. Proceed. */
1030 int sockdestroy(void)
1036 void hit_socket(void)
1040 extern struct sockaddr_in bind_addr;
1042 /* throw something at the socket to terminate the select() in the daemon thread. */
1043 if (( s = socket( AF_INET, SOCK_STREAM, 0 )) == AC_SOCKET_INVALID )
1044 Debug( LDAP_DEBUG_ANY,
1045 "slap_set_shutdown: socket failed\n\tWSAGetLastError=%d (%s)\n",
1046 WSAGetLastError(), WSAGetLastErrorString(), 0 );
1048 if ( ioctlsocket( s, FIONBIO, &on ) == -1 )
1049 Debug( LDAP_DEBUG_ANY,
1050 "slap_set_shutdown:FIONBIO ioctl on %d faled\n\tWSAGetLastError=%d (%s)\n",
1051 s, WSAGetLastError(), WSAGetLastError() );
1053 bind_addr.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
1055 if ( connect( s, (struct sockaddr *)&bind_addr, sizeof( struct sockaddr_in )) == SOCKET_ERROR ) {
1056 Debug( LDAP_DEBUG_ANY,
1057 "hit_socket: error on connect: %d\n",
1058 WSAGetLastError(), 0, 0 );
1059 /* we can probably expect some error to occur here, mostly WSAEWOULDBLOCK */
1066 static int sockinit(void)
1068 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
1073 static int sockdestroy(void)
1080 static int sockinit(void)
1084 static int sockdestroy(void)
1091 slap_set_shutdown( int sig )
1094 slapd_shutdown = sig;
1095 #ifndef HAVE_WINSOCK
1096 if(slapd_listener) {
1097 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
1100 /* trying to "hit" the socket seems to always get a */
1101 /* EWOULDBLOCK error, so just close the listen socket to */
1102 /* break out of the select since we're shutting down anyway */
1103 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1104 if ( slap_listeners[l]->sl_sd >= 0 ) {
1105 tcp_close( slap_listeners[l]->sl_sd );
1109 tcp_write( sel_exit_fd, "0", 1 );
1110 /* reinstall self */
1111 (void) SIGNAL( sig, slap_set_shutdown );
1115 slap_do_nothing( int sig )
1117 /* reinstall self */
1118 (void) SIGNAL( sig, slap_do_nothing );