]> git.sur5r.net Git - openldap/blob - servers/slapd/daemon.c
Initial check of connection states. Have only implemented
[openldap] / servers / slapd / daemon.c
1
2 /* Revision history
3  *
4  * 5-Jun-96     hodges
5  *      Added locking of new_conn_mutex when traversing the c[] array.
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/ctype.h>
13 #include <ac/errno.h>
14 #include <ac/signal.h>
15 #include <ac/socket.h>
16 #include <ac/string.h>
17 #include <ac/time.h>
18 #include <ac/unistd.h>
19
20 #include "ldapconfig.h"
21 #include "slap.h"
22
23 #ifdef HAVE_SYS_FILIO_H
24 #include <sys/filio.h>
25 #elif HAVE_SYS_IOCTL_H
26 #include <sys/ioctl.h>
27 #endif
28
29 #ifdef LDAP_DEBUG
30 #include <assert.h>
31 #else
32 #define assert( cond )
33 #endif
34
35 #ifdef HAVE_TCPD
36 #include <tcpd.h>
37
38 int allow_severity = LOG_INFO;
39 int deny_severity = LOG_NOTICE;
40 #endif /* TCP Wrappers */
41
42 int             dtblsize;
43 Connection      *c;
44
45 static volatile sig_atomic_t slapd_shutdown = 0;
46
47 /* a link to the slapd.conf configuration parameters */
48 extern char *slapd_pid_file;
49 extern char *slapd_args_file;
50
51 void *
52 slapd_daemon(
53     void *port
54 )
55 {
56         int                     i;
57         int                     tcps, ns;
58         struct sockaddr_in      addr;
59         fd_set                  readfds;
60         fd_set                  writefds;
61         FILE                    *fp;
62
63 #ifdef HAVE_SYSCONF
64         dtblsize = sysconf( _SC_OPEN_MAX );
65 #elif HAVE_GETDTABLESIZE
66         dtblsize = getdtablesize();
67 #else
68         dtblsize = FD_SETSIZE
69 #endif
70
71 #ifdef FD_SETSIZE
72         if(dtblsize > FD_SETSIZE) {
73                 dtblsize = FD_SETSIZE;
74         }
75 #endif  /* !FD_SETSIZE */
76
77         c = (Connection *) ch_calloc( 1, dtblsize * sizeof(Connection) );
78
79         for ( i = 0; i < dtblsize; i++ ) {
80                 c[i].c_dn = NULL;
81                 c[i].c_cdn = NULL;
82                 c[i].c_addr = NULL;
83                 c[i].c_domain = NULL;
84                 c[i].c_ops = NULL;
85                 lber_pvt_sb_init( &c[i].c_sb );
86                 ldap_pvt_thread_mutex_init( &c[i].c_dnmutex );
87                 ldap_pvt_thread_mutex_init( &c[i].c_opsmutex );
88                 ldap_pvt_thread_mutex_init( &c[i].c_pdumutex );
89                 ldap_pvt_thread_cond_init( &c[i].c_wcv );
90         }
91
92         if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
93                 Debug( LDAP_DEBUG_ANY, "socket() failed errno %d (%s)", errno,
94                     errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
95                     "unknown", 0 );
96                 exit( 1 );
97         }
98
99         i = 1;
100         if ( setsockopt( tcps, SOL_SOCKET, SO_REUSEADDR, (char *) &i,
101             sizeof(i) ) == -1 ) {
102                 Debug( LDAP_DEBUG_ANY, "setsockopt() failed errno %d (%s)",
103                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
104                     "unknown", 0 );
105         }
106
107         (void) memset( (void *) &addr, '\0', sizeof(addr) );
108         addr.sin_family = AF_INET;
109         addr.sin_addr.s_addr = INADDR_ANY;
110         addr.sin_port = htons( (int)port );
111         if ( bind( tcps, (struct sockaddr *) &addr, sizeof(addr) ) == -1 ) {
112                 Debug( LDAP_DEBUG_ANY, "bind() failed errno %d (%s)\n",
113                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
114                     "unknown", 0 );
115                 exit( 1 );
116         }
117
118         if ( listen( tcps, 5 ) == -1 ) {
119                 Debug( LDAP_DEBUG_ANY, "listen() failed errno %d (%s)",
120                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
121                     "unknown", 0 );
122                 exit( 1 );
123         }
124
125         Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
126
127         if (( slapd_pid_file != NULL ) &&
128                         (( fp = fopen( slapd_pid_file, "w" )) != NULL )) {
129                 fprintf( fp, "%d\n", (int) getpid() );
130                 fclose( fp );
131         }
132
133         if (( slapd_args_file != NULL ) &&
134                         (( fp = fopen( slapd_args_file, "w" )) != NULL )) {
135                 for ( i = 0; i < g_argc; i++ ) {
136                         fprintf( fp, "%s ", g_argv[i] );
137                 }
138                 fprintf( fp, "\n" );
139                 fclose( fp );
140         }
141
142         while ( !slapd_shutdown ) {
143                 struct sockaddr_in      from;
144                 struct hostent          *hp;
145                 struct timeval          zero;
146                 struct timeval          *tvp;
147                 int                     len;
148                 int                     data_ready;
149
150                 char    *client_name;
151                 char    *client_addr;
152
153                 FD_ZERO( &writefds );
154                 FD_ZERO( &readfds );
155                 FD_SET( tcps, &readfds );
156
157                 zero.tv_sec = 0;
158                 zero.tv_usec = 0;
159
160                 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
161                 Debug( LDAP_DEBUG_CONNS,
162                     "listening for connections on %d, activity on:",
163                     tcps, 0, 0 );
164            
165                 data_ready = 0;
166
167                 ldap_pvt_thread_mutex_lock( &new_conn_mutex );
168                 for ( i = 0; i < dtblsize; i++ ) {
169                         if ( (c[i].c_state != SLAP_C_INACTIVE)  
170                                 && (c[i].c_state != SLAP_C_CLOSING) )
171                         {
172 #ifdef LDAP_DEBUG
173                                 assert(lber_pvt_sb_in_use( &c[i].c_sb ));
174 #endif
175                                 FD_SET( lber_pvt_sb_get_desc(&c[i].c_sb),
176                                         &readfds );
177                                 if (lber_pvt_sb_data_ready(&c[i].c_sb))
178                                         data_ready = 1;
179                                 if ( c[i].c_writewaiter ) {
180                                         FD_SET( lber_pvt_sb_get_desc(&c[i].c_sb),
181                                                 &writefds );
182                                 }
183                                 Debug( LDAP_DEBUG_CONNS, " %dr%s", i,
184                                     c[i].c_writewaiter ? "w" : "", 0 );
185                         }
186                 }
187
188                 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
189                 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
190
191                 Debug( LDAP_DEBUG_CONNS, "before select active_threads %d\n",
192                     active_threads, 0, 0 );
193 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
194                 tvp = (data_ready) ? &zero : NULL;
195 #else
196                 tvp = (active_threads || data_ready) ? &zero : NULL;
197 #endif
198                 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
199
200                 switch ( i = select( dtblsize, &readfds, &writefds, 0, tvp ) ) {
201                 case -1:        /* failure - try again */
202                         Debug( LDAP_DEBUG_CONNS,
203                             "select failed errno %d (%s)\n",
204                             errno, errno > -1 && errno < sys_nerr ?
205                             sys_errlist[errno] : "unknown", 0 );
206                         continue;
207
208                 case 0:         /* timeout - let threads run */
209                         Debug( LDAP_DEBUG_CONNS, "select timeout - yielding\n",
210                             0, 0, 0 );
211                         if (!data_ready)
212                                 ldap_pvt_thread_yield();
213                         continue;
214
215                 default:        /* something happened - deal with it */
216                         Debug( LDAP_DEBUG_CONNS, "select activity on %d descriptors\n", i, 0, 0 );
217                         ;       /* FALL */
218                 }
219                 ldap_pvt_thread_mutex_lock( &currenttime_mutex );
220                 time( &currenttime );
221                 ldap_pvt_thread_mutex_unlock( &currenttime_mutex );
222
223                 /* new connection */
224                 ldap_pvt_thread_mutex_lock( &new_conn_mutex );
225                 if ( FD_ISSET( tcps, &readfds ) ) {
226                         len = sizeof(from);
227                         if ( (ns = accept( tcps, (struct sockaddr *) &from,
228                             &len )) == -1 ) {
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                                 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
234                                 continue;
235                         }
236
237                         /* make sure descriptor number isn't too great */
238                         if ( ns >= dtblsize ) {
239                                 Debug( LDAP_DEBUG_ANY,
240                                         "new connection on %d beyond descriptor table size %d\n",
241                                         ns, dtblsize, 0 );
242                                 close(ns);
243                                 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
244                                 continue;
245                         }
246                    
247                         Debug( LDAP_DEBUG_CONNS, "new connection on %d\n", ns,
248                             0, 0 );
249
250                         len = sizeof(from);
251
252                         if ( getpeername( ns, (struct sockaddr *) &from, &len )
253                             == 0 ) {
254                                 char *s;
255                                 client_addr = inet_ntoa( from.sin_addr );
256
257 #if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
258                                 hp = gethostbyaddr( (char *)
259                                     &(from.sin_addr.s_addr),
260                                     sizeof(from.sin_addr.s_addr), AF_INET );
261
262                                 if(hp) {
263                                         client_name = hp->h_name;
264
265                                         /* normalize the domain */
266                                         for ( s = client_name; *s; s++ ) {
267                                                 *s = TOLOWER( (unsigned char) *s );
268                                         }
269
270                                 } else {
271                                         client_name = NULL;
272                                 }
273 #else
274                                 client_name = NULL;
275 #endif
276
277                         } else {
278                                 client_name = NULL;;
279                                 client_addr = NULL;
280                         }
281
282 #ifdef HAVE_TCPD
283                         if(!hosts_ctl("slapd",
284                                 client_name != NULL ? client_name : STRING_UNKNOWN,
285                                 client_addr != NULL ? client_addr : STRING_UNKNOWN,
286                                 STRING_UNKNOWN))
287                         {
288                                 /* DENY ACCESS */
289                                 Statslog( LDAP_DEBUG_ANY,
290                                  "fd=%d connection from %s (%s) denied.\n",
291                                         ns,
292                                                 client_name == NULL ? "unknown" : client_name,
293                                                 client_addr == NULL ? "unknown" : client_addr,
294                                   0, 0 );
295
296                                 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
297                                 continue;
298                         }
299 #endif /* HAVE_TCPD */
300
301
302                         ldap_pvt_thread_mutex_lock( &ops_mutex );
303                         c[ns].c_connid = num_conns++;
304                         ldap_pvt_thread_mutex_unlock( &ops_mutex );
305
306                         Statslog( LDAP_DEBUG_STATS,
307                             "conn=%d fd=%d connection from %s (%s) accepted.\n",
308                                 c[ns].c_connid, ns,
309                                         client_name == NULL ? "unknown" : client_name,
310                                         client_addr == NULL ? "unknown" : client_addr,
311                              0 );
312
313                         if ( c[ns].c_addr != NULL ) {
314                                 free( c[ns].c_addr );
315                         }
316                         c[ns].c_addr = ch_strdup( client_addr );
317
318                         if ( c[ns].c_domain != NULL ) {
319                                 free( c[ns].c_domain );
320                         }
321
322                         c[ns].c_domain = ch_strdup( client_name == NULL
323                                 ? "" : client_name );
324
325                         ldap_pvt_thread_mutex_lock( &c[ns].c_dnmutex );
326                         if ( c[ns].c_dn != NULL ) {
327                                 free( c[ns].c_dn );
328                                 c[ns].c_dn = NULL;
329                         }
330                         if ( c[ns].c_cdn != NULL ) {
331                                 free( c[ns].c_cdn );
332                                 c[ns].c_cdn = NULL;
333                         }
334                         ldap_pvt_thread_mutex_unlock( &c[ns].c_dnmutex );
335
336                         c[ns].c_starttime = currenttime;
337                         c[ns].c_ops_received = 0;
338                         c[ns].c_ops_executing = 0;
339                         c[ns].c_ops_pending = 0;
340                         c[ns].c_ops_completed = 0;
341
342                         lber_pvt_sb_set_desc( &c[ns].c_sb, ns );
343                         lber_pvt_sb_set_io( &c[ns].c_sb, &lber_pvt_sb_io_tcp, NULL );
344                    
345                         if (lber_pvt_sb_set_nonblock( &c[ns].c_sb, 1)<0) {                         
346                                 Debug( LDAP_DEBUG_ANY,
347                                     "FIONBIO ioctl on %d failed\n", ns, 0, 0 );
348                         }
349
350                         c[ns].c_state = SLAP_C_ACTIVE;
351                 }
352                 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
353
354                 Debug( LDAP_DEBUG_CONNS, "activity on:", 0, 0, 0 );
355                 for ( i = 0; i < dtblsize; i++ ) {
356                         int     r, w;
357
358                         r = FD_ISSET( i, &readfds );
359                         w = FD_ISSET( i, &writefds );
360                         if ( i != tcps && (r || w) ) {
361                                 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
362                                     r ? "r" : "", w ? "w" : "" );
363                         }
364                 }
365                 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
366
367                 for ( i = 0; i < dtblsize; i++ ) {
368                         if ( i == tcps || (! FD_ISSET( i, &readfds ) &&
369                             ! FD_ISSET( i, &writefds )) ) {
370                                 continue;
371                         }
372
373                         if ( FD_ISSET( i, &writefds ) ) {
374                                 Debug( LDAP_DEBUG_CONNS,
375                                     "signaling write waiter on %d\n", i, 0, 0 );
376
377                                 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
378                                 active_threads++;
379                                 c[i].c_writewaiter = 0;
380                                 ldap_pvt_thread_cond_signal( &c[i].c_wcv );
381                                 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
382                         }
383
384                         if ( FD_ISSET( i, &readfds ) || 
385                                 lber_pvt_sb_data_ready( &c[i].c_sb ) ) {
386                                 Debug( LDAP_DEBUG_CONNS,
387                                     "read activity on %d\n", i, 0, 0 );
388
389                                 connection_activity( &c[i] );
390                         }
391                 }
392
393                 ldap_pvt_thread_yield();
394         }
395
396         Debug( LDAP_DEBUG_TRACE,
397             "slapd shutdown: shutdown initiated.\n",
398             0, 0, 0 );
399
400         close( tcps );
401
402         ldap_pvt_thread_mutex_lock( &active_threads_mutex );
403         Debug( LDAP_DEBUG_ANY,
404             "slapd shutdown: waiting for %d threads to terminate\n",
405             active_threads, 0, 0 );
406         while ( active_threads > 0 ) {
407                 ldap_pvt_thread_cond_wait(&active_threads_cond, &active_threads_mutex);
408         }
409         ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
410
411         return NULL;
412 }
413
414 void
415 slap_set_shutdown( int sig )
416 {
417         Debug( LDAP_DEBUG_ANY, "slapd got shutdown signal %d\n", sig, 0, 0 );
418         slapd_shutdown = 1;
419         ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
420
421         /* reinstall self */
422         (void) SIGNAL( sig, slap_set_shutdown );
423 }
424
425 void
426 slap_do_nothing( int sig )
427 {
428         Debug( LDAP_DEBUG_TRACE, "slapd got do_nothing signal %d\n", sig, 0, 0 );
429
430         /* reinstall self */
431         (void) SIGNAL( sig, slap_do_nothing );
432 }