2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2006 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>
45 #elif defined(SLAP_X_DEVPOLL) && defined(HAVE_SYS_DEVPOLL_H) && defined(HAVE_DEVPOLL)
46 # include <sys/types.h>
47 # include <sys/stat.h>
49 # include <sys/devpoll.h>
50 #endif /* ! epoll && ! /dev/poll */
54 int allow_severity = LOG_INFO;
55 int deny_severity = LOG_NOTICE;
57 # define SLAP_STRING_UNKNOWN STRING_UNKNOWN
58 #else /* ! TCP Wrappers */
59 # define SLAP_STRING_UNKNOWN "unknown"
60 #endif /* ! TCP Wrappers */
63 # include <sys/stat.h>
64 /* this should go in <ldap.h> as soon as it is accepted */
65 # define LDAPI_MOD_URLEXT "x-mod"
66 #endif /* LDAP_PF_LOCAL */
69 int slap_inet4or6 = AF_UNSPEC;
71 int slap_inet4or6 = AF_INET;
76 ber_socket_t dtblsize;
77 slap_ssf_t local_ssf = LDAP_PVT_SASL_LOCAL_SSF;
78 struct runqueue_s slapd_rq;
80 Listener **slap_listeners = NULL;
82 #ifndef SLAPD_LISTEN_BACKLOG
83 #define SLAPD_LISTEN_BACKLOG 1024
84 #endif /* ! SLAPD_LISTEN_BACKLOG */
86 static ber_socket_t wake_sds[2];
89 static volatile int waking;
91 #define WAKE_LISTENER(w) do { \
92 if ((w) && ++waking < 5) { \
93 tcp_write( wake_sds[1], "0", 1 ); \
96 #else /* ! NO_THREADS */
97 #define WAKE_LISTENER(w) do { \
99 tcp_write( wake_sds[1], "0", 1 ); \
102 #endif /* ! NO_THREADS */
104 volatile sig_atomic_t slapd_shutdown = 0;
105 volatile sig_atomic_t slapd_gentle_shutdown = 0;
106 volatile sig_atomic_t slapd_abrupt_shutdown = 0;
108 static struct slap_daemon {
109 ldap_pvt_thread_mutex_t sd_mutex;
111 ldap_pvt_thread_mutex_t sd_tcpd_mutex;
112 #endif /* TCP Wrappers */
114 ber_socket_t sd_nactives;
117 #if defined(HAVE_EPOLL)
118 struct epoll_event *sd_epolls;
122 #elif defined(SLAP_X_DEVPOLL) && defined(HAVE_DEVPOLL)
124 struct pollfd *sd_pollfd;
129 #else /* ! epoll && ! /dev/poll */
131 /* In winsock, accept() returns values higher than dtblsize
132 so don't bother with this optimization */
134 #endif /* ! HAVE_WINSOCK */
138 #endif /* ! epoll && ! /dev/poll */
142 * NOTE: naming convention for macros:
144 * - SLAP_SOCK_* and SLAP_EVENT_* for public interface that deals
145 * with file descriptors and events respectively
147 * - SLAP_<type>_* for private interface; type by now is one of
148 * EPOLL, DEVPOLL, SELECT
150 * private interface should not be used in the code.
152 #if defined(HAVE_EPOLL)
153 /***************************************
154 * Use epoll infrastructure - epoll(4) *
155 ***************************************/
156 # define SLAP_EVENT_FNAME "epoll"
157 # define SLAP_EVENTS_ARE_INDEXED 0
158 # define SLAP_EPOLL_SOCK_IX(s) (slap_daemon.sd_index[(s)])
159 # define SLAP_EPOLL_SOCK_EP(s) (slap_daemon.sd_epolls[SLAP_EPOLL_SOCK_IX(s)])
160 # define SLAP_EPOLL_SOCK_EV(s) (SLAP_EPOLL_SOCK_EP(s).events)
161 # define SLAP_SOCK_IS_ACTIVE(s) (SLAP_EPOLL_SOCK_IX(s) != -1)
162 # define SLAP_SOCK_NOT_ACTIVE(s) (SLAP_EPOLL_SOCK_IX(s) == -1)
163 # define SLAP_EPOLL_SOCK_IS_SET(s, mode) (SLAP_EPOLL_SOCK_EV(s) & (mode))
165 # define SLAP_SOCK_IS_READ(s) SLAP_EPOLL_SOCK_IS_SET((s), EPOLLIN)
166 # define SLAP_SOCK_IS_WRITE(s) SLAP_EPOLL_SOCK_IS_SET((s), EPOLLOUT)
168 # define SLAP_EPOLL_SOCK_SET(s, mode) do { \
169 if ( (SLAP_EPOLL_SOCK_EV(s) & (mode)) != (mode) ) { \
170 SLAP_EPOLL_SOCK_EV(s) |= (mode); \
171 epoll_ctl( slap_daemon.sd_epfd, EPOLL_CTL_MOD, (s), \
172 &SLAP_EPOLL_SOCK_EP(s) ); \
176 # define SLAP_EPOLL_SOCK_CLR(s, mode) do { \
177 if ( (SLAP_EPOLL_SOCK_EV(s) & (mode)) ) { \
178 SLAP_EPOLL_SOCK_EV(s) &= ~(mode); \
179 epoll_ctl( slap_daemon.sd_epfd, EPOLL_CTL_MOD, s, \
180 &SLAP_EPOLL_SOCK_EP(s) ); \
184 # define SLAP_SOCK_SET_READ(s) SLAP_EPOLL_SOCK_SET(s, EPOLLIN)
185 # define SLAP_SOCK_SET_WRITE(s) SLAP_EPOLL_SOCK_SET(s, EPOLLOUT)
187 # define SLAP_SOCK_CLR_READ(s) SLAP_EPOLL_SOCK_CLR((s), EPOLLIN)
188 # define SLAP_SOCK_CLR_WRITE(s) SLAP_EPOLL_SOCK_CLR((s), EPOLLOUT)
190 # ifdef SLAP_LIGHTWEIGHT_DISPATCHER
191 # define SLAP_SOCK_SET_SUSPEND(s) \
192 ( slap_daemon.sd_suspend[SLAP_EPOLL_SOCK_IX(s)] = 1 )
193 # define SLAP_SOCK_CLR_SUSPEND(s) \
194 ( slap_daemon.sd_suspend[SLAP_EPOLL_SOCK_IX(s)] = 0 )
195 # define SLAP_SOCK_IS_SUSPEND(s) \
196 ( slap_daemon.sd_suspend[SLAP_EPOLL_SOCK_IX(s)] == 1 )
197 # endif /* SLAP_LIGHTWEIGHT_DISPATCHER */
199 # define SLAP_EPOLL_EVENT_CLR(i, mode) (revents[(i)].events &= ~(mode))
201 # define SLAP_EVENT_MAX slap_daemon.sd_nfds
203 /* If a Listener address is provided, store that as the epoll data.
204 * Otherwise, store the address of this socket's slot in the
205 * index array. If we can't do this add, the system is out of
206 * resources and we need to shutdown.
208 # define SLAP_SOCK_ADD(s, l) do { \
210 SLAP_EPOLL_SOCK_IX((s)) = slap_daemon.sd_nfds; \
211 SLAP_EPOLL_SOCK_EP((s)).data.ptr = (l) ? (l) : (void *)(&SLAP_EPOLL_SOCK_IX(s)); \
212 SLAP_EPOLL_SOCK_EV((s)) = EPOLLIN; \
213 rc = epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_ADD, \
214 (s), &SLAP_EPOLL_SOCK_EP((s))); \
216 slap_daemon.sd_nfds++; \
218 Debug( LDAP_DEBUG_ANY, \
219 "daemon: epoll_ctl(ADD,fd=%d) failed, errno=%d, shutting down\n", \
221 slapd_shutdown = 2; \
225 # define SLAP_EPOLL_EV_LISTENER(ptr) \
226 (((int *)(ptr) >= slap_daemon.sd_index && \
227 (int *)(ptr) <= &slap_daemon.sd_index[dtblsize]) ? 0 : 1 )
229 # define SLAP_EPOLL_EV_PTRFD(ptr) (SLAP_EPOLL_EV_LISTENER(ptr) ? \
230 ((Listener *)ptr)->sl_sd : (int *)(ptr) - slap_daemon.sd_index)
232 # define SLAP_SOCK_DEL(s) do { \
233 int fd, rc, index = SLAP_EPOLL_SOCK_IX((s)); \
234 if ( index < 0 ) break; \
235 rc = epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_DEL, \
236 (s), &SLAP_EPOLL_SOCK_EP((s))); \
237 slap_daemon.sd_epolls[index] = \
238 slap_daemon.sd_epolls[slap_daemon.sd_nfds-1]; \
239 fd = SLAP_EPOLL_EV_PTRFD(slap_daemon.sd_epolls[index].data.ptr); \
240 slap_daemon.sd_index[fd] = index; \
241 slap_daemon.sd_index[(s)] = -1; \
242 slap_daemon.sd_nfds--; \
245 # define SLAP_EVENT_CLR_READ(i) SLAP_EPOLL_EVENT_CLR((i), EPOLLIN)
246 # define SLAP_EVENT_CLR_WRITE(i) SLAP_EPOLL_EVENT_CLR((i), EPOLLOUT)
248 # define SLAP_EPOLL_EVENT_CHK(i, mode) (revents[(i)].events & mode)
250 # define SLAP_EVENT_IS_READ(i) SLAP_EPOLL_EVENT_CHK((i), EPOLLIN)
251 # define SLAP_EVENT_IS_WRITE(i) SLAP_EPOLL_EVENT_CHK((i), EPOLLOUT)
252 # define SLAP_EVENT_IS_LISTENER(i) SLAP_EPOLL_EV_LISTENER(revents[(i)].data.ptr)
253 # define SLAP_EVENT_LISTENER(i) ((Listener *)(revents[(i)].data.ptr))
255 # define SLAP_EVENT_FD(i) SLAP_EPOLL_EV_PTRFD(revents[(i)].data.ptr)
257 # define SLAP_SOCK_INIT do { \
258 slap_daemon.sd_epolls = ch_calloc(1, \
259 ( sizeof(struct epoll_event) * 2 \
260 + sizeof(int) ) * dtblsize * 2); \
261 slap_daemon.sd_index = (int *)&slap_daemon.sd_epolls[ 2 * dtblsize ]; \
262 slap_daemon.sd_epfd = epoll_create( dtblsize ); \
263 for ( i = 0; i < dtblsize; i++ ) slap_daemon.sd_index[i] = -1; \
266 # define SLAP_SOCK_DESTROY do { \
267 if ( slap_daemon.sd_epolls != NULL ) { \
268 ch_free( slap_daemon.sd_epolls ); \
269 slap_daemon.sd_epolls = NULL; \
270 slap_daemon.sd_index = NULL; \
271 close( slap_daemon.sd_epfd ); \
275 # define SLAP_EVENT_DECL struct epoll_event *revents
277 # define SLAP_EVENT_INIT do { \
278 revents = slap_daemon.sd_epolls + dtblsize; \
281 # define SLAP_EVENT_WAIT(tvp, nsp) do { \
282 *(nsp) = epoll_wait( slap_daemon.sd_epfd, revents, \
283 dtblsize, (tvp) ? (tvp)->tv_sec * 1000 : -1 ); \
286 #elif defined(SLAP_X_DEVPOLL) && defined(HAVE_DEVPOLL)
288 /*************************************************************
289 * Use Solaris' (>= 2.7) /dev/poll infrastructure - poll(7d) *
290 *************************************************************/
291 # define SLAP_EVENT_FNAME "/dev/poll"
292 # define SLAP_EVENTS_ARE_INDEXED 0
294 * - sd_index is used much like with epoll()
295 * - sd_l is maintained as an array containing the address
296 * of the listener; the index is the fd itself
297 * - sd_pollfd is used to keep track of what data has been
298 * registered in /dev/poll
300 # define SLAP_DEVPOLL_SOCK_IX(s) (slap_daemon.sd_index[(s)])
301 # define SLAP_DEVPOLL_SOCK_LX(s) (slap_daemon.sd_l[(s)])
302 # define SLAP_DEVPOLL_SOCK_EP(s) (slap_daemon.sd_pollfd[SLAP_DEVPOLL_SOCK_IX((s))])
303 # define SLAP_DEVPOLL_SOCK_FD(s) (SLAP_DEVPOLL_SOCK_EP((s)).fd)
304 # define SLAP_DEVPOLL_SOCK_EV(s) (SLAP_DEVPOLL_SOCK_EP((s)).events)
305 # define SLAP_SOCK_IS_ACTIVE(s) (SLAP_DEVPOLL_SOCK_IX((s)) != -1)
306 # define SLAP_SOCK_NOT_ACTIVE(s) (SLAP_DEVPOLL_SOCK_IX((s)) == -1)
307 # define SLAP_SOCK_IS_SET(s, mode) (SLAP_DEVPOLL_SOCK_EV((s)) & (mode))
309 # define SLAP_SOCK_IS_READ(s) SLAP_SOCK_IS_SET((s), POLLIN)
310 # define SLAP_SOCK_IS_WRITE(s) SLAP_SOCK_IS_SET((s), POLLOUT)
312 /* as far as I understand, any time we need to communicate with the kernel
313 * about the number and/or properties of a file descriptor we need it to
314 * wait for, we have to rewrite the whole set */
315 # define SLAP_DEVPOLL_WRITE_POLLFD(s, pfd, n, what, shdn) do { \
317 size_t size = (n) * sizeof( struct pollfd ); \
318 /* FIXME: use pwrite? */ \
319 rc = write( slap_daemon.sd_dpfd, (pfd), size ); \
320 if ( rc != size ) { \
321 Debug( LDAP_DEBUG_ANY, "daemon: " SLAP_EVENT_FNAME ": " \
322 "%s fd=%d failed errno=%d\n", \
323 (what), (s), errno ); \
325 slapd_shutdown = 2; \
330 # define SLAP_DEVPOLL_SOCK_SET(s, mode) do { \
331 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_SET_%s(%d)=%d\n", \
332 (mode) == POLLIN ? "READ" : "WRITE", (s), \
333 ( (SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) != (mode) ) ); \
334 if ( (SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) != (mode) ) { \
336 SLAP_DEVPOLL_SOCK_EV((s)) |= (mode); \
337 pfd.fd = SLAP_DEVPOLL_SOCK_FD((s)); \
338 pfd.events = /* (mode) */ SLAP_DEVPOLL_SOCK_EV((s)); \
339 SLAP_DEVPOLL_WRITE_POLLFD((s), &pfd, 1, "SET", 0); \
343 # define SLAP_DEVPOLL_SOCK_CLR(s, mode) do { \
344 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_CLR_%s(%d)=%d\n", \
345 (mode) == POLLIN ? "READ" : "WRITE", (s), \
346 ( (SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) == (mode) ) ); \
347 if ((SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) == (mode) ) { \
348 struct pollfd pfd[2]; \
349 SLAP_DEVPOLL_SOCK_EV((s)) &= ~(mode); \
350 pfd[0].fd = SLAP_DEVPOLL_SOCK_FD((s)); \
351 pfd[0].events = POLLREMOVE; \
352 pfd[1] = SLAP_DEVPOLL_SOCK_EP((s)); \
353 SLAP_DEVPOLL_WRITE_POLLFD((s), &pfd[0], 2, "CLR", 0); \
357 # define SLAP_SOCK_SET_READ(s) SLAP_DEVPOLL_SOCK_SET(s, POLLIN)
358 # define SLAP_SOCK_SET_WRITE(s) SLAP_DEVPOLL_SOCK_SET(s, POLLOUT)
360 # define SLAP_SOCK_CLR_READ(s) SLAP_DEVPOLL_SOCK_CLR((s), POLLIN)
361 # define SLAP_SOCK_CLR_WRITE(s) SLAP_DEVPOLL_SOCK_CLR((s), POLLOUT)
363 # ifdef SLAP_LIGHTWEIGHT_DISPATCHER
364 # define SLAP_SOCK_SET_SUSPEND(s) \
365 ( slap_daemon.sd_suspend[SLAP_DEVPOLL_SOCK_IX((s))] = 1 )
366 # define SLAP_SOCK_CLR_SUSPEND(s) \
367 ( slap_daemon.sd_suspend[SLAP_DEVPOLL_SOCK_IX((s))] = 0 )
368 # define SLAP_SOCK_IS_SUSPEND(s) \
369 ( slap_daemon.sd_suspend[SLAP_DEVPOLL_SOCK_IX((s))] == 1 )
370 # endif /* SLAP_LIGHTWEIGHT_DISPATCHER */
372 # define SLAP_DEVPOLL_EVENT_CLR(i, mode) (revents[(i)].events &= ~(mode))
374 # define SLAP_EVENT_MAX slap_daemon.sd_nfds
376 /* If a Listener address is provided, store that in the sd_l array.
377 * If we can't do this add, the system is out of resources and we
380 # define SLAP_SOCK_ADD(s, l) do { \
381 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_ADD(%d, %p)\n", (s), (l), 0 ); \
382 SLAP_DEVPOLL_SOCK_IX((s)) = slap_daemon.sd_nfds; \
383 SLAP_DEVPOLL_SOCK_LX((s)) = (l); \
384 SLAP_DEVPOLL_SOCK_FD((s)) = (s); \
385 SLAP_DEVPOLL_SOCK_EV((s)) = POLLIN; \
386 SLAP_DEVPOLL_WRITE_POLLFD((s), &SLAP_DEVPOLL_SOCK_EP((s)), 1, "ADD", 1); \
387 slap_daemon.sd_nfds++; \
390 # define SLAP_DEVPOLL_EV_LISTENER(ptr) ((ptr) != NULL)
392 # define SLAP_SOCK_DEL(s) do { \
393 int fd, index = SLAP_DEVPOLL_SOCK_IX((s)); \
394 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_DEL(%d)\n", (s), 0, 0 ); \
395 if ( index < 0 ) break; \
396 if ( index < slap_daemon.sd_nfds - 1 ) { \
397 struct pollfd pfd = slap_daemon.sd_pollfd[index]; \
398 fd = slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1].fd; \
399 slap_daemon.sd_pollfd[index] = slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1]; \
400 slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1] = pfd; \
401 slap_daemon.sd_index[fd] = index; \
403 slap_daemon.sd_index[(s)] = -1; \
404 slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1].events = POLLREMOVE; \
405 SLAP_DEVPOLL_WRITE_POLLFD((s), &slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1], 1, "DEL", 0); \
406 slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1].events = 0; \
407 slap_daemon.sd_nfds--; \
410 # define SLAP_EVENT_CLR_READ(i) SLAP_DEVPOLL_EVENT_CLR((i), POLLIN)
411 # define SLAP_EVENT_CLR_WRITE(i) SLAP_DEVPOLL_EVENT_CLR((i), POLLOUT)
413 # define SLAP_DEVPOLL_EVENT_CHK(i, mode) (revents[(i)].events & (mode))
415 # define SLAP_EVENT_FD(i) (revents[(i)].fd)
417 # define SLAP_EVENT_IS_READ(i) SLAP_DEVPOLL_EVENT_CHK((i), POLLIN)
418 # define SLAP_EVENT_IS_WRITE(i) SLAP_DEVPOLL_EVENT_CHK((i), POLLOUT)
419 # define SLAP_EVENT_IS_LISTENER(i) SLAP_DEVPOLL_EV_LISTENER(SLAP_DEVPOLL_SOCK_LX(SLAP_EVENT_FD((i))))
420 # define SLAP_EVENT_LISTENER(i) SLAP_DEVPOLL_SOCK_LX(SLAP_EVENT_FD((i)))
422 # define SLAP_SOCK_INIT do { \
423 slap_daemon.sd_pollfd = ch_calloc( 1, \
424 ( sizeof(struct pollfd) * 2 \
426 + sizeof( Listener * ) ) * dtblsize ); \
427 slap_daemon.sd_index = (int *)&slap_daemon.sd_pollfd[ 2 * dtblsize ]; \
428 slap_daemon.sd_l = (Listener **)&slap_daemon.sd_index[ dtblsize ]; \
429 slap_daemon.sd_dpfd = open( SLAP_EVENT_FNAME, O_RDWR ); \
430 if ( slap_daemon.sd_dpfd == -1 ) { \
431 Debug( LDAP_DEBUG_ANY, "daemon: " SLAP_EVENT_FNAME ": " \
432 "open(\"" SLAP_EVENT_FNAME "\") failed errno=%d\n", \
437 for ( i = 0; i < dtblsize; i++ ) { \
438 slap_daemon.sd_pollfd[i].fd = -1; \
439 slap_daemon.sd_index[i] = -1; \
443 # define SLAP_SOCK_DESTROY do { \
444 if ( slap_daemon.sd_pollfd != NULL ) { \
445 ch_free( slap_daemon.sd_pollfd ); \
446 slap_daemon.sd_pollfd = NULL; \
447 slap_daemon.sd_index = NULL; \
448 slap_daemon.sd_l = NULL; \
449 close( slap_daemon.sd_dpfd ); \
453 # define SLAP_EVENT_DECL struct pollfd *revents
455 # define SLAP_EVENT_INIT do { \
456 revents = &slap_daemon.sd_pollfd[ dtblsize ]; \
459 # define SLAP_EVENT_WAIT(tvp, nsp) do { \
460 struct dvpoll sd_dvpoll; \
461 sd_dvpoll.dp_timeout = (tvp) ? (tvp)->tv_sec * 1000 : -1; \
462 sd_dvpoll.dp_nfds = dtblsize; \
463 sd_dvpoll.dp_fds = revents; \
464 *(nsp) = ioctl( slap_daemon.sd_dpfd, DP_POLL, &sd_dvpoll ); \
467 #else /* ! epoll && ! /dev/poll */
469 /**************************************
470 * Use select system call - select(2) *
471 **************************************/
472 # define SLAP_EVENT_FNAME "select"
475 # define SLAP_EVENTS_ARE_INDEXED 1
476 # define SLAP_EVENT_DECL fd_set readfds, writefds
478 # define SLAP_EVENT_INIT do { \
479 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) ); \
481 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) ); \
483 FD_ZERO( &writefds ); \
488 # define SLAP_SELECT_CHK_SETSIZE do { \
489 if (dtblsize > FD_SETSIZE) dtblsize = FD_SETSIZE; \
491 # else /* ! FD_SETSIZE */
492 # define SLAP_SELECT_CHK_SETSIZE do { ; } while (0)
493 # endif /* ! FD_SETSIZE */
495 # define SLAP_SOCK_INIT do { \
496 SLAP_SELECT_CHK_SETSIZE; \
497 FD_ZERO(&slap_daemon.sd_readers); \
498 FD_ZERO(&slap_daemon.sd_writers); \
501 # define SLAP_SOCK_DESTROY
503 # define SLAP_SOCK_IS_ACTIVE(fd) FD_ISSET((fd), &slap_daemon.sd_actives)
504 # define SLAP_SOCK_IS_READ(fd) FD_ISSET((fd), &slap_daemon.sd_readers)
505 # define SLAP_SOCK_IS_WRITE(fd) FD_ISSET((fd), &slap_daemon.sd_writers)
507 # define SLAP_SOCK_NOT_ACTIVE(fd) (!SLAP_SOCK_IS_ACTIVE(fd) && \
508 !SLAP_SOCK_IS_READ(fd) && !SLAP_SOCK_IS_WRITE(fd))
511 # define SLAP_SOCK_SET_READ(fd) do { \
512 if (!SLAP_SOCK_IS_READ(fd)) { FD_SET((fd), &slap_daemon.sd_readers); } \
514 # define SLAP_SOCK_SET_WRITE(fd) do { \
515 if (!SLAP_SOCK_IS_WRITE(fd)) { FD_SET((fd), &slap_daemon.sd_writers); } \
518 # define SLAP_SELECT_ADDTEST(s)
519 # define SLAP_EVENT_MAX dtblsize
520 # else /* ! HAVE_WINSOCK */
521 # define SLAP_SOCK_SET_READ(fd) FD_SET((fd), &slap_daemon.sd_readers)
522 # define SLAP_SOCK_SET_WRITE(fd) FD_SET((fd), &slap_daemon.sd_writers)
524 # define SLAP_EVENT_MAX slap_daemon.sd_nfds
525 # define SLAP_SELECT_ADDTEST(s) do { \
526 if ((s) >= slap_daemon.sd_nfds) slap_daemon.sd_nfds = (s)+1; \
528 # endif /* ! HAVE_WINSOCK */
530 # define SLAP_SOCK_CLR_READ(fd) FD_CLR((fd), &slap_daemon.sd_readers)
531 # define SLAP_SOCK_CLR_WRITE(fd) FD_CLR((fd), &slap_daemon.sd_writers)
533 # define SLAP_SOCK_ADD(s, l) do { \
534 SLAP_SELECT_ADDTEST((s)); \
535 FD_SET((s), &slap_daemon.sd_actives); \
536 FD_SET((s), &slap_daemon.sd_readers); \
539 # define SLAP_SOCK_DEL(s) do { \
540 FD_CLR((s), &slap_daemon.sd_actives); \
541 FD_CLR((s), &slap_daemon.sd_readers); \
542 FD_CLR((s), &slap_daemon.sd_writers); \
545 # define SLAP_EVENT_IS_READ(fd) FD_ISSET((fd), &readfds)
546 # define SLAP_EVENT_IS_WRITE(fd) FD_ISSET((fd), &writefds)
548 # define SLAP_EVENT_CLR_READ(fd) FD_CLR((fd), &readfds)
549 # define SLAP_EVENT_CLR_WRITE(fd) FD_CLR((fd), &writefds)
551 # define SLAP_EVENT_WAIT(tvp, nsp) do { \
552 *(nsp) = select( SLAP_EVENT_MAX, &readfds, \
553 nwriters > 0 ? &writefds : NULL, NULL, (tvp) ); \
555 #endif /* ! epoll && ! /dev/poll */
559 * SLP related functions
563 #define LDAP_SRVTYPE_PREFIX "service:ldap://"
564 #define LDAPS_SRVTYPE_PREFIX "service:ldaps://"
565 static char** slapd_srvurls = NULL;
566 static SLPHandle slapd_hslp = 0;
567 int slapd_register_slp = 0;
568 const char *slapd_slp_attrs = NULL;
570 static SLPError slapd_slp_cookie;
573 slapd_slp_init( const char* urls )
578 slapd_srvurls = ldap_str2charray( urls, " " );
580 if ( slapd_srvurls == NULL ) return;
582 /* find and expand INADDR_ANY URLs */
583 for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
584 if ( strcmp( slapd_srvurls[i], "ldap:///" ) == 0 ) {
585 char *host = ldap_pvt_get_fqdn( NULL );
586 if ( host != NULL ) {
587 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
589 sizeof( LDAP_SRVTYPE_PREFIX ) );
590 strcpy( lutil_strcopy(slapd_srvurls[i],
591 LDAP_SRVTYPE_PREFIX ), host );
596 } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0 ) {
597 char *host = ldap_pvt_get_fqdn( NULL );
598 if ( host != NULL ) {
599 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
601 sizeof( LDAPS_SRVTYPE_PREFIX ) );
602 strcpy( lutil_strcopy(slapd_srvurls[i],
603 LDAPS_SRVTYPE_PREFIX ), host );
610 /* open the SLP handle */
611 err = SLPOpen( "en", 0, &slapd_hslp );
613 if ( err != SLP_OK ) {
614 Debug( LDAP_DEBUG_CONNS, "daemon: SLPOpen() failed with %ld\n",
620 slapd_slp_deinit( void )
622 if ( slapd_srvurls == NULL ) return;
624 ldap_charray_free( slapd_srvurls );
625 slapd_srvurls = NULL;
627 /* close the SLP handle */
628 SLPClose( slapd_hslp );
637 /* return the error code in the cookie */
638 *(SLPError*)cookie = errcode;
647 if ( slapd_srvurls == NULL ) return;
649 for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
650 if ( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
651 sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
652 strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
653 sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
655 err = SLPReg( slapd_hslp,
657 SLP_LIFETIME_MAXIMUM,
659 (slapd_slp_attrs) ? slapd_slp_attrs : "",
664 if ( err != SLP_OK || slapd_slp_cookie != SLP_OK ) {
665 Debug( LDAP_DEBUG_CONNS,
666 "daemon: SLPReg(%s) failed with %ld, cookie = %ld\n",
667 slapd_srvurls[i], (long)err, (long)slapd_slp_cookie );
674 slapd_slp_dereg( void )
679 if ( slapd_srvurls == NULL ) return;
681 for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
682 err = SLPDereg( slapd_hslp,
687 if ( err != SLP_OK || slapd_slp_cookie != SLP_OK ) {
688 Debug( LDAP_DEBUG_CONNS,
689 "daemon: SLPDereg(%s) failed with %ld, cookie = %ld\n",
690 slapd_srvurls[i], (long)err, (long)slapd_slp_cookie );
694 #endif /* HAVE_SLP */
697 * Add a descriptor to daemon control
699 * If isactive, the descriptor is a live server session and is subject
700 * to idletimeout control. Otherwise, the descriptor is a passive
701 * listener or an outbound client session, and not subject to
702 * idletimeout. The underlying event handler may record the Listener
703 * argument to differentiate Listener's from real sessions.
706 slapd_add( ber_socket_t s, int isactive, Listener *sl )
708 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
710 assert( SLAP_SOCK_NOT_ACTIVE(s) );
712 if ( isactive ) slap_daemon.sd_nactives++;
714 SLAP_SOCK_ADD(s, sl);
716 Debug( LDAP_DEBUG_CONNS, "daemon: added %ldr%s listener=%p\n",
717 (long) s, isactive ? " (active)" : "", (void *)sl );
719 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
721 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
723 #endif /* SLAP_LIGHTWEIGHT_DISPATCHER */
730 slapd_sd_lock( void )
732 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
739 slapd_sd_unlock( void )
741 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
745 * Remove the descriptor from daemon control
759 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
761 assert( SLAP_SOCK_IS_ACTIVE( s ));
763 if ( wasactive ) slap_daemon.sd_nactives--;
765 waswriter = SLAP_SOCK_IS_WRITE(s);
766 wasreader = SLAP_SOCK_IS_READ(s);
768 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
770 wasreader ? "r" : "",
771 waswriter ? "w" : "" );
773 if ( waswriter ) slap_daemon.sd_nwriters--;
778 ber_sockbuf_free(sb);
780 /* If we ran out of file descriptors, we dropped a listener from
781 * the select() loop. Now that we're removing a session from our
782 * control, we can try to resume a dropped listener to use.
786 for ( i = 0; slap_listeners[i] != NULL; i++ ) {
787 Listener *lr = slap_listeners[i];
789 if ( lr->sl_sd == AC_SOCKET_INVALID ) continue;
790 if ( lr->sl_sd == s ) continue;
797 /* Walked the entire list without enabling anything; emfile
798 * counter is stale. Reset it.
800 if ( slap_listeners[i] == NULL ) emfile = 0;
802 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
803 WAKE_LISTENER(wake || slapd_gentle_shutdown == 2);
807 slapd_clr_write( ber_socket_t s, int wake )
809 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
811 assert( SLAP_SOCK_IS_ACTIVE( s ));
813 if ( SLAP_SOCK_IS_WRITE( s )) {
814 SLAP_SOCK_CLR_WRITE( s );
815 slap_daemon.sd_nwriters--;
818 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
823 slapd_set_write( ber_socket_t s, int wake )
825 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
827 assert( SLAP_SOCK_IS_ACTIVE( s ));
829 if ( !SLAP_SOCK_IS_WRITE( s )) {
830 SLAP_SOCK_SET_WRITE( s );
831 slap_daemon.sd_nwriters++;
834 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
839 slapd_clr_read( ber_socket_t s, int wake )
842 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
844 if ( SLAP_SOCK_IS_ACTIVE( s )) {
845 SLAP_SOCK_CLR_READ( s );
848 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
855 slapd_set_read( ber_socket_t s, int wake )
857 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
859 assert( SLAP_SOCK_IS_ACTIVE( s ));
860 if (!SLAP_SOCK_IS_READ( s )) SLAP_SOCK_SET_READ( s );
862 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
867 slapd_close( ber_socket_t s )
869 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
875 slap_free_listener_addresses( struct sockaddr **sal )
877 struct sockaddr **sap;
878 if (sal == NULL) return;
879 for (sap = sal; *sap != NULL; sap++) ch_free(*sap);
883 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
892 assert( exts != NULL );
893 assert( perms != NULL );
894 assert( crit != NULL );
897 for ( i = 0; exts[ i ]; i++ ) {
898 char *type = exts[ i ];
901 if ( type[ 0 ] == '!' ) {
906 if ( strncasecmp( type, LDAPI_MOD_URLEXT "=",
907 sizeof(LDAPI_MOD_URLEXT "=") - 1 ) == 0 )
909 char *value = type + ( sizeof(LDAPI_MOD_URLEXT "=") - 1 );
913 switch (strlen(value)) {
915 /* skip leading '0' */
916 if ( value[ 0 ] != '0' ) return LDAP_OTHER;
920 for ( j = 0; j < 3; j++) {
923 v = value[ j ] - '0';
925 if ( v < 0 || v > 7 ) return LDAP_OTHER;
932 for ( j = 1; j < 10; j++ ) {
933 static mode_t m[] = { 0,
934 S_IRUSR, S_IWUSR, S_IXUSR,
935 S_IRGRP, S_IWGRP, S_IXGRP,
936 S_IROTH, S_IWOTH, S_IXOTH
938 static char c[] = "-rwxrwxrwx";
940 if ( value[ j ] == c[ j ] ) {
943 } else if ( value[ j ] != '-' ) {
962 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
964 /* port = 0 indicates AF_LOCAL */
966 slap_get_listener_addresses(
969 struct sockaddr ***sal )
971 struct sockaddr **sap;
975 *sal = ch_malloc(2 * sizeof(void *));
976 if (*sal == NULL) return -1;
979 *sap = ch_malloc(sizeof(struct sockaddr_un));
980 if (*sap == NULL) goto errexit;
984 (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) )
986 Debug( LDAP_DEBUG_ANY,
987 "daemon: domain socket path (%s) too long in URL",
992 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
993 (*sap)->sa_family = AF_LOCAL;
994 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
996 #endif /* LDAP_PF_LOCAL */
998 #ifdef HAVE_GETADDRINFO
999 struct addrinfo hints, *res, *sai;
1003 memset( &hints, '\0', sizeof(hints) );
1004 hints.ai_flags = AI_PASSIVE;
1005 hints.ai_socktype = SOCK_STREAM;
1006 hints.ai_family = slap_inet4or6;
1007 snprintf(serv, sizeof serv, "%d", port);
1009 if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
1010 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo() failed: %s\n",
1011 AC_GAI_STRERROR(err), 0, 0);
1016 for (n=2; (sai = sai->ai_next) != NULL; n++) {
1019 *sal = ch_calloc(n, sizeof(void *));
1020 if (*sal == NULL) return -1;
1025 for ( sai=res; sai; sai=sai->ai_next ) {
1026 if( sai->ai_addr == NULL ) {
1027 Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
1028 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
1033 switch (sai->ai_family) {
1034 # ifdef LDAP_PF_INET6
1036 *sap = ch_malloc(sizeof(struct sockaddr_in6));
1041 *(struct sockaddr_in6 *)*sap =
1042 *((struct sockaddr_in6 *)sai->ai_addr);
1044 # endif /* LDAP_PF_INET6 */
1046 *sap = ch_malloc(sizeof(struct sockaddr_in));
1051 *(struct sockaddr_in *)*sap =
1052 *((struct sockaddr_in *)sai->ai_addr);
1060 (*sap)->sa_family = sai->ai_family;
1068 #else /* ! HAVE_GETADDRINFO */
1071 struct hostent *he = NULL;
1073 if ( host == NULL ) {
1074 in.s_addr = htonl(INADDR_ANY);
1076 } else if ( !inet_aton( host, &in ) ) {
1077 he = gethostbyname( host );
1079 Debug( LDAP_DEBUG_ANY,
1080 "daemon: invalid host %s", host, 0, 0);
1083 for (n = 0; he->h_addr_list[n]; n++) /* empty */;
1086 *sal = ch_malloc((n+1) * sizeof(void *));
1087 if (*sal == NULL) return -1;
1090 for ( i = 0; i<n; i++ ) {
1091 sap[i] = ch_malloc(sizeof(struct sockaddr_in));
1092 if (*sap == NULL) goto errexit;
1094 (void)memset( (void *)sap[i], '\0', sizeof(struct sockaddr_in) );
1095 sap[i]->sa_family = AF_INET;
1096 ((struct sockaddr_in *)sap[i])->sin_port = htons(port);
1097 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr,
1098 he ? (struct in_addr *)he->h_addr_list[i] : &in,
1099 sizeof(struct in_addr) );
1102 #endif /* ! HAVE_GETADDRINFO */
1108 slap_free_listener_addresses(*sal);
1122 unsigned short port;
1123 int err, addrlen = 0;
1124 struct sockaddr **sal, **psal;
1125 int socktype = SOCK_STREAM; /* default to COTS */
1127 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
1132 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
1134 rc = ldap_url_parse( url, &lud );
1136 if( rc != LDAP_URL_SUCCESS ) {
1137 Debug( LDAP_DEBUG_ANY,
1138 "daemon: listen URL \"%s\" parse error=%d\n",
1143 l.sl_url.bv_val = NULL;
1145 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1147 #endif /* SLAP_LIGHTWEIGHT_DISPATCHER */
1150 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
1151 Debug( LDAP_DEBUG_ANY, "daemon: TLS not supported (%s)\n",
1153 ldap_free_urldesc( lud );
1157 if(! lud->lud_port ) lud->lud_port = LDAP_PORT;
1159 #else /* HAVE_TLS */
1160 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
1162 if(! lud->lud_port ) {
1163 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
1165 #endif /* HAVE_TLS */
1167 port = (unsigned short) lud->lud_port;
1169 tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme);
1170 if ( tmp == LDAP_PROTO_IPC ) {
1171 #ifdef LDAP_PF_LOCAL
1172 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
1173 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
1175 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
1177 #else /* ! LDAP_PF_LOCAL */
1179 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
1181 ldap_free_urldesc( lud );
1183 #endif /* ! LDAP_PF_LOCAL */
1185 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
1186 || strcmp(lud->lud_host, "*") == 0 )
1188 err = slap_get_listener_addresses(NULL, port, &sal);
1190 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
1194 #ifdef LDAP_CONNECTIONLESS
1195 l.sl_is_udp = ( tmp == LDAP_PROTO_UDP );
1196 #endif /* LDAP_CONNECTIONLESS */
1198 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
1199 if ( lud->lud_exts ) {
1200 err = get_url_perms( lud->lud_exts, &l.sl_perms, &crit );
1202 l.sl_perms = S_IRWXU | S_IRWXO;
1204 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
1206 ldap_free_urldesc( lud );
1207 if ( err ) return -1;
1209 /* If we got more than one address returned, we need to make space
1210 * for it in the slap_listeners array.
1212 for ( num=0; sal[num]; num++ ) /* empty */;
1214 *listeners += num-1;
1215 slap_listeners = ch_realloc( slap_listeners,
1216 (*listeners + 1) * sizeof(Listener *) );
1220 while ( *sal != NULL ) {
1222 switch( (*sal)->sa_family ) {
1226 #ifdef LDAP_PF_INET6
1230 #endif /* LDAP_PF_INET6 */
1231 #ifdef LDAP_PF_LOCAL
1235 #endif /* LDAP_PF_LOCAL */
1241 #ifdef LDAP_CONNECTIONLESS
1242 if( l.sl_is_udp ) socktype = SOCK_DGRAM;
1243 #endif /* LDAP_CONNECTIONLESS */
1245 l.sl_sd = socket( (*sal)->sa_family, socktype, 0);
1246 if ( l.sl_sd == AC_SOCKET_INVALID ) {
1247 int err = sock_errno();
1248 Debug( LDAP_DEBUG_ANY,
1249 "daemon: %s socket() failed errno=%d (%s)\n",
1250 af, err, sock_errstr(err) );
1255 #ifndef HAVE_WINSOCK
1256 if ( l.sl_sd >= dtblsize ) {
1257 Debug( LDAP_DEBUG_ANY,
1258 "daemon: listener descriptor %ld is too great %ld\n",
1259 (long) l.sl_sd, (long) dtblsize, 0 );
1260 tcp_close( l.sl_sd );
1264 #endif /* ! HAVE_WINSOCK */
1266 #ifdef LDAP_PF_LOCAL
1267 if ( (*sal)->sa_family == AF_LOCAL ) {
1268 unlink( ((struct sockaddr_un *)*sal)->sun_path );
1270 #endif /* LDAP_PF_LOCAL */
1273 /* enable address reuse */
1275 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
1276 (char *) &tmp, sizeof(tmp) );
1277 if ( rc == AC_SOCKET_ERROR ) {
1278 int err = sock_errno();
1279 Debug( LDAP_DEBUG_ANY, "slapd(%ld): "
1280 "setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
1281 (long) l.sl_sd, err, sock_errstr(err) );
1283 #endif /* SO_REUSEADDR */
1286 switch( (*sal)->sa_family ) {
1288 addrlen = sizeof(struct sockaddr_in);
1290 #ifdef LDAP_PF_INET6
1293 /* Try to use IPv6 sockets for IPv6 only */
1295 rc = setsockopt( l.sl_sd, IPPROTO_IPV6, IPV6_V6ONLY,
1296 (char *) &tmp, sizeof(tmp) );
1297 if ( rc == AC_SOCKET_ERROR ) {
1298 int err = sock_errno();
1299 Debug( LDAP_DEBUG_ANY, "slapd(%ld): "
1300 "setsockopt(IPV6_V6ONLY) failed errno=%d (%s)\n",
1301 (long) l.sl_sd, err, sock_errstr(err) );
1303 #endif /* IPV6_V6ONLY */
1304 addrlen = sizeof(struct sockaddr_in6);
1306 #endif /* LDAP_PF_INET6 */
1308 #ifdef LDAP_PF_LOCAL
1313 setsockopt( l.sl_sd, 0, LOCAL_CREDS, &one, sizeof( one ) );
1315 #endif /* LOCAL_CREDS */
1317 addrlen = sizeof( struct sockaddr_un );
1319 #endif /* LDAP_PF_LOCAL */
1322 #ifdef LDAP_PF_LOCAL
1323 /* create socket with all permissions set for those systems
1324 * that honor permissions on sockets (e.g. Linux); typically,
1325 * only write is required. To exploit filesystem permissions,
1326 * place the socket in a directory and use directory's
1327 * permissions. Need write perms to the directory to
1328 * create/unlink the socket; likely need exec perms to access
1329 * the socket (ITS#4709) */
1333 if ( (*sal)->sa_family == AF_LOCAL ) {
1334 old_umask = umask( 0 );
1336 #endif /* LDAP_PF_LOCAL */
1337 rc = bind( l.sl_sd, *sal, addrlen );
1338 #ifdef LDAP_PF_LOCAL
1339 if ( (*sal)->sa_family == AF_LOCAL ) {
1343 #endif /* LDAP_PF_LOCAL */
1346 Debug( LDAP_DEBUG_ANY,
1347 "daemon: bind(%ld) failed errno=%d (%s)\n",
1348 (long)l.sl_sd, err, sock_errstr( err ) );
1349 tcp_close( l.sl_sd );
1354 switch ( (*sal)->sa_family ) {
1355 #ifdef LDAP_PF_LOCAL
1357 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
1358 l.sl_name.bv_len = strlen(addr) + sizeof("PATH=") - 1;
1359 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len + 1 );
1360 snprintf( l.sl_name.bv_val, l.sl_name.bv_len + 1,
1363 #endif /* LDAP_PF_LOCAL */
1367 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
1368 char addr[INET_ADDRSTRLEN];
1369 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
1370 addr, sizeof(addr) );
1372 #else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1373 s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr );
1374 #endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1375 port = ntohs( ((struct sockaddr_in *)*sal) ->sin_port );
1377 ber_memalloc( sizeof("IP=255.255.255.255:65535") );
1378 snprintf( l.sl_name.bv_val, sizeof("IP=255.255.255.255:65535"),
1380 s != NULL ? s : SLAP_STRING_UNKNOWN, port );
1381 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1384 #ifdef LDAP_PF_INET6
1386 char addr[INET6_ADDRSTRLEN];
1387 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
1389 port = ntohs( ((struct sockaddr_in6 *)*sal)->sin6_port );
1390 l.sl_name.bv_len = strlen(addr) + sizeof("IP= 65535");
1391 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len );
1392 snprintf( l.sl_name.bv_val, l.sl_name.bv_len, "IP=%s %d",
1394 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1396 #endif /* LDAP_PF_INET6 */
1399 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
1400 (int) (*sal)->sa_family, 0, 0 );
1404 AC_MEMCPY(&l.sl_sa, *sal, addrlen);
1405 ber_str2bv( url, 0, 1, &l.sl_url);
1406 li = ch_malloc( sizeof( Listener ) );
1408 slap_listeners[*cur] = li;
1413 slap_free_listener_addresses(psal);
1415 if ( l.sl_url.bv_val == NULL ) {
1416 Debug( LDAP_DEBUG_TRACE,
1417 "slap_open_listener: failed on %s\n", url, 0, 0 );
1421 Debug( LDAP_DEBUG_TRACE, "daemon: listener initialized %s\n",
1422 l.sl_url.bv_val, 0, 0 );
1426 static int sockinit(void);
1427 static int sockdestroy(void);
1430 slapd_daemon_init( const char *urls )
1435 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
1436 urls ? urls : "<null>", 0, 0 );
1438 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
1440 ldap_pvt_thread_mutex_init( &slap_daemon.sd_tcpd_mutex );
1441 #endif /* TCP Wrappers */
1443 if( (rc = sockinit()) != 0 ) return rc;
1446 dtblsize = sysconf( _SC_OPEN_MAX );
1447 #elif HAVE_GETDTABLESIZE
1448 dtblsize = getdtablesize();
1449 #else /* ! HAVE_SYSCONF && ! HAVE_GETDTABLESIZE */
1450 dtblsize = FD_SETSIZE;
1451 #endif /* ! HAVE_SYSCONF && ! HAVE_GETDTABLESIZE */
1453 /* open a pipe (or something equivalent connected to itself).
1454 * we write a byte on this fd whenever we catch a signal. The main
1455 * loop will be select'ing on this socket, and will wake up when
1456 * this byte arrives.
1458 if( (rc = lutil_pair( wake_sds )) < 0 ) {
1459 Debug( LDAP_DEBUG_ANY,
1460 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
1466 if( urls == NULL ) urls = "ldap:///";
1468 u = ldap_str2charray( urls, " " );
1470 if( u == NULL || u[0] == NULL ) {
1471 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
1474 ldap_charray_free( u );
1478 for( i=0; u[i] != NULL; i++ ) {
1479 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
1484 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
1486 ldap_charray_free( u );
1490 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
1492 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
1494 for(n = 0, j = 0; u[n]; n++ ) {
1495 if ( slap_open_listener( u[n], &i, &j ) ) {
1496 ldap_charray_free( u );
1500 slap_listeners[j] = NULL;
1502 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
1507 if( slapd_register_slp ) {
1508 slapd_slp_init( urls );
1511 #endif /* HAVE_SLP */
1513 ldap_charray_free( u );
1520 slapd_daemon_destroy( void )
1522 connections_destroy();
1523 tcp_close( wake_sds[1] );
1524 tcp_close( wake_sds[0] );
1528 if( slapd_register_slp ) {
1532 #endif /* HAVE_SLP */
1535 ldap_pvt_thread_mutex_destroy( &slap_daemon.sd_tcpd_mutex );
1536 #endif /* TCP Wrappers */
1538 ldap_pvt_thread_mutex_destroy( &slap_daemon.sd_mutex );
1549 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1550 Listener *lr = slap_listeners[l];
1552 if ( lr->sl_sd != AC_SOCKET_INVALID ) {
1553 if ( remove ) slapd_remove( lr->sl_sd, NULL, 0, 0, 0 );
1555 #ifdef LDAP_PF_LOCAL
1556 if ( lr->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1557 unlink( lr->sl_sa.sa_un_addr.sun_path );
1559 #endif /* LDAP_PF_LOCAL */
1561 slapd_close( lr->sl_sd );
1564 if ( lr->sl_url.bv_val ) {
1565 ber_memfree( lr->sl_url.bv_val );
1568 if ( lr->sl_name.bv_val ) {
1569 ber_memfree( lr->sl_name.bv_val );
1573 slap_listeners[l] = NULL;
1584 socklen_t len = sizeof(from);
1587 struct berval authid = BER_BVNULL;
1588 #ifdef SLAPD_RLOOKUPS
1589 char hbuf[NI_MAXHOST];
1590 #endif /* SLAPD_RLOOKUPS */
1592 char *dnsname = NULL;
1593 char *peeraddr = NULL;
1594 #ifdef LDAP_PF_LOCAL
1595 char peername[MAXPATHLEN + sizeof("PATH=")];
1596 #elif defined(LDAP_PF_INET6)
1597 char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
1598 #else /* ! LDAP_PF_LOCAL && ! LDAP_PF_INET6 */
1599 char peername[sizeof("IP=255.255.255.255:65336")];
1600 #endif /* LDAP_PF_LOCAL */
1602 Debug( LDAP_DEBUG_TRACE,
1603 ">>> slap_listener(%s)",
1604 sl->sl_url.bv_val, 0, 0 );
1608 #ifdef LDAP_CONNECTIONLESS
1609 if ( sl->sl_is_udp ) return 1;
1610 #endif /* LDAP_CONNECTIONLESS */
1612 # ifdef LDAP_PF_LOCAL
1613 /* FIXME: apparently accept doesn't fill
1614 * the sun_path sun_path member */
1615 from.sa_un_addr.sun_path[0] = '\0';
1616 # endif /* LDAP_PF_LOCAL */
1618 s = accept( sl->sl_sd, (struct sockaddr *) &from, &len );
1620 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1621 /* Resume the listener FD to allow concurrent-processing of
1622 * additional incoming connections.
1626 #endif /* SLAP_LIGHTWEIGHT_DISPATCHER */
1628 if ( s == AC_SOCKET_INVALID ) {
1629 int err = sock_errno();
1640 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1642 /* Stop listening until an existing session closes */
1644 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1647 Debug( LDAP_DEBUG_ANY,
1648 "daemon: accept(%ld) failed errno=%d (%s)\n",
1649 (long) sl->sl_sd, err, sock_errstr(err) );
1650 ldap_pvt_thread_yield();
1654 #ifndef HAVE_WINSOCK
1655 /* make sure descriptor number isn't too great */
1656 if ( s >= dtblsize ) {
1657 Debug( LDAP_DEBUG_ANY,
1658 "daemon: %ld beyond descriptor table size %ld\n",
1659 (long) s, (long) dtblsize, 0 );
1662 ldap_pvt_thread_yield();
1665 #endif /* ! HAVE_WINSOCK */
1668 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1669 /* newly accepted stream should not be in any of the FD SETS */
1670 assert( SLAP_SOCK_NOT_ACTIVE( s ));
1671 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1672 #endif /* LDAP_DEBUG */
1674 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1675 #ifdef LDAP_PF_LOCAL
1676 /* for IPv4 and IPv6 sockets only */
1677 if ( from.sa_addr.sa_family != AF_LOCAL )
1678 #endif /* LDAP_PF_LOCAL */
1683 /* enable keep alives */
1685 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1686 (char *) &tmp, sizeof(tmp) );
1687 if ( rc == AC_SOCKET_ERROR ) {
1688 int err = sock_errno();
1689 Debug( LDAP_DEBUG_ANY,
1690 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1691 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1693 #endif /* SO_KEEPALIVE */
1695 /* enable no delay */
1697 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1698 (char *)&tmp, sizeof(tmp) );
1699 if ( rc == AC_SOCKET_ERROR ) {
1700 int err = sock_errno();
1701 Debug( LDAP_DEBUG_ANY,
1702 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1703 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1705 #endif /* TCP_NODELAY */
1707 #endif /* SO_KEEPALIVE || TCP_NODELAY */
1709 Debug( LDAP_DEBUG_CONNS,
1710 "daemon: listen=%ld, new connection on %ld\n",
1711 (long) sl->sl_sd, (long) s, 0 );
1713 switch ( from.sa_addr.sa_family ) {
1714 # ifdef LDAP_PF_LOCAL
1716 /* FIXME: apparently accept doesn't fill
1717 * the sun_path sun_path member */
1718 if ( from.sa_un_addr.sun_path[0] == '\0' ) {
1719 AC_MEMCPY( from.sa_un_addr.sun_path,
1720 sl->sl_sa.sa_un_addr.sun_path,
1721 sizeof( from.sa_un_addr.sun_path ) );
1724 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1730 if( getpeereid( s, &uid, &gid ) == 0 ) {
1731 authid.bv_val = ch_malloc(
1732 STRLENOF( "gidNumber=4294967295+uidNumber=4294967295,"
1733 "cn=peercred,cn=external,cn=auth" ) + 1 );
1734 authid.bv_len = sprintf( authid.bv_val,
1735 "gidNumber=%d+uidNumber=%d,"
1736 "cn=peercred,cn=external,cn=auth",
1737 (int) gid, (int) uid );
1738 assert( authid.bv_len <=
1739 STRLENOF( "gidNumber=4294967295+uidNumber=4294967295,"
1740 "cn=peercred,cn=external,cn=auth" ) );
1745 #endif /* LDAP_PF_LOCAL */
1747 # ifdef LDAP_PF_INET6
1749 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1750 peeraddr = inet_ntoa( *((struct in_addr *)
1751 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1752 sprintf( peername, "IP=%s:%d",
1753 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1754 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1756 char addr[INET6_ADDRSTRLEN];
1758 peeraddr = (char *) inet_ntop( AF_INET6,
1759 &from.sa_in6_addr.sin6_addr,
1760 addr, sizeof addr );
1761 sprintf( peername, "IP=%s %d",
1762 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1763 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1766 # endif /* LDAP_PF_INET6 */
1769 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1770 sprintf( peername, "IP=%s:%d",
1771 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1772 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1780 if ( ( from.sa_addr.sa_family == AF_INET )
1781 #ifdef LDAP_PF_INET6
1782 || ( from.sa_addr.sa_family == AF_INET6 )
1783 #endif /* LDAP_PF_INET6 */
1787 #ifdef SLAPD_RLOOKUPS
1788 if ( use_reverse_lookup ) {
1790 if (ldap_pvt_get_hname( (const struct sockaddr *)&from, len, hbuf,
1791 sizeof(hbuf), &herr ) == 0) {
1792 ldap_pvt_str2lower( hbuf );
1796 #endif /* SLAPD_RLOOKUPS */
1801 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_tcpd_mutex );
1802 rc = hosts_ctl("slapd",
1803 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1804 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1805 SLAP_STRING_UNKNOWN );
1806 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_tcpd_mutex );
1809 Statslog( LDAP_DEBUG_STATS,
1810 "fd=%ld DENIED from %s (%s)\n",
1812 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1813 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1819 #endif /* HAVE_TCPD */
1822 id = connection_init(s, sl,
1823 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1826 sl->sl_is_tls ? CONN_IS_TLS : 0,
1827 #else /* ! HAVE_TLS */
1829 #endif /* ! HAVE_TLS */
1831 authid.bv_val ? &authid : NULL );
1833 if( authid.bv_val ) ch_free(authid.bv_val);
1836 Debug( LDAP_DEBUG_ANY,
1837 "daemon: connection_init(%ld, %s, %s) failed.\n",
1838 (long) s, peername, sl->sl_name.bv_val );
1843 Statslog( LDAP_DEBUG_STATS,
1844 "conn=%ld fd=%ld ACCEPT from %s (%s)\n",
1845 id, (long) s, peername, sl->sl_name.bv_val,
1851 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1853 slap_listener_thread(
1858 Listener *sl = (Listener *)ptr;
1860 rc = slap_listener( sl );
1862 if( rc != LDAP_SUCCESS ) {
1863 Debug( LDAP_DEBUG_ANY,
1864 "slap_listener_thread(%s): failed err=%d",
1865 sl->sl_url.bv_val, rc, 0 );
1872 slap_listener_activate(
1877 Debug( LDAP_DEBUG_TRACE, "slap_listener_activate(%d): %s\n",
1878 sl->sl_sd, sl->sl_busy ? "busy" : "", 0 );
1882 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1883 slap_listener_thread, (void *) sl );
1886 Debug( LDAP_DEBUG_ANY,
1887 "listener_activate(%d): submit failed (%d)\n",
1892 #endif /* SLAP_LIGHTWEIGHT_DISPATCHER */
1899 time_t last_idle_check = 0;
1900 struct timeval idle;
1903 #define SLAPD_IDLE_CHECK_LIMIT 4
1905 if ( global_idletimeout > 0 ) {
1906 last_idle_check = slap_get_time();
1907 /* Set the select timeout.
1908 * Don't just truncate, preserve the fractions of
1909 * seconds to prevent sleeping for zero time.
1911 idle.tv_sec = global_idletimeout / SLAPD_IDLE_CHECK_LIMIT;
1912 idle.tv_usec = global_idletimeout - \
1913 ( idle.tv_sec * SLAPD_IDLE_CHECK_LIMIT );
1914 idle.tv_usec *= 1000000 / SLAPD_IDLE_CHECK_LIMIT;
1920 slapd_add( wake_sds[0], 0, NULL );
1922 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1923 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ) continue;
1925 #ifdef LDAP_CONNECTIONLESS
1926 /* Since this is connectionless, the data port is the
1927 * listening port. The listen() and accept() calls
1930 if ( slap_listeners[l]->sl_is_udp )
1932 #endif /* LDAP_CONNECTIONLESS */
1934 if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN_BACKLOG ) == -1 ) {
1935 int err = sock_errno();
1937 #ifdef LDAP_PF_INET6
1938 /* If error is EADDRINUSE, we are trying to listen to INADDR_ANY and
1939 * we are already listening to in6addr_any, then we want to ignore
1940 * this and continue.
1942 if ( err == EADDRINUSE ) {
1944 struct sockaddr_in sa = slap_listeners[l]->sl_sa.sa_in_addr;
1945 struct sockaddr_in6 sa6;
1947 if ( sa.sin_family == AF_INET &&
1948 sa.sin_addr.s_addr == htonl(INADDR_ANY) ) {
1949 for ( i = 0 ; i < l; i++ ) {
1950 sa6 = slap_listeners[i]->sl_sa.sa_in6_addr;
1951 if ( sa6.sin6_family == AF_INET6 &&
1952 !memcmp( &sa6.sin6_addr, &in6addr_any,
1953 sizeof(struct in6_addr) ) )
1960 /* We are already listening to in6addr_any */
1961 Debug( LDAP_DEBUG_CONNS,
1962 "daemon: Attempt to listen to 0.0.0.0 failed, "
1963 "already listening on ::, assuming IPv4 included\n",
1965 slapd_close( slap_listeners[l]->sl_sd );
1966 slap_listeners[l]->sl_sd = AC_SOCKET_INVALID;
1971 #endif /* LDAP_PF_INET6 */
1972 Debug( LDAP_DEBUG_ANY,
1973 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
1974 slap_listeners[l]->sl_url.bv_val, err,
1979 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1980 /* make the listening socket non-blocking */
1981 if ( ber_pvt_socket_set_nonblock( slap_listeners[l]->sl_sd, 1 ) < 0 ) {
1982 Debug( LDAP_DEBUG_ANY, "slapd_daemon_task: "
1983 "set nonblocking on a listening socket failed\n",
1988 #endif /* SLAP_LIGHTWEIGHT_DISPATCHER */
1990 slapd_add( slap_listeners[l]->sl_sd, 0, slap_listeners[l] );
1993 #ifdef HAVE_NT_SERVICE_MANAGER
1994 if ( started_event != NULL ) {
1995 ldap_pvt_thread_cond_signal( &started_event );
1997 #endif /* HAVE_NT_SERVICE_MANAGER */
1999 #ifdef SLAP_SEM_LOAD_CONTROL
2001 * initialize count and lazyness of a semaphore
2003 (void) ldap_lazy_sem_init(
2004 SLAP_MAX_WORKER_THREADS + 4 /* max workers + margin */,
2006 #endif /* SLAP_SEM_LOAD_CONTROL */
2008 /* initialization complete. Here comes the loop. */
2010 while ( !slapd_shutdown ) {
2015 #if SLAP_EVENTS_ARE_INDEXED
2016 ber_socket_t nrfds, nwfds;
2017 #endif /* SLAP_EVENTS_ARE_INDEXED */
2018 #define SLAPD_EBADF_LIMIT 16
2025 struct timeval *tvp;
2031 now = slap_get_time();
2033 if ( ( global_idletimeout > 0 ) &&
2034 difftime( last_idle_check +
2035 global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 )
2037 connections_timeout_idle( now );
2038 last_idle_check = now;
2043 if ( slapd_gentle_shutdown ) {
2044 ber_socket_t active;
2046 if ( slapd_gentle_shutdown == 1 ) {
2048 Debug( LDAP_DEBUG_ANY, "slapd gentle shutdown\n", 0, 0, 0 );
2049 close_listeners( 1 );
2050 frontendDB->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
2051 LDAP_STAILQ_FOREACH(be, &backendDB, be_next) {
2052 be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
2054 slapd_gentle_shutdown = 2;
2057 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
2058 active = slap_daemon.sd_nactives;
2059 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
2060 if ( active == 0 ) {
2068 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
2070 nwriters = slap_daemon.sd_nwriters;
2072 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2073 Listener *lr = slap_listeners[l];
2075 if ( lr->sl_sd == AC_SOCKET_INVALID ) continue;
2077 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
2078 if ( lr->sl_mute || lr->sl_busy )
2079 #else /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
2081 #endif /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
2083 SLAP_SOCK_CLR_READ( lr->sl_sd );
2085 SLAP_SOCK_SET_READ( lr->sl_sd );
2091 nfds = SLAP_EVENT_MAX;
2093 if ( global_idletimeout && slap_daemon.sd_nactives ) at = 1;
2095 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
2098 #if defined(HAVE_YIELDING_SELECT) || defined(NO_THREADS)
2099 && ( tv.tv_sec || tv.tv_usec )
2100 #endif /* HAVE_YIELDING_SELECT || NO_THREADS */
2108 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2109 rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
2110 while ( rtask && cat.tv_sec && cat.tv_sec <= now ) {
2111 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
2112 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
2114 ldap_pvt_runqueue_runtask( &slapd_rq, rtask );
2115 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
2116 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2117 ldap_pvt_thread_pool_submit( &connection_pool,
2118 rtask->routine, (void *) rtask );
2119 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2121 rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
2123 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2125 if ( rtask && cat.tv_sec ) {
2126 time_t diff = difftime( cat.tv_sec, now );
2127 if ( diff == 0 ) diff = tdelta;
2128 if ( tvp == NULL || diff < tv.tv_sec ) {
2135 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2136 Listener *lr = slap_listeners[l];
2138 if ( lr->sl_sd == AC_SOCKET_INVALID ) {
2142 if ( lr->sl_mute ) {
2143 Debug( LDAP_DEBUG_CONNS,
2144 "daemon: " SLAP_EVENT_FNAME ": "
2145 "listen=%d muted\n",
2150 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
2151 if ( lr->sl_busy ) {
2152 Debug( LDAP_DEBUG_CONNS,
2153 "daemon: " SLAP_EVENT_FNAME ": "
2158 #endif /* SLAP_LIGHTWEIGHT_DISPATCHER */
2160 Debug( LDAP_DEBUG_CONNS,
2161 "daemon: " SLAP_EVENT_FNAME ": "
2162 "listen=%d active_threads=%d tvp=%s\n",
2163 lr->sl_sd, at, tvp == NULL ? "NULL" : "zero" );
2166 SLAP_EVENT_WAIT( tvp, &ns );
2168 case -1: { /* failure - try again */
2169 int err = sock_errno();
2171 if ( err != EINTR ) {
2174 /* Don't log unless we got it twice in a row */
2175 if ( !( ebadf & 1 ) ) {
2176 Debug( LDAP_DEBUG_ANY,
2182 sock_errstr( err ) );
2184 if ( ebadf >= SLAPD_EBADF_LIMIT ) {
2191 case 0: /* timeout - let threads run */
2193 #ifndef HAVE_YIELDING_SELECT
2194 Debug( LDAP_DEBUG_CONNS, "daemon: " SLAP_EVENT_FNAME
2195 "timeout - yielding\n",
2198 ldap_pvt_thread_yield();
2199 #endif /* ! HAVE_YIELDING_SELECT */
2202 default: /* something happened - deal with it */
2203 if ( slapd_shutdown ) continue;
2206 Debug( LDAP_DEBUG_CONNS,
2207 "daemon: activity on %d descriptor%s\n",
2208 ns, ns != 1 ? "s" : "", 0 );
2212 #if SLAP_EVENTS_ARE_INDEXED
2213 if ( SLAP_EVENT_IS_READ( wake_sds[0] ) ) {
2215 SLAP_EVENT_CLR_READ( wake_sds[0] );
2217 tcp_read( wake_sds[0], c, sizeof(c) );
2218 Debug( LDAP_DEBUG_CONNS, "daemon: waked\n", 0, 0, 0 );
2222 /* The event slot equals the descriptor number - this is
2223 * true for Unix select and poll. We treat Windows select
2224 * like this too, even though it's a kludge.
2226 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2229 if ( ns <= 0 ) break;
2230 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ) continue;
2231 #ifdef LDAP_CONNECTIONLESS
2232 if ( slap_listeners[l]->sl_is_udp ) continue;
2233 #endif /* LDAP_CONNECTIONLESS */
2234 if ( !SLAP_EVENT_IS_READ( slap_listeners[l]->sl_sd ) ) continue;
2237 SLAP_EVENT_CLR_READ( slap_listeners[l]->sl_sd );
2238 SLAP_EVENT_CLR_WRITE( slap_listeners[l]->sl_sd );
2241 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
2242 rc = slap_listener_activate( slap_listeners[l] );
2243 #else /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
2244 rc = slap_listener( slap_listeners[l] );
2245 #endif /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
2248 /* bypass the following tests if no descriptors left */
2250 #ifndef HAVE_YIELDING_SELECT
2251 ldap_pvt_thread_yield();
2252 #endif /* HAVE_YIELDING_SELECT */
2256 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
2258 nrfds = readfds.fd_count;
2259 nwfds = writefds.fd_count;
2260 for ( i = 0; i < readfds.fd_count; i++ ) {
2261 Debug( LDAP_DEBUG_CONNS, " %d%s",
2262 readfds.fd_array[i], "r", 0 );
2264 for ( i = 0; i < writefds.fd_count; i++ ) {
2265 Debug( LDAP_DEBUG_CONNS, " %d%s",
2266 writefds.fd_array[i], "w", 0 );
2269 #else /* ! HAVE_WINSOCK */
2272 for ( i = 0; i < nfds; i++ ) {
2275 r = SLAP_EVENT_IS_READ( i );
2276 /* writefds was not initialized if nwriters was zero */
2277 w = nwriters ? SLAP_EVENT_IS_WRITE( i ) : 0;
2279 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
2280 r ? "r" : "", w ? "w" : "" );
2290 if ( ns <= 0 ) break;
2292 #endif /* ! HAVE_WINSOCK */
2293 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
2295 /* loop through the writers */
2296 for ( i = 0; nwfds > 0; i++ ) {
2299 wd = writefds.fd_array[i];
2300 #else /* ! HAVE_WINSOCK */
2301 if ( ! SLAP_EVENT_IS_WRITE( i ) ) continue;
2303 #endif /* ! HAVE_WINSOCK */
2305 SLAP_EVENT_CLR_WRITE( wd );
2308 Debug( LDAP_DEBUG_CONNS,
2309 "daemon: write active on %d\n",
2313 * NOTE: it is possible that the connection was closed
2314 * and that the stream is now inactive.
2315 * connection_write() must validate the stream is still
2318 * ITS#4338: if the stream is invalid, there is no need to
2319 * close it here. It has already been closed in connection.c.
2321 if ( connection_write( wd ) < 0 ) {
2322 if ( SLAP_EVENT_IS_READ( wd ) ) {
2323 SLAP_EVENT_CLR_READ( (unsigned) wd );
2329 for ( i = 0; nrfds > 0; i++ ) {
2332 rd = readfds.fd_array[i];
2333 #else /* ! HAVE_WINSOCK */
2334 if ( ! SLAP_EVENT_IS_READ( i ) ) continue;
2336 #endif /* ! HAVE_WINSOCK */
2337 SLAP_EVENT_CLR_READ( rd );
2340 Debug ( LDAP_DEBUG_CONNS,
2341 "daemon: read activity on %d\n", rd, 0, 0 );
2343 * NOTE: it is possible that the connection was closed
2344 * and that the stream is now inactive.
2345 * connection_read() must valid the stream is still
2349 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
2350 connection_read_activate( rd );
2351 #else /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
2352 connection_read( rd );
2353 #endif /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
2355 #else /* !SLAP_EVENTS_ARE_INDEXED */
2357 /* The events are returned in an arbitrary list. This is true
2358 * for /dev/poll, epoll and kqueue. In order to prioritize things
2359 * so that we can handle wake_sds first, listeners second, and then
2360 * all other connections last (as we do for select), we would need
2361 * to use multiple event handles and cascade them.
2363 * That seems like a bit of hassle. So the wake_sds check has been
2364 * skipped. For epoll and kqueue we can associate arbitrary data with
2365 * an event, so we could use pointers to the listener structure
2366 * instead of just the file descriptor. For /dev/poll we have to
2367 * search the listeners array for a matching descriptor.
2369 * We now handle wake events when we see them; they are not given
2373 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
2375 for ( i = 0; i < ns; i++ ) {
2378 /* Don't log listener events */
2379 if ( SLAP_EVENT_IS_LISTENER( i )
2380 #ifdef LDAP_CONNECTIONLESS
2381 && !( (SLAP_EVENT_LISTENER( i ))->sl_is_udp )
2382 #endif /* LDAP_CONNECTIONLESS */
2388 /* Don't log internal wake events */
2389 if ( SLAP_EVENT_FD( i ) == wake_sds[0] ) continue;
2391 r = SLAP_EVENT_IS_READ( i );
2392 w = SLAP_EVENT_IS_WRITE( i );
2394 Debug( LDAP_DEBUG_CONNS, " %d%s%s", SLAP_EVENT_FD(i),
2395 r ? "r" : "", w ? "w" : "" );
2398 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
2399 #endif /* LDAP_DEBUG */
2401 for ( i = 0; i < ns; i++ ) {
2404 if ( SLAP_EVENT_IS_LISTENER( i ) ) {
2405 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
2406 rc = slap_listener_activate( SLAP_EVENT_LISTENER( i ) );
2407 #else /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
2408 rc = slap_listener( SLAP_EVENT_LISTENER( i ) );
2409 #endif /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
2412 /* If we found a regular listener, rc is now zero, and we
2413 * can skip the data portion. But if it was a UDP listener
2414 * then rc is still 1, and we want to handle the data.
2417 fd = SLAP_EVENT_FD( i );
2419 /* Handle wake events */
2420 if ( fd == wake_sds[0] ) {
2423 tcp_read( wake_sds[0], c, sizeof(c) );
2427 if ( SLAP_EVENT_IS_WRITE( i ) ) {
2428 Debug( LDAP_DEBUG_CONNS,
2429 "daemon: write active on %d\n",
2432 SLAP_EVENT_CLR_WRITE( i );
2435 * NOTE: it is possible that the connection was closed
2436 * and that the stream is now inactive.
2437 * connection_write() must valid the stream is still
2440 if ( connection_write( fd ) < 0 ) {
2444 /* If event is a read */
2445 if ( SLAP_EVENT_IS_READ( i ) ) {
2446 Debug( LDAP_DEBUG_CONNS,
2447 "daemon: read active on %d\n",
2450 SLAP_EVENT_CLR_READ( i );
2451 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
2452 connection_read_activate( fd );
2453 #else /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
2455 * NOTE: it is possible that the connection was closed
2456 * and that the stream is now inactive.
2457 * connection_read() must valid the stream is still
2460 connection_read( fd );
2461 #endif /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
2463 Debug( LDAP_DEBUG_CONNS,
2464 "daemon: hangup on %d\n", fd, 0, 0 );
2468 #endif /* SLAP_EVENTS_ARE_INDEXED */
2470 #ifndef HAVE_YIELDING_SELECT
2471 ldap_pvt_thread_yield();
2472 #endif /* ! HAVE_YIELDING_SELECT */
2475 if ( slapd_shutdown == 1 ) {
2476 Debug( LDAP_DEBUG_ANY,
2477 "daemon: shutdown requested and initiated.\n",
2480 } else if ( slapd_shutdown == 2 ) {
2481 #ifdef HAVE_NT_SERVICE_MANAGER
2482 Debug( LDAP_DEBUG_ANY,
2483 "daemon: shutdown initiated by Service Manager.\n",
2485 #else /* !HAVE_NT_SERVICE_MANAGER */
2486 Debug( LDAP_DEBUG_ANY,
2487 "daemon: abnormal condition, shutdown initiated.\n",
2489 #endif /* !HAVE_NT_SERVICE_MANAGER */
2491 Debug( LDAP_DEBUG_ANY,
2492 "daemon: no active streams, shutdown initiated.\n",
2496 if ( slapd_gentle_shutdown != 2 ) close_listeners ( 0 );
2498 if ( !slapd_gentle_shutdown ) {
2499 slapd_abrupt_shutdown = 1;
2500 connections_shutdown();
2503 Debug( LDAP_DEBUG_ANY,
2504 "slapd shutdown: waiting for %d threads to terminate\n",
2505 ldap_pvt_thread_pool_backload( &connection_pool ), 0, 0 );
2506 ldap_pvt_thread_pool_destroy( &connection_pool, 1 );
2508 free( slap_listeners );
2509 slap_listeners = NULL;
2515 #ifdef LDAP_CONNECTIONLESS
2517 connectionless_init( void )
2521 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2522 Listener *lr = slap_listeners[l];
2525 if ( !lr->sl_is_udp ) {
2529 id = connection_init( lr->sl_sd, lr, "", "",
2530 CONN_IS_UDP, (slap_ssf_t) 0, NULL );
2533 Debug( LDAP_DEBUG_TRACE,
2534 "connectionless_init: failed on %s (%d)\n",
2535 lr->sl_url, lr->sl_sd, 0 );
2543 #endif /* LDAP_CONNECTIONLESS */
2546 slapd_daemon( void )
2551 #ifdef LDAP_CONNECTIONLESS
2552 connectionless_init();
2553 #endif /* LDAP_CONNECTIONLESS */
2555 #define SLAPD_LISTENER_THREAD 1
2556 #if defined( SLAPD_LISTENER_THREAD )
2558 ldap_pvt_thread_t listener_tid;
2560 /* listener as a separate THREAD */
2561 rc = ldap_pvt_thread_create( &listener_tid,
2562 0, slapd_daemon_task, NULL );
2565 Debug( LDAP_DEBUG_ANY,
2566 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
2570 /* wait for the listener thread to complete */
2571 ldap_pvt_thread_join( listener_tid, (void *)NULL );
2573 #else /* ! SLAPD_LISTENER_THREAD */
2574 /* experimental code */
2575 slapd_daemon_task( NULL );
2576 #endif /* ! SLAPD_LISTENER_THREAD */
2584 #if defined( HAVE_WINSOCK2 )
2585 WORD wVersionRequested;
2589 wVersionRequested = MAKEWORD( 2, 0 );
2591 err = WSAStartup( wVersionRequested, &wsaData );
2593 /* Tell the user that we couldn't find a usable */
2598 /* Confirm that the WinSock DLL supports 2.0.*/
2599 /* Note that if the DLL supports versions greater */
2600 /* than 2.0 in addition to 2.0, it will still return */
2601 /* 2.0 in wVersion since that is the version we */
2604 if ( LOBYTE( wsaData.wVersion ) != 2 ||
2605 HIBYTE( wsaData.wVersion ) != 0 )
2607 /* Tell the user that we couldn't find a usable */
2613 /* The WinSock DLL is acceptable. Proceed. */
2614 #elif defined( HAVE_WINSOCK )
2616 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) return -1;
2617 #endif /* ! HAVE_WINSOCK2 && ! HAVE_WINSOCK */
2625 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
2627 #endif /* HAVE_WINSOCK2 || HAVE_WINSOCK */
2634 slap_sig_shutdown( int sig )
2637 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
2641 * If the NT Service Manager is controlling the server, we don't
2642 * want SIGBREAK to kill the server. For some strange reason,
2643 * SIGBREAK is generated when a user logs out.
2646 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
2647 if (is_NT_Service && sig == SIGBREAK) {
2650 #endif /* HAVE_NT_SERVICE_MANAGER && SIGBREAK */
2652 if (sig == SIGHUP && global_gentlehup && slapd_gentle_shutdown == 0) {
2653 slapd_gentle_shutdown = 1;
2662 /* reinstall self */
2663 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
2667 slap_sig_wake( int sig )
2671 /* reinstall self */
2672 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
2677 slapd_add_internal( ber_socket_t s, int isactive )
2679 slapd_add( s, isactive, NULL );
2683 slapd_get_listeners( void )
2685 return slap_listeners;
2689 slap_wake_listener()