]> git.sur5r.net Git - openldap/blob - servers/slapd/daemon.c
Namespace changes
[openldap] / servers / slapd / daemon.c
1 #include "portable.h"
2
3 #include <stdio.h>
4
5 #include <ac/ctype.h>
6 #include <ac/errno.h>
7 #include <ac/signal.h>
8 #include <ac/socket.h>
9 #include <ac/string.h>
10 #include <ac/time.h>
11 #include <ac/unistd.h>
12
13 #include "ldap_defaults.h"
14 #include "slap.h"
15
16 #ifdef HAVE_TCPD
17 #include <tcpd.h>
18
19 int allow_severity = LOG_INFO;
20 int deny_severity = LOG_NOTICE;
21 #endif /* TCP Wrappers */
22
23 /* globals */
24 time_t starttime;
25 ber_socket_t dtblsize;
26
27 typedef struct slap_listener {
28         char* sl_url;
29 #ifdef HAVE_TLS
30         int             sl_is_tls;
31 #endif
32         ber_socket_t            sl_sd;
33         struct sockaddr_in      sl_addr;
34 } Listener;
35
36 Listener **slap_listeners;
37
38 #ifdef HAVE_WINSOCK2
39 /* in nt_main.c */
40 extern ldap_pvt_thread_cond_t                   started_event;
41
42 /* forward reference */
43 static void hit_socket(void);
44 /* In wsa_err.c */
45 char *WSAGetLastErrorString();
46 static ldap_pvt_thread_t hit_tid;
47
48 #define WAKE_LISTENER(w) \
49 do {\
50     if( w ) {\
51         ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );\
52         hit_socket(); \
53     }\
54 } while(0)
55 #else
56 #define WAKE_LISTENER(w) \
57 do {\
58     if( w ) {\
59         ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );\
60     }\
61 } while(0)
62 #endif
63
64 #ifndef HAVE_WINSOCK
65 static 
66 #endif
67 volatile sig_atomic_t slapd_shutdown = 0;
68
69 static ldap_pvt_thread_t        listener_tid;
70 static volatile sig_atomic_t slapd_listener = 0;
71
72 static struct slap_daemon {
73         ldap_pvt_thread_mutex_t sd_mutex;
74
75         int sd_nactives;
76
77 #ifndef HAVE_WINSOCK
78         /* In winsock, accept() returns values higher than dtblsize
79                 so don't bother with this optimization */
80         int sd_nfds;
81 #endif
82
83         fd_set sd_actives;
84         fd_set sd_readers;
85         fd_set sd_writers;
86 } slap_daemon; 
87
88 /*
89  * Add a descriptor to daemon control
90  */
91 static void slapd_add(ber_socket_t s) {
92         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
93
94         assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
95         assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
96         assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
97
98 #ifndef HAVE_WINSOCK
99         if (s >= slap_daemon.sd_nfds) {
100                 slap_daemon.sd_nfds = s + 1;
101         }
102 #endif
103
104         FD_SET( s, &slap_daemon.sd_actives );
105         FD_SET( s, &slap_daemon.sd_readers );
106
107         Debug( LDAP_DEBUG_CONNS, "daemon: added %ld%s%s\n",
108                 (long) s,
109             FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
110                 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
111
112         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
113 }
114
115 /*
116  * Remove the descriptor from daemon control
117  */
118 void slapd_remove(ber_socket_t s, int wake) {
119         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
120         WAKE_LISTENER(wake);
121
122         Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
123                 (long) s,
124             FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
125                 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
126
127         FD_CLR( s, &slap_daemon.sd_actives );
128         FD_CLR( s, &slap_daemon.sd_readers );
129         FD_CLR( s, &slap_daemon.sd_writers );
130
131         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
132 }
133
134 void slapd_clr_write(ber_socket_t s, int wake) {
135         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
136         WAKE_LISTENER(wake);
137
138         assert( FD_ISSET( s, &slap_daemon.sd_actives) );
139         FD_CLR( s, &slap_daemon.sd_writers );
140
141         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
142
143         if( wake ) {
144                 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
145         }
146 }
147
148 void slapd_set_write(ber_socket_t s, int wake) {
149         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
150     WAKE_LISTENER(wake);
151
152         assert( FD_ISSET( s, &slap_daemon.sd_actives) );
153         FD_SET( (unsigned) s, &slap_daemon.sd_writers );
154
155         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
156
157         if( wake ) {
158                 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
159         }
160 }
161
162 void slapd_clr_read(ber_socket_t s, int wake) {
163         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
164     WAKE_LISTENER(wake);
165
166         assert( FD_ISSET( s, &slap_daemon.sd_actives) );
167         FD_CLR( s, &slap_daemon.sd_readers );
168
169         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
170
171         if( wake ) {
172                 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
173         }
174 }
175
176 void slapd_set_read(ber_socket_t s, int wake) {
177         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
178     WAKE_LISTENER(wake);
179
180         assert( FD_ISSET( s, &slap_daemon.sd_actives) );
181         FD_SET( s, &slap_daemon.sd_readers );
182
183         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
184
185         if( wake ) {
186                 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
187         }
188 }
189
190 static void slapd_close(ber_socket_t s) {
191         Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
192                 (long) s, 0, 0 );
193         tcp_close(s);
194 }
195
196
197 Listener *
198 open_listener(
199         char* url,
200         int port,
201         int tls_port )
202 {
203         int     tmp, rc;
204         Listener l;
205         Listener *li;
206         LDAPURLDesc *lud;
207
208         rc = ldap_url_parse( url, &lud );
209
210         if( rc != LDAP_URL_SUCCESS ) {
211                 Debug( LDAP_DEBUG_ANY,
212                         "daemon: listen URL \"%s\" parse error=%d\n",
213                         url, rc, 0 );
214                 return NULL;
215         }
216
217 #ifndef HAVE_TLS
218         if( lud->lud_ldaps ) {
219                 Debug( LDAP_DEBUG_ANY,
220                         "daemon: TLS not supported (%s)\n",
221                         url, 0, 0 );
222                 ldap_free_urldesc( lud );
223                 return NULL;
224         }
225
226         if(! lud->lud_port ) {
227                 lud->lud_port = port;
228         }
229
230 #else
231         if(! lud->lud_port ) {
232                 lud->lud_port = lud->lud_ldaps ? tls_port : port;
233         }
234 #endif
235
236         (void) memset( (void*) &l.sl_addr, '\0', sizeof(l.sl_addr) );
237
238         l.sl_addr.sin_family = AF_INET;
239         l.sl_addr.sin_port = htons( (unsigned short) lud->lud_port );
240
241         if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
242                 || strcmp(lud->lud_host, "*") == 0 )
243         {
244                 l.sl_addr.sin_addr.s_addr = htonl(INADDR_ANY);
245
246         } else {
247                 /* host or address was specified */
248
249                 if( isdigit( lud->lud_host[0] ) ) {
250 #ifdef HAVE_WINSOCK
251                         if(!(l.sl_addr.sin_addr.S_un.S_addr = inet_addr(lud->lud_host)))
252 #else
253                         if(!inet_aton(lud->lud_host, &l.sl_addr.sin_addr))
254 #endif  
255                         {
256                                 Debug( LDAP_DEBUG_ANY, "invalid address (%s) in URL: %s",
257                                         lud->lud_host, url, 0);
258                                 ldap_free_urldesc( lud );
259                                 return NULL;
260                         }
261
262                 } else {
263                         struct hostent *he = gethostbyname( lud->lud_host );
264                         if( he == NULL ) {
265                                 Debug( LDAP_DEBUG_ANY, "invalid host (%s) in URL: %s",
266                                         lud->lud_host, url, 0);
267                                 ldap_free_urldesc( lud );
268                                 return NULL;
269                         }
270
271 #ifdef HAVE_WINSOCK
272                         if(!(l.sl_addr.sin_addr.S_un.S_addr = inet_addr(he->h_addr)))
273 #else
274                         if(!inet_aton(he->h_addr, &l.sl_addr.sin_addr))
275 #endif  
276                         {
277                                 Debug( LDAP_DEBUG_ANY, "%s has invalid address (%s) in URL: %s",
278                                         lud->lud_host, he->h_addr, url );
279                                 ldap_free_urldesc( lud );
280                                 return NULL;
281                         }
282                 }
283         }
284
285         ldap_free_urldesc( lud );
286
287
288         if ( (l.sl_sd = socket( AF_INET, SOCK_STREAM, 0 )) == AC_SOCKET_INVALID ) {
289 #ifndef HAVE_WINSOCK
290                 int err = errno;
291                 Debug( LDAP_DEBUG_ANY,
292                         "daemon: socket() failed errno %d (%s)\n", err,
293                 err > -1 && err < sys_nerr ? sys_errlist[err] :
294                 "unknown", 0 );
295 #else
296                 Debug( LDAP_DEBUG_ANY, 
297                         "daemon: socket() failed errno %d (%s)\n",
298                         WSAGetLastError(),
299                 WSAGetLastErrorString(), 0 );
300 #endif
301                 return( AC_SOCKET_INVALID );
302         }
303
304 #ifndef HAVE_WINSOCK
305         if ( l.sl_sd >= dtblsize ) {
306                 Debug( LDAP_DEBUG_ANY,
307                         "daemon: listener descriptor %ld is too great %ld\n",
308                         (long) l.sl_sd, (long) dtblsize, 0 );
309                 tcp_close( l.sl_sd );
310                 return( AC_SOCKET_INVALID );
311         }
312 #endif
313
314 #ifdef SO_REUSEADDR
315         tmp = 1;
316         if ( setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
317                 (char *) &tmp, sizeof(tmp) ) == -1 )
318         {
319                 int err = errno;
320                 Debug( LDAP_DEBUG_ANY,
321                "slapd(%ld): setsockopt() failed errno %d (%s)\n",
322                 (long) l.sl_sd, err,
323                         err > -1 && err < sys_nerr
324                                 ? sys_errlist[err] : "unknown" );
325         }
326 #endif
327 #ifdef SO_KEEPALIVE
328         tmp = 1;
329         if ( setsockopt( l.sl_sd, SOL_SOCKET, SO_KEEPALIVE,
330                 (char *) &tmp, sizeof(tmp) ) == -1 )
331         {
332                 int err = errno;
333                 Debug( LDAP_DEBUG_ANY,
334                         "slapd(%ld): setsockopt(KEEPALIVE) failed errno %d (%s)\n",
335                 (long) l.sl_sd, err,
336                         err > -1 && err < sys_nerr
337                                 ? sys_errlist[err] : "unknown" );
338         }
339 #endif
340
341         if ( bind( l.sl_sd, (struct sockaddr *) &l.sl_addr, sizeof(l.sl_addr) ) == -1 ) {
342                 int err = errno;
343                 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno %d (%s)\n",
344                 (long) l.sl_sd, err,
345                         err > -1 && err < sys_nerr
346                                 ? sys_errlist[err] : "unknown" );
347                 tcp_close( l.sl_sd );
348                 return AC_SOCKET_INVALID;
349         }
350
351         l.sl_url = ch_strdup( url );
352
353         li = ch_malloc( sizeof( Listener ) );
354         *li = l;
355
356         return li;
357 }
358
359 static int sockinit(void);
360 static int sockdestroy(void);
361
362 slapd_daemon_init(char *urls, int port, int tls_port )
363 {
364         int i, rc;
365         char **u;
366
367 #ifndef HAVE_TLS
368         assert( tls_port == 0 );
369 #endif
370
371         if( rc = sockinit() ) {
372                 return rc;
373         }
374
375 #ifdef HAVE_SYSCONF
376         dtblsize = sysconf( _SC_OPEN_MAX );
377 #elif HAVE_GETDTABLESIZE
378         dtblsize = getdtablesize();
379 #else
380         dtblsize = FD_SETSIZE;
381 #endif
382
383 #ifdef FD_SETSIZE
384         if(dtblsize > FD_SETSIZE) {
385                 dtblsize = FD_SETSIZE;
386         }
387 #endif  /* !FD_SETSIZE */
388
389         FD_ZERO( &slap_daemon.sd_readers );
390         FD_ZERO( &slap_daemon.sd_writers );
391
392         if( urls == NULL ) {
393                 urls = ch_strdup("ldap://");
394         }
395
396         u = str2charray( urls, " " );
397
398         if( u == NULL ) {
399                 return -1;
400         }
401
402         for(i = 0; u[i] == NULL; i++ ) {
403                 /* EMPTY */ ;
404         }
405
406         slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
407
408         for(i = 0; u[i] == NULL; i++ ) {
409                 slap_listeners[i] = open_listener( u[i], port, tls_port );
410
411                 if( slap_listeners[i] == NULL ) {
412                         return -1;
413                 }
414         }
415         slap_listeners[i] = NULL;
416
417
418         charray_free( u );
419
420         ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
421
422         return 0;
423 }
424
425
426 slapd_daemon_destroy(void)
427 {
428         connections_destroy();
429         sockdestroy();
430         return 0;
431 }
432
433
434 static void *
435 slapd_daemon_task(
436         void *ptr
437 )
438 {
439         int l;
440
441         time( &starttime );
442
443         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
444                 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
445                         continue;
446
447                 if ( listen( slap_listeners[l]->sl_sd, 5 ) == -1 ) {
448                         int err = errno;
449                         Debug( LDAP_DEBUG_ANY,
450                                 "daemon: listen(%s, 5) failed errno %d (%s)\n",
451                                         (long) slap_listeners[l]->sl_url, err,
452                                         err > -1 && err < sys_nerr
453                                         ? sys_errlist[err] : "unknown" );
454
455                         return( (void*)-1 );
456                 }
457
458                 slapd_add( slap_listeners[l]->sl_sd );
459         }
460
461 #ifdef HAVE_WINSOCK
462         if ( started_event != NULL ) {
463                 ldap_pvt_thread_cond_signal( &started_event );
464         }
465 #endif
466         /* initialization complete. Here comes the loop. */
467
468         while ( !slapd_shutdown ) {
469                 ber_socket_t i;
470                 int ns;
471                 int at;
472                 ber_socket_t nfds;
473 #define SLAPD_EBADF_LIMIT 10
474                 int ebadf = 0;
475
476 #define SLAPD_IDLE_CHECK_LIMIT 4
477                 time_t  last_idle_check = slap_get_time();
478                 time_t  now;
479
480
481                 fd_set                  readfds;
482                 fd_set                  writefds;
483
484                 struct sockaddr_in      from;
485 #if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
486         struct hostent          *hp;
487 #endif
488                 struct timeval          zero;
489                 struct timeval          *tvp;
490
491                 char    *client_name;
492                 char    *client_addr;
493
494                 if( global_idletimeout > 0 && difftime(
495                         last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT,
496                         now ) < 0 )
497                 {
498                         connections_timeout_idle(now);
499                 }
500
501                 FD_ZERO( &writefds );
502                 FD_ZERO( &readfds );
503
504                 zero.tv_sec = 0;
505                 zero.tv_usec = 0;
506
507                 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
508
509 #ifdef FD_SET_MANUAL_COPY
510                 for( s = 0; s < nfds; s++ ) {
511                         if(FD_ISSET( &slap_sd_writers, s )) {
512                                 FD_SET( &writefds, s );
513                         }
514                         if(FD_ISSET( &slap_sd_writers, s )) {
515                                 FD_SET( &writefds, s );
516                         }
517                 }
518 #else
519                 memcpy( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
520                 memcpy( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
521 #endif
522
523                 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
524                         if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
525                                 continue;
526                         FD_SET( slap_listeners[l]->sl_sd, &readfds );
527                 }
528
529 #ifndef HAVE_WINSOCK
530                 nfds = slap_daemon.sd_nfds;
531 #else
532                 nfds = dtblsize;
533 #endif
534
535                 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
536
537                 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
538                 at = active_threads;
539                 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
540
541 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
542                 tvp = NULL;
543 #else
544                 tvp = at ? &zero : NULL;
545 #endif
546
547                 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
548                         if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
549                                 continue;
550
551                         Debug( LDAP_DEBUG_CONNS,
552                                 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
553                                         slap_listeners[l]->sl_sd, at,
554                                         tvp == NULL ? "NULL" : "zero" );
555                 }
556
557                 switch(ns = select( nfds, &readfds,
558 #ifdef HAVE_WINSOCK
559                         /* don't pass empty fd_set */
560                         ( writefds.fd_count > 0 ? &writefds : NULL ),
561 #else
562                         &writefds,
563 #endif
564                         NULL, tvp ))
565                 {
566                 case -1: {      /* failure - try again */
567 #ifdef HAVE_WINSOCK
568                                 int err = WSAGetLastError();
569 #else
570                                 int err = errno;
571 #endif
572
573                                 if( err == EBADF && ++ebadf < SLAPD_EBADF_LIMIT) {
574                                         continue;
575                                 }
576
577                                 if( err != EINTR ) {
578                                         Debug( LDAP_DEBUG_CONNS,
579                                                 "daemon: select failed (%d): %s\n",
580                                                 err,
581                                                 err >= 0 && err < sys_nerr
582                                                         ? sys_errlist[err] : "unknown",
583                                                 0 );
584
585
586                                 slapd_shutdown = -1;
587                                 }
588                         }
589                         continue;
590
591                 case 0:         /* timeout - let threads run */
592                         ebadf = 0;
593                         Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
594                             0, 0, 0 );
595                 ldap_pvt_thread_yield();
596                         continue;
597
598                 default:        /* something happened - deal with it */
599                         ebadf = 0;
600                         Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
601                                 ns, 0, 0 );
602                         /* FALL THRU */
603                 }
604
605                 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
606                         ber_int_t s;
607                         socklen_t len = sizeof(from);
608                         long id;
609
610                         if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
611                                 continue;
612
613                         if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
614                                 continue;
615
616                         if ( (s = accept( slap_listeners[l]->sl_sd,
617                                 (struct sockaddr *) &from, &len )) == AC_SOCKET_INVALID )
618                         {
619                                 int err = errno;
620                                 Debug( LDAP_DEBUG_ANY,
621                                     "daemon: accept(%ld) failed errno %d (%s)\n", err,
622                                     (long) slap_listeners[l]->sl_sd,
623                                     err >= 0 && err < sys_nerr ?
624                                     sys_errlist[err] : "unknown");
625                                 continue;
626                         }
627
628 #ifdef LDAP_DEBUG
629                         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
630
631                         /* newly accepted stream should not be in any of the FD SETS */
632
633                         assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
634                         assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
635                         assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
636
637                         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
638 #endif
639
640 #ifndef HAVE_WINSOCK
641                         /* make sure descriptor number isn't too great */
642                         if ( s >= dtblsize ) {
643                                 Debug( LDAP_DEBUG_ANY,
644                                         "daemon: %ld beyond descriptor table size %ld\n",
645                                         (long) s, (long) dtblsize, 0 );
646                                 slapd_close(s);
647                                 continue;
648                         }
649 #endif
650                    
651                         Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
652                                 (long) s, 0, 0 );
653
654                         len = sizeof(from);
655                         if ( getpeername( s, (struct sockaddr *) &from, &len ) == 0 ) {
656                                 client_addr = inet_ntoa( from.sin_addr );
657
658 #if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
659                                 hp = gethostbyaddr( (char *)
660                                     &(from.sin_addr.s_addr),
661                                     sizeof(from.sin_addr.s_addr), AF_INET );
662
663                                 if(hp) {
664                                         char *p;
665                                         client_name = hp->h_name;
666
667                                         /* normalize the domain */
668                                         for ( p = client_name; *p; p++ ) {
669                                                 *p = TOLOWER( (unsigned char) *p );
670                                         }
671
672                                 } else {
673                                         client_name = NULL;
674                                 }
675 #else
676                                 client_name = NULL;
677 #endif
678
679                         } else {
680                                 client_name = NULL;;
681                                 client_addr = NULL;
682                         }
683
684 #ifdef HAVE_TCPD
685                         if(!hosts_ctl("slapd",
686                                 client_name != NULL ? client_name : STRING_UNKNOWN,
687                                 client_addr != NULL ? client_addr : STRING_UNKNOWN,
688                                 STRING_UNKNOWN))
689                         {
690                                 /* DENY ACCESS */
691                                 Statslog( LDAP_DEBUG_ANY,
692                                  "fd=%ld connection from %s (%s) denied.\n",
693                                         (long) s,
694                                         client_name == NULL ? "unknown" : client_name,
695                                         client_addr == NULL ? "unknown" : client_addr,
696                                   0, 0 );
697
698                                 slapd_close(s);
699                                 continue;
700                         }
701 #endif /* HAVE_TCPD */
702
703                         if( (id = connection_init(s, client_name, client_addr,
704 #ifdef HAVE_TLS
705                                 slap_listeners[l]->sl_is_tls
706 #else
707                                 0
708 #endif
709                                 )) < 0 )
710                         {
711                                 Debug( LDAP_DEBUG_ANY,
712                                         "daemon: connection_init(%ld, %s, %s) failed.\n",
713                                         (long) s,
714                                         client_name == NULL ? "unknown" : client_name,
715                                         client_addr == NULL ? "unknown" : client_addr);
716                                 slapd_close(s);
717                                 continue;
718                         }
719
720                         Statslog( LDAP_DEBUG_STATS,
721                                 "daemon: conn=%d fd=%ld connection from %s (%s) accepted.\n",
722                                 id, (long) s,
723                                 client_name == NULL ? "unknown" : client_name,
724                                 client_addr == NULL ? "unknown" : client_addr,
725                                 0 );
726
727                         slapd_add( s );
728                         continue;
729                 }
730
731 #ifdef LDAP_DEBUG
732                 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
733 #ifdef HAVE_WINSOCK
734                 for ( i = 0; i < readfds.fd_count; i++ ) {
735                         Debug( LDAP_DEBUG_CONNS, " %d%s",
736                                 readfds.fd_array[i], "r", 0 );
737                 }
738                 for ( i = 0; i < writefds.fd_count; i++ ) {
739                         Debug( LDAP_DEBUG_CONNS, " %d%s",
740                                 writefds.fd_array[i], "w", 0 );
741                 }
742 #else
743                 for ( i = 0; i < nfds; i++ ) {
744                         int     a, r, w;
745                         int     is_listener = 0;
746
747                         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
748                                 if ( i == slap_listeners[l]->sl_sd ) {
749                                         is_listener = 1;
750                                         break;
751                                 }
752                         }
753                         if ( is_listener ) {
754                                 continue;
755                         }
756                         r = FD_ISSET( i, &readfds );
757                         w = FD_ISSET( i, &writefds );
758                         if ( r || w ) {
759                                 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
760                                     r ? "r" : "", w ? "w" : "" );
761                         }
762                 }
763 #endif
764                 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
765 #endif
766
767                 /* loop through the writers */
768 #ifdef HAVE_WINSOCK
769                 for ( i = 0; i < writefds.fd_count; i++ )
770 #else
771                 for ( i = 0; i < nfds; i++ )
772 #endif
773                 {
774                         ber_socket_t wd;
775                         int is_listener = 0;
776 #ifdef HAVE_WINSOCK
777                         wd = writefds.fd_array[i];
778 #else
779                         if( ! FD_ISSET( i, &writefds ) ) {
780                                 continue;
781                         }
782                         wd = i;
783 #endif
784
785                         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
786                                 if ( i == slap_listeners[l]->sl_sd ) {
787                                         is_listener = 1;
788                                         break;
789                                 }
790                         }
791                         if ( is_listener ) {
792                                 continue;
793                         }
794                         Debug( LDAP_DEBUG_CONNS,
795                                 "daemon: write active on %d\n",
796                                 wd, 0, 0 );
797
798                         /*
799                          * NOTE: it is possible that the connection was closed
800                          * and that the stream is now inactive.
801                          * connection_write() must valid the stream is still
802                          * active.
803                          */
804
805                         if ( connection_write( wd ) < 0 ) {
806                                 FD_CLR( (unsigned) wd, &readfds );
807                                 slapd_close( wd );
808                         }
809                 }
810
811 #ifdef HAVE_WINSOCK
812                 for ( i = 0; i < readfds.fd_count; i++ )
813 #else
814                 for ( i = 0; i < nfds; i++ )
815 #endif
816                 {
817                         ber_socket_t rd;
818                         int is_listener = 0;
819                         int rc;
820
821 #ifdef HAVE_WINSOCK
822                         rd = readfds.fd_array[i];
823 #else
824                         if( ! FD_ISSET( i, &readfds ) ) {
825                                 continue;
826                         }
827                         rd = i;
828 #endif
829
830                         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
831                                 if ( i == slap_listeners[l]->sl_sd ) {
832                                         is_listener = 1;
833                                         break;
834                                 }
835                         }
836                         if ( is_listener ) {
837                                 continue;
838                         }
839
840                         Debug ( LDAP_DEBUG_CONNS,
841                                 "daemon: read activity on %d\n", rd, 0, 0 );
842
843                         /*
844                          * NOTE: it is possible that the connection was closed
845                          * and that the stream is now inactive.
846                          * connection_read() must valid the stream is still
847                          * active.
848                          */
849
850                         while ( ( rc = connection_read( rd ) ) > 0 )
851                                 ;
852                         if ( rc < 0 ) {
853                                 slapd_close( rd );
854                         }
855                 }
856                 ldap_pvt_thread_yield();
857         }
858
859         if( slapd_shutdown > 0 ) {
860                 Debug( LDAP_DEBUG_TRACE,
861                         "daemon: shutdown requested and initiated.\n",
862                         0, 0, 0 );
863
864         } else if ( slapd_shutdown < 0 ) {
865                 Debug( LDAP_DEBUG_TRACE,
866                         "daemon: abnormal condition, shutdown initiated.\n",
867                         0, 0, 0 );
868         } else {
869                 Debug( LDAP_DEBUG_TRACE,
870                         "daemon: no active streams, shutdown initiated.\n",
871                         0, 0, 0 );
872         }
873
874         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
875                 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
876                         slapd_close( slap_listeners[l]->sl_sd );
877                         break;
878                 }
879         }
880
881         ldap_pvt_thread_mutex_lock( &active_threads_mutex );
882         Debug( LDAP_DEBUG_ANY,
883             "slapd shutdown: waiting for %d threads to terminate\n",
884             active_threads, 0, 0 );
885         while ( active_threads > 0 ) {
886                 ldap_pvt_thread_cond_wait(&active_threads_cond, &active_threads_mutex);
887         }
888         ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
889
890         return NULL;
891 }
892
893
894 int slapd_daemon( void )
895 {
896         int rc;
897
898         connections_init();
899
900 #define SLAPD_LISTENER_THREAD 1
901 #if defined( SLAPD_LISTENER_THREAD ) || !defined(HAVE_PTHREADS)
902
903         /* listener as a separate THREAD */
904         rc = ldap_pvt_thread_create( &listener_tid,
905                 0, slapd_daemon_task, NULL );
906
907         if ( rc != 0 ) {
908                 Debug( LDAP_DEBUG_ANY,
909                     "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
910                 return rc;
911         }
912
913         /* wait for the listener thread to complete */
914         ldap_pvt_thread_join( listener_tid, (void *) NULL );
915 #else
916         /* expermimental code */
917         listener_tid = pthread_self();
918         slapd_daemon_task( NULL );
919 #endif
920
921         return 0;
922
923 }
924
925 #ifdef HAVE_WINSOCK2
926 int sockinit(void)
927 {
928     WORD wVersionRequested;
929         WSADATA wsaData;
930         int err;
931  
932         wVersionRequested = MAKEWORD( 2, 0 );
933  
934         err = WSAStartup( wVersionRequested, &wsaData );
935         if ( err != 0 ) {
936                 /* Tell the user that we couldn't find a usable */
937                 /* WinSock DLL.                                  */
938                 return -1;
939         }
940  
941         /* Confirm that the WinSock DLL supports 2.0.*/
942         /* Note that if the DLL supports versions greater    */
943         /* than 2.0 in addition to 2.0, it will still return */
944         /* 2.0 in wVersion since that is the version we      */
945         /* requested.                                        */
946  
947         if ( LOBYTE( wsaData.wVersion ) != 2 ||
948                 HIBYTE( wsaData.wVersion ) != 0 )
949         {
950             /* Tell the user that we couldn't find a usable */
951             /* WinSock DLL.                                  */
952             WSACleanup();
953             return -1; 
954         }
955
956         /* The WinSock DLL is acceptable. Proceed. */
957         return 0;
958 }
959
960 int sockdestroy(void)
961 {
962         WSACleanup();
963         return 0;
964 }
965
966 void hit_socket(void)
967 {
968         ber_socket_t s;
969         int on = 1;
970         extern struct sockaddr_in       bind_addr;
971
972         /* throw something at the socket to terminate the select() in the daemon thread. */
973         if (( s = socket( AF_INET, SOCK_STREAM, 0 )) == AC_SOCKET_INVALID )
974                 Debug( LDAP_DEBUG_ANY,
975                         "slap_set_shutdown: socket failed\n\tWSAGetLastError=%d (%s)\n",
976                         WSAGetLastError(), WSAGetLastErrorString(), 0 );
977
978         if ( ioctlsocket( s, FIONBIO, &on ) == -1 ) 
979                 Debug( LDAP_DEBUG_ANY,
980                         "slap_set_shutdown:FIONBIO ioctl on %d faled\n\tWSAGetLastError=%d (%s)\n",
981                         s, WSAGetLastError(), WSAGetLastError() );
982         
983         bind_addr.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
984
985         if ( connect( s, (struct sockaddr *)&bind_addr, sizeof( struct sockaddr_in )) == SOCKET_ERROR ) {
986                 Debug( LDAP_DEBUG_ANY,
987                         "hit_socket: error on connect: %d\n",
988                         WSAGetLastError(), 0, 0 );
989                 /* we can probably expect some error to occur here, mostly WSAEWOULDBLOCK */
990         }
991
992         tcp_close(s);
993 }
994
995 #elif HAVE_WINSOCK
996 static int sockinit(void)
997 {       WSADATA wsaData;
998         if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
999             return -1;
1000         }
1001         return 0;
1002 }
1003 static int sockdestroy(void)
1004 {
1005         WSACleanup();
1006         return 0;
1007 }
1008
1009 #else
1010 static int sockinit(void)
1011 {
1012         return 0;
1013 }
1014 static int sockdestroy(void)
1015 {
1016         return 0;
1017 }
1018 #endif
1019
1020 void
1021 slap_set_shutdown( int sig )
1022 {
1023         int l;
1024         slapd_shutdown = sig;
1025 #ifndef HAVE_WINSOCK
1026         if(slapd_listener) {
1027                 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
1028         }
1029 #else
1030         Debug( LDAP_DEBUG_TRACE, "Shutdown %d ordered", sig, 0, 0 );
1031         /* trying to "hit" the socket seems to always get a */
1032         /* EWOULDBLOCK error, so just close the listen socket to */
1033         /* break out of the select since we're shutting down anyway */
1034         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1035                 if ( slap_listeners[l]->sl_sd >= 0 ) {
1036                         tcp_close( slap_listeners[l]->sl_sd );
1037                 }
1038         }
1039 #endif
1040         /* reinstall self */
1041         (void) SIGNAL( sig, slap_set_shutdown );
1042 }
1043
1044 void
1045 slap_do_nothing( int sig )
1046 {
1047         /* reinstall self */
1048         (void) SIGNAL( sig, slap_do_nothing );
1049 }