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