5 * Added locking of new_conn_mutex when traversing the c[] array.
8 #define DISABLE_BRIDGE /* disable bridge code */
12 #include <ac/string.h>
13 #include <sys/types.h>
14 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
22 #include <sys/select.h>
25 #include "ldapconfig.h"
27 #include <sys/filio.h>
28 #else /* NEED_FILIO */
29 #include <sys/ioctl.h>
30 #endif /* NEED_FILIO */
33 #endif /* USE_SYSCONF */
38 int allow_severity = LOG_INFO;
39 int deny_severity = LOG_NOTICE;
40 #endif /* TCP_WRAPPERS */
42 extern Operation *op_add();
44 #ifdef DECL_SYS_ERRLIST
46 extern char *sys_errlist[];
49 extern time_t currenttime;
50 extern pthread_mutex_t currenttime_mutex;
51 extern int active_threads;
52 extern pthread_mutex_t active_threads_mutex;
53 extern pthread_mutex_t new_conn_mutex;
54 extern int slapd_shutdown;
55 extern pthread_t listener_tid;
57 extern pthread_mutex_t ops_mutex;
64 static void set_shutdown();
65 static void do_nothing();
74 unsigned long len, tag, msgid;
77 struct sockaddr_in addr;
84 dtblsize = sysconf( _SC_OPEN_MAX );
85 #else /* USE_SYSCONF */
86 dtblsize = getdtablesize();
87 #endif /* USE_SYSCONF */
89 * Add greg@greg.rim.or.jp
92 if(dtblsize > FD_SETSIZE) {
93 dtblsize = FD_SETSIZE;
95 #endif /* !FD_SETSIZE */
97 c = (Connection *) ch_calloc( 1, dtblsize * sizeof(Connection) );
99 for ( i = 0; i < dtblsize; i++ ) {
102 c[i].c_domain = NULL;
104 c[i].c_sb.sb_sd = -1;
105 c[i].c_sb.sb_options = LBER_NO_READ_AHEAD;
106 c[i].c_sb.sb_naddr = 0;
107 c[i].c_sb.sb_ber.ber_buf = NULL;
108 c[i].c_sb.sb_ber.ber_ptr = NULL;
109 c[i].c_sb.sb_ber.ber_end = NULL;
110 c[i].c_writewaiter = 0;
112 pthread_mutex_init( &c[i].c_dnmutex,
113 pthread_mutexattr_default );
114 pthread_mutex_init( &c[i].c_opsmutex,
115 pthread_mutexattr_default );
116 pthread_mutex_init( &c[i].c_pdumutex,
117 pthread_mutexattr_default );
118 pthread_cond_init( &c[i].c_wcv, pthread_condattr_default );
121 if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
122 Debug( LDAP_DEBUG_ANY, "socket() failed errno %d (%s)", errno,
123 errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
129 if ( setsockopt( tcps, SOL_SOCKET, SO_REUSEADDR, (char *) &i,
130 sizeof(i) ) == -1 ) {
131 Debug( LDAP_DEBUG_ANY, "setsockopt() failed errno %d (%s)",
132 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
136 (void) memset( (void *) &addr, '\0', sizeof(addr) );
137 addr.sin_family = AF_INET;
138 addr.sin_addr.s_addr = INADDR_ANY;
139 addr.sin_port = htons( port );
140 if ( bind( tcps, (struct sockaddr *) &addr, sizeof(addr) ) == -1 ) {
141 Debug( LDAP_DEBUG_ANY, "bind() failed errno %d (%s)\n",
142 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
147 if ( listen( tcps, 5 ) == -1 ) {
148 Debug( LDAP_DEBUG_ANY, "listen() failed errno %d (%s)",
149 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
154 (void) SIGNAL( SIGPIPE, SIG_IGN );
157 * LinuxThreads are implemented using SIGUSR1/USR2,
158 * so we'll use SIGSTKFLT and SIGUNUSED
160 (void) SIGNAL( SIGSTKFLT, (void *) do_nothing );
161 (void) SIGNAL( SIGUNUSED, (void *) set_shutdown );
163 (void) SIGNAL( SIGUSR1, (void *) do_nothing );
164 (void) SIGNAL( SIGUSR2, (void *) set_shutdown );
166 (void) SIGNAL( SIGTERM, (void *) set_shutdown );
167 (void) SIGNAL( SIGINT, (void *) set_shutdown );
168 (void) SIGNAL( SIGHUP, (void *) set_shutdown );
170 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
172 if ( (fp = fopen( SLAPD_PIDFILE, "w" )) != NULL ) {
173 fprintf( fp, "%d\n", getpid() );
177 #ifdef SLAPD_ARGSFILE
178 if ( (fp = fopen( SLAPD_ARGSFILE, "w" )) != NULL ) {
179 for ( i = 0; i < g_argc; i++ ) {
180 fprintf( fp, "%s ", g_argv[i] );
187 while ( !slapd_shutdown ) {
188 struct sockaddr_in from;
197 FD_ZERO( &writefds );
199 FD_SET( tcps, &readfds );
204 pthread_mutex_lock( &active_threads_mutex );
205 Debug( LDAP_DEBUG_CONNS,
206 "listening for connections on %d, activity on:",
209 pthread_mutex_lock( &new_conn_mutex );
210 for ( i = 0; i < dtblsize; i++ ) {
211 if ( c[i].c_sb.sb_sd != -1 ) {
212 FD_SET( c[i].c_sb.sb_sd, &readfds );
214 if ( c[i].c_writewaiter ) {
215 FD_SET( c[i].c_sb.sb_sd, &writefds );
217 Debug( LDAP_DEBUG_CONNS, " %dr%s", i,
218 c[i].c_writewaiter ? "w" : "", 0 );
221 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
222 pthread_mutex_unlock( &new_conn_mutex );
224 Debug( LDAP_DEBUG_CONNS, "before select active_threads %d\n",
225 active_threads, 0, 0 );
226 #if defined(THREAD_PREEMPTIVE) || defined(NO_THREADS)
229 tvp = active_threads ? &zero : NULL;
231 pthread_mutex_unlock( &active_threads_mutex );
233 switch ( i = select( dtblsize, &readfds, &writefds, 0, tvp ) ) {
234 case -1: /* failure - try again */
235 Debug( LDAP_DEBUG_CONNS,
236 "select failed errno %d (%s)\n",
237 errno, errno > -1 && errno < sys_nerr ?
238 sys_errlist[errno] : "unknown", 0 );
241 case 0: /* timeout - let threads run */
242 Debug( LDAP_DEBUG_CONNS, "select timeout - yielding\n",
247 default: /* something happened - deal with it */
248 Debug( LDAP_DEBUG_CONNS, "select activity on %d descriptors\n", i, 0, 0 );
251 pthread_mutex_lock( ¤ttime_mutex );
252 time( ¤ttime );
253 pthread_mutex_unlock( ¤ttime_mutex );
256 pthread_mutex_lock( &new_conn_mutex );
257 if ( FD_ISSET( tcps, &readfds ) ) {
259 if ( (ns = accept( tcps, (struct sockaddr *) &from,
261 Debug( LDAP_DEBUG_ANY,
262 "accept() failed errno %d (%s)", errno,
263 errno > -1 && errno < sys_nerr ?
264 sys_errlist[errno] : "unknown", 0 );
265 pthread_mutex_unlock( &new_conn_mutex );
268 if ( ioctl( ns, FIONBIO, (caddr_t) &on ) == -1 ) {
269 Debug( LDAP_DEBUG_ANY,
270 "FIONBIO ioctl on %d failed\n", ns, 0, 0 );
273 c[ns].c_sb.sb_sd = ns;
274 Debug( LDAP_DEBUG_CONNS, "new connection on %d\n", ns,
277 pthread_mutex_lock( &ops_mutex );
278 c[ns].c_connid = num_conns++;
279 pthread_mutex_unlock( &ops_mutex );
283 if ( getpeername( ns, (struct sockaddr *) &from, &len )
286 client_addr = inet_ntoa( from.sin_addr );
288 #if defined(REVERSE_LOOKUP) || defined(TCP_WRAPPERS)
289 hp = gethostbyaddr( (char *)
290 &(from.sin_addr.s_addr),
291 sizeof(from.sin_addr.s_addr), AF_INET );
294 client_name = hp->h_name;
296 /* normalize the domain */
297 for ( s = client_name; *s; s++ ) {
314 if(!hosts_ctl("slapd", client_name, client_addr,
318 Statslog( LDAP_DEBUG_STATS,
319 "conn=%d fd=%d connection from %s (%s) denied.\n",
321 client_name == NULL ? "unknown" : client_name,
322 client_addr == NULL ? "unknown" : client_addr,
326 pthread_mutex_unlock( &new_conn_mutex );
329 #endif /* TCP_WRAPPERS */
331 Statslog( LDAP_DEBUG_STATS,
332 "conn=%d fd=%d connection from %s (%s) accepted.\n",
334 client_name == NULL ? "unknown" : client_name,
335 client_addr == NULL ? "unknown" : client_addr,
338 if ( c[ns].c_addr != NULL ) {
339 free( c[ns].c_addr );
341 c[ns].c_addr = strdup( client_addr );
343 if ( c[ns].c_domain != NULL ) {
344 free( c[ns].c_domain );
347 c[ns].c_domain = strdup( client_name == NULL
348 ? "" : client_name );
350 pthread_mutex_lock( &c[ns].c_dnmutex );
351 if ( c[ns].c_dn != NULL ) {
355 pthread_mutex_unlock( &c[ns].c_dnmutex );
356 c[ns].c_starttime = currenttime;
357 c[ns].c_opsinitiated = 0;
358 c[ns].c_opscompleted = 0;
360 pthread_mutex_unlock( &new_conn_mutex );
362 Debug( LDAP_DEBUG_CONNS, "activity on:", 0, 0, 0 );
363 for ( i = 0; i < dtblsize; i++ ) {
366 r = FD_ISSET( i, &readfds );
367 w = FD_ISSET( i, &writefds );
368 if ( i != tcps && (r || w) ) {
369 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
370 r ? "r" : "", w ? "w" : "" );
373 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
375 for ( i = 0; i < dtblsize; i++ ) {
376 if ( i == tcps || (! FD_ISSET( i, &readfds ) &&
377 ! FD_ISSET( i, &writefds )) ) {
381 if ( FD_ISSET( i, &writefds ) ) {
382 Debug( LDAP_DEBUG_CONNS,
383 "signaling write waiter on %d\n", i, 0, 0 );
385 pthread_mutex_lock( &active_threads_mutex );
386 pthread_cond_signal( &c[i].c_wcv );
387 c[i].c_writewaiter = 0;
389 pthread_mutex_unlock( &active_threads_mutex );
392 if ( FD_ISSET( i, &readfds ) ) {
393 Debug( LDAP_DEBUG_CONNS,
394 "read activity on %d\n", i, 0, 0 );
396 connection_activity( &c[i] );
404 pthread_mutex_lock( &active_threads_mutex );
405 Debug( LDAP_DEBUG_ANY,
406 "slapd shutting down - waiting for %d threads to terminate\n",
407 active_threads, 0, 0 );
408 while ( active_threads > 0 ) {
409 pthread_mutex_unlock( &active_threads_mutex );
411 pthread_mutex_lock( &active_threads_mutex );
413 pthread_mutex_unlock( &active_threads_mutex );
415 /* let backends do whatever cleanup they need to do */
416 Debug( LDAP_DEBUG_TRACE,
417 "slapd shutting down - waiting for backends to close down\n", 0, 0,
420 Debug( LDAP_DEBUG_ANY, "slapd stopping\n", 0, 0, 0 );
426 Debug( LDAP_DEBUG_ANY, "slapd got shutdown signal\n", 0, 0, 0 );
430 * LinuxThreads are implemented using SIGUSR1/USR2,
431 * so we'll use SIGSTKFLT and SIGUNUSED
433 pthread_kill( listener_tid, SIGSTKFLT );
434 (void) SIGNAL( SIGUNUSED, (void *) set_shutdown );
436 pthread_kill( listener_tid, SIGUSR1 );
437 (void) SIGNAL( SIGUSR2, (void *) set_shutdown );
439 (void) SIGNAL( SIGTERM, (void *) set_shutdown );
440 (void) SIGNAL( SIGINT, (void *) set_shutdown );
441 (void) SIGNAL( SIGHUP, (void *) set_shutdown );
447 Debug( LDAP_DEBUG_TRACE, "slapd got do_nothing signal\n", 0, 0, 0 );
450 * LinuxThreads are implemented using SIGUSR1/USR2,
451 * so we'll use SIGSTKFLT and SIGUNUSED
453 (void) SIGNAL( SIGSTKFLT, (void *) do_nothing );
455 (void) SIGNAL( SIGUSR1, (void *) do_nothing );