5 * Added locking of new_conn_mutex when traversing the c[] array.
11 #include <ac/string.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
21 #include <sys/select.h>
24 #include "ldapconfig.h"
26 #include <sys/filio.h>
27 #else /* NEED_FILIO */
28 #include <sys/ioctl.h>
29 #endif /* NEED_FILIO */
32 #endif /* USE_SYSCONF */
37 int allow_severity = LOG_INFO;
38 int deny_severity = LOG_NOTICE;
39 #endif /* TCP_WRAPPERS */
41 extern Operation *op_add();
43 #ifdef DECL_SYS_ERRLIST
45 extern char *sys_errlist[];
48 extern time_t currenttime;
49 extern pthread_mutex_t currenttime_mutex;
50 extern int active_threads;
51 extern pthread_mutex_t active_threads_mutex;
52 extern pthread_mutex_t new_conn_mutex;
53 extern int slapd_shutdown;
54 extern pthread_t listener_tid;
56 extern pthread_mutex_t ops_mutex;
63 static void set_shutdown();
64 static void do_nothing();
73 unsigned long len, tag, msgid;
76 struct sockaddr_in addr;
83 dtblsize = sysconf( _SC_OPEN_MAX );
84 #else /* USE_SYSCONF */
85 dtblsize = getdtablesize();
86 #endif /* USE_SYSCONF */
88 * Add greg@greg.rim.or.jp
91 if(dtblsize > FD_SETSIZE) {
92 dtblsize = FD_SETSIZE;
94 #endif /* !FD_SETSIZE */
96 c = (Connection *) ch_calloc( 1, dtblsize * sizeof(Connection) );
98 for ( i = 0; i < dtblsize; i++ ) {
101 c[i].c_domain = NULL;
103 c[i].c_sb.sb_sd = -1;
104 c[i].c_sb.sb_options = LBER_NO_READ_AHEAD;
105 c[i].c_sb.sb_naddr = 0;
106 c[i].c_sb.sb_ber.ber_buf = NULL;
107 c[i].c_sb.sb_ber.ber_ptr = NULL;
108 c[i].c_sb.sb_ber.ber_end = NULL;
109 c[i].c_writewaiter = 0;
111 pthread_mutex_init( &c[i].c_dnmutex,
112 pthread_mutexattr_default );
113 pthread_mutex_init( &c[i].c_opsmutex,
114 pthread_mutexattr_default );
115 pthread_mutex_init( &c[i].c_pdumutex,
116 pthread_mutexattr_default );
117 pthread_cond_init( &c[i].c_wcv, pthread_condattr_default );
120 if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
121 Debug( LDAP_DEBUG_ANY, "socket() failed errno %d (%s)", errno,
122 errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
128 if ( setsockopt( tcps, SOL_SOCKET, SO_REUSEADDR, (char *) &i,
129 sizeof(i) ) == -1 ) {
130 Debug( LDAP_DEBUG_ANY, "setsockopt() failed errno %d (%s)",
131 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
135 (void) memset( (void *) &addr, '\0', sizeof(addr) );
136 addr.sin_family = AF_INET;
137 addr.sin_addr.s_addr = INADDR_ANY;
138 addr.sin_port = htons( port );
139 if ( bind( tcps, (struct sockaddr *) &addr, sizeof(addr) ) == -1 ) {
140 Debug( LDAP_DEBUG_ANY, "bind() failed errno %d (%s)\n",
141 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
146 if ( listen( tcps, 5 ) == -1 ) {
147 Debug( LDAP_DEBUG_ANY, "listen() failed errno %d (%s)",
148 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
153 (void) SIGNAL( SIGPIPE, SIG_IGN );
156 * LinuxThreads are implemented using SIGUSR1/USR2,
157 * so we'll use SIGSTKFLT and SIGUNUSED
159 (void) SIGNAL( SIGSTKFLT, (void *) do_nothing );
160 (void) SIGNAL( SIGUNUSED, (void *) set_shutdown );
162 (void) SIGNAL( SIGUSR1, (void *) do_nothing );
163 (void) SIGNAL( SIGUSR2, (void *) set_shutdown );
165 (void) SIGNAL( SIGTERM, (void *) set_shutdown );
166 (void) SIGNAL( SIGINT, (void *) set_shutdown );
167 (void) SIGNAL( SIGHUP, (void *) set_shutdown );
169 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
171 if ( (fp = fopen( SLAPD_PIDFILE, "w" )) != NULL ) {
172 fprintf( fp, "%d\n", getpid() );
176 #ifdef SLAPD_ARGSFILE
177 if ( (fp = fopen( SLAPD_ARGSFILE, "w" )) != NULL ) {
178 for ( i = 0; i < g_argc; i++ ) {
179 fprintf( fp, "%s ", g_argv[i] );
186 while ( !slapd_shutdown ) {
187 struct sockaddr_in from;
196 FD_ZERO( &writefds );
198 FD_SET( tcps, &readfds );
203 pthread_mutex_lock( &active_threads_mutex );
204 Debug( LDAP_DEBUG_CONNS,
205 "listening for connections on %d, activity on:",
208 pthread_mutex_lock( &new_conn_mutex );
209 for ( i = 0; i < dtblsize; i++ ) {
210 if ( c[i].c_sb.sb_sd != -1 ) {
211 FD_SET( c[i].c_sb.sb_sd, &readfds );
213 if ( c[i].c_writewaiter ) {
214 FD_SET( c[i].c_sb.sb_sd, &writefds );
216 Debug( LDAP_DEBUG_CONNS, " %dr%s", i,
217 c[i].c_writewaiter ? "w" : "", 0 );
220 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
221 pthread_mutex_unlock( &new_conn_mutex );
223 Debug( LDAP_DEBUG_CONNS, "before select active_threads %d\n",
224 active_threads, 0, 0 );
225 #if defined(THREAD_PREEMPTIVE) || defined(NO_THREADS)
228 tvp = active_threads ? &zero : NULL;
230 pthread_mutex_unlock( &active_threads_mutex );
232 switch ( i = select( dtblsize, &readfds, &writefds, 0, tvp ) ) {
233 case -1: /* failure - try again */
234 Debug( LDAP_DEBUG_CONNS,
235 "select failed errno %d (%s)\n",
236 errno, errno > -1 && errno < sys_nerr ?
237 sys_errlist[errno] : "unknown", 0 );
240 case 0: /* timeout - let threads run */
241 Debug( LDAP_DEBUG_CONNS, "select timeout - yielding\n",
246 default: /* something happened - deal with it */
247 Debug( LDAP_DEBUG_CONNS, "select activity on %d descriptors\n", i, 0, 0 );
250 pthread_mutex_lock( ¤ttime_mutex );
251 time( ¤ttime );
252 pthread_mutex_unlock( ¤ttime_mutex );
255 pthread_mutex_lock( &new_conn_mutex );
256 if ( FD_ISSET( tcps, &readfds ) ) {
258 if ( (ns = accept( tcps, (struct sockaddr *) &from,
260 Debug( LDAP_DEBUG_ANY,
261 "accept() failed errno %d (%s)", errno,
262 errno > -1 && errno < sys_nerr ?
263 sys_errlist[errno] : "unknown", 0 );
264 pthread_mutex_unlock( &new_conn_mutex );
267 if ( ioctl( ns, FIONBIO, (caddr_t) &on ) == -1 ) {
268 Debug( LDAP_DEBUG_ANY,
269 "FIONBIO ioctl on %d failed\n", ns, 0, 0 );
272 c[ns].c_sb.sb_sd = ns;
273 Debug( LDAP_DEBUG_CONNS, "new connection on %d\n", ns,
276 pthread_mutex_lock( &ops_mutex );
277 c[ns].c_connid = num_conns++;
278 pthread_mutex_unlock( &ops_mutex );
282 if ( getpeername( ns, (struct sockaddr *) &from, &len )
285 client_addr = inet_ntoa( from.sin_addr );
287 #if defined(REVERSE_LOOKUP) || defined(TCP_WRAPPERS)
288 hp = gethostbyaddr( (char *)
289 &(from.sin_addr.s_addr),
290 sizeof(from.sin_addr.s_addr), AF_INET );
293 client_name = hp->h_name;
295 /* normalize the domain */
296 for ( s = client_name; *s; s++ ) {
313 if(!hosts_ctl("slapd", client_name, client_addr,
317 Statslog( LDAP_DEBUG_STATS,
318 "conn=%d fd=%d connection from %s (%s) denied.\n",
320 client_name == NULL ? "unknown" : client_name,
321 client_addr == NULL ? "unknown" : client_addr,
325 pthread_mutex_unlock( &new_conn_mutex );
328 #endif /* TCP_WRAPPERS */
330 Statslog( LDAP_DEBUG_STATS,
331 "conn=%d fd=%d connection from %s (%s) accepted.\n",
333 client_name == NULL ? "unknown" : client_name,
334 client_addr == NULL ? "unknown" : client_addr,
337 if ( c[ns].c_addr != NULL ) {
338 free( c[ns].c_addr );
340 c[ns].c_addr = strdup( client_addr );
342 if ( c[ns].c_domain != NULL ) {
343 free( c[ns].c_domain );
346 c[ns].c_domain = strdup( client_name == NULL
347 ? "" : client_name );
349 pthread_mutex_lock( &c[ns].c_dnmutex );
350 if ( c[ns].c_dn != NULL ) {
354 pthread_mutex_unlock( &c[ns].c_dnmutex );
355 c[ns].c_starttime = currenttime;
356 c[ns].c_opsinitiated = 0;
357 c[ns].c_opscompleted = 0;
359 pthread_mutex_unlock( &new_conn_mutex );
361 Debug( LDAP_DEBUG_CONNS, "activity on:", 0, 0, 0 );
362 for ( i = 0; i < dtblsize; i++ ) {
365 r = FD_ISSET( i, &readfds );
366 w = FD_ISSET( i, &writefds );
367 if ( i != tcps && (r || w) ) {
368 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
369 r ? "r" : "", w ? "w" : "" );
372 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
374 for ( i = 0; i < dtblsize; i++ ) {
375 if ( i == tcps || (! FD_ISSET( i, &readfds ) &&
376 ! FD_ISSET( i, &writefds )) ) {
380 if ( FD_ISSET( i, &writefds ) ) {
381 Debug( LDAP_DEBUG_CONNS,
382 "signaling write waiter on %d\n", i, 0, 0 );
384 pthread_mutex_lock( &active_threads_mutex );
385 pthread_cond_signal( &c[i].c_wcv );
386 c[i].c_writewaiter = 0;
388 pthread_mutex_unlock( &active_threads_mutex );
391 if ( FD_ISSET( i, &readfds ) ) {
392 Debug( LDAP_DEBUG_CONNS,
393 "read activity on %d\n", i, 0, 0 );
395 connection_activity( &c[i] );
403 pthread_mutex_lock( &active_threads_mutex );
404 Debug( LDAP_DEBUG_ANY,
405 "slapd shutting down - waiting for %d threads to terminate\n",
406 active_threads, 0, 0 );
407 while ( active_threads > 0 ) {
408 pthread_mutex_unlock( &active_threads_mutex );
410 pthread_mutex_lock( &active_threads_mutex );
412 pthread_mutex_unlock( &active_threads_mutex );
414 /* let backends do whatever cleanup they need to do */
415 Debug( LDAP_DEBUG_TRACE,
416 "slapd shutting down - waiting for backends to close down\n", 0, 0,
419 Debug( LDAP_DEBUG_ANY, "slapd stopping\n", 0, 0, 0 );
425 Debug( LDAP_DEBUG_ANY, "slapd got shutdown signal\n", 0, 0, 0 );
429 * LinuxThreads are implemented using SIGUSR1/USR2,
430 * so we'll use SIGSTKFLT and SIGUNUSED
432 pthread_kill( listener_tid, SIGSTKFLT );
433 (void) SIGNAL( SIGUNUSED, (void *) set_shutdown );
435 pthread_kill( listener_tid, SIGUSR1 );
436 (void) SIGNAL( SIGUSR2, (void *) set_shutdown );
438 (void) SIGNAL( SIGTERM, (void *) set_shutdown );
439 (void) SIGNAL( SIGINT, (void *) set_shutdown );
440 (void) SIGNAL( SIGHUP, (void *) set_shutdown );
446 Debug( LDAP_DEBUG_TRACE, "slapd got do_nothing signal\n", 0, 0, 0 );
449 * LinuxThreads are implemented using SIGUSR1/USR2,
450 * so we'll use SIGSTKFLT and SIGUNUSED
452 (void) SIGNAL( SIGSTKFLT, (void *) do_nothing );
454 (void) SIGNAL( SIGUSR1, (void *) do_nothing );