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);
50 unsigned long len, tag, msgid;
53 struct sockaddr_in addr;
60 dtblsize = sysconf( _SC_OPEN_MAX );
61 #elif HAVE_GETDTABLESIZE
62 dtblsize = getdtablesize();
68 if(dtblsize > FD_SETSIZE) {
69 dtblsize = FD_SETSIZE;
71 #endif /* !FD_SETSIZE */
73 c = (Connection *) ch_calloc( 1, dtblsize * sizeof(Connection) );
75 for ( i = 0; i < dtblsize; i++ ) {
81 c[i].c_sb.sb_options = LBER_NO_READ_AHEAD;
82 c[i].c_sb.sb_naddr = 0;
83 c[i].c_sb.sb_ber.ber_buf = NULL;
84 c[i].c_sb.sb_ber.ber_ptr = NULL;
85 c[i].c_sb.sb_ber.ber_end = NULL;
86 c[i].c_writewaiter = 0;
88 pthread_mutex_init( &c[i].c_dnmutex,
89 pthread_mutexattr_default );
90 pthread_mutex_init( &c[i].c_opsmutex,
91 pthread_mutexattr_default );
92 pthread_mutex_init( &c[i].c_pdumutex,
93 pthread_mutexattr_default );
94 pthread_cond_init( &c[i].c_wcv, pthread_condattr_default );
97 if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
98 Debug( LDAP_DEBUG_ANY, "socket() failed errno %d (%s)", errno,
99 errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
105 if ( setsockopt( tcps, SOL_SOCKET, SO_REUSEADDR, (char *) &i,
106 sizeof(i) ) == -1 ) {
107 Debug( LDAP_DEBUG_ANY, "setsockopt() failed errno %d (%s)",
108 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
112 (void) memset( (void *) &addr, '\0', sizeof(addr) );
113 addr.sin_family = AF_INET;
114 addr.sin_addr.s_addr = INADDR_ANY;
115 addr.sin_port = htons( (int)port );
116 if ( bind( tcps, (struct sockaddr *) &addr, sizeof(addr) ) == -1 ) {
117 Debug( LDAP_DEBUG_ANY, "bind() failed errno %d (%s)\n",
118 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
123 if ( listen( tcps, 5 ) == -1 ) {
124 Debug( LDAP_DEBUG_ANY, "listen() failed errno %d (%s)",
125 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
130 (void) SIGNAL( SIGPIPE, SIG_IGN );
131 (void) SIGNAL( LDAP_SIGUSR1, do_nothing );
132 (void) SIGNAL( LDAP_SIGUSR2, set_shutdown );
133 (void) SIGNAL( SIGTERM, set_shutdown );
134 (void) SIGNAL( SIGINT, set_shutdown );
135 (void) SIGNAL( SIGHUP, set_shutdown );
137 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
139 if ( (fp = fopen( SLAPD_PIDFILE, "w" )) != NULL ) {
140 fprintf( fp, "%d\n", (int) getpid() );
144 #ifdef SLAPD_ARGSFILE
145 if ( (fp = fopen( SLAPD_ARGSFILE, "w" )) != NULL ) {
146 for ( i = 0; i < g_argc; i++ ) {
147 fprintf( fp, "%s ", g_argv[i] );
154 while ( !slapd_shutdown ) {
155 struct sockaddr_in from;
164 FD_ZERO( &writefds );
166 FD_SET( tcps, &readfds );
171 pthread_mutex_lock( &active_threads_mutex );
172 Debug( LDAP_DEBUG_CONNS,
173 "listening for connections on %d, activity on:",
176 pthread_mutex_lock( &new_conn_mutex );
177 for ( i = 0; i < dtblsize; i++ ) {
178 if ( c[i].c_sb.sb_sd != -1 ) {
179 FD_SET( c[i].c_sb.sb_sd, &readfds );
181 if ( c[i].c_writewaiter ) {
182 FD_SET( c[i].c_sb.sb_sd, &writefds );
184 Debug( LDAP_DEBUG_CONNS, " %dr%s", i,
185 c[i].c_writewaiter ? "w" : "", 0 );
188 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
189 pthread_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 )
196 tvp = active_threads ? &zero : NULL;
198 pthread_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",
214 default: /* something happened - deal with it */
215 Debug( LDAP_DEBUG_CONNS, "select activity on %d descriptors\n", i, 0, 0 );
218 pthread_mutex_lock( ¤ttime_mutex );
219 time( ¤ttime );
220 pthread_mutex_unlock( ¤ttime_mutex );
223 pthread_mutex_lock( &new_conn_mutex );
224 if ( FD_ISSET( tcps, &readfds ) ) {
226 if ( (ns = accept( tcps, (struct sockaddr *) &from,
228 Debug( LDAP_DEBUG_ANY,
229 "accept() failed errno %d (%s)", errno,
230 errno > -1 && errno < sys_nerr ?
231 sys_errlist[errno] : "unknown", 0 );
232 pthread_mutex_unlock( &new_conn_mutex );
235 if ( ioctl( ns, FIONBIO, (caddr_t) &on ) == -1 ) {
236 Debug( LDAP_DEBUG_ANY,
237 "FIONBIO ioctl on %d failed\n", ns, 0, 0 );
240 c[ns].c_sb.sb_sd = ns;
241 Debug( LDAP_DEBUG_CONNS, "new connection on %d\n", ns,
244 pthread_mutex_lock( &ops_mutex );
245 c[ns].c_connid = num_conns++;
246 pthread_mutex_unlock( &ops_mutex );
250 if ( getpeername( ns, (struct sockaddr *) &from, &len )
253 client_addr = inet_ntoa( from.sin_addr );
255 #if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
256 hp = gethostbyaddr( (char *)
257 &(from.sin_addr.s_addr),
258 sizeof(from.sin_addr.s_addr), AF_INET );
261 client_name = hp->h_name;
263 /* normalize the domain */
264 for ( s = client_name; *s; s++ ) {
281 if(!hosts_ctl("slapd", client_name, client_addr,
285 Statslog( LDAP_DEBUG_STATS,
286 "conn=%d fd=%d connection from %s (%s) denied.\n",
288 client_name == NULL ? "unknown" : client_name,
289 client_addr == NULL ? "unknown" : client_addr,
293 pthread_mutex_unlock( &new_conn_mutex );
296 #endif /* HAVE_TCPD */
298 Statslog( LDAP_DEBUG_STATS,
299 "conn=%d fd=%d connection from %s (%s) accepted.\n",
301 client_name == NULL ? "unknown" : client_name,
302 client_addr == NULL ? "unknown" : client_addr,
305 if ( c[ns].c_addr != NULL ) {
306 free( c[ns].c_addr );
308 c[ns].c_addr = ch_strdup( client_addr );
310 if ( c[ns].c_domain != NULL ) {
311 free( c[ns].c_domain );
314 c[ns].c_domain = ch_strdup( client_name == NULL
315 ? "" : client_name );
317 pthread_mutex_lock( &c[ns].c_dnmutex );
318 if ( c[ns].c_dn != NULL ) {
322 pthread_mutex_unlock( &c[ns].c_dnmutex );
323 c[ns].c_starttime = currenttime;
324 c[ns].c_opsinitiated = 0;
325 c[ns].c_opscompleted = 0;
327 pthread_mutex_unlock( &new_conn_mutex );
329 Debug( LDAP_DEBUG_CONNS, "activity on:", 0, 0, 0 );
330 for ( i = 0; i < dtblsize; i++ ) {
333 r = FD_ISSET( i, &readfds );
334 w = FD_ISSET( i, &writefds );
335 if ( i != tcps && (r || w) ) {
336 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
337 r ? "r" : "", w ? "w" : "" );
340 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
342 for ( i = 0; i < dtblsize; i++ ) {
343 if ( i == tcps || (! FD_ISSET( i, &readfds ) &&
344 ! FD_ISSET( i, &writefds )) ) {
348 if ( FD_ISSET( i, &writefds ) ) {
349 Debug( LDAP_DEBUG_CONNS,
350 "signaling write waiter on %d\n", i, 0, 0 );
352 pthread_mutex_lock( &active_threads_mutex );
353 pthread_cond_signal( &c[i].c_wcv );
354 c[i].c_writewaiter = 0;
356 pthread_mutex_unlock( &active_threads_mutex );
359 if ( FD_ISSET( i, &readfds ) ) {
360 Debug( LDAP_DEBUG_CONNS,
361 "read activity on %d\n", i, 0, 0 );
363 connection_activity( &c[i] );
371 pthread_mutex_lock( &active_threads_mutex );
372 Debug( LDAP_DEBUG_ANY,
373 "slapd shutting down - waiting for %d threads to terminate\n",
374 active_threads, 0, 0 );
375 while ( active_threads > 0 ) {
376 pthread_mutex_unlock( &active_threads_mutex );
378 pthread_mutex_lock( &active_threads_mutex );
380 pthread_mutex_unlock( &active_threads_mutex );
382 /* let backends do whatever cleanup they need to do */
383 Debug( LDAP_DEBUG_TRACE,
384 "slapd shutting down - waiting for backends to close down\n", 0, 0,
387 Debug( LDAP_DEBUG_ANY, "slapd stopping\n", 0, 0, 0 );
392 set_shutdown( int sig )
394 Debug( LDAP_DEBUG_ANY, "slapd got shutdown signal %d\n", sig, 0, 0 );
396 pthread_kill( listener_tid, LDAP_SIGUSR1 );
397 (void) SIGNAL( LDAP_SIGUSR2, set_shutdown );
398 (void) SIGNAL( SIGTERM, set_shutdown );
399 (void) SIGNAL( SIGINT, set_shutdown );
400 (void) SIGNAL( SIGHUP, set_shutdown );
404 do_nothing( int sig )
406 Debug( LDAP_DEBUG_TRACE, "slapd got do_nothing signal %d\n", sig, 0, 0 );
407 (void) SIGNAL( LDAP_SIGUSR1, do_nothing );