5 * Added locking of new_conn_mutex when traversing the c[] array.
14 #include <ac/signal.h>
15 #include <ac/socket.h>
16 #include <ac/string.h>
18 #include <ac/unistd.h>
20 #include "ldapconfig.h"
23 #ifdef HAVE_SYS_FILIO_H
24 #include <sys/filio.h>
25 #elif HAVE_SYS_IOCTL_H
26 #include <sys/ioctl.h>
32 #define assert( cond )
38 int allow_severity = LOG_INFO;
39 int deny_severity = LOG_NOTICE;
40 #endif /* TCP Wrappers */
45 static volatile sig_atomic_t slapd_shutdown = 0;
47 /* a link to the slapd.conf configuration parameters */
48 extern char *slapd_pid_file;
49 extern char *slapd_args_file;
58 struct sockaddr_in addr;
64 dtblsize = sysconf( _SC_OPEN_MAX );
65 #elif HAVE_GETDTABLESIZE
66 dtblsize = getdtablesize();
72 if(dtblsize > FD_SETSIZE) {
73 dtblsize = FD_SETSIZE;
75 #endif /* !FD_SETSIZE */
77 c = (Connection *) ch_calloc( 1, dtblsize * sizeof(Connection) );
79 for ( i = 0; i < dtblsize; i++ ) {
85 lber_pvt_sb_init( &c[i].c_sb );
86 ldap_pvt_thread_mutex_init( &c[i].c_dnmutex );
87 ldap_pvt_thread_mutex_init( &c[i].c_opsmutex );
88 ldap_pvt_thread_mutex_init( &c[i].c_pdumutex );
89 ldap_pvt_thread_cond_init( &c[i].c_wcv );
92 if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
93 Debug( LDAP_DEBUG_ANY, "socket() failed errno %d (%s)", errno,
94 errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
100 if ( setsockopt( tcps, SOL_SOCKET, SO_REUSEADDR, (char *) &i,
101 sizeof(i) ) == -1 ) {
102 Debug( LDAP_DEBUG_ANY, "setsockopt() failed errno %d (%s)",
103 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
107 (void) memset( (void *) &addr, '\0', sizeof(addr) );
108 addr.sin_family = AF_INET;
109 addr.sin_addr.s_addr = INADDR_ANY;
110 addr.sin_port = htons( (int)port );
111 if ( bind( tcps, (struct sockaddr *) &addr, sizeof(addr) ) == -1 ) {
112 Debug( LDAP_DEBUG_ANY, "bind() failed errno %d (%s)\n",
113 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
118 if ( listen( tcps, 5 ) == -1 ) {
119 Debug( LDAP_DEBUG_ANY, "listen() failed errno %d (%s)",
120 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
125 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
127 if (( slapd_pid_file != NULL ) &&
128 (( fp = fopen( slapd_pid_file, "w" )) != NULL )) {
129 fprintf( fp, "%d\n", (int) getpid() );
133 if (( slapd_args_file != NULL ) &&
134 (( fp = fopen( slapd_args_file, "w" )) != NULL )) {
135 for ( i = 0; i < g_argc; i++ ) {
136 fprintf( fp, "%s ", g_argv[i] );
142 while ( !slapd_shutdown ) {
143 struct sockaddr_in from;
153 FD_ZERO( &writefds );
155 FD_SET( tcps, &readfds );
160 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
161 Debug( LDAP_DEBUG_CONNS,
162 "listening for connections on %d, activity on:",
167 ldap_pvt_thread_mutex_lock( &new_conn_mutex );
168 for ( i = 0; i < dtblsize; i++ ) {
169 if ( (c[i].c_state != SLAP_C_INACTIVE)
170 && (c[i].c_state != SLAP_C_CLOSING) )
173 assert(lber_pvt_sb_in_use( &c[i].c_sb ));
175 FD_SET( lber_pvt_sb_get_desc(&c[i].c_sb),
177 if (lber_pvt_sb_data_ready(&c[i].c_sb))
179 if ( c[i].c_writewaiter ) {
180 FD_SET( lber_pvt_sb_get_desc(&c[i].c_sb),
183 Debug( LDAP_DEBUG_CONNS, " %dr%s", i,
184 c[i].c_writewaiter ? "w" : "", 0 );
188 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
189 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
191 Debug( LDAP_DEBUG_CONNS, "before select active_threads %d\n",
192 active_threads, 0, 0 );
193 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
194 tvp = (data_ready) ? &zero : NULL;
196 tvp = (active_threads || data_ready) ? &zero : NULL;
198 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
200 switch ( i = select( dtblsize, &readfds, &writefds, 0, tvp ) ) {
201 case -1: /* failure - try again */
202 Debug( LDAP_DEBUG_CONNS,
203 "select failed errno %d (%s)\n",
204 errno, errno > -1 && errno < sys_nerr ?
205 sys_errlist[errno] : "unknown", 0 );
208 case 0: /* timeout - let threads run */
209 Debug( LDAP_DEBUG_CONNS, "select timeout - yielding\n",
212 ldap_pvt_thread_yield();
215 default: /* something happened - deal with it */
216 Debug( LDAP_DEBUG_CONNS, "select activity on %d descriptors\n", i, 0, 0 );
219 ldap_pvt_thread_mutex_lock( ¤ttime_mutex );
220 time( ¤ttime );
221 ldap_pvt_thread_mutex_unlock( ¤ttime_mutex );
224 ldap_pvt_thread_mutex_lock( &new_conn_mutex );
225 if ( FD_ISSET( tcps, &readfds ) ) {
227 if ( (ns = accept( tcps, (struct sockaddr *) &from,
229 Debug( LDAP_DEBUG_ANY,
230 "accept() failed errno %d (%s)", errno,
231 errno > -1 && errno < sys_nerr ?
232 sys_errlist[errno] : "unknown", 0 );
233 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
237 /* make sure descriptor number isn't too great */
238 if ( ns >= dtblsize ) {
239 Debug( LDAP_DEBUG_ANY,
240 "new connection on %d beyond descriptor table size %d\n",
243 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
247 Debug( LDAP_DEBUG_CONNS, "new connection on %d\n", ns,
252 if ( getpeername( ns, (struct sockaddr *) &from, &len )
255 client_addr = inet_ntoa( from.sin_addr );
257 #if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
258 hp = gethostbyaddr( (char *)
259 &(from.sin_addr.s_addr),
260 sizeof(from.sin_addr.s_addr), AF_INET );
263 client_name = hp->h_name;
265 /* normalize the domain */
266 for ( s = client_name; *s; s++ ) {
267 *s = TOLOWER( (unsigned char) *s );
283 if(!hosts_ctl("slapd",
284 client_name != NULL ? client_name : STRING_UNKNOWN,
285 client_addr != NULL ? client_addr : STRING_UNKNOWN,
289 Statslog( LDAP_DEBUG_ANY,
290 "fd=%d connection from %s (%s) denied.\n",
292 client_name == NULL ? "unknown" : client_name,
293 client_addr == NULL ? "unknown" : client_addr,
296 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
299 #endif /* HAVE_TCPD */
302 ldap_pvt_thread_mutex_lock( &ops_mutex );
303 c[ns].c_connid = num_conns++;
304 ldap_pvt_thread_mutex_unlock( &ops_mutex );
306 Statslog( LDAP_DEBUG_STATS,
307 "conn=%d fd=%d connection from %s (%s) accepted.\n",
309 client_name == NULL ? "unknown" : client_name,
310 client_addr == NULL ? "unknown" : client_addr,
313 if ( c[ns].c_addr != NULL ) {
314 free( c[ns].c_addr );
316 c[ns].c_addr = ch_strdup( client_addr );
318 if ( c[ns].c_domain != NULL ) {
319 free( c[ns].c_domain );
322 c[ns].c_domain = ch_strdup( client_name == NULL
323 ? "" : client_name );
325 ldap_pvt_thread_mutex_lock( &c[ns].c_dnmutex );
326 if ( c[ns].c_dn != NULL ) {
330 if ( c[ns].c_cdn != NULL ) {
334 ldap_pvt_thread_mutex_unlock( &c[ns].c_dnmutex );
336 c[ns].c_starttime = currenttime;
337 c[ns].c_ops_received = 0;
338 c[ns].c_ops_executing = 0;
339 c[ns].c_ops_pending = 0;
340 c[ns].c_ops_completed = 0;
342 lber_pvt_sb_set_desc( &c[ns].c_sb, ns );
343 lber_pvt_sb_set_io( &c[ns].c_sb, &lber_pvt_sb_io_tcp, NULL );
345 if (lber_pvt_sb_set_nonblock( &c[ns].c_sb, 1)<0) {
346 Debug( LDAP_DEBUG_ANY,
347 "FIONBIO ioctl on %d failed\n", ns, 0, 0 );
350 c[ns].c_state = SLAP_C_ACTIVE;
352 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
354 Debug( LDAP_DEBUG_CONNS, "activity on:", 0, 0, 0 );
355 for ( i = 0; i < dtblsize; i++ ) {
358 r = FD_ISSET( i, &readfds );
359 w = FD_ISSET( i, &writefds );
360 if ( i != tcps && (r || w) ) {
361 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
362 r ? "r" : "", w ? "w" : "" );
365 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
367 for ( i = 0; i < dtblsize; i++ ) {
368 if ( i == tcps || (! FD_ISSET( i, &readfds ) &&
369 ! FD_ISSET( i, &writefds )) ) {
373 if ( FD_ISSET( i, &writefds ) ) {
374 Debug( LDAP_DEBUG_CONNS,
375 "signaling write waiter on %d\n", i, 0, 0 );
377 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
379 c[i].c_writewaiter = 0;
380 ldap_pvt_thread_cond_signal( &c[i].c_wcv );
381 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
384 if ( FD_ISSET( i, &readfds ) ||
385 lber_pvt_sb_data_ready( &c[i].c_sb ) ) {
386 Debug( LDAP_DEBUG_CONNS,
387 "read activity on %d\n", i, 0, 0 );
389 connection_activity( &c[i] );
393 ldap_pvt_thread_yield();
396 Debug( LDAP_DEBUG_TRACE,
397 "slapd shutdown: shutdown initiated.\n",
402 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
403 Debug( LDAP_DEBUG_ANY,
404 "slapd shutdown: waiting for %d threads to terminate\n",
405 active_threads, 0, 0 );
406 while ( active_threads > 0 ) {
407 ldap_pvt_thread_cond_wait(&active_threads_cond, &active_threads_mutex);
409 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
415 slap_set_shutdown( int sig )
417 Debug( LDAP_DEBUG_ANY, "slapd got shutdown signal %d\n", sig, 0, 0 );
419 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
422 (void) SIGNAL( sig, slap_set_shutdown );
426 slap_do_nothing( int sig )
428 Debug( LDAP_DEBUG_TRACE, "slapd got do_nothing signal %d\n", sig, 0, 0 );
431 (void) SIGNAL( sig, slap_do_nothing );