]> git.sur5r.net Git - openldap/blob - servers/slapd/daemon.c
fb01e403f34809d4efff0caad573c4774a25ad88
[openldap] / servers / slapd / daemon.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2009 The OpenLDAP Foundation.
5  * Portions Copyright 2007 by Howard Chu, Symas Corporation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
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.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/ctype.h>
32 #include <ac/errno.h>
33 #include <ac/socket.h>
34 #include <ac/string.h>
35 #include <ac/time.h>
36 #include <ac/unistd.h>
37
38 #include "slap.h"
39 #include "ldap_pvt_thread.h"
40 #include "lutil.h"
41
42 #include "ldap_rq.h"
43
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>
49 # include <fcntl.h>
50 # include <sys/devpoll.h>
51 #endif /* ! epoll && ! /dev/poll */
52
53 #ifdef HAVE_TCPD
54 int allow_severity = LOG_INFO;
55 int deny_severity = LOG_NOTICE;
56 #endif /* TCP Wrappers */
57
58 #ifdef LDAP_PF_LOCAL
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 */
63
64 #ifdef LDAP_PF_INET6
65 int slap_inet4or6 = AF_UNSPEC;
66 #else /* ! INETv6 */
67 int slap_inet4or6 = AF_INET;
68 #endif /* ! INETv6 */
69
70 /* globals */
71 time_t starttime;
72 ber_socket_t dtblsize;
73 slap_ssf_t local_ssf = LDAP_PVT_SASL_LOCAL_SSF;
74 struct runqueue_s slapd_rq;
75
76 Listener **slap_listeners = NULL;
77
78 #ifndef SLAPD_LISTEN_BACKLOG
79 #define SLAPD_LISTEN_BACKLOG 1024
80 #endif /* ! SLAPD_LISTEN_BACKLOG */
81
82 static ber_socket_t wake_sds[2]
83 #ifdef HAVE_WINSOCK
84         = { INVALID_SOCKET, INVALID_SOCKET }
85 #endif /* HAVE_WINSOCK */
86         ;
87 static int emfile;
88
89 static volatile int waking;
90 #ifdef NO_THREADS
91 #define WAKE_LISTENER(w)        do { \
92         if ((w) && ++waking < 5) { \
93                 tcp_write( SLAP_FD2SOCK(wake_sds[1]), "0", 1 ); \
94         } \
95 } while (0)
96 #else /* ! NO_THREADS */
97 #define WAKE_LISTENER(w)        do { \
98         if (w) { \
99                 tcp_write( SLAP_FD2SOCK(wake_sds[1]), "0", 1 ); \
100         } \
101 } while (0)
102 #endif /* ! NO_THREADS */
103
104 volatile sig_atomic_t slapd_shutdown = 0;
105 volatile sig_atomic_t slapd_gentle_shutdown = 0;
106 volatile sig_atomic_t slapd_abrupt_shutdown = 0;
107
108 #ifdef HAVE_WINSOCK
109 ldap_pvt_thread_mutex_t slapd_ws_mutex;
110 SOCKET *slapd_ws_sockets;
111 #define SD_READ 1
112 #define SD_WRITE        2
113 #define SD_ACTIVE       4
114 #define SD_LISTENER     8
115 #endif
116
117 static struct slap_daemon {
118         ldap_pvt_thread_mutex_t sd_mutex;
119 #ifdef HAVE_TCPD
120         ldap_pvt_thread_mutex_t sd_tcpd_mutex;
121 #endif /* TCP Wrappers */
122
123         ber_socket_t            sd_nactives;
124         int                     sd_nwriters;
125         int                     sd_nfds;
126
127 #if defined(HAVE_EPOLL)
128         struct epoll_event      *sd_epolls;
129         int                     *sd_index;
130         int                     sd_epfd;
131 #elif defined(SLAP_X_DEVPOLL) && defined(HAVE_DEVPOLL)
132         /* eXperimental */
133         struct pollfd           *sd_pollfd;
134         int                     *sd_index;
135         Listener                **sd_l;
136         int                     sd_dpfd;
137 #else /* ! epoll && ! /dev/poll */
138 #ifdef HAVE_WINSOCK
139         char    *sd_flags;
140         char    *sd_rflags;
141 #else /* ! HAVE_WINSOCK */
142         fd_set                  sd_actives;
143         fd_set                  sd_readers;
144         fd_set                  sd_writers;
145 #endif /* ! HAVE_WINSOCK */
146 #endif /* ! epoll && ! /dev/poll */
147 } slap_daemon;
148
149 /*
150  * NOTE: naming convention for macros:
151  *
152  * - SLAP_SOCK_* and SLAP_EVENT_* for public interface that deals
153  *   with file descriptors and events respectively
154  *
155  * - SLAP_<type>_* for private interface; type by now is one of
156  *   EPOLL, DEVPOLL, SELECT
157  *
158  * private interface should not be used in the code.
159  */
160 #if defined(HAVE_EPOLL)
161 /***************************************
162  * Use epoll infrastructure - epoll(4) *
163  ***************************************/
164
165 #undef SLAP_EVENT_ACK           /* events trigger once per descriptor */
166
167 # define SLAP_EVENT_FNAME               "epoll"
168 # define SLAP_EVENTS_ARE_INDEXED        0
169 # define SLAP_EPOLL_SOCK_IX(s)          (slap_daemon.sd_index[(s)])
170 # define SLAP_EPOLL_SOCK_EP(s)          (slap_daemon.sd_epolls[SLAP_EPOLL_SOCK_IX(s)])
171 # define SLAP_EPOLL_SOCK_EV(s)          (SLAP_EPOLL_SOCK_EP(s).events)
172 # define SLAP_SOCK_IS_ACTIVE(s)         (SLAP_EPOLL_SOCK_IX(s) != -1)
173 # define SLAP_SOCK_NOT_ACTIVE(s)        (SLAP_EPOLL_SOCK_IX(s) == -1)
174 # define SLAP_EPOLL_SOCK_IS_SET(s, mode)        (SLAP_EPOLL_SOCK_EV(s) & (mode))
175
176 # define SLAP_SOCK_IS_READ(s)           SLAP_EPOLL_SOCK_IS_SET((s), EPOLLIN)
177 # define SLAP_SOCK_IS_WRITE(s)          SLAP_EPOLL_SOCK_IS_SET((s), EPOLLOUT)
178
179 # define SLAP_EPOLL_SOCK_SET(s, mode)   do { \
180         if ( (SLAP_EPOLL_SOCK_EV(s) & (mode)) != (mode) ) {     \
181                 SLAP_EPOLL_SOCK_EV(s) |= (mode); \
182                 epoll_ctl( slap_daemon.sd_epfd, EPOLL_CTL_MOD, (s), \
183                         &SLAP_EPOLL_SOCK_EP(s) ); \
184         } \
185 } while (0)
186
187 # define SLAP_EPOLL_SOCK_CLR(s, mode)   do { \
188         if ( (SLAP_EPOLL_SOCK_EV(s) & (mode)) ) { \
189                 SLAP_EPOLL_SOCK_EV(s) &= ~(mode);       \
190                 epoll_ctl( slap_daemon.sd_epfd, EPOLL_CTL_MOD, s, \
191                         &SLAP_EPOLL_SOCK_EP(s) ); \
192         } \
193 } while (0)
194
195 # define SLAP_SOCK_SET_READ(s)          SLAP_EPOLL_SOCK_SET(s, EPOLLIN)
196 # define SLAP_SOCK_SET_WRITE(s)         SLAP_EPOLL_SOCK_SET(s, EPOLLOUT)
197
198 # define SLAP_SOCK_CLR_READ(s)          SLAP_EPOLL_SOCK_CLR((s), EPOLLIN)
199 # define SLAP_SOCK_CLR_WRITE(s)         SLAP_EPOLL_SOCK_CLR((s), EPOLLOUT)
200
201 #  define SLAP_SOCK_SET_SUSPEND(s) \
202         ( slap_daemon.sd_suspend[SLAP_EPOLL_SOCK_IX(s)] = 1 )
203 #  define SLAP_SOCK_CLR_SUSPEND(s) \
204         ( slap_daemon.sd_suspend[SLAP_EPOLL_SOCK_IX(s)] = 0 )
205 #  define SLAP_SOCK_IS_SUSPEND(s) \
206         ( slap_daemon.sd_suspend[SLAP_EPOLL_SOCK_IX(s)] == 1 )
207
208 # define SLAP_EPOLL_EVENT_CLR(i, mode)  (revents[(i)].events &= ~(mode))
209
210 # define SLAP_EVENT_MAX                 slap_daemon.sd_nfds
211
212 /* If a Listener address is provided, store that as the epoll data.
213  * Otherwise, store the address of this socket's slot in the
214  * index array. If we can't do this add, the system is out of
215  * resources and we need to shutdown.
216  */
217 # define SLAP_SOCK_ADD(s, l)            do { \
218         int rc; \
219         SLAP_EPOLL_SOCK_IX((s)) = slap_daemon.sd_nfds; \
220         SLAP_EPOLL_SOCK_EP((s)).data.ptr = (l) ? (l) : (void *)(&SLAP_EPOLL_SOCK_IX(s)); \
221         SLAP_EPOLL_SOCK_EV((s)) = EPOLLIN|EPOLLET; \
222         rc = epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_ADD, \
223                 (s), &SLAP_EPOLL_SOCK_EP((s))); \
224         if ( rc == 0 ) { \
225                 slap_daemon.sd_nfds++; \
226         } else { \
227                 Debug( LDAP_DEBUG_ANY, \
228                         "daemon: epoll_ctl(ADD,fd=%d) failed, errno=%d, shutting down\n", \
229                         s, errno, 0 ); \
230                 slapd_shutdown = 2; \
231         } \
232 } while (0)
233
234 # define SLAP_EPOLL_EV_LISTENER(ptr) \
235         (((int *)(ptr) >= slap_daemon.sd_index && \
236         (int *)(ptr) <= &slap_daemon.sd_index[dtblsize]) ? 0 : 1 )
237
238 # define SLAP_EPOLL_EV_PTRFD(ptr)               (SLAP_EPOLL_EV_LISTENER(ptr) ? \
239         ((Listener *)ptr)->sl_sd : \
240         (ber_socket_t) ((int *)(ptr) - slap_daemon.sd_index))
241
242 # define SLAP_SOCK_DEL(s)               do { \
243         int fd, rc, index = SLAP_EPOLL_SOCK_IX((s)); \
244         if ( index < 0 ) break; \
245         rc = epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_DEL, \
246                 (s), &SLAP_EPOLL_SOCK_EP((s))); \
247         slap_daemon.sd_epolls[index] = \
248                 slap_daemon.sd_epolls[slap_daemon.sd_nfds-1]; \
249         fd = SLAP_EPOLL_EV_PTRFD(slap_daemon.sd_epolls[index].data.ptr); \
250         slap_daemon.sd_index[fd] = index; \
251         slap_daemon.sd_index[(s)] = -1; \
252         slap_daemon.sd_nfds--; \
253 } while (0)
254
255 # define SLAP_EVENT_CLR_READ(i)         SLAP_EPOLL_EVENT_CLR((i), EPOLLIN)
256 # define SLAP_EVENT_CLR_WRITE(i)        SLAP_EPOLL_EVENT_CLR((i), EPOLLOUT)
257
258 # define SLAP_EPOLL_EVENT_CHK(i, mode)  (revents[(i)].events & mode)
259
260 # define SLAP_EVENT_IS_READ(i)          SLAP_EPOLL_EVENT_CHK((i), EPOLLIN)
261 # define SLAP_EVENT_IS_WRITE(i)         SLAP_EPOLL_EVENT_CHK((i), EPOLLOUT)
262 # define SLAP_EVENT_IS_LISTENER(i)      SLAP_EPOLL_EV_LISTENER(revents[(i)].data.ptr)
263 # define SLAP_EVENT_LISTENER(i)         ((Listener *)(revents[(i)].data.ptr))
264
265 # define SLAP_EVENT_FD(i)               SLAP_EPOLL_EV_PTRFD(revents[(i)].data.ptr)
266
267 # define SLAP_SOCK_INIT         do { \
268         slap_daemon.sd_epolls = ch_calloc(1, \
269                 ( sizeof(struct epoll_event) * 2 \
270                         + sizeof(int) ) * dtblsize * 2); \
271         slap_daemon.sd_index = (int *)&slap_daemon.sd_epolls[ 2 * dtblsize ]; \
272         slap_daemon.sd_epfd = epoll_create( dtblsize ); \
273         for ( i = 0; i < dtblsize; i++ ) slap_daemon.sd_index[i] = -1; \
274 } while (0)
275
276 # define SLAP_SOCK_DESTROY              do { \
277         if ( slap_daemon.sd_epolls != NULL ) { \
278                 ch_free( slap_daemon.sd_epolls ); \
279                 slap_daemon.sd_epolls = NULL; \
280                 slap_daemon.sd_index = NULL; \
281                 close( slap_daemon.sd_epfd ); \
282         } \
283 } while ( 0 )
284
285 # define SLAP_EVENT_DECL                struct epoll_event *revents
286
287 # define SLAP_EVENT_INIT                do { \
288         revents = slap_daemon.sd_epolls + dtblsize; \
289 } while (0)
290
291 # define SLAP_EVENT_WAIT(tvp, nsp)      do { \
292         *(nsp) = epoll_wait( slap_daemon.sd_epfd, revents, \
293                 dtblsize, (tvp) ? (tvp)->tv_sec * 1000 : -1 ); \
294 } while (0)
295
296 #elif defined(SLAP_X_DEVPOLL) && defined(HAVE_DEVPOLL)
297 /*************************************************************
298  * Use Solaris' (>= 2.7) /dev/poll infrastructure - poll(7d) *
299  *************************************************************/
300
301 #define SLAP_EVENT_ACK  1       /* events keep signalling unless we stop them */
302
303 # define SLAP_EVENT_FNAME               "/dev/poll"
304 # define SLAP_EVENTS_ARE_INDEXED        0
305 /*
306  * - sd_index   is used much like with epoll()
307  * - sd_l       is maintained as an array containing the address
308  *              of the listener; the index is the fd itself
309  * - sd_pollfd  is used to keep track of what data has been
310  *              registered in /dev/poll
311  */
312 # define SLAP_DEVPOLL_SOCK_IX(s)        (slap_daemon.sd_index[(s)])
313 # define SLAP_DEVPOLL_SOCK_LX(s)        (slap_daemon.sd_l[(s)])
314 # define SLAP_DEVPOLL_SOCK_EP(s)        (slap_daemon.sd_pollfd[SLAP_DEVPOLL_SOCK_IX((s))])
315 # define SLAP_DEVPOLL_SOCK_FD(s)        (SLAP_DEVPOLL_SOCK_EP((s)).fd)
316 # define SLAP_DEVPOLL_SOCK_EV(s)        (SLAP_DEVPOLL_SOCK_EP((s)).events)
317 # define SLAP_SOCK_IS_ACTIVE(s)         (SLAP_DEVPOLL_SOCK_IX((s)) != -1)
318 # define SLAP_SOCK_NOT_ACTIVE(s)        (SLAP_DEVPOLL_SOCK_IX((s)) == -1)
319 # define SLAP_SOCK_IS_SET(s, mode)      (SLAP_DEVPOLL_SOCK_EV((s)) & (mode))
320
321 # define SLAP_SOCK_IS_READ(s)           SLAP_SOCK_IS_SET((s), POLLIN)
322 # define SLAP_SOCK_IS_WRITE(s)          SLAP_SOCK_IS_SET((s), POLLOUT)
323
324 /* as far as I understand, any time we need to communicate with the kernel
325  * about the number and/or properties of a file descriptor we need it to
326  * wait for, we have to rewrite the whole set */
327 # define SLAP_DEVPOLL_WRITE_POLLFD(s, pfd, n, what, shdn)       do { \
328         int rc; \
329         size_t size = (n) * sizeof( struct pollfd ); \
330         /* FIXME: use pwrite? */ \
331         rc = write( slap_daemon.sd_dpfd, (pfd), size ); \
332         if ( rc != size ) { \
333                 Debug( LDAP_DEBUG_ANY, "daemon: " SLAP_EVENT_FNAME ": " \
334                         "%s fd=%d failed errno=%d\n", \
335                         (what), (s), errno ); \
336                 if ( (shdn) ) { \
337                         slapd_shutdown = 2; \
338                 } \
339         } \
340 } while (0)
341
342 # define SLAP_DEVPOLL_SOCK_SET(s, mode)         do { \
343         Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_SET_%s(%d)=%d\n", \
344                 (mode) == POLLIN ? "READ" : "WRITE", (s), \
345                 ( (SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) != (mode) ) ); \
346         if ( (SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) != (mode) ) { \
347                 struct pollfd pfd; \
348                 SLAP_DEVPOLL_SOCK_EV((s)) |= (mode); \
349                 pfd.fd = SLAP_DEVPOLL_SOCK_FD((s)); \
350                 pfd.events = /* (mode) */ SLAP_DEVPOLL_SOCK_EV((s)); \
351                 SLAP_DEVPOLL_WRITE_POLLFD((s), &pfd, 1, "SET", 0); \
352         } \
353 } while (0)
354
355 # define SLAP_DEVPOLL_SOCK_CLR(s, mode)         do { \
356         Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_CLR_%s(%d)=%d\n", \
357                 (mode) == POLLIN ? "READ" : "WRITE", (s), \
358                 ( (SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) == (mode) ) ); \
359         if ((SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) == (mode) ) { \
360                 struct pollfd pfd[2]; \
361                 SLAP_DEVPOLL_SOCK_EV((s)) &= ~(mode); \
362                 pfd[0].fd = SLAP_DEVPOLL_SOCK_FD((s)); \
363                 pfd[0].events = POLLREMOVE; \
364                 pfd[1] = SLAP_DEVPOLL_SOCK_EP((s)); \
365                 SLAP_DEVPOLL_WRITE_POLLFD((s), &pfd[0], 2, "CLR", 0); \
366         } \
367 } while (0)
368
369 # define SLAP_SOCK_SET_READ(s)          SLAP_DEVPOLL_SOCK_SET(s, POLLIN)
370 # define SLAP_SOCK_SET_WRITE(s)         SLAP_DEVPOLL_SOCK_SET(s, POLLOUT)
371
372 # define SLAP_SOCK_CLR_READ(s)          SLAP_DEVPOLL_SOCK_CLR((s), POLLIN)
373 # define SLAP_SOCK_CLR_WRITE(s)         SLAP_DEVPOLL_SOCK_CLR((s), POLLOUT)
374
375 #  define SLAP_SOCK_SET_SUSPEND(s) \
376         ( slap_daemon.sd_suspend[SLAP_DEVPOLL_SOCK_IX((s))] = 1 )
377 #  define SLAP_SOCK_CLR_SUSPEND(s) \
378         ( slap_daemon.sd_suspend[SLAP_DEVPOLL_SOCK_IX((s))] = 0 )
379 #  define SLAP_SOCK_IS_SUSPEND(s) \
380         ( slap_daemon.sd_suspend[SLAP_DEVPOLL_SOCK_IX((s))] == 1 )
381
382 # define SLAP_DEVPOLL_EVENT_CLR(i, mode)        (revents[(i)].events &= ~(mode))
383
384 # define SLAP_EVENT_MAX                 slap_daemon.sd_nfds
385
386 /* If a Listener address is provided, store that in the sd_l array.
387  * If we can't do this add, the system is out of resources and we 
388  * need to shutdown.
389  */
390 # define SLAP_SOCK_ADD(s, l)            do { \
391         Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_ADD(%d, %p)\n", (s), (l), 0 ); \
392         SLAP_DEVPOLL_SOCK_IX((s)) = slap_daemon.sd_nfds; \
393         SLAP_DEVPOLL_SOCK_LX((s)) = (l); \
394         SLAP_DEVPOLL_SOCK_FD((s)) = (s); \
395         SLAP_DEVPOLL_SOCK_EV((s)) = POLLIN; \
396         SLAP_DEVPOLL_WRITE_POLLFD((s), &SLAP_DEVPOLL_SOCK_EP((s)), 1, "ADD", 1); \
397         slap_daemon.sd_nfds++; \
398 } while (0)
399
400 # define SLAP_DEVPOLL_EV_LISTENER(ptr)  ((ptr) != NULL)
401
402 # define SLAP_SOCK_DEL(s)               do { \
403         int fd, index = SLAP_DEVPOLL_SOCK_IX((s)); \
404         Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_DEL(%d)\n", (s), 0, 0 ); \
405         if ( index < 0 ) break; \
406         if ( index < slap_daemon.sd_nfds - 1 ) { \
407                 struct pollfd pfd = slap_daemon.sd_pollfd[index]; \
408                 fd = slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1].fd; \
409                 slap_daemon.sd_pollfd[index] = slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1]; \
410                 slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1] = pfd; \
411                 slap_daemon.sd_index[fd] = index; \
412         } \
413         slap_daemon.sd_index[(s)] = -1; \
414         slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1].events = POLLREMOVE; \
415         SLAP_DEVPOLL_WRITE_POLLFD((s), &slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1], 1, "DEL", 0); \
416         slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1].events = 0; \
417         slap_daemon.sd_nfds--; \
418 } while (0)
419
420 # define SLAP_EVENT_CLR_READ(i)         SLAP_DEVPOLL_EVENT_CLR((i), POLLIN)
421 # define SLAP_EVENT_CLR_WRITE(i)        SLAP_DEVPOLL_EVENT_CLR((i), POLLOUT)
422
423 # define SLAP_DEVPOLL_EVENT_CHK(i, mode)        (revents[(i)].events & (mode))
424
425 # define SLAP_EVENT_FD(i)               (revents[(i)].fd)
426
427 # define SLAP_EVENT_IS_READ(i)          SLAP_DEVPOLL_EVENT_CHK((i), POLLIN)
428 # define SLAP_EVENT_IS_WRITE(i)         SLAP_DEVPOLL_EVENT_CHK((i), POLLOUT)
429 # define SLAP_EVENT_IS_LISTENER(i)      SLAP_DEVPOLL_EV_LISTENER(SLAP_DEVPOLL_SOCK_LX(SLAP_EVENT_FD((i))))
430 # define SLAP_EVENT_LISTENER(i)         SLAP_DEVPOLL_SOCK_LX(SLAP_EVENT_FD((i)))
431
432 # define SLAP_SOCK_INIT         do { \
433         slap_daemon.sd_pollfd = ch_calloc( 1, \
434                 ( sizeof(struct pollfd) * 2 \
435                         + sizeof( int ) \
436                         + sizeof( Listener * ) ) * dtblsize ); \
437         slap_daemon.sd_index = (int *)&slap_daemon.sd_pollfd[ 2 * dtblsize ]; \
438         slap_daemon.sd_l = (Listener **)&slap_daemon.sd_index[ dtblsize ]; \
439         slap_daemon.sd_dpfd = open( SLAP_EVENT_FNAME, O_RDWR ); \
440         if ( slap_daemon.sd_dpfd == -1 ) { \
441                 Debug( LDAP_DEBUG_ANY, "daemon: " SLAP_EVENT_FNAME ": " \
442                         "open(\"" SLAP_EVENT_FNAME "\") failed errno=%d\n", \
443                         errno, 0, 0 ); \
444                 SLAP_SOCK_DESTROY; \
445                 return -1; \
446         } \
447         for ( i = 0; i < dtblsize; i++ ) { \
448                 slap_daemon.sd_pollfd[i].fd = -1; \
449                 slap_daemon.sd_index[i] = -1; \
450         } \
451 } while (0)
452
453 # define SLAP_SOCK_DESTROY              do { \
454         if ( slap_daemon.sd_pollfd != NULL ) { \
455                 ch_free( slap_daemon.sd_pollfd ); \
456                 slap_daemon.sd_pollfd = NULL; \
457                 slap_daemon.sd_index = NULL; \
458                 slap_daemon.sd_l = NULL; \
459                 close( slap_daemon.sd_dpfd ); \
460         } \
461 } while ( 0 )
462
463 # define SLAP_EVENT_DECL                struct pollfd *revents
464
465 # define SLAP_EVENT_INIT                do { \
466         revents = &slap_daemon.sd_pollfd[ dtblsize ]; \
467 } while (0)
468
469 # define SLAP_EVENT_WAIT(tvp, nsp)      do { \
470         struct dvpoll           sd_dvpoll; \
471         sd_dvpoll.dp_timeout = (tvp) ? (tvp)->tv_sec * 1000 : -1; \
472         sd_dvpoll.dp_nfds = dtblsize; \
473         sd_dvpoll.dp_fds = revents; \
474         *(nsp) = ioctl( slap_daemon.sd_dpfd, DP_POLL, &sd_dvpoll ); \
475 } while (0)
476
477 #else /* ! epoll && ! /dev/poll */
478
479 #define SLAP_EVENT_ACK  1       /* events keep signalling unless we stop them */
480
481 # ifdef HAVE_WINSOCK
482 # define SLAP_EVENT_FNAME               "WSselect"
483 /* Winsock provides a "select" function but its fd_sets are
484  * actually arrays of sockets. Since these sockets are handles
485  * and not a contiguous range of small integers, we manage our
486  * own "fd" table of socket handles and use their indices as
487  * descriptors.
488  *
489  * All of our listener/connection structures use fds; the actual
490  * I/O functions use sockets. The SLAP_FD2SOCK macro in proto-slap.h
491  * handles the mapping.
492  *
493  * Despite the mapping overhead, this is about 45% more efficient
494  * than just using Winsock's select and FD_ISSET directly.
495  *
496  * Unfortunately Winsock's select implementation doesn't scale well
497  * as the number of connections increases. This probably needs to be
498  * rewritten to use the Winsock overlapped/asynchronous I/O functions.
499  */
500 # define SLAP_EVENTS_ARE_INDEXED        1
501 # define SLAP_EVENT_DECL                fd_set readfds, writefds
502 # define SLAP_EVENT_INIT        do { \
503         int i; \
504         FD_ZERO( &readfds ); \
505         FD_ZERO( &writefds ); \
506         memset( slap_daemon.sd_rflags, 0, slap_daemon.sd_nfds ); \
507         for ( i=0; i<slap_daemon.sd_nfds; i++ ) { \
508                 if ( slap_daemon.sd_flags[i] & SD_READ ) \
509                         FD_SET( slapd_ws_sockets[i], &readfds );\
510                 if ( slap_daemon.sd_flags[i] & SD_WRITE ) \
511                         FD_SET( slapd_ws_sockets[i], &writefds ); \
512         } } while ( 0 )
513
514 # define SLAP_EVENT_MAX         slap_daemon.sd_nfds
515
516 # define SLAP_EVENT_WAIT(tvp, nsp)      do { \
517         int i; \
518         *(nsp) = select( SLAP_EVENT_MAX, &readfds, \
519                 nwriters > 0 ? &writefds : NULL, NULL, (tvp) ); \
520         for ( i=0; i<readfds.fd_count; i++) { \
521                 int fd = slapd_sock2fd(readfds.fd_array[i]); \
522                 if ( fd >= 0 ) { \
523                         slap_daemon.sd_rflags[fd] = SD_READ; \
524                         if ( fd >= *(nsp)) *(nsp) = fd+1; \
525                 } \
526         } \
527         for ( i=0; i<writefds.fd_count; i++) { \
528                 int fd = slapd_sock2fd(writefds.fd_array[i]); \
529                 if ( fd >= 0 ) { \
530                         slap_daemon.sd_rflags[fd] = SD_WRITE; \
531                         if ( fd >= *(nsp)) *(nsp) = fd+1; \
532                 } \
533         } \
534 } while (0)
535
536 # define SLAP_EVENT_IS_READ(fd)         (slap_daemon.sd_rflags[fd] & SD_READ)
537 # define SLAP_EVENT_IS_WRITE(fd)        (slap_daemon.sd_rflags[fd] & SD_WRITE)
538
539 # define SLAP_EVENT_CLR_READ(fd)        slap_daemon.sd_rflags[fd] &= ~SD_READ;
540 # define SLAP_EVENT_CLR_WRITE(fd)       slap_daemon.sd_rflags[fd] &= ~SD_WRITE;
541
542 # define SLAP_SOCK_INIT         do { \
543         ldap_pvt_thread_mutex_init( &slapd_ws_mutex ); \
544         slapd_ws_sockets = ch_malloc( dtblsize * ( sizeof(SOCKET) + 2)); \
545         slap_daemon.sd_flags = (char *)(slapd_ws_sockets + dtblsize); \
546         slap_daemon.sd_rflags = slap_daemon.sd_flags + dtblsize; \
547         memset( slapd_ws_sockets, -1, dtblsize * sizeof(SOCKET) ); \
548         slapd_ws_sockets[0] = wake_sds[0]; \
549         slapd_ws_sockets[1] = wake_sds[1]; \
550         wake_sds[0] = 0; \
551         wake_sds[1] = 1; \
552         slap_daemon.sd_nfds = 2; \
553         } while ( 0 )
554
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 ); \
560         } while ( 0 )
561
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])
566
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 )
569
570 # define SLAP_SELECT_ADDTEST(s) do { \
571         if ((s) >= slap_daemon.sd_nfds) slap_daemon.sd_nfds = (s)+1; \
572 } while (0)
573
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 )
576
577 # define SLAP_SOCK_ADD(s, l)    do { \
578         SLAP_SELECT_ADDTEST((s)); \
579         slap_daemon.sd_flags[s] = SD_ACTIVE|SD_READ; \
580 } while ( 0 )
581
582 # define SLAP_SOCK_DEL(s) do { \
583         slap_daemon.sd_flags[s] = 0; \
584         slapd_sockdel( s ); \
585 } while ( 0 )
586
587 # else /* !HAVE_WINSOCK */
588
589 /**************************************
590  * Use select system call - select(2) *
591  **************************************/
592 # define SLAP_EVENT_FNAME               "select"
593 /* select */
594 # define SLAP_EVENTS_ARE_INDEXED        1
595 # define SLAP_EVENT_DECL                fd_set readfds, writefds
596
597 # define SLAP_EVENT_INIT                do { \
598         AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) ); \
599         if ( nwriters ) { \
600                 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) ); \
601         } else { \
602                 FD_ZERO( &writefds ); \
603         } \
604 } while (0)
605
606 # ifdef FD_SETSIZE
607 #  define SLAP_SELECT_CHK_SETSIZE       do { \
608         if (dtblsize > FD_SETSIZE) dtblsize = FD_SETSIZE; \
609 } while (0)
610 # else /* ! FD_SETSIZE */
611 #  define SLAP_SELECT_CHK_SETSIZE       do { ; } while (0)
612 # endif /* ! FD_SETSIZE */
613
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); \
619 } while (0)
620
621 # define SLAP_SOCK_DESTROY
622
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)
626
627 # define SLAP_SOCK_NOT_ACTIVE(fd)       (!SLAP_SOCK_IS_ACTIVE(fd) && \
628          !SLAP_SOCK_IS_READ(fd) && !SLAP_SOCK_IS_WRITE(fd))
629
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)
632
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; \
636 } while (0)
637
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)
640
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); \
645 } while (0)
646
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); \
651 } while (0)
652
653 # define SLAP_EVENT_IS_READ(fd)         FD_ISSET((fd), &readfds)
654 # define SLAP_EVENT_IS_WRITE(fd)        FD_ISSET((fd), &writefds)
655
656 # define SLAP_EVENT_CLR_READ(fd)        FD_CLR((fd), &readfds)
657 # define SLAP_EVENT_CLR_WRITE(fd)       FD_CLR((fd), &writefds)
658
659 # define SLAP_EVENT_WAIT(tvp, nsp)      do { \
660         *(nsp) = select( SLAP_EVENT_MAX, &readfds, \
661                 nwriters > 0 ? &writefds : NULL, NULL, (tvp) ); \
662 } while (0)
663 # endif /* !HAVE_WINSOCK */
664 #endif /* ! epoll && ! /dev/poll */
665
666 #ifdef HAVE_SLP
667 /*
668  * SLP related functions
669  */
670 #include <slp.h>
671
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;
678
679 static SLPError slapd_slp_cookie;
680
681 static void
682 slapd_slp_init( const char* urls )
683 {
684         int i;
685         SLPError err;
686
687         slapd_srvurls = ldap_str2charray( urls, " " );
688
689         if ( slapd_srvurls == NULL ) return;
690
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                                 strlen( global_host ) +
696                                 sizeof( LDAP_SRVTYPE_PREFIX ) );
697                         strcpy( lutil_strcopy(slapd_srvurls[i],
698                                 LDAP_SRVTYPE_PREFIX ), global_host );
699                 } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0 ) {
700                         slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
701                                 strlen( global_host ) +
702                                 sizeof( LDAPS_SRVTYPE_PREFIX ) );
703                         strcpy( lutil_strcopy(slapd_srvurls[i],
704                                 LDAPS_SRVTYPE_PREFIX ), global_host );
705                 }
706         }
707
708         /* open the SLP handle */
709         err = SLPOpen( "en", 0, &slapd_hslp );
710
711         if ( err != SLP_OK ) {
712                 Debug( LDAP_DEBUG_CONNS, "daemon: SLPOpen() failed with %ld\n",
713                         (long)err, 0, 0 );
714         }
715 }
716
717 static void
718 slapd_slp_deinit( void )
719 {
720         if ( slapd_srvurls == NULL ) return;
721
722         ldap_charray_free( slapd_srvurls );
723         slapd_srvurls = NULL;
724
725         /* close the SLP handle */
726         SLPClose( slapd_hslp );
727 }
728
729 static void
730 slapd_slp_regreport(
731         SLPHandle       hslp,
732         SLPError        errcode,
733         void            *cookie )
734 {
735         /* return the error code in the cookie */
736         *(SLPError*)cookie = errcode; 
737 }
738
739 static void
740 slapd_slp_reg()
741 {
742         int i;
743         SLPError err;
744
745         if ( slapd_srvurls == NULL ) return;
746
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 )
752                 {
753                         err = SLPReg( slapd_hslp,
754                                 slapd_srvurls[i],
755                                 SLP_LIFETIME_MAXIMUM,
756                                 "ldap",
757                                 (slapd_slp_attrs) ? slapd_slp_attrs : "",
758                                 SLP_TRUE,
759                                 slapd_slp_regreport,
760                                 &slapd_slp_cookie );
761
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 );
766                         }       
767                 }
768         }
769 }
770
771 static void
772 slapd_slp_dereg( void )
773 {
774         int i;
775         SLPError err;
776
777         if ( slapd_srvurls == NULL ) return;
778
779         for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
780                 err = SLPDereg( slapd_hslp,
781                         slapd_srvurls[i],
782                         slapd_slp_regreport,
783                         &slapd_slp_cookie );
784                 
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 );
789                 }
790         }
791 }
792 #endif /* HAVE_SLP */
793
794 #ifdef HAVE_WINSOCK
795 /* Manage the descriptor to socket table */
796 ber_socket_t
797 slapd_socknew( ber_socket_t s )
798 {
799         ber_socket_t i;
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 );
804         } else {
805                 slapd_ws_sockets[i] = s;
806         }
807         ldap_pvt_thread_mutex_unlock( &slapd_ws_mutex );
808         return i;
809 }
810
811 void
812 slapd_sockdel( ber_socket_t s )
813 {
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 );
817 }
818
819 ber_socket_t
820 slapd_sock2fd( ber_socket_t s )
821 {
822         ber_socket_t i;
823         for ( i=0; i<dtblsize && slapd_ws_sockets[i] != s; i++);
824         if ( i == dtblsize )
825                 i = -1;
826         return i;
827 }
828 #endif
829
830 /*
831  * Add a descriptor to daemon control
832  *
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.
838  */
839 static void
840 slapd_add( ber_socket_t s, int isactive, Listener *sl )
841 {
842         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
843
844         assert( SLAP_SOCK_NOT_ACTIVE(s) );
845
846         if ( isactive ) slap_daemon.sd_nactives++;
847
848         SLAP_SOCK_ADD(s, sl);
849
850         Debug( LDAP_DEBUG_CONNS, "daemon: added %ldr%s listener=%p\n",
851                 (long) s, isactive ? " (active)" : "", (void *)sl );
852
853         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
854
855         WAKE_LISTENER(1);
856 }
857
858 /*
859  * NOTE: unused
860  */
861 void
862 slapd_sd_lock( void )
863 {
864         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
865 }
866
867 /*
868  * NOTE: unused
869  */
870 void
871 slapd_sd_unlock( void )
872 {
873         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
874 }
875
876 /*
877  * Remove the descriptor from daemon control
878  */
879 void
880 slapd_remove(
881         ber_socket_t s,
882         Sockbuf *sb,
883         int wasactive,
884         int wake,
885         int locked )
886 {
887         int waswriter;
888         int wasreader;
889
890         if ( !locked )
891                 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
892
893         assert( SLAP_SOCK_IS_ACTIVE( s ));
894
895         if ( wasactive ) slap_daemon.sd_nactives--;
896
897         waswriter = SLAP_SOCK_IS_WRITE(s);
898         wasreader = SLAP_SOCK_IS_READ(s);
899
900         Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
901                 (long) s,
902                 wasreader ? "r" : "",
903                 waswriter ? "w" : "" );
904
905         if ( waswriter ) slap_daemon.sd_nwriters--;
906
907         SLAP_SOCK_DEL(s);
908
909         if ( sb )
910                 ber_sockbuf_free(sb);
911
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.
915          */
916         if ( emfile ) {
917                 int i;
918                 for ( i = 0; slap_listeners[i] != NULL; i++ ) {
919                         Listener *lr = slap_listeners[i];
920
921                         if ( lr->sl_sd == AC_SOCKET_INVALID ) continue;
922                         if ( lr->sl_sd == s ) continue;
923                         if ( lr->sl_mute ) {
924                                 lr->sl_mute = 0;
925                                 emfile--;
926                                 break;
927                         }
928                 }
929                 /* Walked the entire list without enabling anything; emfile
930                  * counter is stale. Reset it.
931                  */
932                 if ( slap_listeners[i] == NULL ) emfile = 0;
933         }
934         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
935         WAKE_LISTENER(wake || slapd_gentle_shutdown == 2);
936 }
937
938 void
939 slapd_ack_write( ber_socket_t s, int wake )
940 {
941 #ifdef SLAP_EVENT_ACK
942         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
943
944         if ( SLAP_SOCK_IS_WRITE( s )) {
945                 assert( SLAP_SOCK_IS_ACTIVE( s ));
946
947                 SLAP_SOCK_CLR_WRITE( s );
948                 slap_daemon.sd_nwriters--;
949         }
950
951         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
952 #endif
953         WAKE_LISTENER(wake);
954 }
955
956 void
957 slapd_clr_write( ber_socket_t s, int wake )
958 {
959         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
960
961         if ( SLAP_SOCK_IS_WRITE( s )) {
962                 assert( SLAP_SOCK_IS_ACTIVE( s ));
963
964                 SLAP_SOCK_CLR_WRITE( s );
965                 slap_daemon.sd_nwriters--;
966         }
967
968         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
969         WAKE_LISTENER(wake);
970 }
971
972 void
973 slapd_set_write( ber_socket_t s, int wake )
974 {
975         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
976
977         assert( SLAP_SOCK_IS_ACTIVE( s ));
978
979         if ( !SLAP_SOCK_IS_WRITE( s )) {
980                 SLAP_SOCK_SET_WRITE( s );
981                 slap_daemon.sd_nwriters++;
982         }
983
984         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
985         WAKE_LISTENER(wake);
986 }
987
988 int
989 slapd_ack_read( ber_socket_t s, int wake )
990 {
991 #ifdef SLAP_EVENT_ACK
992         int rc = 1;
993         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
994
995         if ( SLAP_SOCK_IS_ACTIVE( s )) {
996                 SLAP_SOCK_CLR_READ( s );
997                 rc = 0;
998         }
999         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1000         if ( !rc )
1001                 WAKE_LISTENER(wake);
1002         return rc;
1003 #else
1004         return 0;
1005 #endif
1006 }
1007
1008 int
1009 slapd_clr_read( ber_socket_t s, int wake )
1010 {
1011         int rc = 1;
1012         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1013
1014         if ( SLAP_SOCK_IS_ACTIVE( s )) {
1015                 SLAP_SOCK_CLR_READ( s );
1016                 rc = 0;
1017         }
1018         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1019         if ( !rc )
1020                 WAKE_LISTENER(wake);
1021         return rc;
1022 }
1023
1024 void
1025 slapd_set_read( ber_socket_t s, int wake )
1026 {
1027         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1028
1029         assert( SLAP_SOCK_IS_ACTIVE( s ));
1030         if (!SLAP_SOCK_IS_READ( s )) SLAP_SOCK_SET_READ( s );
1031
1032         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1033         WAKE_LISTENER(wake);
1034 }
1035
1036 static void
1037 slapd_close( ber_socket_t s )
1038 {
1039         Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
1040                 (long) s, 0, 0 );
1041         tcp_close( SLAP_FD2SOCK(s) );
1042 #ifdef HAVE_WINSOCK
1043         slapd_sockdel( s );
1044 #endif
1045 }
1046
1047 static void
1048 slap_free_listener_addresses( struct sockaddr **sal )
1049 {
1050         struct sockaddr **sap;
1051         if (sal == NULL) return;
1052         for (sap = sal; *sap != NULL; sap++) ch_free(*sap);
1053         ch_free(sal);
1054 }
1055
1056 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
1057 static int
1058 get_url_perms(
1059         char    **exts,
1060         mode_t  *perms,
1061         int     *crit )
1062 {
1063         int     i;
1064
1065         assert( exts != NULL );
1066         assert( perms != NULL );
1067         assert( crit != NULL );
1068
1069         *crit = 0;
1070         for ( i = 0; exts[ i ]; i++ ) {
1071                 char    *type = exts[ i ];
1072                 int     c = 0;
1073
1074                 if ( type[ 0 ] == '!' ) {
1075                         c = 1;
1076                         type++;
1077                 }
1078
1079                 if ( strncasecmp( type, LDAPI_MOD_URLEXT "=",
1080                         sizeof(LDAPI_MOD_URLEXT "=") - 1 ) == 0 )
1081                 {
1082                         char *value = type + ( sizeof(LDAPI_MOD_URLEXT "=") - 1 );
1083                         mode_t p = 0;
1084                         int j;
1085
1086                         switch (strlen(value)) {
1087                         case 4:
1088                                 /* skip leading '0' */
1089                                 if ( value[ 0 ] != '0' ) return LDAP_OTHER;
1090                                 value++;
1091
1092                         case 3:
1093                                 for ( j = 0; j < 3; j++) {
1094                                         int     v;
1095
1096                                         v = value[ j ] - '0';
1097
1098                                         if ( v < 0 || v > 7 ) return LDAP_OTHER;
1099
1100                                         p |= v << 3*(2-j);
1101                                 }
1102                                 break;
1103
1104                         case 10:
1105                                 for ( j = 1; j < 10; j++ ) {
1106                                         static mode_t   m[] = { 0, 
1107                                                 S_IRUSR, S_IWUSR, S_IXUSR,
1108                                                 S_IRGRP, S_IWGRP, S_IXGRP,
1109                                                 S_IROTH, S_IWOTH, S_IXOTH
1110                                         };
1111                                         static const char       c[] = "-rwxrwxrwx"; 
1112
1113                                         if ( value[ j ] == c[ j ] ) {
1114                                                 p |= m[ j ];
1115         
1116                                         } else if ( value[ j ] != '-' ) {
1117                                                 return LDAP_OTHER;
1118                                         }
1119                                 }
1120                                 break;
1121
1122                         default:
1123                                 return LDAP_OTHER;
1124                         } 
1125
1126                         *crit = c;
1127                         *perms = p;
1128
1129                         return LDAP_SUCCESS;
1130                 }
1131         }
1132
1133         return LDAP_OTHER;
1134 }
1135 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
1136
1137 /* port = 0 indicates AF_LOCAL */
1138 static int
1139 slap_get_listener_addresses(
1140         const char *host,
1141         unsigned short port,
1142         struct sockaddr ***sal )
1143 {
1144         struct sockaddr **sap;
1145
1146 #ifdef LDAP_PF_LOCAL
1147         if ( port == 0 ) {
1148                 *sal = ch_malloc(2 * sizeof(void *));
1149                 if (*sal == NULL) return -1;
1150
1151                 sap = *sal;
1152                 *sap = ch_malloc(sizeof(struct sockaddr_un));
1153                 if (*sap == NULL) goto errexit;
1154                 sap[1] = NULL;
1155
1156                 if ( strlen(host) >
1157                         (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) )
1158                 {
1159                         Debug( LDAP_DEBUG_ANY,
1160                                 "daemon: domain socket path (%s) too long in URL",
1161                                 host, 0, 0);
1162                         goto errexit;
1163                 }
1164
1165                 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
1166                 (*sap)->sa_family = AF_LOCAL;
1167                 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
1168         } else
1169 #endif /* LDAP_PF_LOCAL */
1170         {
1171 #ifdef HAVE_GETADDRINFO
1172                 struct addrinfo hints, *res, *sai;
1173                 int n, err;
1174                 char serv[7];
1175
1176                 memset( &hints, '\0', sizeof(hints) );
1177                 hints.ai_flags = AI_PASSIVE;
1178                 hints.ai_socktype = SOCK_STREAM;
1179                 hints.ai_family = slap_inet4or6;
1180                 snprintf(serv, sizeof serv, "%d", port);
1181
1182                 if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
1183                         Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo() failed: %s\n",
1184                                 AC_GAI_STRERROR(err), 0, 0);
1185                         return -1;
1186                 }
1187
1188                 sai = res;
1189                 for (n=2; (sai = sai->ai_next) != NULL; n++) {
1190                         /* EMPTY */ ;
1191                 }
1192                 *sal = ch_calloc(n, sizeof(void *));
1193                 if (*sal == NULL) return -1;
1194
1195                 sap = *sal;
1196                 *sap = NULL;
1197
1198                 for ( sai=res; sai; sai=sai->ai_next ) {
1199                         if( sai->ai_addr == NULL ) {
1200                                 Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
1201                                         "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
1202                                 freeaddrinfo(res);
1203                                 goto errexit;
1204                         }
1205
1206                         switch (sai->ai_family) {
1207 #  ifdef LDAP_PF_INET6
1208                         case AF_INET6:
1209                                 *sap = ch_malloc(sizeof(struct sockaddr_in6));
1210                                 if (*sap == NULL) {
1211                                         freeaddrinfo(res);
1212                                         goto errexit;
1213                                 }
1214                                 *(struct sockaddr_in6 *)*sap =
1215                                         *((struct sockaddr_in6 *)sai->ai_addr);
1216                                 break;
1217 #  endif /* LDAP_PF_INET6 */
1218                         case AF_INET:
1219                                 *sap = ch_malloc(sizeof(struct sockaddr_in));
1220                                 if (*sap == NULL) {
1221                                         freeaddrinfo(res);
1222                                         goto errexit;
1223                                 }
1224                                 *(struct sockaddr_in *)*sap =
1225                                         *((struct sockaddr_in *)sai->ai_addr);
1226                                 break;
1227                         default:
1228                                 *sap = NULL;
1229                                 break;
1230                         }
1231
1232                         if (*sap != NULL) {
1233                                 (*sap)->sa_family = sai->ai_family;
1234                                 sap++;
1235                                 *sap = NULL;
1236                         }
1237                 }
1238
1239                 freeaddrinfo(res);
1240
1241 #else /* ! HAVE_GETADDRINFO */
1242                 int i, n = 1;
1243                 struct in_addr in;
1244                 struct hostent *he = NULL;
1245
1246                 if ( host == NULL ) {
1247                         in.s_addr = htonl(INADDR_ANY);
1248
1249                 } else if ( !inet_aton( host, &in ) ) {
1250                         he = gethostbyname( host );
1251                         if( he == NULL ) {
1252                                 Debug( LDAP_DEBUG_ANY,
1253                                         "daemon: invalid host %s", host, 0, 0);
1254                                 return -1;
1255                         }
1256                         for (n = 0; he->h_addr_list[n]; n++) /* empty */;
1257                 }
1258
1259                 *sal = ch_malloc((n+1) * sizeof(void *));
1260                 if (*sal == NULL) return -1;
1261
1262                 sap = *sal;
1263                 for ( i = 0; i<n; i++ ) {
1264                         sap[i] = ch_malloc(sizeof(struct sockaddr_in));
1265                         if (*sap == NULL) goto errexit;
1266
1267                         (void)memset( (void *)sap[i], '\0', sizeof(struct sockaddr_in) );
1268                         sap[i]->sa_family = AF_INET;
1269                         ((struct sockaddr_in *)sap[i])->sin_port = htons(port);
1270                         AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr,
1271                                 he ? (struct in_addr *)he->h_addr_list[i] : &in,
1272                                 sizeof(struct in_addr) );
1273                 }
1274                 sap[i] = NULL;
1275 #endif /* ! HAVE_GETADDRINFO */
1276         }
1277
1278         return 0;
1279
1280 errexit:
1281         slap_free_listener_addresses(*sal);
1282         return -1;
1283 }
1284
1285 static int
1286 slap_open_listener(
1287         const char* url,
1288         int *listeners,
1289         int *cur )
1290 {
1291         int     num, tmp, rc;
1292         Listener l;
1293         Listener *li;
1294         LDAPURLDesc *lud;
1295         unsigned short port;
1296         int err, addrlen = 0;
1297         struct sockaddr **sal, **psal;
1298         int socktype = SOCK_STREAM;     /* default to COTS */
1299         ber_socket_t s;
1300
1301 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
1302         /*
1303          * use safe defaults
1304          */
1305         int     crit = 1;
1306 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
1307
1308         rc = ldap_url_parse( url, &lud );
1309
1310         if( rc != LDAP_URL_SUCCESS ) {
1311                 Debug( LDAP_DEBUG_ANY,
1312                         "daemon: listen URL \"%s\" parse error=%d\n",
1313                         url, rc, 0 );
1314                 return rc;
1315         }
1316
1317         l.sl_url.bv_val = NULL;
1318         l.sl_mute = 0;
1319         l.sl_busy = 0;
1320
1321 #ifndef HAVE_TLS
1322         if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
1323                 Debug( LDAP_DEBUG_ANY, "daemon: TLS not supported (%s)\n",
1324                         url, 0, 0 );
1325                 ldap_free_urldesc( lud );
1326                 return -1;
1327         }
1328
1329         if(! lud->lud_port ) lud->lud_port = LDAP_PORT;
1330
1331 #else /* HAVE_TLS */
1332         l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
1333
1334         if(! lud->lud_port ) {
1335                 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
1336         }
1337 #endif /* HAVE_TLS */
1338
1339         port = (unsigned short) lud->lud_port;
1340
1341         tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme);
1342         if ( tmp == LDAP_PROTO_IPC ) {
1343 #ifdef LDAP_PF_LOCAL
1344                 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
1345                         err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
1346                 } else {
1347                         err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
1348                 }
1349 #else /* ! LDAP_PF_LOCAL */
1350
1351                 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
1352                         url, 0, 0);
1353                 ldap_free_urldesc( lud );
1354                 return -1;
1355 #endif /* ! LDAP_PF_LOCAL */
1356         } else {
1357                 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
1358                         || strcmp(lud->lud_host, "*") == 0 )
1359                 {
1360                         err = slap_get_listener_addresses(NULL, port, &sal);
1361                 } else {
1362                         err = slap_get_listener_addresses(lud->lud_host, port, &sal);
1363                 }
1364         }
1365
1366 #ifdef LDAP_CONNECTIONLESS
1367         l.sl_is_udp = ( tmp == LDAP_PROTO_UDP );
1368 #endif /* LDAP_CONNECTIONLESS */
1369
1370 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
1371         if ( lud->lud_exts ) {
1372                 err = get_url_perms( lud->lud_exts, &l.sl_perms, &crit );
1373         } else {
1374                 l.sl_perms = S_IRWXU | S_IRWXO;
1375         }
1376 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
1377
1378         ldap_free_urldesc( lud );
1379         if ( err ) return -1;
1380
1381         /* If we got more than one address returned, we need to make space
1382          * for it in the slap_listeners array.
1383          */
1384         for ( num=0; sal[num]; num++ ) /* empty */;
1385         if ( num > 1 ) {
1386                 *listeners += num-1;
1387                 slap_listeners = ch_realloc( slap_listeners,
1388                         (*listeners + 1) * sizeof(Listener *) );
1389         }
1390
1391         psal = sal;
1392         while ( *sal != NULL ) {
1393                 char *af;
1394                 switch( (*sal)->sa_family ) {
1395                 case AF_INET:
1396                         af = "IPv4";
1397                         break;
1398 #ifdef LDAP_PF_INET6
1399                 case AF_INET6:
1400                         af = "IPv6";
1401                         break;
1402 #endif /* LDAP_PF_INET6 */
1403 #ifdef LDAP_PF_LOCAL
1404                 case AF_LOCAL:
1405                         af = "Local";
1406                         break;
1407 #endif /* LDAP_PF_LOCAL */
1408                 default:
1409                         sal++;
1410                         continue;
1411                 }
1412
1413 #ifdef LDAP_CONNECTIONLESS
1414                 if( l.sl_is_udp ) socktype = SOCK_DGRAM;
1415 #endif /* LDAP_CONNECTIONLESS */
1416
1417                 s = socket( (*sal)->sa_family, socktype, 0);
1418                 if ( s == AC_SOCKET_INVALID ) {
1419                         int err = sock_errno();
1420                         Debug( LDAP_DEBUG_ANY,
1421                                 "daemon: %s socket() failed errno=%d (%s)\n",
1422                                 af, err, sock_errstr(err) );
1423                         sal++;
1424                         continue;
1425                 }
1426                 l.sl_sd = SLAP_SOCKNEW( s );
1427
1428                 if ( l.sl_sd >= dtblsize ) {
1429                         Debug( LDAP_DEBUG_ANY,
1430                                 "daemon: listener descriptor %ld is too great %ld\n",
1431                                 (long) l.sl_sd, (long) dtblsize, 0 );
1432                         tcp_close( s );
1433                         sal++;
1434                         continue;
1435                 }
1436
1437 #ifdef LDAP_PF_LOCAL
1438                 if ( (*sal)->sa_family == AF_LOCAL ) {
1439                         unlink( ((struct sockaddr_un *)*sal)->sun_path );
1440                 } else
1441 #endif /* LDAP_PF_LOCAL */
1442                 {
1443 #ifdef SO_REUSEADDR
1444                         /* enable address reuse */
1445                         tmp = 1;
1446                         rc = setsockopt( s, SOL_SOCKET, SO_REUSEADDR,
1447                                 (char *) &tmp, sizeof(tmp) );
1448                         if ( rc == AC_SOCKET_ERROR ) {
1449                                 int err = sock_errno();
1450                                 Debug( LDAP_DEBUG_ANY, "slapd(%ld): "
1451                                         "setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
1452                                         (long) l.sl_sd, err, sock_errstr(err) );
1453                         }
1454 #endif /* SO_REUSEADDR */
1455                 }
1456
1457                 switch( (*sal)->sa_family ) {
1458                 case AF_INET:
1459                         addrlen = sizeof(struct sockaddr_in);
1460                         break;
1461 #ifdef LDAP_PF_INET6
1462                 case AF_INET6:
1463 #ifdef IPV6_V6ONLY
1464                         /* Try to use IPv6 sockets for IPv6 only */
1465                         tmp = 1;
1466                         rc = setsockopt( s , IPPROTO_IPV6, IPV6_V6ONLY,
1467                                 (char *) &tmp, sizeof(tmp) );
1468                         if ( rc == AC_SOCKET_ERROR ) {
1469                                 int err = sock_errno();
1470                                 Debug( LDAP_DEBUG_ANY, "slapd(%ld): "
1471                                         "setsockopt(IPV6_V6ONLY) failed errno=%d (%s)\n",
1472                                         (long) l.sl_sd, err, sock_errstr(err) );
1473                         }
1474 #endif /* IPV6_V6ONLY */
1475                         addrlen = sizeof(struct sockaddr_in6);
1476                         break;
1477 #endif /* LDAP_PF_INET6 */
1478
1479 #ifdef LDAP_PF_LOCAL
1480                 case AF_LOCAL:
1481 #ifdef LOCAL_CREDS
1482                         {
1483                                 int one = 1;
1484                                 setsockopt( s, 0, LOCAL_CREDS, &one, sizeof( one ) );
1485                         }
1486 #endif /* LOCAL_CREDS */
1487
1488                         addrlen = sizeof( struct sockaddr_un );
1489                         break;
1490 #endif /* LDAP_PF_LOCAL */
1491                 }
1492
1493 #ifdef LDAP_PF_LOCAL
1494                 /* create socket with all permissions set for those systems
1495                  * that honor permissions on sockets (e.g. Linux); typically,
1496                  * only write is required.  To exploit filesystem permissions,
1497                  * place the socket in a directory and use directory's
1498                  * permissions.  Need write perms to the directory to 
1499                  * create/unlink the socket; likely need exec perms to access
1500                  * the socket (ITS#4709) */
1501                 {
1502                         mode_t old_umask;
1503
1504                         if ( (*sal)->sa_family == AF_LOCAL ) {
1505                                 old_umask = umask( 0 );
1506                         }
1507 #endif /* LDAP_PF_LOCAL */
1508                         rc = bind( s, *sal, addrlen );
1509 #ifdef LDAP_PF_LOCAL
1510                         if ( (*sal)->sa_family == AF_LOCAL ) {
1511                                 umask( old_umask );
1512                         }
1513                 }
1514 #endif /* LDAP_PF_LOCAL */
1515                 if ( rc ) {
1516                         err = sock_errno();
1517                         Debug( LDAP_DEBUG_ANY,
1518                                 "daemon: bind(%ld) failed errno=%d (%s)\n",
1519                                 (long)l.sl_sd, err, sock_errstr( err ) );
1520                         tcp_close( s );
1521                         sal++;
1522                         continue;
1523                 }
1524
1525                 switch ( (*sal)->sa_family ) {
1526 #ifdef LDAP_PF_LOCAL
1527                 case AF_LOCAL: {
1528                         char *addr = ((struct sockaddr_un *)*sal)->sun_path;
1529                         l.sl_name.bv_len = strlen(addr) + sizeof("PATH=") - 1;
1530                         l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len + 1 );
1531                         snprintf( l.sl_name.bv_val, l.sl_name.bv_len + 1, 
1532                                 "PATH=%s", addr );
1533                 } break;
1534 #endif /* LDAP_PF_LOCAL */
1535
1536                 case AF_INET: {
1537                         char *s;
1538 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
1539                         char addr[INET_ADDRSTRLEN];
1540                         inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
1541                                 addr, sizeof(addr) );
1542                         s = addr;
1543 #else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1544                         s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr );
1545 #endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
1546                         port = ntohs( ((struct sockaddr_in *)*sal) ->sin_port );
1547                         l.sl_name.bv_val =
1548                                 ber_memalloc( sizeof("IP=255.255.255.255:65535") );
1549                         snprintf( l.sl_name.bv_val, sizeof("IP=255.255.255.255:65535"),
1550                                 "IP=%s:%d",
1551                                  s != NULL ? s : SLAP_STRING_UNKNOWN, port );
1552                         l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1553                 } break;
1554
1555 #ifdef LDAP_PF_INET6
1556                 case AF_INET6: {
1557                         char addr[INET6_ADDRSTRLEN];
1558                         inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
1559                                 addr, sizeof addr);
1560                         port = ntohs( ((struct sockaddr_in6 *)*sal)->sin6_port );
1561                         l.sl_name.bv_len = strlen(addr) + sizeof("IP=[]:65535");
1562                         l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len );
1563                         snprintf( l.sl_name.bv_val, l.sl_name.bv_len, "IP=[%s]:%d", 
1564                                 addr, port );
1565                         l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1566                 } break;
1567 #endif /* LDAP_PF_INET6 */
1568
1569                 default:
1570                         Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
1571                                 (int) (*sal)->sa_family, 0, 0 );
1572                         break;
1573                 }
1574
1575                 AC_MEMCPY(&l.sl_sa, *sal, addrlen);
1576                 ber_str2bv( url, 0, 1, &l.sl_url);
1577                 li = ch_malloc( sizeof( Listener ) );
1578                 *li = l;
1579                 slap_listeners[*cur] = li;
1580                 (*cur)++;
1581                 sal++;
1582         }
1583
1584         slap_free_listener_addresses(psal);
1585
1586         if ( l.sl_url.bv_val == NULL ) {
1587                 Debug( LDAP_DEBUG_TRACE,
1588                         "slap_open_listener: failed on %s\n", url, 0, 0 );
1589                 return -1;
1590         }
1591
1592         Debug( LDAP_DEBUG_TRACE, "daemon: listener initialized %s\n",
1593                 l.sl_url.bv_val, 0, 0 );
1594         return 0;
1595 }
1596
1597 static int sockinit(void);
1598 static int sockdestroy(void);
1599
1600 int
1601 slapd_daemon_init( const char *urls )
1602 {
1603         int i, j, n, rc;
1604         char **u;
1605
1606         Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
1607                 urls ? urls : "<null>", 0, 0 );
1608
1609         ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
1610 #ifdef HAVE_TCPD
1611         ldap_pvt_thread_mutex_init( &slap_daemon.sd_tcpd_mutex );
1612 #endif /* TCP Wrappers */
1613
1614         if( (rc = sockinit()) != 0 ) return rc;
1615
1616 #ifdef HAVE_SYSCONF
1617         dtblsize = sysconf( _SC_OPEN_MAX );
1618 #elif defined(HAVE_GETDTABLESIZE)
1619         dtblsize = getdtablesize();
1620 #else /* ! HAVE_SYSCONF && ! HAVE_GETDTABLESIZE */
1621         dtblsize = FD_SETSIZE;
1622 #endif /* ! HAVE_SYSCONF && ! HAVE_GETDTABLESIZE */
1623
1624         /* open a pipe (or something equivalent connected to itself).
1625          * we write a byte on this fd whenever we catch a signal. The main
1626          * loop will be select'ing on this socket, and will wake up when
1627          * this byte arrives.
1628          */
1629         if( (rc = lutil_pair( wake_sds )) < 0 ) {
1630                 Debug( LDAP_DEBUG_ANY,
1631                         "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
1632                 return rc;
1633         }
1634
1635         SLAP_SOCK_INIT;
1636
1637         if( urls == NULL ) urls = "ldap:///";
1638
1639         u = ldap_str2charray( urls, " " );
1640
1641         if( u == NULL || u[0] == NULL ) {
1642                 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
1643                         urls, 0, 0 );
1644                 if ( u )
1645                         ldap_charray_free( u );
1646                 return -1;
1647         }
1648
1649         for( i=0; u[i] != NULL; i++ ) {
1650                 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
1651                         u[i], 0, 0 );
1652         }
1653
1654         if( i == 0 ) {
1655                 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
1656                         urls, 0, 0 );
1657                 ldap_charray_free( u );
1658                 return -1;
1659         }
1660
1661         Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
1662                 i, 0, 0 );
1663         slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
1664
1665         for(n = 0, j = 0; u[n]; n++ ) {
1666                 if ( slap_open_listener( u[n], &i, &j ) ) {
1667                         ldap_charray_free( u );
1668                         return -1;
1669                 }
1670         }
1671         slap_listeners[j] = NULL;
1672
1673         Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
1674                 i, 0, 0 );
1675
1676
1677 #ifdef HAVE_SLP
1678         if( slapd_register_slp ) {
1679                 slapd_slp_init( urls );
1680                 slapd_slp_reg();
1681         }
1682 #endif /* HAVE_SLP */
1683
1684         ldap_charray_free( u );
1685
1686         return !i;
1687 }
1688
1689
1690 int
1691 slapd_daemon_destroy( void )
1692 {
1693         connections_destroy();
1694 #ifdef HAVE_WINSOCK
1695         if ( wake_sds[1] != INVALID_SOCKET && wake_sds[1] != wake_sds[0] )
1696 #endif /* HAVE_WINSOCK */
1697                 tcp_close( SLAP_FD2SOCK(wake_sds[1]) );
1698 #ifdef HAVE_WINSOCK
1699         if ( wake_sds[0] != INVALID_SOCKET )
1700 #endif /* HAVE_WINSOCK */
1701                 tcp_close( SLAP_FD2SOCK(wake_sds[0]) );
1702         sockdestroy();
1703
1704 #ifdef HAVE_SLP
1705         if( slapd_register_slp ) {
1706                 slapd_slp_dereg();
1707                 slapd_slp_deinit();
1708         }
1709 #endif /* HAVE_SLP */
1710
1711 #ifdef HAVE_TCPD
1712         ldap_pvt_thread_mutex_destroy( &slap_daemon.sd_tcpd_mutex );
1713 #endif /* TCP Wrappers */
1714
1715         ldap_pvt_thread_mutex_destroy( &slap_daemon.sd_mutex );
1716         return 0;
1717 }
1718
1719
1720 static void
1721 close_listeners(
1722         int remove )
1723 {
1724         int l;
1725
1726         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1727                 Listener *lr = slap_listeners[l];
1728
1729                 if ( lr->sl_sd != AC_SOCKET_INVALID ) {
1730                         if ( remove ) slapd_remove( lr->sl_sd, NULL, 0, 0, 0 );
1731
1732 #ifdef LDAP_PF_LOCAL
1733                         if ( lr->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1734                                 unlink( lr->sl_sa.sa_un_addr.sun_path );
1735                         }
1736 #endif /* LDAP_PF_LOCAL */
1737
1738                         slapd_close( lr->sl_sd );
1739                 }
1740
1741                 if ( lr->sl_url.bv_val ) {
1742                         ber_memfree( lr->sl_url.bv_val );
1743                 }
1744
1745                 if ( lr->sl_name.bv_val ) {
1746                         ber_memfree( lr->sl_name.bv_val );
1747                 }
1748
1749                 free( lr );
1750                 slap_listeners[l] = NULL;
1751         }
1752 }
1753
1754 static int
1755 slap_listener(
1756         Listener *sl )
1757 {
1758         Sockaddr                from;
1759
1760         ber_socket_t s, sfd;
1761         ber_socklen_t len = sizeof(from);
1762         Connection *c;
1763         slap_ssf_t ssf = 0;
1764         struct berval authid = BER_BVNULL;
1765 #ifdef SLAPD_RLOOKUPS
1766         char hbuf[NI_MAXHOST];
1767 #endif /* SLAPD_RLOOKUPS */
1768
1769         char    *dnsname = NULL;
1770         char    *peeraddr = NULL;
1771 #ifdef LDAP_PF_LOCAL
1772         char peername[MAXPATHLEN + sizeof("PATH=")];
1773 #ifdef LDAP_PF_LOCAL_SENDMSG
1774         char peerbuf[8];
1775         struct berval peerbv = BER_BVNULL;
1776 #endif
1777 #elif defined(LDAP_PF_INET6)
1778         char peername[sizeof("IP=[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535")];
1779 #else /* ! LDAP_PF_LOCAL && ! LDAP_PF_INET6 */
1780         char peername[sizeof("IP=255.255.255.255:65336")];
1781 #endif /* LDAP_PF_LOCAL */
1782         int cflag;
1783
1784         Debug( LDAP_DEBUG_TRACE,
1785                 ">>> slap_listener(%s)\n",
1786                 sl->sl_url.bv_val, 0, 0 );
1787
1788         peername[0] = '\0';
1789
1790 #ifdef LDAP_CONNECTIONLESS
1791         if ( sl->sl_is_udp ) return 1;
1792 #endif /* LDAP_CONNECTIONLESS */
1793
1794 #  ifdef LDAP_PF_LOCAL
1795         /* FIXME: apparently accept doesn't fill
1796          * the sun_path sun_path member */
1797         from.sa_un_addr.sun_path[0] = '\0';
1798 #  endif /* LDAP_PF_LOCAL */
1799
1800         s = accept( SLAP_FD2SOCK( sl->sl_sd ), (struct sockaddr *) &from, &len );
1801
1802         /* Resume the listener FD to allow concurrent-processing of
1803          * additional incoming connections.
1804          */
1805         sl->sl_busy = 0;
1806         WAKE_LISTENER(1);
1807
1808         if ( s == AC_SOCKET_INVALID ) {
1809                 int err = sock_errno();
1810
1811                 if(
1812 #ifdef EMFILE
1813                     err == EMFILE ||
1814 #endif /* EMFILE */
1815 #ifdef ENFILE
1816                     err == ENFILE ||
1817 #endif /* ENFILE */
1818                     0 )
1819                 {
1820                         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1821                         emfile++;
1822                         /* Stop listening until an existing session closes */
1823                         sl->sl_mute = 1;
1824                         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1825                 }
1826
1827                 Debug( LDAP_DEBUG_ANY,
1828                         "daemon: accept(%ld) failed errno=%d (%s)\n",
1829                         (long) sl->sl_sd, err, sock_errstr(err) );
1830                 ldap_pvt_thread_yield();
1831                 return 0;
1832         }
1833         sfd = SLAP_SOCKNEW( s );
1834
1835         /* make sure descriptor number isn't too great */
1836         if ( sfd >= dtblsize ) {
1837                 Debug( LDAP_DEBUG_ANY,
1838                         "daemon: %ld beyond descriptor table size %ld\n",
1839                         (long) sfd, (long) dtblsize, 0 );
1840
1841                 tcp_close(s);
1842                 ldap_pvt_thread_yield();
1843                 return 0;
1844         }
1845
1846 #ifdef LDAP_DEBUG
1847         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1848         /* newly accepted stream should not be in any of the FD SETS */
1849         assert( SLAP_SOCK_NOT_ACTIVE( sfd ));
1850         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1851 #endif /* LDAP_DEBUG */
1852
1853 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1854 #ifdef LDAP_PF_LOCAL
1855         /* for IPv4 and IPv6 sockets only */
1856         if ( from.sa_addr.sa_family != AF_LOCAL )
1857 #endif /* LDAP_PF_LOCAL */
1858         {
1859                 int rc;
1860                 int tmp;
1861 #ifdef SO_KEEPALIVE
1862                 /* enable keep alives */
1863                 tmp = 1;
1864                 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1865                         (char *) &tmp, sizeof(tmp) );
1866                 if ( rc == AC_SOCKET_ERROR ) {
1867                         int err = sock_errno();
1868                         Debug( LDAP_DEBUG_ANY,
1869                                 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1870                                 "errno=%d (%s)\n", (long) sfd, err, sock_errstr(err) );
1871                 }
1872 #endif /* SO_KEEPALIVE */
1873 #ifdef TCP_NODELAY
1874                 /* enable no delay */
1875                 tmp = 1;
1876                 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1877                         (char *)&tmp, sizeof(tmp) );
1878                 if ( rc == AC_SOCKET_ERROR ) {
1879                         int err = sock_errno();
1880                         Debug( LDAP_DEBUG_ANY,
1881                                 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1882                                 "errno=%d (%s)\n", (long) sfd, err, sock_errstr(err) );
1883                 }
1884 #endif /* TCP_NODELAY */
1885         }
1886 #endif /* SO_KEEPALIVE || TCP_NODELAY */
1887
1888         Debug( LDAP_DEBUG_CONNS,
1889                 "daemon: listen=%ld, new connection on %ld\n",
1890                 (long) sl->sl_sd, (long) sfd, 0 );
1891
1892         cflag = 0;
1893         switch ( from.sa_addr.sa_family ) {
1894 #  ifdef LDAP_PF_LOCAL
1895         case AF_LOCAL:
1896                 cflag |= CONN_IS_IPC;
1897
1898                 /* FIXME: apparently accept doesn't fill
1899                  * the sun_path sun_path member */
1900                 if ( from.sa_un_addr.sun_path[0] == '\0' ) {
1901                         AC_MEMCPY( from.sa_un_addr.sun_path,
1902                                         sl->sl_sa.sa_un_addr.sun_path,
1903                                         sizeof( from.sa_un_addr.sun_path ) );
1904                 }
1905
1906                 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1907                 ssf = local_ssf;
1908                 {
1909                         uid_t uid;
1910                         gid_t gid;
1911
1912 #ifdef LDAP_PF_LOCAL_SENDMSG
1913                         peerbv.bv_val = peerbuf;
1914                         peerbv.bv_len = sizeof( peerbuf );
1915 #endif
1916                         if( LUTIL_GETPEEREID( s, &uid, &gid, &peerbv ) == 0 ) {
1917                                 authid.bv_val = ch_malloc(
1918                                         STRLENOF( "gidNumber=4294967295+uidNumber=4294967295,"
1919                                         "cn=peercred,cn=external,cn=auth" ) + 1 );
1920                                 authid.bv_len = sprintf( authid.bv_val,
1921                                         "gidNumber=%d+uidNumber=%d,"
1922                                         "cn=peercred,cn=external,cn=auth",
1923                                         (int) gid, (int) uid );
1924                                 assert( authid.bv_len <=
1925                                         STRLENOF( "gidNumber=4294967295+uidNumber=4294967295,"
1926                                         "cn=peercred,cn=external,cn=auth" ) );
1927                         }
1928                 }
1929                 dnsname = "local";
1930                 break;
1931 #endif /* LDAP_PF_LOCAL */
1932
1933 #  ifdef LDAP_PF_INET6
1934         case AF_INET6:
1935         if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1936                 peeraddr = inet_ntoa( *((struct in_addr *)
1937                                         &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1938                 sprintf( peername, "IP=%s:%d",
1939                          peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1940                          (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1941         } else {
1942                 char addr[INET6_ADDRSTRLEN];
1943
1944                 peeraddr = (char *) inet_ntop( AF_INET6,
1945                                       &from.sa_in6_addr.sin6_addr,
1946                                       addr, sizeof addr );
1947                 sprintf( peername, "IP=[%s]:%d",
1948                          peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1949                          (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1950         }
1951         break;
1952 #  endif /* LDAP_PF_INET6 */
1953
1954         case AF_INET:
1955                 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1956                 sprintf( peername, "IP=%s:%d",
1957                         peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1958                         (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1959                 break;
1960
1961         default:
1962                 slapd_close(sfd);
1963                 return 0;
1964         }
1965
1966         if ( ( from.sa_addr.sa_family == AF_INET )
1967 #ifdef LDAP_PF_INET6
1968                 || ( from.sa_addr.sa_family == AF_INET6 )
1969 #endif /* LDAP_PF_INET6 */
1970                 )
1971         {
1972                 dnsname = NULL;
1973 #ifdef SLAPD_RLOOKUPS
1974                 if ( use_reverse_lookup ) {
1975                         char *herr;
1976                         if (ldap_pvt_get_hname( (const struct sockaddr *)&from, len, hbuf,
1977                                 sizeof(hbuf), &herr ) == 0) {
1978                                 ldap_pvt_str2lower( hbuf );
1979                                 dnsname = hbuf;
1980                         }
1981                 }
1982 #endif /* SLAPD_RLOOKUPS */
1983
1984 #ifdef HAVE_TCPD
1985                 {
1986                         int rc;
1987                         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_tcpd_mutex );
1988                         rc = hosts_ctl("slapd",
1989                                 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1990                                 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1991                                 SLAP_STRING_UNKNOWN );
1992                         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_tcpd_mutex );
1993                         if ( !rc ) {
1994                                 /* DENY ACCESS */
1995                                 Statslog( LDAP_DEBUG_STATS,
1996                                         "fd=%ld DENIED from %s (%s)\n",
1997                                         (long) sfd,
1998                                         dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1999                                         peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
2000                                         0, 0 );
2001                                 slapd_close(sfd);
2002                                 return 0;
2003                         }
2004                 }
2005 #endif /* HAVE_TCPD */
2006         }
2007
2008 #ifdef HAVE_TLS
2009         if ( sl->sl_is_tls ) cflag |= CONN_IS_TLS;
2010 #endif
2011         c = connection_init(sfd, sl,
2012                 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
2013                 peername, cflag, ssf,
2014                 authid.bv_val ? &authid : NULL
2015                 LDAP_PF_LOCAL_SENDMSG_ARG(&peerbv));
2016
2017         if( authid.bv_val ) ch_free(authid.bv_val);
2018
2019         if( !c ) {
2020                 Debug( LDAP_DEBUG_ANY,
2021                         "daemon: connection_init(%ld, %s, %s) failed.\n",
2022                         (long) sfd, peername, sl->sl_name.bv_val );
2023                 slapd_close(sfd);
2024                 return 0;
2025         }
2026
2027         Statslog( LDAP_DEBUG_STATS,
2028                 "conn=%ld fd=%ld ACCEPT from %s (%s)\n",
2029                 c->c_connid, (long) sfd, peername, sl->sl_name.bv_val,
2030                 0 );
2031
2032         return 0;
2033 }
2034
2035 static void*
2036 slap_listener_thread(
2037         void* ctx,
2038         void* ptr )
2039 {
2040         int             rc;
2041         Listener        *sl = (Listener *)ptr;
2042
2043         rc = slap_listener( sl );
2044
2045         if( rc != LDAP_SUCCESS ) {
2046                 Debug( LDAP_DEBUG_ANY,
2047                         "slap_listener_thread(%s): failed err=%d",
2048                         sl->sl_url.bv_val, rc, 0 );
2049         }
2050
2051         return (void*)NULL;
2052 }
2053
2054 static int
2055 slap_listener_activate(
2056         Listener* sl )
2057 {
2058         int rc;
2059
2060         Debug( LDAP_DEBUG_TRACE, "slap_listener_activate(%d): %s\n",
2061                 sl->sl_sd, sl->sl_busy ? "busy" : "", 0 );
2062
2063         sl->sl_busy++;
2064
2065         rc = ldap_pvt_thread_pool_submit( &connection_pool,
2066                 slap_listener_thread, (void *) sl );
2067
2068         if( rc != 0 ) {
2069                 Debug( LDAP_DEBUG_ANY,
2070                         "listener_activate(%d): submit failed (%d)\n",
2071                         sl->sl_sd, rc, 0 );
2072         }
2073         return rc;
2074 }
2075
2076 static void *
2077 slapd_daemon_task(
2078         void *ptr )
2079 {
2080         int l;
2081         time_t last_idle_check = 0;
2082         struct timeval idle;
2083         int ebadf = 0;
2084
2085 #define SLAPD_IDLE_CHECK_LIMIT 4
2086
2087         if ( global_idletimeout > 0 ) {
2088                 last_idle_check = slap_get_time();
2089                 /* Set the select timeout.
2090                  * Don't just truncate, preserve the fractions of
2091                  * seconds to prevent sleeping for zero time.
2092                  */
2093                 idle.tv_sec = global_idletimeout / SLAPD_IDLE_CHECK_LIMIT;
2094                 idle.tv_usec = global_idletimeout - \
2095                         ( idle.tv_sec * SLAPD_IDLE_CHECK_LIMIT );
2096                 idle.tv_usec *= 1000000 / SLAPD_IDLE_CHECK_LIMIT;
2097         } else {
2098                 idle.tv_sec = 0;
2099                 idle.tv_usec = 0;
2100         }
2101
2102         slapd_add( wake_sds[0], 0, NULL );
2103
2104         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2105                 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ) continue;
2106
2107 #ifdef LDAP_CONNECTIONLESS
2108                 /* Since this is connectionless, the data port is the
2109                  * listening port. The listen() and accept() calls
2110                  * are unnecessary.
2111                  */
2112                 if ( slap_listeners[l]->sl_is_udp )
2113                         continue;
2114 #endif /* LDAP_CONNECTIONLESS */
2115
2116                 if ( listen( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ), SLAPD_LISTEN_BACKLOG ) == -1 ) {
2117                         int err = sock_errno();
2118
2119 #ifdef LDAP_PF_INET6
2120                         /* If error is EADDRINUSE, we are trying to listen to INADDR_ANY and
2121                          * we are already listening to in6addr_any, then we want to ignore
2122                          * this and continue.
2123                          */
2124                         if ( err == EADDRINUSE ) {
2125                                 int i;
2126                                 struct sockaddr_in sa = slap_listeners[l]->sl_sa.sa_in_addr;
2127                                 struct sockaddr_in6 sa6;
2128                                 
2129                                 if ( sa.sin_family == AF_INET &&
2130                                      sa.sin_addr.s_addr == htonl(INADDR_ANY) ) {
2131                                         for ( i = 0 ; i < l; i++ ) {
2132                                                 sa6 = slap_listeners[i]->sl_sa.sa_in6_addr;
2133                                                 if ( sa6.sin6_family == AF_INET6 &&
2134                                                      !memcmp( &sa6.sin6_addr, &in6addr_any,
2135                                                                 sizeof(struct in6_addr) ) )
2136                                                 {
2137                                                         break;
2138                                                 }
2139                                         }
2140
2141                                         if ( i < l ) {
2142                                                 /* We are already listening to in6addr_any */
2143                                                 Debug( LDAP_DEBUG_CONNS,
2144                                                         "daemon: Attempt to listen to 0.0.0.0 failed, "
2145                                                         "already listening on ::, assuming IPv4 included\n",
2146                                                         0, 0, 0 );
2147                                                 slapd_close( slap_listeners[l]->sl_sd );
2148                                                 slap_listeners[l]->sl_sd = AC_SOCKET_INVALID;
2149                                                 continue;
2150                                         }
2151                                 }
2152                         }
2153 #endif /* LDAP_PF_INET6 */
2154                         Debug( LDAP_DEBUG_ANY,
2155                                 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
2156                                         slap_listeners[l]->sl_url.bv_val, err,
2157                                         sock_errstr(err) );
2158                         return (void*)-1;
2159                 }
2160
2161                 /* make the listening socket non-blocking */
2162                 if ( ber_pvt_socket_set_nonblock( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ), 1 ) < 0 ) {
2163                         Debug( LDAP_DEBUG_ANY, "slapd_daemon_task: "
2164                                 "set nonblocking on a listening socket failed\n",
2165                                 0, 0, 0 );
2166                         slapd_shutdown = 2;
2167                         return (void*)-1;
2168                 }
2169
2170                 slapd_add( slap_listeners[l]->sl_sd, 0, slap_listeners[l] );
2171         }
2172
2173 #ifdef HAVE_NT_SERVICE_MANAGER
2174         if ( started_event != NULL ) {
2175                 ldap_pvt_thread_cond_signal( &started_event );
2176         }
2177 #endif /* HAVE_NT_SERVICE_MANAGER */
2178
2179         /* initialization complete. Here comes the loop. */
2180
2181         while ( !slapd_shutdown ) {
2182                 ber_socket_t            i;
2183                 int                     ns, nwriters;
2184                 int                     at;
2185                 ber_socket_t            nfds;
2186 #if SLAP_EVENTS_ARE_INDEXED
2187                 ber_socket_t            nrfds, nwfds;
2188 #endif /* SLAP_EVENTS_ARE_INDEXED */
2189 #define SLAPD_EBADF_LIMIT 16
2190
2191                 time_t                  now;
2192
2193                 SLAP_EVENT_DECL;
2194
2195                 struct timeval          tv;
2196                 struct timeval          *tvp;
2197
2198                 struct timeval          cat;
2199                 time_t                  tdelta = 1;
2200                 struct re_s*            rtask;
2201
2202                 now = slap_get_time();
2203
2204                 if ( ( global_idletimeout > 0 ) &&
2205                         difftime( last_idle_check +
2206                                 global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 )
2207                 {
2208                         connections_timeout_idle( now );
2209                         last_idle_check = now;
2210                 }
2211                 tv = idle;
2212
2213 #ifdef SIGHUP
2214                 if ( slapd_gentle_shutdown ) {
2215                         ber_socket_t active;
2216
2217                         if ( slapd_gentle_shutdown == 1 ) {
2218                                 BackendDB *be;
2219                                 Debug( LDAP_DEBUG_ANY, "slapd gentle shutdown\n", 0, 0, 0 );
2220                                 close_listeners( 1 );
2221                                 frontendDB->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
2222                                 LDAP_STAILQ_FOREACH(be, &backendDB, be_next) {
2223                                         be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
2224                                 }
2225                                 slapd_gentle_shutdown = 2;
2226                         }
2227
2228                         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
2229                         active = slap_daemon.sd_nactives;
2230                         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
2231                         if ( active == 0 ) {
2232                                 slapd_shutdown = 1;
2233                                 break;
2234                         }
2235                 }
2236 #endif /* SIGHUP */
2237                 at = 0;
2238
2239                 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
2240
2241                 nwriters = slap_daemon.sd_nwriters;
2242
2243                 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2244                         Listener *lr = slap_listeners[l];
2245
2246                         if ( lr->sl_sd == AC_SOCKET_INVALID ) continue;
2247
2248                         if ( lr->sl_mute || lr->sl_busy )
2249                         {
2250                                 SLAP_SOCK_CLR_READ( lr->sl_sd );
2251                         } else {
2252                                 SLAP_SOCK_SET_READ( lr->sl_sd );
2253                         }
2254                 }
2255
2256                 SLAP_EVENT_INIT;
2257
2258                 nfds = SLAP_EVENT_MAX;
2259
2260                 if ( global_idletimeout && slap_daemon.sd_nactives ) at = 1;
2261
2262                 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
2263
2264                 if ( at 
2265 #if defined(HAVE_YIELDING_SELECT) || defined(NO_THREADS)
2266                         &&  ( tv.tv_sec || tv.tv_usec )
2267 #endif /* HAVE_YIELDING_SELECT || NO_THREADS */
2268                         )
2269                 {
2270                         tvp = &tv;
2271                 } else {
2272                         tvp = NULL;
2273                 }
2274
2275                 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2276                 rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
2277                 while ( rtask && cat.tv_sec && cat.tv_sec <= now ) {
2278                         if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
2279                                 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
2280                         } else {
2281                                 ldap_pvt_runqueue_runtask( &slapd_rq, rtask );
2282                                 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
2283                                 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2284                                 ldap_pvt_thread_pool_submit( &connection_pool,
2285                                         rtask->routine, (void *) rtask );
2286                                 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2287                         }
2288                         rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
2289                 }
2290                 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2291
2292                 if ( rtask && cat.tv_sec ) {
2293                         /* NOTE: diff __should__ always be >= 0,
2294                          * AFAI understand; however (ITS#4872),
2295                          * time_t might be unsigned in some systems,
2296                          * while difftime() returns a double */
2297                         double diff = difftime( cat.tv_sec, now );
2298                         if ( diff <= 0 ) {
2299                                 diff = tdelta;
2300                         }
2301                         if ( tvp == NULL || diff < tv.tv_sec ) {
2302                                 tv.tv_sec = diff;
2303                                 tv.tv_usec = 0;
2304                                 tvp = &tv;
2305                         }
2306                 }
2307
2308                 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2309                         Listener *lr = slap_listeners[l];
2310
2311                         if ( lr->sl_sd == AC_SOCKET_INVALID ) {
2312                                 continue;
2313                         }
2314
2315                         if ( lr->sl_mute ) {
2316                                 Debug( LDAP_DEBUG_CONNS,
2317                                         "daemon: " SLAP_EVENT_FNAME ": "
2318                                         "listen=%d muted\n",
2319                                         lr->sl_sd, 0, 0 );
2320                                 continue;
2321                         }
2322
2323                         if ( lr->sl_busy ) {
2324                                 Debug( LDAP_DEBUG_CONNS,
2325                                         "daemon: " SLAP_EVENT_FNAME ": "
2326                                         "listen=%d busy\n",
2327                                         lr->sl_sd, 0, 0 );
2328                                 continue;
2329                         }
2330
2331                         Debug( LDAP_DEBUG_CONNS,
2332                                 "daemon: " SLAP_EVENT_FNAME ": "
2333                                 "listen=%d active_threads=%d tvp=%s\n",
2334                                 lr->sl_sd, at, tvp == NULL ? "NULL" : "zero" );
2335                 }
2336
2337                 SLAP_EVENT_WAIT( tvp, &ns );
2338                 switch ( ns ) {
2339                 case -1: {      /* failure - try again */
2340                                 int err = sock_errno();
2341
2342                                 if ( err != EINTR ) {
2343                                         ebadf++;
2344
2345                                         /* Don't log unless we got it twice in a row */
2346                                         if ( !( ebadf & 1 ) ) {
2347                                                 Debug( LDAP_DEBUG_ANY,
2348                                                         "daemon: "
2349                                                         SLAP_EVENT_FNAME
2350                                                         " failed count %d "
2351                                                         "err (%d): %s\n",
2352                                                         ebadf, err,
2353                                                         sock_errstr( err ) );
2354                                         }
2355                                         if ( ebadf >= SLAPD_EBADF_LIMIT ) {
2356                                                 slapd_shutdown = 2;
2357                                         }
2358                                 }
2359                         }
2360                         continue;
2361
2362                 case 0:         /* timeout - let threads run */
2363                         ebadf = 0;
2364 #ifndef HAVE_YIELDING_SELECT
2365                         Debug( LDAP_DEBUG_CONNS, "daemon: " SLAP_EVENT_FNAME
2366                                 "timeout - yielding\n",
2367                                 0, 0, 0 );
2368
2369                         ldap_pvt_thread_yield();
2370 #endif /* ! HAVE_YIELDING_SELECT */
2371                         continue;
2372
2373                 default:        /* something happened - deal with it */
2374                         if ( slapd_shutdown ) continue;
2375
2376                         ebadf = 0;
2377                         Debug( LDAP_DEBUG_CONNS,
2378                                 "daemon: activity on %d descriptor%s\n",
2379                                 ns, ns != 1 ? "s" : "", 0 );
2380                         /* FALL THRU */
2381                 }
2382
2383 #if SLAP_EVENTS_ARE_INDEXED
2384                 if ( SLAP_EVENT_IS_READ( wake_sds[0] ) ) {
2385                         char c[BUFSIZ];
2386                         SLAP_EVENT_CLR_READ( wake_sds[0] );
2387                         waking = 0;
2388                         tcp_read( SLAP_FD2SOCK(wake_sds[0]), c, sizeof(c) );
2389                         Debug( LDAP_DEBUG_CONNS, "daemon: waked\n", 0, 0, 0 );
2390                         continue;
2391                 }
2392
2393                 /* The event slot equals the descriptor number - this is
2394                  * true for Unix select and poll. We treat Windows select
2395                  * like this too, even though it's a kludge.
2396                  */
2397                 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2398                         int rc;
2399
2400                         if ( ns <= 0 ) break;
2401                         if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ) continue;
2402 #ifdef LDAP_CONNECTIONLESS
2403                         if ( slap_listeners[l]->sl_is_udp ) continue;
2404 #endif /* LDAP_CONNECTIONLESS */
2405                         if ( !SLAP_EVENT_IS_READ( slap_listeners[l]->sl_sd ) ) continue;
2406                         
2407                         /* clear events */
2408                         SLAP_EVENT_CLR_READ( slap_listeners[l]->sl_sd );
2409                         SLAP_EVENT_CLR_WRITE( slap_listeners[l]->sl_sd );
2410                         ns--;
2411
2412                         rc = slap_listener_activate( slap_listeners[l] );
2413                 }
2414
2415                 /* bypass the following tests if no descriptors left */
2416                 if ( ns <= 0 ) {
2417 #ifndef HAVE_YIELDING_SELECT
2418                         ldap_pvt_thread_yield();
2419 #endif /* HAVE_YIELDING_SELECT */
2420                         continue;
2421                 }
2422
2423                 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
2424                 nrfds = 0;
2425                 nwfds = 0;
2426                 for ( i = 0; i < nfds; i++ ) {
2427                         int     r, w;
2428
2429                         r = SLAP_EVENT_IS_READ( i );
2430                         /* writefds was not initialized if nwriters was zero */
2431                         w = nwriters ? SLAP_EVENT_IS_WRITE( i ) : 0;
2432                         if ( r || w ) {
2433                                 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
2434                                     r ? "r" : "", w ? "w" : "" );
2435                                 if ( r ) {
2436                                         nrfds++;
2437                                         ns--;
2438                                 }
2439                                 if ( w ) {
2440                                         nwfds++;
2441                                         ns--;
2442                                 }
2443                         }
2444                         if ( ns <= 0 ) break;
2445                 }
2446                 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
2447
2448                 /* loop through the writers */
2449                 for ( i = 0; nwfds > 0; i++ ) {
2450                         ber_socket_t wd;
2451                         if ( ! SLAP_EVENT_IS_WRITE( i ) ) continue;
2452                         wd = i;
2453
2454                         SLAP_EVENT_CLR_WRITE( wd );
2455                         nwfds--;
2456
2457                         Debug( LDAP_DEBUG_CONNS,
2458                                 "daemon: write active on %d\n",
2459                                 wd, 0, 0 );
2460
2461                         /*
2462                          * NOTE: it is possible that the connection was closed
2463                          * and that the stream is now inactive.
2464                          * connection_write() must validate the stream is still
2465                          * active.
2466                          *
2467                          * ITS#4338: if the stream is invalid, there is no need to
2468                          * close it here. It has already been closed in connection.c.
2469                          */
2470                         if ( connection_write( wd ) < 0 ) {
2471                                 if ( SLAP_EVENT_IS_READ( wd ) ) {
2472                                         SLAP_EVENT_CLR_READ( (unsigned) wd );
2473                                         nrfds--;
2474                                 }
2475                         }
2476                 }
2477
2478                 for ( i = 0; nrfds > 0; i++ ) {
2479                         ber_socket_t rd;
2480                         if ( ! SLAP_EVENT_IS_READ( i ) ) continue;
2481                         rd = i;
2482                         SLAP_EVENT_CLR_READ( rd );
2483                         nrfds--;
2484
2485                         Debug ( LDAP_DEBUG_CONNS,
2486                                 "daemon: read activity on %d\n", rd, 0, 0 );
2487                         /*
2488                          * NOTE: it is possible that the connection was closed
2489                          * and that the stream is now inactive.
2490                          * connection_read() must valid the stream is still
2491                          * active.
2492                          */
2493
2494                         connection_read_activate( rd );
2495                 }
2496 #else   /* !SLAP_EVENTS_ARE_INDEXED */
2497         /* FIXME */
2498         /* The events are returned in an arbitrary list. This is true
2499          * for /dev/poll, epoll and kqueue. In order to prioritize things
2500          * so that we can handle wake_sds first, listeners second, and then
2501          * all other connections last (as we do for select), we would need
2502          * to use multiple event handles and cascade them.
2503          *
2504          * That seems like a bit of hassle. So the wake_sds check has been
2505          * skipped. For epoll and kqueue we can associate arbitrary data with
2506          * an event, so we could use pointers to the listener structure
2507          * instead of just the file descriptor. For /dev/poll we have to
2508          * search the listeners array for a matching descriptor.
2509          *
2510          * We now handle wake events when we see them; they are not given
2511          * higher priority.
2512          */
2513 #ifdef LDAP_DEBUG
2514                 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
2515
2516                 for ( i = 0; i < ns; i++ ) {
2517                         int     r, w, fd;
2518
2519                         /* Don't log listener events */
2520                         if ( SLAP_EVENT_IS_LISTENER( i )
2521 #ifdef LDAP_CONNECTIONLESS
2522                                 && !( (SLAP_EVENT_LISTENER( i ))->sl_is_udp )
2523 #endif /* LDAP_CONNECTIONLESS */
2524                                 )
2525                         {
2526                                 continue;
2527                         }
2528
2529                         fd = SLAP_EVENT_FD( i );
2530                         /* Don't log internal wake events */
2531                         if ( fd == wake_sds[0] ) continue;
2532
2533                         r = SLAP_EVENT_IS_READ( i );
2534                         w = SLAP_EVENT_IS_WRITE( i );
2535                         if ( r || w ) {
2536                                 Debug( LDAP_DEBUG_CONNS, " %d%s%s", fd,
2537                                     r ? "r" : "", w ? "w" : "" );
2538                         }
2539                 }
2540                 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
2541 #endif /* LDAP_DEBUG */
2542
2543                 for ( i = 0; i < ns; i++ ) {
2544                         int rc = 1, fd, w = 0;
2545
2546                         if ( SLAP_EVENT_IS_LISTENER( i ) ) {
2547                                 rc = slap_listener_activate( SLAP_EVENT_LISTENER( i ) );
2548                         }
2549
2550                         /* If we found a regular listener, rc is now zero, and we
2551                          * can skip the data portion. But if it was a UDP listener
2552                          * then rc is still 1, and we want to handle the data.
2553                          */
2554                         if ( rc ) {
2555                                 fd = SLAP_EVENT_FD( i );
2556
2557                                 /* Handle wake events */
2558                                 if ( fd == wake_sds[0] ) {
2559                                         char c[BUFSIZ];
2560                                         waking = 0;
2561                                         tcp_read( SLAP_FD2SOCK(wake_sds[0]), c, sizeof(c) );
2562                                         continue;
2563                                 }
2564
2565                                 if ( SLAP_EVENT_IS_WRITE( i ) ) {
2566                                         Debug( LDAP_DEBUG_CONNS,
2567                                                 "daemon: write active on %d\n",
2568                                                 fd, 0, 0 );
2569
2570                                         SLAP_EVENT_CLR_WRITE( i );
2571                                         w = 1;
2572
2573                                         /*
2574                                          * NOTE: it is possible that the connection was closed
2575                                          * and that the stream is now inactive.
2576                                          * connection_write() must valid the stream is still
2577                                          * active.
2578                                          */
2579                                         if ( connection_write( fd ) < 0 ) {
2580                                                 continue;
2581                                         }
2582                                 }
2583                                 /* If event is a read */
2584                                 if ( SLAP_EVENT_IS_READ( i ) ) {
2585                                         Debug( LDAP_DEBUG_CONNS,
2586                                                 "daemon: read active on %d\n",
2587                                                 fd, 0, 0 );
2588
2589                                         SLAP_EVENT_CLR_READ( i );
2590                                         connection_read_activate( fd );
2591                                 } else if ( !w ) {
2592                                         Debug( LDAP_DEBUG_CONNS,
2593                                                 "daemon: hangup on %d\n", fd, 0, 0 );
2594                                         connection_hangup( fd );
2595                                 }
2596                         }
2597                 }
2598 #endif  /* SLAP_EVENTS_ARE_INDEXED */
2599
2600 #ifndef HAVE_YIELDING_SELECT
2601                 ldap_pvt_thread_yield();
2602 #endif /* ! HAVE_YIELDING_SELECT */
2603         }
2604
2605         if ( slapd_shutdown == 1 ) {
2606                 Debug( LDAP_DEBUG_ANY,
2607                         "daemon: shutdown requested and initiated.\n",
2608                         0, 0, 0 );
2609
2610         } else if ( slapd_shutdown == 2 ) {
2611 #ifdef HAVE_NT_SERVICE_MANAGER
2612                         Debug( LDAP_DEBUG_ANY,
2613                                "daemon: shutdown initiated by Service Manager.\n",
2614                                0, 0, 0);
2615 #else /* !HAVE_NT_SERVICE_MANAGER */
2616                         Debug( LDAP_DEBUG_ANY,
2617                                "daemon: abnormal condition, shutdown initiated.\n",
2618                                0, 0, 0 );
2619 #endif /* !HAVE_NT_SERVICE_MANAGER */
2620         } else {
2621                 Debug( LDAP_DEBUG_ANY,
2622                        "daemon: no active streams, shutdown initiated.\n",
2623                        0, 0, 0 );
2624         }
2625
2626         if ( slapd_gentle_shutdown != 2 ) close_listeners ( 0 );
2627
2628         if ( !slapd_gentle_shutdown ) {
2629                 slapd_abrupt_shutdown = 1;
2630                 connections_shutdown();
2631         }
2632
2633         if ( LogTest( LDAP_DEBUG_ANY )) {
2634                 int t = ldap_pvt_thread_pool_backload( &connection_pool );
2635                 Debug( LDAP_DEBUG_ANY,
2636                         "slapd shutdown: waiting for %d operations/tasks to finish\n",
2637                         t, 0, 0 );
2638         }
2639         ldap_pvt_thread_pool_destroy( &connection_pool, 1 );
2640
2641         free( slap_listeners );
2642         slap_listeners = NULL;
2643
2644         return NULL;
2645 }
2646
2647
2648 #ifdef LDAP_CONNECTIONLESS
2649 static int
2650 connectionless_init( void )
2651 {
2652         int l;
2653
2654         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
2655                 Listener *lr = slap_listeners[l];
2656                 Connection *c;
2657
2658                 if ( !lr->sl_is_udp ) {
2659                         continue;
2660                 }
2661
2662                 c = connection_init( lr->sl_sd, lr, "", "",
2663                         CONN_IS_UDP, (slap_ssf_t) 0, NULL
2664                         LDAP_PF_LOCAL_SENDMSG_ARG(NULL));
2665
2666                 if ( !c ) {
2667                         Debug( LDAP_DEBUG_TRACE,
2668                                 "connectionless_init: failed on %s (%d)\n",
2669                                 lr->sl_url, lr->sl_sd, 0 );
2670                         return -1;
2671                 }
2672                 lr->sl_is_udp++;
2673         }
2674
2675         return 0;
2676 }
2677 #endif /* LDAP_CONNECTIONLESS */
2678
2679 int
2680 slapd_daemon( void )
2681 {
2682         int rc;
2683
2684 #ifdef LDAP_CONNECTIONLESS
2685         connectionless_init();
2686 #endif /* LDAP_CONNECTIONLESS */
2687
2688 #define SLAPD_LISTENER_THREAD 1
2689 #if defined( SLAPD_LISTENER_THREAD )
2690         {
2691                 ldap_pvt_thread_t       listener_tid;
2692
2693                 /* listener as a separate THREAD */
2694                 rc = ldap_pvt_thread_create( &listener_tid,
2695                         0, slapd_daemon_task, NULL );
2696
2697                 if ( rc != 0 ) {
2698                         Debug( LDAP_DEBUG_ANY,
2699                         "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
2700                         return rc;
2701                 }
2702  
2703                 /* wait for the listener thread to complete */
2704                 ldap_pvt_thread_join( listener_tid, (void *)NULL );
2705         }
2706 #else /* ! SLAPD_LISTENER_THREAD */
2707         /* experimental code */
2708         slapd_daemon_task( NULL );
2709 #endif /* ! SLAPD_LISTENER_THREAD */
2710
2711         return 0;
2712 }
2713
2714 static int
2715 sockinit( void )
2716 {
2717 #if defined( HAVE_WINSOCK2 )
2718         WORD wVersionRequested;
2719         WSADATA wsaData;
2720         int err;
2721
2722         wVersionRequested = MAKEWORD( 2, 0 );
2723
2724         err = WSAStartup( wVersionRequested, &wsaData );
2725         if ( err != 0 ) {
2726                 /* Tell the user that we couldn't find a usable */
2727                 /* WinSock DLL.                                  */
2728                 return -1;
2729         }
2730
2731         /* Confirm that the WinSock DLL supports 2.0.*/
2732         /* Note that if the DLL supports versions greater    */
2733         /* than 2.0 in addition to 2.0, it will still return */
2734         /* 2.0 in wVersion since that is the version we      */
2735         /* requested.                                        */
2736
2737         if ( LOBYTE( wsaData.wVersion ) != 2 ||
2738                 HIBYTE( wsaData.wVersion ) != 0 )
2739         {
2740             /* Tell the user that we couldn't find a usable */
2741             /* WinSock DLL.                                  */
2742             WSACleanup();
2743             return -1;
2744         }
2745
2746         /* The WinSock DLL is acceptable. Proceed. */
2747 #elif defined( HAVE_WINSOCK )
2748         WSADATA wsaData;
2749         if ( WSAStartup( 0x0101, &wsaData ) != 0 ) return -1;
2750 #endif /* ! HAVE_WINSOCK2 && ! HAVE_WINSOCK */
2751
2752         return 0;
2753 }
2754
2755 static int
2756 sockdestroy( void )
2757 {
2758 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
2759         WSACleanup();
2760 #endif /* HAVE_WINSOCK2 || HAVE_WINSOCK */
2761         SLAP_SOCK_DESTROY;
2762
2763         return 0;
2764 }
2765
2766 RETSIGTYPE
2767 slap_sig_shutdown( int sig )
2768 {
2769 #if 0
2770         Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
2771 #endif
2772
2773         /*
2774          * If the NT Service Manager is controlling the server, we don't
2775          * want SIGBREAK to kill the server. For some strange reason,
2776          * SIGBREAK is generated when a user logs out.
2777          */
2778
2779 #if defined(HAVE_NT_SERVICE_MANAGER) && defined(SIGBREAK)
2780         if (is_NT_Service && sig == SIGBREAK) {
2781                 /* empty */;
2782         } else
2783 #endif /* HAVE_NT_SERVICE_MANAGER && SIGBREAK */
2784 #ifdef SIGHUP
2785         if (sig == SIGHUP && global_gentlehup && slapd_gentle_shutdown == 0) {
2786                 slapd_gentle_shutdown = 1;
2787         } else
2788 #endif /* SIGHUP */
2789         {
2790                 slapd_shutdown = 1;
2791         }
2792
2793         WAKE_LISTENER(1);
2794
2795         /* reinstall self */
2796         (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
2797 }
2798
2799 RETSIGTYPE
2800 slap_sig_wake( int sig )
2801 {
2802         WAKE_LISTENER(1);
2803
2804         /* reinstall self */
2805         (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
2806 }
2807
2808
2809 void
2810 slapd_add_internal( ber_socket_t s, int isactive )
2811 {
2812         slapd_add( s, isactive, NULL );
2813 }
2814
2815 Listener **
2816 slapd_get_listeners( void )
2817 {
2818         return slap_listeners;
2819 }
2820
2821 void
2822 slap_wake_listener()
2823 {
2824         WAKE_LISTENER(1);
2825 }