4 #include <sys/socket.h>
7 #include <netinet/in.h>
12 #include <sys/select.h>
16 #include "ldapconfig.h"
18 #include <sys/filio.h>
19 #else /* NEED_FILIO */
20 #include <sys/ioctl.h>
21 #endif /* NEED_FILIO */
24 #endif /* USE_SYSCONF */
26 extern Operation *op_add();
28 #ifndef SYSERRLIST_IN_STDIO
30 extern char *sys_errlist[];
32 extern time_t currenttime;
33 extern pthread_mutex_t currenttime_mutex;
34 extern int active_threads;
35 extern pthread_mutex_t active_threads_mutex;
36 extern pthread_mutex_t new_conn_mutex;
37 extern int slapd_shutdown;
38 extern pthread_t listener_tid;
40 extern pthread_mutex_t ops_mutex;
47 static void set_shutdown();
48 static void do_nothing();
57 unsigned long len, tag, msgid;
60 struct sockaddr_in addr;
67 dtblsize = sysconf( _SC_OPEN_MAX );
68 #else /* USE_SYSCONF */
69 dtblsize = getdtablesize();
70 #endif /* USE_SYSCONF */
72 c = (Connection *) ch_calloc( 1, dtblsize * sizeof(Connection) );
74 for ( i = 0; i < dtblsize; i++ ) {
80 c[i].c_sb.sb_options = LBER_NO_READ_AHEAD;
81 c[i].c_sb.sb_naddr = 0;
82 c[i].c_sb.sb_ber.ber_buf = NULL;
83 c[i].c_sb.sb_ber.ber_ptr = NULL;
84 c[i].c_sb.sb_ber.ber_end = NULL;
85 c[i].c_writewaiter = 0;
87 pthread_mutex_init( &c[i].c_dnmutex,
88 pthread_mutexattr_default );
89 pthread_mutex_init( &c[i].c_opsmutex,
90 pthread_mutexattr_default );
91 pthread_mutex_init( &c[i].c_pdumutex,
92 pthread_mutexattr_default );
93 pthread_cond_init( &c[i].c_wcv, pthread_condattr_default );
96 if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
97 Debug( LDAP_DEBUG_ANY, "socket() failed errno %d (%s)", errno,
98 errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
104 if ( setsockopt( tcps, SOL_SOCKET, SO_REUSEADDR, (char *) &i,
105 sizeof(i) ) == -1 ) {
106 Debug( LDAP_DEBUG_ANY, "setsockopt() failed errno %d (%s)",
107 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
111 (void) memset( (void *) &addr, '\0', sizeof(addr) );
112 addr.sin_family = AF_INET;
113 addr.sin_addr.s_addr = INADDR_ANY;
114 addr.sin_port = htons( port );
115 if ( bind( tcps, (struct sockaddr *) &addr, sizeof(addr) ) == -1 ) {
116 Debug( LDAP_DEBUG_ANY, "bind() failed errno %d (%s)\n",
117 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
122 if ( listen( tcps, 5 ) == -1 ) {
123 Debug( LDAP_DEBUG_ANY, "listen() failed errno %d (%s)",
124 errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
129 (void) SIGNAL( SIGPIPE, SIG_IGN );
130 (void) SIGNAL( SIGUSR1, (void *) do_nothing );
131 (void) SIGNAL( SIGUSR2, (void *) set_shutdown );
132 (void) SIGNAL( SIGTERM, (void *) set_shutdown );
133 (void) SIGNAL( SIGHUP, (void *) set_shutdown );
135 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
137 if ( (fp = fopen( SLAPD_PIDFILE, "w" )) != NULL ) {
138 fprintf( fp, "%d\n", getpid() );
142 #ifdef SLAPD_ARGSFILE
143 if ( (fp = fopen( SLAPD_ARGSFILE, "w" )) != NULL ) {
144 for ( i = 0; i < g_argc; i++ ) {
145 fprintf( fp, "%s ", g_argv[i] );
152 while ( !slapd_shutdown ) {
153 struct sockaddr_in from;
159 FD_ZERO( &writefds );
161 FD_SET( tcps, &readfds );
163 pthread_mutex_lock( &active_threads_mutex );
164 Debug( LDAP_DEBUG_CONNS,
165 "listening for connections on %d, activity on:",
167 for ( i = 0; i < dtblsize; i++ ) {
168 if ( c[i].c_sb.sb_sd != -1 ) {
169 FD_SET( c[i].c_sb.sb_sd, &readfds );
171 if ( c[i].c_writewaiter ) {
172 FD_SET( c[i].c_sb.sb_sd, &writefds );
174 Debug( LDAP_DEBUG_CONNS, " %dr%s", i,
175 c[i].c_writewaiter ? "w" : "", 0 );
178 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
182 Debug( LDAP_DEBUG_CONNS, "before select active_threads %d\n",
183 active_threads, 0, 0 );
184 #ifdef PTHREAD_PREEMPTIVE
187 tvp = active_threads ? &zero : NULL;
189 pthread_mutex_unlock( &active_threads_mutex );
191 switch ( select( dtblsize, &readfds, &writefds, 0, tvp ) ) {
192 case -1: /* failure - try again */
193 Debug( LDAP_DEBUG_CONNS,
194 "select failed errno %d (%s)\n",
195 errno, errno > -1 && errno < sys_nerr ?
196 sys_errlist[errno] : "unknown", 0 );
199 case 0: /* timeout - let threads run */
200 Debug( LDAP_DEBUG_CONNS, "select timeout - yielding\n",
205 default: /* something happened - deal with it */
206 Debug( LDAP_DEBUG_CONNS, "select activity\n", 0, 0, 0 );
209 pthread_mutex_lock( ¤ttime_mutex );
210 time( ¤ttime );
211 pthread_mutex_unlock( ¤ttime_mutex );
214 pthread_mutex_lock( &new_conn_mutex );
215 if ( FD_ISSET( tcps, &readfds ) ) {
217 if ( (ns = accept( tcps, (struct sockaddr *) &from,
219 Debug( LDAP_DEBUG_ANY,
220 "accept() failed errno %d (%s)", errno,
221 errno > -1 && errno < sys_nerr ?
222 sys_errlist[errno] : "unknown", 0 );
223 pthread_mutex_unlock( &new_conn_mutex );
226 if ( ioctl( ns, FIONBIO, (caddr_t) &on ) == -1 ) {
227 Debug( LDAP_DEBUG_ANY,
228 "FIONBIO ioctl on %d faled\n", ns, 0, 0 );
230 c[ns].c_sb.sb_sd = ns;
231 Debug( LDAP_DEBUG_CONNS, "new connection on %d\n", ns,
234 pthread_mutex_lock( &ops_mutex );
235 c[ns].c_connid = num_conns++;
236 pthread_mutex_unlock( &ops_mutex );
238 if ( getpeername( ns, (struct sockaddr *) &from, &len )
241 #ifdef REVERSE_LOOKUP
242 hp = gethostbyaddr( (char *)
243 &(from.sin_addr.s_addr),
244 sizeof(from.sin_addr.s_addr), AF_INET );
249 Statslog( LDAP_DEBUG_STATS,
250 "conn=%d fd=%d connection from %s (%s)\n",
251 c[ns].c_connid, ns, hp == NULL ? "unknown"
252 : hp->h_name, inet_ntoa( from.sin_addr ),
255 if ( c[ns].c_addr != NULL ) {
256 free( c[ns].c_addr );
258 c[ns].c_addr = strdup( inet_ntoa(
260 if ( c[ns].c_domain != NULL ) {
261 free( c[ns].c_domain );
263 c[ns].c_domain = strdup( hp == NULL ? "" :
265 /* normalize the domain */
266 for ( s = c[ns].c_domain; *s; s++ ) {
270 Statslog( LDAP_DEBUG_STATS,
271 "conn=%d fd=%d connection from unknown\n",
272 c[ns].c_connid, ns, 0, 0, 0 );
274 pthread_mutex_lock( &c[ns].c_dnmutex );
275 if ( c[ns].c_dn != NULL ) {
279 pthread_mutex_unlock( &c[ns].c_dnmutex );
280 c[ns].c_starttime = currenttime;
281 c[ns].c_opsinitiated = 0;
282 c[ns].c_opscompleted = 0;
284 pthread_mutex_unlock( &new_conn_mutex );
286 Debug( LDAP_DEBUG_CONNS, "activity on:", 0, 0, 0 );
287 for ( i = 0; i < dtblsize; i++ ) {
290 r = FD_ISSET( i, &readfds );
291 w = FD_ISSET( i, &writefds );
292 if ( i != tcps && (r || w) ) {
293 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
294 r ? "r" : "", w ? "w" : "" );
297 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
299 for ( i = 0; i < dtblsize; i++ ) {
300 if ( i == tcps || (! FD_ISSET( i, &readfds ) &&
301 ! FD_ISSET( i, &writefds )) ) {
305 if ( FD_ISSET( i, &writefds ) ) {
306 Debug( LDAP_DEBUG_CONNS,
307 "signaling write waiter on %d\n", i, 0, 0 );
309 pthread_mutex_lock( &active_threads_mutex );
310 pthread_cond_signal( &c[i].c_wcv );
311 c[i].c_writewaiter = 0;
313 pthread_mutex_unlock( &active_threads_mutex );
316 if ( FD_ISSET( i, &readfds ) ) {
317 Debug( LDAP_DEBUG_CONNS,
318 "read activity on %d\n", i, 0, 0 );
320 connection_activity( &c[i] );
328 pthread_mutex_lock( &active_threads_mutex );
329 Debug( LDAP_DEBUG_ANY,
330 "slapd shutting down - waiting for %d threads to terminate\n",
331 active_threads, 0, 0 );
332 while ( active_threads > 0 ) {
333 pthread_mutex_unlock( &active_threads_mutex );
335 pthread_mutex_lock( &active_threads_mutex );
337 pthread_mutex_unlock( &active_threads_mutex );
339 /* let backends do whatever cleanup they need to do */
340 Debug( LDAP_DEBUG_TRACE,
341 "slapd shutting down - waiting for backends to close down\n", 0, 0,
344 Debug( LDAP_DEBUG_ANY, "slapd stopping\n", 0, 0, 0 );
350 Debug( LDAP_DEBUG_ANY, "slapd got shutdown signal\n", 0, 0, 0 );
352 pthread_kill( listener_tid, SIGUSR1 );
353 (void) SIGNAL( SIGUSR2, (void *) set_shutdown );
354 (void) SIGNAL( SIGTERM, (void *) set_shutdown );
355 (void) SIGNAL( SIGHUP, (void *) set_shutdown );
361 Debug( LDAP_DEBUG_TRACE, "slapd got SIGUSR1\n", 0, 0, 0 );
362 (void) SIGNAL( SIGUSR1, (void *) do_nothing );