5 * Added locking of new_conn_mutex when traversing the c[] array.
10 #include <sys/types.h>
11 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
19 #include <sys/select.h>
23 #include "ldapconfig.h"
25 #include <sys/filio.h>
26 #else /* NEED_FILIO */
27 #include <sys/ioctl.h>
28 #endif /* NEED_FILIO */
31 #endif /* USE_SYSCONF */
36 int allow_severity = LOG_INFO;
37 int deny_severity = LOG_NOTICE;
38 #endif /* TCP_WRAPPERS */
40 extern Operation *op_add();
42 #ifndef SYSERRLIST_IN_STDIO
44 extern char *sys_errlist[];
46 extern time_t currenttime;
47 extern pthread_mutex_t currenttime_mutex;
48 extern int active_threads;
49 extern pthread_mutex_t active_threads_mutex;
50 extern pthread_mutex_t new_conn_mutex;
51 extern int slapd_shutdown;
52 extern pthread_t listener_tid;
54 extern pthread_mutex_t ops_mutex;
61 static void set_shutdown();
62 static void do_nothing();
71 unsigned long len, tag, msgid;
74 struct sockaddr_in addr;
81 dtblsize = sysconf( _SC_OPEN_MAX );
82 #else /* USE_SYSCONF */
83 dtblsize = getdtablesize();
84 #endif /* USE_SYSCONF */
86 * Add greg@greg.rim.or.jp
89 if(dtblsize > FD_SETSIZE) {
90 dtblsize = FD_SETSIZE;
92 #endif /* !FD_SETSIZE */
94 c = (Connection *) ch_calloc( 1, dtblsize * sizeof(Connection) );
96 for ( i = 0; i < dtblsize; i++ ) {
101 c[i].c_sb.sb_sd = -1;
102 c[i].c_sb.sb_options = LBER_NO_READ_AHEAD;
103 c[i].c_sb.sb_naddr = 0;
104 c[i].c_sb.sb_ber.ber_buf = NULL;
105 c[i].c_sb.sb_ber.ber_ptr = NULL;
106 c[i].c_sb.sb_ber.ber_end = NULL;
107 c[i].c_writewaiter = 0;
109 pthread_mutex_init( &c[i].c_dnmutex,
110 pthread_mutexattr_default );
111 pthread_mutex_init( &c[i].c_opsmutex,
112 pthread_mutexattr_default );
113 pthread_mutex_init( &c[i].c_pdumutex,
114 pthread_mutexattr_default );
115 pthread_cond_init( &c[i].c_wcv, pthread_condattr_default );
118 if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
119 Debug( LDAP_DEBUG_ANY, "socket() failed errno %d (%s)", errno,
120 errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
126 if ( setsockopt( tcps, SOL_SOCKET, SO_REUSEADDR, (char *) &i,
127 sizeof(i) ) == -1 ) {
128 Debug( LDAP_DEBUG_ANY, "setsockopt() failed errno %d (%s)",
129 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
133 (void) memset( (void *) &addr, '\0', sizeof(addr) );
134 addr.sin_family = AF_INET;
135 addr.sin_addr.s_addr = INADDR_ANY;
136 addr.sin_port = htons( port );
137 if ( bind( tcps, (struct sockaddr *) &addr, sizeof(addr) ) == -1 ) {
138 Debug( LDAP_DEBUG_ANY, "bind() failed errno %d (%s)\n",
139 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
144 if ( listen( tcps, 5 ) == -1 ) {
145 Debug( LDAP_DEBUG_ANY, "listen() failed errno %d (%s)",
146 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
151 (void) SIGNAL( SIGPIPE, SIG_IGN );
154 * LinuxThreads are implemented using SIGUSR1/USR2,
155 * so we'll use SIGSTKFLT and SIGUNUSED
157 (void) SIGNAL( SIGSTKFLT, (void *) do_nothing );
158 (void) SIGNAL( SIGUNUSED, (void *) set_shutdown );
160 (void) SIGNAL( SIGUSR1, (void *) do_nothing );
161 (void) SIGNAL( SIGUSR2, (void *) set_shutdown );
163 (void) SIGNAL( SIGTERM, (void *) set_shutdown );
164 (void) SIGNAL( SIGINT, (void *) set_shutdown );
165 (void) SIGNAL( SIGHUP, (void *) set_shutdown );
167 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
169 if ( (fp = fopen( SLAPD_PIDFILE, "w" )) != NULL ) {
170 fprintf( fp, "%d\n", getpid() );
174 #ifdef SLAPD_ARGSFILE
175 if ( (fp = fopen( SLAPD_ARGSFILE, "w" )) != NULL ) {
176 for ( i = 0; i < g_argc; i++ ) {
177 fprintf( fp, "%s ", g_argv[i] );
184 while ( !slapd_shutdown ) {
185 struct sockaddr_in from;
194 FD_ZERO( &writefds );
196 FD_SET( tcps, &readfds );
198 pthread_mutex_lock( &active_threads_mutex );
199 Debug( LDAP_DEBUG_CONNS,
200 "listening for connections on %d, activity on:",
203 pthread_mutex_lock( &new_conn_mutex );
204 for ( i = 0; i < dtblsize; i++ ) {
205 if ( c[i].c_sb.sb_sd != -1 ) {
206 FD_SET( c[i].c_sb.sb_sd, &readfds );
208 if ( c[i].c_writewaiter ) {
209 FD_SET( c[i].c_sb.sb_sd, &writefds );
211 Debug( LDAP_DEBUG_CONNS, " %dr%s", i,
212 c[i].c_writewaiter ? "w" : "", 0 );
215 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
216 pthread_mutex_unlock( &new_conn_mutex );
220 Debug( LDAP_DEBUG_CONNS, "before select active_threads %d\n",
221 active_threads, 0, 0 );
222 #if defined(PTHREAD_PREEMPTIVE) || defined(NO_THREADS)
225 tvp = active_threads ? &zero : NULL;
227 pthread_mutex_unlock( &active_threads_mutex );
229 switch ( i = select( dtblsize, &readfds, &writefds, 0, tvp ) ) {
230 case -1: /* failure - try again */
231 Debug( LDAP_DEBUG_CONNS,
232 "select failed errno %d (%s)\n",
233 errno, errno > -1 && errno < sys_nerr ?
234 sys_errlist[errno] : "unknown", 0 );
237 case 0: /* timeout - let threads run */
238 Debug( LDAP_DEBUG_CONNS, "select timeout - yielding\n",
243 default: /* something happened - deal with it */
244 Debug( LDAP_DEBUG_CONNS, "select activity on %d descriptors\n", i, 0, 0 );
247 pthread_mutex_lock( ¤ttime_mutex );
248 time( ¤ttime );
249 pthread_mutex_unlock( ¤ttime_mutex );
252 pthread_mutex_lock( &new_conn_mutex );
253 if ( FD_ISSET( tcps, &readfds ) ) {
255 if ( (ns = accept( tcps, (struct sockaddr *) &from,
257 Debug( LDAP_DEBUG_ANY,
258 "accept() failed errno %d (%s)", errno,
259 errno > -1 && errno < sys_nerr ?
260 sys_errlist[errno] : "unknown", 0 );
261 pthread_mutex_unlock( &new_conn_mutex );
264 if ( ioctl( ns, FIONBIO, (caddr_t) &on ) == -1 ) {
265 Debug( LDAP_DEBUG_ANY,
266 "FIONBIO ioctl on %d failed\n", ns, 0, 0 );
269 c[ns].c_sb.sb_sd = ns;
270 Debug( LDAP_DEBUG_CONNS, "new connection on %d\n", ns,
273 pthread_mutex_lock( &ops_mutex );
274 c[ns].c_connid = num_conns++;
275 pthread_mutex_unlock( &ops_mutex );
279 if ( getpeername( ns, (struct sockaddr *) &from, &len )
282 client_addr = inet_ntoa( from.sin_addr );
284 #if defined(REVERSE_LOOKUP) || defined(TCP_WRAPPERS)
285 hp = gethostbyaddr( (char *)
286 &(from.sin_addr.s_addr),
287 sizeof(from.sin_addr.s_addr), AF_INET );
290 client_name = hp->h_name;
292 /* normalize the domain */
293 for ( s = client_name; *s; s++ ) {
310 if(!hosts_ctl("slapd", client_name, client_addr,
314 Statslog( LDAP_DEBUG_STATS,
315 "conn=%d fd=%d connection from %s (%s) denied.\n",
317 client_name == NULL ? "unknown" : client_name,
318 client_addr == NULL ? "unknown" : client_addr,
322 pthread_mutex_unlock( &new_conn_mutex );
325 #endif /* TCP_WRAPPERS */
327 Statslog( LDAP_DEBUG_STATS,
328 "conn=%d fd=%d connection from %s (%s) accepted.\n",
330 client_name == NULL ? "unknown" : client_name,
331 client_addr == NULL ? "unknown" : client_addr,
334 if ( c[ns].c_addr != NULL ) {
335 free( c[ns].c_addr );
337 c[ns].c_addr = strdup( client_addr );
339 if ( c[ns].c_domain != NULL ) {
340 free( c[ns].c_domain );
343 c[ns].c_domain = strdup( client_name == NULL
344 ? "" : client_name );
346 pthread_mutex_lock( &c[ns].c_dnmutex );
347 if ( c[ns].c_dn != NULL ) {
351 pthread_mutex_unlock( &c[ns].c_dnmutex );
352 c[ns].c_starttime = currenttime;
353 c[ns].c_opsinitiated = 0;
354 c[ns].c_opscompleted = 0;
356 pthread_mutex_unlock( &new_conn_mutex );
358 Debug( LDAP_DEBUG_CONNS, "activity on:", 0, 0, 0 );
359 for ( i = 0; i < dtblsize; i++ ) {
362 r = FD_ISSET( i, &readfds );
363 w = FD_ISSET( i, &writefds );
364 if ( i != tcps && (r || w) ) {
365 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
366 r ? "r" : "", w ? "w" : "" );
369 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
371 for ( i = 0; i < dtblsize; i++ ) {
372 if ( i == tcps || (! FD_ISSET( i, &readfds ) &&
373 ! FD_ISSET( i, &writefds )) ) {
377 if ( FD_ISSET( i, &writefds ) ) {
378 Debug( LDAP_DEBUG_CONNS,
379 "signaling write waiter on %d\n", i, 0, 0 );
381 pthread_mutex_lock( &active_threads_mutex );
382 pthread_cond_signal( &c[i].c_wcv );
383 c[i].c_writewaiter = 0;
385 pthread_mutex_unlock( &active_threads_mutex );
388 if ( FD_ISSET( i, &readfds ) ) {
389 Debug( LDAP_DEBUG_CONNS,
390 "read activity on %d\n", i, 0, 0 );
392 connection_activity( &c[i] );
400 pthread_mutex_lock( &active_threads_mutex );
401 Debug( LDAP_DEBUG_ANY,
402 "slapd shutting down - waiting for %d threads to terminate\n",
403 active_threads, 0, 0 );
404 while ( active_threads > 0 ) {
405 pthread_mutex_unlock( &active_threads_mutex );
407 pthread_mutex_lock( &active_threads_mutex );
409 pthread_mutex_unlock( &active_threads_mutex );
411 /* let backends do whatever cleanup they need to do */
412 Debug( LDAP_DEBUG_TRACE,
413 "slapd shutting down - waiting for backends to close down\n", 0, 0,
416 Debug( LDAP_DEBUG_ANY, "slapd stopping\n", 0, 0, 0 );
422 Debug( LDAP_DEBUG_ANY, "slapd got shutdown signal\n", 0, 0, 0 );
426 * LinuxThreads are implemented using SIGUSR1/USR2,
427 * so we'll use SIGSTKFLT and SIGUNUSED
429 pthread_kill( listener_tid, SIGSTKFLT );
430 (void) SIGNAL( SIGUNUSED, (void *) set_shutdown );
432 pthread_kill( listener_tid, SIGUSR1 );
433 (void) SIGNAL( SIGUSR2, (void *) set_shutdown );
435 (void) SIGNAL( SIGTERM, (void *) set_shutdown );
436 (void) SIGNAL( SIGINT, (void *) set_shutdown );
437 (void) SIGNAL( SIGHUP, (void *) set_shutdown );
443 Debug( LDAP_DEBUG_TRACE, "slapd got do_nothing signal\n", 0, 0, 0 );
446 * LinuxThreads are implemented using SIGUSR1/USR2,
447 * so we'll use SIGSTKFLT and SIGUNUSED
449 (void) SIGNAL( SIGSTKFLT, (void *) do_nothing );
451 (void) SIGNAL( SIGUSR1, (void *) do_nothing );