2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2012 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 #define MAX_DAEMON_THREADS 16
77 int slapd_daemon_threads = 1;
78 int slapd_daemon_mask;
80 #ifdef LDAP_TCP_BUFFER
83 #endif /* LDAP_TCP_BUFFER */
85 Listener **slap_listeners = NULL;
86 static volatile sig_atomic_t listening = 1; /* 0 when slap_listeners closed */
87 static ldap_pvt_thread_t *listener_tid;
89 #ifndef SLAPD_LISTEN_BACKLOG
90 #define SLAPD_LISTEN_BACKLOG 1024
91 #endif /* ! SLAPD_LISTEN_BACKLOG */
93 #define DAEMON_ID(fd) (fd & slapd_daemon_mask)
95 static ber_socket_t wake_sds[MAX_DAEMON_THREADS][2];
98 static time_t chk_writetime;
100 static volatile int waking;
102 #define WAKE_LISTENER(l,w) do { \
103 if ((w) && ++waking < 5) { \
104 tcp_write( SLAP_FD2SOCK(wake_sds[l][1]), "0", 1 ); \
107 #else /* ! NO_THREADS */
108 #define WAKE_LISTENER(l,w) do { \
110 tcp_write( SLAP_FD2SOCK(wake_sds[l][1]), "0", 1 ); \
113 #endif /* ! NO_THREADS */
115 volatile sig_atomic_t slapd_shutdown = 0;
116 volatile sig_atomic_t slapd_gentle_shutdown = 0;
117 volatile sig_atomic_t slapd_abrupt_shutdown = 0;
120 ldap_pvt_thread_mutex_t slapd_ws_mutex;
121 SOCKET *slapd_ws_sockets;
125 #define SD_LISTENER 8
129 static ldap_pvt_thread_mutex_t sd_tcpd_mutex;
130 #endif /* TCP Wrappers */
132 typedef struct slap_daemon_st {
133 ldap_pvt_thread_mutex_t sd_mutex;
135 ber_socket_t sd_nactives;
139 #if defined(HAVE_EPOLL)
140 struct epoll_event *sd_epolls;
143 #elif defined(SLAP_X_DEVPOLL) && defined(HAVE_DEVPOLL)
145 struct pollfd *sd_pollfd;
149 #else /* ! epoll && ! /dev/poll */
153 #else /* ! HAVE_WINSOCK */
157 #endif /* ! HAVE_WINSOCK */
158 #endif /* ! epoll && ! /dev/poll */
161 static slap_daemon_st slap_daemon[MAX_DAEMON_THREADS];
164 * NOTE: naming convention for macros:
166 * - SLAP_SOCK_* and SLAP_EVENT_* for public interface that deals
167 * with file descriptors and events respectively
169 * - SLAP_<type>_* for private interface; type by now is one of
170 * EPOLL, DEVPOLL, SELECT
172 * private interface should not be used in the code.
174 #if defined(HAVE_EPOLL)
175 /***************************************
176 * Use epoll infrastructure - epoll(4) *
177 ***************************************/
178 # define SLAP_EVENT_FNAME "epoll"
179 # define SLAP_EVENTS_ARE_INDEXED 0
180 # define SLAP_EPOLL_SOCK_IX(t,s) (slap_daemon[t].sd_index[(s)])
181 # define SLAP_EPOLL_SOCK_EP(t,s) (slap_daemon[t].sd_epolls[SLAP_EPOLL_SOCK_IX(t,s)])
182 # define SLAP_EPOLL_SOCK_EV(t,s) (SLAP_EPOLL_SOCK_EP(t,s).events)
183 # define SLAP_SOCK_IS_ACTIVE(t,s) (SLAP_EPOLL_SOCK_IX(t,s) != -1)
184 # define SLAP_SOCK_NOT_ACTIVE(t,s) (SLAP_EPOLL_SOCK_IX(t,s) == -1)
185 # define SLAP_EPOLL_SOCK_IS_SET(t,s, mode) (SLAP_EPOLL_SOCK_EV(t,s) & (mode))
187 # define SLAP_SOCK_IS_READ(t,s) SLAP_EPOLL_SOCK_IS_SET(t,(s), EPOLLIN)
188 # define SLAP_SOCK_IS_WRITE(t,s) SLAP_EPOLL_SOCK_IS_SET(t,(s), EPOLLOUT)
190 # define SLAP_EPOLL_SOCK_SET(t,s, mode) do { \
191 if ( (SLAP_EPOLL_SOCK_EV(t,s) & (mode)) != (mode) ) { \
192 SLAP_EPOLL_SOCK_EV(t,s) |= (mode); \
193 epoll_ctl( slap_daemon[t].sd_epfd, EPOLL_CTL_MOD, (s), \
194 &SLAP_EPOLL_SOCK_EP(t,s) ); \
198 # define SLAP_EPOLL_SOCK_CLR(t,s, mode) do { \
199 if ( (SLAP_EPOLL_SOCK_EV(t,s) & (mode)) ) { \
200 SLAP_EPOLL_SOCK_EV(t,s) &= ~(mode); \
201 epoll_ctl( slap_daemon[t].sd_epfd, EPOLL_CTL_MOD, s, \
202 &SLAP_EPOLL_SOCK_EP(t,s) ); \
206 # define SLAP_SOCK_SET_READ(t,s) SLAP_EPOLL_SOCK_SET(t,s, EPOLLIN)
207 # define SLAP_SOCK_SET_WRITE(t,s) SLAP_EPOLL_SOCK_SET(t,s, EPOLLOUT)
209 # define SLAP_SOCK_CLR_READ(t,s) SLAP_EPOLL_SOCK_CLR(t,(s), EPOLLIN)
210 # define SLAP_SOCK_CLR_WRITE(t,s) SLAP_EPOLL_SOCK_CLR(t,(s), EPOLLOUT)
212 # define SLAP_SOCK_SET_SUSPEND(t,s) \
213 ( slap_daemon[t].sd_suspend[SLAP_EPOLL_SOCK_IX(t,s)] = 1 )
214 # define SLAP_SOCK_CLR_SUSPEND(t,s) \
215 ( slap_daemon[t].sd_suspend[SLAP_EPOLL_SOCK_IX(t,s)] = 0 )
216 # define SLAP_SOCK_IS_SUSPEND(t,s) \
217 ( slap_daemon[t].sd_suspend[SLAP_EPOLL_SOCK_IX(t,s)] == 1 )
219 # define SLAP_EPOLL_EVENT_CLR(i, mode) (revents[(i)].events &= ~(mode))
221 # define SLAP_EVENT_MAX(t) slap_daemon[t].sd_nfds
223 /* If a Listener address is provided, store that as the epoll data.
224 * Otherwise, store the address of this socket's slot in the
225 * index array. If we can't do this add, the system is out of
226 * resources and we need to shutdown.
228 # define SLAP_SOCK_ADD(t, s, l) do { \
230 SLAP_EPOLL_SOCK_IX(t,(s)) = slap_daemon[t].sd_nfds; \
231 SLAP_EPOLL_SOCK_EP(t,(s)).data.ptr = (l) ? (l) : (void *)(&SLAP_EPOLL_SOCK_IX(t,s)); \
232 SLAP_EPOLL_SOCK_EV(t,(s)) = EPOLLIN; \
233 rc = epoll_ctl(slap_daemon[t].sd_epfd, EPOLL_CTL_ADD, \
234 (s), &SLAP_EPOLL_SOCK_EP(t,(s))); \
236 slap_daemon[t].sd_nfds++; \
238 Debug( LDAP_DEBUG_ANY, \
239 "daemon: epoll_ctl(ADD,fd=%d) failed, errno=%d, shutting down\n", \
241 slapd_shutdown = 2; \
245 # define SLAP_EPOLL_EV_LISTENER(t,ptr) \
246 (((int *)(ptr) >= slap_daemon[t].sd_index && \
247 (int *)(ptr) <= &slap_daemon[t].sd_index[dtblsize]) ? 0 : 1 )
249 # define SLAP_EPOLL_EV_PTRFD(t,ptr) (SLAP_EPOLL_EV_LISTENER(t,ptr) ? \
250 ((Listener *)ptr)->sl_sd : \
251 (ber_socket_t) ((int *)(ptr) - slap_daemon[t].sd_index))
253 # define SLAP_SOCK_DEL(t,s) do { \
254 int fd, rc, index = SLAP_EPOLL_SOCK_IX(t,(s)); \
255 if ( index < 0 ) break; \
256 rc = epoll_ctl(slap_daemon[t].sd_epfd, EPOLL_CTL_DEL, \
257 (s), &SLAP_EPOLL_SOCK_EP(t,(s))); \
258 slap_daemon[t].sd_epolls[index] = \
259 slap_daemon[t].sd_epolls[slap_daemon[t].sd_nfds-1]; \
260 fd = SLAP_EPOLL_EV_PTRFD(t,slap_daemon[t].sd_epolls[index].data.ptr); \
261 slap_daemon[t].sd_index[fd] = index; \
262 slap_daemon[t].sd_index[(s)] = -1; \
263 slap_daemon[t].sd_nfds--; \
266 # define SLAP_EVENT_CLR_READ(i) SLAP_EPOLL_EVENT_CLR((i), EPOLLIN)
267 # define SLAP_EVENT_CLR_WRITE(i) SLAP_EPOLL_EVENT_CLR((i), EPOLLOUT)
269 # define SLAP_EPOLL_EVENT_CHK(i, mode) (revents[(i)].events & mode)
271 # define SLAP_EVENT_IS_READ(i) SLAP_EPOLL_EVENT_CHK((i), EPOLLIN)
272 # define SLAP_EVENT_IS_WRITE(i) SLAP_EPOLL_EVENT_CHK((i), EPOLLOUT)
273 # define SLAP_EVENT_IS_LISTENER(t,i) SLAP_EPOLL_EV_LISTENER(t,revents[(i)].data.ptr)
274 # define SLAP_EVENT_LISTENER(t,i) ((Listener *)(revents[(i)].data.ptr))
276 # define SLAP_EVENT_FD(t,i) SLAP_EPOLL_EV_PTRFD(t,revents[(i)].data.ptr)
278 # define SLAP_SOCK_INIT(t) do { \
280 slap_daemon[t].sd_epolls = ch_calloc(1, \
281 ( sizeof(struct epoll_event) * 2 \
282 + sizeof(int) ) * dtblsize * 2); \
283 slap_daemon[t].sd_index = (int *)&slap_daemon[t].sd_epolls[ 2 * dtblsize ]; \
284 slap_daemon[t].sd_epfd = epoll_create( dtblsize / slapd_daemon_threads ); \
285 for ( j = 0; j < dtblsize; j++ ) slap_daemon[t].sd_index[j] = -1; \
288 # define SLAP_SOCK_DESTROY(t) do { \
289 if ( slap_daemon[t].sd_epolls != NULL ) { \
290 ch_free( slap_daemon[t].sd_epolls ); \
291 slap_daemon[t].sd_epolls = NULL; \
292 slap_daemon[t].sd_index = NULL; \
293 close( slap_daemon[t].sd_epfd ); \
297 # define SLAP_EVENT_DECL struct epoll_event *revents
299 # define SLAP_EVENT_INIT(t) do { \
300 revents = slap_daemon[t].sd_epolls + dtblsize; \
303 # define SLAP_EVENT_WAIT(t, tvp, nsp) do { \
304 *(nsp) = epoll_wait( slap_daemon[t].sd_epfd, revents, \
305 dtblsize, (tvp) ? (tvp)->tv_sec * 1000 : -1 ); \
308 #elif defined(SLAP_X_DEVPOLL) && defined(HAVE_DEVPOLL)
310 /*************************************************************
311 * Use Solaris' (>= 2.7) /dev/poll infrastructure - poll(7d) *
312 *************************************************************/
313 # define SLAP_EVENT_FNAME "/dev/poll"
314 # define SLAP_EVENTS_ARE_INDEXED 0
316 * - sd_index is used much like with epoll()
317 * - sd_l is maintained as an array containing the address
318 * of the listener; the index is the fd itself
319 * - sd_pollfd is used to keep track of what data has been
320 * registered in /dev/poll
322 # define SLAP_DEVPOLL_SOCK_IX(t,s) (slap_daemon[t].sd_index[(s)])
323 # define SLAP_DEVPOLL_SOCK_LX(t,s) (slap_daemon[t].sd_l[(s)])
324 # define SLAP_DEVPOLL_SOCK_EP(t,s) (slap_daemon[t].sd_pollfd[SLAP_DEVPOLL_SOCK_IX(t,(s))])
325 # define SLAP_DEVPOLL_SOCK_FD(t,s) (SLAP_DEVPOLL_SOCK_EP(t,(s)).fd)
326 # define SLAP_DEVPOLL_SOCK_EV(t,s) (SLAP_DEVPOLL_SOCK_EP(t,(s)).events)
327 # define SLAP_SOCK_IS_ACTIVE(t,s) (SLAP_DEVPOLL_SOCK_IX(t,(s)) != -1)
328 # define SLAP_SOCK_NOT_ACTIVE(t,s) (SLAP_DEVPOLL_SOCK_IX(t,(s)) == -1)
329 # define SLAP_SOCK_IS_SET(t,s, mode) (SLAP_DEVPOLL_SOCK_EV(t,(s)) & (mode))
331 # define SLAP_SOCK_IS_READ(t,s) SLAP_SOCK_IS_SET(t,(s), POLLIN)
332 # define SLAP_SOCK_IS_WRITE(t,s) SLAP_SOCK_IS_SET(t,(s), POLLOUT)
334 /* as far as I understand, any time we need to communicate with the kernel
335 * about the number and/or properties of a file descriptor we need it to
336 * wait for, we have to rewrite the whole set */
337 # define SLAP_DEVPOLL_WRITE_POLLFD(t,s, pfd, n, what, shdn) do { \
339 size_t size = (n) * sizeof( struct pollfd ); \
340 /* FIXME: use pwrite? */ \
341 rc = write( slap_daemon[t].sd_dpfd, (pfd), size ); \
342 if ( rc != size ) { \
343 Debug( LDAP_DEBUG_ANY, "daemon: " SLAP_EVENT_FNAME ": " \
344 "%s fd=%d failed errno=%d\n", \
345 (what), (s), errno ); \
347 slapd_shutdown = 2; \
352 # define SLAP_DEVPOLL_SOCK_SET(t,s, mode) do { \
353 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_SET_%s(%d)=%d\n", \
354 (mode) == POLLIN ? "READ" : "WRITE", (s), \
355 ( (SLAP_DEVPOLL_SOCK_EV(t,(s)) & (mode)) != (mode) ) ); \
356 if ( (SLAP_DEVPOLL_SOCK_EV(t,(s)) & (mode)) != (mode) ) { \
358 SLAP_DEVPOLL_SOCK_EV(t,(s)) |= (mode); \
359 pfd.fd = SLAP_DEVPOLL_SOCK_FD(t,(s)); \
360 pfd.events = /* (mode) */ SLAP_DEVPOLL_SOCK_EV(t,(s)); \
361 SLAP_DEVPOLL_WRITE_POLLFD(t,(s), &pfd, 1, "SET", 0); \
365 # define SLAP_DEVPOLL_SOCK_CLR(t,s, mode) do { \
366 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_CLR_%s(%d)=%d\n", \
367 (mode) == POLLIN ? "READ" : "WRITE", (s), \
368 ( (SLAP_DEVPOLL_SOCK_EV(t,(s)) & (mode)) == (mode) ) ); \
369 if ((SLAP_DEVPOLL_SOCK_EV(t,(s)) & (mode)) == (mode) ) { \
370 struct pollfd pfd[2]; \
371 SLAP_DEVPOLL_SOCK_EV(t,(s)) &= ~(mode); \
372 pfd[0].fd = SLAP_DEVPOLL_SOCK_FD(t,(s)); \
373 pfd[0].events = POLLREMOVE; \
374 pfd[1] = SLAP_DEVPOLL_SOCK_EP(t,(s)); \
375 SLAP_DEVPOLL_WRITE_POLLFD(t,(s), &pfd[0], 2, "CLR", 0); \
379 # define SLAP_SOCK_SET_READ(t,s) SLAP_DEVPOLL_SOCK_SET(t,s, POLLIN)
380 # define SLAP_SOCK_SET_WRITE(t,s) SLAP_DEVPOLL_SOCK_SET(t,s, POLLOUT)
382 # define SLAP_SOCK_CLR_READ(t,s) SLAP_DEVPOLL_SOCK_CLR(t,(s), POLLIN)
383 # define SLAP_SOCK_CLR_WRITE(t,s) SLAP_DEVPOLL_SOCK_CLR(t,(s), POLLOUT)
385 # define SLAP_SOCK_SET_SUSPEND(t,s) \
386 ( slap_daemon[t].sd_suspend[SLAP_DEVPOLL_SOCK_IX(t,(s))] = 1 )
387 # define SLAP_SOCK_CLR_SUSPEND(t,s) \
388 ( slap_daemon[t].sd_suspend[SLAP_DEVPOLL_SOCK_IX(t,(s))] = 0 )
389 # define SLAP_SOCK_IS_SUSPEND(t,s) \
390 ( slap_daemon[t].sd_suspend[SLAP_DEVPOLL_SOCK_IX(t,(s))] == 1 )
392 # define SLAP_DEVPOLL_EVENT_CLR(i, mode) (revents[(i)].events &= ~(mode))
394 # define SLAP_EVENT_MAX(t) slap_daemon[t].sd_nfds
396 /* If a Listener address is provided, store that in the sd_l array.
397 * If we can't do this add, the system is out of resources and we
400 # define SLAP_SOCK_ADD(t, s, l) do { \
401 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_ADD(%d, %p)\n", (s), (l), 0 ); \
402 SLAP_DEVPOLL_SOCK_IX(t,(s)) = slap_daemon[t].sd_nfds; \
403 SLAP_DEVPOLL_SOCK_LX(t,(s)) = (l); \
404 SLAP_DEVPOLL_SOCK_FD(t,(s)) = (s); \
405 SLAP_DEVPOLL_SOCK_EV(t,(s)) = POLLIN; \
406 SLAP_DEVPOLL_WRITE_POLLFD(t,(s), &SLAP_DEVPOLL_SOCK_EP((s)), 1, "ADD", 1); \
407 slap_daemon[t].sd_nfds++; \
410 # define SLAP_DEVPOLL_EV_LISTENER(ptr) ((ptr) != NULL)
412 # define SLAP_SOCK_DEL(t,s) do { \
413 int fd, index = SLAP_DEVPOLL_SOCK_IX(t,(s)); \
414 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_DEL(%d)\n", (s), 0, 0 ); \
415 if ( index < 0 ) break; \
416 if ( index < slap_daemon[t].sd_nfds - 1 ) { \
417 struct pollfd pfd = slap_daemon[t].sd_pollfd[index]; \
418 fd = slap_daemon[t].sd_pollfd[slap_daemon[t].sd_nfds - 1].fd; \
419 slap_daemon[t].sd_pollfd[index] = slap_daemon[t].sd_pollfd[slap_daemon[t].sd_nfds - 1]; \
420 slap_daemon[t].sd_pollfd[slap_daemon[t].sd_nfds - 1] = pfd; \
421 slap_daemon[t].sd_index[fd] = index; \
423 slap_daemon[t].sd_index[(s)] = -1; \
424 slap_daemon[t].sd_pollfd[slap_daemon[t].sd_nfds - 1].events = POLLREMOVE; \
425 SLAP_DEVPOLL_WRITE_POLLFD(t,(s), &slap_daemon[t].sd_pollfd[slap_daemon[t].sd_nfds - 1], 1, "DEL", 0); \
426 slap_daemon[t].sd_pollfd[slap_daemon[t].sd_nfds - 1].events = 0; \
427 slap_daemon[t].sd_nfds--; \
430 # define SLAP_EVENT_CLR_READ(i) SLAP_DEVPOLL_EVENT_CLR((i), POLLIN)
431 # define SLAP_EVENT_CLR_WRITE(i) SLAP_DEVPOLL_EVENT_CLR((i), POLLOUT)
433 # define SLAP_DEVPOLL_EVENT_CHK(i, mode) (revents[(i)].events & (mode))
435 # define SLAP_EVENT_FD(t,i) (revents[(i)].fd)
437 # define SLAP_EVENT_IS_READ(i) SLAP_DEVPOLL_EVENT_CHK((i), POLLIN)
438 # define SLAP_EVENT_IS_WRITE(i) SLAP_DEVPOLL_EVENT_CHK((i), POLLOUT)
439 # define SLAP_EVENT_IS_LISTENER(t,i) SLAP_DEVPOLL_EV_LISTENER(SLAP_DEVPOLL_SOCK_LX(SLAP_EVENT_FD(t,(i))))
440 # define SLAP_EVENT_LISTENER(t,i) SLAP_DEVPOLL_SOCK_LX(SLAP_EVENT_FD(t,(i)))
442 # define SLAP_SOCK_INIT(t) do { \
443 slap_daemon[t].sd_pollfd = ch_calloc( 1, \
444 ( sizeof(struct pollfd) * 2 \
446 + sizeof( Listener * ) ) * dtblsize ); \
447 slap_daemon[t].sd_index = (int *)&slap_daemon[t].sd_pollfd[ 2 * dtblsize ]; \
448 slap_daemon[t].sd_l = (Listener **)&slap_daemon[t].sd_index[ dtblsize ]; \
449 slap_daemon[t].sd_dpfd = open( SLAP_EVENT_FNAME, O_RDWR ); \
450 if ( slap_daemon[t].sd_dpfd == -1 ) { \
451 Debug( LDAP_DEBUG_ANY, "daemon: " SLAP_EVENT_FNAME ": " \
452 "open(\"" SLAP_EVENT_FNAME "\") failed errno=%d\n", \
457 for ( i = 0; i < dtblsize; i++ ) { \
458 slap_daemon[t].sd_pollfd[i].fd = -1; \
459 slap_daemon[t].sd_index[i] = -1; \
463 # define SLAP_SOCK_DESTROY(t) do { \
464 if ( slap_daemon[t].sd_pollfd != NULL ) { \
465 ch_free( slap_daemon[t].sd_pollfd ); \
466 slap_daemon[t].sd_pollfd = NULL; \
467 slap_daemon[t].sd_index = NULL; \
468 slap_daemon[t].sd_l = NULL; \
469 close( slap_daemon[t].sd_dpfd ); \
473 # define SLAP_EVENT_DECL struct pollfd *revents
475 # define SLAP_EVENT_INIT(t) do { \
476 revents = &slap_daemon[t].sd_pollfd[ dtblsize ]; \
479 # define SLAP_EVENT_WAIT(t, tvp, nsp) do { \
480 struct dvpoll sd_dvpoll; \
481 sd_dvpoll.dp_timeout = (tvp) ? (tvp)->tv_sec * 1000 : -1; \
482 sd_dvpoll.dp_nfds = dtblsize; \
483 sd_dvpoll.dp_fds = revents; \
484 *(nsp) = ioctl( slap_daemon[t].sd_dpfd, DP_POLL, &sd_dvpoll ); \
487 #else /* ! epoll && ! /dev/poll */
489 # define SLAP_EVENT_FNAME "WSselect"
490 /* Winsock provides a "select" function but its fd_sets are
491 * actually arrays of sockets. Since these sockets are handles
492 * and not a contiguous range of small integers, we manage our
493 * own "fd" table of socket handles and use their indices as
496 * All of our listener/connection structures use fds; the actual
497 * I/O functions use sockets. The SLAP_FD2SOCK macro in proto-slap.h
498 * handles the mapping.
500 * Despite the mapping overhead, this is about 45% more efficient
501 * than just using Winsock's select and FD_ISSET directly.
503 * Unfortunately Winsock's select implementation doesn't scale well
504 * as the number of connections increases. This probably needs to be
505 * rewritten to use the Winsock overlapped/asynchronous I/O functions.
507 # define SLAP_EVENTS_ARE_INDEXED 1
508 # define SLAP_EVENT_DECL fd_set readfds, writefds; char *rflags
509 # define SLAP_EVENT_INIT(t) do { \
511 FD_ZERO( &readfds ); \
512 FD_ZERO( &writefds ); \
513 rflags = slap_daemon[t].sd_rflags; \
514 memset( rflags, 0, slap_daemon[t].sd_nfds ); \
515 for ( i=0; i<slap_daemon[t].sd_nfds; i++ ) { \
516 if ( slap_daemon[t].sd_flags[i] & SD_READ ) \
517 FD_SET( slapd_ws_sockets[i], &readfds );\
518 if ( slap_daemon[t].sd_flags[i] & SD_WRITE ) \
519 FD_SET( slapd_ws_sockets[i], &writefds ); \
522 # define SLAP_EVENT_MAX(t) slap_daemon[t].sd_nfds
524 # define SLAP_EVENT_WAIT(t, tvp, nsp) do { \
526 *(nsp) = select( SLAP_EVENT_MAX(t), &readfds, \
527 nwriters > 0 ? &writefds : NULL, NULL, (tvp) ); \
528 for ( i=0; i<readfds.fd_count; i++) { \
529 int fd = slapd_sock2fd(readfds.fd_array[i]); \
531 slap_daemon[t].sd_rflags[fd] = SD_READ; \
532 if ( fd >= *(nsp)) *(nsp) = fd+1; \
535 for ( i=0; i<writefds.fd_count; i++) { \
536 int fd = slapd_sock2fd(writefds.fd_array[i]); \
538 slap_daemon[t].sd_rflags[fd] = SD_WRITE; \
539 if ( fd >= *(nsp)) *(nsp) = fd+1; \
544 # define SLAP_EVENT_IS_READ(fd) (rflags[fd] & SD_READ)
545 # define SLAP_EVENT_IS_WRITE(fd) (rflags[fd] & SD_WRITE)
547 # define SLAP_EVENT_CLR_READ(fd) rflags[fd] &= ~SD_READ
548 # define SLAP_EVENT_CLR_WRITE(fd) rflags[fd] &= ~SD_WRITE
550 # define SLAP_SOCK_INIT(t) do { \
552 ldap_pvt_thread_mutex_init( &slapd_ws_mutex ); \
553 slapd_ws_sockets = ch_malloc( dtblsize * ( sizeof(SOCKET) + 2)); \
554 memset( slapd_ws_sockets, -1, dtblsize * sizeof(SOCKET) ); \
556 slap_daemon[t].sd_flags = (char *)(slapd_ws_sockets + dtblsize); \
557 slap_daemon[t].sd_rflags = slap_daemon[t].sd_flags + dtblsize; \
558 memset( slap_daemon[t].sd_flags, 0, dtblsize ); \
559 slapd_ws_sockets[t*2] = wake_sds[t][0]; \
560 slapd_ws_sockets[t*2+1] = wake_sds[t][1]; \
561 wake_sds[t][0] = t*2; \
562 wake_sds[t][1] = t*2+1; \
563 slap_daemon[t].sd_nfds = t*2 + 2; \
566 # define SLAP_SOCK_DESTROY(t) do { \
567 ch_free( slapd_ws_sockets ); slapd_ws_sockets = NULL; \
568 slap_daemon[t].sd_flags = NULL; \
569 slap_daemon[t].sd_rflags = NULL; \
570 ldap_pvt_thread_mutex_destroy( &slapd_ws_mutex ); \
573 # define SLAP_SOCK_IS_ACTIVE(t,fd) ( slap_daemon[t].sd_flags[fd] & SD_ACTIVE )
574 # define SLAP_SOCK_IS_READ(t,fd) ( slap_daemon[t].sd_flags[fd] & SD_READ )
575 # define SLAP_SOCK_IS_WRITE(t,fd) ( slap_daemon[t].sd_flags[fd] & SD_WRITE )
576 # define SLAP_SOCK_NOT_ACTIVE(t,fd) (!slap_daemon[t].sd_flags[fd])
578 # define SLAP_SOCK_SET_READ(t,fd) ( slap_daemon[t].sd_flags[fd] |= SD_READ )
579 # define SLAP_SOCK_SET_WRITE(t,fd) ( slap_daemon[t].sd_flags[fd] |= SD_WRITE )
581 # define SLAP_SELECT_ADDTEST(t,s) do { \
582 if ((s) >= slap_daemon[t].sd_nfds) slap_daemon[t].sd_nfds = (s)+1; \
585 # define SLAP_SOCK_CLR_READ(t,fd) ( slap_daemon[t].sd_flags[fd] &= ~SD_READ )
586 # define SLAP_SOCK_CLR_WRITE(t,fd) ( slap_daemon[t].sd_flags[fd] &= ~SD_WRITE )
588 # define SLAP_SOCK_ADD(t,s, l) do { \
589 SLAP_SELECT_ADDTEST(t,(s)); \
590 slap_daemon[t].sd_flags[s] = SD_ACTIVE|SD_READ; \
593 # define SLAP_SOCK_DEL(t,s) do { \
594 slap_daemon[t].sd_flags[s] = 0; \
595 slapd_sockdel( s ); \
598 # else /* !HAVE_WINSOCK */
600 /**************************************
601 * Use select system call - select(2) *
602 **************************************/
603 # define SLAP_EVENT_FNAME "select"
605 # define SLAP_EVENTS_ARE_INDEXED 1
606 # define SLAP_EVENT_DECL fd_set readfds, writefds
608 # define SLAP_EVENT_INIT(t) do { \
609 AC_MEMCPY( &readfds, &slap_daemon[t].sd_readers, sizeof(fd_set) ); \
611 AC_MEMCPY( &writefds, &slap_daemon[t].sd_writers, sizeof(fd_set) ); \
613 FD_ZERO( &writefds ); \
618 # define SLAP_SELECT_CHK_SETSIZE do { \
619 if (dtblsize > FD_SETSIZE) dtblsize = FD_SETSIZE; \
621 # else /* ! FD_SETSIZE */
622 # define SLAP_SELECT_CHK_SETSIZE do { ; } while (0)
623 # endif /* ! FD_SETSIZE */
625 # define SLAP_SOCK_INIT(t) do { \
626 SLAP_SELECT_CHK_SETSIZE; \
627 FD_ZERO(&slap_daemon[t].sd_actives); \
628 FD_ZERO(&slap_daemon[t].sd_readers); \
629 FD_ZERO(&slap_daemon[t].sd_writers); \
632 # define SLAP_SOCK_DESTROY(t)
634 # define SLAP_SOCK_IS_ACTIVE(t,fd) FD_ISSET((fd), &slap_daemon[t].sd_actives)
635 # define SLAP_SOCK_IS_READ(t,fd) FD_ISSET((fd), &slap_daemon[t].sd_readers)
636 # define SLAP_SOCK_IS_WRITE(t,fd) FD_ISSET((fd), &slap_daemon[t].sd_writers)
638 # define SLAP_SOCK_NOT_ACTIVE(t,fd) (!SLAP_SOCK_IS_ACTIVE(t,fd) && \
639 !SLAP_SOCK_IS_READ(t,fd) && !SLAP_SOCK_IS_WRITE(t,fd))
641 # define SLAP_SOCK_SET_READ(t,fd) FD_SET((fd), &slap_daemon[t].sd_readers)
642 # define SLAP_SOCK_SET_WRITE(t,fd) FD_SET((fd), &slap_daemon[t].sd_writers)
644 # define SLAP_EVENT_MAX(t) slap_daemon[t].sd_nfds
645 # define SLAP_SELECT_ADDTEST(t,s) do { \
646 if ((s) >= slap_daemon[t].sd_nfds) slap_daemon[t].sd_nfds = (s)+1; \
649 # define SLAP_SOCK_CLR_READ(t,fd) FD_CLR((fd), &slap_daemon[t].sd_readers)
650 # define SLAP_SOCK_CLR_WRITE(t,fd) FD_CLR((fd), &slap_daemon[t].sd_writers)
652 # define SLAP_SOCK_ADD(t,s, l) do { \
653 SLAP_SELECT_ADDTEST(t,(s)); \
654 FD_SET((s), &slap_daemon[t].sd_actives); \
655 FD_SET((s), &slap_daemon[t].sd_readers); \
658 # define SLAP_SOCK_DEL(t,s) do { \
659 FD_CLR((s), &slap_daemon[t].sd_actives); \
660 FD_CLR((s), &slap_daemon[t].sd_readers); \
661 FD_CLR((s), &slap_daemon[t].sd_writers); \
664 # define SLAP_EVENT_IS_READ(fd) FD_ISSET((fd), &readfds)
665 # define SLAP_EVENT_IS_WRITE(fd) FD_ISSET((fd), &writefds)
667 # define SLAP_EVENT_CLR_READ(fd) FD_CLR((fd), &readfds)
668 # define SLAP_EVENT_CLR_WRITE(fd) FD_CLR((fd), &writefds)
670 # define SLAP_EVENT_WAIT(t, tvp, nsp) do { \
671 *(nsp) = select( SLAP_EVENT_MAX(t), &readfds, \
672 nwriters > 0 ? &writefds : NULL, NULL, (tvp) ); \
674 # endif /* !HAVE_WINSOCK */
675 #endif /* ! epoll && ! /dev/poll */
679 * SLP related functions
683 #define LDAP_SRVTYPE_PREFIX "service:ldap://"
684 #define LDAPS_SRVTYPE_PREFIX "service:ldaps://"
685 static char** slapd_srvurls = NULL;
686 static SLPHandle slapd_hslp = 0;
687 int slapd_register_slp = 0;
688 const char *slapd_slp_attrs = NULL;
690 static SLPError slapd_slp_cookie;
693 slapd_slp_init( const char* urls )
698 slapd_srvurls = ldap_str2charray( urls, " " );
700 if ( slapd_srvurls == NULL ) return;
702 /* find and expand INADDR_ANY URLs */
703 for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
704 if ( strcmp( slapd_srvurls[i], "ldap:///" ) == 0 ) {
705 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
706 global_host_bv.bv_len +
707 sizeof( LDAP_SRVTYPE_PREFIX ) );
708 strcpy( lutil_strcopy(slapd_srvurls[i],
709 LDAP_SRVTYPE_PREFIX ), global_host_bv.bv_val );
710 } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0 ) {
711 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
712 global_host_bv.bv_len +
713 sizeof( LDAPS_SRVTYPE_PREFIX ) );
714 strcpy( lutil_strcopy(slapd_srvurls[i],
715 LDAPS_SRVTYPE_PREFIX ), global_host_bv.bv_val );
719 /* open the SLP handle */
720 err = SLPOpen( "en", 0, &slapd_hslp );
722 if ( err != SLP_OK ) {
723 Debug( LDAP_DEBUG_CONNS, "daemon: SLPOpen() failed with %ld\n",
729 slapd_slp_deinit( void )
731 if ( slapd_srvurls == NULL ) return;
733 ldap_charray_free( slapd_srvurls );
734 slapd_srvurls = NULL;
736 /* close the SLP handle */
737 SLPClose( slapd_hslp );
746 /* return the error code in the cookie */
747 *(SLPError*)cookie = errcode;
756 if ( slapd_srvurls == NULL ) return;
758 for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
759 if ( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
760 sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
761 strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
762 sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
764 err = SLPReg( slapd_hslp,
766 SLP_LIFETIME_MAXIMUM,
768 (slapd_slp_attrs) ? slapd_slp_attrs : "",
773 if ( err != SLP_OK || slapd_slp_cookie != SLP_OK ) {
774 Debug( LDAP_DEBUG_CONNS,
775 "daemon: SLPReg(%s) failed with %ld, cookie = %ld\n",
776 slapd_srvurls[i], (long)err, (long)slapd_slp_cookie );
783 slapd_slp_dereg( void )
788 if ( slapd_srvurls == NULL ) return;
790 for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
791 err = SLPDereg( slapd_hslp,
796 if ( err != SLP_OK || slapd_slp_cookie != SLP_OK ) {
797 Debug( LDAP_DEBUG_CONNS,
798 "daemon: SLPDereg(%s) failed with %ld, cookie = %ld\n",
799 slapd_srvurls[i], (long)err, (long)slapd_slp_cookie );
803 #endif /* HAVE_SLP */
806 /* Manage the descriptor to socket table */
808 slapd_socknew( ber_socket_t s )
811 ldap_pvt_thread_mutex_lock( &slapd_ws_mutex );
812 for ( i = 0; i < dtblsize && slapd_ws_sockets[i] != INVALID_SOCKET; i++ );
813 if ( i == dtblsize ) {
814 WSASetLastError( WSAEMFILE );
816 slapd_ws_sockets[i] = s;
818 ldap_pvt_thread_mutex_unlock( &slapd_ws_mutex );
823 slapd_sockdel( ber_socket_t s )
825 ldap_pvt_thread_mutex_lock( &slapd_ws_mutex );
826 slapd_ws_sockets[s] = INVALID_SOCKET;
827 ldap_pvt_thread_mutex_unlock( &slapd_ws_mutex );
831 slapd_sock2fd( ber_socket_t s )
834 for ( i=0; i<dtblsize && slapd_ws_sockets[i] != s; i++);
842 * Add a descriptor to daemon control
844 * If isactive, the descriptor is a live server session and is subject
845 * to idletimeout control. Otherwise, the descriptor is a passive
846 * listener or an outbound client session, and not subject to
847 * idletimeout. The underlying event handler may record the Listener
848 * argument to differentiate Listener's from real sessions.
851 slapd_add( ber_socket_t s, int isactive, Listener *sl, int id )
855 ldap_pvt_thread_mutex_lock( &slap_daemon[id].sd_mutex );
857 assert( SLAP_SOCK_NOT_ACTIVE(id, s) );
859 if ( isactive ) slap_daemon[id].sd_nactives++;
861 SLAP_SOCK_ADD(id, s, sl);
863 Debug( LDAP_DEBUG_CONNS, "daemon: added %ldr%s listener=%p\n",
864 (long) s, isactive ? " (active)" : "", (void *)sl );
866 ldap_pvt_thread_mutex_unlock( &slap_daemon[id].sd_mutex );
872 * Remove the descriptor from daemon control
884 int id = DAEMON_ID(s);
887 ldap_pvt_thread_mutex_lock( &slap_daemon[id].sd_mutex );
889 assert( SLAP_SOCK_IS_ACTIVE( id, s ));
891 if ( wasactive ) slap_daemon[id].sd_nactives--;
893 waswriter = SLAP_SOCK_IS_WRITE(id, s);
894 wasreader = SLAP_SOCK_IS_READ(id, s);
896 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
898 wasreader ? "r" : "",
899 waswriter ? "w" : "" );
901 if ( waswriter ) slap_daemon[id].sd_nwriters--;
903 SLAP_SOCK_DEL(id, s);
906 ber_sockbuf_free(sb);
908 /* If we ran out of file descriptors, we dropped a listener from
909 * the select() loop. Now that we're removing a session from our
910 * control, we can try to resume a dropped listener to use.
912 if ( emfile && listening ) {
914 for ( i = 0; slap_listeners[i] != NULL; i++ ) {
915 Listener *lr = slap_listeners[i];
917 if ( lr->sl_sd == AC_SOCKET_INVALID ) continue;
918 if ( lr->sl_sd == s ) continue;
922 if ( DAEMON_ID(lr->sl_sd) != id )
923 WAKE_LISTENER(DAEMON_ID(lr->sl_sd), wake);
927 /* Walked the entire list without enabling anything; emfile
928 * counter is stale. Reset it.
930 if ( slap_listeners[i] == NULL ) emfile = 0;
932 ldap_pvt_thread_mutex_unlock( &slap_daemon[id].sd_mutex );
933 WAKE_LISTENER(id, wake || slapd_gentle_shutdown == 2);
937 slapd_clr_write( ber_socket_t s, int wake )
939 int id = DAEMON_ID(s);
940 ldap_pvt_thread_mutex_lock( &slap_daemon[id].sd_mutex );
942 if ( SLAP_SOCK_IS_WRITE( id, s )) {
943 assert( SLAP_SOCK_IS_ACTIVE( id, s ));
945 SLAP_SOCK_CLR_WRITE( id, s );
946 slap_daemon[id].sd_nwriters--;
949 ldap_pvt_thread_mutex_unlock( &slap_daemon[id].sd_mutex );
950 WAKE_LISTENER(id,wake);
954 slapd_set_write( ber_socket_t s, int wake )
956 int id = DAEMON_ID(s);
957 ldap_pvt_thread_mutex_lock( &slap_daemon[id].sd_mutex );
959 assert( SLAP_SOCK_IS_ACTIVE( id, s ));
961 if ( !SLAP_SOCK_IS_WRITE( id, s )) {
962 SLAP_SOCK_SET_WRITE( id, s );
963 slap_daemon[id].sd_nwriters++;
965 if (( wake & 2 ) && global_writetimeout && !chk_writetime ) {
967 ldap_pvt_thread_mutex_lock( &slap_daemon[0].sd_mutex );
969 chk_writetime = slap_get_time();
971 ldap_pvt_thread_mutex_unlock( &slap_daemon[0].sd_mutex );
974 ldap_pvt_thread_mutex_unlock( &slap_daemon[id].sd_mutex );
975 WAKE_LISTENER(id,wake);
979 slapd_clr_read( ber_socket_t s, int wake )
982 int id = DAEMON_ID(s);
983 ldap_pvt_thread_mutex_lock( &slap_daemon[id].sd_mutex );
985 if ( SLAP_SOCK_IS_ACTIVE( id, s )) {
986 SLAP_SOCK_CLR_READ( id, s );
989 ldap_pvt_thread_mutex_unlock( &slap_daemon[id].sd_mutex );
991 WAKE_LISTENER(id,wake);
996 slapd_set_read( ber_socket_t s, int wake )
999 int id = DAEMON_ID(s);
1000 ldap_pvt_thread_mutex_lock( &slap_daemon[id].sd_mutex );
1002 if( SLAP_SOCK_IS_ACTIVE( id, s ) && !SLAP_SOCK_IS_READ( id, s )) {
1003 SLAP_SOCK_SET_READ( id, s );
1007 ldap_pvt_thread_mutex_unlock( &slap_daemon[id].sd_mutex );
1009 WAKE_LISTENER(id,wake);
1013 slapd_get_writetime()
1016 ldap_pvt_thread_mutex_lock( &slap_daemon[0].sd_mutex );
1017 cur = chk_writetime;
1018 ldap_pvt_thread_mutex_unlock( &slap_daemon[0].sd_mutex );
1023 slapd_clr_writetime( time_t old )
1025 ldap_pvt_thread_mutex_lock( &slap_daemon[0].sd_mutex );
1026 if ( chk_writetime == old )
1028 ldap_pvt_thread_mutex_unlock( &slap_daemon[0].sd_mutex );
1032 slapd_close( ber_socket_t s )
1034 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
1036 tcp_close( SLAP_FD2SOCK(s) );
1043 slap_free_listener_addresses( struct sockaddr **sal )
1045 struct sockaddr **sap;
1046 if (sal == NULL) return;
1047 for (sap = sal; *sap != NULL; sap++) ch_free(*sap);
1051 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
1060 assert( exts != NULL );
1061 assert( perms != NULL );
1062 assert( crit != NULL );
1065 for ( i = 0; exts[ i ]; i++ ) {
1066 char *type = exts[ i ];
1069 if ( type[ 0 ] == '!' ) {
1074 if ( strncasecmp( type, LDAPI_MOD_URLEXT "=",
1075 sizeof(LDAPI_MOD_URLEXT "=") - 1 ) == 0 )
1077 char *value = type + ( sizeof(LDAPI_MOD_URLEXT "=") - 1 );
1081 switch (strlen(value)) {
1083 /* skip leading '0' */
1084 if ( value[ 0 ] != '0' ) return LDAP_OTHER;
1088 for ( j = 0; j < 3; j++) {
1091 v = value[ j ] - '0';
1093 if ( v < 0 || v > 7 ) return LDAP_OTHER;
1100 for ( j = 1; j < 10; j++ ) {
1101 static mode_t m[] = { 0,
1102 S_IRUSR, S_IWUSR, S_IXUSR,
1103 S_IRGRP, S_IWGRP, S_IXGRP,
1104 S_IROTH, S_IWOTH, S_IXOTH
1106 static const char c[] = "-rwxrwxrwx";
1108 if ( value[ j ] == c[ j ] ) {
1111 } else if ( value[ j ] != '-' ) {
1124 return LDAP_SUCCESS;
1130 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
1132 /* port = 0 indicates AF_LOCAL */
1134 slap_get_listener_addresses(
1136 unsigned short port,
1137 struct sockaddr ***sal )
1139 struct sockaddr **sap;
1141 #ifdef LDAP_PF_LOCAL
1143 *sal = ch_malloc(2 * sizeof(void *));
1144 if (*sal == NULL) return -1;
1147 *sap = ch_malloc(sizeof(struct sockaddr_un));
1148 if (*sap == NULL) goto errexit;
1152 (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) )
1154 Debug( LDAP_DEBUG_ANY,
1155 "daemon: domain socket path (%s) too long in URL",
1160 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
1161 (*sap)->sa_family = AF_LOCAL;
1162 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
1164 #endif /* LDAP_PF_LOCAL */
1166 #ifdef HAVE_GETADDRINFO
1167 struct addrinfo hints, *res, *sai;
1171 memset( &hints, '\0', sizeof(hints) );
1172 hints.ai_flags = AI_PASSIVE;
1173 hints.ai_socktype = SOCK_STREAM;
1174 hints.ai_family = slap_inet4or6;
1175 snprintf(serv, sizeof serv, "%d", port);
1177 if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
1178 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo() failed: %s\n",
1179 AC_GAI_STRERROR(err), 0, 0);
1184 for (n=2; (sai = sai->ai_next) != NULL; n++) {
1187 *sal = ch_calloc(n, sizeof(void *));
1188 if (*sal == NULL) return -1;
1193 for ( sai=res; sai; sai=sai->ai_next ) {
1194 if( sai->ai_addr == NULL ) {
1195 Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
1196 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
1201 switch (sai->ai_family) {
1202 # ifdef LDAP_PF_INET6
1204 *sap = ch_malloc(sizeof(struct sockaddr_in6));
1209 *(struct sockaddr_in6 *)*sap =
1210 *((struct sockaddr_in6 *)sai->ai_addr);
1212 # endif /* LDAP_PF_INET6 */
1214 *sap = ch_malloc(sizeof(struct sockaddr_in));
1219 *(struct sockaddr_in *)*sap =
1220 *((struct sockaddr_in *)sai->ai_addr);
1228 (*sap)->sa_family = sai->ai_family;
1236 #else /* ! HAVE_GETADDRINFO */
1239 struct hostent *he = NULL;
1241 if ( host == NULL ) {
1242 in.s_addr = htonl(INADDR_ANY);
1244 } else if ( !inet_aton( host, &in ) ) {
1245 he = gethostbyname( host );
1247 Debug( LDAP_DEBUG_ANY,
1248 "daemon: invalid host %s", host, 0, 0);
1251 for (n = 0; he->h_addr_list[n]; n++) /* empty */;
1254 *sal = ch_malloc((n+1) * sizeof(void *));
1255 if (*sal == NULL) return -1;
1258 for ( i = 0; i<n; i++ ) {
1259 sap[i] = ch_malloc(sizeof(struct sockaddr_in));
1260 if (*sap == NULL) goto errexit;
1262 (void)memset( (void *)sap[i], '\0', sizeof(struct sockaddr_in) );
1263 sap[i]->sa_family = AF_INET;
1264 ((struct sockaddr_in *)sap[i])->sin_port = htons(port);
1265 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr,
1266 he ? (struct in_addr *)he->h_addr_list[i] : &in,
1267 sizeof(struct in_addr) );
1270 #endif /* ! HAVE_GETADDRINFO */
1276 slap_free_listener_addresses(*sal);
1290 unsigned short port;
1291 int err, addrlen = 0;
1292 struct sockaddr **sal, **psal;
1293 int socktype = SOCK_STREAM; /* default to COTS */
1296 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
1301 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
1303 rc = ldap_url_parse( url, &lud );
1305 if( rc != LDAP_URL_SUCCESS ) {
1306 Debug( LDAP_DEBUG_ANY,
1307 "daemon: listen URL \"%s\" parse error=%d\n",
1312 l.sl_url.bv_val = NULL;
1317 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
1318 Debug( LDAP_DEBUG_ANY, "daemon: TLS not supported (%s)\n",
1320 ldap_free_urldesc( lud );
1324 if(! lud->lud_port ) lud->lud_port = LDAP_PORT;
1326 #else /* HAVE_TLS */
1327 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
1329 if(! lud->lud_port ) {
1330 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
1332 #endif /* HAVE_TLS */
1334 #ifdef LDAP_TCP_BUFFER
1337 #endif /* LDAP_TCP_BUFFER */
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) */
1502 mode_t old_umask = 0;
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 ( old_umask != 0 ) {
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 *path = ((struct sockaddr_un *)*sal)->sun_path;
1529 l.sl_name.bv_len = strlen(path) + STRLENOF("PATH=");
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 */
1537 char addr[INET_ADDRSTRLEN];
1539 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
1540 s = inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
1541 addr, sizeof(addr) );
1542 #else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1543 s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr );
1544 #endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1545 if (!s) s = SLAP_STRING_UNKNOWN;
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"),
1550 "IP=%s:%d", s, port );
1551 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1554 #ifdef LDAP_PF_INET6
1556 char addr[INET6_ADDRSTRLEN];
1558 s = inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
1560 if (!s) s = SLAP_STRING_UNKNOWN;
1561 port = ntohs( ((struct sockaddr_in6 *)*sal)->sin6_port );
1562 l.sl_name.bv_len = strlen(s) + sizeof("IP=[]:65535");
1563 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len );
1564 snprintf( l.sl_name.bv_val, l.sl_name.bv_len, "IP=[%s]:%d",
1566 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1568 #endif /* LDAP_PF_INET6 */
1571 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
1572 (int) (*sal)->sa_family, 0, 0 );
1576 AC_MEMCPY(&l.sl_sa, *sal, addrlen);
1577 ber_str2bv( url, 0, 1, &l.sl_url);
1578 li = ch_malloc( sizeof( Listener ) );
1580 slap_listeners[*cur] = li;
1585 slap_free_listener_addresses(psal);
1587 if ( l.sl_url.bv_val == NULL ) {
1588 Debug( LDAP_DEBUG_TRACE,
1589 "slap_open_listener: failed on %s\n", url, 0, 0 );
1593 Debug( LDAP_DEBUG_TRACE, "daemon: listener initialized %s\n",
1594 l.sl_url.bv_val, 0, 0 );
1598 static int sockinit(void);
1599 static int sockdestroy(void);
1601 static int daemon_inited = 0;
1604 slapd_daemon_init( const char *urls )
1609 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
1610 urls ? urls : "<null>", 0, 0 );
1612 for ( i=0; i<MAX_DAEMON_THREADS; i++ ) {
1613 wake_sds[i][0] = AC_SOCKET_INVALID;
1614 wake_sds[i][1] = AC_SOCKET_INVALID;
1617 ldap_pvt_thread_mutex_init( &slap_daemon[0].sd_mutex );
1619 ldap_pvt_thread_mutex_init( &sd_tcpd_mutex );
1620 #endif /* TCP Wrappers */
1624 if( (rc = sockinit()) != 0 ) return rc;
1627 dtblsize = sysconf( _SC_OPEN_MAX );
1628 #elif defined(HAVE_GETDTABLESIZE)
1629 dtblsize = getdtablesize();
1630 #else /* ! HAVE_SYSCONF && ! HAVE_GETDTABLESIZE */
1631 dtblsize = FD_SETSIZE;
1632 #endif /* ! HAVE_SYSCONF && ! HAVE_GETDTABLESIZE */
1634 /* open a pipe (or something equivalent connected to itself).
1635 * we write a byte on this fd whenever we catch a signal. The main
1636 * loop will be select'ing on this socket, and will wake up when
1637 * this byte arrives.
1639 if( (rc = lutil_pair( wake_sds[0] )) < 0 ) {
1640 Debug( LDAP_DEBUG_ANY,
1641 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
1644 ber_pvt_socket_set_nonblock( wake_sds[0][1], 1 );
1648 if( urls == NULL ) urls = "ldap:///";
1650 u = ldap_str2charray( urls, " " );
1652 if( u == NULL || u[0] == NULL ) {
1653 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
1656 ldap_charray_free( u );
1660 for( i=0; u[i] != NULL; i++ ) {
1661 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
1666 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
1668 ldap_charray_free( u );
1672 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
1674 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
1676 for(n = 0, j = 0; u[n]; n++ ) {
1677 if ( slap_open_listener( u[n], &i, &j ) ) {
1678 ldap_charray_free( u );
1682 slap_listeners[j] = NULL;
1684 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
1689 if( slapd_register_slp ) {
1690 slapd_slp_init( urls );
1693 #endif /* HAVE_SLP */
1695 ldap_charray_free( u );
1702 slapd_daemon_destroy( void )
1704 connections_destroy();
1705 if ( daemon_inited ) {
1708 for ( i=0; i<slapd_daemon_threads; i++ ) {
1710 if ( wake_sds[i][1] != INVALID_SOCKET &&
1711 SLAP_FD2SOCK( wake_sds[i][1] ) != SLAP_FD2SOCK( wake_sds[i][0] ))
1712 #endif /* HAVE_WINSOCK */
1713 tcp_close( SLAP_FD2SOCK(wake_sds[i][1]) );
1715 if ( wake_sds[i][0] != INVALID_SOCKET )
1716 #endif /* HAVE_WINSOCK */
1717 tcp_close( SLAP_FD2SOCK(wake_sds[i][0]) );
1718 ldap_pvt_thread_mutex_destroy( &slap_daemon[i].sd_mutex );
1719 SLAP_SOCK_DESTROY(i);
1723 ldap_pvt_thread_mutex_destroy( &sd_tcpd_mutex );
1724 #endif /* TCP Wrappers */
1729 if( slapd_register_slp ) {
1733 #endif /* HAVE_SLP */
1749 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1750 Listener *lr = slap_listeners[l];
1752 if ( lr->sl_sd != AC_SOCKET_INVALID ) {
1754 lr->sl_sd = AC_SOCKET_INVALID;
1755 if ( remove ) slapd_remove( s, NULL, 0, 0, 0 );
1757 #ifdef LDAP_PF_LOCAL
1758 if ( lr->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1759 unlink( lr->sl_sa.sa_un_addr.sun_path );
1761 #endif /* LDAP_PF_LOCAL */
1769 destroy_listeners( void )
1771 Listener *lr, **ll = slap_listeners;
1776 while ( (lr = *ll++) != NULL ) {
1777 if ( lr->sl_url.bv_val ) {
1778 ber_memfree( lr->sl_url.bv_val );
1781 if ( lr->sl_name.bv_val ) {
1782 ber_memfree( lr->sl_name.bv_val );
1788 free( slap_listeners );
1789 slap_listeners = NULL;
1798 ber_socket_t s, sfd;
1799 ber_socklen_t len = sizeof(from);
1802 struct berval authid = BER_BVNULL;
1803 #ifdef SLAPD_RLOOKUPS
1804 char hbuf[NI_MAXHOST];
1805 #endif /* SLAPD_RLOOKUPS */
1807 char *dnsname = NULL;
1808 const char *peeraddr = NULL;
1809 /* we assume INET6_ADDRSTRLEN > INET_ADDRSTRLEN */
1810 char addr[INET6_ADDRSTRLEN];
1811 #ifdef LDAP_PF_LOCAL
1812 char peername[MAXPATHLEN + sizeof("PATH=")];
1813 #ifdef LDAP_PF_LOCAL_SENDMSG
1815 struct berval peerbv = BER_BVNULL;
1817 #elif defined(LDAP_PF_INET6)
1818 char peername[sizeof("IP=[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535")];
1819 #else /* ! LDAP_PF_LOCAL && ! LDAP_PF_INET6 */
1820 char peername[sizeof("IP=255.255.255.255:65336")];
1821 #endif /* LDAP_PF_LOCAL */
1825 Debug( LDAP_DEBUG_TRACE,
1826 ">>> slap_listener(%s)\n",
1827 sl->sl_url.bv_val, 0, 0 );
1831 #ifdef LDAP_CONNECTIONLESS
1832 if ( sl->sl_is_udp ) return 1;
1833 #endif /* LDAP_CONNECTIONLESS */
1835 # ifdef LDAP_PF_LOCAL
1836 /* FIXME: apparently accept doesn't fill
1837 * the sun_path sun_path member */
1838 from.sa_un_addr.sun_path[0] = '\0';
1839 # endif /* LDAP_PF_LOCAL */
1841 s = accept( SLAP_FD2SOCK( sl->sl_sd ), (struct sockaddr *) &from, &len );
1843 /* Resume the listener FD to allow concurrent-processing of
1844 * additional incoming connections.
1847 WAKE_LISTENER(DAEMON_ID(sl->sl_sd),1);
1849 if ( s == AC_SOCKET_INVALID ) {
1850 int err = sock_errno();
1861 ldap_pvt_thread_mutex_lock( &slap_daemon[0].sd_mutex );
1863 /* Stop listening until an existing session closes */
1865 ldap_pvt_thread_mutex_unlock( &slap_daemon[0].sd_mutex );
1868 Debug( LDAP_DEBUG_ANY,
1869 "daemon: accept(%ld) failed errno=%d (%s)\n",
1870 (long) sl->sl_sd, err, sock_errstr(err) );
1871 ldap_pvt_thread_yield();
1874 sfd = SLAP_SOCKNEW( s );
1876 /* make sure descriptor number isn't too great */
1877 if ( sfd >= dtblsize ) {
1878 Debug( LDAP_DEBUG_ANY,
1879 "daemon: %ld beyond descriptor table size %ld\n",
1880 (long) sfd, (long) dtblsize, 0 );
1883 ldap_pvt_thread_yield();
1886 tid = DAEMON_ID(sfd);
1889 ldap_pvt_thread_mutex_lock( &slap_daemon[tid].sd_mutex );
1890 /* newly accepted stream should not be in any of the FD SETS */
1891 assert( SLAP_SOCK_NOT_ACTIVE( tid, sfd ));
1892 ldap_pvt_thread_mutex_unlock( &slap_daemon[tid].sd_mutex );
1893 #endif /* LDAP_DEBUG */
1895 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1896 #ifdef LDAP_PF_LOCAL
1897 /* for IPv4 and IPv6 sockets only */
1898 if ( from.sa_addr.sa_family != AF_LOCAL )
1899 #endif /* LDAP_PF_LOCAL */
1904 /* enable keep alives */
1906 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1907 (char *) &tmp, sizeof(tmp) );
1908 if ( rc == AC_SOCKET_ERROR ) {
1909 int err = sock_errno();
1910 Debug( LDAP_DEBUG_ANY,
1911 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1912 "errno=%d (%s)\n", (long) sfd, err, sock_errstr(err) );
1914 #endif /* SO_KEEPALIVE */
1916 /* enable no delay */
1918 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1919 (char *)&tmp, sizeof(tmp) );
1920 if ( rc == AC_SOCKET_ERROR ) {
1921 int err = sock_errno();
1922 Debug( LDAP_DEBUG_ANY,
1923 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1924 "errno=%d (%s)\n", (long) sfd, err, sock_errstr(err) );
1926 #endif /* TCP_NODELAY */
1928 #endif /* SO_KEEPALIVE || TCP_NODELAY */
1930 Debug( LDAP_DEBUG_CONNS,
1931 "daemon: listen=%ld, new connection on %ld\n",
1932 (long) sl->sl_sd, (long) sfd, 0 );
1935 switch ( from.sa_addr.sa_family ) {
1936 # ifdef LDAP_PF_LOCAL
1938 cflag |= CONN_IS_IPC;
1940 /* FIXME: apparently accept doesn't fill
1941 * the sun_path sun_path member */
1942 if ( from.sa_un_addr.sun_path[0] == '\0' ) {
1943 AC_MEMCPY( from.sa_un_addr.sun_path,
1944 sl->sl_sa.sa_un_addr.sun_path,
1945 sizeof( from.sa_un_addr.sun_path ) );
1948 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1954 #ifdef LDAP_PF_LOCAL_SENDMSG
1955 peerbv.bv_val = peerbuf;
1956 peerbv.bv_len = sizeof( peerbuf );
1958 if( LUTIL_GETPEEREID( s, &uid, &gid, &peerbv ) == 0 ) {
1959 authid.bv_val = ch_malloc(
1960 STRLENOF( "gidNumber=4294967295+uidNumber=4294967295,"
1961 "cn=peercred,cn=external,cn=auth" ) + 1 );
1962 authid.bv_len = sprintf( authid.bv_val,
1963 "gidNumber=%d+uidNumber=%d,"
1964 "cn=peercred,cn=external,cn=auth",
1965 (int) gid, (int) uid );
1966 assert( authid.bv_len <=
1967 STRLENOF( "gidNumber=4294967295+uidNumber=4294967295,"
1968 "cn=peercred,cn=external,cn=auth" ) );
1973 #endif /* LDAP_PF_LOCAL */
1975 # ifdef LDAP_PF_INET6
1977 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1978 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
1979 peeraddr = inet_ntop( AF_INET,
1980 ((struct in_addr *)&from.sa_in6_addr.sin6_addr.s6_addr[12]),
1981 addr, sizeof(addr) );
1982 #else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1983 peeraddr = inet_ntoa( *((struct in_addr *)
1984 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1985 #endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1986 if ( !peeraddr ) peeraddr = SLAP_STRING_UNKNOWN;
1987 sprintf( peername, "IP=%s:%d", peeraddr,
1988 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1990 peeraddr = inet_ntop( AF_INET6,
1991 &from.sa_in6_addr.sin6_addr,
1992 addr, sizeof addr );
1993 if ( !peeraddr ) peeraddr = SLAP_STRING_UNKNOWN;
1994 sprintf( peername, "IP=[%s]:%d", peeraddr,
1995 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1998 # endif /* LDAP_PF_INET6 */
2001 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
2002 peeraddr = inet_ntop( AF_INET, &from.sa_in_addr.sin_addr,
2003 addr, sizeof(addr) );
2004 #else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
2005 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
2006 #endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
2007 if ( !peeraddr ) peeraddr = SLAP_STRING_UNKNOWN;
2008 sprintf( peername, "IP=%s:%d", peeraddr,
2009 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
2017 if ( ( from.sa_addr.sa_family == AF_INET )
2018 #ifdef LDAP_PF_INET6
2019 || ( from.sa_addr.sa_family == AF_INET6 )
2020 #endif /* LDAP_PF_INET6 */
2024 #ifdef SLAPD_RLOOKUPS
2025 if ( use_reverse_lookup ) {
2027 if (ldap_pvt_get_hname( (const struct sockaddr *)&from, len, hbuf,
2028 sizeof(hbuf), &herr ) == 0) {
2029 ldap_pvt_str2lower( hbuf );
2033 #endif /* SLAPD_RLOOKUPS */
2038 ldap_pvt_thread_mutex_lock( &sd_tcpd_mutex );
2039 rc = hosts_ctl("slapd",
2040 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
2042 SLAP_STRING_UNKNOWN );
2043 ldap_pvt_thread_mutex_unlock( &sd_tcpd_mutex );
2046 Statslog( LDAP_DEBUG_STATS,
2047 "fd=%ld DENIED from %s (%s)\n",
2049 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
2055 #endif /* HAVE_TCPD */
2059 if ( sl->sl_is_tls ) cflag |= CONN_IS_TLS;
2061 c = connection_init(sfd, sl,
2062 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
2063 peername, cflag, ssf,
2064 authid.bv_val ? &authid : NULL
2065 LDAP_PF_LOCAL_SENDMSG_ARG(&peerbv));
2067 if( authid.bv_val ) ch_free(authid.bv_val);
2070 Debug( LDAP_DEBUG_ANY,
2071 "daemon: connection_init(%ld, %s, %s) failed.\n",
2072 (long) sfd, peername, sl->sl_name.bv_val );
2077 Statslog( LDAP_DEBUG_STATS,
2078 "conn=%ld fd=%ld ACCEPT from %s (%s)\n",
2079 c->c_connid, (long) sfd, peername, sl->sl_name.bv_val,
2086 slap_listener_thread(
2091 Listener *sl = (Listener *)ptr;
2093 rc = slap_listener( sl );
2095 if( rc != LDAP_SUCCESS ) {
2096 Debug( LDAP_DEBUG_ANY,
2097 "slap_listener_thread(%s): failed err=%d",
2098 sl->sl_url.bv_val, rc, 0 );
2105 slap_listener_activate(
2110 Debug( LDAP_DEBUG_TRACE, "slap_listener_activate(%d): %s\n",
2111 sl->sl_sd, sl->sl_busy ? "busy" : "", 0 );
2115 rc = ldap_pvt_thread_pool_submit( &connection_pool,
2116 slap_listener_thread, (void *) sl );
2119 Debug( LDAP_DEBUG_ANY,
2120 "slap_listener_activate(%d): submit failed (%d)\n",
2131 time_t last_idle_check = 0;
2133 int tid = (ldap_pvt_thread_t *) ptr - listener_tid;
2135 #define SLAPD_IDLE_CHECK_LIMIT 4
2137 slapd_add( wake_sds[tid][0], 0, NULL, tid );
2141 /* Init stuff done only by thread 0 */
2143 last_idle_check = slap_get_time();
2145 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2146 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ) continue;
2148 #ifdef LDAP_CONNECTIONLESS
2149 /* Since this is connectionless, the data port is the
2150 * listening port. The listen() and accept() calls
2153 if ( slap_listeners[l]->sl_is_udp )
2155 #endif /* LDAP_CONNECTIONLESS */
2157 /* FIXME: TCP-only! */
2158 #ifdef LDAP_TCP_BUFFER
2160 int origsize, size, realsize, rc;
2162 char buf[ SLAP_TEXT_BUFLEN ];
2165 if ( slap_listeners[l]->sl_tcp_rmem > 0 ) {
2166 size = slap_listeners[l]->sl_tcp_rmem;
2167 } else if ( slapd_tcp_rmem > 0 ) {
2168 size = slapd_tcp_rmem;
2172 optlen = sizeof( origsize );
2173 rc = getsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2180 int err = sock_errno();
2181 Debug( LDAP_DEBUG_ANY,
2182 "slapd_daemon_task: getsockopt(SO_RCVBUF) failed errno=%d (%s)\n",
2183 err, sock_errstr(err), 0 );
2186 optlen = sizeof( size );
2187 rc = setsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2190 (const void *)&size,
2194 int err = sock_errno();
2195 Debug( LDAP_DEBUG_ANY,
2196 "slapd_daemon_task: setsockopt(SO_RCVBUF) failed errno=%d (%s)\n",
2197 err, sock_errstr(err), 0 );
2200 optlen = sizeof( realsize );
2201 rc = getsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2208 int err = sock_errno();
2209 Debug( LDAP_DEBUG_ANY,
2210 "slapd_daemon_task: getsockopt(SO_RCVBUF) failed errno=%d (%s)\n",
2211 err, sock_errstr(err), 0 );
2214 snprintf( buf, sizeof( buf ),
2215 "url=%s (#%d) RCVBUF original size=%d requested size=%d real size=%d",
2216 slap_listeners[l]->sl_url.bv_val, l, origsize, size, realsize );
2217 Debug( LDAP_DEBUG_ANY,
2218 "slapd_daemon_task: %s\n",
2223 if ( slap_listeners[l]->sl_tcp_wmem > 0 ) {
2224 size = slap_listeners[l]->sl_tcp_wmem;
2225 } else if ( slapd_tcp_wmem > 0 ) {
2226 size = slapd_tcp_wmem;
2230 optlen = sizeof( origsize );
2231 rc = getsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2238 int err = sock_errno();
2239 Debug( LDAP_DEBUG_ANY,
2240 "slapd_daemon_task: getsockopt(SO_SNDBUF) failed errno=%d (%s)\n",
2241 err, sock_errstr(err), 0 );
2244 optlen = sizeof( size );
2245 rc = setsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2248 (const void *)&size,
2252 int err = sock_errno();
2253 Debug( LDAP_DEBUG_ANY,
2254 "slapd_daemon_task: setsockopt(SO_SNDBUF) failed errno=%d (%s)",
2255 err, sock_errstr(err), 0 );
2258 optlen = sizeof( realsize );
2259 rc = getsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2266 int err = sock_errno();
2267 Debug( LDAP_DEBUG_ANY,
2268 "slapd_daemon_task: getsockopt(SO_SNDBUF) failed errno=%d (%s)\n",
2269 err, sock_errstr(err), 0 );
2272 snprintf( buf, sizeof( buf ),
2273 "url=%s (#%d) SNDBUF original size=%d requested size=%d real size=%d",
2274 slap_listeners[l]->sl_url.bv_val, l, origsize, size, realsize );
2275 Debug( LDAP_DEBUG_ANY,
2276 "slapd_daemon_task: %s\n",
2280 #endif /* LDAP_TCP_BUFFER */
2282 if ( listen( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ), SLAPD_LISTEN_BACKLOG ) == -1 ) {
2283 int err = sock_errno();
2285 #ifdef LDAP_PF_INET6
2286 /* If error is EADDRINUSE, we are trying to listen to INADDR_ANY and
2287 * we are already listening to in6addr_any, then we want to ignore
2288 * this and continue.
2290 if ( err == EADDRINUSE ) {
2292 struct sockaddr_in sa = slap_listeners[l]->sl_sa.sa_in_addr;
2293 struct sockaddr_in6 sa6;
2295 if ( sa.sin_family == AF_INET &&
2296 sa.sin_addr.s_addr == htonl(INADDR_ANY) ) {
2297 for ( i = 0 ; i < l; i++ ) {
2298 sa6 = slap_listeners[i]->sl_sa.sa_in6_addr;
2299 if ( sa6.sin6_family == AF_INET6 &&
2300 !memcmp( &sa6.sin6_addr, &in6addr_any,
2301 sizeof(struct in6_addr) ) )
2308 /* We are already listening to in6addr_any */
2309 Debug( LDAP_DEBUG_CONNS,
2310 "daemon: Attempt to listen to 0.0.0.0 failed, "
2311 "already listening on ::, assuming IPv4 included\n",
2313 slapd_close( slap_listeners[l]->sl_sd );
2314 slap_listeners[l]->sl_sd = AC_SOCKET_INVALID;
2319 #endif /* LDAP_PF_INET6 */
2320 Debug( LDAP_DEBUG_ANY,
2321 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
2322 slap_listeners[l]->sl_url.bv_val, err,
2327 /* make the listening socket non-blocking */
2328 if ( ber_pvt_socket_set_nonblock( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ), 1 ) < 0 ) {
2329 Debug( LDAP_DEBUG_ANY, "slapd_daemon_task: "
2330 "set nonblocking on a listening socket failed\n",
2336 slapd_add( slap_listeners[l]->sl_sd, 0, slap_listeners[l], -1 );
2339 #ifdef HAVE_NT_SERVICE_MANAGER
2340 if ( started_event != NULL ) {
2341 ldap_pvt_thread_cond_signal( &started_event );
2343 #endif /* HAVE_NT_SERVICE_MANAGER */
2347 /* initialization complete. Here comes the loop. */
2349 while ( !slapd_shutdown ) {
2354 #if SLAP_EVENTS_ARE_INDEXED
2355 ber_socket_t nrfds, nwfds;
2356 #endif /* SLAP_EVENTS_ARE_INDEXED */
2357 #define SLAPD_EBADF_LIMIT 16
2364 struct timeval *tvp;
2370 now = slap_get_time();
2372 if ( !tid && ( global_idletimeout > 0 || chk_writetime )) {
2374 /* Set the select timeout.
2375 * Don't just truncate, preserve the fractions of
2376 * seconds to prevent sleeping for zero time.
2378 if ( chk_writetime ) {
2379 tv.tv_sec = global_writetimeout;
2381 if ( difftime( chk_writetime, now ) < 0 )
2384 tv.tv_sec = global_idletimeout / SLAPD_IDLE_CHECK_LIMIT;
2385 tv.tv_usec = global_idletimeout - \
2386 ( tv.tv_sec * SLAPD_IDLE_CHECK_LIMIT );
2387 tv.tv_usec *= 1000000 / SLAPD_IDLE_CHECK_LIMIT;
2388 if ( difftime( last_idle_check +
2389 global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 )
2393 connections_timeout_idle( now );
2394 last_idle_check = now;
2402 if ( slapd_gentle_shutdown ) {
2403 ber_socket_t active;
2405 if ( !tid && slapd_gentle_shutdown == 1 ) {
2407 Debug( LDAP_DEBUG_ANY, "slapd gentle shutdown\n", 0, 0, 0 );
2408 close_listeners( 1 );
2409 frontendDB->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
2410 LDAP_STAILQ_FOREACH(be, &backendDB, be_next) {
2411 be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
2413 slapd_gentle_shutdown = 2;
2416 ldap_pvt_thread_mutex_lock( &slap_daemon[tid].sd_mutex );
2417 active = slap_daemon[tid].sd_nactives;
2418 ldap_pvt_thread_mutex_unlock( &slap_daemon[tid].sd_mutex );
2420 if ( active == 0 ) {
2422 for ( l=1; l<slapd_daemon_threads; l++ ) {
2423 ldap_pvt_thread_mutex_lock( &slap_daemon[l].sd_mutex );
2424 active += slap_daemon[l].sd_nactives;
2425 ldap_pvt_thread_mutex_unlock( &slap_daemon[l].sd_mutex );
2437 ldap_pvt_thread_mutex_lock( &slap_daemon[tid].sd_mutex );
2439 nwriters = slap_daemon[tid].sd_nwriters;
2442 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2443 Listener *lr = slap_listeners[l];
2445 if ( lr->sl_sd == AC_SOCKET_INVALID ) continue;
2446 if ( DAEMON_ID( lr->sl_sd ) != tid ) continue;
2448 if ( lr->sl_mute || lr->sl_busy )
2450 SLAP_SOCK_CLR_READ( tid, lr->sl_sd );
2452 SLAP_SOCK_SET_READ( tid, lr->sl_sd );
2456 SLAP_EVENT_INIT(tid);
2458 nfds = SLAP_EVENT_MAX(tid);
2460 if (( chk_writetime || global_idletimeout ) && slap_daemon[tid].sd_nactives ) at = 1;
2462 ldap_pvt_thread_mutex_unlock( &slap_daemon[tid].sd_mutex );
2465 #if defined(HAVE_YIELDING_SELECT) || defined(NO_THREADS)
2466 && ( tv.tv_sec || tv.tv_usec )
2467 #endif /* HAVE_YIELDING_SELECT || NO_THREADS */
2475 /* Only thread 0 handles runqueue */
2477 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2478 rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
2479 while ( rtask && cat.tv_sec && cat.tv_sec <= now ) {
2480 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
2481 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
2483 ldap_pvt_runqueue_runtask( &slapd_rq, rtask );
2484 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
2485 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2486 ldap_pvt_thread_pool_submit( &connection_pool,
2487 rtask->routine, (void *) rtask );
2488 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2490 rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
2492 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2494 if ( rtask && cat.tv_sec ) {
2495 /* NOTE: diff __should__ always be >= 0,
2496 * AFAI understand; however (ITS#4872),
2497 * time_t might be unsigned in some systems,
2498 * while difftime() returns a double */
2499 double diff = difftime( cat.tv_sec, now );
2503 if ( tvp == NULL || diff < tv.tv_sec ) {
2511 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2512 Listener *lr = slap_listeners[l];
2514 if ( lr->sl_sd == AC_SOCKET_INVALID ) {
2518 if ( lr->sl_mute ) {
2519 Debug( LDAP_DEBUG_CONNS,
2520 "daemon: " SLAP_EVENT_FNAME ": "
2521 "listen=%d muted\n",
2526 if ( lr->sl_busy ) {
2527 Debug( LDAP_DEBUG_CONNS,
2528 "daemon: " SLAP_EVENT_FNAME ": "
2534 Debug( LDAP_DEBUG_CONNS,
2535 "daemon: " SLAP_EVENT_FNAME ": "
2536 "listen=%d active_threads=%d tvp=%s\n",
2537 lr->sl_sd, at, tvp == NULL ? "NULL" : "zero" );
2540 SLAP_EVENT_WAIT( tid, tvp, &ns );
2542 case -1: { /* failure - try again */
2543 int err = sock_errno();
2545 if ( err != EINTR ) {
2548 /* Don't log unless we got it twice in a row */
2549 if ( !( ebadf & 1 ) ) {
2550 Debug( LDAP_DEBUG_ANY,
2556 sock_errstr( err ) );
2558 if ( ebadf >= SLAPD_EBADF_LIMIT ) {
2565 case 0: /* timeout - let threads run */
2567 #ifndef HAVE_YIELDING_SELECT
2568 Debug( LDAP_DEBUG_CONNS, "daemon: " SLAP_EVENT_FNAME
2569 "timeout - yielding\n",
2572 ldap_pvt_thread_yield();
2573 #endif /* ! HAVE_YIELDING_SELECT */
2576 default: /* something happened - deal with it */
2577 if ( slapd_shutdown ) continue;
2580 Debug( LDAP_DEBUG_CONNS,
2581 "daemon: activity on %d descriptor%s\n",
2582 ns, ns != 1 ? "s" : "", 0 );
2586 #if SLAP_EVENTS_ARE_INDEXED
2587 if ( SLAP_EVENT_IS_READ( wake_sds[tid][0] ) ) {
2589 SLAP_EVENT_CLR_READ( wake_sds[tid][0] );
2591 tcp_read( SLAP_FD2SOCK(wake_sds[tid][0]), c, sizeof(c) );
2592 Debug( LDAP_DEBUG_CONNS, "daemon: waked\n", 0, 0, 0 );
2596 /* The event slot equals the descriptor number - this is
2597 * true for Unix select and poll. We treat Windows select
2598 * like this too, even though it's a kludge.
2601 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2604 if ( ns <= 0 ) break;
2605 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ) continue;
2606 #ifdef LDAP_CONNECTIONLESS
2607 if ( slap_listeners[l]->sl_is_udp ) continue;
2608 #endif /* LDAP_CONNECTIONLESS */
2609 if ( !SLAP_EVENT_IS_READ( slap_listeners[l]->sl_sd ) ) continue;
2612 SLAP_EVENT_CLR_READ( slap_listeners[l]->sl_sd );
2613 SLAP_EVENT_CLR_WRITE( slap_listeners[l]->sl_sd );
2616 rc = slap_listener_activate( slap_listeners[l] );
2619 /* bypass the following tests if no descriptors left */
2621 #ifndef HAVE_YIELDING_SELECT
2622 ldap_pvt_thread_yield();
2623 #endif /* HAVE_YIELDING_SELECT */
2627 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
2630 for ( i = 0; i < nfds; i++ ) {
2633 r = SLAP_EVENT_IS_READ( i );
2634 /* writefds was not initialized if nwriters was zero */
2635 w = nwriters ? SLAP_EVENT_IS_WRITE( i ) : 0;
2637 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
2638 r ? "r" : "", w ? "w" : "" );
2648 if ( ns <= 0 ) break;
2650 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
2652 /* loop through the writers */
2653 for ( i = 0; nwfds > 0; i++ ) {
2655 if ( ! SLAP_EVENT_IS_WRITE( i ) ) continue;
2658 SLAP_EVENT_CLR_WRITE( wd );
2661 Debug( LDAP_DEBUG_CONNS,
2662 "daemon: write active on %d\n",
2666 * NOTE: it is possible that the connection was closed
2667 * and that the stream is now inactive.
2668 * connection_write() must validate the stream is still
2671 * ITS#4338: if the stream is invalid, there is no need to
2672 * close it here. It has already been closed in connection.c.
2674 if ( connection_write( wd ) < 0 ) {
2675 if ( SLAP_EVENT_IS_READ( wd ) ) {
2676 SLAP_EVENT_CLR_READ( (unsigned) wd );
2682 for ( i = 0; nrfds > 0; i++ ) {
2684 if ( ! SLAP_EVENT_IS_READ( i ) ) continue;
2686 SLAP_EVENT_CLR_READ( rd );
2689 Debug ( LDAP_DEBUG_CONNS,
2690 "daemon: read activity on %d\n", rd, 0, 0 );
2692 * NOTE: it is possible that the connection was closed
2693 * and that the stream is now inactive.
2694 * connection_read() must valid the stream is still
2698 connection_read_activate( rd );
2700 #else /* !SLAP_EVENTS_ARE_INDEXED */
2702 /* The events are returned in an arbitrary list. This is true
2703 * for /dev/poll, epoll and kqueue. In order to prioritize things
2704 * so that we can handle wake_sds first, listeners second, and then
2705 * all other connections last (as we do for select), we would need
2706 * to use multiple event handles and cascade them.
2708 * That seems like a bit of hassle. So the wake_sds check has been
2709 * skipped. For epoll and kqueue we can associate arbitrary data with
2710 * an event, so we could use pointers to the listener structure
2711 * instead of just the file descriptor. For /dev/poll we have to
2712 * search the listeners array for a matching descriptor.
2714 * We now handle wake events when we see them; they are not given
2718 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
2720 for ( i = 0; i < ns; i++ ) {
2723 /* Don't log listener events */
2724 if ( SLAP_EVENT_IS_LISTENER( tid, i )
2725 #ifdef LDAP_CONNECTIONLESS
2726 && !( (SLAP_EVENT_LISTENER( tid, i ))->sl_is_udp )
2727 #endif /* LDAP_CONNECTIONLESS */
2733 fd = SLAP_EVENT_FD( tid, i );
2734 /* Don't log internal wake events */
2735 if ( fd == wake_sds[tid][0] ) continue;
2737 r = SLAP_EVENT_IS_READ( i );
2738 w = SLAP_EVENT_IS_WRITE( i );
2740 Debug( LDAP_DEBUG_CONNS, " %d%s%s", fd,
2741 r ? "r" : "", w ? "w" : "" );
2744 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
2745 #endif /* LDAP_DEBUG */
2747 for ( i = 0; i < ns; i++ ) {
2748 int rc = 1, fd, w = 0, r = 0;
2750 if ( SLAP_EVENT_IS_LISTENER( tid, i ) ) {
2751 rc = slap_listener_activate( SLAP_EVENT_LISTENER( tid, i ) );
2754 /* If we found a regular listener, rc is now zero, and we
2755 * can skip the data portion. But if it was a UDP listener
2756 * then rc is still 1, and we want to handle the data.
2759 fd = SLAP_EVENT_FD( tid, i );
2761 /* Handle wake events */
2762 if ( fd == wake_sds[tid][0] ) {
2765 tcp_read( SLAP_FD2SOCK(wake_sds[tid][0]), c, sizeof(c) );
2769 if ( SLAP_EVENT_IS_WRITE( i ) ) {
2770 Debug( LDAP_DEBUG_CONNS,
2771 "daemon: write active on %d\n",
2774 SLAP_EVENT_CLR_WRITE( i );
2778 * NOTE: it is possible that the connection was closed
2779 * and that the stream is now inactive.
2780 * connection_write() must valid the stream is still
2783 if ( connection_write( fd ) < 0 ) {
2787 /* If event is a read */
2788 if ( SLAP_EVENT_IS_READ( i )) {
2790 Debug( LDAP_DEBUG_CONNS,
2791 "daemon: read active on %d\n",
2794 SLAP_EVENT_CLR_READ( i );
2795 connection_read_activate( fd );
2798 /* Don't keep reporting the hangup
2800 if ( SLAP_SOCK_IS_ACTIVE( tid, fd )) {
2801 SLAP_EPOLL_SOCK_SET( tid, fd, EPOLLET );
2807 #endif /* SLAP_EVENTS_ARE_INDEXED */
2809 #ifndef HAVE_YIELDING_SELECT
2810 ldap_pvt_thread_yield();
2811 #endif /* ! HAVE_YIELDING_SELECT */
2814 /* Only thread 0 handles shutdown */
2818 if ( slapd_shutdown == 1 ) {
2819 Debug( LDAP_DEBUG_ANY,
2820 "daemon: shutdown requested and initiated.\n",
2823 } else if ( slapd_shutdown == 2 ) {
2824 #ifdef HAVE_NT_SERVICE_MANAGER
2825 Debug( LDAP_DEBUG_ANY,
2826 "daemon: shutdown initiated by Service Manager.\n",
2828 #else /* !HAVE_NT_SERVICE_MANAGER */
2829 Debug( LDAP_DEBUG_ANY,
2830 "daemon: abnormal condition, shutdown initiated.\n",
2832 #endif /* !HAVE_NT_SERVICE_MANAGER */
2834 Debug( LDAP_DEBUG_ANY,
2835 "daemon: no active streams, shutdown initiated.\n",
2839 close_listeners( 0 );
2841 if ( !slapd_gentle_shutdown ) {
2842 slapd_abrupt_shutdown = 1;
2843 connections_shutdown();
2846 if ( LogTest( LDAP_DEBUG_ANY )) {
2847 int t = ldap_pvt_thread_pool_backload( &connection_pool );
2848 Debug( LDAP_DEBUG_ANY,
2849 "slapd shutdown: waiting for %d operations/tasks to finish\n",
2852 ldap_pvt_thread_pool_destroy( &connection_pool, 1 );
2858 #ifdef LDAP_CONNECTIONLESS
2860 connectionless_init( void )
2864 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2865 Listener *lr = slap_listeners[l];
2868 if ( !lr->sl_is_udp ) {
2872 c = connection_init( lr->sl_sd, lr, "", "",
2873 CONN_IS_UDP, (slap_ssf_t) 0, NULL
2874 LDAP_PF_LOCAL_SENDMSG_ARG(NULL));
2877 Debug( LDAP_DEBUG_TRACE,
2878 "connectionless_init: failed on %s (%d)\n",
2879 lr->sl_url.bv_val, lr->sl_sd, 0 );
2887 #endif /* LDAP_CONNECTIONLESS */
2890 slapd_daemon( void )
2894 #ifdef LDAP_CONNECTIONLESS
2895 connectionless_init();
2896 #endif /* LDAP_CONNECTIONLESS */
2898 listener_tid = ch_malloc(slapd_daemon_threads * sizeof(ldap_pvt_thread_t));
2900 /* daemon_init only inits element 0 */
2901 for ( i=1; i<slapd_daemon_threads; i++ )
2903 ldap_pvt_thread_mutex_init( &slap_daemon[i].sd_mutex );
2905 if( (rc = lutil_pair( wake_sds[i] )) < 0 ) {
2906 Debug( LDAP_DEBUG_ANY,
2907 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
2910 ber_pvt_socket_set_nonblock( wake_sds[i][1], 1 );
2915 for ( i=0; i<slapd_daemon_threads; i++ )
2917 /* listener as a separate THREAD */
2918 rc = ldap_pvt_thread_create( &listener_tid[i],
2919 0, slapd_daemon_task, &listener_tid[i] );
2922 Debug( LDAP_DEBUG_ANY,
2923 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
2928 /* wait for the listener threads to complete */
2929 for ( i=0; i<slapd_daemon_threads; i++ )
2930 ldap_pvt_thread_join( listener_tid[i], (void *)NULL );
2932 destroy_listeners();
2933 ch_free( listener_tid );
2934 listener_tid = NULL;
2942 #if defined( HAVE_WINSOCK2 )
2943 WORD wVersionRequested;
2947 wVersionRequested = MAKEWORD( 2, 0 );
2949 err = WSAStartup( wVersionRequested, &wsaData );
2951 /* Tell the user that we couldn't find a usable */
2956 /* Confirm that the WinSock DLL supports 2.0.*/
2957 /* Note that if the DLL supports versions greater */
2958 /* than 2.0 in addition to 2.0, it will still return */
2959 /* 2.0 in wVersion since that is the version we */
2962 if ( LOBYTE( wsaData.wVersion ) != 2 ||
2963 HIBYTE( wsaData.wVersion ) != 0 )
2965 /* Tell the user that we couldn't find a usable */
2971 /* The WinSock DLL is acceptable. Proceed. */
2972 #elif defined( HAVE_WINSOCK )
2974 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) return -1;
2975 #endif /* ! HAVE_WINSOCK2 && ! HAVE_WINSOCK */
2983 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
2985 #endif /* HAVE_WINSOCK2 || HAVE_WINSOCK */
2991 slap_sig_shutdown( int sig )
2993 int save_errno = errno;
2997 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
3001 * If the NT Service Manager is controlling the server, we don't
3002 * want SIGBREAK to kill the server. For some strange reason,
3003 * SIGBREAK is generated when a user logs out.
3006 #if defined(HAVE_NT_SERVICE_MANAGER) && defined(SIGBREAK)
3007 if (is_NT_Service && sig == SIGBREAK) {
3010 #endif /* HAVE_NT_SERVICE_MANAGER && SIGBREAK */
3012 if (sig == SIGHUP && global_gentlehup && slapd_gentle_shutdown == 0) {
3013 slapd_gentle_shutdown = 1;
3020 for (i=0; i<slapd_daemon_threads; i++) {
3024 /* reinstall self */
3025 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
3031 slap_sig_wake( int sig )
3033 int save_errno = errno;
3037 /* reinstall self */
3038 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
3045 slapd_add_internal( ber_socket_t s, int isactive )
3047 slapd_add( s, isactive, NULL, -1 );
3051 slapd_get_listeners( void )
3053 /* Could return array with no listeners if !listening, but current
3054 * callers mostly look at the URLs. E.g. syncrepl uses this to
3055 * identify the server, which means it wants the startup arguments.
3057 return slap_listeners;
3060 /* Reject all incoming requests */
3062 slap_suspend_listeners( void )
3065 for (i=0; slap_listeners[i]; i++) {
3066 slap_listeners[i]->sl_mute = 1;
3067 listen( slap_listeners[i]->sl_sd, 0 );
3071 /* Resume after a suspend */
3073 slap_resume_listeners( void )
3076 for (i=0; slap_listeners[i]; i++) {
3077 slap_listeners[i]->sl_mute = 0;
3078 listen( slap_listeners[i]->sl_sd, SLAPD_LISTEN_BACKLOG );
3083 slap_wake_listener()