2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2014 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 #ifndef SLAPD_MAX_DAEMON_THREADS
77 #define SLAPD_MAX_DAEMON_THREADS 16
79 int slapd_daemon_threads = 1;
80 int slapd_daemon_mask;
82 #ifdef LDAP_TCP_BUFFER
85 #endif /* LDAP_TCP_BUFFER */
87 Listener **slap_listeners = NULL;
88 static volatile sig_atomic_t listening = 1; /* 0 when slap_listeners closed */
89 static ldap_pvt_thread_t *listener_tid;
91 #ifndef SLAPD_LISTEN_BACKLOG
92 #define SLAPD_LISTEN_BACKLOG 1024
93 #endif /* ! SLAPD_LISTEN_BACKLOG */
95 #define DAEMON_ID(fd) (fd & slapd_daemon_mask)
97 static ber_socket_t wake_sds[SLAPD_MAX_DAEMON_THREADS][2];
100 static time_t chk_writetime;
102 static volatile int waking;
104 #define WAKE_LISTENER(l,w) do { \
105 if ((w) && ++waking < 5) { \
106 tcp_write( SLAP_FD2SOCK(wake_sds[l][1]), "0", 1 ); \
109 #else /* ! NO_THREADS */
110 #define WAKE_LISTENER(l,w) do { \
112 tcp_write( SLAP_FD2SOCK(wake_sds[l][1]), "0", 1 ); \
115 #endif /* ! NO_THREADS */
117 volatile sig_atomic_t slapd_shutdown = 0;
118 volatile sig_atomic_t slapd_gentle_shutdown = 0;
119 volatile sig_atomic_t slapd_abrupt_shutdown = 0;
122 ldap_pvt_thread_mutex_t slapd_ws_mutex;
123 SOCKET *slapd_ws_sockets;
127 #define SD_LISTENER 8
131 static ldap_pvt_thread_mutex_t sd_tcpd_mutex;
132 #endif /* TCP Wrappers */
134 typedef struct slap_daemon_st {
135 ldap_pvt_thread_mutex_t sd_mutex;
137 ber_socket_t sd_nactives;
141 #if defined(HAVE_EPOLL)
142 struct epoll_event *sd_epolls;
145 #elif defined(SLAP_X_DEVPOLL) && defined(HAVE_DEVPOLL)
147 struct pollfd *sd_pollfd;
151 #else /* ! epoll && ! /dev/poll */
155 #else /* ! HAVE_WINSOCK */
159 #endif /* ! HAVE_WINSOCK */
160 #endif /* ! epoll && ! /dev/poll */
163 static slap_daemon_st slap_daemon[SLAPD_MAX_DAEMON_THREADS];
166 * NOTE: naming convention for macros:
168 * - SLAP_SOCK_* and SLAP_EVENT_* for public interface that deals
169 * with file descriptors and events respectively
171 * - SLAP_<type>_* for private interface; type by now is one of
172 * EPOLL, DEVPOLL, SELECT
174 * private interface should not be used in the code.
176 #if defined(HAVE_EPOLL)
177 /***************************************
178 * Use epoll infrastructure - epoll(4) *
179 ***************************************/
180 # define SLAP_EVENT_FNAME "epoll"
181 # define SLAP_EVENTS_ARE_INDEXED 0
182 # define SLAP_EPOLL_SOCK_IX(t,s) (slap_daemon[t].sd_index[(s)])
183 # define SLAP_EPOLL_SOCK_EP(t,s) (slap_daemon[t].sd_epolls[SLAP_EPOLL_SOCK_IX(t,s)])
184 # define SLAP_EPOLL_SOCK_EV(t,s) (SLAP_EPOLL_SOCK_EP(t,s).events)
185 # define SLAP_SOCK_IS_ACTIVE(t,s) (SLAP_EPOLL_SOCK_IX(t,s) != -1)
186 # define SLAP_SOCK_NOT_ACTIVE(t,s) (SLAP_EPOLL_SOCK_IX(t,s) == -1)
187 # define SLAP_EPOLL_SOCK_IS_SET(t,s, mode) (SLAP_EPOLL_SOCK_EV(t,s) & (mode))
189 # define SLAP_SOCK_IS_READ(t,s) SLAP_EPOLL_SOCK_IS_SET(t,(s), EPOLLIN)
190 # define SLAP_SOCK_IS_WRITE(t,s) SLAP_EPOLL_SOCK_IS_SET(t,(s), EPOLLOUT)
192 # define SLAP_EPOLL_SOCK_SET(t,s, mode) do { \
193 if ( (SLAP_EPOLL_SOCK_EV(t,s) & (mode)) != (mode) ) { \
194 SLAP_EPOLL_SOCK_EV(t,s) |= (mode); \
195 epoll_ctl( slap_daemon[t].sd_epfd, EPOLL_CTL_MOD, (s), \
196 &SLAP_EPOLL_SOCK_EP(t,s) ); \
200 # define SLAP_EPOLL_SOCK_CLR(t,s, mode) do { \
201 if ( (SLAP_EPOLL_SOCK_EV(t,s) & (mode)) ) { \
202 SLAP_EPOLL_SOCK_EV(t,s) &= ~(mode); \
203 epoll_ctl( slap_daemon[t].sd_epfd, EPOLL_CTL_MOD, s, \
204 &SLAP_EPOLL_SOCK_EP(t,s) ); \
208 # define SLAP_SOCK_SET_READ(t,s) SLAP_EPOLL_SOCK_SET(t,s, EPOLLIN)
209 # define SLAP_SOCK_SET_WRITE(t,s) SLAP_EPOLL_SOCK_SET(t,s, EPOLLOUT)
211 # define SLAP_SOCK_CLR_READ(t,s) SLAP_EPOLL_SOCK_CLR(t,(s), EPOLLIN)
212 # define SLAP_SOCK_CLR_WRITE(t,s) SLAP_EPOLL_SOCK_CLR(t,(s), EPOLLOUT)
214 # define SLAP_SOCK_SET_SUSPEND(t,s) \
215 ( slap_daemon[t].sd_suspend[SLAP_EPOLL_SOCK_IX(t,s)] = 1 )
216 # define SLAP_SOCK_CLR_SUSPEND(t,s) \
217 ( slap_daemon[t].sd_suspend[SLAP_EPOLL_SOCK_IX(t,s)] = 0 )
218 # define SLAP_SOCK_IS_SUSPEND(t,s) \
219 ( slap_daemon[t].sd_suspend[SLAP_EPOLL_SOCK_IX(t,s)] == 1 )
221 # define SLAP_EPOLL_EVENT_CLR(i, mode) (revents[(i)].events &= ~(mode))
223 # define SLAP_EVENT_MAX(t) slap_daemon[t].sd_nfds
225 /* If a Listener address is provided, store that as the epoll data.
226 * Otherwise, store the address of this socket's slot in the
227 * index array. If we can't do this add, the system is out of
228 * resources and we need to shutdown.
230 # define SLAP_SOCK_ADD(t, s, l) do { \
232 SLAP_EPOLL_SOCK_IX(t,(s)) = slap_daemon[t].sd_nfds; \
233 SLAP_EPOLL_SOCK_EP(t,(s)).data.ptr = (l) ? (l) : (void *)(&SLAP_EPOLL_SOCK_IX(t,s)); \
234 SLAP_EPOLL_SOCK_EV(t,(s)) = EPOLLIN; \
235 rc = epoll_ctl(slap_daemon[t].sd_epfd, EPOLL_CTL_ADD, \
236 (s), &SLAP_EPOLL_SOCK_EP(t,(s))); \
238 slap_daemon[t].sd_nfds++; \
240 Debug( LDAP_DEBUG_ANY, \
241 "daemon: epoll_ctl(ADD,fd=%d) failed, errno=%d, shutting down\n", \
243 slapd_shutdown = 2; \
247 # define SLAP_EPOLL_EV_LISTENER(t,ptr) \
248 (((int *)(ptr) >= slap_daemon[t].sd_index && \
249 (int *)(ptr) <= &slap_daemon[t].sd_index[dtblsize]) ? 0 : 1 )
251 # define SLAP_EPOLL_EV_PTRFD(t,ptr) (SLAP_EPOLL_EV_LISTENER(t,ptr) ? \
252 ((Listener *)ptr)->sl_sd : \
253 (ber_socket_t) ((int *)(ptr) - slap_daemon[t].sd_index))
255 # define SLAP_SOCK_DEL(t,s) do { \
256 int fd, rc, index = SLAP_EPOLL_SOCK_IX(t,(s)); \
257 if ( index < 0 ) break; \
258 rc = epoll_ctl(slap_daemon[t].sd_epfd, EPOLL_CTL_DEL, \
259 (s), &SLAP_EPOLL_SOCK_EP(t,(s))); \
260 slap_daemon[t].sd_epolls[index] = \
261 slap_daemon[t].sd_epolls[slap_daemon[t].sd_nfds-1]; \
262 fd = SLAP_EPOLL_EV_PTRFD(t,slap_daemon[t].sd_epolls[index].data.ptr); \
263 slap_daemon[t].sd_index[fd] = index; \
264 slap_daemon[t].sd_index[(s)] = -1; \
265 slap_daemon[t].sd_nfds--; \
268 # define SLAP_EVENT_CLR_READ(i) SLAP_EPOLL_EVENT_CLR((i), EPOLLIN)
269 # define SLAP_EVENT_CLR_WRITE(i) SLAP_EPOLL_EVENT_CLR((i), EPOLLOUT)
271 # define SLAP_EPOLL_EVENT_CHK(i, mode) (revents[(i)].events & mode)
273 # define SLAP_EVENT_IS_READ(i) SLAP_EPOLL_EVENT_CHK((i), EPOLLIN)
274 # define SLAP_EVENT_IS_WRITE(i) SLAP_EPOLL_EVENT_CHK((i), EPOLLOUT)
275 # define SLAP_EVENT_IS_LISTENER(t,i) SLAP_EPOLL_EV_LISTENER(t,revents[(i)].data.ptr)
276 # define SLAP_EVENT_LISTENER(t,i) ((Listener *)(revents[(i)].data.ptr))
278 # define SLAP_EVENT_FD(t,i) SLAP_EPOLL_EV_PTRFD(t,revents[(i)].data.ptr)
280 # define SLAP_SOCK_INIT(t) do { \
282 slap_daemon[t].sd_epolls = ch_calloc(1, \
283 ( sizeof(struct epoll_event) * 2 \
284 + sizeof(int) ) * dtblsize * 2); \
285 slap_daemon[t].sd_index = (int *)&slap_daemon[t].sd_epolls[ 2 * dtblsize ]; \
286 slap_daemon[t].sd_epfd = epoll_create( dtblsize / slapd_daemon_threads ); \
287 for ( j = 0; j < dtblsize; j++ ) slap_daemon[t].sd_index[j] = -1; \
290 # define SLAP_SOCK_DESTROY(t) do { \
291 if ( slap_daemon[t].sd_epolls != NULL ) { \
292 ch_free( slap_daemon[t].sd_epolls ); \
293 slap_daemon[t].sd_epolls = NULL; \
294 slap_daemon[t].sd_index = NULL; \
295 close( slap_daemon[t].sd_epfd ); \
299 # define SLAP_EVENT_DECL struct epoll_event *revents
301 # define SLAP_EVENT_INIT(t) do { \
302 revents = slap_daemon[t].sd_epolls + dtblsize; \
305 # define SLAP_EVENT_WAIT(t, tvp, nsp) do { \
306 *(nsp) = epoll_wait( slap_daemon[t].sd_epfd, revents, \
307 dtblsize, (tvp) ? (tvp)->tv_sec * 1000 : -1 ); \
310 #elif defined(SLAP_X_DEVPOLL) && defined(HAVE_DEVPOLL)
312 /*************************************************************
313 * Use Solaris' (>= 2.7) /dev/poll infrastructure - poll(7d) *
314 *************************************************************/
315 # define SLAP_EVENT_FNAME "/dev/poll"
316 # define SLAP_EVENTS_ARE_INDEXED 0
318 * - sd_index is used much like with epoll()
319 * - sd_l is maintained as an array containing the address
320 * of the listener; the index is the fd itself
321 * - sd_pollfd is used to keep track of what data has been
322 * registered in /dev/poll
324 # define SLAP_DEVPOLL_SOCK_IX(t,s) (slap_daemon[t].sd_index[(s)])
325 # define SLAP_DEVPOLL_SOCK_LX(t,s) (slap_daemon[t].sd_l[(s)])
326 # define SLAP_DEVPOLL_SOCK_EP(t,s) (slap_daemon[t].sd_pollfd[SLAP_DEVPOLL_SOCK_IX(t,(s))])
327 # define SLAP_DEVPOLL_SOCK_FD(t,s) (SLAP_DEVPOLL_SOCK_EP(t,(s)).fd)
328 # define SLAP_DEVPOLL_SOCK_EV(t,s) (SLAP_DEVPOLL_SOCK_EP(t,(s)).events)
329 # define SLAP_SOCK_IS_ACTIVE(t,s) (SLAP_DEVPOLL_SOCK_IX(t,(s)) != -1)
330 # define SLAP_SOCK_NOT_ACTIVE(t,s) (SLAP_DEVPOLL_SOCK_IX(t,(s)) == -1)
331 # define SLAP_SOCK_IS_SET(t,s, mode) (SLAP_DEVPOLL_SOCK_EV(t,(s)) & (mode))
333 # define SLAP_SOCK_IS_READ(t,s) SLAP_SOCK_IS_SET(t,(s), POLLIN)
334 # define SLAP_SOCK_IS_WRITE(t,s) SLAP_SOCK_IS_SET(t,(s), POLLOUT)
336 /* as far as I understand, any time we need to communicate with the kernel
337 * about the number and/or properties of a file descriptor we need it to
338 * wait for, we have to rewrite the whole set */
339 # define SLAP_DEVPOLL_WRITE_POLLFD(t,s, pfd, n, what, shdn) do { \
341 size_t size = (n) * sizeof( struct pollfd ); \
342 /* FIXME: use pwrite? */ \
343 rc = write( slap_daemon[t].sd_dpfd, (pfd), size ); \
344 if ( rc != size ) { \
345 Debug( LDAP_DEBUG_ANY, "daemon: " SLAP_EVENT_FNAME ": " \
346 "%s fd=%d failed errno=%d\n", \
347 (what), (s), errno ); \
349 slapd_shutdown = 2; \
354 # define SLAP_DEVPOLL_SOCK_SET(t,s, mode) do { \
355 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_SET_%s(%d)=%d\n", \
356 (mode) == POLLIN ? "READ" : "WRITE", (s), \
357 ( (SLAP_DEVPOLL_SOCK_EV(t,(s)) & (mode)) != (mode) ) ); \
358 if ( (SLAP_DEVPOLL_SOCK_EV(t,(s)) & (mode)) != (mode) ) { \
360 SLAP_DEVPOLL_SOCK_EV(t,(s)) |= (mode); \
361 pfd.fd = SLAP_DEVPOLL_SOCK_FD(t,(s)); \
362 pfd.events = /* (mode) */ SLAP_DEVPOLL_SOCK_EV(t,(s)); \
363 SLAP_DEVPOLL_WRITE_POLLFD(t,(s), &pfd, 1, "SET", 0); \
367 # define SLAP_DEVPOLL_SOCK_CLR(t,s, mode) do { \
368 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_CLR_%s(%d)=%d\n", \
369 (mode) == POLLIN ? "READ" : "WRITE", (s), \
370 ( (SLAP_DEVPOLL_SOCK_EV(t,(s)) & (mode)) == (mode) ) ); \
371 if ((SLAP_DEVPOLL_SOCK_EV(t,(s)) & (mode)) == (mode) ) { \
372 struct pollfd pfd[2]; \
373 SLAP_DEVPOLL_SOCK_EV(t,(s)) &= ~(mode); \
374 pfd[0].fd = SLAP_DEVPOLL_SOCK_FD(t,(s)); \
375 pfd[0].events = POLLREMOVE; \
376 pfd[1] = SLAP_DEVPOLL_SOCK_EP(t,(s)); \
377 SLAP_DEVPOLL_WRITE_POLLFD(t,(s), &pfd[0], 2, "CLR", 0); \
381 # define SLAP_SOCK_SET_READ(t,s) SLAP_DEVPOLL_SOCK_SET(t,s, POLLIN)
382 # define SLAP_SOCK_SET_WRITE(t,s) SLAP_DEVPOLL_SOCK_SET(t,s, POLLOUT)
384 # define SLAP_SOCK_CLR_READ(t,s) SLAP_DEVPOLL_SOCK_CLR(t,(s), POLLIN)
385 # define SLAP_SOCK_CLR_WRITE(t,s) SLAP_DEVPOLL_SOCK_CLR(t,(s), POLLOUT)
387 # define SLAP_SOCK_SET_SUSPEND(t,s) \
388 ( slap_daemon[t].sd_suspend[SLAP_DEVPOLL_SOCK_IX(t,(s))] = 1 )
389 # define SLAP_SOCK_CLR_SUSPEND(t,s) \
390 ( slap_daemon[t].sd_suspend[SLAP_DEVPOLL_SOCK_IX(t,(s))] = 0 )
391 # define SLAP_SOCK_IS_SUSPEND(t,s) \
392 ( slap_daemon[t].sd_suspend[SLAP_DEVPOLL_SOCK_IX(t,(s))] == 1 )
394 # define SLAP_DEVPOLL_EVENT_CLR(i, mode) (revents[(i)].events &= ~(mode))
396 # define SLAP_EVENT_MAX(t) slap_daemon[t].sd_nfds
398 /* If a Listener address is provided, store that in the sd_l array.
399 * If we can't do this add, the system is out of resources and we
402 # define SLAP_SOCK_ADD(t, s, l) do { \
403 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_ADD(%d, %p)\n", (s), (l), 0 ); \
404 SLAP_DEVPOLL_SOCK_IX(t,(s)) = slap_daemon[t].sd_nfds; \
405 SLAP_DEVPOLL_SOCK_LX(t,(s)) = (l); \
406 SLAP_DEVPOLL_SOCK_FD(t,(s)) = (s); \
407 SLAP_DEVPOLL_SOCK_EV(t,(s)) = POLLIN; \
408 SLAP_DEVPOLL_WRITE_POLLFD(t,(s), &SLAP_DEVPOLL_SOCK_EP((s)), 1, "ADD", 1); \
409 slap_daemon[t].sd_nfds++; \
412 # define SLAP_DEVPOLL_EV_LISTENER(ptr) ((ptr) != NULL)
414 # define SLAP_SOCK_DEL(t,s) do { \
415 int fd, index = SLAP_DEVPOLL_SOCK_IX(t,(s)); \
416 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_DEL(%d)\n", (s), 0, 0 ); \
417 if ( index < 0 ) break; \
418 if ( index < slap_daemon[t].sd_nfds - 1 ) { \
419 struct pollfd pfd = slap_daemon[t].sd_pollfd[index]; \
420 fd = slap_daemon[t].sd_pollfd[slap_daemon[t].sd_nfds - 1].fd; \
421 slap_daemon[t].sd_pollfd[index] = slap_daemon[t].sd_pollfd[slap_daemon[t].sd_nfds - 1]; \
422 slap_daemon[t].sd_pollfd[slap_daemon[t].sd_nfds - 1] = pfd; \
423 slap_daemon[t].sd_index[fd] = index; \
425 slap_daemon[t].sd_index[(s)] = -1; \
426 slap_daemon[t].sd_pollfd[slap_daemon[t].sd_nfds - 1].events = POLLREMOVE; \
427 SLAP_DEVPOLL_WRITE_POLLFD(t,(s), &slap_daemon[t].sd_pollfd[slap_daemon[t].sd_nfds - 1], 1, "DEL", 0); \
428 slap_daemon[t].sd_pollfd[slap_daemon[t].sd_nfds - 1].events = 0; \
429 slap_daemon[t].sd_nfds--; \
432 # define SLAP_EVENT_CLR_READ(i) SLAP_DEVPOLL_EVENT_CLR((i), POLLIN)
433 # define SLAP_EVENT_CLR_WRITE(i) SLAP_DEVPOLL_EVENT_CLR((i), POLLOUT)
435 # define SLAP_DEVPOLL_EVENT_CHK(i, mode) (revents[(i)].events & (mode))
437 # define SLAP_EVENT_FD(t,i) (revents[(i)].fd)
439 # define SLAP_EVENT_IS_READ(i) SLAP_DEVPOLL_EVENT_CHK((i), POLLIN)
440 # define SLAP_EVENT_IS_WRITE(i) SLAP_DEVPOLL_EVENT_CHK((i), POLLOUT)
441 # define SLAP_EVENT_IS_LISTENER(t,i) SLAP_DEVPOLL_EV_LISTENER(SLAP_DEVPOLL_SOCK_LX(SLAP_EVENT_FD(t,(i))))
442 # define SLAP_EVENT_LISTENER(t,i) SLAP_DEVPOLL_SOCK_LX(SLAP_EVENT_FD(t,(i)))
444 # define SLAP_SOCK_INIT(t) do { \
445 slap_daemon[t].sd_pollfd = ch_calloc( 1, \
446 ( sizeof(struct pollfd) * 2 \
448 + sizeof( Listener * ) ) * dtblsize ); \
449 slap_daemon[t].sd_index = (int *)&slap_daemon[t].sd_pollfd[ 2 * dtblsize ]; \
450 slap_daemon[t].sd_l = (Listener **)&slap_daemon[t].sd_index[ dtblsize ]; \
451 slap_daemon[t].sd_dpfd = open( SLAP_EVENT_FNAME, O_RDWR ); \
452 if ( slap_daemon[t].sd_dpfd == -1 ) { \
453 Debug( LDAP_DEBUG_ANY, "daemon: " SLAP_EVENT_FNAME ": " \
454 "open(\"" SLAP_EVENT_FNAME "\") failed errno=%d\n", \
459 for ( i = 0; i < dtblsize; i++ ) { \
460 slap_daemon[t].sd_pollfd[i].fd = -1; \
461 slap_daemon[t].sd_index[i] = -1; \
465 # define SLAP_SOCK_DESTROY(t) do { \
466 if ( slap_daemon[t].sd_pollfd != NULL ) { \
467 ch_free( slap_daemon[t].sd_pollfd ); \
468 slap_daemon[t].sd_pollfd = NULL; \
469 slap_daemon[t].sd_index = NULL; \
470 slap_daemon[t].sd_l = NULL; \
471 close( slap_daemon[t].sd_dpfd ); \
475 # define SLAP_EVENT_DECL struct pollfd *revents
477 # define SLAP_EVENT_INIT(t) do { \
478 revents = &slap_daemon[t].sd_pollfd[ dtblsize ]; \
481 # define SLAP_EVENT_WAIT(t, tvp, nsp) do { \
482 struct dvpoll sd_dvpoll; \
483 sd_dvpoll.dp_timeout = (tvp) ? (tvp)->tv_sec * 1000 : -1; \
484 sd_dvpoll.dp_nfds = dtblsize; \
485 sd_dvpoll.dp_fds = revents; \
486 *(nsp) = ioctl( slap_daemon[t].sd_dpfd, DP_POLL, &sd_dvpoll ); \
489 #else /* ! epoll && ! /dev/poll */
491 # define SLAP_EVENT_FNAME "WSselect"
492 /* Winsock provides a "select" function but its fd_sets are
493 * actually arrays of sockets. Since these sockets are handles
494 * and not a contiguous range of small integers, we manage our
495 * own "fd" table of socket handles and use their indices as
498 * All of our listener/connection structures use fds; the actual
499 * I/O functions use sockets. The SLAP_FD2SOCK macro in proto-slap.h
500 * handles the mapping.
502 * Despite the mapping overhead, this is about 45% more efficient
503 * than just using Winsock's select and FD_ISSET directly.
505 * Unfortunately Winsock's select implementation doesn't scale well
506 * as the number of connections increases. This probably needs to be
507 * rewritten to use the Winsock overlapped/asynchronous I/O functions.
509 # define SLAP_EVENTS_ARE_INDEXED 1
510 # define SLAP_EVENT_DECL fd_set readfds, writefds; char *rflags
511 # define SLAP_EVENT_INIT(t) do { \
513 FD_ZERO( &readfds ); \
514 FD_ZERO( &writefds ); \
515 rflags = slap_daemon[t].sd_rflags; \
516 memset( rflags, 0, slap_daemon[t].sd_nfds ); \
517 for ( i=0; i<slap_daemon[t].sd_nfds; i++ ) { \
518 if ( slap_daemon[t].sd_flags[i] & SD_READ ) \
519 FD_SET( slapd_ws_sockets[i], &readfds );\
520 if ( slap_daemon[t].sd_flags[i] & SD_WRITE ) \
521 FD_SET( slapd_ws_sockets[i], &writefds ); \
524 # define SLAP_EVENT_MAX(t) slap_daemon[t].sd_nfds
526 # define SLAP_EVENT_WAIT(t, tvp, nsp) do { \
528 *(nsp) = select( SLAP_EVENT_MAX(t), &readfds, \
529 nwriters > 0 ? &writefds : NULL, NULL, (tvp) ); \
530 for ( i=0; i<readfds.fd_count; i++) { \
531 int fd = slapd_sock2fd(readfds.fd_array[i]); \
533 slap_daemon[t].sd_rflags[fd] = SD_READ; \
534 if ( fd >= *(nsp)) *(nsp) = fd+1; \
537 for ( i=0; i<writefds.fd_count; i++) { \
538 int fd = slapd_sock2fd(writefds.fd_array[i]); \
540 slap_daemon[t].sd_rflags[fd] = SD_WRITE; \
541 if ( fd >= *(nsp)) *(nsp) = fd+1; \
546 # define SLAP_EVENT_IS_READ(fd) (rflags[fd] & SD_READ)
547 # define SLAP_EVENT_IS_WRITE(fd) (rflags[fd] & SD_WRITE)
549 # define SLAP_EVENT_CLR_READ(fd) rflags[fd] &= ~SD_READ
550 # define SLAP_EVENT_CLR_WRITE(fd) rflags[fd] &= ~SD_WRITE
552 # define SLAP_SOCK_INIT(t) do { \
554 ldap_pvt_thread_mutex_init( &slapd_ws_mutex ); \
555 slapd_ws_sockets = ch_malloc( dtblsize * ( sizeof(SOCKET) + 2)); \
556 memset( slapd_ws_sockets, -1, dtblsize * sizeof(SOCKET) ); \
558 slap_daemon[t].sd_flags = (char *)(slapd_ws_sockets + dtblsize); \
559 slap_daemon[t].sd_rflags = slap_daemon[t].sd_flags + dtblsize; \
560 memset( slap_daemon[t].sd_flags, 0, dtblsize ); \
561 slapd_ws_sockets[t*2] = wake_sds[t][0]; \
562 slapd_ws_sockets[t*2+1] = wake_sds[t][1]; \
563 wake_sds[t][0] = t*2; \
564 wake_sds[t][1] = t*2+1; \
565 slap_daemon[t].sd_nfds = t*2 + 2; \
568 # define SLAP_SOCK_DESTROY(t) do { \
569 ch_free( slapd_ws_sockets ); slapd_ws_sockets = NULL; \
570 slap_daemon[t].sd_flags = NULL; \
571 slap_daemon[t].sd_rflags = NULL; \
572 ldap_pvt_thread_mutex_destroy( &slapd_ws_mutex ); \
575 # define SLAP_SOCK_IS_ACTIVE(t,fd) ( slap_daemon[t].sd_flags[fd] & SD_ACTIVE )
576 # define SLAP_SOCK_IS_READ(t,fd) ( slap_daemon[t].sd_flags[fd] & SD_READ )
577 # define SLAP_SOCK_IS_WRITE(t,fd) ( slap_daemon[t].sd_flags[fd] & SD_WRITE )
578 # define SLAP_SOCK_NOT_ACTIVE(t,fd) (!slap_daemon[t].sd_flags[fd])
580 # define SLAP_SOCK_SET_READ(t,fd) ( slap_daemon[t].sd_flags[fd] |= SD_READ )
581 # define SLAP_SOCK_SET_WRITE(t,fd) ( slap_daemon[t].sd_flags[fd] |= SD_WRITE )
583 # define SLAP_SELECT_ADDTEST(t,s) do { \
584 if ((s) >= slap_daemon[t].sd_nfds) slap_daemon[t].sd_nfds = (s)+1; \
587 # define SLAP_SOCK_CLR_READ(t,fd) ( slap_daemon[t].sd_flags[fd] &= ~SD_READ )
588 # define SLAP_SOCK_CLR_WRITE(t,fd) ( slap_daemon[t].sd_flags[fd] &= ~SD_WRITE )
590 # define SLAP_SOCK_ADD(t,s, l) do { \
591 SLAP_SELECT_ADDTEST(t,(s)); \
592 slap_daemon[t].sd_flags[s] = SD_ACTIVE|SD_READ; \
595 # define SLAP_SOCK_DEL(t,s) do { \
596 slap_daemon[t].sd_flags[s] = 0; \
597 slapd_sockdel( s ); \
600 # else /* !HAVE_WINSOCK */
602 /**************************************
603 * Use select system call - select(2) *
604 **************************************/
605 # define SLAP_EVENT_FNAME "select"
607 # define SLAP_EVENTS_ARE_INDEXED 1
608 # define SLAP_EVENT_DECL fd_set readfds, writefds
610 # define SLAP_EVENT_INIT(t) do { \
611 AC_MEMCPY( &readfds, &slap_daemon[t].sd_readers, sizeof(fd_set) ); \
613 AC_MEMCPY( &writefds, &slap_daemon[t].sd_writers, sizeof(fd_set) ); \
615 FD_ZERO( &writefds ); \
620 # define SLAP_SELECT_CHK_SETSIZE do { \
621 if (dtblsize > FD_SETSIZE) dtblsize = FD_SETSIZE; \
623 # else /* ! FD_SETSIZE */
624 # define SLAP_SELECT_CHK_SETSIZE do { ; } while (0)
625 # endif /* ! FD_SETSIZE */
627 # define SLAP_SOCK_INIT(t) do { \
628 SLAP_SELECT_CHK_SETSIZE; \
629 FD_ZERO(&slap_daemon[t].sd_actives); \
630 FD_ZERO(&slap_daemon[t].sd_readers); \
631 FD_ZERO(&slap_daemon[t].sd_writers); \
634 # define SLAP_SOCK_DESTROY(t)
636 # define SLAP_SOCK_IS_ACTIVE(t,fd) FD_ISSET((fd), &slap_daemon[t].sd_actives)
637 # define SLAP_SOCK_IS_READ(t,fd) FD_ISSET((fd), &slap_daemon[t].sd_readers)
638 # define SLAP_SOCK_IS_WRITE(t,fd) FD_ISSET((fd), &slap_daemon[t].sd_writers)
640 # define SLAP_SOCK_NOT_ACTIVE(t,fd) (!SLAP_SOCK_IS_ACTIVE(t,fd) && \
641 !SLAP_SOCK_IS_READ(t,fd) && !SLAP_SOCK_IS_WRITE(t,fd))
643 # define SLAP_SOCK_SET_READ(t,fd) FD_SET((fd), &slap_daemon[t].sd_readers)
644 # define SLAP_SOCK_SET_WRITE(t,fd) FD_SET((fd), &slap_daemon[t].sd_writers)
646 # define SLAP_EVENT_MAX(t) slap_daemon[t].sd_nfds
647 # define SLAP_SELECT_ADDTEST(t,s) do { \
648 if ((s) >= slap_daemon[t].sd_nfds) slap_daemon[t].sd_nfds = (s)+1; \
651 # define SLAP_SOCK_CLR_READ(t,fd) FD_CLR((fd), &slap_daemon[t].sd_readers)
652 # define SLAP_SOCK_CLR_WRITE(t,fd) FD_CLR((fd), &slap_daemon[t].sd_writers)
654 # define SLAP_SOCK_ADD(t,s, l) do { \
655 SLAP_SELECT_ADDTEST(t,(s)); \
656 FD_SET((s), &slap_daemon[t].sd_actives); \
657 FD_SET((s), &slap_daemon[t].sd_readers); \
660 # define SLAP_SOCK_DEL(t,s) do { \
661 FD_CLR((s), &slap_daemon[t].sd_actives); \
662 FD_CLR((s), &slap_daemon[t].sd_readers); \
663 FD_CLR((s), &slap_daemon[t].sd_writers); \
666 # define SLAP_EVENT_IS_READ(fd) FD_ISSET((fd), &readfds)
667 # define SLAP_EVENT_IS_WRITE(fd) FD_ISSET((fd), &writefds)
669 # define SLAP_EVENT_CLR_READ(fd) FD_CLR((fd), &readfds)
670 # define SLAP_EVENT_CLR_WRITE(fd) FD_CLR((fd), &writefds)
672 # define SLAP_EVENT_WAIT(t, tvp, nsp) do { \
673 *(nsp) = select( SLAP_EVENT_MAX(t), &readfds, \
674 nwriters > 0 ? &writefds : NULL, NULL, (tvp) ); \
676 # endif /* !HAVE_WINSOCK */
677 #endif /* ! epoll && ! /dev/poll */
681 * SLP related functions
685 #define LDAP_SRVTYPE_PREFIX "service:ldap://"
686 #define LDAPS_SRVTYPE_PREFIX "service:ldaps://"
687 static char** slapd_srvurls = NULL;
688 static SLPHandle slapd_hslp = 0;
689 int slapd_register_slp = 0;
690 const char *slapd_slp_attrs = NULL;
692 static SLPError slapd_slp_cookie;
695 slapd_slp_init( const char* urls )
700 slapd_srvurls = ldap_str2charray( urls, " " );
702 if ( slapd_srvurls == NULL ) return;
704 /* find and expand INADDR_ANY URLs */
705 for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
706 if ( strcmp( slapd_srvurls[i], "ldap:///" ) == 0 ) {
707 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
708 global_host_bv.bv_len +
709 sizeof( LDAP_SRVTYPE_PREFIX ) );
710 strcpy( lutil_strcopy(slapd_srvurls[i],
711 LDAP_SRVTYPE_PREFIX ), global_host_bv.bv_val );
712 } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0 ) {
713 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
714 global_host_bv.bv_len +
715 sizeof( LDAPS_SRVTYPE_PREFIX ) );
716 strcpy( lutil_strcopy(slapd_srvurls[i],
717 LDAPS_SRVTYPE_PREFIX ), global_host_bv.bv_val );
721 /* open the SLP handle */
722 err = SLPOpen( "en", 0, &slapd_hslp );
724 if ( err != SLP_OK ) {
725 Debug( LDAP_DEBUG_CONNS, "daemon: SLPOpen() failed with %ld\n",
731 slapd_slp_deinit( void )
733 if ( slapd_srvurls == NULL ) return;
735 ldap_charray_free( slapd_srvurls );
736 slapd_srvurls = NULL;
738 /* close the SLP handle */
739 SLPClose( slapd_hslp );
748 /* return the error code in the cookie */
749 *(SLPError*)cookie = errcode;
758 if ( slapd_srvurls == NULL ) return;
760 for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
761 if ( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
762 sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
763 strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
764 sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
766 err = SLPReg( slapd_hslp,
768 SLP_LIFETIME_MAXIMUM,
770 (slapd_slp_attrs) ? slapd_slp_attrs : "",
775 if ( err != SLP_OK || slapd_slp_cookie != SLP_OK ) {
776 Debug( LDAP_DEBUG_CONNS,
777 "daemon: SLPReg(%s) failed with %ld, cookie = %ld\n",
778 slapd_srvurls[i], (long)err, (long)slapd_slp_cookie );
785 slapd_slp_dereg( void )
790 if ( slapd_srvurls == NULL ) return;
792 for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
793 err = SLPDereg( slapd_hslp,
798 if ( err != SLP_OK || slapd_slp_cookie != SLP_OK ) {
799 Debug( LDAP_DEBUG_CONNS,
800 "daemon: SLPDereg(%s) failed with %ld, cookie = %ld\n",
801 slapd_srvurls[i], (long)err, (long)slapd_slp_cookie );
805 #endif /* HAVE_SLP */
808 /* Manage the descriptor to socket table */
810 slapd_socknew( ber_socket_t s )
813 ldap_pvt_thread_mutex_lock( &slapd_ws_mutex );
814 for ( i = 0; i < dtblsize && slapd_ws_sockets[i] != INVALID_SOCKET; i++ );
815 if ( i == dtblsize ) {
816 WSASetLastError( WSAEMFILE );
818 slapd_ws_sockets[i] = s;
820 ldap_pvt_thread_mutex_unlock( &slapd_ws_mutex );
825 slapd_sockdel( ber_socket_t s )
827 ldap_pvt_thread_mutex_lock( &slapd_ws_mutex );
828 slapd_ws_sockets[s] = INVALID_SOCKET;
829 ldap_pvt_thread_mutex_unlock( &slapd_ws_mutex );
833 slapd_sock2fd( ber_socket_t s )
836 for ( i=0; i<dtblsize && slapd_ws_sockets[i] != s; i++);
844 * Add a descriptor to daemon control
846 * If isactive, the descriptor is a live server session and is subject
847 * to idletimeout control. Otherwise, the descriptor is a passive
848 * listener or an outbound client session, and not subject to
849 * idletimeout. The underlying event handler may record the Listener
850 * argument to differentiate Listener's from real sessions.
853 slapd_add( ber_socket_t s, int isactive, Listener *sl, int id )
857 ldap_pvt_thread_mutex_lock( &slap_daemon[id].sd_mutex );
859 assert( SLAP_SOCK_NOT_ACTIVE(id, s) );
861 if ( isactive ) slap_daemon[id].sd_nactives++;
863 SLAP_SOCK_ADD(id, s, sl);
865 Debug( LDAP_DEBUG_CONNS, "daemon: added %ldr%s listener=%p\n",
866 (long) s, isactive ? " (active)" : "", (void *)sl );
868 ldap_pvt_thread_mutex_unlock( &slap_daemon[id].sd_mutex );
874 * Remove the descriptor from daemon control
886 int id = DAEMON_ID(s);
889 ldap_pvt_thread_mutex_lock( &slap_daemon[id].sd_mutex );
891 assert( SLAP_SOCK_IS_ACTIVE( id, s ));
893 if ( wasactive ) slap_daemon[id].sd_nactives--;
895 waswriter = SLAP_SOCK_IS_WRITE(id, s);
896 wasreader = SLAP_SOCK_IS_READ(id, s);
898 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
900 wasreader ? "r" : "",
901 waswriter ? "w" : "" );
903 if ( waswriter ) slap_daemon[id].sd_nwriters--;
905 SLAP_SOCK_DEL(id, s);
908 ber_sockbuf_free(sb);
910 /* If we ran out of file descriptors, we dropped a listener from
911 * the select() loop. Now that we're removing a session from our
912 * control, we can try to resume a dropped listener to use.
914 if ( emfile && listening ) {
916 for ( i = 0; slap_listeners[i] != NULL; i++ ) {
917 Listener *lr = slap_listeners[i];
919 if ( lr->sl_sd == AC_SOCKET_INVALID ) continue;
920 if ( lr->sl_sd == s ) continue;
924 if ( DAEMON_ID(lr->sl_sd) != id )
925 WAKE_LISTENER(DAEMON_ID(lr->sl_sd), wake);
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[id].sd_mutex );
935 WAKE_LISTENER(id, wake || slapd_gentle_shutdown == 2);
939 slapd_clr_write( ber_socket_t s, int wake )
941 int id = DAEMON_ID(s);
942 ldap_pvt_thread_mutex_lock( &slap_daemon[id].sd_mutex );
944 if ( SLAP_SOCK_IS_WRITE( id, s )) {
945 assert( SLAP_SOCK_IS_ACTIVE( id, s ));
947 SLAP_SOCK_CLR_WRITE( id, s );
948 slap_daemon[id].sd_nwriters--;
951 ldap_pvt_thread_mutex_unlock( &slap_daemon[id].sd_mutex );
952 WAKE_LISTENER(id,wake);
956 slapd_set_write( ber_socket_t s, int wake )
958 int id = DAEMON_ID(s);
959 ldap_pvt_thread_mutex_lock( &slap_daemon[id].sd_mutex );
961 assert( SLAP_SOCK_IS_ACTIVE( id, s ));
963 if ( !SLAP_SOCK_IS_WRITE( id, s )) {
964 SLAP_SOCK_SET_WRITE( id, s );
965 slap_daemon[id].sd_nwriters++;
967 if (( wake & 2 ) && global_writetimeout && !chk_writetime ) {
969 ldap_pvt_thread_mutex_lock( &slap_daemon[0].sd_mutex );
971 chk_writetime = slap_get_time();
973 ldap_pvt_thread_mutex_unlock( &slap_daemon[0].sd_mutex );
976 ldap_pvt_thread_mutex_unlock( &slap_daemon[id].sd_mutex );
977 WAKE_LISTENER(id,wake);
981 slapd_clr_read( ber_socket_t s, int wake )
984 int id = DAEMON_ID(s);
985 ldap_pvt_thread_mutex_lock( &slap_daemon[id].sd_mutex );
987 if ( SLAP_SOCK_IS_ACTIVE( id, s )) {
988 SLAP_SOCK_CLR_READ( id, s );
991 ldap_pvt_thread_mutex_unlock( &slap_daemon[id].sd_mutex );
993 WAKE_LISTENER(id,wake);
998 slapd_set_read( ber_socket_t s, int wake )
1001 int id = DAEMON_ID(s);
1002 ldap_pvt_thread_mutex_lock( &slap_daemon[id].sd_mutex );
1004 if( SLAP_SOCK_IS_ACTIVE( id, s ) && !SLAP_SOCK_IS_READ( id, s )) {
1005 SLAP_SOCK_SET_READ( id, s );
1009 ldap_pvt_thread_mutex_unlock( &slap_daemon[id].sd_mutex );
1011 WAKE_LISTENER(id,wake);
1015 slapd_get_writetime()
1018 ldap_pvt_thread_mutex_lock( &slap_daemon[0].sd_mutex );
1019 cur = chk_writetime;
1020 ldap_pvt_thread_mutex_unlock( &slap_daemon[0].sd_mutex );
1025 slapd_clr_writetime( time_t old )
1027 ldap_pvt_thread_mutex_lock( &slap_daemon[0].sd_mutex );
1028 if ( chk_writetime == old )
1030 ldap_pvt_thread_mutex_unlock( &slap_daemon[0].sd_mutex );
1034 slapd_close( ber_socket_t s )
1036 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
1038 tcp_close( SLAP_FD2SOCK(s) );
1045 slap_free_listener_addresses( struct sockaddr **sal )
1047 struct sockaddr **sap;
1048 if (sal == NULL) return;
1049 for (sap = sal; *sap != NULL; sap++) ch_free(*sap);
1053 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
1062 assert( exts != NULL );
1063 assert( perms != NULL );
1064 assert( crit != NULL );
1067 for ( i = 0; exts[ i ]; i++ ) {
1068 char *type = exts[ i ];
1071 if ( type[ 0 ] == '!' ) {
1076 if ( strncasecmp( type, LDAPI_MOD_URLEXT "=",
1077 sizeof(LDAPI_MOD_URLEXT "=") - 1 ) == 0 )
1079 char *value = type + ( sizeof(LDAPI_MOD_URLEXT "=") - 1 );
1083 switch (strlen(value)) {
1085 /* skip leading '0' */
1086 if ( value[ 0 ] != '0' ) return LDAP_OTHER;
1090 for ( j = 0; j < 3; j++) {
1093 v = value[ j ] - '0';
1095 if ( v < 0 || v > 7 ) return LDAP_OTHER;
1102 for ( j = 1; j < 10; j++ ) {
1103 static mode_t m[] = { 0,
1104 S_IRUSR, S_IWUSR, S_IXUSR,
1105 S_IRGRP, S_IWGRP, S_IXGRP,
1106 S_IROTH, S_IWOTH, S_IXOTH
1108 static const char c[] = "-rwxrwxrwx";
1110 if ( value[ j ] == c[ j ] ) {
1113 } else if ( value[ j ] != '-' ) {
1126 return LDAP_SUCCESS;
1132 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
1134 /* port = 0 indicates AF_LOCAL */
1136 slap_get_listener_addresses(
1138 unsigned short port,
1139 struct sockaddr ***sal )
1141 struct sockaddr **sap;
1143 #ifdef LDAP_PF_LOCAL
1145 *sal = ch_malloc(2 * sizeof(void *));
1146 if (*sal == NULL) return -1;
1149 *sap = ch_malloc(sizeof(struct sockaddr_un));
1150 if (*sap == NULL) goto errexit;
1154 (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) )
1156 Debug( LDAP_DEBUG_ANY,
1157 "daemon: domain socket path (%s) too long in URL",
1162 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
1163 (*sap)->sa_family = AF_LOCAL;
1164 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
1166 #endif /* LDAP_PF_LOCAL */
1168 #ifdef HAVE_GETADDRINFO
1169 struct addrinfo hints, *res, *sai;
1173 memset( &hints, '\0', sizeof(hints) );
1174 hints.ai_flags = AI_PASSIVE;
1175 hints.ai_socktype = SOCK_STREAM;
1176 hints.ai_family = slap_inet4or6;
1177 snprintf(serv, sizeof serv, "%d", port);
1179 if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
1180 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo() failed: %s\n",
1181 AC_GAI_STRERROR(err), 0, 0);
1186 for (n=2; (sai = sai->ai_next) != NULL; n++) {
1189 *sal = ch_calloc(n, sizeof(void *));
1190 if (*sal == NULL) return -1;
1195 for ( sai=res; sai; sai=sai->ai_next ) {
1196 if( sai->ai_addr == NULL ) {
1197 Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
1198 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
1203 switch (sai->ai_family) {
1204 # ifdef LDAP_PF_INET6
1206 *sap = ch_malloc(sizeof(struct sockaddr_in6));
1211 *(struct sockaddr_in6 *)*sap =
1212 *((struct sockaddr_in6 *)sai->ai_addr);
1214 # endif /* LDAP_PF_INET6 */
1216 *sap = ch_malloc(sizeof(struct sockaddr_in));
1221 *(struct sockaddr_in *)*sap =
1222 *((struct sockaddr_in *)sai->ai_addr);
1230 (*sap)->sa_family = sai->ai_family;
1238 #else /* ! HAVE_GETADDRINFO */
1241 struct hostent *he = NULL;
1243 if ( host == NULL ) {
1244 in.s_addr = htonl(INADDR_ANY);
1246 } else if ( !inet_aton( host, &in ) ) {
1247 he = gethostbyname( host );
1249 Debug( LDAP_DEBUG_ANY,
1250 "daemon: invalid host %s", host, 0, 0);
1253 for (n = 0; he->h_addr_list[n]; n++) /* empty */;
1256 *sal = ch_malloc((n+1) * sizeof(void *));
1257 if (*sal == NULL) return -1;
1260 for ( i = 0; i<n; i++ ) {
1261 sap[i] = ch_malloc(sizeof(struct sockaddr_in));
1262 if (*sap == NULL) goto errexit;
1264 (void)memset( (void *)sap[i], '\0', sizeof(struct sockaddr_in) );
1265 sap[i]->sa_family = AF_INET;
1266 ((struct sockaddr_in *)sap[i])->sin_port = htons(port);
1267 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr,
1268 he ? (struct in_addr *)he->h_addr_list[i] : &in,
1269 sizeof(struct in_addr) );
1272 #endif /* ! HAVE_GETADDRINFO */
1278 slap_free_listener_addresses(*sal);
1292 unsigned short port;
1293 int err, addrlen = 0;
1294 struct sockaddr **sal, **psal;
1295 int socktype = SOCK_STREAM; /* default to COTS */
1298 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
1303 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
1305 rc = ldap_url_parse( url, &lud );
1307 if( rc != LDAP_URL_SUCCESS ) {
1308 Debug( LDAP_DEBUG_ANY,
1309 "daemon: listen URL \"%s\" parse error=%d\n",
1314 l.sl_url.bv_val = NULL;
1319 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
1320 Debug( LDAP_DEBUG_ANY, "daemon: TLS not supported (%s)\n",
1322 ldap_free_urldesc( lud );
1326 if(! lud->lud_port ) lud->lud_port = LDAP_PORT;
1328 #else /* HAVE_TLS */
1329 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
1331 if(! lud->lud_port ) {
1332 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
1334 #endif /* HAVE_TLS */
1336 #ifdef LDAP_TCP_BUFFER
1339 #endif /* LDAP_TCP_BUFFER */
1341 port = (unsigned short) lud->lud_port;
1343 tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme);
1344 if ( tmp == LDAP_PROTO_IPC ) {
1345 #ifdef LDAP_PF_LOCAL
1346 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
1347 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
1349 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
1351 #else /* ! LDAP_PF_LOCAL */
1353 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
1355 ldap_free_urldesc( lud );
1357 #endif /* ! LDAP_PF_LOCAL */
1359 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
1360 || strcmp(lud->lud_host, "*") == 0 )
1362 err = slap_get_listener_addresses(NULL, port, &sal);
1364 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
1368 #ifdef LDAP_CONNECTIONLESS
1369 l.sl_is_udp = ( tmp == LDAP_PROTO_UDP );
1370 #endif /* LDAP_CONNECTIONLESS */
1372 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
1373 if ( lud->lud_exts ) {
1374 err = get_url_perms( lud->lud_exts, &l.sl_perms, &crit );
1376 l.sl_perms = S_IRWXU | S_IRWXO;
1378 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
1380 ldap_free_urldesc( lud );
1382 slap_free_listener_addresses(sal);
1386 /* If we got more than one address returned, we need to make space
1387 * for it in the slap_listeners array.
1389 for ( num=0; sal[num]; num++ ) /* empty */;
1391 *listeners += num-1;
1392 slap_listeners = ch_realloc( slap_listeners,
1393 (*listeners + 1) * sizeof(Listener *) );
1397 while ( *sal != NULL ) {
1399 switch( (*sal)->sa_family ) {
1403 #ifdef LDAP_PF_INET6
1407 #endif /* LDAP_PF_INET6 */
1408 #ifdef LDAP_PF_LOCAL
1412 #endif /* LDAP_PF_LOCAL */
1418 #ifdef LDAP_CONNECTIONLESS
1419 if( l.sl_is_udp ) socktype = SOCK_DGRAM;
1420 #endif /* LDAP_CONNECTIONLESS */
1422 s = socket( (*sal)->sa_family, socktype, 0);
1423 if ( s == AC_SOCKET_INVALID ) {
1424 int err = sock_errno();
1425 Debug( LDAP_DEBUG_ANY,
1426 "daemon: %s socket() failed errno=%d (%s)\n",
1427 af, err, sock_errstr(err) );
1431 l.sl_sd = SLAP_SOCKNEW( s );
1433 if ( l.sl_sd >= dtblsize ) {
1434 Debug( LDAP_DEBUG_ANY,
1435 "daemon: listener descriptor %ld is too great %ld\n",
1436 (long) l.sl_sd, (long) dtblsize, 0 );
1442 #ifdef LDAP_PF_LOCAL
1443 if ( (*sal)->sa_family == AF_LOCAL ) {
1444 unlink( ((struct sockaddr_un *)*sal)->sun_path );
1446 #endif /* LDAP_PF_LOCAL */
1449 /* enable address reuse */
1451 rc = setsockopt( s, SOL_SOCKET, SO_REUSEADDR,
1452 (char *) &tmp, sizeof(tmp) );
1453 if ( rc == AC_SOCKET_ERROR ) {
1454 int err = sock_errno();
1455 Debug( LDAP_DEBUG_ANY, "slapd(%ld): "
1456 "setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
1457 (long) l.sl_sd, err, sock_errstr(err) );
1459 #endif /* SO_REUSEADDR */
1462 switch( (*sal)->sa_family ) {
1464 addrlen = sizeof(struct sockaddr_in);
1466 #ifdef LDAP_PF_INET6
1469 /* Try to use IPv6 sockets for IPv6 only */
1471 rc = setsockopt( s , IPPROTO_IPV6, IPV6_V6ONLY,
1472 (char *) &tmp, sizeof(tmp) );
1473 if ( rc == AC_SOCKET_ERROR ) {
1474 int err = sock_errno();
1475 Debug( LDAP_DEBUG_ANY, "slapd(%ld): "
1476 "setsockopt(IPV6_V6ONLY) failed errno=%d (%s)\n",
1477 (long) l.sl_sd, err, sock_errstr(err) );
1479 #endif /* IPV6_V6ONLY */
1480 addrlen = sizeof(struct sockaddr_in6);
1482 #endif /* LDAP_PF_INET6 */
1484 #ifdef LDAP_PF_LOCAL
1489 setsockopt( s, 0, LOCAL_CREDS, &one, sizeof( one ) );
1491 #endif /* LOCAL_CREDS */
1493 addrlen = sizeof( struct sockaddr_un );
1495 #endif /* LDAP_PF_LOCAL */
1498 #ifdef LDAP_PF_LOCAL
1499 /* create socket with all permissions set for those systems
1500 * that honor permissions on sockets (e.g. Linux); typically,
1501 * only write is required. To exploit filesystem permissions,
1502 * place the socket in a directory and use directory's
1503 * permissions. Need write perms to the directory to
1504 * create/unlink the socket; likely need exec perms to access
1505 * the socket (ITS#4709) */
1507 mode_t old_umask = 0;
1509 if ( (*sal)->sa_family == AF_LOCAL ) {
1510 old_umask = umask( 0 );
1512 #endif /* LDAP_PF_LOCAL */
1513 rc = bind( s, *sal, addrlen );
1514 #ifdef LDAP_PF_LOCAL
1515 if ( old_umask != 0 ) {
1519 #endif /* LDAP_PF_LOCAL */
1522 Debug( LDAP_DEBUG_ANY,
1523 "daemon: bind(%ld) failed errno=%d (%s)\n",
1524 (long)l.sl_sd, err, sock_errstr( err ) );
1530 switch ( (*sal)->sa_family ) {
1531 #ifdef LDAP_PF_LOCAL
1533 char *path = ((struct sockaddr_un *)*sal)->sun_path;
1534 l.sl_name.bv_len = strlen(path) + STRLENOF("PATH=");
1535 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len + 1 );
1536 snprintf( l.sl_name.bv_val, l.sl_name.bv_len + 1,
1539 #endif /* LDAP_PF_LOCAL */
1542 char addr[INET_ADDRSTRLEN];
1544 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
1545 s = inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
1546 addr, sizeof(addr) );
1547 #else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1548 s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr );
1549 #endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1550 if (!s) s = SLAP_STRING_UNKNOWN;
1551 port = ntohs( ((struct sockaddr_in *)*sal) ->sin_port );
1553 ber_memalloc( sizeof("IP=255.255.255.255:65535") );
1554 snprintf( l.sl_name.bv_val, sizeof("IP=255.255.255.255:65535"),
1555 "IP=%s:%d", s, port );
1556 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1559 #ifdef LDAP_PF_INET6
1561 char addr[INET6_ADDRSTRLEN];
1563 s = inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
1565 if (!s) s = SLAP_STRING_UNKNOWN;
1566 port = ntohs( ((struct sockaddr_in6 *)*sal)->sin6_port );
1567 l.sl_name.bv_len = strlen(s) + sizeof("IP=[]:65535");
1568 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len );
1569 snprintf( l.sl_name.bv_val, l.sl_name.bv_len, "IP=[%s]:%d",
1571 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1573 #endif /* LDAP_PF_INET6 */
1576 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
1577 (int) (*sal)->sa_family, 0, 0 );
1581 AC_MEMCPY(&l.sl_sa, *sal, addrlen);
1582 ber_str2bv( url, 0, 1, &l.sl_url);
1583 li = ch_malloc( sizeof( Listener ) );
1585 slap_listeners[*cur] = li;
1590 slap_free_listener_addresses(psal);
1592 if ( l.sl_url.bv_val == NULL ) {
1593 Debug( LDAP_DEBUG_TRACE,
1594 "slap_open_listener: failed on %s\n", url, 0, 0 );
1598 Debug( LDAP_DEBUG_TRACE, "daemon: listener initialized %s\n",
1599 l.sl_url.bv_val, 0, 0 );
1603 static int sockinit(void);
1604 static int sockdestroy(void);
1606 static int daemon_inited = 0;
1609 slapd_daemon_init( const char *urls )
1614 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
1615 urls ? urls : "<null>", 0, 0 );
1617 for ( i=0; i<SLAPD_MAX_DAEMON_THREADS; i++ ) {
1618 wake_sds[i][0] = AC_SOCKET_INVALID;
1619 wake_sds[i][1] = AC_SOCKET_INVALID;
1622 ldap_pvt_thread_mutex_init( &slap_daemon[0].sd_mutex );
1624 ldap_pvt_thread_mutex_init( &sd_tcpd_mutex );
1625 #endif /* TCP Wrappers */
1629 if( (rc = sockinit()) != 0 ) return rc;
1632 dtblsize = sysconf( _SC_OPEN_MAX );
1633 #elif defined(HAVE_GETDTABLESIZE)
1634 dtblsize = getdtablesize();
1635 #else /* ! HAVE_SYSCONF && ! HAVE_GETDTABLESIZE */
1636 dtblsize = FD_SETSIZE;
1637 #endif /* ! HAVE_SYSCONF && ! HAVE_GETDTABLESIZE */
1639 /* open a pipe (or something equivalent connected to itself).
1640 * we write a byte on this fd whenever we catch a signal. The main
1641 * loop will be select'ing on this socket, and will wake up when
1642 * this byte arrives.
1644 if( (rc = lutil_pair( wake_sds[0] )) < 0 ) {
1645 Debug( LDAP_DEBUG_ANY,
1646 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
1649 ber_pvt_socket_set_nonblock( wake_sds[0][1], 1 );
1653 if( urls == NULL ) urls = "ldap:///";
1655 u = ldap_str2charray( urls, " " );
1657 if( u == NULL || u[0] == NULL ) {
1658 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
1661 ldap_charray_free( u );
1665 for( i=0; u[i] != NULL; i++ ) {
1666 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
1671 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
1673 ldap_charray_free( u );
1677 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
1679 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
1681 for(n = 0, j = 0; u[n]; n++ ) {
1682 if ( slap_open_listener( u[n], &i, &j ) ) {
1683 ldap_charray_free( u );
1687 slap_listeners[j] = NULL;
1689 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
1694 if( slapd_register_slp ) {
1695 slapd_slp_init( urls );
1698 #endif /* HAVE_SLP */
1700 ldap_charray_free( u );
1707 slapd_daemon_destroy( void )
1709 connections_destroy();
1710 if ( daemon_inited ) {
1713 for ( i=0; i<slapd_daemon_threads; i++ ) {
1715 if ( wake_sds[i][1] != INVALID_SOCKET &&
1716 SLAP_FD2SOCK( wake_sds[i][1] ) != SLAP_FD2SOCK( wake_sds[i][0] ))
1717 #endif /* HAVE_WINSOCK */
1718 tcp_close( SLAP_FD2SOCK(wake_sds[i][1]) );
1720 if ( wake_sds[i][0] != INVALID_SOCKET )
1721 #endif /* HAVE_WINSOCK */
1722 tcp_close( SLAP_FD2SOCK(wake_sds[i][0]) );
1723 ldap_pvt_thread_mutex_destroy( &slap_daemon[i].sd_mutex );
1724 SLAP_SOCK_DESTROY(i);
1728 ldap_pvt_thread_mutex_destroy( &sd_tcpd_mutex );
1729 #endif /* TCP Wrappers */
1734 if( slapd_register_slp ) {
1738 #endif /* HAVE_SLP */
1754 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1755 Listener *lr = slap_listeners[l];
1757 if ( lr->sl_sd != AC_SOCKET_INVALID ) {
1759 lr->sl_sd = AC_SOCKET_INVALID;
1760 if ( remove ) slapd_remove( s, NULL, 0, 0, 0 );
1762 #ifdef LDAP_PF_LOCAL
1763 if ( lr->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1764 unlink( lr->sl_sa.sa_un_addr.sun_path );
1766 #endif /* LDAP_PF_LOCAL */
1774 destroy_listeners( void )
1776 Listener *lr, **ll = slap_listeners;
1781 while ( (lr = *ll++) != NULL ) {
1782 if ( lr->sl_url.bv_val ) {
1783 ber_memfree( lr->sl_url.bv_val );
1786 if ( lr->sl_name.bv_val ) {
1787 ber_memfree( lr->sl_name.bv_val );
1793 free( slap_listeners );
1794 slap_listeners = NULL;
1803 ber_socket_t s, sfd;
1804 ber_socklen_t len = sizeof(from);
1807 struct berval authid = BER_BVNULL;
1808 #ifdef SLAPD_RLOOKUPS
1809 char hbuf[NI_MAXHOST];
1810 #endif /* SLAPD_RLOOKUPS */
1812 char *dnsname = NULL;
1813 const char *peeraddr = NULL;
1814 /* we assume INET6_ADDRSTRLEN > INET_ADDRSTRLEN */
1815 char addr[INET6_ADDRSTRLEN];
1816 #ifdef LDAP_PF_LOCAL
1817 char peername[MAXPATHLEN + sizeof("PATH=")];
1818 #ifdef LDAP_PF_LOCAL_SENDMSG
1820 struct berval peerbv = BER_BVNULL;
1822 #elif defined(LDAP_PF_INET6)
1823 char peername[sizeof("IP=[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535")];
1824 #else /* ! LDAP_PF_LOCAL && ! LDAP_PF_INET6 */
1825 char peername[sizeof("IP=255.255.255.255:65336")];
1826 #endif /* LDAP_PF_LOCAL */
1830 Debug( LDAP_DEBUG_TRACE,
1831 ">>> slap_listener(%s)\n",
1832 sl->sl_url.bv_val, 0, 0 );
1836 #ifdef LDAP_CONNECTIONLESS
1837 if ( sl->sl_is_udp ) return 1;
1838 #endif /* LDAP_CONNECTIONLESS */
1840 # ifdef LDAP_PF_LOCAL
1841 /* FIXME: apparently accept doesn't fill
1842 * the sun_path sun_path member */
1843 from.sa_un_addr.sun_path[0] = '\0';
1844 # endif /* LDAP_PF_LOCAL */
1846 s = accept( SLAP_FD2SOCK( sl->sl_sd ), (struct sockaddr *) &from, &len );
1848 /* Resume the listener FD to allow concurrent-processing of
1849 * additional incoming connections.
1852 WAKE_LISTENER(DAEMON_ID(sl->sl_sd),1);
1854 if ( s == AC_SOCKET_INVALID ) {
1855 int err = sock_errno();
1866 ldap_pvt_thread_mutex_lock( &slap_daemon[0].sd_mutex );
1868 /* Stop listening until an existing session closes */
1870 ldap_pvt_thread_mutex_unlock( &slap_daemon[0].sd_mutex );
1873 Debug( LDAP_DEBUG_ANY,
1874 "daemon: accept(%ld) failed errno=%d (%s)\n",
1875 (long) sl->sl_sd, err, sock_errstr(err) );
1876 ldap_pvt_thread_yield();
1879 sfd = SLAP_SOCKNEW( s );
1881 /* make sure descriptor number isn't too great */
1882 if ( sfd >= dtblsize ) {
1883 Debug( LDAP_DEBUG_ANY,
1884 "daemon: %ld beyond descriptor table size %ld\n",
1885 (long) sfd, (long) dtblsize, 0 );
1888 ldap_pvt_thread_yield();
1891 tid = DAEMON_ID(sfd);
1894 ldap_pvt_thread_mutex_lock( &slap_daemon[tid].sd_mutex );
1895 /* newly accepted stream should not be in any of the FD SETS */
1896 assert( SLAP_SOCK_NOT_ACTIVE( tid, sfd ));
1897 ldap_pvt_thread_mutex_unlock( &slap_daemon[tid].sd_mutex );
1898 #endif /* LDAP_DEBUG */
1900 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1901 #ifdef LDAP_PF_LOCAL
1902 /* for IPv4 and IPv6 sockets only */
1903 if ( from.sa_addr.sa_family != AF_LOCAL )
1904 #endif /* LDAP_PF_LOCAL */
1909 /* enable keep alives */
1911 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1912 (char *) &tmp, sizeof(tmp) );
1913 if ( rc == AC_SOCKET_ERROR ) {
1914 int err = sock_errno();
1915 Debug( LDAP_DEBUG_ANY,
1916 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1917 "errno=%d (%s)\n", (long) sfd, err, sock_errstr(err) );
1919 #endif /* SO_KEEPALIVE */
1921 /* enable no delay */
1923 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1924 (char *)&tmp, sizeof(tmp) );
1925 if ( rc == AC_SOCKET_ERROR ) {
1926 int err = sock_errno();
1927 Debug( LDAP_DEBUG_ANY,
1928 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1929 "errno=%d (%s)\n", (long) sfd, err, sock_errstr(err) );
1931 #endif /* TCP_NODELAY */
1933 #endif /* SO_KEEPALIVE || TCP_NODELAY */
1935 Debug( LDAP_DEBUG_CONNS,
1936 "daemon: listen=%ld, new connection on %ld\n",
1937 (long) sl->sl_sd, (long) sfd, 0 );
1940 switch ( from.sa_addr.sa_family ) {
1941 # ifdef LDAP_PF_LOCAL
1943 cflag |= CONN_IS_IPC;
1945 /* FIXME: apparently accept doesn't fill
1946 * the sun_path sun_path member */
1947 if ( from.sa_un_addr.sun_path[0] == '\0' ) {
1948 AC_MEMCPY( from.sa_un_addr.sun_path,
1949 sl->sl_sa.sa_un_addr.sun_path,
1950 sizeof( from.sa_un_addr.sun_path ) );
1953 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1959 #ifdef LDAP_PF_LOCAL_SENDMSG
1960 peerbv.bv_val = peerbuf;
1961 peerbv.bv_len = sizeof( peerbuf );
1963 if( LUTIL_GETPEEREID( s, &uid, &gid, &peerbv ) == 0 ) {
1964 authid.bv_val = ch_malloc(
1965 STRLENOF( "gidNumber=4294967295+uidNumber=4294967295,"
1966 "cn=peercred,cn=external,cn=auth" ) + 1 );
1967 authid.bv_len = sprintf( authid.bv_val,
1968 "gidNumber=%d+uidNumber=%d,"
1969 "cn=peercred,cn=external,cn=auth",
1970 (int) gid, (int) uid );
1971 assert( authid.bv_len <=
1972 STRLENOF( "gidNumber=4294967295+uidNumber=4294967295,"
1973 "cn=peercred,cn=external,cn=auth" ) );
1978 #endif /* LDAP_PF_LOCAL */
1980 # ifdef LDAP_PF_INET6
1982 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1983 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
1984 peeraddr = inet_ntop( AF_INET,
1985 ((struct in_addr *)&from.sa_in6_addr.sin6_addr.s6_addr[12]),
1986 addr, sizeof(addr) );
1987 #else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1988 peeraddr = inet_ntoa( *((struct in_addr *)
1989 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1990 #endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1991 if ( !peeraddr ) peeraddr = SLAP_STRING_UNKNOWN;
1992 sprintf( peername, "IP=%s:%d", peeraddr,
1993 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1995 peeraddr = inet_ntop( AF_INET6,
1996 &from.sa_in6_addr.sin6_addr,
1997 addr, sizeof addr );
1998 if ( !peeraddr ) peeraddr = SLAP_STRING_UNKNOWN;
1999 sprintf( peername, "IP=[%s]:%d", peeraddr,
2000 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
2003 # endif /* LDAP_PF_INET6 */
2006 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
2007 peeraddr = inet_ntop( AF_INET, &from.sa_in_addr.sin_addr,
2008 addr, sizeof(addr) );
2009 #else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
2010 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
2011 #endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
2012 if ( !peeraddr ) peeraddr = SLAP_STRING_UNKNOWN;
2013 sprintf( peername, "IP=%s:%d", peeraddr,
2014 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
2022 if ( ( from.sa_addr.sa_family == AF_INET )
2023 #ifdef LDAP_PF_INET6
2024 || ( from.sa_addr.sa_family == AF_INET6 )
2025 #endif /* LDAP_PF_INET6 */
2029 #ifdef SLAPD_RLOOKUPS
2030 if ( use_reverse_lookup ) {
2032 if (ldap_pvt_get_hname( (const struct sockaddr *)&from, len, hbuf,
2033 sizeof(hbuf), &herr ) == 0) {
2034 ldap_pvt_str2lower( hbuf );
2038 #endif /* SLAPD_RLOOKUPS */
2043 ldap_pvt_thread_mutex_lock( &sd_tcpd_mutex );
2044 rc = hosts_ctl("slapd",
2045 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
2047 SLAP_STRING_UNKNOWN );
2048 ldap_pvt_thread_mutex_unlock( &sd_tcpd_mutex );
2051 Statslog( LDAP_DEBUG_STATS,
2052 "fd=%ld DENIED from %s (%s)\n",
2054 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
2060 #endif /* HAVE_TCPD */
2064 if ( sl->sl_is_tls ) cflag |= CONN_IS_TLS;
2066 c = connection_init(sfd, sl,
2067 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
2068 peername, cflag, ssf,
2069 authid.bv_val ? &authid : NULL
2070 LDAP_PF_LOCAL_SENDMSG_ARG(&peerbv));
2072 if( authid.bv_val ) ch_free(authid.bv_val);
2075 Debug( LDAP_DEBUG_ANY,
2076 "daemon: connection_init(%ld, %s, %s) failed.\n",
2077 (long) sfd, peername, sl->sl_name.bv_val );
2085 slap_listener_thread(
2090 Listener *sl = (Listener *)ptr;
2092 rc = slap_listener( sl );
2094 if( rc != LDAP_SUCCESS ) {
2095 Debug( LDAP_DEBUG_ANY,
2096 "slap_listener_thread(%s): failed err=%d",
2097 sl->sl_url.bv_val, rc, 0 );
2104 slap_listener_activate(
2109 Debug( LDAP_DEBUG_TRACE, "slap_listener_activate(%d): %s\n",
2110 sl->sl_sd, sl->sl_busy ? "busy" : "", 0 );
2114 rc = ldap_pvt_thread_pool_submit( &connection_pool,
2115 slap_listener_thread, (void *) sl );
2118 Debug( LDAP_DEBUG_ANY,
2119 "slap_listener_activate(%d): submit failed (%d)\n",
2130 time_t last_idle_check = 0;
2132 int tid = (ldap_pvt_thread_t *) ptr - listener_tid;
2134 #define SLAPD_IDLE_CHECK_LIMIT 4
2136 slapd_add( wake_sds[tid][0], 0, NULL, tid );
2140 /* Init stuff done only by thread 0 */
2142 last_idle_check = slap_get_time();
2144 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2145 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ) continue;
2147 #ifdef LDAP_CONNECTIONLESS
2148 /* Since this is connectionless, the data port is the
2149 * listening port. The listen() and accept() calls
2152 if ( slap_listeners[l]->sl_is_udp )
2154 #endif /* LDAP_CONNECTIONLESS */
2156 /* FIXME: TCP-only! */
2157 #ifdef LDAP_TCP_BUFFER
2159 int origsize, size, realsize, rc;
2161 char buf[ SLAP_TEXT_BUFLEN ];
2164 if ( slap_listeners[l]->sl_tcp_rmem > 0 ) {
2165 size = slap_listeners[l]->sl_tcp_rmem;
2166 } else if ( slapd_tcp_rmem > 0 ) {
2167 size = slapd_tcp_rmem;
2171 optlen = sizeof( origsize );
2172 rc = getsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2179 int err = sock_errno();
2180 Debug( LDAP_DEBUG_ANY,
2181 "slapd_daemon_task: getsockopt(SO_RCVBUF) failed errno=%d (%s)\n",
2182 err, sock_errstr(err), 0 );
2185 optlen = sizeof( size );
2186 rc = setsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2189 (const void *)&size,
2193 int err = sock_errno();
2194 Debug( LDAP_DEBUG_ANY,
2195 "slapd_daemon_task: setsockopt(SO_RCVBUF) failed errno=%d (%s)\n",
2196 err, sock_errstr(err), 0 );
2199 optlen = sizeof( realsize );
2200 rc = getsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2207 int err = sock_errno();
2208 Debug( LDAP_DEBUG_ANY,
2209 "slapd_daemon_task: getsockopt(SO_RCVBUF) failed errno=%d (%s)\n",
2210 err, sock_errstr(err), 0 );
2213 snprintf( buf, sizeof( buf ),
2214 "url=%s (#%d) RCVBUF original size=%d requested size=%d real size=%d",
2215 slap_listeners[l]->sl_url.bv_val, l, origsize, size, realsize );
2216 Debug( LDAP_DEBUG_ANY,
2217 "slapd_daemon_task: %s\n",
2222 if ( slap_listeners[l]->sl_tcp_wmem > 0 ) {
2223 size = slap_listeners[l]->sl_tcp_wmem;
2224 } else if ( slapd_tcp_wmem > 0 ) {
2225 size = slapd_tcp_wmem;
2229 optlen = sizeof( origsize );
2230 rc = getsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2237 int err = sock_errno();
2238 Debug( LDAP_DEBUG_ANY,
2239 "slapd_daemon_task: getsockopt(SO_SNDBUF) failed errno=%d (%s)\n",
2240 err, sock_errstr(err), 0 );
2243 optlen = sizeof( size );
2244 rc = setsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2247 (const void *)&size,
2251 int err = sock_errno();
2252 Debug( LDAP_DEBUG_ANY,
2253 "slapd_daemon_task: setsockopt(SO_SNDBUF) failed errno=%d (%s)",
2254 err, sock_errstr(err), 0 );
2257 optlen = sizeof( realsize );
2258 rc = getsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2265 int err = sock_errno();
2266 Debug( LDAP_DEBUG_ANY,
2267 "slapd_daemon_task: getsockopt(SO_SNDBUF) failed errno=%d (%s)\n",
2268 err, sock_errstr(err), 0 );
2271 snprintf( buf, sizeof( buf ),
2272 "url=%s (#%d) SNDBUF original size=%d requested size=%d real size=%d",
2273 slap_listeners[l]->sl_url.bv_val, l, origsize, size, realsize );
2274 Debug( LDAP_DEBUG_ANY,
2275 "slapd_daemon_task: %s\n",
2279 #endif /* LDAP_TCP_BUFFER */
2281 if ( listen( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ), SLAPD_LISTEN_BACKLOG ) == -1 ) {
2282 int err = sock_errno();
2284 #ifdef LDAP_PF_INET6
2285 /* If error is EADDRINUSE, we are trying to listen to INADDR_ANY and
2286 * we are already listening to in6addr_any, then we want to ignore
2287 * this and continue.
2289 if ( err == EADDRINUSE ) {
2291 struct sockaddr_in sa = slap_listeners[l]->sl_sa.sa_in_addr;
2292 struct sockaddr_in6 sa6;
2294 if ( sa.sin_family == AF_INET &&
2295 sa.sin_addr.s_addr == htonl(INADDR_ANY) ) {
2296 for ( i = 0 ; i < l; i++ ) {
2297 sa6 = slap_listeners[i]->sl_sa.sa_in6_addr;
2298 if ( sa6.sin6_family == AF_INET6 &&
2299 !memcmp( &sa6.sin6_addr, &in6addr_any,
2300 sizeof(struct in6_addr) ) )
2307 /* We are already listening to in6addr_any */
2308 Debug( LDAP_DEBUG_CONNS,
2309 "daemon: Attempt to listen to 0.0.0.0 failed, "
2310 "already listening on ::, assuming IPv4 included\n",
2312 slapd_close( slap_listeners[l]->sl_sd );
2313 slap_listeners[l]->sl_sd = AC_SOCKET_INVALID;
2318 #endif /* LDAP_PF_INET6 */
2319 Debug( LDAP_DEBUG_ANY,
2320 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
2321 slap_listeners[l]->sl_url.bv_val, err,
2326 /* make the listening socket non-blocking */
2327 if ( ber_pvt_socket_set_nonblock( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ), 1 ) < 0 ) {
2328 Debug( LDAP_DEBUG_ANY, "slapd_daemon_task: "
2329 "set nonblocking on a listening socket failed\n",
2335 slapd_add( slap_listeners[l]->sl_sd, 0, slap_listeners[l], -1 );
2338 #ifdef HAVE_NT_SERVICE_MANAGER
2339 if ( started_event != NULL ) {
2340 ldap_pvt_thread_cond_signal( &started_event );
2342 #endif /* HAVE_NT_SERVICE_MANAGER */
2346 /* initialization complete. Here comes the loop. */
2348 while ( !slapd_shutdown ) {
2353 #if SLAP_EVENTS_ARE_INDEXED
2354 ber_socket_t nrfds, nwfds;
2355 #endif /* SLAP_EVENTS_ARE_INDEXED */
2356 #define SLAPD_EBADF_LIMIT 16
2363 struct timeval *tvp;
2369 now = slap_get_time();
2371 if ( !tid && ( global_idletimeout > 0 || chk_writetime )) {
2373 /* Set the select timeout.
2374 * Don't just truncate, preserve the fractions of
2375 * seconds to prevent sleeping for zero time.
2377 if ( chk_writetime ) {
2378 tv.tv_sec = global_writetimeout;
2380 if ( difftime( chk_writetime, now ) < 0 )
2383 tv.tv_sec = global_idletimeout / SLAPD_IDLE_CHECK_LIMIT;
2384 tv.tv_usec = global_idletimeout - \
2385 ( tv.tv_sec * SLAPD_IDLE_CHECK_LIMIT );
2386 tv.tv_usec *= 1000000 / SLAPD_IDLE_CHECK_LIMIT;
2387 if ( difftime( last_idle_check +
2388 global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 )
2392 connections_timeout_idle( now );
2393 last_idle_check = now;
2401 if ( slapd_gentle_shutdown ) {
2402 ber_socket_t active;
2404 if ( !tid && slapd_gentle_shutdown == 1 ) {
2406 Debug( LDAP_DEBUG_ANY, "slapd gentle shutdown\n", 0, 0, 0 );
2407 close_listeners( 1 );
2408 frontendDB->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
2409 LDAP_STAILQ_FOREACH(be, &backendDB, be_next) {
2410 be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
2412 slapd_gentle_shutdown = 2;
2415 ldap_pvt_thread_mutex_lock( &slap_daemon[tid].sd_mutex );
2416 active = slap_daemon[tid].sd_nactives;
2417 ldap_pvt_thread_mutex_unlock( &slap_daemon[tid].sd_mutex );
2419 if ( active == 0 ) {
2421 for ( l=1; l<slapd_daemon_threads; l++ ) {
2422 ldap_pvt_thread_mutex_lock( &slap_daemon[l].sd_mutex );
2423 active += slap_daemon[l].sd_nactives;
2424 ldap_pvt_thread_mutex_unlock( &slap_daemon[l].sd_mutex );
2436 ldap_pvt_thread_mutex_lock( &slap_daemon[tid].sd_mutex );
2438 nwriters = slap_daemon[tid].sd_nwriters;
2441 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2442 Listener *lr = slap_listeners[l];
2444 if ( lr->sl_sd == AC_SOCKET_INVALID ) continue;
2445 if ( DAEMON_ID( lr->sl_sd ) != tid ) continue;
2447 if ( lr->sl_mute || lr->sl_busy )
2449 SLAP_SOCK_CLR_READ( tid, lr->sl_sd );
2451 SLAP_SOCK_SET_READ( tid, lr->sl_sd );
2455 SLAP_EVENT_INIT(tid);
2457 nfds = SLAP_EVENT_MAX(tid);
2459 if (( chk_writetime || global_idletimeout ) && slap_daemon[tid].sd_nactives ) at = 1;
2461 ldap_pvt_thread_mutex_unlock( &slap_daemon[tid].sd_mutex );
2464 #if defined(HAVE_YIELDING_SELECT) || defined(NO_THREADS)
2465 && ( tv.tv_sec || tv.tv_usec )
2466 #endif /* HAVE_YIELDING_SELECT || NO_THREADS */
2474 /* Only thread 0 handles runqueue */
2476 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2477 rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
2478 while ( rtask && cat.tv_sec && cat.tv_sec <= now ) {
2479 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
2480 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
2482 ldap_pvt_runqueue_runtask( &slapd_rq, rtask );
2483 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
2484 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2485 ldap_pvt_thread_pool_submit( &connection_pool,
2486 rtask->routine, (void *) rtask );
2487 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2489 rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
2491 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2493 if ( rtask && cat.tv_sec ) {
2494 /* NOTE: diff __should__ always be >= 0,
2495 * AFAI understand; however (ITS#4872),
2496 * time_t might be unsigned in some systems,
2497 * while difftime() returns a double */
2498 double diff = difftime( cat.tv_sec, now );
2502 if ( tvp == NULL || diff < tv.tv_sec ) {
2510 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2511 Listener *lr = slap_listeners[l];
2513 if ( lr->sl_sd == AC_SOCKET_INVALID ) {
2517 if ( lr->sl_mute ) {
2518 Debug( LDAP_DEBUG_CONNS,
2519 "daemon: " SLAP_EVENT_FNAME ": "
2520 "listen=%d muted\n",
2525 if ( lr->sl_busy ) {
2526 Debug( LDAP_DEBUG_CONNS,
2527 "daemon: " SLAP_EVENT_FNAME ": "
2533 Debug( LDAP_DEBUG_CONNS,
2534 "daemon: " SLAP_EVENT_FNAME ": "
2535 "listen=%d active_threads=%d tvp=%s\n",
2536 lr->sl_sd, at, tvp == NULL ? "NULL" : "zero" );
2539 SLAP_EVENT_WAIT( tid, tvp, &ns );
2541 case -1: { /* failure - try again */
2542 int err = sock_errno();
2544 if ( err != EINTR ) {
2547 /* Don't log unless we got it twice in a row */
2548 if ( !( ebadf & 1 ) ) {
2549 Debug( LDAP_DEBUG_ANY,
2555 sock_errstr( err ) );
2557 if ( ebadf >= SLAPD_EBADF_LIMIT ) {
2564 case 0: /* timeout - let threads run */
2566 #ifndef HAVE_YIELDING_SELECT
2567 Debug( LDAP_DEBUG_CONNS, "daemon: " SLAP_EVENT_FNAME
2568 "timeout - yielding\n",
2571 ldap_pvt_thread_yield();
2572 #endif /* ! HAVE_YIELDING_SELECT */
2575 default: /* something happened - deal with it */
2576 if ( slapd_shutdown ) continue;
2579 Debug( LDAP_DEBUG_CONNS,
2580 "daemon: activity on %d descriptor%s\n",
2581 ns, ns != 1 ? "s" : "", 0 );
2585 #if SLAP_EVENTS_ARE_INDEXED
2586 if ( SLAP_EVENT_IS_READ( wake_sds[tid][0] ) ) {
2588 SLAP_EVENT_CLR_READ( wake_sds[tid][0] );
2590 tcp_read( SLAP_FD2SOCK(wake_sds[tid][0]), c, sizeof(c) );
2591 Debug( LDAP_DEBUG_CONNS, "daemon: waked\n", 0, 0, 0 );
2595 /* The event slot equals the descriptor number - this is
2596 * true for Unix select and poll. We treat Windows select
2597 * like this too, even though it's a kludge.
2600 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2603 if ( ns <= 0 ) break;
2604 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ) continue;
2605 #ifdef LDAP_CONNECTIONLESS
2606 if ( slap_listeners[l]->sl_is_udp ) continue;
2607 #endif /* LDAP_CONNECTIONLESS */
2608 if ( !SLAP_EVENT_IS_READ( slap_listeners[l]->sl_sd ) ) continue;
2611 SLAP_EVENT_CLR_READ( slap_listeners[l]->sl_sd );
2612 SLAP_EVENT_CLR_WRITE( slap_listeners[l]->sl_sd );
2615 rc = slap_listener_activate( slap_listeners[l] );
2618 /* bypass the following tests if no descriptors left */
2620 #ifndef HAVE_YIELDING_SELECT
2621 ldap_pvt_thread_yield();
2622 #endif /* HAVE_YIELDING_SELECT */
2626 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
2629 for ( i = 0; i < nfds; i++ ) {
2632 r = SLAP_EVENT_IS_READ( i );
2633 /* writefds was not initialized if nwriters was zero */
2634 w = nwriters ? SLAP_EVENT_IS_WRITE( i ) : 0;
2636 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
2637 r ? "r" : "", w ? "w" : "" );
2647 if ( ns <= 0 ) break;
2649 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
2651 /* loop through the writers */
2652 for ( i = 0; nwfds > 0; i++ ) {
2654 if ( ! SLAP_EVENT_IS_WRITE( i ) ) continue;
2657 SLAP_EVENT_CLR_WRITE( wd );
2660 Debug( LDAP_DEBUG_CONNS,
2661 "daemon: write active on %d\n",
2665 * NOTE: it is possible that the connection was closed
2666 * and that the stream is now inactive.
2667 * connection_write() must validate the stream is still
2670 * ITS#4338: if the stream is invalid, there is no need to
2671 * close it here. It has already been closed in connection.c.
2673 if ( connection_write( wd ) < 0 ) {
2674 if ( SLAP_EVENT_IS_READ( wd ) ) {
2675 SLAP_EVENT_CLR_READ( (unsigned) wd );
2681 for ( i = 0; nrfds > 0; i++ ) {
2683 if ( ! SLAP_EVENT_IS_READ( i ) ) continue;
2685 SLAP_EVENT_CLR_READ( rd );
2688 Debug ( LDAP_DEBUG_CONNS,
2689 "daemon: read activity on %d\n", rd, 0, 0 );
2691 * NOTE: it is possible that the connection was closed
2692 * and that the stream is now inactive.
2693 * connection_read() must valid the stream is still
2697 connection_read_activate( rd );
2699 #else /* !SLAP_EVENTS_ARE_INDEXED */
2701 /* The events are returned in an arbitrary list. This is true
2702 * for /dev/poll, epoll and kqueue. In order to prioritize things
2703 * so that we can handle wake_sds first, listeners second, and then
2704 * all other connections last (as we do for select), we would need
2705 * to use multiple event handles and cascade them.
2707 * That seems like a bit of hassle. So the wake_sds check has been
2708 * skipped. For epoll and kqueue we can associate arbitrary data with
2709 * an event, so we could use pointers to the listener structure
2710 * instead of just the file descriptor. For /dev/poll we have to
2711 * search the listeners array for a matching descriptor.
2713 * We now handle wake events when we see them; they are not given
2717 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
2719 for ( i = 0; i < ns; i++ ) {
2722 /* Don't log listener events */
2723 if ( SLAP_EVENT_IS_LISTENER( tid, i )
2724 #ifdef LDAP_CONNECTIONLESS
2725 && !( (SLAP_EVENT_LISTENER( tid, i ))->sl_is_udp )
2726 #endif /* LDAP_CONNECTIONLESS */
2732 fd = SLAP_EVENT_FD( tid, i );
2733 /* Don't log internal wake events */
2734 if ( fd == wake_sds[tid][0] ) continue;
2736 r = SLAP_EVENT_IS_READ( i );
2737 w = SLAP_EVENT_IS_WRITE( i );
2739 Debug( LDAP_DEBUG_CONNS, " %d%s%s", fd,
2740 r ? "r" : "", w ? "w" : "" );
2743 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
2744 #endif /* LDAP_DEBUG */
2746 for ( i = 0; i < ns; i++ ) {
2747 int rc = 1, fd, w = 0, r = 0;
2749 if ( SLAP_EVENT_IS_LISTENER( tid, i ) ) {
2750 rc = slap_listener_activate( SLAP_EVENT_LISTENER( tid, i ) );
2753 /* If we found a regular listener, rc is now zero, and we
2754 * can skip the data portion. But if it was a UDP listener
2755 * then rc is still 1, and we want to handle the data.
2758 fd = SLAP_EVENT_FD( tid, i );
2760 /* Handle wake events */
2761 if ( fd == wake_sds[tid][0] ) {
2764 tcp_read( SLAP_FD2SOCK(wake_sds[tid][0]), c, sizeof(c) );
2768 if ( SLAP_EVENT_IS_WRITE( i ) ) {
2769 Debug( LDAP_DEBUG_CONNS,
2770 "daemon: write active on %d\n",
2773 SLAP_EVENT_CLR_WRITE( i );
2777 * NOTE: it is possible that the connection was closed
2778 * and that the stream is now inactive.
2779 * connection_write() must valid the stream is still
2782 if ( connection_write( fd ) < 0 ) {
2786 /* If event is a read */
2787 if ( SLAP_EVENT_IS_READ( i )) {
2789 Debug( LDAP_DEBUG_CONNS,
2790 "daemon: read active on %d\n",
2793 SLAP_EVENT_CLR_READ( i );
2794 connection_read_activate( fd );
2797 /* Don't keep reporting the hangup
2799 if ( SLAP_SOCK_IS_ACTIVE( tid, fd )) {
2800 SLAP_EPOLL_SOCK_SET( tid, fd, EPOLLET );
2806 #endif /* SLAP_EVENTS_ARE_INDEXED */
2808 #ifndef HAVE_YIELDING_SELECT
2809 ldap_pvt_thread_yield();
2810 #endif /* ! HAVE_YIELDING_SELECT */
2813 /* Only thread 0 handles shutdown */
2817 if ( slapd_shutdown == 1 ) {
2818 Debug( LDAP_DEBUG_ANY,
2819 "daemon: shutdown requested and initiated.\n",
2822 } else if ( slapd_shutdown == 2 ) {
2823 #ifdef HAVE_NT_SERVICE_MANAGER
2824 Debug( LDAP_DEBUG_ANY,
2825 "daemon: shutdown initiated by Service Manager.\n",
2827 #else /* !HAVE_NT_SERVICE_MANAGER */
2828 Debug( LDAP_DEBUG_ANY,
2829 "daemon: abnormal condition, shutdown initiated.\n",
2831 #endif /* !HAVE_NT_SERVICE_MANAGER */
2833 Debug( LDAP_DEBUG_ANY,
2834 "daemon: no active streams, shutdown initiated.\n",
2838 close_listeners( 0 );
2840 if ( !slapd_gentle_shutdown ) {
2841 slapd_abrupt_shutdown = 1;
2842 connections_shutdown();
2845 if ( LogTest( LDAP_DEBUG_ANY )) {
2846 int t = ldap_pvt_thread_pool_backload( &connection_pool );
2847 Debug( LDAP_DEBUG_ANY,
2848 "slapd shutdown: waiting for %d operations/tasks to finish\n",
2851 ldap_pvt_thread_pool_destroy( &connection_pool, 1 );
2857 #ifdef LDAP_CONNECTIONLESS
2859 connectionless_init( void )
2863 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2864 Listener *lr = slap_listeners[l];
2867 if ( !lr->sl_is_udp ) {
2871 c = connection_init( lr->sl_sd, lr, "", "",
2872 CONN_IS_UDP, (slap_ssf_t) 0, NULL
2873 LDAP_PF_LOCAL_SENDMSG_ARG(NULL));
2876 Debug( LDAP_DEBUG_TRACE,
2877 "connectionless_init: failed on %s (%d)\n",
2878 lr->sl_url.bv_val, lr->sl_sd, 0 );
2886 #endif /* LDAP_CONNECTIONLESS */
2889 slapd_daemon( void )
2893 #ifdef LDAP_CONNECTIONLESS
2894 connectionless_init();
2895 #endif /* LDAP_CONNECTIONLESS */
2897 if ( slapd_daemon_threads > SLAPD_MAX_DAEMON_THREADS )
2898 slapd_daemon_threads = SLAPD_MAX_DAEMON_THREADS;
2900 listener_tid = ch_malloc(slapd_daemon_threads * sizeof(ldap_pvt_thread_t));
2902 /* daemon_init only inits element 0 */
2903 for ( i=1; i<slapd_daemon_threads; i++ )
2905 ldap_pvt_thread_mutex_init( &slap_daemon[i].sd_mutex );
2907 if( (rc = lutil_pair( wake_sds[i] )) < 0 ) {
2908 Debug( LDAP_DEBUG_ANY,
2909 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
2912 ber_pvt_socket_set_nonblock( wake_sds[i][1], 1 );
2917 for ( i=0; i<slapd_daemon_threads; i++ )
2919 /* listener as a separate THREAD */
2920 rc = ldap_pvt_thread_create( &listener_tid[i],
2921 0, slapd_daemon_task, &listener_tid[i] );
2924 Debug( LDAP_DEBUG_ANY,
2925 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
2930 /* wait for the listener threads to complete */
2931 for ( i=0; i<slapd_daemon_threads; i++ )
2932 ldap_pvt_thread_join( listener_tid[i], (void *)NULL );
2934 destroy_listeners();
2935 ch_free( listener_tid );
2936 listener_tid = NULL;
2944 #if defined( HAVE_WINSOCK2 )
2945 WORD wVersionRequested;
2949 wVersionRequested = MAKEWORD( 2, 0 );
2951 err = WSAStartup( wVersionRequested, &wsaData );
2953 /* Tell the user that we couldn't find a usable */
2958 /* Confirm that the WinSock DLL supports 2.0.*/
2959 /* Note that if the DLL supports versions greater */
2960 /* than 2.0 in addition to 2.0, it will still return */
2961 /* 2.0 in wVersion since that is the version we */
2964 if ( LOBYTE( wsaData.wVersion ) != 2 ||
2965 HIBYTE( wsaData.wVersion ) != 0 )
2967 /* Tell the user that we couldn't find a usable */
2973 /* The WinSock DLL is acceptable. Proceed. */
2974 #elif defined( HAVE_WINSOCK )
2976 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) return -1;
2977 #endif /* ! HAVE_WINSOCK2 && ! HAVE_WINSOCK */
2985 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
2987 #endif /* HAVE_WINSOCK2 || HAVE_WINSOCK */
2993 slap_sig_shutdown( int sig )
2995 int save_errno = errno;
2999 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
3003 * If the NT Service Manager is controlling the server, we don't
3004 * want SIGBREAK to kill the server. For some strange reason,
3005 * SIGBREAK is generated when a user logs out.
3008 #if defined(HAVE_NT_SERVICE_MANAGER) && defined(SIGBREAK)
3009 if (is_NT_Service && sig == SIGBREAK) {
3012 #endif /* HAVE_NT_SERVICE_MANAGER && SIGBREAK */
3014 if (sig == SIGHUP && global_gentlehup && slapd_gentle_shutdown == 0) {
3015 slapd_gentle_shutdown = 1;
3022 for (i=0; i<slapd_daemon_threads; i++) {
3026 /* reinstall self */
3027 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
3033 slap_sig_wake( int sig )
3035 int save_errno = errno;
3039 /* reinstall self */
3040 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
3047 slapd_add_internal( ber_socket_t s, int isactive )
3049 slapd_add( s, isactive, NULL, -1 );
3053 slapd_get_listeners( void )
3055 /* Could return array with no listeners if !listening, but current
3056 * callers mostly look at the URLs. E.g. syncrepl uses this to
3057 * identify the server, which means it wants the startup arguments.
3059 return slap_listeners;
3062 /* Reject all incoming requests */
3064 slap_suspend_listeners( void )
3067 for (i=0; slap_listeners[i]; i++) {
3068 slap_listeners[i]->sl_mute = 1;
3069 listen( slap_listeners[i]->sl_sd, 0 );
3073 /* Resume after a suspend */
3075 slap_resume_listeners( void )
3078 for (i=0; slap_listeners[i]; i++) {
3079 slap_listeners[i]->sl_mute = 0;
3080 listen( slap_listeners[i]->sl_sd, SLAPD_LISTEN_BACKLOG );
3085 slap_wake_listener()