]> git.sur5r.net Git - openldap/blob - servers/slapd/daemon.c
2279666ddad624012b36268538333f8c2af04077
[openldap] / servers / slapd / daemon.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10
11 #include <ac/ctype.h>
12 #include <ac/errno.h>
13 #include <ac/signal.h>
14 #include <ac/socket.h>
15 #include <ac/string.h>
16 #include <ac/time.h>
17 #include <ac/unistd.h>
18
19 #include "ldap_pvt.h"
20 #include "lutil.h"
21 #include "slap.h"
22
23 #ifdef HAVE_TCPD
24 #include <tcpd.h>
25
26 int allow_severity = LOG_INFO;
27 int deny_severity = LOG_NOTICE;
28 #endif /* TCP Wrappers */
29
30 #ifdef LDAP_PF_LOCAL
31 #include <sys/stat.h>
32 #endif /* LDAP_PF_LOCAL */
33
34 /* globals */
35 time_t starttime;
36 ber_socket_t dtblsize;
37
38 typedef union slap_sockaddr {
39         struct sockaddr sa_addr;
40         struct sockaddr_in sa_in_addr;
41 #ifdef LDAP_INET6
42         struct sockaddr_in6 sa_in6_addr;
43 #endif
44 #ifdef LDAP_PF_LOCAL
45         struct sockaddr_un sa_un_addr;
46 #endif
47 } Sockaddr;
48
49 typedef struct slap_listener {
50         char* sl_url;
51         char* sl_name;
52 #ifdef HAVE_TLS
53         int             sl_is_tls;
54 #endif
55         ber_socket_t            sl_sd;
56         Sockaddr sl_sa;
57 #define sl_addr sl_sa.sa_in_addr
58 } Listener;
59
60 Listener **slap_listeners = NULL;
61
62 static ber_socket_t wake_sds[2];
63
64 #ifdef NO_THREADS
65 static int waking;
66 #define WAKE_LISTENER(w) \
67 ((w && !waking) ? tcp_write( wake_sds[1], "0", 1 ), waking=1 : 0)
68 #else
69 #define WAKE_LISTENER(w) \
70 do { if (w) tcp_write( wake_sds[1], "0", 1 ); } while(0)
71 #endif
72
73 #ifdef HAVE_NT_SERVICE_MANAGER
74 /* in nt_main.c */
75 extern ldap_pvt_thread_cond_t                   started_event;
76 extern int        is_NT_Service;
77 #endif
78
79 #ifndef HAVE_WINSOCK
80 static 
81 #endif
82 volatile sig_atomic_t slapd_shutdown = 0;
83
84 static ldap_pvt_thread_t        listener_tid;
85
86 static struct slap_daemon {
87         ldap_pvt_thread_mutex_t sd_mutex;
88
89         int sd_nactives;
90
91 #ifndef HAVE_WINSOCK
92         /* In winsock, accept() returns values higher than dtblsize
93                 so don't bother with this optimization */
94         int sd_nfds;
95 #endif
96
97         fd_set sd_actives;
98         fd_set sd_readers;
99         fd_set sd_writers;
100 } slap_daemon; 
101
102 /*
103  * Add a descriptor to daemon control
104  */
105 static void slapd_add(ber_socket_t s) {
106         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
107
108         assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
109         assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
110         assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
111
112 #ifndef HAVE_WINSOCK
113         if (s >= slap_daemon.sd_nfds) {
114                 slap_daemon.sd_nfds = s + 1;
115         }
116 #endif
117
118         FD_SET( s, &slap_daemon.sd_actives );
119         FD_SET( s, &slap_daemon.sd_readers );
120
121         Debug( LDAP_DEBUG_CONNS, "daemon: added %ld%s%s\n",
122                 (long) s,
123             FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
124                 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
125
126         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
127 }
128
129 /*
130  * Remove the descriptor from daemon control
131  */
132 void slapd_remove(ber_socket_t s, int wake) {
133         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
134
135         Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
136                 (long) s,
137             FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
138                 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
139
140         FD_CLR( s, &slap_daemon.sd_actives );
141         FD_CLR( s, &slap_daemon.sd_readers );
142         FD_CLR( s, &slap_daemon.sd_writers );
143
144         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
145         WAKE_LISTENER(wake);
146 }
147
148 void slapd_clr_write(ber_socket_t s, int wake) {
149         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
150
151         assert( FD_ISSET( s, &slap_daemon.sd_actives) );
152         FD_CLR( s, &slap_daemon.sd_writers );
153
154         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
155         WAKE_LISTENER(wake);
156 }
157
158 void slapd_set_write(ber_socket_t s, int wake) {
159         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
160
161         assert( FD_ISSET( s, &slap_daemon.sd_actives) );
162         if (!FD_ISSET(s, &slap_daemon.sd_writers))
163             FD_SET( (unsigned) s, &slap_daemon.sd_writers );
164
165         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
166         WAKE_LISTENER(wake);
167 }
168
169 void slapd_clr_read(ber_socket_t s, int wake) {
170         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
171
172         assert( FD_ISSET( s, &slap_daemon.sd_actives) );
173         FD_CLR( s, &slap_daemon.sd_readers );
174
175         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
176         WAKE_LISTENER(wake);
177 }
178
179 void slapd_set_read(ber_socket_t s, int wake) {
180         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
181
182         assert( FD_ISSET( s, &slap_daemon.sd_actives) );
183         if (!FD_ISSET(s, &slap_daemon.sd_readers))
184             FD_SET( s, &slap_daemon.sd_readers );
185
186         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
187         WAKE_LISTENER(wake);
188 }
189
190 static void slapd_close(ber_socket_t s) {
191         Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
192                 (long) s, 0, 0 );
193         tcp_close(s);
194 }
195
196
197 static Listener * open_listener( const char* url )
198 {
199         int     tmp, rc;
200         Listener l;
201         Listener *li;
202         LDAPURLDesc *lud;
203         char *s;
204         int port;
205 #ifdef LDAP_INET6
206         char serv[7];
207         struct addrinfo hints, *res, *sai;
208         int err;
209 #endif
210
211         rc = ldap_url_parse( url, &lud );
212
213         if( rc != LDAP_URL_SUCCESS ) {
214                 Debug( LDAP_DEBUG_ANY,
215                         "daemon: listen URL \"%s\" parse error=%d\n",
216                         url, rc, 0 );
217                 return NULL;
218         }
219
220 #ifndef HAVE_TLS
221         if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
222                 Debug( LDAP_DEBUG_ANY,
223                         "daemon: TLS not supported (%s)\n",
224                         url, 0, 0 );
225                 ldap_free_urldesc( lud );
226                 return NULL;
227         }
228
229         if(! lud->lud_port ) {
230                 lud->lud_port = LDAP_PORT;
231         }
232
233 #else
234         l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
235
236         if(! lud->lud_port ) {
237                 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
238         }
239 #endif
240
241 #ifdef LDAP_INET6
242         memset( &hints, '\0', sizeof(hints) );
243         hints.ai_flags = AI_PASSIVE;
244         hints.ai_family = AF_UNSPEC;
245         hints.ai_socktype = SOCK_STREAM;
246
247 #  ifdef LDAP_PF_LOCAL
248         if (lud->lud_protocol == LDAP_PROTO_LOCAL) {
249                 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
250                         err = getaddrinfo(NULL, "/tmp/.ldap-sock", &hints, &res);
251                         if (!err)
252                                 unlink( "/tmp/.ldap-sock" );
253                 } else {
254                         err = getaddrinfo(NULL, lud->lud_host, &hints, &res);
255                         if (!err)
256                                 unlink( lud->lud_host );
257                 }
258         } else
259 #  endif /* LDAP_PF_LOCAL */
260         {
261                 snprintf(serv, sizeof serv, "%d", lud->lud_port);
262                 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
263                         || strcmp(lud->lud_host, "*") == 0 )
264                 {
265                         err = getaddrinfo(NULL, serv, &hints, &res);
266                 } else {
267                         err = getaddrinfo(lud->lud_host, serv, &hints, &res);
268                 }
269         }
270
271         if ( err ) {
272                 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed\n", 0, 0, 0);
273                 ldap_free_urldesc( lud );
274                 return NULL;
275         }
276
277         ldap_free_urldesc( lud );
278         sai = res;
279         do {
280                 l.sl_sd = socket( sai->ai_family, sai->ai_socktype, sai->ai_protocol);
281                 if ( l.sl_sd == AC_SOCKET_INVALID ) {
282                         Debug( LDAP_DEBUG_ANY,
283                                 "daemon: socket() failed errno=%d (%s)\n", err,
284                                 sock_errstr(err), 0 );
285                         continue;
286                 }
287
288                 if ( sai->ai_family != AF_UNIX ) {
289 #else
290 #ifdef LDAP_PF_LOCAL
291         if (lud->lud_protocol == LDAP_PROTO_LOCAL) {
292                 port = 0;
293                 (void) memset( (void *)&l.sl_sa.sa_un_addr, '\0', sizeof(l.sl_sa.sa_un_addr) );
294
295                 l.sl_sa.sa_un_addr.sun_family = AF_UNIX;
296
297                 /* hack: overload the host to be the path */
298                 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
299                         strcpy( l.sl_sa.sa_un_addr.sun_path, "/tmp/.ldap-sock" );
300                 } else {
301                         if ( strlen(lud->lud_host) > (sizeof(l.sl_sa.sa_un_addr.sun_path) - 1) ) {
302                                 Debug( LDAP_DEBUG_ANY,
303                                         "daemon: domain socket path (%s) too long in URL: %s",
304                                         lud->lud_host, url, 0);
305                                 ldap_free_urldesc( lud );
306                                 return NULL;
307                         }
308                         strcpy( l.sl_sa.sa_un_addr.sun_path, lud->lud_host );
309                 }
310                 unlink( l.sl_sa.sa_un_addr.sun_path ); 
311 #if 0
312                 /* I don't think we need to set this. */
313                 l.sl_sa.sa_un_addr.sun_len = sizeof( l.sl_sa.sa_un_addr.sun_len ) +
314                         sizeof( l.sl_sa.sa_un_addr.sun_family ) +
315                         strlen( l.sl_sa.sa_un_addr.sun_path ) + 1;
316 #endif
317         } else
318 #endif /* LDAP_PF_LOCAL */
319         {
320
321         port = lud->lud_port;
322
323         (void) memset( (void*) &l.sl_addr, '\0', sizeof(l.sl_addr) );
324
325         l.sl_addr.sin_family = AF_INET;
326         l.sl_addr.sin_port = htons( (unsigned short) lud->lud_port );
327
328         if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
329                 || strcmp(lud->lud_host, "*") == 0 )
330         {
331                 l.sl_addr.sin_addr.s_addr = htonl(INADDR_ANY);
332
333         } else {
334                 /* host or address was specified */
335                 if( !inet_aton( lud->lud_host, &l.sl_addr.sin_addr ) ) {
336                         struct hostent *he = gethostbyname( lud->lud_host );
337                         if( he == NULL ) {
338                                 Debug( LDAP_DEBUG_ANY,
339                                         "daemon: invalid host (%s) in URL: %s",
340                                         lud->lud_host, url, 0);
341                                 ldap_free_urldesc( lud );
342                                 return NULL;
343                         }
344
345                         memcpy( &l.sl_addr.sin_addr, he->h_addr,
346                                sizeof( l.sl_addr.sin_addr ) );
347                 }
348         }
349         }
350
351         ldap_free_urldesc( lud );
352
353         l.sl_sd = socket( l.sl_sa.sa_addr.sa_family, SOCK_STREAM, 0 );
354         if ( l.sl_sd == AC_SOCKET_INVALID ) {
355                 int err = sock_errno();
356                 Debug( LDAP_DEBUG_ANY,
357                         "daemon: socket() failed errno=%d (%s)\n", err,
358                         sock_errstr(err), 0 );
359                 return NULL;
360         }
361
362 #ifndef HAVE_WINSOCK
363         if ( l.sl_sd >= dtblsize ) {
364                 Debug( LDAP_DEBUG_ANY,
365                         "daemon: listener descriptor %ld is too great %ld\n",
366                         (long) l.sl_sd, (long) dtblsize, 0 );
367                 tcp_close( l.sl_sd );
368                 return NULL;
369         }
370 #endif
371
372 #ifdef LDAP_PF_LOCAL
373         /* for IP sockets only */
374         if ( l.sl_sa.sa_addr.sa_family == AF_INET ) {
375 #endif /* LDAP_PF_LOCAL */
376 #endif /* LDAP_INET6 */
377
378 #ifdef SO_REUSEADDR
379         /* enable address reuse */
380         tmp = 1;
381         rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
382                 (char *) &tmp, sizeof(tmp) );
383         if ( rc == AC_SOCKET_ERROR ) {
384                 int err = sock_errno();
385                 Debug( LDAP_DEBUG_ANY,
386                "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
387                 (long) l.sl_sd, err, sock_errstr(err) );
388         }
389 #endif
390 #ifdef SO_KEEPALIVE
391         /* enable keep alives */
392         tmp = 1;
393         rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_KEEPALIVE,
394                 (char *) &tmp, sizeof(tmp) );
395         if ( rc == AC_SOCKET_ERROR ) {
396                 int err = sock_errno();
397                 Debug( LDAP_DEBUG_ANY,
398                         "slapd(%ld): setsockopt(SO_KEEPALIVE) failed errno=%d (%s)\n",
399                 (long) l.sl_sd, err, sock_errstr(err) );
400         }
401 #endif
402 #ifdef TCP_NODELAY
403         /* enable no delay */
404         tmp = 1;
405         rc = setsockopt( l.sl_sd, IPPROTO_TCP, TCP_NODELAY,
406                 (char *)&tmp, sizeof(tmp) );
407         if ( rc == AC_SOCKET_ERROR ) {
408                 int err = sock_errno();
409                 Debug( LDAP_DEBUG_ANY,
410                         "slapd(%ld): setsockopt(TCP_NODELAY) failed errno=%d (%s)\n",
411                 (long) l.sl_sd, err, sock_errstr(err) );
412         }
413 #endif
414
415 #ifdef LDAP_INET6
416                 } /* sai->ai_family != AF_UNIX */
417                 if (!bind(l.sl_sd, sai->ai_addr, sai->ai_addrlen))
418                         break;
419                 err = sock_errno();
420                 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
421                         (long) l.sl_sd, err, sock_errstr(err) );
422                 tcp_close( l.sl_sd );
423         } while ((sai = sai->ai_next) != NULL);
424
425         if (!sai) {
426                 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed\n",
427                         (long) l.sl_sd, 0, 0 );
428                 return NULL;
429         }
430
431         switch ( sai->ai_family ) {
432 #ifdef LDAP_PF_LOCAL
433         case AF_UNIX:
434                 if ( chmod( (char *)sai->ai_addr, S_IRWXU ) < 0 ) {
435                         err = sock_errno();
436                         Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
437                                 (long) l.sl_sd, err, sock_errstr(err) );
438                         tcp_close( l.sl_sd );
439                         return NULL;
440                 }
441                 l.sl_name = ch_malloc( strlen((char *)sai->ai_addr) + sizeof("PATH=") );
442                 sprintf( l.sl_name, "PATH=%s", sai->ai_addr );
443                 break;
444 #  endif /* LDAP_PF_LOCAL */
445
446         case AF_INET: {
447                 char addr[INET_ADDRSTRLEN];
448                 inet_ntop( AF_INET,
449                         &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
450                         addr, sizeof(addr) );
451                 l.sl_name = ch_malloc( strlen(addr) + strlen(serv) + sizeof("IP=:") );
452                 sprintf( l.sl_name, "IP=%s:%s", addr, serv );
453         } break;
454
455 #ifdef AF_INET6
456         case AF_INET6: {
457                 char addr[INET6_ADDRSTRLEN];
458                 inet_ntop( AF_INET6,
459                         &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
460                         addr, sizeof addr);
461                 l.sl_name = ch_malloc( strlen(addr) + strlen(serv) + sizeof("IP= ") );
462                 sprintf( l.sl_name, "IP=%s %s", addr, serv );
463         } break;
464 #endif
465
466         default:
467                 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
468                         (int) sai->ai_family, 0, 0 );
469                 break;
470         }
471 #else
472 #ifdef LDAP_PF_LOCAL
473         /* close conditional */
474         }
475 #endif /* LDAP_PF_LOCAL */
476
477         switch ( l.sl_sa.sa_addr.sa_family ) {
478 #ifdef LDAP_PF_LOCAL
479                 case AF_UNIX:
480                         rc = bind( l.sl_sd, (struct sockaddr *)&l.sl_sa,
481                                 sizeof(l.sl_sa.sa_un_addr) );
482                         break;
483 #endif
484
485                 case AF_INET:
486                         rc = bind( l.sl_sd, (struct sockaddr *)&l.sl_sa,
487                                 sizeof(l.sl_sa.sa_in_addr) );
488                         break;
489
490                 default:
491                         rc = AC_SOCKET_ERROR;
492                         errno = EINVAL;
493                         break;
494         }
495
496         if ( rc == AC_SOCKET_ERROR ) {
497                 int err = sock_errno();
498                 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
499                 (long) l.sl_sd, err, sock_errstr(err) );
500                 tcp_close( l.sl_sd );
501                 return NULL;
502         }
503
504         switch ( l.sl_sa.sa_addr.sa_family ) {
505 #ifdef LDAP_PF_LOCAL
506                 case AF_UNIX:
507                         if ( chmod( l.sl_sa.sa_un_addr.sun_path, S_IRWXU ) < 0 ) {
508                                 int err = sock_errno();
509                                 Debug( LDAP_DEBUG_ANY,
510                                         "daemon: chmod(%ld) failed errno=%d (%s)",
511                                         (long) l.sl_sd, err, sock_errstr(err) );
512                                 tcp_close( l.sl_sd );
513                                 return NULL;
514                         }
515
516                         l.sl_name = ch_malloc( strlen(l.sl_sa.sa_un_addr.sun_path)
517                                 + sizeof("PATH=") );
518                         sprintf( l.sl_name, "PATH=%s", l.sl_sa.sa_un_addr.sun_path );
519                         break;
520 #  endif /* LDAP_PF_LOCAL */
521
522                 case AF_INET:
523                         l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65336") );
524                         s = inet_ntoa( l.sl_addr.sin_addr );
525                         sprintf( l.sl_name, "IP=%s:%d",
526                                 s != NULL ? s : "unknown" , port );
527                         break;
528
529                 default:
530                         l.sl_name = ch_strdup( "UNKNOWN" );
531                         break;
532         }
533
534 #endif /* LDAP_INET6 */
535
536         l.sl_url = ch_strdup( url );
537         li = ch_malloc( sizeof( Listener ) );
538         *li = l;
539
540         Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
541                 l.sl_url, 0, 0 );
542
543         return li;
544 }
545
546 static int sockinit(void);
547 static int sockdestroy(void);
548
549 int slapd_daemon_init( const char *urls )
550 {
551         int i, rc;
552         char **u;
553
554         Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
555                 urls ? urls : "<null>", 0, 0 );
556
557         if( (rc = sockinit()) != 0 ) {
558                 return rc;
559         }
560
561 #ifdef HAVE_SYSCONF
562         dtblsize = sysconf( _SC_OPEN_MAX );
563 #elif HAVE_GETDTABLESIZE
564         dtblsize = getdtablesize();
565 #else
566         dtblsize = FD_SETSIZE;
567 #endif
568
569 #ifdef FD_SETSIZE
570         if(dtblsize > FD_SETSIZE) {
571                 dtblsize = FD_SETSIZE;
572         }
573 #endif  /* !FD_SETSIZE */
574
575         /* open a pipe (or something equivalent connected to itself).
576          * we write a byte on this fd whenever we catch a signal. The main
577          * loop will be select'ing on this socket, and will wake up when
578          * this byte arrives.
579          */
580         if( (rc = lutil_pair( wake_sds )) < 0 ) {
581                 Debug( LDAP_DEBUG_ANY,
582                         "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
583                 return rc;
584         }
585
586         FD_ZERO( &slap_daemon.sd_readers );
587         FD_ZERO( &slap_daemon.sd_writers );
588
589         if( urls == NULL ) {
590                 urls = "ldap:///";
591         }
592
593         u = str2charray( urls, " " );
594
595         if( u == NULL || u[0] == NULL ) {
596                 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
597                         urls, 0, 0 );
598
599                 return -1;
600         }
601
602         for( i=0; u[i] != NULL; i++ ) {
603                 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
604                         u[i], 0, 0 );
605         }
606
607         if( i == 0 ) {
608                 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
609                         urls, 0, 0 );
610                 charray_free( u );
611                 return -1;
612         }
613
614         Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
615                 i, 0, 0 );
616
617         slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
618
619         for(i = 0; u[i] != NULL; i++ ) {
620                 slap_listeners[i] = open_listener( u[i] );
621
622                 if( slap_listeners[i] == NULL ) {
623                         charray_free( u );
624                         return -1;
625                 }
626         }
627         slap_listeners[i] = NULL;
628
629         Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
630                 i, 0, 0 );
631
632         charray_free( u );
633         ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
634         return !i;
635 }
636
637
638 int
639 slapd_daemon_destroy(void)
640 {
641         connections_destroy();
642         tcp_close( wake_sds[1] );
643         tcp_close( wake_sds[0] );
644         sockdestroy();
645         return 0;
646 }
647
648
649 static void *
650 slapd_daemon_task(
651         void *ptr
652 )
653 {
654         int l;
655         time_t  last_idle_check = slap_get_time();
656         time( &starttime );
657
658         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
659                 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
660                         continue;
661
662                 if ( listen( slap_listeners[l]->sl_sd, 5 ) == -1 ) {
663                         int err = sock_errno();
664                         Debug( LDAP_DEBUG_ANY,
665                                 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
666                                         slap_listeners[l]->sl_url, err,
667                                         sock_errstr(err) );
668                         return( (void*)-1 );
669                 }
670
671                 slapd_add( slap_listeners[l]->sl_sd );
672         }
673
674 #ifdef HAVE_NT_SERVICE_MANAGER
675         if ( started_event != NULL ) {
676                 ldap_pvt_thread_cond_signal( &started_event );
677         }
678 #endif
679         /* initialization complete. Here comes the loop. */
680
681         while ( !slapd_shutdown ) {
682                 ber_socket_t i;
683                 int ns;
684                 int at;
685                 ber_socket_t nfds;
686 #define SLAPD_EBADF_LIMIT 10
687                 int ebadf = 0;
688
689 #define SLAPD_IDLE_CHECK_LIMIT 4
690                 time_t  now = slap_get_time();
691
692
693                 fd_set                  readfds;
694                 fd_set                  writefds;
695 #if defined( LDAP_PF_LOCAL ) || defined( LDAP_INET6 )
696                 Sockaddr                from;
697 /* minimize impact, undefine later. */
698 #define sin_addr sa_in_addr.sin_addr
699 #define sin_port sa_in_addr.sin_port
700 #else
701                 struct sockaddr_in      from;
702 #endif /* LDAP_PF_LOCAL || LDAP_INET6 */
703 #if defined(SLAPD_RLOOKUPS)
704         struct hostent          *hp;
705 #endif
706                 struct timeval          zero;
707                 struct timeval          *tvp;
708
709                 if( global_idletimeout > 0 && difftime(
710                         last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT,
711                         now ) < 0 )
712                 {
713                         connections_timeout_idle(now);
714                 }
715
716                 FD_ZERO( &writefds );
717                 FD_ZERO( &readfds );
718
719                 zero.tv_sec = 0;
720                 zero.tv_usec = 0;
721
722                 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
723
724 #ifdef FD_SET_MANUAL_COPY
725                 for( s = 0; s < nfds; s++ ) {
726                         if(FD_ISSET( &slap_sd_readers, s )) {
727                                 FD_SET( s, &readfds );
728                         }
729                         if(FD_ISSET( &slap_sd_writers, s )) {
730                                 FD_SET( s, &writefds );
731                         }
732                 }
733 #else
734                 memcpy( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
735                 memcpy( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
736 #endif
737                 assert(!FD_ISSET(wake_sds[0], &readfds));
738                 FD_SET( wake_sds[0], &readfds );
739
740                 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
741                         if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
742                                 continue;
743                         if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
744                             FD_SET( slap_listeners[l]->sl_sd, &readfds );
745                 }
746
747 #ifndef HAVE_WINSOCK
748                 nfds = slap_daemon.sd_nfds;
749 #else
750                 nfds = dtblsize;
751 #endif
752
753                 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
754
755                 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
756                 at = active_threads;
757                 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
758
759 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
760                 tvp = NULL;
761 #else
762                 tvp = at ? &zero : NULL;
763 #endif
764
765                 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
766                         if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
767                                 continue;
768
769                         Debug( LDAP_DEBUG_CONNS,
770                                 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
771                                         slap_listeners[l]->sl_sd, at,
772                                         tvp == NULL ? "NULL" : "zero" );
773                 }
774
775                 switch(ns = select( nfds, &readfds,
776 #ifdef HAVE_WINSOCK
777                         /* don't pass empty fd_set */
778                         ( writefds.fd_count > 0 ? &writefds : NULL ),
779 #else
780                         &writefds,
781 #endif
782                         NULL, tvp ))
783                 {
784                 case -1: {      /* failure - try again */
785                                 int err = sock_errno();
786
787                                 if( err == EBADF 
788 #ifdef HAVE_WINSOCK
789                                         || err == WSAENOTSOCK   /* you'd think this would be EBADF */
790 #endif
791                                 ) {
792                                         if (++ebadf < SLAPD_EBADF_LIMIT)
793                                                 continue;
794                                 }
795
796                                 if( err != EINTR ) {
797                                         Debug( LDAP_DEBUG_CONNS,
798                                                 "daemon: select failed (%d): %s\n",
799                                                 err, sock_errstr(err), 0 );
800
801                                         slapd_shutdown = -1;
802                                 }
803                         }
804                         continue;
805
806                 case 0:         /* timeout - let threads run */
807                         ebadf = 0;
808                         Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
809                             0, 0, 0 );
810                 ldap_pvt_thread_yield();
811                         continue;
812
813                 default:        /* something happened - deal with it */
814                         ebadf = 0;
815                         Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
816                                 ns, 0, 0 );
817                         /* FALL THRU */
818                 }
819
820                 if( FD_ISSET( wake_sds[0], &readfds ) ) {
821                         char c[BUFSIZ];
822                         tcp_read( wake_sds[0], c, sizeof(c) );
823 #ifdef NO_THREADS
824                         waking = 0;
825 #endif
826                         continue;
827                 }
828
829                 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
830                         ber_int_t s;
831                         socklen_t len = sizeof(from);
832                         long id;
833
834                         char    *dnsname;
835                         char    *peeraddr;
836 #ifdef LDAP_PF_LOCAL
837                         char    peername[MAXPATHLEN + sizeof("PATH=")];
838 #elif defined(LDAP_INET6)
839                         char    peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
840 #else
841                         char    peername[sizeof("IP=255.255.255.255:65336")];
842 #endif /* LDAP_PF_LOCAL */
843
844                         peername[0] = '\0';
845
846                         if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
847                                 continue;
848
849                         if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
850                                 continue;
851
852                         if ( (s = accept( slap_listeners[l]->sl_sd,
853                                 (struct sockaddr *) &from, &len )) == AC_SOCKET_INVALID )
854                         {
855                                 int err = sock_errno();
856                                 Debug( LDAP_DEBUG_ANY,
857                                     "daemon: accept(%ld) failed errno=%d (%s)\n",
858                                     (long) slap_listeners[l]->sl_sd, err,
859                                     sock_errstr(err) );
860                                 continue;
861                         }
862
863 #ifdef LDAP_DEBUG
864                         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
865
866                         /* newly accepted stream should not be in any of the FD SETS */
867
868                         assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
869                         assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
870                         assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
871
872                         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
873 #endif
874
875 #ifndef HAVE_WINSOCK
876                         /* make sure descriptor number isn't too great */
877                         if ( s >= dtblsize ) {
878                                 Debug( LDAP_DEBUG_ANY,
879                                         "daemon: %ld beyond descriptor table size %ld\n",
880                                         (long) s, (long) dtblsize, 0 );
881                                 slapd_close(s);
882                                 continue;
883                         }
884 #endif
885                    
886                         Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
887                                 (long) s, 0, 0 );
888
889                         len = sizeof(from);
890
891                         if ( getpeername( s, (struct sockaddr *) &from, &len ) != 0 ) {
892                                 int err = sock_errno();
893                                 Debug( LDAP_DEBUG_ANY,
894                                         "daemon: getpeername( %ld ) failed: errno=%d (%s)\n",
895                                         (long) s, err, sock_errstr(err) );
896                                 slapd_close(s);
897                                 continue;
898                         }
899
900 #if defined( LDAP_PF_LOCAL ) || defined( LDAP_INET6 )
901                         switch ( from.sa_addr.sa_family ) {
902 #  ifdef LDAP_PF_LOCAL
903                         case AF_UNIX:
904                                 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
905                                 break;
906 #endif /* LDAP_PF_LOCAL */
907
908 #  ifdef LDAP_INET6
909                         case AF_INET6: {
910                                 char addr[INET6_ADDRSTRLEN];
911                                 sprintf( peername, "IP=%s %d",
912                                         inet_ntop( AF_INET6,
913                                                 &from.sa_in6_addr.sin6_addr,
914                                             addr, sizeof addr) ? addr : "unknown",
915                                         (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
916                         } break;
917 #  endif /* LDAP_INET6 */
918
919                         case AF_INET:
920 #endif /* LDAP_PF_LOCAL || LDAP_INET6 */
921                         peeraddr = inet_ntoa( from.sin_addr );
922                         sprintf( peername, "IP=%s:%d",
923                                 peeraddr != NULL ? peeraddr : "unknown",
924                                 (unsigned) ntohs( from.sin_port ) );
925 #if defined( LDAP_PF_LOCAL ) || defined( LDAP_INET6 )
926                                 break;
927
928                         default:
929                                 slapd_close(s);
930                                 continue;
931                         }
932                         if ( from.sa_addr.sa_family != AF_UNIX ) {
933 #endif /* LDAP_PF_LOCAL || LDAP_INET6 */
934 #ifdef SLAPD_RLOOKUPS
935 #  ifdef LDAP_INET6
936                 if ( from.sa_addr.sa_family == AF_INET6 )
937                         hp = gethostbyaddr( (char *)
938                                 &(from.sa_in6_addr.sin6_addr),
939                                 sizeof(from.sa_in6_addr.sin6_addr),
940                                 AF_INET6 );
941                 else
942 #  endif LDAP_INET6
943                         hp = gethostbyaddr( (char *) &(from.sin_addr),
944                             sizeof(from.sin_addr), AF_INET );
945
946                         dnsname = hp ? ldap_pvt_str2lower( hp->h_name ) : NULL;
947 #else
948                         dnsname = NULL;
949 #endif /* SLAPD_RLOOKUPS */
950
951 #ifdef HAVE_TCPD
952                         if( !hosts_ctl("slapd",
953                                 dnsname != NULL ? dnsname : STRING_UNKNOWN,
954                                 peeraddr != NULL ? peeraddr : STRING_UNKNOWN,
955                                 STRING_UNKNOWN ))
956                         {
957                                 /* DENY ACCESS */
958                                 Statslog( LDAP_DEBUG_ANY,
959                                  "fd=%ld connection from %s (%s) denied.\n",
960                                         (long) s,
961                                         dnsname != NULL ? dnsname : "unknown",
962                                         peeraddr != NULL ? peeraddr : "unknown",
963                                   0, 0 );
964
965                                 slapd_close(s);
966                                 continue;
967                         }
968 #endif /* HAVE_TCPD */
969 #if defined( LDAP_PF_LOCAL ) || defined( LDAP_INET6 )
970                         }
971 #undef  sin_addr
972 #undef  sin_port
973 #endif /* LDAP_PF_LOCAL || LDAP_INET6 */
974
975                         if( (id = connection_init(s,
976                                 slap_listeners[l]->sl_url,
977                                 dnsname != NULL ? dnsname : "unknown",
978                                 peername,
979                                 slap_listeners[l]->sl_name,
980 #ifdef HAVE_TLS
981                                 slap_listeners[l]->sl_is_tls
982 #else
983                                 0
984 #endif
985                                 )) < 0 )
986                         {
987                                 Debug( LDAP_DEBUG_ANY,
988                                         "daemon: connection_init(%ld, %s, %s) failed.\n",
989                                         (long) s,
990                                         peername,
991                                         slap_listeners[l]->sl_name );
992                                 slapd_close(s);
993                                 continue;
994                         }
995
996                         Statslog( LDAP_DEBUG_STATS,
997                                 "daemon: conn=%ld fd=%ld connection from %s (%s) accepted.\n",
998                                 id, (long) s,
999                                 peername,
1000                                 slap_listeners[l]->sl_name,
1001                                 0 );
1002
1003                         slapd_add( s );
1004                         continue;
1005                 }
1006
1007 #ifdef LDAP_DEBUG
1008                 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1009 #ifdef HAVE_WINSOCK
1010                 for ( i = 0; i < readfds.fd_count; i++ ) {
1011                         Debug( LDAP_DEBUG_CONNS, " %d%s",
1012                                 readfds.fd_array[i], "r", 0 );
1013                 }
1014                 for ( i = 0; i < writefds.fd_count; i++ ) {
1015                         Debug( LDAP_DEBUG_CONNS, " %d%s",
1016                                 writefds.fd_array[i], "w", 0 );
1017                 }
1018 #else
1019                 for ( i = 0; i < nfds; i++ ) {
1020                         int     r, w;
1021                         int     is_listener = 0;
1022
1023                         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1024                                 if ( i == slap_listeners[l]->sl_sd ) {
1025                                         is_listener = 1;
1026                                         break;
1027                                 }
1028                         }
1029                         if ( is_listener ) {
1030                                 continue;
1031                         }
1032                         r = FD_ISSET( i, &readfds );
1033                         w = FD_ISSET( i, &writefds );
1034                         if ( r || w ) {
1035                                 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1036                                     r ? "r" : "", w ? "w" : "" );
1037                         }
1038                 }
1039 #endif
1040                 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1041 #endif
1042
1043                 /* loop through the writers */
1044 #ifdef HAVE_WINSOCK
1045                 for ( i = 0; i < writefds.fd_count; i++ )
1046 #else
1047                 for ( i = 0; i < nfds; i++ )
1048 #endif
1049                 {
1050                         ber_socket_t wd;
1051                         int is_listener = 0;
1052 #ifdef HAVE_WINSOCK
1053                         wd = writefds.fd_array[i];
1054 #else
1055                         if( ! FD_ISSET( i, &writefds ) ) {
1056                                 continue;
1057                         }
1058                         wd = i;
1059 #endif
1060
1061                         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1062                                 if ( i == slap_listeners[l]->sl_sd ) {
1063                                         is_listener = 1;
1064                                         break;
1065                                 }
1066                         }
1067                         if ( is_listener ) {
1068                                 continue;
1069                         }
1070                         Debug( LDAP_DEBUG_CONNS,
1071                                 "daemon: write active on %d\n",
1072                                 wd, 0, 0 );
1073
1074                         /*
1075                          * NOTE: it is possible that the connection was closed
1076                          * and that the stream is now inactive.
1077                          * connection_write() must valid the stream is still
1078                          * active.
1079                          */
1080
1081                         if ( connection_write( wd ) < 0 ) {
1082                                 FD_CLR( (unsigned) wd, &readfds );
1083                                 slapd_close( wd );
1084                         }
1085                 }
1086
1087 #ifdef HAVE_WINSOCK
1088                 for ( i = 0; i < readfds.fd_count; i++ )
1089 #else
1090                 for ( i = 0; i < nfds; i++ )
1091 #endif
1092                 {
1093                         ber_socket_t rd;
1094                         int is_listener = 0;
1095
1096 #ifdef HAVE_WINSOCK
1097                         rd = readfds.fd_array[i];
1098 #else
1099                         if( ! FD_ISSET( i, &readfds ) ) {
1100                                 continue;
1101                         }
1102                         rd = i;
1103 #endif
1104
1105                         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1106                                 if ( rd == slap_listeners[l]->sl_sd ) {
1107                                         is_listener = 1;
1108                                         break;
1109                                 }
1110                         }
1111                         if ( is_listener ) {
1112                                 continue;
1113                         }
1114
1115                         Debug ( LDAP_DEBUG_CONNS,
1116                                 "daemon: read activity on %d\n", rd, 0, 0 );
1117
1118                         /*
1119                          * NOTE: it is possible that the connection was closed
1120                          * and that the stream is now inactive.
1121                          * connection_read() must valid the stream is still
1122                          * active.
1123                          */
1124
1125                         if ( connection_read( rd ) < 0 ) {
1126                                 slapd_close( rd );
1127                         }
1128                 }
1129                 ldap_pvt_thread_yield();
1130         }
1131
1132         if( slapd_shutdown > 0 ) {
1133                 Debug( LDAP_DEBUG_TRACE,
1134                         "daemon: shutdown requested and initiated.\n",
1135                         0, 0, 0 );
1136
1137         } else if ( slapd_shutdown < 0 ) {
1138 #ifdef HAVE_NT_SERVICE_MANAGER
1139                 if (slapd_shutdown == -1)
1140                     Debug( LDAP_DEBUG_TRACE,
1141                           "daemon: shutdown initiated by Service Manager.\n",
1142                           0, 0, 0);
1143                 else
1144 #endif
1145                 Debug( LDAP_DEBUG_TRACE,
1146                         "daemon: abnormal condition, shutdown initiated.\n",
1147                         0, 0, 0 );
1148         } else {
1149                 Debug( LDAP_DEBUG_TRACE,
1150                         "daemon: no active streams, shutdown initiated.\n",
1151                         0, 0, 0 );
1152         }
1153
1154         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1155                 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1156 #ifdef LDAP_PF_LOCAL
1157                         if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_UNIX ) {
1158                                 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1159                         }
1160 #endif /* LDAP_PF_LOCAL */
1161                         slapd_close( slap_listeners[l]->sl_sd );
1162                         break;
1163                 }
1164         }
1165
1166         ldap_pvt_thread_pool_destroy(connection_pool, 1);
1167
1168         ldap_pvt_thread_mutex_lock( &active_threads_mutex );
1169         Debug( LDAP_DEBUG_ANY,
1170             "slapd shutdown: waiting for %d threads to terminate\n",
1171             active_threads, 0, 0 );
1172         while ( active_threads > 0 ) {
1173                 ldap_pvt_thread_cond_wait(&active_threads_cond, &active_threads_mutex);
1174         }
1175         ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
1176
1177         return NULL;
1178 }
1179
1180
1181 int slapd_daemon( void )
1182 {
1183         int rc;
1184
1185         connections_init();
1186
1187 #define SLAPD_LISTENER_THREAD 1
1188 #if defined( SLAPD_LISTENER_THREAD ) || !defined(HAVE_PTHREADS)
1189
1190         /* listener as a separate THREAD */
1191         rc = ldap_pvt_thread_create( &listener_tid,
1192                 0, slapd_daemon_task, NULL );
1193
1194         if ( rc != 0 ) {
1195                 Debug( LDAP_DEBUG_ANY,
1196                     "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
1197                 return rc;
1198         }
1199
1200         /* wait for the listener thread to complete */
1201         ldap_pvt_thread_join( listener_tid, (void *) NULL );
1202 #else
1203         /* expermimental code */
1204         listener_tid = pthread_self();
1205         slapd_daemon_task( NULL );
1206 #endif
1207
1208         return 0;
1209
1210 }
1211
1212 #ifdef HAVE_WINSOCK2
1213 int sockinit(void)
1214 {
1215     WORD wVersionRequested;
1216         WSADATA wsaData;
1217         int err;
1218  
1219         wVersionRequested = MAKEWORD( 2, 0 );
1220  
1221         err = WSAStartup( wVersionRequested, &wsaData );
1222         if ( err != 0 ) {
1223                 /* Tell the user that we couldn't find a usable */
1224                 /* WinSock DLL.                                  */
1225                 return -1;
1226         }
1227  
1228         /* Confirm that the WinSock DLL supports 2.0.*/
1229         /* Note that if the DLL supports versions greater    */
1230         /* than 2.0 in addition to 2.0, it will still return */
1231         /* 2.0 in wVersion since that is the version we      */
1232         /* requested.                                        */
1233  
1234         if ( LOBYTE( wsaData.wVersion ) != 2 ||
1235                 HIBYTE( wsaData.wVersion ) != 0 )
1236         {
1237             /* Tell the user that we couldn't find a usable */
1238             /* WinSock DLL.                                  */
1239             WSACleanup();
1240             return -1; 
1241         }
1242
1243         /* The WinSock DLL is acceptable. Proceed. */
1244         return 0;
1245 }
1246
1247 int sockdestroy(void)
1248 {
1249         WSACleanup();
1250         return 0;
1251 }
1252
1253 #elif HAVE_WINSOCK
1254 static int sockinit(void)
1255 {
1256         WSADATA wsaData;
1257         if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
1258             return -1;
1259         }
1260         return 0;
1261 }
1262 static int sockdestroy(void)
1263 {
1264         WSACleanup();
1265         return 0;
1266 }
1267
1268 #else
1269 static int sockinit(void)
1270 {
1271         return 0;
1272 }
1273 static int sockdestroy(void)
1274 {
1275         return 0;
1276 }
1277 #endif
1278
1279 RETSIGTYPE
1280 slap_sig_shutdown( int sig )
1281 {
1282         Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
1283
1284         /*
1285          * If the NT Service Manager is controlling the server, we don't
1286          * want SIGBREAK to kill the server. For some strange reason,
1287          * SIGBREAK is generated when a user logs out.
1288          */
1289
1290 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
1291         if (is_NT_Service && sig == SIGBREAK)
1292             Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: SIGBREAK ignored.\n",
1293                   0, 0, 0);
1294         else
1295 #endif
1296         slapd_shutdown = sig;
1297
1298         WAKE_LISTENER(1);
1299
1300         /* reinstall self */
1301         (void) SIGNAL( sig, slap_sig_shutdown );
1302 }
1303
1304 RETSIGTYPE
1305 slap_sig_wake( int sig )
1306 {
1307         WAKE_LISTENER(1);
1308
1309         /* reinstall self */
1310         (void) SIGNAL( sig, slap_sig_wake );
1311 }