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 c = (Connection *) ch_calloc( 1, dtblsize * sizeof(Connection) );
81 for ( i = 0; i < dtblsize; i++ ) {
87 c[i].c_sb.sb_options = LBER_NO_READ_AHEAD;
88 c[i].c_sb.sb_naddr = 0;
89 c[i].c_sb.sb_ber.ber_buf = NULL;
90 c[i].c_sb.sb_ber.ber_ptr = NULL;
91 c[i].c_sb.sb_ber.ber_end = NULL;
92 c[i].c_writewaiter = 0;
94 pthread_mutex_init( &c[i].c_dnmutex,
95 pthread_mutexattr_default );
96 pthread_mutex_init( &c[i].c_opsmutex,
97 pthread_mutexattr_default );
98 pthread_mutex_init( &c[i].c_pdumutex,
99 pthread_mutexattr_default );
100 pthread_cond_init( &c[i].c_wcv, pthread_condattr_default );
103 if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
104 Debug( LDAP_DEBUG_ANY, "socket() failed errno %d (%s)", errno,
105 errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
111 if ( setsockopt( tcps, SOL_SOCKET, SO_REUSEADDR, (char *) &i,
112 sizeof(i) ) == -1 ) {
113 Debug( LDAP_DEBUG_ANY, "setsockopt() failed errno %d (%s)",
114 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
118 (void) memset( (void *) &addr, '\0', sizeof(addr) );
119 addr.sin_family = AF_INET;
120 addr.sin_addr.s_addr = INADDR_ANY;
121 addr.sin_port = htons( port );
122 if ( bind( tcps, (struct sockaddr *) &addr, sizeof(addr) ) == -1 ) {
123 Debug( LDAP_DEBUG_ANY, "bind() failed errno %d (%s)\n",
124 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
129 if ( listen( tcps, 5 ) == -1 ) {
130 Debug( LDAP_DEBUG_ANY, "listen() failed errno %d (%s)",
131 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
136 (void) SIGNAL( SIGPIPE, SIG_IGN );
137 (void) SIGNAL( SIGUSR1, (void *) do_nothing );
138 (void) SIGNAL( SIGUSR2, (void *) set_shutdown );
139 (void) SIGNAL( SIGTERM, (void *) set_shutdown );
140 (void) SIGNAL( SIGHUP, (void *) set_shutdown );
142 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
144 if ( (fp = fopen( SLAPD_PIDFILE, "w" )) != NULL ) {
145 fprintf( fp, "%d\n", getpid() );
149 #ifdef SLAPD_ARGSFILE
150 if ( (fp = fopen( SLAPD_ARGSFILE, "w" )) != NULL ) {
151 for ( i = 0; i < g_argc; i++ ) {
152 fprintf( fp, "%s ", g_argv[i] );
159 while ( !slapd_shutdown ) {
160 struct sockaddr_in from;
166 FD_ZERO( &writefds );
168 FD_SET( tcps, &readfds );
170 pthread_mutex_lock( &active_threads_mutex );
171 Debug( LDAP_DEBUG_CONNS,
172 "listening for connections on %d, activity on:",
175 pthread_mutex_lock( &new_conn_mutex );
176 for ( i = 0; i < dtblsize; i++ ) {
177 if ( c[i].c_sb.sb_sd != -1 ) {
178 FD_SET( c[i].c_sb.sb_sd, &readfds );
180 if ( c[i].c_writewaiter ) {
181 FD_SET( c[i].c_sb.sb_sd, &writefds );
183 Debug( LDAP_DEBUG_CONNS, " %dr%s", i,
184 c[i].c_writewaiter ? "w" : "", 0 );
187 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
188 pthread_mutex_unlock( &new_conn_mutex );
192 Debug( LDAP_DEBUG_CONNS, "before select active_threads %d\n",
193 active_threads, 0, 0 );
194 #ifdef PTHREAD_PREEMPTIVE
197 tvp = active_threads ? &zero : NULL;
199 pthread_mutex_unlock( &active_threads_mutex );
201 switch ( select( dtblsize, &readfds, &writefds, 0, tvp ) ) {
202 case -1: /* failure - try again */
203 Debug( LDAP_DEBUG_CONNS,
204 "select failed errno %d (%s)\n",
205 errno, errno > -1 && errno < sys_nerr ?
206 sys_errlist[errno] : "unknown", 0 );
209 case 0: /* timeout - let threads run */
210 Debug( LDAP_DEBUG_CONNS, "select timeout - yielding\n",
215 default: /* something happened - deal with it */
216 Debug( LDAP_DEBUG_CONNS, "select activity\n", 0, 0, 0 );
219 pthread_mutex_lock( ¤ttime_mutex );
220 time( ¤ttime );
221 pthread_mutex_unlock( ¤ttime_mutex );
224 pthread_mutex_lock( &new_conn_mutex );
225 if ( FD_ISSET( tcps, &readfds ) ) {
227 if ( (ns = accept( tcps, (struct sockaddr *) &from,
229 Debug( LDAP_DEBUG_ANY,
230 "accept() failed errno %d (%s)", errno,
231 errno > -1 && errno < sys_nerr ?
232 sys_errlist[errno] : "unknown", 0 );
233 pthread_mutex_unlock( &new_conn_mutex );
236 if ( ioctl( ns, FIONBIO, (caddr_t) &on ) == -1 ) {
237 Debug( LDAP_DEBUG_ANY,
238 "FIONBIO ioctl on %d faled\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 );
248 if ( getpeername( ns, (struct sockaddr *) &from, &len )
251 #ifdef REVERSE_LOOKUP
252 hp = gethostbyaddr( (char *)
253 &(from.sin_addr.s_addr),
254 sizeof(from.sin_addr.s_addr), AF_INET );
259 Statslog( LDAP_DEBUG_STATS,
260 "conn=%d fd=%d connection from %s (%s)\n",
261 c[ns].c_connid, ns, hp == NULL ? "unknown"
262 : hp->h_name, inet_ntoa( from.sin_addr ),
265 if ( c[ns].c_addr != NULL ) {
266 free( c[ns].c_addr );
268 c[ns].c_addr = strdup( inet_ntoa(
270 if ( c[ns].c_domain != NULL ) {
271 free( c[ns].c_domain );
273 c[ns].c_domain = strdup( hp == NULL ? "" :
275 /* normalize the domain */
276 for ( s = c[ns].c_domain; *s; s++ ) {
280 Statslog( LDAP_DEBUG_STATS,
281 "conn=%d fd=%d connection from unknown\n",
282 c[ns].c_connid, ns, 0, 0, 0 );
284 pthread_mutex_lock( &c[ns].c_dnmutex );
285 if ( c[ns].c_dn != NULL ) {
289 pthread_mutex_unlock( &c[ns].c_dnmutex );
290 c[ns].c_starttime = currenttime;
291 c[ns].c_opsinitiated = 0;
292 c[ns].c_opscompleted = 0;
294 pthread_mutex_unlock( &new_conn_mutex );
296 Debug( LDAP_DEBUG_CONNS, "activity on:", 0, 0, 0 );
297 for ( i = 0; i < dtblsize; i++ ) {
300 r = FD_ISSET( i, &readfds );
301 w = FD_ISSET( i, &writefds );
302 if ( i != tcps && (r || w) ) {
303 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
304 r ? "r" : "", w ? "w" : "" );
307 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
309 for ( i = 0; i < dtblsize; i++ ) {
310 if ( i == tcps || (! FD_ISSET( i, &readfds ) &&
311 ! FD_ISSET( i, &writefds )) ) {
315 if ( FD_ISSET( i, &writefds ) ) {
316 Debug( LDAP_DEBUG_CONNS,
317 "signaling write waiter on %d\n", i, 0, 0 );
319 pthread_mutex_lock( &active_threads_mutex );
320 pthread_cond_signal( &c[i].c_wcv );
321 c[i].c_writewaiter = 0;
323 pthread_mutex_unlock( &active_threads_mutex );
326 if ( FD_ISSET( i, &readfds ) ) {
327 Debug( LDAP_DEBUG_CONNS,
328 "read activity on %d\n", i, 0, 0 );
330 connection_activity( &c[i] );
338 pthread_mutex_lock( &active_threads_mutex );
339 Debug( LDAP_DEBUG_ANY,
340 "slapd shutting down - waiting for %d threads to terminate\n",
341 active_threads, 0, 0 );
342 while ( active_threads > 0 ) {
343 pthread_mutex_unlock( &active_threads_mutex );
345 pthread_mutex_lock( &active_threads_mutex );
347 pthread_mutex_unlock( &active_threads_mutex );
349 /* let backends do whatever cleanup they need to do */
350 Debug( LDAP_DEBUG_TRACE,
351 "slapd shutting down - waiting for backends to close down\n", 0, 0,
354 Debug( LDAP_DEBUG_ANY, "slapd stopping\n", 0, 0, 0 );
360 Debug( LDAP_DEBUG_ANY, "slapd got shutdown signal\n", 0, 0, 0 );
362 pthread_kill( listener_tid, SIGUSR1 );
363 (void) SIGNAL( SIGUSR2, (void *) set_shutdown );
364 (void) SIGNAL( SIGTERM, (void *) set_shutdown );
365 (void) SIGNAL( SIGHUP, (void *) set_shutdown );
371 Debug( LDAP_DEBUG_TRACE, "slapd got SIGUSR1\n", 0, 0, 0 );
372 (void) SIGNAL( SIGUSR1, (void *) do_nothing );