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