2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms are permitted
19 * provided that this notice is preserved and that due credit is given
20 * to the University of Michigan at Ann Arbor. The name of the University
21 * may not be used to endorse or promote products derived from this
22 * software without specific prior written permission. This software
23 * is provided ``as is'' without express or implied warranty.
32 #include <ac/socket.h>
33 #include <ac/string.h>
35 #include <ac/unistd.h>
38 #include "ldap_pvt_thread.h"
46 #include <sys/epoll.h>
51 #define SLAP_STRING_UNKNOWN STRING_UNKNOWN
53 int allow_severity = LOG_INFO;
54 int deny_severity = LOG_NOTICE;
55 #else /* ! TCP Wrappers */
56 #define SLAP_STRING_UNKNOWN "unknown"
57 #endif /* ! TCP Wrappers */
61 /* this should go in <ldap.h> as soon as it is accepted */
62 #define LDAPI_MOD_URLEXT "x-mod"
63 #endif /* LDAP_PF_LOCAL */
66 int slap_inet4or6 = AF_UNSPEC;
68 int slap_inet4or6 = AF_INET;
73 ber_socket_t dtblsize;
74 slap_ssf_t local_ssf = LDAP_PVT_SASL_LOCAL_SSF;
76 Listener **slap_listeners = NULL;
78 #define SLAPD_LISTEN 10
80 static ber_socket_t wake_sds[2];
84 #define WAKE_LISTENER(w) \
85 do { if (w && waking < 5) { tcp_write( wake_sds[1], "0", 1 ); waking++;} } while(0)
87 volatile sig_atomic_t slapd_shutdown = 0, slapd_gentle_shutdown = 0;
88 volatile sig_atomic_t slapd_abrupt_shutdown = 0;
90 static struct slap_daemon {
91 ldap_pvt_thread_mutex_t sd_mutex;
93 ber_socket_t sd_nactives;
97 struct epoll_event *sd_epolls;
105 /* In winsock, accept() returns values higher than dtblsize
106 so don't bother with this optimization */
117 #define SLAP_EVENTS_ARE_INDEXED 0
118 #define SLAP_SOCK_IX(s) (slap_daemon.sd_index[s])
119 #define SLAP_SOCK_EP(s) (slap_daemon.sd_epolls[SLAP_SOCK_IX(s)])
120 #define SLAP_SOCK_FD(s) (SLAP_SOCK_EP(s).data.fd)
121 #define SLAP_SOCK_EV(s) (SLAP_SOCK_EP(s).events)
122 #define SLAP_SOCK_IS_ACTIVE(s) (SLAP_SOCK_IX(s) != -1 && SLAP_SOCK_FD(s) == s)
123 #define SLAP_SOCK_NOT_ACTIVE(s) (SLAP_SOCK_IX(s) == -1)
124 #define SLAP_SOCK_IS_SET(s, mode) (SLAP_SOCK_EV(s) & mode)
126 #define SLAP_SOCK_IS_READ(s) SLAP_SOCK_IS_SET(s, EPOLLIN)
127 #define SLAP_SOCK_IS_WRITE(s) SLAP_SOCK_IS_SET(s, EPOLLOUT)
129 #define SLAP_SET_SOCK(s, events) do { \
130 if ((SLAP_SOCK_EV(s) & events) != events) { \
131 SLAP_SOCK_EV(s) |= events; \
132 epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_MOD, s, \
137 #define SLAP_CLR_SOCK(s, events) do { \
138 if ((SLAP_SOCK_EV(s) & events)) { \
139 SLAP_SOCK_EV(s) &= ~events; \
140 epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_MOD, s, \
145 #define SLAP_SOCK_SET_READ(s) SLAP_SET_SOCK(s, EPOLLIN)
146 #define SLAP_SOCK_SET_WRITE(s) SLAP_SET_SOCK(s, EPOLLOUT)
148 #define SLAP_SOCK_CLR_READ(s) SLAP_CLR_SOCK(s, EPOLLIN)
149 #define SLAP_SOCK_CLR_WRITE(s) SLAP_CLR_SOCK(s, EPOLLOUT)
151 #define SLAP_CLR_EVENT(i, events) (revents[i].events &= ~events)
153 #define SLAP_EVENT_CLR_READ(i) SLAP_CLR_EVENT(i, EPOLLIN)
154 #define SLAP_EVENT_CLR_WRITE(i) SLAP_CLR_EVENT(i, EPOLLOUT)
156 #define SLAP_CHK_EVENT(i, events) (revents[i].events & events)
158 #define SLAP_EVENT_IS_READ(i) SLAP_CHK_EVENT(i, EPOLLIN)
159 #define SLAP_EVENT_IS_WRITE(i) SLAP_CHK_EVENT(i, EPOLLOUT)
161 #define SLAP_EVENT_FD(i) (revents[i].data.fd)
163 #define SLAP_EVENT_MAX slap_daemon.sd_nfds
165 #define SLAP_ADD_SOCK(s) do { \
167 SLAP_SOCK_IX(s) = slap_daemon.sd_nfds; \
168 SLAP_SOCK_FD(s) = s; \
169 SLAP_SOCK_EV(s) = EPOLLIN; \
170 rc = epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_ADD, s, \
172 if ( rc == 0 ) slap_daemon.sd_nfds++; \
175 #define SLAP_DEL_SOCK(s) do { \
176 int rc, index = SLAP_SOCK_IX(s); \
177 rc = epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_DEL, s, \
179 slap_daemon.sd_epolls[index] = slap_daemon.sd_epolls[slap_daemon.sd_nfds-1]; \
180 slap_daemon.sd_index[slap_daemon.sd_epolls[index].data.fd] = index; \
181 slap_daemon.sd_index[s] = -1; \
182 slap_daemon.sd_nfds--; \
185 #define SLAP_SOCK_SET_MUTE(s) SLAP_SOCK_CLR_READ(s)
186 #define SLAP_SOCK_CLR_MUTE(s) SLAP_SOCK_SET_READ(s)
187 #define SLAP_SOCK_IS_MUTE(s) !SLAP_SOCK_IS_READ(s)
189 #define SLAP_SOCK_SET_INIT \
190 slap_daemon.sd_epolls = ch_malloc(sizeof(struct epoll_event) * dtblsize * 2); \
191 slap_daemon.sd_index = ch_malloc(sizeof(int) * dtblsize); \
192 slap_daemon.sd_epfd = epoll_create( dtblsize )
194 #define SLAP_EVENT_DECL \
195 struct epoll_event *revents
197 #define SLAP_EVENT_INIT \
198 revents = slap_daemon.sd_epolls + dtblsize; \
200 #define SLAP_EVENT_WAIT(tvp) \
201 epoll_wait( slap_daemon.sd_epfd, revents, dtblsize, tvp ? tvp->tv_sec * 1000 : -1 )
206 #define SLAP_EVENTS_ARE_INDEXED 1
207 #define SLAP_EVENT_DECL \
208 fd_set readfds, writefds
210 #define SLAP_EVENT_INIT \
211 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) ); \
213 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
216 #define CHK_SETSIZE \
217 if (dtblsize > FD_SETSIZE) dtblsize = FD_SETSIZE
222 #define SLAP_SOCK_SET_INIT \
224 FD_ZERO(&slap_daemon.sd_readers); \
225 FD_ZERO(&slap_daemon.sd_writers)
227 #define SLAP_SOCK_IS_ACTIVE(fd) FD_ISSET(fd, &slap_daemon.sd_actives)
229 #define SLAP_SOCK_IS_READ(fd) FD_ISSET(fd, &slap_daemon.sd_readers)
230 #define SLAP_SOCK_IS_WRITE(fd) FD_ISSET(fd, &slap_daemon.sd_writers)
232 #define SLAP_SOCK_NOT_ACTIVE(fd) (!SLAP_SOCK_IS_ACTIVE(fd) && \
233 !SLAP_SOCK_IS_READ(fd) && !SLAP_SOCK_IS_WRITE(fd))
236 #define SLAP_SOCK_SET_READ(fd) do { \
237 if (!SLAP_SOCK_IS_READ(fd)) {FD_SET(fd, &slap_daemon.sd_readers);} \
239 #define SLAP_SOCK_SET_WRITE(fd) do { \
240 if (!SLAP_SOCK_IS_WRITE(fd)) {FD_SET(fd, &slap_daemon.sd_writers);} \
243 #define SLAP_ADDTEST(s)
244 #define SLAP_EVENT_MAX dtblsize
246 #define SLAP_SOCK_SET_READ(fd) FD_SET(fd, &slap_daemon.sd_readers)
247 #define SLAP_SOCK_SET_WRITE(fd) FD_SET(fd, &slap_daemon.sd_writers)
249 #define SLAP_EVENT_MAX slap_daemon.sd_nfds
250 #define SLAP_ADDTEST(s) if (s >= slap_daemon.sd_nfds) slap_daemon.sd_nfds = s+1
253 #define SLAP_SOCK_CLR_READ(fd) FD_CLR(fd, &slap_daemon.sd_readers)
254 #define SLAP_SOCK_CLR_WRITE(fd) FD_CLR(fd, &slap_daemon.sd_writers)
256 #define SLAP_ADD_SOCK(s) do { \
258 FD_SET(s, &slap_daemon.sd_actives); \
259 FD_SET(s, &slap_daemon.sd_readers); \
262 #define SLAP_DEL_SOCK(s) do { \
263 FD_CLR(s, &slap_daemon.sd_actives); \
264 FD_CLR(s, &slap_daemon.sd_readers); \
265 FD_CLR(s, &slap_daemon.sd_writers); \
268 #define SLAP_EVENT_IS_READ(fd) FD_ISSET(fd, &readfds)
269 #define SLAP_EVENT_IS_WRITE(fd) FD_ISSET(fd, &writefds)
271 #define SLAP_EVENT_CLR_READ(fd) FD_CLR(fd, &readfds)
272 #define SLAP_EVENT_CLR_WRITE(fd) FD_CLR(fd, &writefds)
274 #define SLAP_EVENT_WAIT(tvp) \
275 select( SLAP_EVENT_MAX, &readfds, \
276 nwriters > 0 ? &writefds : NULL, NULL, tvp )
278 #define SLAP_SOCK_SET_MUTE(s) FD_CLR(s, &readfds)
279 #define SLAP_SOCK_CLR_MUTE(s) FD_SET(s, &readfds)
280 #define SLAP_SOCK_IS_MUTE(s) FD_ISSET(s, &readfds)
287 * SLP related functions
291 #define LDAP_SRVTYPE_PREFIX "service:ldap://"
292 #define LDAPS_SRVTYPE_PREFIX "service:ldaps://"
293 static char** slapd_srvurls = NULL;
294 static SLPHandle slapd_hslp = 0;
295 int slapd_register_slp = 0;
297 void slapd_slp_init( const char* urls ) {
300 slapd_srvurls = ldap_str2charray( urls, " " );
302 if( slapd_srvurls == NULL ) return;
304 /* find and expand INADDR_ANY URLs */
305 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
306 if( strcmp( slapd_srvurls[i], "ldap:///" ) == 0) {
307 char *host = ldap_pvt_get_fqdn( NULL );
308 if ( host != NULL ) {
309 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
311 sizeof( LDAP_SRVTYPE_PREFIX ) );
312 strcpy( lutil_strcopy(slapd_srvurls[i],
313 LDAP_SRVTYPE_PREFIX ), host );
318 } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0) {
319 char *host = ldap_pvt_get_fqdn( NULL );
320 if ( host != NULL ) {
321 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
323 sizeof( LDAPS_SRVTYPE_PREFIX ) );
324 strcpy( lutil_strcopy(slapd_srvurls[i],
325 LDAPS_SRVTYPE_PREFIX ), host );
332 /* open the SLP handle */
333 SLPOpen( "en", 0, &slapd_hslp );
336 void slapd_slp_deinit() {
337 if( slapd_srvurls == NULL ) return;
339 ldap_charray_free( slapd_srvurls );
340 slapd_srvurls = NULL;
342 /* close the SLP handle */
343 SLPClose( slapd_hslp );
346 void slapd_slp_regreport(
354 void slapd_slp_reg() {
357 if( slapd_srvurls == NULL ) return;
359 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
360 if( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
361 sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
362 strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
363 sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
367 SLP_LIFETIME_MAXIMUM,
377 void slapd_slp_dereg() {
380 if( slapd_srvurls == NULL ) return;
382 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
383 SLPDereg( slapd_hslp,
389 #endif /* HAVE_SLP */
392 * Add a descriptor to daemon control
394 * If isactive, the descriptor is a live server session and is subject
395 * to idletimeout control. Otherwise, the descriptor is a passive
396 * listener or an outbound client session, and not subject to
399 static void slapd_add(ber_socket_t s, int isactive) {
400 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
402 assert( SLAP_SOCK_NOT_ACTIVE(s) );
405 slap_daemon.sd_nactives++;
410 Debug( LDAP_DEBUG_CONNS, "daemon: added %ldr\n",
412 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
416 * Remove the descriptor from daemon control
418 void slapd_remove(ber_socket_t s, int wasactive, int wake) {
420 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
423 slap_daemon.sd_nactives--;
425 waswriter = SLAP_SOCK_IS_WRITE(s);
427 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
428 (long) s, SLAP_SOCK_IS_READ(s) ? "r" : "",
429 waswriter ? "w" : "" );
430 if ( waswriter ) slap_daemon.sd_nwriters--;
434 /* If we ran out of file descriptors, we dropped a listener from
435 * the select() loop. Now that we're removing a session from our
436 * control, we can try to resume a dropped listener to use.
440 for ( i = 0; slap_listeners[i] != NULL; i++ ) {
441 if ( slap_listeners[i]->sl_sd != AC_SOCKET_INVALID ) {
442 if ( slap_listeners[i]->sl_sd == s ) continue;
443 if ( slap_listeners[i]->sl_is_mute ) {
444 slap_listeners[i]->sl_is_mute = 0;
450 /* Walked the entire list without enabling anything; emfile
451 * counter is stale. Reset it.
453 if ( slap_listeners[i] == NULL )
456 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
457 WAKE_LISTENER(wake || slapd_gentle_shutdown == 2);
460 void slapd_clr_write(ber_socket_t s, int wake) {
461 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
463 assert( SLAP_SOCK_IS_ACTIVE( s ));
464 if ( SLAP_SOCK_IS_WRITE( s )) {
465 SLAP_SOCK_CLR_WRITE( s );
466 slap_daemon.sd_nwriters--;
469 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
473 void slapd_set_write(ber_socket_t s, int wake) {
474 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
476 assert( SLAP_SOCK_IS_ACTIVE( s ));
477 if ( !SLAP_SOCK_IS_WRITE( s )) {
478 SLAP_SOCK_SET_WRITE( s );
479 slap_daemon.sd_nwriters++;
482 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
486 void slapd_clr_read(ber_socket_t s, int wake) {
487 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
489 assert( SLAP_SOCK_IS_ACTIVE( s ));
490 SLAP_SOCK_CLR_READ( s );
492 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
496 void slapd_set_read(ber_socket_t s, int wake) {
497 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
499 assert( SLAP_SOCK_IS_ACTIVE( s ));
500 if (!SLAP_SOCK_IS_READ( s ))
501 SLAP_SOCK_SET_READ( s );
503 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
507 static void slapd_close(ber_socket_t s) {
508 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
513 static void slap_free_listener_addresses(struct sockaddr **sal)
515 struct sockaddr **sap;
521 for (sap = sal; *sap != NULL; sap++) {
528 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
529 static int get_url_perms(
541 for ( i = 0; exts[ i ]; i++ ) {
542 char *type = exts[ i ];
545 if ( type[ 0 ] == '!' ) {
550 if ( strncasecmp( type, LDAPI_MOD_URLEXT "=", sizeof(LDAPI_MOD_URLEXT "=") - 1 ) == 0 ) {
552 + ( sizeof(LDAPI_MOD_URLEXT "=") - 1 );
556 switch (strlen(value)) {
558 /* skip leading '0' */
559 if ( value[ 0 ] != '0' ) {
565 for ( j = 0; j < 3; j++) {
568 v = value[ j ] - '0';
570 if ( v < 0 || v > 7 ) {
579 for ( j = 1; j < 10; j++ ) {
580 static mode_t m[] = { 0,
581 S_IRUSR, S_IWUSR, S_IXUSR,
582 S_IRGRP, S_IWGRP, S_IXGRP,
583 S_IROTH, S_IWOTH, S_IXOTH
585 static char c[] = "-rwxrwxrwx";
587 if ( value[ j ] == c[ j ] ) {
590 } else if ( value[ j ] != '-' ) {
609 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
611 /* port = 0 indicates AF_LOCAL */
612 static int slap_get_listener_addresses(
615 struct sockaddr ***sal)
617 struct sockaddr **sap;
621 *sal = ch_malloc(2 * sizeof(void *));
627 *sap = ch_malloc(sizeof(struct sockaddr_un));
633 (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) ) {
634 Debug( LDAP_DEBUG_ANY,
635 "daemon: domain socket path (%s) too long in URL",
640 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
641 (*sap)->sa_family = AF_LOCAL;
642 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
646 #ifdef HAVE_GETADDRINFO
647 struct addrinfo hints, *res, *sai;
651 memset( &hints, '\0', sizeof(hints) );
652 hints.ai_flags = AI_PASSIVE;
653 hints.ai_socktype = SOCK_STREAM;
654 hints.ai_family = slap_inet4or6;
655 snprintf(serv, sizeof serv, "%d", port);
657 if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
658 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
659 AC_GAI_STRERROR(err), 0, 0);
664 for (n=2; (sai = sai->ai_next) != NULL; n++) {
667 *sal = ch_calloc(n, sizeof(void *));
675 for ( sai=res; sai; sai=sai->ai_next ) {
676 if( sai->ai_addr == NULL ) {
677 Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
678 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
683 switch (sai->ai_family) {
684 # ifdef LDAP_PF_INET6
686 *sap = ch_malloc(sizeof(struct sockaddr_in6));
691 *(struct sockaddr_in6 *)*sap =
692 *((struct sockaddr_in6 *)sai->ai_addr);
696 *sap = ch_malloc(sizeof(struct sockaddr_in));
701 *(struct sockaddr_in *)*sap =
702 *((struct sockaddr_in *)sai->ai_addr);
710 (*sap)->sa_family = sai->ai_family;
720 struct hostent *he = NULL;
722 if ( host == NULL ) {
723 in.s_addr = htonl(INADDR_ANY);
725 } else if ( !inet_aton( host, &in ) ) {
726 he = gethostbyname( host );
728 Debug( LDAP_DEBUG_ANY,
729 "daemon: invalid host %s", host, 0, 0);
732 for (n = 0; he->h_addr_list[n]; n++) ;
735 *sal = ch_malloc((n+1) * sizeof(void *));
741 for ( i = 0; i<n; i++ ) {
742 sap[i] = ch_malloc(sizeof(struct sockaddr_in));
746 (void)memset( (void *)sap[i], '\0', sizeof(struct sockaddr_in) );
747 sap[i]->sa_family = AF_INET;
748 ((struct sockaddr_in *)sap[i])->sin_port = htons(port);
750 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr, he->h_addr_list[i], sizeof(struct in_addr) );
752 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr, &in, sizeof(struct in_addr) );
762 slap_free_listener_addresses(*sal);
766 static int slap_open_listener(
777 int err, addrlen = 0;
778 struct sockaddr **sal, **psal;
779 int socktype = SOCK_STREAM; /* default to COTS */
781 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
786 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
788 rc = ldap_url_parse( url, &lud );
790 if( rc != LDAP_URL_SUCCESS ) {
791 Debug( LDAP_DEBUG_ANY,
792 "daemon: listen URL \"%s\" parse error=%d\n",
797 l.sl_url.bv_val = NULL;
801 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
802 Debug( LDAP_DEBUG_ANY,
803 "daemon: TLS not supported (%s)\n",
805 ldap_free_urldesc( lud );
809 if(! lud->lud_port ) {
810 lud->lud_port = LDAP_PORT;
814 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
816 if(! lud->lud_port ) {
817 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
821 port = (unsigned short) lud->lud_port;
823 tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme);
824 if ( tmp == LDAP_PROTO_IPC ) {
826 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
827 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
829 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
833 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
835 ldap_free_urldesc( lud );
839 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
840 || strcmp(lud->lud_host, "*") == 0 )
842 err = slap_get_listener_addresses(NULL, port, &sal);
844 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
847 #ifdef LDAP_CONNECTIONLESS
848 l.sl_is_udp = ( tmp == LDAP_PROTO_UDP );
851 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
852 if ( lud->lud_exts ) {
853 err = get_url_perms( lud->lud_exts, &l.sl_perms, &crit );
855 l.sl_perms = S_IRWXU | S_IRWXO;
857 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
859 ldap_free_urldesc( lud );
864 /* If we got more than one address returned, we need to make space
865 * for it in the slap_listeners array.
867 for ( num=0; sal[num]; num++ );
870 slap_listeners = ch_realloc( slap_listeners, (*listeners + 1) * sizeof(Listener *) );
874 while ( *sal != NULL ) {
876 switch( (*sal)->sa_family ) {
894 #ifdef LDAP_CONNECTIONLESS
895 if( l.sl_is_udp ) socktype = SOCK_DGRAM;
897 l.sl_sd = socket( (*sal)->sa_family, socktype, 0);
898 if ( l.sl_sd == AC_SOCKET_INVALID ) {
899 int err = sock_errno();
900 Debug( LDAP_DEBUG_ANY,
901 "daemon: %s socket() failed errno=%d (%s)\n",
902 af, err, sock_errstr(err) );
907 if ( l.sl_sd >= dtblsize ) {
908 Debug( LDAP_DEBUG_ANY,
909 "daemon: listener descriptor %ld is too great %ld\n",
910 (long) l.sl_sd, (long) dtblsize, 0 );
911 tcp_close( l.sl_sd );
917 if ( (*sal)->sa_family == AF_LOCAL ) {
918 unlink ( ((struct sockaddr_un *)*sal)->sun_path );
923 /* enable address reuse */
925 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
926 (char *) &tmp, sizeof(tmp) );
927 if ( rc == AC_SOCKET_ERROR ) {
928 int err = sock_errno();
929 Debug( LDAP_DEBUG_ANY,
930 "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
931 (long) l.sl_sd, err, sock_errstr(err) );
936 switch( (*sal)->sa_family ) {
938 addrlen = sizeof(struct sockaddr_in);
943 /* Try to use IPv6 sockets for IPv6 only */
945 rc = setsockopt( l.sl_sd, IPPROTO_IPV6, IPV6_V6ONLY,
946 (char *) &tmp, sizeof(tmp) );
947 if ( rc == AC_SOCKET_ERROR ) {
948 int err = sock_errno();
949 Debug( LDAP_DEBUG_ANY,
950 "slapd(%ld): setsockopt(IPV6_V6ONLY) failed errno=%d (%s)\n",
951 (long) l.sl_sd, err, sock_errstr(err) );
954 addrlen = sizeof(struct sockaddr_in6);
959 addrlen = sizeof(struct sockaddr_un);
964 if (bind(l.sl_sd, *sal, addrlen)) {
966 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
967 (long) l.sl_sd, err, sock_errstr(err) );
968 tcp_close( l.sl_sd );
973 switch ( (*sal)->sa_family ) {
976 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
977 #if 0 /* don't muck with socket perms */
978 if ( chmod( addr, l.sl_perms ) < 0 && crit ) {
979 int err = sock_errno();
980 Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
981 (long) l.sl_sd, err, sock_errstr(err) );
982 tcp_close( l.sl_sd );
983 slap_free_listener_addresses(psal);
987 l.sl_name.bv_len = strlen(addr) + sizeof("PATH=") - 1;
988 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len + 1 );
989 snprintf( l.sl_name.bv_val, l.sl_name.bv_len + 1,
992 #endif /* LDAP_PF_LOCAL */
996 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
997 char addr[INET_ADDRSTRLEN];
998 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
999 addr, sizeof(addr) );
1002 s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr );
1004 port = ntohs( ((struct sockaddr_in *)*sal) ->sin_port );
1005 l.sl_name.bv_val = ber_memalloc( sizeof("IP=255.255.255.255:65535") );
1006 snprintf( l.sl_name.bv_val, sizeof("IP=255.255.255.255:65535"),
1008 s != NULL ? s : SLAP_STRING_UNKNOWN, port );
1009 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1012 #ifdef LDAP_PF_INET6
1014 char addr[INET6_ADDRSTRLEN];
1015 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
1017 port = ntohs( ((struct sockaddr_in6 *)*sal)->sin6_port );
1018 l.sl_name.bv_len = strlen(addr) + sizeof("IP= 65535");
1019 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len );
1020 snprintf( l.sl_name.bv_val, l.sl_name.bv_len, "IP=%s %d",
1022 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1024 #endif /* LDAP_PF_INET6 */
1027 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
1028 (int) (*sal)->sa_family, 0, 0 );
1032 AC_MEMCPY(&l.sl_sa, *sal, addrlen);
1033 ber_str2bv( url, 0, 1, &l.sl_url);
1034 li = ch_malloc( sizeof( Listener ) );
1036 slap_listeners[*cur] = li;
1040 } /* while ( *sal != NULL ) */
1042 slap_free_listener_addresses(psal);
1044 if ( l.sl_url.bv_val == NULL )
1046 Debug( LDAP_DEBUG_TRACE,
1047 "slap_open_listener: failed on %s\n", url, 0, 0 );
1051 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
1052 l.sl_url.bv_val, 0, 0 );
1056 static int sockinit(void);
1057 static int sockdestroy(void);
1059 int slapd_daemon_init( const char *urls )
1064 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
1065 urls ? urls : "<null>", 0, 0 );
1066 if( (rc = sockinit()) != 0 ) {
1071 dtblsize = sysconf( _SC_OPEN_MAX );
1072 #elif HAVE_GETDTABLESIZE
1073 dtblsize = getdtablesize();
1075 dtblsize = FD_SETSIZE;
1078 /* open a pipe (or something equivalent connected to itself).
1079 * we write a byte on this fd whenever we catch a signal. The main
1080 * loop will be select'ing on this socket, and will wake up when
1081 * this byte arrives.
1083 if( (rc = lutil_pair( wake_sds )) < 0 ) {
1084 Debug( LDAP_DEBUG_ANY,
1085 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
1091 if( urls == NULL ) {
1095 u = ldap_str2charray( urls, " " );
1097 if( u == NULL || u[0] == NULL ) {
1098 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
1103 for( i=0; u[i] != NULL; i++ ) {
1104 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
1109 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
1111 ldap_charray_free( u );
1115 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
1117 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
1119 for(n = 0, j = 0; u[n]; n++ ) {
1120 if ( slap_open_listener( u[n], &i, &j ) ) {
1121 ldap_charray_free( u );
1125 slap_listeners[j] = NULL;
1127 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
1131 if( slapd_register_slp ) {
1132 slapd_slp_init( urls );
1137 ldap_charray_free( u );
1138 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
1144 slapd_daemon_destroy(void)
1146 connections_destroy();
1147 tcp_close( wake_sds[1] );
1148 tcp_close( wake_sds[0] );
1152 if( slapd_register_slp ) {
1169 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1170 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1172 slapd_remove( slap_listeners[l]->sl_sd, 0, 0 );
1173 #ifdef LDAP_PF_LOCAL
1174 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1175 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1177 #endif /* LDAP_PF_LOCAL */
1178 slapd_close( slap_listeners[l]->sl_sd );
1180 if ( slap_listeners[l]->sl_url.bv_val )
1181 ber_memfree( slap_listeners[l]->sl_url.bv_val );
1182 if ( slap_listeners[l]->sl_name.bv_val )
1183 ber_memfree( slap_listeners[l]->sl_name.bv_val );
1184 free ( slap_listeners[l] );
1185 slap_listeners[l] = NULL;
1190 slapd_handle_listener(
1197 socklen_t len = sizeof(from);
1200 struct berval authid = BER_BVNULL;
1201 #ifdef SLAPD_RLOOKUPS
1202 char hbuf[NI_MAXHOST];
1205 char *dnsname = NULL;
1206 char *peeraddr = NULL;
1207 #ifdef LDAP_PF_LOCAL
1208 char peername[MAXPATHLEN + sizeof("PATH=")];
1209 #elif defined(LDAP_PF_INET6)
1210 char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
1212 char peername[sizeof("IP=255.255.255.255:65336")];
1213 #endif /* LDAP_PF_LOCAL */
1217 #ifdef LDAP_CONNECTIONLESS
1218 if ( sl->sl_is_udp ) {
1219 /* The first time we receive a query, we set this
1220 * up as a "connection". It remains open for the life
1223 if ( sl->sl_is_udp < 2 ) {
1224 id = connection_init( sl->sl_sd, sl, "", "",
1225 CONN_IS_UDP, ssf, NULL );
1232 # ifdef LDAP_PF_LOCAL
1233 /* FIXME: apparently accept doesn't fill
1234 * the sun_path sun_path member */
1235 from.sa_un_addr.sun_path[0] = '\0';
1236 # endif /* LDAP_PF_LOCAL */
1238 s = accept( sl->sl_sd, (struct sockaddr *) &from, &len );
1239 if ( s == AC_SOCKET_INVALID ) {
1240 int err = sock_errno();
1251 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1253 /* Stop listening until an existing session closes */
1255 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1258 Debug( LDAP_DEBUG_ANY,
1259 "daemon: accept(%ld) failed errno=%d (%s)\n",
1260 (long) sl->sl_sd, err,
1262 ldap_pvt_thread_yield();
1266 #ifndef HAVE_WINSOCK
1267 /* make sure descriptor number isn't too great */
1268 if ( s >= dtblsize ) {
1269 Debug( LDAP_DEBUG_ANY,
1270 "daemon: %ld beyond descriptor table size %ld\n",
1271 (long) s, (long) dtblsize, 0 );
1274 ldap_pvt_thread_yield();
1280 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1282 /* newly accepted stream should not be in any of the FD SETS */
1283 assert( SLAP_SOCK_NOT_ACTIVE( s ));
1285 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1288 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1289 #ifdef LDAP_PF_LOCAL
1290 /* for IPv4 and IPv6 sockets only */
1291 if ( from.sa_addr.sa_family != AF_LOCAL )
1292 #endif /* LDAP_PF_LOCAL */
1297 /* enable keep alives */
1299 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1300 (char *) &tmp, sizeof(tmp) );
1301 if ( rc == AC_SOCKET_ERROR ) {
1302 int err = sock_errno();
1303 Debug( LDAP_DEBUG_ANY,
1304 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1305 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1309 /* enable no delay */
1311 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1312 (char *)&tmp, sizeof(tmp) );
1313 if ( rc == AC_SOCKET_ERROR ) {
1314 int err = sock_errno();
1315 Debug( LDAP_DEBUG_ANY,
1316 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1317 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1323 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
1325 switch ( from.sa_addr.sa_family ) {
1326 # ifdef LDAP_PF_LOCAL
1328 /* FIXME: apparently accept doesn't fill
1329 * the sun_path sun_path member */
1330 if ( from.sa_un_addr.sun_path[0] == '\0' ) {
1331 AC_MEMCPY( from.sa_un_addr.sun_path,
1332 sl->sl_sa.sa_un_addr.sun_path,
1333 sizeof( from.sa_un_addr.sun_path ) );
1336 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1342 if( getpeereid( s, &uid, &gid ) == 0 ) {
1343 authid.bv_val = ch_malloc(
1344 sizeof("uidnumber=4294967295+gidnumber=4294967295,"
1345 "cn=peercred,cn=external,cn=auth"));
1346 authid.bv_len = sprintf( authid.bv_val,
1347 "uidnumber=%d+gidnumber=%d,"
1348 "cn=peercred,cn=external,cn=auth",
1349 (int) uid, (int) gid);
1354 #endif /* LDAP_PF_LOCAL */
1356 # ifdef LDAP_PF_INET6
1358 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1359 peeraddr = inet_ntoa( *((struct in_addr *)
1360 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1361 sprintf( peername, "IP=%s:%d",
1362 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1363 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1365 char addr[INET6_ADDRSTRLEN];
1367 peeraddr = (char *) inet_ntop( AF_INET6,
1368 &from.sa_in6_addr.sin6_addr,
1369 addr, sizeof addr );
1370 sprintf( peername, "IP=%s %d",
1371 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1372 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1375 # endif /* LDAP_PF_INET6 */
1378 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1379 sprintf( peername, "IP=%s:%d",
1380 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1381 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1389 if ( ( from.sa_addr.sa_family == AF_INET )
1390 #ifdef LDAP_PF_INET6
1391 || ( from.sa_addr.sa_family == AF_INET6 )
1394 #ifdef SLAPD_RLOOKUPS
1395 if ( use_reverse_lookup ) {
1397 if (ldap_pvt_get_hname( (const struct sockaddr *)&from, len, hbuf,
1398 sizeof(hbuf), &herr ) == 0) {
1399 ldap_pvt_str2lower( hbuf );
1405 #endif /* SLAPD_RLOOKUPS */
1408 if ( !hosts_ctl("slapd",
1409 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1410 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1411 SLAP_STRING_UNKNOWN ))
1414 Statslog( LDAP_DEBUG_STATS,
1415 "fd=%ld DENIED from %s (%s)\n",
1417 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1418 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1423 #endif /* HAVE_TCPD */
1426 id = connection_init(s, sl,
1427 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1430 sl->sl_is_tls ? CONN_IS_TLS : 0,
1435 authid.bv_val ? &authid : NULL );
1437 if( authid.bv_val ) ch_free(authid.bv_val);
1440 Debug( LDAP_DEBUG_ANY,
1441 "daemon: connection_init(%ld, %s, %s) failed.\n",
1442 (long) s, peername, sl->sl_name.bv_val );
1447 Statslog( LDAP_DEBUG_STATS,
1448 "conn=%ld fd=%ld ACCEPT from %s (%s)\n",
1449 id, (long) s, peername, sl->sl_name.bv_val,
1462 time_t last_idle_check = 0;
1463 struct timeval idle;
1465 #define SLAPD_IDLE_CHECK_LIMIT 4
1467 if ( global_idletimeout > 0 ) {
1468 last_idle_check = slap_get_time();
1469 /* Set the select timeout.
1470 * Don't just truncate, preserve the fractions of
1471 * seconds to prevent sleeping for zero time.
1473 idle.tv_sec = global_idletimeout/SLAPD_IDLE_CHECK_LIMIT;
1474 idle.tv_usec = global_idletimeout - idle.tv_sec * SLAPD_IDLE_CHECK_LIMIT;
1475 idle.tv_usec *= 1000000 / SLAPD_IDLE_CHECK_LIMIT;
1481 slapd_add( wake_sds[0], 0 );
1483 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1484 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1486 #ifdef LDAP_CONNECTIONLESS
1487 /* Since this is connectionless, the data port is the
1488 * listening port. The listen() and accept() calls
1491 if ( slap_listeners[l]->sl_is_udp ) {
1492 slapd_add( slap_listeners[l]->sl_sd, 1 );
1497 if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN ) == -1 ) {
1498 int err = sock_errno();
1500 #ifdef LDAP_PF_INET6
1501 /* If error is EADDRINUSE, we are trying to listen to INADDR_ANY and
1502 * we are already listening to in6addr_any, then we want to ignore
1503 * this and continue.
1505 if ( err == EADDRINUSE ) {
1507 struct sockaddr_in sa = slap_listeners[l]->sl_sa.sa_in_addr;
1508 struct sockaddr_in6 sa6;
1510 if ( sa.sin_family == AF_INET &&
1511 sa.sin_addr.s_addr == htonl(INADDR_ANY) ) {
1512 for ( i = 0 ; i < l; i++ ) {
1513 sa6 = slap_listeners[i]->sl_sa.sa_in6_addr;
1514 if ( sa6.sin6_family == AF_INET6 &&
1515 !memcmp( &sa6.sin6_addr, &in6addr_any, sizeof(struct in6_addr) ) )
1520 /* We are already listening to in6addr_any */
1521 Debug( LDAP_DEBUG_CONNS,
1522 "daemon: Attempt to listen to 0.0.0.0 failed, already listening on ::, assuming IPv4 included\n",
1524 slapd_close( slap_listeners[l]->sl_sd );
1525 slap_listeners[l]->sl_sd = AC_SOCKET_INVALID;
1531 Debug( LDAP_DEBUG_ANY,
1532 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
1533 slap_listeners[l]->sl_url.bv_val, err,
1535 return( (void*)-1 );
1538 slapd_add( slap_listeners[l]->sl_sd, 0 );
1541 #ifdef HAVE_NT_SERVICE_MANAGER
1542 if ( started_event != NULL ) {
1543 ldap_pvt_thread_cond_signal( &started_event );
1546 /* initialization complete. Here comes the loop. */
1548 while ( !slapd_shutdown ) {
1552 ber_socket_t nfds, nrfds, nwfds;
1553 #define SLAPD_EBADF_LIMIT 16
1561 struct timeval *tvp;
1563 struct timeval *cat;
1566 now = slap_get_time();
1568 if( ( global_idletimeout > 0 ) &&
1569 difftime( last_idle_check +
1570 global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 ) {
1571 connections_timeout_idle( now );
1572 last_idle_check = now;
1577 if( slapd_gentle_shutdown ) {
1578 ber_socket_t active;
1580 if( slapd_gentle_shutdown == 1 ) {
1581 Debug( LDAP_DEBUG_ANY, "slapd gentle shutdown\n", 0, 0, 0 );
1582 close_listeners( 1 );
1583 frontendDB->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
1584 slapd_gentle_shutdown = 2;
1587 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1588 active = slap_daemon.sd_nactives;
1589 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1599 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1601 nwriters = slap_daemon.sd_nwriters;
1604 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1605 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1607 if ( slap_listeners[l]->sl_is_mute )
1608 SLAP_SOCK_SET_MUTE( slap_listeners[l]->sl_sd );
1610 if ( SLAP_SOCK_IS_MUTE( slap_listeners[l]->sl_sd ))
1611 SLAP_SOCK_CLR_MUTE( slap_listeners[l]->sl_sd );
1614 nfds = SLAP_EVENT_MAX;
1616 if ( global_idletimeout && slap_daemon.sd_nactives )
1619 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1622 #if defined(HAVE_YIELDING_SELECT) || defined(NO_THREADS)
1623 && ( tv.tv_sec || tv.tv_usec )
1630 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
1631 rtask = ldap_pvt_runqueue_next_sched( &syncrepl_rq, &cat );
1632 while ( cat && cat->tv_sec && cat->tv_sec <= now ) {
1633 if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
1634 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, 0 );
1636 ldap_pvt_runqueue_runtask( &syncrepl_rq, rtask );
1637 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, 0 );
1638 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
1639 ldap_pvt_thread_pool_submit( &connection_pool,
1640 rtask->routine, (void *) rtask );
1641 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
1643 rtask = ldap_pvt_runqueue_next_sched( &syncrepl_rq, &cat );
1645 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
1647 if ( cat != NULL ) {
1648 time_t diff = difftime( cat->tv_sec, now );
1651 if ( tvp == NULL || diff < tv.tv_sec ) {
1658 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1659 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ||
1660 slap_listeners[l]->sl_is_mute )
1663 Debug( LDAP_DEBUG_CONNS,
1664 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
1665 slap_listeners[l]->sl_sd, at,
1666 tvp == NULL ? "NULL" : "zero" );
1669 switch(ns = SLAP_EVENT_WAIT(tvp))
1671 case -1: { /* failure - try again */
1672 int err = sock_errno();
1676 /* you'd think this would be EBADF */
1677 || err == WSAENOTSOCK
1680 if (++ebadf < SLAPD_EBADF_LIMIT)
1684 if( err != EINTR ) {
1685 Debug( LDAP_DEBUG_CONNS,
1686 "daemon: select failed (%d): %s\n",
1687 err, sock_errstr(err), 0 );
1693 case 0: /* timeout - let threads run */
1695 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
1698 ldap_pvt_thread_yield();
1701 default: /* something happened - deal with it */
1702 if( slapd_shutdown ) continue;
1705 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
1710 /* We don't need to examine the event status for wake_sds;
1711 * if waking is set and we woke up, we'll read whatever
1716 tcp_read( wake_sds[0], c, sizeof(c) );
1722 #if SLAP_EVENTS_ARE_INDEXED
1723 /* The event slot equals the descriptor number - this is
1724 * true for Unix select and poll. We treat Windows select
1725 * like this too, even though it's a kludge.
1727 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1730 if ( ns <= 0 ) break;
1732 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1735 if ( !SLAP_EVENT_IS_READ( slap_listeners[l]->sl_sd ))
1740 rc = slapd_handle_listener(slap_listeners[l]);
1742 #ifdef LDAP_CONNECTIONLESS
1746 /* Don't need to look at this in the data loops */
1747 SLAP_EVENT_CLR_READ( slap_listeners[l]->sl_sd );
1748 SLAP_EVENT_CLR_WRITE( slap_listeners[l]->sl_sd );
1751 /* bypass the following tests if no descriptors left */
1753 ldap_pvt_thread_yield();
1757 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1759 nrfds = readfds.fd_count;
1760 nwfds = writefds.fd_count;
1761 for ( i = 0; i < readfds.fd_count; i++ ) {
1762 Debug( LDAP_DEBUG_CONNS, " %d%s",
1763 readfds.fd_array[i], "r", 0 );
1765 for ( i = 0; i < writefds.fd_count; i++ ) {
1766 Debug( LDAP_DEBUG_CONNS, " %d%s",
1767 writefds.fd_array[i], "w", 0 );
1773 for ( i = 0; i < nfds; i++ ) {
1776 r = SLAP_EVENT_IS_READ( i );
1777 w = SLAP_EVENT_IS_WRITE( i );
1779 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1780 r ? "r" : "", w ? "w" : "" );
1790 if ( ns <= 0 ) break;
1793 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1796 /* loop through the writers */
1797 for ( i = 0; nwfds > 0; i++ )
1801 wd = writefds.fd_array[i];
1803 if( ! SLAP_EVENT_IS_WRITE( i ) ) {
1810 Debug( LDAP_DEBUG_CONNS,
1811 "daemon: write active on %d\n",
1814 * NOTE: it is possible that the connection was closed
1815 * and that the stream is now inactive.
1816 * connection_write() must valid the stream is still
1820 if ( connection_write( wd ) < 0 ) {
1821 if ( SLAP_EVENT_IS_READ( wd )) {
1822 SLAP_EVENT_CLR_READ( (unsigned) wd );
1829 for ( i = 0; nrfds > 0; i++ )
1833 rd = readfds.fd_array[i];
1835 if( ! SLAP_EVENT_IS_READ( i ) ) {
1842 Debug ( LDAP_DEBUG_CONNS,
1843 "daemon: read activity on %d\n", rd, 0, 0 );
1845 * NOTE: it is possible that the connection was closed
1846 * and that the stream is now inactive.
1847 * connection_read() must valid the stream is still
1851 if ( connection_read( rd ) < 0 ) {
1855 #else /* !SLAP_EVENTS_ARE_INDEXED */
1857 /* The events are returned in an arbitrary list. This is true
1858 * for /dev/poll, epoll and kqueue. In order to prioritize things
1859 * so that we can handle wake_sds first, listeners second, and then
1860 * all other connections last (as we do for select), we would need
1861 * to use multiple event handles and cascade them.
1863 * That seems like a bit of hassle. So the wake_sds check has moved
1864 * above. For epoll and kqueue we can associate arbitrary data with
1865 * an event, so we could use pointers to the listener structure
1866 * instead of just the file descriptor. For /dev/poll we have to
1867 * search the listeners array for a matching descriptor.
1870 ldap_pvt_thread_yield();
1873 if( slapd_shutdown == 1 ) {
1874 Debug( LDAP_DEBUG_TRACE,
1875 "daemon: shutdown requested and initiated.\n",
1878 } else if ( slapd_shutdown == 2 ) {
1879 #ifdef HAVE_NT_SERVICE_MANAGER
1880 Debug( LDAP_DEBUG_TRACE,
1881 "daemon: shutdown initiated by Service Manager.\n",
1883 #else /* !HAVE_NT_SERVICE_MANAGER */
1884 Debug( LDAP_DEBUG_TRACE,
1885 "daemon: abnormal condition, shutdown initiated.\n",
1887 #endif /* !HAVE_NT_SERVICE_MANAGER */
1889 Debug( LDAP_DEBUG_TRACE,
1890 "daemon: no active streams, shutdown initiated.\n",
1894 if( slapd_gentle_shutdown != 2 ) {
1895 close_listeners ( 0 );
1898 free ( slap_listeners );
1899 slap_listeners = NULL;
1901 if( !slapd_gentle_shutdown ) {
1902 slapd_abrupt_shutdown = 1;
1903 connections_shutdown();
1906 Debug( LDAP_DEBUG_ANY,
1907 "slapd shutdown: waiting for %d threads to terminate\n",
1908 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1909 ldap_pvt_thread_pool_destroy(&connection_pool, 1);
1915 int slapd_daemon( void )
1921 #define SLAPD_LISTENER_THREAD 1
1922 #if defined( SLAPD_LISTENER_THREAD )
1924 ldap_pvt_thread_t listener_tid;
1926 /* listener as a separate THREAD */
1927 rc = ldap_pvt_thread_create( &listener_tid,
1928 0, slapd_daemon_task, NULL );
1931 Debug( LDAP_DEBUG_ANY,
1932 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
1936 /* wait for the listener thread to complete */
1937 ldap_pvt_thread_join( listener_tid, (void *) NULL );
1940 /* experimental code */
1941 slapd_daemon_task( NULL );
1950 #if defined( HAVE_WINSOCK2 )
1951 WORD wVersionRequested;
1955 wVersionRequested = MAKEWORD( 2, 0 );
1957 err = WSAStartup( wVersionRequested, &wsaData );
1959 /* Tell the user that we couldn't find a usable */
1964 /* Confirm that the WinSock DLL supports 2.0.*/
1965 /* Note that if the DLL supports versions greater */
1966 /* than 2.0 in addition to 2.0, it will still return */
1967 /* 2.0 in wVersion since that is the version we */
1970 if ( LOBYTE( wsaData.wVersion ) != 2 ||
1971 HIBYTE( wsaData.wVersion ) != 0 )
1973 /* Tell the user that we couldn't find a usable */
1979 /* The WinSock DLL is acceptable. Proceed. */
1980 #elif defined( HAVE_WINSOCK )
1982 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
1989 int sockdestroy(void)
1991 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
1998 slap_sig_shutdown( int sig )
2001 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
2005 * If the NT Service Manager is controlling the server, we don't
2006 * want SIGBREAK to kill the server. For some strange reason,
2007 * SIGBREAK is generated when a user logs out.
2010 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
2011 if (is_NT_Service && sig == SIGBREAK)
2016 if (sig == SIGHUP && global_gentlehup && slapd_gentle_shutdown == 0)
2017 slapd_gentle_shutdown = 1;
2023 /* reinstall self */
2024 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
2028 slap_sig_wake( int sig )
2032 /* reinstall self */
2033 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
2037 void slapd_add_internal(ber_socket_t s, int isactive) {
2038 slapd_add(s, isactive);
2041 Listener ** slapd_get_listeners(void) {
2042 return slap_listeners;
2045 void slap_wake_listener()