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 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 ***************************************/
165 #undef SLAP_EVENT_ACK /* events trigger once per descriptor */
167 # define SLAP_EVENT_FNAME "epoll"
168 # define SLAP_EVENTS_ARE_INDEXED 0
169 # define SLAP_EPOLL_SOCK_IX(s) (slap_daemon.sd_index[(s)])
170 # define SLAP_EPOLL_SOCK_EP(s) (slap_daemon.sd_epolls[SLAP_EPOLL_SOCK_IX(s)])
171 # define SLAP_EPOLL_SOCK_EV(s) (SLAP_EPOLL_SOCK_EP(s).events)
172 # define SLAP_SOCK_IS_ACTIVE(s) (SLAP_EPOLL_SOCK_IX(s) != -1)
173 # define SLAP_SOCK_NOT_ACTIVE(s) (SLAP_EPOLL_SOCK_IX(s) == -1)
174 # define SLAP_EPOLL_SOCK_IS_SET(s, mode) (SLAP_EPOLL_SOCK_EV(s) & (mode))
176 # define SLAP_SOCK_IS_READ(s) SLAP_EPOLL_SOCK_IS_SET((s), EPOLLIN)
177 # define SLAP_SOCK_IS_WRITE(s) SLAP_EPOLL_SOCK_IS_SET((s), EPOLLOUT)
179 # define SLAP_EPOLL_SOCK_SET(s, mode) do { \
180 if ( (SLAP_EPOLL_SOCK_EV(s) & (mode)) != (mode) ) { \
181 SLAP_EPOLL_SOCK_EV(s) |= (mode); \
182 epoll_ctl( slap_daemon.sd_epfd, EPOLL_CTL_MOD, (s), \
183 &SLAP_EPOLL_SOCK_EP(s) ); \
187 # define SLAP_EPOLL_SOCK_CLR(s, mode) do { \
188 if ( (SLAP_EPOLL_SOCK_EV(s) & (mode)) ) { \
189 SLAP_EPOLL_SOCK_EV(s) &= ~(mode); \
190 epoll_ctl( slap_daemon.sd_epfd, EPOLL_CTL_MOD, s, \
191 &SLAP_EPOLL_SOCK_EP(s) ); \
195 # define SLAP_SOCK_SET_READ(s) SLAP_EPOLL_SOCK_SET(s, EPOLLIN)
196 # define SLAP_SOCK_SET_WRITE(s) SLAP_EPOLL_SOCK_SET(s, EPOLLOUT)
198 # define SLAP_SOCK_CLR_READ(s) SLAP_EPOLL_SOCK_CLR((s), EPOLLIN)
199 # define SLAP_SOCK_CLR_WRITE(s) SLAP_EPOLL_SOCK_CLR((s), EPOLLOUT)
201 # define SLAP_SOCK_SET_SUSPEND(s) \
202 ( slap_daemon.sd_suspend[SLAP_EPOLL_SOCK_IX(s)] = 1 )
203 # define SLAP_SOCK_CLR_SUSPEND(s) \
204 ( slap_daemon.sd_suspend[SLAP_EPOLL_SOCK_IX(s)] = 0 )
205 # define SLAP_SOCK_IS_SUSPEND(s) \
206 ( slap_daemon.sd_suspend[SLAP_EPOLL_SOCK_IX(s)] == 1 )
208 # define SLAP_EPOLL_EVENT_CLR(i, mode) (revents[(i)].events &= ~(mode))
210 # define SLAP_EVENT_MAX slap_daemon.sd_nfds
212 /* If a Listener address is provided, store that as the epoll data.
213 * Otherwise, store the address of this socket's slot in the
214 * index array. If we can't do this add, the system is out of
215 * resources and we need to shutdown.
217 # define SLAP_SOCK_ADD(s, l) do { \
219 SLAP_EPOLL_SOCK_IX((s)) = slap_daemon.sd_nfds; \
220 SLAP_EPOLL_SOCK_EP((s)).data.ptr = (l) ? (l) : (void *)(&SLAP_EPOLL_SOCK_IX(s)); \
221 SLAP_EPOLL_SOCK_EV((s)) = EPOLLIN|EPOLLET; \
222 rc = epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_ADD, \
223 (s), &SLAP_EPOLL_SOCK_EP((s))); \
225 slap_daemon.sd_nfds++; \
227 Debug( LDAP_DEBUG_ANY, \
228 "daemon: epoll_ctl(ADD,fd=%d) failed, errno=%d, shutting down\n", \
230 slapd_shutdown = 2; \
234 # define SLAP_EPOLL_EV_LISTENER(ptr) \
235 (((int *)(ptr) >= slap_daemon.sd_index && \
236 (int *)(ptr) <= &slap_daemon.sd_index[dtblsize]) ? 0 : 1 )
238 # define SLAP_EPOLL_EV_PTRFD(ptr) (SLAP_EPOLL_EV_LISTENER(ptr) ? \
239 ((Listener *)ptr)->sl_sd : \
240 (ber_socket_t) ((int *)(ptr) - slap_daemon.sd_index))
242 # define SLAP_SOCK_DEL(s) do { \
243 int fd, rc, index = SLAP_EPOLL_SOCK_IX((s)); \
244 if ( index < 0 ) break; \
245 rc = epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_DEL, \
246 (s), &SLAP_EPOLL_SOCK_EP((s))); \
247 slap_daemon.sd_epolls[index] = \
248 slap_daemon.sd_epolls[slap_daemon.sd_nfds-1]; \
249 fd = SLAP_EPOLL_EV_PTRFD(slap_daemon.sd_epolls[index].data.ptr); \
250 slap_daemon.sd_index[fd] = index; \
251 slap_daemon.sd_index[(s)] = -1; \
252 slap_daemon.sd_nfds--; \
255 # define SLAP_EVENT_CLR_READ(i) SLAP_EPOLL_EVENT_CLR((i), EPOLLIN)
256 # define SLAP_EVENT_CLR_WRITE(i) SLAP_EPOLL_EVENT_CLR((i), EPOLLOUT)
258 # define SLAP_EPOLL_EVENT_CHK(i, mode) (revents[(i)].events & mode)
260 # define SLAP_EVENT_IS_READ(i) SLAP_EPOLL_EVENT_CHK((i), EPOLLIN)
261 # define SLAP_EVENT_IS_WRITE(i) SLAP_EPOLL_EVENT_CHK((i), EPOLLOUT)
262 # define SLAP_EVENT_IS_LISTENER(i) SLAP_EPOLL_EV_LISTENER(revents[(i)].data.ptr)
263 # define SLAP_EVENT_LISTENER(i) ((Listener *)(revents[(i)].data.ptr))
265 # define SLAP_EVENT_FD(i) SLAP_EPOLL_EV_PTRFD(revents[(i)].data.ptr)
267 # define SLAP_SOCK_INIT do { \
268 slap_daemon.sd_epolls = ch_calloc(1, \
269 ( sizeof(struct epoll_event) * 2 \
270 + sizeof(int) ) * dtblsize * 2); \
271 slap_daemon.sd_index = (int *)&slap_daemon.sd_epolls[ 2 * dtblsize ]; \
272 slap_daemon.sd_epfd = epoll_create( dtblsize ); \
273 for ( i = 0; i < dtblsize; i++ ) slap_daemon.sd_index[i] = -1; \
276 # define SLAP_SOCK_DESTROY do { \
277 if ( slap_daemon.sd_epolls != NULL ) { \
278 ch_free( slap_daemon.sd_epolls ); \
279 slap_daemon.sd_epolls = NULL; \
280 slap_daemon.sd_index = NULL; \
281 close( slap_daemon.sd_epfd ); \
285 # define SLAP_EVENT_DECL struct epoll_event *revents
287 # define SLAP_EVENT_INIT do { \
288 revents = slap_daemon.sd_epolls + dtblsize; \
291 # define SLAP_EVENT_WAIT(tvp, nsp) do { \
292 *(nsp) = epoll_wait( slap_daemon.sd_epfd, revents, \
293 dtblsize, (tvp) ? (tvp)->tv_sec * 1000 : -1 ); \
296 #elif defined(SLAP_X_DEVPOLL) && defined(HAVE_DEVPOLL)
297 /*************************************************************
298 * Use Solaris' (>= 2.7) /dev/poll infrastructure - poll(7d) *
299 *************************************************************/
301 #define SLAP_EVENT_ACK 1 /* events keep signalling unless we stop them */
303 # define SLAP_EVENT_FNAME "/dev/poll"
304 # define SLAP_EVENTS_ARE_INDEXED 0
306 * - sd_index is used much like with epoll()
307 * - sd_l is maintained as an array containing the address
308 * of the listener; the index is the fd itself
309 * - sd_pollfd is used to keep track of what data has been
310 * registered in /dev/poll
312 # define SLAP_DEVPOLL_SOCK_IX(s) (slap_daemon.sd_index[(s)])
313 # define SLAP_DEVPOLL_SOCK_LX(s) (slap_daemon.sd_l[(s)])
314 # define SLAP_DEVPOLL_SOCK_EP(s) (slap_daemon.sd_pollfd[SLAP_DEVPOLL_SOCK_IX((s))])
315 # define SLAP_DEVPOLL_SOCK_FD(s) (SLAP_DEVPOLL_SOCK_EP((s)).fd)
316 # define SLAP_DEVPOLL_SOCK_EV(s) (SLAP_DEVPOLL_SOCK_EP((s)).events)
317 # define SLAP_SOCK_IS_ACTIVE(s) (SLAP_DEVPOLL_SOCK_IX((s)) != -1)
318 # define SLAP_SOCK_NOT_ACTIVE(s) (SLAP_DEVPOLL_SOCK_IX((s)) == -1)
319 # define SLAP_SOCK_IS_SET(s, mode) (SLAP_DEVPOLL_SOCK_EV((s)) & (mode))
321 # define SLAP_SOCK_IS_READ(s) SLAP_SOCK_IS_SET((s), POLLIN)
322 # define SLAP_SOCK_IS_WRITE(s) SLAP_SOCK_IS_SET((s), POLLOUT)
324 /* as far as I understand, any time we need to communicate with the kernel
325 * about the number and/or properties of a file descriptor we need it to
326 * wait for, we have to rewrite the whole set */
327 # define SLAP_DEVPOLL_WRITE_POLLFD(s, pfd, n, what, shdn) do { \
329 size_t size = (n) * sizeof( struct pollfd ); \
330 /* FIXME: use pwrite? */ \
331 rc = write( slap_daemon.sd_dpfd, (pfd), size ); \
332 if ( rc != size ) { \
333 Debug( LDAP_DEBUG_ANY, "daemon: " SLAP_EVENT_FNAME ": " \
334 "%s fd=%d failed errno=%d\n", \
335 (what), (s), errno ); \
337 slapd_shutdown = 2; \
342 # define SLAP_DEVPOLL_SOCK_SET(s, mode) do { \
343 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_SET_%s(%d)=%d\n", \
344 (mode) == POLLIN ? "READ" : "WRITE", (s), \
345 ( (SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) != (mode) ) ); \
346 if ( (SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) != (mode) ) { \
348 SLAP_DEVPOLL_SOCK_EV((s)) |= (mode); \
349 pfd.fd = SLAP_DEVPOLL_SOCK_FD((s)); \
350 pfd.events = /* (mode) */ SLAP_DEVPOLL_SOCK_EV((s)); \
351 SLAP_DEVPOLL_WRITE_POLLFD((s), &pfd, 1, "SET", 0); \
355 # define SLAP_DEVPOLL_SOCK_CLR(s, mode) do { \
356 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_CLR_%s(%d)=%d\n", \
357 (mode) == POLLIN ? "READ" : "WRITE", (s), \
358 ( (SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) == (mode) ) ); \
359 if ((SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) == (mode) ) { \
360 struct pollfd pfd[2]; \
361 SLAP_DEVPOLL_SOCK_EV((s)) &= ~(mode); \
362 pfd[0].fd = SLAP_DEVPOLL_SOCK_FD((s)); \
363 pfd[0].events = POLLREMOVE; \
364 pfd[1] = SLAP_DEVPOLL_SOCK_EP((s)); \
365 SLAP_DEVPOLL_WRITE_POLLFD((s), &pfd[0], 2, "CLR", 0); \
369 # define SLAP_SOCK_SET_READ(s) SLAP_DEVPOLL_SOCK_SET(s, POLLIN)
370 # define SLAP_SOCK_SET_WRITE(s) SLAP_DEVPOLL_SOCK_SET(s, POLLOUT)
372 # define SLAP_SOCK_CLR_READ(s) SLAP_DEVPOLL_SOCK_CLR((s), POLLIN)
373 # define SLAP_SOCK_CLR_WRITE(s) SLAP_DEVPOLL_SOCK_CLR((s), POLLOUT)
375 # define SLAP_SOCK_SET_SUSPEND(s) \
376 ( slap_daemon.sd_suspend[SLAP_DEVPOLL_SOCK_IX((s))] = 1 )
377 # define SLAP_SOCK_CLR_SUSPEND(s) \
378 ( slap_daemon.sd_suspend[SLAP_DEVPOLL_SOCK_IX((s))] = 0 )
379 # define SLAP_SOCK_IS_SUSPEND(s) \
380 ( slap_daemon.sd_suspend[SLAP_DEVPOLL_SOCK_IX((s))] == 1 )
382 # define SLAP_DEVPOLL_EVENT_CLR(i, mode) (revents[(i)].events &= ~(mode))
384 # define SLAP_EVENT_MAX slap_daemon.sd_nfds
386 /* If a Listener address is provided, store that in the sd_l array.
387 * If we can't do this add, the system is out of resources and we
390 # define SLAP_SOCK_ADD(s, l) do { \
391 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_ADD(%d, %p)\n", (s), (l), 0 ); \
392 SLAP_DEVPOLL_SOCK_IX((s)) = slap_daemon.sd_nfds; \
393 SLAP_DEVPOLL_SOCK_LX((s)) = (l); \
394 SLAP_DEVPOLL_SOCK_FD((s)) = (s); \
395 SLAP_DEVPOLL_SOCK_EV((s)) = POLLIN; \
396 SLAP_DEVPOLL_WRITE_POLLFD((s), &SLAP_DEVPOLL_SOCK_EP((s)), 1, "ADD", 1); \
397 slap_daemon.sd_nfds++; \
400 # define SLAP_DEVPOLL_EV_LISTENER(ptr) ((ptr) != NULL)
402 # define SLAP_SOCK_DEL(s) do { \
403 int fd, index = SLAP_DEVPOLL_SOCK_IX((s)); \
404 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_DEL(%d)\n", (s), 0, 0 ); \
405 if ( index < 0 ) break; \
406 if ( index < slap_daemon.sd_nfds - 1 ) { \
407 struct pollfd pfd = slap_daemon.sd_pollfd[index]; \
408 fd = slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1].fd; \
409 slap_daemon.sd_pollfd[index] = slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1]; \
410 slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1] = pfd; \
411 slap_daemon.sd_index[fd] = index; \
413 slap_daemon.sd_index[(s)] = -1; \
414 slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1].events = POLLREMOVE; \
415 SLAP_DEVPOLL_WRITE_POLLFD((s), &slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1], 1, "DEL", 0); \
416 slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1].events = 0; \
417 slap_daemon.sd_nfds--; \
420 # define SLAP_EVENT_CLR_READ(i) SLAP_DEVPOLL_EVENT_CLR((i), POLLIN)
421 # define SLAP_EVENT_CLR_WRITE(i) SLAP_DEVPOLL_EVENT_CLR((i), POLLOUT)
423 # define SLAP_DEVPOLL_EVENT_CHK(i, mode) (revents[(i)].events & (mode))
425 # define SLAP_EVENT_FD(i) (revents[(i)].fd)
427 # define SLAP_EVENT_IS_READ(i) SLAP_DEVPOLL_EVENT_CHK((i), POLLIN)
428 # define SLAP_EVENT_IS_WRITE(i) SLAP_DEVPOLL_EVENT_CHK((i), POLLOUT)
429 # define SLAP_EVENT_IS_LISTENER(i) SLAP_DEVPOLL_EV_LISTENER(SLAP_DEVPOLL_SOCK_LX(SLAP_EVENT_FD((i))))
430 # define SLAP_EVENT_LISTENER(i) SLAP_DEVPOLL_SOCK_LX(SLAP_EVENT_FD((i)))
432 # define SLAP_SOCK_INIT do { \
433 slap_daemon.sd_pollfd = ch_calloc( 1, \
434 ( sizeof(struct pollfd) * 2 \
436 + sizeof( Listener * ) ) * dtblsize ); \
437 slap_daemon.sd_index = (int *)&slap_daemon.sd_pollfd[ 2 * dtblsize ]; \
438 slap_daemon.sd_l = (Listener **)&slap_daemon.sd_index[ dtblsize ]; \
439 slap_daemon.sd_dpfd = open( SLAP_EVENT_FNAME, O_RDWR ); \
440 if ( slap_daemon.sd_dpfd == -1 ) { \
441 Debug( LDAP_DEBUG_ANY, "daemon: " SLAP_EVENT_FNAME ": " \
442 "open(\"" SLAP_EVENT_FNAME "\") failed errno=%d\n", \
447 for ( i = 0; i < dtblsize; i++ ) { \
448 slap_daemon.sd_pollfd[i].fd = -1; \
449 slap_daemon.sd_index[i] = -1; \
453 # define SLAP_SOCK_DESTROY do { \
454 if ( slap_daemon.sd_pollfd != NULL ) { \
455 ch_free( slap_daemon.sd_pollfd ); \
456 slap_daemon.sd_pollfd = NULL; \
457 slap_daemon.sd_index = NULL; \
458 slap_daemon.sd_l = NULL; \
459 close( slap_daemon.sd_dpfd ); \
463 # define SLAP_EVENT_DECL struct pollfd *revents
465 # define SLAP_EVENT_INIT do { \
466 revents = &slap_daemon.sd_pollfd[ dtblsize ]; \
469 # define SLAP_EVENT_WAIT(tvp, nsp) do { \
470 struct dvpoll sd_dvpoll; \
471 sd_dvpoll.dp_timeout = (tvp) ? (tvp)->tv_sec * 1000 : -1; \
472 sd_dvpoll.dp_nfds = dtblsize; \
473 sd_dvpoll.dp_fds = revents; \
474 *(nsp) = ioctl( slap_daemon.sd_dpfd, DP_POLL, &sd_dvpoll ); \
477 #else /* ! epoll && ! /dev/poll */
479 #define SLAP_EVENT_ACK 1 /* events keep signalling unless we stop them */
482 # define SLAP_EVENT_FNAME "WSselect"
483 /* Winsock provides a "select" function but its fd_sets are
484 * actually arrays of sockets. Since these sockets are handles
485 * and not a contiguous range of small integers, we manage our
486 * own "fd" table of socket handles and use their indices as
489 * All of our listener/connection structures use fds; the actual
490 * I/O functions use sockets. The SLAP_FD2SOCK macro in proto-slap.h
491 * handles the mapping.
493 * Despite the mapping overhead, this is about 45% more efficient
494 * than just using Winsock's select and FD_ISSET directly.
496 * Unfortunately Winsock's select implementation doesn't scale well
497 * as the number of connections increases. This probably needs to be
498 * rewritten to use the Winsock overlapped/asynchronous I/O functions.
500 # define SLAP_EVENTS_ARE_INDEXED 1
501 # define SLAP_EVENT_DECL fd_set readfds, writefds
502 # define SLAP_EVENT_INIT do { \
504 FD_ZERO( &readfds ); \
505 FD_ZERO( &writefds ); \
506 memset( slap_daemon.sd_rflags, 0, slap_daemon.sd_nfds ); \
507 for ( i=0; i<slap_daemon.sd_nfds; i++ ) { \
508 if ( slap_daemon.sd_flags[i] & SD_READ ) \
509 FD_SET( slapd_ws_sockets[i], &readfds );\
510 if ( slap_daemon.sd_flags[i] & SD_WRITE ) \
511 FD_SET( slapd_ws_sockets[i], &writefds ); \
514 # define SLAP_EVENT_MAX slap_daemon.sd_nfds
516 # define SLAP_EVENT_WAIT(tvp, nsp) do { \
518 *(nsp) = select( SLAP_EVENT_MAX, &readfds, \
519 nwriters > 0 ? &writefds : NULL, NULL, (tvp) ); \
520 for ( i=0; i<readfds.fd_count; i++) { \
521 int fd = slapd_sock2fd(readfds.fd_array[i]); \
523 slap_daemon.sd_rflags[fd] = SD_READ; \
524 if ( fd >= *(nsp)) *(nsp) = fd+1; \
527 for ( i=0; i<writefds.fd_count; i++) { \
528 int fd = slapd_sock2fd(writefds.fd_array[i]); \
530 slap_daemon.sd_rflags[fd] = SD_WRITE; \
531 if ( fd >= *(nsp)) *(nsp) = fd+1; \
536 # define SLAP_EVENT_IS_READ(fd) (slap_daemon.sd_rflags[fd] & SD_READ)
537 # define SLAP_EVENT_IS_WRITE(fd) (slap_daemon.sd_rflags[fd] & SD_WRITE)
539 # define SLAP_EVENT_CLR_READ(fd) slap_daemon.sd_rflags[fd] &= ~SD_READ;
540 # define SLAP_EVENT_CLR_WRITE(fd) slap_daemon.sd_rflags[fd] &= ~SD_WRITE;
542 # define SLAP_SOCK_INIT do { \
543 ldap_pvt_thread_mutex_init( &slapd_ws_mutex ); \
544 slapd_ws_sockets = ch_malloc( dtblsize * ( sizeof(SOCKET) + 2)); \
545 slap_daemon.sd_flags = (char *)(slapd_ws_sockets + dtblsize); \
546 slap_daemon.sd_rflags = slap_daemon.sd_flags + dtblsize; \
547 memset( slapd_ws_sockets, -1, dtblsize * sizeof(SOCKET) ); \
548 slapd_ws_sockets[0] = wake_sds[0]; \
549 slapd_ws_sockets[1] = wake_sds[1]; \
552 slap_daemon.sd_nfds = 2; \
555 # define SLAP_SOCK_DESTROY do { \
556 ch_free( slapd_ws_sockets ); slapd_ws_sockets = NULL; \
557 slap_daemon.sd_flags = NULL; \
558 slap_daemon.sd_rflags = NULL; \
559 ldap_pvt_thread_mutex_destroy( &slapd_ws_mutex ); \
562 # define SLAP_SOCK_IS_ACTIVE(fd) ( slap_daemon.sd_flags[fd] & SD_ACTIVE )
563 # define SLAP_SOCK_IS_READ(fd) ( slap_daemon.sd_flags[fd] & SD_READ )
564 # define SLAP_SOCK_IS_WRITE(fd) ( slap_daemon.sd_flags[fd] & SD_WRITE )
565 # define SLAP_SOCK_NOT_ACTIVE(fd) (!slap_daemon.sd_flags[fd])
567 # define SLAP_SOCK_SET_READ(fd) ( slap_daemon.sd_flags[fd] |= SD_READ )
568 # define SLAP_SOCK_SET_WRITE(fd) ( slap_daemon.sd_flags[fd] |= SD_WRITE )
570 # define SLAP_SELECT_ADDTEST(s) do { \
571 if ((s) >= slap_daemon.sd_nfds) slap_daemon.sd_nfds = (s)+1; \
574 # define SLAP_SOCK_CLR_READ(fd) ( slap_daemon.sd_flags[fd] &= ~SD_READ )
575 # define SLAP_SOCK_CLR_WRITE(fd) ( slap_daemon.sd_flags[fd] &= ~SD_WRITE )
577 # define SLAP_SOCK_ADD(s, l) do { \
578 SLAP_SELECT_ADDTEST((s)); \
579 slap_daemon.sd_flags[s] = SD_ACTIVE|SD_READ; \
582 # define SLAP_SOCK_DEL(s) do { \
583 slap_daemon.sd_flags[s] = 0; \
584 slapd_sockdel( s ); \
587 # else /* !HAVE_WINSOCK */
589 /**************************************
590 * Use select system call - select(2) *
591 **************************************/
592 # define SLAP_EVENT_FNAME "select"
594 # define SLAP_EVENTS_ARE_INDEXED 1
595 # define SLAP_EVENT_DECL fd_set readfds, writefds
597 # define SLAP_EVENT_INIT do { \
598 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) ); \
600 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) ); \
602 FD_ZERO( &writefds ); \
607 # define SLAP_SELECT_CHK_SETSIZE do { \
608 if (dtblsize > FD_SETSIZE) dtblsize = FD_SETSIZE; \
610 # else /* ! FD_SETSIZE */
611 # define SLAP_SELECT_CHK_SETSIZE do { ; } while (0)
612 # endif /* ! FD_SETSIZE */
614 # define SLAP_SOCK_INIT do { \
615 SLAP_SELECT_CHK_SETSIZE; \
616 FD_ZERO(&slap_daemon.sd_actives); \
617 FD_ZERO(&slap_daemon.sd_readers); \
618 FD_ZERO(&slap_daemon.sd_writers); \
621 # define SLAP_SOCK_DESTROY
623 # define SLAP_SOCK_IS_ACTIVE(fd) FD_ISSET((fd), &slap_daemon.sd_actives)
624 # define SLAP_SOCK_IS_READ(fd) FD_ISSET((fd), &slap_daemon.sd_readers)
625 # define SLAP_SOCK_IS_WRITE(fd) FD_ISSET((fd), &slap_daemon.sd_writers)
627 # define SLAP_SOCK_NOT_ACTIVE(fd) (!SLAP_SOCK_IS_ACTIVE(fd) && \
628 !SLAP_SOCK_IS_READ(fd) && !SLAP_SOCK_IS_WRITE(fd))
630 # define SLAP_SOCK_SET_READ(fd) FD_SET((fd), &slap_daemon.sd_readers)
631 # define SLAP_SOCK_SET_WRITE(fd) FD_SET((fd), &slap_daemon.sd_writers)
633 # define SLAP_EVENT_MAX slap_daemon.sd_nfds
634 # define SLAP_SELECT_ADDTEST(s) do { \
635 if ((s) >= slap_daemon.sd_nfds) slap_daemon.sd_nfds = (s)+1; \
638 # define SLAP_SOCK_CLR_READ(fd) FD_CLR((fd), &slap_daemon.sd_readers)
639 # define SLAP_SOCK_CLR_WRITE(fd) FD_CLR((fd), &slap_daemon.sd_writers)
641 # define SLAP_SOCK_ADD(s, l) do { \
642 SLAP_SELECT_ADDTEST((s)); \
643 FD_SET((s), &slap_daemon.sd_actives); \
644 FD_SET((s), &slap_daemon.sd_readers); \
647 # define SLAP_SOCK_DEL(s) do { \
648 FD_CLR((s), &slap_daemon.sd_actives); \
649 FD_CLR((s), &slap_daemon.sd_readers); \
650 FD_CLR((s), &slap_daemon.sd_writers); \
653 # define SLAP_EVENT_IS_READ(fd) FD_ISSET((fd), &readfds)
654 # define SLAP_EVENT_IS_WRITE(fd) FD_ISSET((fd), &writefds)
656 # define SLAP_EVENT_CLR_READ(fd) FD_CLR((fd), &readfds)
657 # define SLAP_EVENT_CLR_WRITE(fd) FD_CLR((fd), &writefds)
659 # define SLAP_EVENT_WAIT(tvp, nsp) do { \
660 *(nsp) = select( SLAP_EVENT_MAX, &readfds, \
661 nwriters > 0 ? &writefds : NULL, NULL, (tvp) ); \
663 # endif /* !HAVE_WINSOCK */
664 #endif /* ! epoll && ! /dev/poll */
668 * SLP related functions
672 #define LDAP_SRVTYPE_PREFIX "service:ldap://"
673 #define LDAPS_SRVTYPE_PREFIX "service:ldaps://"
674 static char** slapd_srvurls = NULL;
675 static SLPHandle slapd_hslp = 0;
676 int slapd_register_slp = 0;
677 const char *slapd_slp_attrs = NULL;
679 static SLPError slapd_slp_cookie;
682 slapd_slp_init( const char* urls )
687 slapd_srvurls = ldap_str2charray( urls, " " );
689 if ( slapd_srvurls == NULL ) return;
691 /* find and expand INADDR_ANY URLs */
692 for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
693 if ( strcmp( slapd_srvurls[i], "ldap:///" ) == 0 ) {
694 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
695 strlen( global_host ) +
696 sizeof( LDAP_SRVTYPE_PREFIX ) );
697 strcpy( lutil_strcopy(slapd_srvurls[i],
698 LDAP_SRVTYPE_PREFIX ), global_host );
699 } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0 ) {
700 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
701 strlen( global_host ) +
702 sizeof( LDAPS_SRVTYPE_PREFIX ) );
703 strcpy( lutil_strcopy(slapd_srvurls[i],
704 LDAPS_SRVTYPE_PREFIX ), global_host );
708 /* open the SLP handle */
709 err = SLPOpen( "en", 0, &slapd_hslp );
711 if ( err != SLP_OK ) {
712 Debug( LDAP_DEBUG_CONNS, "daemon: SLPOpen() failed with %ld\n",
718 slapd_slp_deinit( void )
720 if ( slapd_srvurls == NULL ) return;
722 ldap_charray_free( slapd_srvurls );
723 slapd_srvurls = NULL;
725 /* close the SLP handle */
726 SLPClose( slapd_hslp );
735 /* return the error code in the cookie */
736 *(SLPError*)cookie = errcode;
745 if ( slapd_srvurls == NULL ) return;
747 for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
748 if ( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
749 sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
750 strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
751 sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
753 err = SLPReg( slapd_hslp,
755 SLP_LIFETIME_MAXIMUM,
757 (slapd_slp_attrs) ? slapd_slp_attrs : "",
762 if ( err != SLP_OK || slapd_slp_cookie != SLP_OK ) {
763 Debug( LDAP_DEBUG_CONNS,
764 "daemon: SLPReg(%s) failed with %ld, cookie = %ld\n",
765 slapd_srvurls[i], (long)err, (long)slapd_slp_cookie );
772 slapd_slp_dereg( void )
777 if ( slapd_srvurls == NULL ) return;
779 for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
780 err = SLPDereg( slapd_hslp,
785 if ( err != SLP_OK || slapd_slp_cookie != SLP_OK ) {
786 Debug( LDAP_DEBUG_CONNS,
787 "daemon: SLPDereg(%s) failed with %ld, cookie = %ld\n",
788 slapd_srvurls[i], (long)err, (long)slapd_slp_cookie );
792 #endif /* HAVE_SLP */
795 /* Manage the descriptor to socket table */
797 slapd_socknew( ber_socket_t s )
800 ldap_pvt_thread_mutex_lock( &slapd_ws_mutex );
801 for ( i = 0; i < dtblsize && slapd_ws_sockets[i] != INVALID_SOCKET; i++ );
802 if ( i == dtblsize ) {
803 WSASetLastError( WSAEMFILE );
805 slapd_ws_sockets[i] = s;
807 ldap_pvt_thread_mutex_unlock( &slapd_ws_mutex );
812 slapd_sockdel( ber_socket_t s )
814 ldap_pvt_thread_mutex_lock( &slapd_ws_mutex );
815 slapd_ws_sockets[s] = INVALID_SOCKET;
816 ldap_pvt_thread_mutex_unlock( &slapd_ws_mutex );
820 slapd_sock2fd( ber_socket_t s )
823 for ( i=0; i<dtblsize && slapd_ws_sockets[i] != s; i++);
831 * Add a descriptor to daemon control
833 * If isactive, the descriptor is a live server session and is subject
834 * to idletimeout control. Otherwise, the descriptor is a passive
835 * listener or an outbound client session, and not subject to
836 * idletimeout. The underlying event handler may record the Listener
837 * argument to differentiate Listener's from real sessions.
840 slapd_add( ber_socket_t s, int isactive, Listener *sl )
842 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
844 assert( SLAP_SOCK_NOT_ACTIVE(s) );
846 if ( isactive ) slap_daemon.sd_nactives++;
848 SLAP_SOCK_ADD(s, sl);
850 Debug( LDAP_DEBUG_CONNS, "daemon: added %ldr%s listener=%p\n",
851 (long) s, isactive ? " (active)" : "", (void *)sl );
853 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
862 slapd_sd_lock( void )
864 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
871 slapd_sd_unlock( void )
873 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
877 * Remove the descriptor from daemon control
891 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
893 assert( SLAP_SOCK_IS_ACTIVE( s ));
895 if ( wasactive ) slap_daemon.sd_nactives--;
897 waswriter = SLAP_SOCK_IS_WRITE(s);
898 wasreader = SLAP_SOCK_IS_READ(s);
900 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
902 wasreader ? "r" : "",
903 waswriter ? "w" : "" );
905 if ( waswriter ) slap_daemon.sd_nwriters--;
910 ber_sockbuf_free(sb);
912 /* If we ran out of file descriptors, we dropped a listener from
913 * the select() loop. Now that we're removing a session from our
914 * control, we can try to resume a dropped listener to use.
918 for ( i = 0; slap_listeners[i] != NULL; i++ ) {
919 Listener *lr = slap_listeners[i];
921 if ( lr->sl_sd == AC_SOCKET_INVALID ) continue;
922 if ( lr->sl_sd == s ) continue;
929 /* Walked the entire list without enabling anything; emfile
930 * counter is stale. Reset it.
932 if ( slap_listeners[i] == NULL ) emfile = 0;
934 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
935 WAKE_LISTENER(wake || slapd_gentle_shutdown == 2);
939 slapd_ack_write( ber_socket_t s, int wake )
941 #ifdef SLAP_EVENT_ACK
942 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
944 if ( SLAP_SOCK_IS_WRITE( s )) {
945 assert( SLAP_SOCK_IS_ACTIVE( s ));
947 SLAP_SOCK_CLR_WRITE( s );
948 slap_daemon.sd_nwriters--;
951 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
957 slapd_clr_write( ber_socket_t s, int wake )
959 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
961 if ( SLAP_SOCK_IS_WRITE( s )) {
962 assert( SLAP_SOCK_IS_ACTIVE( s ));
964 SLAP_SOCK_CLR_WRITE( s );
965 slap_daemon.sd_nwriters--;
968 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
973 slapd_set_write( ber_socket_t s, int wake )
975 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
977 assert( SLAP_SOCK_IS_ACTIVE( s ));
979 if ( !SLAP_SOCK_IS_WRITE( s )) {
980 SLAP_SOCK_SET_WRITE( s );
981 slap_daemon.sd_nwriters++;
984 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
989 slapd_ack_read( ber_socket_t s, int wake )
991 #ifdef SLAP_EVENT_ACK
993 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
995 if ( SLAP_SOCK_IS_ACTIVE( s )) {
996 SLAP_SOCK_CLR_READ( s );
999 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1001 WAKE_LISTENER(wake);
1009 slapd_clr_read( ber_socket_t s, int wake )
1012 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1014 if ( SLAP_SOCK_IS_ACTIVE( s )) {
1015 SLAP_SOCK_CLR_READ( s );
1018 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1020 WAKE_LISTENER(wake);
1025 slapd_set_read( ber_socket_t s, int wake )
1027 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1029 assert( SLAP_SOCK_IS_ACTIVE( s ));
1030 if (!SLAP_SOCK_IS_READ( s )) SLAP_SOCK_SET_READ( s );
1032 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1033 WAKE_LISTENER(wake);
1037 slapd_close( ber_socket_t s )
1039 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
1041 tcp_close( SLAP_FD2SOCK(s) );
1048 slap_free_listener_addresses( struct sockaddr **sal )
1050 struct sockaddr **sap;
1051 if (sal == NULL) return;
1052 for (sap = sal; *sap != NULL; sap++) ch_free(*sap);
1056 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
1065 assert( exts != NULL );
1066 assert( perms != NULL );
1067 assert( crit != NULL );
1070 for ( i = 0; exts[ i ]; i++ ) {
1071 char *type = exts[ i ];
1074 if ( type[ 0 ] == '!' ) {
1079 if ( strncasecmp( type, LDAPI_MOD_URLEXT "=",
1080 sizeof(LDAPI_MOD_URLEXT "=") - 1 ) == 0 )
1082 char *value = type + ( sizeof(LDAPI_MOD_URLEXT "=") - 1 );
1086 switch (strlen(value)) {
1088 /* skip leading '0' */
1089 if ( value[ 0 ] != '0' ) return LDAP_OTHER;
1093 for ( j = 0; j < 3; j++) {
1096 v = value[ j ] - '0';
1098 if ( v < 0 || v > 7 ) return LDAP_OTHER;
1105 for ( j = 1; j < 10; j++ ) {
1106 static mode_t m[] = { 0,
1107 S_IRUSR, S_IWUSR, S_IXUSR,
1108 S_IRGRP, S_IWGRP, S_IXGRP,
1109 S_IROTH, S_IWOTH, S_IXOTH
1111 static const char c[] = "-rwxrwxrwx";
1113 if ( value[ j ] == c[ j ] ) {
1116 } else if ( value[ j ] != '-' ) {
1129 return LDAP_SUCCESS;
1135 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
1137 /* port = 0 indicates AF_LOCAL */
1139 slap_get_listener_addresses(
1141 unsigned short port,
1142 struct sockaddr ***sal )
1144 struct sockaddr **sap;
1146 #ifdef LDAP_PF_LOCAL
1148 *sal = ch_malloc(2 * sizeof(void *));
1149 if (*sal == NULL) return -1;
1152 *sap = ch_malloc(sizeof(struct sockaddr_un));
1153 if (*sap == NULL) goto errexit;
1157 (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) )
1159 Debug( LDAP_DEBUG_ANY,
1160 "daemon: domain socket path (%s) too long in URL",
1165 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
1166 (*sap)->sa_family = AF_LOCAL;
1167 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
1169 #endif /* LDAP_PF_LOCAL */
1171 #ifdef HAVE_GETADDRINFO
1172 struct addrinfo hints, *res, *sai;
1176 memset( &hints, '\0', sizeof(hints) );
1177 hints.ai_flags = AI_PASSIVE;
1178 hints.ai_socktype = SOCK_STREAM;
1179 hints.ai_family = slap_inet4or6;
1180 snprintf(serv, sizeof serv, "%d", port);
1182 if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
1183 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo() failed: %s\n",
1184 AC_GAI_STRERROR(err), 0, 0);
1189 for (n=2; (sai = sai->ai_next) != NULL; n++) {
1192 *sal = ch_calloc(n, sizeof(void *));
1193 if (*sal == NULL) return -1;
1198 for ( sai=res; sai; sai=sai->ai_next ) {
1199 if( sai->ai_addr == NULL ) {
1200 Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
1201 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
1206 switch (sai->ai_family) {
1207 # ifdef LDAP_PF_INET6
1209 *sap = ch_malloc(sizeof(struct sockaddr_in6));
1214 *(struct sockaddr_in6 *)*sap =
1215 *((struct sockaddr_in6 *)sai->ai_addr);
1217 # endif /* LDAP_PF_INET6 */
1219 *sap = ch_malloc(sizeof(struct sockaddr_in));
1224 *(struct sockaddr_in *)*sap =
1225 *((struct sockaddr_in *)sai->ai_addr);
1233 (*sap)->sa_family = sai->ai_family;
1241 #else /* ! HAVE_GETADDRINFO */
1244 struct hostent *he = NULL;
1246 if ( host == NULL ) {
1247 in.s_addr = htonl(INADDR_ANY);
1249 } else if ( !inet_aton( host, &in ) ) {
1250 he = gethostbyname( host );
1252 Debug( LDAP_DEBUG_ANY,
1253 "daemon: invalid host %s", host, 0, 0);
1256 for (n = 0; he->h_addr_list[n]; n++) /* empty */;
1259 *sal = ch_malloc((n+1) * sizeof(void *));
1260 if (*sal == NULL) return -1;
1263 for ( i = 0; i<n; i++ ) {
1264 sap[i] = ch_malloc(sizeof(struct sockaddr_in));
1265 if (*sap == NULL) goto errexit;
1267 (void)memset( (void *)sap[i], '\0', sizeof(struct sockaddr_in) );
1268 sap[i]->sa_family = AF_INET;
1269 ((struct sockaddr_in *)sap[i])->sin_port = htons(port);
1270 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr,
1271 he ? (struct in_addr *)he->h_addr_list[i] : &in,
1272 sizeof(struct in_addr) );
1275 #endif /* ! HAVE_GETADDRINFO */
1281 slap_free_listener_addresses(*sal);
1295 unsigned short port;
1296 int err, addrlen = 0;
1297 struct sockaddr **sal, **psal;
1298 int socktype = SOCK_STREAM; /* default to COTS */
1301 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
1306 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
1308 rc = ldap_url_parse( url, &lud );
1310 if( rc != LDAP_URL_SUCCESS ) {
1311 Debug( LDAP_DEBUG_ANY,
1312 "daemon: listen URL \"%s\" parse error=%d\n",
1317 l.sl_url.bv_val = NULL;
1322 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
1323 Debug( LDAP_DEBUG_ANY, "daemon: TLS not supported (%s)\n",
1325 ldap_free_urldesc( lud );
1329 if(! lud->lud_port ) lud->lud_port = LDAP_PORT;
1331 #else /* HAVE_TLS */
1332 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
1334 if(! lud->lud_port ) {
1335 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
1337 #endif /* HAVE_TLS */
1339 port = (unsigned short) lud->lud_port;
1341 tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme);
1342 if ( tmp == LDAP_PROTO_IPC ) {
1343 #ifdef LDAP_PF_LOCAL
1344 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
1345 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
1347 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
1349 #else /* ! LDAP_PF_LOCAL */
1351 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
1353 ldap_free_urldesc( lud );
1355 #endif /* ! LDAP_PF_LOCAL */
1357 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
1358 || strcmp(lud->lud_host, "*") == 0 )
1360 err = slap_get_listener_addresses(NULL, port, &sal);
1362 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
1366 #ifdef LDAP_CONNECTIONLESS
1367 l.sl_is_udp = ( tmp == LDAP_PROTO_UDP );
1368 #endif /* LDAP_CONNECTIONLESS */
1370 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
1371 if ( lud->lud_exts ) {
1372 err = get_url_perms( lud->lud_exts, &l.sl_perms, &crit );
1374 l.sl_perms = S_IRWXU | S_IRWXO;
1376 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
1378 ldap_free_urldesc( lud );
1379 if ( err ) return -1;
1381 /* If we got more than one address returned, we need to make space
1382 * for it in the slap_listeners array.
1384 for ( num=0; sal[num]; num++ ) /* empty */;
1386 *listeners += num-1;
1387 slap_listeners = ch_realloc( slap_listeners,
1388 (*listeners + 1) * sizeof(Listener *) );
1392 while ( *sal != NULL ) {
1394 switch( (*sal)->sa_family ) {
1398 #ifdef LDAP_PF_INET6
1402 #endif /* LDAP_PF_INET6 */
1403 #ifdef LDAP_PF_LOCAL
1407 #endif /* LDAP_PF_LOCAL */
1413 #ifdef LDAP_CONNECTIONLESS
1414 if( l.sl_is_udp ) socktype = SOCK_DGRAM;
1415 #endif /* LDAP_CONNECTIONLESS */
1417 s = socket( (*sal)->sa_family, socktype, 0);
1418 if ( s == AC_SOCKET_INVALID ) {
1419 int err = sock_errno();
1420 Debug( LDAP_DEBUG_ANY,
1421 "daemon: %s socket() failed errno=%d (%s)\n",
1422 af, err, sock_errstr(err) );
1426 l.sl_sd = SLAP_SOCKNEW( s );
1428 if ( l.sl_sd >= dtblsize ) {
1429 Debug( LDAP_DEBUG_ANY,
1430 "daemon: listener descriptor %ld is too great %ld\n",
1431 (long) l.sl_sd, (long) dtblsize, 0 );
1437 #ifdef LDAP_PF_LOCAL
1438 if ( (*sal)->sa_family == AF_LOCAL ) {
1439 unlink( ((struct sockaddr_un *)*sal)->sun_path );
1441 #endif /* LDAP_PF_LOCAL */
1444 /* enable address reuse */
1446 rc = setsockopt( s, SOL_SOCKET, SO_REUSEADDR,
1447 (char *) &tmp, sizeof(tmp) );
1448 if ( rc == AC_SOCKET_ERROR ) {
1449 int err = sock_errno();
1450 Debug( LDAP_DEBUG_ANY, "slapd(%ld): "
1451 "setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
1452 (long) l.sl_sd, err, sock_errstr(err) );
1454 #endif /* SO_REUSEADDR */
1457 switch( (*sal)->sa_family ) {
1459 addrlen = sizeof(struct sockaddr_in);
1461 #ifdef LDAP_PF_INET6
1464 /* Try to use IPv6 sockets for IPv6 only */
1466 rc = setsockopt( s , IPPROTO_IPV6, IPV6_V6ONLY,
1467 (char *) &tmp, sizeof(tmp) );
1468 if ( rc == AC_SOCKET_ERROR ) {
1469 int err = sock_errno();
1470 Debug( LDAP_DEBUG_ANY, "slapd(%ld): "
1471 "setsockopt(IPV6_V6ONLY) failed errno=%d (%s)\n",
1472 (long) l.sl_sd, err, sock_errstr(err) );
1474 #endif /* IPV6_V6ONLY */
1475 addrlen = sizeof(struct sockaddr_in6);
1477 #endif /* LDAP_PF_INET6 */
1479 #ifdef LDAP_PF_LOCAL
1484 setsockopt( s, 0, LOCAL_CREDS, &one, sizeof( one ) );
1486 #endif /* LOCAL_CREDS */
1488 addrlen = sizeof( struct sockaddr_un );
1490 #endif /* LDAP_PF_LOCAL */
1493 #ifdef LDAP_PF_LOCAL
1494 /* create socket with all permissions set for those systems
1495 * that honor permissions on sockets (e.g. Linux); typically,
1496 * only write is required. To exploit filesystem permissions,
1497 * place the socket in a directory and use directory's
1498 * permissions. Need write perms to the directory to
1499 * create/unlink the socket; likely need exec perms to access
1500 * the socket (ITS#4709) */
1504 if ( (*sal)->sa_family == AF_LOCAL ) {
1505 old_umask = umask( 0 );
1507 #endif /* LDAP_PF_LOCAL */
1508 rc = bind( s, *sal, addrlen );
1509 #ifdef LDAP_PF_LOCAL
1510 if ( (*sal)->sa_family == AF_LOCAL ) {
1514 #endif /* LDAP_PF_LOCAL */
1517 Debug( LDAP_DEBUG_ANY,
1518 "daemon: bind(%ld) failed errno=%d (%s)\n",
1519 (long)l.sl_sd, err, sock_errstr( err ) );
1525 switch ( (*sal)->sa_family ) {
1526 #ifdef LDAP_PF_LOCAL
1528 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
1529 l.sl_name.bv_len = strlen(addr) + sizeof("PATH=") - 1;
1530 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len + 1 );
1531 snprintf( l.sl_name.bv_val, l.sl_name.bv_len + 1,
1534 #endif /* LDAP_PF_LOCAL */
1538 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
1539 char addr[INET_ADDRSTRLEN];
1540 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
1541 addr, sizeof(addr) );
1543 #else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1544 s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr );
1545 #endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1546 port = ntohs( ((struct sockaddr_in *)*sal) ->sin_port );
1548 ber_memalloc( sizeof("IP=255.255.255.255:65535") );
1549 snprintf( l.sl_name.bv_val, sizeof("IP=255.255.255.255:65535"),
1551 s != NULL ? s : SLAP_STRING_UNKNOWN, port );
1552 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1555 #ifdef LDAP_PF_INET6
1557 char addr[INET6_ADDRSTRLEN];
1558 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
1560 port = ntohs( ((struct sockaddr_in6 *)*sal)->sin6_port );
1561 l.sl_name.bv_len = strlen(addr) + sizeof("IP=[]:65535");
1562 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len );
1563 snprintf( l.sl_name.bv_val, l.sl_name.bv_len, "IP=[%s]:%d",
1565 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1567 #endif /* LDAP_PF_INET6 */
1570 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
1571 (int) (*sal)->sa_family, 0, 0 );
1575 AC_MEMCPY(&l.sl_sa, *sal, addrlen);
1576 ber_str2bv( url, 0, 1, &l.sl_url);
1577 li = ch_malloc( sizeof( Listener ) );
1579 slap_listeners[*cur] = li;
1584 slap_free_listener_addresses(psal);
1586 if ( l.sl_url.bv_val == NULL ) {
1587 Debug( LDAP_DEBUG_TRACE,
1588 "slap_open_listener: failed on %s\n", url, 0, 0 );
1592 Debug( LDAP_DEBUG_TRACE, "daemon: listener initialized %s\n",
1593 l.sl_url.bv_val, 0, 0 );
1597 static int sockinit(void);
1598 static int sockdestroy(void);
1601 slapd_daemon_init( const char *urls )
1606 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
1607 urls ? urls : "<null>", 0, 0 );
1609 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
1611 ldap_pvt_thread_mutex_init( &slap_daemon.sd_tcpd_mutex );
1612 #endif /* TCP Wrappers */
1614 if( (rc = sockinit()) != 0 ) return rc;
1617 dtblsize = sysconf( _SC_OPEN_MAX );
1618 #elif defined(HAVE_GETDTABLESIZE)
1619 dtblsize = getdtablesize();
1620 #else /* ! HAVE_SYSCONF && ! HAVE_GETDTABLESIZE */
1621 dtblsize = FD_SETSIZE;
1622 #endif /* ! HAVE_SYSCONF && ! HAVE_GETDTABLESIZE */
1624 /* open a pipe (or something equivalent connected to itself).
1625 * we write a byte on this fd whenever we catch a signal. The main
1626 * loop will be select'ing on this socket, and will wake up when
1627 * this byte arrives.
1629 if( (rc = lutil_pair( wake_sds )) < 0 ) {
1630 Debug( LDAP_DEBUG_ANY,
1631 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
1637 if( urls == NULL ) urls = "ldap:///";
1639 u = ldap_str2charray( urls, " " );
1641 if( u == NULL || u[0] == NULL ) {
1642 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
1645 ldap_charray_free( u );
1649 for( i=0; u[i] != NULL; i++ ) {
1650 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
1655 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
1657 ldap_charray_free( u );
1661 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
1663 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
1665 for(n = 0, j = 0; u[n]; n++ ) {
1666 if ( slap_open_listener( u[n], &i, &j ) ) {
1667 ldap_charray_free( u );
1671 slap_listeners[j] = NULL;
1673 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
1678 if( slapd_register_slp ) {
1679 slapd_slp_init( urls );
1682 #endif /* HAVE_SLP */
1684 ldap_charray_free( u );
1691 slapd_daemon_destroy( void )
1693 connections_destroy();
1695 if ( wake_sds[1] != INVALID_SOCKET && wake_sds[1] != wake_sds[0] )
1696 #endif /* HAVE_WINSOCK */
1697 tcp_close( SLAP_FD2SOCK(wake_sds[1]) );
1699 if ( wake_sds[0] != INVALID_SOCKET )
1700 #endif /* HAVE_WINSOCK */
1701 tcp_close( SLAP_FD2SOCK(wake_sds[0]) );
1705 if( slapd_register_slp ) {
1709 #endif /* HAVE_SLP */
1712 ldap_pvt_thread_mutex_destroy( &slap_daemon.sd_tcpd_mutex );
1713 #endif /* TCP Wrappers */
1715 ldap_pvt_thread_mutex_destroy( &slap_daemon.sd_mutex );
1726 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1727 Listener *lr = slap_listeners[l];
1729 if ( lr->sl_sd != AC_SOCKET_INVALID ) {
1730 if ( remove ) slapd_remove( lr->sl_sd, NULL, 0, 0, 0 );
1732 #ifdef LDAP_PF_LOCAL
1733 if ( lr->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1734 unlink( lr->sl_sa.sa_un_addr.sun_path );
1736 #endif /* LDAP_PF_LOCAL */
1738 slapd_close( lr->sl_sd );
1741 if ( lr->sl_url.bv_val ) {
1742 ber_memfree( lr->sl_url.bv_val );
1745 if ( lr->sl_name.bv_val ) {
1746 ber_memfree( lr->sl_name.bv_val );
1750 slap_listeners[l] = NULL;
1760 ber_socket_t s, sfd;
1761 ber_socklen_t len = sizeof(from);
1764 struct berval authid = BER_BVNULL;
1765 #ifdef SLAPD_RLOOKUPS
1766 char hbuf[NI_MAXHOST];
1767 #endif /* SLAPD_RLOOKUPS */
1769 char *dnsname = NULL;
1770 char *peeraddr = NULL;
1771 #ifdef LDAP_PF_LOCAL
1772 char peername[MAXPATHLEN + sizeof("PATH=")];
1773 #ifdef LDAP_PF_LOCAL_SENDMSG
1775 struct berval peerbv = BER_BVNULL;
1777 #elif defined(LDAP_PF_INET6)
1778 char peername[sizeof("IP=[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535")];
1779 #else /* ! LDAP_PF_LOCAL && ! LDAP_PF_INET6 */
1780 char peername[sizeof("IP=255.255.255.255:65336")];
1781 #endif /* LDAP_PF_LOCAL */
1784 Debug( LDAP_DEBUG_TRACE,
1785 ">>> slap_listener(%s)\n",
1786 sl->sl_url.bv_val, 0, 0 );
1790 #ifdef LDAP_CONNECTIONLESS
1791 if ( sl->sl_is_udp ) return 1;
1792 #endif /* LDAP_CONNECTIONLESS */
1794 # ifdef LDAP_PF_LOCAL
1795 /* FIXME: apparently accept doesn't fill
1796 * the sun_path sun_path member */
1797 from.sa_un_addr.sun_path[0] = '\0';
1798 # endif /* LDAP_PF_LOCAL */
1800 s = accept( SLAP_FD2SOCK( sl->sl_sd ), (struct sockaddr *) &from, &len );
1802 /* Resume the listener FD to allow concurrent-processing of
1803 * additional incoming connections.
1808 if ( s == AC_SOCKET_INVALID ) {
1809 int err = sock_errno();
1820 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1822 /* Stop listening until an existing session closes */
1824 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1827 Debug( LDAP_DEBUG_ANY,
1828 "daemon: accept(%ld) failed errno=%d (%s)\n",
1829 (long) sl->sl_sd, err, sock_errstr(err) );
1830 ldap_pvt_thread_yield();
1833 sfd = SLAP_SOCKNEW( s );
1835 /* make sure descriptor number isn't too great */
1836 if ( sfd >= dtblsize ) {
1837 Debug( LDAP_DEBUG_ANY,
1838 "daemon: %ld beyond descriptor table size %ld\n",
1839 (long) sfd, (long) dtblsize, 0 );
1842 ldap_pvt_thread_yield();
1847 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1848 /* newly accepted stream should not be in any of the FD SETS */
1849 assert( SLAP_SOCK_NOT_ACTIVE( sfd ));
1850 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1851 #endif /* LDAP_DEBUG */
1853 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1854 #ifdef LDAP_PF_LOCAL
1855 /* for IPv4 and IPv6 sockets only */
1856 if ( from.sa_addr.sa_family != AF_LOCAL )
1857 #endif /* LDAP_PF_LOCAL */
1862 /* enable keep alives */
1864 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1865 (char *) &tmp, sizeof(tmp) );
1866 if ( rc == AC_SOCKET_ERROR ) {
1867 int err = sock_errno();
1868 Debug( LDAP_DEBUG_ANY,
1869 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1870 "errno=%d (%s)\n", (long) sfd, err, sock_errstr(err) );
1872 #endif /* SO_KEEPALIVE */
1874 /* enable no delay */
1876 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1877 (char *)&tmp, sizeof(tmp) );
1878 if ( rc == AC_SOCKET_ERROR ) {
1879 int err = sock_errno();
1880 Debug( LDAP_DEBUG_ANY,
1881 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1882 "errno=%d (%s)\n", (long) sfd, err, sock_errstr(err) );
1884 #endif /* TCP_NODELAY */
1886 #endif /* SO_KEEPALIVE || TCP_NODELAY */
1888 Debug( LDAP_DEBUG_CONNS,
1889 "daemon: listen=%ld, new connection on %ld\n",
1890 (long) sl->sl_sd, (long) sfd, 0 );
1893 switch ( from.sa_addr.sa_family ) {
1894 # ifdef LDAP_PF_LOCAL
1896 cflag |= CONN_IS_IPC;
1898 /* FIXME: apparently accept doesn't fill
1899 * the sun_path sun_path member */
1900 if ( from.sa_un_addr.sun_path[0] == '\0' ) {
1901 AC_MEMCPY( from.sa_un_addr.sun_path,
1902 sl->sl_sa.sa_un_addr.sun_path,
1903 sizeof( from.sa_un_addr.sun_path ) );
1906 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1912 #ifdef LDAP_PF_LOCAL_SENDMSG
1913 peerbv.bv_val = peerbuf;
1914 peerbv.bv_len = sizeof( peerbuf );
1916 if( LUTIL_GETPEEREID( s, &uid, &gid, &peerbv ) == 0 ) {
1917 authid.bv_val = ch_malloc(
1918 STRLENOF( "gidNumber=4294967295+uidNumber=4294967295,"
1919 "cn=peercred,cn=external,cn=auth" ) + 1 );
1920 authid.bv_len = sprintf( authid.bv_val,
1921 "gidNumber=%d+uidNumber=%d,"
1922 "cn=peercred,cn=external,cn=auth",
1923 (int) gid, (int) uid );
1924 assert( authid.bv_len <=
1925 STRLENOF( "gidNumber=4294967295+uidNumber=4294967295,"
1926 "cn=peercred,cn=external,cn=auth" ) );
1931 #endif /* LDAP_PF_LOCAL */
1933 # ifdef LDAP_PF_INET6
1935 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1936 peeraddr = inet_ntoa( *((struct in_addr *)
1937 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1938 sprintf( peername, "IP=%s:%d",
1939 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1940 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1942 char addr[INET6_ADDRSTRLEN];
1944 peeraddr = (char *) inet_ntop( AF_INET6,
1945 &from.sa_in6_addr.sin6_addr,
1946 addr, sizeof addr );
1947 sprintf( peername, "IP=[%s]:%d",
1948 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1949 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1952 # endif /* LDAP_PF_INET6 */
1955 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1956 sprintf( peername, "IP=%s:%d",
1957 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1958 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1966 if ( ( from.sa_addr.sa_family == AF_INET )
1967 #ifdef LDAP_PF_INET6
1968 || ( from.sa_addr.sa_family == AF_INET6 )
1969 #endif /* LDAP_PF_INET6 */
1973 #ifdef SLAPD_RLOOKUPS
1974 if ( use_reverse_lookup ) {
1976 if (ldap_pvt_get_hname( (const struct sockaddr *)&from, len, hbuf,
1977 sizeof(hbuf), &herr ) == 0) {
1978 ldap_pvt_str2lower( hbuf );
1982 #endif /* SLAPD_RLOOKUPS */
1987 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_tcpd_mutex );
1988 rc = hosts_ctl("slapd",
1989 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1990 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1991 SLAP_STRING_UNKNOWN );
1992 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_tcpd_mutex );
1995 Statslog( LDAP_DEBUG_STATS,
1996 "fd=%ld DENIED from %s (%s)\n",
1998 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1999 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
2005 #endif /* HAVE_TCPD */
2009 if ( sl->sl_is_tls ) cflag |= CONN_IS_TLS;
2011 c = connection_init(sfd, sl,
2012 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
2013 peername, cflag, ssf,
2014 authid.bv_val ? &authid : NULL
2015 LDAP_PF_LOCAL_SENDMSG_ARG(&peerbv));
2017 if( authid.bv_val ) ch_free(authid.bv_val);
2020 Debug( LDAP_DEBUG_ANY,
2021 "daemon: connection_init(%ld, %s, %s) failed.\n",
2022 (long) sfd, peername, sl->sl_name.bv_val );
2027 Statslog( LDAP_DEBUG_STATS,
2028 "conn=%ld fd=%ld ACCEPT from %s (%s)\n",
2029 c->c_connid, (long) sfd, peername, sl->sl_name.bv_val,
2036 slap_listener_thread(
2041 Listener *sl = (Listener *)ptr;
2043 rc = slap_listener( sl );
2045 if( rc != LDAP_SUCCESS ) {
2046 Debug( LDAP_DEBUG_ANY,
2047 "slap_listener_thread(%s): failed err=%d",
2048 sl->sl_url.bv_val, rc, 0 );
2055 slap_listener_activate(
2060 Debug( LDAP_DEBUG_TRACE, "slap_listener_activate(%d): %s\n",
2061 sl->sl_sd, sl->sl_busy ? "busy" : "", 0 );
2065 rc = ldap_pvt_thread_pool_submit( &connection_pool,
2066 slap_listener_thread, (void *) sl );
2069 Debug( LDAP_DEBUG_ANY,
2070 "listener_activate(%d): submit failed (%d)\n",
2081 time_t last_idle_check = 0;
2082 struct timeval idle;
2085 #define SLAPD_IDLE_CHECK_LIMIT 4
2087 if ( global_idletimeout > 0 ) {
2088 last_idle_check = slap_get_time();
2089 /* Set the select timeout.
2090 * Don't just truncate, preserve the fractions of
2091 * seconds to prevent sleeping for zero time.
2093 idle.tv_sec = global_idletimeout / SLAPD_IDLE_CHECK_LIMIT;
2094 idle.tv_usec = global_idletimeout - \
2095 ( idle.tv_sec * SLAPD_IDLE_CHECK_LIMIT );
2096 idle.tv_usec *= 1000000 / SLAPD_IDLE_CHECK_LIMIT;
2102 slapd_add( wake_sds[0], 0, NULL );
2104 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2105 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ) continue;
2107 #ifdef LDAP_CONNECTIONLESS
2108 /* Since this is connectionless, the data port is the
2109 * listening port. The listen() and accept() calls
2112 if ( slap_listeners[l]->sl_is_udp )
2114 #endif /* LDAP_CONNECTIONLESS */
2116 if ( listen( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ), SLAPD_LISTEN_BACKLOG ) == -1 ) {
2117 int err = sock_errno();
2119 #ifdef LDAP_PF_INET6
2120 /* If error is EADDRINUSE, we are trying to listen to INADDR_ANY and
2121 * we are already listening to in6addr_any, then we want to ignore
2122 * this and continue.
2124 if ( err == EADDRINUSE ) {
2126 struct sockaddr_in sa = slap_listeners[l]->sl_sa.sa_in_addr;
2127 struct sockaddr_in6 sa6;
2129 if ( sa.sin_family == AF_INET &&
2130 sa.sin_addr.s_addr == htonl(INADDR_ANY) ) {
2131 for ( i = 0 ; i < l; i++ ) {
2132 sa6 = slap_listeners[i]->sl_sa.sa_in6_addr;
2133 if ( sa6.sin6_family == AF_INET6 &&
2134 !memcmp( &sa6.sin6_addr, &in6addr_any,
2135 sizeof(struct in6_addr) ) )
2142 /* We are already listening to in6addr_any */
2143 Debug( LDAP_DEBUG_CONNS,
2144 "daemon: Attempt to listen to 0.0.0.0 failed, "
2145 "already listening on ::, assuming IPv4 included\n",
2147 slapd_close( slap_listeners[l]->sl_sd );
2148 slap_listeners[l]->sl_sd = AC_SOCKET_INVALID;
2153 #endif /* LDAP_PF_INET6 */
2154 Debug( LDAP_DEBUG_ANY,
2155 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
2156 slap_listeners[l]->sl_url.bv_val, err,
2161 /* make the listening socket non-blocking */
2162 if ( ber_pvt_socket_set_nonblock( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ), 1 ) < 0 ) {
2163 Debug( LDAP_DEBUG_ANY, "slapd_daemon_task: "
2164 "set nonblocking on a listening socket failed\n",
2170 slapd_add( slap_listeners[l]->sl_sd, 0, slap_listeners[l] );
2173 #ifdef HAVE_NT_SERVICE_MANAGER
2174 if ( started_event != NULL ) {
2175 ldap_pvt_thread_cond_signal( &started_event );
2177 #endif /* HAVE_NT_SERVICE_MANAGER */
2179 /* initialization complete. Here comes the loop. */
2181 while ( !slapd_shutdown ) {
2186 #if SLAP_EVENTS_ARE_INDEXED
2187 ber_socket_t nrfds, nwfds;
2188 #endif /* SLAP_EVENTS_ARE_INDEXED */
2189 #define SLAPD_EBADF_LIMIT 16
2196 struct timeval *tvp;
2202 now = slap_get_time();
2204 if ( ( global_idletimeout > 0 ) &&
2205 difftime( last_idle_check +
2206 global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 )
2208 connections_timeout_idle( now );
2209 last_idle_check = now;
2214 if ( slapd_gentle_shutdown ) {
2215 ber_socket_t active;
2217 if ( slapd_gentle_shutdown == 1 ) {
2219 Debug( LDAP_DEBUG_ANY, "slapd gentle shutdown\n", 0, 0, 0 );
2220 close_listeners( 1 );
2221 frontendDB->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
2222 LDAP_STAILQ_FOREACH(be, &backendDB, be_next) {
2223 be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
2225 slapd_gentle_shutdown = 2;
2228 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
2229 active = slap_daemon.sd_nactives;
2230 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
2231 if ( active == 0 ) {
2239 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
2241 nwriters = slap_daemon.sd_nwriters;
2243 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2244 Listener *lr = slap_listeners[l];
2246 if ( lr->sl_sd == AC_SOCKET_INVALID ) continue;
2248 if ( lr->sl_mute || lr->sl_busy )
2250 SLAP_SOCK_CLR_READ( lr->sl_sd );
2252 SLAP_SOCK_SET_READ( lr->sl_sd );
2258 nfds = SLAP_EVENT_MAX;
2260 if ( global_idletimeout && slap_daemon.sd_nactives ) at = 1;
2262 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
2265 #if defined(HAVE_YIELDING_SELECT) || defined(NO_THREADS)
2266 && ( tv.tv_sec || tv.tv_usec )
2267 #endif /* HAVE_YIELDING_SELECT || NO_THREADS */
2275 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2276 rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
2277 while ( rtask && cat.tv_sec && cat.tv_sec <= now ) {
2278 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
2279 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
2281 ldap_pvt_runqueue_runtask( &slapd_rq, rtask );
2282 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
2283 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2284 ldap_pvt_thread_pool_submit( &connection_pool,
2285 rtask->routine, (void *) rtask );
2286 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2288 rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
2290 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2292 if ( rtask && cat.tv_sec ) {
2293 /* NOTE: diff __should__ always be >= 0,
2294 * AFAI understand; however (ITS#4872),
2295 * time_t might be unsigned in some systems,
2296 * while difftime() returns a double */
2297 double diff = difftime( cat.tv_sec, now );
2301 if ( tvp == NULL || diff < tv.tv_sec ) {
2308 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2309 Listener *lr = slap_listeners[l];
2311 if ( lr->sl_sd == AC_SOCKET_INVALID ) {
2315 if ( lr->sl_mute ) {
2316 Debug( LDAP_DEBUG_CONNS,
2317 "daemon: " SLAP_EVENT_FNAME ": "
2318 "listen=%d muted\n",
2323 if ( lr->sl_busy ) {
2324 Debug( LDAP_DEBUG_CONNS,
2325 "daemon: " SLAP_EVENT_FNAME ": "
2331 Debug( LDAP_DEBUG_CONNS,
2332 "daemon: " SLAP_EVENT_FNAME ": "
2333 "listen=%d active_threads=%d tvp=%s\n",
2334 lr->sl_sd, at, tvp == NULL ? "NULL" : "zero" );
2337 SLAP_EVENT_WAIT( tvp, &ns );
2339 case -1: { /* failure - try again */
2340 int err = sock_errno();
2342 if ( err != EINTR ) {
2345 /* Don't log unless we got it twice in a row */
2346 if ( !( ebadf & 1 ) ) {
2347 Debug( LDAP_DEBUG_ANY,
2353 sock_errstr( err ) );
2355 if ( ebadf >= SLAPD_EBADF_LIMIT ) {
2362 case 0: /* timeout - let threads run */
2364 #ifndef HAVE_YIELDING_SELECT
2365 Debug( LDAP_DEBUG_CONNS, "daemon: " SLAP_EVENT_FNAME
2366 "timeout - yielding\n",
2369 ldap_pvt_thread_yield();
2370 #endif /* ! HAVE_YIELDING_SELECT */
2373 default: /* something happened - deal with it */
2374 if ( slapd_shutdown ) continue;
2377 Debug( LDAP_DEBUG_CONNS,
2378 "daemon: activity on %d descriptor%s\n",
2379 ns, ns != 1 ? "s" : "", 0 );
2383 #if SLAP_EVENTS_ARE_INDEXED
2384 if ( SLAP_EVENT_IS_READ( wake_sds[0] ) ) {
2386 SLAP_EVENT_CLR_READ( wake_sds[0] );
2388 tcp_read( SLAP_FD2SOCK(wake_sds[0]), c, sizeof(c) );
2389 Debug( LDAP_DEBUG_CONNS, "daemon: waked\n", 0, 0, 0 );
2393 /* The event slot equals the descriptor number - this is
2394 * true for Unix select and poll. We treat Windows select
2395 * like this too, even though it's a kludge.
2397 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2400 if ( ns <= 0 ) break;
2401 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ) continue;
2402 #ifdef LDAP_CONNECTIONLESS
2403 if ( slap_listeners[l]->sl_is_udp ) continue;
2404 #endif /* LDAP_CONNECTIONLESS */
2405 if ( !SLAP_EVENT_IS_READ( slap_listeners[l]->sl_sd ) ) continue;
2408 SLAP_EVENT_CLR_READ( slap_listeners[l]->sl_sd );
2409 SLAP_EVENT_CLR_WRITE( slap_listeners[l]->sl_sd );
2412 rc = slap_listener_activate( slap_listeners[l] );
2415 /* bypass the following tests if no descriptors left */
2417 #ifndef HAVE_YIELDING_SELECT
2418 ldap_pvt_thread_yield();
2419 #endif /* HAVE_YIELDING_SELECT */
2423 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
2426 for ( i = 0; i < nfds; i++ ) {
2429 r = SLAP_EVENT_IS_READ( i );
2430 /* writefds was not initialized if nwriters was zero */
2431 w = nwriters ? SLAP_EVENT_IS_WRITE( i ) : 0;
2433 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
2434 r ? "r" : "", w ? "w" : "" );
2444 if ( ns <= 0 ) break;
2446 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
2448 /* loop through the writers */
2449 for ( i = 0; nwfds > 0; i++ ) {
2451 if ( ! SLAP_EVENT_IS_WRITE( i ) ) continue;
2454 SLAP_EVENT_CLR_WRITE( wd );
2457 Debug( LDAP_DEBUG_CONNS,
2458 "daemon: write active on %d\n",
2462 * NOTE: it is possible that the connection was closed
2463 * and that the stream is now inactive.
2464 * connection_write() must validate the stream is still
2467 * ITS#4338: if the stream is invalid, there is no need to
2468 * close it here. It has already been closed in connection.c.
2470 if ( connection_write( wd ) < 0 ) {
2471 if ( SLAP_EVENT_IS_READ( wd ) ) {
2472 SLAP_EVENT_CLR_READ( (unsigned) wd );
2478 for ( i = 0; nrfds > 0; i++ ) {
2480 if ( ! SLAP_EVENT_IS_READ( i ) ) continue;
2482 SLAP_EVENT_CLR_READ( rd );
2485 Debug ( LDAP_DEBUG_CONNS,
2486 "daemon: read activity on %d\n", rd, 0, 0 );
2488 * NOTE: it is possible that the connection was closed
2489 * and that the stream is now inactive.
2490 * connection_read() must valid the stream is still
2494 connection_read_activate( rd );
2496 #else /* !SLAP_EVENTS_ARE_INDEXED */
2498 /* The events are returned in an arbitrary list. This is true
2499 * for /dev/poll, epoll and kqueue. In order to prioritize things
2500 * so that we can handle wake_sds first, listeners second, and then
2501 * all other connections last (as we do for select), we would need
2502 * to use multiple event handles and cascade them.
2504 * That seems like a bit of hassle. So the wake_sds check has been
2505 * skipped. For epoll and kqueue we can associate arbitrary data with
2506 * an event, so we could use pointers to the listener structure
2507 * instead of just the file descriptor. For /dev/poll we have to
2508 * search the listeners array for a matching descriptor.
2510 * We now handle wake events when we see them; they are not given
2514 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
2516 for ( i = 0; i < ns; i++ ) {
2519 /* Don't log listener events */
2520 if ( SLAP_EVENT_IS_LISTENER( i )
2521 #ifdef LDAP_CONNECTIONLESS
2522 && !( (SLAP_EVENT_LISTENER( i ))->sl_is_udp )
2523 #endif /* LDAP_CONNECTIONLESS */
2529 fd = SLAP_EVENT_FD( i );
2530 /* Don't log internal wake events */
2531 if ( fd == wake_sds[0] ) continue;
2533 r = SLAP_EVENT_IS_READ( i );
2534 w = SLAP_EVENT_IS_WRITE( i );
2536 Debug( LDAP_DEBUG_CONNS, " %d%s%s", fd,
2537 r ? "r" : "", w ? "w" : "" );
2540 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
2541 #endif /* LDAP_DEBUG */
2543 for ( i = 0; i < ns; i++ ) {
2544 int rc = 1, fd, w = 0;
2546 if ( SLAP_EVENT_IS_LISTENER( i ) ) {
2547 rc = slap_listener_activate( SLAP_EVENT_LISTENER( i ) );
2550 /* If we found a regular listener, rc is now zero, and we
2551 * can skip the data portion. But if it was a UDP listener
2552 * then rc is still 1, and we want to handle the data.
2555 fd = SLAP_EVENT_FD( i );
2557 /* Handle wake events */
2558 if ( fd == wake_sds[0] ) {
2561 tcp_read( SLAP_FD2SOCK(wake_sds[0]), c, sizeof(c) );
2565 if ( SLAP_EVENT_IS_WRITE( i ) ) {
2566 Debug( LDAP_DEBUG_CONNS,
2567 "daemon: write active on %d\n",
2570 SLAP_EVENT_CLR_WRITE( i );
2574 * NOTE: it is possible that the connection was closed
2575 * and that the stream is now inactive.
2576 * connection_write() must valid the stream is still
2579 if ( connection_write( fd ) < 0 ) {
2583 /* If event is a read */
2584 if ( SLAP_EVENT_IS_READ( i ) ) {
2585 Debug( LDAP_DEBUG_CONNS,
2586 "daemon: read active on %d\n",
2589 SLAP_EVENT_CLR_READ( i );
2590 connection_read_activate( fd );
2592 Debug( LDAP_DEBUG_CONNS,
2593 "daemon: hangup on %d\n", fd, 0, 0 );
2594 connection_hangup( fd );
2598 #endif /* SLAP_EVENTS_ARE_INDEXED */
2600 #ifndef HAVE_YIELDING_SELECT
2601 ldap_pvt_thread_yield();
2602 #endif /* ! HAVE_YIELDING_SELECT */
2605 if ( slapd_shutdown == 1 ) {
2606 Debug( LDAP_DEBUG_ANY,
2607 "daemon: shutdown requested and initiated.\n",
2610 } else if ( slapd_shutdown == 2 ) {
2611 #ifdef HAVE_NT_SERVICE_MANAGER
2612 Debug( LDAP_DEBUG_ANY,
2613 "daemon: shutdown initiated by Service Manager.\n",
2615 #else /* !HAVE_NT_SERVICE_MANAGER */
2616 Debug( LDAP_DEBUG_ANY,
2617 "daemon: abnormal condition, shutdown initiated.\n",
2619 #endif /* !HAVE_NT_SERVICE_MANAGER */
2621 Debug( LDAP_DEBUG_ANY,
2622 "daemon: no active streams, shutdown initiated.\n",
2626 if ( slapd_gentle_shutdown != 2 ) close_listeners ( 0 );
2628 if ( !slapd_gentle_shutdown ) {
2629 slapd_abrupt_shutdown = 1;
2630 connections_shutdown();
2633 if ( LogTest( LDAP_DEBUG_ANY )) {
2634 int t = ldap_pvt_thread_pool_backload( &connection_pool );
2635 Debug( LDAP_DEBUG_ANY,
2636 "slapd shutdown: waiting for %d operations/tasks to finish\n",
2639 ldap_pvt_thread_pool_destroy( &connection_pool, 1 );
2641 free( slap_listeners );
2642 slap_listeners = NULL;
2648 #ifdef LDAP_CONNECTIONLESS
2650 connectionless_init( void )
2654 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2655 Listener *lr = slap_listeners[l];
2658 if ( !lr->sl_is_udp ) {
2662 c = connection_init( lr->sl_sd, lr, "", "",
2663 CONN_IS_UDP, (slap_ssf_t) 0, NULL
2664 LDAP_PF_LOCAL_SENDMSG_ARG(NULL));
2667 Debug( LDAP_DEBUG_TRACE,
2668 "connectionless_init: failed on %s (%d)\n",
2669 lr->sl_url, lr->sl_sd, 0 );
2677 #endif /* LDAP_CONNECTIONLESS */
2680 slapd_daemon( void )
2684 #ifdef LDAP_CONNECTIONLESS
2685 connectionless_init();
2686 #endif /* LDAP_CONNECTIONLESS */
2688 #define SLAPD_LISTENER_THREAD 1
2689 #if defined( SLAPD_LISTENER_THREAD )
2691 ldap_pvt_thread_t listener_tid;
2693 /* listener as a separate THREAD */
2694 rc = ldap_pvt_thread_create( &listener_tid,
2695 0, slapd_daemon_task, NULL );
2698 Debug( LDAP_DEBUG_ANY,
2699 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
2703 /* wait for the listener thread to complete */
2704 ldap_pvt_thread_join( listener_tid, (void *)NULL );
2706 #else /* ! SLAPD_LISTENER_THREAD */
2707 /* experimental code */
2708 slapd_daemon_task( NULL );
2709 #endif /* ! SLAPD_LISTENER_THREAD */
2717 #if defined( HAVE_WINSOCK2 )
2718 WORD wVersionRequested;
2722 wVersionRequested = MAKEWORD( 2, 0 );
2724 err = WSAStartup( wVersionRequested, &wsaData );
2726 /* Tell the user that we couldn't find a usable */
2731 /* Confirm that the WinSock DLL supports 2.0.*/
2732 /* Note that if the DLL supports versions greater */
2733 /* than 2.0 in addition to 2.0, it will still return */
2734 /* 2.0 in wVersion since that is the version we */
2737 if ( LOBYTE( wsaData.wVersion ) != 2 ||
2738 HIBYTE( wsaData.wVersion ) != 0 )
2740 /* Tell the user that we couldn't find a usable */
2746 /* The WinSock DLL is acceptable. Proceed. */
2747 #elif defined( HAVE_WINSOCK )
2749 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) return -1;
2750 #endif /* ! HAVE_WINSOCK2 && ! HAVE_WINSOCK */
2758 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
2760 #endif /* HAVE_WINSOCK2 || HAVE_WINSOCK */
2767 slap_sig_shutdown( int sig )
2770 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
2774 * If the NT Service Manager is controlling the server, we don't
2775 * want SIGBREAK to kill the server. For some strange reason,
2776 * SIGBREAK is generated when a user logs out.
2779 #if defined(HAVE_NT_SERVICE_MANAGER) && defined(SIGBREAK)
2780 if (is_NT_Service && sig == SIGBREAK) {
2783 #endif /* HAVE_NT_SERVICE_MANAGER && SIGBREAK */
2785 if (sig == SIGHUP && global_gentlehup && slapd_gentle_shutdown == 0) {
2786 slapd_gentle_shutdown = 1;
2795 /* reinstall self */
2796 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
2800 slap_sig_wake( int sig )
2804 /* reinstall self */
2805 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
2810 slapd_add_internal( ber_socket_t s, int isactive )
2812 slapd_add( s, isactive, NULL );
2816 slapd_get_listeners( void )
2818 return slap_listeners;
2822 slap_wake_listener()