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 struct slap_listener {
44 #ifdef LDAP_CONNECTIONLESS
45 int sl_is_udp; /* UDP listener is also data port */
49 #define sl_addr sl_sa.sa_in_addr
52 Listener **slap_listeners = NULL;
54 #define SLAPD_LISTEN 10
56 static ber_socket_t wake_sds[2];
60 #define WAKE_LISTENER(w) \
61 ((w && !waking) ? tcp_write( wake_sds[1], "0", 1 ), waking=1 : 0)
63 #define WAKE_LISTENER(w) \
64 do { if (w) tcp_write( wake_sds[1], "0", 1 ); } while(0)
67 #ifdef HAVE_NT_SERVICE_MANAGER
69 extern ldap_pvt_thread_cond_t started_event;
70 extern int is_NT_Service;
76 volatile sig_atomic_t slapd_shutdown = 0;
78 static struct slap_daemon {
79 ldap_pvt_thread_mutex_t sd_mutex;
84 /* In winsock, accept() returns values higher than dtblsize
85 so don't bother with this optimization */
98 * SLP related functions
102 #define LDAP_SRVTYPE_PREFIX "service:ldap://"
103 #define LDAPS_SRVTYPE_PREFIX "service:ldaps://"
104 static char** slapd_srvurls = NULL;
105 static SLPHandle slapd_hslp = 0;
107 void slapd_slp_init( const char* urls ) {
110 slapd_srvurls = str2charray( urls, " " );
112 if( slapd_srvurls == NULL ) return;
114 /* find and expand INADDR_ANY URLs */
115 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
116 if( strcmp( slapd_srvurls[i], "ldap:///" ) == 0) {
117 char *host = ldap_pvt_get_fqdn( NULL );
118 if ( host != NULL ) {
119 slapd_srvurls[i] = (char *) realloc( slapd_srvurls[i],
121 sizeof( LDAP_SRVTYPE_PREFIX ) );
122 strcpy( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX );
123 strcat( slapd_srvurls[i], host );
128 } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0) {
129 char *host = ldap_pvt_get_fqdn( NULL );
130 if ( host != NULL ) {
131 slapd_srvurls[i] = (char *) realloc( slapd_srvurls[i],
133 sizeof( LDAPS_SRVTYPE_PREFIX ) );
134 strcpy( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX );
135 strcat( slapd_srvurls[i], host );
142 /* open the SLP handle */
143 SLPOpen( "en", 0, &slapd_hslp );
146 void slapd_slp_deinit() {
147 if( slapd_srvurls == NULL ) return;
149 charray_free( slapd_srvurls );
150 slapd_srvurls = NULL;
152 /* close the SLP handle */
153 SLPClose( slapd_hslp );
156 void slapd_slp_regreport(
164 void slapd_slp_reg() {
167 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
168 if( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
169 sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
170 strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
171 sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
175 SLP_LIFETIME_MAXIMUM,
185 void slapd_slp_dereg() {
188 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
189 SLPDereg( slapd_hslp,
195 #endif /* HAVE_SLP */
198 * Add a descriptor to daemon control
200 static void slapd_add(ber_socket_t s) {
201 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
203 assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
204 assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
205 assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
208 if (s >= slap_daemon.sd_nfds) {
209 slap_daemon.sd_nfds = s + 1;
213 FD_SET( s, &slap_daemon.sd_actives );
214 FD_SET( s, &slap_daemon.sd_readers );
217 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
218 "slapd_add: added %ld%s%s\n",
220 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
221 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" ));
223 Debug( LDAP_DEBUG_CONNS, "daemon: added %ld%s%s\n",
225 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
226 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
228 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
232 * Remove the descriptor from daemon control
234 void slapd_remove(ber_socket_t s, int wake) {
235 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
238 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
239 "slapd_remove: removing %ld%s%s\n",
241 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
242 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" ));
244 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
246 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
247 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
249 FD_CLR( s, &slap_daemon.sd_actives );
250 FD_CLR( s, &slap_daemon.sd_readers );
251 FD_CLR( s, &slap_daemon.sd_writers );
253 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
257 void slapd_clr_write(ber_socket_t s, int wake) {
258 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
260 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
261 FD_CLR( s, &slap_daemon.sd_writers );
263 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
267 void slapd_set_write(ber_socket_t s, int wake) {
268 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
270 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
271 if (!FD_ISSET(s, &slap_daemon.sd_writers))
272 FD_SET( (unsigned) s, &slap_daemon.sd_writers );
274 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
278 void slapd_clr_read(ber_socket_t s, int wake) {
279 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
281 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
282 FD_CLR( s, &slap_daemon.sd_readers );
284 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
288 void slapd_set_read(ber_socket_t s, int wake) {
289 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
291 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
292 if (!FD_ISSET(s, &slap_daemon.sd_readers))
293 FD_SET( s, &slap_daemon.sd_readers );
295 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
299 static void slapd_close(ber_socket_t s) {
301 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
302 "slapd_close: closing %ld\n", (long)s ));
304 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
310 static void slap_free_listener_addresses(struct sockaddr **sal)
312 struct sockaddr **sap;
318 for (sap = sal; *sap != NULL; sap++) {
325 /* port = 0 indicates AF_LOCAL */
326 static int slap_get_listener_addresses(
329 struct sockaddr ***sal)
331 struct sockaddr **sap;
335 *sal = ch_malloc(2 * sizeof(void *));
341 *sap = ch_malloc(sizeof(struct sockaddr_un));
347 (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) ) {
349 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
350 "slap_get_listener_addresses: domain socket path (%s) too long in URL\n",
353 Debug( LDAP_DEBUG_ANY,
354 "daemon: domain socket path (%s) too long in URL",
360 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
361 (*sap)->sa_family = AF_LOCAL;
362 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
366 #ifdef HAVE_GETADDRINFO
367 struct addrinfo hints, *res, *sai;
371 memset( &hints, '\0', sizeof(hints) );
372 hints.ai_flags = AI_PASSIVE;
373 hints.ai_socktype = SOCK_STREAM;
374 hints.ai_family = AF_UNSPEC;
375 snprintf(serv, sizeof serv, "%d", port);
377 if (err = getaddrinfo(host, serv, &hints, &res)) {
379 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
380 "slap_get_listener_addresses: getaddrinfo failed: %s\n",
381 AC_GAI_STRERROR(err) ));
383 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
384 AC_GAI_STRERROR(err), 0, 0);
390 for (n=2; (sai = sai->ai_next) != NULL; n++) {
393 *sal = ch_malloc(n * sizeof(void *));
401 for ( sai=res; sai; sai=sai->ai_next ) {
402 if( sai->ai_addr == NULL ) {
407 switch (sai->ai_family) {
408 # ifdef LDAP_PF_INET6
410 *sap = ch_malloc(sizeof(struct sockaddr_in6));
415 *(struct sockaddr_in6 *)*sap =
416 *((struct sockaddr_in6 *)sai->ai_addr);
420 *sap = ch_malloc(sizeof(struct sockaddr_in));
425 *(struct sockaddr_in *)*sap =
426 *((struct sockaddr_in *)sai->ai_addr);
433 (*sap)->sa_family = sai->ai_family;
443 if ( host == NULL ) {
444 in.s_addr = htonl(INADDR_ANY);
446 } else if ( !inet_aton( host, &in ) ) {
447 struct hostent *he = gethostbyname( host );
450 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
451 "slap_get_listener_addresses: invalid host %s\n",
454 Debug( LDAP_DEBUG_ANY,
455 "daemon: invalid host %s", host, 0, 0);
459 AC_MEMCPY( &in, he->h_addr, sizeof( in ) );
462 *sal = ch_malloc(2 * sizeof(void *));
468 *sap = ch_malloc(sizeof(struct sockaddr_in));
474 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_in) );
475 (*sap)->sa_family = AF_INET;
476 ((struct sockaddr_in *)*sap)->sin_port = htons(port);
477 ((struct sockaddr_in *)*sap)->sin_addr = in;
484 slap_free_listener_addresses(*sal);
488 static Listener * slap_open_listener(
497 struct sockaddr **sal, **psal;
498 int socktype = SOCK_STREAM; /* default to COTS */
500 rc = ldap_url_parse( url, &lud );
502 if( rc != LDAP_URL_SUCCESS ) {
504 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
505 "slap_open_listener: listen URL \"%s\" parse error %d\n",
508 Debug( LDAP_DEBUG_ANY,
509 "daemon: listen URL \"%s\" parse error=%d\n",
516 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
518 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
519 "slap_open_listener: TLS is not supported (%s)\n",
522 Debug( LDAP_DEBUG_ANY,
523 "daemon: TLS not supported (%s)\n",
526 ldap_free_urldesc( lud );
530 if(! lud->lud_port ) {
531 lud->lud_port = LDAP_PORT;
535 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
537 if(! lud->lud_port ) {
538 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
542 port = (unsigned short) lud->lud_port;
544 tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme);
545 if ( tmp == LDAP_PROTO_IPC ) {
547 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
548 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
550 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
555 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
556 "slap_open_listener: URL scheme is not supported: %s\n",
559 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
562 ldap_free_urldesc( lud );
566 #ifdef LDAP_CONNECTIONLESS
567 if ( tmp == LDAP_PROTO_UDP )
570 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
571 || strcmp(lud->lud_host, "*") == 0 )
573 err = slap_get_listener_addresses(NULL, port, &sal);
575 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
579 ldap_free_urldesc( lud );
585 while ( *sal != NULL ) {
586 switch( (*sal)->sa_family ) {
599 #ifdef LDAP_CONNECTIONLESS
601 socktype = SOCK_DGRAM;
603 l.sl_sd = socket( (*sal)->sa_family, socktype, 0);
604 if ( l.sl_sd == AC_SOCKET_INVALID ) {
605 int err = sock_errno();
607 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
608 "slap_open_listener: socket() failed errno=%d (%s)\n",
609 err, sock_errstr(err) ));
611 Debug( LDAP_DEBUG_ANY,
612 "daemon: socket() failed errno=%d (%s)\n", err,
613 sock_errstr(err), 0 );
619 if ( l.sl_sd >= dtblsize ) {
621 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
622 "slap_open_listener: listener descriptor %ld is too great %ld\n",
623 (long)l.sl_sd, (long)dtblsize ));
625 Debug( LDAP_DEBUG_ANY,
626 "daemon: listener descriptor %ld is too great %ld\n",
627 (long) l.sl_sd, (long) dtblsize, 0 );
629 tcp_close( l.sl_sd );
635 if ( (*sal)->sa_family == AF_LOCAL ) {
636 unlink ( ((struct sockaddr_un *)*sal)->sun_path );
641 /* enable address reuse */
643 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
644 (char *) &tmp, sizeof(tmp) );
645 if ( rc == AC_SOCKET_ERROR ) {
646 int err = sock_errno();
648 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
649 "slap_open_listener: setsockopt( %ld, SO_REUSEADDR ) failed errno %d (%s)\n",
650 (long)l.sl_sd, err, sock_errstr(err) ));
652 Debug( LDAP_DEBUG_ANY,
653 "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
654 (long) l.sl_sd, err, sock_errstr(err) );
660 switch( (*sal)->sa_family ) {
662 addrlen = sizeof(struct sockaddr_in);
666 addrlen = sizeof(struct sockaddr_in6);
671 addrlen = sizeof(struct sockaddr_un);
676 if (!bind(l.sl_sd, *sal, addrlen))
680 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
681 "slap_open_listener: bind(%ld) failed errno=%d (%s)\n",
682 (long)l.sl_sd, err, sock_errstr(err) ));
684 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
685 (long) l.sl_sd, err, sock_errstr(err) );
687 tcp_close( l.sl_sd );
689 } /* while ( *sal != NULL ) */
691 if ( *sal == NULL ) {
693 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
694 "slap_open_listener: bind(%ld) failed.\n", (long)l.sl_sd ));
696 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed\n",
697 (long) l.sl_sd, 0, 0 );
699 slap_free_listener_addresses(psal);
703 switch ( (*sal)->sa_family ) {
706 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
707 if ( chmod( addr, S_IRWXU ) < 0 ) {
708 int err = sock_errno();
710 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
711 "slap_open_listener: fchmod(%ld) failed errno=%d (%s)\n",
712 (long)l.sl_sd, err, sock_errstr(err) ));
714 Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
715 (long) l.sl_sd, err, sock_errstr(err) );
717 tcp_close( l.sl_sd );
718 slap_free_listener_addresses(psal);
721 l.sl_name = ch_malloc( strlen(addr) + sizeof("PATH=") );
722 sprintf( l.sl_name, "PATH=%s", addr );
724 #endif /* LDAP_PF_LOCAL */
728 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
729 char addr[INET_ADDRSTRLEN];
730 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
731 addr, sizeof(addr) );
733 port = ((struct sockaddr_in *)*sal) ->sin_port;
735 s = inet_ntoa( l.sl_addr.sin_addr );
736 port = l.sl_addr.sin_port;
738 l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65535") );
739 sprintf( l.sl_name, "IP=%s:%d",
740 s != NULL ? s : "unknown" , port );
745 char addr[INET6_ADDRSTRLEN];
746 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
748 port = ((struct sockaddr_in6 *)*sal)->sin6_port;
749 l.sl_name = ch_malloc( strlen(addr) + sizeof("IP= 65535") );
750 sprintf( l.sl_name, "IP=%s %d", addr, port );
752 #endif /* LDAP_PF_INET6 */
756 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
757 "slap_open_listener: unsupported address family (%d)\n",
758 (int)(*sal)->sa_family ));
760 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
761 (int) (*sal)->sa_family, 0, 0 );
766 slap_free_listener_addresses(psal);
768 l.sl_url = ch_strdup( url );
769 li = ch_malloc( sizeof( Listener ) );
773 LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
774 "slap_open_listener: daemon initialzed %s\n", l.sl_url ));
776 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
782 static int sockinit(void);
783 static int sockdestroy(void);
785 int slapd_daemon_init( const char *urls )
791 LDAP_LOG(( "connection", LDAP_LEVEL_ARGS,
792 "slapd_daemon_init: %s\n",
793 urls ? urls : "<null>" ));
795 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
796 urls ? urls : "<null>", 0, 0 );
798 if( (rc = sockinit()) != 0 ) {
803 dtblsize = sysconf( _SC_OPEN_MAX );
804 #elif HAVE_GETDTABLESIZE
805 dtblsize = getdtablesize();
807 dtblsize = FD_SETSIZE;
811 if(dtblsize > FD_SETSIZE) {
812 dtblsize = FD_SETSIZE;
814 #endif /* !FD_SETSIZE */
816 /* open a pipe (or something equivalent connected to itself).
817 * we write a byte on this fd whenever we catch a signal. The main
818 * loop will be select'ing on this socket, and will wake up when
821 if( (rc = lutil_pair( wake_sds )) < 0 ) {
823 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
824 "slap_daemon_init: lutil_pair() failed rc=%d\n", rc ));
826 Debug( LDAP_DEBUG_ANY,
827 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
832 FD_ZERO( &slap_daemon.sd_readers );
833 FD_ZERO( &slap_daemon.sd_writers );
839 u = str2charray( urls, " " );
841 if( u == NULL || u[0] == NULL ) {
843 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
844 "slap_daemon_init: no urls (%s) provided.\n", urls ));
846 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
852 for( i=0; u[i] != NULL; i++ ) {
854 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
855 "slap_daemon_init: listen on %s\n.", u[i] ));
857 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
864 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
865 "slap_daemon_init: no listeners to open (%s)\n", urls ));
867 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
875 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
876 "slap_daemon_init: %d listeners to open...\n", i ));
878 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
881 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
883 for(i = 0; u[i] != NULL; i++ ) {
884 slap_listeners[i] = slap_open_listener( u[i] );
886 if( slap_listeners[i] == NULL ) {
891 slap_listeners[i] = NULL;
894 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
895 "slap_daemon_init: %d listeners opened\n", i ));
897 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
902 slapd_slp_init( urls );
907 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
913 slapd_daemon_destroy(void)
915 connections_destroy();
916 tcp_close( wake_sds[1] );
917 tcp_close( wake_sds[0] );
935 time_t last_idle_check = slap_get_time();
938 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
939 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
941 #ifdef LDAP_CONNECTIONLESS
942 /* Since this is connectionless, the data port is the
943 * listening port. The listen() and accept() calls
946 if ( slap_listeners[l]->sl_is_udp )
948 slapd_add( slap_listeners[l]->sl_sd );
953 if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN ) == -1 ) {
954 int err = sock_errno();
956 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
957 "slapd_daemon_task: listen( %s, 5 ) failed errno=%d (%s)\n",
958 slap_listeners[l]->sl_url, err, sock_errstr(err) ));
960 Debug( LDAP_DEBUG_ANY,
961 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
962 slap_listeners[l]->sl_url, err,
968 slapd_add( slap_listeners[l]->sl_sd );
971 #ifdef HAVE_NT_SERVICE_MANAGER
972 if ( started_event != NULL ) {
973 ldap_pvt_thread_cond_signal( &started_event );
976 /* initialization complete. Here comes the loop. */
978 while ( !slapd_shutdown ) {
983 #define SLAPD_EBADF_LIMIT 16
986 #define SLAPD_IDLE_CHECK_LIMIT 4
987 time_t now = slap_get_time();
994 #if defined(SLAPD_RLOOKUPS)
1000 if( global_idletimeout > 0 && difftime(
1001 last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT,
1004 connections_timeout_idle(now);
1007 FD_ZERO( &writefds );
1008 FD_ZERO( &readfds );
1013 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1015 #ifdef FD_SET_MANUAL_COPY
1016 for( s = 0; s < nfds; s++ ) {
1017 if(FD_ISSET( &slap_sd_readers, s )) {
1018 FD_SET( s, &readfds );
1020 if(FD_ISSET( &slap_sd_writers, s )) {
1021 FD_SET( s, &writefds );
1025 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
1026 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
1028 assert(!FD_ISSET(wake_sds[0], &readfds));
1029 FD_SET( wake_sds[0], &readfds );
1031 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1032 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1034 if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
1035 FD_SET( slap_listeners[l]->sl_sd, &readfds );
1038 #ifndef HAVE_WINSOCK
1039 nfds = slap_daemon.sd_nfds;
1044 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1046 at = ldap_pvt_thread_pool_backload(&connection_pool);
1048 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
1051 tvp = at ? &zero : NULL;
1054 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1055 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1059 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1060 "slapd_daemon_task: select: listen=%d active_threads=%d tvp=%s\n",
1061 slap_listeners[l]->sl_sd, at, tvp == NULL ? "NULL" : "zero" ));
1063 Debug( LDAP_DEBUG_CONNS,
1064 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
1065 slap_listeners[l]->sl_sd, at,
1066 tvp == NULL ? "NULL" : "zero" );
1070 switch(ns = select( nfds, &readfds,
1072 /* don't pass empty fd_set */
1073 ( writefds.fd_count > 0 ? &writefds : NULL ),
1079 case -1: { /* failure - try again */
1080 int err = sock_errno();
1084 /* you'd think this would be EBADF */
1085 || err == WSAENOTSOCK
1088 if (++ebadf < SLAPD_EBADF_LIMIT)
1092 if( err != EINTR ) {
1094 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1095 "slapd_daemon_task: select failed (%d): %s\n",
1096 err, sock_errstr(err) ));
1098 Debug( LDAP_DEBUG_CONNS,
1099 "daemon: select failed (%d): %s\n",
1100 err, sock_errstr(err), 0 );
1102 slapd_shutdown = -1;
1107 case 0: /* timeout - let threads run */
1110 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1111 "slapd_daemon_task: select timeout - yielding\n" ));
1113 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
1116 ldap_pvt_thread_yield();
1119 default: /* something happened - deal with it */
1120 if( slapd_shutdown ) continue;
1124 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1125 "slapd_daemon_task: activity on %d descriptors\n", ns ));
1127 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
1133 if( FD_ISSET( wake_sds[0], &readfds ) ) {
1135 tcp_read( wake_sds[0], c, sizeof(c) );
1142 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1144 socklen_t len = sizeof(from);
1147 char *authid = NULL;
1151 #ifdef LDAP_PF_LOCAL
1152 char peername[MAXPATHLEN + sizeof("PATH=")];
1153 #elif defined(LDAP_PF_INET6)
1154 char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
1156 char peername[sizeof("IP=255.255.255.255:65336")];
1157 #endif /* LDAP_PF_LOCAL */
1161 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1164 if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
1167 #ifdef LDAP_CONNECTIONLESS
1168 if ( slap_listeners[l]->sl_is_udp )
1170 /* The first time we receive a query, we set this
1171 * up as a "connection". It remains open for the life
1174 if ( slap_listeners[l]->sl_is_udp < 2 )
1176 id = connection_init(
1177 slap_listeners[l]->sl_sd,
1178 slap_listeners[l]->sl_url, "", "",
1179 slap_listeners[l]->sl_name,
1181 slap_listeners[l]->sl_is_udp++;
1187 s = accept( slap_listeners[l]->sl_sd,
1188 (struct sockaddr *) &from, &len );
1189 if ( s == AC_SOCKET_INVALID ) {
1190 int err = sock_errno();
1192 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1193 "slapd_daemon_task: accept(%ld) failed errno=%d (%s)\n",
1194 (long)slap_listeners[l]->sl_sd, err, sock_errstr(err) ));
1196 Debug( LDAP_DEBUG_ANY,
1197 "daemon: accept(%ld) failed errno=%d (%s)\n",
1198 (long) slap_listeners[l]->sl_sd, err,
1201 ldap_pvt_thread_yield();
1205 #ifndef HAVE_WINSOCK
1206 /* make sure descriptor number isn't too great */
1207 if ( s >= dtblsize ) {
1209 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1210 "slapd_daemon_task: %ld beyond descriptor table size %ld\n",
1211 (long)s, (long)dtblsize ));
1213 Debug( LDAP_DEBUG_ANY,
1214 "daemon: %ld beyond descriptor table size %ld\n",
1215 (long) s, (long) dtblsize, 0 );
1218 ldap_pvt_thread_yield();
1224 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1226 /* newly accepted stream should not be in any of the FD SETS */
1227 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
1228 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
1229 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
1231 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1234 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1235 #ifdef LDAP_PF_LOCAL
1236 /* for IPv4 and IPv6 sockets only */
1237 if ( from.sa_addr.sa_family != AF_LOCAL )
1238 #endif /* LDAP_PF_LOCAL */
1243 /* enable keep alives */
1245 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1246 (char *) &tmp, sizeof(tmp) );
1247 if ( rc == AC_SOCKET_ERROR ) {
1248 int err = sock_errno();
1250 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1251 "slapd_daemon_task: setsockopt( %ld, SO_KEEPALIVE) failed errno=%d (%s)\n",
1252 (long)s, err, sock_errstr(err) ));
1254 Debug( LDAP_DEBUG_ANY,
1255 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1256 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1261 /* enable no delay */
1263 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1264 (char *)&tmp, sizeof(tmp) );
1265 if ( rc == AC_SOCKET_ERROR ) {
1266 int err = sock_errno();
1268 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1269 "slapd_daemon_task: setsockopt( %ld, TCP_NODELAY) failed errno=%d (%s)\n",
1270 (long)s, err, sock_errstr(err) ));
1272 Debug( LDAP_DEBUG_ANY,
1273 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1274 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1282 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1283 "slapd_daemon_task: new connection on %ld\n", (long)s ));
1285 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
1288 switch ( from.sa_addr.sa_family ) {
1289 # ifdef LDAP_PF_LOCAL
1291 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1292 ssf = LDAP_PVT_SASL_LOCAL_SSF;
1294 #endif /* LDAP_PF_LOCAL */
1296 # ifdef LDAP_PF_INET6
1298 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1299 peeraddr = inet_ntoa( *((struct in_addr *)
1300 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1301 sprintf( peername, "IP=%s:%d",
1302 peeraddr != NULL ? peeraddr : "unknown",
1303 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1305 char addr[INET6_ADDRSTRLEN];
1306 sprintf( peername, "IP=%s %d",
1307 inet_ntop( AF_INET6,
1308 &from.sa_in6_addr.sin6_addr,
1309 addr, sizeof addr) ? addr : "unknown",
1310 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1313 # endif /* LDAP_PF_INET6 */
1316 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1317 sprintf( peername, "IP=%s:%d",
1318 peeraddr != NULL ? peeraddr : "unknown",
1319 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1327 if ( ( from.sa_addr.sa_family == AF_INET )
1328 #ifdef LDAP_PF_INET6
1329 || ( from.sa_addr.sa_family == AF_INET6 )
1332 #ifdef SLAPD_RLOOKUPS
1333 # ifdef LDAP_PF_INET6
1334 if ( from.sa_addr.sa_family == AF_INET6 )
1336 (char *)&(from.sa_in6_addr.sin6_addr),
1337 sizeof(from.sa_in6_addr.sin6_addr),
1340 # endif /* LDAP_PF_INET6 */
1342 (char *) &(from.sa_in_addr.sin_addr),
1343 sizeof(from.sa_in_addr.sin_addr),
1345 dnsname = hp ? ldap_pvt_str2lower( hp->h_name ) : NULL;
1348 #endif /* SLAPD_RLOOKUPS */
1351 if ( !hosts_ctl("slapd",
1352 dnsname != NULL ? dnsname : STRING_UNKNOWN,
1353 peeraddr != NULL ? peeraddr : STRING_UNKNOWN,
1357 Statslog( LDAP_DEBUG_ANY,
1358 "fd=%ld host access from %s (%s) denied.\n",
1360 dnsname != NULL ? dnsname : "unknown",
1361 peeraddr != NULL ? peeraddr : "unknown",
1366 #endif /* HAVE_TCPD */
1369 id = connection_init(s,
1370 slap_listeners[l]->sl_url,
1371 dnsname != NULL ? dnsname : "unknown",
1373 slap_listeners[l]->sl_name,
1375 slap_listeners[l]->sl_is_tls,
1382 if( authid ) ch_free(authid);
1386 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1387 "slapd_daemon_task: connection_init(%ld, %s, %s) failed.\n",
1388 (long)s, peername, slap_listeners[l]->sl_name ));
1390 Debug( LDAP_DEBUG_ANY,
1391 "daemon: connection_init(%ld, %s, %s) failed.\n",
1394 slap_listeners[l]->sl_name );
1400 Statslog( LDAP_DEBUG_STATS,
1401 "daemon: conn=%ld fd=%ld connection from %s (%s) accepted.\n",
1404 slap_listeners[l]->sl_name,
1413 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1414 "slapd_daemon_task: activity on " ));
1416 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1419 for ( i = 0; i < readfds.fd_count; i++ ) {
1421 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1422 " %d%s", readfds.fd_array[i], "r", 0 ));
1424 Debug( LDAP_DEBUG_CONNS, " %d%s",
1425 readfds.fd_array[i], "r", 0 );
1428 for ( i = 0; i < writefds.fd_count; i++ ) {
1430 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1431 " %d%s", writefds.fd_array[i], "w" ));
1433 Debug( LDAP_DEBUG_CONNS, " %d%s",
1434 writefds.fd_array[i], "w", 0 );
1439 for ( i = 0; i < nfds; i++ ) {
1441 int is_listener = 0;
1443 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1444 if ( i == slap_listeners[l]->sl_sd ) {
1445 #ifdef LDAP_CONNECTIONLESS
1446 /* The listener is the data port. Don't
1449 if (slap_listeners[l]->sl_is_udp)
1456 if ( is_listener ) {
1459 r = FD_ISSET( i, &readfds );
1460 w = FD_ISSET( i, &writefds );
1463 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1465 r ? "r" : "", w ? "w" : "" ));
1467 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1468 r ? "r" : "", w ? "w" : "" );
1474 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2, "\n" ));
1476 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1481 /* loop through the writers */
1483 for ( i = 0; i < writefds.fd_count; i++ )
1485 for ( i = 0; i < nfds; i++ )
1489 int is_listener = 0;
1491 wd = writefds.fd_array[i];
1493 if( ! FD_ISSET( i, &writefds ) ) {
1499 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1500 if ( i == slap_listeners[l]->sl_sd ) {
1501 #ifdef LDAP_CONNECTIONLESS
1502 if (slap_listeners[l]->sl_is_udp)
1509 if ( is_listener ) {
1513 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1514 "slapd_daemon_task: write active on %d\n", wd ));
1516 Debug( LDAP_DEBUG_CONNS,
1517 "daemon: write active on %d\n",
1521 * NOTE: it is possible that the connection was closed
1522 * and that the stream is now inactive.
1523 * connection_write() must valid the stream is still
1527 if ( connection_write( wd ) < 0 ) {
1528 FD_CLR( (unsigned) wd, &readfds );
1534 for ( i = 0; i < readfds.fd_count; i++ )
1536 for ( i = 0; i < nfds; i++ )
1540 int is_listener = 0;
1543 rd = readfds.fd_array[i];
1545 if( ! FD_ISSET( i, &readfds ) ) {
1551 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1552 if ( rd == slap_listeners[l]->sl_sd ) {
1553 #ifdef LDAP_CONNECTIONLESS
1554 if (slap_listeners[l]->sl_is_udp)
1561 if ( is_listener ) {
1566 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1567 "slapd_daemon_task: read activity on %d\n", rd ));
1569 Debug ( LDAP_DEBUG_CONNS,
1570 "daemon: read activity on %d\n", rd, 0, 0 );
1573 * NOTE: it is possible that the connection was closed
1574 * and that the stream is now inactive.
1575 * connection_read() must valid the stream is still
1579 if ( connection_read( rd ) < 0 ) {
1583 ldap_pvt_thread_yield();
1586 if( slapd_shutdown > 0 ) {
1588 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1589 "slapd_daemon_task: shutdown requested and initiated.\n"));
1591 Debug( LDAP_DEBUG_TRACE,
1592 "daemon: shutdown requested and initiated.\n",
1596 } else if ( slapd_shutdown < 0 ) {
1597 #ifdef HAVE_NT_SERVICE_MANAGER
1598 if (slapd_shutdown == -1)
1601 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1602 "slapd_daemon_task: shutdown initiated by Service Manager.\n"));
1604 Debug( LDAP_DEBUG_TRACE,
1605 "daemon: shutdown initiated by Service Manager.\n",
1613 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1614 "slapd_daemon_task: abnormal condition, shutdown initiated.\n" ));
1616 Debug( LDAP_DEBUG_TRACE,
1617 "daemon: abnormal condition, shutdown initiated.\n",
1623 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1624 "slapd_daemon_task: no active streams, shutdown initiated.\n" ));
1626 Debug( LDAP_DEBUG_TRACE,
1627 "daemon: no active streams, shutdown initiated.\n",
1632 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1633 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1634 #ifdef LDAP_PF_LOCAL
1635 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1636 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1638 #endif /* LDAP_PF_LOCAL */
1639 slapd_close( slap_listeners[l]->sl_sd );
1645 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1646 "slapd_daemon_task: shutdown waiting for %d threads to terminate.\n",
1647 ldap_pvt_thread_pool_backload(&connection_pool) ));
1649 Debug( LDAP_DEBUG_ANY,
1650 "slapd shutdown: waiting for %d threads to terminate\n",
1651 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1653 ldap_pvt_thread_pool_destroy(&connection_pool, 1);
1659 int slapd_daemon( void )
1665 #define SLAPD_LISTENER_THREAD 1
1666 #if defined( SLAPD_LISTENER_THREAD )
1668 ldap_pvt_thread_t listener_tid;
1670 /* listener as a separate THREAD */
1671 rc = ldap_pvt_thread_create( &listener_tid,
1672 0, slapd_daemon_task, NULL );
1676 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1677 "slapd_daemon: listener ldap_pvt_thread_create failed (%d).\n", rc ));
1679 Debug( LDAP_DEBUG_ANY,
1680 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
1685 /* wait for the listener thread to complete */
1686 ldap_pvt_thread_join( listener_tid, (void *) NULL );
1689 /* experimental code */
1690 slapd_daemon_task( NULL );
1699 #if defined( HAVE_WINSOCK2 )
1700 WORD wVersionRequested;
1704 wVersionRequested = MAKEWORD( 2, 0 );
1706 err = WSAStartup( wVersionRequested, &wsaData );
1708 /* Tell the user that we couldn't find a usable */
1713 /* Confirm that the WinSock DLL supports 2.0.*/
1714 /* Note that if the DLL supports versions greater */
1715 /* than 2.0 in addition to 2.0, it will still return */
1716 /* 2.0 in wVersion since that is the version we */
1719 if ( LOBYTE( wsaData.wVersion ) != 2 ||
1720 HIBYTE( wsaData.wVersion ) != 0 )
1722 /* Tell the user that we couldn't find a usable */
1728 /* The WinSock DLL is acceptable. Proceed. */
1729 #elif defined( HAVE_WINSOCK )
1731 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
1738 int sockdestroy(void)
1740 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
1747 slap_sig_shutdown( int sig )
1750 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1751 "slap_sig_shutdown: signal %d\n", sig ));
1753 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
1757 * If the NT Service Manager is controlling the server, we don't
1758 * want SIGBREAK to kill the server. For some strange reason,
1759 * SIGBREAK is generated when a user logs out.
1762 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
1763 if (is_NT_Service && sig == SIGBREAK)
1765 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1766 "slap_sig_shutdown: SIGBREAK ignored.\n" ));
1768 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: SIGBREAK ignored.\n",
1773 slapd_shutdown = sig;
1777 /* reinstall self */
1778 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
1782 slap_sig_wake( int sig )
1786 /* reinstall self */
1787 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
1791 void slapd_add_internal(ber_socket_t s) {