2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2009 The OpenLDAP Foundation.
5 * Portions Copyright 2007 by Howard Chu, Symas Corporation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
33 #include <ac/socket.h>
34 #include <ac/string.h>
36 #include <ac/unistd.h>
39 #include "ldap_pvt_thread.h"
44 #if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL)
45 # include <sys/epoll.h>
46 #elif defined(SLAP_X_DEVPOLL) && defined(HAVE_SYS_DEVPOLL_H) && defined(HAVE_DEVPOLL)
47 # include <sys/types.h>
48 # include <sys/stat.h>
50 # include <sys/devpoll.h>
51 #endif /* ! epoll && ! /dev/poll */
54 int allow_severity = LOG_INFO;
55 int deny_severity = LOG_NOTICE;
56 #endif /* TCP Wrappers */
59 # include <sys/stat.h>
60 /* this should go in <ldap.h> as soon as it is accepted */
61 # define LDAPI_MOD_URLEXT "x-mod"
62 #endif /* LDAP_PF_LOCAL */
65 int slap_inet4or6 = AF_UNSPEC;
67 int slap_inet4or6 = AF_INET;
72 ber_socket_t dtblsize;
73 slap_ssf_t local_ssf = LDAP_PVT_SASL_LOCAL_SSF;
74 struct runqueue_s slapd_rq;
76 #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 )
992 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
994 assert( SLAP_SOCK_IS_ACTIVE( s ));
995 if (!SLAP_SOCK_IS_READ( s )) SLAP_SOCK_SET_READ( s );
997 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1002 slapd_get_writetime()
1005 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1006 cur = chk_writetime;
1007 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1012 slapd_clr_writetime( time_t old )
1014 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1015 if ( chk_writetime == old )
1017 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1021 slapd_close( ber_socket_t s )
1023 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
1025 tcp_close( SLAP_FD2SOCK(s) );
1032 slap_free_listener_addresses( struct sockaddr **sal )
1034 struct sockaddr **sap;
1035 if (sal == NULL) return;
1036 for (sap = sal; *sap != NULL; sap++) ch_free(*sap);
1040 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
1049 assert( exts != NULL );
1050 assert( perms != NULL );
1051 assert( crit != NULL );
1054 for ( i = 0; exts[ i ]; i++ ) {
1055 char *type = exts[ i ];
1058 if ( type[ 0 ] == '!' ) {
1063 if ( strncasecmp( type, LDAPI_MOD_URLEXT "=",
1064 sizeof(LDAPI_MOD_URLEXT "=") - 1 ) == 0 )
1066 char *value = type + ( sizeof(LDAPI_MOD_URLEXT "=") - 1 );
1070 switch (strlen(value)) {
1072 /* skip leading '0' */
1073 if ( value[ 0 ] != '0' ) return LDAP_OTHER;
1077 for ( j = 0; j < 3; j++) {
1080 v = value[ j ] - '0';
1082 if ( v < 0 || v > 7 ) return LDAP_OTHER;
1089 for ( j = 1; j < 10; j++ ) {
1090 static mode_t m[] = { 0,
1091 S_IRUSR, S_IWUSR, S_IXUSR,
1092 S_IRGRP, S_IWGRP, S_IXGRP,
1093 S_IROTH, S_IWOTH, S_IXOTH
1095 static const char c[] = "-rwxrwxrwx";
1097 if ( value[ j ] == c[ j ] ) {
1100 } else if ( value[ j ] != '-' ) {
1113 return LDAP_SUCCESS;
1119 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
1121 /* port = 0 indicates AF_LOCAL */
1123 slap_get_listener_addresses(
1125 unsigned short port,
1126 struct sockaddr ***sal )
1128 struct sockaddr **sap;
1130 #ifdef LDAP_PF_LOCAL
1132 *sal = ch_malloc(2 * sizeof(void *));
1133 if (*sal == NULL) return -1;
1136 *sap = ch_malloc(sizeof(struct sockaddr_un));
1137 if (*sap == NULL) goto errexit;
1141 (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) )
1143 Debug( LDAP_DEBUG_ANY,
1144 "daemon: domain socket path (%s) too long in URL",
1149 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
1150 (*sap)->sa_family = AF_LOCAL;
1151 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
1153 #endif /* LDAP_PF_LOCAL */
1155 #ifdef HAVE_GETADDRINFO
1156 struct addrinfo hints, *res, *sai;
1160 memset( &hints, '\0', sizeof(hints) );
1161 hints.ai_flags = AI_PASSIVE;
1162 hints.ai_socktype = SOCK_STREAM;
1163 hints.ai_family = slap_inet4or6;
1164 snprintf(serv, sizeof serv, "%d", port);
1166 if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
1167 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo() failed: %s\n",
1168 AC_GAI_STRERROR(err), 0, 0);
1173 for (n=2; (sai = sai->ai_next) != NULL; n++) {
1176 *sal = ch_calloc(n, sizeof(void *));
1177 if (*sal == NULL) return -1;
1182 for ( sai=res; sai; sai=sai->ai_next ) {
1183 if( sai->ai_addr == NULL ) {
1184 Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
1185 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
1190 switch (sai->ai_family) {
1191 # ifdef LDAP_PF_INET6
1193 *sap = ch_malloc(sizeof(struct sockaddr_in6));
1198 *(struct sockaddr_in6 *)*sap =
1199 *((struct sockaddr_in6 *)sai->ai_addr);
1201 # endif /* LDAP_PF_INET6 */
1203 *sap = ch_malloc(sizeof(struct sockaddr_in));
1208 *(struct sockaddr_in *)*sap =
1209 *((struct sockaddr_in *)sai->ai_addr);
1217 (*sap)->sa_family = sai->ai_family;
1225 #else /* ! HAVE_GETADDRINFO */
1228 struct hostent *he = NULL;
1230 if ( host == NULL ) {
1231 in.s_addr = htonl(INADDR_ANY);
1233 } else if ( !inet_aton( host, &in ) ) {
1234 he = gethostbyname( host );
1236 Debug( LDAP_DEBUG_ANY,
1237 "daemon: invalid host %s", host, 0, 0);
1240 for (n = 0; he->h_addr_list[n]; n++) /* empty */;
1243 *sal = ch_malloc((n+1) * sizeof(void *));
1244 if (*sal == NULL) return -1;
1247 for ( i = 0; i<n; i++ ) {
1248 sap[i] = ch_malloc(sizeof(struct sockaddr_in));
1249 if (*sap == NULL) goto errexit;
1251 (void)memset( (void *)sap[i], '\0', sizeof(struct sockaddr_in) );
1252 sap[i]->sa_family = AF_INET;
1253 ((struct sockaddr_in *)sap[i])->sin_port = htons(port);
1254 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr,
1255 he ? (struct in_addr *)he->h_addr_list[i] : &in,
1256 sizeof(struct in_addr) );
1259 #endif /* ! HAVE_GETADDRINFO */
1265 slap_free_listener_addresses(*sal);
1279 unsigned short port;
1280 int err, addrlen = 0;
1281 struct sockaddr **sal, **psal;
1282 int socktype = SOCK_STREAM; /* default to COTS */
1285 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
1290 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
1292 rc = ldap_url_parse( url, &lud );
1294 if( rc != LDAP_URL_SUCCESS ) {
1295 Debug( LDAP_DEBUG_ANY,
1296 "daemon: listen URL \"%s\" parse error=%d\n",
1301 l.sl_url.bv_val = NULL;
1306 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
1307 Debug( LDAP_DEBUG_ANY, "daemon: TLS not supported (%s)\n",
1309 ldap_free_urldesc( lud );
1313 if(! lud->lud_port ) lud->lud_port = LDAP_PORT;
1315 #else /* HAVE_TLS */
1316 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
1318 if(! lud->lud_port ) {
1319 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
1321 #endif /* HAVE_TLS */
1323 #ifdef LDAP_TCP_BUFFER
1326 #endif /* LDAP_TCP_BUFFER */
1328 port = (unsigned short) lud->lud_port;
1330 tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme);
1331 if ( tmp == LDAP_PROTO_IPC ) {
1332 #ifdef LDAP_PF_LOCAL
1333 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
1334 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
1336 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
1338 #else /* ! LDAP_PF_LOCAL */
1340 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
1342 ldap_free_urldesc( lud );
1344 #endif /* ! LDAP_PF_LOCAL */
1346 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
1347 || strcmp(lud->lud_host, "*") == 0 )
1349 err = slap_get_listener_addresses(NULL, port, &sal);
1351 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
1355 #ifdef LDAP_CONNECTIONLESS
1356 l.sl_is_udp = ( tmp == LDAP_PROTO_UDP );
1357 #endif /* LDAP_CONNECTIONLESS */
1359 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
1360 if ( lud->lud_exts ) {
1361 err = get_url_perms( lud->lud_exts, &l.sl_perms, &crit );
1363 l.sl_perms = S_IRWXU | S_IRWXO;
1365 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
1367 ldap_free_urldesc( lud );
1368 if ( err ) return -1;
1370 /* If we got more than one address returned, we need to make space
1371 * for it in the slap_listeners array.
1373 for ( num=0; sal[num]; num++ ) /* empty */;
1375 *listeners += num-1;
1376 slap_listeners = ch_realloc( slap_listeners,
1377 (*listeners + 1) * sizeof(Listener *) );
1381 while ( *sal != NULL ) {
1383 switch( (*sal)->sa_family ) {
1387 #ifdef LDAP_PF_INET6
1391 #endif /* LDAP_PF_INET6 */
1392 #ifdef LDAP_PF_LOCAL
1396 #endif /* LDAP_PF_LOCAL */
1402 #ifdef LDAP_CONNECTIONLESS
1403 if( l.sl_is_udp ) socktype = SOCK_DGRAM;
1404 #endif /* LDAP_CONNECTIONLESS */
1406 s = socket( (*sal)->sa_family, socktype, 0);
1407 if ( s == AC_SOCKET_INVALID ) {
1408 int err = sock_errno();
1409 Debug( LDAP_DEBUG_ANY,
1410 "daemon: %s socket() failed errno=%d (%s)\n",
1411 af, err, sock_errstr(err) );
1415 l.sl_sd = SLAP_SOCKNEW( s );
1417 if ( l.sl_sd >= dtblsize ) {
1418 Debug( LDAP_DEBUG_ANY,
1419 "daemon: listener descriptor %ld is too great %ld\n",
1420 (long) l.sl_sd, (long) dtblsize, 0 );
1426 #ifdef LDAP_PF_LOCAL
1427 if ( (*sal)->sa_family == AF_LOCAL ) {
1428 unlink( ((struct sockaddr_un *)*sal)->sun_path );
1430 #endif /* LDAP_PF_LOCAL */
1433 /* enable address reuse */
1435 rc = setsockopt( s, SOL_SOCKET, SO_REUSEADDR,
1436 (char *) &tmp, sizeof(tmp) );
1437 if ( rc == AC_SOCKET_ERROR ) {
1438 int err = sock_errno();
1439 Debug( LDAP_DEBUG_ANY, "slapd(%ld): "
1440 "setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
1441 (long) l.sl_sd, err, sock_errstr(err) );
1443 #endif /* SO_REUSEADDR */
1446 switch( (*sal)->sa_family ) {
1448 addrlen = sizeof(struct sockaddr_in);
1450 #ifdef LDAP_PF_INET6
1453 /* Try to use IPv6 sockets for IPv6 only */
1455 rc = setsockopt( s , IPPROTO_IPV6, IPV6_V6ONLY,
1456 (char *) &tmp, sizeof(tmp) );
1457 if ( rc == AC_SOCKET_ERROR ) {
1458 int err = sock_errno();
1459 Debug( LDAP_DEBUG_ANY, "slapd(%ld): "
1460 "setsockopt(IPV6_V6ONLY) failed errno=%d (%s)\n",
1461 (long) l.sl_sd, err, sock_errstr(err) );
1463 #endif /* IPV6_V6ONLY */
1464 addrlen = sizeof(struct sockaddr_in6);
1466 #endif /* LDAP_PF_INET6 */
1468 #ifdef LDAP_PF_LOCAL
1473 setsockopt( s, 0, LOCAL_CREDS, &one, sizeof( one ) );
1475 #endif /* LOCAL_CREDS */
1477 addrlen = sizeof( struct sockaddr_un );
1479 #endif /* LDAP_PF_LOCAL */
1482 #ifdef LDAP_PF_LOCAL
1483 /* create socket with all permissions set for those systems
1484 * that honor permissions on sockets (e.g. Linux); typically,
1485 * only write is required. To exploit filesystem permissions,
1486 * place the socket in a directory and use directory's
1487 * permissions. Need write perms to the directory to
1488 * create/unlink the socket; likely need exec perms to access
1489 * the socket (ITS#4709) */
1491 mode_t old_umask = 0;
1493 if ( (*sal)->sa_family == AF_LOCAL ) {
1494 old_umask = umask( 0 );
1496 #endif /* LDAP_PF_LOCAL */
1497 rc = bind( s, *sal, addrlen );
1498 #ifdef LDAP_PF_LOCAL
1499 if ( old_umask != 0 ) {
1503 #endif /* LDAP_PF_LOCAL */
1506 Debug( LDAP_DEBUG_ANY,
1507 "daemon: bind(%ld) failed errno=%d (%s)\n",
1508 (long)l.sl_sd, err, sock_errstr( err ) );
1514 switch ( (*sal)->sa_family ) {
1515 #ifdef LDAP_PF_LOCAL
1517 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
1518 l.sl_name.bv_len = strlen(addr) + sizeof("PATH=") - 1;
1519 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len + 1 );
1520 snprintf( l.sl_name.bv_val, l.sl_name.bv_len + 1,
1523 #endif /* LDAP_PF_LOCAL */
1527 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
1528 char addr[INET_ADDRSTRLEN];
1529 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
1530 addr, sizeof(addr) );
1532 #else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1533 s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr );
1534 #endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1535 port = ntohs( ((struct sockaddr_in *)*sal) ->sin_port );
1537 ber_memalloc( sizeof("IP=255.255.255.255:65535") );
1538 snprintf( l.sl_name.bv_val, sizeof("IP=255.255.255.255:65535"),
1540 s != NULL ? s : SLAP_STRING_UNKNOWN, port );
1541 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1544 #ifdef LDAP_PF_INET6
1546 char addr[INET6_ADDRSTRLEN];
1547 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
1549 port = ntohs( ((struct sockaddr_in6 *)*sal)->sin6_port );
1550 l.sl_name.bv_len = strlen(addr) + sizeof("IP=[]:65535");
1551 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len );
1552 snprintf( l.sl_name.bv_val, l.sl_name.bv_len, "IP=[%s]:%d",
1554 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1556 #endif /* LDAP_PF_INET6 */
1559 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
1560 (int) (*sal)->sa_family, 0, 0 );
1564 AC_MEMCPY(&l.sl_sa, *sal, addrlen);
1565 ber_str2bv( url, 0, 1, &l.sl_url);
1566 li = ch_malloc( sizeof( Listener ) );
1568 slap_listeners[*cur] = li;
1573 slap_free_listener_addresses(psal);
1575 if ( l.sl_url.bv_val == NULL ) {
1576 Debug( LDAP_DEBUG_TRACE,
1577 "slap_open_listener: failed on %s\n", url, 0, 0 );
1581 Debug( LDAP_DEBUG_TRACE, "daemon: listener initialized %s\n",
1582 l.sl_url.bv_val, 0, 0 );
1586 static int sockinit(void);
1587 static int sockdestroy(void);
1590 slapd_daemon_init( const char *urls )
1595 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
1596 urls ? urls : "<null>", 0, 0 );
1598 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
1600 ldap_pvt_thread_mutex_init( &slap_daemon.sd_tcpd_mutex );
1601 #endif /* TCP Wrappers */
1603 if( (rc = sockinit()) != 0 ) return rc;
1606 dtblsize = sysconf( _SC_OPEN_MAX );
1607 #elif defined(HAVE_GETDTABLESIZE)
1608 dtblsize = getdtablesize();
1609 #else /* ! HAVE_SYSCONF && ! HAVE_GETDTABLESIZE */
1610 dtblsize = FD_SETSIZE;
1611 #endif /* ! HAVE_SYSCONF && ! HAVE_GETDTABLESIZE */
1613 /* open a pipe (or something equivalent connected to itself).
1614 * we write a byte on this fd whenever we catch a signal. The main
1615 * loop will be select'ing on this socket, and will wake up when
1616 * this byte arrives.
1618 if( (rc = lutil_pair( wake_sds )) < 0 ) {
1619 Debug( LDAP_DEBUG_ANY,
1620 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
1623 ber_pvt_socket_set_nonblock( wake_sds[1], 1 );
1627 if( urls == NULL ) urls = "ldap:///";
1629 u = ldap_str2charray( urls, " " );
1631 if( u == NULL || u[0] == NULL ) {
1632 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
1635 ldap_charray_free( u );
1639 for( i=0; u[i] != NULL; i++ ) {
1640 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
1645 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
1647 ldap_charray_free( u );
1651 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
1653 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
1655 for(n = 0, j = 0; u[n]; n++ ) {
1656 if ( slap_open_listener( u[n], &i, &j ) ) {
1657 ldap_charray_free( u );
1661 slap_listeners[j] = NULL;
1663 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
1668 if( slapd_register_slp ) {
1669 slapd_slp_init( urls );
1672 #endif /* HAVE_SLP */
1674 ldap_charray_free( u );
1681 slapd_daemon_destroy( void )
1683 connections_destroy();
1685 if ( wake_sds[1] != INVALID_SOCKET &&
1686 SLAP_FD2SOCK( wake_sds[1] ) != SLAP_FD2SOCK( wake_sds[0] ))
1687 #endif /* HAVE_WINSOCK */
1688 tcp_close( SLAP_FD2SOCK(wake_sds[1]) );
1690 if ( wake_sds[0] != INVALID_SOCKET )
1691 #endif /* HAVE_WINSOCK */
1692 tcp_close( SLAP_FD2SOCK(wake_sds[0]) );
1696 if( slapd_register_slp ) {
1700 #endif /* HAVE_SLP */
1703 ldap_pvt_thread_mutex_destroy( &slap_daemon.sd_tcpd_mutex );
1704 #endif /* TCP Wrappers */
1706 ldap_pvt_thread_mutex_destroy( &slap_daemon.sd_mutex );
1717 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1718 Listener *lr = slap_listeners[l];
1720 if ( lr->sl_sd != AC_SOCKET_INVALID ) {
1721 if ( remove ) slapd_remove( lr->sl_sd, NULL, 0, 0, 0 );
1723 #ifdef LDAP_PF_LOCAL
1724 if ( lr->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1725 unlink( lr->sl_sa.sa_un_addr.sun_path );
1727 #endif /* LDAP_PF_LOCAL */
1729 slapd_close( lr->sl_sd );
1732 if ( lr->sl_url.bv_val ) {
1733 ber_memfree( lr->sl_url.bv_val );
1736 if ( lr->sl_name.bv_val ) {
1737 ber_memfree( lr->sl_name.bv_val );
1741 slap_listeners[l] = NULL;
1751 ber_socket_t s, sfd;
1752 ber_socklen_t len = sizeof(from);
1755 struct berval authid = BER_BVNULL;
1756 #ifdef SLAPD_RLOOKUPS
1757 char hbuf[NI_MAXHOST];
1758 #endif /* SLAPD_RLOOKUPS */
1760 char *dnsname = NULL;
1761 char *peeraddr = NULL;
1762 #ifdef LDAP_PF_LOCAL
1763 char peername[MAXPATHLEN + sizeof("PATH=")];
1764 #ifdef LDAP_PF_LOCAL_SENDMSG
1766 struct berval peerbv = BER_BVNULL;
1768 #elif defined(LDAP_PF_INET6)
1769 char peername[sizeof("IP=[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535")];
1770 #else /* ! LDAP_PF_LOCAL && ! LDAP_PF_INET6 */
1771 char peername[sizeof("IP=255.255.255.255:65336")];
1772 #endif /* LDAP_PF_LOCAL */
1775 Debug( LDAP_DEBUG_TRACE,
1776 ">>> slap_listener(%s)\n",
1777 sl->sl_url.bv_val, 0, 0 );
1781 #ifdef LDAP_CONNECTIONLESS
1782 if ( sl->sl_is_udp ) return 1;
1783 #endif /* LDAP_CONNECTIONLESS */
1785 # ifdef LDAP_PF_LOCAL
1786 /* FIXME: apparently accept doesn't fill
1787 * the sun_path sun_path member */
1788 from.sa_un_addr.sun_path[0] = '\0';
1789 # endif /* LDAP_PF_LOCAL */
1791 s = accept( SLAP_FD2SOCK( sl->sl_sd ), (struct sockaddr *) &from, &len );
1793 /* Resume the listener FD to allow concurrent-processing of
1794 * additional incoming connections.
1799 if ( s == AC_SOCKET_INVALID ) {
1800 int err = sock_errno();
1811 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1813 /* Stop listening until an existing session closes */
1815 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1818 Debug( LDAP_DEBUG_ANY,
1819 "daemon: accept(%ld) failed errno=%d (%s)\n",
1820 (long) sl->sl_sd, err, sock_errstr(err) );
1821 ldap_pvt_thread_yield();
1824 sfd = SLAP_SOCKNEW( s );
1826 /* make sure descriptor number isn't too great */
1827 if ( sfd >= dtblsize ) {
1828 Debug( LDAP_DEBUG_ANY,
1829 "daemon: %ld beyond descriptor table size %ld\n",
1830 (long) sfd, (long) dtblsize, 0 );
1833 ldap_pvt_thread_yield();
1838 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1839 /* newly accepted stream should not be in any of the FD SETS */
1840 assert( SLAP_SOCK_NOT_ACTIVE( sfd ));
1841 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1842 #endif /* LDAP_DEBUG */
1844 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1845 #ifdef LDAP_PF_LOCAL
1846 /* for IPv4 and IPv6 sockets only */
1847 if ( from.sa_addr.sa_family != AF_LOCAL )
1848 #endif /* LDAP_PF_LOCAL */
1853 /* enable keep alives */
1855 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1856 (char *) &tmp, sizeof(tmp) );
1857 if ( rc == AC_SOCKET_ERROR ) {
1858 int err = sock_errno();
1859 Debug( LDAP_DEBUG_ANY,
1860 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1861 "errno=%d (%s)\n", (long) sfd, err, sock_errstr(err) );
1863 #endif /* SO_KEEPALIVE */
1865 /* enable no delay */
1867 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1868 (char *)&tmp, sizeof(tmp) );
1869 if ( rc == AC_SOCKET_ERROR ) {
1870 int err = sock_errno();
1871 Debug( LDAP_DEBUG_ANY,
1872 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1873 "errno=%d (%s)\n", (long) sfd, err, sock_errstr(err) );
1875 #endif /* TCP_NODELAY */
1877 #endif /* SO_KEEPALIVE || TCP_NODELAY */
1879 Debug( LDAP_DEBUG_CONNS,
1880 "daemon: listen=%ld, new connection on %ld\n",
1881 (long) sl->sl_sd, (long) sfd, 0 );
1884 switch ( from.sa_addr.sa_family ) {
1885 # ifdef LDAP_PF_LOCAL
1887 cflag |= CONN_IS_IPC;
1889 /* FIXME: apparently accept doesn't fill
1890 * the sun_path sun_path member */
1891 if ( from.sa_un_addr.sun_path[0] == '\0' ) {
1892 AC_MEMCPY( from.sa_un_addr.sun_path,
1893 sl->sl_sa.sa_un_addr.sun_path,
1894 sizeof( from.sa_un_addr.sun_path ) );
1897 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1903 #ifdef LDAP_PF_LOCAL_SENDMSG
1904 peerbv.bv_val = peerbuf;
1905 peerbv.bv_len = sizeof( peerbuf );
1907 if( LUTIL_GETPEEREID( s, &uid, &gid, &peerbv ) == 0 ) {
1908 authid.bv_val = ch_malloc(
1909 STRLENOF( "gidNumber=4294967295+uidNumber=4294967295,"
1910 "cn=peercred,cn=external,cn=auth" ) + 1 );
1911 authid.bv_len = sprintf( authid.bv_val,
1912 "gidNumber=%d+uidNumber=%d,"
1913 "cn=peercred,cn=external,cn=auth",
1914 (int) gid, (int) uid );
1915 assert( authid.bv_len <=
1916 STRLENOF( "gidNumber=4294967295+uidNumber=4294967295,"
1917 "cn=peercred,cn=external,cn=auth" ) );
1922 #endif /* LDAP_PF_LOCAL */
1924 # ifdef LDAP_PF_INET6
1926 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1927 peeraddr = inet_ntoa( *((struct in_addr *)
1928 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1929 sprintf( peername, "IP=%s:%d",
1930 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1931 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1933 char addr[INET6_ADDRSTRLEN];
1935 peeraddr = (char *) inet_ntop( AF_INET6,
1936 &from.sa_in6_addr.sin6_addr,
1937 addr, sizeof addr );
1938 sprintf( peername, "IP=[%s]:%d",
1939 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1940 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1943 # endif /* LDAP_PF_INET6 */
1946 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1947 sprintf( peername, "IP=%s:%d",
1948 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1949 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1957 if ( ( from.sa_addr.sa_family == AF_INET )
1958 #ifdef LDAP_PF_INET6
1959 || ( from.sa_addr.sa_family == AF_INET6 )
1960 #endif /* LDAP_PF_INET6 */
1964 #ifdef SLAPD_RLOOKUPS
1965 if ( use_reverse_lookup ) {
1967 if (ldap_pvt_get_hname( (const struct sockaddr *)&from, len, hbuf,
1968 sizeof(hbuf), &herr ) == 0) {
1969 ldap_pvt_str2lower( hbuf );
1973 #endif /* SLAPD_RLOOKUPS */
1978 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_tcpd_mutex );
1979 rc = hosts_ctl("slapd",
1980 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1981 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1982 SLAP_STRING_UNKNOWN );
1983 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_tcpd_mutex );
1986 Statslog( LDAP_DEBUG_STATS,
1987 "fd=%ld DENIED from %s (%s)\n",
1989 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1990 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1996 #endif /* HAVE_TCPD */
2000 if ( sl->sl_is_tls ) cflag |= CONN_IS_TLS;
2002 c = connection_init(sfd, sl,
2003 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
2004 peername, cflag, ssf,
2005 authid.bv_val ? &authid : NULL
2006 LDAP_PF_LOCAL_SENDMSG_ARG(&peerbv));
2008 if( authid.bv_val ) ch_free(authid.bv_val);
2011 Debug( LDAP_DEBUG_ANY,
2012 "daemon: connection_init(%ld, %s, %s) failed.\n",
2013 (long) sfd, peername, sl->sl_name.bv_val );
2018 Statslog( LDAP_DEBUG_STATS,
2019 "conn=%ld fd=%ld ACCEPT from %s (%s)\n",
2020 c->c_connid, (long) sfd, peername, sl->sl_name.bv_val,
2027 slap_listener_thread(
2032 Listener *sl = (Listener *)ptr;
2034 rc = slap_listener( sl );
2036 if( rc != LDAP_SUCCESS ) {
2037 Debug( LDAP_DEBUG_ANY,
2038 "slap_listener_thread(%s): failed err=%d",
2039 sl->sl_url.bv_val, rc, 0 );
2046 slap_listener_activate(
2051 Debug( LDAP_DEBUG_TRACE, "slap_listener_activate(%d): %s\n",
2052 sl->sl_sd, sl->sl_busy ? "busy" : "", 0 );
2056 rc = ldap_pvt_thread_pool_submit( &connection_pool,
2057 slap_listener_thread, (void *) sl );
2060 Debug( LDAP_DEBUG_ANY,
2061 "listener_activate(%d): submit failed (%d)\n",
2072 time_t last_idle_check = 0;
2075 #define SLAPD_IDLE_CHECK_LIMIT 4
2077 if ( global_idletimeout > 0 ) {
2078 last_idle_check = slap_get_time();
2081 slapd_add( wake_sds[0], 0, NULL );
2083 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2084 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ) continue;
2086 #ifdef LDAP_CONNECTIONLESS
2087 /* Since this is connectionless, the data port is the
2088 * listening port. The listen() and accept() calls
2091 if ( slap_listeners[l]->sl_is_udp )
2093 #endif /* LDAP_CONNECTIONLESS */
2095 /* FIXME: TCP-only! */
2096 #ifdef LDAP_TCP_BUFFER
2098 int origsize, size, realsize, rc;
2100 char buf[ SLAP_TEXT_BUFLEN ];
2103 if ( slap_listeners[l]->sl_tcp_rmem > 0 ) {
2104 size = slap_listeners[l]->sl_tcp_rmem;
2105 } else if ( slapd_tcp_rmem > 0 ) {
2106 size = slapd_tcp_rmem;
2110 optlen = sizeof( origsize );
2111 rc = getsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2118 int err = sock_errno();
2119 Debug( LDAP_DEBUG_ANY,
2120 "slapd_daemon_task: getsockopt(SO_RCVBUF) failed errno=%d (%s)\n",
2121 err, sock_errstr(err), 0 );
2124 optlen = sizeof( size );
2125 rc = setsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2128 (const void *)&size,
2132 int err = sock_errno();
2133 Debug( LDAP_DEBUG_ANY,
2134 "slapd_daemon_task: setsockopt(SO_RCVBUF) failed errno=%d (%s)\n",
2135 err, sock_errstr(err), 0 );
2138 optlen = sizeof( realsize );
2139 rc = getsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2146 int err = sock_errno();
2147 Debug( LDAP_DEBUG_ANY,
2148 "slapd_daemon_task: getsockopt(SO_RCVBUF) failed errno=%d (%s)\n",
2149 err, sock_errstr(err), 0 );
2152 snprintf( buf, sizeof( buf ),
2153 "url=%s (#%d) RCVBUF original size=%d requested size=%d real size=%d",
2154 slap_listeners[l]->sl_url.bv_val, l, origsize, size, realsize );
2155 Debug( LDAP_DEBUG_ANY,
2156 "slapd_daemon_task: %s\n",
2161 if ( slap_listeners[l]->sl_tcp_wmem > 0 ) {
2162 size = slap_listeners[l]->sl_tcp_wmem;
2163 } else if ( slapd_tcp_wmem > 0 ) {
2164 size = slapd_tcp_wmem;
2168 optlen = sizeof( origsize );
2169 rc = getsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2176 int err = sock_errno();
2177 Debug( LDAP_DEBUG_ANY,
2178 "slapd_daemon_task: getsockopt(SO_SNDBUF) failed errno=%d (%s)\n",
2179 err, sock_errstr(err), 0 );
2182 optlen = sizeof( size );
2183 rc = setsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2186 (const void *)&size,
2190 int err = sock_errno();
2191 Debug( LDAP_DEBUG_ANY,
2192 "slapd_daemon_task: setsockopt(SO_SNDBUF) failed errno=%d (%s)",
2193 err, sock_errstr(err), 0 );
2196 optlen = sizeof( realsize );
2197 rc = getsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2204 int err = sock_errno();
2205 Debug( LDAP_DEBUG_ANY,
2206 "slapd_daemon_task: getsockopt(SO_SNDBUF) failed errno=%d (%s)\n",
2207 err, sock_errstr(err), 0 );
2210 snprintf( buf, sizeof( buf ),
2211 "url=%s (#%d) SNDBUF original size=%d requested size=%d real size=%d",
2212 slap_listeners[l]->sl_url.bv_val, l, origsize, size, realsize );
2213 Debug( LDAP_DEBUG_ANY,
2214 "slapd_daemon_task: %s\n",
2218 #endif /* LDAP_TCP_BUFFER */
2220 if ( listen( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ), SLAPD_LISTEN_BACKLOG ) == -1 ) {
2221 int err = sock_errno();
2223 #ifdef LDAP_PF_INET6
2224 /* If error is EADDRINUSE, we are trying to listen to INADDR_ANY and
2225 * we are already listening to in6addr_any, then we want to ignore
2226 * this and continue.
2228 if ( err == EADDRINUSE ) {
2230 struct sockaddr_in sa = slap_listeners[l]->sl_sa.sa_in_addr;
2231 struct sockaddr_in6 sa6;
2233 if ( sa.sin_family == AF_INET &&
2234 sa.sin_addr.s_addr == htonl(INADDR_ANY) ) {
2235 for ( i = 0 ; i < l; i++ ) {
2236 sa6 = slap_listeners[i]->sl_sa.sa_in6_addr;
2237 if ( sa6.sin6_family == AF_INET6 &&
2238 !memcmp( &sa6.sin6_addr, &in6addr_any,
2239 sizeof(struct in6_addr) ) )
2246 /* We are already listening to in6addr_any */
2247 Debug( LDAP_DEBUG_CONNS,
2248 "daemon: Attempt to listen to 0.0.0.0 failed, "
2249 "already listening on ::, assuming IPv4 included\n",
2251 slapd_close( slap_listeners[l]->sl_sd );
2252 slap_listeners[l]->sl_sd = AC_SOCKET_INVALID;
2257 #endif /* LDAP_PF_INET6 */
2258 Debug( LDAP_DEBUG_ANY,
2259 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
2260 slap_listeners[l]->sl_url.bv_val, err,
2265 /* make the listening socket non-blocking */
2266 if ( ber_pvt_socket_set_nonblock( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ), 1 ) < 0 ) {
2267 Debug( LDAP_DEBUG_ANY, "slapd_daemon_task: "
2268 "set nonblocking on a listening socket failed\n",
2274 slapd_add( slap_listeners[l]->sl_sd, 0, slap_listeners[l] );
2277 #ifdef HAVE_NT_SERVICE_MANAGER
2278 if ( started_event != NULL ) {
2279 ldap_pvt_thread_cond_signal( &started_event );
2281 #endif /* HAVE_NT_SERVICE_MANAGER */
2283 /* initialization complete. Here comes the loop. */
2285 while ( !slapd_shutdown ) {
2290 #if SLAP_EVENTS_ARE_INDEXED
2291 ber_socket_t nrfds, nwfds;
2292 #endif /* SLAP_EVENTS_ARE_INDEXED */
2293 #define SLAPD_EBADF_LIMIT 16
2300 struct timeval *tvp;
2306 now = slap_get_time();
2308 if ( global_idletimeout > 0 || chk_writetime ) {
2310 /* Set the select timeout.
2311 * Don't just truncate, preserve the fractions of
2312 * seconds to prevent sleeping for zero time.
2314 if ( chk_writetime ) {
2315 tv.tv_sec = global_writetimeout;
2317 if ( difftime( chk_writetime, now ) < 0 )
2320 tv.tv_sec = global_idletimeout / SLAPD_IDLE_CHECK_LIMIT;
2321 tv.tv_usec = global_idletimeout - \
2322 ( tv.tv_sec * SLAPD_IDLE_CHECK_LIMIT );
2323 tv.tv_usec *= 1000000 / SLAPD_IDLE_CHECK_LIMIT;
2324 if ( difftime( last_idle_check +
2325 global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 )
2329 connections_timeout_idle( now );
2330 last_idle_check = now;
2338 if ( slapd_gentle_shutdown ) {
2339 ber_socket_t active;
2341 if ( slapd_gentle_shutdown == 1 ) {
2343 Debug( LDAP_DEBUG_ANY, "slapd gentle shutdown\n", 0, 0, 0 );
2344 close_listeners( 1 );
2345 frontendDB->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
2346 LDAP_STAILQ_FOREACH(be, &backendDB, be_next) {
2347 be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
2349 slapd_gentle_shutdown = 2;
2352 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
2353 active = slap_daemon.sd_nactives;
2354 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
2355 if ( active == 0 ) {
2363 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
2365 nwriters = slap_daemon.sd_nwriters;
2367 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2368 Listener *lr = slap_listeners[l];
2370 if ( lr->sl_sd == AC_SOCKET_INVALID ) continue;
2372 if ( lr->sl_mute || lr->sl_busy )
2374 SLAP_SOCK_CLR_READ( lr->sl_sd );
2376 SLAP_SOCK_SET_READ( lr->sl_sd );
2382 nfds = SLAP_EVENT_MAX;
2384 if (( chk_writetime || global_idletimeout ) && slap_daemon.sd_nactives ) at = 1;
2386 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
2389 #if defined(HAVE_YIELDING_SELECT) || defined(NO_THREADS)
2390 && ( tv.tv_sec || tv.tv_usec )
2391 #endif /* HAVE_YIELDING_SELECT || NO_THREADS */
2399 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2400 rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
2401 while ( rtask && cat.tv_sec && cat.tv_sec <= now ) {
2402 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
2403 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
2405 ldap_pvt_runqueue_runtask( &slapd_rq, rtask );
2406 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
2407 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2408 ldap_pvt_thread_pool_submit( &connection_pool,
2409 rtask->routine, (void *) rtask );
2410 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2412 rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
2414 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2416 if ( rtask && cat.tv_sec ) {
2417 /* NOTE: diff __should__ always be >= 0,
2418 * AFAI understand; however (ITS#4872),
2419 * time_t might be unsigned in some systems,
2420 * while difftime() returns a double */
2421 double diff = difftime( cat.tv_sec, now );
2425 if ( tvp == NULL || diff < tv.tv_sec ) {
2432 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2433 Listener *lr = slap_listeners[l];
2435 if ( lr->sl_sd == AC_SOCKET_INVALID ) {
2439 if ( lr->sl_mute ) {
2440 Debug( LDAP_DEBUG_CONNS,
2441 "daemon: " SLAP_EVENT_FNAME ": "
2442 "listen=%d muted\n",
2447 if ( lr->sl_busy ) {
2448 Debug( LDAP_DEBUG_CONNS,
2449 "daemon: " SLAP_EVENT_FNAME ": "
2455 Debug( LDAP_DEBUG_CONNS,
2456 "daemon: " SLAP_EVENT_FNAME ": "
2457 "listen=%d active_threads=%d tvp=%s\n",
2458 lr->sl_sd, at, tvp == NULL ? "NULL" : "zero" );
2461 SLAP_EVENT_WAIT( tvp, &ns );
2463 case -1: { /* failure - try again */
2464 int err = sock_errno();
2466 if ( err != EINTR ) {
2469 /* Don't log unless we got it twice in a row */
2470 if ( !( ebadf & 1 ) ) {
2471 Debug( LDAP_DEBUG_ANY,
2477 sock_errstr( err ) );
2479 if ( ebadf >= SLAPD_EBADF_LIMIT ) {
2486 case 0: /* timeout - let threads run */
2488 #ifndef HAVE_YIELDING_SELECT
2489 Debug( LDAP_DEBUG_CONNS, "daemon: " SLAP_EVENT_FNAME
2490 "timeout - yielding\n",
2493 ldap_pvt_thread_yield();
2494 #endif /* ! HAVE_YIELDING_SELECT */
2497 default: /* something happened - deal with it */
2498 if ( slapd_shutdown ) continue;
2501 Debug( LDAP_DEBUG_CONNS,
2502 "daemon: activity on %d descriptor%s\n",
2503 ns, ns != 1 ? "s" : "", 0 );
2507 #if SLAP_EVENTS_ARE_INDEXED
2508 if ( SLAP_EVENT_IS_READ( wake_sds[0] ) ) {
2510 SLAP_EVENT_CLR_READ( wake_sds[0] );
2512 tcp_read( SLAP_FD2SOCK(wake_sds[0]), c, sizeof(c) );
2513 Debug( LDAP_DEBUG_CONNS, "daemon: waked\n", 0, 0, 0 );
2517 /* The event slot equals the descriptor number - this is
2518 * true for Unix select and poll. We treat Windows select
2519 * like this too, even though it's a kludge.
2521 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2524 if ( ns <= 0 ) break;
2525 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ) continue;
2526 #ifdef LDAP_CONNECTIONLESS
2527 if ( slap_listeners[l]->sl_is_udp ) continue;
2528 #endif /* LDAP_CONNECTIONLESS */
2529 if ( !SLAP_EVENT_IS_READ( slap_listeners[l]->sl_sd ) ) continue;
2532 SLAP_EVENT_CLR_READ( slap_listeners[l]->sl_sd );
2533 SLAP_EVENT_CLR_WRITE( slap_listeners[l]->sl_sd );
2536 rc = slap_listener_activate( slap_listeners[l] );
2539 /* bypass the following tests if no descriptors left */
2541 #ifndef HAVE_YIELDING_SELECT
2542 ldap_pvt_thread_yield();
2543 #endif /* HAVE_YIELDING_SELECT */
2547 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
2550 for ( i = 0; i < nfds; i++ ) {
2553 r = SLAP_EVENT_IS_READ( i );
2554 /* writefds was not initialized if nwriters was zero */
2555 w = nwriters ? SLAP_EVENT_IS_WRITE( i ) : 0;
2557 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
2558 r ? "r" : "", w ? "w" : "" );
2568 if ( ns <= 0 ) break;
2570 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
2572 /* loop through the writers */
2573 for ( i = 0; nwfds > 0; i++ ) {
2575 if ( ! SLAP_EVENT_IS_WRITE( i ) ) continue;
2578 SLAP_EVENT_CLR_WRITE( wd );
2581 Debug( LDAP_DEBUG_CONNS,
2582 "daemon: write active on %d\n",
2586 * NOTE: it is possible that the connection was closed
2587 * and that the stream is now inactive.
2588 * connection_write() must validate the stream is still
2591 * ITS#4338: if the stream is invalid, there is no need to
2592 * close it here. It has already been closed in connection.c.
2594 if ( connection_write( wd ) < 0 ) {
2595 if ( SLAP_EVENT_IS_READ( wd ) ) {
2596 SLAP_EVENT_CLR_READ( (unsigned) wd );
2602 for ( i = 0; nrfds > 0; i++ ) {
2604 if ( ! SLAP_EVENT_IS_READ( i ) ) continue;
2606 SLAP_EVENT_CLR_READ( rd );
2609 Debug ( LDAP_DEBUG_CONNS,
2610 "daemon: read activity on %d\n", rd, 0, 0 );
2612 * NOTE: it is possible that the connection was closed
2613 * and that the stream is now inactive.
2614 * connection_read() must valid the stream is still
2618 connection_read_activate( rd );
2620 #else /* !SLAP_EVENTS_ARE_INDEXED */
2622 /* The events are returned in an arbitrary list. This is true
2623 * for /dev/poll, epoll and kqueue. In order to prioritize things
2624 * so that we can handle wake_sds first, listeners second, and then
2625 * all other connections last (as we do for select), we would need
2626 * to use multiple event handles and cascade them.
2628 * That seems like a bit of hassle. So the wake_sds check has been
2629 * skipped. For epoll and kqueue we can associate arbitrary data with
2630 * an event, so we could use pointers to the listener structure
2631 * instead of just the file descriptor. For /dev/poll we have to
2632 * search the listeners array for a matching descriptor.
2634 * We now handle wake events when we see them; they are not given
2638 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
2640 for ( i = 0; i < ns; i++ ) {
2643 /* Don't log listener events */
2644 if ( SLAP_EVENT_IS_LISTENER( i )
2645 #ifdef LDAP_CONNECTIONLESS
2646 && !( (SLAP_EVENT_LISTENER( i ))->sl_is_udp )
2647 #endif /* LDAP_CONNECTIONLESS */
2653 fd = SLAP_EVENT_FD( i );
2654 /* Don't log internal wake events */
2655 if ( fd == wake_sds[0] ) continue;
2657 r = SLAP_EVENT_IS_READ( i );
2658 w = SLAP_EVENT_IS_WRITE( i );
2660 Debug( LDAP_DEBUG_CONNS, " %d%s%s", fd,
2661 r ? "r" : "", w ? "w" : "" );
2664 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
2665 #endif /* LDAP_DEBUG */
2667 for ( i = 0; i < ns; i++ ) {
2668 int rc = 1, fd, w = 0, r = 0;
2670 if ( SLAP_EVENT_IS_LISTENER( i ) ) {
2671 rc = slap_listener_activate( SLAP_EVENT_LISTENER( i ) );
2674 /* If we found a regular listener, rc is now zero, and we
2675 * can skip the data portion. But if it was a UDP listener
2676 * then rc is still 1, and we want to handle the data.
2679 fd = SLAP_EVENT_FD( i );
2681 /* Handle wake events */
2682 if ( fd == wake_sds[0] ) {
2685 tcp_read( SLAP_FD2SOCK(wake_sds[0]), c, sizeof(c) );
2689 if ( SLAP_EVENT_IS_WRITE( i ) ) {
2690 Debug( LDAP_DEBUG_CONNS,
2691 "daemon: write active on %d\n",
2694 SLAP_EVENT_CLR_WRITE( i );
2698 * NOTE: it is possible that the connection was closed
2699 * and that the stream is now inactive.
2700 * connection_write() must valid the stream is still
2703 if ( connection_write( fd ) < 0 ) {
2707 /* If event is a read */
2708 if ( SLAP_EVENT_IS_READ( i ))
2711 Debug( LDAP_DEBUG_CONNS,
2712 "daemon: read active on %d\n",
2716 SLAP_EVENT_CLR_READ( i );
2719 /* Don't keep reporting the hangup
2721 if ( SLAP_SOCK_IS_ACTIVE( fd )) {
2722 SLAP_EPOLL_SOCK_SET( fd, EPOLLET );
2726 connection_read_activate( fd );
2730 #endif /* SLAP_EVENTS_ARE_INDEXED */
2732 #ifndef HAVE_YIELDING_SELECT
2733 ldap_pvt_thread_yield();
2734 #endif /* ! HAVE_YIELDING_SELECT */
2737 if ( slapd_shutdown == 1 ) {
2738 Debug( LDAP_DEBUG_ANY,
2739 "daemon: shutdown requested and initiated.\n",
2742 } else if ( slapd_shutdown == 2 ) {
2743 #ifdef HAVE_NT_SERVICE_MANAGER
2744 Debug( LDAP_DEBUG_ANY,
2745 "daemon: shutdown initiated by Service Manager.\n",
2747 #else /* !HAVE_NT_SERVICE_MANAGER */
2748 Debug( LDAP_DEBUG_ANY,
2749 "daemon: abnormal condition, shutdown initiated.\n",
2751 #endif /* !HAVE_NT_SERVICE_MANAGER */
2753 Debug( LDAP_DEBUG_ANY,
2754 "daemon: no active streams, shutdown initiated.\n",
2758 if ( slapd_gentle_shutdown != 2 ) close_listeners ( 0 );
2760 if ( !slapd_gentle_shutdown ) {
2761 slapd_abrupt_shutdown = 1;
2762 connections_shutdown();
2765 if ( LogTest( LDAP_DEBUG_ANY )) {
2766 int t = ldap_pvt_thread_pool_backload( &connection_pool );
2767 Debug( LDAP_DEBUG_ANY,
2768 "slapd shutdown: waiting for %d operations/tasks to finish\n",
2771 ldap_pvt_thread_pool_destroy( &connection_pool, 1 );
2773 free( slap_listeners );
2774 slap_listeners = NULL;
2780 #ifdef LDAP_CONNECTIONLESS
2782 connectionless_init( void )
2786 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2787 Listener *lr = slap_listeners[l];
2790 if ( !lr->sl_is_udp ) {
2794 c = connection_init( lr->sl_sd, lr, "", "",
2795 CONN_IS_UDP, (slap_ssf_t) 0, NULL
2796 LDAP_PF_LOCAL_SENDMSG_ARG(NULL));
2799 Debug( LDAP_DEBUG_TRACE,
2800 "connectionless_init: failed on %s (%d)\n",
2801 lr->sl_url, lr->sl_sd, 0 );
2809 #endif /* LDAP_CONNECTIONLESS */
2812 slapd_daemon( void )
2816 #ifdef LDAP_CONNECTIONLESS
2817 connectionless_init();
2818 #endif /* LDAP_CONNECTIONLESS */
2820 #define SLAPD_LISTENER_THREAD 1
2821 #if defined( SLAPD_LISTENER_THREAD )
2823 ldap_pvt_thread_t listener_tid;
2825 /* listener as a separate THREAD */
2826 rc = ldap_pvt_thread_create( &listener_tid,
2827 0, slapd_daemon_task, NULL );
2830 Debug( LDAP_DEBUG_ANY,
2831 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
2835 /* wait for the listener thread to complete */
2836 ldap_pvt_thread_join( listener_tid, (void *)NULL );
2838 #else /* ! SLAPD_LISTENER_THREAD */
2839 /* experimental code */
2840 slapd_daemon_task( NULL );
2841 #endif /* ! SLAPD_LISTENER_THREAD */
2849 #if defined( HAVE_WINSOCK2 )
2850 WORD wVersionRequested;
2854 wVersionRequested = MAKEWORD( 2, 0 );
2856 err = WSAStartup( wVersionRequested, &wsaData );
2858 /* Tell the user that we couldn't find a usable */
2863 /* Confirm that the WinSock DLL supports 2.0.*/
2864 /* Note that if the DLL supports versions greater */
2865 /* than 2.0 in addition to 2.0, it will still return */
2866 /* 2.0 in wVersion since that is the version we */
2869 if ( LOBYTE( wsaData.wVersion ) != 2 ||
2870 HIBYTE( wsaData.wVersion ) != 0 )
2872 /* Tell the user that we couldn't find a usable */
2878 /* The WinSock DLL is acceptable. Proceed. */
2879 #elif defined( HAVE_WINSOCK )
2881 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) return -1;
2882 #endif /* ! HAVE_WINSOCK2 && ! HAVE_WINSOCK */
2890 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
2892 #endif /* HAVE_WINSOCK2 || HAVE_WINSOCK */
2899 slap_sig_shutdown( int sig )
2901 int save_errno = errno;
2904 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
2908 * If the NT Service Manager is controlling the server, we don't
2909 * want SIGBREAK to kill the server. For some strange reason,
2910 * SIGBREAK is generated when a user logs out.
2913 #if defined(HAVE_NT_SERVICE_MANAGER) && defined(SIGBREAK)
2914 if (is_NT_Service && sig == SIGBREAK) {
2917 #endif /* HAVE_NT_SERVICE_MANAGER && SIGBREAK */
2919 if (sig == SIGHUP && global_gentlehup && slapd_gentle_shutdown == 0) {
2920 slapd_gentle_shutdown = 1;
2929 /* reinstall self */
2930 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
2936 slap_sig_wake( int sig )
2938 int save_errno = errno;
2942 /* reinstall self */
2943 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
2950 slapd_add_internal( ber_socket_t s, int isactive )
2952 slapd_add( s, isactive, NULL );
2956 slapd_get_listeners( void )
2958 return slap_listeners;
2962 slap_wake_listener()