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 void set_shutdown(int sig);
40 static void do_nothing (int sig);
49 unsigned long len, tag, msgid;
52 struct sockaddr_in addr;
59 dtblsize = sysconf( _SC_OPEN_MAX );
60 #elif HAVE_GETDTABLESIZE
61 dtblsize = getdtablesize();
67 if(dtblsize > FD_SETSIZE) {
68 dtblsize = FD_SETSIZE;
70 #endif /* !FD_SETSIZE */
72 c = (Connection *) ch_calloc( 1, dtblsize * sizeof(Connection) );
74 for ( i = 0; i < dtblsize; i++ ) {
80 c[i].c_sb.sb_options = LBER_NO_READ_AHEAD;
81 c[i].c_sb.sb_naddr = 0;
82 c[i].c_sb.sb_ber.ber_buf = NULL;
83 c[i].c_sb.sb_ber.ber_ptr = NULL;
84 c[i].c_sb.sb_ber.ber_end = NULL;
85 c[i].c_writewaiter = 0;
87 pthread_mutex_init( &c[i].c_dnmutex,
88 pthread_mutexattr_default );
89 pthread_mutex_init( &c[i].c_opsmutex,
90 pthread_mutexattr_default );
91 pthread_mutex_init( &c[i].c_pdumutex,
92 pthread_mutexattr_default );
93 pthread_cond_init( &c[i].c_wcv, pthread_condattr_default );
96 if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
97 Debug( LDAP_DEBUG_ANY, "socket() failed errno %d (%s)", errno,
98 errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
104 if ( setsockopt( tcps, SOL_SOCKET, SO_REUSEADDR, (char *) &i,
105 sizeof(i) ) == -1 ) {
106 Debug( LDAP_DEBUG_ANY, "setsockopt() failed errno %d (%s)",
107 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
111 (void) memset( (void *) &addr, '\0', sizeof(addr) );
112 addr.sin_family = AF_INET;
113 addr.sin_addr.s_addr = INADDR_ANY;
114 addr.sin_port = htons( (int)port );
115 if ( bind( tcps, (struct sockaddr *) &addr, sizeof(addr) ) == -1 ) {
116 Debug( LDAP_DEBUG_ANY, "bind() failed errno %d (%s)\n",
117 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
122 if ( listen( tcps, 5 ) == -1 ) {
123 Debug( LDAP_DEBUG_ANY, "listen() failed errno %d (%s)",
124 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
129 (void) SIGNAL( SIGPIPE, SIG_IGN );
130 #ifdef HAVE_LINUX_THREADS
132 * LinuxThreads are implemented using SIGUSR1/USR2,
133 * so we'll use SIGSTKFLT and SIGUNUSED
135 (void) SIGNAL( SIGSTKFLT, do_nothing );
136 (void) SIGNAL( SIGUNUSED, set_shutdown );
138 (void) SIGNAL( SIGUSR1, do_nothing );
139 (void) SIGNAL( SIGUSR2, set_shutdown );
141 (void) SIGNAL( SIGTERM, set_shutdown );
142 (void) SIGNAL( SIGINT, set_shutdown );
143 (void) SIGNAL( SIGHUP, set_shutdown );
145 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
147 if ( (fp = fopen( SLAPD_PIDFILE, "w" )) != NULL ) {
148 fprintf( fp, "%d\n", (int) getpid() );
152 #ifdef SLAPD_ARGSFILE
153 if ( (fp = fopen( SLAPD_ARGSFILE, "w" )) != NULL ) {
154 for ( i = 0; i < g_argc; i++ ) {
155 fprintf( fp, "%s ", g_argv[i] );
162 while ( !slapd_shutdown ) {
163 struct sockaddr_in from;
172 FD_ZERO( &writefds );
174 FD_SET( tcps, &readfds );
179 pthread_mutex_lock( &active_threads_mutex );
180 Debug( LDAP_DEBUG_CONNS,
181 "listening for connections on %d, activity on:",
184 pthread_mutex_lock( &new_conn_mutex );
185 for ( i = 0; i < dtblsize; i++ ) {
186 if ( c[i].c_sb.sb_sd != -1 ) {
187 FD_SET( c[i].c_sb.sb_sd, &readfds );
189 if ( c[i].c_writewaiter ) {
190 FD_SET( c[i].c_sb.sb_sd, &writefds );
192 Debug( LDAP_DEBUG_CONNS, " %dr%s", i,
193 c[i].c_writewaiter ? "w" : "", 0 );
196 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
197 pthread_mutex_unlock( &new_conn_mutex );
199 Debug( LDAP_DEBUG_CONNS, "before select active_threads %d\n",
200 active_threads, 0, 0 );
201 #ifdef PREEMPTIVE_THREADS
204 tvp = active_threads ? &zero : NULL;
206 pthread_mutex_unlock( &active_threads_mutex );
208 switch ( i = select( dtblsize, &readfds, &writefds, 0, tvp ) ) {
209 case -1: /* failure - try again */
210 Debug( LDAP_DEBUG_CONNS,
211 "select failed errno %d (%s)\n",
212 errno, errno > -1 && errno < sys_nerr ?
213 sys_errlist[errno] : "unknown", 0 );
216 case 0: /* timeout - let threads run */
217 Debug( LDAP_DEBUG_CONNS, "select timeout - yielding\n",
222 default: /* something happened - deal with it */
223 Debug( LDAP_DEBUG_CONNS, "select activity on %d descriptors\n", i, 0, 0 );
226 pthread_mutex_lock( ¤ttime_mutex );
227 time( ¤ttime );
228 pthread_mutex_unlock( ¤ttime_mutex );
231 pthread_mutex_lock( &new_conn_mutex );
232 if ( FD_ISSET( tcps, &readfds ) ) {
234 if ( (ns = accept( tcps, (struct sockaddr *) &from,
236 Debug( LDAP_DEBUG_ANY,
237 "accept() failed errno %d (%s)", errno,
238 errno > -1 && errno < sys_nerr ?
239 sys_errlist[errno] : "unknown", 0 );
240 pthread_mutex_unlock( &new_conn_mutex );
243 if ( ioctl( ns, FIONBIO, (caddr_t) &on ) == -1 ) {
244 Debug( LDAP_DEBUG_ANY,
245 "FIONBIO ioctl on %d failed\n", ns, 0, 0 );
248 c[ns].c_sb.sb_sd = ns;
249 Debug( LDAP_DEBUG_CONNS, "new connection on %d\n", ns,
252 pthread_mutex_lock( &ops_mutex );
253 c[ns].c_connid = num_conns++;
254 pthread_mutex_unlock( &ops_mutex );
258 if ( getpeername( ns, (struct sockaddr *) &from, &len )
261 client_addr = inet_ntoa( from.sin_addr );
263 #if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
264 hp = gethostbyaddr( (char *)
265 &(from.sin_addr.s_addr),
266 sizeof(from.sin_addr.s_addr), AF_INET );
269 client_name = hp->h_name;
271 /* normalize the domain */
272 for ( s = client_name; *s; s++ ) {
289 if(!hosts_ctl("slapd", client_name, client_addr,
293 Statslog( LDAP_DEBUG_STATS,
294 "conn=%d fd=%d connection from %s (%s) denied.\n",
296 client_name == NULL ? "unknown" : client_name,
297 client_addr == NULL ? "unknown" : client_addr,
301 pthread_mutex_unlock( &new_conn_mutex );
304 #endif /* HAVE_TCPD */
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 = strdup( client_addr );
318 if ( c[ns].c_domain != NULL ) {
319 free( c[ns].c_domain );
322 c[ns].c_domain = strdup( client_name == NULL
323 ? "" : client_name );
325 pthread_mutex_lock( &c[ns].c_dnmutex );
326 if ( c[ns].c_dn != NULL ) {
330 pthread_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 pthread_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 pthread_mutex_lock( &active_threads_mutex );
361 pthread_cond_signal( &c[i].c_wcv );
362 c[i].c_writewaiter = 0;
364 pthread_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] );
379 pthread_mutex_lock( &active_threads_mutex );
380 Debug( LDAP_DEBUG_ANY,
381 "slapd shutting down - waiting for %d threads to terminate\n",
382 active_threads, 0, 0 );
383 while ( active_threads > 0 ) {
384 pthread_mutex_unlock( &active_threads_mutex );
386 pthread_mutex_lock( &active_threads_mutex );
388 pthread_mutex_unlock( &active_threads_mutex );
390 /* let backends do whatever cleanup they need to do */
391 Debug( LDAP_DEBUG_TRACE,
392 "slapd shutting down - waiting for backends to close down\n", 0, 0,
395 Debug( LDAP_DEBUG_ANY, "slapd stopping\n", 0, 0, 0 );
400 set_shutdown( int sig )
402 Debug( LDAP_DEBUG_ANY, "slapd got shutdown signal %d\n", sig, 0, 0 );
404 #ifdef HAVE_LINUX_THREADS
406 * LinuxThreads are implemented using SIGUSR1/USR2,
407 * so we'll use SIGSTKFLT and SIGUNUSED
409 pthread_kill( listener_tid, SIGSTKFLT );
410 (void) SIGNAL( SIGUNUSED, set_shutdown );
412 pthread_kill( listener_tid, SIGUSR1 );
413 (void) SIGNAL( SIGUSR2, set_shutdown );
415 (void) SIGNAL( SIGTERM, set_shutdown );
416 (void) SIGNAL( SIGINT, set_shutdown );
417 (void) SIGNAL( SIGHUP, set_shutdown );
421 do_nothing( int sig )
423 Debug( LDAP_DEBUG_TRACE, "slapd got do_nothing signal %d\n", sig, 0, 0 );
424 #ifdef HAVE_LINUX_THREADS
426 * LinuxThreads are implemented using SIGUSR1/USR2,
427 * so we'll use SIGSTKFLT and SIGUNUSED
429 (void) SIGNAL( SIGSTKFLT, do_nothing );
431 (void) SIGNAL( SIGUSR1, do_nothing );