]> git.sur5r.net Git - openldap/blob - servers/slapd/daemon.c
5dc5aaaa1337f14e967ff82d82e0790fab0fb0d5
[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_is_ldaps_url( lud ) ) {
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_is_ldaps_url( lud );
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 #else
839 #  ifdef LDAP_INET6
840                         char    peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
841 #  else
842                         char    peername[sizeof("IP=255.255.255.255:65336")];
843 #  endif
844 #endif /* LDAP_PF_LOCAL */
845                         if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
846                                 continue;
847
848                         if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
849                                 continue;
850
851                         if ( (s = accept( slap_listeners[l]->sl_sd,
852                                 (struct sockaddr *) &from, &len )) == AC_SOCKET_INVALID )
853                         {
854                                 int err = sock_errno();
855                                 Debug( LDAP_DEBUG_ANY,
856                                     "daemon: accept(%ld) failed errno=%d (%s)\n",
857                                     (long) slap_listeners[l]->sl_sd, err,
858                                     sock_errstr(err) );
859                                 continue;
860                         }
861
862 #ifdef LDAP_DEBUG
863                         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
864
865                         /* newly accepted stream should not be in any of the FD SETS */
866
867                         assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
868                         assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
869                         assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
870
871                         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
872 #endif
873
874 #ifndef HAVE_WINSOCK
875                         /* make sure descriptor number isn't too great */
876                         if ( s >= dtblsize ) {
877                                 Debug( LDAP_DEBUG_ANY,
878                                         "daemon: %ld beyond descriptor table size %ld\n",
879                                         (long) s, (long) dtblsize, 0 );
880                                 slapd_close(s);
881                                 continue;
882                         }
883 #endif
884                    
885                         Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
886                                 (long) s, 0, 0 );
887
888                         len = sizeof(from);
889
890                         if ( getpeername( s, (struct sockaddr *) &from, &len ) != 0 ) {
891                                 int err = sock_errno();
892                                 Debug( LDAP_DEBUG_ANY,
893                                         "daemon: getpeername( %ld ) failed: errno=%d (%s)\n",
894                                         (long) s, err, sock_errstr(err) );
895                                 slapd_close(s);
896                                 continue;
897                         }
898
899 #if defined( LDAP_PF_LOCAL ) || defined( LDAP_INET6 )
900                         switch ( from.sa_addr.sa_family ) {
901 #  ifdef LDAP_PF_LOCAL
902                         case AF_UNIX:
903                                 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
904                                 break;
905 #endif /* LDAP_PF_LOCAL */
906
907 #  ifdef LDAP_INET6
908                         case AF_INET6: {
909                                 char addr[INET6_ADDRSTRLEN];
910                                 sprintf( peername, "IP=%s %d",
911                                         inet_ntop( AF_INET6,
912                                                 &from.sa_in6_addr.sin6_addr,
913                                             addr, sizeof addr) ? addr : "unknown",
914                                         (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
915                         } break;
916 #  endif /* LDAP_INET6 */
917
918                         case AF_INET:
919 #endif /* LDAP_PF_LOCAL || LDAP_INET6 */
920                         peeraddr = inet_ntoa( from.sin_addr );
921                         sprintf( peername, "IP=%s:%d",
922                                 peeraddr != NULL ? peeraddr : "unknown",
923                                 (unsigned) ntohs( from.sin_port ) );
924 #if defined( LDAP_PF_LOCAL ) || defined( LDAP_INET6 )
925                                 break;
926
927                         default:
928                                 slapd_close(s);
929                                 continue;
930                         }
931                         if ( from.sa_addr.sa_family != AF_UNIX ) {
932 #endif /* LDAP_PF_LOCAL || LDAP_INET6 */
933 #ifdef SLAPD_RLOOKUPS
934 #  ifdef LDAP_INET6
935                 if ( from.sa_addr.sa_family == AF_INET6 )
936                         hp = gethostbyaddr( (char *)
937                                 &(from.sa_in6_addr.sin6_addr),
938                                 sizeof(from.sa_in6_addr.sin6_addr),
939                                 AF_INET6 );
940                 else
941 #  endif LDAP_INET6
942                         hp = gethostbyaddr( (char *) &(from.sin_addr),
943                             sizeof(from.sin_addr), AF_INET );
944
945                         dnsname = hp ? ldap_pvt_str2lower( hp->h_name ) : NULL;
946 #else
947                         dnsname = NULL;
948 #endif /* SLAPD_RLOOKUPS */
949
950 #ifdef HAVE_TCPD
951                         if( !hosts_ctl("slapd",
952                                 dnsname != NULL ? dnsname : STRING_UNKNOWN,
953                                 peeraddr != NULL ? peeraddr : STRING_UNKNOWN,
954                                 STRING_UNKNOWN ))
955                         {
956                                 /* DENY ACCESS */
957                                 Statslog( LDAP_DEBUG_ANY,
958                                  "fd=%ld connection from %s (%s) denied.\n",
959                                         (long) s,
960                                         dnsname != NULL ? dnsname : "unknown",
961                                         peeraddr != NULL ? peeraddr : "unknown",
962                                   0, 0 );
963
964                                 slapd_close(s);
965                                 continue;
966                         }
967 #endif /* HAVE_TCPD */
968 #if defined( LDAP_PF_LOCAL ) || defined( LDAP_INET6 )
969                         }
970 #undef  sin_addr
971 #undef  sin_port
972 #endif /* LDAP_PF_LOCAL || LDAP_INET6 */
973
974                         if( (id = connection_init(s,
975                                 slap_listeners[l]->sl_url,
976                                 dnsname != NULL ? dnsname : "unknown",
977                                 peername,
978                                 slap_listeners[l]->sl_name,
979 #ifdef HAVE_TLS
980                                 slap_listeners[l]->sl_is_tls
981 #else
982                                 0
983 #endif
984                                 )) < 0 )
985                         {
986                                 Debug( LDAP_DEBUG_ANY,
987                                         "daemon: connection_init(%ld, %s, %s) failed.\n",
988                                         (long) s,
989                                         peername,
990                                         slap_listeners[l]->sl_name );
991                                 slapd_close(s);
992                                 continue;
993                         }
994
995                         Statslog( LDAP_DEBUG_STATS,
996                                 "daemon: conn=%ld fd=%ld connection from %s (%s) accepted.\n",
997                                 id, (long) s,
998                                 peername,
999                                 slap_listeners[l]->sl_name,
1000                                 0 );
1001
1002                         slapd_add( s );
1003                         continue;
1004                 }
1005
1006 #ifdef LDAP_DEBUG
1007                 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1008 #ifdef HAVE_WINSOCK
1009                 for ( i = 0; i < readfds.fd_count; i++ ) {
1010                         Debug( LDAP_DEBUG_CONNS, " %d%s",
1011                                 readfds.fd_array[i], "r", 0 );
1012                 }
1013                 for ( i = 0; i < writefds.fd_count; i++ ) {
1014                         Debug( LDAP_DEBUG_CONNS, " %d%s",
1015                                 writefds.fd_array[i], "w", 0 );
1016                 }
1017 #else
1018                 for ( i = 0; i < nfds; i++ ) {
1019                         int     r, w;
1020                         int     is_listener = 0;
1021
1022                         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1023                                 if ( i == slap_listeners[l]->sl_sd ) {
1024                                         is_listener = 1;
1025                                         break;
1026                                 }
1027                         }
1028                         if ( is_listener ) {
1029                                 continue;
1030                         }
1031                         r = FD_ISSET( i, &readfds );
1032                         w = FD_ISSET( i, &writefds );
1033                         if ( r || w ) {
1034                                 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1035                                     r ? "r" : "", w ? "w" : "" );
1036                         }
1037                 }
1038 #endif
1039                 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1040 #endif
1041
1042                 /* loop through the writers */
1043 #ifdef HAVE_WINSOCK
1044                 for ( i = 0; i < writefds.fd_count; i++ )
1045 #else
1046                 for ( i = 0; i < nfds; i++ )
1047 #endif
1048                 {
1049                         ber_socket_t wd;
1050                         int is_listener = 0;
1051 #ifdef HAVE_WINSOCK
1052                         wd = writefds.fd_array[i];
1053 #else
1054                         if( ! FD_ISSET( i, &writefds ) ) {
1055                                 continue;
1056                         }
1057                         wd = i;
1058 #endif
1059
1060                         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1061                                 if ( i == slap_listeners[l]->sl_sd ) {
1062                                         is_listener = 1;
1063                                         break;
1064                                 }
1065                         }
1066                         if ( is_listener ) {
1067                                 continue;
1068                         }
1069                         Debug( LDAP_DEBUG_CONNS,
1070                                 "daemon: write active on %d\n",
1071                                 wd, 0, 0 );
1072
1073                         /*
1074                          * NOTE: it is possible that the connection was closed
1075                          * and that the stream is now inactive.
1076                          * connection_write() must valid the stream is still
1077                          * active.
1078                          */
1079
1080                         if ( connection_write( wd ) < 0 ) {
1081                                 FD_CLR( (unsigned) wd, &readfds );
1082                                 slapd_close( wd );
1083                         }
1084                 }
1085
1086 #ifdef HAVE_WINSOCK
1087                 for ( i = 0; i < readfds.fd_count; i++ )
1088 #else
1089                 for ( i = 0; i < nfds; i++ )
1090 #endif
1091                 {
1092                         ber_socket_t rd;
1093                         int is_listener = 0;
1094
1095 #ifdef HAVE_WINSOCK
1096                         rd = readfds.fd_array[i];
1097 #else
1098                         if( ! FD_ISSET( i, &readfds ) ) {
1099                                 continue;
1100                         }
1101                         rd = i;
1102 #endif
1103
1104                         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1105                                 if ( rd == slap_listeners[l]->sl_sd ) {
1106                                         is_listener = 1;
1107                                         break;
1108                                 }
1109                         }
1110                         if ( is_listener ) {
1111                                 continue;
1112                         }
1113
1114                         Debug ( LDAP_DEBUG_CONNS,
1115                                 "daemon: read activity on %d\n", rd, 0, 0 );
1116
1117                         /*
1118                          * NOTE: it is possible that the connection was closed
1119                          * and that the stream is now inactive.
1120                          * connection_read() must valid the stream is still
1121                          * active.
1122                          */
1123
1124                         if ( connection_read( rd ) < 0 ) {
1125                                 slapd_close( rd );
1126                         }
1127                 }
1128                 ldap_pvt_thread_yield();
1129         }
1130
1131         if( slapd_shutdown > 0 ) {
1132                 Debug( LDAP_DEBUG_TRACE,
1133                         "daemon: shutdown requested and initiated.\n",
1134                         0, 0, 0 );
1135
1136         } else if ( slapd_shutdown < 0 ) {
1137 #ifdef HAVE_NT_SERVICE_MANAGER
1138                 if (slapd_shutdown == -1)
1139                     Debug( LDAP_DEBUG_TRACE,
1140                           "daemon: shutdown initiated by Service Manager.\n",
1141                           0, 0, 0);
1142                 else
1143 #endif
1144                 Debug( LDAP_DEBUG_TRACE,
1145                         "daemon: abnormal condition, shutdown initiated.\n",
1146                         0, 0, 0 );
1147         } else {
1148                 Debug( LDAP_DEBUG_TRACE,
1149                         "daemon: no active streams, shutdown initiated.\n",
1150                         0, 0, 0 );
1151         }
1152
1153         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1154                 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1155 #ifdef LDAP_PF_LOCAL
1156                         if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_UNIX ) {
1157                                 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1158                         }
1159 #endif /* LDAP_PF_LOCAL */
1160                         slapd_close( slap_listeners[l]->sl_sd );
1161                         break;
1162                 }
1163         }
1164
1165         ldap_pvt_thread_mutex_lock( &active_threads_mutex );
1166         Debug( LDAP_DEBUG_ANY,
1167             "slapd shutdown: waiting for %d threads to terminate\n",
1168             active_threads, 0, 0 );
1169         while ( active_threads > 0 ) {
1170                 ldap_pvt_thread_cond_wait(&active_threads_cond, &active_threads_mutex);
1171         }
1172         ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
1173
1174         return NULL;
1175 }
1176
1177
1178 int slapd_daemon( void )
1179 {
1180         int rc;
1181
1182         connections_init();
1183
1184 #define SLAPD_LISTENER_THREAD 1
1185 #if defined( SLAPD_LISTENER_THREAD ) || !defined(HAVE_PTHREADS)
1186
1187         /* listener as a separate THREAD */
1188         rc = ldap_pvt_thread_create( &listener_tid,
1189                 0, slapd_daemon_task, NULL );
1190
1191         if ( rc != 0 ) {
1192                 Debug( LDAP_DEBUG_ANY,
1193                     "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
1194                 return rc;
1195         }
1196
1197         /* wait for the listener thread to complete */
1198         ldap_pvt_thread_join( listener_tid, (void *) NULL );
1199 #else
1200         /* expermimental code */
1201         listener_tid = pthread_self();
1202         slapd_daemon_task( NULL );
1203 #endif
1204
1205         return 0;
1206
1207 }
1208
1209 #ifdef HAVE_WINSOCK2
1210 int sockinit(void)
1211 {
1212     WORD wVersionRequested;
1213         WSADATA wsaData;
1214         int err;
1215  
1216         wVersionRequested = MAKEWORD( 2, 0 );
1217  
1218         err = WSAStartup( wVersionRequested, &wsaData );
1219         if ( err != 0 ) {
1220                 /* Tell the user that we couldn't find a usable */
1221                 /* WinSock DLL.                                  */
1222                 return -1;
1223         }
1224  
1225         /* Confirm that the WinSock DLL supports 2.0.*/
1226         /* Note that if the DLL supports versions greater    */
1227         /* than 2.0 in addition to 2.0, it will still return */
1228         /* 2.0 in wVersion since that is the version we      */
1229         /* requested.                                        */
1230  
1231         if ( LOBYTE( wsaData.wVersion ) != 2 ||
1232                 HIBYTE( wsaData.wVersion ) != 0 )
1233         {
1234             /* Tell the user that we couldn't find a usable */
1235             /* WinSock DLL.                                  */
1236             WSACleanup();
1237             return -1; 
1238         }
1239
1240         /* The WinSock DLL is acceptable. Proceed. */
1241         return 0;
1242 }
1243
1244 int sockdestroy(void)
1245 {
1246         WSACleanup();
1247         return 0;
1248 }
1249
1250 #elif HAVE_WINSOCK
1251 static int sockinit(void)
1252 {
1253         WSADATA wsaData;
1254         if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
1255             return -1;
1256         }
1257         return 0;
1258 }
1259 static int sockdestroy(void)
1260 {
1261         WSACleanup();
1262         return 0;
1263 }
1264
1265 #else
1266 static int sockinit(void)
1267 {
1268         return 0;
1269 }
1270 static int sockdestroy(void)
1271 {
1272         return 0;
1273 }
1274 #endif
1275
1276 RETSIGTYPE
1277 slap_sig_shutdown( int sig )
1278 {
1279         Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
1280
1281         /*
1282          * If the NT Service Manager is controlling the server, we don't
1283          * want SIGBREAK to kill the server. For some strange reason,
1284          * SIGBREAK is generated when a user logs out.
1285          */
1286
1287 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
1288         if (is_NT_Service && sig == SIGBREAK)
1289             Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: SIGBREAK ignored.\n",
1290                   0, 0, 0);
1291         else
1292 #endif
1293         slapd_shutdown = sig;
1294
1295         WAKE_LISTENER(1);
1296
1297         /* reinstall self */
1298         (void) SIGNAL( sig, slap_sig_shutdown );
1299 }
1300
1301 RETSIGTYPE
1302 slap_sig_wake( int sig )
1303 {
1304         WAKE_LISTENER(1);
1305
1306         /* reinstall self */
1307         (void) SIGNAL( sig, slap_sig_wake );
1308 }