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