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 /* we need the server's name for constructing the pid/args file names */
44 #if defined( SLAPD_PIDFILE ) || defined( SLAPD_ARGSFILE )
45 extern char *serverName;
46 #define DEFAULT_SERVERNAME "slapd"
56 unsigned long len, tag, msgid;
59 struct sockaddr_in addr;
69 char argsFile[BUFSIZ];
73 dtblsize = sysconf( _SC_OPEN_MAX );
74 #elif HAVE_GETDTABLESIZE
75 dtblsize = getdtablesize();
81 if(dtblsize > FD_SETSIZE) {
82 dtblsize = FD_SETSIZE;
84 #endif /* !FD_SETSIZE */
86 c = (Connection *) ch_calloc( 1, dtblsize * sizeof(Connection) );
88 for ( i = 0; i < dtblsize; i++ ) {
94 c[i].c_sb.sb_options = LBER_NO_READ_AHEAD;
95 c[i].c_sb.sb_naddr = 0;
96 c[i].c_sb.sb_ber.ber_buf = NULL;
97 c[i].c_sb.sb_ber.ber_ptr = NULL;
98 c[i].c_sb.sb_ber.ber_end = NULL;
99 c[i].c_writewaiter = 0;
101 pthread_mutex_init( &c[i].c_dnmutex,
102 pthread_mutexattr_default );
103 pthread_mutex_init( &c[i].c_opsmutex,
104 pthread_mutexattr_default );
105 pthread_mutex_init( &c[i].c_pdumutex,
106 pthread_mutexattr_default );
107 pthread_cond_init( &c[i].c_wcv, pthread_condattr_default );
110 if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
111 Debug( LDAP_DEBUG_ANY, "socket() failed errno %d (%s)", errno,
112 errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
118 if ( setsockopt( tcps, SOL_SOCKET, SO_REUSEADDR, (char *) &i,
119 sizeof(i) ) == -1 ) {
120 Debug( LDAP_DEBUG_ANY, "setsockopt() failed errno %d (%s)",
121 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
125 (void) memset( (void *) &addr, '\0', sizeof(addr) );
126 addr.sin_family = AF_INET;
127 addr.sin_addr.s_addr = INADDR_ANY;
128 addr.sin_port = htons( (int)port );
129 if ( bind( tcps, (struct sockaddr *) &addr, sizeof(addr) ) == -1 ) {
130 Debug( LDAP_DEBUG_ANY, "bind() failed errno %d (%s)\n",
131 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
136 if ( listen( tcps, 5 ) == -1 ) {
137 Debug( LDAP_DEBUG_ANY, "listen() failed errno %d (%s)",
138 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
143 (void) SIGNAL( SIGPIPE, SIG_IGN );
144 (void) SIGNAL( LDAP_SIGUSR1, do_nothing );
145 (void) SIGNAL( LDAP_SIGUSR2, set_shutdown );
146 (void) SIGNAL( SIGTERM, set_shutdown );
147 (void) SIGNAL( SIGINT, set_shutdown );
148 (void) SIGNAL( SIGHUP, set_shutdown );
150 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
152 #if defined( SLAPD_PIDFILE ) || defined( SLAPD_ARGSFILE )
153 if ( !serverName ) serverName = DEFAULT_SERVERNAME;
157 sprintf( pidFile, "%s%s%s", SLAPD_PIDDIR, serverName, SLAPD_PIDEXT );
158 if ( (fp = fopen( pidFile, "w" )) != NULL ) {
159 fprintf( fp, "%d\n", (int) getpid() );
163 #ifdef SLAPD_ARGSFILE
164 sprintf( argsFile, "%s%s%s", SLAPD_ARGSDIR, serverName, SLAPD_ARGSEXT );
165 if ( (fp = fopen( argsFile, "w" )) != NULL ) {
166 for ( i = 0; i < g_argc; i++ ) {
167 fprintf( fp, "%s ", g_argv[i] );
174 while ( !slapd_shutdown ) {
175 struct sockaddr_in from;
184 FD_ZERO( &writefds );
186 FD_SET( tcps, &readfds );
191 pthread_mutex_lock( &active_threads_mutex );
192 Debug( LDAP_DEBUG_CONNS,
193 "listening for connections on %d, activity on:",
196 pthread_mutex_lock( &new_conn_mutex );
197 for ( i = 0; i < dtblsize; i++ ) {
198 if ( c[i].c_sb.sb_sd != -1 ) {
199 FD_SET( c[i].c_sb.sb_sd, &readfds );
201 if ( c[i].c_writewaiter ) {
202 FD_SET( c[i].c_sb.sb_sd, &writefds );
204 Debug( LDAP_DEBUG_CONNS, " %dr%s", i,
205 c[i].c_writewaiter ? "w" : "", 0 );
208 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
209 pthread_mutex_unlock( &new_conn_mutex );
211 Debug( LDAP_DEBUG_CONNS, "before select active_threads %d\n",
212 active_threads, 0, 0 );
213 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
216 tvp = active_threads ? &zero : NULL;
218 pthread_mutex_unlock( &active_threads_mutex );
220 switch ( i = select( dtblsize, &readfds, &writefds, 0, tvp ) ) {
221 case -1: /* failure - try again */
222 Debug( LDAP_DEBUG_CONNS,
223 "select failed errno %d (%s)\n",
224 errno, errno > -1 && errno < sys_nerr ?
225 sys_errlist[errno] : "unknown", 0 );
228 case 0: /* timeout - let threads run */
229 Debug( LDAP_DEBUG_CONNS, "select timeout - yielding\n",
234 default: /* something happened - deal with it */
235 Debug( LDAP_DEBUG_CONNS, "select activity on %d descriptors\n", i, 0, 0 );
238 pthread_mutex_lock( ¤ttime_mutex );
239 time( ¤ttime );
240 pthread_mutex_unlock( ¤ttime_mutex );
243 pthread_mutex_lock( &new_conn_mutex );
244 if ( FD_ISSET( tcps, &readfds ) ) {
246 if ( (ns = accept( tcps, (struct sockaddr *) &from,
248 Debug( LDAP_DEBUG_ANY,
249 "accept() failed errno %d (%s)", errno,
250 errno > -1 && errno < sys_nerr ?
251 sys_errlist[errno] : "unknown", 0 );
252 pthread_mutex_unlock( &new_conn_mutex );
255 if ( ioctl( ns, FIONBIO, (caddr_t) &on ) == -1 ) {
256 Debug( LDAP_DEBUG_ANY,
257 "FIONBIO ioctl on %d failed\n", ns, 0, 0 );
260 c[ns].c_sb.sb_sd = ns;
261 Debug( LDAP_DEBUG_CONNS, "new connection on %d\n", ns,
264 pthread_mutex_lock( &ops_mutex );
265 c[ns].c_connid = num_conns++;
266 pthread_mutex_unlock( &ops_mutex );
270 if ( getpeername( ns, (struct sockaddr *) &from, &len )
273 client_addr = inet_ntoa( from.sin_addr );
275 #if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
276 hp = gethostbyaddr( (char *)
277 &(from.sin_addr.s_addr),
278 sizeof(from.sin_addr.s_addr), AF_INET );
281 client_name = hp->h_name;
283 /* normalize the domain */
284 for ( s = client_name; *s; s++ ) {
301 if(!hosts_ctl("slapd",
302 client_name != NULL ? client_name : STRING_UNKNOWN,
303 client_addr != NULL ? client_addr : STRING_UNKNOWN,
307 Statslog( LDAP_DEBUG_STATS,
308 "conn=%d fd=%d connection from %s (%s) denied.\n",
310 client_name == NULL ? "unknown" : client_name,
311 client_addr == NULL ? "unknown" : client_addr,
315 pthread_mutex_unlock( &new_conn_mutex );
318 #endif /* HAVE_TCPD */
320 Statslog( LDAP_DEBUG_STATS,
321 "conn=%d fd=%d connection from %s (%s) accepted.\n",
323 client_name == NULL ? "unknown" : client_name,
324 client_addr == NULL ? "unknown" : client_addr,
327 if ( c[ns].c_addr != NULL ) {
328 free( c[ns].c_addr );
330 c[ns].c_addr = ch_strdup( client_addr );
332 if ( c[ns].c_domain != NULL ) {
333 free( c[ns].c_domain );
336 c[ns].c_domain = ch_strdup( client_name == NULL
337 ? "" : client_name );
339 pthread_mutex_lock( &c[ns].c_dnmutex );
340 if ( c[ns].c_dn != NULL ) {
344 pthread_mutex_unlock( &c[ns].c_dnmutex );
345 c[ns].c_starttime = currenttime;
346 c[ns].c_opsinitiated = 0;
347 c[ns].c_opscompleted = 0;
349 pthread_mutex_unlock( &new_conn_mutex );
351 Debug( LDAP_DEBUG_CONNS, "activity on:", 0, 0, 0 );
352 for ( i = 0; i < dtblsize; i++ ) {
355 r = FD_ISSET( i, &readfds );
356 w = FD_ISSET( i, &writefds );
357 if ( i != tcps && (r || w) ) {
358 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
359 r ? "r" : "", w ? "w" : "" );
362 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
364 for ( i = 0; i < dtblsize; i++ ) {
365 if ( i == tcps || (! FD_ISSET( i, &readfds ) &&
366 ! FD_ISSET( i, &writefds )) ) {
370 if ( FD_ISSET( i, &writefds ) ) {
371 Debug( LDAP_DEBUG_CONNS,
372 "signaling write waiter on %d\n", i, 0, 0 );
374 pthread_mutex_lock( &active_threads_mutex );
375 pthread_cond_signal( &c[i].c_wcv );
376 c[i].c_writewaiter = 0;
378 pthread_mutex_unlock( &active_threads_mutex );
381 if ( FD_ISSET( i, &readfds ) ) {
382 Debug( LDAP_DEBUG_CONNS,
383 "read activity on %d\n", i, 0, 0 );
385 connection_activity( &c[i] );
393 pthread_mutex_lock( &active_threads_mutex );
394 Debug( LDAP_DEBUG_ANY,
395 "slapd shutting down - waiting for %d threads to terminate\n",
396 active_threads, 0, 0 );
397 while ( active_threads > 0 ) {
398 pthread_mutex_unlock( &active_threads_mutex );
400 pthread_mutex_lock( &active_threads_mutex );
402 pthread_mutex_unlock( &active_threads_mutex );
404 /* let backends do whatever cleanup they need to do */
405 Debug( LDAP_DEBUG_TRACE,
406 "slapd shutting down - waiting for backends to close down\n", 0, 0,
409 Debug( LDAP_DEBUG_ANY, "slapd stopping\n", 0, 0, 0 );
414 set_shutdown( int sig )
416 Debug( LDAP_DEBUG_ANY, "slapd got shutdown signal %d\n", sig, 0, 0 );
418 pthread_kill( listener_tid, LDAP_SIGUSR1 );
419 (void) SIGNAL( LDAP_SIGUSR2, set_shutdown );
420 (void) SIGNAL( SIGTERM, set_shutdown );
421 (void) SIGNAL( SIGINT, set_shutdown );
422 (void) SIGNAL( SIGHUP, set_shutdown );
426 do_nothing( int sig )
428 Debug( LDAP_DEBUG_TRACE, "slapd got do_nothing signal %d\n", sig, 0, 0 );
429 (void) SIGNAL( LDAP_SIGUSR1, do_nothing );