]> git.sur5r.net Git - openldap/blob - servers/slapd/daemon.c
small cleanup
[openldap] / servers / slapd / daemon.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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/socket.h>
14 #include <ac/string.h>
15 #include <ac/time.h>
16 #include <ac/unistd.h>
17
18 #include "ldap_pvt.h"
19 #include "ldap_pvt_thread.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 /* this should go in <ldap.h> as soon as it is accepted */
33 #define LDAPI_MOD_URLEXT                "x-mod"
34 #endif /* LDAP_PF_LOCAL */
35
36 /* globals */
37 time_t starttime;
38 ber_socket_t dtblsize;
39
40 Listener **slap_listeners = NULL;
41
42 #define SLAPD_LISTEN 10
43
44 static ber_socket_t wake_sds[2];
45
46 #ifdef NO_THREADS
47 static int waking;
48 #define WAKE_LISTENER(w) \
49 ((w && !waking) ? tcp_write( wake_sds[1], "0", 1 ), waking=1 : 0)
50 #else
51 #define WAKE_LISTENER(w) \
52 do { if (w) tcp_write( wake_sds[1], "0", 1 ); } while(0)
53 #endif
54
55 #ifndef HAVE_WINSOCK
56 static
57 #endif
58 volatile sig_atomic_t slapd_shutdown = 0, slapd_gentle_shutdown = 0;
59
60 static struct slap_daemon {
61         ldap_pvt_thread_mutex_t sd_mutex;
62
63         ber_socket_t sd_nactives;
64
65 #ifndef HAVE_WINSOCK
66         /* In winsock, accept() returns values higher than dtblsize
67                 so don't bother with this optimization */
68         int sd_nfds;
69 #endif
70
71         fd_set sd_actives;
72         fd_set sd_readers;
73         fd_set sd_writers;
74 } slap_daemon;
75
76
77
78 #ifdef HAVE_SLP
79 /*
80  * SLP related functions
81  */
82 #include <slp.h>
83
84 #define LDAP_SRVTYPE_PREFIX "service:ldap://"
85 #define LDAPS_SRVTYPE_PREFIX "service:ldaps://"
86 static char** slapd_srvurls = NULL;
87 static SLPHandle slapd_hslp = 0;
88
89 void slapd_slp_init( const char* urls ) {
90         int i;
91
92         slapd_srvurls = str2charray( urls, " " );
93
94         if( slapd_srvurls == NULL ) return;
95
96         /* find and expand INADDR_ANY URLs */
97         for( i=0; slapd_srvurls[i] != NULL; i++ ) {
98                 if( strcmp( slapd_srvurls[i], "ldap:///" ) == 0) {
99                         char *host = ldap_pvt_get_fqdn( NULL );
100                         if ( host != NULL ) {
101                                 slapd_srvurls[i] = (char *) realloc( slapd_srvurls[i],
102                                         strlen( host ) +
103                                         sizeof( LDAP_SRVTYPE_PREFIX ) );
104                                 strcpy( slap_strcopy(slapd_srvurls[i],
105                                         LDAP_SRVTYPE_PREFIX ), host );
106
107                                 ch_free( host );
108                         }
109
110                 } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0) {
111                         char *host = ldap_pvt_get_fqdn( NULL );
112                         if ( host != NULL ) {
113                                 slapd_srvurls[i] = (char *) realloc( slapd_srvurls[i],
114                                         strlen( host ) +
115                                         sizeof( LDAPS_SRVTYPE_PREFIX ) );
116                                 strcpy( slap_strcopy(slapd_srvurls[i],
117                                         LDAPS_SRVTYPE_PREFIX ), host );
118
119                                 ch_free( host );
120                         }
121                 }
122         }
123
124         /* open the SLP handle */
125         SLPOpen( "en", 0, &slapd_hslp );
126 }
127
128 void slapd_slp_deinit() {
129         if( slapd_srvurls == NULL ) return;
130
131         charray_free( slapd_srvurls );
132         slapd_srvurls = NULL;
133
134         /* close the SLP handle */
135         SLPClose( slapd_hslp );
136 }
137
138 void slapd_slp_regreport(
139         SLPHandle hslp,
140         SLPError errcode,
141         void* cookie )
142 {
143         /* empty report */
144 }
145
146 void slapd_slp_reg() {
147         int i;
148
149         for( i=0; slapd_srvurls[i] != NULL; i++ ) {
150                 if( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
151                                 sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
152                     strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
153                                 sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
154                 {
155                         SLPReg( slapd_hslp,
156                                 slapd_srvurls[i],
157                                 SLP_LIFETIME_MAXIMUM,
158                                 "ldap",
159                                 "",
160                                 1,
161                                 slapd_slp_regreport,
162                                 NULL );
163                 }
164         }
165 }
166
167 void slapd_slp_dereg() {
168         int i;
169
170         for( i=0; slapd_srvurls[i] != NULL; i++ ) {
171                 SLPDereg( slapd_hslp,
172                         slapd_srvurls[i],
173                         slapd_slp_regreport,
174                         NULL );
175         }
176 }
177 #endif /* HAVE_SLP */
178
179 /*
180  * Add a descriptor to daemon control
181  */
182 static void slapd_add(ber_socket_t s) {
183         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
184
185         assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
186         assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
187         assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
188
189 #ifndef HAVE_WINSOCK
190         if (s >= slap_daemon.sd_nfds) {
191                 slap_daemon.sd_nfds = s + 1;
192         }
193 #endif
194
195         slap_daemon.sd_nactives++;
196
197         FD_SET( s, &slap_daemon.sd_actives );
198         FD_SET( s, &slap_daemon.sd_readers );
199
200 #ifdef NEW_LOGGING
201         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
202                    "slapd_add: added %ld%s%s\n",
203                    (long)s,
204                    FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
205                    FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" ));
206 #else
207         Debug( LDAP_DEBUG_CONNS, "daemon: added %ld%s%s\n",
208                 (long) s,
209             FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
210                 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
211 #endif
212         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
213 }
214
215 /*
216  * Remove the descriptor from daemon control
217  */
218 void slapd_remove(ber_socket_t s, int wake) {
219         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
220
221         slap_daemon.sd_nactives--;
222
223 #ifdef NEW_LOGGING
224         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
225                    "slapd_remove: removing %ld%s%s\n",
226                    (long) s,
227                    FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
228                    FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : ""  ));
229 #else
230         Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
231                 (long) s,
232             FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
233                 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
234 #endif
235         FD_CLR( s, &slap_daemon.sd_actives );
236         FD_CLR( s, &slap_daemon.sd_readers );
237         FD_CLR( s, &slap_daemon.sd_writers );
238
239         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
240         WAKE_LISTENER(wake || slapd_gentle_shutdown == 2);
241 }
242
243 void slapd_clr_write(ber_socket_t s, int wake) {
244         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
245
246         assert( FD_ISSET( s, &slap_daemon.sd_actives) );
247         FD_CLR( s, &slap_daemon.sd_writers );
248
249         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
250         WAKE_LISTENER(wake);
251 }
252
253 void slapd_set_write(ber_socket_t s, int wake) {
254         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
255
256         assert( FD_ISSET( s, &slap_daemon.sd_actives) );
257         if (!FD_ISSET(s, &slap_daemon.sd_writers))
258             FD_SET( (unsigned) s, &slap_daemon.sd_writers );
259
260         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
261         WAKE_LISTENER(wake);
262 }
263
264 void slapd_clr_read(ber_socket_t s, int wake) {
265         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
266
267         assert( FD_ISSET( s, &slap_daemon.sd_actives) );
268         FD_CLR( s, &slap_daemon.sd_readers );
269
270         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
271         WAKE_LISTENER(wake);
272 }
273
274 void slapd_set_read(ber_socket_t s, int wake) {
275         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
276
277         assert( FD_ISSET( s, &slap_daemon.sd_actives) );
278         if (!FD_ISSET(s, &slap_daemon.sd_readers))
279             FD_SET( s, &slap_daemon.sd_readers );
280
281         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
282         WAKE_LISTENER(wake);
283 }
284
285 static void slapd_close(ber_socket_t s) {
286 #ifdef NEW_LOGGING
287         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
288                    "slapd_close: closing %ld\n", (long)s ));
289 #else
290         Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
291                 (long) s, 0, 0 );
292 #endif
293         tcp_close(s);
294 }
295
296 static void slap_free_listener_addresses(struct sockaddr **sal)
297 {
298         struct sockaddr **sap;
299
300         if (sal == NULL) {
301                 return;
302         }
303
304         for (sap = sal; *sap != NULL; sap++) {
305                 ch_free(*sap);
306         }
307
308         ch_free(sal);
309 }
310
311 #ifdef LDAP_PF_LOCAL
312 static int get_url_perms(
313         char    **exts,
314         mode_t  *perms,
315         int     *crit )
316 {
317         int     i;
318
319         assert( exts );
320         assert( perms );
321         assert( crit );
322
323         *crit = 0;
324         for ( i = 0; exts[ i ]; i++ ) {
325                 char    *type = exts[ i ];
326                 int     c = 0;
327
328                 if ( type[ 0 ] == '!' ) {
329                         c = 1;
330                         type++;
331                 }
332
333                 if ( strncasecmp( type, LDAPI_MOD_URLEXT "=", sizeof(LDAPI_MOD_URLEXT "=") - 1 ) == 0 ) {
334                         char    *value = type + sizeof(LDAPI_MOD_URLEXT "=") - 1;
335                         mode_t  p = 0;
336                         int     j;
337
338                         if ( strlen(value) != 3 ) {
339                                 return LDAP_OTHER;
340                         } 
341
342                         for ( j = 0; j < 3; j++ ) {
343                                 static mode_t   m[ 3 ] 
344                                         = { S_IRWXU, S_IRWXG, S_IRWXO };
345
346                                 switch ( value[ j ] ) {
347                                 case 'w':
348                                         p |= m[ j ];
349                                         break;
350                                 case '-':
351                                         break;
352                                 default:
353                                         return LDAP_OTHER;
354                                 }
355                         } 
356
357                         *crit = c;
358                         *perms = p;
359
360                         return LDAP_SUCCESS;
361                 }
362         }
363
364         return LDAP_OTHER;
365 }
366 #endif /* LDAP_PF_LOCAL */
367
368 /* port = 0 indicates AF_LOCAL */
369 static int slap_get_listener_addresses(
370         const char *host,
371         unsigned short port,
372         struct sockaddr ***sal)
373 {
374         struct sockaddr **sap;
375
376 #ifdef LDAP_PF_LOCAL
377         if ( port == 0 ) {
378                 *sal = ch_malloc(2 * sizeof(void *));
379                 if (*sal == NULL) {
380                         return -1;
381                 }
382
383                 sap = *sal;
384                 *sap = ch_malloc(sizeof(struct sockaddr_un));
385                 if (*sap == NULL)
386                         goto errexit;
387                 sap[1] = NULL;
388
389                 if ( strlen(host) >
390                      (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) ) {
391 #ifdef NEW_LOGGING
392                         LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
393                                    "slap_get_listener_addresses: domain socket path (%s) too long in URL\n",
394                                    host ));
395 #else
396                         Debug( LDAP_DEBUG_ANY,
397                                "daemon: domain socket path (%s) too long in URL",
398                                host, 0, 0);
399 #endif
400                         goto errexit;
401                 }
402
403                 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
404                 (*sap)->sa_family = AF_LOCAL;
405                 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
406         } else
407 #endif
408         {
409 #ifdef HAVE_GETADDRINFO
410                 struct addrinfo hints, *res, *sai;
411                 int n, err;
412                 char serv[7];
413
414                 memset( &hints, '\0', sizeof(hints) );
415                 hints.ai_flags = AI_PASSIVE;
416                 hints.ai_socktype = SOCK_STREAM;
417                 hints.ai_family = AF_UNSPEC;
418                 snprintf(serv, sizeof serv, "%d", port);
419
420                 if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
421 #ifdef NEW_LOGGING
422                         LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
423                                    "slap_get_listener_addresses: getaddrinfo failed: %s\n",
424                                    AC_GAI_STRERROR(err) ));
425 #else
426                         Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
427                                 AC_GAI_STRERROR(err), 0, 0);
428 #endif
429                         return -1;
430                 }
431
432                 sai = res;
433                 for (n=2; (sai = sai->ai_next) != NULL; n++) {
434                         /* EMPTY */ ;
435                 }
436                 *sal = ch_calloc(n, sizeof(void *));
437                 if (*sal == NULL) {
438                         return -1;
439                 }
440
441                 sap = *sal;
442                 *sap = NULL;
443
444                 for ( sai=res; sai; sai=sai->ai_next ) {
445                         if( sai->ai_addr == NULL ) {
446 #ifdef NEW_LOGGING
447                                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
448                                         "slap_get_listener_addresses: "
449                                         "getaddrinfo ai_addr is NULL?\n" ));
450 #else
451                                 Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
452                                         "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
453 #endif
454                                 freeaddrinfo(res);
455                                 goto errexit;
456                         }
457
458                         switch (sai->ai_family) {
459 #  ifdef LDAP_PF_INET6
460                         case AF_INET6:
461                                 *sap = ch_malloc(sizeof(struct sockaddr_in6));
462                                 if (*sap == NULL) {
463                                         freeaddrinfo(res);
464                                         goto errexit;
465                                 }
466                                 *(struct sockaddr_in6 *)*sap =
467                                         *((struct sockaddr_in6 *)sai->ai_addr);
468                                 break;
469 #  endif
470                         case AF_INET:
471                                 *sap = ch_malloc(sizeof(struct sockaddr_in));
472                                 if (*sap == NULL) {
473                                         freeaddrinfo(res);
474                                         goto errexit;
475                                 }
476                                 *(struct sockaddr_in *)*sap =
477                                         *((struct sockaddr_in *)sai->ai_addr);
478                                 break;
479                         default:
480                                 *sap = NULL;
481                                 break;
482                         }
483
484                         if (*sap != NULL) {
485                                 (*sap)->sa_family = sai->ai_family;
486                                 sap++;
487                                 *sap = NULL;
488                         }
489                 }
490
491                 freeaddrinfo(res);
492 #else
493                 int i, n = 1;
494                 struct in_addr in;
495                 struct hostent *he = NULL;
496
497                 if ( host == NULL ) {
498                         in.s_addr = htonl(INADDR_ANY);
499
500                 } else if ( !inet_aton( host, &in ) ) {
501                         he = gethostbyname( host );
502                         if( he == NULL ) {
503 #ifdef NEW_LOGGING
504                                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
505                                            "slap_get_listener_addresses: invalid host %s\n",
506                                            host ));
507 #else
508                                 Debug( LDAP_DEBUG_ANY,
509                                        "daemon: invalid host %s", host, 0, 0);
510 #endif
511                                 return -1;
512                         }
513                         for (n = 0; he->h_addr_list[n]; n++) ;
514                 }
515
516                 *sal = ch_malloc((n+1) * sizeof(void *));
517                 if (*sal == NULL) {
518                         return -1;
519                 }
520
521                 sap = *sal;
522                 for ( i = 0; i<n; i++ ) {
523                         sap[i] = ch_malloc(sizeof(struct sockaddr_in));
524                         if (*sap == NULL) {
525                                 goto errexit;
526                         }
527                         (void)memset( (void *)sap[i], '\0', sizeof(struct sockaddr_in) );
528                         sap[i]->sa_family = AF_INET;
529                         ((struct sockaddr_in *)sap[i])->sin_port = htons(port);
530                         if (he) {
531                                 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr, he->h_addr_list[i], sizeof(struct in_addr) );
532                         } else {
533                                 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr, &in, sizeof(struct in_addr) );
534                         }
535                 }
536                 sap[i] = NULL;
537 #endif
538         }
539
540         return 0;
541
542 errexit:
543         slap_free_listener_addresses(*sal);
544         return -1;
545 }
546
547 static int slap_open_listener(
548         const char* url,
549         int *listeners,
550         int *cur
551         )
552 {
553         int     num, tmp, rc;
554         Listener l;
555         Listener *li;
556         LDAPURLDesc *lud;
557         unsigned short port;
558         int err, addrlen = 0;
559         struct sockaddr **sal, **psal;
560         int socktype = SOCK_STREAM;     /* default to COTS */
561 #ifdef LDAP_PF_LOCAL
562         mode_t  perms = S_IRWXU;
563         int     crit = 1;
564 #endif
565
566         rc = ldap_url_parse( url, &lud );
567
568         if( rc != LDAP_URL_SUCCESS ) {
569 #ifdef NEW_LOGGING
570                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
571                            "slap_open_listener: listen URL \"%s\" parse error %d\n",
572                            url, rc ));
573 #else
574                 Debug( LDAP_DEBUG_ANY,
575                         "daemon: listen URL \"%s\" parse error=%d\n",
576                         url, rc, 0 );
577 #endif
578                 return rc;
579         }
580
581 #ifndef HAVE_TLS
582         if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
583 #ifdef NEW_LOGGING
584                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
585                            "slap_open_listener: TLS is not supported (%s)\n",
586                            url ));
587 #else
588                 Debug( LDAP_DEBUG_ANY,
589                         "daemon: TLS not supported (%s)\n",
590                         url, 0, 0 );
591 #endif
592                 ldap_free_urldesc( lud );
593                 return -1;
594         }
595
596         if(! lud->lud_port ) {
597                 lud->lud_port = LDAP_PORT;
598         }
599
600 #else
601         l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
602
603         if(! lud->lud_port ) {
604                 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
605         }
606 #endif
607
608         port = (unsigned short) lud->lud_port;
609
610         tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme);
611         if ( tmp == LDAP_PROTO_IPC ) {
612 #ifdef LDAP_PF_LOCAL
613                 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
614                         err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
615                 } else {
616                         err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
617                 }
618
619                 if ( lud->lud_exts ) {
620                         err = get_url_perms( lud->lud_exts, &perms, &crit );
621                 }
622 #else
623
624 #ifdef NEW_LOGGING
625                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
626                            "slap_open_listener: URL scheme is not supported: %s\n",
627                            url ));
628 #else
629                 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
630                         url, 0, 0);
631 #endif
632                 ldap_free_urldesc( lud );
633                 return -1;
634 #endif
635         } else {
636 #ifdef LDAP_CONNECTIONLESS
637                 l.sl_is_udp = ( tmp == LDAP_PROTO_UDP );
638 #endif
639                 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
640                         || strcmp(lud->lud_host, "*") == 0 )
641                 {
642                         err = slap_get_listener_addresses(NULL, port, &sal);
643                 } else {
644                         err = slap_get_listener_addresses(lud->lud_host, port, &sal);
645                 }
646         }
647
648         ldap_free_urldesc( lud );
649         if ( err ) {
650                 return -1;
651         }
652
653         /* If we got more than one address returned, we need to make space
654          * for it in the slap_listeners array.
655          */
656         for ( num=0; sal[num]; num++ );
657         if ( num > 1 ) {
658                 *listeners += num-1;
659                 slap_listeners = ch_realloc( slap_listeners, (*listeners + 1) * sizeof(Listener *) );
660         }
661
662         psal = sal;
663         while ( *sal != NULL ) {
664                 switch( (*sal)->sa_family ) {
665                 case AF_INET:
666 #ifdef LDAP_PF_INET6
667                 case AF_INET6:
668 #endif
669 #ifdef LDAP_PF_LOCAL
670                 case AF_LOCAL:
671 #endif
672                         break;
673                 default:
674                         sal++;
675                         continue;
676                 }
677 #ifdef LDAP_CONNECTIONLESS
678                 if (l.sl_is_udp)
679                     socktype = SOCK_DGRAM;
680 #endif
681                 l.sl_sd = socket( (*sal)->sa_family, socktype, 0);
682                 if ( l.sl_sd == AC_SOCKET_INVALID ) {
683                         int err = sock_errno();
684 #ifdef NEW_LOGGING
685                         LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
686                                    "slap_open_listener: socket() failed errno=%d (%s)\n",
687                                    err, sock_errstr(err) ));
688 #else
689                         Debug( LDAP_DEBUG_ANY,
690                                 "daemon: socket() failed errno=%d (%s)\n", err,
691                                 sock_errstr(err), 0 );
692 #endif
693                         sal++;
694                         continue;
695                 }
696 #ifndef HAVE_WINSOCK
697                 if ( l.sl_sd >= dtblsize ) {
698 #ifdef NEW_LOGGING
699                         LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
700                                    "slap_open_listener: listener descriptor %ld is too great %ld\n",
701                                    (long)l.sl_sd, (long)dtblsize ));
702 #else
703                         Debug( LDAP_DEBUG_ANY,
704                                "daemon: listener descriptor %ld is too great %ld\n",
705                                (long) l.sl_sd, (long) dtblsize, 0 );
706 #endif
707                         tcp_close( l.sl_sd );
708                         sal++;
709                         continue;
710                 }
711 #endif
712 #ifdef LDAP_PF_LOCAL
713                 if ( (*sal)->sa_family == AF_LOCAL ) {
714                         unlink ( ((struct sockaddr_un *)*sal)->sun_path );
715                 } else
716 #endif
717                 {
718 #ifdef SO_REUSEADDR
719                         /* enable address reuse */
720                         tmp = 1;
721                         rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
722                                          (char *) &tmp, sizeof(tmp) );
723                         if ( rc == AC_SOCKET_ERROR ) {
724                                 int err = sock_errno();
725 #ifdef NEW_LOGGING
726                                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
727                                            "slap_open_listener: setsockopt( %ld, SO_REUSEADDR ) failed errno %d (%s)\n",
728                                            (long)l.sl_sd, err, sock_errstr(err) ));
729 #else
730                                 Debug( LDAP_DEBUG_ANY,
731                                        "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
732                                        (long) l.sl_sd, err, sock_errstr(err) );
733 #endif
734                         }
735 #endif
736                 }
737
738                 switch( (*sal)->sa_family ) {
739                 case AF_INET:
740                         addrlen = sizeof(struct sockaddr_in);
741                         break;
742 #ifdef LDAP_PF_INET6
743                 case AF_INET6:
744                         addrlen = sizeof(struct sockaddr_in6);
745                         break;
746 #endif
747 #ifdef LDAP_PF_LOCAL
748                 case AF_LOCAL:
749                         addrlen = sizeof(struct sockaddr_un);
750                         break;
751 #endif
752                 }
753
754                 if (bind(l.sl_sd, *sal, addrlen)) {
755                         err = sock_errno();
756 #ifdef NEW_LOGGING
757                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
758                            "slap_open_listener: bind(%ld) failed errno=%d (%s)\n",
759                            (long)l.sl_sd, err, sock_errstr(err) ));
760 #else
761                 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
762                        (long) l.sl_sd, err, sock_errstr(err) );
763 #endif
764                         tcp_close( l.sl_sd );
765                         sal++;
766                         continue;
767                 }
768
769         switch ( (*sal)->sa_family ) {
770 #ifdef LDAP_PF_LOCAL
771         case AF_LOCAL: {
772                 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
773                 if ( chmod( addr, perms ) < 0 && crit ) {
774                         int err = sock_errno();
775 #ifdef NEW_LOGGING
776                         LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
777                                    "slap_open_listener: fchmod(%ld) failed errno=%d (%s)\n",
778                                    (long)l.sl_sd, err, sock_errstr(err) ));
779 #else
780                         Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
781                                (long) l.sl_sd, err, sock_errstr(err) );
782 #endif
783                         tcp_close( l.sl_sd );
784                         slap_free_listener_addresses(psal);
785                         return -1;
786                 }
787                 l.sl_name = ch_malloc( strlen(addr) + sizeof("PATH=") );
788                 sprintf( l.sl_name, "PATH=%s", addr );
789         } break;
790 #endif /* LDAP_PF_LOCAL */
791
792         case AF_INET: {
793                 char *s;
794 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
795                 char addr[INET_ADDRSTRLEN];
796                 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
797                            addr, sizeof(addr) );
798                 s = addr;
799 #else
800                 s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr );
801 #endif
802                 port = ntohs( ((struct sockaddr_in *)*sal) ->sin_port );
803                 l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65535") );
804                 sprintf( l.sl_name, "IP=%s:%d",
805                          s != NULL ? s : "unknown" , port );
806         } break;
807
808 #ifdef LDAP_PF_INET6
809         case AF_INET6: {
810                 char addr[INET6_ADDRSTRLEN];
811                 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
812                            addr, sizeof addr);
813                 port = ntohs( ((struct sockaddr_in6 *)*sal)->sin6_port );
814                 l.sl_name = ch_malloc( strlen(addr) + sizeof("IP= 65535") );
815                 sprintf( l.sl_name, "IP=%s %d", addr, port );
816         } break;
817 #endif /* LDAP_PF_INET6 */
818
819         default:
820 #ifdef NEW_LOGGING
821                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
822                            "slap_open_listener: unsupported address family (%d)\n",
823                            (int)(*sal)->sa_family ));
824 #else
825                 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
826                         (int) (*sal)->sa_family, 0, 0 );
827 #endif
828                 break;
829         }
830
831         AC_MEMCPY(&l.sl_sa, *sal, addrlen);
832         l.sl_url = ch_strdup( url );
833         li = ch_malloc( sizeof( Listener ) );
834         *li = l;
835         slap_listeners[*cur] = li;
836         (*cur)++;
837         sal++;
838
839         } /* while ( *sal != NULL ) */
840
841         slap_free_listener_addresses(psal);
842
843
844 #ifdef NEW_LOGGING
845         LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
846                    "slap_open_listener: daemon initialized %s\n", l.sl_url ));
847 #else
848         Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
849                 l.sl_url, 0, 0 );
850 #endif
851         return 0;
852 }
853
854 static int sockinit(void);
855 static int sockdestroy(void);
856
857 int slapd_daemon_init( const char *urls )
858 {
859         int i, j, n, rc;
860         char **u;
861
862 #ifdef NEW_LOGGING
863         LDAP_LOG(( "connection", LDAP_LEVEL_ARGS,
864                    "slapd_daemon_init: %s\n",
865                    urls ? urls : "<null>" ));
866 #else
867         Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
868                 urls ? urls : "<null>", 0, 0 );
869 #endif
870         if( (rc = sockinit()) != 0 ) {
871                 return rc;
872         }
873
874 #ifdef HAVE_SYSCONF
875         dtblsize = sysconf( _SC_OPEN_MAX );
876 #elif HAVE_GETDTABLESIZE
877         dtblsize = getdtablesize();
878 #else
879         dtblsize = FD_SETSIZE;
880 #endif
881
882 #ifdef FD_SETSIZE
883         if(dtblsize > FD_SETSIZE) {
884                 dtblsize = FD_SETSIZE;
885         }
886 #endif  /* !FD_SETSIZE */
887
888         /* open a pipe (or something equivalent connected to itself).
889          * we write a byte on this fd whenever we catch a signal. The main
890          * loop will be select'ing on this socket, and will wake up when
891          * this byte arrives.
892          */
893         if( (rc = lutil_pair( wake_sds )) < 0 ) {
894 #ifdef NEW_LOGGING
895                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
896                            "slap_daemon_init: lutil_pair() failed rc=%d\n", rc ));
897 #else
898                 Debug( LDAP_DEBUG_ANY,
899                         "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
900 #endif
901                 return rc;
902         }
903
904         FD_ZERO( &slap_daemon.sd_readers );
905         FD_ZERO( &slap_daemon.sd_writers );
906
907         if( urls == NULL ) {
908                 urls = "ldap:///";
909         }
910
911         u = str2charray( urls, " " );
912
913         if( u == NULL || u[0] == NULL ) {
914 #ifdef NEW_LOGGING
915                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
916                            "slap_daemon_init: no urls (%s) provided.\n", urls ));
917 #else
918                 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
919                         urls, 0, 0 );
920 #endif
921                 return -1;
922         }
923
924         for( i=0; u[i] != NULL; i++ ) {
925 #ifdef NEW_LOGGING
926                 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
927                            "slap_daemon_init: listen on %s\n.", u[i] ));
928 #else
929                 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
930                         u[i], 0, 0 );
931 #endif
932         }
933
934         if( i == 0 ) {
935 #ifdef NEW_LOGGING
936                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
937                            "slap_daemon_init: no listeners to open (%s)\n", urls ));
938 #else
939                 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
940                         urls, 0, 0 );
941 #endif
942                 charray_free( u );
943                 return -1;
944         }
945
946 #ifdef NEW_LOGGING
947         LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
948                    "slap_daemon_init: %d listeners to open...\n", i ));
949 #else
950         Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
951                 i, 0, 0 );
952 #endif
953         slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
954
955         for(n = 0, j = 0; u[n]; n++ ) {
956                 if ( slap_open_listener( u[n], &i, &j ) ) {
957                         charray_free( u );
958                         return -1;
959                 }
960         }
961         slap_listeners[j] = NULL;
962
963 #ifdef NEW_LOGGING
964         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
965                    "slap_daemon_init: %d listeners opened\n", i ));
966 #else
967         Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
968                 i, 0, 0 );
969 #endif
970
971 #ifdef HAVE_SLP
972         slapd_slp_init( urls );
973         slapd_slp_reg();
974 #endif
975
976         charray_free( u );
977         ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
978         return !i;
979 }
980
981
982 int
983 slapd_daemon_destroy(void)
984 {
985         connections_destroy();
986         tcp_close( wake_sds[1] );
987         tcp_close( wake_sds[0] );
988         sockdestroy();
989
990 #ifdef HAVE_SLP
991         slapd_slp_dereg();
992         slapd_slp_deinit();
993 #endif
994
995         return 0;
996 }
997
998
999 static void
1000 close_listeners(
1001         int remove
1002 )
1003 {
1004         int l;
1005
1006         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1007                 if ( remove )
1008                         slapd_remove( slap_listeners[l]->sl_sd, 0 );
1009                 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1010 #ifdef LDAP_PF_LOCAL
1011                         if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1012                                 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1013                         }
1014 #endif /* LDAP_PF_LOCAL */
1015                         slapd_close( slap_listeners[l]->sl_sd );
1016                 }
1017                 if ( slap_listeners[l]->sl_url )
1018                         free ( slap_listeners[l]->sl_url );
1019                 if ( slap_listeners[l]->sl_name )
1020                         free ( slap_listeners[l]->sl_name );
1021                 free ( slap_listeners[l] );
1022                 slap_listeners[l] = NULL;
1023         }
1024 }
1025
1026
1027 static void *
1028 slapd_daemon_task(
1029         void *ptr
1030 )
1031 {
1032         int l;
1033         time_t  last_idle_check = 0;
1034         time( &starttime );
1035
1036         if ( global_idletimeout > 0 ) {
1037                 last_idle_check = slap_get_time();
1038         }
1039         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1040                 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1041                         continue;
1042 #ifdef LDAP_CONNECTIONLESS
1043                 /* Since this is connectionless, the data port is the
1044                  * listening port. The listen() and accept() calls
1045                  * are unnecessary.
1046                  */
1047                 if ( slap_listeners[l]->sl_is_udp )
1048                 {
1049                         slapd_add( slap_listeners[l]->sl_sd );
1050                         continue;
1051                 }
1052 #endif
1053
1054                 if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN ) == -1 ) {
1055                         int err = sock_errno();
1056 #ifdef NEW_LOGGING
1057                         LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1058                                    "slapd_daemon_task: listen( %s, 5 ) failed errno=%d (%s)\n",
1059                                    slap_listeners[l]->sl_url, err, sock_errstr(err) ));
1060 #else
1061                         Debug( LDAP_DEBUG_ANY,
1062                                 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
1063                                         slap_listeners[l]->sl_url, err,
1064                                         sock_errstr(err) );
1065 #endif
1066                         return( (void*)-1 );
1067                 }
1068
1069                 slapd_add( slap_listeners[l]->sl_sd );
1070         }
1071
1072 #ifdef HAVE_NT_SERVICE_MANAGER
1073         if ( started_event != NULL ) {
1074                 ldap_pvt_thread_cond_signal( &started_event );
1075         }
1076 #endif
1077         /* initialization complete. Here comes the loop. */
1078
1079         while ( !slapd_shutdown ) {
1080                 ber_socket_t i;
1081                 int ns;
1082                 int at;
1083                 ber_socket_t nfds;
1084 #define SLAPD_EBADF_LIMIT 16
1085                 int ebadf = 0;
1086                 int emfile = 0;
1087
1088 #define SLAPD_IDLE_CHECK_LIMIT 4
1089                 time_t  now;
1090
1091
1092                 fd_set                  readfds;
1093                 fd_set                  writefds;
1094                 Sockaddr                from;
1095
1096 #if defined(SLAPD_RLOOKUPS)
1097                 struct hostent          *hp;
1098 #endif
1099                 struct timeval          zero;
1100                 struct timeval          *tvp;
1101
1102                 if( emfile ) {
1103                         now = slap_get_time();
1104                         connections_timeout_idle( now );
1105                 }
1106                 else if ( global_idletimeout > 0 ) {
1107                         now = slap_get_time();
1108                         if ( difftime( last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 ) {
1109                                 connections_timeout_idle( now );
1110                         }
1111                 }
1112
1113 #ifdef SIGHUP
1114                 if( slapd_gentle_shutdown ) {
1115                         ber_socket_t active;
1116
1117                         if( slapd_gentle_shutdown == 1 ) {
1118                                 Debug( LDAP_DEBUG_ANY, "slapd gentle shutdown\n", 0, 0, 0 );
1119                                 close_listeners( 1 );
1120                                 global_restrictops |= SLAP_RESTRICT_OP_WRITES;
1121                                 slapd_gentle_shutdown = 2;
1122                         }
1123
1124                         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1125                         active = slap_daemon.sd_nactives;
1126                         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1127                         if( active == 0 ) {
1128                                 slapd_shutdown = 2;
1129                                 break;
1130                         }
1131                 }
1132 #endif
1133
1134                 FD_ZERO( &writefds );
1135                 FD_ZERO( &readfds );
1136
1137                 zero.tv_sec = 0;
1138                 zero.tv_usec = 0;
1139
1140                 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1141
1142 #ifdef FD_SET_MANUAL_COPY
1143                 for( s = 0; s < nfds; s++ ) {
1144                         if(FD_ISSET( &slap_sd_readers, s )) {
1145                                 FD_SET( s, &readfds );
1146                         }
1147                         if(FD_ISSET( &slap_sd_writers, s )) {
1148                                 FD_SET( s, &writefds );
1149                         }
1150                 }
1151 #else
1152                 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
1153                 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
1154 #endif
1155                 assert(!FD_ISSET(wake_sds[0], &readfds));
1156                 FD_SET( wake_sds[0], &readfds );
1157
1158                 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1159                         if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1160                                 continue;
1161                         if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
1162                             FD_SET( slap_listeners[l]->sl_sd, &readfds );
1163                 }
1164
1165 #ifndef HAVE_WINSOCK
1166                 nfds = slap_daemon.sd_nfds;
1167 #else
1168                 nfds = dtblsize;
1169 #endif
1170
1171                 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1172
1173                 at = ldap_pvt_thread_pool_backload(&connection_pool);
1174
1175 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
1176                 tvp = NULL;
1177 #else
1178                 tvp = at ? &zero : NULL;
1179 #endif
1180
1181                 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1182                         if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1183                                 continue;
1184
1185 #ifdef NEW_LOGGING
1186                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1187                                    "slapd_daemon_task: select: listen=%d active_threads=%d tvp=%s\n",
1188                                    slap_listeners[l]->sl_sd, at, tvp == NULL ? "NULL" : "zero" ));
1189 #else
1190                         Debug( LDAP_DEBUG_CONNS,
1191                                 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
1192                                         slap_listeners[l]->sl_sd, at,
1193                                         tvp == NULL ? "NULL" : "zero" );
1194 #endif
1195                 }
1196
1197                 switch(ns = select( nfds, &readfds,
1198 #ifdef HAVE_WINSOCK
1199                         /* don't pass empty fd_set */
1200                         ( writefds.fd_count > 0 ? &writefds : NULL ),
1201 #else
1202                         &writefds,
1203 #endif
1204                         NULL, tvp ))
1205                 {
1206                 case -1: {      /* failure - try again */
1207                                 int err = sock_errno();
1208
1209                                 if( err == EBADF
1210 #ifdef WSAENOTSOCK
1211                                         /* you'd think this would be EBADF */
1212                                         || err == WSAENOTSOCK
1213 #endif
1214                                 ) {
1215                                         if (++ebadf < SLAPD_EBADF_LIMIT)
1216                                                 continue;
1217                                 }
1218
1219                                 if( err != EINTR ) {
1220 #ifdef NEW_LOGGING
1221                                         LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1222                                                    "slapd_daemon_task: select failed (%d): %s\n",
1223                                                    err, sock_errstr(err) ));
1224 #else
1225                                         Debug( LDAP_DEBUG_CONNS,
1226                                                 "daemon: select failed (%d): %s\n",
1227                                                 err, sock_errstr(err), 0 );
1228 #endif
1229                                         slapd_shutdown = 2;
1230                                 }
1231                         }
1232                         continue;
1233
1234                 case 0:         /* timeout - let threads run */
1235                         ebadf = 0;
1236 #ifdef NEW_LOGGING
1237                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1238                                    "slapd_daemon_task: select timeout - yielding\n" ));
1239 #else
1240                         Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
1241                             0, 0, 0 );
1242 #endif
1243                         ldap_pvt_thread_yield();
1244                         continue;
1245
1246                 default:        /* something happened - deal with it */
1247                         if( slapd_shutdown ) continue;
1248
1249                         ebadf = 0;
1250 #ifdef NEW_LOGGING
1251                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1252                                    "slapd_daemon_task: activity on %d descriptors\n", ns ));
1253 #else
1254                         Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
1255                                 ns, 0, 0 );
1256 #endif
1257                         /* FALL THRU */
1258                 }
1259
1260                 if( FD_ISSET( wake_sds[0], &readfds ) ) {
1261                         char c[BUFSIZ];
1262                         tcp_read( wake_sds[0], c, sizeof(c) );
1263 #ifdef NO_THREADS
1264                         waking = 0;
1265 #endif
1266                         continue;
1267                 }
1268
1269                 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1270                         ber_socket_t s;
1271                         socklen_t len = sizeof(from);
1272                         long id;
1273                         slap_ssf_t ssf = 0;
1274                         char *authid = NULL;
1275
1276                         char    *dnsname = NULL;
1277                         char    *peeraddr;
1278 #ifdef LDAP_PF_LOCAL
1279                         char    peername[MAXPATHLEN + sizeof("PATH=")];
1280 #elif defined(LDAP_PF_INET6)
1281                         char    peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
1282 #else
1283                         char    peername[sizeof("IP=255.255.255.255:65336")];
1284 #endif /* LDAP_PF_LOCAL */
1285
1286                         peername[0] = '\0';
1287
1288                         if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1289                                 continue;
1290
1291                         if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
1292                                 continue;
1293
1294 #ifdef LDAP_CONNECTIONLESS
1295                         if ( slap_listeners[l]->sl_is_udp )
1296                         {
1297                         /* The first time we receive a query, we set this
1298                          * up as a "connection". It remains open for the life
1299                          * of the slapd.
1300                          */
1301                                 if ( slap_listeners[l]->sl_is_udp < 2 )
1302                                 {
1303                                     id = connection_init(
1304                                         slap_listeners[l]->sl_sd,
1305                                         slap_listeners[l]->sl_url, "", "",
1306                                         slap_listeners[l]->sl_name,
1307                                         2, ssf, authid );
1308                                     slap_listeners[l]->sl_is_udp++;
1309                                 }
1310                                 continue;
1311                         }
1312 #endif
1313
1314                         s = accept( slap_listeners[l]->sl_sd,
1315                                 (struct sockaddr *) &from, &len );
1316                         if ( s == AC_SOCKET_INVALID ) {
1317                                 int err = sock_errno();
1318
1319 #ifdef EMFILE
1320                                 if( err == EMFILE ) {
1321                                         emfile++;
1322                                 } else
1323 #endif
1324 #ifdef ENFILE
1325                                 if( err == ENFILE ) {
1326                                         emfile++;
1327                                 } else 
1328 #endif
1329                                 {
1330                                         emfile=0;
1331                                 }
1332
1333                                 if( emfile < 3 ) {
1334 #ifdef NEW_LOGGING
1335                                         LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1336                                                 "slapd_daemon_task: accept(%ld) failed errno=%d (%s)\n",
1337                                                 (long)slap_listeners[l]->sl_sd, err, sock_errstr(err) ));
1338 #else
1339                                         Debug( LDAP_DEBUG_ANY,
1340                                             "daemon: accept(%ld) failed errno=%d (%s)\n",
1341                                             (long) slap_listeners[l]->sl_sd, err,
1342                                             sock_errstr(err) );
1343 #endif
1344                                 } else {
1345                                         /* prevent busy loop */
1346 #  ifdef HAVE_USLEEP
1347                                         if( emfile % 4 == 3 ) usleep( 250 );
1348 #  else
1349                                         if( emfile % 8 == 7 ) sleep( 1 );
1350 #  endif
1351                                 }
1352
1353                                 ldap_pvt_thread_yield();
1354                                 continue;
1355                         }
1356                         emfile = 0;
1357
1358 #ifndef HAVE_WINSOCK
1359                         /* make sure descriptor number isn't too great */
1360                         if ( s >= dtblsize ) {
1361 #ifdef NEW_LOGGING
1362                                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1363                                    "slapd_daemon_task: %ld beyond descriptor table size %ld\n",
1364                                    (long)s, (long)dtblsize ));
1365 #else
1366                                 Debug( LDAP_DEBUG_ANY,
1367                                         "daemon: %ld beyond descriptor table size %ld\n",
1368                                         (long) s, (long) dtblsize, 0 );
1369 #endif
1370
1371                                 slapd_close(s);
1372                                 ldap_pvt_thread_yield();
1373                                 continue;
1374                         }
1375 #endif
1376
1377 #ifdef LDAP_DEBUG
1378                         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1379
1380                         /* newly accepted stream should not be in any of the FD SETS */
1381                         assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
1382                         assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
1383                         assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
1384
1385                         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1386 #endif
1387
1388 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1389 #ifdef LDAP_PF_LOCAL
1390                         /* for IPv4 and IPv6 sockets only */
1391                         if ( from.sa_addr.sa_family != AF_LOCAL )
1392 #endif /* LDAP_PF_LOCAL */
1393                         {
1394                                 int rc;
1395                                 int tmp;
1396 #ifdef SO_KEEPALIVE
1397                                 /* enable keep alives */
1398                                 tmp = 1;
1399                                 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1400                                         (char *) &tmp, sizeof(tmp) );
1401                                 if ( rc == AC_SOCKET_ERROR ) {
1402                                         int err = sock_errno();
1403 #ifdef NEW_LOGGING
1404                                         LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1405                                                    "slapd_daemon_task: setsockopt( %ld, SO_KEEPALIVE) failed errno=%d (%s)\n",
1406                                                    (long)s, err, sock_errstr(err) ));
1407 #else
1408                                         Debug( LDAP_DEBUG_ANY,
1409                                                 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1410                                                 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1411 #endif
1412                                 }
1413 #endif
1414 #ifdef TCP_NODELAY
1415                                 /* enable no delay */
1416                                 tmp = 1;
1417                                 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1418                                         (char *)&tmp, sizeof(tmp) );
1419                                 if ( rc == AC_SOCKET_ERROR ) {
1420                                         int err = sock_errno();
1421 #ifdef NEW_LOGGING
1422                                         LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1423                                                    "slapd_daemon_task: setsockopt( %ld, TCP_NODELAY) failed errno=%d (%s)\n",
1424                                                    (long)s, err, sock_errstr(err) ));
1425 #else
1426                                         Debug( LDAP_DEBUG_ANY,
1427                                                 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1428                                                 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1429 #endif
1430                                 }
1431 #endif
1432                         }
1433 #endif
1434
1435 #ifdef NEW_LOGGING
1436                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1437                                    "slapd_daemon_task: new connection on %ld\n", (long)s ));
1438 #else
1439                         Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
1440                                 (long) s, 0, 0 );
1441 #endif
1442                         switch ( from.sa_addr.sa_family ) {
1443 #  ifdef LDAP_PF_LOCAL
1444                         case AF_LOCAL:
1445                                 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1446                                 ssf = LDAP_PVT_SASL_LOCAL_SSF;
1447                                 dnsname = "local";
1448                                 break;
1449 #endif /* LDAP_PF_LOCAL */
1450
1451 #  ifdef LDAP_PF_INET6
1452                         case AF_INET6:
1453                         if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1454                                 peeraddr = inet_ntoa( *((struct in_addr *)
1455                                                         &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1456                                 sprintf( peername, "IP=%s:%d",
1457                                          peeraddr != NULL ? peeraddr : "unknown",
1458                                          (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1459                         } else {
1460                                 char addr[INET6_ADDRSTRLEN];
1461                                 sprintf( peername, "IP=%s %d",
1462                                          inet_ntop( AF_INET6,
1463                                                     &from.sa_in6_addr.sin6_addr,
1464                                                     addr, sizeof addr) ? addr : "unknown",
1465                                          (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1466                         }
1467                         break;
1468 #  endif /* LDAP_PF_INET6 */
1469
1470                         case AF_INET:
1471                         peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1472                         sprintf( peername, "IP=%s:%d",
1473                                 peeraddr != NULL ? peeraddr : "unknown",
1474                                 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1475                                 break;
1476
1477                         default:
1478                                 slapd_close(s);
1479                                 continue;
1480                         }
1481
1482                         if ( ( from.sa_addr.sa_family == AF_INET )
1483 #ifdef LDAP_PF_INET6
1484                                 || ( from.sa_addr.sa_family == AF_INET6 )
1485 #endif
1486                         ) {
1487 #ifdef SLAPD_RLOOKUPS
1488                                 if ( use_reverse_lookup ) {
1489 #  ifdef LDAP_PF_INET6
1490                                         if ( from.sa_addr.sa_family == AF_INET6 )
1491                                                 hp = gethostbyaddr(
1492                                                         (char *)&(from.sa_in6_addr.sin6_addr),
1493                                                         sizeof(from.sa_in6_addr.sin6_addr),
1494                                                         AF_INET6 );
1495                                         else
1496 #  endif /* LDAP_PF_INET6 */
1497                                         hp = gethostbyaddr(
1498                                                 (char *) &(from.sa_in_addr.sin_addr),
1499                                                 sizeof(from.sa_in_addr.sin_addr),
1500                                                 AF_INET );
1501                                         dnsname = hp ? ldap_pvt_str2lower( hp->h_name ) : NULL;
1502                                 }
1503 #else
1504                                 dnsname = NULL;
1505 #endif /* SLAPD_RLOOKUPS */
1506
1507 #ifdef HAVE_TCPD
1508                                 if ( !hosts_ctl("slapd",
1509                                                 dnsname != NULL ? dnsname : STRING_UNKNOWN,
1510                                                 peeraddr != NULL ? peeraddr : STRING_UNKNOWN,
1511                                                 STRING_UNKNOWN ))
1512                                 {
1513                                         /* DENY ACCESS */
1514                                         Statslog( LDAP_DEBUG_ANY,
1515                                                 "fd=%ld host access from %s (%s) denied.\n",
1516                                                 (long) s,
1517                                                 dnsname != NULL ? dnsname : "unknown",
1518                                                 peeraddr != NULL ? peeraddr : "unknown",
1519                                                 0, 0 );
1520                                         slapd_close(s);
1521                                         continue;
1522                                 }
1523 #endif /* HAVE_TCPD */
1524                         }
1525
1526                         id = connection_init(s,
1527                                 slap_listeners[l]->sl_url,
1528                                 dnsname != NULL ? dnsname : "unknown",
1529                                 peername,
1530                                 slap_listeners[l]->sl_name,
1531 #ifdef HAVE_TLS
1532                                 slap_listeners[l]->sl_is_tls,
1533 #else
1534                                 0,
1535 #endif
1536                                 ssf,
1537                                 authid );
1538
1539                         if( authid ) ch_free(authid);
1540
1541                         if( id < 0 ) {
1542 #ifdef NEW_LOGGING
1543                                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1544                                            "slapd_daemon_task: connection_init(%ld, %s, %s) failed.\n",
1545                                            (long)s, peername, slap_listeners[l]->sl_name ));
1546 #else
1547                                 Debug( LDAP_DEBUG_ANY,
1548                                         "daemon: connection_init(%ld, %s, %s) failed.\n",
1549                                         (long) s,
1550                                         peername,
1551                                         slap_listeners[l]->sl_name );
1552 #endif
1553                                 slapd_close(s);
1554                                 continue;
1555                         }
1556
1557                         Statslog( LDAP_DEBUG_STATS,
1558                                 "daemon: conn=%ld fd=%ld connection from %s (%s) accepted.\n",
1559                                 id, (long) s,
1560                                 peername,
1561                                 slap_listeners[l]->sl_name,
1562                                 0 );
1563
1564                         slapd_add( s );
1565                         continue;
1566                 }
1567
1568 #ifdef LDAP_DEBUG
1569 #ifdef NEW_LOGGING
1570                 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1571                            "slapd_daemon_task: activity on " ));
1572 #else
1573                 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1574 #endif
1575 #ifdef HAVE_WINSOCK
1576                 for ( i = 0; i < readfds.fd_count; i++ ) {
1577 #ifdef NEW_LOGGING
1578                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1579                                    " %d%s", readfds.fd_array[i], "r", 0 ));
1580 #else
1581                         Debug( LDAP_DEBUG_CONNS, " %d%s",
1582                                 readfds.fd_array[i], "r", 0 );
1583 #endif
1584                 }
1585                 for ( i = 0; i < writefds.fd_count; i++ ) {
1586 #ifdef NEW_LOGGING
1587                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1588                                    " %d%s", writefds.fd_array[i], "w" ));
1589 #else
1590                         Debug( LDAP_DEBUG_CONNS, " %d%s",
1591                                 writefds.fd_array[i], "w", 0 );
1592 #endif
1593                 }
1594
1595 #else
1596                 for ( i = 0; i < nfds; i++ ) {
1597                         int     r, w;
1598                         int     is_listener = 0;
1599
1600                         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1601                                 if ( i == slap_listeners[l]->sl_sd ) {
1602 #ifdef LDAP_CONNECTIONLESS
1603                                 /* The listener is the data port. Don't
1604                                  * skip it.
1605                                  */
1606                                         if (slap_listeners[l]->sl_is_udp)
1607                                                 continue;
1608 #endif
1609                                         is_listener = 1;
1610                                         break;
1611                                 }
1612                         }
1613                         if ( is_listener ) {
1614                                 continue;
1615                         }
1616                         r = FD_ISSET( i, &readfds );
1617                         w = FD_ISSET( i, &writefds );
1618                         if ( r || w ) {
1619 #ifdef NEW_LOGGING
1620                                 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1621                                            " %d%s%s", i,
1622                                            r ? "r" : "", w ? "w" : "" ));
1623 #else
1624                                 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1625                                     r ? "r" : "", w ? "w" : "" );
1626 #endif
1627                         }
1628                 }
1629 #endif
1630 #ifdef NEW_LOGGING
1631                 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2, "\n" ));
1632 #else
1633                 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1634 #endif
1635
1636 #endif
1637
1638                 /* loop through the writers */
1639 #ifdef HAVE_WINSOCK
1640                 for ( i = 0; i < writefds.fd_count; i++ )
1641 #else
1642                 for ( i = 0; i < nfds; i++ )
1643 #endif
1644                 {
1645                         ber_socket_t wd;
1646                         int is_listener = 0;
1647 #ifdef HAVE_WINSOCK
1648                         wd = writefds.fd_array[i];
1649 #else
1650                         if( ! FD_ISSET( i, &writefds ) ) {
1651                                 continue;
1652                         }
1653                         wd = i;
1654 #endif
1655
1656                         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1657                                 if ( i == slap_listeners[l]->sl_sd ) {
1658 #ifdef LDAP_CONNECTIONLESS
1659                                         if (slap_listeners[l]->sl_is_udp)
1660                                                 continue;
1661 #endif
1662                                         is_listener = 1;
1663                                         break;
1664                                 }
1665                         }
1666                         if ( is_listener ) {
1667                                 continue;
1668                         }
1669 #ifdef NEW_LOGGING
1670                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1671                                    "slapd_daemon_task: write active on %d\n", wd ));
1672 #else
1673                         Debug( LDAP_DEBUG_CONNS,
1674                                 "daemon: write active on %d\n",
1675                                 wd, 0, 0 );
1676 #endif
1677                         /*
1678                          * NOTE: it is possible that the connection was closed
1679                          * and that the stream is now inactive.
1680                          * connection_write() must valid the stream is still
1681                          * active.
1682                          */
1683
1684                         if ( connection_write( wd ) < 0 ) {
1685                                 FD_CLR( (unsigned) wd, &readfds );
1686                                 slapd_close( wd );
1687                         }
1688                 }
1689
1690 #ifdef HAVE_WINSOCK
1691                 for ( i = 0; i < readfds.fd_count; i++ )
1692 #else
1693                 for ( i = 0; i < nfds; i++ )
1694 #endif
1695                 {
1696                         ber_socket_t rd;
1697                         int is_listener = 0;
1698
1699 #ifdef HAVE_WINSOCK
1700                         rd = readfds.fd_array[i];
1701 #else
1702                         if( ! FD_ISSET( i, &readfds ) ) {
1703                                 continue;
1704                         }
1705                         rd = i;
1706 #endif
1707
1708                         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1709                                 if ( rd == slap_listeners[l]->sl_sd ) {
1710 #ifdef LDAP_CONNECTIONLESS
1711                                         if (slap_listeners[l]->sl_is_udp)
1712                                                 continue;
1713 #endif
1714                                         is_listener = 1;
1715                                         break;
1716                                 }
1717                         }
1718                         if ( is_listener ) {
1719                                 continue;
1720                         }
1721
1722 #ifdef NEW_LOGGING
1723                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1724                                    "slapd_daemon_task: read activity on %d\n", rd ));
1725 #else
1726                         Debug ( LDAP_DEBUG_CONNS,
1727                                 "daemon: read activity on %d\n", rd, 0, 0 );
1728 #endif
1729                         /*
1730                          * NOTE: it is possible that the connection was closed
1731                          * and that the stream is now inactive.
1732                          * connection_read() must valid the stream is still
1733                          * active.
1734                          */
1735
1736                         if ( connection_read( rd ) < 0 ) {
1737                                 slapd_close( rd );
1738                         }
1739                 }
1740                 ldap_pvt_thread_yield();
1741         }
1742
1743         if( slapd_shutdown == 1 ) {
1744 #ifdef NEW_LOGGING
1745                 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1746                            "slapd_daemon_task: shutdown requested and initiated.\n"));
1747 #else
1748                 Debug( LDAP_DEBUG_TRACE,
1749                         "daemon: shutdown requested and initiated.\n",
1750                         0, 0, 0 );
1751 #endif
1752
1753         } else if ( slapd_shutdown == 2 ) {
1754 #ifdef HAVE_NT_SERVICE_MANAGER
1755 #ifdef NEW_LOGGING
1756                         LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1757                                    "slapd_daemon_task: shutdown initiated by Service Manager.\n"));
1758 #else
1759                         Debug( LDAP_DEBUG_TRACE,
1760                                "daemon: shutdown initiated by Service Manager.\n",
1761                                0, 0, 0);
1762 #endif
1763 #else /* !HAVE_NT_SERVICE_MANAGER */
1764 #ifdef NEW_LOGGING
1765                         LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1766                                    "slapd_daemon_task: abnormal condition, shutdown initiated.\n" ));
1767 #else
1768                         Debug( LDAP_DEBUG_TRACE,
1769                                "daemon: abnormal condition, shutdown initiated.\n",
1770                                0, 0, 0 );
1771 #endif
1772 #endif /* !HAVE_NT_SERVICE_MANAGER */
1773         } else {
1774 #ifdef NEW_LOGGING
1775                 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1776                            "slapd_daemon_task: no active streams, shutdown initiated.\n" ));
1777 #else
1778                 Debug( LDAP_DEBUG_TRACE,
1779                        "daemon: no active streams, shutdown initiated.\n",
1780                        0, 0, 0 );
1781 #endif
1782         }
1783
1784         if( slapd_gentle_shutdown != 2 )
1785                 close_listeners ( 0 );
1786         free ( slap_listeners );
1787         slap_listeners = NULL;
1788
1789 #ifdef NEW_LOGGING
1790         LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1791                    "slapd_daemon_task: shutdown waiting for %d threads to terminate.\n",
1792                    ldap_pvt_thread_pool_backload(&connection_pool) ));
1793 #else
1794         Debug( LDAP_DEBUG_ANY,
1795             "slapd shutdown: waiting for %d threads to terminate\n",
1796             ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1797 #endif
1798         ldap_pvt_thread_pool_destroy(&connection_pool, 1);
1799
1800         return NULL;
1801 }
1802
1803
1804 int slapd_daemon( void )
1805 {
1806         int rc;
1807
1808         connections_init();
1809
1810 #define SLAPD_LISTENER_THREAD 1
1811 #if defined( SLAPD_LISTENER_THREAD )
1812         {
1813                 ldap_pvt_thread_t       listener_tid;
1814
1815                 /* listener as a separate THREAD */
1816                 rc = ldap_pvt_thread_create( &listener_tid,
1817                         0, slapd_daemon_task, NULL );
1818
1819                 if ( rc != 0 ) {
1820 #ifdef NEW_LOGGING
1821                         LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1822                                    "slapd_daemon: listener ldap_pvt_thread_create failed (%d).\n", rc ));
1823 #else
1824                         Debug( LDAP_DEBUG_ANY,
1825                         "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
1826 #endif
1827                         return rc;
1828                 }
1829  
1830                 /* wait for the listener thread to complete */
1831                 ldap_pvt_thread_join( listener_tid, (void *) NULL );
1832         }
1833 #else
1834         /* experimental code */
1835         slapd_daemon_task( NULL );
1836 #endif
1837
1838         return 0;
1839
1840 }
1841
1842 int sockinit(void)
1843 {
1844 #if defined( HAVE_WINSOCK2 )
1845     WORD wVersionRequested;
1846         WSADATA wsaData;
1847         int err;
1848
1849         wVersionRequested = MAKEWORD( 2, 0 );
1850
1851         err = WSAStartup( wVersionRequested, &wsaData );
1852         if ( err != 0 ) {
1853                 /* Tell the user that we couldn't find a usable */
1854                 /* WinSock DLL.                                  */
1855                 return -1;
1856         }
1857
1858         /* Confirm that the WinSock DLL supports 2.0.*/
1859         /* Note that if the DLL supports versions greater    */
1860         /* than 2.0 in addition to 2.0, it will still return */
1861         /* 2.0 in wVersion since that is the version we      */
1862         /* requested.                                        */
1863
1864         if ( LOBYTE( wsaData.wVersion ) != 2 ||
1865                 HIBYTE( wsaData.wVersion ) != 0 )
1866         {
1867             /* Tell the user that we couldn't find a usable */
1868             /* WinSock DLL.                                  */
1869             WSACleanup();
1870             return -1;
1871         }
1872
1873         /* The WinSock DLL is acceptable. Proceed. */
1874 #elif defined( HAVE_WINSOCK )
1875         WSADATA wsaData;
1876         if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
1877             return -1;
1878         }
1879 #endif
1880         return 0;
1881 }
1882
1883 int sockdestroy(void)
1884 {
1885 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
1886         WSACleanup();
1887 #endif
1888         return 0;
1889 }
1890
1891 RETSIGTYPE
1892 slap_sig_shutdown( int sig )
1893 {
1894 #ifdef NEW_LOGGING
1895         LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1896                    "slap_sig_shutdown: signal %d\n", sig ));
1897 #else
1898         Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
1899 #endif
1900
1901         /*
1902          * If the NT Service Manager is controlling the server, we don't
1903          * want SIGBREAK to kill the server. For some strange reason,
1904          * SIGBREAK is generated when a user logs out.
1905          */
1906
1907 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
1908         if (is_NT_Service && sig == SIGBREAK)
1909 #ifdef NEW_LOGGING
1910             LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1911                        "slap_sig_shutdown: SIGBREAK ignored.\n" ));
1912 #else
1913             Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: SIGBREAK ignored.\n",
1914                   0, 0, 0);
1915 #endif
1916         else
1917 #endif
1918 #ifdef SIGHUP
1919         if (sig == SIGHUP && global_gentlehup && slapd_gentle_shutdown == 0)
1920                 slapd_gentle_shutdown = 1;
1921         else
1922 #endif
1923         slapd_shutdown = 1;
1924
1925         WAKE_LISTENER(1);
1926
1927         /* reinstall self */
1928         (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
1929 }
1930
1931 RETSIGTYPE
1932 slap_sig_wake( int sig )
1933 {
1934         WAKE_LISTENER(1);
1935
1936         /* reinstall self */
1937         (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
1938 }
1939
1940
1941 void slapd_add_internal(ber_socket_t s) {
1942         slapd_add(s);
1943 }
1944
1945 Listener ** slapd_get_listeners(void) {
1946         return slap_listeners;
1947 }