11 #include <ac/unistd.h>
13 #include "ldapconfig.h"
19 int allow_severity = LOG_INFO;
20 int deny_severity = LOG_NOTICE;
21 #endif /* TCP Wrappers */
26 static ldap_pvt_thread_t listener_tid;
27 static volatile sig_atomic_t slapd_shutdown = 0;
30 ldap_pvt_thread_mutex_t sd_mutex;
35 /* In winsock, accept() returns values higher than dtblsize
36 so don't bother with this optimization */
46 * Add a descriptor to daemon control
48 static void slapd_add(int s) {
49 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
51 assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
52 assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
53 assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
56 if (s >= slap_daemon.sd_nfds) {
57 slap_daemon.sd_nfds = s + 1;
61 FD_SET( s, &slap_daemon.sd_actives );
62 FD_SET( s, &slap_daemon.sd_readers );
64 Debug( LDAP_DEBUG_CONNS, "daemon: added %d%s%s\n", s,
65 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
66 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
68 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
72 * Remove the descriptor from daemon control
74 void slapd_remove(int s) {
75 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
77 assert( FD_ISSET( s, &slap_daemon.sd_actives ));
79 Debug( LDAP_DEBUG_CONNS, "daemon: removing %d%s%s\n", s,
80 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
81 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
83 FD_CLR( s, &slap_daemon.sd_actives );
84 FD_CLR( s, &slap_daemon.sd_readers );
85 FD_CLR( s, &slap_daemon.sd_writers );
87 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
90 void slapd_clr_write(int s, int wake) {
91 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
93 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
94 FD_CLR( s, &slap_daemon.sd_writers );
96 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
99 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
103 void slapd_set_write(int s, int wake) {
104 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
106 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
107 FD_SET( s, &slap_daemon.sd_writers );
109 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
112 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
116 void slapd_clr_read(int s, int wake) {
117 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
119 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
120 FD_CLR( s, &slap_daemon.sd_readers );
122 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
125 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
129 void slapd_set_read(int s, int wake) {
130 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
132 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
133 FD_SET( s, &slap_daemon.sd_readers );
135 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
138 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
142 static void slapd_close(int s) {
145 Debug( LDAP_DEBUG_CONNS, "daemon: closing %d\n", s, 0, 0 );
150 set_socket( struct sockaddr_in *addr )
155 dtblsize = sysconf( _SC_OPEN_MAX );
156 #elif HAVE_GETDTABLESIZE
157 dtblsize = getdtablesize();
159 dtblsize = FD_SETSIZE;
163 if(dtblsize > FD_SETSIZE) {
164 dtblsize = FD_SETSIZE;
166 #endif /* !FD_SETSIZE */
170 WORD vers = MAKEWORD( 2, 0);
173 err = WSAStartup( vers, &wsaData );
180 if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
181 Debug( LDAP_DEBUG_ANY,
182 "daemon: socket() failed errno %d (%s)\n", errno,
183 errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
189 if ( tcps >= dtblsize ) {
190 Debug( LDAP_DEBUG_ANY,
191 "daemon: listener descriptor %d is too great\n",
198 if ( setsockopt( tcps, SOL_SOCKET, SO_REUSEADDR,
199 (char *) &tmp, sizeof(tmp) ) == -1 )
201 Debug( LDAP_DEBUG_ANY,
202 "slapd(%d): setsockopt() failed errno %d (%s)\n",
204 errno > -1 && errno < sys_nerr
205 ? sys_errlist[errno] : "unknown" );
210 if ( bind( tcps, (struct sockaddr *) addr, sizeof(*addr) ) == -1 ) {
211 Debug( LDAP_DEBUG_ANY, "daemon: bind(%d) failed errno %d (%s)\n",
213 errno > -1 && errno < sys_nerr
214 ? sys_errlist[errno] : "unknown" );
227 int inetd = ((int *)ptr) [0];
228 int tcps = ((int *)ptr) [1];
233 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
234 FD_ZERO( &slap_daemon.sd_readers );
235 FD_ZERO( &slap_daemon.sd_writers );
238 if ( listen( tcps, 5 ) == -1 ) {
239 Debug( LDAP_DEBUG_ANY,
240 "daemon: listen(%d, 5) failed errno %d (%s)\n",
242 errno > -1 && errno < sys_nerr
243 ? sys_errlist[errno] : "unknown" );
250 if( connection_init( 0, NULL, NULL ) ) {
251 Debug( LDAP_DEBUG_ANY,
252 "connection_init(%d) failed.\n",
261 while ( !slapd_shutdown ) {
268 struct sockaddr_in from;
276 FD_ZERO( &writefds );
282 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
284 #ifdef FD_SET_MANUAL_COPY
285 for( s = 0; s < nfds; s++ ) {
286 if(FD_ISSET( &slap_sd_writers, s )) {
287 FD_SET( &writefds, s );
289 if(FD_ISSET( &slap_sd_writers, s )) {
290 FD_SET( &writefds, s );
294 memcpy( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
295 memcpy( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
298 FD_SET( tcps, &readfds );
301 nfds = slap_daemon.sd_nfds;
306 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
308 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
309 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
312 tvp = active_threads ? &zero : NULL;
315 Debug( LDAP_DEBUG_CONNS,
316 "daemon: select: tcps=%d active_threads=%d tvp=%s\n",
317 tcps, active_threads,
318 tvp == NULL ? "NULL" : "zero" );
321 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
323 switch(ns = select( nfds, &readfds, &writefds, 0, tvp )) {
324 case -1: /* failure - try again */
325 if( errno != EINTR ) {
326 Debug( LDAP_DEBUG_CONNS,
327 "daemon: select failed (%d): %s\n",
329 errno >= 0 && errno < sys_nerr
330 ? sys_errlist[errno] : "unknown",
338 case 0: /* timeout - let threads run */
339 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
341 ldap_pvt_thread_yield();
344 default: /* something happened - deal with it */
345 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
350 if ( FD_ISSET( tcps, &readfds ) ) {
352 int len = sizeof(from);
355 if ( (s = accept( tcps,
356 (struct sockaddr *) &from, &len )) == -1 )
358 Debug( LDAP_DEBUG_ANY,
359 "daemon: accept(%d) failed errno %d (%s)\n", errno,
360 tcps, errno >= 0 && errno < sys_nerr ?
361 sys_errlist[errno] : "unknown");
365 assert( !FD_ISSET( 0, &slap_daemon.sd_actives) );
366 assert( !FD_ISSET( 0, &slap_daemon.sd_readers) );
367 assert( !FD_ISSET( 0, &slap_daemon.sd_writers) );
370 /* make sure descriptor number isn't too great */
371 if ( s >= dtblsize ) {
372 Debug( LDAP_DEBUG_ANY,
373 "daemon: %d beyond descriptor table size %d\n",
380 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %d\n",
384 if ( getpeername( s, (struct sockaddr *) &from, &len ) == 0 ) {
385 client_addr = inet_ntoa( from.sin_addr );
387 #if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
388 hp = gethostbyaddr( (char *)
389 &(from.sin_addr.s_addr),
390 sizeof(from.sin_addr.s_addr), AF_INET );
394 client_name = hp->h_name;
396 /* normalize the domain */
397 for ( p = client_name; *p; p++ ) {
398 *p = TOLOWER( (unsigned char) *p );
414 if(!hosts_ctl("slapd",
415 client_name != NULL ? client_name : STRING_UNKNOWN,
416 client_addr != NULL ? client_addr : STRING_UNKNOWN,
420 Statslog( LDAP_DEBUG_ANY,
421 "fd=%d connection from %s (%s) denied.\n",
423 client_name == NULL ? "unknown" : client_name,
424 client_addr == NULL ? "unknown" : client_addr,
430 #endif /* HAVE_TCPD */
432 if( (id = connection_init(s, client_name, client_addr)) < 0 ) {
433 Debug( LDAP_DEBUG_ANY,
434 "daemon: connection_init(%d, %s, %s) failed.\n",
436 client_name == NULL ? "unknown" : client_name,
437 client_addr == NULL ? "unknown" : client_addr);
442 Statslog( LDAP_DEBUG_STATS,
443 "daemon: conn=%d fd=%d connection from %s (%s) accepted.\n",
445 client_name == NULL ? "unknown" : client_name,
446 client_addr == NULL ? "unknown" : client_addr,
454 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
456 for ( i = 0; i < readfds.fd_count; i++ )
458 Debug( LDAP_DEBUG_CONNS, " %d%s", readfds.fd_array[i], "r" );
460 for ( i = 0; i < writefds.fd_count; i++ )
462 Debug( LDAP_DEBUG_CONNS, " %d%s", writefds.fd_array[i], "w" );
465 for ( i = 0; i < nfds; i++ ) {
468 r = FD_ISSET( i, &readfds );
469 w = FD_ISSET( i, &writefds );
470 if ( i != tcps && (r || w) ) {
471 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
472 r ? "r" : "", w ? "w" : "" );
476 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
479 /* loop through the writers */
481 for ( i = 0; i < writefds.fd_count; i++ ) {
482 if ( writefds.fd_array[i] == tcps ) {
485 Debug( LDAP_DEBUG_CONNS,
486 "daemon: signalling write waiter on %d\n",
487 writefds.fd_array[i], 0, 0 );
488 assert( FD_ISSET( 0, &slap_daemon.sd_actives) );
490 slapd_clr_write( writefds.fd_array[i], 0 );
491 if ( connection_write( writefds.fd_array[i] ) < 0 ) {
492 FD_CLR( writefds.fd_array[i], &readfds );
493 slapd_close( writefds.fd_array[i] );
497 for ( i = 0; i < nfds; i++ ) {
501 if ( FD_ISSET( i, &writefds ) ) {
502 Debug( LDAP_DEBUG_CONNS,
503 "daemon: signaling write waiter on %d\n", i, 0, 0 );
505 assert( FD_ISSET( 0, &slap_daemon.sd_actives) );
507 /* clear the write flag */
508 slapd_clr_write( i, 0 );
510 if( connection_write( i ) < 0 ) {
511 FD_CLR( i, &readfds );
519 for ( i = 0; i < readfds.fd_count; i++ ) {
520 if ( readfds.fd_array[i] == tcps ) {
523 Debug ( LDAP_DEBUG_CONNS,
524 "daemon: read activity on %d\n", readfds.fd_array[i], 0, 0 );
525 assert( FD_ISSET( readfds.fd_array[i], &slap_daemon.sd_actives) );
527 if ( connection_read( readfds.fd_array[i] ) < 0 ) {
532 for ( i = 0; i < nfds; i++ ) {
537 if ( FD_ISSET( i, &readfds ) ) {
538 Debug( LDAP_DEBUG_CONNS,
539 "daemon: read activity on %d\n", i, 0, 0 );
541 assert( FD_ISSET( i, &slap_daemon.sd_actives) );
543 if( connection_read( i ) < 0) {
549 ldap_pvt_thread_yield();
552 if( slapd_shutdown > 0 ) {
553 Debug( LDAP_DEBUG_TRACE,
554 "daemon: shutdown requested and initiated.\n",
557 } else if ( slapd_shutdown < 0 ) {
558 Debug( LDAP_DEBUG_TRACE,
559 "daemon: abnormal condition, shutdown initiated.\n",
562 Debug( LDAP_DEBUG_TRACE,
563 "daemon: no active streams, shutdown initiated.\n",
571 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
572 Debug( LDAP_DEBUG_ANY,
573 "slapd shutdown: waiting for %d threads to terminate\n",
574 active_threads, 0, 0 );
575 while ( active_threads > 0 ) {
576 ldap_pvt_thread_cond_wait(&active_threads_cond, &active_threads_mutex);
578 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
583 int slapd_daemon( int inetd, int tcps )
586 int *args = ch_malloc( sizeof( int[2] ) );
590 status = ldap_pvt_thread_create( &listener_tid, 0,
591 slapd_daemon_task, args );
594 Debug( LDAP_DEBUG_ANY,
595 "listener ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
598 /* wait for the listener thread to complete */
599 ldap_pvt_thread_join( listener_tid, (void *) NULL );
606 slap_set_shutdown( int sig )
609 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
612 (void) SIGNAL( sig, slap_set_shutdown );
616 slap_do_nothing( int sig )
619 (void) SIGNAL( sig, slap_do_nothing );