2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2010 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 #ifdef LDAP_TCP_BUFFER
79 #endif /* LDAP_TCP_BUFFER */
81 Listener **slap_listeners = NULL;
83 #ifndef SLAPD_LISTEN_BACKLOG
84 #define SLAPD_LISTEN_BACKLOG 1024
85 #endif /* ! SLAPD_LISTEN_BACKLOG */
87 static ber_socket_t wake_sds[2]
89 = { INVALID_SOCKET, INVALID_SOCKET }
90 #endif /* HAVE_WINSOCK */
94 static time_t chk_writetime;
96 static volatile int waking;
98 #define WAKE_LISTENER(w) do { \
99 if ((w) && ++waking < 5) { \
100 tcp_write( SLAP_FD2SOCK(wake_sds[1]), "0", 1 ); \
103 #else /* ! NO_THREADS */
104 #define WAKE_LISTENER(w) do { \
106 tcp_write( SLAP_FD2SOCK(wake_sds[1]), "0", 1 ); \
109 #endif /* ! NO_THREADS */
111 volatile sig_atomic_t slapd_shutdown = 0;
112 volatile sig_atomic_t slapd_gentle_shutdown = 0;
113 volatile sig_atomic_t slapd_abrupt_shutdown = 0;
116 ldap_pvt_thread_mutex_t slapd_ws_mutex;
117 SOCKET *slapd_ws_sockets;
121 #define SD_LISTENER 8
124 static struct slap_daemon {
125 ldap_pvt_thread_mutex_t sd_mutex;
127 ldap_pvt_thread_mutex_t sd_tcpd_mutex;
128 #endif /* TCP Wrappers */
130 ber_socket_t sd_nactives;
134 #if defined(HAVE_EPOLL)
135 struct epoll_event *sd_epolls;
138 #elif defined(SLAP_X_DEVPOLL) && defined(HAVE_DEVPOLL)
140 struct pollfd *sd_pollfd;
144 #else /* ! epoll && ! /dev/poll */
148 #else /* ! HAVE_WINSOCK */
152 #endif /* ! HAVE_WINSOCK */
153 #endif /* ! epoll && ! /dev/poll */
157 * NOTE: naming convention for macros:
159 * - SLAP_SOCK_* and SLAP_EVENT_* for public interface that deals
160 * with file descriptors and events respectively
162 * - SLAP_<type>_* for private interface; type by now is one of
163 * EPOLL, DEVPOLL, SELECT
165 * private interface should not be used in the code.
167 #if defined(HAVE_EPOLL)
168 /***************************************
169 * Use epoll infrastructure - epoll(4) *
170 ***************************************/
171 # define SLAP_EVENT_FNAME "epoll"
172 # define SLAP_EVENTS_ARE_INDEXED 0
173 # define SLAP_EPOLL_SOCK_IX(s) (slap_daemon.sd_index[(s)])
174 # define SLAP_EPOLL_SOCK_EP(s) (slap_daemon.sd_epolls[SLAP_EPOLL_SOCK_IX(s)])
175 # define SLAP_EPOLL_SOCK_EV(s) (SLAP_EPOLL_SOCK_EP(s).events)
176 # define SLAP_SOCK_IS_ACTIVE(s) (SLAP_EPOLL_SOCK_IX(s) != -1)
177 # define SLAP_SOCK_NOT_ACTIVE(s) (SLAP_EPOLL_SOCK_IX(s) == -1)
178 # define SLAP_EPOLL_SOCK_IS_SET(s, mode) (SLAP_EPOLL_SOCK_EV(s) & (mode))
180 # define SLAP_SOCK_IS_READ(s) SLAP_EPOLL_SOCK_IS_SET((s), EPOLLIN)
181 # define SLAP_SOCK_IS_WRITE(s) SLAP_EPOLL_SOCK_IS_SET((s), EPOLLOUT)
183 # define SLAP_EPOLL_SOCK_SET(s, mode) do { \
184 if ( (SLAP_EPOLL_SOCK_EV(s) & (mode)) != (mode) ) { \
185 SLAP_EPOLL_SOCK_EV(s) |= (mode); \
186 epoll_ctl( slap_daemon.sd_epfd, EPOLL_CTL_MOD, (s), \
187 &SLAP_EPOLL_SOCK_EP(s) ); \
191 # define SLAP_EPOLL_SOCK_CLR(s, mode) do { \
192 if ( (SLAP_EPOLL_SOCK_EV(s) & (mode)) ) { \
193 SLAP_EPOLL_SOCK_EV(s) &= ~(mode); \
194 epoll_ctl( slap_daemon.sd_epfd, EPOLL_CTL_MOD, s, \
195 &SLAP_EPOLL_SOCK_EP(s) ); \
199 # define SLAP_SOCK_SET_READ(s) SLAP_EPOLL_SOCK_SET(s, EPOLLIN)
200 # define SLAP_SOCK_SET_WRITE(s) SLAP_EPOLL_SOCK_SET(s, EPOLLOUT)
202 # define SLAP_SOCK_CLR_READ(s) SLAP_EPOLL_SOCK_CLR((s), EPOLLIN)
203 # define SLAP_SOCK_CLR_WRITE(s) SLAP_EPOLL_SOCK_CLR((s), EPOLLOUT)
205 # define SLAP_SOCK_SET_SUSPEND(s) \
206 ( slap_daemon.sd_suspend[SLAP_EPOLL_SOCK_IX(s)] = 1 )
207 # define SLAP_SOCK_CLR_SUSPEND(s) \
208 ( slap_daemon.sd_suspend[SLAP_EPOLL_SOCK_IX(s)] = 0 )
209 # define SLAP_SOCK_IS_SUSPEND(s) \
210 ( slap_daemon.sd_suspend[SLAP_EPOLL_SOCK_IX(s)] == 1 )
212 # define SLAP_EPOLL_EVENT_CLR(i, mode) (revents[(i)].events &= ~(mode))
214 # define SLAP_EVENT_MAX slap_daemon.sd_nfds
216 /* If a Listener address is provided, store that as the epoll data.
217 * Otherwise, store the address of this socket's slot in the
218 * index array. If we can't do this add, the system is out of
219 * resources and we need to shutdown.
221 # define SLAP_SOCK_ADD(s, l) do { \
223 SLAP_EPOLL_SOCK_IX((s)) = slap_daemon.sd_nfds; \
224 SLAP_EPOLL_SOCK_EP((s)).data.ptr = (l) ? (l) : (void *)(&SLAP_EPOLL_SOCK_IX(s)); \
225 SLAP_EPOLL_SOCK_EV((s)) = EPOLLIN; \
226 rc = epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_ADD, \
227 (s), &SLAP_EPOLL_SOCK_EP((s))); \
229 slap_daemon.sd_nfds++; \
231 Debug( LDAP_DEBUG_ANY, \
232 "daemon: epoll_ctl(ADD,fd=%d) failed, errno=%d, shutting down\n", \
234 slapd_shutdown = 2; \
238 # define SLAP_EPOLL_EV_LISTENER(ptr) \
239 (((int *)(ptr) >= slap_daemon.sd_index && \
240 (int *)(ptr) <= &slap_daemon.sd_index[dtblsize]) ? 0 : 1 )
242 # define SLAP_EPOLL_EV_PTRFD(ptr) (SLAP_EPOLL_EV_LISTENER(ptr) ? \
243 ((Listener *)ptr)->sl_sd : \
244 (ber_socket_t) ((int *)(ptr) - slap_daemon.sd_index))
246 # define SLAP_SOCK_DEL(s) do { \
247 int fd, rc, index = SLAP_EPOLL_SOCK_IX((s)); \
248 if ( index < 0 ) break; \
249 rc = epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_DEL, \
250 (s), &SLAP_EPOLL_SOCK_EP((s))); \
251 slap_daemon.sd_epolls[index] = \
252 slap_daemon.sd_epolls[slap_daemon.sd_nfds-1]; \
253 fd = SLAP_EPOLL_EV_PTRFD(slap_daemon.sd_epolls[index].data.ptr); \
254 slap_daemon.sd_index[fd] = index; \
255 slap_daemon.sd_index[(s)] = -1; \
256 slap_daemon.sd_nfds--; \
259 # define SLAP_EVENT_CLR_READ(i) SLAP_EPOLL_EVENT_CLR((i), EPOLLIN)
260 # define SLAP_EVENT_CLR_WRITE(i) SLAP_EPOLL_EVENT_CLR((i), EPOLLOUT)
262 # define SLAP_EPOLL_EVENT_CHK(i, mode) (revents[(i)].events & mode)
264 # define SLAP_EVENT_IS_READ(i) SLAP_EPOLL_EVENT_CHK((i), EPOLLIN)
265 # define SLAP_EVENT_IS_WRITE(i) SLAP_EPOLL_EVENT_CHK((i), EPOLLOUT)
266 # define SLAP_EVENT_IS_LISTENER(i) SLAP_EPOLL_EV_LISTENER(revents[(i)].data.ptr)
267 # define SLAP_EVENT_LISTENER(i) ((Listener *)(revents[(i)].data.ptr))
269 # define SLAP_EVENT_FD(i) SLAP_EPOLL_EV_PTRFD(revents[(i)].data.ptr)
271 # define SLAP_SOCK_INIT do { \
272 slap_daemon.sd_epolls = ch_calloc(1, \
273 ( sizeof(struct epoll_event) * 2 \
274 + sizeof(int) ) * dtblsize * 2); \
275 slap_daemon.sd_index = (int *)&slap_daemon.sd_epolls[ 2 * dtblsize ]; \
276 slap_daemon.sd_epfd = epoll_create( dtblsize ); \
277 for ( i = 0; i < dtblsize; i++ ) slap_daemon.sd_index[i] = -1; \
280 # define SLAP_SOCK_DESTROY do { \
281 if ( slap_daemon.sd_epolls != NULL ) { \
282 ch_free( slap_daemon.sd_epolls ); \
283 slap_daemon.sd_epolls = NULL; \
284 slap_daemon.sd_index = NULL; \
285 close( slap_daemon.sd_epfd ); \
289 # define SLAP_EVENT_DECL struct epoll_event *revents
291 # define SLAP_EVENT_INIT do { \
292 revents = slap_daemon.sd_epolls + dtblsize; \
295 # define SLAP_EVENT_WAIT(tvp, nsp) do { \
296 *(nsp) = epoll_wait( slap_daemon.sd_epfd, revents, \
297 dtblsize, (tvp) ? (tvp)->tv_sec * 1000 : -1 ); \
300 #elif defined(SLAP_X_DEVPOLL) && defined(HAVE_DEVPOLL)
302 /*************************************************************
303 * Use Solaris' (>= 2.7) /dev/poll infrastructure - poll(7d) *
304 *************************************************************/
305 # define SLAP_EVENT_FNAME "/dev/poll"
306 # define SLAP_EVENTS_ARE_INDEXED 0
308 * - sd_index is used much like with epoll()
309 * - sd_l is maintained as an array containing the address
310 * of the listener; the index is the fd itself
311 * - sd_pollfd is used to keep track of what data has been
312 * registered in /dev/poll
314 # define SLAP_DEVPOLL_SOCK_IX(s) (slap_daemon.sd_index[(s)])
315 # define SLAP_DEVPOLL_SOCK_LX(s) (slap_daemon.sd_l[(s)])
316 # define SLAP_DEVPOLL_SOCK_EP(s) (slap_daemon.sd_pollfd[SLAP_DEVPOLL_SOCK_IX((s))])
317 # define SLAP_DEVPOLL_SOCK_FD(s) (SLAP_DEVPOLL_SOCK_EP((s)).fd)
318 # define SLAP_DEVPOLL_SOCK_EV(s) (SLAP_DEVPOLL_SOCK_EP((s)).events)
319 # define SLAP_SOCK_IS_ACTIVE(s) (SLAP_DEVPOLL_SOCK_IX((s)) != -1)
320 # define SLAP_SOCK_NOT_ACTIVE(s) (SLAP_DEVPOLL_SOCK_IX((s)) == -1)
321 # define SLAP_SOCK_IS_SET(s, mode) (SLAP_DEVPOLL_SOCK_EV((s)) & (mode))
323 # define SLAP_SOCK_IS_READ(s) SLAP_SOCK_IS_SET((s), POLLIN)
324 # define SLAP_SOCK_IS_WRITE(s) SLAP_SOCK_IS_SET((s), POLLOUT)
326 /* as far as I understand, any time we need to communicate with the kernel
327 * about the number and/or properties of a file descriptor we need it to
328 * wait for, we have to rewrite the whole set */
329 # define SLAP_DEVPOLL_WRITE_POLLFD(s, pfd, n, what, shdn) do { \
331 size_t size = (n) * sizeof( struct pollfd ); \
332 /* FIXME: use pwrite? */ \
333 rc = write( slap_daemon.sd_dpfd, (pfd), size ); \
334 if ( rc != size ) { \
335 Debug( LDAP_DEBUG_ANY, "daemon: " SLAP_EVENT_FNAME ": " \
336 "%s fd=%d failed errno=%d\n", \
337 (what), (s), errno ); \
339 slapd_shutdown = 2; \
344 # define SLAP_DEVPOLL_SOCK_SET(s, mode) do { \
345 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_SET_%s(%d)=%d\n", \
346 (mode) == POLLIN ? "READ" : "WRITE", (s), \
347 ( (SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) != (mode) ) ); \
348 if ( (SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) != (mode) ) { \
350 SLAP_DEVPOLL_SOCK_EV((s)) |= (mode); \
351 pfd.fd = SLAP_DEVPOLL_SOCK_FD((s)); \
352 pfd.events = /* (mode) */ SLAP_DEVPOLL_SOCK_EV((s)); \
353 SLAP_DEVPOLL_WRITE_POLLFD((s), &pfd, 1, "SET", 0); \
357 # define SLAP_DEVPOLL_SOCK_CLR(s, mode) do { \
358 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_CLR_%s(%d)=%d\n", \
359 (mode) == POLLIN ? "READ" : "WRITE", (s), \
360 ( (SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) == (mode) ) ); \
361 if ((SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) == (mode) ) { \
362 struct pollfd pfd[2]; \
363 SLAP_DEVPOLL_SOCK_EV((s)) &= ~(mode); \
364 pfd[0].fd = SLAP_DEVPOLL_SOCK_FD((s)); \
365 pfd[0].events = POLLREMOVE; \
366 pfd[1] = SLAP_DEVPOLL_SOCK_EP((s)); \
367 SLAP_DEVPOLL_WRITE_POLLFD((s), &pfd[0], 2, "CLR", 0); \
371 # define SLAP_SOCK_SET_READ(s) SLAP_DEVPOLL_SOCK_SET(s, POLLIN)
372 # define SLAP_SOCK_SET_WRITE(s) SLAP_DEVPOLL_SOCK_SET(s, POLLOUT)
374 # define SLAP_SOCK_CLR_READ(s) SLAP_DEVPOLL_SOCK_CLR((s), POLLIN)
375 # define SLAP_SOCK_CLR_WRITE(s) SLAP_DEVPOLL_SOCK_CLR((s), POLLOUT)
377 # define SLAP_SOCK_SET_SUSPEND(s) \
378 ( slap_daemon.sd_suspend[SLAP_DEVPOLL_SOCK_IX((s))] = 1 )
379 # define SLAP_SOCK_CLR_SUSPEND(s) \
380 ( slap_daemon.sd_suspend[SLAP_DEVPOLL_SOCK_IX((s))] = 0 )
381 # define SLAP_SOCK_IS_SUSPEND(s) \
382 ( slap_daemon.sd_suspend[SLAP_DEVPOLL_SOCK_IX((s))] == 1 )
384 # define SLAP_DEVPOLL_EVENT_CLR(i, mode) (revents[(i)].events &= ~(mode))
386 # define SLAP_EVENT_MAX slap_daemon.sd_nfds
388 /* If a Listener address is provided, store that in the sd_l array.
389 * If we can't do this add, the system is out of resources and we
392 # define SLAP_SOCK_ADD(s, l) do { \
393 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_ADD(%d, %p)\n", (s), (l), 0 ); \
394 SLAP_DEVPOLL_SOCK_IX((s)) = slap_daemon.sd_nfds; \
395 SLAP_DEVPOLL_SOCK_LX((s)) = (l); \
396 SLAP_DEVPOLL_SOCK_FD((s)) = (s); \
397 SLAP_DEVPOLL_SOCK_EV((s)) = POLLIN; \
398 SLAP_DEVPOLL_WRITE_POLLFD((s), &SLAP_DEVPOLL_SOCK_EP((s)), 1, "ADD", 1); \
399 slap_daemon.sd_nfds++; \
402 # define SLAP_DEVPOLL_EV_LISTENER(ptr) ((ptr) != NULL)
404 # define SLAP_SOCK_DEL(s) do { \
405 int fd, index = SLAP_DEVPOLL_SOCK_IX((s)); \
406 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_DEL(%d)\n", (s), 0, 0 ); \
407 if ( index < 0 ) break; \
408 if ( index < slap_daemon.sd_nfds - 1 ) { \
409 struct pollfd pfd = slap_daemon.sd_pollfd[index]; \
410 fd = slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1].fd; \
411 slap_daemon.sd_pollfd[index] = slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1]; \
412 slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1] = pfd; \
413 slap_daemon.sd_index[fd] = index; \
415 slap_daemon.sd_index[(s)] = -1; \
416 slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1].events = POLLREMOVE; \
417 SLAP_DEVPOLL_WRITE_POLLFD((s), &slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1], 1, "DEL", 0); \
418 slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1].events = 0; \
419 slap_daemon.sd_nfds--; \
422 # define SLAP_EVENT_CLR_READ(i) SLAP_DEVPOLL_EVENT_CLR((i), POLLIN)
423 # define SLAP_EVENT_CLR_WRITE(i) SLAP_DEVPOLL_EVENT_CLR((i), POLLOUT)
425 # define SLAP_DEVPOLL_EVENT_CHK(i, mode) (revents[(i)].events & (mode))
427 # define SLAP_EVENT_FD(i) (revents[(i)].fd)
429 # define SLAP_EVENT_IS_READ(i) SLAP_DEVPOLL_EVENT_CHK((i), POLLIN)
430 # define SLAP_EVENT_IS_WRITE(i) SLAP_DEVPOLL_EVENT_CHK((i), POLLOUT)
431 # define SLAP_EVENT_IS_LISTENER(i) SLAP_DEVPOLL_EV_LISTENER(SLAP_DEVPOLL_SOCK_LX(SLAP_EVENT_FD((i))))
432 # define SLAP_EVENT_LISTENER(i) SLAP_DEVPOLL_SOCK_LX(SLAP_EVENT_FD((i)))
434 # define SLAP_SOCK_INIT do { \
435 slap_daemon.sd_pollfd = ch_calloc( 1, \
436 ( sizeof(struct pollfd) * 2 \
438 + sizeof( Listener * ) ) * dtblsize ); \
439 slap_daemon.sd_index = (int *)&slap_daemon.sd_pollfd[ 2 * dtblsize ]; \
440 slap_daemon.sd_l = (Listener **)&slap_daemon.sd_index[ dtblsize ]; \
441 slap_daemon.sd_dpfd = open( SLAP_EVENT_FNAME, O_RDWR ); \
442 if ( slap_daemon.sd_dpfd == -1 ) { \
443 Debug( LDAP_DEBUG_ANY, "daemon: " SLAP_EVENT_FNAME ": " \
444 "open(\"" SLAP_EVENT_FNAME "\") failed errno=%d\n", \
449 for ( i = 0; i < dtblsize; i++ ) { \
450 slap_daemon.sd_pollfd[i].fd = -1; \
451 slap_daemon.sd_index[i] = -1; \
455 # define SLAP_SOCK_DESTROY do { \
456 if ( slap_daemon.sd_pollfd != NULL ) { \
457 ch_free( slap_daemon.sd_pollfd ); \
458 slap_daemon.sd_pollfd = NULL; \
459 slap_daemon.sd_index = NULL; \
460 slap_daemon.sd_l = NULL; \
461 close( slap_daemon.sd_dpfd ); \
465 # define SLAP_EVENT_DECL struct pollfd *revents
467 # define SLAP_EVENT_INIT do { \
468 revents = &slap_daemon.sd_pollfd[ dtblsize ]; \
471 # define SLAP_EVENT_WAIT(tvp, nsp) do { \
472 struct dvpoll sd_dvpoll; \
473 sd_dvpoll.dp_timeout = (tvp) ? (tvp)->tv_sec * 1000 : -1; \
474 sd_dvpoll.dp_nfds = dtblsize; \
475 sd_dvpoll.dp_fds = revents; \
476 *(nsp) = ioctl( slap_daemon.sd_dpfd, DP_POLL, &sd_dvpoll ); \
479 #else /* ! epoll && ! /dev/poll */
481 # define SLAP_EVENT_FNAME "WSselect"
482 /* Winsock provides a "select" function but its fd_sets are
483 * actually arrays of sockets. Since these sockets are handles
484 * and not a contiguous range of small integers, we manage our
485 * own "fd" table of socket handles and use their indices as
488 * All of our listener/connection structures use fds; the actual
489 * I/O functions use sockets. The SLAP_FD2SOCK macro in proto-slap.h
490 * handles the mapping.
492 * Despite the mapping overhead, this is about 45% more efficient
493 * than just using Winsock's select and FD_ISSET directly.
495 * Unfortunately Winsock's select implementation doesn't scale well
496 * as the number of connections increases. This probably needs to be
497 * rewritten to use the Winsock overlapped/asynchronous I/O functions.
499 # define SLAP_EVENTS_ARE_INDEXED 1
500 # define SLAP_EVENT_DECL fd_set readfds, writefds
501 # define SLAP_EVENT_INIT do { \
503 FD_ZERO( &readfds ); \
504 FD_ZERO( &writefds ); \
505 memset( slap_daemon.sd_rflags, 0, slap_daemon.sd_nfds ); \
506 for ( i=0; i<slap_daemon.sd_nfds; i++ ) { \
507 if ( slap_daemon.sd_flags[i] & SD_READ ) \
508 FD_SET( slapd_ws_sockets[i], &readfds );\
509 if ( slap_daemon.sd_flags[i] & SD_WRITE ) \
510 FD_SET( slapd_ws_sockets[i], &writefds ); \
513 # define SLAP_EVENT_MAX slap_daemon.sd_nfds
515 # define SLAP_EVENT_WAIT(tvp, nsp) do { \
517 *(nsp) = select( SLAP_EVENT_MAX, &readfds, \
518 nwriters > 0 ? &writefds : NULL, NULL, (tvp) ); \
519 for ( i=0; i<readfds.fd_count; i++) { \
520 int fd = slapd_sock2fd(readfds.fd_array[i]); \
522 slap_daemon.sd_rflags[fd] = SD_READ; \
523 if ( fd >= *(nsp)) *(nsp) = fd+1; \
526 for ( i=0; i<writefds.fd_count; i++) { \
527 int fd = slapd_sock2fd(writefds.fd_array[i]); \
529 slap_daemon.sd_rflags[fd] = SD_WRITE; \
530 if ( fd >= *(nsp)) *(nsp) = fd+1; \
535 # define SLAP_EVENT_IS_READ(fd) (slap_daemon.sd_rflags[fd] & SD_READ)
536 # define SLAP_EVENT_IS_WRITE(fd) (slap_daemon.sd_rflags[fd] & SD_WRITE)
538 # define SLAP_EVENT_CLR_READ(fd) slap_daemon.sd_rflags[fd] &= ~SD_READ;
539 # define SLAP_EVENT_CLR_WRITE(fd) slap_daemon.sd_rflags[fd] &= ~SD_WRITE;
541 # define SLAP_SOCK_INIT do { \
542 ldap_pvt_thread_mutex_init( &slapd_ws_mutex ); \
543 slapd_ws_sockets = ch_malloc( dtblsize * ( sizeof(SOCKET) + 2)); \
544 slap_daemon.sd_flags = (char *)(slapd_ws_sockets + dtblsize); \
545 slap_daemon.sd_rflags = slap_daemon.sd_flags + dtblsize; \
546 memset( slapd_ws_sockets, -1, dtblsize * sizeof(SOCKET) ); \
547 memset( slap_daemon.sd_flags, 0, dtblsize ); \
548 slapd_ws_sockets[0] = wake_sds[0]; \
549 slapd_ws_sockets[1] = wake_sds[1]; \
552 slap_daemon.sd_nfds = 2; \
555 # define SLAP_SOCK_DESTROY do { \
556 ch_free( slapd_ws_sockets ); slapd_ws_sockets = NULL; \
557 slap_daemon.sd_flags = NULL; \
558 slap_daemon.sd_rflags = NULL; \
559 ldap_pvt_thread_mutex_destroy( &slapd_ws_mutex ); \
562 # define SLAP_SOCK_IS_ACTIVE(fd) ( slap_daemon.sd_flags[fd] & SD_ACTIVE )
563 # define SLAP_SOCK_IS_READ(fd) ( slap_daemon.sd_flags[fd] & SD_READ )
564 # define SLAP_SOCK_IS_WRITE(fd) ( slap_daemon.sd_flags[fd] & SD_WRITE )
565 # define SLAP_SOCK_NOT_ACTIVE(fd) (!slap_daemon.sd_flags[fd])
567 # define SLAP_SOCK_SET_READ(fd) ( slap_daemon.sd_flags[fd] |= SD_READ )
568 # define SLAP_SOCK_SET_WRITE(fd) ( slap_daemon.sd_flags[fd] |= SD_WRITE )
570 # define SLAP_SELECT_ADDTEST(s) do { \
571 if ((s) >= slap_daemon.sd_nfds) slap_daemon.sd_nfds = (s)+1; \
574 # define SLAP_SOCK_CLR_READ(fd) ( slap_daemon.sd_flags[fd] &= ~SD_READ )
575 # define SLAP_SOCK_CLR_WRITE(fd) ( slap_daemon.sd_flags[fd] &= ~SD_WRITE )
577 # define SLAP_SOCK_ADD(s, l) do { \
578 SLAP_SELECT_ADDTEST((s)); \
579 slap_daemon.sd_flags[s] = SD_ACTIVE|SD_READ; \
582 # define SLAP_SOCK_DEL(s) do { \
583 slap_daemon.sd_flags[s] = 0; \
584 slapd_sockdel( s ); \
587 # else /* !HAVE_WINSOCK */
589 /**************************************
590 * Use select system call - select(2) *
591 **************************************/
592 # define SLAP_EVENT_FNAME "select"
594 # define SLAP_EVENTS_ARE_INDEXED 1
595 # define SLAP_EVENT_DECL fd_set readfds, writefds
597 # define SLAP_EVENT_INIT do { \
598 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) ); \
600 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) ); \
602 FD_ZERO( &writefds ); \
607 # define SLAP_SELECT_CHK_SETSIZE do { \
608 if (dtblsize > FD_SETSIZE) dtblsize = FD_SETSIZE; \
610 # else /* ! FD_SETSIZE */
611 # define SLAP_SELECT_CHK_SETSIZE do { ; } while (0)
612 # endif /* ! FD_SETSIZE */
614 # define SLAP_SOCK_INIT do { \
615 SLAP_SELECT_CHK_SETSIZE; \
616 FD_ZERO(&slap_daemon.sd_actives); \
617 FD_ZERO(&slap_daemon.sd_readers); \
618 FD_ZERO(&slap_daemon.sd_writers); \
621 # define SLAP_SOCK_DESTROY
623 # define SLAP_SOCK_IS_ACTIVE(fd) FD_ISSET((fd), &slap_daemon.sd_actives)
624 # define SLAP_SOCK_IS_READ(fd) FD_ISSET((fd), &slap_daemon.sd_readers)
625 # define SLAP_SOCK_IS_WRITE(fd) FD_ISSET((fd), &slap_daemon.sd_writers)
627 # define SLAP_SOCK_NOT_ACTIVE(fd) (!SLAP_SOCK_IS_ACTIVE(fd) && \
628 !SLAP_SOCK_IS_READ(fd) && !SLAP_SOCK_IS_WRITE(fd))
630 # define SLAP_SOCK_SET_READ(fd) FD_SET((fd), &slap_daemon.sd_readers)
631 # define SLAP_SOCK_SET_WRITE(fd) FD_SET((fd), &slap_daemon.sd_writers)
633 # define SLAP_EVENT_MAX slap_daemon.sd_nfds
634 # define SLAP_SELECT_ADDTEST(s) do { \
635 if ((s) >= slap_daemon.sd_nfds) slap_daemon.sd_nfds = (s)+1; \
638 # define SLAP_SOCK_CLR_READ(fd) FD_CLR((fd), &slap_daemon.sd_readers)
639 # define SLAP_SOCK_CLR_WRITE(fd) FD_CLR((fd), &slap_daemon.sd_writers)
641 # define SLAP_SOCK_ADD(s, l) do { \
642 SLAP_SELECT_ADDTEST((s)); \
643 FD_SET((s), &slap_daemon.sd_actives); \
644 FD_SET((s), &slap_daemon.sd_readers); \
647 # define SLAP_SOCK_DEL(s) do { \
648 FD_CLR((s), &slap_daemon.sd_actives); \
649 FD_CLR((s), &slap_daemon.sd_readers); \
650 FD_CLR((s), &slap_daemon.sd_writers); \
653 # define SLAP_EVENT_IS_READ(fd) FD_ISSET((fd), &readfds)
654 # define SLAP_EVENT_IS_WRITE(fd) FD_ISSET((fd), &writefds)
656 # define SLAP_EVENT_CLR_READ(fd) FD_CLR((fd), &readfds)
657 # define SLAP_EVENT_CLR_WRITE(fd) FD_CLR((fd), &writefds)
659 # define SLAP_EVENT_WAIT(tvp, nsp) do { \
660 *(nsp) = select( SLAP_EVENT_MAX, &readfds, \
661 nwriters > 0 ? &writefds : NULL, NULL, (tvp) ); \
663 # endif /* !HAVE_WINSOCK */
664 #endif /* ! epoll && ! /dev/poll */
668 * SLP related functions
672 #define LDAP_SRVTYPE_PREFIX "service:ldap://"
673 #define LDAPS_SRVTYPE_PREFIX "service:ldaps://"
674 static char** slapd_srvurls = NULL;
675 static SLPHandle slapd_hslp = 0;
676 int slapd_register_slp = 0;
677 const char *slapd_slp_attrs = NULL;
679 static SLPError slapd_slp_cookie;
682 slapd_slp_init( const char* urls )
687 slapd_srvurls = ldap_str2charray( urls, " " );
689 if ( slapd_srvurls == NULL ) return;
691 /* find and expand INADDR_ANY URLs */
692 for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
693 if ( strcmp( slapd_srvurls[i], "ldap:///" ) == 0 ) {
694 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
695 global_host_bv.bv_len +
696 sizeof( LDAP_SRVTYPE_PREFIX ) );
697 strcpy( lutil_strcopy(slapd_srvurls[i],
698 LDAP_SRVTYPE_PREFIX ), global_host_bv.bv_val );
699 } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0 ) {
700 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
701 global_host_bv.bv_len +
702 sizeof( LDAPS_SRVTYPE_PREFIX ) );
703 strcpy( lutil_strcopy(slapd_srvurls[i],
704 LDAPS_SRVTYPE_PREFIX ), global_host_bv.bv_val );
708 /* open the SLP handle */
709 err = SLPOpen( "en", 0, &slapd_hslp );
711 if ( err != SLP_OK ) {
712 Debug( LDAP_DEBUG_CONNS, "daemon: SLPOpen() failed with %ld\n",
718 slapd_slp_deinit( void )
720 if ( slapd_srvurls == NULL ) return;
722 ldap_charray_free( slapd_srvurls );
723 slapd_srvurls = NULL;
725 /* close the SLP handle */
726 SLPClose( slapd_hslp );
735 /* return the error code in the cookie */
736 *(SLPError*)cookie = errcode;
745 if ( slapd_srvurls == NULL ) return;
747 for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
748 if ( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
749 sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
750 strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
751 sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
753 err = SLPReg( slapd_hslp,
755 SLP_LIFETIME_MAXIMUM,
757 (slapd_slp_attrs) ? slapd_slp_attrs : "",
762 if ( err != SLP_OK || slapd_slp_cookie != SLP_OK ) {
763 Debug( LDAP_DEBUG_CONNS,
764 "daemon: SLPReg(%s) failed with %ld, cookie = %ld\n",
765 slapd_srvurls[i], (long)err, (long)slapd_slp_cookie );
772 slapd_slp_dereg( void )
777 if ( slapd_srvurls == NULL ) return;
779 for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
780 err = SLPDereg( slapd_hslp,
785 if ( err != SLP_OK || slapd_slp_cookie != SLP_OK ) {
786 Debug( LDAP_DEBUG_CONNS,
787 "daemon: SLPDereg(%s) failed with %ld, cookie = %ld\n",
788 slapd_srvurls[i], (long)err, (long)slapd_slp_cookie );
792 #endif /* HAVE_SLP */
795 /* Manage the descriptor to socket table */
797 slapd_socknew( ber_socket_t s )
800 ldap_pvt_thread_mutex_lock( &slapd_ws_mutex );
801 for ( i = 0; i < dtblsize && slapd_ws_sockets[i] != INVALID_SOCKET; i++ );
802 if ( i == dtblsize ) {
803 WSASetLastError( WSAEMFILE );
805 slapd_ws_sockets[i] = s;
807 ldap_pvt_thread_mutex_unlock( &slapd_ws_mutex );
812 slapd_sockdel( ber_socket_t s )
814 ldap_pvt_thread_mutex_lock( &slapd_ws_mutex );
815 slapd_ws_sockets[s] = INVALID_SOCKET;
816 ldap_pvt_thread_mutex_unlock( &slapd_ws_mutex );
820 slapd_sock2fd( ber_socket_t s )
823 for ( i=0; i<dtblsize && slapd_ws_sockets[i] != s; i++);
831 * Add a descriptor to daemon control
833 * If isactive, the descriptor is a live server session and is subject
834 * to idletimeout control. Otherwise, the descriptor is a passive
835 * listener or an outbound client session, and not subject to
836 * idletimeout. The underlying event handler may record the Listener
837 * argument to differentiate Listener's from real sessions.
840 slapd_add( ber_socket_t s, int isactive, Listener *sl )
842 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
844 assert( SLAP_SOCK_NOT_ACTIVE(s) );
846 if ( isactive ) slap_daemon.sd_nactives++;
848 SLAP_SOCK_ADD(s, sl);
850 Debug( LDAP_DEBUG_CONNS, "daemon: added %ldr%s listener=%p\n",
851 (long) s, isactive ? " (active)" : "", (void *)sl );
853 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
862 slapd_sd_lock( void )
864 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
871 slapd_sd_unlock( void )
873 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
877 * Remove the descriptor from daemon control
891 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
893 assert( SLAP_SOCK_IS_ACTIVE( s ));
895 if ( wasactive ) slap_daemon.sd_nactives--;
897 waswriter = SLAP_SOCK_IS_WRITE(s);
898 wasreader = SLAP_SOCK_IS_READ(s);
900 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
902 wasreader ? "r" : "",
903 waswriter ? "w" : "" );
905 if ( waswriter ) slap_daemon.sd_nwriters--;
910 ber_sockbuf_free(sb);
912 /* If we ran out of file descriptors, we dropped a listener from
913 * the select() loop. Now that we're removing a session from our
914 * control, we can try to resume a dropped listener to use.
918 for ( i = 0; slap_listeners[i] != NULL; i++ ) {
919 Listener *lr = slap_listeners[i];
921 if ( lr->sl_sd == AC_SOCKET_INVALID ) continue;
922 if ( lr->sl_sd == s ) continue;
929 /* Walked the entire list without enabling anything; emfile
930 * counter is stale. Reset it.
932 if ( slap_listeners[i] == NULL ) emfile = 0;
934 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
935 WAKE_LISTENER(wake || slapd_gentle_shutdown == 2);
939 slapd_clr_write( ber_socket_t s, int wake )
941 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
943 if ( SLAP_SOCK_IS_WRITE( s )) {
944 assert( SLAP_SOCK_IS_ACTIVE( s ));
946 SLAP_SOCK_CLR_WRITE( s );
947 slap_daemon.sd_nwriters--;
950 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
955 slapd_set_write( ber_socket_t s, int wake )
957 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
959 assert( SLAP_SOCK_IS_ACTIVE( s ));
961 if ( !SLAP_SOCK_IS_WRITE( s )) {
962 SLAP_SOCK_SET_WRITE( s );
963 slap_daemon.sd_nwriters++;
965 if (( wake & 2 ) && global_writetimeout && !chk_writetime ) {
966 chk_writetime = slap_get_time();
969 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
974 slapd_clr_read( ber_socket_t s, int wake )
977 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
979 if ( SLAP_SOCK_IS_ACTIVE( s )) {
980 SLAP_SOCK_CLR_READ( s );
983 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
990 slapd_set_read( ber_socket_t s, int wake )
993 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
995 if( SLAP_SOCK_IS_ACTIVE( s ) && !SLAP_SOCK_IS_READ( s )) {
996 SLAP_SOCK_SET_READ( s );
1000 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1002 WAKE_LISTENER(wake);
1006 slapd_get_writetime()
1009 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1010 cur = chk_writetime;
1011 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1016 slapd_clr_writetime( time_t old )
1018 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1019 if ( chk_writetime == old )
1021 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1025 slapd_close( ber_socket_t s )
1027 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
1029 tcp_close( SLAP_FD2SOCK(s) );
1036 slap_free_listener_addresses( struct sockaddr **sal )
1038 struct sockaddr **sap;
1039 if (sal == NULL) return;
1040 for (sap = sal; *sap != NULL; sap++) ch_free(*sap);
1044 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
1053 assert( exts != NULL );
1054 assert( perms != NULL );
1055 assert( crit != NULL );
1058 for ( i = 0; exts[ i ]; i++ ) {
1059 char *type = exts[ i ];
1062 if ( type[ 0 ] == '!' ) {
1067 if ( strncasecmp( type, LDAPI_MOD_URLEXT "=",
1068 sizeof(LDAPI_MOD_URLEXT "=") - 1 ) == 0 )
1070 char *value = type + ( sizeof(LDAPI_MOD_URLEXT "=") - 1 );
1074 switch (strlen(value)) {
1076 /* skip leading '0' */
1077 if ( value[ 0 ] != '0' ) return LDAP_OTHER;
1081 for ( j = 0; j < 3; j++) {
1084 v = value[ j ] - '0';
1086 if ( v < 0 || v > 7 ) return LDAP_OTHER;
1093 for ( j = 1; j < 10; j++ ) {
1094 static mode_t m[] = { 0,
1095 S_IRUSR, S_IWUSR, S_IXUSR,
1096 S_IRGRP, S_IWGRP, S_IXGRP,
1097 S_IROTH, S_IWOTH, S_IXOTH
1099 static const char c[] = "-rwxrwxrwx";
1101 if ( value[ j ] == c[ j ] ) {
1104 } else if ( value[ j ] != '-' ) {
1117 return LDAP_SUCCESS;
1123 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
1125 /* port = 0 indicates AF_LOCAL */
1127 slap_get_listener_addresses(
1129 unsigned short port,
1130 struct sockaddr ***sal )
1132 struct sockaddr **sap;
1134 #ifdef LDAP_PF_LOCAL
1136 *sal = ch_malloc(2 * sizeof(void *));
1137 if (*sal == NULL) return -1;
1140 *sap = ch_malloc(sizeof(struct sockaddr_un));
1141 if (*sap == NULL) goto errexit;
1145 (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) )
1147 Debug( LDAP_DEBUG_ANY,
1148 "daemon: domain socket path (%s) too long in URL",
1153 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
1154 (*sap)->sa_family = AF_LOCAL;
1155 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
1157 #endif /* LDAP_PF_LOCAL */
1159 #ifdef HAVE_GETADDRINFO
1160 struct addrinfo hints, *res, *sai;
1164 memset( &hints, '\0', sizeof(hints) );
1165 hints.ai_flags = AI_PASSIVE;
1166 hints.ai_socktype = SOCK_STREAM;
1167 hints.ai_family = slap_inet4or6;
1168 snprintf(serv, sizeof serv, "%d", port);
1170 if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
1171 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo() failed: %s\n",
1172 AC_GAI_STRERROR(err), 0, 0);
1177 for (n=2; (sai = sai->ai_next) != NULL; n++) {
1180 *sal = ch_calloc(n, sizeof(void *));
1181 if (*sal == NULL) return -1;
1186 for ( sai=res; sai; sai=sai->ai_next ) {
1187 if( sai->ai_addr == NULL ) {
1188 Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
1189 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
1194 switch (sai->ai_family) {
1195 # ifdef LDAP_PF_INET6
1197 *sap = ch_malloc(sizeof(struct sockaddr_in6));
1202 *(struct sockaddr_in6 *)*sap =
1203 *((struct sockaddr_in6 *)sai->ai_addr);
1205 # endif /* LDAP_PF_INET6 */
1207 *sap = ch_malloc(sizeof(struct sockaddr_in));
1212 *(struct sockaddr_in *)*sap =
1213 *((struct sockaddr_in *)sai->ai_addr);
1221 (*sap)->sa_family = sai->ai_family;
1229 #else /* ! HAVE_GETADDRINFO */
1232 struct hostent *he = NULL;
1234 if ( host == NULL ) {
1235 in.s_addr = htonl(INADDR_ANY);
1237 } else if ( !inet_aton( host, &in ) ) {
1238 he = gethostbyname( host );
1240 Debug( LDAP_DEBUG_ANY,
1241 "daemon: invalid host %s", host, 0, 0);
1244 for (n = 0; he->h_addr_list[n]; n++) /* empty */;
1247 *sal = ch_malloc((n+1) * sizeof(void *));
1248 if (*sal == NULL) return -1;
1251 for ( i = 0; i<n; i++ ) {
1252 sap[i] = ch_malloc(sizeof(struct sockaddr_in));
1253 if (*sap == NULL) goto errexit;
1255 (void)memset( (void *)sap[i], '\0', sizeof(struct sockaddr_in) );
1256 sap[i]->sa_family = AF_INET;
1257 ((struct sockaddr_in *)sap[i])->sin_port = htons(port);
1258 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr,
1259 he ? (struct in_addr *)he->h_addr_list[i] : &in,
1260 sizeof(struct in_addr) );
1263 #endif /* ! HAVE_GETADDRINFO */
1269 slap_free_listener_addresses(*sal);
1283 unsigned short port;
1284 int err, addrlen = 0;
1285 struct sockaddr **sal, **psal;
1286 int socktype = SOCK_STREAM; /* default to COTS */
1289 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
1294 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
1296 rc = ldap_url_parse( url, &lud );
1298 if( rc != LDAP_URL_SUCCESS ) {
1299 Debug( LDAP_DEBUG_ANY,
1300 "daemon: listen URL \"%s\" parse error=%d\n",
1305 l.sl_url.bv_val = NULL;
1310 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
1311 Debug( LDAP_DEBUG_ANY, "daemon: TLS not supported (%s)\n",
1313 ldap_free_urldesc( lud );
1317 if(! lud->lud_port ) lud->lud_port = LDAP_PORT;
1319 #else /* HAVE_TLS */
1320 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
1322 if(! lud->lud_port ) {
1323 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
1325 #endif /* HAVE_TLS */
1327 #ifdef LDAP_TCP_BUFFER
1330 #endif /* LDAP_TCP_BUFFER */
1332 port = (unsigned short) lud->lud_port;
1334 tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme);
1335 if ( tmp == LDAP_PROTO_IPC ) {
1336 #ifdef LDAP_PF_LOCAL
1337 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
1338 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
1340 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
1342 #else /* ! LDAP_PF_LOCAL */
1344 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
1346 ldap_free_urldesc( lud );
1348 #endif /* ! LDAP_PF_LOCAL */
1350 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
1351 || strcmp(lud->lud_host, "*") == 0 )
1353 err = slap_get_listener_addresses(NULL, port, &sal);
1355 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
1359 #ifdef LDAP_CONNECTIONLESS
1360 l.sl_is_udp = ( tmp == LDAP_PROTO_UDP );
1361 #endif /* LDAP_CONNECTIONLESS */
1363 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
1364 if ( lud->lud_exts ) {
1365 err = get_url_perms( lud->lud_exts, &l.sl_perms, &crit );
1367 l.sl_perms = S_IRWXU | S_IRWXO;
1369 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
1371 ldap_free_urldesc( lud );
1372 if ( err ) return -1;
1374 /* If we got more than one address returned, we need to make space
1375 * for it in the slap_listeners array.
1377 for ( num=0; sal[num]; num++ ) /* empty */;
1379 *listeners += num-1;
1380 slap_listeners = ch_realloc( slap_listeners,
1381 (*listeners + 1) * sizeof(Listener *) );
1385 while ( *sal != NULL ) {
1387 switch( (*sal)->sa_family ) {
1391 #ifdef LDAP_PF_INET6
1395 #endif /* LDAP_PF_INET6 */
1396 #ifdef LDAP_PF_LOCAL
1400 #endif /* LDAP_PF_LOCAL */
1406 #ifdef LDAP_CONNECTIONLESS
1407 if( l.sl_is_udp ) socktype = SOCK_DGRAM;
1408 #endif /* LDAP_CONNECTIONLESS */
1410 s = socket( (*sal)->sa_family, socktype, 0);
1411 if ( s == AC_SOCKET_INVALID ) {
1412 int err = sock_errno();
1413 Debug( LDAP_DEBUG_ANY,
1414 "daemon: %s socket() failed errno=%d (%s)\n",
1415 af, err, sock_errstr(err) );
1419 l.sl_sd = SLAP_SOCKNEW( s );
1421 if ( l.sl_sd >= dtblsize ) {
1422 Debug( LDAP_DEBUG_ANY,
1423 "daemon: listener descriptor %ld is too great %ld\n",
1424 (long) l.sl_sd, (long) dtblsize, 0 );
1430 #ifdef LDAP_PF_LOCAL
1431 if ( (*sal)->sa_family == AF_LOCAL ) {
1432 unlink( ((struct sockaddr_un *)*sal)->sun_path );
1434 #endif /* LDAP_PF_LOCAL */
1437 /* enable address reuse */
1439 rc = setsockopt( s, SOL_SOCKET, SO_REUSEADDR,
1440 (char *) &tmp, sizeof(tmp) );
1441 if ( rc == AC_SOCKET_ERROR ) {
1442 int err = sock_errno();
1443 Debug( LDAP_DEBUG_ANY, "slapd(%ld): "
1444 "setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
1445 (long) l.sl_sd, err, sock_errstr(err) );
1447 #endif /* SO_REUSEADDR */
1450 switch( (*sal)->sa_family ) {
1452 addrlen = sizeof(struct sockaddr_in);
1454 #ifdef LDAP_PF_INET6
1457 /* Try to use IPv6 sockets for IPv6 only */
1459 rc = setsockopt( s , IPPROTO_IPV6, IPV6_V6ONLY,
1460 (char *) &tmp, sizeof(tmp) );
1461 if ( rc == AC_SOCKET_ERROR ) {
1462 int err = sock_errno();
1463 Debug( LDAP_DEBUG_ANY, "slapd(%ld): "
1464 "setsockopt(IPV6_V6ONLY) failed errno=%d (%s)\n",
1465 (long) l.sl_sd, err, sock_errstr(err) );
1467 #endif /* IPV6_V6ONLY */
1468 addrlen = sizeof(struct sockaddr_in6);
1470 #endif /* LDAP_PF_INET6 */
1472 #ifdef LDAP_PF_LOCAL
1477 setsockopt( s, 0, LOCAL_CREDS, &one, sizeof( one ) );
1479 #endif /* LOCAL_CREDS */
1481 addrlen = sizeof( struct sockaddr_un );
1483 #endif /* LDAP_PF_LOCAL */
1486 #ifdef LDAP_PF_LOCAL
1487 /* create socket with all permissions set for those systems
1488 * that honor permissions on sockets (e.g. Linux); typically,
1489 * only write is required. To exploit filesystem permissions,
1490 * place the socket in a directory and use directory's
1491 * permissions. Need write perms to the directory to
1492 * create/unlink the socket; likely need exec perms to access
1493 * the socket (ITS#4709) */
1495 mode_t old_umask = 0;
1497 if ( (*sal)->sa_family == AF_LOCAL ) {
1498 old_umask = umask( 0 );
1500 #endif /* LDAP_PF_LOCAL */
1501 rc = bind( s, *sal, addrlen );
1502 #ifdef LDAP_PF_LOCAL
1503 if ( old_umask != 0 ) {
1507 #endif /* LDAP_PF_LOCAL */
1510 Debug( LDAP_DEBUG_ANY,
1511 "daemon: bind(%ld) failed errno=%d (%s)\n",
1512 (long)l.sl_sd, err, sock_errstr( err ) );
1518 switch ( (*sal)->sa_family ) {
1519 #ifdef LDAP_PF_LOCAL
1521 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
1522 l.sl_name.bv_len = strlen(addr) + sizeof("PATH=") - 1;
1523 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len + 1 );
1524 snprintf( l.sl_name.bv_val, l.sl_name.bv_len + 1,
1527 #endif /* LDAP_PF_LOCAL */
1531 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
1532 char addr[INET_ADDRSTRLEN];
1533 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
1534 addr, sizeof(addr) );
1536 #else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1537 s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr );
1538 #endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1539 port = ntohs( ((struct sockaddr_in *)*sal) ->sin_port );
1541 ber_memalloc( sizeof("IP=255.255.255.255:65535") );
1542 snprintf( l.sl_name.bv_val, sizeof("IP=255.255.255.255:65535"),
1544 s != NULL ? s : SLAP_STRING_UNKNOWN, port );
1545 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1548 #ifdef LDAP_PF_INET6
1550 char addr[INET6_ADDRSTRLEN];
1551 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
1553 port = ntohs( ((struct sockaddr_in6 *)*sal)->sin6_port );
1554 l.sl_name.bv_len = strlen(addr) + sizeof("IP=[]:65535");
1555 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len );
1556 snprintf( l.sl_name.bv_val, l.sl_name.bv_len, "IP=[%s]:%d",
1558 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1560 #endif /* LDAP_PF_INET6 */
1563 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
1564 (int) (*sal)->sa_family, 0, 0 );
1568 AC_MEMCPY(&l.sl_sa, *sal, addrlen);
1569 ber_str2bv( url, 0, 1, &l.sl_url);
1570 li = ch_malloc( sizeof( Listener ) );
1572 slap_listeners[*cur] = li;
1577 slap_free_listener_addresses(psal);
1579 if ( l.sl_url.bv_val == NULL ) {
1580 Debug( LDAP_DEBUG_TRACE,
1581 "slap_open_listener: failed on %s\n", url, 0, 0 );
1585 Debug( LDAP_DEBUG_TRACE, "daemon: listener initialized %s\n",
1586 l.sl_url.bv_val, 0, 0 );
1590 static int sockinit(void);
1591 static int sockdestroy(void);
1594 slapd_daemon_init( const char *urls )
1599 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
1600 urls ? urls : "<null>", 0, 0 );
1602 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
1604 ldap_pvt_thread_mutex_init( &slap_daemon.sd_tcpd_mutex );
1605 #endif /* TCP Wrappers */
1607 if( (rc = sockinit()) != 0 ) return rc;
1610 dtblsize = sysconf( _SC_OPEN_MAX );
1611 #elif defined(HAVE_GETDTABLESIZE)
1612 dtblsize = getdtablesize();
1613 #else /* ! HAVE_SYSCONF && ! HAVE_GETDTABLESIZE */
1614 dtblsize = FD_SETSIZE;
1615 #endif /* ! HAVE_SYSCONF && ! HAVE_GETDTABLESIZE */
1617 /* open a pipe (or something equivalent connected to itself).
1618 * we write a byte on this fd whenever we catch a signal. The main
1619 * loop will be select'ing on this socket, and will wake up when
1620 * this byte arrives.
1622 if( (rc = lutil_pair( wake_sds )) < 0 ) {
1623 Debug( LDAP_DEBUG_ANY,
1624 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
1627 ber_pvt_socket_set_nonblock( wake_sds[1], 1 );
1631 if( urls == NULL ) urls = "ldap:///";
1633 u = ldap_str2charray( urls, " " );
1635 if( u == NULL || u[0] == NULL ) {
1636 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
1639 ldap_charray_free( u );
1643 for( i=0; u[i] != NULL; i++ ) {
1644 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
1649 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
1651 ldap_charray_free( u );
1655 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
1657 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
1659 for(n = 0, j = 0; u[n]; n++ ) {
1660 if ( slap_open_listener( u[n], &i, &j ) ) {
1661 ldap_charray_free( u );
1665 slap_listeners[j] = NULL;
1667 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
1672 if( slapd_register_slp ) {
1673 slapd_slp_init( urls );
1676 #endif /* HAVE_SLP */
1678 ldap_charray_free( u );
1685 slapd_daemon_destroy( void )
1687 connections_destroy();
1689 if ( wake_sds[1] != INVALID_SOCKET &&
1690 SLAP_FD2SOCK( wake_sds[1] ) != SLAP_FD2SOCK( wake_sds[0] ))
1691 #endif /* HAVE_WINSOCK */
1692 tcp_close( SLAP_FD2SOCK(wake_sds[1]) );
1694 if ( wake_sds[0] != INVALID_SOCKET )
1695 #endif /* HAVE_WINSOCK */
1696 tcp_close( SLAP_FD2SOCK(wake_sds[0]) );
1700 if( slapd_register_slp ) {
1704 #endif /* HAVE_SLP */
1707 ldap_pvt_thread_mutex_destroy( &slap_daemon.sd_tcpd_mutex );
1708 #endif /* TCP Wrappers */
1710 ldap_pvt_thread_mutex_destroy( &slap_daemon.sd_mutex );
1721 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1722 Listener *lr = slap_listeners[l];
1723 slap_listeners[l] = NULL;
1725 if ( lr->sl_sd != AC_SOCKET_INVALID ) {
1727 lr->sl_sd = AC_SOCKET_INVALID;
1728 if ( remove ) slapd_remove( s, NULL, 0, 0, 0 );
1730 #ifdef LDAP_PF_LOCAL
1731 if ( lr->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1732 unlink( lr->sl_sa.sa_un_addr.sun_path );
1734 #endif /* LDAP_PF_LOCAL */
1739 if ( lr->sl_url.bv_val ) {
1740 ber_memfree( lr->sl_url.bv_val );
1743 if ( lr->sl_name.bv_val ) {
1744 ber_memfree( lr->sl_name.bv_val );
1757 ber_socket_t s, sfd;
1758 ber_socklen_t len = sizeof(from);
1761 struct berval authid = BER_BVNULL;
1762 #ifdef SLAPD_RLOOKUPS
1763 char hbuf[NI_MAXHOST];
1764 #endif /* SLAPD_RLOOKUPS */
1766 char *dnsname = NULL;
1767 char *peeraddr = NULL;
1768 #ifdef LDAP_PF_LOCAL
1769 char peername[MAXPATHLEN + sizeof("PATH=")];
1770 #ifdef LDAP_PF_LOCAL_SENDMSG
1772 struct berval peerbv = BER_BVNULL;
1774 #elif defined(LDAP_PF_INET6)
1775 char peername[sizeof("IP=[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535")];
1776 #else /* ! LDAP_PF_LOCAL && ! LDAP_PF_INET6 */
1777 char peername[sizeof("IP=255.255.255.255:65336")];
1778 #endif /* LDAP_PF_LOCAL */
1781 Debug( LDAP_DEBUG_TRACE,
1782 ">>> slap_listener(%s)\n",
1783 sl->sl_url.bv_val, 0, 0 );
1787 #ifdef LDAP_CONNECTIONLESS
1788 if ( sl->sl_is_udp ) return 1;
1789 #endif /* LDAP_CONNECTIONLESS */
1791 # ifdef LDAP_PF_LOCAL
1792 /* FIXME: apparently accept doesn't fill
1793 * the sun_path sun_path member */
1794 from.sa_un_addr.sun_path[0] = '\0';
1795 # endif /* LDAP_PF_LOCAL */
1797 s = accept( SLAP_FD2SOCK( sl->sl_sd ), (struct sockaddr *) &from, &len );
1799 /* Resume the listener FD to allow concurrent-processing of
1800 * additional incoming connections.
1805 if ( s == AC_SOCKET_INVALID ) {
1806 int err = sock_errno();
1817 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1819 /* Stop listening until an existing session closes */
1821 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1824 Debug( LDAP_DEBUG_ANY,
1825 "daemon: accept(%ld) failed errno=%d (%s)\n",
1826 (long) sl->sl_sd, err, sock_errstr(err) );
1827 ldap_pvt_thread_yield();
1830 sfd = SLAP_SOCKNEW( s );
1832 /* make sure descriptor number isn't too great */
1833 if ( sfd >= dtblsize ) {
1834 Debug( LDAP_DEBUG_ANY,
1835 "daemon: %ld beyond descriptor table size %ld\n",
1836 (long) sfd, (long) dtblsize, 0 );
1839 ldap_pvt_thread_yield();
1844 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1845 /* newly accepted stream should not be in any of the FD SETS */
1846 assert( SLAP_SOCK_NOT_ACTIVE( sfd ));
1847 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1848 #endif /* LDAP_DEBUG */
1850 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1851 #ifdef LDAP_PF_LOCAL
1852 /* for IPv4 and IPv6 sockets only */
1853 if ( from.sa_addr.sa_family != AF_LOCAL )
1854 #endif /* LDAP_PF_LOCAL */
1859 /* enable keep alives */
1861 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1862 (char *) &tmp, sizeof(tmp) );
1863 if ( rc == AC_SOCKET_ERROR ) {
1864 int err = sock_errno();
1865 Debug( LDAP_DEBUG_ANY,
1866 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1867 "errno=%d (%s)\n", (long) sfd, err, sock_errstr(err) );
1869 #endif /* SO_KEEPALIVE */
1871 /* enable no delay */
1873 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1874 (char *)&tmp, sizeof(tmp) );
1875 if ( rc == AC_SOCKET_ERROR ) {
1876 int err = sock_errno();
1877 Debug( LDAP_DEBUG_ANY,
1878 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1879 "errno=%d (%s)\n", (long) sfd, err, sock_errstr(err) );
1881 #endif /* TCP_NODELAY */
1883 #endif /* SO_KEEPALIVE || TCP_NODELAY */
1885 Debug( LDAP_DEBUG_CONNS,
1886 "daemon: listen=%ld, new connection on %ld\n",
1887 (long) sl->sl_sd, (long) sfd, 0 );
1890 switch ( from.sa_addr.sa_family ) {
1891 # ifdef LDAP_PF_LOCAL
1893 cflag |= CONN_IS_IPC;
1895 /* FIXME: apparently accept doesn't fill
1896 * the sun_path sun_path member */
1897 if ( from.sa_un_addr.sun_path[0] == '\0' ) {
1898 AC_MEMCPY( from.sa_un_addr.sun_path,
1899 sl->sl_sa.sa_un_addr.sun_path,
1900 sizeof( from.sa_un_addr.sun_path ) );
1903 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1909 #ifdef LDAP_PF_LOCAL_SENDMSG
1910 peerbv.bv_val = peerbuf;
1911 peerbv.bv_len = sizeof( peerbuf );
1913 if( LUTIL_GETPEEREID( s, &uid, &gid, &peerbv ) == 0 ) {
1914 authid.bv_val = ch_malloc(
1915 STRLENOF( "gidNumber=4294967295+uidNumber=4294967295,"
1916 "cn=peercred,cn=external,cn=auth" ) + 1 );
1917 authid.bv_len = sprintf( authid.bv_val,
1918 "gidNumber=%d+uidNumber=%d,"
1919 "cn=peercred,cn=external,cn=auth",
1920 (int) gid, (int) uid );
1921 assert( authid.bv_len <=
1922 STRLENOF( "gidNumber=4294967295+uidNumber=4294967295,"
1923 "cn=peercred,cn=external,cn=auth" ) );
1928 #endif /* LDAP_PF_LOCAL */
1930 # ifdef LDAP_PF_INET6
1932 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1933 peeraddr = inet_ntoa( *((struct in_addr *)
1934 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1935 sprintf( peername, "IP=%s:%d",
1936 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1937 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1939 char addr[INET6_ADDRSTRLEN];
1941 peeraddr = (char *) inet_ntop( AF_INET6,
1942 &from.sa_in6_addr.sin6_addr,
1943 addr, sizeof addr );
1944 sprintf( peername, "IP=[%s]:%d",
1945 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1946 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1949 # endif /* LDAP_PF_INET6 */
1952 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1953 sprintf( peername, "IP=%s:%d",
1954 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1955 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1963 if ( ( from.sa_addr.sa_family == AF_INET )
1964 #ifdef LDAP_PF_INET6
1965 || ( from.sa_addr.sa_family == AF_INET6 )
1966 #endif /* LDAP_PF_INET6 */
1970 #ifdef SLAPD_RLOOKUPS
1971 if ( use_reverse_lookup ) {
1973 if (ldap_pvt_get_hname( (const struct sockaddr *)&from, len, hbuf,
1974 sizeof(hbuf), &herr ) == 0) {
1975 ldap_pvt_str2lower( hbuf );
1979 #endif /* SLAPD_RLOOKUPS */
1984 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_tcpd_mutex );
1985 rc = hosts_ctl("slapd",
1986 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1987 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1988 SLAP_STRING_UNKNOWN );
1989 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_tcpd_mutex );
1992 Statslog( LDAP_DEBUG_STATS,
1993 "fd=%ld DENIED from %s (%s)\n",
1995 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1996 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
2002 #endif /* HAVE_TCPD */
2006 if ( sl->sl_is_tls ) cflag |= CONN_IS_TLS;
2008 c = connection_init(sfd, sl,
2009 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
2010 peername, cflag, ssf,
2011 authid.bv_val ? &authid : NULL
2012 LDAP_PF_LOCAL_SENDMSG_ARG(&peerbv));
2014 if( authid.bv_val ) ch_free(authid.bv_val);
2017 Debug( LDAP_DEBUG_ANY,
2018 "daemon: connection_init(%ld, %s, %s) failed.\n",
2019 (long) sfd, peername, sl->sl_name.bv_val );
2024 Statslog( LDAP_DEBUG_STATS,
2025 "conn=%ld fd=%ld ACCEPT from %s (%s)\n",
2026 c->c_connid, (long) sfd, peername, sl->sl_name.bv_val,
2033 slap_listener_thread(
2038 Listener *sl = (Listener *)ptr;
2040 rc = slap_listener( sl );
2042 if( rc != LDAP_SUCCESS ) {
2043 Debug( LDAP_DEBUG_ANY,
2044 "slap_listener_thread(%s): failed err=%d",
2045 sl->sl_url.bv_val, rc, 0 );
2052 slap_listener_activate(
2057 Debug( LDAP_DEBUG_TRACE, "slap_listener_activate(%d): %s\n",
2058 sl->sl_sd, sl->sl_busy ? "busy" : "", 0 );
2062 rc = ldap_pvt_thread_pool_submit( &connection_pool,
2063 slap_listener_thread, (void *) sl );
2066 Debug( LDAP_DEBUG_ANY,
2067 "listener_activate(%d): submit failed (%d)\n",
2078 time_t last_idle_check = 0;
2081 #define SLAPD_IDLE_CHECK_LIMIT 4
2083 if ( global_idletimeout > 0 ) {
2084 last_idle_check = slap_get_time();
2087 slapd_add( wake_sds[0], 0, NULL );
2089 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2090 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ) continue;
2092 #ifdef LDAP_CONNECTIONLESS
2093 /* Since this is connectionless, the data port is the
2094 * listening port. The listen() and accept() calls
2097 if ( slap_listeners[l]->sl_is_udp )
2099 #endif /* LDAP_CONNECTIONLESS */
2101 /* FIXME: TCP-only! */
2102 #ifdef LDAP_TCP_BUFFER
2104 int origsize, size, realsize, rc;
2106 char buf[ SLAP_TEXT_BUFLEN ];
2109 if ( slap_listeners[l]->sl_tcp_rmem > 0 ) {
2110 size = slap_listeners[l]->sl_tcp_rmem;
2111 } else if ( slapd_tcp_rmem > 0 ) {
2112 size = slapd_tcp_rmem;
2116 optlen = sizeof( origsize );
2117 rc = getsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2124 int err = sock_errno();
2125 Debug( LDAP_DEBUG_ANY,
2126 "slapd_daemon_task: getsockopt(SO_RCVBUF) failed errno=%d (%s)\n",
2127 err, sock_errstr(err), 0 );
2130 optlen = sizeof( size );
2131 rc = setsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2134 (const void *)&size,
2138 int err = sock_errno();
2139 Debug( LDAP_DEBUG_ANY,
2140 "slapd_daemon_task: setsockopt(SO_RCVBUF) failed errno=%d (%s)\n",
2141 err, sock_errstr(err), 0 );
2144 optlen = sizeof( realsize );
2145 rc = getsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2152 int err = sock_errno();
2153 Debug( LDAP_DEBUG_ANY,
2154 "slapd_daemon_task: getsockopt(SO_RCVBUF) failed errno=%d (%s)\n",
2155 err, sock_errstr(err), 0 );
2158 snprintf( buf, sizeof( buf ),
2159 "url=%s (#%d) RCVBUF original size=%d requested size=%d real size=%d",
2160 slap_listeners[l]->sl_url.bv_val, l, origsize, size, realsize );
2161 Debug( LDAP_DEBUG_ANY,
2162 "slapd_daemon_task: %s\n",
2167 if ( slap_listeners[l]->sl_tcp_wmem > 0 ) {
2168 size = slap_listeners[l]->sl_tcp_wmem;
2169 } else if ( slapd_tcp_wmem > 0 ) {
2170 size = slapd_tcp_wmem;
2174 optlen = sizeof( origsize );
2175 rc = getsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2182 int err = sock_errno();
2183 Debug( LDAP_DEBUG_ANY,
2184 "slapd_daemon_task: getsockopt(SO_SNDBUF) failed errno=%d (%s)\n",
2185 err, sock_errstr(err), 0 );
2188 optlen = sizeof( size );
2189 rc = setsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2192 (const void *)&size,
2196 int err = sock_errno();
2197 Debug( LDAP_DEBUG_ANY,
2198 "slapd_daemon_task: setsockopt(SO_SNDBUF) failed errno=%d (%s)",
2199 err, sock_errstr(err), 0 );
2202 optlen = sizeof( realsize );
2203 rc = getsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2210 int err = sock_errno();
2211 Debug( LDAP_DEBUG_ANY,
2212 "slapd_daemon_task: getsockopt(SO_SNDBUF) failed errno=%d (%s)\n",
2213 err, sock_errstr(err), 0 );
2216 snprintf( buf, sizeof( buf ),
2217 "url=%s (#%d) SNDBUF original size=%d requested size=%d real size=%d",
2218 slap_listeners[l]->sl_url.bv_val, l, origsize, size, realsize );
2219 Debug( LDAP_DEBUG_ANY,
2220 "slapd_daemon_task: %s\n",
2224 #endif /* LDAP_TCP_BUFFER */
2226 if ( listen( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ), SLAPD_LISTEN_BACKLOG ) == -1 ) {
2227 int err = sock_errno();
2229 #ifdef LDAP_PF_INET6
2230 /* If error is EADDRINUSE, we are trying to listen to INADDR_ANY and
2231 * we are already listening to in6addr_any, then we want to ignore
2232 * this and continue.
2234 if ( err == EADDRINUSE ) {
2236 struct sockaddr_in sa = slap_listeners[l]->sl_sa.sa_in_addr;
2237 struct sockaddr_in6 sa6;
2239 if ( sa.sin_family == AF_INET &&
2240 sa.sin_addr.s_addr == htonl(INADDR_ANY) ) {
2241 for ( i = 0 ; i < l; i++ ) {
2242 sa6 = slap_listeners[i]->sl_sa.sa_in6_addr;
2243 if ( sa6.sin6_family == AF_INET6 &&
2244 !memcmp( &sa6.sin6_addr, &in6addr_any,
2245 sizeof(struct in6_addr) ) )
2252 /* We are already listening to in6addr_any */
2253 Debug( LDAP_DEBUG_CONNS,
2254 "daemon: Attempt to listen to 0.0.0.0 failed, "
2255 "already listening on ::, assuming IPv4 included\n",
2257 slapd_close( slap_listeners[l]->sl_sd );
2258 slap_listeners[l]->sl_sd = AC_SOCKET_INVALID;
2263 #endif /* LDAP_PF_INET6 */
2264 Debug( LDAP_DEBUG_ANY,
2265 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
2266 slap_listeners[l]->sl_url.bv_val, err,
2271 /* make the listening socket non-blocking */
2272 if ( ber_pvt_socket_set_nonblock( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ), 1 ) < 0 ) {
2273 Debug( LDAP_DEBUG_ANY, "slapd_daemon_task: "
2274 "set nonblocking on a listening socket failed\n",
2280 slapd_add( slap_listeners[l]->sl_sd, 0, slap_listeners[l] );
2283 #ifdef HAVE_NT_SERVICE_MANAGER
2284 if ( started_event != NULL ) {
2285 ldap_pvt_thread_cond_signal( &started_event );
2287 #endif /* HAVE_NT_SERVICE_MANAGER */
2289 /* initialization complete. Here comes the loop. */
2291 while ( !slapd_shutdown ) {
2296 #if SLAP_EVENTS_ARE_INDEXED
2297 ber_socket_t nrfds, nwfds;
2298 #endif /* SLAP_EVENTS_ARE_INDEXED */
2299 #define SLAPD_EBADF_LIMIT 16
2306 struct timeval *tvp;
2312 now = slap_get_time();
2314 if ( global_idletimeout > 0 || chk_writetime ) {
2316 /* Set the select timeout.
2317 * Don't just truncate, preserve the fractions of
2318 * seconds to prevent sleeping for zero time.
2320 if ( chk_writetime ) {
2321 tv.tv_sec = global_writetimeout;
2323 if ( difftime( chk_writetime, now ) < 0 )
2326 tv.tv_sec = global_idletimeout / SLAPD_IDLE_CHECK_LIMIT;
2327 tv.tv_usec = global_idletimeout - \
2328 ( tv.tv_sec * SLAPD_IDLE_CHECK_LIMIT );
2329 tv.tv_usec *= 1000000 / SLAPD_IDLE_CHECK_LIMIT;
2330 if ( difftime( last_idle_check +
2331 global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 )
2335 connections_timeout_idle( now );
2336 last_idle_check = now;
2344 if ( slapd_gentle_shutdown ) {
2345 ber_socket_t active;
2347 if ( slapd_gentle_shutdown == 1 ) {
2349 Debug( LDAP_DEBUG_ANY, "slapd gentle shutdown\n", 0, 0, 0 );
2350 close_listeners( 1 );
2351 frontendDB->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
2352 LDAP_STAILQ_FOREACH(be, &backendDB, be_next) {
2353 be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
2355 slapd_gentle_shutdown = 2;
2358 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
2359 active = slap_daemon.sd_nactives;
2360 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
2361 if ( active == 0 ) {
2369 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
2371 nwriters = slap_daemon.sd_nwriters;
2373 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2374 Listener *lr = slap_listeners[l];
2376 if ( lr->sl_sd == AC_SOCKET_INVALID ) continue;
2378 if ( lr->sl_mute || lr->sl_busy )
2380 SLAP_SOCK_CLR_READ( lr->sl_sd );
2382 SLAP_SOCK_SET_READ( lr->sl_sd );
2388 nfds = SLAP_EVENT_MAX;
2390 if (( chk_writetime || global_idletimeout ) && slap_daemon.sd_nactives ) at = 1;
2392 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
2395 #if defined(HAVE_YIELDING_SELECT) || defined(NO_THREADS)
2396 && ( tv.tv_sec || tv.tv_usec )
2397 #endif /* HAVE_YIELDING_SELECT || NO_THREADS */
2405 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2406 rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
2407 while ( rtask && cat.tv_sec && cat.tv_sec <= now ) {
2408 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
2409 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
2411 ldap_pvt_runqueue_runtask( &slapd_rq, rtask );
2412 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
2413 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2414 ldap_pvt_thread_pool_submit( &connection_pool,
2415 rtask->routine, (void *) rtask );
2416 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2418 rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
2420 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2422 if ( rtask && cat.tv_sec ) {
2423 /* NOTE: diff __should__ always be >= 0,
2424 * AFAI understand; however (ITS#4872),
2425 * time_t might be unsigned in some systems,
2426 * while difftime() returns a double */
2427 double diff = difftime( cat.tv_sec, now );
2431 if ( tvp == NULL || diff < tv.tv_sec ) {
2438 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2439 Listener *lr = slap_listeners[l];
2441 if ( lr->sl_sd == AC_SOCKET_INVALID ) {
2445 if ( lr->sl_mute ) {
2446 Debug( LDAP_DEBUG_CONNS,
2447 "daemon: " SLAP_EVENT_FNAME ": "
2448 "listen=%d muted\n",
2453 if ( lr->sl_busy ) {
2454 Debug( LDAP_DEBUG_CONNS,
2455 "daemon: " SLAP_EVENT_FNAME ": "
2461 Debug( LDAP_DEBUG_CONNS,
2462 "daemon: " SLAP_EVENT_FNAME ": "
2463 "listen=%d active_threads=%d tvp=%s\n",
2464 lr->sl_sd, at, tvp == NULL ? "NULL" : "zero" );
2467 SLAP_EVENT_WAIT( tvp, &ns );
2469 case -1: { /* failure - try again */
2470 int err = sock_errno();
2472 if ( err != EINTR ) {
2475 /* Don't log unless we got it twice in a row */
2476 if ( !( ebadf & 1 ) ) {
2477 Debug( LDAP_DEBUG_ANY,
2483 sock_errstr( err ) );
2485 if ( ebadf >= SLAPD_EBADF_LIMIT ) {
2492 case 0: /* timeout - let threads run */
2494 #ifndef HAVE_YIELDING_SELECT
2495 Debug( LDAP_DEBUG_CONNS, "daemon: " SLAP_EVENT_FNAME
2496 "timeout - yielding\n",
2499 ldap_pvt_thread_yield();
2500 #endif /* ! HAVE_YIELDING_SELECT */
2503 default: /* something happened - deal with it */
2504 if ( slapd_shutdown ) continue;
2507 Debug( LDAP_DEBUG_CONNS,
2508 "daemon: activity on %d descriptor%s\n",
2509 ns, ns != 1 ? "s" : "", 0 );
2513 #if SLAP_EVENTS_ARE_INDEXED
2514 if ( SLAP_EVENT_IS_READ( wake_sds[0] ) ) {
2516 SLAP_EVENT_CLR_READ( wake_sds[0] );
2518 tcp_read( SLAP_FD2SOCK(wake_sds[0]), c, sizeof(c) );
2519 Debug( LDAP_DEBUG_CONNS, "daemon: waked\n", 0, 0, 0 );
2523 /* The event slot equals the descriptor number - this is
2524 * true for Unix select and poll. We treat Windows select
2525 * like this too, even though it's a kludge.
2527 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2530 if ( ns <= 0 ) break;
2531 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ) continue;
2532 #ifdef LDAP_CONNECTIONLESS
2533 if ( slap_listeners[l]->sl_is_udp ) continue;
2534 #endif /* LDAP_CONNECTIONLESS */
2535 if ( !SLAP_EVENT_IS_READ( slap_listeners[l]->sl_sd ) ) continue;
2538 SLAP_EVENT_CLR_READ( slap_listeners[l]->sl_sd );
2539 SLAP_EVENT_CLR_WRITE( slap_listeners[l]->sl_sd );
2542 rc = slap_listener_activate( slap_listeners[l] );
2545 /* bypass the following tests if no descriptors left */
2547 #ifndef HAVE_YIELDING_SELECT
2548 ldap_pvt_thread_yield();
2549 #endif /* HAVE_YIELDING_SELECT */
2553 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
2556 for ( i = 0; i < nfds; i++ ) {
2559 r = SLAP_EVENT_IS_READ( i );
2560 /* writefds was not initialized if nwriters was zero */
2561 w = nwriters ? SLAP_EVENT_IS_WRITE( i ) : 0;
2563 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
2564 r ? "r" : "", w ? "w" : "" );
2574 if ( ns <= 0 ) break;
2576 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
2578 /* loop through the writers */
2579 for ( i = 0; nwfds > 0; i++ ) {
2581 if ( ! SLAP_EVENT_IS_WRITE( i ) ) continue;
2584 SLAP_EVENT_CLR_WRITE( wd );
2587 Debug( LDAP_DEBUG_CONNS,
2588 "daemon: write active on %d\n",
2592 * NOTE: it is possible that the connection was closed
2593 * and that the stream is now inactive.
2594 * connection_write() must validate the stream is still
2597 * ITS#4338: if the stream is invalid, there is no need to
2598 * close it here. It has already been closed in connection.c.
2600 if ( connection_write( wd ) < 0 ) {
2601 if ( SLAP_EVENT_IS_READ( wd ) ) {
2602 SLAP_EVENT_CLR_READ( (unsigned) wd );
2608 for ( i = 0; nrfds > 0; i++ ) {
2610 if ( ! SLAP_EVENT_IS_READ( i ) ) continue;
2612 SLAP_EVENT_CLR_READ( rd );
2615 Debug ( LDAP_DEBUG_CONNS,
2616 "daemon: read activity on %d\n", rd, 0, 0 );
2618 * NOTE: it is possible that the connection was closed
2619 * and that the stream is now inactive.
2620 * connection_read() must valid the stream is still
2624 connection_read_activate( rd );
2626 #else /* !SLAP_EVENTS_ARE_INDEXED */
2628 /* The events are returned in an arbitrary list. This is true
2629 * for /dev/poll, epoll and kqueue. In order to prioritize things
2630 * so that we can handle wake_sds first, listeners second, and then
2631 * all other connections last (as we do for select), we would need
2632 * to use multiple event handles and cascade them.
2634 * That seems like a bit of hassle. So the wake_sds check has been
2635 * skipped. For epoll and kqueue we can associate arbitrary data with
2636 * an event, so we could use pointers to the listener structure
2637 * instead of just the file descriptor. For /dev/poll we have to
2638 * search the listeners array for a matching descriptor.
2640 * We now handle wake events when we see them; they are not given
2644 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
2646 for ( i = 0; i < ns; i++ ) {
2649 /* Don't log listener events */
2650 if ( SLAP_EVENT_IS_LISTENER( i )
2651 #ifdef LDAP_CONNECTIONLESS
2652 && !( (SLAP_EVENT_LISTENER( i ))->sl_is_udp )
2653 #endif /* LDAP_CONNECTIONLESS */
2659 fd = SLAP_EVENT_FD( i );
2660 /* Don't log internal wake events */
2661 if ( fd == wake_sds[0] ) continue;
2663 r = SLAP_EVENT_IS_READ( i );
2664 w = SLAP_EVENT_IS_WRITE( i );
2666 Debug( LDAP_DEBUG_CONNS, " %d%s%s", fd,
2667 r ? "r" : "", w ? "w" : "" );
2670 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
2671 #endif /* LDAP_DEBUG */
2673 for ( i = 0; i < ns; i++ ) {
2674 int rc = 1, fd, w = 0, r = 0;
2676 if ( SLAP_EVENT_IS_LISTENER( i ) ) {
2677 rc = slap_listener_activate( SLAP_EVENT_LISTENER( i ) );
2680 /* If we found a regular listener, rc is now zero, and we
2681 * can skip the data portion. But if it was a UDP listener
2682 * then rc is still 1, and we want to handle the data.
2685 fd = SLAP_EVENT_FD( i );
2687 /* Handle wake events */
2688 if ( fd == wake_sds[0] ) {
2691 tcp_read( SLAP_FD2SOCK(wake_sds[0]), c, sizeof(c) );
2695 if ( SLAP_EVENT_IS_WRITE( i ) ) {
2696 Debug( LDAP_DEBUG_CONNS,
2697 "daemon: write active on %d\n",
2700 SLAP_EVENT_CLR_WRITE( i );
2704 * NOTE: it is possible that the connection was closed
2705 * and that the stream is now inactive.
2706 * connection_write() must valid the stream is still
2709 if ( connection_write( fd ) < 0 ) {
2713 /* If event is a read */
2714 if ( SLAP_EVENT_IS_READ( i ))
2717 Debug( LDAP_DEBUG_CONNS,
2718 "daemon: read active on %d\n",
2722 SLAP_EVENT_CLR_READ( i );
2725 /* Don't keep reporting the hangup
2727 if ( SLAP_SOCK_IS_ACTIVE( fd )) {
2728 SLAP_EPOLL_SOCK_SET( fd, EPOLLET );
2732 connection_read_activate( fd );
2736 #endif /* SLAP_EVENTS_ARE_INDEXED */
2738 #ifndef HAVE_YIELDING_SELECT
2739 ldap_pvt_thread_yield();
2740 #endif /* ! HAVE_YIELDING_SELECT */
2743 if ( slapd_shutdown == 1 ) {
2744 Debug( LDAP_DEBUG_ANY,
2745 "daemon: shutdown requested and initiated.\n",
2748 } else if ( slapd_shutdown == 2 ) {
2749 #ifdef HAVE_NT_SERVICE_MANAGER
2750 Debug( LDAP_DEBUG_ANY,
2751 "daemon: shutdown initiated by Service Manager.\n",
2753 #else /* !HAVE_NT_SERVICE_MANAGER */
2754 Debug( LDAP_DEBUG_ANY,
2755 "daemon: abnormal condition, shutdown initiated.\n",
2757 #endif /* !HAVE_NT_SERVICE_MANAGER */
2759 Debug( LDAP_DEBUG_ANY,
2760 "daemon: no active streams, shutdown initiated.\n",
2764 if ( slapd_gentle_shutdown != 2 ) close_listeners ( 0 );
2766 if ( !slapd_gentle_shutdown ) {
2767 slapd_abrupt_shutdown = 1;
2768 connections_shutdown();
2771 if ( LogTest( LDAP_DEBUG_ANY )) {
2772 int t = ldap_pvt_thread_pool_backload( &connection_pool );
2773 Debug( LDAP_DEBUG_ANY,
2774 "slapd shutdown: waiting for %d operations/tasks to finish\n",
2777 ldap_pvt_thread_pool_destroy( &connection_pool, 1 );
2779 free( slap_listeners );
2780 slap_listeners = NULL;
2786 #ifdef LDAP_CONNECTIONLESS
2788 connectionless_init( void )
2792 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2793 Listener *lr = slap_listeners[l];
2796 if ( !lr->sl_is_udp ) {
2800 c = connection_init( lr->sl_sd, lr, "", "",
2801 CONN_IS_UDP, (slap_ssf_t) 0, NULL
2802 LDAP_PF_LOCAL_SENDMSG_ARG(NULL));
2805 Debug( LDAP_DEBUG_TRACE,
2806 "connectionless_init: failed on %s (%d)\n",
2807 lr->sl_url, lr->sl_sd, 0 );
2815 #endif /* LDAP_CONNECTIONLESS */
2818 slapd_daemon( void )
2822 #ifdef LDAP_CONNECTIONLESS
2823 connectionless_init();
2824 #endif /* LDAP_CONNECTIONLESS */
2826 #define SLAPD_LISTENER_THREAD 1
2827 #if defined( SLAPD_LISTENER_THREAD )
2829 ldap_pvt_thread_t listener_tid;
2831 /* listener as a separate THREAD */
2832 rc = ldap_pvt_thread_create( &listener_tid,
2833 0, slapd_daemon_task, NULL );
2836 Debug( LDAP_DEBUG_ANY,
2837 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
2841 /* wait for the listener thread to complete */
2842 ldap_pvt_thread_join( listener_tid, (void *)NULL );
2844 #else /* ! SLAPD_LISTENER_THREAD */
2845 /* experimental code */
2846 slapd_daemon_task( NULL );
2847 #endif /* ! SLAPD_LISTENER_THREAD */
2855 #if defined( HAVE_WINSOCK2 )
2856 WORD wVersionRequested;
2860 wVersionRequested = MAKEWORD( 2, 0 );
2862 err = WSAStartup( wVersionRequested, &wsaData );
2864 /* Tell the user that we couldn't find a usable */
2869 /* Confirm that the WinSock DLL supports 2.0.*/
2870 /* Note that if the DLL supports versions greater */
2871 /* than 2.0 in addition to 2.0, it will still return */
2872 /* 2.0 in wVersion since that is the version we */
2875 if ( LOBYTE( wsaData.wVersion ) != 2 ||
2876 HIBYTE( wsaData.wVersion ) != 0 )
2878 /* Tell the user that we couldn't find a usable */
2884 /* The WinSock DLL is acceptable. Proceed. */
2885 #elif defined( HAVE_WINSOCK )
2887 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) return -1;
2888 #endif /* ! HAVE_WINSOCK2 && ! HAVE_WINSOCK */
2896 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
2898 #endif /* HAVE_WINSOCK2 || HAVE_WINSOCK */
2905 slap_sig_shutdown( int sig )
2907 int save_errno = errno;
2910 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
2914 * If the NT Service Manager is controlling the server, we don't
2915 * want SIGBREAK to kill the server. For some strange reason,
2916 * SIGBREAK is generated when a user logs out.
2919 #if defined(HAVE_NT_SERVICE_MANAGER) && defined(SIGBREAK)
2920 if (is_NT_Service && sig == SIGBREAK) {
2923 #endif /* HAVE_NT_SERVICE_MANAGER && SIGBREAK */
2925 if (sig == SIGHUP && global_gentlehup && slapd_gentle_shutdown == 0) {
2926 slapd_gentle_shutdown = 1;
2935 /* reinstall self */
2936 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
2942 slap_sig_wake( int sig )
2944 int save_errno = errno;
2948 /* reinstall self */
2949 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
2956 slapd_add_internal( ber_socket_t s, int isactive )
2958 slapd_add( s, isactive, NULL );
2962 slapd_get_listeners( void )
2964 return slap_listeners;
2968 slap_wake_listener()