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 int allow_severity = LOG_INFO;
33 int deny_severity = LOG_NOTICE;
34 #endif /* TCP Wrappers */
39 static volatile sig_atomic_t slapd_shutdown = 0;
40 static void set_shutdown(int sig);
41 static void do_nothing (int sig);
43 /* a link to the slapd.conf configuration parameters */
44 extern char *slapd_pid_file;
45 extern char *slapd_args_file;
54 unsigned long len, tag, msgid;
57 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++ ) {
86 c[i].c_sb.sb_options = LBER_NO_READ_AHEAD;
87 c[i].c_sb.sb_naddr = 0;
88 c[i].c_sb.sb_ber.ber_buf = NULL;
89 c[i].c_sb.sb_ber.ber_ptr = NULL;
90 c[i].c_sb.sb_ber.ber_end = NULL;
91 c[i].c_writewaiter = 0;
93 ldap_pvt_thread_mutex_init( &c[i].c_dnmutex );
94 ldap_pvt_thread_mutex_init( &c[i].c_opsmutex );
95 ldap_pvt_thread_mutex_init( &c[i].c_pdumutex );
96 ldap_pvt_thread_cond_init( &c[i].c_wcv );
99 if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
100 Debug( LDAP_DEBUG_ANY, "socket() failed errno %d (%s)", errno,
101 errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
107 if ( setsockopt( tcps, SOL_SOCKET, SO_REUSEADDR, (char *) &i,
108 sizeof(i) ) == -1 ) {
109 Debug( LDAP_DEBUG_ANY, "setsockopt() failed errno %d (%s)",
110 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
114 (void) memset( (void *) &addr, '\0', sizeof(addr) );
115 addr.sin_family = AF_INET;
116 addr.sin_addr.s_addr = INADDR_ANY;
117 addr.sin_port = htons( (int)port );
118 if ( bind( tcps, (struct sockaddr *) &addr, sizeof(addr) ) == -1 ) {
119 Debug( LDAP_DEBUG_ANY, "bind() failed errno %d (%s)\n",
120 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
125 if ( listen( tcps, 5 ) == -1 ) {
126 Debug( LDAP_DEBUG_ANY, "listen() failed errno %d (%s)",
127 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
132 (void) SIGNAL( SIGPIPE, SIG_IGN );
133 (void) SIGNAL( LDAP_SIGUSR1, do_nothing );
134 (void) SIGNAL( LDAP_SIGUSR2, set_shutdown );
135 (void) SIGNAL( SIGTERM, set_shutdown );
136 (void) SIGNAL( SIGINT, set_shutdown );
137 (void) SIGNAL( SIGHUP, set_shutdown );
139 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
141 if (( slapd_pid_file != NULL ) &&
142 (( fp = fopen( slapd_pid_file, "w" )) != NULL )) {
143 fprintf( fp, "%d\n", (int) getpid() );
147 if (( slapd_args_file != NULL ) &&
148 (( fp = fopen( slapd_args_file, "w" )) != NULL )) {
149 for ( i = 0; i < g_argc; i++ ) {
150 fprintf( fp, "%s ", g_argv[i] );
156 while ( !slapd_shutdown ) {
157 struct sockaddr_in from;
166 FD_ZERO( &writefds );
168 FD_SET( tcps, &readfds );
173 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
174 Debug( LDAP_DEBUG_CONNS,
175 "listening for connections on %d, activity on:",
178 ldap_pvt_thread_mutex_lock( &new_conn_mutex );
179 for ( i = 0; i < dtblsize; i++ ) {
180 if ( c[i].c_sb.sb_sd != -1 ) {
181 FD_SET( c[i].c_sb.sb_sd, &readfds );
183 if ( c[i].c_writewaiter ) {
184 FD_SET( c[i].c_sb.sb_sd, &writefds );
186 Debug( LDAP_DEBUG_CONNS, " %dr%s", i,
187 c[i].c_writewaiter ? "w" : "", 0 );
190 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
191 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
193 Debug( LDAP_DEBUG_CONNS, "before select active_threads %d\n",
194 active_threads, 0, 0 );
195 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
198 tvp = active_threads ? &zero : NULL;
200 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
202 switch ( i = select( dtblsize, &readfds, &writefds, 0, tvp ) ) {
203 case -1: /* failure - try again */
204 Debug( LDAP_DEBUG_CONNS,
205 "select failed errno %d (%s)\n",
206 errno, errno > -1 && errno < sys_nerr ?
207 sys_errlist[errno] : "unknown", 0 );
210 case 0: /* timeout - let threads run */
211 Debug( LDAP_DEBUG_CONNS, "select timeout - yielding\n",
213 ldap_pvt_thread_yield();
216 default: /* something happened - deal with it */
217 Debug( LDAP_DEBUG_CONNS, "select activity on %d descriptors\n", i, 0, 0 );
220 ldap_pvt_thread_mutex_lock( ¤ttime_mutex );
221 time( ¤ttime );
222 ldap_pvt_thread_mutex_unlock( ¤ttime_mutex );
225 ldap_pvt_thread_mutex_lock( &new_conn_mutex );
226 if ( FD_ISSET( tcps, &readfds ) ) {
228 if ( (ns = accept( tcps, (struct sockaddr *) &from,
230 Debug( LDAP_DEBUG_ANY,
231 "accept() failed errno %d (%s)", errno,
232 errno > -1 && errno < sys_nerr ?
233 sys_errlist[errno] : "unknown", 0 );
234 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
237 if ( ioctl( ns, FIONBIO, (caddr_t) &on ) == -1 ) {
238 Debug( LDAP_DEBUG_ANY,
239 "FIONBIO ioctl on %d failed\n", ns, 0, 0 );
242 Debug( LDAP_DEBUG_CONNS, "new connection on %d\n", ns,
247 if ( getpeername( ns, (struct sockaddr *) &from, &len )
250 client_addr = inet_ntoa( from.sin_addr );
252 #if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
253 hp = gethostbyaddr( (char *)
254 &(from.sin_addr.s_addr),
255 sizeof(from.sin_addr.s_addr), AF_INET );
258 client_name = hp->h_name;
260 /* normalize the domain */
261 for ( s = client_name; *s; s++ ) {
278 if(!hosts_ctl("slapd",
279 client_name != NULL ? client_name : STRING_UNKNOWN,
280 client_addr != NULL ? client_addr : STRING_UNKNOWN,
284 Statslog( LDAP_DEBUG_ANY,
285 "fd=%d connection from %s (%s) denied.\n",
287 client_name == NULL ? "unknown" : client_name,
288 client_addr == NULL ? "unknown" : client_addr,
292 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
295 #endif /* HAVE_TCPD */
297 c[ns].c_sb.sb_sd = ns;
298 ldap_pvt_thread_mutex_lock( &ops_mutex );
299 c[ns].c_connid = num_conns++;
300 ldap_pvt_thread_mutex_unlock( &ops_mutex );
302 Statslog( LDAP_DEBUG_STATS,
303 "conn=%d fd=%d connection from %s (%s) accepted.\n",
305 client_name == NULL ? "unknown" : client_name,
306 client_addr == NULL ? "unknown" : client_addr,
309 if ( c[ns].c_addr != NULL ) {
310 free( c[ns].c_addr );
312 c[ns].c_addr = ch_strdup( client_addr );
314 if ( c[ns].c_domain != NULL ) {
315 free( c[ns].c_domain );
318 c[ns].c_domain = ch_strdup( client_name == NULL
319 ? "" : client_name );
321 ldap_pvt_thread_mutex_lock( &c[ns].c_dnmutex );
322 if ( c[ns].c_dn != NULL ) {
326 if ( c[ns].c_cdn != NULL ) {
330 ldap_pvt_thread_mutex_unlock( &c[ns].c_dnmutex );
331 c[ns].c_starttime = currenttime;
332 c[ns].c_opsinitiated = 0;
333 c[ns].c_opscompleted = 0;
335 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
337 Debug( LDAP_DEBUG_CONNS, "activity on:", 0, 0, 0 );
338 for ( i = 0; i < dtblsize; i++ ) {
341 r = FD_ISSET( i, &readfds );
342 w = FD_ISSET( i, &writefds );
343 if ( i != tcps && (r || w) ) {
344 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
345 r ? "r" : "", w ? "w" : "" );
348 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
350 for ( i = 0; i < dtblsize; i++ ) {
351 if ( i == tcps || (! FD_ISSET( i, &readfds ) &&
352 ! FD_ISSET( i, &writefds )) ) {
356 if ( FD_ISSET( i, &writefds ) ) {
357 Debug( LDAP_DEBUG_CONNS,
358 "signaling write waiter on %d\n", i, 0, 0 );
360 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
362 c[i].c_writewaiter = 0;
363 ldap_pvt_thread_cond_signal( &c[i].c_wcv );
364 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
367 if ( FD_ISSET( i, &readfds ) ) {
368 Debug( LDAP_DEBUG_CONNS,
369 "read activity on %d\n", i, 0, 0 );
371 connection_activity( &c[i] );
375 ldap_pvt_thread_yield();
380 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
381 Debug( LDAP_DEBUG_ANY,
382 "slapd shutting down - waiting for %d threads to terminate\n",
383 active_threads, 0, 0 );
384 while ( active_threads > 0 ) {
385 ldap_pvt_thread_cond_wait(&active_threads_cond, &active_threads_mutex);
387 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
389 /* let backends do whatever cleanup they need to do */
390 Debug( LDAP_DEBUG_TRACE,
391 "slapd shutting down - waiting for backends to close down\n", 0, 0,
394 Debug( LDAP_DEBUG_ANY, "slapd stopping\n", 0, 0, 0 );
399 set_shutdown( int sig )
401 Debug( LDAP_DEBUG_ANY, "slapd got shutdown signal %d\n", sig, 0, 0 );
403 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
404 (void) SIGNAL( LDAP_SIGUSR2, set_shutdown );
405 (void) SIGNAL( SIGTERM, set_shutdown );
406 (void) SIGNAL( SIGINT, set_shutdown );
407 (void) SIGNAL( SIGHUP, set_shutdown );
411 do_nothing( int sig )
413 Debug( LDAP_DEBUG_TRACE, "slapd got do_nothing signal %d\n", sig, 0, 0 );
414 (void) SIGNAL( LDAP_SIGUSR1, do_nothing );