2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2009 The OpenLDAP Foundation.
5 * Portions Copyright 2007 by Howard Chu, Symas Corporation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
33 #include <ac/socket.h>
34 #include <ac/string.h>
36 #include <ac/unistd.h>
39 #include "ldap_pvt_thread.h"
44 #if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL)
45 # include <sys/epoll.h>
46 #elif defined(SLAP_X_DEVPOLL) && defined(HAVE_SYS_DEVPOLL_H) && defined(HAVE_DEVPOLL)
47 # include <sys/types.h>
48 # include <sys/stat.h>
50 # include <sys/devpoll.h>
51 #endif /* ! epoll && ! /dev/poll */
54 int allow_severity = LOG_INFO;
55 int deny_severity = LOG_NOTICE;
56 #endif /* TCP Wrappers */
59 # include <sys/stat.h>
60 /* this should go in <ldap.h> as soon as it is accepted */
61 # define LDAPI_MOD_URLEXT "x-mod"
62 #endif /* LDAP_PF_LOCAL */
65 int slap_inet4or6 = AF_UNSPEC;
67 int slap_inet4or6 = AF_INET;
72 ber_socket_t dtblsize;
73 slap_ssf_t local_ssf = LDAP_PVT_SASL_LOCAL_SSF;
74 struct runqueue_s slapd_rq;
76 Listener **slap_listeners = NULL;
78 #ifndef SLAPD_LISTEN_BACKLOG
79 #define SLAPD_LISTEN_BACKLOG 1024
80 #endif /* ! SLAPD_LISTEN_BACKLOG */
82 static ber_socket_t wake_sds[2]
84 = { INVALID_SOCKET, INVALID_SOCKET }
85 #endif /* HAVE_WINSOCK */
89 static time_t chk_writetime;
91 static volatile int waking;
93 #define WAKE_LISTENER(w) do { \
94 if ((w) && ++waking < 5) { \
95 tcp_write( SLAP_FD2SOCK(wake_sds[1]), "0", 1 ); \
98 #else /* ! NO_THREADS */
99 #define WAKE_LISTENER(w) do { \
101 tcp_write( SLAP_FD2SOCK(wake_sds[1]), "0", 1 ); \
104 #endif /* ! NO_THREADS */
106 volatile sig_atomic_t slapd_shutdown = 0;
107 volatile sig_atomic_t slapd_gentle_shutdown = 0;
108 volatile sig_atomic_t slapd_abrupt_shutdown = 0;
111 ldap_pvt_thread_mutex_t slapd_ws_mutex;
112 SOCKET *slapd_ws_sockets;
116 #define SD_LISTENER 8
119 static struct slap_daemon {
120 ldap_pvt_thread_mutex_t sd_mutex;
122 ldap_pvt_thread_mutex_t sd_tcpd_mutex;
123 #endif /* TCP Wrappers */
125 ber_socket_t sd_nactives;
129 #if defined(HAVE_EPOLL)
130 struct epoll_event *sd_epolls;
133 #elif defined(SLAP_X_DEVPOLL) && defined(HAVE_DEVPOLL)
135 struct pollfd *sd_pollfd;
139 #else /* ! epoll && ! /dev/poll */
143 #else /* ! HAVE_WINSOCK */
147 #endif /* ! HAVE_WINSOCK */
148 #endif /* ! epoll && ! /dev/poll */
152 * NOTE: naming convention for macros:
154 * - SLAP_SOCK_* and SLAP_EVENT_* for public interface that deals
155 * with file descriptors and events respectively
157 * - SLAP_<type>_* for private interface; type by now is one of
158 * EPOLL, DEVPOLL, SELECT
160 * private interface should not be used in the code.
162 #if defined(HAVE_EPOLL)
163 /***************************************
164 * Use epoll infrastructure - epoll(4) *
165 ***************************************/
166 # define SLAP_EVENT_FNAME "epoll"
167 # define SLAP_EVENTS_ARE_INDEXED 0
168 # define SLAP_EPOLL_SOCK_IX(s) (slap_daemon.sd_index[(s)])
169 # define SLAP_EPOLL_SOCK_EP(s) (slap_daemon.sd_epolls[SLAP_EPOLL_SOCK_IX(s)])
170 # define SLAP_EPOLL_SOCK_EV(s) (SLAP_EPOLL_SOCK_EP(s).events)
171 # define SLAP_SOCK_IS_ACTIVE(s) (SLAP_EPOLL_SOCK_IX(s) != -1)
172 # define SLAP_SOCK_NOT_ACTIVE(s) (SLAP_EPOLL_SOCK_IX(s) == -1)
173 # define SLAP_EPOLL_SOCK_IS_SET(s, mode) (SLAP_EPOLL_SOCK_EV(s) & (mode))
175 # define SLAP_SOCK_IS_READ(s) SLAP_EPOLL_SOCK_IS_SET((s), EPOLLIN)
176 # define SLAP_SOCK_IS_WRITE(s) SLAP_EPOLL_SOCK_IS_SET((s), EPOLLOUT)
178 # define SLAP_EPOLL_SOCK_SET(s, mode) do { \
179 if ( (SLAP_EPOLL_SOCK_EV(s) & (mode)) != (mode) ) { \
180 SLAP_EPOLL_SOCK_EV(s) |= (mode); \
181 epoll_ctl( slap_daemon.sd_epfd, EPOLL_CTL_MOD, (s), \
182 &SLAP_EPOLL_SOCK_EP(s) ); \
186 # define SLAP_EPOLL_SOCK_CLR(s, mode) do { \
187 if ( (SLAP_EPOLL_SOCK_EV(s) & (mode)) ) { \
188 SLAP_EPOLL_SOCK_EV(s) &= ~(mode); \
189 epoll_ctl( slap_daemon.sd_epfd, EPOLL_CTL_MOD, s, \
190 &SLAP_EPOLL_SOCK_EP(s) ); \
194 # define SLAP_SOCK_SET_READ(s) SLAP_EPOLL_SOCK_SET(s, EPOLLIN)
195 # define SLAP_SOCK_SET_WRITE(s) SLAP_EPOLL_SOCK_SET(s, EPOLLOUT)
197 # define SLAP_SOCK_CLR_READ(s) SLAP_EPOLL_SOCK_CLR((s), EPOLLIN)
198 # define SLAP_SOCK_CLR_WRITE(s) SLAP_EPOLL_SOCK_CLR((s), EPOLLOUT)
200 # define SLAP_SOCK_SET_SUSPEND(s) \
201 ( slap_daemon.sd_suspend[SLAP_EPOLL_SOCK_IX(s)] = 1 )
202 # define SLAP_SOCK_CLR_SUSPEND(s) \
203 ( slap_daemon.sd_suspend[SLAP_EPOLL_SOCK_IX(s)] = 0 )
204 # define SLAP_SOCK_IS_SUSPEND(s) \
205 ( slap_daemon.sd_suspend[SLAP_EPOLL_SOCK_IX(s)] == 1 )
207 # define SLAP_EPOLL_EVENT_CLR(i, mode) (revents[(i)].events &= ~(mode))
209 # define SLAP_EVENT_MAX slap_daemon.sd_nfds
211 /* If a Listener address is provided, store that as the epoll data.
212 * Otherwise, store the address of this socket's slot in the
213 * index array. If we can't do this add, the system is out of
214 * resources and we need to shutdown.
216 # define SLAP_SOCK_ADD(s, l) do { \
218 SLAP_EPOLL_SOCK_IX((s)) = slap_daemon.sd_nfds; \
219 SLAP_EPOLL_SOCK_EP((s)).data.ptr = (l) ? (l) : (void *)(&SLAP_EPOLL_SOCK_IX(s)); \
220 SLAP_EPOLL_SOCK_EV((s)) = EPOLLIN; \
221 rc = epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_ADD, \
222 (s), &SLAP_EPOLL_SOCK_EP((s))); \
224 slap_daemon.sd_nfds++; \
226 Debug( LDAP_DEBUG_ANY, \
227 "daemon: epoll_ctl(ADD,fd=%d) failed, errno=%d, shutting down\n", \
229 slapd_shutdown = 2; \
233 # define SLAP_EPOLL_EV_LISTENER(ptr) \
234 (((int *)(ptr) >= slap_daemon.sd_index && \
235 (int *)(ptr) <= &slap_daemon.sd_index[dtblsize]) ? 0 : 1 )
237 # define SLAP_EPOLL_EV_PTRFD(ptr) (SLAP_EPOLL_EV_LISTENER(ptr) ? \
238 ((Listener *)ptr)->sl_sd : \
239 (ber_socket_t) ((int *)(ptr) - slap_daemon.sd_index))
241 # define SLAP_SOCK_DEL(s) do { \
242 int fd, rc, index = SLAP_EPOLL_SOCK_IX((s)); \
243 if ( index < 0 ) break; \
244 rc = epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_DEL, \
245 (s), &SLAP_EPOLL_SOCK_EP((s))); \
246 slap_daemon.sd_epolls[index] = \
247 slap_daemon.sd_epolls[slap_daemon.sd_nfds-1]; \
248 fd = SLAP_EPOLL_EV_PTRFD(slap_daemon.sd_epolls[index].data.ptr); \
249 slap_daemon.sd_index[fd] = index; \
250 slap_daemon.sd_index[(s)] = -1; \
251 slap_daemon.sd_nfds--; \
254 # define SLAP_EVENT_CLR_READ(i) SLAP_EPOLL_EVENT_CLR((i), EPOLLIN)
255 # define SLAP_EVENT_CLR_WRITE(i) SLAP_EPOLL_EVENT_CLR((i), EPOLLOUT)
257 # define SLAP_EPOLL_EVENT_CHK(i, mode) (revents[(i)].events & mode)
259 # define SLAP_EVENT_IS_READ(i) SLAP_EPOLL_EVENT_CHK((i), EPOLLIN)
260 # define SLAP_EVENT_IS_WRITE(i) SLAP_EPOLL_EVENT_CHK((i), EPOLLOUT)
261 # define SLAP_EVENT_IS_LISTENER(i) SLAP_EPOLL_EV_LISTENER(revents[(i)].data.ptr)
262 # define SLAP_EVENT_LISTENER(i) ((Listener *)(revents[(i)].data.ptr))
264 # define SLAP_EVENT_FD(i) SLAP_EPOLL_EV_PTRFD(revents[(i)].data.ptr)
266 # define SLAP_SOCK_INIT do { \
267 slap_daemon.sd_epolls = ch_calloc(1, \
268 ( sizeof(struct epoll_event) * 2 \
269 + sizeof(int) ) * dtblsize * 2); \
270 slap_daemon.sd_index = (int *)&slap_daemon.sd_epolls[ 2 * dtblsize ]; \
271 slap_daemon.sd_epfd = epoll_create( dtblsize ); \
272 for ( i = 0; i < dtblsize; i++ ) slap_daemon.sd_index[i] = -1; \
275 # define SLAP_SOCK_DESTROY do { \
276 if ( slap_daemon.sd_epolls != NULL ) { \
277 ch_free( slap_daemon.sd_epolls ); \
278 slap_daemon.sd_epolls = NULL; \
279 slap_daemon.sd_index = NULL; \
280 close( slap_daemon.sd_epfd ); \
284 # define SLAP_EVENT_DECL struct epoll_event *revents
286 # define SLAP_EVENT_INIT do { \
287 revents = slap_daemon.sd_epolls + dtblsize; \
290 # define SLAP_EVENT_WAIT(tvp, nsp) do { \
291 *(nsp) = epoll_wait( slap_daemon.sd_epfd, revents, \
292 dtblsize, (tvp) ? (tvp)->tv_sec * 1000 : -1 ); \
295 #elif defined(SLAP_X_DEVPOLL) && defined(HAVE_DEVPOLL)
297 /*************************************************************
298 * Use Solaris' (>= 2.7) /dev/poll infrastructure - poll(7d) *
299 *************************************************************/
300 # define SLAP_EVENT_FNAME "/dev/poll"
301 # define SLAP_EVENTS_ARE_INDEXED 0
303 * - sd_index is used much like with epoll()
304 * - sd_l is maintained as an array containing the address
305 * of the listener; the index is the fd itself
306 * - sd_pollfd is used to keep track of what data has been
307 * registered in /dev/poll
309 # define SLAP_DEVPOLL_SOCK_IX(s) (slap_daemon.sd_index[(s)])
310 # define SLAP_DEVPOLL_SOCK_LX(s) (slap_daemon.sd_l[(s)])
311 # define SLAP_DEVPOLL_SOCK_EP(s) (slap_daemon.sd_pollfd[SLAP_DEVPOLL_SOCK_IX((s))])
312 # define SLAP_DEVPOLL_SOCK_FD(s) (SLAP_DEVPOLL_SOCK_EP((s)).fd)
313 # define SLAP_DEVPOLL_SOCK_EV(s) (SLAP_DEVPOLL_SOCK_EP((s)).events)
314 # define SLAP_SOCK_IS_ACTIVE(s) (SLAP_DEVPOLL_SOCK_IX((s)) != -1)
315 # define SLAP_SOCK_NOT_ACTIVE(s) (SLAP_DEVPOLL_SOCK_IX((s)) == -1)
316 # define SLAP_SOCK_IS_SET(s, mode) (SLAP_DEVPOLL_SOCK_EV((s)) & (mode))
318 # define SLAP_SOCK_IS_READ(s) SLAP_SOCK_IS_SET((s), POLLIN)
319 # define SLAP_SOCK_IS_WRITE(s) SLAP_SOCK_IS_SET((s), POLLOUT)
321 /* as far as I understand, any time we need to communicate with the kernel
322 * about the number and/or properties of a file descriptor we need it to
323 * wait for, we have to rewrite the whole set */
324 # define SLAP_DEVPOLL_WRITE_POLLFD(s, pfd, n, what, shdn) do { \
326 size_t size = (n) * sizeof( struct pollfd ); \
327 /* FIXME: use pwrite? */ \
328 rc = write( slap_daemon.sd_dpfd, (pfd), size ); \
329 if ( rc != size ) { \
330 Debug( LDAP_DEBUG_ANY, "daemon: " SLAP_EVENT_FNAME ": " \
331 "%s fd=%d failed errno=%d\n", \
332 (what), (s), errno ); \
334 slapd_shutdown = 2; \
339 # define SLAP_DEVPOLL_SOCK_SET(s, mode) do { \
340 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_SET_%s(%d)=%d\n", \
341 (mode) == POLLIN ? "READ" : "WRITE", (s), \
342 ( (SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) != (mode) ) ); \
343 if ( (SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) != (mode) ) { \
345 SLAP_DEVPOLL_SOCK_EV((s)) |= (mode); \
346 pfd.fd = SLAP_DEVPOLL_SOCK_FD((s)); \
347 pfd.events = /* (mode) */ SLAP_DEVPOLL_SOCK_EV((s)); \
348 SLAP_DEVPOLL_WRITE_POLLFD((s), &pfd, 1, "SET", 0); \
352 # define SLAP_DEVPOLL_SOCK_CLR(s, mode) do { \
353 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_CLR_%s(%d)=%d\n", \
354 (mode) == POLLIN ? "READ" : "WRITE", (s), \
355 ( (SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) == (mode) ) ); \
356 if ((SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) == (mode) ) { \
357 struct pollfd pfd[2]; \
358 SLAP_DEVPOLL_SOCK_EV((s)) &= ~(mode); \
359 pfd[0].fd = SLAP_DEVPOLL_SOCK_FD((s)); \
360 pfd[0].events = POLLREMOVE; \
361 pfd[1] = SLAP_DEVPOLL_SOCK_EP((s)); \
362 SLAP_DEVPOLL_WRITE_POLLFD((s), &pfd[0], 2, "CLR", 0); \
366 # define SLAP_SOCK_SET_READ(s) SLAP_DEVPOLL_SOCK_SET(s, POLLIN)
367 # define SLAP_SOCK_SET_WRITE(s) SLAP_DEVPOLL_SOCK_SET(s, POLLOUT)
369 # define SLAP_SOCK_CLR_READ(s) SLAP_DEVPOLL_SOCK_CLR((s), POLLIN)
370 # define SLAP_SOCK_CLR_WRITE(s) SLAP_DEVPOLL_SOCK_CLR((s), POLLOUT)
372 # define SLAP_SOCK_SET_SUSPEND(s) \
373 ( slap_daemon.sd_suspend[SLAP_DEVPOLL_SOCK_IX((s))] = 1 )
374 # define SLAP_SOCK_CLR_SUSPEND(s) \
375 ( slap_daemon.sd_suspend[SLAP_DEVPOLL_SOCK_IX((s))] = 0 )
376 # define SLAP_SOCK_IS_SUSPEND(s) \
377 ( slap_daemon.sd_suspend[SLAP_DEVPOLL_SOCK_IX((s))] == 1 )
379 # define SLAP_DEVPOLL_EVENT_CLR(i, mode) (revents[(i)].events &= ~(mode))
381 # define SLAP_EVENT_MAX slap_daemon.sd_nfds
383 /* If a Listener address is provided, store that in the sd_l array.
384 * If we can't do this add, the system is out of resources and we
387 # define SLAP_SOCK_ADD(s, l) do { \
388 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_ADD(%d, %p)\n", (s), (l), 0 ); \
389 SLAP_DEVPOLL_SOCK_IX((s)) = slap_daemon.sd_nfds; \
390 SLAP_DEVPOLL_SOCK_LX((s)) = (l); \
391 SLAP_DEVPOLL_SOCK_FD((s)) = (s); \
392 SLAP_DEVPOLL_SOCK_EV((s)) = POLLIN; \
393 SLAP_DEVPOLL_WRITE_POLLFD((s), &SLAP_DEVPOLL_SOCK_EP((s)), 1, "ADD", 1); \
394 slap_daemon.sd_nfds++; \
397 # define SLAP_DEVPOLL_EV_LISTENER(ptr) ((ptr) != NULL)
399 # define SLAP_SOCK_DEL(s) do { \
400 int fd, index = SLAP_DEVPOLL_SOCK_IX((s)); \
401 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_DEL(%d)\n", (s), 0, 0 ); \
402 if ( index < 0 ) break; \
403 if ( index < slap_daemon.sd_nfds - 1 ) { \
404 struct pollfd pfd = slap_daemon.sd_pollfd[index]; \
405 fd = slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1].fd; \
406 slap_daemon.sd_pollfd[index] = slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1]; \
407 slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1] = pfd; \
408 slap_daemon.sd_index[fd] = index; \
410 slap_daemon.sd_index[(s)] = -1; \
411 slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1].events = POLLREMOVE; \
412 SLAP_DEVPOLL_WRITE_POLLFD((s), &slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1], 1, "DEL", 0); \
413 slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1].events = 0; \
414 slap_daemon.sd_nfds--; \
417 # define SLAP_EVENT_CLR_READ(i) SLAP_DEVPOLL_EVENT_CLR((i), POLLIN)
418 # define SLAP_EVENT_CLR_WRITE(i) SLAP_DEVPOLL_EVENT_CLR((i), POLLOUT)
420 # define SLAP_DEVPOLL_EVENT_CHK(i, mode) (revents[(i)].events & (mode))
422 # define SLAP_EVENT_FD(i) (revents[(i)].fd)
424 # define SLAP_EVENT_IS_READ(i) SLAP_DEVPOLL_EVENT_CHK((i), POLLIN)
425 # define SLAP_EVENT_IS_WRITE(i) SLAP_DEVPOLL_EVENT_CHK((i), POLLOUT)
426 # define SLAP_EVENT_IS_LISTENER(i) SLAP_DEVPOLL_EV_LISTENER(SLAP_DEVPOLL_SOCK_LX(SLAP_EVENT_FD((i))))
427 # define SLAP_EVENT_LISTENER(i) SLAP_DEVPOLL_SOCK_LX(SLAP_EVENT_FD((i)))
429 # define SLAP_SOCK_INIT do { \
430 slap_daemon.sd_pollfd = ch_calloc( 1, \
431 ( sizeof(struct pollfd) * 2 \
433 + sizeof( Listener * ) ) * dtblsize ); \
434 slap_daemon.sd_index = (int *)&slap_daemon.sd_pollfd[ 2 * dtblsize ]; \
435 slap_daemon.sd_l = (Listener **)&slap_daemon.sd_index[ dtblsize ]; \
436 slap_daemon.sd_dpfd = open( SLAP_EVENT_FNAME, O_RDWR ); \
437 if ( slap_daemon.sd_dpfd == -1 ) { \
438 Debug( LDAP_DEBUG_ANY, "daemon: " SLAP_EVENT_FNAME ": " \
439 "open(\"" SLAP_EVENT_FNAME "\") failed errno=%d\n", \
444 for ( i = 0; i < dtblsize; i++ ) { \
445 slap_daemon.sd_pollfd[i].fd = -1; \
446 slap_daemon.sd_index[i] = -1; \
450 # define SLAP_SOCK_DESTROY do { \
451 if ( slap_daemon.sd_pollfd != NULL ) { \
452 ch_free( slap_daemon.sd_pollfd ); \
453 slap_daemon.sd_pollfd = NULL; \
454 slap_daemon.sd_index = NULL; \
455 slap_daemon.sd_l = NULL; \
456 close( slap_daemon.sd_dpfd ); \
460 # define SLAP_EVENT_DECL struct pollfd *revents
462 # define SLAP_EVENT_INIT do { \
463 revents = &slap_daemon.sd_pollfd[ dtblsize ]; \
466 # define SLAP_EVENT_WAIT(tvp, nsp) do { \
467 struct dvpoll sd_dvpoll; \
468 sd_dvpoll.dp_timeout = (tvp) ? (tvp)->tv_sec * 1000 : -1; \
469 sd_dvpoll.dp_nfds = dtblsize; \
470 sd_dvpoll.dp_fds = revents; \
471 *(nsp) = ioctl( slap_daemon.sd_dpfd, DP_POLL, &sd_dvpoll ); \
474 #else /* ! epoll && ! /dev/poll */
476 # define SLAP_EVENT_FNAME "WSselect"
477 /* Winsock provides a "select" function but its fd_sets are
478 * actually arrays of sockets. Since these sockets are handles
479 * and not a contiguous range of small integers, we manage our
480 * own "fd" table of socket handles and use their indices as
483 * All of our listener/connection structures use fds; the actual
484 * I/O functions use sockets. The SLAP_FD2SOCK macro in proto-slap.h
485 * handles the mapping.
487 * Despite the mapping overhead, this is about 45% more efficient
488 * than just using Winsock's select and FD_ISSET directly.
490 * Unfortunately Winsock's select implementation doesn't scale well
491 * as the number of connections increases. This probably needs to be
492 * rewritten to use the Winsock overlapped/asynchronous I/O functions.
494 # define SLAP_EVENTS_ARE_INDEXED 1
495 # define SLAP_EVENT_DECL fd_set readfds, writefds
496 # define SLAP_EVENT_INIT do { \
498 FD_ZERO( &readfds ); \
499 FD_ZERO( &writefds ); \
500 memset( slap_daemon.sd_rflags, 0, slap_daemon.sd_nfds ); \
501 for ( i=0; i<slap_daemon.sd_nfds; i++ ) { \
502 if ( slap_daemon.sd_flags[i] & SD_READ ) \
503 FD_SET( slapd_ws_sockets[i], &readfds );\
504 if ( slap_daemon.sd_flags[i] & SD_WRITE ) \
505 FD_SET( slapd_ws_sockets[i], &writefds ); \
508 # define SLAP_EVENT_MAX slap_daemon.sd_nfds
510 # define SLAP_EVENT_WAIT(tvp, nsp) do { \
512 *(nsp) = select( SLAP_EVENT_MAX, &readfds, \
513 nwriters > 0 ? &writefds : NULL, NULL, (tvp) ); \
514 for ( i=0; i<readfds.fd_count; i++) { \
515 int fd = slapd_sock2fd(readfds.fd_array[i]); \
517 slap_daemon.sd_rflags[fd] = SD_READ; \
518 if ( fd >= *(nsp)) *(nsp) = fd+1; \
521 for ( i=0; i<writefds.fd_count; i++) { \
522 int fd = slapd_sock2fd(writefds.fd_array[i]); \
524 slap_daemon.sd_rflags[fd] = SD_WRITE; \
525 if ( fd >= *(nsp)) *(nsp) = fd+1; \
530 # define SLAP_EVENT_IS_READ(fd) (slap_daemon.sd_rflags[fd] & SD_READ)
531 # define SLAP_EVENT_IS_WRITE(fd) (slap_daemon.sd_rflags[fd] & SD_WRITE)
533 # define SLAP_EVENT_CLR_READ(fd) slap_daemon.sd_rflags[fd] &= ~SD_READ;
534 # define SLAP_EVENT_CLR_WRITE(fd) slap_daemon.sd_rflags[fd] &= ~SD_WRITE;
536 # define SLAP_SOCK_INIT do { \
537 ldap_pvt_thread_mutex_init( &slapd_ws_mutex ); \
538 slapd_ws_sockets = ch_malloc( dtblsize * ( sizeof(SOCKET) + 2)); \
539 slap_daemon.sd_flags = (char *)(slapd_ws_sockets + dtblsize); \
540 slap_daemon.sd_rflags = slap_daemon.sd_flags + dtblsize; \
541 memset( slapd_ws_sockets, -1, dtblsize * sizeof(SOCKET) ); \
542 slapd_ws_sockets[0] = wake_sds[0]; \
543 slapd_ws_sockets[1] = wake_sds[1]; \
546 slap_daemon.sd_nfds = 2; \
549 # define SLAP_SOCK_DESTROY do { \
550 ch_free( slapd_ws_sockets ); slapd_ws_sockets = NULL; \
551 slap_daemon.sd_flags = NULL; \
552 slap_daemon.sd_rflags = NULL; \
553 ldap_pvt_thread_mutex_destroy( &slapd_ws_mutex ); \
556 # define SLAP_SOCK_IS_ACTIVE(fd) ( slap_daemon.sd_flags[fd] & SD_ACTIVE )
557 # define SLAP_SOCK_IS_READ(fd) ( slap_daemon.sd_flags[fd] & SD_READ )
558 # define SLAP_SOCK_IS_WRITE(fd) ( slap_daemon.sd_flags[fd] & SD_WRITE )
559 # define SLAP_SOCK_NOT_ACTIVE(fd) (!slap_daemon.sd_flags[fd])
561 # define SLAP_SOCK_SET_READ(fd) ( slap_daemon.sd_flags[fd] |= SD_READ )
562 # define SLAP_SOCK_SET_WRITE(fd) ( slap_daemon.sd_flags[fd] |= SD_WRITE )
564 # define SLAP_SELECT_ADDTEST(s) do { \
565 if ((s) >= slap_daemon.sd_nfds) slap_daemon.sd_nfds = (s)+1; \
568 # define SLAP_SOCK_CLR_READ(fd) ( slap_daemon.sd_flags[fd] &= ~SD_READ )
569 # define SLAP_SOCK_CLR_WRITE(fd) ( slap_daemon.sd_flags[fd] &= ~SD_WRITE )
571 # define SLAP_SOCK_ADD(s, l) do { \
572 SLAP_SELECT_ADDTEST((s)); \
573 slap_daemon.sd_flags[s] = SD_ACTIVE|SD_READ; \
576 # define SLAP_SOCK_DEL(s) do { \
577 slap_daemon.sd_flags[s] = 0; \
578 slapd_sockdel( s ); \
581 # else /* !HAVE_WINSOCK */
583 /**************************************
584 * Use select system call - select(2) *
585 **************************************/
586 # define SLAP_EVENT_FNAME "select"
588 # define SLAP_EVENTS_ARE_INDEXED 1
589 # define SLAP_EVENT_DECL fd_set readfds, writefds
591 # define SLAP_EVENT_INIT do { \
592 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) ); \
594 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) ); \
596 FD_ZERO( &writefds ); \
601 # define SLAP_SELECT_CHK_SETSIZE do { \
602 if (dtblsize > FD_SETSIZE) dtblsize = FD_SETSIZE; \
604 # else /* ! FD_SETSIZE */
605 # define SLAP_SELECT_CHK_SETSIZE do { ; } while (0)
606 # endif /* ! FD_SETSIZE */
608 # define SLAP_SOCK_INIT do { \
609 SLAP_SELECT_CHK_SETSIZE; \
610 FD_ZERO(&slap_daemon.sd_actives); \
611 FD_ZERO(&slap_daemon.sd_readers); \
612 FD_ZERO(&slap_daemon.sd_writers); \
615 # define SLAP_SOCK_DESTROY
617 # define SLAP_SOCK_IS_ACTIVE(fd) FD_ISSET((fd), &slap_daemon.sd_actives)
618 # define SLAP_SOCK_IS_READ(fd) FD_ISSET((fd), &slap_daemon.sd_readers)
619 # define SLAP_SOCK_IS_WRITE(fd) FD_ISSET((fd), &slap_daemon.sd_writers)
621 # define SLAP_SOCK_NOT_ACTIVE(fd) (!SLAP_SOCK_IS_ACTIVE(fd) && \
622 !SLAP_SOCK_IS_READ(fd) && !SLAP_SOCK_IS_WRITE(fd))
624 # define SLAP_SOCK_SET_READ(fd) FD_SET((fd), &slap_daemon.sd_readers)
625 # define SLAP_SOCK_SET_WRITE(fd) FD_SET((fd), &slap_daemon.sd_writers)
627 # define SLAP_EVENT_MAX slap_daemon.sd_nfds
628 # define SLAP_SELECT_ADDTEST(s) do { \
629 if ((s) >= slap_daemon.sd_nfds) slap_daemon.sd_nfds = (s)+1; \
632 # define SLAP_SOCK_CLR_READ(fd) FD_CLR((fd), &slap_daemon.sd_readers)
633 # define SLAP_SOCK_CLR_WRITE(fd) FD_CLR((fd), &slap_daemon.sd_writers)
635 # define SLAP_SOCK_ADD(s, l) do { \
636 SLAP_SELECT_ADDTEST((s)); \
637 FD_SET((s), &slap_daemon.sd_actives); \
638 FD_SET((s), &slap_daemon.sd_readers); \
641 # define SLAP_SOCK_DEL(s) do { \
642 FD_CLR((s), &slap_daemon.sd_actives); \
643 FD_CLR((s), &slap_daemon.sd_readers); \
644 FD_CLR((s), &slap_daemon.sd_writers); \
647 # define SLAP_EVENT_IS_READ(fd) FD_ISSET((fd), &readfds)
648 # define SLAP_EVENT_IS_WRITE(fd) FD_ISSET((fd), &writefds)
650 # define SLAP_EVENT_CLR_READ(fd) FD_CLR((fd), &readfds)
651 # define SLAP_EVENT_CLR_WRITE(fd) FD_CLR((fd), &writefds)
653 # define SLAP_EVENT_WAIT(tvp, nsp) do { \
654 *(nsp) = select( SLAP_EVENT_MAX, &readfds, \
655 nwriters > 0 ? &writefds : NULL, NULL, (tvp) ); \
657 # endif /* !HAVE_WINSOCK */
658 #endif /* ! epoll && ! /dev/poll */
662 * SLP related functions
666 #define LDAP_SRVTYPE_PREFIX "service:ldap://"
667 #define LDAPS_SRVTYPE_PREFIX "service:ldaps://"
668 static char** slapd_srvurls = NULL;
669 static SLPHandle slapd_hslp = 0;
670 int slapd_register_slp = 0;
671 const char *slapd_slp_attrs = NULL;
673 static SLPError slapd_slp_cookie;
676 slapd_slp_init( const char* urls )
681 slapd_srvurls = ldap_str2charray( urls, " " );
683 if ( slapd_srvurls == NULL ) return;
685 /* find and expand INADDR_ANY URLs */
686 for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
687 if ( strcmp( slapd_srvurls[i], "ldap:///" ) == 0 ) {
688 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
689 strlen( global_host ) +
690 sizeof( LDAP_SRVTYPE_PREFIX ) );
691 strcpy( lutil_strcopy(slapd_srvurls[i],
692 LDAP_SRVTYPE_PREFIX ), global_host );
693 } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0 ) {
694 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
695 strlen( global_host ) +
696 sizeof( LDAPS_SRVTYPE_PREFIX ) );
697 strcpy( lutil_strcopy(slapd_srvurls[i],
698 LDAPS_SRVTYPE_PREFIX ), global_host );
702 /* open the SLP handle */
703 err = SLPOpen( "en", 0, &slapd_hslp );
705 if ( err != SLP_OK ) {
706 Debug( LDAP_DEBUG_CONNS, "daemon: SLPOpen() failed with %ld\n",
712 slapd_slp_deinit( void )
714 if ( slapd_srvurls == NULL ) return;
716 ldap_charray_free( slapd_srvurls );
717 slapd_srvurls = NULL;
719 /* close the SLP handle */
720 SLPClose( slapd_hslp );
729 /* return the error code in the cookie */
730 *(SLPError*)cookie = errcode;
739 if ( slapd_srvurls == NULL ) return;
741 for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
742 if ( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
743 sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
744 strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
745 sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
747 err = SLPReg( slapd_hslp,
749 SLP_LIFETIME_MAXIMUM,
751 (slapd_slp_attrs) ? slapd_slp_attrs : "",
756 if ( err != SLP_OK || slapd_slp_cookie != SLP_OK ) {
757 Debug( LDAP_DEBUG_CONNS,
758 "daemon: SLPReg(%s) failed with %ld, cookie = %ld\n",
759 slapd_srvurls[i], (long)err, (long)slapd_slp_cookie );
766 slapd_slp_dereg( void )
771 if ( slapd_srvurls == NULL ) return;
773 for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
774 err = SLPDereg( slapd_hslp,
779 if ( err != SLP_OK || slapd_slp_cookie != SLP_OK ) {
780 Debug( LDAP_DEBUG_CONNS,
781 "daemon: SLPDereg(%s) failed with %ld, cookie = %ld\n",
782 slapd_srvurls[i], (long)err, (long)slapd_slp_cookie );
786 #endif /* HAVE_SLP */
789 /* Manage the descriptor to socket table */
791 slapd_socknew( ber_socket_t s )
794 ldap_pvt_thread_mutex_lock( &slapd_ws_mutex );
795 for ( i = 0; i < dtblsize && slapd_ws_sockets[i] != INVALID_SOCKET; i++ );
796 if ( i == dtblsize ) {
797 WSASetLastError( WSAEMFILE );
799 slapd_ws_sockets[i] = s;
801 ldap_pvt_thread_mutex_unlock( &slapd_ws_mutex );
806 slapd_sockdel( ber_socket_t s )
808 ldap_pvt_thread_mutex_lock( &slapd_ws_mutex );
809 slapd_ws_sockets[s] = INVALID_SOCKET;
810 ldap_pvt_thread_mutex_unlock( &slapd_ws_mutex );
814 slapd_sock2fd( ber_socket_t s )
817 for ( i=0; i<dtblsize && slapd_ws_sockets[i] != s; i++);
825 * Add a descriptor to daemon control
827 * If isactive, the descriptor is a live server session and is subject
828 * to idletimeout control. Otherwise, the descriptor is a passive
829 * listener or an outbound client session, and not subject to
830 * idletimeout. The underlying event handler may record the Listener
831 * argument to differentiate Listener's from real sessions.
834 slapd_add( ber_socket_t s, int isactive, Listener *sl )
836 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
838 assert( SLAP_SOCK_NOT_ACTIVE(s) );
840 if ( isactive ) slap_daemon.sd_nactives++;
842 SLAP_SOCK_ADD(s, sl);
844 Debug( LDAP_DEBUG_CONNS, "daemon: added %ldr%s listener=%p\n",
845 (long) s, isactive ? " (active)" : "", (void *)sl );
847 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
856 slapd_sd_lock( void )
858 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
865 slapd_sd_unlock( void )
867 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
871 * Remove the descriptor from daemon control
885 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
887 assert( SLAP_SOCK_IS_ACTIVE( s ));
889 if ( wasactive ) slap_daemon.sd_nactives--;
891 waswriter = SLAP_SOCK_IS_WRITE(s);
892 wasreader = SLAP_SOCK_IS_READ(s);
894 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
896 wasreader ? "r" : "",
897 waswriter ? "w" : "" );
899 if ( waswriter ) slap_daemon.sd_nwriters--;
904 ber_sockbuf_free(sb);
906 /* If we ran out of file descriptors, we dropped a listener from
907 * the select() loop. Now that we're removing a session from our
908 * control, we can try to resume a dropped listener to use.
912 for ( i = 0; slap_listeners[i] != NULL; i++ ) {
913 Listener *lr = slap_listeners[i];
915 if ( lr->sl_sd == AC_SOCKET_INVALID ) continue;
916 if ( lr->sl_sd == s ) continue;
923 /* Walked the entire list without enabling anything; emfile
924 * counter is stale. Reset it.
926 if ( slap_listeners[i] == NULL ) emfile = 0;
928 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
929 WAKE_LISTENER(wake || slapd_gentle_shutdown == 2);
933 slapd_clr_write( ber_socket_t s, int wake )
935 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
937 if ( SLAP_SOCK_IS_WRITE( s )) {
938 assert( SLAP_SOCK_IS_ACTIVE( s ));
940 SLAP_SOCK_CLR_WRITE( s );
941 slap_daemon.sd_nwriters--;
944 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
949 slapd_set_write( ber_socket_t s, int wake )
951 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
953 assert( SLAP_SOCK_IS_ACTIVE( s ));
955 if ( !SLAP_SOCK_IS_WRITE( s )) {
956 SLAP_SOCK_SET_WRITE( s );
957 slap_daemon.sd_nwriters++;
959 if (( wake & 2 ) && global_writetimeout ) {
960 chk_writetime = slap_get_time();
963 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
968 slapd_clr_read( ber_socket_t s, int wake )
971 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
973 if ( SLAP_SOCK_IS_ACTIVE( s )) {
974 SLAP_SOCK_CLR_READ( s );
977 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
984 slapd_set_read( ber_socket_t s, int wake )
986 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
988 assert( SLAP_SOCK_IS_ACTIVE( s ));
989 if (!SLAP_SOCK_IS_READ( s )) SLAP_SOCK_SET_READ( s );
991 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
996 slapd_get_writetime()
999 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1000 cur = chk_writetime;
1001 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1006 slapd_clr_writetime( time_t old )
1008 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1009 if ( chk_writetime == old )
1011 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1015 slapd_close( ber_socket_t s )
1017 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
1019 tcp_close( SLAP_FD2SOCK(s) );
1026 slap_free_listener_addresses( struct sockaddr **sal )
1028 struct sockaddr **sap;
1029 if (sal == NULL) return;
1030 for (sap = sal; *sap != NULL; sap++) ch_free(*sap);
1034 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
1043 assert( exts != NULL );
1044 assert( perms != NULL );
1045 assert( crit != NULL );
1048 for ( i = 0; exts[ i ]; i++ ) {
1049 char *type = exts[ i ];
1052 if ( type[ 0 ] == '!' ) {
1057 if ( strncasecmp( type, LDAPI_MOD_URLEXT "=",
1058 sizeof(LDAPI_MOD_URLEXT "=") - 1 ) == 0 )
1060 char *value = type + ( sizeof(LDAPI_MOD_URLEXT "=") - 1 );
1064 switch (strlen(value)) {
1066 /* skip leading '0' */
1067 if ( value[ 0 ] != '0' ) return LDAP_OTHER;
1071 for ( j = 0; j < 3; j++) {
1074 v = value[ j ] - '0';
1076 if ( v < 0 || v > 7 ) return LDAP_OTHER;
1083 for ( j = 1; j < 10; j++ ) {
1084 static mode_t m[] = { 0,
1085 S_IRUSR, S_IWUSR, S_IXUSR,
1086 S_IRGRP, S_IWGRP, S_IXGRP,
1087 S_IROTH, S_IWOTH, S_IXOTH
1089 static const char c[] = "-rwxrwxrwx";
1091 if ( value[ j ] == c[ j ] ) {
1094 } else if ( value[ j ] != '-' ) {
1107 return LDAP_SUCCESS;
1113 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
1115 /* port = 0 indicates AF_LOCAL */
1117 slap_get_listener_addresses(
1119 unsigned short port,
1120 struct sockaddr ***sal )
1122 struct sockaddr **sap;
1124 #ifdef LDAP_PF_LOCAL
1126 *sal = ch_malloc(2 * sizeof(void *));
1127 if (*sal == NULL) return -1;
1130 *sap = ch_malloc(sizeof(struct sockaddr_un));
1131 if (*sap == NULL) goto errexit;
1135 (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) )
1137 Debug( LDAP_DEBUG_ANY,
1138 "daemon: domain socket path (%s) too long in URL",
1143 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
1144 (*sap)->sa_family = AF_LOCAL;
1145 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
1147 #endif /* LDAP_PF_LOCAL */
1149 #ifdef HAVE_GETADDRINFO
1150 struct addrinfo hints, *res, *sai;
1154 memset( &hints, '\0', sizeof(hints) );
1155 hints.ai_flags = AI_PASSIVE;
1156 hints.ai_socktype = SOCK_STREAM;
1157 hints.ai_family = slap_inet4or6;
1158 snprintf(serv, sizeof serv, "%d", port);
1160 if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
1161 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo() failed: %s\n",
1162 AC_GAI_STRERROR(err), 0, 0);
1167 for (n=2; (sai = sai->ai_next) != NULL; n++) {
1170 *sal = ch_calloc(n, sizeof(void *));
1171 if (*sal == NULL) return -1;
1176 for ( sai=res; sai; sai=sai->ai_next ) {
1177 if( sai->ai_addr == NULL ) {
1178 Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
1179 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
1184 switch (sai->ai_family) {
1185 # ifdef LDAP_PF_INET6
1187 *sap = ch_malloc(sizeof(struct sockaddr_in6));
1192 *(struct sockaddr_in6 *)*sap =
1193 *((struct sockaddr_in6 *)sai->ai_addr);
1195 # endif /* LDAP_PF_INET6 */
1197 *sap = ch_malloc(sizeof(struct sockaddr_in));
1202 *(struct sockaddr_in *)*sap =
1203 *((struct sockaddr_in *)sai->ai_addr);
1211 (*sap)->sa_family = sai->ai_family;
1219 #else /* ! HAVE_GETADDRINFO */
1222 struct hostent *he = NULL;
1224 if ( host == NULL ) {
1225 in.s_addr = htonl(INADDR_ANY);
1227 } else if ( !inet_aton( host, &in ) ) {
1228 he = gethostbyname( host );
1230 Debug( LDAP_DEBUG_ANY,
1231 "daemon: invalid host %s", host, 0, 0);
1234 for (n = 0; he->h_addr_list[n]; n++) /* empty */;
1237 *sal = ch_malloc((n+1) * sizeof(void *));
1238 if (*sal == NULL) return -1;
1241 for ( i = 0; i<n; i++ ) {
1242 sap[i] = ch_malloc(sizeof(struct sockaddr_in));
1243 if (*sap == NULL) goto errexit;
1245 (void)memset( (void *)sap[i], '\0', sizeof(struct sockaddr_in) );
1246 sap[i]->sa_family = AF_INET;
1247 ((struct sockaddr_in *)sap[i])->sin_port = htons(port);
1248 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr,
1249 he ? (struct in_addr *)he->h_addr_list[i] : &in,
1250 sizeof(struct in_addr) );
1253 #endif /* ! HAVE_GETADDRINFO */
1259 slap_free_listener_addresses(*sal);
1273 unsigned short port;
1274 int err, addrlen = 0;
1275 struct sockaddr **sal, **psal;
1276 int socktype = SOCK_STREAM; /* default to COTS */
1279 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
1284 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
1286 rc = ldap_url_parse( url, &lud );
1288 if( rc != LDAP_URL_SUCCESS ) {
1289 Debug( LDAP_DEBUG_ANY,
1290 "daemon: listen URL \"%s\" parse error=%d\n",
1295 l.sl_url.bv_val = NULL;
1300 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
1301 Debug( LDAP_DEBUG_ANY, "daemon: TLS not supported (%s)\n",
1303 ldap_free_urldesc( lud );
1307 if(! lud->lud_port ) lud->lud_port = LDAP_PORT;
1309 #else /* HAVE_TLS */
1310 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
1312 if(! lud->lud_port ) {
1313 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
1315 #endif /* HAVE_TLS */
1317 port = (unsigned short) lud->lud_port;
1319 tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme);
1320 if ( tmp == LDAP_PROTO_IPC ) {
1321 #ifdef LDAP_PF_LOCAL
1322 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
1323 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
1325 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
1327 #else /* ! LDAP_PF_LOCAL */
1329 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
1331 ldap_free_urldesc( lud );
1333 #endif /* ! LDAP_PF_LOCAL */
1335 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
1336 || strcmp(lud->lud_host, "*") == 0 )
1338 err = slap_get_listener_addresses(NULL, port, &sal);
1340 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
1344 #ifdef LDAP_CONNECTIONLESS
1345 l.sl_is_udp = ( tmp == LDAP_PROTO_UDP );
1346 #endif /* LDAP_CONNECTIONLESS */
1348 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
1349 if ( lud->lud_exts ) {
1350 err = get_url_perms( lud->lud_exts, &l.sl_perms, &crit );
1352 l.sl_perms = S_IRWXU | S_IRWXO;
1354 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
1356 ldap_free_urldesc( lud );
1357 if ( err ) return -1;
1359 /* If we got more than one address returned, we need to make space
1360 * for it in the slap_listeners array.
1362 for ( num=0; sal[num]; num++ ) /* empty */;
1364 *listeners += num-1;
1365 slap_listeners = ch_realloc( slap_listeners,
1366 (*listeners + 1) * sizeof(Listener *) );
1370 while ( *sal != NULL ) {
1372 switch( (*sal)->sa_family ) {
1376 #ifdef LDAP_PF_INET6
1380 #endif /* LDAP_PF_INET6 */
1381 #ifdef LDAP_PF_LOCAL
1385 #endif /* LDAP_PF_LOCAL */
1391 #ifdef LDAP_CONNECTIONLESS
1392 if( l.sl_is_udp ) socktype = SOCK_DGRAM;
1393 #endif /* LDAP_CONNECTIONLESS */
1395 s = socket( (*sal)->sa_family, socktype, 0);
1396 if ( s == AC_SOCKET_INVALID ) {
1397 int err = sock_errno();
1398 Debug( LDAP_DEBUG_ANY,
1399 "daemon: %s socket() failed errno=%d (%s)\n",
1400 af, err, sock_errstr(err) );
1404 l.sl_sd = SLAP_SOCKNEW( s );
1406 if ( l.sl_sd >= dtblsize ) {
1407 Debug( LDAP_DEBUG_ANY,
1408 "daemon: listener descriptor %ld is too great %ld\n",
1409 (long) l.sl_sd, (long) dtblsize, 0 );
1415 #ifdef LDAP_PF_LOCAL
1416 if ( (*sal)->sa_family == AF_LOCAL ) {
1417 unlink( ((struct sockaddr_un *)*sal)->sun_path );
1419 #endif /* LDAP_PF_LOCAL */
1422 /* enable address reuse */
1424 rc = setsockopt( s, SOL_SOCKET, SO_REUSEADDR,
1425 (char *) &tmp, sizeof(tmp) );
1426 if ( rc == AC_SOCKET_ERROR ) {
1427 int err = sock_errno();
1428 Debug( LDAP_DEBUG_ANY, "slapd(%ld): "
1429 "setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
1430 (long) l.sl_sd, err, sock_errstr(err) );
1432 #endif /* SO_REUSEADDR */
1435 switch( (*sal)->sa_family ) {
1437 addrlen = sizeof(struct sockaddr_in);
1439 #ifdef LDAP_PF_INET6
1442 /* Try to use IPv6 sockets for IPv6 only */
1444 rc = setsockopt( s , IPPROTO_IPV6, IPV6_V6ONLY,
1445 (char *) &tmp, sizeof(tmp) );
1446 if ( rc == AC_SOCKET_ERROR ) {
1447 int err = sock_errno();
1448 Debug( LDAP_DEBUG_ANY, "slapd(%ld): "
1449 "setsockopt(IPV6_V6ONLY) failed errno=%d (%s)\n",
1450 (long) l.sl_sd, err, sock_errstr(err) );
1452 #endif /* IPV6_V6ONLY */
1453 addrlen = sizeof(struct sockaddr_in6);
1455 #endif /* LDAP_PF_INET6 */
1457 #ifdef LDAP_PF_LOCAL
1462 setsockopt( s, 0, LOCAL_CREDS, &one, sizeof( one ) );
1464 #endif /* LOCAL_CREDS */
1466 addrlen = sizeof( struct sockaddr_un );
1468 #endif /* LDAP_PF_LOCAL */
1471 #ifdef LDAP_PF_LOCAL
1472 /* create socket with all permissions set for those systems
1473 * that honor permissions on sockets (e.g. Linux); typically,
1474 * only write is required. To exploit filesystem permissions,
1475 * place the socket in a directory and use directory's
1476 * permissions. Need write perms to the directory to
1477 * create/unlink the socket; likely need exec perms to access
1478 * the socket (ITS#4709) */
1482 if ( (*sal)->sa_family == AF_LOCAL ) {
1483 old_umask = umask( 0 );
1485 #endif /* LDAP_PF_LOCAL */
1486 rc = bind( s, *sal, addrlen );
1487 #ifdef LDAP_PF_LOCAL
1488 if ( (*sal)->sa_family == AF_LOCAL ) {
1492 #endif /* LDAP_PF_LOCAL */
1495 Debug( LDAP_DEBUG_ANY,
1496 "daemon: bind(%ld) failed errno=%d (%s)\n",
1497 (long)l.sl_sd, err, sock_errstr( err ) );
1503 switch ( (*sal)->sa_family ) {
1504 #ifdef LDAP_PF_LOCAL
1506 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
1507 l.sl_name.bv_len = strlen(addr) + sizeof("PATH=") - 1;
1508 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len + 1 );
1509 snprintf( l.sl_name.bv_val, l.sl_name.bv_len + 1,
1512 #endif /* LDAP_PF_LOCAL */
1516 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
1517 char addr[INET_ADDRSTRLEN];
1518 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
1519 addr, sizeof(addr) );
1521 #else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1522 s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr );
1523 #endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1524 port = ntohs( ((struct sockaddr_in *)*sal) ->sin_port );
1526 ber_memalloc( sizeof("IP=255.255.255.255:65535") );
1527 snprintf( l.sl_name.bv_val, sizeof("IP=255.255.255.255:65535"),
1529 s != NULL ? s : SLAP_STRING_UNKNOWN, port );
1530 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1533 #ifdef LDAP_PF_INET6
1535 char addr[INET6_ADDRSTRLEN];
1536 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
1538 port = ntohs( ((struct sockaddr_in6 *)*sal)->sin6_port );
1539 l.sl_name.bv_len = strlen(addr) + sizeof("IP=[]:65535");
1540 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len );
1541 snprintf( l.sl_name.bv_val, l.sl_name.bv_len, "IP=[%s]:%d",
1543 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1545 #endif /* LDAP_PF_INET6 */
1548 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
1549 (int) (*sal)->sa_family, 0, 0 );
1553 AC_MEMCPY(&l.sl_sa, *sal, addrlen);
1554 ber_str2bv( url, 0, 1, &l.sl_url);
1555 li = ch_malloc( sizeof( Listener ) );
1557 slap_listeners[*cur] = li;
1562 slap_free_listener_addresses(psal);
1564 if ( l.sl_url.bv_val == NULL ) {
1565 Debug( LDAP_DEBUG_TRACE,
1566 "slap_open_listener: failed on %s\n", url, 0, 0 );
1570 Debug( LDAP_DEBUG_TRACE, "daemon: listener initialized %s\n",
1571 l.sl_url.bv_val, 0, 0 );
1575 static int sockinit(void);
1576 static int sockdestroy(void);
1579 slapd_daemon_init( const char *urls )
1584 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
1585 urls ? urls : "<null>", 0, 0 );
1587 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
1589 ldap_pvt_thread_mutex_init( &slap_daemon.sd_tcpd_mutex );
1590 #endif /* TCP Wrappers */
1592 if( (rc = sockinit()) != 0 ) return rc;
1595 dtblsize = sysconf( _SC_OPEN_MAX );
1596 #elif defined(HAVE_GETDTABLESIZE)
1597 dtblsize = getdtablesize();
1598 #else /* ! HAVE_SYSCONF && ! HAVE_GETDTABLESIZE */
1599 dtblsize = FD_SETSIZE;
1600 #endif /* ! HAVE_SYSCONF && ! HAVE_GETDTABLESIZE */
1602 /* open a pipe (or something equivalent connected to itself).
1603 * we write a byte on this fd whenever we catch a signal. The main
1604 * loop will be select'ing on this socket, and will wake up when
1605 * this byte arrives.
1607 if( (rc = lutil_pair( wake_sds )) < 0 ) {
1608 Debug( LDAP_DEBUG_ANY,
1609 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
1615 if( urls == NULL ) urls = "ldap:///";
1617 u = ldap_str2charray( urls, " " );
1619 if( u == NULL || u[0] == NULL ) {
1620 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
1623 ldap_charray_free( u );
1627 for( i=0; u[i] != NULL; i++ ) {
1628 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
1633 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
1635 ldap_charray_free( u );
1639 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
1641 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
1643 for(n = 0, j = 0; u[n]; n++ ) {
1644 if ( slap_open_listener( u[n], &i, &j ) ) {
1645 ldap_charray_free( u );
1649 slap_listeners[j] = NULL;
1651 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
1656 if( slapd_register_slp ) {
1657 slapd_slp_init( urls );
1660 #endif /* HAVE_SLP */
1662 ldap_charray_free( u );
1669 slapd_daemon_destroy( void )
1671 connections_destroy();
1673 if ( wake_sds[1] != INVALID_SOCKET && wake_sds[1] != wake_sds[0] )
1674 #endif /* HAVE_WINSOCK */
1675 tcp_close( SLAP_FD2SOCK(wake_sds[1]) );
1677 if ( wake_sds[0] != INVALID_SOCKET )
1678 #endif /* HAVE_WINSOCK */
1679 tcp_close( SLAP_FD2SOCK(wake_sds[0]) );
1683 if( slapd_register_slp ) {
1687 #endif /* HAVE_SLP */
1690 ldap_pvt_thread_mutex_destroy( &slap_daemon.sd_tcpd_mutex );
1691 #endif /* TCP Wrappers */
1693 ldap_pvt_thread_mutex_destroy( &slap_daemon.sd_mutex );
1704 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1705 Listener *lr = slap_listeners[l];
1707 if ( lr->sl_sd != AC_SOCKET_INVALID ) {
1708 if ( remove ) slapd_remove( lr->sl_sd, NULL, 0, 0, 0 );
1710 #ifdef LDAP_PF_LOCAL
1711 if ( lr->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1712 unlink( lr->sl_sa.sa_un_addr.sun_path );
1714 #endif /* LDAP_PF_LOCAL */
1716 slapd_close( lr->sl_sd );
1719 if ( lr->sl_url.bv_val ) {
1720 ber_memfree( lr->sl_url.bv_val );
1723 if ( lr->sl_name.bv_val ) {
1724 ber_memfree( lr->sl_name.bv_val );
1728 slap_listeners[l] = NULL;
1738 ber_socket_t s, sfd;
1739 ber_socklen_t len = sizeof(from);
1742 struct berval authid = BER_BVNULL;
1743 #ifdef SLAPD_RLOOKUPS
1744 char hbuf[NI_MAXHOST];
1745 #endif /* SLAPD_RLOOKUPS */
1747 char *dnsname = NULL;
1748 char *peeraddr = NULL;
1749 #ifdef LDAP_PF_LOCAL
1750 char peername[MAXPATHLEN + sizeof("PATH=")];
1751 #ifdef LDAP_PF_LOCAL_SENDMSG
1753 struct berval peerbv = BER_BVNULL;
1755 #elif defined(LDAP_PF_INET6)
1756 char peername[sizeof("IP=[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535")];
1757 #else /* ! LDAP_PF_LOCAL && ! LDAP_PF_INET6 */
1758 char peername[sizeof("IP=255.255.255.255:65336")];
1759 #endif /* LDAP_PF_LOCAL */
1762 Debug( LDAP_DEBUG_TRACE,
1763 ">>> slap_listener(%s)\n",
1764 sl->sl_url.bv_val, 0, 0 );
1768 #ifdef LDAP_CONNECTIONLESS
1769 if ( sl->sl_is_udp ) return 1;
1770 #endif /* LDAP_CONNECTIONLESS */
1772 # ifdef LDAP_PF_LOCAL
1773 /* FIXME: apparently accept doesn't fill
1774 * the sun_path sun_path member */
1775 from.sa_un_addr.sun_path[0] = '\0';
1776 # endif /* LDAP_PF_LOCAL */
1778 s = accept( SLAP_FD2SOCK( sl->sl_sd ), (struct sockaddr *) &from, &len );
1780 /* Resume the listener FD to allow concurrent-processing of
1781 * additional incoming connections.
1786 if ( s == AC_SOCKET_INVALID ) {
1787 int err = sock_errno();
1798 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1800 /* Stop listening until an existing session closes */
1802 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1805 Debug( LDAP_DEBUG_ANY,
1806 "daemon: accept(%ld) failed errno=%d (%s)\n",
1807 (long) sl->sl_sd, err, sock_errstr(err) );
1808 ldap_pvt_thread_yield();
1811 sfd = SLAP_SOCKNEW( s );
1813 /* make sure descriptor number isn't too great */
1814 if ( sfd >= dtblsize ) {
1815 Debug( LDAP_DEBUG_ANY,
1816 "daemon: %ld beyond descriptor table size %ld\n",
1817 (long) sfd, (long) dtblsize, 0 );
1820 ldap_pvt_thread_yield();
1825 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1826 /* newly accepted stream should not be in any of the FD SETS */
1827 assert( SLAP_SOCK_NOT_ACTIVE( sfd ));
1828 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1829 #endif /* LDAP_DEBUG */
1831 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1832 #ifdef LDAP_PF_LOCAL
1833 /* for IPv4 and IPv6 sockets only */
1834 if ( from.sa_addr.sa_family != AF_LOCAL )
1835 #endif /* LDAP_PF_LOCAL */
1840 /* enable keep alives */
1842 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1843 (char *) &tmp, sizeof(tmp) );
1844 if ( rc == AC_SOCKET_ERROR ) {
1845 int err = sock_errno();
1846 Debug( LDAP_DEBUG_ANY,
1847 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1848 "errno=%d (%s)\n", (long) sfd, err, sock_errstr(err) );
1850 #endif /* SO_KEEPALIVE */
1852 /* enable no delay */
1854 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1855 (char *)&tmp, sizeof(tmp) );
1856 if ( rc == AC_SOCKET_ERROR ) {
1857 int err = sock_errno();
1858 Debug( LDAP_DEBUG_ANY,
1859 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1860 "errno=%d (%s)\n", (long) sfd, err, sock_errstr(err) );
1862 #endif /* TCP_NODELAY */
1864 #endif /* SO_KEEPALIVE || TCP_NODELAY */
1866 Debug( LDAP_DEBUG_CONNS,
1867 "daemon: listen=%ld, new connection on %ld\n",
1868 (long) sl->sl_sd, (long) sfd, 0 );
1871 switch ( from.sa_addr.sa_family ) {
1872 # ifdef LDAP_PF_LOCAL
1874 cflag |= CONN_IS_IPC;
1876 /* FIXME: apparently accept doesn't fill
1877 * the sun_path sun_path member */
1878 if ( from.sa_un_addr.sun_path[0] == '\0' ) {
1879 AC_MEMCPY( from.sa_un_addr.sun_path,
1880 sl->sl_sa.sa_un_addr.sun_path,
1881 sizeof( from.sa_un_addr.sun_path ) );
1884 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1890 #ifdef LDAP_PF_LOCAL_SENDMSG
1891 peerbv.bv_val = peerbuf;
1892 peerbv.bv_len = sizeof( peerbuf );
1894 if( LUTIL_GETPEEREID( s, &uid, &gid, &peerbv ) == 0 ) {
1895 authid.bv_val = ch_malloc(
1896 STRLENOF( "gidNumber=4294967295+uidNumber=4294967295,"
1897 "cn=peercred,cn=external,cn=auth" ) + 1 );
1898 authid.bv_len = sprintf( authid.bv_val,
1899 "gidNumber=%d+uidNumber=%d,"
1900 "cn=peercred,cn=external,cn=auth",
1901 (int) gid, (int) uid );
1902 assert( authid.bv_len <=
1903 STRLENOF( "gidNumber=4294967295+uidNumber=4294967295,"
1904 "cn=peercred,cn=external,cn=auth" ) );
1909 #endif /* LDAP_PF_LOCAL */
1911 # ifdef LDAP_PF_INET6
1913 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1914 peeraddr = inet_ntoa( *((struct in_addr *)
1915 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1916 sprintf( peername, "IP=%s:%d",
1917 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1918 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1920 char addr[INET6_ADDRSTRLEN];
1922 peeraddr = (char *) inet_ntop( AF_INET6,
1923 &from.sa_in6_addr.sin6_addr,
1924 addr, sizeof addr );
1925 sprintf( peername, "IP=[%s]:%d",
1926 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1927 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1930 # endif /* LDAP_PF_INET6 */
1933 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1934 sprintf( peername, "IP=%s:%d",
1935 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1936 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1944 if ( ( from.sa_addr.sa_family == AF_INET )
1945 #ifdef LDAP_PF_INET6
1946 || ( from.sa_addr.sa_family == AF_INET6 )
1947 #endif /* LDAP_PF_INET6 */
1951 #ifdef SLAPD_RLOOKUPS
1952 if ( use_reverse_lookup ) {
1954 if (ldap_pvt_get_hname( (const struct sockaddr *)&from, len, hbuf,
1955 sizeof(hbuf), &herr ) == 0) {
1956 ldap_pvt_str2lower( hbuf );
1960 #endif /* SLAPD_RLOOKUPS */
1965 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_tcpd_mutex );
1966 rc = hosts_ctl("slapd",
1967 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1968 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1969 SLAP_STRING_UNKNOWN );
1970 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_tcpd_mutex );
1973 Statslog( LDAP_DEBUG_STATS,
1974 "fd=%ld DENIED from %s (%s)\n",
1976 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1977 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1983 #endif /* HAVE_TCPD */
1987 if ( sl->sl_is_tls ) cflag |= CONN_IS_TLS;
1989 c = connection_init(sfd, sl,
1990 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1991 peername, cflag, ssf,
1992 authid.bv_val ? &authid : NULL
1993 LDAP_PF_LOCAL_SENDMSG_ARG(&peerbv));
1995 if( authid.bv_val ) ch_free(authid.bv_val);
1998 Debug( LDAP_DEBUG_ANY,
1999 "daemon: connection_init(%ld, %s, %s) failed.\n",
2000 (long) sfd, peername, sl->sl_name.bv_val );
2005 Statslog( LDAP_DEBUG_STATS,
2006 "conn=%ld fd=%ld ACCEPT from %s (%s)\n",
2007 c->c_connid, (long) sfd, peername, sl->sl_name.bv_val,
2014 slap_listener_thread(
2019 Listener *sl = (Listener *)ptr;
2021 rc = slap_listener( sl );
2023 if( rc != LDAP_SUCCESS ) {
2024 Debug( LDAP_DEBUG_ANY,
2025 "slap_listener_thread(%s): failed err=%d",
2026 sl->sl_url.bv_val, rc, 0 );
2033 slap_listener_activate(
2038 Debug( LDAP_DEBUG_TRACE, "slap_listener_activate(%d): %s\n",
2039 sl->sl_sd, sl->sl_busy ? "busy" : "", 0 );
2043 rc = ldap_pvt_thread_pool_submit( &connection_pool,
2044 slap_listener_thread, (void *) sl );
2047 Debug( LDAP_DEBUG_ANY,
2048 "listener_activate(%d): submit failed (%d)\n",
2059 time_t last_idle_check = 0;
2062 #define SLAPD_IDLE_CHECK_LIMIT 4
2064 if ( global_idletimeout > 0 ) {
2065 last_idle_check = slap_get_time();
2068 slapd_add( wake_sds[0], 0, NULL );
2070 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2071 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ) continue;
2073 #ifdef LDAP_CONNECTIONLESS
2074 /* Since this is connectionless, the data port is the
2075 * listening port. The listen() and accept() calls
2078 if ( slap_listeners[l]->sl_is_udp )
2080 #endif /* LDAP_CONNECTIONLESS */
2082 if ( listen( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ), SLAPD_LISTEN_BACKLOG ) == -1 ) {
2083 int err = sock_errno();
2085 #ifdef LDAP_PF_INET6
2086 /* If error is EADDRINUSE, we are trying to listen to INADDR_ANY and
2087 * we are already listening to in6addr_any, then we want to ignore
2088 * this and continue.
2090 if ( err == EADDRINUSE ) {
2092 struct sockaddr_in sa = slap_listeners[l]->sl_sa.sa_in_addr;
2093 struct sockaddr_in6 sa6;
2095 if ( sa.sin_family == AF_INET &&
2096 sa.sin_addr.s_addr == htonl(INADDR_ANY) ) {
2097 for ( i = 0 ; i < l; i++ ) {
2098 sa6 = slap_listeners[i]->sl_sa.sa_in6_addr;
2099 if ( sa6.sin6_family == AF_INET6 &&
2100 !memcmp( &sa6.sin6_addr, &in6addr_any,
2101 sizeof(struct in6_addr) ) )
2108 /* We are already listening to in6addr_any */
2109 Debug( LDAP_DEBUG_CONNS,
2110 "daemon: Attempt to listen to 0.0.0.0 failed, "
2111 "already listening on ::, assuming IPv4 included\n",
2113 slapd_close( slap_listeners[l]->sl_sd );
2114 slap_listeners[l]->sl_sd = AC_SOCKET_INVALID;
2119 #endif /* LDAP_PF_INET6 */
2120 Debug( LDAP_DEBUG_ANY,
2121 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
2122 slap_listeners[l]->sl_url.bv_val, err,
2127 /* make the listening socket non-blocking */
2128 if ( ber_pvt_socket_set_nonblock( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ), 1 ) < 0 ) {
2129 Debug( LDAP_DEBUG_ANY, "slapd_daemon_task: "
2130 "set nonblocking on a listening socket failed\n",
2136 slapd_add( slap_listeners[l]->sl_sd, 0, slap_listeners[l] );
2139 #ifdef HAVE_NT_SERVICE_MANAGER
2140 if ( started_event != NULL ) {
2141 ldap_pvt_thread_cond_signal( &started_event );
2143 #endif /* HAVE_NT_SERVICE_MANAGER */
2145 /* initialization complete. Here comes the loop. */
2147 while ( !slapd_shutdown ) {
2152 #if SLAP_EVENTS_ARE_INDEXED
2153 ber_socket_t nrfds, nwfds;
2154 #endif /* SLAP_EVENTS_ARE_INDEXED */
2155 #define SLAPD_EBADF_LIMIT 16
2162 struct timeval *tvp;
2168 now = slap_get_time();
2170 if ( global_idletimeout > 0 || chk_writetime ) {
2172 /* Set the select timeout.
2173 * Don't just truncate, preserve the fractions of
2174 * seconds to prevent sleeping for zero time.
2176 if ( chk_writetime ) {
2177 tv.tv_sec = global_writetimeout;
2178 tv.tv_usec = global_writetimeout;
2179 if ( difftime( chk_writetime, now ) < 0 )
2182 tv.tv_sec = global_idletimeout / SLAPD_IDLE_CHECK_LIMIT;
2183 tv.tv_usec = global_idletimeout - \
2184 ( tv.tv_sec * SLAPD_IDLE_CHECK_LIMIT );
2185 tv.tv_usec *= 1000000 / SLAPD_IDLE_CHECK_LIMIT;
2186 if ( difftime( last_idle_check +
2187 global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 )
2191 connections_timeout_idle( now );
2192 last_idle_check = now;
2200 if ( slapd_gentle_shutdown ) {
2201 ber_socket_t active;
2203 if ( slapd_gentle_shutdown == 1 ) {
2205 Debug( LDAP_DEBUG_ANY, "slapd gentle shutdown\n", 0, 0, 0 );
2206 close_listeners( 1 );
2207 frontendDB->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
2208 LDAP_STAILQ_FOREACH(be, &backendDB, be_next) {
2209 be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
2211 slapd_gentle_shutdown = 2;
2214 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
2215 active = slap_daemon.sd_nactives;
2216 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
2217 if ( active == 0 ) {
2225 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
2227 nwriters = slap_daemon.sd_nwriters;
2229 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2230 Listener *lr = slap_listeners[l];
2232 if ( lr->sl_sd == AC_SOCKET_INVALID ) continue;
2234 if ( lr->sl_mute || lr->sl_busy )
2236 SLAP_SOCK_CLR_READ( lr->sl_sd );
2238 SLAP_SOCK_SET_READ( lr->sl_sd );
2244 nfds = SLAP_EVENT_MAX;
2246 if ( global_idletimeout && slap_daemon.sd_nactives ) at = 1;
2248 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
2251 #if defined(HAVE_YIELDING_SELECT) || defined(NO_THREADS)
2252 && ( tv.tv_sec || tv.tv_usec )
2253 #endif /* HAVE_YIELDING_SELECT || NO_THREADS */
2261 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2262 rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
2263 while ( rtask && cat.tv_sec && cat.tv_sec <= now ) {
2264 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
2265 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
2267 ldap_pvt_runqueue_runtask( &slapd_rq, rtask );
2268 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
2269 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2270 ldap_pvt_thread_pool_submit( &connection_pool,
2271 rtask->routine, (void *) rtask );
2272 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2274 rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
2276 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2278 if ( rtask && cat.tv_sec ) {
2279 /* NOTE: diff __should__ always be >= 0,
2280 * AFAI understand; however (ITS#4872),
2281 * time_t might be unsigned in some systems,
2282 * while difftime() returns a double */
2283 double diff = difftime( cat.tv_sec, now );
2287 if ( tvp == NULL || diff < tv.tv_sec ) {
2294 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2295 Listener *lr = slap_listeners[l];
2297 if ( lr->sl_sd == AC_SOCKET_INVALID ) {
2301 if ( lr->sl_mute ) {
2302 Debug( LDAP_DEBUG_CONNS,
2303 "daemon: " SLAP_EVENT_FNAME ": "
2304 "listen=%d muted\n",
2309 if ( lr->sl_busy ) {
2310 Debug( LDAP_DEBUG_CONNS,
2311 "daemon: " SLAP_EVENT_FNAME ": "
2317 Debug( LDAP_DEBUG_CONNS,
2318 "daemon: " SLAP_EVENT_FNAME ": "
2319 "listen=%d active_threads=%d tvp=%s\n",
2320 lr->sl_sd, at, tvp == NULL ? "NULL" : "zero" );
2323 SLAP_EVENT_WAIT( tvp, &ns );
2325 case -1: { /* failure - try again */
2326 int err = sock_errno();
2328 if ( err != EINTR ) {
2331 /* Don't log unless we got it twice in a row */
2332 if ( !( ebadf & 1 ) ) {
2333 Debug( LDAP_DEBUG_ANY,
2339 sock_errstr( err ) );
2341 if ( ebadf >= SLAPD_EBADF_LIMIT ) {
2348 case 0: /* timeout - let threads run */
2350 #ifndef HAVE_YIELDING_SELECT
2351 Debug( LDAP_DEBUG_CONNS, "daemon: " SLAP_EVENT_FNAME
2352 "timeout - yielding\n",
2355 ldap_pvt_thread_yield();
2356 #endif /* ! HAVE_YIELDING_SELECT */
2359 default: /* something happened - deal with it */
2360 if ( slapd_shutdown ) continue;
2363 Debug( LDAP_DEBUG_CONNS,
2364 "daemon: activity on %d descriptor%s\n",
2365 ns, ns != 1 ? "s" : "", 0 );
2369 #if SLAP_EVENTS_ARE_INDEXED
2370 if ( SLAP_EVENT_IS_READ( wake_sds[0] ) ) {
2372 SLAP_EVENT_CLR_READ( wake_sds[0] );
2374 tcp_read( SLAP_FD2SOCK(wake_sds[0]), c, sizeof(c) );
2375 Debug( LDAP_DEBUG_CONNS, "daemon: waked\n", 0, 0, 0 );
2379 /* The event slot equals the descriptor number - this is
2380 * true for Unix select and poll. We treat Windows select
2381 * like this too, even though it's a kludge.
2383 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2386 if ( ns <= 0 ) break;
2387 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ) continue;
2388 #ifdef LDAP_CONNECTIONLESS
2389 if ( slap_listeners[l]->sl_is_udp ) continue;
2390 #endif /* LDAP_CONNECTIONLESS */
2391 if ( !SLAP_EVENT_IS_READ( slap_listeners[l]->sl_sd ) ) continue;
2394 SLAP_EVENT_CLR_READ( slap_listeners[l]->sl_sd );
2395 SLAP_EVENT_CLR_WRITE( slap_listeners[l]->sl_sd );
2398 rc = slap_listener_activate( slap_listeners[l] );
2401 /* bypass the following tests if no descriptors left */
2403 #ifndef HAVE_YIELDING_SELECT
2404 ldap_pvt_thread_yield();
2405 #endif /* HAVE_YIELDING_SELECT */
2409 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
2412 for ( i = 0; i < nfds; i++ ) {
2415 r = SLAP_EVENT_IS_READ( i );
2416 /* writefds was not initialized if nwriters was zero */
2417 w = nwriters ? SLAP_EVENT_IS_WRITE( i ) : 0;
2419 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
2420 r ? "r" : "", w ? "w" : "" );
2430 if ( ns <= 0 ) break;
2432 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
2434 /* loop through the writers */
2435 for ( i = 0; nwfds > 0; i++ ) {
2437 if ( ! SLAP_EVENT_IS_WRITE( i ) ) continue;
2440 SLAP_EVENT_CLR_WRITE( wd );
2443 Debug( LDAP_DEBUG_CONNS,
2444 "daemon: write active on %d\n",
2448 * NOTE: it is possible that the connection was closed
2449 * and that the stream is now inactive.
2450 * connection_write() must validate the stream is still
2453 * ITS#4338: if the stream is invalid, there is no need to
2454 * close it here. It has already been closed in connection.c.
2456 if ( connection_write( wd ) < 0 ) {
2457 if ( SLAP_EVENT_IS_READ( wd ) ) {
2458 SLAP_EVENT_CLR_READ( (unsigned) wd );
2464 for ( i = 0; nrfds > 0; i++ ) {
2466 if ( ! SLAP_EVENT_IS_READ( i ) ) continue;
2468 SLAP_EVENT_CLR_READ( rd );
2471 Debug ( LDAP_DEBUG_CONNS,
2472 "daemon: read activity on %d\n", rd, 0, 0 );
2474 * NOTE: it is possible that the connection was closed
2475 * and that the stream is now inactive.
2476 * connection_read() must valid the stream is still
2480 connection_read_activate( rd );
2482 #else /* !SLAP_EVENTS_ARE_INDEXED */
2484 /* The events are returned in an arbitrary list. This is true
2485 * for /dev/poll, epoll and kqueue. In order to prioritize things
2486 * so that we can handle wake_sds first, listeners second, and then
2487 * all other connections last (as we do for select), we would need
2488 * to use multiple event handles and cascade them.
2490 * That seems like a bit of hassle. So the wake_sds check has been
2491 * skipped. For epoll and kqueue we can associate arbitrary data with
2492 * an event, so we could use pointers to the listener structure
2493 * instead of just the file descriptor. For /dev/poll we have to
2494 * search the listeners array for a matching descriptor.
2496 * We now handle wake events when we see them; they are not given
2500 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
2502 for ( i = 0; i < ns; i++ ) {
2505 /* Don't log listener events */
2506 if ( SLAP_EVENT_IS_LISTENER( i )
2507 #ifdef LDAP_CONNECTIONLESS
2508 && !( (SLAP_EVENT_LISTENER( i ))->sl_is_udp )
2509 #endif /* LDAP_CONNECTIONLESS */
2515 fd = SLAP_EVENT_FD( i );
2516 /* Don't log internal wake events */
2517 if ( fd == wake_sds[0] ) continue;
2519 r = SLAP_EVENT_IS_READ( i );
2520 w = SLAP_EVENT_IS_WRITE( i );
2522 Debug( LDAP_DEBUG_CONNS, " %d%s%s", fd,
2523 r ? "r" : "", w ? "w" : "" );
2526 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
2527 #endif /* LDAP_DEBUG */
2529 for ( i = 0; i < ns; i++ ) {
2530 int rc = 1, fd, w = 0;
2532 if ( SLAP_EVENT_IS_LISTENER( i ) ) {
2533 rc = slap_listener_activate( SLAP_EVENT_LISTENER( i ) );
2536 /* If we found a regular listener, rc is now zero, and we
2537 * can skip the data portion. But if it was a UDP listener
2538 * then rc is still 1, and we want to handle the data.
2541 fd = SLAP_EVENT_FD( i );
2543 /* Handle wake events */
2544 if ( fd == wake_sds[0] ) {
2547 tcp_read( SLAP_FD2SOCK(wake_sds[0]), c, sizeof(c) );
2551 if ( SLAP_EVENT_IS_WRITE( i ) ) {
2552 Debug( LDAP_DEBUG_CONNS,
2553 "daemon: write active on %d\n",
2556 SLAP_EVENT_CLR_WRITE( i );
2560 * NOTE: it is possible that the connection was closed
2561 * and that the stream is now inactive.
2562 * connection_write() must valid the stream is still
2565 if ( connection_write( fd ) < 0 ) {
2569 /* If event is a read */
2570 if ( SLAP_EVENT_IS_READ( i ) ) {
2571 Debug( LDAP_DEBUG_CONNS,
2572 "daemon: read active on %d\n",
2575 SLAP_EVENT_CLR_READ( i );
2576 connection_read_activate( fd );
2578 Debug( LDAP_DEBUG_CONNS,
2579 "daemon: hangup on %d\n", fd, 0, 0 );
2580 if ( SLAP_SOCK_IS_ACTIVE( fd )) {
2582 /* Don't keep reporting the hangup
2584 SLAP_EPOLL_SOCK_SET( fd, EPOLLET );
2586 connection_hangup( fd );
2591 #endif /* SLAP_EVENTS_ARE_INDEXED */
2593 #ifndef HAVE_YIELDING_SELECT
2594 ldap_pvt_thread_yield();
2595 #endif /* ! HAVE_YIELDING_SELECT */
2598 if ( slapd_shutdown == 1 ) {
2599 Debug( LDAP_DEBUG_ANY,
2600 "daemon: shutdown requested and initiated.\n",
2603 } else if ( slapd_shutdown == 2 ) {
2604 #ifdef HAVE_NT_SERVICE_MANAGER
2605 Debug( LDAP_DEBUG_ANY,
2606 "daemon: shutdown initiated by Service Manager.\n",
2608 #else /* !HAVE_NT_SERVICE_MANAGER */
2609 Debug( LDAP_DEBUG_ANY,
2610 "daemon: abnormal condition, shutdown initiated.\n",
2612 #endif /* !HAVE_NT_SERVICE_MANAGER */
2614 Debug( LDAP_DEBUG_ANY,
2615 "daemon: no active streams, shutdown initiated.\n",
2619 if ( slapd_gentle_shutdown != 2 ) close_listeners ( 0 );
2621 if ( !slapd_gentle_shutdown ) {
2622 slapd_abrupt_shutdown = 1;
2623 connections_shutdown();
2626 if ( LogTest( LDAP_DEBUG_ANY )) {
2627 int t = ldap_pvt_thread_pool_backload( &connection_pool );
2628 Debug( LDAP_DEBUG_ANY,
2629 "slapd shutdown: waiting for %d operations/tasks to finish\n",
2632 ldap_pvt_thread_pool_destroy( &connection_pool, 1 );
2634 free( slap_listeners );
2635 slap_listeners = NULL;
2641 #ifdef LDAP_CONNECTIONLESS
2643 connectionless_init( void )
2647 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2648 Listener *lr = slap_listeners[l];
2651 if ( !lr->sl_is_udp ) {
2655 c = connection_init( lr->sl_sd, lr, "", "",
2656 CONN_IS_UDP, (slap_ssf_t) 0, NULL
2657 LDAP_PF_LOCAL_SENDMSG_ARG(NULL));
2660 Debug( LDAP_DEBUG_TRACE,
2661 "connectionless_init: failed on %s (%d)\n",
2662 lr->sl_url, lr->sl_sd, 0 );
2670 #endif /* LDAP_CONNECTIONLESS */
2673 slapd_daemon( void )
2677 #ifdef LDAP_CONNECTIONLESS
2678 connectionless_init();
2679 #endif /* LDAP_CONNECTIONLESS */
2681 #define SLAPD_LISTENER_THREAD 1
2682 #if defined( SLAPD_LISTENER_THREAD )
2684 ldap_pvt_thread_t listener_tid;
2686 /* listener as a separate THREAD */
2687 rc = ldap_pvt_thread_create( &listener_tid,
2688 0, slapd_daemon_task, NULL );
2691 Debug( LDAP_DEBUG_ANY,
2692 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
2696 /* wait for the listener thread to complete */
2697 ldap_pvt_thread_join( listener_tid, (void *)NULL );
2699 #else /* ! SLAPD_LISTENER_THREAD */
2700 /* experimental code */
2701 slapd_daemon_task( NULL );
2702 #endif /* ! SLAPD_LISTENER_THREAD */
2710 #if defined( HAVE_WINSOCK2 )
2711 WORD wVersionRequested;
2715 wVersionRequested = MAKEWORD( 2, 0 );
2717 err = WSAStartup( wVersionRequested, &wsaData );
2719 /* Tell the user that we couldn't find a usable */
2724 /* Confirm that the WinSock DLL supports 2.0.*/
2725 /* Note that if the DLL supports versions greater */
2726 /* than 2.0 in addition to 2.0, it will still return */
2727 /* 2.0 in wVersion since that is the version we */
2730 if ( LOBYTE( wsaData.wVersion ) != 2 ||
2731 HIBYTE( wsaData.wVersion ) != 0 )
2733 /* Tell the user that we couldn't find a usable */
2739 /* The WinSock DLL is acceptable. Proceed. */
2740 #elif defined( HAVE_WINSOCK )
2742 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) return -1;
2743 #endif /* ! HAVE_WINSOCK2 && ! HAVE_WINSOCK */
2751 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
2753 #endif /* HAVE_WINSOCK2 || HAVE_WINSOCK */
2760 slap_sig_shutdown( int sig )
2763 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
2767 * If the NT Service Manager is controlling the server, we don't
2768 * want SIGBREAK to kill the server. For some strange reason,
2769 * SIGBREAK is generated when a user logs out.
2772 #if defined(HAVE_NT_SERVICE_MANAGER) && defined(SIGBREAK)
2773 if (is_NT_Service && sig == SIGBREAK) {
2776 #endif /* HAVE_NT_SERVICE_MANAGER && SIGBREAK */
2778 if (sig == SIGHUP && global_gentlehup && slapd_gentle_shutdown == 0) {
2779 slapd_gentle_shutdown = 1;
2788 /* reinstall self */
2789 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
2793 slap_sig_wake( int sig )
2797 /* reinstall self */
2798 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
2803 slapd_add_internal( ber_socket_t s, int isactive )
2805 slapd_add( s, isactive, NULL );
2809 slapd_get_listeners( void )
2811 return slap_listeners;
2815 slap_wake_listener()