2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2014 The OpenLDAP Foundation.
5 * Portions Copyright 2007 by Howard Chu, Symas Corporation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
33 #include <ac/socket.h>
34 #include <ac/string.h>
36 #include <ac/unistd.h>
39 #include "ldap_pvt_thread.h"
48 #if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL)
49 # include <sys/epoll.h>
50 #elif defined(SLAP_X_DEVPOLL) && defined(HAVE_SYS_DEVPOLL_H) && defined(HAVE_DEVPOLL)
51 # include <sys/types.h>
52 # include <sys/stat.h>
54 # include <sys/devpoll.h>
55 #endif /* ! epoll && ! /dev/poll */
58 int allow_severity = LOG_INFO;
59 int deny_severity = LOG_NOTICE;
60 #endif /* TCP Wrappers */
63 # include <sys/stat.h>
64 /* this should go in <ldap.h> as soon as it is accepted */
65 # define LDAPI_MOD_URLEXT "x-mod"
66 #endif /* LDAP_PF_LOCAL */
69 int slap_inet4or6 = AF_UNSPEC;
71 int slap_inet4or6 = AF_INET;
76 ber_socket_t dtblsize;
77 slap_ssf_t local_ssf = LDAP_PVT_SASL_LOCAL_SSF;
78 struct runqueue_s slapd_rq;
80 #ifndef SLAPD_MAX_DAEMON_THREADS
81 #define SLAPD_MAX_DAEMON_THREADS 16
83 int slapd_daemon_threads = 1;
84 int slapd_daemon_mask;
86 #ifdef LDAP_TCP_BUFFER
89 #endif /* LDAP_TCP_BUFFER */
91 Listener **slap_listeners = NULL;
92 static volatile sig_atomic_t listening = 1; /* 0 when slap_listeners closed */
93 static ldap_pvt_thread_t *listener_tid;
95 #ifndef SLAPD_LISTEN_BACKLOG
96 #define SLAPD_LISTEN_BACKLOG 1024
97 #endif /* ! SLAPD_LISTEN_BACKLOG */
99 #define DAEMON_ID(fd) (fd & slapd_daemon_mask)
101 static ber_socket_t wake_sds[SLAPD_MAX_DAEMON_THREADS][2];
104 static volatile int waking;
106 #define WAKE_LISTENER(l,w) do { \
107 if ((w) && ++waking < 5) { \
108 tcp_write( SLAP_FD2SOCK(wake_sds[l][1]), "0", 1 ); \
111 #else /* ! NO_THREADS */
112 #define WAKE_LISTENER(l,w) do { \
114 tcp_write( SLAP_FD2SOCK(wake_sds[l][1]), "0", 1 ); \
117 #endif /* ! NO_THREADS */
119 volatile sig_atomic_t slapd_shutdown = 0;
120 volatile sig_atomic_t slapd_gentle_shutdown = 0;
121 volatile sig_atomic_t slapd_abrupt_shutdown = 0;
124 ldap_pvt_thread_mutex_t slapd_ws_mutex;
125 SOCKET *slapd_ws_sockets;
129 #define SD_LISTENER 8
133 static ldap_pvt_thread_mutex_t sd_tcpd_mutex;
134 #endif /* TCP Wrappers */
136 typedef struct slap_daemon_st {
137 ldap_pvt_thread_mutex_t sd_mutex;
139 ber_socket_t sd_nactives;
143 #if defined(HAVE_EPOLL)
144 struct epoll_event *sd_epolls;
147 #elif defined(SLAP_X_DEVPOLL) && defined(HAVE_DEVPOLL)
149 struct pollfd *sd_pollfd;
153 #else /* ! epoll && ! /dev/poll */
157 #else /* ! HAVE_WINSOCK */
161 #endif /* ! HAVE_WINSOCK */
162 #endif /* ! epoll && ! /dev/poll */
165 static slap_daemon_st slap_daemon[SLAPD_MAX_DAEMON_THREADS];
168 * NOTE: naming convention for macros:
170 * - SLAP_SOCK_* and SLAP_EVENT_* for public interface that deals
171 * with file descriptors and events respectively
173 * - SLAP_<type>_* for private interface; type by now is one of
174 * EPOLL, DEVPOLL, SELECT
176 * private interface should not be used in the code.
178 #if defined(HAVE_EPOLL)
179 /***************************************
180 * Use epoll infrastructure - epoll(4) *
181 ***************************************/
182 # define SLAP_EVENT_FNAME "epoll"
183 # define SLAP_EVENTS_ARE_INDEXED 0
184 # define SLAP_EPOLL_SOCK_IX(t,s) (slap_daemon[t].sd_index[(s)])
185 # define SLAP_EPOLL_SOCK_EP(t,s) (slap_daemon[t].sd_epolls[SLAP_EPOLL_SOCK_IX(t,s)])
186 # define SLAP_EPOLL_SOCK_EV(t,s) (SLAP_EPOLL_SOCK_EP(t,s).events)
187 # define SLAP_SOCK_IS_ACTIVE(t,s) (SLAP_EPOLL_SOCK_IX(t,s) != -1)
188 # define SLAP_SOCK_NOT_ACTIVE(t,s) (SLAP_EPOLL_SOCK_IX(t,s) == -1)
189 # define SLAP_EPOLL_SOCK_IS_SET(t,s, mode) (SLAP_EPOLL_SOCK_EV(t,s) & (mode))
191 # define SLAP_SOCK_IS_READ(t,s) SLAP_EPOLL_SOCK_IS_SET(t,(s), EPOLLIN)
192 # define SLAP_SOCK_IS_WRITE(t,s) SLAP_EPOLL_SOCK_IS_SET(t,(s), EPOLLOUT)
194 # define SLAP_EPOLL_SOCK_SET(t,s, mode) do { \
195 if ( (SLAP_EPOLL_SOCK_EV(t,s) & (mode)) != (mode) ) { \
196 SLAP_EPOLL_SOCK_EV(t,s) |= (mode); \
197 epoll_ctl( slap_daemon[t].sd_epfd, EPOLL_CTL_MOD, (s), \
198 &SLAP_EPOLL_SOCK_EP(t,s) ); \
202 # define SLAP_EPOLL_SOCK_CLR(t,s, mode) do { \
203 if ( (SLAP_EPOLL_SOCK_EV(t,s) & (mode)) ) { \
204 SLAP_EPOLL_SOCK_EV(t,s) &= ~(mode); \
205 epoll_ctl( slap_daemon[t].sd_epfd, EPOLL_CTL_MOD, s, \
206 &SLAP_EPOLL_SOCK_EP(t,s) ); \
210 # define SLAP_SOCK_SET_READ(t,s) SLAP_EPOLL_SOCK_SET(t,s, EPOLLIN)
211 # define SLAP_SOCK_SET_WRITE(t,s) SLAP_EPOLL_SOCK_SET(t,s, EPOLLOUT)
213 # define SLAP_SOCK_CLR_READ(t,s) SLAP_EPOLL_SOCK_CLR(t,(s), EPOLLIN)
214 # define SLAP_SOCK_CLR_WRITE(t,s) SLAP_EPOLL_SOCK_CLR(t,(s), EPOLLOUT)
216 # define SLAP_SOCK_SET_SUSPEND(t,s) \
217 ( slap_daemon[t].sd_suspend[SLAP_EPOLL_SOCK_IX(t,s)] = 1 )
218 # define SLAP_SOCK_CLR_SUSPEND(t,s) \
219 ( slap_daemon[t].sd_suspend[SLAP_EPOLL_SOCK_IX(t,s)] = 0 )
220 # define SLAP_SOCK_IS_SUSPEND(t,s) \
221 ( slap_daemon[t].sd_suspend[SLAP_EPOLL_SOCK_IX(t,s)] == 1 )
223 # define SLAP_EPOLL_EVENT_CLR(i, mode) (revents[(i)].events &= ~(mode))
225 # define SLAP_EVENT_MAX(t) slap_daemon[t].sd_nfds
227 /* If a Listener address is provided, store that as the epoll data.
228 * Otherwise, store the address of this socket's slot in the
229 * index array. If we can't do this add, the system is out of
230 * resources and we need to shutdown.
232 # define SLAP_SOCK_ADD(t, s, l) do { \
234 SLAP_EPOLL_SOCK_IX(t,(s)) = slap_daemon[t].sd_nfds; \
235 SLAP_EPOLL_SOCK_EP(t,(s)).data.ptr = (l) ? (l) : (void *)(&SLAP_EPOLL_SOCK_IX(t,s)); \
236 SLAP_EPOLL_SOCK_EV(t,(s)) = EPOLLIN; \
237 rc = epoll_ctl(slap_daemon[t].sd_epfd, EPOLL_CTL_ADD, \
238 (s), &SLAP_EPOLL_SOCK_EP(t,(s))); \
240 slap_daemon[t].sd_nfds++; \
242 Debug( LDAP_DEBUG_ANY, \
243 "daemon: epoll_ctl(ADD,fd=%d) failed, errno=%d, shutting down\n", \
245 slapd_shutdown = 2; \
249 # define SLAP_EPOLL_EV_LISTENER(t,ptr) \
250 (((int *)(ptr) >= slap_daemon[t].sd_index && \
251 (int *)(ptr) <= &slap_daemon[t].sd_index[dtblsize]) ? 0 : 1 )
253 # define SLAP_EPOLL_EV_PTRFD(t,ptr) (SLAP_EPOLL_EV_LISTENER(t,ptr) ? \
254 ((Listener *)ptr)->sl_sd : \
255 (ber_socket_t) ((int *)(ptr) - slap_daemon[t].sd_index))
257 # define SLAP_SOCK_DEL(t,s) do { \
258 int fd, rc, index = SLAP_EPOLL_SOCK_IX(t,(s)); \
259 if ( index < 0 ) break; \
260 rc = epoll_ctl(slap_daemon[t].sd_epfd, EPOLL_CTL_DEL, \
261 (s), &SLAP_EPOLL_SOCK_EP(t,(s))); \
262 slap_daemon[t].sd_epolls[index] = \
263 slap_daemon[t].sd_epolls[slap_daemon[t].sd_nfds-1]; \
264 fd = SLAP_EPOLL_EV_PTRFD(t,slap_daemon[t].sd_epolls[index].data.ptr); \
265 slap_daemon[t].sd_index[fd] = index; \
266 slap_daemon[t].sd_index[(s)] = -1; \
267 slap_daemon[t].sd_nfds--; \
270 # define SLAP_EVENT_CLR_READ(i) SLAP_EPOLL_EVENT_CLR((i), EPOLLIN)
271 # define SLAP_EVENT_CLR_WRITE(i) SLAP_EPOLL_EVENT_CLR((i), EPOLLOUT)
273 # define SLAP_EPOLL_EVENT_CHK(i, mode) (revents[(i)].events & mode)
275 # define SLAP_EVENT_IS_READ(i) SLAP_EPOLL_EVENT_CHK((i), EPOLLIN)
276 # define SLAP_EVENT_IS_WRITE(i) SLAP_EPOLL_EVENT_CHK((i), EPOLLOUT)
277 # define SLAP_EVENT_IS_LISTENER(t,i) SLAP_EPOLL_EV_LISTENER(t,revents[(i)].data.ptr)
278 # define SLAP_EVENT_LISTENER(t,i) ((Listener *)(revents[(i)].data.ptr))
280 # define SLAP_EVENT_FD(t,i) SLAP_EPOLL_EV_PTRFD(t,revents[(i)].data.ptr)
282 # define SLAP_SOCK_INIT(t) do { \
284 slap_daemon[t].sd_epolls = ch_calloc(1, \
285 ( sizeof(struct epoll_event) * 2 \
286 + sizeof(int) ) * dtblsize * 2); \
287 slap_daemon[t].sd_index = (int *)&slap_daemon[t].sd_epolls[ 2 * dtblsize ]; \
288 slap_daemon[t].sd_epfd = epoll_create( dtblsize / slapd_daemon_threads ); \
289 for ( j = 0; j < dtblsize; j++ ) slap_daemon[t].sd_index[j] = -1; \
292 # define SLAP_SOCK_DESTROY(t) do { \
293 if ( slap_daemon[t].sd_epolls != NULL ) { \
294 ch_free( slap_daemon[t].sd_epolls ); \
295 slap_daemon[t].sd_epolls = NULL; \
296 slap_daemon[t].sd_index = NULL; \
297 close( slap_daemon[t].sd_epfd ); \
301 # define SLAP_EVENT_DECL struct epoll_event *revents
303 # define SLAP_EVENT_INIT(t) do { \
304 revents = slap_daemon[t].sd_epolls + dtblsize; \
307 # define SLAP_EVENT_WAIT(t, tvp, nsp) do { \
308 *(nsp) = epoll_wait( slap_daemon[t].sd_epfd, revents, \
309 dtblsize, (tvp) ? (tvp)->tv_sec * 1000 : -1 ); \
312 #elif defined(SLAP_X_DEVPOLL) && defined(HAVE_DEVPOLL)
314 /*************************************************************
315 * Use Solaris' (>= 2.7) /dev/poll infrastructure - poll(7d) *
316 *************************************************************/
317 # define SLAP_EVENT_FNAME "/dev/poll"
318 # define SLAP_EVENTS_ARE_INDEXED 0
320 * - sd_index is used much like with epoll()
321 * - sd_l is maintained as an array containing the address
322 * of the listener; the index is the fd itself
323 * - sd_pollfd is used to keep track of what data has been
324 * registered in /dev/poll
326 # define SLAP_DEVPOLL_SOCK_IX(t,s) (slap_daemon[t].sd_index[(s)])
327 # define SLAP_DEVPOLL_SOCK_LX(t,s) (slap_daemon[t].sd_l[(s)])
328 # define SLAP_DEVPOLL_SOCK_EP(t,s) (slap_daemon[t].sd_pollfd[SLAP_DEVPOLL_SOCK_IX(t,(s))])
329 # define SLAP_DEVPOLL_SOCK_FD(t,s) (SLAP_DEVPOLL_SOCK_EP(t,(s)).fd)
330 # define SLAP_DEVPOLL_SOCK_EV(t,s) (SLAP_DEVPOLL_SOCK_EP(t,(s)).events)
331 # define SLAP_SOCK_IS_ACTIVE(t,s) (SLAP_DEVPOLL_SOCK_IX(t,(s)) != -1)
332 # define SLAP_SOCK_NOT_ACTIVE(t,s) (SLAP_DEVPOLL_SOCK_IX(t,(s)) == -1)
333 # define SLAP_SOCK_IS_SET(t,s, mode) (SLAP_DEVPOLL_SOCK_EV(t,(s)) & (mode))
335 # define SLAP_SOCK_IS_READ(t,s) SLAP_SOCK_IS_SET(t,(s), POLLIN)
336 # define SLAP_SOCK_IS_WRITE(t,s) SLAP_SOCK_IS_SET(t,(s), POLLOUT)
338 /* as far as I understand, any time we need to communicate with the kernel
339 * about the number and/or properties of a file descriptor we need it to
340 * wait for, we have to rewrite the whole set */
341 # define SLAP_DEVPOLL_WRITE_POLLFD(t,s, pfd, n, what, shdn) do { \
343 size_t size = (n) * sizeof( struct pollfd ); \
344 /* FIXME: use pwrite? */ \
345 rc = write( slap_daemon[t].sd_dpfd, (pfd), size ); \
346 if ( rc != size ) { \
347 Debug( LDAP_DEBUG_ANY, "daemon: " SLAP_EVENT_FNAME ": " \
348 "%s fd=%d failed errno=%d\n", \
349 (what), (s), errno ); \
351 slapd_shutdown = 2; \
356 # define SLAP_DEVPOLL_SOCK_SET(t,s, mode) do { \
357 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_SET_%s(%d)=%d\n", \
358 (mode) == POLLIN ? "READ" : "WRITE", (s), \
359 ( (SLAP_DEVPOLL_SOCK_EV(t,(s)) & (mode)) != (mode) ) ); \
360 if ( (SLAP_DEVPOLL_SOCK_EV(t,(s)) & (mode)) != (mode) ) { \
362 SLAP_DEVPOLL_SOCK_EV(t,(s)) |= (mode); \
363 pfd.fd = SLAP_DEVPOLL_SOCK_FD(t,(s)); \
364 pfd.events = /* (mode) */ SLAP_DEVPOLL_SOCK_EV(t,(s)); \
365 SLAP_DEVPOLL_WRITE_POLLFD(t,(s), &pfd, 1, "SET", 0); \
369 # define SLAP_DEVPOLL_SOCK_CLR(t,s, mode) do { \
370 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_CLR_%s(%d)=%d\n", \
371 (mode) == POLLIN ? "READ" : "WRITE", (s), \
372 ( (SLAP_DEVPOLL_SOCK_EV(t,(s)) & (mode)) == (mode) ) ); \
373 if ((SLAP_DEVPOLL_SOCK_EV(t,(s)) & (mode)) == (mode) ) { \
374 struct pollfd pfd[2]; \
375 SLAP_DEVPOLL_SOCK_EV(t,(s)) &= ~(mode); \
376 pfd[0].fd = SLAP_DEVPOLL_SOCK_FD(t,(s)); \
377 pfd[0].events = POLLREMOVE; \
378 pfd[1] = SLAP_DEVPOLL_SOCK_EP(t,(s)); \
379 SLAP_DEVPOLL_WRITE_POLLFD(t,(s), &pfd[0], 2, "CLR", 0); \
383 # define SLAP_SOCK_SET_READ(t,s) SLAP_DEVPOLL_SOCK_SET(t,s, POLLIN)
384 # define SLAP_SOCK_SET_WRITE(t,s) SLAP_DEVPOLL_SOCK_SET(t,s, POLLOUT)
386 # define SLAP_SOCK_CLR_READ(t,s) SLAP_DEVPOLL_SOCK_CLR(t,(s), POLLIN)
387 # define SLAP_SOCK_CLR_WRITE(t,s) SLAP_DEVPOLL_SOCK_CLR(t,(s), POLLOUT)
389 # define SLAP_SOCK_SET_SUSPEND(t,s) \
390 ( slap_daemon[t].sd_suspend[SLAP_DEVPOLL_SOCK_IX(t,(s))] = 1 )
391 # define SLAP_SOCK_CLR_SUSPEND(t,s) \
392 ( slap_daemon[t].sd_suspend[SLAP_DEVPOLL_SOCK_IX(t,(s))] = 0 )
393 # define SLAP_SOCK_IS_SUSPEND(t,s) \
394 ( slap_daemon[t].sd_suspend[SLAP_DEVPOLL_SOCK_IX(t,(s))] == 1 )
396 # define SLAP_DEVPOLL_EVENT_CLR(i, mode) (revents[(i)].events &= ~(mode))
398 # define SLAP_EVENT_MAX(t) slap_daemon[t].sd_nfds
400 /* If a Listener address is provided, store that in the sd_l array.
401 * If we can't do this add, the system is out of resources and we
404 # define SLAP_SOCK_ADD(t, s, l) do { \
405 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_ADD(%d, %p)\n", (s), (l), 0 ); \
406 SLAP_DEVPOLL_SOCK_IX(t,(s)) = slap_daemon[t].sd_nfds; \
407 SLAP_DEVPOLL_SOCK_LX(t,(s)) = (l); \
408 SLAP_DEVPOLL_SOCK_FD(t,(s)) = (s); \
409 SLAP_DEVPOLL_SOCK_EV(t,(s)) = POLLIN; \
410 SLAP_DEVPOLL_WRITE_POLLFD(t,(s), &SLAP_DEVPOLL_SOCK_EP((s)), 1, "ADD", 1); \
411 slap_daemon[t].sd_nfds++; \
414 # define SLAP_DEVPOLL_EV_LISTENER(ptr) ((ptr) != NULL)
416 # define SLAP_SOCK_DEL(t,s) do { \
417 int fd, index = SLAP_DEVPOLL_SOCK_IX(t,(s)); \
418 Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_DEL(%d)\n", (s), 0, 0 ); \
419 if ( index < 0 ) break; \
420 if ( index < slap_daemon[t].sd_nfds - 1 ) { \
421 struct pollfd pfd = slap_daemon[t].sd_pollfd[index]; \
422 fd = slap_daemon[t].sd_pollfd[slap_daemon[t].sd_nfds - 1].fd; \
423 slap_daemon[t].sd_pollfd[index] = slap_daemon[t].sd_pollfd[slap_daemon[t].sd_nfds - 1]; \
424 slap_daemon[t].sd_pollfd[slap_daemon[t].sd_nfds - 1] = pfd; \
425 slap_daemon[t].sd_index[fd] = index; \
427 slap_daemon[t].sd_index[(s)] = -1; \
428 slap_daemon[t].sd_pollfd[slap_daemon[t].sd_nfds - 1].events = POLLREMOVE; \
429 SLAP_DEVPOLL_WRITE_POLLFD(t,(s), &slap_daemon[t].sd_pollfd[slap_daemon[t].sd_nfds - 1], 1, "DEL", 0); \
430 slap_daemon[t].sd_pollfd[slap_daemon[t].sd_nfds - 1].events = 0; \
431 slap_daemon[t].sd_nfds--; \
434 # define SLAP_EVENT_CLR_READ(i) SLAP_DEVPOLL_EVENT_CLR((i), POLLIN)
435 # define SLAP_EVENT_CLR_WRITE(i) SLAP_DEVPOLL_EVENT_CLR((i), POLLOUT)
437 # define SLAP_DEVPOLL_EVENT_CHK(i, mode) (revents[(i)].events & (mode))
439 # define SLAP_EVENT_FD(t,i) (revents[(i)].fd)
441 # define SLAP_EVENT_IS_READ(i) SLAP_DEVPOLL_EVENT_CHK((i), POLLIN)
442 # define SLAP_EVENT_IS_WRITE(i) SLAP_DEVPOLL_EVENT_CHK((i), POLLOUT)
443 # define SLAP_EVENT_IS_LISTENER(t,i) SLAP_DEVPOLL_EV_LISTENER(SLAP_DEVPOLL_SOCK_LX(SLAP_EVENT_FD(t,(i))))
444 # define SLAP_EVENT_LISTENER(t,i) SLAP_DEVPOLL_SOCK_LX(SLAP_EVENT_FD(t,(i)))
446 # define SLAP_SOCK_INIT(t) do { \
447 slap_daemon[t].sd_pollfd = ch_calloc( 1, \
448 ( sizeof(struct pollfd) * 2 \
450 + sizeof( Listener * ) ) * dtblsize ); \
451 slap_daemon[t].sd_index = (int *)&slap_daemon[t].sd_pollfd[ 2 * dtblsize ]; \
452 slap_daemon[t].sd_l = (Listener **)&slap_daemon[t].sd_index[ dtblsize ]; \
453 slap_daemon[t].sd_dpfd = open( SLAP_EVENT_FNAME, O_RDWR ); \
454 if ( slap_daemon[t].sd_dpfd == -1 ) { \
455 Debug( LDAP_DEBUG_ANY, "daemon: " SLAP_EVENT_FNAME ": " \
456 "open(\"" SLAP_EVENT_FNAME "\") failed errno=%d\n", \
461 for ( i = 0; i < dtblsize; i++ ) { \
462 slap_daemon[t].sd_pollfd[i].fd = -1; \
463 slap_daemon[t].sd_index[i] = -1; \
467 # define SLAP_SOCK_DESTROY(t) do { \
468 if ( slap_daemon[t].sd_pollfd != NULL ) { \
469 ch_free( slap_daemon[t].sd_pollfd ); \
470 slap_daemon[t].sd_pollfd = NULL; \
471 slap_daemon[t].sd_index = NULL; \
472 slap_daemon[t].sd_l = NULL; \
473 close( slap_daemon[t].sd_dpfd ); \
477 # define SLAP_EVENT_DECL struct pollfd *revents
479 # define SLAP_EVENT_INIT(t) do { \
480 revents = &slap_daemon[t].sd_pollfd[ dtblsize ]; \
483 # define SLAP_EVENT_WAIT(t, tvp, nsp) do { \
484 struct dvpoll sd_dvpoll; \
485 sd_dvpoll.dp_timeout = (tvp) ? (tvp)->tv_sec * 1000 : -1; \
486 sd_dvpoll.dp_nfds = dtblsize; \
487 sd_dvpoll.dp_fds = revents; \
488 *(nsp) = ioctl( slap_daemon[t].sd_dpfd, DP_POLL, &sd_dvpoll ); \
491 #else /* ! epoll && ! /dev/poll */
493 # define SLAP_EVENT_FNAME "WSselect"
494 /* Winsock provides a "select" function but its fd_sets are
495 * actually arrays of sockets. Since these sockets are handles
496 * and not a contiguous range of small integers, we manage our
497 * own "fd" table of socket handles and use their indices as
500 * All of our listener/connection structures use fds; the actual
501 * I/O functions use sockets. The SLAP_FD2SOCK macro in proto-slap.h
502 * handles the mapping.
504 * Despite the mapping overhead, this is about 45% more efficient
505 * than just using Winsock's select and FD_ISSET directly.
507 * Unfortunately Winsock's select implementation doesn't scale well
508 * as the number of connections increases. This probably needs to be
509 * rewritten to use the Winsock overlapped/asynchronous I/O functions.
511 # define SLAP_EVENTS_ARE_INDEXED 1
512 # define SLAP_EVENT_DECL fd_set readfds, writefds; char *rflags
513 # define SLAP_EVENT_INIT(t) do { \
515 FD_ZERO( &readfds ); \
516 FD_ZERO( &writefds ); \
517 rflags = slap_daemon[t].sd_rflags; \
518 memset( rflags, 0, slap_daemon[t].sd_nfds ); \
519 for ( i=0; i<slap_daemon[t].sd_nfds; i++ ) { \
520 if ( slap_daemon[t].sd_flags[i] & SD_READ ) \
521 FD_SET( slapd_ws_sockets[i], &readfds );\
522 if ( slap_daemon[t].sd_flags[i] & SD_WRITE ) \
523 FD_SET( slapd_ws_sockets[i], &writefds ); \
526 # define SLAP_EVENT_MAX(t) slap_daemon[t].sd_nfds
528 # define SLAP_EVENT_WAIT(t, tvp, nsp) do { \
530 *(nsp) = select( SLAP_EVENT_MAX(t), &readfds, \
531 nwriters > 0 ? &writefds : NULL, NULL, (tvp) ); \
532 for ( i=0; i<readfds.fd_count; i++) { \
533 int fd = slapd_sock2fd(readfds.fd_array[i]); \
535 slap_daemon[t].sd_rflags[fd] = SD_READ; \
536 if ( fd >= *(nsp)) *(nsp) = fd+1; \
539 for ( i=0; i<writefds.fd_count; i++) { \
540 int fd = slapd_sock2fd(writefds.fd_array[i]); \
542 slap_daemon[t].sd_rflags[fd] = SD_WRITE; \
543 if ( fd >= *(nsp)) *(nsp) = fd+1; \
548 # define SLAP_EVENT_IS_READ(fd) (rflags[fd] & SD_READ)
549 # define SLAP_EVENT_IS_WRITE(fd) (rflags[fd] & SD_WRITE)
551 # define SLAP_EVENT_CLR_READ(fd) rflags[fd] &= ~SD_READ
552 # define SLAP_EVENT_CLR_WRITE(fd) rflags[fd] &= ~SD_WRITE
554 # define SLAP_SOCK_INIT(t) do { \
556 ldap_pvt_thread_mutex_init( &slapd_ws_mutex ); \
557 slapd_ws_sockets = ch_malloc( dtblsize * ( sizeof(SOCKET) + 2)); \
558 memset( slapd_ws_sockets, -1, dtblsize * sizeof(SOCKET) ); \
560 slap_daemon[t].sd_flags = (char *)(slapd_ws_sockets + dtblsize); \
561 slap_daemon[t].sd_rflags = slap_daemon[t].sd_flags + dtblsize; \
562 memset( slap_daemon[t].sd_flags, 0, dtblsize ); \
563 slapd_ws_sockets[t*2] = wake_sds[t][0]; \
564 slapd_ws_sockets[t*2+1] = wake_sds[t][1]; \
565 wake_sds[t][0] = t*2; \
566 wake_sds[t][1] = t*2+1; \
567 slap_daemon[t].sd_nfds = t*2 + 2; \
570 # define SLAP_SOCK_DESTROY(t) do { \
571 ch_free( slapd_ws_sockets ); slapd_ws_sockets = NULL; \
572 slap_daemon[t].sd_flags = NULL; \
573 slap_daemon[t].sd_rflags = NULL; \
574 ldap_pvt_thread_mutex_destroy( &slapd_ws_mutex ); \
577 # define SLAP_SOCK_IS_ACTIVE(t,fd) ( slap_daemon[t].sd_flags[fd] & SD_ACTIVE )
578 # define SLAP_SOCK_IS_READ(t,fd) ( slap_daemon[t].sd_flags[fd] & SD_READ )
579 # define SLAP_SOCK_IS_WRITE(t,fd) ( slap_daemon[t].sd_flags[fd] & SD_WRITE )
580 # define SLAP_SOCK_NOT_ACTIVE(t,fd) (!slap_daemon[t].sd_flags[fd])
582 # define SLAP_SOCK_SET_READ(t,fd) ( slap_daemon[t].sd_flags[fd] |= SD_READ )
583 # define SLAP_SOCK_SET_WRITE(t,fd) ( slap_daemon[t].sd_flags[fd] |= SD_WRITE )
585 # define SLAP_SELECT_ADDTEST(t,s) do { \
586 if ((s) >= slap_daemon[t].sd_nfds) slap_daemon[t].sd_nfds = (s)+1; \
589 # define SLAP_SOCK_CLR_READ(t,fd) ( slap_daemon[t].sd_flags[fd] &= ~SD_READ )
590 # define SLAP_SOCK_CLR_WRITE(t,fd) ( slap_daemon[t].sd_flags[fd] &= ~SD_WRITE )
592 # define SLAP_SOCK_ADD(t,s, l) do { \
593 SLAP_SELECT_ADDTEST(t,(s)); \
594 slap_daemon[t].sd_flags[s] = SD_ACTIVE|SD_READ; \
597 # define SLAP_SOCK_DEL(t,s) do { \
598 slap_daemon[t].sd_flags[s] = 0; \
599 slapd_sockdel( s ); \
602 # else /* !HAVE_WINSOCK */
604 /**************************************
605 * Use select system call - select(2) *
606 **************************************/
607 # define SLAP_EVENT_FNAME "select"
609 # define SLAP_EVENTS_ARE_INDEXED 1
610 # define SLAP_EVENT_DECL fd_set readfds, writefds
612 # define SLAP_EVENT_INIT(t) do { \
613 AC_MEMCPY( &readfds, &slap_daemon[t].sd_readers, sizeof(fd_set) ); \
615 AC_MEMCPY( &writefds, &slap_daemon[t].sd_writers, sizeof(fd_set) ); \
617 FD_ZERO( &writefds ); \
622 # define SLAP_SELECT_CHK_SETSIZE do { \
623 if (dtblsize > FD_SETSIZE) dtblsize = FD_SETSIZE; \
625 # else /* ! FD_SETSIZE */
626 # define SLAP_SELECT_CHK_SETSIZE do { ; } while (0)
627 # endif /* ! FD_SETSIZE */
629 # define SLAP_SOCK_INIT(t) do { \
630 SLAP_SELECT_CHK_SETSIZE; \
631 FD_ZERO(&slap_daemon[t].sd_actives); \
632 FD_ZERO(&slap_daemon[t].sd_readers); \
633 FD_ZERO(&slap_daemon[t].sd_writers); \
636 # define SLAP_SOCK_DESTROY(t)
638 # define SLAP_SOCK_IS_ACTIVE(t,fd) FD_ISSET((fd), &slap_daemon[t].sd_actives)
639 # define SLAP_SOCK_IS_READ(t,fd) FD_ISSET((fd), &slap_daemon[t].sd_readers)
640 # define SLAP_SOCK_IS_WRITE(t,fd) FD_ISSET((fd), &slap_daemon[t].sd_writers)
642 # define SLAP_SOCK_NOT_ACTIVE(t,fd) (!SLAP_SOCK_IS_ACTIVE(t,fd) && \
643 !SLAP_SOCK_IS_READ(t,fd) && !SLAP_SOCK_IS_WRITE(t,fd))
645 # define SLAP_SOCK_SET_READ(t,fd) FD_SET((fd), &slap_daemon[t].sd_readers)
646 # define SLAP_SOCK_SET_WRITE(t,fd) FD_SET((fd), &slap_daemon[t].sd_writers)
648 # define SLAP_EVENT_MAX(t) slap_daemon[t].sd_nfds
649 # define SLAP_SELECT_ADDTEST(t,s) do { \
650 if ((s) >= slap_daemon[t].sd_nfds) slap_daemon[t].sd_nfds = (s)+1; \
653 # define SLAP_SOCK_CLR_READ(t,fd) FD_CLR((fd), &slap_daemon[t].sd_readers)
654 # define SLAP_SOCK_CLR_WRITE(t,fd) FD_CLR((fd), &slap_daemon[t].sd_writers)
656 # define SLAP_SOCK_ADD(t,s, l) do { \
657 SLAP_SELECT_ADDTEST(t,(s)); \
658 FD_SET((s), &slap_daemon[t].sd_actives); \
659 FD_SET((s), &slap_daemon[t].sd_readers); \
662 # define SLAP_SOCK_DEL(t,s) do { \
663 FD_CLR((s), &slap_daemon[t].sd_actives); \
664 FD_CLR((s), &slap_daemon[t].sd_readers); \
665 FD_CLR((s), &slap_daemon[t].sd_writers); \
668 # define SLAP_EVENT_IS_READ(fd) FD_ISSET((fd), &readfds)
669 # define SLAP_EVENT_IS_WRITE(fd) FD_ISSET((fd), &writefds)
671 # define SLAP_EVENT_CLR_READ(fd) FD_CLR((fd), &readfds)
672 # define SLAP_EVENT_CLR_WRITE(fd) FD_CLR((fd), &writefds)
674 # define SLAP_EVENT_WAIT(t, tvp, nsp) do { \
675 *(nsp) = select( SLAP_EVENT_MAX(t), &readfds, \
676 nwriters > 0 ? &writefds : NULL, NULL, (tvp) ); \
678 # endif /* !HAVE_WINSOCK */
679 #endif /* ! epoll && ! /dev/poll */
683 * SLP related functions
687 #define LDAP_SRVTYPE_PREFIX "service:ldap://"
688 #define LDAPS_SRVTYPE_PREFIX "service:ldaps://"
689 static char** slapd_srvurls = NULL;
690 static SLPHandle slapd_hslp = 0;
691 int slapd_register_slp = 0;
692 const char *slapd_slp_attrs = NULL;
694 static SLPError slapd_slp_cookie;
697 slapd_slp_init( const char* urls )
702 slapd_srvurls = ldap_str2charray( urls, " " );
704 if ( slapd_srvurls == NULL ) return;
706 /* find and expand INADDR_ANY URLs */
707 for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
708 if ( strcmp( slapd_srvurls[i], "ldap:///" ) == 0 ) {
709 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
710 global_host_bv.bv_len +
711 sizeof( LDAP_SRVTYPE_PREFIX ) );
712 strcpy( lutil_strcopy(slapd_srvurls[i],
713 LDAP_SRVTYPE_PREFIX ), global_host_bv.bv_val );
714 } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0 ) {
715 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
716 global_host_bv.bv_len +
717 sizeof( LDAPS_SRVTYPE_PREFIX ) );
718 strcpy( lutil_strcopy(slapd_srvurls[i],
719 LDAPS_SRVTYPE_PREFIX ), global_host_bv.bv_val );
723 /* open the SLP handle */
724 err = SLPOpen( "en", 0, &slapd_hslp );
726 if ( err != SLP_OK ) {
727 Debug( LDAP_DEBUG_CONNS, "daemon: SLPOpen() failed with %ld\n",
733 slapd_slp_deinit( void )
735 if ( slapd_srvurls == NULL ) return;
737 ldap_charray_free( slapd_srvurls );
738 slapd_srvurls = NULL;
740 /* close the SLP handle */
741 SLPClose( slapd_hslp );
750 /* return the error code in the cookie */
751 *(SLPError*)cookie = errcode;
760 if ( slapd_srvurls == NULL ) return;
762 for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
763 if ( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
764 sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
765 strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
766 sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
768 err = SLPReg( slapd_hslp,
770 SLP_LIFETIME_MAXIMUM,
772 (slapd_slp_attrs) ? slapd_slp_attrs : "",
777 if ( err != SLP_OK || slapd_slp_cookie != SLP_OK ) {
778 Debug( LDAP_DEBUG_CONNS,
779 "daemon: SLPReg(%s) failed with %ld, cookie = %ld\n",
780 slapd_srvurls[i], (long)err, (long)slapd_slp_cookie );
787 slapd_slp_dereg( void )
792 if ( slapd_srvurls == NULL ) return;
794 for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
795 err = SLPDereg( slapd_hslp,
800 if ( err != SLP_OK || slapd_slp_cookie != SLP_OK ) {
801 Debug( LDAP_DEBUG_CONNS,
802 "daemon: SLPDereg(%s) failed with %ld, cookie = %ld\n",
803 slapd_srvurls[i], (long)err, (long)slapd_slp_cookie );
807 #endif /* HAVE_SLP */
810 /* Manage the descriptor to socket table */
812 slapd_socknew( ber_socket_t s )
815 ldap_pvt_thread_mutex_lock( &slapd_ws_mutex );
816 for ( i = 0; i < dtblsize && slapd_ws_sockets[i] != INVALID_SOCKET; i++ );
817 if ( i == dtblsize ) {
818 WSASetLastError( WSAEMFILE );
820 slapd_ws_sockets[i] = s;
822 ldap_pvt_thread_mutex_unlock( &slapd_ws_mutex );
827 slapd_sockdel( ber_socket_t s )
829 ldap_pvt_thread_mutex_lock( &slapd_ws_mutex );
830 slapd_ws_sockets[s] = INVALID_SOCKET;
831 ldap_pvt_thread_mutex_unlock( &slapd_ws_mutex );
835 slapd_sock2fd( ber_socket_t s )
838 for ( i=0; i<dtblsize && slapd_ws_sockets[i] != s; i++);
846 * Add a descriptor to daemon control
848 * If isactive, the descriptor is a live server session and is subject
849 * to idletimeout control. Otherwise, the descriptor is a passive
850 * listener or an outbound client session, and not subject to
851 * idletimeout. The underlying event handler may record the Listener
852 * argument to differentiate Listener's from real sessions.
855 slapd_add( ber_socket_t s, int isactive, Listener *sl, int id )
859 ldap_pvt_thread_mutex_lock( &slap_daemon[id].sd_mutex );
861 assert( SLAP_SOCK_NOT_ACTIVE(id, s) );
863 if ( isactive ) slap_daemon[id].sd_nactives++;
865 SLAP_SOCK_ADD(id, s, sl);
867 Debug( LDAP_DEBUG_CONNS, "daemon: added %ldr%s listener=%p\n",
868 (long) s, isactive ? " (active)" : "", (void *)sl );
870 ldap_pvt_thread_mutex_unlock( &slap_daemon[id].sd_mutex );
876 * Remove the descriptor from daemon control
888 int id = DAEMON_ID(s);
891 ldap_pvt_thread_mutex_lock( &slap_daemon[id].sd_mutex );
893 assert( SLAP_SOCK_IS_ACTIVE( id, s ));
895 if ( wasactive ) slap_daemon[id].sd_nactives--;
897 waswriter = SLAP_SOCK_IS_WRITE(id, s);
898 wasreader = SLAP_SOCK_IS_READ(id, s);
900 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
902 wasreader ? "r" : "",
903 waswriter ? "w" : "" );
905 if ( waswriter ) slap_daemon[id].sd_nwriters--;
907 SLAP_SOCK_DEL(id, s);
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.
916 if ( emfile && listening ) {
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;
926 if ( DAEMON_ID(lr->sl_sd) != id )
927 WAKE_LISTENER(DAEMON_ID(lr->sl_sd), wake);
931 /* Walked the entire list without enabling anything; emfile
932 * counter is stale. Reset it.
934 if ( slap_listeners[i] == NULL ) emfile = 0;
936 ldap_pvt_thread_mutex_unlock( &slap_daemon[id].sd_mutex );
937 WAKE_LISTENER(id, wake || slapd_gentle_shutdown == 2);
941 slapd_clr_write( ber_socket_t s, int wake )
943 int id = DAEMON_ID(s);
944 ldap_pvt_thread_mutex_lock( &slap_daemon[id].sd_mutex );
946 if ( SLAP_SOCK_IS_WRITE( id, s )) {
947 assert( SLAP_SOCK_IS_ACTIVE( id, s ));
949 SLAP_SOCK_CLR_WRITE( id, s );
950 slap_daemon[id].sd_nwriters--;
953 ldap_pvt_thread_mutex_unlock( &slap_daemon[id].sd_mutex );
954 WAKE_LISTENER(id,wake);
958 slapd_set_write( ber_socket_t s, int wake )
960 int id = DAEMON_ID(s);
961 ldap_pvt_thread_mutex_lock( &slap_daemon[id].sd_mutex );
963 assert( SLAP_SOCK_IS_ACTIVE( id, s ));
965 if ( !SLAP_SOCK_IS_WRITE( id, s )) {
966 SLAP_SOCK_SET_WRITE( id, s );
967 slap_daemon[id].sd_nwriters++;
970 ldap_pvt_thread_mutex_unlock( &slap_daemon[id].sd_mutex );
971 WAKE_LISTENER(id,wake);
975 slapd_clr_read( ber_socket_t s, int wake )
978 int id = DAEMON_ID(s);
979 ldap_pvt_thread_mutex_lock( &slap_daemon[id].sd_mutex );
981 if ( SLAP_SOCK_IS_ACTIVE( id, s )) {
982 SLAP_SOCK_CLR_READ( id, s );
985 ldap_pvt_thread_mutex_unlock( &slap_daemon[id].sd_mutex );
987 WAKE_LISTENER(id,wake);
992 slapd_set_read( ber_socket_t s, int wake )
995 int id = DAEMON_ID(s);
996 ldap_pvt_thread_mutex_lock( &slap_daemon[id].sd_mutex );
998 if( SLAP_SOCK_IS_ACTIVE( id, s ) && !SLAP_SOCK_IS_READ( id, s )) {
999 SLAP_SOCK_SET_READ( id, s );
1003 ldap_pvt_thread_mutex_unlock( &slap_daemon[id].sd_mutex );
1005 WAKE_LISTENER(id,wake);
1009 slapd_close( ber_socket_t s )
1011 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
1013 tcp_close( SLAP_FD2SOCK(s) );
1020 slapd_shutsock( ber_socket_t s )
1022 Debug( LDAP_DEBUG_CONNS, "daemon: shutdown socket %ld\n",
1024 shutdown( SLAP_FD2SOCK(s), 2 );
1028 slap_free_listener_addresses( struct sockaddr **sal )
1030 struct sockaddr **sap;
1031 if (sal == NULL) return;
1032 for (sap = sal; *sap != NULL; sap++) ch_free(*sap);
1036 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
1045 assert( exts != NULL );
1046 assert( perms != NULL );
1047 assert( crit != NULL );
1050 for ( i = 0; exts[ i ]; i++ ) {
1051 char *type = exts[ i ];
1054 if ( type[ 0 ] == '!' ) {
1059 if ( strncasecmp( type, LDAPI_MOD_URLEXT "=",
1060 sizeof(LDAPI_MOD_URLEXT "=") - 1 ) == 0 )
1062 char *value = type + ( sizeof(LDAPI_MOD_URLEXT "=") - 1 );
1066 switch (strlen(value)) {
1068 /* skip leading '0' */
1069 if ( value[ 0 ] != '0' ) return LDAP_OTHER;
1073 for ( j = 0; j < 3; j++) {
1076 v = value[ j ] - '0';
1078 if ( v < 0 || v > 7 ) return LDAP_OTHER;
1085 for ( j = 1; j < 10; j++ ) {
1086 static mode_t m[] = { 0,
1087 S_IRUSR, S_IWUSR, S_IXUSR,
1088 S_IRGRP, S_IWGRP, S_IXGRP,
1089 S_IROTH, S_IWOTH, S_IXOTH
1091 static const char c[] = "-rwxrwxrwx";
1093 if ( value[ j ] == c[ j ] ) {
1096 } else if ( value[ j ] != '-' ) {
1109 return LDAP_SUCCESS;
1115 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
1117 /* port = 0 indicates AF_LOCAL */
1119 slap_get_listener_addresses(
1121 unsigned short port,
1122 struct sockaddr ***sal )
1124 struct sockaddr **sap;
1126 #ifdef LDAP_PF_LOCAL
1128 *sal = ch_malloc(2 * sizeof(void *));
1129 if (*sal == NULL) return -1;
1132 *sap = ch_malloc(sizeof(struct sockaddr_un));
1133 if (*sap == NULL) goto errexit;
1137 (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) )
1139 Debug( LDAP_DEBUG_ANY,
1140 "daemon: domain socket path (%s) too long in URL",
1145 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
1146 (*sap)->sa_family = AF_LOCAL;
1147 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
1149 #endif /* LDAP_PF_LOCAL */
1151 #ifdef HAVE_GETADDRINFO
1152 struct addrinfo hints, *res, *sai;
1156 memset( &hints, '\0', sizeof(hints) );
1157 hints.ai_flags = AI_PASSIVE;
1158 hints.ai_socktype = SOCK_STREAM;
1159 hints.ai_family = slap_inet4or6;
1160 snprintf(serv, sizeof serv, "%d", port);
1162 if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
1163 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo() failed: %s\n",
1164 AC_GAI_STRERROR(err), 0, 0);
1169 for (n=2; (sai = sai->ai_next) != NULL; n++) {
1172 *sal = ch_calloc(n, sizeof(void *));
1173 if (*sal == NULL) return -1;
1178 for ( sai=res; sai; sai=sai->ai_next ) {
1179 if( sai->ai_addr == NULL ) {
1180 Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
1181 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
1186 switch (sai->ai_family) {
1187 # ifdef LDAP_PF_INET6
1189 *sap = ch_malloc(sizeof(struct sockaddr_in6));
1194 *(struct sockaddr_in6 *)*sap =
1195 *((struct sockaddr_in6 *)sai->ai_addr);
1197 # endif /* LDAP_PF_INET6 */
1199 *sap = ch_malloc(sizeof(struct sockaddr_in));
1204 *(struct sockaddr_in *)*sap =
1205 *((struct sockaddr_in *)sai->ai_addr);
1213 (*sap)->sa_family = sai->ai_family;
1221 #else /* ! HAVE_GETADDRINFO */
1224 struct hostent *he = NULL;
1226 if ( host == NULL ) {
1227 in.s_addr = htonl(INADDR_ANY);
1229 } else if ( !inet_aton( host, &in ) ) {
1230 he = gethostbyname( host );
1232 Debug( LDAP_DEBUG_ANY,
1233 "daemon: invalid host %s", host, 0, 0);
1236 for (n = 0; he->h_addr_list[n]; n++) /* empty */;
1239 *sal = ch_malloc((n+1) * sizeof(void *));
1240 if (*sal == NULL) return -1;
1243 for ( i = 0; i<n; i++ ) {
1244 sap[i] = ch_malloc(sizeof(struct sockaddr_in));
1245 if (*sap == NULL) goto errexit;
1247 (void)memset( (void *)sap[i], '\0', sizeof(struct sockaddr_in) );
1248 sap[i]->sa_family = AF_INET;
1249 ((struct sockaddr_in *)sap[i])->sin_port = htons(port);
1250 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr,
1251 he ? (struct in_addr *)he->h_addr_list[i] : &in,
1252 sizeof(struct in_addr) );
1255 #endif /* ! HAVE_GETADDRINFO */
1261 slap_free_listener_addresses(*sal);
1275 unsigned short port;
1276 int err, addrlen = 0;
1277 struct sockaddr **sal, **psal;
1278 int socktype = SOCK_STREAM; /* default to COTS */
1281 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
1286 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
1288 rc = ldap_url_parse( url, &lud );
1290 if( rc != LDAP_URL_SUCCESS ) {
1291 Debug( LDAP_DEBUG_ANY,
1292 "daemon: listen URL \"%s\" parse error=%d\n",
1297 l.sl_url.bv_val = NULL;
1302 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
1303 Debug( LDAP_DEBUG_ANY, "daemon: TLS not supported (%s)\n",
1305 ldap_free_urldesc( lud );
1309 if(! lud->lud_port ) lud->lud_port = LDAP_PORT;
1311 #else /* HAVE_TLS */
1312 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
1314 if(! lud->lud_port ) {
1315 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
1317 #endif /* HAVE_TLS */
1319 #ifdef LDAP_TCP_BUFFER
1322 #endif /* LDAP_TCP_BUFFER */
1324 port = (unsigned short) lud->lud_port;
1326 tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme);
1327 if ( tmp == LDAP_PROTO_IPC ) {
1328 #ifdef LDAP_PF_LOCAL
1329 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
1330 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
1332 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
1334 #else /* ! LDAP_PF_LOCAL */
1336 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
1338 ldap_free_urldesc( lud );
1340 #endif /* ! LDAP_PF_LOCAL */
1342 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
1343 || strcmp(lud->lud_host, "*") == 0 )
1345 err = slap_get_listener_addresses(NULL, port, &sal);
1347 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
1351 #ifdef LDAP_CONNECTIONLESS
1352 l.sl_is_udp = ( tmp == LDAP_PROTO_UDP );
1353 #endif /* LDAP_CONNECTIONLESS */
1355 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
1356 if ( lud->lud_exts ) {
1357 err = get_url_perms( lud->lud_exts, &l.sl_perms, &crit );
1359 l.sl_perms = S_IRWXU | S_IRWXO;
1361 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
1363 ldap_free_urldesc( lud );
1364 if ( err ) return -1;
1366 /* If we got more than one address returned, we need to make space
1367 * for it in the slap_listeners array.
1369 for ( num=0; sal[num]; num++ ) /* empty */;
1371 *listeners += num-1;
1372 slap_listeners = ch_realloc( slap_listeners,
1373 (*listeners + 1) * sizeof(Listener *) );
1377 while ( *sal != NULL ) {
1379 switch( (*sal)->sa_family ) {
1383 #ifdef LDAP_PF_INET6
1387 #endif /* LDAP_PF_INET6 */
1388 #ifdef LDAP_PF_LOCAL
1392 #endif /* LDAP_PF_LOCAL */
1398 #ifdef LDAP_CONNECTIONLESS
1399 if( l.sl_is_udp ) socktype = SOCK_DGRAM;
1400 #endif /* LDAP_CONNECTIONLESS */
1402 s = socket( (*sal)->sa_family, socktype, 0);
1403 if ( s == AC_SOCKET_INVALID ) {
1404 int err = sock_errno();
1405 Debug( LDAP_DEBUG_ANY,
1406 "daemon: %s socket() failed errno=%d (%s)\n",
1407 af, err, sock_errstr(err) );
1411 l.sl_sd = SLAP_SOCKNEW( s );
1413 if ( l.sl_sd >= dtblsize ) {
1414 Debug( LDAP_DEBUG_ANY,
1415 "daemon: listener descriptor %ld is too great %ld\n",
1416 (long) l.sl_sd, (long) dtblsize, 0 );
1422 #ifdef LDAP_PF_LOCAL
1423 if ( (*sal)->sa_family == AF_LOCAL ) {
1424 unlink( ((struct sockaddr_un *)*sal)->sun_path );
1426 #endif /* LDAP_PF_LOCAL */
1429 /* enable address reuse */
1431 rc = setsockopt( s, SOL_SOCKET, SO_REUSEADDR,
1432 (char *) &tmp, sizeof(tmp) );
1433 if ( rc == AC_SOCKET_ERROR ) {
1434 int err = sock_errno();
1435 Debug( LDAP_DEBUG_ANY, "slapd(%ld): "
1436 "setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
1437 (long) l.sl_sd, err, sock_errstr(err) );
1439 #endif /* SO_REUSEADDR */
1442 switch( (*sal)->sa_family ) {
1444 addrlen = sizeof(struct sockaddr_in);
1446 #ifdef LDAP_PF_INET6
1449 /* Try to use IPv6 sockets for IPv6 only */
1451 rc = setsockopt( s , IPPROTO_IPV6, IPV6_V6ONLY,
1452 (char *) &tmp, sizeof(tmp) );
1453 if ( rc == AC_SOCKET_ERROR ) {
1454 int err = sock_errno();
1455 Debug( LDAP_DEBUG_ANY, "slapd(%ld): "
1456 "setsockopt(IPV6_V6ONLY) failed errno=%d (%s)\n",
1457 (long) l.sl_sd, err, sock_errstr(err) );
1459 #endif /* IPV6_V6ONLY */
1460 addrlen = sizeof(struct sockaddr_in6);
1462 #endif /* LDAP_PF_INET6 */
1464 #ifdef LDAP_PF_LOCAL
1469 setsockopt( s, 0, LOCAL_CREDS, &one, sizeof( one ) );
1471 #endif /* LOCAL_CREDS */
1473 addrlen = sizeof( struct sockaddr_un );
1475 #endif /* LDAP_PF_LOCAL */
1478 #ifdef LDAP_PF_LOCAL
1479 /* create socket with all permissions set for those systems
1480 * that honor permissions on sockets (e.g. Linux); typically,
1481 * only write is required. To exploit filesystem permissions,
1482 * place the socket in a directory and use directory's
1483 * permissions. Need write perms to the directory to
1484 * create/unlink the socket; likely need exec perms to access
1485 * the socket (ITS#4709) */
1487 mode_t old_umask = 0;
1489 if ( (*sal)->sa_family == AF_LOCAL ) {
1490 old_umask = umask( 0 );
1492 #endif /* LDAP_PF_LOCAL */
1493 rc = bind( s, *sal, addrlen );
1494 #ifdef LDAP_PF_LOCAL
1495 if ( old_umask != 0 ) {
1499 #endif /* LDAP_PF_LOCAL */
1502 Debug( LDAP_DEBUG_ANY,
1503 "daemon: bind(%ld) failed errno=%d (%s)\n",
1504 (long)l.sl_sd, err, sock_errstr( err ) );
1510 switch ( (*sal)->sa_family ) {
1511 #ifdef LDAP_PF_LOCAL
1513 char *path = ((struct sockaddr_un *)*sal)->sun_path;
1514 l.sl_name.bv_len = strlen(path) + STRLENOF("PATH=");
1515 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len + 1 );
1516 snprintf( l.sl_name.bv_val, l.sl_name.bv_len + 1,
1519 #endif /* LDAP_PF_LOCAL */
1522 char addr[INET_ADDRSTRLEN];
1524 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
1525 s = inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
1526 addr, sizeof(addr) );
1527 #else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1528 s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr );
1529 #endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1530 if (!s) s = SLAP_STRING_UNKNOWN;
1531 port = ntohs( ((struct sockaddr_in *)*sal) ->sin_port );
1533 ber_memalloc( sizeof("IP=255.255.255.255:65535") );
1534 snprintf( l.sl_name.bv_val, sizeof("IP=255.255.255.255:65535"),
1535 "IP=%s:%d", s, port );
1536 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1539 #ifdef LDAP_PF_INET6
1541 char addr[INET6_ADDRSTRLEN];
1543 s = inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
1545 if (!s) s = SLAP_STRING_UNKNOWN;
1546 port = ntohs( ((struct sockaddr_in6 *)*sal)->sin6_port );
1547 l.sl_name.bv_len = strlen(s) + sizeof("IP=[]:65535");
1548 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len );
1549 snprintf( l.sl_name.bv_val, l.sl_name.bv_len, "IP=[%s]:%d",
1551 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1553 #endif /* LDAP_PF_INET6 */
1556 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
1557 (int) (*sal)->sa_family, 0, 0 );
1561 AC_MEMCPY(&l.sl_sa, *sal, addrlen);
1562 ber_str2bv( url, 0, 1, &l.sl_url);
1563 li = ch_malloc( sizeof( Listener ) );
1565 slap_listeners[*cur] = li;
1570 slap_free_listener_addresses(psal);
1572 if ( l.sl_url.bv_val == NULL ) {
1573 Debug( LDAP_DEBUG_TRACE,
1574 "slap_open_listener: failed on %s\n", url, 0, 0 );
1578 Debug( LDAP_DEBUG_TRACE, "daemon: listener initialized %s\n",
1579 l.sl_url.bv_val, 0, 0 );
1583 static int sockinit(void);
1584 static int sockdestroy(void);
1586 static int daemon_inited = 0;
1589 slapd_daemon_init( const char *urls )
1594 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
1595 urls ? urls : "<null>", 0, 0 );
1597 for ( i=0; i<SLAPD_MAX_DAEMON_THREADS; i++ ) {
1598 wake_sds[i][0] = AC_SOCKET_INVALID;
1599 wake_sds[i][1] = AC_SOCKET_INVALID;
1602 ldap_pvt_thread_mutex_init( &slap_daemon[0].sd_mutex );
1604 ldap_pvt_thread_mutex_init( &sd_tcpd_mutex );
1605 #endif /* TCP Wrappers */
1609 if( (rc = sockinit()) != 0 ) return rc;
1612 dtblsize = sysconf( _SC_OPEN_MAX );
1613 #elif defined(HAVE_GETDTABLESIZE)
1614 dtblsize = getdtablesize();
1615 #else /* ! HAVE_SYSCONF && ! HAVE_GETDTABLESIZE */
1616 dtblsize = FD_SETSIZE;
1617 #endif /* ! HAVE_SYSCONF && ! HAVE_GETDTABLESIZE */
1619 /* open a pipe (or something equivalent connected to itself).
1620 * we write a byte on this fd whenever we catch a signal. The main
1621 * loop will be select'ing on this socket, and will wake up when
1622 * this byte arrives.
1624 if( (rc = lutil_pair( wake_sds[0] )) < 0 ) {
1625 Debug( LDAP_DEBUG_ANY,
1626 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
1629 ber_pvt_socket_set_nonblock( wake_sds[0][1], 1 );
1633 if( urls == NULL ) urls = "ldap:///";
1635 u = ldap_str2charray( urls, " " );
1637 if( u == NULL || u[0] == NULL ) {
1638 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
1641 ldap_charray_free( u );
1645 for( i=0; u[i] != NULL; i++ ) {
1646 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
1651 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
1653 ldap_charray_free( u );
1657 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
1659 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
1661 for(n = 0, j = 0; u[n]; n++ ) {
1662 if ( slap_open_listener( u[n], &i, &j ) ) {
1663 ldap_charray_free( u );
1667 slap_listeners[j] = NULL;
1669 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
1674 if( slapd_register_slp ) {
1675 slapd_slp_init( urls );
1678 #endif /* HAVE_SLP */
1680 ldap_charray_free( u );
1687 slapd_daemon_destroy( void )
1689 connections_destroy();
1690 if ( daemon_inited ) {
1693 for ( i=0; i<slapd_daemon_threads; i++ ) {
1695 if ( wake_sds[i][1] != INVALID_SOCKET &&
1696 SLAP_FD2SOCK( wake_sds[i][1] ) != SLAP_FD2SOCK( wake_sds[i][0] ))
1697 #endif /* HAVE_WINSOCK */
1698 tcp_close( SLAP_FD2SOCK(wake_sds[i][1]) );
1700 if ( wake_sds[i][0] != INVALID_SOCKET )
1701 #endif /* HAVE_WINSOCK */
1702 tcp_close( SLAP_FD2SOCK(wake_sds[i][0]) );
1703 ldap_pvt_thread_mutex_destroy( &slap_daemon[i].sd_mutex );
1704 SLAP_SOCK_DESTROY(i);
1708 ldap_pvt_thread_mutex_destroy( &sd_tcpd_mutex );
1709 #endif /* TCP Wrappers */
1714 if( slapd_register_slp ) {
1718 #endif /* HAVE_SLP */
1734 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1735 Listener *lr = slap_listeners[l];
1737 if ( lr->sl_sd != AC_SOCKET_INVALID ) {
1739 lr->sl_sd = AC_SOCKET_INVALID;
1740 if ( remove ) slapd_remove( s, NULL, 0, 0, 0 );
1742 #ifdef LDAP_PF_LOCAL
1743 if ( lr->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1744 unlink( lr->sl_sa.sa_un_addr.sun_path );
1746 #endif /* LDAP_PF_LOCAL */
1754 destroy_listeners( void )
1756 Listener *lr, **ll = slap_listeners;
1761 while ( (lr = *ll++) != NULL ) {
1762 if ( lr->sl_url.bv_val ) {
1763 ber_memfree( lr->sl_url.bv_val );
1766 if ( lr->sl_name.bv_val ) {
1767 ber_memfree( lr->sl_name.bv_val );
1773 free( slap_listeners );
1774 slap_listeners = NULL;
1783 ber_socket_t s, sfd;
1784 ber_socklen_t len = sizeof(from);
1787 struct berval authid = BER_BVNULL;
1788 #ifdef SLAPD_RLOOKUPS
1789 char hbuf[NI_MAXHOST];
1790 #endif /* SLAPD_RLOOKUPS */
1792 char *dnsname = NULL;
1793 const char *peeraddr = NULL;
1794 /* we assume INET6_ADDRSTRLEN > INET_ADDRSTRLEN */
1795 char addr[INET6_ADDRSTRLEN];
1796 #ifdef LDAP_PF_LOCAL
1797 char peername[MAXPATHLEN + sizeof("PATH=")];
1798 #ifdef LDAP_PF_LOCAL_SENDMSG
1800 struct berval peerbv = BER_BVNULL;
1802 #elif defined(LDAP_PF_INET6)
1803 char peername[sizeof("IP=[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535")];
1804 #else /* ! LDAP_PF_LOCAL && ! LDAP_PF_INET6 */
1805 char peername[sizeof("IP=255.255.255.255:65336")];
1806 #endif /* LDAP_PF_LOCAL */
1810 Debug( LDAP_DEBUG_TRACE,
1811 ">>> slap_listener(%s)\n",
1812 sl->sl_url.bv_val, 0, 0 );
1816 #ifdef LDAP_CONNECTIONLESS
1817 if ( sl->sl_is_udp ) return 1;
1818 #endif /* LDAP_CONNECTIONLESS */
1820 # ifdef LDAP_PF_LOCAL
1821 /* FIXME: apparently accept doesn't fill
1822 * the sun_path sun_path member */
1823 from.sa_un_addr.sun_path[0] = '\0';
1824 # endif /* LDAP_PF_LOCAL */
1826 s = accept( SLAP_FD2SOCK( sl->sl_sd ), (struct sockaddr *) &from, &len );
1828 /* Resume the listener FD to allow concurrent-processing of
1829 * additional incoming connections.
1832 WAKE_LISTENER(DAEMON_ID(sl->sl_sd),1);
1834 if ( s == AC_SOCKET_INVALID ) {
1835 int err = sock_errno();
1846 ldap_pvt_thread_mutex_lock( &slap_daemon[0].sd_mutex );
1848 /* Stop listening until an existing session closes */
1850 ldap_pvt_thread_mutex_unlock( &slap_daemon[0].sd_mutex );
1853 Debug( LDAP_DEBUG_ANY,
1854 "daemon: accept(%ld) failed errno=%d (%s)\n",
1855 (long) sl->sl_sd, err, sock_errstr(err) );
1856 ldap_pvt_thread_yield();
1859 sfd = SLAP_SOCKNEW( s );
1861 /* make sure descriptor number isn't too great */
1862 if ( sfd >= dtblsize ) {
1863 Debug( LDAP_DEBUG_ANY,
1864 "daemon: %ld beyond descriptor table size %ld\n",
1865 (long) sfd, (long) dtblsize, 0 );
1868 ldap_pvt_thread_yield();
1871 tid = DAEMON_ID(sfd);
1874 ldap_pvt_thread_mutex_lock( &slap_daemon[tid].sd_mutex );
1875 /* newly accepted stream should not be in any of the FD SETS */
1876 assert( SLAP_SOCK_NOT_ACTIVE( tid, sfd ));
1877 ldap_pvt_thread_mutex_unlock( &slap_daemon[tid].sd_mutex );
1878 #endif /* LDAP_DEBUG */
1880 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1881 #ifdef LDAP_PF_LOCAL
1882 /* for IPv4 and IPv6 sockets only */
1883 if ( from.sa_addr.sa_family != AF_LOCAL )
1884 #endif /* LDAP_PF_LOCAL */
1889 /* enable keep alives */
1891 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1892 (char *) &tmp, sizeof(tmp) );
1893 if ( rc == AC_SOCKET_ERROR ) {
1894 int err = sock_errno();
1895 Debug( LDAP_DEBUG_ANY,
1896 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1897 "errno=%d (%s)\n", (long) sfd, err, sock_errstr(err) );
1899 #endif /* SO_KEEPALIVE */
1901 /* enable no delay */
1903 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1904 (char *)&tmp, sizeof(tmp) );
1905 if ( rc == AC_SOCKET_ERROR ) {
1906 int err = sock_errno();
1907 Debug( LDAP_DEBUG_ANY,
1908 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1909 "errno=%d (%s)\n", (long) sfd, err, sock_errstr(err) );
1911 #endif /* TCP_NODELAY */
1913 #endif /* SO_KEEPALIVE || TCP_NODELAY */
1915 Debug( LDAP_DEBUG_CONNS,
1916 "daemon: listen=%ld, new connection on %ld\n",
1917 (long) sl->sl_sd, (long) sfd, 0 );
1920 switch ( from.sa_addr.sa_family ) {
1921 # ifdef LDAP_PF_LOCAL
1923 cflag |= CONN_IS_IPC;
1925 /* FIXME: apparently accept doesn't fill
1926 * the sun_path sun_path member */
1927 if ( from.sa_un_addr.sun_path[0] == '\0' ) {
1928 AC_MEMCPY( from.sa_un_addr.sun_path,
1929 sl->sl_sa.sa_un_addr.sun_path,
1930 sizeof( from.sa_un_addr.sun_path ) );
1933 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1939 #ifdef LDAP_PF_LOCAL_SENDMSG
1940 peerbv.bv_val = peerbuf;
1941 peerbv.bv_len = sizeof( peerbuf );
1943 if( LUTIL_GETPEEREID( s, &uid, &gid, &peerbv ) == 0 ) {
1944 authid.bv_val = ch_malloc(
1945 STRLENOF( "gidNumber=4294967295+uidNumber=4294967295,"
1946 "cn=peercred,cn=external,cn=auth" ) + 1 );
1947 authid.bv_len = sprintf( authid.bv_val,
1948 "gidNumber=%d+uidNumber=%d,"
1949 "cn=peercred,cn=external,cn=auth",
1950 (int) gid, (int) uid );
1951 assert( authid.bv_len <=
1952 STRLENOF( "gidNumber=4294967295+uidNumber=4294967295,"
1953 "cn=peercred,cn=external,cn=auth" ) );
1958 #endif /* LDAP_PF_LOCAL */
1960 # ifdef LDAP_PF_INET6
1962 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1963 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
1964 peeraddr = inet_ntop( AF_INET,
1965 ((struct in_addr *)&from.sa_in6_addr.sin6_addr.s6_addr[12]),
1966 addr, sizeof(addr) );
1967 #else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1968 peeraddr = inet_ntoa( *((struct in_addr *)
1969 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1970 #endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1971 if ( !peeraddr ) peeraddr = SLAP_STRING_UNKNOWN;
1972 sprintf( peername, "IP=%s:%d", peeraddr,
1973 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1975 peeraddr = inet_ntop( AF_INET6,
1976 &from.sa_in6_addr.sin6_addr,
1977 addr, sizeof addr );
1978 if ( !peeraddr ) peeraddr = SLAP_STRING_UNKNOWN;
1979 sprintf( peername, "IP=[%s]:%d", peeraddr,
1980 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1983 # endif /* LDAP_PF_INET6 */
1986 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
1987 peeraddr = inet_ntop( AF_INET, &from.sa_in_addr.sin_addr,
1988 addr, sizeof(addr) );
1989 #else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1990 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1991 #endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1992 if ( !peeraddr ) peeraddr = SLAP_STRING_UNKNOWN;
1993 sprintf( peername, "IP=%s:%d", peeraddr,
1994 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
2002 if ( ( from.sa_addr.sa_family == AF_INET )
2003 #ifdef LDAP_PF_INET6
2004 || ( from.sa_addr.sa_family == AF_INET6 )
2005 #endif /* LDAP_PF_INET6 */
2009 #ifdef SLAPD_RLOOKUPS
2010 if ( use_reverse_lookup ) {
2012 if (ldap_pvt_get_hname( (const struct sockaddr *)&from, len, hbuf,
2013 sizeof(hbuf), &herr ) == 0) {
2014 ldap_pvt_str2lower( hbuf );
2018 #endif /* SLAPD_RLOOKUPS */
2023 ldap_pvt_thread_mutex_lock( &sd_tcpd_mutex );
2024 rc = hosts_ctl("slapd",
2025 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
2027 SLAP_STRING_UNKNOWN );
2028 ldap_pvt_thread_mutex_unlock( &sd_tcpd_mutex );
2031 Statslog( LDAP_DEBUG_STATS,
2032 "fd=%ld DENIED from %s (%s)\n",
2034 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
2040 #endif /* HAVE_TCPD */
2044 if ( sl->sl_is_tls ) cflag |= CONN_IS_TLS;
2046 c = connection_init(sfd, sl,
2047 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
2048 peername, cflag, ssf,
2049 authid.bv_val ? &authid : NULL
2050 LDAP_PF_LOCAL_SENDMSG_ARG(&peerbv));
2052 if( authid.bv_val ) ch_free(authid.bv_val);
2055 Debug( LDAP_DEBUG_ANY,
2056 "daemon: connection_init(%ld, %s, %s) failed.\n",
2057 (long) sfd, peername, sl->sl_name.bv_val );
2065 slap_listener_thread(
2070 Listener *sl = (Listener *)ptr;
2072 rc = slap_listener( sl );
2074 if( rc != LDAP_SUCCESS ) {
2075 Debug( LDAP_DEBUG_ANY,
2076 "slap_listener_thread(%s): failed err=%d",
2077 sl->sl_url.bv_val, rc, 0 );
2084 slap_listener_activate(
2089 Debug( LDAP_DEBUG_TRACE, "slap_listener_activate(%d): %s\n",
2090 sl->sl_sd, sl->sl_busy ? "busy" : "", 0 );
2094 rc = ldap_pvt_thread_pool_submit( &connection_pool,
2095 slap_listener_thread, (void *) sl );
2098 Debug( LDAP_DEBUG_ANY,
2099 "slap_listener_activate(%d): submit failed (%d)\n",
2110 time_t last_idle_check = 0;
2112 int tid = (ldap_pvt_thread_t *) ptr - listener_tid;
2114 #define SLAPD_IDLE_CHECK_LIMIT 4
2116 slapd_add( wake_sds[tid][0], 0, NULL, tid );
2120 /* Init stuff done only by thread 0 */
2122 last_idle_check = slap_get_time();
2124 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2125 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ) continue;
2127 #ifdef LDAP_CONNECTIONLESS
2128 /* Since this is connectionless, the data port is the
2129 * listening port. The listen() and accept() calls
2132 if ( slap_listeners[l]->sl_is_udp )
2134 #endif /* LDAP_CONNECTIONLESS */
2136 /* FIXME: TCP-only! */
2137 #ifdef LDAP_TCP_BUFFER
2139 int origsize, size, realsize, rc;
2141 char buf[ SLAP_TEXT_BUFLEN ];
2144 if ( slap_listeners[l]->sl_tcp_rmem > 0 ) {
2145 size = slap_listeners[l]->sl_tcp_rmem;
2146 } else if ( slapd_tcp_rmem > 0 ) {
2147 size = slapd_tcp_rmem;
2151 optlen = sizeof( origsize );
2152 rc = getsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2159 int err = sock_errno();
2160 Debug( LDAP_DEBUG_ANY,
2161 "slapd_daemon_task: getsockopt(SO_RCVBUF) failed errno=%d (%s)\n",
2162 err, sock_errstr(err), 0 );
2165 optlen = sizeof( size );
2166 rc = setsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2169 (const void *)&size,
2173 int err = sock_errno();
2174 Debug( LDAP_DEBUG_ANY,
2175 "slapd_daemon_task: setsockopt(SO_RCVBUF) failed errno=%d (%s)\n",
2176 err, sock_errstr(err), 0 );
2179 optlen = sizeof( realsize );
2180 rc = getsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2187 int err = sock_errno();
2188 Debug( LDAP_DEBUG_ANY,
2189 "slapd_daemon_task: getsockopt(SO_RCVBUF) failed errno=%d (%s)\n",
2190 err, sock_errstr(err), 0 );
2193 snprintf( buf, sizeof( buf ),
2194 "url=%s (#%d) RCVBUF original size=%d requested size=%d real size=%d",
2195 slap_listeners[l]->sl_url.bv_val, l, origsize, size, realsize );
2196 Debug( LDAP_DEBUG_ANY,
2197 "slapd_daemon_task: %s\n",
2202 if ( slap_listeners[l]->sl_tcp_wmem > 0 ) {
2203 size = slap_listeners[l]->sl_tcp_wmem;
2204 } else if ( slapd_tcp_wmem > 0 ) {
2205 size = slapd_tcp_wmem;
2209 optlen = sizeof( origsize );
2210 rc = getsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2217 int err = sock_errno();
2218 Debug( LDAP_DEBUG_ANY,
2219 "slapd_daemon_task: getsockopt(SO_SNDBUF) failed errno=%d (%s)\n",
2220 err, sock_errstr(err), 0 );
2223 optlen = sizeof( size );
2224 rc = setsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2227 (const void *)&size,
2231 int err = sock_errno();
2232 Debug( LDAP_DEBUG_ANY,
2233 "slapd_daemon_task: setsockopt(SO_SNDBUF) failed errno=%d (%s)",
2234 err, sock_errstr(err), 0 );
2237 optlen = sizeof( realsize );
2238 rc = getsockopt( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ),
2245 int err = sock_errno();
2246 Debug( LDAP_DEBUG_ANY,
2247 "slapd_daemon_task: getsockopt(SO_SNDBUF) failed errno=%d (%s)\n",
2248 err, sock_errstr(err), 0 );
2251 snprintf( buf, sizeof( buf ),
2252 "url=%s (#%d) SNDBUF original size=%d requested size=%d real size=%d",
2253 slap_listeners[l]->sl_url.bv_val, l, origsize, size, realsize );
2254 Debug( LDAP_DEBUG_ANY,
2255 "slapd_daemon_task: %s\n",
2259 #endif /* LDAP_TCP_BUFFER */
2261 if ( listen( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ), SLAPD_LISTEN_BACKLOG ) == -1 ) {
2262 int err = sock_errno();
2264 #ifdef LDAP_PF_INET6
2265 /* If error is EADDRINUSE, we are trying to listen to INADDR_ANY and
2266 * we are already listening to in6addr_any, then we want to ignore
2267 * this and continue.
2269 if ( err == EADDRINUSE ) {
2271 struct sockaddr_in sa = slap_listeners[l]->sl_sa.sa_in_addr;
2272 struct sockaddr_in6 sa6;
2274 if ( sa.sin_family == AF_INET &&
2275 sa.sin_addr.s_addr == htonl(INADDR_ANY) ) {
2276 for ( i = 0 ; i < l; i++ ) {
2277 sa6 = slap_listeners[i]->sl_sa.sa_in6_addr;
2278 if ( sa6.sin6_family == AF_INET6 &&
2279 !memcmp( &sa6.sin6_addr, &in6addr_any,
2280 sizeof(struct in6_addr) ) )
2287 /* We are already listening to in6addr_any */
2288 Debug( LDAP_DEBUG_CONNS,
2289 "daemon: Attempt to listen to 0.0.0.0 failed, "
2290 "already listening on ::, assuming IPv4 included\n",
2292 slapd_close( slap_listeners[l]->sl_sd );
2293 slap_listeners[l]->sl_sd = AC_SOCKET_INVALID;
2298 #endif /* LDAP_PF_INET6 */
2299 Debug( LDAP_DEBUG_ANY,
2300 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
2301 slap_listeners[l]->sl_url.bv_val, err,
2306 /* make the listening socket non-blocking */
2307 if ( ber_pvt_socket_set_nonblock( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ), 1 ) < 0 ) {
2308 Debug( LDAP_DEBUG_ANY, "slapd_daemon_task: "
2309 "set nonblocking on a listening socket failed\n",
2315 slapd_add( slap_listeners[l]->sl_sd, 0, slap_listeners[l], -1 );
2318 #ifdef HAVE_NT_SERVICE_MANAGER
2319 if ( started_event != NULL ) {
2320 ldap_pvt_thread_cond_signal( &started_event );
2322 #endif /* HAVE_NT_SERVICE_MANAGER */
2326 /* initialization complete. Here comes the loop. */
2328 while ( !slapd_shutdown ) {
2333 #if SLAP_EVENTS_ARE_INDEXED
2334 ber_socket_t nrfds, nwfds;
2335 #endif /* SLAP_EVENTS_ARE_INDEXED */
2336 #define SLAPD_EBADF_LIMIT 16
2343 struct timeval *tvp;
2349 now = slap_get_time();
2351 if ( !tid && ( global_idletimeout > 0 )) {
2353 /* Set the select timeout.
2354 * Don't just truncate, preserve the fractions of
2355 * seconds to prevent sleeping for zero time.
2358 tv.tv_sec = global_idletimeout / SLAPD_IDLE_CHECK_LIMIT;
2359 tv.tv_usec = global_idletimeout - \
2360 ( tv.tv_sec * SLAPD_IDLE_CHECK_LIMIT );
2361 tv.tv_usec *= 1000000 / SLAPD_IDLE_CHECK_LIMIT;
2362 if ( difftime( last_idle_check +
2363 global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 )
2367 connections_timeout_idle( now );
2368 last_idle_check = now;
2376 if ( slapd_gentle_shutdown ) {
2377 ber_socket_t active;
2379 if ( !tid && slapd_gentle_shutdown == 1 ) {
2381 Debug( LDAP_DEBUG_ANY, "slapd gentle shutdown\n", 0, 0, 0 );
2382 close_listeners( 1 );
2383 frontendDB->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
2384 LDAP_STAILQ_FOREACH(be, &backendDB, be_next) {
2385 be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
2387 slapd_gentle_shutdown = 2;
2390 ldap_pvt_thread_mutex_lock( &slap_daemon[tid].sd_mutex );
2391 active = slap_daemon[tid].sd_nactives;
2392 ldap_pvt_thread_mutex_unlock( &slap_daemon[tid].sd_mutex );
2394 if ( active == 0 ) {
2396 for ( l=1; l<slapd_daemon_threads; l++ ) {
2397 ldap_pvt_thread_mutex_lock( &slap_daemon[l].sd_mutex );
2398 active += slap_daemon[l].sd_nactives;
2399 ldap_pvt_thread_mutex_unlock( &slap_daemon[l].sd_mutex );
2411 ldap_pvt_thread_mutex_lock( &slap_daemon[tid].sd_mutex );
2413 nwriters = slap_daemon[tid].sd_nwriters;
2416 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2417 Listener *lr = slap_listeners[l];
2419 if ( lr->sl_sd == AC_SOCKET_INVALID ) continue;
2420 if ( DAEMON_ID( lr->sl_sd ) != tid ) continue;
2422 if ( lr->sl_mute || lr->sl_busy )
2424 SLAP_SOCK_CLR_READ( tid, lr->sl_sd );
2426 SLAP_SOCK_SET_READ( tid, lr->sl_sd );
2430 SLAP_EVENT_INIT(tid);
2432 nfds = SLAP_EVENT_MAX(tid);
2434 if (( global_idletimeout ) && slap_daemon[tid].sd_nactives ) at = 1;
2436 ldap_pvt_thread_mutex_unlock( &slap_daemon[tid].sd_mutex );
2439 #if defined(HAVE_YIELDING_SELECT) || defined(NO_THREADS)
2440 && ( tv.tv_sec || tv.tv_usec )
2441 #endif /* HAVE_YIELDING_SELECT || NO_THREADS */
2449 /* Only thread 0 handles runqueue */
2451 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2452 rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
2453 while ( rtask && cat.tv_sec && cat.tv_sec <= now ) {
2454 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
2455 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
2457 ldap_pvt_runqueue_runtask( &slapd_rq, rtask );
2458 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
2459 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2460 ldap_pvt_thread_pool_submit( &connection_pool,
2461 rtask->routine, (void *) rtask );
2462 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2464 rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
2466 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2468 if ( rtask && cat.tv_sec ) {
2469 /* NOTE: diff __should__ always be >= 0,
2470 * AFAI understand; however (ITS#4872),
2471 * time_t might be unsigned in some systems,
2472 * while difftime() returns a double */
2473 double diff = difftime( cat.tv_sec, now );
2477 if ( tvp == NULL || diff < tv.tv_sec ) {
2485 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2486 Listener *lr = slap_listeners[l];
2488 if ( lr->sl_sd == AC_SOCKET_INVALID ) {
2492 if ( lr->sl_mute ) {
2493 Debug( LDAP_DEBUG_CONNS,
2494 "daemon: " SLAP_EVENT_FNAME ": "
2495 "listen=%d muted\n",
2500 if ( lr->sl_busy ) {
2501 Debug( LDAP_DEBUG_CONNS,
2502 "daemon: " SLAP_EVENT_FNAME ": "
2508 Debug( LDAP_DEBUG_CONNS,
2509 "daemon: " SLAP_EVENT_FNAME ": "
2510 "listen=%d active_threads=%d tvp=%s\n",
2511 lr->sl_sd, at, tvp == NULL ? "NULL" : "zero" );
2514 SLAP_EVENT_WAIT( tid, tvp, &ns );
2516 case -1: { /* failure - try again */
2517 int err = sock_errno();
2519 if ( err != EINTR ) {
2522 /* Don't log unless we got it twice in a row */
2523 if ( !( ebadf & 1 ) ) {
2524 Debug( LDAP_DEBUG_ANY,
2530 sock_errstr( err ) );
2532 if ( ebadf >= SLAPD_EBADF_LIMIT ) {
2539 case 0: /* timeout - let threads run */
2541 #ifndef HAVE_YIELDING_SELECT
2542 Debug( LDAP_DEBUG_CONNS, "daemon: " SLAP_EVENT_FNAME
2543 "timeout - yielding\n",
2546 ldap_pvt_thread_yield();
2547 #endif /* ! HAVE_YIELDING_SELECT */
2550 default: /* something happened - deal with it */
2551 if ( slapd_shutdown ) continue;
2554 Debug( LDAP_DEBUG_CONNS,
2555 "daemon: activity on %d descriptor%s\n",
2556 ns, ns != 1 ? "s" : "", 0 );
2560 #if SLAP_EVENTS_ARE_INDEXED
2561 if ( SLAP_EVENT_IS_READ( wake_sds[tid][0] ) ) {
2563 SLAP_EVENT_CLR_READ( wake_sds[tid][0] );
2565 tcp_read( SLAP_FD2SOCK(wake_sds[tid][0]), c, sizeof(c) );
2566 Debug( LDAP_DEBUG_CONNS, "daemon: waked\n", 0, 0, 0 );
2570 /* The event slot equals the descriptor number - this is
2571 * true for Unix select and poll. We treat Windows select
2572 * like this too, even though it's a kludge.
2575 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2578 if ( ns <= 0 ) break;
2579 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ) continue;
2580 #ifdef LDAP_CONNECTIONLESS
2581 if ( slap_listeners[l]->sl_is_udp ) continue;
2582 #endif /* LDAP_CONNECTIONLESS */
2583 if ( !SLAP_EVENT_IS_READ( slap_listeners[l]->sl_sd ) ) continue;
2586 SLAP_EVENT_CLR_READ( slap_listeners[l]->sl_sd );
2587 SLAP_EVENT_CLR_WRITE( slap_listeners[l]->sl_sd );
2590 rc = slap_listener_activate( slap_listeners[l] );
2593 /* bypass the following tests if no descriptors left */
2595 #ifndef HAVE_YIELDING_SELECT
2596 ldap_pvt_thread_yield();
2597 #endif /* HAVE_YIELDING_SELECT */
2601 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
2604 for ( i = 0; i < nfds; i++ ) {
2607 r = SLAP_EVENT_IS_READ( i );
2608 /* writefds was not initialized if nwriters was zero */
2609 w = nwriters ? SLAP_EVENT_IS_WRITE( i ) : 0;
2611 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
2612 r ? "r" : "", w ? "w" : "" );
2622 if ( ns <= 0 ) break;
2624 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
2626 /* loop through the writers */
2627 for ( i = 0; nwfds > 0; i++ ) {
2629 if ( ! SLAP_EVENT_IS_WRITE( i ) ) continue;
2632 SLAP_EVENT_CLR_WRITE( wd );
2635 Debug( LDAP_DEBUG_CONNS,
2636 "daemon: write active on %d\n",
2640 * NOTE: it is possible that the connection was closed
2641 * and that the stream is now inactive.
2642 * connection_write() must validate the stream is still
2645 * ITS#4338: if the stream is invalid, there is no need to
2646 * close it here. It has already been closed in connection.c.
2648 if ( connection_write( wd ) < 0 ) {
2649 if ( SLAP_EVENT_IS_READ( wd ) ) {
2650 SLAP_EVENT_CLR_READ( (unsigned) wd );
2656 for ( i = 0; nrfds > 0; i++ ) {
2658 if ( ! SLAP_EVENT_IS_READ( i ) ) continue;
2660 SLAP_EVENT_CLR_READ( rd );
2663 Debug ( LDAP_DEBUG_CONNS,
2664 "daemon: read activity on %d\n", rd, 0, 0 );
2666 * NOTE: it is possible that the connection was closed
2667 * and that the stream is now inactive.
2668 * connection_read() must valid the stream is still
2672 connection_read_activate( rd );
2674 #else /* !SLAP_EVENTS_ARE_INDEXED */
2676 /* The events are returned in an arbitrary list. This is true
2677 * for /dev/poll, epoll and kqueue. In order to prioritize things
2678 * so that we can handle wake_sds first, listeners second, and then
2679 * all other connections last (as we do for select), we would need
2680 * to use multiple event handles and cascade them.
2682 * That seems like a bit of hassle. So the wake_sds check has been
2683 * skipped. For epoll and kqueue we can associate arbitrary data with
2684 * an event, so we could use pointers to the listener structure
2685 * instead of just the file descriptor. For /dev/poll we have to
2686 * search the listeners array for a matching descriptor.
2688 * We now handle wake events when we see them; they are not given
2692 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
2694 for ( i = 0; i < ns; i++ ) {
2697 /* Don't log listener events */
2698 if ( SLAP_EVENT_IS_LISTENER( tid, i )
2699 #ifdef LDAP_CONNECTIONLESS
2700 && !( (SLAP_EVENT_LISTENER( tid, i ))->sl_is_udp )
2701 #endif /* LDAP_CONNECTIONLESS */
2707 fd = SLAP_EVENT_FD( tid, i );
2708 /* Don't log internal wake events */
2709 if ( fd == wake_sds[tid][0] ) continue;
2711 r = SLAP_EVENT_IS_READ( i );
2712 w = SLAP_EVENT_IS_WRITE( i );
2714 Debug( LDAP_DEBUG_CONNS, " %d%s%s", fd,
2715 r ? "r" : "", w ? "w" : "" );
2718 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
2719 #endif /* LDAP_DEBUG */
2721 for ( i = 0; i < ns; i++ ) {
2722 int rc = 1, fd, w = 0, r = 0;
2724 if ( SLAP_EVENT_IS_LISTENER( tid, i ) ) {
2725 rc = slap_listener_activate( SLAP_EVENT_LISTENER( tid, i ) );
2728 /* If we found a regular listener, rc is now zero, and we
2729 * can skip the data portion. But if it was a UDP listener
2730 * then rc is still 1, and we want to handle the data.
2733 fd = SLAP_EVENT_FD( tid, i );
2735 /* Handle wake events */
2736 if ( fd == wake_sds[tid][0] ) {
2739 tcp_read( SLAP_FD2SOCK(wake_sds[tid][0]), c, sizeof(c) );
2743 if ( SLAP_EVENT_IS_WRITE( i ) ) {
2744 Debug( LDAP_DEBUG_CONNS,
2745 "daemon: write active on %d\n",
2748 SLAP_EVENT_CLR_WRITE( i );
2752 * NOTE: it is possible that the connection was closed
2753 * and that the stream is now inactive.
2754 * connection_write() must valid the stream is still
2757 if ( connection_write( fd ) < 0 ) {
2761 /* If event is a read */
2762 if ( SLAP_EVENT_IS_READ( i )) {
2764 Debug( LDAP_DEBUG_CONNS,
2765 "daemon: read active on %d\n",
2768 SLAP_EVENT_CLR_READ( i );
2769 connection_read_activate( fd );
2772 /* Don't keep reporting the hangup
2774 if ( SLAP_SOCK_IS_ACTIVE( tid, fd )) {
2775 SLAP_EPOLL_SOCK_SET( tid, fd, EPOLLET );
2781 #endif /* SLAP_EVENTS_ARE_INDEXED */
2783 #ifndef HAVE_YIELDING_SELECT
2784 ldap_pvt_thread_yield();
2785 #endif /* ! HAVE_YIELDING_SELECT */
2788 /* Only thread 0 handles shutdown */
2792 if ( slapd_shutdown == 1 ) {
2793 Debug( LDAP_DEBUG_ANY,
2794 "daemon: shutdown requested and initiated.\n",
2797 } else if ( slapd_shutdown == 2 ) {
2798 #ifdef HAVE_NT_SERVICE_MANAGER
2799 Debug( LDAP_DEBUG_ANY,
2800 "daemon: shutdown initiated by Service Manager.\n",
2802 #else /* !HAVE_NT_SERVICE_MANAGER */
2803 Debug( LDAP_DEBUG_ANY,
2804 "daemon: abnormal condition, shutdown initiated.\n",
2806 #endif /* !HAVE_NT_SERVICE_MANAGER */
2808 Debug( LDAP_DEBUG_ANY,
2809 "daemon: no active streams, shutdown initiated.\n",
2813 close_listeners( 0 );
2815 if ( !slapd_gentle_shutdown ) {
2816 slapd_abrupt_shutdown = 1;
2817 connections_shutdown();
2820 if ( LogTest( LDAP_DEBUG_ANY )) {
2821 int t = ldap_pvt_thread_pool_backload( &connection_pool );
2822 Debug( LDAP_DEBUG_ANY,
2823 "slapd shutdown: waiting for %d operations/tasks to finish\n",
2826 ldap_pvt_thread_pool_destroy( &connection_pool, 1 );
2832 #ifdef LDAP_CONNECTIONLESS
2834 connectionless_init( void )
2838 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2839 Listener *lr = slap_listeners[l];
2842 if ( !lr->sl_is_udp ) {
2846 c = connection_init( lr->sl_sd, lr, "", "",
2847 CONN_IS_UDP, (slap_ssf_t) 0, NULL
2848 LDAP_PF_LOCAL_SENDMSG_ARG(NULL));
2851 Debug( LDAP_DEBUG_TRACE,
2852 "connectionless_init: failed on %s (%d)\n",
2853 lr->sl_url.bv_val, lr->sl_sd, 0 );
2861 #endif /* LDAP_CONNECTIONLESS */
2864 slapd_daemon( void )
2868 #ifdef LDAP_CONNECTIONLESS
2869 connectionless_init();
2870 #endif /* LDAP_CONNECTIONLESS */
2872 if ( slapd_daemon_threads > SLAPD_MAX_DAEMON_THREADS )
2873 slapd_daemon_threads = SLAPD_MAX_DAEMON_THREADS;
2875 listener_tid = ch_malloc(slapd_daemon_threads * sizeof(ldap_pvt_thread_t));
2877 /* daemon_init only inits element 0 */
2878 for ( i=1; i<slapd_daemon_threads; i++ )
2880 ldap_pvt_thread_mutex_init( &slap_daemon[i].sd_mutex );
2882 if( (rc = lutil_pair( wake_sds[i] )) < 0 ) {
2883 Debug( LDAP_DEBUG_ANY,
2884 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
2887 ber_pvt_socket_set_nonblock( wake_sds[i][1], 1 );
2892 for ( i=0; i<slapd_daemon_threads; i++ )
2894 /* listener as a separate THREAD */
2895 rc = ldap_pvt_thread_create( &listener_tid[i],
2896 0, slapd_daemon_task, &listener_tid[i] );
2899 Debug( LDAP_DEBUG_ANY,
2900 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
2905 /* wait for the listener threads to complete */
2906 for ( i=0; i<slapd_daemon_threads; i++ )
2907 ldap_pvt_thread_join( listener_tid[i], (void *)NULL );
2909 destroy_listeners();
2910 ch_free( listener_tid );
2911 listener_tid = NULL;
2919 #if defined( HAVE_WINSOCK2 )
2920 WORD wVersionRequested;
2924 wVersionRequested = MAKEWORD( 2, 0 );
2926 err = WSAStartup( wVersionRequested, &wsaData );
2928 /* Tell the user that we couldn't find a usable */
2933 /* Confirm that the WinSock DLL supports 2.0.*/
2934 /* Note that if the DLL supports versions greater */
2935 /* than 2.0 in addition to 2.0, it will still return */
2936 /* 2.0 in wVersion since that is the version we */
2939 if ( LOBYTE( wsaData.wVersion ) != 2 ||
2940 HIBYTE( wsaData.wVersion ) != 0 )
2942 /* Tell the user that we couldn't find a usable */
2948 /* The WinSock DLL is acceptable. Proceed. */
2949 #elif defined( HAVE_WINSOCK )
2951 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) return -1;
2952 #endif /* ! HAVE_WINSOCK2 && ! HAVE_WINSOCK */
2960 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
2962 #endif /* HAVE_WINSOCK2 || HAVE_WINSOCK */
2968 slap_sig_shutdown( int sig )
2970 int save_errno = errno;
2974 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
2978 * If the NT Service Manager is controlling the server, we don't
2979 * want SIGBREAK to kill the server. For some strange reason,
2980 * SIGBREAK is generated when a user logs out.
2983 #if defined(HAVE_NT_SERVICE_MANAGER) && defined(SIGBREAK)
2984 if (is_NT_Service && sig == SIGBREAK) {
2987 #endif /* HAVE_NT_SERVICE_MANAGER && SIGBREAK */
2989 if (sig == SIGHUP && global_gentlehup && slapd_gentle_shutdown == 0) {
2990 slapd_gentle_shutdown = 1;
2997 for (i=0; i<slapd_daemon_threads; i++) {
3001 /* reinstall self */
3002 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
3008 slap_sig_wake( int sig )
3010 int save_errno = errno;
3014 /* reinstall self */
3015 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
3022 slapd_add_internal( ber_socket_t s, int isactive )
3024 slapd_add( s, isactive, NULL, -1 );
3028 slapd_get_listeners( void )
3030 /* Could return array with no listeners if !listening, but current
3031 * callers mostly look at the URLs. E.g. syncrepl uses this to
3032 * identify the server, which means it wants the startup arguments.
3034 return slap_listeners;
3037 /* Reject all incoming requests */
3039 slap_suspend_listeners( void )
3042 for (i=0; slap_listeners[i]; i++) {
3043 slap_listeners[i]->sl_mute = 1;
3044 listen( slap_listeners[i]->sl_sd, 0 );
3048 /* Resume after a suspend */
3050 slap_resume_listeners( void )
3053 for (i=0; slap_listeners[i]; i++) {
3054 slap_listeners[i]->sl_mute = 0;
3055 listen( slap_listeners[i]->sl_sd, SLAPD_LISTEN_BACKLOG );
3060 slap_wake_listener()
3065 /* return 0 on timeout, 1 on writer ready
3066 * -1 on general error
3069 slapd_wait_writer( ber_socket_t sd )
3073 struct timeval tv, *tvp;
3075 FD_ZERO( &writefds );
3076 FD_SET( slapd_ws_sockets[sd], &writefds );
3077 if ( global_writetimeout ) {
3078 tv.tv_sec = global_writetimeout;
3084 return select( 0, NULL, &writefds, NULL, tvp );
3087 int timeout = global_writetimeout ? global_writetimeout * 1000 : -1;
3090 fds.events = POLLOUT;
3092 return poll( &fds, 1, timeout );