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 */
33 extern Operation *op_add();
35 #ifndef SYSERRLIST_IN_STDIO
37 extern char *sys_errlist[];
39 extern time_t currenttime;
40 extern pthread_mutex_t currenttime_mutex;
41 extern int active_threads;
42 extern pthread_mutex_t active_threads_mutex;
43 extern pthread_mutex_t new_conn_mutex;
44 extern int slapd_shutdown;
45 extern pthread_t listener_tid;
47 extern pthread_mutex_t ops_mutex;
54 static void set_shutdown();
55 static void do_nothing();
64 unsigned long len, tag, msgid;
67 struct sockaddr_in addr;
74 dtblsize = sysconf( _SC_OPEN_MAX );
75 #else /* USE_SYSCONF */
76 dtblsize = getdtablesize();
77 #endif /* USE_SYSCONF */
79 * Add greg@greg.rim.or.jp
81 if(dtblsize > FD_SETSIZE) {
82 dtblsize = FD_SETSIZE;
84 c = (Connection *) ch_calloc( 1, dtblsize * sizeof(Connection) );
86 for ( i = 0; i < dtblsize; i++ ) {
92 c[i].c_sb.sb_options = LBER_NO_READ_AHEAD;
93 c[i].c_sb.sb_naddr = 0;
94 c[i].c_sb.sb_ber.ber_buf = NULL;
95 c[i].c_sb.sb_ber.ber_ptr = NULL;
96 c[i].c_sb.sb_ber.ber_end = NULL;
97 c[i].c_writewaiter = 0;
99 pthread_mutex_init( &c[i].c_dnmutex,
100 pthread_mutexattr_default );
101 pthread_mutex_init( &c[i].c_opsmutex,
102 pthread_mutexattr_default );
103 pthread_mutex_init( &c[i].c_pdumutex,
104 pthread_mutexattr_default );
105 pthread_cond_init( &c[i].c_wcv, pthread_condattr_default );
108 if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
109 Debug( LDAP_DEBUG_ANY, "socket() failed errno %d (%s)", errno,
110 errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
116 if ( setsockopt( tcps, SOL_SOCKET, SO_REUSEADDR, (char *) &i,
117 sizeof(i) ) == -1 ) {
118 Debug( LDAP_DEBUG_ANY, "setsockopt() failed errno %d (%s)",
119 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
123 (void) memset( (void *) &addr, '\0', sizeof(addr) );
124 addr.sin_family = AF_INET;
125 addr.sin_addr.s_addr = INADDR_ANY;
126 addr.sin_port = htons( port );
127 if ( bind( tcps, (struct sockaddr *) &addr, sizeof(addr) ) == -1 ) {
128 Debug( LDAP_DEBUG_ANY, "bind() failed errno %d (%s)\n",
129 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
134 if ( listen( tcps, 5 ) == -1 ) {
135 Debug( LDAP_DEBUG_ANY, "listen() failed errno %d (%s)",
136 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
141 (void) SIGNAL( SIGPIPE, SIG_IGN );
143 (void) SIGNAL( SIGSTKFLT, (void *) do_nothing );
145 (void) SIGNAL( SIGUSR1, (void *) do_nothing );
148 (void) SIGNAL( SIGUNUSED, (void *) set_shutdown );
150 (void) SIGNAL( SIGUSR2, (void *) set_shutdown );
152 (void) SIGNAL( SIGTERM, (void *) set_shutdown );
153 (void) SIGNAL( SIGINT, (void *) set_shutdown );
154 (void) SIGNAL( SIGHUP, (void *) set_shutdown );
156 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
158 if ( (fp = fopen( SLAPD_PIDFILE, "w" )) != NULL ) {
159 fprintf( fp, "%d\n", getpid() );
163 #ifdef SLAPD_ARGSFILE
164 if ( (fp = fopen( SLAPD_ARGSFILE, "w" )) != NULL ) {
165 for ( i = 0; i < g_argc; i++ ) {
166 fprintf( fp, "%s ", g_argv[i] );
173 while ( !slapd_shutdown ) {
174 struct sockaddr_in from;
180 FD_ZERO( &writefds );
182 FD_SET( tcps, &readfds );
184 pthread_mutex_lock( &active_threads_mutex );
185 Debug( LDAP_DEBUG_CONNS,
186 "listening for connections on %d, activity on:",
189 pthread_mutex_lock( &new_conn_mutex );
190 for ( i = 0; i < dtblsize; i++ ) {
191 if ( c[i].c_sb.sb_sd != -1 ) {
192 FD_SET( c[i].c_sb.sb_sd, &readfds );
194 if ( c[i].c_writewaiter ) {
195 FD_SET( c[i].c_sb.sb_sd, &writefds );
197 Debug( LDAP_DEBUG_CONNS, " %dr%s", i,
198 c[i].c_writewaiter ? "w" : "", 0 );
201 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
202 pthread_mutex_unlock( &new_conn_mutex );
206 Debug( LDAP_DEBUG_CONNS, "before select active_threads %d\n",
207 active_threads, 0, 0 );
208 #ifdef PTHREAD_PREEMPTIVE
211 tvp = active_threads ? &zero : NULL;
213 pthread_mutex_unlock( &active_threads_mutex );
215 switch ( select( dtblsize, &readfds, &writefds, 0, tvp ) ) {
216 case -1: /* failure - try again */
217 Debug( LDAP_DEBUG_CONNS,
218 "select failed errno %d (%s)\n",
219 errno, errno > -1 && errno < sys_nerr ?
220 sys_errlist[errno] : "unknown", 0 );
223 case 0: /* timeout - let threads run */
224 Debug( LDAP_DEBUG_CONNS, "select timeout - yielding\n",
229 default: /* something happened - deal with it */
230 Debug( LDAP_DEBUG_CONNS, "select activity\n", 0, 0, 0 );
233 pthread_mutex_lock( ¤ttime_mutex );
234 time( ¤ttime );
235 pthread_mutex_unlock( ¤ttime_mutex );
238 pthread_mutex_lock( &new_conn_mutex );
239 if ( FD_ISSET( tcps, &readfds ) ) {
241 if ( (ns = accept( tcps, (struct sockaddr *) &from,
243 Debug( LDAP_DEBUG_ANY,
244 "accept() failed errno %d (%s)", errno,
245 errno > -1 && errno < sys_nerr ?
246 sys_errlist[errno] : "unknown", 0 );
247 pthread_mutex_unlock( &new_conn_mutex );
250 if ( ioctl( ns, FIONBIO, (caddr_t) &on ) == -1 ) {
251 Debug( LDAP_DEBUG_ANY,
252 "FIONBIO ioctl on %d faled\n", ns, 0, 0 );
254 c[ns].c_sb.sb_sd = ns;
255 Debug( LDAP_DEBUG_CONNS, "new connection on %d\n", ns,
258 pthread_mutex_lock( &ops_mutex );
259 c[ns].c_connid = num_conns++;
260 pthread_mutex_unlock( &ops_mutex );
262 if ( getpeername( ns, (struct sockaddr *) &from, &len )
265 #ifdef REVERSE_LOOKUP
266 hp = gethostbyaddr( (char *)
267 &(from.sin_addr.s_addr),
268 sizeof(from.sin_addr.s_addr), AF_INET );
273 Statslog( LDAP_DEBUG_STATS,
274 "conn=%d fd=%d connection from %s (%s)\n",
275 c[ns].c_connid, ns, hp == NULL ? "unknown"
276 : hp->h_name, inet_ntoa( from.sin_addr ),
279 if ( c[ns].c_addr != NULL ) {
280 free( c[ns].c_addr );
282 c[ns].c_addr = strdup( inet_ntoa(
284 if ( c[ns].c_domain != NULL ) {
285 free( c[ns].c_domain );
287 c[ns].c_domain = strdup( hp == NULL ? "" :
289 /* normalize the domain */
290 for ( s = c[ns].c_domain; *s; s++ ) {
294 Statslog( LDAP_DEBUG_STATS,
295 "conn=%d fd=%d connection from unknown\n",
296 c[ns].c_connid, ns, 0, 0, 0 );
298 pthread_mutex_lock( &c[ns].c_dnmutex );
299 if ( c[ns].c_dn != NULL ) {
303 pthread_mutex_unlock( &c[ns].c_dnmutex );
304 c[ns].c_starttime = currenttime;
305 c[ns].c_opsinitiated = 0;
306 c[ns].c_opscompleted = 0;
308 pthread_mutex_unlock( &new_conn_mutex );
310 Debug( LDAP_DEBUG_CONNS, "activity on:", 0, 0, 0 );
311 for ( i = 0; i < dtblsize; i++ ) {
314 r = FD_ISSET( i, &readfds );
315 w = FD_ISSET( i, &writefds );
316 if ( i != tcps && (r || w) ) {
317 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
318 r ? "r" : "", w ? "w" : "" );
321 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
323 for ( i = 0; i < dtblsize; i++ ) {
324 if ( i == tcps || (! FD_ISSET( i, &readfds ) &&
325 ! FD_ISSET( i, &writefds )) ) {
329 if ( FD_ISSET( i, &writefds ) ) {
330 Debug( LDAP_DEBUG_CONNS,
331 "signaling write waiter on %d\n", i, 0, 0 );
333 pthread_mutex_lock( &active_threads_mutex );
334 pthread_cond_signal( &c[i].c_wcv );
335 c[i].c_writewaiter = 0;
337 pthread_mutex_unlock( &active_threads_mutex );
340 if ( FD_ISSET( i, &readfds ) ) {
341 Debug( LDAP_DEBUG_CONNS,
342 "read activity on %d\n", i, 0, 0 );
344 connection_activity( &c[i] );
352 pthread_mutex_lock( &active_threads_mutex );
353 Debug( LDAP_DEBUG_ANY,
354 "slapd shutting down - waiting for %d threads to terminate\n",
355 active_threads, 0, 0 );
356 while ( active_threads > 0 ) {
357 pthread_mutex_unlock( &active_threads_mutex );
359 pthread_mutex_lock( &active_threads_mutex );
361 pthread_mutex_unlock( &active_threads_mutex );
363 /* let backends do whatever cleanup they need to do */
364 Debug( LDAP_DEBUG_TRACE,
365 "slapd shutting down - waiting for backends to close down\n", 0, 0,
368 Debug( LDAP_DEBUG_ANY, "slapd stopping\n", 0, 0, 0 );
374 Debug( LDAP_DEBUG_ANY, "slapd got shutdown signal\n", 0, 0, 0 );
377 pthread_kill( listener_tid, SIGSTKFLT );
379 pthread_kill( listener_tid, SIGUSR1 );
382 (void) SIGNAL( SIGUNUSED, (void *) set_shutdown );
384 (void) SIGNAL( SIGUSR2, (void *) set_shutdown );
386 (void) SIGNAL( SIGTERM, (void *) set_shutdown );
387 (void) SIGNAL( SIGINT, (void *) set_shutdown );
388 (void) SIGNAL( SIGHUP, (void *) set_shutdown );
394 Debug( LDAP_DEBUG_TRACE, "slapd got do_nothing signal\n", 0, 0, 0 );
396 (void) SIGNAL( SIGSTKFLT, (void *) do_nothing );
398 (void) SIGNAL( SIGUSR1, (void *) do_nothing );