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 *));
400 for ( sai=res; sai; sai=sai->ai_next ) {
401 if( sai->ai_addr == NULL ) {
406 switch (sai->ai_family) {
407 # ifdef LDAP_PF_INET6
409 *sap = ch_malloc(sizeof(struct sockaddr_in6));
414 *(struct sockaddr_in6 *)*sap =
415 *((struct sockaddr_in6 *)sai->ai_addr);
419 *sap = ch_malloc(sizeof(struct sockaddr_in));
424 *(struct sockaddr_in *)*sap =
425 *((struct sockaddr_in *)sai->ai_addr);
432 (*sap)->sa_family = sai->ai_family;
442 if ( host == NULL ) {
443 in.s_addr = htonl(INADDR_ANY);
445 } else if ( !inet_aton( host, &in ) ) {
446 struct hostent *he = gethostbyname( host );
449 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
450 "slap_get_listener_addresses: invalid host %s\n",
453 Debug( LDAP_DEBUG_ANY,
454 "daemon: invalid host %s", host, 0, 0);
458 AC_MEMCPY( &in, he->h_addr, sizeof( in ) );
461 *sal = ch_malloc(2 * sizeof(void *));
467 *sap = ch_malloc(sizeof(struct sockaddr_in));
473 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_in) );
474 (*sap)->sa_family = AF_INET;
475 ((struct sockaddr_in *)*sap)->sin_port = htons(port);
476 ((struct sockaddr_in *)*sap)->sin_addr = in;
483 slap_free_listener_addresses(*sal);
487 static Listener * slap_open_listener(
496 struct sockaddr **sal, **psal;
497 int socktype = SOCK_STREAM; /* default to COTS */
499 rc = ldap_url_parse( url, &lud );
501 if( rc != LDAP_URL_SUCCESS ) {
503 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
504 "slap_open_listener: listen URL \"%s\" parse error %d\n",
507 Debug( LDAP_DEBUG_ANY,
508 "daemon: listen URL \"%s\" parse error=%d\n",
515 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
517 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
518 "slap_open_listener: TLS is not supported (%s)\n",
521 Debug( LDAP_DEBUG_ANY,
522 "daemon: TLS not supported (%s)\n",
525 ldap_free_urldesc( lud );
529 if(! lud->lud_port ) {
530 lud->lud_port = LDAP_PORT;
534 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
536 if(! lud->lud_port ) {
537 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
541 port = (unsigned short) lud->lud_port;
543 tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme);
544 if ( tmp == LDAP_PROTO_IPC ) {
546 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
547 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
549 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
554 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
555 "slap_open_listener: URL scheme is not supported: %s\n",
558 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
561 ldap_free_urldesc( lud );
565 #ifdef LDAP_CONNECTIONLESS
566 if ( tmp == LDAP_PROTO_UDP )
569 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
570 || strcmp(lud->lud_host, "*") == 0 )
572 err = slap_get_listener_addresses(NULL, port, &sal);
574 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
578 ldap_free_urldesc( lud );
584 while ( *sal != NULL ) {
585 switch( (*sal)->sa_family ) {
598 #ifdef LDAP_CONNECTIONLESS
600 socktype = SOCK_DGRAM;
602 l.sl_sd = socket( (*sal)->sa_family, socktype, 0);
603 if ( l.sl_sd == AC_SOCKET_INVALID ) {
604 int err = sock_errno();
606 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
607 "slap_open_listener: socket() failed errno=%d (%s)\n",
608 err, sock_errstr(err) ));
610 Debug( LDAP_DEBUG_ANY,
611 "daemon: socket() failed errno=%d (%s)\n", err,
612 sock_errstr(err), 0 );
618 if ( l.sl_sd >= dtblsize ) {
620 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
621 "slap_open_listener: listener descriptor %ld is too great %ld\n",
622 (long)l.sl_sd, (long)dtblsize ));
624 Debug( LDAP_DEBUG_ANY,
625 "daemon: listener descriptor %ld is too great %ld\n",
626 (long) l.sl_sd, (long) dtblsize, 0 );
628 tcp_close( l.sl_sd );
634 if ( (*sal)->sa_family == AF_LOCAL ) {
635 unlink ( ((struct sockaddr_un *)*sal)->sun_path );
640 /* enable address reuse */
642 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
643 (char *) &tmp, sizeof(tmp) );
644 if ( rc == AC_SOCKET_ERROR ) {
645 int err = sock_errno();
647 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
648 "slap_open_listener: setsockopt( %ld, SO_REUSEADDR ) failed errno %d (%s)\n",
649 (long)l.sl_sd, err, sock_errstr(err) ));
651 Debug( LDAP_DEBUG_ANY,
652 "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
653 (long) l.sl_sd, err, sock_errstr(err) );
659 switch( (*sal)->sa_family ) {
661 addrlen = sizeof(struct sockaddr_in);
665 addrlen = sizeof(struct sockaddr_in6);
670 addrlen = sizeof(struct sockaddr_un);
675 if (!bind(l.sl_sd, *sal, addrlen))
679 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
680 "slap_open_listener: bind(%ld) failed errno=%d (%s)\n",
681 (long)l.sl_sd, err, sock_errstr(err) ));
683 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
684 (long) l.sl_sd, err, sock_errstr(err) );
686 tcp_close( l.sl_sd );
688 } /* while ( *sal != NULL ) */
690 if ( *sal == NULL ) {
692 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
693 "slap_open_listener: bind(%ld) failed.\n", (long)l.sl_sd ));
695 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed\n",
696 (long) l.sl_sd, 0, 0 );
698 slap_free_listener_addresses(psal);
702 switch ( (*sal)->sa_family ) {
705 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
706 if ( chmod( addr, S_IRWXU ) < 0 ) {
707 int err = sock_errno();
709 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
710 "slap_open_listener: fchmod(%ld) failed errno=%d (%s)\n",
711 (long)l.sl_sd, err, sock_errstr(err) ));
713 Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
714 (long) l.sl_sd, err, sock_errstr(err) );
716 tcp_close( l.sl_sd );
717 slap_free_listener_addresses(psal);
720 l.sl_name = ch_malloc( strlen(addr) + sizeof("PATH=") );
721 sprintf( l.sl_name, "PATH=%s", addr );
723 #endif /* LDAP_PF_LOCAL */
727 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
728 char addr[INET_ADDRSTRLEN];
729 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
730 addr, sizeof(addr) );
732 port = ((struct sockaddr_in *)*sal) ->sin_port;
734 s = inet_ntoa( l.sl_addr.sin_addr );
735 port = l.sl_addr.sin_port;
737 l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65535") );
738 sprintf( l.sl_name, "IP=%s:%d",
739 s != NULL ? s : "unknown" , port );
744 char addr[INET6_ADDRSTRLEN];
745 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
747 port = ((struct sockaddr_in6 *)*sal)->sin6_port;
748 l.sl_name = ch_malloc( strlen(addr) + sizeof("IP= 65535") );
749 sprintf( l.sl_name, "IP=%s %d", addr, port );
751 #endif /* LDAP_PF_INET6 */
755 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
756 "slap_open_listener: unsupported address family (%d)\n",
757 (int)(*sal)->sa_family ));
759 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
760 (int) (*sal)->sa_family, 0, 0 );
765 slap_free_listener_addresses(psal);
767 l.sl_url = ch_strdup( url );
768 li = ch_malloc( sizeof( Listener ) );
772 LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
773 "slap_open_listener: daemon initialzed %s\n", l.sl_url ));
775 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
781 static int sockinit(void);
782 static int sockdestroy(void);
784 int slapd_daemon_init( const char *urls )
790 LDAP_LOG(( "connection", LDAP_LEVEL_ARGS,
791 "slapd_daemon_init: %s\n",
792 urls ? urls : "<null>" ));
794 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
795 urls ? urls : "<null>", 0, 0 );
797 if( (rc = sockinit()) != 0 ) {
802 dtblsize = sysconf( _SC_OPEN_MAX );
803 #elif HAVE_GETDTABLESIZE
804 dtblsize = getdtablesize();
806 dtblsize = FD_SETSIZE;
810 if(dtblsize > FD_SETSIZE) {
811 dtblsize = FD_SETSIZE;
813 #endif /* !FD_SETSIZE */
815 /* open a pipe (or something equivalent connected to itself).
816 * we write a byte on this fd whenever we catch a signal. The main
817 * loop will be select'ing on this socket, and will wake up when
820 if( (rc = lutil_pair( wake_sds )) < 0 ) {
822 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
823 "slap_daemon_init: lutil_pair() failed rc=%d\n", rc ));
825 Debug( LDAP_DEBUG_ANY,
826 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
831 FD_ZERO( &slap_daemon.sd_readers );
832 FD_ZERO( &slap_daemon.sd_writers );
838 u = str2charray( urls, " " );
840 if( u == NULL || u[0] == NULL ) {
842 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
843 "slap_daemon_init: no urls (%s) provided.\n", urls ));
845 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
851 for( i=0; u[i] != NULL; i++ ) {
853 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
854 "slap_daemon_init: listen on %s\n.", u[i] ));
856 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
863 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
864 "slap_daemon_init: no listeners to open (%s)\n", urls ));
866 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
874 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
875 "slap_daemon_init: %d listeners to open...\n", i ));
877 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
880 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
882 for(i = 0; u[i] != NULL; i++ ) {
883 slap_listeners[i] = slap_open_listener( u[i] );
885 if( slap_listeners[i] == NULL ) {
890 slap_listeners[i] = NULL;
893 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
894 "slap_daemon_init: %d listeners opened\n", i ));
896 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
901 slapd_slp_init( urls );
906 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
912 slapd_daemon_destroy(void)
914 connections_destroy();
915 tcp_close( wake_sds[1] );
916 tcp_close( wake_sds[0] );
934 time_t last_idle_check = slap_get_time();
937 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
938 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
940 #ifdef LDAP_CONNECTIONLESS
941 /* Since this is connectionless, the data port is the
942 * listening port. The listen() and accept() calls
945 if ( slap_listeners[l]->sl_is_udp )
947 slapd_add( slap_listeners[l]->sl_sd );
952 if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN ) == -1 ) {
953 int err = sock_errno();
955 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
956 "slapd_daemon_task: listen( %s, 5 ) failed errno=%d (%s)\n",
957 slap_listeners[l]->sl_url, err, sock_errstr(err) ));
959 Debug( LDAP_DEBUG_ANY,
960 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
961 slap_listeners[l]->sl_url, err,
967 slapd_add( slap_listeners[l]->sl_sd );
970 #ifdef HAVE_NT_SERVICE_MANAGER
971 if ( started_event != NULL ) {
972 ldap_pvt_thread_cond_signal( &started_event );
975 /* initialization complete. Here comes the loop. */
977 while ( !slapd_shutdown ) {
982 #define SLAPD_EBADF_LIMIT 16
985 #define SLAPD_IDLE_CHECK_LIMIT 4
986 time_t now = slap_get_time();
993 #if defined(SLAPD_RLOOKUPS)
999 if( global_idletimeout > 0 && difftime(
1000 last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT,
1003 connections_timeout_idle(now);
1006 FD_ZERO( &writefds );
1007 FD_ZERO( &readfds );
1012 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1014 #ifdef FD_SET_MANUAL_COPY
1015 for( s = 0; s < nfds; s++ ) {
1016 if(FD_ISSET( &slap_sd_readers, s )) {
1017 FD_SET( s, &readfds );
1019 if(FD_ISSET( &slap_sd_writers, s )) {
1020 FD_SET( s, &writefds );
1024 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
1025 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
1027 assert(!FD_ISSET(wake_sds[0], &readfds));
1028 FD_SET( wake_sds[0], &readfds );
1030 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1031 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1033 if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
1034 FD_SET( slap_listeners[l]->sl_sd, &readfds );
1037 #ifndef HAVE_WINSOCK
1038 nfds = slap_daemon.sd_nfds;
1043 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1045 at = ldap_pvt_thread_pool_backload(&connection_pool);
1047 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
1050 tvp = at ? &zero : NULL;
1053 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1054 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1058 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1059 "slapd_daemon_task: select: listen=%d active_threads=%d tvp=%s\n",
1060 slap_listeners[l]->sl_sd, at, tvp == NULL ? "NULL" : "zero" ));
1062 Debug( LDAP_DEBUG_CONNS,
1063 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
1064 slap_listeners[l]->sl_sd, at,
1065 tvp == NULL ? "NULL" : "zero" );
1069 switch(ns = select( nfds, &readfds,
1071 /* don't pass empty fd_set */
1072 ( writefds.fd_count > 0 ? &writefds : NULL ),
1078 case -1: { /* failure - try again */
1079 int err = sock_errno();
1083 /* you'd think this would be EBADF */
1084 || err == WSAENOTSOCK
1087 if (++ebadf < SLAPD_EBADF_LIMIT)
1091 if( err != EINTR ) {
1093 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1094 "slapd_daemon_task: select failed (%d): %s\n",
1095 err, sock_errstr(err) ));
1097 Debug( LDAP_DEBUG_CONNS,
1098 "daemon: select failed (%d): %s\n",
1099 err, sock_errstr(err), 0 );
1101 slapd_shutdown = -1;
1106 case 0: /* timeout - let threads run */
1109 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1110 "slapd_daemon_task: select timeout - yielding\n" ));
1112 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
1115 ldap_pvt_thread_yield();
1118 default: /* something happened - deal with it */
1119 if( slapd_shutdown ) continue;
1123 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1124 "slapd_daemon_task: activity on %d descriptors\n", ns ));
1126 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
1132 if( FD_ISSET( wake_sds[0], &readfds ) ) {
1134 tcp_read( wake_sds[0], c, sizeof(c) );
1141 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1143 socklen_t len = sizeof(from);
1146 char *authid = NULL;
1150 #ifdef LDAP_PF_LOCAL
1151 char peername[MAXPATHLEN + sizeof("PATH=")];
1152 #elif defined(LDAP_PF_INET6)
1153 char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
1155 char peername[sizeof("IP=255.255.255.255:65336")];
1156 #endif /* LDAP_PF_LOCAL */
1160 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1163 if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
1166 #ifdef LDAP_CONNECTIONLESS
1167 if ( slap_listeners[l]->sl_is_udp )
1169 /* The first time we receive a query, we set this
1170 * up as a "connection". It remains open for the life
1173 if ( slap_listeners[l]->sl_is_udp < 2 )
1175 id = connection_init(
1176 slap_listeners[l]->sl_sd,
1177 slap_listeners[l]->sl_url, "", "",
1178 slap_listeners[l]->sl_name,
1180 slap_listeners[l]->sl_is_udp++;
1186 s = accept( slap_listeners[l]->sl_sd,
1187 (struct sockaddr *) &from, &len );
1188 if ( s == AC_SOCKET_INVALID ) {
1189 int err = sock_errno();
1191 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1192 "slapd_daemon_task: accept(%ld) failed errno=%d (%s)\n",
1193 (long)slap_listeners[l]->sl_sd, err, sock_errstr(err) ));
1195 Debug( LDAP_DEBUG_ANY,
1196 "daemon: accept(%ld) failed errno=%d (%s)\n",
1197 (long) slap_listeners[l]->sl_sd, err,
1200 ldap_pvt_thread_yield();
1204 #ifndef HAVE_WINSOCK
1205 /* make sure descriptor number isn't too great */
1206 if ( s >= dtblsize ) {
1208 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1209 "slapd_daemon_task: %ld beyond descriptor table size %ld\n",
1210 (long)s, (long)dtblsize ));
1212 Debug( LDAP_DEBUG_ANY,
1213 "daemon: %ld beyond descriptor table size %ld\n",
1214 (long) s, (long) dtblsize, 0 );
1217 ldap_pvt_thread_yield();
1223 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1225 /* newly accepted stream should not be in any of the FD SETS */
1226 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
1227 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
1228 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
1230 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1233 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1234 #ifdef LDAP_PF_LOCAL
1235 /* for IPv4 and IPv6 sockets only */
1236 if ( from.sa_addr.sa_family != AF_LOCAL )
1237 #endif /* LDAP_PF_LOCAL */
1242 /* enable keep alives */
1244 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1245 (char *) &tmp, sizeof(tmp) );
1246 if ( rc == AC_SOCKET_ERROR ) {
1247 int err = sock_errno();
1249 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1250 "slapd_daemon_task: setsockopt( %ld, SO_KEEPALIVE) failed errno=%d (%s)\n",
1251 (long)s, err, sock_errstr(err) ));
1253 Debug( LDAP_DEBUG_ANY,
1254 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1255 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1260 /* enable no delay */
1262 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1263 (char *)&tmp, sizeof(tmp) );
1264 if ( rc == AC_SOCKET_ERROR ) {
1265 int err = sock_errno();
1267 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1268 "slapd_daemon_task: setsockopt( %ld, TCP_NODELAY) failed errno=%d (%s)\n",
1269 (long)s, err, sock_errstr(err) ));
1271 Debug( LDAP_DEBUG_ANY,
1272 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1273 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1281 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1282 "slapd_daemon_task: new connection on %ld\n", (long)s ));
1284 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
1287 switch ( from.sa_addr.sa_family ) {
1288 # ifdef LDAP_PF_LOCAL
1290 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1291 ssf = LDAP_PVT_SASL_LOCAL_SSF;
1293 #endif /* LDAP_PF_LOCAL */
1295 # ifdef LDAP_PF_INET6
1297 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1298 peeraddr = inet_ntoa( *((struct in_addr *)
1299 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1300 sprintf( peername, "IP=%s:%d",
1301 peeraddr != NULL ? peeraddr : "unknown",
1302 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1304 char addr[INET6_ADDRSTRLEN];
1305 sprintf( peername, "IP=%s %d",
1306 inet_ntop( AF_INET6,
1307 &from.sa_in6_addr.sin6_addr,
1308 addr, sizeof addr) ? addr : "unknown",
1309 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1312 # endif /* LDAP_PF_INET6 */
1315 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1316 sprintf( peername, "IP=%s:%d",
1317 peeraddr != NULL ? peeraddr : "unknown",
1318 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1326 if ( ( from.sa_addr.sa_family == AF_INET )
1327 #ifdef LDAP_PF_INET6
1328 || ( from.sa_addr.sa_family == AF_INET6 )
1331 #ifdef SLAPD_RLOOKUPS
1332 # ifdef LDAP_PF_INET6
1333 if ( from.sa_addr.sa_family == AF_INET6 )
1335 (char *)&(from.sa_in6_addr.sin6_addr),
1336 sizeof(from.sa_in6_addr.sin6_addr),
1339 # endif /* LDAP_PF_INET6 */
1341 (char *) &(from.sa_in_addr.sin_addr),
1342 sizeof(from.sa_in_addr.sin_addr),
1344 dnsname = hp ? ldap_pvt_str2lower( hp->h_name ) : NULL;
1347 #endif /* SLAPD_RLOOKUPS */
1350 if ( !hosts_ctl("slapd",
1351 dnsname != NULL ? dnsname : STRING_UNKNOWN,
1352 peeraddr != NULL ? peeraddr : STRING_UNKNOWN,
1356 Statslog( LDAP_DEBUG_ANY,
1357 "fd=%ld host access from %s (%s) denied.\n",
1359 dnsname != NULL ? dnsname : "unknown",
1360 peeraddr != NULL ? peeraddr : "unknown",
1365 #endif /* HAVE_TCPD */
1368 id = connection_init(s,
1369 slap_listeners[l]->sl_url,
1370 dnsname != NULL ? dnsname : "unknown",
1372 slap_listeners[l]->sl_name,
1374 slap_listeners[l]->sl_is_tls,
1381 if( authid ) ch_free(authid);
1385 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1386 "slapd_daemon_task: connection_init(%ld, %s, %s) failed.\n",
1387 (long)s, peername, slap_listeners[l]->sl_name ));
1389 Debug( LDAP_DEBUG_ANY,
1390 "daemon: connection_init(%ld, %s, %s) failed.\n",
1393 slap_listeners[l]->sl_name );
1399 Statslog( LDAP_DEBUG_STATS,
1400 "daemon: conn=%ld fd=%ld connection from %s (%s) accepted.\n",
1403 slap_listeners[l]->sl_name,
1412 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1413 "slapd_daemon_task: activity on " ));
1415 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1418 for ( i = 0; i < readfds.fd_count; i++ ) {
1420 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1421 " %d%s", readfds.fd_array[i], "r", 0 ));
1423 Debug( LDAP_DEBUG_CONNS, " %d%s",
1424 readfds.fd_array[i], "r", 0 );
1427 for ( i = 0; i < writefds.fd_count; i++ ) {
1429 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1430 " %d%s", writefds.fd_array[i], "w" ));
1432 Debug( LDAP_DEBUG_CONNS, " %d%s",
1433 writefds.fd_array[i], "w", 0 );
1438 for ( i = 0; i < nfds; i++ ) {
1440 int is_listener = 0;
1442 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1443 if ( i == slap_listeners[l]->sl_sd ) {
1444 #ifdef LDAP_CONNECTIONLESS
1445 /* The listener is the data port. Don't
1448 if (slap_listeners[l]->sl_is_udp)
1455 if ( is_listener ) {
1458 r = FD_ISSET( i, &readfds );
1459 w = FD_ISSET( i, &writefds );
1462 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1464 r ? "r" : "", w ? "w" : "" ));
1466 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1467 r ? "r" : "", w ? "w" : "" );
1473 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2, "\n" ));
1475 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1480 /* loop through the writers */
1482 for ( i = 0; i < writefds.fd_count; i++ )
1484 for ( i = 0; i < nfds; i++ )
1488 int is_listener = 0;
1490 wd = writefds.fd_array[i];
1492 if( ! FD_ISSET( i, &writefds ) ) {
1498 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1499 if ( i == slap_listeners[l]->sl_sd ) {
1500 #ifdef LDAP_CONNECTIONLESS
1501 if (slap_listeners[l]->sl_is_udp)
1508 if ( is_listener ) {
1512 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1513 "slapd_daemon_task: write active on %d\n", wd ));
1515 Debug( LDAP_DEBUG_CONNS,
1516 "daemon: write active on %d\n",
1520 * NOTE: it is possible that the connection was closed
1521 * and that the stream is now inactive.
1522 * connection_write() must valid the stream is still
1526 if ( connection_write( wd ) < 0 ) {
1527 FD_CLR( (unsigned) wd, &readfds );
1533 for ( i = 0; i < readfds.fd_count; i++ )
1535 for ( i = 0; i < nfds; i++ )
1539 int is_listener = 0;
1542 rd = readfds.fd_array[i];
1544 if( ! FD_ISSET( i, &readfds ) ) {
1550 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1551 if ( rd == slap_listeners[l]->sl_sd ) {
1552 #ifdef LDAP_CONNECTIONLESS
1553 if (slap_listeners[l]->sl_is_udp)
1560 if ( is_listener ) {
1565 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1566 "slapd_daemon_task: read activity on %d\n", rd ));
1568 Debug ( LDAP_DEBUG_CONNS,
1569 "daemon: read activity on %d\n", rd, 0, 0 );
1572 * NOTE: it is possible that the connection was closed
1573 * and that the stream is now inactive.
1574 * connection_read() must valid the stream is still
1578 if ( connection_read( rd ) < 0 ) {
1582 ldap_pvt_thread_yield();
1585 if( slapd_shutdown > 0 ) {
1587 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1588 "slapd_daemon_task: shutdown requested and initiated.\n"));
1590 Debug( LDAP_DEBUG_TRACE,
1591 "daemon: shutdown requested and initiated.\n",
1595 } else if ( slapd_shutdown < 0 ) {
1596 #ifdef HAVE_NT_SERVICE_MANAGER
1597 if (slapd_shutdown == -1)
1600 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1601 "slapd_daemon_task: shutdown initiated by Service Manager.\n"));
1603 Debug( LDAP_DEBUG_TRACE,
1604 "daemon: shutdown initiated by Service Manager.\n",
1612 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1613 "slapd_daemon_task: abnormal condition, shutdown initiated.\n" ));
1615 Debug( LDAP_DEBUG_TRACE,
1616 "daemon: abnormal condition, shutdown initiated.\n",
1622 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1623 "slapd_daemon_task: no active streams, shutdown initiated.\n" ));
1625 Debug( LDAP_DEBUG_TRACE,
1626 "daemon: no active streams, shutdown initiated.\n",
1631 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1632 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1633 #ifdef LDAP_PF_LOCAL
1634 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1635 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1637 #endif /* LDAP_PF_LOCAL */
1638 slapd_close( slap_listeners[l]->sl_sd );
1644 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1645 "slapd_daemon_task: shutdown waiting for %d threads to terminate.\n",
1646 ldap_pvt_thread_pool_backload(&connection_pool) ));
1648 Debug( LDAP_DEBUG_ANY,
1649 "slapd shutdown: waiting for %d threads to terminate\n",
1650 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1652 ldap_pvt_thread_pool_destroy(&connection_pool, 1);
1658 int slapd_daemon( void )
1664 #define SLAPD_LISTENER_THREAD 1
1665 #if defined( SLAPD_LISTENER_THREAD )
1667 ldap_pvt_thread_t listener_tid;
1669 /* listener as a separate THREAD */
1670 rc = ldap_pvt_thread_create( &listener_tid,
1671 0, slapd_daemon_task, NULL );
1675 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1676 "slapd_daemon: listener ldap_pvt_thread_create failed (%d).\n", rc ));
1678 Debug( LDAP_DEBUG_ANY,
1679 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
1684 /* wait for the listener thread to complete */
1685 ldap_pvt_thread_join( listener_tid, (void *) NULL );
1688 /* experimental code */
1689 slapd_daemon_task( NULL );
1698 #if defined( HAVE_WINSOCK2 )
1699 WORD wVersionRequested;
1703 wVersionRequested = MAKEWORD( 2, 0 );
1705 err = WSAStartup( wVersionRequested, &wsaData );
1707 /* Tell the user that we couldn't find a usable */
1712 /* Confirm that the WinSock DLL supports 2.0.*/
1713 /* Note that if the DLL supports versions greater */
1714 /* than 2.0 in addition to 2.0, it will still return */
1715 /* 2.0 in wVersion since that is the version we */
1718 if ( LOBYTE( wsaData.wVersion ) != 2 ||
1719 HIBYTE( wsaData.wVersion ) != 0 )
1721 /* Tell the user that we couldn't find a usable */
1727 /* The WinSock DLL is acceptable. Proceed. */
1728 #elif defined( HAVE_WINSOCK )
1730 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
1737 int sockdestroy(void)
1739 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
1746 slap_sig_shutdown( int sig )
1749 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1750 "slap_sig_shutdown: signal %d\n", sig ));
1752 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
1756 * If the NT Service Manager is controlling the server, we don't
1757 * want SIGBREAK to kill the server. For some strange reason,
1758 * SIGBREAK is generated when a user logs out.
1761 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
1762 if (is_NT_Service && sig == SIGBREAK)
1764 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1765 "slap_sig_shutdown: SIGBREAK ignored.\n" ));
1767 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: SIGBREAK ignored.\n",
1772 slapd_shutdown = sig;
1776 /* reinstall self */
1777 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
1781 slap_sig_wake( int sig )
1785 /* reinstall self */
1786 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
1790 void slapd_add_internal(ber_socket_t s) {