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 int allow_severity = LOG_INFO;
50 int deny_severity = LOG_NOTICE;
52 # define SLAP_STRING_UNKNOWN STRING_UNKNOWN
53 #else /* ! TCP Wrappers */
54 # define SLAP_STRING_UNKNOWN "unknown"
55 #endif /* ! TCP Wrappers */
58 # include <sys/stat.h>
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) { \
88 tcp_write( wake_sds[1], "0", 1 ); \
92 volatile sig_atomic_t slapd_shutdown = 0;
93 volatile sig_atomic_t slapd_gentle_shutdown = 0;
94 volatile sig_atomic_t slapd_abrupt_shutdown = 0;
96 static struct slap_daemon {
97 ldap_pvt_thread_mutex_t sd_mutex;
99 ber_socket_t sd_nactives;
103 struct epoll_event *sd_epolls;
110 /* In winsock, accept() returns values higher than dtblsize
111 so don't bother with this optimization */
121 # define SLAP_EVENTS_ARE_INDEXED 0
122 # define SLAP_SOCK_IX(s) (slap_daemon.sd_index[(s)])
123 # define SLAP_SOCK_EP(s) (slap_daemon.sd_epolls[SLAP_SOCK_IX(s)])
124 # define SLAP_SOCK_EV(s) (SLAP_SOCK_EP(s).events)
125 # define SLAP_SOCK_IS_ACTIVE(s) (SLAP_SOCK_IX(s) != -1)
126 # define SLAP_SOCK_NOT_ACTIVE(s) (SLAP_SOCK_IX(s) == -1)
127 # define SLAP_SOCK_IS_SET(s, mode) (SLAP_SOCK_EV(s) & (mode))
129 # define SLAP_SOCK_IS_READ(s) SLAP_SOCK_IS_SET((s), EPOLLIN)
130 # define SLAP_SOCK_IS_WRITE(s) SLAP_SOCK_IS_SET((s), EPOLLOUT)
132 # define SLAP_SET_SOCK(s, mode) do { \
133 if ((SLAP_SOCK_EV(s) & (mode)) != (mode)) { \
134 SLAP_SOCK_EV(s) |= (mode); \
135 epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_MOD, (s), \
140 # define SLAP_CLR_SOCK(s, mode) do { \
141 if ((SLAP_SOCK_EV(s) & (mode))) { \
142 SLAP_SOCK_EV(s) &= ~(mode); \
143 epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_MOD, s, \
148 # define SLAP_SOCK_SET_READ(s) SLAP_SET_SOCK(s, EPOLLIN)
149 # define SLAP_SOCK_SET_WRITE(s) SLAP_SET_SOCK(s, EPOLLOUT)
151 # ifdef SLAP_LIGHTWEIGHT_DISPATCHER
152 # define SLAP_SOCK_SET_SUSPEND(s) \
153 ( slap_daemon.sd_suspend[SLAP_SOCK_IX(s)] = 1 )
154 # define SLAP_SOCK_CLR_SUSPEND(s) \
155 ( slap_daemon.sd_suspend[SLAP_SOCK_IX(s)] = 0 )
156 # define SLAP_SOCK_IS_SUSPEND(s) \
157 ( slap_daemon.sd_suspend[SLAP_SOCK_IX(s)] == 1 )
160 # define SLAP_SOCK_CLR_READ(s) SLAP_CLR_SOCK((s), EPOLLIN)
161 # define SLAP_SOCK_CLR_WRITE(s) SLAP_CLR_SOCK((s), EPOLLOUT)
163 # define SLAP_CLR_EVENT(i, mode) (revents[(i)].events &= ~(mode))
165 # define SLAP_EVENT_MAX slap_daemon.sd_nfds
167 /* If a Listener address is provided, store that as the epoll data.
168 * Otherwise, store the address of this socket's slot in the
169 * index array. If we can't do this add, the system is out of
170 * resources and we need to shutdown.
172 # define SLAP_ADD_SOCK(s, l) do { \
174 SLAP_SOCK_IX((s)) = slap_daemon.sd_nfds; \
175 SLAP_SOCK_EP((s)).data.ptr = (l) ? (l) : (void *)(&SLAP_SOCK_IX(s)); \
176 SLAP_SOCK_EV((s)) = EPOLLIN; \
177 rc = epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_ADD, \
178 (s), &SLAP_SOCK_EP((s))); \
180 slap_daemon.sd_nfds++; \
182 Debug( LDAP_DEBUG_ANY, \
183 "daemon: epoll_ctl(ADD,fd=%d) failed, errno=%d, shutting down\n", \
185 slapd_shutdown = 2; \
189 # define SLAP_EV_LISTENER(ptr) (((int *)(ptr) >= slap_daemon.sd_index && \
190 (int *)(ptr) <= (slap_daemon.sd_index+dtblsize)) ? 0 : 1 )
192 # define SLAP_EV_PTRFD(ptr) (SLAP_EV_LISTENER(ptr) ? \
193 ((Listener *)ptr)->sl_sd : (int *)(ptr) - slap_daemon.sd_index)
195 # define SLAP_DEL_SOCK(s) do { \
196 int fd, rc, index = SLAP_SOCK_IX((s)); \
197 rc = epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_DEL, \
198 (s), &SLAP_SOCK_EP((s))); \
199 slap_daemon.sd_epolls[index] = \
200 slap_daemon.sd_epolls[slap_daemon.sd_nfds-1]; \
201 fd = SLAP_EV_PTRFD(slap_daemon.sd_epolls[index].data.ptr); \
202 slap_daemon.sd_index[fd] = index; \
203 slap_daemon.sd_index[(s)] = -1; \
204 slap_daemon.sd_nfds--; \
207 # define SLAP_EVENT_CLR_READ(i) SLAP_CLR_EVENT((i), EPOLLIN)
208 # define SLAP_EVENT_CLR_WRITE(i) SLAP_CLR_EVENT((i), EPOLLOUT)
210 # define SLAP_CHK_EVENT(i, mode) (revents[(i)].events & mode)
212 # define SLAP_EVENT_IS_READ(i) SLAP_CHK_EVENT((i), EPOLLIN)
213 # define SLAP_EVENT_IS_WRITE(i) SLAP_CHK_EVENT((i), EPOLLOUT)
214 # define SLAP_EVENT_IS_LISTENER(i) SLAP_EV_LISTENER(revents[(i)].data.ptr)
215 # define SLAP_EVENT_LISTENER(i) (revents[(i)].data.ptr)
217 # define SLAP_EVENT_FD(i) SLAP_EV_PTRFD(revents[(i)].data.ptr)
219 # define SLAP_SOCK_SET_INIT do { \
220 slap_daemon.sd_epolls = ch_calloc(1, \
221 sizeof(struct epoll_event) * dtblsize * 2); \
222 slap_daemon.sd_index = ch_malloc(sizeof(int) * dtblsize); \
223 slap_daemon.sd_epfd = epoll_create( dtblsize ); \
224 for (i=0; i<dtblsize; i++) slap_daemon.sd_index[i] = -1; \
227 # define SLAP_EVENT_DECL struct epoll_event *revents
229 # define SLAP_EVENT_INIT do { \
230 revents = slap_daemon.sd_epolls + dtblsize; \
233 # define SLAP_EVENT_WAIT(tvp) \
234 epoll_wait( slap_daemon.sd_epfd, revents, \
235 dtblsize, (tvp) ? (tvp)->tv_sec * 1000 : -1 )
240 # define SLAP_EVENTS_ARE_INDEXED 1
241 # define SLAP_EVENT_DECL \
242 fd_set readfds, writefds
244 # define SLAP_EVENT_INIT do { \
245 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) ); \
247 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) ); \
249 FD_ZERO( &writefds ); \
254 # define CHK_SETSIZE do { \
255 if (dtblsize > FD_SETSIZE) dtblsize = FD_SETSIZE; \
258 # define CHK_SETSIZE do { ; } while (0)
261 # define SLAP_SOCK_SET_INIT do { \
263 FD_ZERO(&slap_daemon.sd_readers); \
264 FD_ZERO(&slap_daemon.sd_writers); \
267 # define SLAP_SOCK_IS_ACTIVE(fd) FD_ISSET((fd), &slap_daemon.sd_actives)
268 # define SLAP_SOCK_IS_READ(fd) FD_ISSET((fd), &slap_daemon.sd_readers)
269 # define SLAP_SOCK_IS_WRITE(fd) FD_ISSET((fd), &slap_daemon.sd_writers)
271 # define SLAP_SOCK_NOT_ACTIVE(fd) (!SLAP_SOCK_IS_ACTIVE(fd) && \
272 !SLAP_SOCK_IS_READ(fd) && !SLAP_SOCK_IS_WRITE(fd))
275 # define SLAP_SOCK_SET_READ(fd) do { \
276 if (!SLAP_SOCK_IS_READ(fd)) { FD_SET((fd), &slap_daemon.sd_readers); } \
278 # define SLAP_SOCK_SET_WRITE(fd) do { \
279 if (!SLAP_SOCK_IS_WRITE(fd)) { FD_SET((fd), &slap_daemon.sd_writers); } \
282 # define SLAP_ADDTEST(s) do { } while 0
283 # define SLAP_EVENT_MAX dtblsize
285 # define SLAP_SOCK_SET_READ(fd) FD_SET((fd), &slap_daemon.sd_readers)
286 # define SLAP_SOCK_SET_WRITE(fd) FD_SET((fd), &slap_daemon.sd_writers)
288 # define SLAP_EVENT_MAX slap_daemon.sd_nfds
289 # define SLAP_ADDTEST(s) do { \
290 if ((s) >= slap_daemon.sd_nfds) slap_daemon.sd_nfds = (s)+1; \
294 # define SLAP_SOCK_CLR_READ(fd) FD_CLR((fd), &slap_daemon.sd_readers)
295 # define SLAP_SOCK_CLR_WRITE(fd) FD_CLR((fd), &slap_daemon.sd_writers)
297 # define SLAP_ADD_SOCK(s, l) do { \
299 FD_SET((s), &slap_daemon.sd_actives); \
300 FD_SET((s), &slap_daemon.sd_readers); \
303 # define SLAP_DEL_SOCK(s) do { \
304 FD_CLR((s), &slap_daemon.sd_actives); \
305 FD_CLR((s), &slap_daemon.sd_readers); \
306 FD_CLR((s), &slap_daemon.sd_writers); \
309 # define SLAP_EVENT_IS_READ(fd) FD_ISSET((fd), &readfds)
310 # define SLAP_EVENT_IS_WRITE(fd) FD_ISSET((fd), &writefds)
312 # define SLAP_EVENT_CLR_READ(fd) FD_CLR((fd), &readfds)
313 # define SLAP_EVENT_CLR_WRITE(fd) FD_CLR((fd), &writefds)
315 # define SLAP_EVENT_WAIT(tvp) \
316 select( SLAP_EVENT_MAX, &readfds, \
317 nwriters > 0 ? &writefds : NULL, NULL, (tvp) )
322 * SLP related functions
326 #define LDAP_SRVTYPE_PREFIX "service:ldap://"
327 #define LDAPS_SRVTYPE_PREFIX "service:ldaps://"
328 static char** slapd_srvurls = NULL;
329 static SLPHandle slapd_hslp = 0;
330 int slapd_register_slp = 0;
332 void slapd_slp_init( const char* urls ) {
335 slapd_srvurls = ldap_str2charray( urls, " " );
337 if( slapd_srvurls == NULL ) return;
339 /* find and expand INADDR_ANY URLs */
340 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
341 if( strcmp( slapd_srvurls[i], "ldap:///" ) == 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( LDAP_SRVTYPE_PREFIX ) );
347 strcpy( lutil_strcopy(slapd_srvurls[i],
348 LDAP_SRVTYPE_PREFIX ), host );
353 } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0) {
354 char *host = ldap_pvt_get_fqdn( NULL );
355 if ( host != NULL ) {
356 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
358 sizeof( LDAPS_SRVTYPE_PREFIX ) );
359 strcpy( lutil_strcopy(slapd_srvurls[i],
360 LDAPS_SRVTYPE_PREFIX ), host );
367 /* open the SLP handle */
368 SLPOpen( "en", 0, &slapd_hslp );
371 void slapd_slp_deinit() {
372 if( slapd_srvurls == NULL ) return;
374 ldap_charray_free( slapd_srvurls );
375 slapd_srvurls = NULL;
377 /* close the SLP handle */
378 SLPClose( slapd_hslp );
381 void slapd_slp_regreport(
389 void slapd_slp_reg() {
392 if( slapd_srvurls == NULL ) return;
394 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
395 if( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
396 sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
397 strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
398 sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
402 SLP_LIFETIME_MAXIMUM,
412 void slapd_slp_dereg() {
415 if( slapd_srvurls == NULL ) return;
417 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
418 SLPDereg( slapd_hslp,
424 #endif /* HAVE_SLP */
427 * Add a descriptor to daemon control
429 * If isactive, the descriptor is a live server session and is subject
430 * to idletimeout control. Otherwise, the descriptor is a passive
431 * listener or an outbound client session, and not subject to
432 * idletimeout. The underlying event handler may record the Listener
433 * argument to differentiate Listener's from real sessions.
435 static void slapd_add(ber_socket_t s, int isactive, Listener *sl) {
436 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
438 assert( SLAP_SOCK_NOT_ACTIVE(s) );
440 if ( isactive ) slap_daemon.sd_nactives++;
442 SLAP_ADD_SOCK(s, sl);
444 Debug( LDAP_DEBUG_CONNS, "daemon: added %ldr\n",
447 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
449 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
455 * Remove the descriptor from daemon control
465 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
467 if ( wasactive ) slap_daemon.sd_nactives--;
469 waswriter = SLAP_SOCK_IS_WRITE(s);
470 wasreader = SLAP_SOCK_IS_READ(s);
472 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
474 wasreader ? "r" : "",
475 waswriter ? "w" : "" );
477 if ( waswriter ) slap_daemon.sd_nwriters--;
481 /* If we ran out of file descriptors, we dropped a listener from
482 * the select() loop. Now that we're removing a session from our
483 * control, we can try to resume a dropped listener to use.
487 for ( i = 0; slap_listeners[i] != NULL; i++ ) {
488 Listener *lr = slap_listeners[i];
490 if ( lr->sl_sd == AC_SOCKET_INVALID ) continue;
491 if ( lr->sl_sd == s ) continue;
498 /* Walked the entire list without enabling anything; emfile
499 * counter is stale. Reset it.
501 if ( slap_listeners[i] == NULL ) emfile = 0;
503 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
504 WAKE_LISTENER(wake || slapd_gentle_shutdown == 2);
507 void slapd_clr_write(ber_socket_t s, int wake) {
508 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
510 assert( SLAP_SOCK_IS_ACTIVE( s ));
512 if ( SLAP_SOCK_IS_WRITE( s )) {
513 SLAP_SOCK_CLR_WRITE( s );
514 slap_daemon.sd_nwriters--;
517 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
521 void slapd_set_write(ber_socket_t s, int wake) {
522 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
524 assert( SLAP_SOCK_IS_ACTIVE( s ));
526 if ( !SLAP_SOCK_IS_WRITE( s )) {
527 SLAP_SOCK_SET_WRITE( s );
528 slap_daemon.sd_nwriters++;
531 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
535 void slapd_clr_read(ber_socket_t s, int wake) {
536 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
538 assert( SLAP_SOCK_IS_ACTIVE( s ));
539 SLAP_SOCK_CLR_READ( s );
541 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
545 void slapd_set_read(ber_socket_t s, int wake) {
546 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
548 assert( SLAP_SOCK_IS_ACTIVE( s ));
549 if (!SLAP_SOCK_IS_READ( s )) SLAP_SOCK_SET_READ( s );
551 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
555 static void slapd_close(ber_socket_t s) {
556 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
561 static void slap_free_listener_addresses(struct sockaddr **sal) {
562 struct sockaddr **sap;
563 if (sal == NULL) return;
564 for (sap = sal; *sap != NULL; sap++) ch_free(*sap);
568 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
569 static int get_url_perms(
576 assert( exts != NULL );
577 assert( perms != NULL );
578 assert( crit != NULL );
581 for ( i = 0; exts[ i ]; i++ ) {
582 char *type = exts[ i ];
585 if ( type[ 0 ] == '!' ) {
590 if ( strncasecmp( type, LDAPI_MOD_URLEXT "=",
591 sizeof(LDAPI_MOD_URLEXT "=") - 1 ) == 0 )
593 char *value = type + ( sizeof(LDAPI_MOD_URLEXT "=") - 1 );
597 switch (strlen(value)) {
599 /* skip leading '0' */
600 if ( value[ 0 ] != '0' ) return LDAP_OTHER;
604 for ( j = 0; j < 3; j++) {
607 v = value[ j ] - '0';
609 if ( v < 0 || v > 7 ) return LDAP_OTHER;
616 for ( j = 1; j < 10; j++ ) {
617 static mode_t m[] = { 0,
618 S_IRUSR, S_IWUSR, S_IXUSR,
619 S_IRGRP, S_IWGRP, S_IXGRP,
620 S_IROTH, S_IWOTH, S_IXOTH
622 static char c[] = "-rwxrwxrwx";
624 if ( value[ j ] == c[ j ] ) {
627 } else if ( value[ j ] != '-' ) {
646 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
648 /* port = 0 indicates AF_LOCAL */
649 static int slap_get_listener_addresses(
652 struct sockaddr ***sal)
654 struct sockaddr **sap;
658 *sal = ch_malloc(2 * sizeof(void *));
659 if (*sal == NULL) return -1;
662 *sap = ch_malloc(sizeof(struct sockaddr_un));
663 if (*sap == NULL) goto errexit;
667 (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) )
669 Debug( LDAP_DEBUG_ANY,
670 "daemon: domain socket path (%s) too long in URL",
675 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
676 (*sap)->sa_family = AF_LOCAL;
677 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
681 #ifdef HAVE_GETADDRINFO
682 struct addrinfo hints, *res, *sai;
686 memset( &hints, '\0', sizeof(hints) );
687 hints.ai_flags = AI_PASSIVE;
688 hints.ai_socktype = SOCK_STREAM;
689 hints.ai_family = slap_inet4or6;
690 snprintf(serv, sizeof serv, "%d", port);
692 if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
693 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo() failed: %s\n",
694 AC_GAI_STRERROR(err), 0, 0);
699 for (n=2; (sai = sai->ai_next) != NULL; n++) {
702 *sal = ch_calloc(n, sizeof(void *));
703 if (*sal == NULL) return -1;
708 for ( sai=res; sai; sai=sai->ai_next ) {
709 if( sai->ai_addr == NULL ) {
710 Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
711 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
716 switch (sai->ai_family) {
717 # ifdef LDAP_PF_INET6
719 *sap = ch_malloc(sizeof(struct sockaddr_in6));
724 *(struct sockaddr_in6 *)*sap =
725 *((struct sockaddr_in6 *)sai->ai_addr);
729 *sap = ch_malloc(sizeof(struct sockaddr_in));
734 *(struct sockaddr_in *)*sap =
735 *((struct sockaddr_in *)sai->ai_addr);
743 (*sap)->sa_family = sai->ai_family;
754 struct hostent *he = NULL;
756 if ( host == NULL ) {
757 in.s_addr = htonl(INADDR_ANY);
759 } else if ( !inet_aton( host, &in ) ) {
760 he = gethostbyname( host );
762 Debug( LDAP_DEBUG_ANY,
763 "daemon: invalid host %s", host, 0, 0);
766 for (n = 0; he->h_addr_list[n]; n++) /* empty */;
769 *sal = ch_malloc((n+1) * sizeof(void *));
770 if (*sal == NULL) return -1;
773 for ( i = 0; i<n; i++ ) {
774 sap[i] = ch_malloc(sizeof(struct sockaddr_in));
775 if (*sap == NULL) goto errexit;
777 (void)memset( (void *)sap[i], '\0', sizeof(struct sockaddr_in) );
778 sap[i]->sa_family = AF_INET;
779 ((struct sockaddr_in *)sap[i])->sin_port = htons(port);
780 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr,
781 he ? he->h_addr_list[i] : &in, sizeof(struct in_addr) );
790 slap_free_listener_addresses(*sal);
794 static int slap_open_listener(
805 int err, addrlen = 0;
806 struct sockaddr **sal, **psal;
807 int socktype = SOCK_STREAM; /* default to COTS */
809 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
814 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
816 rc = ldap_url_parse( url, &lud );
818 if( rc != LDAP_URL_SUCCESS ) {
819 Debug( LDAP_DEBUG_ANY,
820 "daemon: listen URL \"%s\" parse error=%d\n",
825 l.sl_url.bv_val = NULL;
827 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
832 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
833 Debug( LDAP_DEBUG_ANY, "daemon: TLS not supported (%s)\n",
835 ldap_free_urldesc( lud );
839 if(! lud->lud_port ) lud->lud_port = LDAP_PORT;
842 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
844 if(! lud->lud_port ) {
845 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
849 port = (unsigned short) lud->lud_port;
851 tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme);
852 if ( tmp == LDAP_PROTO_IPC ) {
854 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
855 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
857 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
861 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
863 ldap_free_urldesc( lud );
867 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
868 || strcmp(lud->lud_host, "*") == 0 )
870 err = slap_get_listener_addresses(NULL, port, &sal);
872 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
876 #ifdef LDAP_CONNECTIONLESS
877 l.sl_is_udp = ( tmp == LDAP_PROTO_UDP );
880 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
881 if ( lud->lud_exts ) {
882 err = get_url_perms( lud->lud_exts, &l.sl_perms, &crit );
884 l.sl_perms = S_IRWXU | S_IRWXO;
886 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
888 ldap_free_urldesc( lud );
889 if ( err ) return -1;
891 /* If we got more than one address returned, we need to make space
892 * for it in the slap_listeners array.
894 for ( num=0; sal[num]; num++ ) /* empty */;
897 slap_listeners = ch_realloc( slap_listeners,
898 (*listeners + 1) * sizeof(Listener *) );
902 while ( *sal != NULL ) {
904 switch( (*sal)->sa_family ) {
923 #ifdef LDAP_CONNECTIONLESS
924 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) );
938 if ( l.sl_sd >= dtblsize ) {
939 Debug( LDAP_DEBUG_ANY,
940 "daemon: listener descriptor %ld is too great %ld\n",
941 (long) l.sl_sd, (long) dtblsize, 0 );
942 tcp_close( l.sl_sd );
949 if ( (*sal)->sa_family == AF_LOCAL ) {
950 unlink( ((struct sockaddr_un *)*sal)->sun_path );
955 /* enable address reuse */
957 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
958 (char *) &tmp, sizeof(tmp) );
959 if ( rc == AC_SOCKET_ERROR ) {
960 int err = sock_errno();
961 Debug( LDAP_DEBUG_ANY, "slapd(%ld): "
962 "setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
963 (long) l.sl_sd, err, sock_errstr(err) );
968 switch( (*sal)->sa_family ) {
970 addrlen = sizeof(struct sockaddr_in);
975 /* Try to use IPv6 sockets for IPv6 only */
977 rc = setsockopt( l.sl_sd, IPPROTO_IPV6, IPV6_V6ONLY,
978 (char *) &tmp, sizeof(tmp) );
979 if ( rc == AC_SOCKET_ERROR ) {
980 int err = sock_errno();
981 Debug( LDAP_DEBUG_ANY, "slapd(%ld): "
982 "setsockopt(IPV6_V6ONLY) failed errno=%d (%s)\n",
983 (long) l.sl_sd, err, sock_errstr(err) );
986 addrlen = sizeof(struct sockaddr_in6);
995 setsockopt(l.sl_sd, 0, LOCAL_CREDS, &one, sizeof one);
998 addrlen = sizeof(struct sockaddr_un);
1003 if (bind(l.sl_sd, *sal, addrlen)) {
1005 Debug( LDAP_DEBUG_ANY,
1006 "daemon: bind(%ld) failed errno=%d (%s)\n",
1007 (long) l.sl_sd, err, sock_errstr(err) );
1008 tcp_close( l.sl_sd );
1013 switch ( (*sal)->sa_family ) {
1014 #ifdef LDAP_PF_LOCAL
1016 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
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 );
1036 ber_memalloc( sizeof("IP=255.255.255.255:65535") );
1037 snprintf( l.sl_name.bv_val, sizeof("IP=255.255.255.255:65535"),
1039 s != NULL ? s : SLAP_STRING_UNKNOWN, port );
1040 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1043 #ifdef LDAP_PF_INET6
1045 char addr[INET6_ADDRSTRLEN];
1046 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
1048 port = ntohs( ((struct sockaddr_in6 *)*sal)->sin6_port );
1049 l.sl_name.bv_len = strlen(addr) + sizeof("IP= 65535");
1050 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len );
1051 snprintf( l.sl_name.bv_val, l.sl_name.bv_len, "IP=%s %d",
1053 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1055 #endif /* LDAP_PF_INET6 */
1058 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
1059 (int) (*sal)->sa_family, 0, 0 );
1063 AC_MEMCPY(&l.sl_sa, *sal, addrlen);
1064 ber_str2bv( url, 0, 1, &l.sl_url);
1065 li = ch_malloc( sizeof( Listener ) );
1067 slap_listeners[*cur] = li;
1072 slap_free_listener_addresses(psal);
1074 if ( l.sl_url.bv_val == NULL ) {
1075 Debug( LDAP_DEBUG_TRACE,
1076 "slap_open_listener: failed on %s\n", url, 0, 0 );
1080 #ifdef LDAP_CONNECTIONLESS
1082 long id = connection_init( l.sl_sd, &l, "", "", CONN_IS_UDP,
1083 (slap_ssf_t) 0, NULL );
1086 Debug( LDAP_DEBUG_TRACE,
1087 "slap_open_listener: connectionless init failed on %s (%d)\n",
1095 Debug( LDAP_DEBUG_TRACE, "daemon: listener initialized %s\n",
1096 l.sl_url.bv_val, 0, 0 );
1100 static int sockinit(void);
1101 static int sockdestroy(void);
1103 int slapd_daemon_init( const char *urls )
1108 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
1109 urls ? urls : "<null>", 0, 0 );
1110 if( (rc = sockinit()) != 0 ) return rc;
1113 dtblsize = sysconf( _SC_OPEN_MAX );
1114 #elif HAVE_GETDTABLESIZE
1115 dtblsize = getdtablesize();
1117 dtblsize = FD_SETSIZE;
1120 /* open a pipe (or something equivalent connected to itself).
1121 * we write a byte on this fd whenever we catch a signal. The main
1122 * loop will be select'ing on this socket, and will wake up when
1123 * this byte arrives.
1125 if( (rc = lutil_pair( wake_sds )) < 0 ) {
1126 Debug( LDAP_DEBUG_ANY,
1127 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
1133 if( urls == NULL ) urls = "ldap:///";
1135 u = ldap_str2charray( urls, " " );
1137 if( u == NULL || u[0] == NULL ) {
1138 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
1143 for( i=0; u[i] != NULL; i++ ) {
1144 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
1149 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
1151 ldap_charray_free( u );
1155 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
1157 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
1159 for(n = 0, j = 0; u[n]; n++ ) {
1160 if ( slap_open_listener( u[n], &i, &j ) ) {
1161 ldap_charray_free( u );
1165 slap_listeners[j] = NULL;
1167 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
1172 if( slapd_register_slp ) {
1173 slapd_slp_init( urls );
1178 ldap_charray_free( u );
1179 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
1186 slapd_daemon_destroy(void)
1188 connections_destroy();
1189 tcp_close( wake_sds[1] );
1190 tcp_close( wake_sds[0] );
1194 if( slapd_register_slp ) {
1210 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1211 Listener *lr = slap_listeners[l];
1213 if ( lr->sl_sd != AC_SOCKET_INVALID ) {
1214 if ( remove ) slapd_remove( lr->sl_sd, 0, 0 );
1216 #ifdef LDAP_PF_LOCAL
1217 if ( lr->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1218 unlink( lr->sl_sa.sa_un_addr.sun_path );
1220 #endif /* LDAP_PF_LOCAL */
1222 slapd_close( lr->sl_sd );
1225 if ( lr->sl_url.bv_val ) {
1226 ber_memfree( lr->sl_url.bv_val );
1229 if ( lr->sl_name.bv_val ) {
1230 ber_memfree( lr->sl_name.bv_val );
1234 slap_listeners[l] = NULL;
1245 socklen_t len = sizeof(from);
1248 struct berval authid = BER_BVNULL;
1249 #ifdef SLAPD_RLOOKUPS
1250 char hbuf[NI_MAXHOST];
1253 char *dnsname = NULL;
1254 char *peeraddr = NULL;
1255 #ifdef LDAP_PF_LOCAL
1256 char peername[MAXPATHLEN + sizeof("PATH=")];
1257 #elif defined(LDAP_PF_INET6)
1258 char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
1260 char peername[sizeof("IP=255.255.255.255:65336")];
1261 #endif /* LDAP_PF_LOCAL */
1265 #ifdef LDAP_CONNECTIONLESS
1266 if ( sl->sl_is_udp ) return 1;
1269 # ifdef LDAP_PF_LOCAL
1270 /* FIXME: apparently accept doesn't fill
1271 * the sun_path sun_path member */
1272 from.sa_un_addr.sun_path[0] = '\0';
1273 # endif /* LDAP_PF_LOCAL */
1275 s = accept( sl->sl_sd, (struct sockaddr *) &from, &len );
1277 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1278 /* Resume the listener FD to allow concurrent-processing of
1279 * additional incoming connections.
1285 if ( s == AC_SOCKET_INVALID ) {
1286 int err = sock_errno();
1297 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1299 /* Stop listening until an existing session closes */
1301 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1304 Debug( LDAP_DEBUG_ANY,
1305 "daemon: accept(%ld) failed errno=%d (%s)\n",
1306 (long) sl->sl_sd, err, sock_errstr(err) );
1307 ldap_pvt_thread_yield();
1311 #ifndef HAVE_WINSOCK
1312 /* make sure descriptor number isn't too great */
1313 if ( s >= dtblsize ) {
1314 Debug( LDAP_DEBUG_ANY,
1315 "daemon: %ld beyond descriptor table size %ld\n",
1316 (long) s, (long) dtblsize, 0 );
1319 ldap_pvt_thread_yield();
1325 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1326 /* newly accepted stream should not be in any of the FD SETS */
1327 assert( SLAP_SOCK_NOT_ACTIVE( s ));
1328 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1331 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1332 #ifdef LDAP_PF_LOCAL
1333 /* for IPv4 and IPv6 sockets only */
1334 if ( from.sa_addr.sa_family != AF_LOCAL )
1335 #endif /* LDAP_PF_LOCAL */
1340 /* enable keep alives */
1342 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1343 (char *) &tmp, sizeof(tmp) );
1344 if ( rc == AC_SOCKET_ERROR ) {
1345 int err = sock_errno();
1346 Debug( LDAP_DEBUG_ANY,
1347 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1348 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1352 /* enable no delay */
1354 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1355 (char *)&tmp, sizeof(tmp) );
1356 if ( rc == AC_SOCKET_ERROR ) {
1357 int err = sock_errno();
1358 Debug( LDAP_DEBUG_ANY,
1359 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1360 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1366 Debug( LDAP_DEBUG_CONNS,
1367 "daemon: listen=%ld, new connection on %ld\n",
1368 (long) sl->sl_sd, (long) s, 0 );
1370 switch ( from.sa_addr.sa_family ) {
1371 # ifdef LDAP_PF_LOCAL
1373 /* FIXME: apparently accept doesn't fill
1374 * the sun_path sun_path member */
1375 if ( from.sa_un_addr.sun_path[0] == '\0' ) {
1376 AC_MEMCPY( from.sa_un_addr.sun_path,
1377 sl->sl_sa.sa_un_addr.sun_path,
1378 sizeof( from.sa_un_addr.sun_path ) );
1381 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1387 if( getpeereid( s, &uid, &gid ) == 0 ) {
1388 authid.bv_val = ch_malloc(
1389 STRLENOF( "gidNumber=4294967295+uidNumber=4294967295,"
1390 "cn=peercred,cn=external,cn=auth" ) + 1 );
1391 authid.bv_len = sprintf( authid.bv_val,
1392 "gidNumber=%d+uidNumber=%d,"
1393 "cn=peercred,cn=external,cn=auth",
1394 (int) gid, (int) uid );
1395 assert( authid.bv_len <=
1396 STRLENOF( "gidNumber=4294967295+uidNumber=4294967295,"
1397 "cn=peercred,cn=external,cn=auth" ) );
1402 #endif /* LDAP_PF_LOCAL */
1404 # ifdef LDAP_PF_INET6
1406 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1407 peeraddr = inet_ntoa( *((struct in_addr *)
1408 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1409 sprintf( peername, "IP=%s:%d",
1410 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1411 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1413 char addr[INET6_ADDRSTRLEN];
1415 peeraddr = (char *) inet_ntop( AF_INET6,
1416 &from.sa_in6_addr.sin6_addr,
1417 addr, sizeof addr );
1418 sprintf( peername, "IP=%s %d",
1419 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1420 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1423 # endif /* LDAP_PF_INET6 */
1426 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1427 sprintf( peername, "IP=%s:%d",
1428 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1429 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1437 if ( ( from.sa_addr.sa_family == AF_INET )
1438 #ifdef LDAP_PF_INET6
1439 || ( from.sa_addr.sa_family == AF_INET6 )
1444 #ifdef SLAPD_RLOOKUPS
1445 if ( use_reverse_lookup ) {
1447 if (ldap_pvt_get_hname( (const struct sockaddr *)&from, len, hbuf,
1448 sizeof(hbuf), &herr ) == 0) {
1449 ldap_pvt_str2lower( hbuf );
1453 #endif /* SLAPD_RLOOKUPS */
1456 if ( !hosts_ctl("slapd",
1457 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1458 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1459 SLAP_STRING_UNKNOWN ))
1462 Statslog( LDAP_DEBUG_STATS,
1463 "fd=%ld DENIED from %s (%s)\n",
1465 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1466 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1471 #endif /* HAVE_TCPD */
1474 id = connection_init(s, sl,
1475 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1478 sl->sl_is_tls ? CONN_IS_TLS : 0,
1483 authid.bv_val ? &authid : NULL );
1485 if( authid.bv_val ) ch_free(authid.bv_val);
1488 Debug( LDAP_DEBUG_ANY,
1489 "daemon: connection_init(%ld, %s, %s) failed.\n",
1490 (long) s, peername, sl->sl_name.bv_val );
1495 Statslog( LDAP_DEBUG_STATS,
1496 "conn=%ld fd=%ld ACCEPT from %s (%s)\n",
1497 id, (long) s, peername, sl->sl_name.bv_val,
1500 slapd_add( s, 1, NULL );
1504 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1506 slap_listener_thread(
1512 rc = slap_listener( (Listener*)ptr );
1514 if( rc != LDAP_SUCCESS ) {
1515 Debug( LDAP_DEBUG_ANY,
1516 "listener_thread: failed %d", rc, 0, 0 );
1523 slap_listener_activate(
1528 Debug( LDAP_DEBUG_TRACE, "slap_listener_activate(%d): %s\n",
1529 sl->sl_sd, sl->sl_busy ? "busy" : "", 0 );
1533 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1534 slap_listener_thread, (void *) sl );
1537 Debug( LDAP_DEBUG_ANY,
1538 "listener_activate(%d): submit failed (%d)\n",
1550 time_t last_idle_check = 0;
1551 struct timeval idle;
1554 #define SLAPD_IDLE_CHECK_LIMIT 4
1556 if ( global_idletimeout > 0 ) {
1557 last_idle_check = slap_get_time();
1558 /* Set the select timeout.
1559 * Don't just truncate, preserve the fractions of
1560 * seconds to prevent sleeping for zero time.
1562 idle.tv_sec = global_idletimeout / SLAPD_IDLE_CHECK_LIMIT;
1563 idle.tv_usec = global_idletimeout - \
1564 ( idle.tv_sec * SLAPD_IDLE_CHECK_LIMIT );
1565 idle.tv_usec *= 1000000 / SLAPD_IDLE_CHECK_LIMIT;
1571 slapd_add( wake_sds[0], 0, NULL );
1573 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1574 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ) continue;
1576 #ifdef LDAP_CONNECTIONLESS
1577 /* Since this is connectionless, the data port is the
1578 * listening port. The listen() and accept() calls
1581 if ( slap_listeners[l]->sl_is_udp ) {
1582 slapd_add( slap_listeners[l]->sl_sd, 1, slap_listeners[l] );
1587 if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN_BACKLOG ) == -1 ) {
1588 int err = sock_errno();
1590 #ifdef LDAP_PF_INET6
1591 /* If error is EADDRINUSE, we are trying to listen to INADDR_ANY and
1592 * we are already listening to in6addr_any, then we want to ignore
1593 * this and continue.
1595 if ( err == EADDRINUSE ) {
1597 struct sockaddr_in sa = slap_listeners[l]->sl_sa.sa_in_addr;
1598 struct sockaddr_in6 sa6;
1600 if ( sa.sin_family == AF_INET &&
1601 sa.sin_addr.s_addr == htonl(INADDR_ANY) ) {
1602 for ( i = 0 ; i < l; i++ ) {
1603 sa6 = slap_listeners[i]->sl_sa.sa_in6_addr;
1604 if ( sa6.sin6_family == AF_INET6 &&
1605 !memcmp( &sa6.sin6_addr, &in6addr_any,
1606 sizeof(struct in6_addr) ) )
1613 /* We are already listening to in6addr_any */
1614 Debug( LDAP_DEBUG_CONNS,
1615 "daemon: Attempt to listen to 0.0.0.0 failed, "
1616 "already listening on ::, assuming IPv4 included\n",
1618 slapd_close( slap_listeners[l]->sl_sd );
1619 slap_listeners[l]->sl_sd = AC_SOCKET_INVALID;
1625 Debug( LDAP_DEBUG_ANY,
1626 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
1627 slap_listeners[l]->sl_url.bv_val, err,
1632 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1633 /* make the listening socket non-blocking */
1634 if ( ber_pvt_socket_set_nonblock( slap_listeners[l]->sl_sd, 1 ) < 0 ) {
1635 Debug( LDAP_DEBUG_ANY, "slapd_daemon_task: "
1636 "set nonblocking on a listening socket failed\n",
1643 slapd_add( slap_listeners[l]->sl_sd, 0, slap_listeners[l] );
1646 #ifdef HAVE_NT_SERVICE_MANAGER
1647 if ( started_event != NULL ) }
1648 ldap_pvt_thread_cond_signal( &started_event );
1652 #ifdef SLAP_SEM_LOAD_CONTROL
1654 * initialize count and lazyness of a semaphore
1656 (void) ldap_lazy_sem_init(
1657 SLAP_MAX_WORKER_THREADS + 4 /* max workers + margin */,
1661 /* initialization complete. Here comes the loop. */
1663 while ( !slapd_shutdown ) {
1668 #if SLAP_EVENTS_ARE_INDEXED
1669 ber_socket_t nrfds, nwfds;
1671 #define SLAPD_EBADF_LIMIT 16
1678 struct timeval *tvp;
1680 struct timeval *cat;
1683 now = slap_get_time();
1685 if( ( global_idletimeout > 0 ) &&
1686 difftime( last_idle_check +
1687 global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 ) {
1688 connections_timeout_idle( now );
1689 last_idle_check = now;
1694 if( slapd_gentle_shutdown ) {
1695 ber_socket_t active;
1697 if( slapd_gentle_shutdown == 1 ) {
1698 Debug( LDAP_DEBUG_ANY, "slapd gentle shutdown\n", 0, 0, 0 );
1699 close_listeners( 1 );
1700 frontendDB->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
1701 slapd_gentle_shutdown = 2;
1704 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1705 active = slap_daemon.sd_nactives;
1706 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1716 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1718 nwriters = slap_daemon.sd_nwriters;
1721 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1722 Listener *lr = slap_listeners[l];
1724 if ( lr->sl_sd == AC_SOCKET_INVALID ) continue;
1726 if ( lr->sl_mute || lr->sl_busy ) {
1727 SLAP_SOCK_CLR_READ( lr->sl_sd );
1729 SLAP_SOCK_SET_READ( lr->sl_sd );
1733 nfds = SLAP_EVENT_MAX;
1735 if ( global_idletimeout && slap_daemon.sd_nactives ) at = 1;
1737 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1740 #if defined(HAVE_YIELDING_SELECT) || defined(NO_THREADS)
1741 && ( tv.tv_sec || tv.tv_usec )
1750 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1751 rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
1752 while ( cat && cat->tv_sec && cat->tv_sec <= now ) {
1753 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
1754 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1756 ldap_pvt_runqueue_runtask( &slapd_rq, rtask );
1757 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1758 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1759 ldap_pvt_thread_pool_submit( &connection_pool,
1760 rtask->routine, (void *) rtask );
1761 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1763 rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
1765 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1767 if ( cat && cat->tv_sec ) {
1768 time_t diff = difftime( cat->tv_sec, now );
1769 if ( diff == 0 ) diff = tdelta;
1770 if ( tvp == NULL || diff < tv.tv_sec ) {
1777 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1778 Listener *lr = slap_listeners[l];
1780 if ( lr->sl_sd == AC_SOCKET_INVALID ) {
1784 if ( lr->sl_mute ) {
1785 Debug( LDAP_DEBUG_CONNS,
1786 "daemon: select: listen=%d muted\n",
1791 if ( lr->sl_busy ) {
1792 Debug( LDAP_DEBUG_CONNS,
1793 "daemon: select: listen=%d busy\n",
1798 Debug( LDAP_DEBUG_CONNS,
1799 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
1800 lr->sl_sd, at, tvp == NULL ? "NULL" : "zero" );
1803 switch(ns = SLAP_EVENT_WAIT(tvp)) {
1804 case -1: { /* failure - try again */
1805 int err = sock_errno();
1809 /* you'd think this would be EBADF */
1810 || err == WSAENOTSOCK
1813 if (++ebadf < SLAPD_EBADF_LIMIT)
1817 if( err != EINTR ) {
1818 Debug( LDAP_DEBUG_CONNS,
1819 "daemon: select failed (%d): %s\n",
1820 err, sock_errstr(err), 0 );
1826 case 0: /* timeout - let threads run */
1828 #ifndef HAVE_YIELDING_SELECT
1829 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
1832 ldap_pvt_thread_yield();
1836 default: /* something happened - deal with it */
1837 if( slapd_shutdown ) continue;
1840 Debug( LDAP_DEBUG_CONNS,
1841 "daemon: activity on %d descriptor%s\n",
1842 ns, ns != 1 ? "s" : "", 0 );
1846 #if SLAP_EVENTS_ARE_INDEXED
1847 if ( SLAP_EVENT_IS_READ( wake_sds[0] )) {
1848 SLAP_EVENT_CLR_READ( wake_sds[0] );
1852 tcp_read( wake_sds[0], c, sizeof(c) );
1854 Debug( LDAP_DEBUG_CONNS, "daemon: waked\n", 0, 0, 0 );
1859 /* The event slot equals the descriptor number - this is
1860 * true for Unix select and poll. We treat Windows select
1861 * like this too, even though it's a kludge.
1863 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1866 if ( ns <= 0 ) break;
1867 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ) continue;
1868 #ifdef LDAP_CONNECTIONLESS
1869 if ( slap_listeners[l]->sl_is_udp ) continue;
1871 if ( !SLAP_EVENT_IS_READ( slap_listeners[l]->sl_sd )) continue;
1874 SLAP_EVENT_CLR_READ( slap_listeners[l]->sl_sd );
1875 SLAP_EVENT_CLR_WRITE( slap_listeners[l]->sl_sd );
1878 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1879 rc = slap_listener_activate(slap_listeners[l]);
1881 rc = slap_listener(slap_listeners[l]);
1885 /* bypass the following tests if no descriptors left */
1887 #ifndef HAVE_YIELDING_SELECT
1888 ldap_pvt_thread_yield();
1893 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1895 nrfds = readfds.fd_count;
1896 nwfds = writefds.fd_count;
1897 for ( i = 0; i < readfds.fd_count; i++ ) {
1898 Debug( LDAP_DEBUG_CONNS, " %d%s",
1899 readfds.fd_array[i], "r", 0 );
1901 for ( i = 0; i < writefds.fd_count; i++ ) {
1902 Debug( LDAP_DEBUG_CONNS, " %d%s",
1903 writefds.fd_array[i], "w", 0 );
1909 for ( i = 0; i < nfds; i++ ) {
1912 r = SLAP_EVENT_IS_READ( i );
1913 /* writefds was not initialized if nwriters was zero */
1914 w = nwriters ? SLAP_EVENT_IS_WRITE( i ) : 0;
1916 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1917 r ? "r" : "", w ? "w" : "" );
1927 if ( ns <= 0 ) break;
1930 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1933 /* loop through the writers */
1934 for ( i = 0; nwfds > 0; i++ ) {
1937 wd = writefds.fd_array[i];
1939 if( ! SLAP_EVENT_IS_WRITE( i ) ) continue;
1943 SLAP_EVENT_CLR_WRITE( wd );
1946 Debug( LDAP_DEBUG_CONNS,
1947 "daemon: write active on %d\n",
1950 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1951 connection_write_activate( wd );
1954 * NOTE: it is possible that the connection was closed
1955 * and that the stream is now inactive.
1956 * connection_write() must validitate the stream is still
1959 if ( connection_write( wd ) < 0 ) {
1960 if ( SLAP_EVENT_IS_READ( wd )) {
1961 SLAP_EVENT_CLR_READ( (unsigned) wd );
1969 for ( i = 0; nrfds > 0; i++ ) {
1972 rd = readfds.fd_array[i];
1974 if( ! SLAP_EVENT_IS_READ( i ) ) continue;
1977 SLAP_EVENT_CLR_READ( rd );
1980 Debug ( LDAP_DEBUG_CONNS,
1981 "daemon: read activity on %d\n", rd, 0, 0 );
1983 * NOTE: it is possible that the connection was closed
1984 * and that the stream is now inactive.
1985 * connection_read() must valid the stream is still
1989 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1990 connection_read_activate( rd );
1992 if ( connection_read( rd ) < 0 ) {
1997 #else /* !SLAP_EVENTS_ARE_INDEXED */
1999 /* The events are returned in an arbitrary list. This is true
2000 * for /dev/poll, epoll and kqueue. In order to prioritize things
2001 * so that we can handle wake_sds first, listeners second, and then
2002 * all other connections last (as we do for select), we would need
2003 * to use multiple event handles and cascade them.
2005 * That seems like a bit of hassle. So the wake_sds check has been
2006 * skipped. For epoll and kqueue we can associate arbitrary data with
2007 * an event, so we could use pointers to the listener structure
2008 * instead of just the file descriptor. For /dev/poll we have to
2009 * search the listeners array for a matching descriptor.
2011 /* if waking is set and we woke up, we'll read whatever
2016 tcp_read( wake_sds[0], c, sizeof(c) );
2023 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
2025 for (i=0; i<ns; i++) {
2028 /* Don't log listener events */
2029 if ( SLAP_EVENT_IS_LISTENER(i)
2030 #ifdef LDAP_CONNECTIONLESS
2031 && !((SLAP_EVENT_LISTENER(i))->sl_is_udp)
2038 /* Don't log internal wake events */
2039 if ( SLAP_EVENT_FD( i ) == wake_sds[0] ) continue;
2041 r = SLAP_EVENT_IS_READ( i );
2042 w = SLAP_EVENT_IS_WRITE( i );
2044 Debug( LDAP_DEBUG_CONNS, " %d%s%s", SLAP_EVENT_FD(i),
2045 r ? "r" : "", w ? "w" : "" );
2050 for (i=0; i<ns; i++) {
2053 if ( SLAP_EVENT_IS_LISTENER(i) ) {
2054 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
2055 rc = slap_listener_activate( SLAP_EVENT_LISTENER( i ));
2057 rc = slap_listener( SLAP_EVENT_LISTENER( i ));
2061 /* If we found a regular listener, rc is now zero, and we
2062 * can skip the data portion. But if it was a UDP listener
2063 * then rc is still 1, and we want to handle the data.
2066 fd = SLAP_EVENT_FD( i );
2068 /* Ignore wake events, they were handled above */
2069 if ( fd == wake_sds[0] ) continue;
2071 if( SLAP_EVENT_IS_WRITE( i ) ) {
2072 Debug( LDAP_DEBUG_CONNS,
2073 "daemon: write active on %d\n",
2076 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
2077 connection_write_activate( fd );
2080 * NOTE: it is possible that the connection was closed
2081 * and that the stream is now inactive.
2082 * connection_write() must valid the stream is still
2085 if ( connection_write( fd ) < 0 ) {
2091 if( SLAP_EVENT_IS_READ( i ) ) {
2092 Debug( LDAP_DEBUG_CONNS,
2093 "daemon: read active on %d\n",
2096 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
2097 connection_read_activate( fd );
2100 * NOTE: it is possible that the connection was closed
2101 * and that the stream is now inactive.
2102 * connection_read() must valid the stream is still
2105 if ( connection_read( fd ) < 0 ) slapd_close( fd );
2110 #endif /* SLAP_EVENTS_ARE_INDEXED */
2112 #ifndef HAVE_YIELDING_SELECT
2113 ldap_pvt_thread_yield();
2117 if( slapd_shutdown == 1 ) {
2118 Debug( LDAP_DEBUG_ANY,
2119 "daemon: shutdown requested and initiated.\n",
2122 } else if ( slapd_shutdown == 2 ) {
2123 #ifdef HAVE_NT_SERVICE_MANAGER
2124 Debug( LDAP_DEBUG_ANY,
2125 "daemon: shutdown initiated by Service Manager.\n",
2127 #else /* !HAVE_NT_SERVICE_MANAGER */
2128 Debug( LDAP_DEBUG_ANY,
2129 "daemon: abnormal condition, shutdown initiated.\n",
2131 #endif /* !HAVE_NT_SERVICE_MANAGER */
2133 Debug( LDAP_DEBUG_ANY,
2134 "daemon: no active streams, shutdown initiated.\n",
2138 if( slapd_gentle_shutdown != 2 ) close_listeners ( 0 );
2140 if( !slapd_gentle_shutdown ) {
2141 slapd_abrupt_shutdown = 1;
2142 connections_shutdown();
2145 Debug( LDAP_DEBUG_ANY,
2146 "slapd shutdown: waiting for %d threads to terminate\n",
2147 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
2148 ldap_pvt_thread_pool_destroy(&connection_pool, 1);
2150 free ( slap_listeners );
2151 slap_listeners = NULL;
2157 int slapd_daemon( void )
2163 #define SLAPD_LISTENER_THREAD 1
2164 #if defined( SLAPD_LISTENER_THREAD )
2166 ldap_pvt_thread_t listener_tid;
2168 /* listener as a separate THREAD */
2169 rc = ldap_pvt_thread_create( &listener_tid,
2170 0, slapd_daemon_task, NULL );
2173 Debug( LDAP_DEBUG_ANY,
2174 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
2178 /* wait for the listener thread to complete */
2179 ldap_pvt_thread_join( listener_tid, (void *) NULL );
2182 /* experimental code */
2183 slapd_daemon_task( NULL );
2190 static int sockinit(void)
2192 #if defined( HAVE_WINSOCK2 )
2193 WORD wVersionRequested;
2197 wVersionRequested = MAKEWORD( 2, 0 );
2199 err = WSAStartup( wVersionRequested, &wsaData );
2201 /* Tell the user that we couldn't find a usable */
2206 /* Confirm that the WinSock DLL supports 2.0.*/
2207 /* Note that if the DLL supports versions greater */
2208 /* than 2.0 in addition to 2.0, it will still return */
2209 /* 2.0 in wVersion since that is the version we */
2212 if ( LOBYTE( wsaData.wVersion ) != 2 ||
2213 HIBYTE( wsaData.wVersion ) != 0 )
2215 /* Tell the user that we couldn't find a usable */
2221 /* The WinSock DLL is acceptable. Proceed. */
2222 #elif defined( HAVE_WINSOCK )
2224 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) return -1;
2230 static int sockdestroy(void)
2232 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
2239 slap_sig_shutdown( int sig )
2242 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
2246 * If the NT Service Manager is controlling the server, we don't
2247 * want SIGBREAK to kill the server. For some strange reason,
2248 * SIGBREAK is generated when a user logs out.
2251 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
2252 if (is_NT_Service && sig == SIGBREAK) {
2257 if (sig == SIGHUP && global_gentlehup && slapd_gentle_shutdown == 0) {
2258 slapd_gentle_shutdown = 1;
2267 /* reinstall self */
2268 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
2272 slap_sig_wake( int sig )
2276 /* reinstall self */
2277 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
2281 void slapd_add_internal(ber_socket_t s, int isactive) {
2282 slapd_add(s, isactive, NULL);
2285 Listener ** slapd_get_listeners(void) {
2286 return slap_listeners;
2289 void slap_wake_listener() {