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 (void) SIGNAL( LDAP_SIGUSR1, do_nothing );
131 (void) SIGNAL( LDAP_SIGUSR2, set_shutdown );
132 (void) SIGNAL( SIGTERM, set_shutdown );
133 (void) SIGNAL( SIGINT, set_shutdown );
134 (void) SIGNAL( SIGHUP, set_shutdown );
136 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
138 if ( (fp = fopen( SLAPD_PIDFILE, "w" )) != NULL ) {
139 fprintf( fp, "%d\n", (int) getpid() );
143 #ifdef SLAPD_ARGSFILE
144 if ( (fp = fopen( SLAPD_ARGSFILE, "w" )) != NULL ) {
145 for ( i = 0; i < g_argc; i++ ) {
146 fprintf( fp, "%s ", g_argv[i] );
153 while ( !slapd_shutdown ) {
154 struct sockaddr_in from;
163 FD_ZERO( &writefds );
165 FD_SET( tcps, &readfds );
170 pthread_mutex_lock( &active_threads_mutex );
171 Debug( LDAP_DEBUG_CONNS,
172 "listening for connections on %d, activity on:",
175 pthread_mutex_lock( &new_conn_mutex );
176 for ( i = 0; i < dtblsize; i++ ) {
177 if ( c[i].c_sb.sb_sd != -1 ) {
178 FD_SET( c[i].c_sb.sb_sd, &readfds );
180 if ( c[i].c_writewaiter ) {
181 FD_SET( c[i].c_sb.sb_sd, &writefds );
183 Debug( LDAP_DEBUG_CONNS, " %dr%s", i,
184 c[i].c_writewaiter ? "w" : "", 0 );
187 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
188 pthread_mutex_unlock( &new_conn_mutex );
190 Debug( LDAP_DEBUG_CONNS, "before select active_threads %d\n",
191 active_threads, 0, 0 );
192 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
195 tvp = active_threads ? &zero : NULL;
197 pthread_mutex_unlock( &active_threads_mutex );
199 switch ( i = select( dtblsize, &readfds, &writefds, 0, tvp ) ) {
200 case -1: /* failure - try again */
201 Debug( LDAP_DEBUG_CONNS,
202 "select failed errno %d (%s)\n",
203 errno, errno > -1 && errno < sys_nerr ?
204 sys_errlist[errno] : "unknown", 0 );
207 case 0: /* timeout - let threads run */
208 Debug( LDAP_DEBUG_CONNS, "select timeout - yielding\n",
213 default: /* something happened - deal with it */
214 Debug( LDAP_DEBUG_CONNS, "select activity on %d descriptors\n", i, 0, 0 );
217 pthread_mutex_lock( ¤ttime_mutex );
218 time( ¤ttime );
219 pthread_mutex_unlock( ¤ttime_mutex );
222 pthread_mutex_lock( &new_conn_mutex );
223 if ( FD_ISSET( tcps, &readfds ) ) {
225 if ( (ns = accept( tcps, (struct sockaddr *) &from,
227 Debug( LDAP_DEBUG_ANY,
228 "accept() failed errno %d (%s)", errno,
229 errno > -1 && errno < sys_nerr ?
230 sys_errlist[errno] : "unknown", 0 );
231 pthread_mutex_unlock( &new_conn_mutex );
234 if ( ioctl( ns, FIONBIO, (caddr_t) &on ) == -1 ) {
235 Debug( LDAP_DEBUG_ANY,
236 "FIONBIO ioctl on %d failed\n", ns, 0, 0 );
239 c[ns].c_sb.sb_sd = ns;
240 Debug( LDAP_DEBUG_CONNS, "new connection on %d\n", ns,
243 pthread_mutex_lock( &ops_mutex );
244 c[ns].c_connid = num_conns++;
245 pthread_mutex_unlock( &ops_mutex );
249 if ( getpeername( ns, (struct sockaddr *) &from, &len )
252 client_addr = inet_ntoa( from.sin_addr );
254 #if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
255 hp = gethostbyaddr( (char *)
256 &(from.sin_addr.s_addr),
257 sizeof(from.sin_addr.s_addr), AF_INET );
260 client_name = hp->h_name;
262 /* normalize the domain */
263 for ( s = client_name; *s; s++ ) {
280 if(!hosts_ctl("slapd",
281 client_name != NULL ? client_name : STRING_UNKNOWN,
282 client_addr != NULL ? client_addr : STRING_UNKNOWN,
286 Statslog( LDAP_DEBUG_STATS,
287 "conn=%d fd=%d connection from %s (%s) denied.\n",
289 client_name == NULL ? "unknown" : client_name,
290 client_addr == NULL ? "unknown" : client_addr,
294 pthread_mutex_unlock( &new_conn_mutex );
297 #endif /* HAVE_TCPD */
299 Statslog( LDAP_DEBUG_STATS,
300 "conn=%d fd=%d connection from %s (%s) accepted.\n",
302 client_name == NULL ? "unknown" : client_name,
303 client_addr == NULL ? "unknown" : client_addr,
306 if ( c[ns].c_addr != NULL ) {
307 free( c[ns].c_addr );
309 c[ns].c_addr = ch_strdup( client_addr );
311 if ( c[ns].c_domain != NULL ) {
312 free( c[ns].c_domain );
315 c[ns].c_domain = ch_strdup( client_name == NULL
316 ? "" : client_name );
318 pthread_mutex_lock( &c[ns].c_dnmutex );
319 if ( c[ns].c_dn != NULL ) {
323 pthread_mutex_unlock( &c[ns].c_dnmutex );
324 c[ns].c_starttime = currenttime;
325 c[ns].c_opsinitiated = 0;
326 c[ns].c_opscompleted = 0;
328 pthread_mutex_unlock( &new_conn_mutex );
330 Debug( LDAP_DEBUG_CONNS, "activity on:", 0, 0, 0 );
331 for ( i = 0; i < dtblsize; i++ ) {
334 r = FD_ISSET( i, &readfds );
335 w = FD_ISSET( i, &writefds );
336 if ( i != tcps && (r || w) ) {
337 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
338 r ? "r" : "", w ? "w" : "" );
341 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
343 for ( i = 0; i < dtblsize; i++ ) {
344 if ( i == tcps || (! FD_ISSET( i, &readfds ) &&
345 ! FD_ISSET( i, &writefds )) ) {
349 if ( FD_ISSET( i, &writefds ) ) {
350 Debug( LDAP_DEBUG_CONNS,
351 "signaling write waiter on %d\n", i, 0, 0 );
353 pthread_mutex_lock( &active_threads_mutex );
354 pthread_cond_signal( &c[i].c_wcv );
355 c[i].c_writewaiter = 0;
357 pthread_mutex_unlock( &active_threads_mutex );
360 if ( FD_ISSET( i, &readfds ) ) {
361 Debug( LDAP_DEBUG_CONNS,
362 "read activity on %d\n", i, 0, 0 );
364 connection_activity( &c[i] );
372 pthread_mutex_lock( &active_threads_mutex );
373 Debug( LDAP_DEBUG_ANY,
374 "slapd shutting down - waiting for %d threads to terminate\n",
375 active_threads, 0, 0 );
376 while ( active_threads > 0 ) {
377 pthread_mutex_unlock( &active_threads_mutex );
379 pthread_mutex_lock( &active_threads_mutex );
381 pthread_mutex_unlock( &active_threads_mutex );
383 /* let backends do whatever cleanup they need to do */
384 Debug( LDAP_DEBUG_TRACE,
385 "slapd shutting down - waiting for backends to close down\n", 0, 0,
388 Debug( LDAP_DEBUG_ANY, "slapd stopping\n", 0, 0, 0 );
393 set_shutdown( int sig )
395 Debug( LDAP_DEBUG_ANY, "slapd got shutdown signal %d\n", sig, 0, 0 );
397 pthread_kill( listener_tid, LDAP_SIGUSR1 );
398 (void) SIGNAL( LDAP_SIGUSR2, set_shutdown );
399 (void) SIGNAL( SIGTERM, set_shutdown );
400 (void) SIGNAL( SIGINT, set_shutdown );
401 (void) SIGNAL( SIGHUP, set_shutdown );
405 do_nothing( int sig )
407 Debug( LDAP_DEBUG_TRACE, "slapd got do_nothing signal %d\n", sig, 0, 0 );
408 (void) SIGNAL( LDAP_SIGUSR1, do_nothing );