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