2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 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"
45 #if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL)
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;
75 struct runqueue_s slapd_rq;
77 Listener **slap_listeners = NULL;
79 #ifndef SLAPD_LISTEN_BACKLOG
80 #define SLAPD_LISTEN_BACKLOG 1024
83 static ber_socket_t wake_sds[2];
87 #define WAKE_LISTENER(w) \
88 do { if (w && waking < 5) { waking++; tcp_write( wake_sds[1], "0", 1 ); } } while(0)
90 volatile sig_atomic_t slapd_shutdown = 0, slapd_gentle_shutdown = 0;
91 volatile sig_atomic_t slapd_abrupt_shutdown = 0;
93 static struct slap_daemon {
94 ldap_pvt_thread_mutex_t sd_mutex;
96 ber_socket_t sd_nactives;
100 struct epoll_event *sd_epolls;
108 /* In winsock, accept() returns values higher than dtblsize
109 so don't bother with this optimization */
120 #define SLAP_EVENTS_ARE_INDEXED 0
121 #define SLAP_SOCK_IX(s) (slap_daemon.sd_index[s])
122 #define SLAP_SOCK_EP(s) (slap_daemon.sd_epolls[SLAP_SOCK_IX(s)])
123 #define SLAP_SOCK_EV(s) (SLAP_SOCK_EP(s).events)
124 #define SLAP_SOCK_IS_ACTIVE(s) (SLAP_SOCK_IX(s) != -1)
125 #define SLAP_SOCK_NOT_ACTIVE(s) (SLAP_SOCK_IX(s) == -1)
126 #define SLAP_SOCK_IS_SET(s, mode) (SLAP_SOCK_EV(s) & mode)
128 #define SLAP_SOCK_IS_READ(s) SLAP_SOCK_IS_SET(s, EPOLLIN)
129 #define SLAP_SOCK_IS_WRITE(s) SLAP_SOCK_IS_SET(s, EPOLLOUT)
131 #define SLAP_SET_SOCK(s, mode) do { \
132 if ((SLAP_SOCK_EV(s) & mode) != mode) { \
133 SLAP_SOCK_EV(s) |= mode; \
134 epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_MOD, s, \
139 #define SLAP_CLR_SOCK(s, mode) do { \
140 if ((SLAP_SOCK_EV(s) & mode)) { \
141 SLAP_SOCK_EV(s) &= ~mode; \
142 epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_MOD, s, \
147 #define SLAP_SOCK_SET_READ(s) SLAP_SET_SOCK(s, EPOLLIN)
148 #define SLAP_SOCK_SET_WRITE(s) SLAP_SET_SOCK(s, EPOLLOUT)
150 #define SLAP_SOCK_CLR_READ(s) SLAP_CLR_SOCK(s, EPOLLIN)
151 #define SLAP_SOCK_CLR_WRITE(s) SLAP_CLR_SOCK(s, EPOLLOUT)
153 #define SLAP_CLR_EVENT(i, mode) (revents[i].events &= ~mode)
156 #define SLAP_EVENT_MAX slap_daemon.sd_nfds
158 /* If a Listener address is provided, store that as the epoll data.
159 * Otherwise, store the address of this socket's slot in the
160 * index array. If we can't do this add, the system is out of
161 * resources and we need to shutdown.
163 #define SLAP_ADD_SOCK(s, l) do { \
165 SLAP_SOCK_IX(s) = slap_daemon.sd_nfds; \
166 SLAP_SOCK_EP(s).data.ptr = (l) ? (l) : (void *)(&SLAP_SOCK_IX(s)); \
167 SLAP_SOCK_EV(s) = EPOLLIN; \
168 rc = epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_ADD, s, \
170 if ( rc == 0 ) slap_daemon.sd_nfds++; \
172 Debug( LDAP_DEBUG_ANY, "daemon: epoll_ctl ADD failed, errno %d, shutting down\n", \
174 slapd_shutdown = 2; \
178 #define SLAP_EV_LISTENER(ptr) (((int *)(ptr) >= slap_daemon.sd_index && \
179 (int *)(ptr) <= (slap_daemon.sd_index+dtblsize)) ? 0 : 1)
181 #define SLAP_EV_PTRFD(ptr) (SLAP_EV_LISTENER(ptr) ? \
182 ((Listener *)ptr)->sl_sd : (int *)(ptr) - slap_daemon.sd_index)
184 #define SLAP_DEL_SOCK(s) do { \
185 int fd, rc, index = SLAP_SOCK_IX(s); \
186 rc = epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_DEL, s, \
188 slap_daemon.sd_epolls[index] = slap_daemon.sd_epolls[slap_daemon.sd_nfds-1]; \
189 fd = SLAP_EV_PTRFD(slap_daemon.sd_epolls[index].data.ptr); \
190 slap_daemon.sd_index[fd] = index; \
191 slap_daemon.sd_index[s] = -1; \
192 slap_daemon.sd_nfds--; \
195 #define SLAP_EVENT_CLR_READ(i) SLAP_CLR_EVENT(i, EPOLLIN)
196 #define SLAP_EVENT_CLR_WRITE(i) SLAP_CLR_EVENT(i, EPOLLOUT)
198 #define SLAP_CHK_EVENT(i, mode) (revents[i].events & mode)
200 #define SLAP_EVENT_IS_READ(i) SLAP_CHK_EVENT(i, EPOLLIN)
201 #define SLAP_EVENT_IS_WRITE(i) SLAP_CHK_EVENT(i, EPOLLOUT)
202 #define SLAP_EVENT_IS_LISTENER(i) SLAP_EV_LISTENER(revents[i].data.ptr)
203 #define SLAP_EVENT_LISTENER(i) (revents[i].data.ptr)
205 #define SLAP_EVENT_FD(i) SLAP_EV_PTRFD(revents[i].data.ptr)
206 #define SLAP_SOCK_SET_MUTE(s) SLAP_SOCK_CLR_READ(s)
207 #define SLAP_SOCK_CLR_MUTE(s) SLAP_SOCK_SET_READ(s)
208 #define SLAP_SOCK_IS_MUTE(s) !SLAP_SOCK_IS_READ(s)
210 #define SLAP_SOCK_SET_INIT \
211 slap_daemon.sd_epolls = ch_malloc(sizeof(struct epoll_event) * dtblsize * 2); \
212 slap_daemon.sd_index = ch_malloc(sizeof(int) * dtblsize); \
213 slap_daemon.sd_epfd = epoll_create( dtblsize ); \
214 for (i=0; i<dtblsize; i++) slap_daemon.sd_index[i] = -1
217 #define SLAP_EVENT_DECL \
218 struct epoll_event *revents
220 #define SLAP_EVENT_INIT \
221 revents = slap_daemon.sd_epolls + dtblsize; \
223 #define SLAP_EVENT_WAIT(tvp) \
224 epoll_wait( slap_daemon.sd_epfd, revents, dtblsize, tvp ? tvp->tv_sec * 1000 : -1 )
229 #define SLAP_EVENTS_ARE_INDEXED 1
230 #define SLAP_EVENT_DECL \
231 fd_set readfds, writefds
233 #define SLAP_EVENT_INIT \
234 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) ); \
236 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) )
239 #define CHK_SETSIZE \
240 if (dtblsize > FD_SETSIZE) dtblsize = FD_SETSIZE
245 #define SLAP_SOCK_SET_INIT \
247 FD_ZERO(&slap_daemon.sd_readers); \
248 FD_ZERO(&slap_daemon.sd_writers)
250 #define SLAP_SOCK_IS_ACTIVE(fd) FD_ISSET(fd, &slap_daemon.sd_actives)
252 #define SLAP_SOCK_IS_READ(fd) FD_ISSET(fd, &slap_daemon.sd_readers)
253 #define SLAP_SOCK_IS_WRITE(fd) FD_ISSET(fd, &slap_daemon.sd_writers)
255 #define SLAP_SOCK_NOT_ACTIVE(fd) (!SLAP_SOCK_IS_ACTIVE(fd) && \
256 !SLAP_SOCK_IS_READ(fd) && !SLAP_SOCK_IS_WRITE(fd))
259 #define SLAP_SOCK_SET_READ(fd) do { \
260 if (!SLAP_SOCK_IS_READ(fd)) {FD_SET(fd, &slap_daemon.sd_readers);} \
262 #define SLAP_SOCK_SET_WRITE(fd) do { \
263 if (!SLAP_SOCK_IS_WRITE(fd)) {FD_SET(fd, &slap_daemon.sd_writers);} \
266 #define SLAP_ADDTEST(s)
267 #define SLAP_EVENT_MAX dtblsize
269 #define SLAP_SOCK_SET_READ(fd) FD_SET(fd, &slap_daemon.sd_readers)
270 #define SLAP_SOCK_SET_WRITE(fd) FD_SET(fd, &slap_daemon.sd_writers)
272 #define SLAP_EVENT_MAX slap_daemon.sd_nfds
273 #define SLAP_ADDTEST(s) if (s >= slap_daemon.sd_nfds) slap_daemon.sd_nfds = s+1
276 #define SLAP_SOCK_CLR_READ(fd) FD_CLR(fd, &slap_daemon.sd_readers)
277 #define SLAP_SOCK_CLR_WRITE(fd) FD_CLR(fd, &slap_daemon.sd_writers)
279 #define SLAP_ADD_SOCK(s, l) do { \
281 FD_SET(s, &slap_daemon.sd_actives); \
282 FD_SET(s, &slap_daemon.sd_readers); \
285 #define SLAP_DEL_SOCK(s) do { \
286 FD_CLR(s, &slap_daemon.sd_actives); \
287 FD_CLR(s, &slap_daemon.sd_readers); \
288 FD_CLR(s, &slap_daemon.sd_writers); \
291 #define SLAP_EVENT_IS_READ(fd) FD_ISSET(fd, &readfds)
292 #define SLAP_EVENT_IS_WRITE(fd) FD_ISSET(fd, &writefds)
294 #define SLAP_EVENT_CLR_READ(fd) FD_CLR(fd, &readfds)
295 #define SLAP_EVENT_CLR_WRITE(fd) FD_CLR(fd, &writefds)
297 #define SLAP_EVENT_WAIT(tvp) \
298 select( SLAP_EVENT_MAX, &readfds, \
299 nwriters > 0 ? &writefds : NULL, NULL, tvp )
301 #define SLAP_SOCK_SET_MUTE(s) FD_CLR(s, &readfds)
302 #define SLAP_SOCK_CLR_MUTE(s) FD_SET(s, &readfds)
303 #define SLAP_SOCK_IS_MUTE(s) FD_ISSET(s, &readfds)
310 * SLP related functions
314 #define LDAP_SRVTYPE_PREFIX "service:ldap://"
315 #define LDAPS_SRVTYPE_PREFIX "service:ldaps://"
316 static char** slapd_srvurls = NULL;
317 static SLPHandle slapd_hslp = 0;
318 int slapd_register_slp = 0;
320 void slapd_slp_init( const char* urls ) {
323 slapd_srvurls = ldap_str2charray( urls, " " );
325 if( slapd_srvurls == NULL ) return;
327 /* find and expand INADDR_ANY URLs */
328 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
329 if( strcmp( slapd_srvurls[i], "ldap:///" ) == 0) {
330 char *host = ldap_pvt_get_fqdn( NULL );
331 if ( host != NULL ) {
332 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
334 sizeof( LDAP_SRVTYPE_PREFIX ) );
335 strcpy( lutil_strcopy(slapd_srvurls[i],
336 LDAP_SRVTYPE_PREFIX ), host );
341 } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0) {
342 char *host = ldap_pvt_get_fqdn( NULL );
343 if ( host != NULL ) {
344 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
346 sizeof( LDAPS_SRVTYPE_PREFIX ) );
347 strcpy( lutil_strcopy(slapd_srvurls[i],
348 LDAPS_SRVTYPE_PREFIX ), host );
355 /* open the SLP handle */
356 SLPOpen( "en", 0, &slapd_hslp );
359 void slapd_slp_deinit() {
360 if( slapd_srvurls == NULL ) return;
362 ldap_charray_free( slapd_srvurls );
363 slapd_srvurls = NULL;
365 /* close the SLP handle */
366 SLPClose( slapd_hslp );
369 void slapd_slp_regreport(
377 void slapd_slp_reg() {
380 if( slapd_srvurls == NULL ) return;
382 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
383 if( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
384 sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
385 strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
386 sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
390 SLP_LIFETIME_MAXIMUM,
400 void slapd_slp_dereg() {
403 if( slapd_srvurls == NULL ) return;
405 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
406 SLPDereg( slapd_hslp,
412 #endif /* HAVE_SLP */
415 * Add a descriptor to daemon control
417 * If isactive, the descriptor is a live server session and is subject
418 * to idletimeout control. Otherwise, the descriptor is a passive
419 * listener or an outbound client session, and not subject to
420 * idletimeout. The underlying event handler may record the Listener
421 * argument to differentiate Listener's from real sessions.
423 static void slapd_add(ber_socket_t s, int isactive, Listener *sl) {
424 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
426 assert( SLAP_SOCK_NOT_ACTIVE(s) );
429 slap_daemon.sd_nactives++;
432 SLAP_ADD_SOCK(s, sl);
434 Debug( LDAP_DEBUG_CONNS, "daemon: added %ldr\n",
436 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
440 * Remove the descriptor from daemon control
442 void slapd_remove(ber_socket_t s, int wasactive, int wake) {
444 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
447 slap_daemon.sd_nactives--;
449 waswriter = SLAP_SOCK_IS_WRITE(s);
451 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
452 (long) s, SLAP_SOCK_IS_READ(s) ? "r" : "",
453 waswriter ? "w" : "" );
454 if ( waswriter ) slap_daemon.sd_nwriters--;
458 /* If we ran out of file descriptors, we dropped a listener from
459 * the select() loop. Now that we're removing a session from our
460 * control, we can try to resume a dropped listener to use.
464 for ( i = 0; slap_listeners[i] != NULL; i++ ) {
465 if ( slap_listeners[i]->sl_sd != AC_SOCKET_INVALID ) {
466 if ( slap_listeners[i]->sl_sd == s ) continue;
467 if ( slap_listeners[i]->sl_is_mute ) {
468 slap_listeners[i]->sl_is_mute = 0;
474 /* Walked the entire list without enabling anything; emfile
475 * counter is stale. Reset it.
477 if ( slap_listeners[i] == NULL )
480 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
481 WAKE_LISTENER(wake || slapd_gentle_shutdown == 2);
484 void slapd_clr_write(ber_socket_t s, int wake) {
485 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
487 assert( SLAP_SOCK_IS_ACTIVE( s ));
488 if ( SLAP_SOCK_IS_WRITE( s )) {
489 SLAP_SOCK_CLR_WRITE( s );
490 slap_daemon.sd_nwriters--;
493 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
497 void slapd_set_write(ber_socket_t s, int wake) {
498 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
500 assert( SLAP_SOCK_IS_ACTIVE( s ));
501 if ( !SLAP_SOCK_IS_WRITE( s )) {
502 SLAP_SOCK_SET_WRITE( s );
503 slap_daemon.sd_nwriters++;
506 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
510 void slapd_clr_read(ber_socket_t s, int wake) {
511 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
513 assert( SLAP_SOCK_IS_ACTIVE( s ));
514 SLAP_SOCK_CLR_READ( s );
516 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
520 void slapd_set_read(ber_socket_t s, int wake) {
521 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
523 assert( SLAP_SOCK_IS_ACTIVE( s ));
524 if (!SLAP_SOCK_IS_READ( s ))
525 SLAP_SOCK_SET_READ( s );
527 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
531 static void slapd_close(ber_socket_t s) {
532 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
537 static void slap_free_listener_addresses(struct sockaddr **sal)
539 struct sockaddr **sap;
545 for (sap = sal; *sap != NULL; sap++) {
552 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
553 static int get_url_perms(
565 for ( i = 0; exts[ i ]; i++ ) {
566 char *type = exts[ i ];
569 if ( type[ 0 ] == '!' ) {
574 if ( strncasecmp( type, LDAPI_MOD_URLEXT "=", sizeof(LDAPI_MOD_URLEXT "=") - 1 ) == 0 ) {
576 + ( sizeof(LDAPI_MOD_URLEXT "=") - 1 );
580 switch (strlen(value)) {
582 /* skip leading '0' */
583 if ( value[ 0 ] != '0' ) {
589 for ( j = 0; j < 3; j++) {
592 v = value[ j ] - '0';
594 if ( v < 0 || v > 7 ) {
603 for ( j = 1; j < 10; j++ ) {
604 static mode_t m[] = { 0,
605 S_IRUSR, S_IWUSR, S_IXUSR,
606 S_IRGRP, S_IWGRP, S_IXGRP,
607 S_IROTH, S_IWOTH, S_IXOTH
609 static char c[] = "-rwxrwxrwx";
611 if ( value[ j ] == c[ j ] ) {
614 } else if ( value[ j ] != '-' ) {
633 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
635 /* port = 0 indicates AF_LOCAL */
636 static int slap_get_listener_addresses(
639 struct sockaddr ***sal)
641 struct sockaddr **sap;
645 *sal = ch_malloc(2 * sizeof(void *));
651 *sap = ch_malloc(sizeof(struct sockaddr_un));
657 (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) ) {
658 Debug( LDAP_DEBUG_ANY,
659 "daemon: domain socket path (%s) too long in URL",
664 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
665 (*sap)->sa_family = AF_LOCAL;
666 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
670 #ifdef HAVE_GETADDRINFO
671 struct addrinfo hints, *res, *sai;
675 memset( &hints, '\0', sizeof(hints) );
676 hints.ai_flags = AI_PASSIVE;
677 hints.ai_socktype = SOCK_STREAM;
678 hints.ai_family = slap_inet4or6;
679 snprintf(serv, sizeof serv, "%d", port);
681 if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
682 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
683 AC_GAI_STRERROR(err), 0, 0);
688 for (n=2; (sai = sai->ai_next) != NULL; n++) {
691 *sal = ch_calloc(n, sizeof(void *));
699 for ( sai=res; sai; sai=sai->ai_next ) {
700 if( sai->ai_addr == NULL ) {
701 Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
702 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
707 switch (sai->ai_family) {
708 # ifdef LDAP_PF_INET6
710 *sap = ch_malloc(sizeof(struct sockaddr_in6));
715 *(struct sockaddr_in6 *)*sap =
716 *((struct sockaddr_in6 *)sai->ai_addr);
720 *sap = ch_malloc(sizeof(struct sockaddr_in));
725 *(struct sockaddr_in *)*sap =
726 *((struct sockaddr_in *)sai->ai_addr);
734 (*sap)->sa_family = sai->ai_family;
744 struct hostent *he = NULL;
746 if ( host == NULL ) {
747 in.s_addr = htonl(INADDR_ANY);
749 } else if ( !inet_aton( host, &in ) ) {
750 he = gethostbyname( host );
752 Debug( LDAP_DEBUG_ANY,
753 "daemon: invalid host %s", host, 0, 0);
756 for (n = 0; he->h_addr_list[n]; n++) ;
759 *sal = ch_malloc((n+1) * sizeof(void *));
765 for ( i = 0; i<n; i++ ) {
766 sap[i] = ch_malloc(sizeof(struct sockaddr_in));
770 (void)memset( (void *)sap[i], '\0', sizeof(struct sockaddr_in) );
771 sap[i]->sa_family = AF_INET;
772 ((struct sockaddr_in *)sap[i])->sin_port = htons(port);
774 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr, he->h_addr_list[i], sizeof(struct in_addr) );
776 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr, &in, sizeof(struct in_addr) );
786 slap_free_listener_addresses(*sal);
790 static int slap_open_listener(
801 int err, addrlen = 0;
802 struct sockaddr **sal, **psal;
803 int socktype = SOCK_STREAM; /* default to COTS */
805 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
810 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
812 rc = ldap_url_parse( url, &lud );
814 if( rc != LDAP_URL_SUCCESS ) {
815 Debug( LDAP_DEBUG_ANY,
816 "daemon: listen URL \"%s\" parse error=%d\n",
821 l.sl_url.bv_val = NULL;
825 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
826 Debug( LDAP_DEBUG_ANY,
827 "daemon: TLS not supported (%s)\n",
829 ldap_free_urldesc( lud );
833 if(! lud->lud_port ) {
834 lud->lud_port = LDAP_PORT;
838 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
840 if(! lud->lud_port ) {
841 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
845 port = (unsigned short) lud->lud_port;
847 tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme);
848 if ( tmp == LDAP_PROTO_IPC ) {
850 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
851 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
853 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
857 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
859 ldap_free_urldesc( lud );
863 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
864 || strcmp(lud->lud_host, "*") == 0 )
866 err = slap_get_listener_addresses(NULL, port, &sal);
868 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
871 #ifdef LDAP_CONNECTIONLESS
872 l.sl_is_udp = ( tmp == LDAP_PROTO_UDP );
875 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
876 if ( lud->lud_exts ) {
877 err = get_url_perms( lud->lud_exts, &l.sl_perms, &crit );
879 l.sl_perms = S_IRWXU | S_IRWXO;
881 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
883 ldap_free_urldesc( lud );
888 /* If we got more than one address returned, we need to make space
889 * for it in the slap_listeners array.
891 for ( num=0; sal[num]; num++ );
894 slap_listeners = ch_realloc( slap_listeners, (*listeners + 1) * sizeof(Listener *) );
898 while ( *sal != NULL ) {
900 switch( (*sal)->sa_family ) {
918 #ifdef LDAP_CONNECTIONLESS
919 if( l.sl_is_udp ) socktype = SOCK_DGRAM;
921 l.sl_sd = socket( (*sal)->sa_family, socktype, 0);
922 if ( l.sl_sd == AC_SOCKET_INVALID ) {
923 int err = sock_errno();
924 Debug( LDAP_DEBUG_ANY,
925 "daemon: %s socket() failed errno=%d (%s)\n",
926 af, err, sock_errstr(err) );
931 if ( l.sl_sd >= dtblsize ) {
932 Debug( LDAP_DEBUG_ANY,
933 "daemon: listener descriptor %ld is too great %ld\n",
934 (long) l.sl_sd, (long) dtblsize, 0 );
935 tcp_close( l.sl_sd );
941 if ( (*sal)->sa_family == AF_LOCAL ) {
942 unlink ( ((struct sockaddr_un *)*sal)->sun_path );
947 /* enable address reuse */
949 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
950 (char *) &tmp, sizeof(tmp) );
951 if ( rc == AC_SOCKET_ERROR ) {
952 int err = sock_errno();
953 Debug( LDAP_DEBUG_ANY,
954 "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
955 (long) l.sl_sd, err, sock_errstr(err) );
960 switch( (*sal)->sa_family ) {
962 addrlen = sizeof(struct sockaddr_in);
967 /* Try to use IPv6 sockets for IPv6 only */
969 rc = setsockopt( l.sl_sd, IPPROTO_IPV6, IPV6_V6ONLY,
970 (char *) &tmp, sizeof(tmp) );
971 if ( rc == AC_SOCKET_ERROR ) {
972 int err = sock_errno();
973 Debug( LDAP_DEBUG_ANY,
974 "slapd(%ld): setsockopt(IPV6_V6ONLY) failed errno=%d (%s)\n",
975 (long) l.sl_sd, err, sock_errstr(err) );
978 addrlen = sizeof(struct sockaddr_in6);
986 setsockopt(l.sl_sd, 0, LOCAL_CREDS, &one, sizeof one);
989 addrlen = sizeof(struct sockaddr_un);
994 if (bind(l.sl_sd, *sal, addrlen)) {
996 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
997 (long) l.sl_sd, err, sock_errstr(err) );
998 tcp_close( l.sl_sd );
1003 switch ( (*sal)->sa_family ) {
1004 #ifdef LDAP_PF_LOCAL
1006 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
1007 #if 0 /* don't muck with socket perms */
1008 if ( chmod( addr, l.sl_perms ) < 0 && crit ) {
1009 int err = sock_errno();
1010 Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
1011 (long) l.sl_sd, err, sock_errstr(err) );
1012 tcp_close( l.sl_sd );
1013 slap_free_listener_addresses(psal);
1017 l.sl_name.bv_len = strlen(addr) + sizeof("PATH=") - 1;
1018 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len + 1 );
1019 snprintf( l.sl_name.bv_val, l.sl_name.bv_len + 1,
1022 #endif /* LDAP_PF_LOCAL */
1026 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
1027 char addr[INET_ADDRSTRLEN];
1028 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
1029 addr, sizeof(addr) );
1032 s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr );
1034 port = ntohs( ((struct sockaddr_in *)*sal) ->sin_port );
1035 l.sl_name.bv_val = ber_memalloc( sizeof("IP=255.255.255.255:65535") );
1036 snprintf( l.sl_name.bv_val, sizeof("IP=255.255.255.255:65535"),
1038 s != NULL ? s : SLAP_STRING_UNKNOWN, port );
1039 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1042 #ifdef LDAP_PF_INET6
1044 char addr[INET6_ADDRSTRLEN];
1045 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
1047 port = ntohs( ((struct sockaddr_in6 *)*sal)->sin6_port );
1048 l.sl_name.bv_len = strlen(addr) + sizeof("IP= 65535");
1049 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len );
1050 snprintf( l.sl_name.bv_val, l.sl_name.bv_len, "IP=%s %d",
1052 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1054 #endif /* LDAP_PF_INET6 */
1057 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
1058 (int) (*sal)->sa_family, 0, 0 );
1062 AC_MEMCPY(&l.sl_sa, *sal, addrlen);
1063 ber_str2bv( url, 0, 1, &l.sl_url);
1064 li = ch_malloc( sizeof( Listener ) );
1066 slap_listeners[*cur] = li;
1070 } /* while ( *sal != NULL ) */
1072 slap_free_listener_addresses(psal);
1074 if ( l.sl_url.bv_val == NULL )
1076 Debug( LDAP_DEBUG_TRACE,
1077 "slap_open_listener: failed on %s\n", url, 0, 0 );
1081 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
1082 l.sl_url.bv_val, 0, 0 );
1086 static int sockinit(void);
1087 static int sockdestroy(void);
1089 int slapd_daemon_init( const char *urls )
1094 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
1095 urls ? urls : "<null>", 0, 0 );
1096 if( (rc = sockinit()) != 0 ) {
1101 dtblsize = sysconf( _SC_OPEN_MAX );
1102 #elif HAVE_GETDTABLESIZE
1103 dtblsize = getdtablesize();
1105 dtblsize = FD_SETSIZE;
1108 /* open a pipe (or something equivalent connected to itself).
1109 * we write a byte on this fd whenever we catch a signal. The main
1110 * loop will be select'ing on this socket, and will wake up when
1111 * this byte arrives.
1113 if( (rc = lutil_pair( wake_sds )) < 0 ) {
1114 Debug( LDAP_DEBUG_ANY,
1115 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
1121 if( urls == NULL ) {
1125 u = ldap_str2charray( urls, " " );
1127 if( u == NULL || u[0] == NULL ) {
1128 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
1133 for( i=0; u[i] != NULL; i++ ) {
1134 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
1139 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
1141 ldap_charray_free( u );
1145 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
1147 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
1149 for(n = 0, j = 0; u[n]; n++ ) {
1150 if ( slap_open_listener( u[n], &i, &j ) ) {
1151 ldap_charray_free( u );
1155 slap_listeners[j] = NULL;
1157 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
1161 if( slapd_register_slp ) {
1162 slapd_slp_init( urls );
1167 ldap_charray_free( u );
1168 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
1174 slapd_daemon_destroy(void)
1176 connections_destroy();
1177 tcp_close( wake_sds[1] );
1178 tcp_close( wake_sds[0] );
1182 if( slapd_register_slp ) {
1199 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1200 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1202 slapd_remove( slap_listeners[l]->sl_sd, 0, 0 );
1203 #ifdef LDAP_PF_LOCAL
1204 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1205 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1207 #endif /* LDAP_PF_LOCAL */
1208 slapd_close( slap_listeners[l]->sl_sd );
1210 if ( slap_listeners[l]->sl_url.bv_val )
1211 ber_memfree( slap_listeners[l]->sl_url.bv_val );
1212 if ( slap_listeners[l]->sl_name.bv_val )
1213 ber_memfree( slap_listeners[l]->sl_name.bv_val );
1214 free ( slap_listeners[l] );
1215 slap_listeners[l] = NULL;
1220 slapd_handle_listener(
1227 socklen_t len = sizeof(from);
1230 struct berval authid = BER_BVNULL;
1231 #ifdef SLAPD_RLOOKUPS
1232 char hbuf[NI_MAXHOST];
1235 char *dnsname = NULL;
1236 char *peeraddr = NULL;
1237 #ifdef LDAP_PF_LOCAL
1238 char peername[MAXPATHLEN + sizeof("PATH=")];
1239 #elif defined(LDAP_PF_INET6)
1240 char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
1242 char peername[sizeof("IP=255.255.255.255:65336")];
1243 #endif /* LDAP_PF_LOCAL */
1247 #ifdef LDAP_CONNECTIONLESS
1248 if ( sl->sl_is_udp ) {
1249 /* The first time we receive a query, we set this
1250 * up as a "connection". It remains open for the life
1253 if ( sl->sl_is_udp < 2 ) {
1254 id = connection_init( sl->sl_sd, sl, "", "",
1255 CONN_IS_UDP, ssf, NULL );
1262 # ifdef LDAP_PF_LOCAL
1263 /* FIXME: apparently accept doesn't fill
1264 * the sun_path sun_path member */
1265 from.sa_un_addr.sun_path[0] = '\0';
1266 # endif /* LDAP_PF_LOCAL */
1268 s = accept( sl->sl_sd, (struct sockaddr *) &from, &len );
1269 if ( s == AC_SOCKET_INVALID ) {
1270 int err = sock_errno();
1281 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1283 /* Stop listening until an existing session closes */
1285 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1288 Debug( LDAP_DEBUG_ANY,
1289 "daemon: accept(%ld) failed errno=%d (%s)\n",
1290 (long) sl->sl_sd, err,
1292 ldap_pvt_thread_yield();
1296 #ifndef HAVE_WINSOCK
1297 /* make sure descriptor number isn't too great */
1298 if ( s >= dtblsize ) {
1299 Debug( LDAP_DEBUG_ANY,
1300 "daemon: %ld beyond descriptor table size %ld\n",
1301 (long) s, (long) dtblsize, 0 );
1304 ldap_pvt_thread_yield();
1310 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1312 /* newly accepted stream should not be in any of the FD SETS */
1313 assert( SLAP_SOCK_NOT_ACTIVE( s ));
1315 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1318 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1319 #ifdef LDAP_PF_LOCAL
1320 /* for IPv4 and IPv6 sockets only */
1321 if ( from.sa_addr.sa_family != AF_LOCAL )
1322 #endif /* LDAP_PF_LOCAL */
1327 /* enable keep alives */
1329 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1330 (char *) &tmp, sizeof(tmp) );
1331 if ( rc == AC_SOCKET_ERROR ) {
1332 int err = sock_errno();
1333 Debug( LDAP_DEBUG_ANY,
1334 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1335 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1339 /* enable no delay */
1341 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1342 (char *)&tmp, sizeof(tmp) );
1343 if ( rc == AC_SOCKET_ERROR ) {
1344 int err = sock_errno();
1345 Debug( LDAP_DEBUG_ANY,
1346 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1347 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1353 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
1355 switch ( from.sa_addr.sa_family ) {
1356 # ifdef LDAP_PF_LOCAL
1358 /* FIXME: apparently accept doesn't fill
1359 * the sun_path sun_path member */
1360 if ( from.sa_un_addr.sun_path[0] == '\0' ) {
1361 AC_MEMCPY( from.sa_un_addr.sun_path,
1362 sl->sl_sa.sa_un_addr.sun_path,
1363 sizeof( from.sa_un_addr.sun_path ) );
1366 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1372 if( getpeereid( s, &uid, &gid ) == 0 ) {
1373 authid.bv_val = ch_malloc(
1374 sizeof("uidnumber=4294967295+gidnumber=4294967295,"
1375 "cn=peercred,cn=external,cn=auth"));
1376 authid.bv_len = sprintf( authid.bv_val,
1377 "uidnumber=%d+gidnumber=%d,"
1378 "cn=peercred,cn=external,cn=auth",
1379 (int) uid, (int) gid);
1384 #endif /* LDAP_PF_LOCAL */
1386 # ifdef LDAP_PF_INET6
1388 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1389 peeraddr = inet_ntoa( *((struct in_addr *)
1390 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1391 sprintf( peername, "IP=%s:%d",
1392 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1393 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1395 char addr[INET6_ADDRSTRLEN];
1397 peeraddr = (char *) inet_ntop( AF_INET6,
1398 &from.sa_in6_addr.sin6_addr,
1399 addr, sizeof addr );
1400 sprintf( peername, "IP=%s %d",
1401 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1402 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1405 # endif /* LDAP_PF_INET6 */
1408 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1409 sprintf( peername, "IP=%s:%d",
1410 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1411 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1419 if ( ( from.sa_addr.sa_family == AF_INET )
1420 #ifdef LDAP_PF_INET6
1421 || ( from.sa_addr.sa_family == AF_INET6 )
1424 #ifdef SLAPD_RLOOKUPS
1425 if ( use_reverse_lookup ) {
1427 if (ldap_pvt_get_hname( (const struct sockaddr *)&from, len, hbuf,
1428 sizeof(hbuf), &herr ) == 0) {
1429 ldap_pvt_str2lower( hbuf );
1435 #endif /* SLAPD_RLOOKUPS */
1438 if ( !hosts_ctl("slapd",
1439 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1440 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1441 SLAP_STRING_UNKNOWN ))
1444 Statslog( LDAP_DEBUG_STATS,
1445 "fd=%ld DENIED from %s (%s)\n",
1447 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1448 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1453 #endif /* HAVE_TCPD */
1456 id = connection_init(s, sl,
1457 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1460 sl->sl_is_tls ? CONN_IS_TLS : 0,
1465 authid.bv_val ? &authid : NULL );
1467 if( authid.bv_val ) ch_free(authid.bv_val);
1470 Debug( LDAP_DEBUG_ANY,
1471 "daemon: connection_init(%ld, %s, %s) failed.\n",
1472 (long) s, peername, sl->sl_name.bv_val );
1477 Statslog( LDAP_DEBUG_STATS,
1478 "conn=%ld fd=%ld ACCEPT from %s (%s)\n",
1479 id, (long) s, peername, sl->sl_name.bv_val,
1482 slapd_add( s, 1, NULL );
1492 time_t last_idle_check = 0;
1493 struct timeval idle;
1496 #define SLAPD_IDLE_CHECK_LIMIT 4
1498 if ( global_idletimeout > 0 ) {
1499 last_idle_check = slap_get_time();
1500 /* Set the select timeout.
1501 * Don't just truncate, preserve the fractions of
1502 * seconds to prevent sleeping for zero time.
1504 idle.tv_sec = global_idletimeout/SLAPD_IDLE_CHECK_LIMIT;
1505 idle.tv_usec = global_idletimeout - idle.tv_sec * SLAPD_IDLE_CHECK_LIMIT;
1506 idle.tv_usec *= 1000000 / SLAPD_IDLE_CHECK_LIMIT;
1512 slapd_add( wake_sds[0], 0, NULL );
1514 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1515 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1517 #ifdef LDAP_CONNECTIONLESS
1518 /* Since this is connectionless, the data port is the
1519 * listening port. The listen() and accept() calls
1522 if ( slap_listeners[l]->sl_is_udp ) {
1523 slapd_add( slap_listeners[l]->sl_sd, 1, slap_listeners[l] );
1528 if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN_BACKLOG ) == -1 ) {
1529 int err = sock_errno();
1531 #ifdef LDAP_PF_INET6
1532 /* If error is EADDRINUSE, we are trying to listen to INADDR_ANY and
1533 * we are already listening to in6addr_any, then we want to ignore
1534 * this and continue.
1536 if ( err == EADDRINUSE ) {
1538 struct sockaddr_in sa = slap_listeners[l]->sl_sa.sa_in_addr;
1539 struct sockaddr_in6 sa6;
1541 if ( sa.sin_family == AF_INET &&
1542 sa.sin_addr.s_addr == htonl(INADDR_ANY) ) {
1543 for ( i = 0 ; i < l; i++ ) {
1544 sa6 = slap_listeners[i]->sl_sa.sa_in6_addr;
1545 if ( sa6.sin6_family == AF_INET6 &&
1546 !memcmp( &sa6.sin6_addr, &in6addr_any, sizeof(struct in6_addr) ) )
1551 /* We are already listening to in6addr_any */
1552 Debug( LDAP_DEBUG_CONNS,
1553 "daemon: Attempt to listen to 0.0.0.0 failed, already listening on ::, assuming IPv4 included\n",
1555 slapd_close( slap_listeners[l]->sl_sd );
1556 slap_listeners[l]->sl_sd = AC_SOCKET_INVALID;
1562 Debug( LDAP_DEBUG_ANY,
1563 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
1564 slap_listeners[l]->sl_url.bv_val, err,
1566 return( (void*)-1 );
1569 slapd_add( slap_listeners[l]->sl_sd, 0, slap_listeners[l] );
1572 #ifdef HAVE_NT_SERVICE_MANAGER
1573 if ( started_event != NULL ) {
1574 ldap_pvt_thread_cond_signal( &started_event );
1577 /* initialization complete. Here comes the loop. */
1579 while ( !slapd_shutdown ) {
1583 ber_socket_t nfds, nrfds, nwfds;
1584 #define SLAPD_EBADF_LIMIT 16
1591 struct timeval *tvp;
1593 struct timeval *cat;
1596 now = slap_get_time();
1598 if( ( global_idletimeout > 0 ) &&
1599 difftime( last_idle_check +
1600 global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 ) {
1601 connections_timeout_idle( now );
1602 last_idle_check = now;
1607 if( slapd_gentle_shutdown ) {
1608 ber_socket_t active;
1610 if( slapd_gentle_shutdown == 1 ) {
1611 Debug( LDAP_DEBUG_ANY, "slapd gentle shutdown\n", 0, 0, 0 );
1612 close_listeners( 1 );
1613 frontendDB->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
1614 slapd_gentle_shutdown = 2;
1617 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1618 active = slap_daemon.sd_nactives;
1619 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1629 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1631 nwriters = slap_daemon.sd_nwriters;
1634 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1635 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1637 if ( slap_listeners[l]->sl_is_mute )
1638 SLAP_SOCK_SET_MUTE( slap_listeners[l]->sl_sd );
1640 if ( SLAP_SOCK_IS_MUTE( slap_listeners[l]->sl_sd ))
1641 SLAP_SOCK_CLR_MUTE( slap_listeners[l]->sl_sd );
1644 nfds = SLAP_EVENT_MAX;
1646 if ( global_idletimeout && slap_daemon.sd_nactives )
1649 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1652 #if defined(HAVE_YIELDING_SELECT) || defined(NO_THREADS)
1653 && ( tv.tv_sec || tv.tv_usec )
1660 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1661 rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
1662 while ( cat && cat->tv_sec && cat->tv_sec <= now ) {
1663 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
1664 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1666 ldap_pvt_runqueue_runtask( &slapd_rq, rtask );
1667 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1668 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1669 ldap_pvt_thread_pool_submit( &connection_pool,
1670 rtask->routine, (void *) rtask );
1671 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1673 rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
1675 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1677 if ( cat && cat->tv_sec ) {
1678 time_t diff = difftime( cat->tv_sec, now );
1681 if ( tvp == NULL || diff < tv.tv_sec ) {
1688 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1689 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ||
1690 slap_listeners[l]->sl_is_mute )
1693 Debug( LDAP_DEBUG_CONNS,
1694 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
1695 slap_listeners[l]->sl_sd, at,
1696 tvp == NULL ? "NULL" : "zero" );
1699 switch(ns = SLAP_EVENT_WAIT(tvp))
1701 case -1: { /* failure - try again */
1702 int err = sock_errno();
1706 /* you'd think this would be EBADF */
1707 || err == WSAENOTSOCK
1710 if (++ebadf < SLAPD_EBADF_LIMIT)
1714 if( err != EINTR ) {
1715 Debug( LDAP_DEBUG_CONNS,
1716 "daemon: select failed (%d): %s\n",
1717 err, sock_errstr(err), 0 );
1723 case 0: /* timeout - let threads run */
1725 #ifndef HAVE_YIELDING_SELECT
1726 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
1729 ldap_pvt_thread_yield();
1733 default: /* something happened - deal with it */
1734 if( slapd_shutdown ) continue;
1737 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
1742 #if SLAP_EVENTS_ARE_INDEXED
1743 if ( SLAP_EVENT_IS_READ( wake_sds[0] )) {
1745 tcp_read( wake_sds[0], c, sizeof(c) );
1748 SLAP_EVENT_CLR_READ( wake_sds[0] );
1752 /* The event slot equals the descriptor number - this is
1753 * true for Unix select and poll. We treat Windows select
1754 * like this too, even though it's a kludge.
1756 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1759 if ( ns <= 0 ) break;
1761 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1764 if ( !SLAP_EVENT_IS_READ( slap_listeners[l]->sl_sd ))
1769 rc = slapd_handle_listener(slap_listeners[l]);
1771 #ifdef LDAP_CONNECTIONLESS
1772 /* This is a UDP session, let the data loop process it */
1776 /* Don't need to look at this in the data loops */
1777 SLAP_EVENT_CLR_READ( slap_listeners[l]->sl_sd );
1778 SLAP_EVENT_CLR_WRITE( slap_listeners[l]->sl_sd );
1781 /* bypass the following tests if no descriptors left */
1783 #ifndef HAVE_YIELDING_SELECT
1784 ldap_pvt_thread_yield();
1789 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1791 nrfds = readfds.fd_count;
1792 nwfds = writefds.fd_count;
1793 for ( i = 0; i < readfds.fd_count; i++ ) {
1794 Debug( LDAP_DEBUG_CONNS, " %d%s",
1795 readfds.fd_array[i], "r", 0 );
1797 for ( i = 0; i < writefds.fd_count; i++ ) {
1798 Debug( LDAP_DEBUG_CONNS, " %d%s",
1799 writefds.fd_array[i], "w", 0 );
1805 for ( i = 0; i < nfds; i++ ) {
1808 r = SLAP_EVENT_IS_READ( i );
1809 w = SLAP_EVENT_IS_WRITE( i );
1811 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1812 r ? "r" : "", w ? "w" : "" );
1822 if ( ns <= 0 ) break;
1825 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1828 /* loop through the writers */
1829 for ( i = 0; nwfds > 0; i++ )
1833 wd = writefds.fd_array[i];
1835 if( ! SLAP_EVENT_IS_WRITE( i ) ) {
1842 Debug( LDAP_DEBUG_CONNS,
1843 "daemon: write active on %d\n",
1846 * NOTE: it is possible that the connection was closed
1847 * and that the stream is now inactive.
1848 * connection_write() must valid the stream is still
1852 if ( connection_write( wd ) < 0 ) {
1853 if ( SLAP_EVENT_IS_READ( wd )) {
1854 SLAP_EVENT_CLR_READ( (unsigned) wd );
1859 SLAP_EVENT_CLR_WRITE( wd );
1862 for ( i = 0; nrfds > 0; i++ )
1866 rd = readfds.fd_array[i];
1868 if( ! SLAP_EVENT_IS_READ( i ) ) {
1875 Debug ( LDAP_DEBUG_CONNS,
1876 "daemon: read activity on %d\n", rd, 0, 0 );
1878 * NOTE: it is possible that the connection was closed
1879 * and that the stream is now inactive.
1880 * connection_read() must valid the stream is still
1884 if ( connection_read( rd ) < 0 ) {
1888 #else /* !SLAP_EVENTS_ARE_INDEXED */
1890 /* The events are returned in an arbitrary list. This is true
1891 * for /dev/poll, epoll and kqueue. In order to prioritize things
1892 * so that we can handle wake_sds first, listeners second, and then
1893 * all other connections last (as we do for select), we would need
1894 * to use multiple event handles and cascade them.
1896 * That seems like a bit of hassle. So the wake_sds check has been
1897 * skipped. For epoll and kqueue we can associate arbitrary data with
1898 * an event, so we could use pointers to the listener structure
1899 * instead of just the file descriptor. For /dev/poll we have to
1900 * search the listeners array for a matching descriptor.
1902 /* if waking is set and we woke up, we'll read whatever
1907 tcp_read( wake_sds[0], c, sizeof(c) );
1914 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1916 for (i=0; i<ns; i++) {
1919 if ( SLAP_EVENT_IS_LISTENER(i)
1920 #ifdef LDAP_CONNECTIONLESS
1921 && !((SLAP_EVENT_LISTENER(i))->sl_is_udp)
1925 r = SLAP_EVENT_IS_READ( i );
1926 w = SLAP_EVENT_IS_WRITE( i );
1928 Debug( LDAP_DEBUG_CONNS, " %d%s%s", SLAP_EVENT_FD(i),
1929 r ? "r" : "", w ? "w" : "" );
1933 for (i=0; i<ns; i++) {
1936 if ( SLAP_EVENT_IS_LISTENER(i) ) {
1937 rc = slapd_handle_listener( SLAP_EVENT_LISTENER( i ));
1939 /* If we found a regular listener, rc is now zero, and we
1940 * can skip the data portion. But if it was a UDP listener
1941 * then rc is still 1, and we want to handle the data.
1944 fd = SLAP_EVENT_FD( i );
1946 if( SLAP_EVENT_IS_WRITE( i ) ) {
1947 Debug( LDAP_DEBUG_CONNS,
1948 "daemon: write active on %d\n",
1951 * NOTE: it is possible that the connection was closed
1952 * and that the stream is now inactive.
1953 * connection_write() must valid the stream is still
1957 if ( connection_write( fd ) < 0 ) {
1962 if( SLAP_EVENT_IS_READ( i ) ) {
1963 Debug( LDAP_DEBUG_CONNS,
1964 "daemon: read active on %d\n",
1967 * NOTE: it is possible that the connection was closed
1968 * and that the stream is now inactive.
1969 * connection_read() must valid the stream is still
1973 if ( connection_read( fd ) < 0 ) {
1979 #endif /* SLAP_EVENTS_ARE_INDEXED */
1981 #ifndef HAVE_YIELDING_SELECT
1982 ldap_pvt_thread_yield();
1986 if( slapd_shutdown == 1 ) {
1987 Debug( LDAP_DEBUG_ANY,
1988 "daemon: shutdown requested and initiated.\n",
1991 } else if ( slapd_shutdown == 2 ) {
1992 #ifdef HAVE_NT_SERVICE_MANAGER
1993 Debug( LDAP_DEBUG_ANY,
1994 "daemon: shutdown initiated by Service Manager.\n",
1996 #else /* !HAVE_NT_SERVICE_MANAGER */
1997 Debug( LDAP_DEBUG_ANY,
1998 "daemon: abnormal condition, shutdown initiated.\n",
2000 #endif /* !HAVE_NT_SERVICE_MANAGER */
2002 Debug( LDAP_DEBUG_ANY,
2003 "daemon: no active streams, shutdown initiated.\n",
2007 if( slapd_gentle_shutdown != 2 ) {
2008 close_listeners ( 0 );
2011 free ( slap_listeners );
2012 slap_listeners = NULL;
2014 if( !slapd_gentle_shutdown ) {
2015 slapd_abrupt_shutdown = 1;
2016 connections_shutdown();
2019 Debug( LDAP_DEBUG_ANY,
2020 "slapd shutdown: waiting for %d threads to terminate\n",
2021 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
2022 ldap_pvt_thread_pool_destroy(&connection_pool, 1);
2028 int slapd_daemon( void )
2034 #define SLAPD_LISTENER_THREAD 1
2035 #if defined( SLAPD_LISTENER_THREAD )
2037 ldap_pvt_thread_t listener_tid;
2039 /* listener as a separate THREAD */
2040 rc = ldap_pvt_thread_create( &listener_tid,
2041 0, slapd_daemon_task, NULL );
2044 Debug( LDAP_DEBUG_ANY,
2045 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
2049 /* wait for the listener thread to complete */
2050 ldap_pvt_thread_join( listener_tid, (void *) NULL );
2053 /* experimental code */
2054 slapd_daemon_task( NULL );
2063 #if defined( HAVE_WINSOCK2 )
2064 WORD wVersionRequested;
2068 wVersionRequested = MAKEWORD( 2, 0 );
2070 err = WSAStartup( wVersionRequested, &wsaData );
2072 /* Tell the user that we couldn't find a usable */
2077 /* Confirm that the WinSock DLL supports 2.0.*/
2078 /* Note that if the DLL supports versions greater */
2079 /* than 2.0 in addition to 2.0, it will still return */
2080 /* 2.0 in wVersion since that is the version we */
2083 if ( LOBYTE( wsaData.wVersion ) != 2 ||
2084 HIBYTE( wsaData.wVersion ) != 0 )
2086 /* Tell the user that we couldn't find a usable */
2092 /* The WinSock DLL is acceptable. Proceed. */
2093 #elif defined( HAVE_WINSOCK )
2095 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
2102 int sockdestroy(void)
2104 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
2111 slap_sig_shutdown( int sig )
2114 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
2118 * If the NT Service Manager is controlling the server, we don't
2119 * want SIGBREAK to kill the server. For some strange reason,
2120 * SIGBREAK is generated when a user logs out.
2123 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
2124 if (is_NT_Service && sig == SIGBREAK)
2129 if (sig == SIGHUP && global_gentlehup && slapd_gentle_shutdown == 0)
2130 slapd_gentle_shutdown = 1;
2136 /* reinstall self */
2137 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
2141 slap_sig_wake( int sig )
2145 /* reinstall self */
2146 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
2150 void slapd_add_internal(ber_socket_t s, int isactive) {
2151 slapd_add(s, isactive, NULL);
2154 Listener ** slapd_get_listeners(void) {
2155 return slap_listeners;
2158 void slap_wake_listener()