3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/socket.h>
14 #include <ac/string.h>
16 #include <ac/unistd.h>
19 #include "ldap_pvt_thread.h"
26 int allow_severity = LOG_INFO;
27 int deny_severity = LOG_NOTICE;
28 #endif /* TCP Wrappers */
32 /* this should go in <ldap.h> as soon as it is accepted */
33 #define LDAPI_MOD_URLEXT "x-mod"
34 #endif /* LDAP_PF_LOCAL */
38 ber_socket_t dtblsize;
40 Listener **slap_listeners = NULL;
42 #define SLAPD_LISTEN 10
44 static ber_socket_t wake_sds[2];
48 #define WAKE_LISTENER(w) \
49 ((w && !waking) ? tcp_write( wake_sds[1], "0", 1 ), waking=1 : 0)
51 #define WAKE_LISTENER(w) \
52 do { if (w) tcp_write( wake_sds[1], "0", 1 ); } while(0)
58 volatile sig_atomic_t slapd_shutdown = 0, slapd_gentle_shutdown = 0;
60 static struct slap_daemon {
61 ldap_pvt_thread_mutex_t sd_mutex;
63 ber_socket_t sd_nactives;
66 /* In winsock, accept() returns values higher than dtblsize
67 so don't bother with this optimization */
80 * SLP related functions
84 #define LDAP_SRVTYPE_PREFIX "service:ldap://"
85 #define LDAPS_SRVTYPE_PREFIX "service:ldaps://"
86 static char** slapd_srvurls = NULL;
87 static SLPHandle slapd_hslp = 0;
89 void slapd_slp_init( const char* urls ) {
92 slapd_srvurls = str2charray( urls, " " );
94 if( slapd_srvurls == NULL ) return;
96 /* find and expand INADDR_ANY URLs */
97 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
98 if( strcmp( slapd_srvurls[i], "ldap:///" ) == 0) {
99 char *host = ldap_pvt_get_fqdn( NULL );
100 if ( host != NULL ) {
101 slapd_srvurls[i] = (char *) realloc( slapd_srvurls[i],
103 sizeof( LDAP_SRVTYPE_PREFIX ) );
104 strcpy( slap_strcopy(slapd_srvurls[i],
105 LDAP_SRVTYPE_PREFIX ), host );
110 } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0) {
111 char *host = ldap_pvt_get_fqdn( NULL );
112 if ( host != NULL ) {
113 slapd_srvurls[i] = (char *) realloc( slapd_srvurls[i],
115 sizeof( LDAPS_SRVTYPE_PREFIX ) );
116 strcpy( slap_strcopy(slapd_srvurls[i],
117 LDAPS_SRVTYPE_PREFIX ), host );
124 /* open the SLP handle */
125 SLPOpen( "en", 0, &slapd_hslp );
128 void slapd_slp_deinit() {
129 if( slapd_srvurls == NULL ) return;
131 charray_free( slapd_srvurls );
132 slapd_srvurls = NULL;
134 /* close the SLP handle */
135 SLPClose( slapd_hslp );
138 void slapd_slp_regreport(
146 void slapd_slp_reg() {
149 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
150 if( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
151 sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
152 strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
153 sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
157 SLP_LIFETIME_MAXIMUM,
167 void slapd_slp_dereg() {
170 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
171 SLPDereg( slapd_hslp,
177 #endif /* HAVE_SLP */
180 * Add a descriptor to daemon control
182 static void slapd_add(ber_socket_t s) {
183 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
185 assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
186 assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
187 assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
190 if (s >= slap_daemon.sd_nfds) {
191 slap_daemon.sd_nfds = s + 1;
195 slap_daemon.sd_nactives++;
197 FD_SET( s, &slap_daemon.sd_actives );
198 FD_SET( s, &slap_daemon.sd_readers );
201 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
202 "slapd_add: added %ld%s%s\n",
204 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
205 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" ));
207 Debug( LDAP_DEBUG_CONNS, "daemon: added %ld%s%s\n",
209 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
210 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
212 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
216 * Remove the descriptor from daemon control
218 void slapd_remove(ber_socket_t s, int wake) {
219 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
221 slap_daemon.sd_nactives--;
224 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
225 "slapd_remove: removing %ld%s%s\n",
227 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
228 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" ));
230 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
232 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
233 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
235 FD_CLR( s, &slap_daemon.sd_actives );
236 FD_CLR( s, &slap_daemon.sd_readers );
237 FD_CLR( s, &slap_daemon.sd_writers );
239 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
240 WAKE_LISTENER(wake || slapd_gentle_shutdown < 0);
243 void slapd_clr_write(ber_socket_t s, int wake) {
244 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
246 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
247 FD_CLR( s, &slap_daemon.sd_writers );
249 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
253 void slapd_set_write(ber_socket_t s, int wake) {
254 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
256 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
257 if (!FD_ISSET(s, &slap_daemon.sd_writers))
258 FD_SET( (unsigned) s, &slap_daemon.sd_writers );
260 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
264 void slapd_clr_read(ber_socket_t s, int wake) {
265 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
267 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
268 FD_CLR( s, &slap_daemon.sd_readers );
270 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
274 void slapd_set_read(ber_socket_t s, int wake) {
275 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
277 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
278 if (!FD_ISSET(s, &slap_daemon.sd_readers))
279 FD_SET( s, &slap_daemon.sd_readers );
281 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
285 static void slapd_close(ber_socket_t s) {
287 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
288 "slapd_close: closing %ld\n", (long)s ));
290 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
296 static void slap_free_listener_addresses(struct sockaddr **sal)
298 struct sockaddr **sap;
304 for (sap = sal; *sap != NULL; sap++) {
312 static int get_url_perms(
324 for ( i = 0; exts[ i ]; i++ ) {
325 char *type = exts[ i ];
328 if ( type[ 0 ] == '!' ) {
333 if ( strncasecmp( type, LDAPI_MOD_URLEXT "=", sizeof(LDAPI_MOD_URLEXT "=") - 1 ) == 0 ) {
334 char *value = type + sizeof(LDAPI_MOD_URLEXT "=") - 1;
338 if ( strlen(value) != 3 ) {
342 for ( j = 0; j < 3; j++ ) {
344 = { S_IRWXU, S_IRWXG, S_IRWXO };
346 switch ( value[ j ] ) {
366 #endif /* LDAP_PF_LOCAL */
368 /* port = 0 indicates AF_LOCAL */
369 static int slap_get_listener_addresses(
372 struct sockaddr ***sal)
374 struct sockaddr **sap;
378 *sal = ch_malloc(2 * sizeof(void *));
384 *sap = ch_malloc(sizeof(struct sockaddr_un));
390 (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) ) {
392 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
393 "slap_get_listener_addresses: domain socket path (%s) too long in URL\n",
396 Debug( LDAP_DEBUG_ANY,
397 "daemon: domain socket path (%s) too long in URL",
403 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
404 (*sap)->sa_family = AF_LOCAL;
405 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
409 #ifdef HAVE_GETADDRINFO
410 struct addrinfo hints, *res, *sai;
414 memset( &hints, '\0', sizeof(hints) );
415 hints.ai_flags = AI_PASSIVE;
416 hints.ai_socktype = SOCK_STREAM;
417 hints.ai_family = AF_UNSPEC;
418 snprintf(serv, sizeof serv, "%d", port);
420 if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
422 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
423 "slap_get_listener_addresses: getaddrinfo failed: %s\n",
424 AC_GAI_STRERROR(err) ));
426 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
427 AC_GAI_STRERROR(err), 0, 0);
433 for (n=2; (sai = sai->ai_next) != NULL; n++) {
436 *sal = ch_calloc(n, sizeof(void *));
444 for ( sai=res; sai; sai=sai->ai_next ) {
445 if( sai->ai_addr == NULL ) {
447 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
448 "slap_get_listener_addresses: "
449 "getaddrinfo ai_addr is NULL?\n" ));
451 Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
452 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
458 switch (sai->ai_family) {
459 # ifdef LDAP_PF_INET6
461 *sap = ch_malloc(sizeof(struct sockaddr_in6));
466 *(struct sockaddr_in6 *)*sap =
467 *((struct sockaddr_in6 *)sai->ai_addr);
471 *sap = ch_malloc(sizeof(struct sockaddr_in));
476 *(struct sockaddr_in *)*sap =
477 *((struct sockaddr_in *)sai->ai_addr);
485 (*sap)->sa_family = sai->ai_family;
495 if ( host == NULL ) {
496 in.s_addr = htonl(INADDR_ANY);
498 } else if ( !inet_aton( host, &in ) ) {
499 struct hostent *he = gethostbyname( host );
502 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
503 "slap_get_listener_addresses: invalid host %s\n",
506 Debug( LDAP_DEBUG_ANY,
507 "daemon: invalid host %s", host, 0, 0);
511 AC_MEMCPY( &in, he->h_addr, sizeof( in ) );
514 *sal = ch_malloc(2 * sizeof(void *));
520 *sap = ch_malloc(sizeof(struct sockaddr_in));
526 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_in) );
527 (*sap)->sa_family = AF_INET;
528 ((struct sockaddr_in *)*sap)->sin_port = htons(port);
529 ((struct sockaddr_in *)*sap)->sin_addr = in;
536 slap_free_listener_addresses(*sal);
540 static Listener * slap_open_listener(
548 int err, addrlen = 0;
549 struct sockaddr **sal, **psal;
550 int socktype = SOCK_STREAM; /* default to COTS */
552 mode_t perms = S_IRWXU;
556 rc = ldap_url_parse( url, &lud );
558 if( rc != LDAP_URL_SUCCESS ) {
560 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
561 "slap_open_listener: listen URL \"%s\" parse error %d\n",
564 Debug( LDAP_DEBUG_ANY,
565 "daemon: listen URL \"%s\" parse error=%d\n",
572 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
574 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
575 "slap_open_listener: TLS is not supported (%s)\n",
578 Debug( LDAP_DEBUG_ANY,
579 "daemon: TLS not supported (%s)\n",
582 ldap_free_urldesc( lud );
586 if(! lud->lud_port ) {
587 lud->lud_port = LDAP_PORT;
591 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
593 if(! lud->lud_port ) {
594 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
598 port = (unsigned short) lud->lud_port;
600 tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme);
601 if ( tmp == LDAP_PROTO_IPC ) {
603 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
604 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
606 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
609 if ( lud->lud_exts ) {
610 err = get_url_perms( lud->lud_exts, &perms, &crit );
615 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
616 "slap_open_listener: URL scheme is not supported: %s\n",
619 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
622 ldap_free_urldesc( lud );
626 #ifdef LDAP_CONNECTIONLESS
627 l.sl_is_udp = ( tmp == LDAP_PROTO_UDP );
629 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
630 || strcmp(lud->lud_host, "*") == 0 )
632 err = slap_get_listener_addresses(NULL, port, &sal);
634 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
638 ldap_free_urldesc( lud );
644 while ( *sal != NULL ) {
645 switch( (*sal)->sa_family ) {
658 #ifdef LDAP_CONNECTIONLESS
660 socktype = SOCK_DGRAM;
662 l.sl_sd = socket( (*sal)->sa_family, socktype, 0);
663 if ( l.sl_sd == AC_SOCKET_INVALID ) {
664 int err = sock_errno();
666 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
667 "slap_open_listener: socket() failed errno=%d (%s)\n",
668 err, sock_errstr(err) ));
670 Debug( LDAP_DEBUG_ANY,
671 "daemon: socket() failed errno=%d (%s)\n", err,
672 sock_errstr(err), 0 );
678 if ( l.sl_sd >= dtblsize ) {
680 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
681 "slap_open_listener: listener descriptor %ld is too great %ld\n",
682 (long)l.sl_sd, (long)dtblsize ));
684 Debug( LDAP_DEBUG_ANY,
685 "daemon: listener descriptor %ld is too great %ld\n",
686 (long) l.sl_sd, (long) dtblsize, 0 );
688 tcp_close( l.sl_sd );
694 if ( (*sal)->sa_family == AF_LOCAL ) {
695 unlink ( ((struct sockaddr_un *)*sal)->sun_path );
700 /* enable address reuse */
702 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
703 (char *) &tmp, sizeof(tmp) );
704 if ( rc == AC_SOCKET_ERROR ) {
705 int err = sock_errno();
707 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
708 "slap_open_listener: setsockopt( %ld, SO_REUSEADDR ) failed errno %d (%s)\n",
709 (long)l.sl_sd, err, sock_errstr(err) ));
711 Debug( LDAP_DEBUG_ANY,
712 "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
713 (long) l.sl_sd, err, sock_errstr(err) );
719 switch( (*sal)->sa_family ) {
721 addrlen = sizeof(struct sockaddr_in);
725 addrlen = sizeof(struct sockaddr_in6);
730 addrlen = sizeof(struct sockaddr_un);
735 if (!bind(l.sl_sd, *sal, addrlen))
739 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
740 "slap_open_listener: bind(%ld) failed errno=%d (%s)\n",
741 (long)l.sl_sd, err, sock_errstr(err) ));
743 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
744 (long) l.sl_sd, err, sock_errstr(err) );
746 tcp_close( l.sl_sd );
748 } /* while ( *sal != NULL ) */
750 if ( *sal == NULL ) {
752 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
753 "slap_open_listener: bind(%ld) failed.\n", (long)l.sl_sd ));
755 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed\n",
756 (long) l.sl_sd, 0, 0 );
758 slap_free_listener_addresses(psal);
762 switch ( (*sal)->sa_family ) {
765 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
766 if ( chmod( addr, perms ) < 0 && crit ) {
767 int err = sock_errno();
769 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
770 "slap_open_listener: fchmod(%ld) failed errno=%d (%s)\n",
771 (long)l.sl_sd, err, sock_errstr(err) ));
773 Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
774 (long) l.sl_sd, err, sock_errstr(err) );
776 tcp_close( l.sl_sd );
777 slap_free_listener_addresses(psal);
780 l.sl_name = ch_malloc( strlen(addr) + sizeof("PATH=") );
781 sprintf( l.sl_name, "PATH=%s", addr );
783 #endif /* LDAP_PF_LOCAL */
787 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
788 char addr[INET_ADDRSTRLEN];
789 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
790 addr, sizeof(addr) );
793 s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr );
795 port = ntohs( ((struct sockaddr_in *)*sal) ->sin_port );
796 l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65535") );
797 sprintf( l.sl_name, "IP=%s:%d",
798 s != NULL ? s : "unknown" , port );
803 char addr[INET6_ADDRSTRLEN];
804 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
806 port = ntohs( ((struct sockaddr_in6 *)*sal)->sin6_port );
807 l.sl_name = ch_malloc( strlen(addr) + sizeof("IP= 65535") );
808 sprintf( l.sl_name, "IP=%s %d", addr, port );
810 #endif /* LDAP_PF_INET6 */
814 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
815 "slap_open_listener: unsupported address family (%d)\n",
816 (int)(*sal)->sa_family ));
818 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
819 (int) (*sal)->sa_family, 0, 0 );
824 slap_free_listener_addresses(psal);
826 l.sl_url = ch_strdup( url );
827 li = ch_malloc( sizeof( Listener ) );
831 LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
832 "slap_open_listener: daemon initialzed %s\n", l.sl_url ));
834 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
840 static int sockinit(void);
841 static int sockdestroy(void);
843 int slapd_daemon_init( const char *urls )
849 LDAP_LOG(( "connection", LDAP_LEVEL_ARGS,
850 "slapd_daemon_init: %s\n",
851 urls ? urls : "<null>" ));
853 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
854 urls ? urls : "<null>", 0, 0 );
856 if( (rc = sockinit()) != 0 ) {
861 dtblsize = sysconf( _SC_OPEN_MAX );
862 #elif HAVE_GETDTABLESIZE
863 dtblsize = getdtablesize();
865 dtblsize = FD_SETSIZE;
869 if(dtblsize > FD_SETSIZE) {
870 dtblsize = FD_SETSIZE;
872 #endif /* !FD_SETSIZE */
874 /* open a pipe (or something equivalent connected to itself).
875 * we write a byte on this fd whenever we catch a signal. The main
876 * loop will be select'ing on this socket, and will wake up when
879 if( (rc = lutil_pair( wake_sds )) < 0 ) {
881 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
882 "slap_daemon_init: lutil_pair() failed rc=%d\n", rc ));
884 Debug( LDAP_DEBUG_ANY,
885 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
890 FD_ZERO( &slap_daemon.sd_readers );
891 FD_ZERO( &slap_daemon.sd_writers );
897 u = str2charray( urls, " " );
899 if( u == NULL || u[0] == NULL ) {
901 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
902 "slap_daemon_init: no urls (%s) provided.\n", urls ));
904 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
910 for( i=0; u[i] != NULL; i++ ) {
912 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
913 "slap_daemon_init: listen on %s\n.", u[i] ));
915 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
922 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
923 "slap_daemon_init: no listeners to open (%s)\n", urls ));
925 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
933 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
934 "slap_daemon_init: %d listeners to open...\n", i ));
936 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
939 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
941 for(i = 0; u[i] != NULL; i++ ) {
942 slap_listeners[i] = slap_open_listener( u[i] );
944 if( slap_listeners[i] == NULL ) {
949 slap_listeners[i] = NULL;
952 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
953 "slap_daemon_init: %d listeners opened\n", i ));
955 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
960 slapd_slp_init( urls );
965 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
971 slapd_daemon_destroy(void)
973 connections_destroy();
974 tcp_close( wake_sds[1] );
975 tcp_close( wake_sds[0] );
994 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
996 slapd_remove( slap_listeners[l]->sl_sd, 0 );
997 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
999 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1000 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1002 #endif /* LDAP_PF_LOCAL */
1003 slapd_close( slap_listeners[l]->sl_sd );
1005 if ( slap_listeners[l]->sl_url )
1006 free ( slap_listeners[l]->sl_url );
1007 if ( slap_listeners[l]->sl_name )
1008 free ( slap_listeners[l]->sl_name );
1009 free ( slap_listeners[l] );
1010 slap_listeners[l] = NULL;
1021 time_t last_idle_check = 0;
1024 if ( global_idletimeout > 0 ) {
1025 last_idle_check = slap_get_time();
1027 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1028 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1030 #ifdef LDAP_CONNECTIONLESS
1031 /* Since this is connectionless, the data port is the
1032 * listening port. The listen() and accept() calls
1035 if ( slap_listeners[l]->sl_is_udp )
1037 slapd_add( slap_listeners[l]->sl_sd );
1042 if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN ) == -1 ) {
1043 int err = sock_errno();
1045 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1046 "slapd_daemon_task: listen( %s, 5 ) failed errno=%d (%s)\n",
1047 slap_listeners[l]->sl_url, err, sock_errstr(err) ));
1049 Debug( LDAP_DEBUG_ANY,
1050 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
1051 slap_listeners[l]->sl_url, err,
1054 return( (void*)-1 );
1057 slapd_add( slap_listeners[l]->sl_sd );
1060 #ifdef HAVE_NT_SERVICE_MANAGER
1061 if ( started_event != NULL ) {
1062 ldap_pvt_thread_cond_signal( &started_event );
1065 /* initialization complete. Here comes the loop. */
1067 while ( !slapd_shutdown ) {
1072 #define SLAPD_EBADF_LIMIT 16
1076 #define SLAPD_IDLE_CHECK_LIMIT 4
1084 #if defined(SLAPD_RLOOKUPS)
1087 struct timeval zero;
1088 struct timeval *tvp;
1091 now = slap_get_time();
1092 connections_timeout_idle( now );
1094 else if ( global_idletimeout > 0 ) {
1095 now = slap_get_time();
1096 if ( difftime( last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 ) {
1097 connections_timeout_idle( now );
1102 if( slapd_gentle_shutdown ) {
1103 ber_socket_t active;
1105 if( slapd_gentle_shutdown > 0 ) {
1106 Debug( LDAP_DEBUG_ANY, "slapd gentle shutdown\n", 0, 0, 0 );
1107 close_listeners( 1 );
1108 slapd_gentle_shutdown = -1;
1111 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1112 active = slap_daemon.sd_nactives;
1113 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1115 slapd_shutdown = -1;
1121 FD_ZERO( &writefds );
1122 FD_ZERO( &readfds );
1127 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1129 #ifdef FD_SET_MANUAL_COPY
1130 for( s = 0; s < nfds; s++ ) {
1131 if(FD_ISSET( &slap_sd_readers, s )) {
1132 FD_SET( s, &readfds );
1134 if(FD_ISSET( &slap_sd_writers, s )) {
1135 FD_SET( s, &writefds );
1139 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
1140 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
1142 assert(!FD_ISSET(wake_sds[0], &readfds));
1143 FD_SET( wake_sds[0], &readfds );
1145 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1146 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1148 if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
1149 FD_SET( slap_listeners[l]->sl_sd, &readfds );
1152 #ifndef HAVE_WINSOCK
1153 nfds = slap_daemon.sd_nfds;
1158 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1160 at = ldap_pvt_thread_pool_backload(&connection_pool);
1162 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
1165 tvp = at ? &zero : NULL;
1168 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1169 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1173 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1174 "slapd_daemon_task: select: listen=%d active_threads=%d tvp=%s\n",
1175 slap_listeners[l]->sl_sd, at, tvp == NULL ? "NULL" : "zero" ));
1177 Debug( LDAP_DEBUG_CONNS,
1178 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
1179 slap_listeners[l]->sl_sd, at,
1180 tvp == NULL ? "NULL" : "zero" );
1184 switch(ns = select( nfds, &readfds,
1186 /* don't pass empty fd_set */
1187 ( writefds.fd_count > 0 ? &writefds : NULL ),
1193 case -1: { /* failure - try again */
1194 int err = sock_errno();
1198 /* you'd think this would be EBADF */
1199 || err == WSAENOTSOCK
1202 if (++ebadf < SLAPD_EBADF_LIMIT)
1206 if( err != EINTR ) {
1208 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1209 "slapd_daemon_task: select failed (%d): %s\n",
1210 err, sock_errstr(err) ));
1212 Debug( LDAP_DEBUG_CONNS,
1213 "daemon: select failed (%d): %s\n",
1214 err, sock_errstr(err), 0 );
1221 case 0: /* timeout - let threads run */
1224 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1225 "slapd_daemon_task: select timeout - yielding\n" ));
1227 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
1230 ldap_pvt_thread_yield();
1233 default: /* something happened - deal with it */
1234 if( slapd_shutdown ) continue;
1238 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1239 "slapd_daemon_task: activity on %d descriptors\n", ns ));
1241 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
1247 if( FD_ISSET( wake_sds[0], &readfds ) ) {
1249 tcp_read( wake_sds[0], c, sizeof(c) );
1256 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1258 socklen_t len = sizeof(from);
1261 char *authid = NULL;
1263 char *dnsname = NULL;
1265 #ifdef LDAP_PF_LOCAL
1266 char peername[MAXPATHLEN + sizeof("PATH=")];
1267 #elif defined(LDAP_PF_INET6)
1268 char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
1270 char peername[sizeof("IP=255.255.255.255:65336")];
1271 #endif /* LDAP_PF_LOCAL */
1275 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1278 if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
1281 #ifdef LDAP_CONNECTIONLESS
1282 if ( slap_listeners[l]->sl_is_udp )
1284 /* The first time we receive a query, we set this
1285 * up as a "connection". It remains open for the life
1288 if ( slap_listeners[l]->sl_is_udp < 2 )
1290 id = connection_init(
1291 slap_listeners[l]->sl_sd,
1292 slap_listeners[l]->sl_url, "", "",
1293 slap_listeners[l]->sl_name,
1295 slap_listeners[l]->sl_is_udp++;
1301 s = accept( slap_listeners[l]->sl_sd,
1302 (struct sockaddr *) &from, &len );
1303 if ( s == AC_SOCKET_INVALID ) {
1304 int err = sock_errno();
1307 if( err == EMFILE ) {
1312 if( err == ENFILE ) {
1322 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1323 "slapd_daemon_task: accept(%ld) failed errno=%d (%s)\n",
1324 (long)slap_listeners[l]->sl_sd, err, sock_errstr(err) ));
1326 Debug( LDAP_DEBUG_ANY,
1327 "daemon: accept(%ld) failed errno=%d (%s)\n",
1328 (long) slap_listeners[l]->sl_sd, err,
1332 /* prevent busy loop */
1334 if( emfile % 4 == 3 ) usleep( 250 );
1336 if( emfile % 8 == 7 ) sleep( 1 );
1340 ldap_pvt_thread_yield();
1345 #ifndef HAVE_WINSOCK
1346 /* make sure descriptor number isn't too great */
1347 if ( s >= dtblsize ) {
1349 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1350 "slapd_daemon_task: %ld beyond descriptor table size %ld\n",
1351 (long)s, (long)dtblsize ));
1353 Debug( LDAP_DEBUG_ANY,
1354 "daemon: %ld beyond descriptor table size %ld\n",
1355 (long) s, (long) dtblsize, 0 );
1359 ldap_pvt_thread_yield();
1365 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1367 /* newly accepted stream should not be in any of the FD SETS */
1368 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
1369 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
1370 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
1372 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1375 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1376 #ifdef LDAP_PF_LOCAL
1377 /* for IPv4 and IPv6 sockets only */
1378 if ( from.sa_addr.sa_family != AF_LOCAL )
1379 #endif /* LDAP_PF_LOCAL */
1384 /* enable keep alives */
1386 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1387 (char *) &tmp, sizeof(tmp) );
1388 if ( rc == AC_SOCKET_ERROR ) {
1389 int err = sock_errno();
1391 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1392 "slapd_daemon_task: setsockopt( %ld, SO_KEEPALIVE) failed errno=%d (%s)\n",
1393 (long)s, err, sock_errstr(err) ));
1395 Debug( LDAP_DEBUG_ANY,
1396 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1397 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1402 /* enable no delay */
1404 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1405 (char *)&tmp, sizeof(tmp) );
1406 if ( rc == AC_SOCKET_ERROR ) {
1407 int err = sock_errno();
1409 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1410 "slapd_daemon_task: setsockopt( %ld, TCP_NODELAY) failed errno=%d (%s)\n",
1411 (long)s, err, sock_errstr(err) ));
1413 Debug( LDAP_DEBUG_ANY,
1414 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1415 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1423 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1424 "slapd_daemon_task: new connection on %ld\n", (long)s ));
1426 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
1429 switch ( from.sa_addr.sa_family ) {
1430 # ifdef LDAP_PF_LOCAL
1432 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1433 ssf = LDAP_PVT_SASL_LOCAL_SSF;
1436 #endif /* LDAP_PF_LOCAL */
1438 # ifdef LDAP_PF_INET6
1440 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1441 peeraddr = inet_ntoa( *((struct in_addr *)
1442 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1443 sprintf( peername, "IP=%s:%d",
1444 peeraddr != NULL ? peeraddr : "unknown",
1445 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1447 char addr[INET6_ADDRSTRLEN];
1448 sprintf( peername, "IP=%s %d",
1449 inet_ntop( AF_INET6,
1450 &from.sa_in6_addr.sin6_addr,
1451 addr, sizeof addr) ? addr : "unknown",
1452 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1455 # endif /* LDAP_PF_INET6 */
1458 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1459 sprintf( peername, "IP=%s:%d",
1460 peeraddr != NULL ? peeraddr : "unknown",
1461 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1469 if ( ( from.sa_addr.sa_family == AF_INET )
1470 #ifdef LDAP_PF_INET6
1471 || ( from.sa_addr.sa_family == AF_INET6 )
1474 #ifdef SLAPD_RLOOKUPS
1475 if ( use_reverse_lookup ) {
1476 # ifdef LDAP_PF_INET6
1477 if ( from.sa_addr.sa_family == AF_INET6 )
1479 (char *)&(from.sa_in6_addr.sin6_addr),
1480 sizeof(from.sa_in6_addr.sin6_addr),
1483 # endif /* LDAP_PF_INET6 */
1485 (char *) &(from.sa_in_addr.sin_addr),
1486 sizeof(from.sa_in_addr.sin_addr),
1488 dnsname = hp ? ldap_pvt_str2lower( hp->h_name ) : NULL;
1492 #endif /* SLAPD_RLOOKUPS */
1495 if ( !hosts_ctl("slapd",
1496 dnsname != NULL ? dnsname : STRING_UNKNOWN,
1497 peeraddr != NULL ? peeraddr : STRING_UNKNOWN,
1501 Statslog( LDAP_DEBUG_ANY,
1502 "fd=%ld host access from %s (%s) denied.\n",
1504 dnsname != NULL ? dnsname : "unknown",
1505 peeraddr != NULL ? peeraddr : "unknown",
1510 #endif /* HAVE_TCPD */
1513 id = connection_init(s,
1514 slap_listeners[l]->sl_url,
1515 dnsname != NULL ? dnsname : "unknown",
1517 slap_listeners[l]->sl_name,
1519 slap_listeners[l]->sl_is_tls,
1526 if( authid ) ch_free(authid);
1530 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1531 "slapd_daemon_task: connection_init(%ld, %s, %s) failed.\n",
1532 (long)s, peername, slap_listeners[l]->sl_name ));
1534 Debug( LDAP_DEBUG_ANY,
1535 "daemon: connection_init(%ld, %s, %s) failed.\n",
1538 slap_listeners[l]->sl_name );
1544 Statslog( LDAP_DEBUG_STATS,
1545 "daemon: conn=%ld fd=%ld connection from %s (%s) accepted.\n",
1548 slap_listeners[l]->sl_name,
1557 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1558 "slapd_daemon_task: activity on " ));
1560 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1563 for ( i = 0; i < readfds.fd_count; i++ ) {
1565 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1566 " %d%s", readfds.fd_array[i], "r", 0 ));
1568 Debug( LDAP_DEBUG_CONNS, " %d%s",
1569 readfds.fd_array[i], "r", 0 );
1572 for ( i = 0; i < writefds.fd_count; i++ ) {
1574 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1575 " %d%s", writefds.fd_array[i], "w" ));
1577 Debug( LDAP_DEBUG_CONNS, " %d%s",
1578 writefds.fd_array[i], "w", 0 );
1583 for ( i = 0; i < nfds; i++ ) {
1585 int is_listener = 0;
1587 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1588 if ( i == slap_listeners[l]->sl_sd ) {
1589 #ifdef LDAP_CONNECTIONLESS
1590 /* The listener is the data port. Don't
1593 if (slap_listeners[l]->sl_is_udp)
1600 if ( is_listener ) {
1603 r = FD_ISSET( i, &readfds );
1604 w = FD_ISSET( i, &writefds );
1607 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1609 r ? "r" : "", w ? "w" : "" ));
1611 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1612 r ? "r" : "", w ? "w" : "" );
1618 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2, "\n" ));
1620 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1625 /* loop through the writers */
1627 for ( i = 0; i < writefds.fd_count; i++ )
1629 for ( i = 0; i < nfds; i++ )
1633 int is_listener = 0;
1635 wd = writefds.fd_array[i];
1637 if( ! FD_ISSET( i, &writefds ) ) {
1643 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1644 if ( i == slap_listeners[l]->sl_sd ) {
1645 #ifdef LDAP_CONNECTIONLESS
1646 if (slap_listeners[l]->sl_is_udp)
1653 if ( is_listener ) {
1657 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1658 "slapd_daemon_task: write active on %d\n", wd ));
1660 Debug( LDAP_DEBUG_CONNS,
1661 "daemon: write active on %d\n",
1665 * NOTE: it is possible that the connection was closed
1666 * and that the stream is now inactive.
1667 * connection_write() must valid the stream is still
1671 if ( connection_write( wd ) < 0 ) {
1672 FD_CLR( (unsigned) wd, &readfds );
1678 for ( i = 0; i < readfds.fd_count; i++ )
1680 for ( i = 0; i < nfds; i++ )
1684 int is_listener = 0;
1687 rd = readfds.fd_array[i];
1689 if( ! FD_ISSET( i, &readfds ) ) {
1695 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1696 if ( rd == slap_listeners[l]->sl_sd ) {
1697 #ifdef LDAP_CONNECTIONLESS
1698 if (slap_listeners[l]->sl_is_udp)
1705 if ( is_listener ) {
1710 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1711 "slapd_daemon_task: read activity on %d\n", rd ));
1713 Debug ( LDAP_DEBUG_CONNS,
1714 "daemon: read activity on %d\n", rd, 0, 0 );
1717 * NOTE: it is possible that the connection was closed
1718 * and that the stream is now inactive.
1719 * connection_read() must valid the stream is still
1723 if ( connection_read( rd ) < 0 ) {
1727 ldap_pvt_thread_yield();
1730 if( slapd_shutdown == 1 ) {
1732 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1733 "slapd_daemon_task: shutdown requested and initiated.\n"));
1735 Debug( LDAP_DEBUG_TRACE,
1736 "daemon: shutdown requested and initiated.\n",
1740 } else if ( slapd_shutdown == 2 ) {
1741 #ifdef HAVE_NT_SERVICE_MANAGER
1743 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1744 "slapd_daemon_task: shutdown initiated by Service Manager.\n"));
1746 Debug( LDAP_DEBUG_TRACE,
1747 "daemon: shutdown initiated by Service Manager.\n",
1750 #else /* !HAVE_NT_SERVICE_MANAGER */
1752 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1753 "slapd_daemon_task: abnormal condition, shutdown initiated.\n" ));
1755 Debug( LDAP_DEBUG_TRACE,
1756 "daemon: abnormal condition, shutdown initiated.\n",
1759 #endif /* !HAVE_NT_SERVICE_MANAGER */
1762 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1763 "slapd_daemon_task: no active streams, shutdown initiated.\n" ));
1765 Debug( LDAP_DEBUG_TRACE,
1766 "daemon: no active streams, shutdown initiated.\n",
1771 if( slapd_gentle_shutdown >= 0 )
1772 close_listeners ( 0 );
1773 free ( slap_listeners );
1774 slap_listeners = NULL;
1777 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1778 "slapd_daemon_task: shutdown waiting for %d threads to terminate.\n",
1779 ldap_pvt_thread_pool_backload(&connection_pool) ));
1781 Debug( LDAP_DEBUG_ANY,
1782 "slapd shutdown: waiting for %d threads to terminate\n",
1783 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1785 ldap_pvt_thread_pool_destroy(&connection_pool, 1);
1791 int slapd_daemon( void )
1797 #define SLAPD_LISTENER_THREAD 1
1798 #if defined( SLAPD_LISTENER_THREAD )
1800 ldap_pvt_thread_t listener_tid;
1802 /* listener as a separate THREAD */
1803 rc = ldap_pvt_thread_create( &listener_tid,
1804 0, slapd_daemon_task, NULL );
1808 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1809 "slapd_daemon: listener ldap_pvt_thread_create failed (%d).\n", rc ));
1811 Debug( LDAP_DEBUG_ANY,
1812 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
1817 /* wait for the listener thread to complete */
1818 ldap_pvt_thread_join( listener_tid, (void *) NULL );
1821 /* experimental code */
1822 slapd_daemon_task( NULL );
1831 #if defined( HAVE_WINSOCK2 )
1832 WORD wVersionRequested;
1836 wVersionRequested = MAKEWORD( 2, 0 );
1838 err = WSAStartup( wVersionRequested, &wsaData );
1840 /* Tell the user that we couldn't find a usable */
1845 /* Confirm that the WinSock DLL supports 2.0.*/
1846 /* Note that if the DLL supports versions greater */
1847 /* than 2.0 in addition to 2.0, it will still return */
1848 /* 2.0 in wVersion since that is the version we */
1851 if ( LOBYTE( wsaData.wVersion ) != 2 ||
1852 HIBYTE( wsaData.wVersion ) != 0 )
1854 /* Tell the user that we couldn't find a usable */
1860 /* The WinSock DLL is acceptable. Proceed. */
1861 #elif defined( HAVE_WINSOCK )
1863 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
1870 int sockdestroy(void)
1872 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
1879 slap_sig_shutdown( int sig )
1882 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1883 "slap_sig_shutdown: signal %d\n", sig ));
1885 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
1889 * If the NT Service Manager is controlling the server, we don't
1890 * want SIGBREAK to kill the server. For some strange reason,
1891 * SIGBREAK is generated when a user logs out.
1894 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
1895 if (is_NT_Service && sig == SIGBREAK)
1897 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1898 "slap_sig_shutdown: SIGBREAK ignored.\n" ));
1900 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: SIGBREAK ignored.\n",
1906 if (sig == SIGHUP && global_gentlehup && slapd_gentle_shutdown == 0)
1907 slapd_gentle_shutdown = 1;
1914 /* reinstall self */
1915 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
1919 slap_sig_wake( int sig )
1923 /* reinstall self */
1924 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
1928 void slapd_add_internal(ber_socket_t s) {
1932 Listener ** slapd_get_listeners(void) {
1933 return slap_listeners;