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"
43 #if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL)
44 #include <sys/epoll.h>
49 #define SLAP_STRING_UNKNOWN STRING_UNKNOWN
51 int allow_severity = LOG_INFO;
52 int deny_severity = LOG_NOTICE;
53 #else /* ! TCP Wrappers */
54 #define SLAP_STRING_UNKNOWN "unknown"
55 #endif /* ! TCP Wrappers */
59 /* this should go in <ldap.h> as soon as it is accepted */
60 #define LDAPI_MOD_URLEXT "x-mod"
61 #endif /* LDAP_PF_LOCAL */
64 int slap_inet4or6 = AF_UNSPEC;
66 int slap_inet4or6 = AF_INET;
71 ber_socket_t dtblsize;
72 slap_ssf_t local_ssf = LDAP_PVT_SASL_LOCAL_SSF;
73 struct runqueue_s slapd_rq;
75 Listener **slap_listeners = NULL;
77 #ifndef SLAPD_LISTEN_BACKLOG
78 #define SLAPD_LISTEN_BACKLOG 1024
81 static ber_socket_t wake_sds[2];
85 #define WAKE_LISTENER(w) do { \
86 if ((w) && waking < 5) { waking++; tcp_write( wake_sds[1], "0", 1 ); } \
89 volatile sig_atomic_t slapd_shutdown = 0, slapd_gentle_shutdown = 0;
90 volatile sig_atomic_t slapd_abrupt_shutdown = 0;
92 static struct slap_daemon {
93 ldap_pvt_thread_mutex_t sd_mutex;
95 ber_socket_t sd_nactives;
99 struct epoll_event *sd_epolls;
107 /* In winsock, accept() returns values higher than dtblsize
108 so don't bother with this optimization */
119 #define SLAP_EVENTS_ARE_INDEXED 0
120 #define SLAP_SOCK_IX(s) (slap_daemon.sd_index[s])
121 #define SLAP_SOCK_EP(s) (slap_daemon.sd_epolls[SLAP_SOCK_IX(s)])
122 #define SLAP_SOCK_EV(s) (SLAP_SOCK_EP(s).events)
123 #define SLAP_SOCK_IS_ACTIVE(s) (SLAP_SOCK_IX(s) != -1)
124 #define SLAP_SOCK_NOT_ACTIVE(s) (SLAP_SOCK_IX(s) == -1)
125 #define SLAP_SOCK_IS_SET(s, mode) (SLAP_SOCK_EV(s) & mode)
127 #define SLAP_SOCK_IS_READ(s) SLAP_SOCK_IS_SET(s, EPOLLIN)
128 #define SLAP_SOCK_IS_WRITE(s) SLAP_SOCK_IS_SET(s, EPOLLOUT)
130 #define SLAP_SET_SOCK(s, mode) do { \
131 if ((SLAP_SOCK_EV(s) & mode) != mode) { \
132 SLAP_SOCK_EV(s) |= mode; \
133 epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_MOD, s, \
138 #define SLAP_CLR_SOCK(s, mode) do { \
139 if ((SLAP_SOCK_EV(s) & mode)) { \
140 SLAP_SOCK_EV(s) &= ~mode; \
141 epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_MOD, s, \
146 #define SLAP_SOCK_SET_READ(s) SLAP_SET_SOCK(s, EPOLLIN)
147 #define SLAP_SOCK_SET_WRITE(s) SLAP_SET_SOCK(s, EPOLLOUT)
149 #define SLAP_SOCK_CLR_READ(s) SLAP_CLR_SOCK(s, EPOLLIN)
150 #define SLAP_SOCK_CLR_WRITE(s) SLAP_CLR_SOCK(s, EPOLLOUT)
152 #define SLAP_CLR_EVENT(i, mode) (revents[i].events &= ~mode)
155 #define SLAP_EVENT_MAX slap_daemon.sd_nfds
157 /* If a Listener address is provided, store that as the epoll data.
158 * Otherwise, store the address of this socket's slot in the
159 * index array. If we can't do this add, the system is out of
160 * resources and we need to shutdown.
162 #define SLAP_ADD_SOCK(s, l) do { \
164 SLAP_SOCK_IX(s) = slap_daemon.sd_nfds; \
165 SLAP_SOCK_EP(s).data.ptr = (l) ? (l) : (void *)(&SLAP_SOCK_IX(s)); \
166 SLAP_SOCK_EV(s) = EPOLLIN; \
167 rc = epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_ADD, s, \
169 if ( rc == 0 ) slap_daemon.sd_nfds++; \
171 Debug( LDAP_DEBUG_ANY, "daemon: epoll_ctl ADD failed, errno %d, shutting down\n", \
173 slapd_shutdown = 2; \
177 #define SLAP_EV_LISTENER(ptr) (((int *)(ptr) >= slap_daemon.sd_index && \
178 (int *)(ptr) <= (slap_daemon.sd_index+dtblsize)) ? 0 : 1)
180 #define SLAP_EV_PTRFD(ptr) (SLAP_EV_LISTENER(ptr) ? \
181 ((Listener *)ptr)->sl_sd : (int *)(ptr) - slap_daemon.sd_index)
183 #define SLAP_DEL_SOCK(s) do { \
184 int fd, rc, index = SLAP_SOCK_IX(s); \
185 rc = epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_DEL, s, \
187 slap_daemon.sd_epolls[index] = slap_daemon.sd_epolls[slap_daemon.sd_nfds-1]; \
188 fd = SLAP_EV_PTRFD(slap_daemon.sd_epolls[index].data.ptr); \
189 slap_daemon.sd_index[fd] = index; \
190 slap_daemon.sd_index[s] = -1; \
191 slap_daemon.sd_nfds--; \
194 #define SLAP_EVENT_CLR_READ(i) SLAP_CLR_EVENT(i, EPOLLIN)
195 #define SLAP_EVENT_CLR_WRITE(i) SLAP_CLR_EVENT(i, EPOLLOUT)
197 #define SLAP_CHK_EVENT(i, mode) (revents[i].events & mode)
199 #define SLAP_EVENT_IS_READ(i) SLAP_CHK_EVENT(i, EPOLLIN)
200 #define SLAP_EVENT_IS_WRITE(i) SLAP_CHK_EVENT(i, EPOLLOUT)
201 #define SLAP_EVENT_IS_LISTENER(i) SLAP_EV_LISTENER(revents[i].data.ptr)
202 #define SLAP_EVENT_LISTENER(i) (revents[i].data.ptr)
204 #define SLAP_EVENT_FD(i) SLAP_EV_PTRFD(revents[i].data.ptr)
205 #define SLAP_SOCK_SET_MUTE(s) SLAP_SOCK_CLR_READ(s)
206 #define SLAP_SOCK_CLR_MUTE(s) SLAP_SOCK_SET_READ(s)
207 #define SLAP_SOCK_IS_MUTE(s) (!SLAP_SOCK_IS_READ(s))
209 #define SLAP_SOCK_SET_INIT \
210 slap_daemon.sd_epolls = ch_calloc(1, sizeof(struct epoll_event) * dtblsize * 2); \
211 slap_daemon.sd_index = ch_malloc(sizeof(int) * dtblsize); \
212 slap_daemon.sd_epfd = epoll_create( dtblsize ); \
213 for (i=0; i<dtblsize; i++) slap_daemon.sd_index[i] = -1
216 #define SLAP_EVENT_DECL \
217 struct epoll_event *revents
219 #define SLAP_EVENT_INIT \
220 revents = slap_daemon.sd_epolls + dtblsize; \
222 #define SLAP_EVENT_WAIT(tvp) \
223 epoll_wait( slap_daemon.sd_epfd, revents, dtblsize, tvp ? tvp->tv_sec * 1000 : -1 )
228 #define SLAP_EVENTS_ARE_INDEXED 1
229 #define SLAP_EVENT_DECL \
230 fd_set readfds, writefds
232 #define SLAP_EVENT_INIT \
233 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) ); \
235 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) )
238 #define CHK_SETSIZE \
239 if (dtblsize > FD_SETSIZE) dtblsize = FD_SETSIZE
244 #define SLAP_SOCK_SET_INIT \
246 FD_ZERO(&slap_daemon.sd_readers); \
247 FD_ZERO(&slap_daemon.sd_writers)
249 #define SLAP_SOCK_IS_ACTIVE(fd) FD_ISSET(fd, &slap_daemon.sd_actives)
251 #define SLAP_SOCK_IS_READ(fd) FD_ISSET(fd, &slap_daemon.sd_readers)
252 #define SLAP_SOCK_IS_WRITE(fd) FD_ISSET(fd, &slap_daemon.sd_writers)
254 #define SLAP_SOCK_NOT_ACTIVE(fd) (!SLAP_SOCK_IS_ACTIVE(fd) && \
255 !SLAP_SOCK_IS_READ(fd) && !SLAP_SOCK_IS_WRITE(fd))
258 #define SLAP_SOCK_SET_READ(fd) do { \
259 if (!SLAP_SOCK_IS_READ(fd)) {FD_SET(fd, &slap_daemon.sd_readers);} \
261 #define SLAP_SOCK_SET_WRITE(fd) do { \
262 if (!SLAP_SOCK_IS_WRITE(fd)) {FD_SET(fd, &slap_daemon.sd_writers);} \
265 #define SLAP_ADDTEST(s)
266 #define SLAP_EVENT_MAX dtblsize
268 #define SLAP_SOCK_SET_READ(fd) FD_SET(fd, &slap_daemon.sd_readers)
269 #define SLAP_SOCK_SET_WRITE(fd) FD_SET(fd, &slap_daemon.sd_writers)
271 #define SLAP_EVENT_MAX slap_daemon.sd_nfds
272 #define SLAP_ADDTEST(s) if (s >= slap_daemon.sd_nfds) slap_daemon.sd_nfds = s+1
275 #define SLAP_SOCK_CLR_READ(fd) FD_CLR(fd, &slap_daemon.sd_readers)
276 #define SLAP_SOCK_CLR_WRITE(fd) FD_CLR(fd, &slap_daemon.sd_writers)
278 #define SLAP_ADD_SOCK(s, l) do { \
280 FD_SET(s, &slap_daemon.sd_actives); \
281 FD_SET(s, &slap_daemon.sd_readers); \
284 #define SLAP_DEL_SOCK(s) do { \
285 FD_CLR(s, &slap_daemon.sd_actives); \
286 FD_CLR(s, &slap_daemon.sd_readers); \
287 FD_CLR(s, &slap_daemon.sd_writers); \
290 #define SLAP_EVENT_IS_READ(fd) FD_ISSET(fd, &readfds)
291 #define SLAP_EVENT_IS_WRITE(fd) FD_ISSET(fd, &writefds)
293 #define SLAP_EVENT_CLR_READ(fd) FD_CLR(fd, &readfds)
294 #define SLAP_EVENT_CLR_WRITE(fd) FD_CLR(fd, &writefds)
296 #define SLAP_EVENT_WAIT(tvp) \
297 select( SLAP_EVENT_MAX, &readfds, \
298 nwriters > 0 ? &writefds : NULL, NULL, tvp )
300 #define SLAP_SOCK_SET_MUTE(s) FD_CLR(s, &readfds)
301 #define SLAP_SOCK_CLR_MUTE(s) FD_SET(s, &readfds)
302 #define SLAP_SOCK_IS_MUTE(s) (!FD_ISSET(s, &readfds))
309 * SLP related functions
313 #define LDAP_SRVTYPE_PREFIX "service:ldap://"
314 #define LDAPS_SRVTYPE_PREFIX "service:ldaps://"
315 static char** slapd_srvurls = NULL;
316 static SLPHandle slapd_hslp = 0;
317 int slapd_register_slp = 0;
319 void slapd_slp_init( const char* urls ) {
322 slapd_srvurls = ldap_str2charray( urls, " " );
324 if( slapd_srvurls == NULL ) return;
326 /* find and expand INADDR_ANY URLs */
327 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
328 if( strcmp( slapd_srvurls[i], "ldap:///" ) == 0) {
329 char *host = ldap_pvt_get_fqdn( NULL );
330 if ( host != NULL ) {
331 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
333 sizeof( LDAP_SRVTYPE_PREFIX ) );
334 strcpy( lutil_strcopy(slapd_srvurls[i],
335 LDAP_SRVTYPE_PREFIX ), host );
340 } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0) {
341 char *host = ldap_pvt_get_fqdn( NULL );
342 if ( host != NULL ) {
343 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
345 sizeof( LDAPS_SRVTYPE_PREFIX ) );
346 strcpy( lutil_strcopy(slapd_srvurls[i],
347 LDAPS_SRVTYPE_PREFIX ), host );
354 /* open the SLP handle */
355 SLPOpen( "en", 0, &slapd_hslp );
358 void slapd_slp_deinit() {
359 if( slapd_srvurls == NULL ) return;
361 ldap_charray_free( slapd_srvurls );
362 slapd_srvurls = NULL;
364 /* close the SLP handle */
365 SLPClose( slapd_hslp );
368 void slapd_slp_regreport(
376 void slapd_slp_reg() {
379 if( slapd_srvurls == NULL ) return;
381 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
382 if( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
383 sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
384 strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
385 sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
389 SLP_LIFETIME_MAXIMUM,
399 void slapd_slp_dereg() {
402 if( slapd_srvurls == NULL ) return;
404 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
405 SLPDereg( slapd_hslp,
411 #endif /* HAVE_SLP */
414 * Add a descriptor to daemon control
416 * If isactive, the descriptor is a live server session and is subject
417 * to idletimeout control. Otherwise, the descriptor is a passive
418 * listener or an outbound client session, and not subject to
419 * idletimeout. The underlying event handler may record the Listener
420 * argument to differentiate Listener's from real sessions.
422 static void slapd_add(ber_socket_t s, int isactive, Listener *sl) {
423 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
425 assert( SLAP_SOCK_NOT_ACTIVE(s) );
428 slap_daemon.sd_nactives++;
431 SLAP_ADD_SOCK(s, sl);
433 Debug( LDAP_DEBUG_CONNS, "daemon: added %ldr\n",
435 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
439 * Remove the descriptor from daemon control
441 void slapd_remove(ber_socket_t s, int wasactive, int wake) {
443 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
446 slap_daemon.sd_nactives--;
448 waswriter = SLAP_SOCK_IS_WRITE(s);
450 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
451 (long) s, SLAP_SOCK_IS_READ(s) ? "r" : "",
452 waswriter ? "w" : "" );
453 if ( waswriter ) slap_daemon.sd_nwriters--;
457 /* If we ran out of file descriptors, we dropped a listener from
458 * the select() loop. Now that we're removing a session from our
459 * control, we can try to resume a dropped listener to use.
463 for ( i = 0; slap_listeners[i] != NULL; i++ ) {
464 if ( slap_listeners[i]->sl_sd != AC_SOCKET_INVALID ) {
465 if ( slap_listeners[i]->sl_sd == s ) continue;
466 if ( slap_listeners[i]->sl_is_mute ) {
467 slap_listeners[i]->sl_is_mute = 0;
473 /* Walked the entire list without enabling anything; emfile
474 * counter is stale. Reset it.
476 if ( slap_listeners[i] == NULL )
479 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
480 WAKE_LISTENER(wake || slapd_gentle_shutdown == 2);
483 void slapd_clr_write(ber_socket_t s, int wake) {
484 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
486 assert( SLAP_SOCK_IS_ACTIVE( s ));
487 if ( SLAP_SOCK_IS_WRITE( s )) {
488 SLAP_SOCK_CLR_WRITE( s );
489 slap_daemon.sd_nwriters--;
492 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
496 void slapd_set_write(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_WRITE( s )) {
501 SLAP_SOCK_SET_WRITE( s );
502 slap_daemon.sd_nwriters++;
505 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
509 void slapd_clr_read(ber_socket_t s, int wake) {
510 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
512 assert( SLAP_SOCK_IS_ACTIVE( s ));
513 SLAP_SOCK_CLR_READ( s );
515 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
519 void slapd_set_read(ber_socket_t s, int wake) {
520 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
522 assert( SLAP_SOCK_IS_ACTIVE( s ));
523 if (!SLAP_SOCK_IS_READ( s ))
524 SLAP_SOCK_SET_READ( s );
526 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
530 static void slapd_close(ber_socket_t s) {
531 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
536 static void slap_free_listener_addresses(struct sockaddr **sal)
538 struct sockaddr **sap;
544 for (sap = sal; *sap != NULL; sap++) {
551 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
552 static int get_url_perms(
559 assert( exts != NULL );
560 assert( perms != NULL );
561 assert( crit != NULL );
564 for ( i = 0; exts[ i ]; i++ ) {
565 char *type = exts[ i ];
568 if ( type[ 0 ] == '!' ) {
573 if ( strncasecmp( type, LDAPI_MOD_URLEXT "=", sizeof(LDAPI_MOD_URLEXT "=") - 1 ) == 0 ) {
575 + ( sizeof(LDAPI_MOD_URLEXT "=") - 1 );
579 switch (strlen(value)) {
581 /* skip leading '0' */
582 if ( value[ 0 ] != '0' ) {
588 for ( j = 0; j < 3; j++) {
591 v = value[ j ] - '0';
593 if ( v < 0 || v > 7 ) {
602 for ( j = 1; j < 10; j++ ) {
603 static mode_t m[] = { 0,
604 S_IRUSR, S_IWUSR, S_IXUSR,
605 S_IRGRP, S_IWGRP, S_IXGRP,
606 S_IROTH, S_IWOTH, S_IXOTH
608 static char c[] = "-rwxrwxrwx";
610 if ( value[ j ] == c[ j ] ) {
613 } else if ( value[ j ] != '-' ) {
632 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
634 /* port = 0 indicates AF_LOCAL */
635 static int slap_get_listener_addresses(
638 struct sockaddr ***sal)
640 struct sockaddr **sap;
644 *sal = ch_malloc(2 * sizeof(void *));
650 *sap = ch_malloc(sizeof(struct sockaddr_un));
656 (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) ) {
657 Debug( LDAP_DEBUG_ANY,
658 "daemon: domain socket path (%s) too long in URL",
663 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
664 (*sap)->sa_family = AF_LOCAL;
665 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
669 #ifdef HAVE_GETADDRINFO
670 struct addrinfo hints, *res, *sai;
674 memset( &hints, '\0', sizeof(hints) );
675 hints.ai_flags = AI_PASSIVE;
676 hints.ai_socktype = SOCK_STREAM;
677 hints.ai_family = slap_inet4or6;
678 snprintf(serv, sizeof serv, "%d", port);
680 if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
681 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
682 AC_GAI_STRERROR(err), 0, 0);
687 for (n=2; (sai = sai->ai_next) != NULL; n++) {
690 *sal = ch_calloc(n, sizeof(void *));
698 for ( sai=res; sai; sai=sai->ai_next ) {
699 if( sai->ai_addr == NULL ) {
700 Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
701 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
706 switch (sai->ai_family) {
707 # ifdef LDAP_PF_INET6
709 *sap = ch_malloc(sizeof(struct sockaddr_in6));
714 *(struct sockaddr_in6 *)*sap =
715 *((struct sockaddr_in6 *)sai->ai_addr);
719 *sap = ch_malloc(sizeof(struct sockaddr_in));
724 *(struct sockaddr_in *)*sap =
725 *((struct sockaddr_in *)sai->ai_addr);
733 (*sap)->sa_family = sai->ai_family;
743 struct hostent *he = NULL;
745 if ( host == NULL ) {
746 in.s_addr = htonl(INADDR_ANY);
748 } else if ( !inet_aton( host, &in ) ) {
749 he = gethostbyname( host );
751 Debug( LDAP_DEBUG_ANY,
752 "daemon: invalid host %s", host, 0, 0);
755 for (n = 0; he->h_addr_list[n]; n++) ;
758 *sal = ch_malloc((n+1) * sizeof(void *));
764 for ( i = 0; i<n; i++ ) {
765 sap[i] = ch_malloc(sizeof(struct sockaddr_in));
769 (void)memset( (void *)sap[i], '\0', sizeof(struct sockaddr_in) );
770 sap[i]->sa_family = AF_INET;
771 ((struct sockaddr_in *)sap[i])->sin_port = htons(port);
773 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr, he->h_addr_list[i], sizeof(struct in_addr) );
775 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr, &in, sizeof(struct in_addr) );
785 slap_free_listener_addresses(*sal);
789 static int slap_open_listener(
800 int err, addrlen = 0;
801 struct sockaddr **sal, **psal;
802 int socktype = SOCK_STREAM; /* default to COTS */
804 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
809 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
811 rc = ldap_url_parse( url, &lud );
813 if( rc != LDAP_URL_SUCCESS ) {
814 Debug( LDAP_DEBUG_ANY,
815 "daemon: listen URL \"%s\" parse error=%d\n",
820 l.sl_url.bv_val = NULL;
824 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
825 Debug( LDAP_DEBUG_ANY,
826 "daemon: TLS not supported (%s)\n",
828 ldap_free_urldesc( lud );
832 if(! lud->lud_port ) {
833 lud->lud_port = LDAP_PORT;
837 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
839 if ( l.sl_is_tls && !slap_tls_ctx ) {
840 Debug( LDAP_DEBUG_ANY,
841 "daemon: TLS not configured (%s)\n",
843 ldap_free_urldesc( lud );
846 if(! lud->lud_port ) {
847 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
851 port = (unsigned short) lud->lud_port;
853 tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme);
854 if ( tmp == LDAP_PROTO_IPC ) {
856 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
857 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
859 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
863 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
865 ldap_free_urldesc( lud );
869 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
870 || strcmp(lud->lud_host, "*") == 0 )
872 err = slap_get_listener_addresses(NULL, port, &sal);
874 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
877 #ifdef LDAP_CONNECTIONLESS
878 l.sl_is_udp = ( tmp == LDAP_PROTO_UDP );
881 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
882 if ( lud->lud_exts ) {
883 err = get_url_perms( lud->lud_exts, &l.sl_perms, &crit );
885 l.sl_perms = S_IRWXU | S_IRWXO;
887 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
889 ldap_free_urldesc( lud );
894 /* If we got more than one address returned, we need to make space
895 * for it in the slap_listeners array.
897 for ( num=0; sal[num]; num++ );
900 slap_listeners = ch_realloc( slap_listeners, (*listeners + 1) * sizeof(Listener *) );
904 while ( *sal != NULL ) {
906 switch( (*sal)->sa_family ) {
924 #ifdef LDAP_CONNECTIONLESS
925 if( l.sl_is_udp ) socktype = SOCK_DGRAM;
927 l.sl_sd = socket( (*sal)->sa_family, socktype, 0);
928 if ( l.sl_sd == AC_SOCKET_INVALID ) {
929 int err = sock_errno();
930 Debug( LDAP_DEBUG_ANY,
931 "daemon: %s socket() failed errno=%d (%s)\n",
932 af, err, sock_errstr(err) );
937 if ( l.sl_sd >= dtblsize ) {
938 Debug( LDAP_DEBUG_ANY,
939 "daemon: listener descriptor %ld is too great %ld\n",
940 (long) l.sl_sd, (long) dtblsize, 0 );
941 tcp_close( l.sl_sd );
947 if ( (*sal)->sa_family == AF_LOCAL ) {
948 unlink ( ((struct sockaddr_un *)*sal)->sun_path );
953 /* enable address reuse */
955 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
956 (char *) &tmp, sizeof(tmp) );
957 if ( rc == AC_SOCKET_ERROR ) {
958 int err = sock_errno();
959 Debug( LDAP_DEBUG_ANY,
960 "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
961 (long) l.sl_sd, err, sock_errstr(err) );
966 switch( (*sal)->sa_family ) {
968 addrlen = sizeof(struct sockaddr_in);
973 /* Try to use IPv6 sockets for IPv6 only */
975 rc = setsockopt( l.sl_sd, IPPROTO_IPV6, IPV6_V6ONLY,
976 (char *) &tmp, sizeof(tmp) );
977 if ( rc == AC_SOCKET_ERROR ) {
978 int err = sock_errno();
979 Debug( LDAP_DEBUG_ANY,
980 "slapd(%ld): setsockopt(IPV6_V6ONLY) failed errno=%d (%s)\n",
981 (long) l.sl_sd, err, sock_errstr(err) );
984 addrlen = sizeof(struct sockaddr_in6);
992 setsockopt(l.sl_sd, 0, LOCAL_CREDS, &one, sizeof one);
995 addrlen = sizeof(struct sockaddr_un);
1000 if (bind(l.sl_sd, *sal, addrlen)) {
1002 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
1003 (long) l.sl_sd, err, sock_errstr(err) );
1004 tcp_close( l.sl_sd );
1009 switch ( (*sal)->sa_family ) {
1010 #ifdef LDAP_PF_LOCAL
1012 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
1013 l.sl_name.bv_len = strlen(addr) + sizeof("PATH=") - 1;
1014 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len + 1 );
1015 snprintf( l.sl_name.bv_val, l.sl_name.bv_len + 1,
1018 #endif /* LDAP_PF_LOCAL */
1022 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
1023 char addr[INET_ADDRSTRLEN];
1024 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
1025 addr, sizeof(addr) );
1028 s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr );
1030 port = ntohs( ((struct sockaddr_in *)*sal) ->sin_port );
1031 l.sl_name.bv_val = ber_memalloc( sizeof("IP=255.255.255.255:65535") );
1032 snprintf( l.sl_name.bv_val, sizeof("IP=255.255.255.255:65535"),
1034 s != NULL ? s : SLAP_STRING_UNKNOWN, port );
1035 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1038 #ifdef LDAP_PF_INET6
1040 char addr[INET6_ADDRSTRLEN];
1041 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
1043 port = ntohs( ((struct sockaddr_in6 *)*sal)->sin6_port );
1044 l.sl_name.bv_len = strlen(addr) + sizeof("IP= 65535");
1045 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len );
1046 snprintf( l.sl_name.bv_val, l.sl_name.bv_len, "IP=%s %d",
1048 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1050 #endif /* LDAP_PF_INET6 */
1053 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
1054 (int) (*sal)->sa_family, 0, 0 );
1058 AC_MEMCPY(&l.sl_sa, *sal, addrlen);
1059 ber_str2bv( url, 0, 1, &l.sl_url);
1060 li = ch_malloc( sizeof( Listener ) );
1062 slap_listeners[*cur] = li;
1066 } /* while ( *sal != NULL ) */
1068 slap_free_listener_addresses(psal);
1070 if ( l.sl_url.bv_val == NULL )
1072 Debug( LDAP_DEBUG_TRACE,
1073 "slap_open_listener: failed on %s\n", url, 0, 0 );
1077 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
1078 l.sl_url.bv_val, 0, 0 );
1082 static int sockinit(void);
1083 static int sockdestroy(void);
1085 int slapd_daemon_init( const char *urls )
1090 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
1091 urls ? urls : "<null>", 0, 0 );
1092 if( (rc = sockinit()) != 0 ) {
1097 dtblsize = sysconf( _SC_OPEN_MAX );
1098 #elif HAVE_GETDTABLESIZE
1099 dtblsize = getdtablesize();
1101 dtblsize = FD_SETSIZE;
1104 /* open a pipe (or something equivalent connected to itself).
1105 * we write a byte on this fd whenever we catch a signal. The main
1106 * loop will be select'ing on this socket, and will wake up when
1107 * this byte arrives.
1109 if( (rc = lutil_pair( wake_sds )) < 0 ) {
1110 Debug( LDAP_DEBUG_ANY,
1111 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
1117 if( urls == NULL ) {
1121 u = ldap_str2charray( urls, " " );
1123 if( u == NULL || u[0] == NULL ) {
1124 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
1129 for( i=0; u[i] != NULL; i++ ) {
1130 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
1135 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
1137 ldap_charray_free( u );
1141 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
1143 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
1145 for(n = 0, j = 0; u[n]; n++ ) {
1146 if ( slap_open_listener( u[n], &i, &j ) ) {
1147 ldap_charray_free( u );
1151 slap_listeners[j] = NULL;
1153 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
1157 if( slapd_register_slp ) {
1158 slapd_slp_init( urls );
1163 ldap_charray_free( u );
1164 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
1170 slapd_daemon_destroy(void)
1172 connections_destroy();
1173 tcp_close( wake_sds[1] );
1174 tcp_close( wake_sds[0] );
1178 if( slapd_register_slp ) {
1195 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1196 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1198 slapd_remove( slap_listeners[l]->sl_sd, 0, 0 );
1199 #ifdef LDAP_PF_LOCAL
1200 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1201 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1203 #endif /* LDAP_PF_LOCAL */
1204 slapd_close( slap_listeners[l]->sl_sd );
1206 if ( slap_listeners[l]->sl_url.bv_val )
1207 ber_memfree( slap_listeners[l]->sl_url.bv_val );
1208 if ( slap_listeners[l]->sl_name.bv_val )
1209 ber_memfree( slap_listeners[l]->sl_name.bv_val );
1210 free ( slap_listeners[l] );
1211 slap_listeners[l] = NULL;
1216 slapd_handle_listener(
1223 socklen_t len = sizeof(from);
1226 struct berval authid = BER_BVNULL;
1227 #ifdef SLAPD_RLOOKUPS
1228 char hbuf[NI_MAXHOST];
1231 char *dnsname = NULL;
1232 char *peeraddr = NULL;
1233 #ifdef LDAP_PF_LOCAL
1234 char peername[MAXPATHLEN + sizeof("PATH=")];
1235 #elif defined(LDAP_PF_INET6)
1236 char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
1238 char peername[sizeof("IP=255.255.255.255:65336")];
1239 #endif /* LDAP_PF_LOCAL */
1243 #ifdef LDAP_CONNECTIONLESS
1244 if ( sl->sl_is_udp ) {
1245 /* The first time we receive a query, we set this
1246 * up as a "connection". It remains open for the life
1249 if ( sl->sl_is_udp < 2 ) {
1250 id = connection_init( sl->sl_sd, sl, "", "",
1251 CONN_IS_UDP, ssf, NULL );
1258 # ifdef LDAP_PF_LOCAL
1259 /* FIXME: apparently accept doesn't fill
1260 * the sun_path sun_path member */
1261 from.sa_un_addr.sun_path[0] = '\0';
1262 # endif /* LDAP_PF_LOCAL */
1264 s = accept( sl->sl_sd, (struct sockaddr *) &from, &len );
1265 if ( s == AC_SOCKET_INVALID ) {
1266 int err = sock_errno();
1277 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1279 /* Stop listening until an existing session closes */
1281 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1284 Debug( LDAP_DEBUG_ANY,
1285 "daemon: accept(%ld) failed errno=%d (%s)\n",
1286 (long) sl->sl_sd, err,
1288 ldap_pvt_thread_yield();
1292 #ifndef HAVE_WINSOCK
1293 /* make sure descriptor number isn't too great */
1294 if ( s >= dtblsize ) {
1295 Debug( LDAP_DEBUG_ANY,
1296 "daemon: %ld beyond descriptor table size %ld\n",
1297 (long) s, (long) dtblsize, 0 );
1300 ldap_pvt_thread_yield();
1306 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1308 /* newly accepted stream should not be in any of the FD SETS */
1309 assert( SLAP_SOCK_NOT_ACTIVE( s ));
1311 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1314 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1315 #ifdef LDAP_PF_LOCAL
1316 /* for IPv4 and IPv6 sockets only */
1317 if ( from.sa_addr.sa_family != AF_LOCAL )
1318 #endif /* LDAP_PF_LOCAL */
1323 /* enable keep alives */
1325 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1326 (char *) &tmp, sizeof(tmp) );
1327 if ( rc == AC_SOCKET_ERROR ) {
1328 int err = sock_errno();
1329 Debug( LDAP_DEBUG_ANY,
1330 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1331 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1335 /* enable no delay */
1337 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1338 (char *)&tmp, sizeof(tmp) );
1339 if ( rc == AC_SOCKET_ERROR ) {
1340 int err = sock_errno();
1341 Debug( LDAP_DEBUG_ANY,
1342 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1343 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1349 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
1351 switch ( from.sa_addr.sa_family ) {
1352 # ifdef LDAP_PF_LOCAL
1354 /* FIXME: apparently accept doesn't fill
1355 * the sun_path sun_path member */
1356 if ( from.sa_un_addr.sun_path[0] == '\0' ) {
1357 AC_MEMCPY( from.sa_un_addr.sun_path,
1358 sl->sl_sa.sa_un_addr.sun_path,
1359 sizeof( from.sa_un_addr.sun_path ) );
1362 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1368 if( getpeereid( s, &uid, &gid ) == 0 ) {
1369 authid.bv_val = ch_malloc(
1370 STRLENOF( "gidNumber=4294967295+uidNumber=4294967295,"
1371 "cn=peercred,cn=external,cn=auth" ) + 1 );
1372 authid.bv_len = sprintf( authid.bv_val,
1373 "gidNumber=%d+uidNumber=%d,"
1374 "cn=peercred,cn=external,cn=auth",
1375 (int) gid, (int) uid );
1376 assert( authid.bv_len <=
1377 STRLENOF( "gidNumber=4294967295+uidNumber=4294967295,"
1378 "cn=peercred,cn=external,cn=auth" ) );
1383 #endif /* LDAP_PF_LOCAL */
1385 # ifdef LDAP_PF_INET6
1387 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1388 peeraddr = inet_ntoa( *((struct in_addr *)
1389 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1390 sprintf( peername, "IP=%s:%d",
1391 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1392 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1394 char addr[INET6_ADDRSTRLEN];
1396 peeraddr = (char *) inet_ntop( AF_INET6,
1397 &from.sa_in6_addr.sin6_addr,
1398 addr, sizeof addr );
1399 sprintf( peername, "IP=%s %d",
1400 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1401 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1404 # endif /* LDAP_PF_INET6 */
1407 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1408 sprintf( peername, "IP=%s:%d",
1409 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1410 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1418 if ( ( from.sa_addr.sa_family == AF_INET )
1419 #ifdef LDAP_PF_INET6
1420 || ( from.sa_addr.sa_family == AF_INET6 )
1423 #ifdef SLAPD_RLOOKUPS
1424 if ( use_reverse_lookup ) {
1426 if (ldap_pvt_get_hname( (const struct sockaddr *)&from, len, hbuf,
1427 sizeof(hbuf), &herr ) == 0) {
1428 ldap_pvt_str2lower( hbuf );
1434 #endif /* SLAPD_RLOOKUPS */
1437 if ( !hosts_ctl("slapd",
1438 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1439 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1440 SLAP_STRING_UNKNOWN ))
1443 Statslog( LDAP_DEBUG_STATS,
1444 "fd=%ld DENIED from %s (%s)\n",
1446 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1447 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1452 #endif /* HAVE_TCPD */
1455 id = connection_init(s, sl,
1456 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1459 sl->sl_is_tls ? CONN_IS_TLS : 0,
1464 authid.bv_val ? &authid : NULL );
1466 if( authid.bv_val ) ch_free(authid.bv_val);
1469 Debug( LDAP_DEBUG_ANY,
1470 "daemon: connection_init(%ld, %s, %s) failed.\n",
1471 (long) s, peername, sl->sl_name.bv_val );
1476 Statslog( LDAP_DEBUG_STATS,
1477 "conn=%ld fd=%ld ACCEPT from %s (%s)\n",
1478 id, (long) s, peername, sl->sl_name.bv_val,
1481 slapd_add( s, 1, NULL );
1491 time_t last_idle_check = 0;
1492 struct timeval idle;
1495 #define SLAPD_IDLE_CHECK_LIMIT 4
1497 if ( global_idletimeout > 0 ) {
1498 last_idle_check = slap_get_time();
1499 /* Set the select timeout.
1500 * Don't just truncate, preserve the fractions of
1501 * seconds to prevent sleeping for zero time.
1503 idle.tv_sec = global_idletimeout/SLAPD_IDLE_CHECK_LIMIT;
1504 idle.tv_usec = global_idletimeout - idle.tv_sec * SLAPD_IDLE_CHECK_LIMIT;
1505 idle.tv_usec *= 1000000 / SLAPD_IDLE_CHECK_LIMIT;
1511 slapd_add( wake_sds[0], 0, NULL );
1513 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1514 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1516 #ifdef LDAP_CONNECTIONLESS
1517 /* Since this is connectionless, the data port is the
1518 * listening port. The listen() and accept() calls
1521 if ( slap_listeners[l]->sl_is_udp ) {
1522 slapd_add( slap_listeners[l]->sl_sd, 1, slap_listeners[l] );
1527 if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN_BACKLOG ) == -1 ) {
1528 int err = sock_errno();
1530 #ifdef LDAP_PF_INET6
1531 /* If error is EADDRINUSE, we are trying to listen to INADDR_ANY and
1532 * we are already listening to in6addr_any, then we want to ignore
1533 * this and continue.
1535 if ( err == EADDRINUSE ) {
1537 struct sockaddr_in sa = slap_listeners[l]->sl_sa.sa_in_addr;
1538 struct sockaddr_in6 sa6;
1540 if ( sa.sin_family == AF_INET &&
1541 sa.sin_addr.s_addr == htonl(INADDR_ANY) ) {
1542 for ( i = 0 ; i < l; i++ ) {
1543 sa6 = slap_listeners[i]->sl_sa.sa_in6_addr;
1544 if ( sa6.sin6_family == AF_INET6 &&
1545 !memcmp( &sa6.sin6_addr, &in6addr_any, sizeof(struct in6_addr) ) )
1550 /* We are already listening to in6addr_any */
1551 Debug( LDAP_DEBUG_CONNS,
1552 "daemon: Attempt to listen to 0.0.0.0 failed, already listening on ::, assuming IPv4 included\n",
1554 slapd_close( slap_listeners[l]->sl_sd );
1555 slap_listeners[l]->sl_sd = AC_SOCKET_INVALID;
1561 Debug( LDAP_DEBUG_ANY,
1562 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
1563 slap_listeners[l]->sl_url.bv_val, err,
1565 return( (void*)-1 );
1568 slapd_add( slap_listeners[l]->sl_sd, 0, slap_listeners[l] );
1571 #ifdef HAVE_NT_SERVICE_MANAGER
1572 if ( started_event != NULL ) {
1573 ldap_pvt_thread_cond_signal( &started_event );
1576 /* initialization complete. Here comes the loop. */
1578 while ( !slapd_shutdown ) {
1583 #if SLAP_EVENTS_ARE_INDEXED
1584 ber_socket_t nrfds, nwfds;
1586 #define SLAPD_EBADF_LIMIT 16
1593 struct timeval *tvp;
1595 struct timeval *cat;
1598 now = slap_get_time();
1600 if( ( global_idletimeout > 0 ) &&
1601 difftime( last_idle_check +
1602 global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 ) {
1603 connections_timeout_idle( now );
1604 last_idle_check = now;
1609 if( slapd_gentle_shutdown ) {
1610 ber_socket_t active;
1612 if( slapd_gentle_shutdown == 1 ) {
1613 Debug( LDAP_DEBUG_ANY, "slapd gentle shutdown\n", 0, 0, 0 );
1614 close_listeners( 1 );
1615 frontendDB->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
1616 slapd_gentle_shutdown = 2;
1619 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1620 active = slap_daemon.sd_nactives;
1621 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1631 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1633 nwriters = slap_daemon.sd_nwriters;
1636 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1637 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1639 if ( slap_listeners[l]->sl_is_mute )
1640 SLAP_SOCK_SET_MUTE( slap_listeners[l]->sl_sd );
1642 if ( SLAP_SOCK_IS_MUTE( slap_listeners[l]->sl_sd ))
1643 SLAP_SOCK_CLR_MUTE( slap_listeners[l]->sl_sd );
1646 nfds = SLAP_EVENT_MAX;
1648 if ( global_idletimeout && slap_daemon.sd_nactives )
1651 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1654 #if defined(HAVE_YIELDING_SELECT) || defined(NO_THREADS)
1655 && ( tv.tv_sec || tv.tv_usec )
1662 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1663 rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
1664 while ( cat && cat->tv_sec && cat->tv_sec <= now ) {
1665 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
1666 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1668 ldap_pvt_runqueue_runtask( &slapd_rq, rtask );
1669 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1670 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1671 ldap_pvt_thread_pool_submit( &connection_pool,
1672 rtask->routine, (void *) rtask );
1673 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1675 rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
1677 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1679 if ( cat && cat->tv_sec ) {
1680 time_t diff = difftime( cat->tv_sec, now );
1683 if ( tvp == NULL || diff < tv.tv_sec ) {
1690 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1691 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ||
1692 slap_listeners[l]->sl_is_mute )
1695 Debug( LDAP_DEBUG_CONNS,
1696 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
1697 slap_listeners[l]->sl_sd, at,
1698 tvp == NULL ? "NULL" : "zero" );
1701 switch(ns = SLAP_EVENT_WAIT(tvp))
1703 case -1: { /* failure - try again */
1704 int err = sock_errno();
1708 /* you'd think this would be EBADF */
1709 || err == WSAENOTSOCK
1712 if (++ebadf < SLAPD_EBADF_LIMIT)
1716 if( err != EINTR ) {
1717 Debug( LDAP_DEBUG_CONNS,
1718 "daemon: select failed (%d): %s\n",
1719 err, sock_errstr(err), 0 );
1725 case 0: /* timeout - let threads run */
1727 #ifndef HAVE_YIELDING_SELECT
1728 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
1731 ldap_pvt_thread_yield();
1735 default: /* something happened - deal with it */
1736 if( slapd_shutdown ) continue;
1739 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
1744 #if SLAP_EVENTS_ARE_INDEXED
1745 if ( SLAP_EVENT_IS_READ( wake_sds[0] )) {
1747 tcp_read( wake_sds[0], c, sizeof(c) );
1750 SLAP_EVENT_CLR_READ( wake_sds[0] );
1754 /* The event slot equals the descriptor number - this is
1755 * true for Unix select and poll. We treat Windows select
1756 * like this too, even though it's a kludge.
1758 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1761 if ( ns <= 0 ) break;
1763 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1766 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 */
1778 /* Don't need to look at this in the data loops */
1779 SLAP_EVENT_CLR_READ( slap_listeners[l]->sl_sd );
1780 SLAP_EVENT_CLR_WRITE( slap_listeners[l]->sl_sd );
1783 /* bypass the following tests if no descriptors left */
1785 #ifndef HAVE_YIELDING_SELECT
1786 ldap_pvt_thread_yield();
1791 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1793 nrfds = readfds.fd_count;
1794 nwfds = writefds.fd_count;
1795 for ( i = 0; i < readfds.fd_count; i++ ) {
1796 Debug( LDAP_DEBUG_CONNS, " %d%s",
1797 readfds.fd_array[i], "r", 0 );
1799 for ( i = 0; i < writefds.fd_count; i++ ) {
1800 Debug( LDAP_DEBUG_CONNS, " %d%s",
1801 writefds.fd_array[i], "w", 0 );
1807 for ( i = 0; i < nfds; i++ ) {
1810 r = SLAP_EVENT_IS_READ( i );
1811 /* writefds was not initialized if nwriters was zero */
1812 w = nwriters ? SLAP_EVENT_IS_WRITE( i ) : 0;
1814 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1815 r ? "r" : "", w ? "w" : "" );
1825 if ( ns <= 0 ) break;
1828 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1831 /* loop through the writers */
1832 for ( i = 0; nwfds > 0; i++ )
1836 wd = writefds.fd_array[i];
1838 if( ! SLAP_EVENT_IS_WRITE( i ) ) {
1845 Debug( LDAP_DEBUG_CONNS,
1846 "daemon: write active on %d\n",
1849 * NOTE: it is possible that the connection was closed
1850 * and that the stream is now inactive.
1851 * connection_write() must valid the stream is still
1855 if ( connection_write( wd ) < 0 ) {
1856 if ( SLAP_EVENT_IS_READ( wd )) {
1857 SLAP_EVENT_CLR_READ( (unsigned) wd );
1862 SLAP_EVENT_CLR_WRITE( wd );
1865 for ( i = 0; nrfds > 0; i++ )
1869 rd = readfds.fd_array[i];
1871 if( ! SLAP_EVENT_IS_READ( i ) ) {
1878 Debug ( LDAP_DEBUG_CONNS,
1879 "daemon: read activity on %d\n", rd, 0, 0 );
1881 * NOTE: it is possible that the connection was closed
1882 * and that the stream is now inactive.
1883 * connection_read() must valid the stream is still
1887 if ( connection_read( rd ) < 0 ) {
1891 #else /* !SLAP_EVENTS_ARE_INDEXED */
1893 /* The events are returned in an arbitrary list. This is true
1894 * for /dev/poll, epoll and kqueue. In order to prioritize things
1895 * so that we can handle wake_sds first, listeners second, and then
1896 * all other connections last (as we do for select), we would need
1897 * to use multiple event handles and cascade them.
1899 * That seems like a bit of hassle. So the wake_sds check has been
1900 * skipped. For epoll and kqueue we can associate arbitrary data with
1901 * an event, so we could use pointers to the listener structure
1902 * instead of just the file descriptor. For /dev/poll we have to
1903 * search the listeners array for a matching descriptor.
1905 /* if waking is set and we woke up, we'll read whatever
1910 tcp_read( wake_sds[0], c, sizeof(c) );
1917 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1919 for (i=0; i<ns; i++) {
1922 if ( SLAP_EVENT_IS_LISTENER(i)
1923 #ifdef LDAP_CONNECTIONLESS
1924 && !((SLAP_EVENT_LISTENER(i))->sl_is_udp)
1928 /* Don't log internal wake events */
1929 if ( SLAP_EVENT_FD( i ) == wake_sds[0] )
1932 r = SLAP_EVENT_IS_READ( i );
1933 w = SLAP_EVENT_IS_WRITE( i );
1935 Debug( LDAP_DEBUG_CONNS, " %d%s%s", SLAP_EVENT_FD(i),
1936 r ? "r" : "", w ? "w" : "" );
1940 for (i=0; i<ns; i++) {
1943 if ( SLAP_EVENT_IS_LISTENER(i) ) {
1944 rc = slapd_handle_listener( SLAP_EVENT_LISTENER( i ));
1946 /* If we found a regular listener, rc is now zero, and we
1947 * can skip the data portion. But if it was a UDP listener
1948 * then rc is still 1, and we want to handle the data.
1951 fd = SLAP_EVENT_FD( i );
1953 /* Ignore wake events, they were handled above */
1954 if ( fd == wake_sds[0] )
1957 if( SLAP_EVENT_IS_WRITE( i ) ) {
1958 Debug( LDAP_DEBUG_CONNS,
1959 "daemon: write active on %d\n",
1962 * NOTE: it is possible that the connection was closed
1963 * and that the stream is now inactive.
1964 * connection_write() must valid the stream is still
1968 if ( connection_write( fd ) < 0 ) {
1973 if( SLAP_EVENT_IS_READ( i ) ) {
1974 Debug( LDAP_DEBUG_CONNS,
1975 "daemon: read active on %d\n",
1978 * NOTE: it is possible that the connection was closed
1979 * and that the stream is now inactive.
1980 * connection_read() must valid the stream is still
1984 if ( connection_read( fd ) < 0 ) {
1990 #endif /* SLAP_EVENTS_ARE_INDEXED */
1992 #ifndef HAVE_YIELDING_SELECT
1993 ldap_pvt_thread_yield();
1997 if( slapd_shutdown == 1 ) {
1998 Debug( LDAP_DEBUG_ANY,
1999 "daemon: shutdown requested and initiated.\n",
2002 } else if ( slapd_shutdown == 2 ) {
2003 #ifdef HAVE_NT_SERVICE_MANAGER
2004 Debug( LDAP_DEBUG_ANY,
2005 "daemon: shutdown initiated by Service Manager.\n",
2007 #else /* !HAVE_NT_SERVICE_MANAGER */
2008 Debug( LDAP_DEBUG_ANY,
2009 "daemon: abnormal condition, shutdown initiated.\n",
2011 #endif /* !HAVE_NT_SERVICE_MANAGER */
2013 Debug( LDAP_DEBUG_ANY,
2014 "daemon: no active streams, shutdown initiated.\n",
2018 if( slapd_gentle_shutdown != 2 ) {
2019 close_listeners ( 0 );
2022 if( !slapd_gentle_shutdown ) {
2023 slapd_abrupt_shutdown = 1;
2024 connections_shutdown();
2027 Debug( LDAP_DEBUG_ANY,
2028 "slapd shutdown: waiting for %d threads to terminate\n",
2029 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
2030 ldap_pvt_thread_pool_destroy(&connection_pool, 1);
2032 free ( slap_listeners );
2033 slap_listeners = NULL;
2039 int slapd_daemon( void )
2045 #define SLAPD_LISTENER_THREAD 1
2046 #if defined( SLAPD_LISTENER_THREAD )
2048 ldap_pvt_thread_t listener_tid;
2050 /* listener as a separate THREAD */
2051 rc = ldap_pvt_thread_create( &listener_tid,
2052 0, slapd_daemon_task, NULL );
2055 Debug( LDAP_DEBUG_ANY,
2056 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
2060 /* wait for the listener thread to complete */
2061 ldap_pvt_thread_join( listener_tid, (void *) NULL );
2064 /* experimental code */
2065 slapd_daemon_task( NULL );
2072 static int sockinit(void)
2074 #if defined( HAVE_WINSOCK2 )
2075 WORD wVersionRequested;
2079 wVersionRequested = MAKEWORD( 2, 0 );
2081 err = WSAStartup( wVersionRequested, &wsaData );
2083 /* Tell the user that we couldn't find a usable */
2088 /* Confirm that the WinSock DLL supports 2.0.*/
2089 /* Note that if the DLL supports versions greater */
2090 /* than 2.0 in addition to 2.0, it will still return */
2091 /* 2.0 in wVersion since that is the version we */
2094 if ( LOBYTE( wsaData.wVersion ) != 2 ||
2095 HIBYTE( wsaData.wVersion ) != 0 )
2097 /* Tell the user that we couldn't find a usable */
2103 /* The WinSock DLL is acceptable. Proceed. */
2104 #elif defined( HAVE_WINSOCK )
2106 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
2113 static int sockdestroy(void)
2115 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
2122 slap_sig_shutdown( int sig )
2125 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
2129 * If the NT Service Manager is controlling the server, we don't
2130 * want SIGBREAK to kill the server. For some strange reason,
2131 * SIGBREAK is generated when a user logs out.
2134 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
2135 if (is_NT_Service && sig == SIGBREAK)
2140 if (sig == SIGHUP && global_gentlehup && slapd_gentle_shutdown == 0)
2141 slapd_gentle_shutdown = 1;
2147 /* reinstall self */
2148 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
2152 slap_sig_wake( int sig )
2156 /* reinstall self */
2157 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
2161 void slapd_add_internal(ber_socket_t s, int isactive) {
2162 slapd_add(s, isactive, NULL);
2165 Listener ** slapd_get_listeners(void) {
2166 return slap_listeners;
2169 void slap_wake_listener()