2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2008 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 volatile int waking;
91 #define WAKE_LISTENER(w) do { \
92 if ((w) && ++waking < 5) { \
93 tcp_write( SLAP_FD2SOCK(wake_sds[1]), "0", 1 ); \
96 #else /* ! NO_THREADS */
97 #define WAKE_LISTENER(w) do { \
99 tcp_write( SLAP_FD2SOCK(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;
109 ldap_pvt_thread_mutex_t slapd_ws_mutex;
110 SOCKET *slapd_ws_sockets;
114 #define SD_LISTENER 8
117 static struct slap_daemon {
118 ldap_pvt_thread_mutex_t sd_mutex;
120 ldap_pvt_thread_mutex_t sd_tcpd_mutex;
121 #endif /* TCP Wrappers */
123 ber_socket_t sd_nactives;
127 #if defined(HAVE_EPOLL)
128 struct epoll_event *sd_epolls;
131 #elif defined(SLAP_X_DEVPOLL) && defined(HAVE_DEVPOLL)
133 struct pollfd *sd_pollfd;
137 #else /* ! epoll && ! /dev/poll */
141 #else /* ! HAVE_WINSOCK */
145 #endif /* ! HAVE_WINSOCK */
146 #endif /* ! epoll && ! /dev/poll */
150 * NOTE: naming convention for macros:
152 * - SLAP_SOCK_* and SLAP_EVENT_* for public interface that deals
153 * with file descriptors and events respectively
155 * - SLAP_<type>_* for private interface; type by now is one of
156 * EPOLL, DEVPOLL, SELECT
158 * private interface should not be used in the code.
160 #if defined(HAVE_EPOLL)
161 /***************************************
162 * Use epoll infrastructure - epoll(4) *
163 ***************************************/
164 # define SLAP_EVENT_FNAME "epoll"
165 # define SLAP_EVENTS_ARE_INDEXED 0
166 # define SLAP_EPOLL_SOCK_IX(s) (slap_daemon.sd_index[(s)])
167 # define SLAP_EPOLL_SOCK_EP(s) (slap_daemon.sd_epolls[SLAP_EPOLL_SOCK_IX(s)])
168 # define SLAP_EPOLL_SOCK_EV(s) (SLAP_EPOLL_SOCK_EP(s).events)
169 # define SLAP_SOCK_IS_ACTIVE(s) (SLAP_EPOLL_SOCK_IX(s) != -1)
170 # define SLAP_SOCK_NOT_ACTIVE(s) (SLAP_EPOLL_SOCK_IX(s) == -1)
171 # define SLAP_EPOLL_SOCK_IS_SET(s, mode) (SLAP_EPOLL_SOCK_EV(s) & (mode))
173 # define SLAP_SOCK_IS_READ(s) SLAP_EPOLL_SOCK_IS_SET((s), EPOLLIN)
174 # define SLAP_SOCK_IS_WRITE(s) SLAP_EPOLL_SOCK_IS_SET((s), EPOLLOUT)
176 # define SLAP_EPOLL_SOCK_SET(s, mode) do { \
177 if ( (SLAP_EPOLL_SOCK_EV(s) & (mode)) != (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_EPOLL_SOCK_CLR(s, mode) do { \
185 if ( (SLAP_EPOLL_SOCK_EV(s) & (mode)) ) { \
186 SLAP_EPOLL_SOCK_EV(s) &= ~(mode); \
187 epoll_ctl( slap_daemon.sd_epfd, EPOLL_CTL_MOD, s, \
188 &SLAP_EPOLL_SOCK_EP(s) ); \
192 # define SLAP_SOCK_SET_READ(s) SLAP_EPOLL_SOCK_SET(s, EPOLLIN)
193 # define SLAP_SOCK_SET_WRITE(s) SLAP_EPOLL_SOCK_SET(s, EPOLLOUT)
195 # define SLAP_SOCK_CLR_READ(s) SLAP_EPOLL_SOCK_CLR((s), EPOLLIN)
196 # define SLAP_SOCK_CLR_WRITE(s) SLAP_EPOLL_SOCK_CLR((s), EPOLLOUT)
198 # define SLAP_SOCK_SET_SUSPEND(s) \
199 ( slap_daemon.sd_suspend[SLAP_EPOLL_SOCK_IX(s)] = 1 )
200 # define SLAP_SOCK_CLR_SUSPEND(s) \
201 ( slap_daemon.sd_suspend[SLAP_EPOLL_SOCK_IX(s)] = 0 )
202 # define SLAP_SOCK_IS_SUSPEND(s) \
203 ( slap_daemon.sd_suspend[SLAP_EPOLL_SOCK_IX(s)] == 1 )
205 # define SLAP_EPOLL_EVENT_CLR(i, mode) (revents[(i)].events &= ~(mode))
207 # define SLAP_EVENT_MAX slap_daemon.sd_nfds
209 /* If a Listener address is provided, store that as the epoll data.
210 * Otherwise, store the address of this socket's slot in the
211 * index array. If we can't do this add, the system is out of
212 * resources and we need to shutdown.
214 # define SLAP_SOCK_ADD(s, l) do { \
216 SLAP_EPOLL_SOCK_IX((s)) = slap_daemon.sd_nfds; \
217 SLAP_EPOLL_SOCK_EP((s)).data.ptr = (l) ? (l) : (void *)(&SLAP_EPOLL_SOCK_IX(s)); \
218 SLAP_EPOLL_SOCK_EV((s)) = EPOLLIN; \
219 rc = epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_ADD, \
220 (s), &SLAP_EPOLL_SOCK_EP((s))); \
222 slap_daemon.sd_nfds++; \
224 Debug( LDAP_DEBUG_ANY, \
225 "daemon: epoll_ctl(ADD,fd=%d) failed, errno=%d, shutting down\n", \
227 slapd_shutdown = 2; \
231 # define SLAP_EPOLL_EV_LISTENER(ptr) \
232 (((int *)(ptr) >= slap_daemon.sd_index && \
233 (int *)(ptr) <= &slap_daemon.sd_index[dtblsize]) ? 0 : 1 )
235 # define SLAP_EPOLL_EV_PTRFD(ptr) (SLAP_EPOLL_EV_LISTENER(ptr) ? \
236 ((Listener *)ptr)->sl_sd : \
237 (ber_socket_t) ((int *)(ptr) - slap_daemon.sd_index))
239 # define SLAP_SOCK_DEL(s) do { \
240 int fd, rc, index = SLAP_EPOLL_SOCK_IX((s)); \
241 if ( index < 0 ) break; \
242 rc = epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_DEL, \
243 (s), &SLAP_EPOLL_SOCK_EP((s))); \
244 slap_daemon.sd_epolls[index] = \
245 slap_daemon.sd_epolls[slap_daemon.sd_nfds-1]; \
246 fd = SLAP_EPOLL_EV_PTRFD(slap_daemon.sd_epolls[index].data.ptr); \
247 slap_daemon.sd_index[fd] = index; \
248 slap_daemon.sd_index[(s)] = -1; \
249 slap_daemon.sd_nfds--; \
252 # define SLAP_EVENT_CLR_READ(i) SLAP_EPOLL_EVENT_CLR((i), EPOLLIN)
253 # define SLAP_EVENT_CLR_WRITE(i) SLAP_EPOLL_EVENT_CLR((i), EPOLLOUT)
255 # define SLAP_EPOLL_EVENT_CHK(i, mode) (revents[(i)].events & mode)
257 # define SLAP_EVENT_IS_READ(i) SLAP_EPOLL_EVENT_CHK((i), EPOLLIN)
258 # define SLAP_EVENT_IS_WRITE(i) SLAP_EPOLL_EVENT_CHK((i), EPOLLOUT)
259 # define SLAP_EVENT_IS_LISTENER(i) SLAP_EPOLL_EV_LISTENER(revents[(i)].data.ptr)
260 # define SLAP_EVENT_LISTENER(i) ((Listener *)(revents[(i)].data.ptr))
262 # define SLAP_EVENT_FD(i) SLAP_EPOLL_EV_PTRFD(revents[(i)].data.ptr)
264 # define SLAP_SOCK_INIT do { \
265 slap_daemon.sd_epolls = ch_calloc(1, \
266 ( sizeof(struct epoll_event) * 2 \
267 + sizeof(int) ) * dtblsize * 2); \
268 slap_daemon.sd_index = (int *)&slap_daemon.sd_epolls[ 2 * dtblsize ]; \
269 slap_daemon.sd_epfd = epoll_create( dtblsize ); \
270 for ( i = 0; i < dtblsize; i++ ) slap_daemon.sd_index[i] = -1; \
273 # define SLAP_SOCK_DESTROY do { \
274 if ( slap_daemon.sd_epolls != NULL ) { \
275 ch_free( slap_daemon.sd_epolls ); \
276 slap_daemon.sd_epolls = NULL; \
277 slap_daemon.sd_index = NULL; \
278 close( slap_daemon.sd_epfd ); \
282 # define SLAP_EVENT_DECL struct epoll_event *revents
284 # define SLAP_EVENT_INIT do { \
285 revents = slap_daemon.sd_epolls + dtblsize; \
288 # define SLAP_EVENT_WAIT(tvp, nsp) do { \
289 *(nsp) = epoll_wait( slap_daemon.sd_epfd, revents, \
290 dtblsize, (tvp) ? (tvp)->tv_sec * 1000 : -1 ); \
293 #elif defined(SLAP_X_DEVPOLL) && defined(HAVE_DEVPOLL)
295 /*************************************************************
296 * Use Solaris' (>= 2.7) /dev/poll infrastructure - poll(7d) *
297 *************************************************************/
298 # define SLAP_EVENT_FNAME "/dev/poll"
299 # define SLAP_EVENTS_ARE_INDEXED 0
301 * - sd_index is used much like with epoll()
302 * - sd_l is maintained as an array containing the address
303 * of the listener; the index is the fd itself
304 * - sd_pollfd is used to keep track of what data has been
305 * registered in /dev/poll
307 # define SLAP_DEVPOLL_SOCK_IX(s) (slap_daemon.sd_index[(s)])
308 # define SLAP_DEVPOLL_SOCK_LX(s) (slap_daemon.sd_l[(s)])
309 # define SLAP_DEVPOLL_SOCK_EP(s) (slap_daemon.sd_pollfd[SLAP_DEVPOLL_SOCK_IX((s))])
310 # define SLAP_DEVPOLL_SOCK_FD(s) (SLAP_DEVPOLL_SOCK_EP((s)).fd)
311 # define SLAP_DEVPOLL_SOCK_EV(s) (SLAP_DEVPOLL_SOCK_EP((s)).events)
312 # define SLAP_SOCK_IS_ACTIVE(s) (SLAP_DEVPOLL_SOCK_IX((s)) != -1)
313 # define SLAP_SOCK_NOT_ACTIVE(s) (SLAP_DEVPOLL_SOCK_IX((s)) == -1)
314 # define SLAP_SOCK_IS_SET(s, mode) (SLAP_DEVPOLL_SOCK_EV((s)) & (mode))
316 # define SLAP_SOCK_IS_READ(s) SLAP_SOCK_IS_SET((s), POLLIN)
317 # define SLAP_SOCK_IS_WRITE(s) SLAP_SOCK_IS_SET((s), POLLOUT)
319 /* as far as I understand, any time we need to communicate with the kernel
320 * about the number and/or properties of a file descriptor we need it to
321 * wait for, we have to rewrite the whole set */
322 # define SLAP_DEVPOLL_WRITE_POLLFD(s, pfd, n, what, shdn) do { \
324 size_t size = (n) * sizeof( struct pollfd ); \
325 /* FIXME: use pwrite? */ \
326 rc = write( slap_daemon.sd_dpfd, (pfd), size ); \
327 if ( rc != size ) { \
328 Debug( LDAP_DEBUG_ANY, "daemon: " SLAP_EVENT_FNAME ": " \
329 "%s fd=%d failed errno=%d\n", \
330 (what), (s), errno ); \
332 slapd_shutdown = 2; \
337 # define SLAP_DEVPOLL_SOCK_SET(s, mode) do { \
338 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_SET_%s(%d)=%d\n", \
339 (mode) == POLLIN ? "READ" : "WRITE", (s), \
340 ( (SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) != (mode) ) ); \
341 if ( (SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) != (mode) ) { \
343 SLAP_DEVPOLL_SOCK_EV((s)) |= (mode); \
344 pfd.fd = SLAP_DEVPOLL_SOCK_FD((s)); \
345 pfd.events = /* (mode) */ SLAP_DEVPOLL_SOCK_EV((s)); \
346 SLAP_DEVPOLL_WRITE_POLLFD((s), &pfd, 1, "SET", 0); \
350 # define SLAP_DEVPOLL_SOCK_CLR(s, mode) do { \
351 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_CLR_%s(%d)=%d\n", \
352 (mode) == POLLIN ? "READ" : "WRITE", (s), \
353 ( (SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) == (mode) ) ); \
354 if ((SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) == (mode) ) { \
355 struct pollfd pfd[2]; \
356 SLAP_DEVPOLL_SOCK_EV((s)) &= ~(mode); \
357 pfd[0].fd = SLAP_DEVPOLL_SOCK_FD((s)); \
358 pfd[0].events = POLLREMOVE; \
359 pfd[1] = SLAP_DEVPOLL_SOCK_EP((s)); \
360 SLAP_DEVPOLL_WRITE_POLLFD((s), &pfd[0], 2, "CLR", 0); \
364 # define SLAP_SOCK_SET_READ(s) SLAP_DEVPOLL_SOCK_SET(s, POLLIN)
365 # define SLAP_SOCK_SET_WRITE(s) SLAP_DEVPOLL_SOCK_SET(s, POLLOUT)
367 # define SLAP_SOCK_CLR_READ(s) SLAP_DEVPOLL_SOCK_CLR((s), POLLIN)
368 # define SLAP_SOCK_CLR_WRITE(s) SLAP_DEVPOLL_SOCK_CLR((s), POLLOUT)
370 # define SLAP_SOCK_SET_SUSPEND(s) \
371 ( slap_daemon.sd_suspend[SLAP_DEVPOLL_SOCK_IX((s))] = 1 )
372 # define SLAP_SOCK_CLR_SUSPEND(s) \
373 ( slap_daemon.sd_suspend[SLAP_DEVPOLL_SOCK_IX((s))] = 0 )
374 # define SLAP_SOCK_IS_SUSPEND(s) \
375 ( slap_daemon.sd_suspend[SLAP_DEVPOLL_SOCK_IX((s))] == 1 )
377 # define SLAP_DEVPOLL_EVENT_CLR(i, mode) (revents[(i)].events &= ~(mode))
379 # define SLAP_EVENT_MAX slap_daemon.sd_nfds
381 /* If a Listener address is provided, store that in the sd_l array.
382 * If we can't do this add, the system is out of resources and we
385 # define SLAP_SOCK_ADD(s, l) do { \
386 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_ADD(%d, %p)\n", (s), (l), 0 ); \
387 SLAP_DEVPOLL_SOCK_IX((s)) = slap_daemon.sd_nfds; \
388 SLAP_DEVPOLL_SOCK_LX((s)) = (l); \
389 SLAP_DEVPOLL_SOCK_FD((s)) = (s); \
390 SLAP_DEVPOLL_SOCK_EV((s)) = POLLIN; \
391 SLAP_DEVPOLL_WRITE_POLLFD((s), &SLAP_DEVPOLL_SOCK_EP((s)), 1, "ADD", 1); \
392 slap_daemon.sd_nfds++; \
395 # define SLAP_DEVPOLL_EV_LISTENER(ptr) ((ptr) != NULL)
397 # define SLAP_SOCK_DEL(s) do { \
398 int fd, index = SLAP_DEVPOLL_SOCK_IX((s)); \
399 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_DEL(%d)\n", (s), 0, 0 ); \
400 if ( index < 0 ) break; \
401 if ( index < slap_daemon.sd_nfds - 1 ) { \
402 struct pollfd pfd = slap_daemon.sd_pollfd[index]; \
403 fd = slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1].fd; \
404 slap_daemon.sd_pollfd[index] = slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1]; \
405 slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1] = pfd; \
406 slap_daemon.sd_index[fd] = index; \
408 slap_daemon.sd_index[(s)] = -1; \
409 slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1].events = POLLREMOVE; \
410 SLAP_DEVPOLL_WRITE_POLLFD((s), &slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1], 1, "DEL", 0); \
411 slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1].events = 0; \
412 slap_daemon.sd_nfds--; \
415 # define SLAP_EVENT_CLR_READ(i) SLAP_DEVPOLL_EVENT_CLR((i), POLLIN)
416 # define SLAP_EVENT_CLR_WRITE(i) SLAP_DEVPOLL_EVENT_CLR((i), POLLOUT)
418 # define SLAP_DEVPOLL_EVENT_CHK(i, mode) (revents[(i)].events & (mode))
420 # define SLAP_EVENT_FD(i) (revents[(i)].fd)
422 # define SLAP_EVENT_IS_READ(i) SLAP_DEVPOLL_EVENT_CHK((i), POLLIN)
423 # define SLAP_EVENT_IS_WRITE(i) SLAP_DEVPOLL_EVENT_CHK((i), POLLOUT)
424 # define SLAP_EVENT_IS_LISTENER(i) SLAP_DEVPOLL_EV_LISTENER(SLAP_DEVPOLL_SOCK_LX(SLAP_EVENT_FD((i))))
425 # define SLAP_EVENT_LISTENER(i) SLAP_DEVPOLL_SOCK_LX(SLAP_EVENT_FD((i)))
427 # define SLAP_SOCK_INIT do { \
428 slap_daemon.sd_pollfd = ch_calloc( 1, \
429 ( sizeof(struct pollfd) * 2 \
431 + sizeof( Listener * ) ) * dtblsize ); \
432 slap_daemon.sd_index = (int *)&slap_daemon.sd_pollfd[ 2 * dtblsize ]; \
433 slap_daemon.sd_l = (Listener **)&slap_daemon.sd_index[ dtblsize ]; \
434 slap_daemon.sd_dpfd = open( SLAP_EVENT_FNAME, O_RDWR ); \
435 if ( slap_daemon.sd_dpfd == -1 ) { \
436 Debug( LDAP_DEBUG_ANY, "daemon: " SLAP_EVENT_FNAME ": " \
437 "open(\"" SLAP_EVENT_FNAME "\") failed errno=%d\n", \
442 for ( i = 0; i < dtblsize; i++ ) { \
443 slap_daemon.sd_pollfd[i].fd = -1; \
444 slap_daemon.sd_index[i] = -1; \
448 # define SLAP_SOCK_DESTROY do { \
449 if ( slap_daemon.sd_pollfd != NULL ) { \
450 ch_free( slap_daemon.sd_pollfd ); \
451 slap_daemon.sd_pollfd = NULL; \
452 slap_daemon.sd_index = NULL; \
453 slap_daemon.sd_l = NULL; \
454 close( slap_daemon.sd_dpfd ); \
458 # define SLAP_EVENT_DECL struct pollfd *revents
460 # define SLAP_EVENT_INIT do { \
461 revents = &slap_daemon.sd_pollfd[ dtblsize ]; \
464 # define SLAP_EVENT_WAIT(tvp, nsp) do { \
465 struct dvpoll sd_dvpoll; \
466 sd_dvpoll.dp_timeout = (tvp) ? (tvp)->tv_sec * 1000 : -1; \
467 sd_dvpoll.dp_nfds = dtblsize; \
468 sd_dvpoll.dp_fds = revents; \
469 *(nsp) = ioctl( slap_daemon.sd_dpfd, DP_POLL, &sd_dvpoll ); \
472 #else /* ! epoll && ! /dev/poll */
474 # define SLAP_EVENT_FNAME "WSselect"
475 /* Winsock provides a "select" function but its fd_sets are
476 * actually arrays of sockets. Since these sockets are handles
477 * and not a contiguous range of small integers, we manage our
478 * own "fd" table of socket handles and use their indices as
481 * All of our listener/connection structures use fds; the actual
482 * I/O functions use sockets. The SLAP_FD2SOCK macro in proto-slap.h
483 * handles the mapping.
485 * Despite the mapping overhead, this is about 45% more efficient
486 * than just using Winsock's select and FD_ISSET directly.
488 * Unfortunately Winsock's select implementation doesn't scale well
489 * as the number of connections increases. This probably needs to be
490 * rewritten to use the Winsock overlapped/asynchronous I/O functions.
492 # define SLAP_EVENTS_ARE_INDEXED 1
493 # define SLAP_EVENT_DECL fd_set readfds, writefds
494 # define SLAP_EVENT_INIT do { \
496 FD_ZERO( &readfds ); \
497 FD_ZERO( &writefds ); \
498 memset( slap_daemon.sd_rflags, 0, slap_daemon.sd_nfds ); \
499 for ( i=0; i<slap_daemon.sd_nfds; i++ ) { \
500 if ( slap_daemon.sd_flags[i] & SD_READ ) \
501 FD_SET( slapd_ws_sockets[i], &readfds );\
502 if ( slap_daemon.sd_flags[i] & SD_WRITE ) \
503 FD_SET( slapd_ws_sockets[i], &writefds ); \
506 # define SLAP_EVENT_MAX slap_daemon.sd_nfds
508 # define SLAP_EVENT_WAIT(tvp, nsp) do { \
510 *(nsp) = select( SLAP_EVENT_MAX, &readfds, \
511 nwriters > 0 ? &writefds : NULL, NULL, (tvp) ); \
512 for ( i=0; i<readfds.fd_count; i++) { \
513 int fd = slapd_sock2fd(readfds.fd_array[i]); \
515 slap_daemon.sd_rflags[fd] = SD_READ; \
516 if ( fd >= *(nsp)) *(nsp) = fd+1; \
519 for ( i=0; i<writefds.fd_count; i++) { \
520 int fd = slapd_sock2fd(writefds.fd_array[i]); \
522 slap_daemon.sd_rflags[fd] = SD_WRITE; \
523 if ( fd >= *(nsp)) *(nsp) = fd+1; \
528 # define SLAP_EVENT_IS_READ(fd) (slap_daemon.sd_rflags[fd] & SD_READ)
529 # define SLAP_EVENT_IS_WRITE(fd) (slap_daemon.sd_rflags[fd] & SD_WRITE)
531 # define SLAP_EVENT_CLR_READ(fd) slap_daemon.sd_rflags[fd] &= ~SD_READ;
532 # define SLAP_EVENT_CLR_WRITE(fd) slap_daemon.sd_rflags[fd] &= ~SD_WRITE;
534 # define SLAP_SOCK_INIT do { \
535 ldap_pvt_thread_mutex_init( &slapd_ws_mutex ); \
536 slapd_ws_sockets = ch_malloc( dtblsize * ( sizeof(SOCKET) + 2)); \
537 slap_daemon.sd_flags = (char *)(slapd_ws_sockets + dtblsize); \
538 slap_daemon.sd_rflags = slap_daemon.sd_flags + dtblsize; \
539 memset( slapd_ws_sockets, -1, dtblsize * sizeof(SOCKET) ); \
540 slapd_ws_sockets[0] = wake_sds[0]; \
541 slapd_ws_sockets[1] = wake_sds[1]; \
544 slap_daemon.sd_nfds = 2; \
547 # define SLAP_SOCK_DESTROY do { \
548 ch_free( slapd_ws_sockets ); slapd_ws_sockets = NULL; \
549 slap_daemon.sd_flags = NULL; \
550 slap_daemon.sd_rflags = NULL; \
551 ldap_pvt_thread_mutex_destroy( &slapd_ws_mutex ); \
554 # define SLAP_SOCK_IS_ACTIVE(fd) ( slap_daemon.sd_flags[fd] & SD_ACTIVE )
555 # define SLAP_SOCK_IS_READ(fd) ( slap_daemon.sd_flags[fd] & SD_READ )
556 # define SLAP_SOCK_IS_WRITE(fd) ( slap_daemon.sd_flags[fd] & SD_WRITE )
557 # define SLAP_SOCK_NOT_ACTIVE(fd) (!slap_daemon.sd_flags[fd])
559 # define SLAP_SOCK_SET_READ(fd) ( slap_daemon.sd_flags[fd] |= SD_READ )
560 # define SLAP_SOCK_SET_WRITE(fd) ( slap_daemon.sd_flags[fd] |= SD_WRITE )
562 # define SLAP_SELECT_ADDTEST(s) do { \
563 if ((s) >= slap_daemon.sd_nfds) slap_daemon.sd_nfds = (s)+1; \
566 # define SLAP_SOCK_CLR_READ(fd) ( slap_daemon.sd_flags[fd] &= ~SD_READ )
567 # define SLAP_SOCK_CLR_WRITE(fd) ( slap_daemon.sd_flags[fd] &= ~SD_WRITE )
569 # define SLAP_SOCK_ADD(s, l) do { \
570 SLAP_SELECT_ADDTEST((s)); \
571 slap_daemon.sd_flags[s] = SD_ACTIVE|SD_READ; \
574 # define SLAP_SOCK_DEL(s) do { \
575 slap_daemon.sd_flags[s] = 0; \
576 slapd_sockdel( s ); \
579 # else /* !HAVE_WINSOCK */
581 /**************************************
582 * Use select system call - select(2) *
583 **************************************/
584 # define SLAP_EVENT_FNAME "select"
586 # define SLAP_EVENTS_ARE_INDEXED 1
587 # define SLAP_EVENT_DECL fd_set readfds, writefds
589 # define SLAP_EVENT_INIT do { \
590 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) ); \
592 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) ); \
594 FD_ZERO( &writefds ); \
599 # define SLAP_SELECT_CHK_SETSIZE do { \
600 if (dtblsize > FD_SETSIZE) dtblsize = FD_SETSIZE; \
602 # else /* ! FD_SETSIZE */
603 # define SLAP_SELECT_CHK_SETSIZE do { ; } while (0)
604 # endif /* ! FD_SETSIZE */
606 # define SLAP_SOCK_INIT do { \
607 SLAP_SELECT_CHK_SETSIZE; \
608 FD_ZERO(&slap_daemon.sd_actives); \
609 FD_ZERO(&slap_daemon.sd_readers); \
610 FD_ZERO(&slap_daemon.sd_writers); \
613 # define SLAP_SOCK_DESTROY
615 # define SLAP_SOCK_IS_ACTIVE(fd) FD_ISSET((fd), &slap_daemon.sd_actives)
616 # define SLAP_SOCK_IS_READ(fd) FD_ISSET((fd), &slap_daemon.sd_readers)
617 # define SLAP_SOCK_IS_WRITE(fd) FD_ISSET((fd), &slap_daemon.sd_writers)
619 # define SLAP_SOCK_NOT_ACTIVE(fd) (!SLAP_SOCK_IS_ACTIVE(fd) && \
620 !SLAP_SOCK_IS_READ(fd) && !SLAP_SOCK_IS_WRITE(fd))
622 # define SLAP_SOCK_SET_READ(fd) FD_SET((fd), &slap_daemon.sd_readers)
623 # define SLAP_SOCK_SET_WRITE(fd) FD_SET((fd), &slap_daemon.sd_writers)
625 # define SLAP_EVENT_MAX slap_daemon.sd_nfds
626 # define SLAP_SELECT_ADDTEST(s) do { \
627 if ((s) >= slap_daemon.sd_nfds) slap_daemon.sd_nfds = (s)+1; \
630 # define SLAP_SOCK_CLR_READ(fd) FD_CLR((fd), &slap_daemon.sd_readers)
631 # define SLAP_SOCK_CLR_WRITE(fd) FD_CLR((fd), &slap_daemon.sd_writers)
633 # define SLAP_SOCK_ADD(s, l) do { \
634 SLAP_SELECT_ADDTEST((s)); \
635 FD_SET((s), &slap_daemon.sd_actives); \
636 FD_SET((s), &slap_daemon.sd_readers); \
639 # define SLAP_SOCK_DEL(s) do { \
640 FD_CLR((s), &slap_daemon.sd_actives); \
641 FD_CLR((s), &slap_daemon.sd_readers); \
642 FD_CLR((s), &slap_daemon.sd_writers); \
645 # define SLAP_EVENT_IS_READ(fd) FD_ISSET((fd), &readfds)
646 # define SLAP_EVENT_IS_WRITE(fd) FD_ISSET((fd), &writefds)
648 # define SLAP_EVENT_CLR_READ(fd) FD_CLR((fd), &readfds)
649 # define SLAP_EVENT_CLR_WRITE(fd) FD_CLR((fd), &writefds)
651 # define SLAP_EVENT_WAIT(tvp, nsp) do { \
652 *(nsp) = select( SLAP_EVENT_MAX, &readfds, \
653 nwriters > 0 ? &writefds : NULL, NULL, (tvp) ); \
655 # endif /* !HAVE_WINSOCK */
656 #endif /* ! epoll && ! /dev/poll */
660 * SLP related functions
664 #define LDAP_SRVTYPE_PREFIX "service:ldap://"
665 #define LDAPS_SRVTYPE_PREFIX "service:ldaps://"
666 static char** slapd_srvurls = NULL;
667 static SLPHandle slapd_hslp = 0;
668 int slapd_register_slp = 0;
669 const char *slapd_slp_attrs = NULL;
671 static SLPError slapd_slp_cookie;
674 slapd_slp_init( const char* urls )
679 slapd_srvurls = ldap_str2charray( urls, " " );
681 if ( slapd_srvurls == NULL ) return;
683 /* find and expand INADDR_ANY URLs */
684 for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
685 if ( strcmp( slapd_srvurls[i], "ldap:///" ) == 0 ) {
686 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
687 strlen( global_host ) +
688 sizeof( LDAP_SRVTYPE_PREFIX ) );
689 strcpy( lutil_strcopy(slapd_srvurls[i],
690 LDAP_SRVTYPE_PREFIX ), global_host );
691 } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0 ) {
692 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
693 strlen( global_host ) +
694 sizeof( LDAPS_SRVTYPE_PREFIX ) );
695 strcpy( lutil_strcopy(slapd_srvurls[i],
696 LDAPS_SRVTYPE_PREFIX ), global_host );
700 /* open the SLP handle */
701 err = SLPOpen( "en", 0, &slapd_hslp );
703 if ( err != SLP_OK ) {
704 Debug( LDAP_DEBUG_CONNS, "daemon: SLPOpen() failed with %ld\n",
710 slapd_slp_deinit( void )
712 if ( slapd_srvurls == NULL ) return;
714 ldap_charray_free( slapd_srvurls );
715 slapd_srvurls = NULL;
717 /* close the SLP handle */
718 SLPClose( slapd_hslp );
727 /* return the error code in the cookie */
728 *(SLPError*)cookie = errcode;
737 if ( slapd_srvurls == NULL ) return;
739 for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
740 if ( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
741 sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
742 strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
743 sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
745 err = SLPReg( slapd_hslp,
747 SLP_LIFETIME_MAXIMUM,
749 (slapd_slp_attrs) ? slapd_slp_attrs : "",
754 if ( err != SLP_OK || slapd_slp_cookie != SLP_OK ) {
755 Debug( LDAP_DEBUG_CONNS,
756 "daemon: SLPReg(%s) failed with %ld, cookie = %ld\n",
757 slapd_srvurls[i], (long)err, (long)slapd_slp_cookie );
764 slapd_slp_dereg( void )
769 if ( slapd_srvurls == NULL ) return;
771 for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
772 err = SLPDereg( slapd_hslp,
777 if ( err != SLP_OK || slapd_slp_cookie != SLP_OK ) {
778 Debug( LDAP_DEBUG_CONNS,
779 "daemon: SLPDereg(%s) failed with %ld, cookie = %ld\n",
780 slapd_srvurls[i], (long)err, (long)slapd_slp_cookie );
784 #endif /* HAVE_SLP */
787 /* Manage the descriptor to socket table */
789 slapd_socknew( ber_socket_t s )
792 ldap_pvt_thread_mutex_lock( &slapd_ws_mutex );
793 for ( i = 0; i < dtblsize && slapd_ws_sockets[i] != INVALID_SOCKET; i++ );
794 if ( i == dtblsize ) {
795 WSASetLastError( WSAEMFILE );
797 slapd_ws_sockets[i] = s;
799 ldap_pvt_thread_mutex_unlock( &slapd_ws_mutex );
804 slapd_sockdel( ber_socket_t s )
806 ldap_pvt_thread_mutex_lock( &slapd_ws_mutex );
807 slapd_ws_sockets[s] = INVALID_SOCKET;
808 ldap_pvt_thread_mutex_unlock( &slapd_ws_mutex );
812 slapd_sock2fd( ber_socket_t s )
815 for ( i=0; i<dtblsize && slapd_ws_sockets[i] != s; i++);
823 * Add a descriptor to daemon control
825 * If isactive, the descriptor is a live server session and is subject
826 * to idletimeout control. Otherwise, the descriptor is a passive
827 * listener or an outbound client session, and not subject to
828 * idletimeout. The underlying event handler may record the Listener
829 * argument to differentiate Listener's from real sessions.
832 slapd_add( ber_socket_t s, int isactive, Listener *sl )
834 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
836 assert( SLAP_SOCK_NOT_ACTIVE(s) );
838 if ( isactive ) slap_daemon.sd_nactives++;
840 SLAP_SOCK_ADD(s, sl);
842 Debug( LDAP_DEBUG_CONNS, "daemon: added %ldr%s listener=%p\n",
843 (long) s, isactive ? " (active)" : "", (void *)sl );
845 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
854 slapd_sd_lock( void )
856 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
863 slapd_sd_unlock( void )
865 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
869 * Remove the descriptor from daemon control
883 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
885 assert( SLAP_SOCK_IS_ACTIVE( s ));
887 if ( wasactive ) slap_daemon.sd_nactives--;
889 waswriter = SLAP_SOCK_IS_WRITE(s);
890 wasreader = SLAP_SOCK_IS_READ(s);
892 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
894 wasreader ? "r" : "",
895 waswriter ? "w" : "" );
897 if ( waswriter ) slap_daemon.sd_nwriters--;
902 ber_sockbuf_free(sb);
904 /* If we ran out of file descriptors, we dropped a listener from
905 * the select() loop. Now that we're removing a session from our
906 * control, we can try to resume a dropped listener to use.
910 for ( i = 0; slap_listeners[i] != NULL; i++ ) {
911 Listener *lr = slap_listeners[i];
913 if ( lr->sl_sd == AC_SOCKET_INVALID ) continue;
914 if ( lr->sl_sd == s ) continue;
921 /* Walked the entire list without enabling anything; emfile
922 * counter is stale. Reset it.
924 if ( slap_listeners[i] == NULL ) emfile = 0;
926 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
927 WAKE_LISTENER(wake || slapd_gentle_shutdown == 2);
931 slapd_clr_write( ber_socket_t s, int wake )
933 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
935 if ( SLAP_SOCK_IS_WRITE( s )) {
936 assert( SLAP_SOCK_IS_ACTIVE( s ));
938 SLAP_SOCK_CLR_WRITE( s );
939 slap_daemon.sd_nwriters--;
942 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
947 slapd_set_write( ber_socket_t s, int wake )
949 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
951 assert( SLAP_SOCK_IS_ACTIVE( s ));
953 if ( !SLAP_SOCK_IS_WRITE( s )) {
954 SLAP_SOCK_SET_WRITE( s );
955 slap_daemon.sd_nwriters++;
958 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
963 slapd_clr_read( ber_socket_t s, int wake )
966 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
968 if ( SLAP_SOCK_IS_ACTIVE( s )) {
969 SLAP_SOCK_CLR_READ( s );
972 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
979 slapd_set_read( ber_socket_t s, int wake )
981 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
983 assert( SLAP_SOCK_IS_ACTIVE( s ));
984 if (!SLAP_SOCK_IS_READ( s )) SLAP_SOCK_SET_READ( s );
986 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
991 slapd_close( ber_socket_t s )
993 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
995 tcp_close( SLAP_FD2SOCK(s) );
1002 slap_free_listener_addresses( struct sockaddr **sal )
1004 struct sockaddr **sap;
1005 if (sal == NULL) return;
1006 for (sap = sal; *sap != NULL; sap++) ch_free(*sap);
1010 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
1019 assert( exts != NULL );
1020 assert( perms != NULL );
1021 assert( crit != NULL );
1024 for ( i = 0; exts[ i ]; i++ ) {
1025 char *type = exts[ i ];
1028 if ( type[ 0 ] == '!' ) {
1033 if ( strncasecmp( type, LDAPI_MOD_URLEXT "=",
1034 sizeof(LDAPI_MOD_URLEXT "=") - 1 ) == 0 )
1036 char *value = type + ( sizeof(LDAPI_MOD_URLEXT "=") - 1 );
1040 switch (strlen(value)) {
1042 /* skip leading '0' */
1043 if ( value[ 0 ] != '0' ) return LDAP_OTHER;
1047 for ( j = 0; j < 3; j++) {
1050 v = value[ j ] - '0';
1052 if ( v < 0 || v > 7 ) return LDAP_OTHER;
1059 for ( j = 1; j < 10; j++ ) {
1060 static mode_t m[] = { 0,
1061 S_IRUSR, S_IWUSR, S_IXUSR,
1062 S_IRGRP, S_IWGRP, S_IXGRP,
1063 S_IROTH, S_IWOTH, S_IXOTH
1065 static const char c[] = "-rwxrwxrwx";
1067 if ( value[ j ] == c[ j ] ) {
1070 } else if ( value[ j ] != '-' ) {
1083 return LDAP_SUCCESS;
1089 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
1091 /* port = 0 indicates AF_LOCAL */
1093 slap_get_listener_addresses(
1095 unsigned short port,
1096 struct sockaddr ***sal )
1098 struct sockaddr **sap;
1100 #ifdef LDAP_PF_LOCAL
1102 *sal = ch_malloc(2 * sizeof(void *));
1103 if (*sal == NULL) return -1;
1106 *sap = ch_malloc(sizeof(struct sockaddr_un));
1107 if (*sap == NULL) goto errexit;
1111 (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) )
1113 Debug( LDAP_DEBUG_ANY,
1114 "daemon: domain socket path (%s) too long in URL",
1119 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
1120 (*sap)->sa_family = AF_LOCAL;
1121 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
1123 #endif /* LDAP_PF_LOCAL */
1125 #ifdef HAVE_GETADDRINFO
1126 struct addrinfo hints, *res, *sai;
1130 memset( &hints, '\0', sizeof(hints) );
1131 hints.ai_flags = AI_PASSIVE;
1132 hints.ai_socktype = SOCK_STREAM;
1133 hints.ai_family = slap_inet4or6;
1134 snprintf(serv, sizeof serv, "%d", port);
1136 if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
1137 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo() failed: %s\n",
1138 AC_GAI_STRERROR(err), 0, 0);
1143 for (n=2; (sai = sai->ai_next) != NULL; n++) {
1146 *sal = ch_calloc(n, sizeof(void *));
1147 if (*sal == NULL) return -1;
1152 for ( sai=res; sai; sai=sai->ai_next ) {
1153 if( sai->ai_addr == NULL ) {
1154 Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
1155 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
1160 switch (sai->ai_family) {
1161 # ifdef LDAP_PF_INET6
1163 *sap = ch_malloc(sizeof(struct sockaddr_in6));
1168 *(struct sockaddr_in6 *)*sap =
1169 *((struct sockaddr_in6 *)sai->ai_addr);
1171 # endif /* LDAP_PF_INET6 */
1173 *sap = ch_malloc(sizeof(struct sockaddr_in));
1178 *(struct sockaddr_in *)*sap =
1179 *((struct sockaddr_in *)sai->ai_addr);
1187 (*sap)->sa_family = sai->ai_family;
1195 #else /* ! HAVE_GETADDRINFO */
1198 struct hostent *he = NULL;
1200 if ( host == NULL ) {
1201 in.s_addr = htonl(INADDR_ANY);
1203 } else if ( !inet_aton( host, &in ) ) {
1204 he = gethostbyname( host );
1206 Debug( LDAP_DEBUG_ANY,
1207 "daemon: invalid host %s", host, 0, 0);
1210 for (n = 0; he->h_addr_list[n]; n++) /* empty */;
1213 *sal = ch_malloc((n+1) * sizeof(void *));
1214 if (*sal == NULL) return -1;
1217 for ( i = 0; i<n; i++ ) {
1218 sap[i] = ch_malloc(sizeof(struct sockaddr_in));
1219 if (*sap == NULL) goto errexit;
1221 (void)memset( (void *)sap[i], '\0', sizeof(struct sockaddr_in) );
1222 sap[i]->sa_family = AF_INET;
1223 ((struct sockaddr_in *)sap[i])->sin_port = htons(port);
1224 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr,
1225 he ? (struct in_addr *)he->h_addr_list[i] : &in,
1226 sizeof(struct in_addr) );
1229 #endif /* ! HAVE_GETADDRINFO */
1235 slap_free_listener_addresses(*sal);
1249 unsigned short port;
1250 int err, addrlen = 0;
1251 struct sockaddr **sal, **psal;
1252 int socktype = SOCK_STREAM; /* default to COTS */
1255 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
1260 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
1262 rc = ldap_url_parse( url, &lud );
1264 if( rc != LDAP_URL_SUCCESS ) {
1265 Debug( LDAP_DEBUG_ANY,
1266 "daemon: listen URL \"%s\" parse error=%d\n",
1271 l.sl_url.bv_val = NULL;
1276 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
1277 Debug( LDAP_DEBUG_ANY, "daemon: TLS not supported (%s)\n",
1279 ldap_free_urldesc( lud );
1283 if(! lud->lud_port ) lud->lud_port = LDAP_PORT;
1285 #else /* HAVE_TLS */
1286 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
1288 if(! lud->lud_port ) {
1289 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
1291 #endif /* HAVE_TLS */
1293 port = (unsigned short) lud->lud_port;
1295 tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme);
1296 if ( tmp == LDAP_PROTO_IPC ) {
1297 #ifdef LDAP_PF_LOCAL
1298 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
1299 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
1301 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
1303 #else /* ! LDAP_PF_LOCAL */
1305 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
1307 ldap_free_urldesc( lud );
1309 #endif /* ! LDAP_PF_LOCAL */
1311 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
1312 || strcmp(lud->lud_host, "*") == 0 )
1314 err = slap_get_listener_addresses(NULL, port, &sal);
1316 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
1320 #ifdef LDAP_CONNECTIONLESS
1321 l.sl_is_udp = ( tmp == LDAP_PROTO_UDP );
1322 #endif /* LDAP_CONNECTIONLESS */
1324 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
1325 if ( lud->lud_exts ) {
1326 err = get_url_perms( lud->lud_exts, &l.sl_perms, &crit );
1328 l.sl_perms = S_IRWXU | S_IRWXO;
1330 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
1332 ldap_free_urldesc( lud );
1333 if ( err ) return -1;
1335 /* If we got more than one address returned, we need to make space
1336 * for it in the slap_listeners array.
1338 for ( num=0; sal[num]; num++ ) /* empty */;
1340 *listeners += num-1;
1341 slap_listeners = ch_realloc( slap_listeners,
1342 (*listeners + 1) * sizeof(Listener *) );
1346 while ( *sal != NULL ) {
1348 switch( (*sal)->sa_family ) {
1352 #ifdef LDAP_PF_INET6
1356 #endif /* LDAP_PF_INET6 */
1357 #ifdef LDAP_PF_LOCAL
1361 #endif /* LDAP_PF_LOCAL */
1367 #ifdef LDAP_CONNECTIONLESS
1368 if( l.sl_is_udp ) socktype = SOCK_DGRAM;
1369 #endif /* LDAP_CONNECTIONLESS */
1371 s = socket( (*sal)->sa_family, socktype, 0);
1372 if ( s == AC_SOCKET_INVALID ) {
1373 int err = sock_errno();
1374 Debug( LDAP_DEBUG_ANY,
1375 "daemon: %s socket() failed errno=%d (%s)\n",
1376 af, err, sock_errstr(err) );
1380 l.sl_sd = SLAP_SOCKNEW( s );
1382 if ( l.sl_sd >= dtblsize ) {
1383 Debug( LDAP_DEBUG_ANY,
1384 "daemon: listener descriptor %ld is too great %ld\n",
1385 (long) l.sl_sd, (long) dtblsize, 0 );
1391 #ifdef LDAP_PF_LOCAL
1392 if ( (*sal)->sa_family == AF_LOCAL ) {
1393 unlink( ((struct sockaddr_un *)*sal)->sun_path );
1395 #endif /* LDAP_PF_LOCAL */
1398 /* enable address reuse */
1400 rc = setsockopt( s, SOL_SOCKET, SO_REUSEADDR,
1401 (char *) &tmp, sizeof(tmp) );
1402 if ( rc == AC_SOCKET_ERROR ) {
1403 int err = sock_errno();
1404 Debug( LDAP_DEBUG_ANY, "slapd(%ld): "
1405 "setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
1406 (long) l.sl_sd, err, sock_errstr(err) );
1408 #endif /* SO_REUSEADDR */
1411 switch( (*sal)->sa_family ) {
1413 addrlen = sizeof(struct sockaddr_in);
1415 #ifdef LDAP_PF_INET6
1418 /* Try to use IPv6 sockets for IPv6 only */
1420 rc = setsockopt( s , IPPROTO_IPV6, IPV6_V6ONLY,
1421 (char *) &tmp, sizeof(tmp) );
1422 if ( rc == AC_SOCKET_ERROR ) {
1423 int err = sock_errno();
1424 Debug( LDAP_DEBUG_ANY, "slapd(%ld): "
1425 "setsockopt(IPV6_V6ONLY) failed errno=%d (%s)\n",
1426 (long) l.sl_sd, err, sock_errstr(err) );
1428 #endif /* IPV6_V6ONLY */
1429 addrlen = sizeof(struct sockaddr_in6);
1431 #endif /* LDAP_PF_INET6 */
1433 #ifdef LDAP_PF_LOCAL
1438 setsockopt( s, 0, LOCAL_CREDS, &one, sizeof( one ) );
1440 #endif /* LOCAL_CREDS */
1442 addrlen = sizeof( struct sockaddr_un );
1444 #endif /* LDAP_PF_LOCAL */
1447 #ifdef LDAP_PF_LOCAL
1448 /* create socket with all permissions set for those systems
1449 * that honor permissions on sockets (e.g. Linux); typically,
1450 * only write is required. To exploit filesystem permissions,
1451 * place the socket in a directory and use directory's
1452 * permissions. Need write perms to the directory to
1453 * create/unlink the socket; likely need exec perms to access
1454 * the socket (ITS#4709) */
1458 if ( (*sal)->sa_family == AF_LOCAL ) {
1459 old_umask = umask( 0 );
1461 #endif /* LDAP_PF_LOCAL */
1462 rc = bind( s, *sal, addrlen );
1463 #ifdef LDAP_PF_LOCAL
1464 if ( (*sal)->sa_family == AF_LOCAL ) {
1468 #endif /* LDAP_PF_LOCAL */
1471 Debug( LDAP_DEBUG_ANY,
1472 "daemon: bind(%ld) failed errno=%d (%s)\n",
1473 (long)l.sl_sd, err, sock_errstr( err ) );
1479 switch ( (*sal)->sa_family ) {
1480 #ifdef LDAP_PF_LOCAL
1482 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
1483 l.sl_name.bv_len = strlen(addr) + sizeof("PATH=") - 1;
1484 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len + 1 );
1485 snprintf( l.sl_name.bv_val, l.sl_name.bv_len + 1,
1488 #endif /* LDAP_PF_LOCAL */
1492 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
1493 char addr[INET_ADDRSTRLEN];
1494 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
1495 addr, sizeof(addr) );
1497 #else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1498 s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr );
1499 #endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1500 port = ntohs( ((struct sockaddr_in *)*sal) ->sin_port );
1502 ber_memalloc( sizeof("IP=255.255.255.255:65535") );
1503 snprintf( l.sl_name.bv_val, sizeof("IP=255.255.255.255:65535"),
1505 s != NULL ? s : SLAP_STRING_UNKNOWN, port );
1506 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1509 #ifdef LDAP_PF_INET6
1511 char addr[INET6_ADDRSTRLEN];
1512 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
1514 port = ntohs( ((struct sockaddr_in6 *)*sal)->sin6_port );
1515 l.sl_name.bv_len = strlen(addr) + sizeof("IP=[]:65535");
1516 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len );
1517 snprintf( l.sl_name.bv_val, l.sl_name.bv_len, "IP=[%s]:%d",
1519 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1521 #endif /* LDAP_PF_INET6 */
1524 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
1525 (int) (*sal)->sa_family, 0, 0 );
1529 AC_MEMCPY(&l.sl_sa, *sal, addrlen);
1530 ber_str2bv( url, 0, 1, &l.sl_url);
1531 li = ch_malloc( sizeof( Listener ) );
1533 slap_listeners[*cur] = li;
1538 slap_free_listener_addresses(psal);
1540 if ( l.sl_url.bv_val == NULL ) {
1541 Debug( LDAP_DEBUG_TRACE,
1542 "slap_open_listener: failed on %s\n", url, 0, 0 );
1546 Debug( LDAP_DEBUG_TRACE, "daemon: listener initialized %s\n",
1547 l.sl_url.bv_val, 0, 0 );
1551 static int sockinit(void);
1552 static int sockdestroy(void);
1555 slapd_daemon_init( const char *urls )
1560 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
1561 urls ? urls : "<null>", 0, 0 );
1563 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
1565 ldap_pvt_thread_mutex_init( &slap_daemon.sd_tcpd_mutex );
1566 #endif /* TCP Wrappers */
1568 if( (rc = sockinit()) != 0 ) return rc;
1571 dtblsize = sysconf( _SC_OPEN_MAX );
1572 #elif defined(HAVE_GETDTABLESIZE)
1573 dtblsize = getdtablesize();
1574 #else /* ! HAVE_SYSCONF && ! HAVE_GETDTABLESIZE */
1575 dtblsize = FD_SETSIZE;
1576 #endif /* ! HAVE_SYSCONF && ! HAVE_GETDTABLESIZE */
1578 /* open a pipe (or something equivalent connected to itself).
1579 * we write a byte on this fd whenever we catch a signal. The main
1580 * loop will be select'ing on this socket, and will wake up when
1581 * this byte arrives.
1583 if( (rc = lutil_pair( wake_sds )) < 0 ) {
1584 Debug( LDAP_DEBUG_ANY,
1585 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
1591 if( urls == NULL ) urls = "ldap:///";
1593 u = ldap_str2charray( urls, " " );
1595 if( u == NULL || u[0] == NULL ) {
1596 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
1599 ldap_charray_free( u );
1603 for( i=0; u[i] != NULL; i++ ) {
1604 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
1609 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
1611 ldap_charray_free( u );
1615 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
1617 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
1619 for(n = 0, j = 0; u[n]; n++ ) {
1620 if ( slap_open_listener( u[n], &i, &j ) ) {
1621 ldap_charray_free( u );
1625 slap_listeners[j] = NULL;
1627 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
1632 if( slapd_register_slp ) {
1633 slapd_slp_init( urls );
1636 #endif /* HAVE_SLP */
1638 ldap_charray_free( u );
1645 slapd_daemon_destroy( void )
1647 connections_destroy();
1649 if ( wake_sds[1] != INVALID_SOCKET && wake_sds[1] != wake_sds[0] )
1650 #endif /* HAVE_WINSOCK */
1651 tcp_close( SLAP_FD2SOCK(wake_sds[1]) );
1653 if ( wake_sds[0] != INVALID_SOCKET )
1654 #endif /* HAVE_WINSOCK */
1655 tcp_close( SLAP_FD2SOCK(wake_sds[0]) );
1659 if( slapd_register_slp ) {
1663 #endif /* HAVE_SLP */
1666 ldap_pvt_thread_mutex_destroy( &slap_daemon.sd_tcpd_mutex );
1667 #endif /* TCP Wrappers */
1669 ldap_pvt_thread_mutex_destroy( &slap_daemon.sd_mutex );
1680 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1681 Listener *lr = slap_listeners[l];
1683 if ( lr->sl_sd != AC_SOCKET_INVALID ) {
1684 if ( remove ) slapd_remove( lr->sl_sd, NULL, 0, 0, 0 );
1686 #ifdef LDAP_PF_LOCAL
1687 if ( lr->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1688 unlink( lr->sl_sa.sa_un_addr.sun_path );
1690 #endif /* LDAP_PF_LOCAL */
1692 slapd_close( lr->sl_sd );
1695 if ( lr->sl_url.bv_val ) {
1696 ber_memfree( lr->sl_url.bv_val );
1699 if ( lr->sl_name.bv_val ) {
1700 ber_memfree( lr->sl_name.bv_val );
1704 slap_listeners[l] = NULL;
1714 ber_socket_t s, sfd;
1715 ber_socklen_t len = sizeof(from);
1718 struct berval authid = BER_BVNULL;
1719 #ifdef SLAPD_RLOOKUPS
1720 char hbuf[NI_MAXHOST];
1721 #endif /* SLAPD_RLOOKUPS */
1723 char *dnsname = NULL;
1724 char *peeraddr = NULL;
1725 #ifdef LDAP_PF_LOCAL
1726 char peername[MAXPATHLEN + sizeof("PATH=")];
1727 #ifdef LDAP_PF_LOCAL_SENDMSG
1729 struct berval peerbv = BER_BVNULL;
1731 #elif defined(LDAP_PF_INET6)
1732 char peername[sizeof("IP=[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535")];
1733 #else /* ! LDAP_PF_LOCAL && ! LDAP_PF_INET6 */
1734 char peername[sizeof("IP=255.255.255.255:65336")];
1735 #endif /* LDAP_PF_LOCAL */
1738 Debug( LDAP_DEBUG_TRACE,
1739 ">>> slap_listener(%s)\n",
1740 sl->sl_url.bv_val, 0, 0 );
1744 #ifdef LDAP_CONNECTIONLESS
1745 if ( sl->sl_is_udp ) return 1;
1746 #endif /* LDAP_CONNECTIONLESS */
1748 # ifdef LDAP_PF_LOCAL
1749 /* FIXME: apparently accept doesn't fill
1750 * the sun_path sun_path member */
1751 from.sa_un_addr.sun_path[0] = '\0';
1752 # endif /* LDAP_PF_LOCAL */
1754 s = accept( SLAP_FD2SOCK( sl->sl_sd ), (struct sockaddr *) &from, &len );
1756 /* Resume the listener FD to allow concurrent-processing of
1757 * additional incoming connections.
1762 if ( s == AC_SOCKET_INVALID ) {
1763 int err = sock_errno();
1774 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1776 /* Stop listening until an existing session closes */
1778 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1781 Debug( LDAP_DEBUG_ANY,
1782 "daemon: accept(%ld) failed errno=%d (%s)\n",
1783 (long) sl->sl_sd, err, sock_errstr(err) );
1784 ldap_pvt_thread_yield();
1787 sfd = SLAP_SOCKNEW( s );
1789 /* make sure descriptor number isn't too great */
1790 if ( sfd >= dtblsize ) {
1791 Debug( LDAP_DEBUG_ANY,
1792 "daemon: %ld beyond descriptor table size %ld\n",
1793 (long) sfd, (long) dtblsize, 0 );
1796 ldap_pvt_thread_yield();
1801 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1802 /* newly accepted stream should not be in any of the FD SETS */
1803 assert( SLAP_SOCK_NOT_ACTIVE( sfd ));
1804 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1805 #endif /* LDAP_DEBUG */
1807 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1808 #ifdef LDAP_PF_LOCAL
1809 /* for IPv4 and IPv6 sockets only */
1810 if ( from.sa_addr.sa_family != AF_LOCAL )
1811 #endif /* LDAP_PF_LOCAL */
1816 /* enable keep alives */
1818 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1819 (char *) &tmp, sizeof(tmp) );
1820 if ( rc == AC_SOCKET_ERROR ) {
1821 int err = sock_errno();
1822 Debug( LDAP_DEBUG_ANY,
1823 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1824 "errno=%d (%s)\n", (long) sfd, err, sock_errstr(err) );
1826 #endif /* SO_KEEPALIVE */
1828 /* enable no delay */
1830 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1831 (char *)&tmp, sizeof(tmp) );
1832 if ( rc == AC_SOCKET_ERROR ) {
1833 int err = sock_errno();
1834 Debug( LDAP_DEBUG_ANY,
1835 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1836 "errno=%d (%s)\n", (long) sfd, err, sock_errstr(err) );
1838 #endif /* TCP_NODELAY */
1840 #endif /* SO_KEEPALIVE || TCP_NODELAY */
1842 Debug( LDAP_DEBUG_CONNS,
1843 "daemon: listen=%ld, new connection on %ld\n",
1844 (long) sl->sl_sd, (long) sfd, 0 );
1847 switch ( from.sa_addr.sa_family ) {
1848 # ifdef LDAP_PF_LOCAL
1850 cflag |= CONN_IS_IPC;
1852 /* FIXME: apparently accept doesn't fill
1853 * the sun_path sun_path member */
1854 if ( from.sa_un_addr.sun_path[0] == '\0' ) {
1855 AC_MEMCPY( from.sa_un_addr.sun_path,
1856 sl->sl_sa.sa_un_addr.sun_path,
1857 sizeof( from.sa_un_addr.sun_path ) );
1860 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1866 #ifdef LDAP_PF_LOCAL_SENDMSG
1867 peerbv.bv_val = peerbuf;
1868 peerbv.bv_len = sizeof( peerbuf );
1870 if( LUTIL_GETPEEREID( s, &uid, &gid, &peerbv ) == 0 ) {
1871 authid.bv_val = ch_malloc(
1872 STRLENOF( "gidNumber=4294967295+uidNumber=4294967295,"
1873 "cn=peercred,cn=external,cn=auth" ) + 1 );
1874 authid.bv_len = sprintf( authid.bv_val,
1875 "gidNumber=%d+uidNumber=%d,"
1876 "cn=peercred,cn=external,cn=auth",
1877 (int) gid, (int) uid );
1878 assert( authid.bv_len <=
1879 STRLENOF( "gidNumber=4294967295+uidNumber=4294967295,"
1880 "cn=peercred,cn=external,cn=auth" ) );
1885 #endif /* LDAP_PF_LOCAL */
1887 # ifdef LDAP_PF_INET6
1889 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1890 peeraddr = inet_ntoa( *((struct in_addr *)
1891 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1892 sprintf( peername, "IP=%s:%d",
1893 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1894 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1896 char addr[INET6_ADDRSTRLEN];
1898 peeraddr = (char *) inet_ntop( AF_INET6,
1899 &from.sa_in6_addr.sin6_addr,
1900 addr, sizeof addr );
1901 sprintf( peername, "IP=[%s]:%d",
1902 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1903 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1906 # endif /* LDAP_PF_INET6 */
1909 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1910 sprintf( peername, "IP=%s:%d",
1911 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1912 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1920 if ( ( from.sa_addr.sa_family == AF_INET )
1921 #ifdef LDAP_PF_INET6
1922 || ( from.sa_addr.sa_family == AF_INET6 )
1923 #endif /* LDAP_PF_INET6 */
1927 #ifdef SLAPD_RLOOKUPS
1928 if ( use_reverse_lookup ) {
1930 if (ldap_pvt_get_hname( (const struct sockaddr *)&from, len, hbuf,
1931 sizeof(hbuf), &herr ) == 0) {
1932 ldap_pvt_str2lower( hbuf );
1936 #endif /* SLAPD_RLOOKUPS */
1941 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_tcpd_mutex );
1942 rc = hosts_ctl("slapd",
1943 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1944 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1945 SLAP_STRING_UNKNOWN );
1946 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_tcpd_mutex );
1949 Statslog( LDAP_DEBUG_STATS,
1950 "fd=%ld DENIED from %s (%s)\n",
1952 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1953 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1959 #endif /* HAVE_TCPD */
1963 if ( sl->sl_is_tls ) cflag |= CONN_IS_TLS;
1965 c = connection_init(sfd, sl,
1966 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1967 peername, cflag, ssf,
1968 authid.bv_val ? &authid : NULL
1969 LDAP_PF_LOCAL_SENDMSG_ARG(&peerbv));
1971 if( authid.bv_val ) ch_free(authid.bv_val);
1974 Debug( LDAP_DEBUG_ANY,
1975 "daemon: connection_init(%ld, %s, %s) failed.\n",
1976 (long) sfd, peername, sl->sl_name.bv_val );
1981 Statslog( LDAP_DEBUG_STATS,
1982 "conn=%ld fd=%ld ACCEPT from %s (%s)\n",
1983 c->c_connid, (long) sfd, peername, sl->sl_name.bv_val,
1990 slap_listener_thread(
1995 Listener *sl = (Listener *)ptr;
1997 rc = slap_listener( sl );
1999 if( rc != LDAP_SUCCESS ) {
2000 Debug( LDAP_DEBUG_ANY,
2001 "slap_listener_thread(%s): failed err=%d",
2002 sl->sl_url.bv_val, rc, 0 );
2009 slap_listener_activate(
2014 Debug( LDAP_DEBUG_TRACE, "slap_listener_activate(%d): %s\n",
2015 sl->sl_sd, sl->sl_busy ? "busy" : "", 0 );
2019 rc = ldap_pvt_thread_pool_submit( &connection_pool,
2020 slap_listener_thread, (void *) sl );
2023 Debug( LDAP_DEBUG_ANY,
2024 "listener_activate(%d): submit failed (%d)\n",
2035 time_t last_idle_check = 0;
2036 struct timeval idle;
2039 #define SLAPD_IDLE_CHECK_LIMIT 4
2041 if ( global_idletimeout > 0 ) {
2042 last_idle_check = slap_get_time();
2043 /* Set the select timeout.
2044 * Don't just truncate, preserve the fractions of
2045 * seconds to prevent sleeping for zero time.
2047 idle.tv_sec = global_idletimeout / SLAPD_IDLE_CHECK_LIMIT;
2048 idle.tv_usec = global_idletimeout - \
2049 ( idle.tv_sec * SLAPD_IDLE_CHECK_LIMIT );
2050 idle.tv_usec *= 1000000 / SLAPD_IDLE_CHECK_LIMIT;
2056 slapd_add( wake_sds[0], 0, NULL );
2058 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2059 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ) continue;
2061 #ifdef LDAP_CONNECTIONLESS
2062 /* Since this is connectionless, the data port is the
2063 * listening port. The listen() and accept() calls
2066 if ( slap_listeners[l]->sl_is_udp )
2068 #endif /* LDAP_CONNECTIONLESS */
2070 if ( listen( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ), SLAPD_LISTEN_BACKLOG ) == -1 ) {
2071 int err = sock_errno();
2073 #ifdef LDAP_PF_INET6
2074 /* If error is EADDRINUSE, we are trying to listen to INADDR_ANY and
2075 * we are already listening to in6addr_any, then we want to ignore
2076 * this and continue.
2078 if ( err == EADDRINUSE ) {
2080 struct sockaddr_in sa = slap_listeners[l]->sl_sa.sa_in_addr;
2081 struct sockaddr_in6 sa6;
2083 if ( sa.sin_family == AF_INET &&
2084 sa.sin_addr.s_addr == htonl(INADDR_ANY) ) {
2085 for ( i = 0 ; i < l; i++ ) {
2086 sa6 = slap_listeners[i]->sl_sa.sa_in6_addr;
2087 if ( sa6.sin6_family == AF_INET6 &&
2088 !memcmp( &sa6.sin6_addr, &in6addr_any,
2089 sizeof(struct in6_addr) ) )
2096 /* We are already listening to in6addr_any */
2097 Debug( LDAP_DEBUG_CONNS,
2098 "daemon: Attempt to listen to 0.0.0.0 failed, "
2099 "already listening on ::, assuming IPv4 included\n",
2101 slapd_close( slap_listeners[l]->sl_sd );
2102 slap_listeners[l]->sl_sd = AC_SOCKET_INVALID;
2107 #endif /* LDAP_PF_INET6 */
2108 Debug( LDAP_DEBUG_ANY,
2109 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
2110 slap_listeners[l]->sl_url.bv_val, err,
2115 /* make the listening socket non-blocking */
2116 if ( ber_pvt_socket_set_nonblock( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ), 1 ) < 0 ) {
2117 Debug( LDAP_DEBUG_ANY, "slapd_daemon_task: "
2118 "set nonblocking on a listening socket failed\n",
2124 slapd_add( slap_listeners[l]->sl_sd, 0, slap_listeners[l] );
2127 #ifdef HAVE_NT_SERVICE_MANAGER
2128 if ( started_event != NULL ) {
2129 ldap_pvt_thread_cond_signal( &started_event );
2131 #endif /* HAVE_NT_SERVICE_MANAGER */
2133 /* initialization complete. Here comes the loop. */
2135 while ( !slapd_shutdown ) {
2140 #if SLAP_EVENTS_ARE_INDEXED
2141 ber_socket_t nrfds, nwfds;
2142 #endif /* SLAP_EVENTS_ARE_INDEXED */
2143 #define SLAPD_EBADF_LIMIT 16
2150 struct timeval *tvp;
2156 now = slap_get_time();
2158 if ( ( global_idletimeout > 0 ) &&
2159 difftime( last_idle_check +
2160 global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 )
2162 connections_timeout_idle( now );
2163 last_idle_check = now;
2168 if ( slapd_gentle_shutdown ) {
2169 ber_socket_t active;
2171 if ( slapd_gentle_shutdown == 1 ) {
2173 Debug( LDAP_DEBUG_ANY, "slapd gentle shutdown\n", 0, 0, 0 );
2174 close_listeners( 1 );
2175 frontendDB->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
2176 LDAP_STAILQ_FOREACH(be, &backendDB, be_next) {
2177 be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
2179 slapd_gentle_shutdown = 2;
2182 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
2183 active = slap_daemon.sd_nactives;
2184 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
2185 if ( active == 0 ) {
2193 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
2195 nwriters = slap_daemon.sd_nwriters;
2197 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2198 Listener *lr = slap_listeners[l];
2200 if ( lr->sl_sd == AC_SOCKET_INVALID ) continue;
2202 if ( lr->sl_mute || lr->sl_busy )
2204 SLAP_SOCK_CLR_READ( lr->sl_sd );
2206 SLAP_SOCK_SET_READ( lr->sl_sd );
2212 nfds = SLAP_EVENT_MAX;
2214 if ( global_idletimeout && slap_daemon.sd_nactives ) at = 1;
2216 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
2219 #if defined(HAVE_YIELDING_SELECT) || defined(NO_THREADS)
2220 && ( tv.tv_sec || tv.tv_usec )
2221 #endif /* HAVE_YIELDING_SELECT || NO_THREADS */
2229 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2230 rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
2231 while ( rtask && cat.tv_sec && cat.tv_sec <= now ) {
2232 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
2233 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
2235 ldap_pvt_runqueue_runtask( &slapd_rq, rtask );
2236 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
2237 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2238 ldap_pvt_thread_pool_submit( &connection_pool,
2239 rtask->routine, (void *) rtask );
2240 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2242 rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
2244 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2246 if ( rtask && cat.tv_sec ) {
2247 /* NOTE: diff __should__ always be >= 0,
2248 * AFAI understand; however (ITS#4872),
2249 * time_t might be unsigned in some systems,
2250 * while difftime() returns a double */
2251 double diff = difftime( cat.tv_sec, now );
2255 if ( tvp == NULL || diff < tv.tv_sec ) {
2262 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2263 Listener *lr = slap_listeners[l];
2265 if ( lr->sl_sd == AC_SOCKET_INVALID ) {
2269 if ( lr->sl_mute ) {
2270 Debug( LDAP_DEBUG_CONNS,
2271 "daemon: " SLAP_EVENT_FNAME ": "
2272 "listen=%d muted\n",
2277 if ( lr->sl_busy ) {
2278 Debug( LDAP_DEBUG_CONNS,
2279 "daemon: " SLAP_EVENT_FNAME ": "
2285 Debug( LDAP_DEBUG_CONNS,
2286 "daemon: " SLAP_EVENT_FNAME ": "
2287 "listen=%d active_threads=%d tvp=%s\n",
2288 lr->sl_sd, at, tvp == NULL ? "NULL" : "zero" );
2291 SLAP_EVENT_WAIT( tvp, &ns );
2293 case -1: { /* failure - try again */
2294 int err = sock_errno();
2296 if ( err != EINTR ) {
2299 /* Don't log unless we got it twice in a row */
2300 if ( !( ebadf & 1 ) ) {
2301 Debug( LDAP_DEBUG_ANY,
2307 sock_errstr( err ) );
2309 if ( ebadf >= SLAPD_EBADF_LIMIT ) {
2316 case 0: /* timeout - let threads run */
2318 #ifndef HAVE_YIELDING_SELECT
2319 Debug( LDAP_DEBUG_CONNS, "daemon: " SLAP_EVENT_FNAME
2320 "timeout - yielding\n",
2323 ldap_pvt_thread_yield();
2324 #endif /* ! HAVE_YIELDING_SELECT */
2327 default: /* something happened - deal with it */
2328 if ( slapd_shutdown ) continue;
2331 Debug( LDAP_DEBUG_CONNS,
2332 "daemon: activity on %d descriptor%s\n",
2333 ns, ns != 1 ? "s" : "", 0 );
2337 #if SLAP_EVENTS_ARE_INDEXED
2338 if ( SLAP_EVENT_IS_READ( wake_sds[0] ) ) {
2340 SLAP_EVENT_CLR_READ( wake_sds[0] );
2342 tcp_read( SLAP_FD2SOCK(wake_sds[0]), c, sizeof(c) );
2343 Debug( LDAP_DEBUG_CONNS, "daemon: waked\n", 0, 0, 0 );
2347 /* The event slot equals the descriptor number - this is
2348 * true for Unix select and poll. We treat Windows select
2349 * like this too, even though it's a kludge.
2351 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2354 if ( ns <= 0 ) break;
2355 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ) continue;
2356 #ifdef LDAP_CONNECTIONLESS
2357 if ( slap_listeners[l]->sl_is_udp ) continue;
2358 #endif /* LDAP_CONNECTIONLESS */
2359 if ( !SLAP_EVENT_IS_READ( slap_listeners[l]->sl_sd ) ) continue;
2362 SLAP_EVENT_CLR_READ( slap_listeners[l]->sl_sd );
2363 SLAP_EVENT_CLR_WRITE( slap_listeners[l]->sl_sd );
2366 rc = slap_listener_activate( slap_listeners[l] );
2369 /* bypass the following tests if no descriptors left */
2371 #ifndef HAVE_YIELDING_SELECT
2372 ldap_pvt_thread_yield();
2373 #endif /* HAVE_YIELDING_SELECT */
2377 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
2380 for ( i = 0; i < nfds; i++ ) {
2383 r = SLAP_EVENT_IS_READ( i );
2384 /* writefds was not initialized if nwriters was zero */
2385 w = nwriters ? SLAP_EVENT_IS_WRITE( i ) : 0;
2387 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
2388 r ? "r" : "", w ? "w" : "" );
2398 if ( ns <= 0 ) break;
2400 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
2402 /* loop through the writers */
2403 for ( i = 0; nwfds > 0; i++ ) {
2405 if ( ! SLAP_EVENT_IS_WRITE( i ) ) continue;
2408 SLAP_EVENT_CLR_WRITE( wd );
2411 Debug( LDAP_DEBUG_CONNS,
2412 "daemon: write active on %d\n",
2416 * NOTE: it is possible that the connection was closed
2417 * and that the stream is now inactive.
2418 * connection_write() must validate the stream is still
2421 * ITS#4338: if the stream is invalid, there is no need to
2422 * close it here. It has already been closed in connection.c.
2424 if ( connection_write( wd ) < 0 ) {
2425 if ( SLAP_EVENT_IS_READ( wd ) ) {
2426 SLAP_EVENT_CLR_READ( (unsigned) wd );
2432 for ( i = 0; nrfds > 0; i++ ) {
2434 if ( ! SLAP_EVENT_IS_READ( i ) ) continue;
2436 SLAP_EVENT_CLR_READ( rd );
2439 Debug ( LDAP_DEBUG_CONNS,
2440 "daemon: read activity on %d\n", rd, 0, 0 );
2442 * NOTE: it is possible that the connection was closed
2443 * and that the stream is now inactive.
2444 * connection_read() must valid the stream is still
2448 connection_read_activate( rd );
2450 #else /* !SLAP_EVENTS_ARE_INDEXED */
2452 /* The events are returned in an arbitrary list. This is true
2453 * for /dev/poll, epoll and kqueue. In order to prioritize things
2454 * so that we can handle wake_sds first, listeners second, and then
2455 * all other connections last (as we do for select), we would need
2456 * to use multiple event handles and cascade them.
2458 * That seems like a bit of hassle. So the wake_sds check has been
2459 * skipped. For epoll and kqueue we can associate arbitrary data with
2460 * an event, so we could use pointers to the listener structure
2461 * instead of just the file descriptor. For /dev/poll we have to
2462 * search the listeners array for a matching descriptor.
2464 * We now handle wake events when we see them; they are not given
2468 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
2470 for ( i = 0; i < ns; i++ ) {
2473 /* Don't log listener events */
2474 if ( SLAP_EVENT_IS_LISTENER( i )
2475 #ifdef LDAP_CONNECTIONLESS
2476 && !( (SLAP_EVENT_LISTENER( i ))->sl_is_udp )
2477 #endif /* LDAP_CONNECTIONLESS */
2483 fd = SLAP_EVENT_FD( i );
2484 /* Don't log internal wake events */
2485 if ( fd == wake_sds[0] ) continue;
2487 r = SLAP_EVENT_IS_READ( i );
2488 w = SLAP_EVENT_IS_WRITE( i );
2490 Debug( LDAP_DEBUG_CONNS, " %d%s%s", fd,
2491 r ? "r" : "", w ? "w" : "" );
2494 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
2495 #endif /* LDAP_DEBUG */
2497 for ( i = 0; i < ns; i++ ) {
2498 int rc = 1, fd, w = 0;
2500 if ( SLAP_EVENT_IS_LISTENER( i ) ) {
2501 rc = slap_listener_activate( SLAP_EVENT_LISTENER( i ) );
2504 /* If we found a regular listener, rc is now zero, and we
2505 * can skip the data portion. But if it was a UDP listener
2506 * then rc is still 1, and we want to handle the data.
2509 fd = SLAP_EVENT_FD( i );
2511 /* Handle wake events */
2512 if ( fd == wake_sds[0] ) {
2515 tcp_read( SLAP_FD2SOCK(wake_sds[0]), c, sizeof(c) );
2519 if ( SLAP_EVENT_IS_WRITE( i ) ) {
2520 Debug( LDAP_DEBUG_CONNS,
2521 "daemon: write active on %d\n",
2524 SLAP_EVENT_CLR_WRITE( i );
2528 * NOTE: it is possible that the connection was closed
2529 * and that the stream is now inactive.
2530 * connection_write() must valid the stream is still
2533 if ( connection_write( fd ) < 0 ) {
2537 /* If event is a read */
2538 if ( SLAP_EVENT_IS_READ( i ) ) {
2539 Debug( LDAP_DEBUG_CONNS,
2540 "daemon: read active on %d\n",
2543 SLAP_EVENT_CLR_READ( i );
2544 connection_read_activate( fd );
2546 Debug( LDAP_DEBUG_CONNS,
2547 "daemon: hangup on %d\n", fd, 0, 0 );
2548 connection_hangup( fd );
2552 #endif /* SLAP_EVENTS_ARE_INDEXED */
2554 #ifndef HAVE_YIELDING_SELECT
2555 ldap_pvt_thread_yield();
2556 #endif /* ! HAVE_YIELDING_SELECT */
2559 if ( slapd_shutdown == 1 ) {
2560 Debug( LDAP_DEBUG_ANY,
2561 "daemon: shutdown requested and initiated.\n",
2564 } else if ( slapd_shutdown == 2 ) {
2565 #ifdef HAVE_NT_SERVICE_MANAGER
2566 Debug( LDAP_DEBUG_ANY,
2567 "daemon: shutdown initiated by Service Manager.\n",
2569 #else /* !HAVE_NT_SERVICE_MANAGER */
2570 Debug( LDAP_DEBUG_ANY,
2571 "daemon: abnormal condition, shutdown initiated.\n",
2573 #endif /* !HAVE_NT_SERVICE_MANAGER */
2575 Debug( LDAP_DEBUG_ANY,
2576 "daemon: no active streams, shutdown initiated.\n",
2580 if ( slapd_gentle_shutdown != 2 ) close_listeners ( 0 );
2582 if ( !slapd_gentle_shutdown ) {
2583 slapd_abrupt_shutdown = 1;
2584 connections_shutdown();
2587 if ( LogTest( LDAP_DEBUG_ANY )) {
2588 int t = ldap_pvt_thread_pool_backload( &connection_pool );
2589 Debug( LDAP_DEBUG_ANY,
2590 "slapd shutdown: waiting for %d operations/tasks to finish\n",
2593 ldap_pvt_thread_pool_destroy( &connection_pool, 1 );
2595 free( slap_listeners );
2596 slap_listeners = NULL;
2602 #ifdef LDAP_CONNECTIONLESS
2604 connectionless_init( void )
2608 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2609 Listener *lr = slap_listeners[l];
2612 if ( !lr->sl_is_udp ) {
2616 c = connection_init( lr->sl_sd, lr, "", "",
2617 CONN_IS_UDP, (slap_ssf_t) 0, NULL
2618 LDAP_PF_LOCAL_SENDMSG_ARG(NULL));
2621 Debug( LDAP_DEBUG_TRACE,
2622 "connectionless_init: failed on %s (%d)\n",
2623 lr->sl_url, lr->sl_sd, 0 );
2631 #endif /* LDAP_CONNECTIONLESS */
2634 slapd_daemon( void )
2638 #ifdef LDAP_CONNECTIONLESS
2639 connectionless_init();
2640 #endif /* LDAP_CONNECTIONLESS */
2642 #define SLAPD_LISTENER_THREAD 1
2643 #if defined( SLAPD_LISTENER_THREAD )
2645 ldap_pvt_thread_t listener_tid;
2647 /* listener as a separate THREAD */
2648 rc = ldap_pvt_thread_create( &listener_tid,
2649 0, slapd_daemon_task, NULL );
2652 Debug( LDAP_DEBUG_ANY,
2653 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
2657 /* wait for the listener thread to complete */
2658 ldap_pvt_thread_join( listener_tid, (void *)NULL );
2660 #else /* ! SLAPD_LISTENER_THREAD */
2661 /* experimental code */
2662 slapd_daemon_task( NULL );
2663 #endif /* ! SLAPD_LISTENER_THREAD */
2671 #if defined( HAVE_WINSOCK2 )
2672 WORD wVersionRequested;
2676 wVersionRequested = MAKEWORD( 2, 0 );
2678 err = WSAStartup( wVersionRequested, &wsaData );
2680 /* Tell the user that we couldn't find a usable */
2685 /* Confirm that the WinSock DLL supports 2.0.*/
2686 /* Note that if the DLL supports versions greater */
2687 /* than 2.0 in addition to 2.0, it will still return */
2688 /* 2.0 in wVersion since that is the version we */
2691 if ( LOBYTE( wsaData.wVersion ) != 2 ||
2692 HIBYTE( wsaData.wVersion ) != 0 )
2694 /* Tell the user that we couldn't find a usable */
2700 /* The WinSock DLL is acceptable. Proceed. */
2701 #elif defined( HAVE_WINSOCK )
2703 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) return -1;
2704 #endif /* ! HAVE_WINSOCK2 && ! HAVE_WINSOCK */
2712 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
2714 #endif /* HAVE_WINSOCK2 || HAVE_WINSOCK */
2721 slap_sig_shutdown( int sig )
2724 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
2728 * If the NT Service Manager is controlling the server, we don't
2729 * want SIGBREAK to kill the server. For some strange reason,
2730 * SIGBREAK is generated when a user logs out.
2733 #if defined(HAVE_NT_SERVICE_MANAGER) && defined(SIGBREAK)
2734 if (is_NT_Service && sig == SIGBREAK) {
2737 #endif /* HAVE_NT_SERVICE_MANAGER && SIGBREAK */
2739 if (sig == SIGHUP && global_gentlehup && slapd_gentle_shutdown == 0) {
2740 slapd_gentle_shutdown = 1;
2749 /* reinstall self */
2750 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
2754 slap_sig_wake( int sig )
2758 /* reinstall self */
2759 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
2764 slapd_add_internal( ber_socket_t s, int isactive )
2766 slapd_add( s, isactive, NULL );
2770 slapd_get_listeners( void )
2772 return slap_listeners;
2776 slap_wake_listener()