]> git.sur5r.net Git - openldap/blob - servers/slapd/daemon.c
72ece692e9911fe775b82cc1ad992c52b6428775
[openldap] / servers / slapd / daemon.c
1 #include "portable.h"
2
3 #include <stdio.h>
4
5 #include <ac/ctype.h>
6 #include <ac/errno.h>
7 #include <ac/signal.h>
8 #include <ac/socket.h>
9 #include <ac/string.h>
10 #include <ac/time.h>
11 #include <ac/unistd.h>
12
13 #include "ldapconfig.h"
14 #include "slap.h"
15
16 #ifdef HAVE_SYS_FILIO_H
17 #include <sys/filio.h>
18 #elif HAVE_SYS_IOCTL_H
19 #include <sys/ioctl.h>
20 #endif
21
22 #ifdef HAVE_TCPD
23 #include <tcpd.h>
24
25 int allow_severity = LOG_INFO;
26 int deny_severity = LOG_NOTICE;
27 #endif /* TCP Wrappers */
28
29 /* globals */
30 int dtblsize;
31
32 static ldap_pvt_thread_t        listener_tid;
33 static volatile sig_atomic_t slapd_shutdown = 0;
34
35 struct slap_daemon {
36         ldap_pvt_thread_mutex_t sd_mutex;
37
38         int sd_nactives;
39
40 #ifndef HAVE_WINSOCK
41         /* In winsock, accept() returns values higher than dtblsize
42                 so don't bother with this optimization */
43         int sd_nfds;
44 #endif
45
46         fd_set sd_actives;
47         fd_set sd_readers;
48         fd_set sd_writers;
49 } slap_daemon; 
50
51 /*
52  * Add a descriptor to daemon control
53  */
54 static void slapd_add(int s) {
55         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
56
57         assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
58         assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
59         assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
60
61         if (s >= slap_daemon.sd_nfds) {
62                 slap_daemon.sd_nfds = s + 1;
63         }
64
65         FD_SET( s, &slap_daemon.sd_actives );
66         FD_SET( s, &slap_daemon.sd_readers );
67
68         Debug( LDAP_DEBUG_CONNS, "daemon: added %d%s%s\n", s,
69             FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
70                 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
71
72         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
73 }
74
75 /*
76  * Remove the descriptor from daemon control
77  */
78 void slapd_remove(int s) {
79         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
80
81         assert( s < slap_daemon.sd_nfds );
82         assert( FD_ISSET( s, &slap_daemon.sd_actives ));
83
84         Debug( LDAP_DEBUG_CONNS, "daemon: removing %d%s%s\n", s,
85             FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
86                 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
87
88         FD_CLR( s, &slap_daemon.sd_actives );
89         FD_CLR( s, &slap_daemon.sd_readers );
90         FD_CLR( s, &slap_daemon.sd_writers );
91
92         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
93 }
94
95 void slapd_clr_write(int s, int wake) {
96         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
97
98         assert( s < slap_daemon.sd_nfds );
99         assert( FD_ISSET( s, &slap_daemon.sd_actives) );
100         assert( FD_ISSET( s, &slap_daemon.sd_writers) );
101         FD_SET( s, &slap_daemon.sd_writers );
102
103         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
104
105         if( wake ) {
106                 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
107         }
108 }
109
110 void slapd_set_write(int s, int wake) {
111         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
112
113         FD_SET( s, &slap_daemon.sd_writers );
114
115         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
116
117         if( wake ) {
118                 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
119         }
120 }
121
122 static void slapd_close(int s) {
123         slapd_remove(s);
124
125         Debug( LDAP_DEBUG_CONNS, "daemon: closing %d\n", s, 0, 0 );
126         close(s);
127 }
128
129 static void *
130 slapd_daemon_task(
131         void *ptr
132 )
133 {
134         struct sockaddr_in *addr = ptr;
135         int     tcps = -1;
136
137 #ifdef HAVE_SYSCONF
138         dtblsize = sysconf( _SC_OPEN_MAX );
139 #elif HAVE_GETDTABLESIZE
140         dtblsize = getdtablesize();
141 #else
142         dtblsize = FD_SETSIZE
143 #endif
144
145 #ifdef FD_SETSIZE
146         if(dtblsize > FD_SETSIZE) {
147                 dtblsize = FD_SETSIZE;
148         }
149 #endif  /* !FD_SETSIZE */
150
151         connections_init();
152
153         ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
154         FD_ZERO( &slap_daemon.sd_readers );
155         FD_ZERO( &slap_daemon.sd_writers );
156
157         if( addr != NULL ) {
158                 int     tmp;
159
160                 if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
161                         Debug( LDAP_DEBUG_ANY,
162                                 "daemon: socket() failed errno %d (%s)", errno,
163                         errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
164                         "unknown", 0 );
165                         exit( 1 );
166                 }
167
168 #ifndef HAVE_WINSOCK
169                 if ( tcps >= dtblsize ) {
170                         Debug( LDAP_DEBUG_ANY,
171                                 "daemon: listener descriptor %d is too great",
172                                 tcps, dtblsize, 0 );
173                         exit( 1 );
174                 }
175 #endif
176
177                 tmp = 1;
178                 if ( setsockopt( tcps, SOL_SOCKET, SO_REUSEADDR,
179                         (char *) &tmp, sizeof(tmp) ) == -1 )
180                 {
181                         Debug( LDAP_DEBUG_ANY,
182                                 "slapd(%d): setsockopt() failed errno %d (%s)",
183                         tcps, errno,
184                                 errno > -1 && errno < sys_nerr
185                                         ? sys_errlist[errno] : "unknown" );
186
187                         errno = 0;
188                 }
189
190                 if ( bind( tcps, (struct sockaddr *) addr, sizeof(*addr) ) == -1 ) {
191                         Debug( LDAP_DEBUG_ANY, "daemon: bind(%d) failed errno %d (%s)\n",
192                         tcps, errno,
193                                 errno > -1 && errno < sys_nerr
194                                         ? sys_errlist[errno] : "unknown" );
195                         exit( 1 );
196                 }
197
198                 if ( listen( tcps, 5 ) == -1 ) {
199                         Debug( LDAP_DEBUG_ANY,
200                                 "daemon: listen(%d, 5) failed errno %d (%s)\n",
201                             tcps, errno,
202                                 errno > -1 && errno < sys_nerr
203                                         ? sys_errlist[errno] : "unknown" );
204                         exit( 1 );
205                 }
206
207                 slapd_add( tcps );
208
209         } else {
210                 if( connection_init( 0, NULL, NULL ) ) {
211                         Debug( LDAP_DEBUG_ANY,
212                                 "connection_init(%d) failed.\n",
213                                 0, 0, 0 );
214
215                         exit( 1 );
216                 }
217
218                 slapd_add( 0 );
219         }
220
221         while ( !slapd_shutdown ) {
222                 int i, ns, nfds;
223
224                 fd_set                  readfds;
225                 fd_set                  writefds;
226
227                 struct sockaddr_in      from;
228                 struct hostent          *hp;
229                 struct timeval          zero;
230                 struct timeval          *tvp;
231
232                 char    *client_name;
233                 char    *client_addr;
234
235                 FD_ZERO( &writefds );
236                 FD_ZERO( &readfds );
237
238                 zero.tv_sec = 0;
239                 zero.tv_usec = 0;
240
241                 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
242
243 #ifdef FD_SET_MANUAL_COPY
244                 for( s = 0; s < nfds; s++ ) {
245                         if(FD_ISSET( &slap_sd_writers, s )) {
246                                 FD_SET( &writefds, s );
247                         }
248                         if(FD_ISSET( &slap_sd_writers, s )) {
249                                 FD_SET( &writefds, s );
250                         }
251                 }
252 #else
253                 memcpy( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
254                 memcpy( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
255 #endif
256
257                 FD_SET( tcps, &readfds );
258
259 #ifndef HAVE_WINSOCK
260                 nfds = slap_daemon.sd_nfds;
261 #else
262                 nfds = dtblsize;
263 #endif
264
265                 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
266
267                 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
268 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
269                 tvp = NULL;
270 #else
271                 tvp = active_threads ? &zero : NULL;
272 #endif
273
274                 Debug( LDAP_DEBUG_CONNS,
275                         "daemon: select: tcps=%d active_threads=%d tvp=%s\n",
276                     tcps, active_threads,
277                         tvp == NULL ? "NULL" : "zero" );
278            
279
280                 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
281
282                 switch(ns = select( nfds, &readfds, &writefds, 0, tvp )) {
283                 case -1:        /* failure - try again */
284                         if( errno != EINTR ) {
285                                 Debug( LDAP_DEBUG_CONNS,
286                                         "daemon: select failed (%d): %s\n",
287                                         errno,
288                                         errno >= 0 && errno < sys_nerr
289                                                 ? sys_errlist[errno] : "unknown",
290                                         0 );
291
292                                 slapd_shutdown = -1;
293                         }
294                         errno = 0;
295                         continue;
296
297                 case 0:         /* timeout - let threads run */
298                         Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
299                             0, 0, 0 );
300                 ldap_pvt_thread_yield();
301                         continue;
302
303                 default:        /* something happened - deal with it */
304                         Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
305                                 ns, 0, 0 );
306                         /* FALL THRU */
307                 }
308
309                 if ( FD_ISSET( tcps, &readfds ) ) {
310                         int s;
311                         int len = sizeof(from);
312                         long id;
313
314                         if ( (s = accept( tcps,
315                                 (struct sockaddr *) &from, &len )) == -1 )
316                         {
317                                 Debug( LDAP_DEBUG_ANY,
318                                     "daemon: accept(%d) failed errno %d (%s)", errno,
319                                     tcps, errno >= 0 && errno < sys_nerr ?
320                                     sys_errlist[errno] : "unknown");
321                                 continue;
322                         }
323
324                         assert( !FD_ISSET( 0, &slap_daemon.sd_actives) );
325                         assert( !FD_ISSET( 0, &slap_daemon.sd_readers) );
326                         assert( !FD_ISSET( 0, &slap_daemon.sd_writers) );
327
328 #ifndef HAVE_WINSOCK
329                         /* make sure descriptor number isn't too great */
330                         if ( s >= dtblsize ) {
331                                 Debug( LDAP_DEBUG_ANY,
332                                         "daemon: %d beyond descriptor table size %d\n",
333                                         s, dtblsize, 0 );
334                                 close(s);
335                                 continue;
336                         }
337 #endif
338                    
339                         Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %d\n",
340                                 s, 0, 0 );
341
342                         len = sizeof(from);
343                         if ( getpeername( s, (struct sockaddr *) &from, &len ) == 0 ) {
344                                 client_addr = inet_ntoa( from.sin_addr );
345
346 #if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
347                                 hp = gethostbyaddr( (char *)
348                                     &(from.sin_addr.s_addr),
349                                     sizeof(from.sin_addr.s_addr), AF_INET );
350
351                                 if(hp) {
352                                         char *p;
353                                         client_name = hp->h_name;
354
355                                         /* normalize the domain */
356                                         for ( p = client_name; *p; p++ ) {
357                                                 *p = TOLOWER( (unsigned char) *p );
358                                         }
359
360                                 } else {
361                                         client_name = NULL;
362                                 }
363 #else
364                                 client_name = NULL;
365 #endif
366
367                         } else {
368                                 client_name = NULL;;
369                                 client_addr = NULL;
370                         }
371
372 #ifdef HAVE_TCPD
373                         if(!hosts_ctl("slapd",
374                                 client_name != NULL ? client_name : STRING_UNKNOWN,
375                                 client_addr != NULL ? client_addr : STRING_UNKNOWN,
376                                 STRING_UNKNOWN))
377                         {
378                                 /* DENY ACCESS */
379                                 Statslog( LDAP_DEBUG_ANY,
380                                  "fd=%d connection from %s (%s) denied.\n",
381                                         s,
382                                         client_name == NULL ? "unknown" : client_name,
383                                         client_addr == NULL ? "unknown" : client_addr,
384                                   0, 0 );
385
386                                 close(s);
387                                 continue;
388                         }
389 #endif /* HAVE_TCPD */
390
391                         if( (id = connection_init(s, client_name, client_addr)) < 0 ) {
392                                 Debug( LDAP_DEBUG_ANY,
393                                         "daemon: connection_init(%d, %s, %s) failed.\n",
394                                         s,
395                                         client_name == NULL ? "unknown" : client_name,
396                                         client_addr == NULL ? "unknown" : client_addr);
397                                 close(s);
398                                 continue;
399                         }
400
401                         Statslog( LDAP_DEBUG_STATS,
402                                 "daemon: conn=%d fd=%d connection from %s (%s) accepted.\n",
403                                 id, s,
404                                 client_name == NULL ? "unknown" : client_name,
405                                 client_addr == NULL ? "unknown" : client_addr,
406                                 0 );
407
408                         slapd_add( s );
409                         continue;
410                 }
411
412 #ifdef LDAP_DEBUG
413                 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
414
415                 for ( i = 0; i < nfds; i++ ) {
416                         int     a, r, w;
417
418                         r = FD_ISSET( i, &readfds );
419                         w = FD_ISSET( i, &writefds );
420                         if ( i != tcps && (r || w) ) {
421                                 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
422                                     r ? "r" : "", w ? "w" : "" );
423                         }
424                 }
425
426                 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
427 #endif
428
429                 /* loop through the writers */
430                 for ( i = 0; i < nfds; i++ ) {
431                         if ( i == tcps ) {
432                                 continue;
433                         }
434
435                         if ( FD_ISSET( i, &writefds ) ) {
436                                 Debug( LDAP_DEBUG_CONNS,
437                                     "daemon: signaling write waiter on %d\n", i, 0, 0 );
438
439                                 assert( FD_ISSET( 0, &slap_daemon.sd_actives) );
440
441                                 /* clear the write flag */
442                                 slapd_clr_write( i, 0 );
443                                 
444                                 if( connection_write( i ) < 0 ) { 
445                                         FD_CLR( i, &readfds );
446                                         slapd_close( i );
447                                 }
448                         }
449                 }
450
451                 for ( i = 0; i < nfds; i++ ) {
452                         if ( i == tcps ) {
453                                 continue;
454                         }
455
456                         if ( FD_ISSET( i, &readfds ) ) {
457                                 Debug( LDAP_DEBUG_CONNS,
458                                     "daemon: read activity on %d\n", i, 0, 0 );
459
460                                 assert( FD_ISSET( i, &slap_daemon.sd_actives) );
461
462                                 if( connection_read( i ) < 0) {
463                                         slapd_close( i );
464                                 }
465                         }
466                 }
467
468                 ldap_pvt_thread_yield();
469         }
470
471         if( slapd_shutdown > 0 ) {
472                 Debug( LDAP_DEBUG_TRACE,
473                         "daemon: shutdown requested and initiated.\n",
474                         0, 0, 0 );
475
476         } else if ( slapd_shutdown < 0 ) {
477                 Debug( LDAP_DEBUG_TRACE,
478                         "daemon: abnormal condition, shutdown initiated.\n",
479                         0, 0, 0 );
480         } else {
481                 Debug( LDAP_DEBUG_TRACE,
482                         "daemon: no active streams, shutdown initiated.\n",
483                         0, 0, 0 );
484         }
485
486         if( tcps >= 0 ) {
487                 close( tcps );
488         }
489
490         ldap_pvt_thread_mutex_lock( &active_threads_mutex );
491         Debug( LDAP_DEBUG_ANY,
492             "slapd shutdown: waiting for %d threads to terminate\n",
493             active_threads, 0, 0 );
494         while ( active_threads > 0 ) {
495                 ldap_pvt_thread_cond_wait(&active_threads_cond, &active_threads_mutex);
496         }
497         ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
498
499         return NULL;
500 }
501
502 int slapd_daemon( struct sockaddr_in *addr )
503 {
504         int status;
505
506         status = ldap_pvt_thread_create( &listener_tid, 0,
507                  slapd_daemon_task, addr );
508
509         if ( status != 0 ) {
510                 Debug( LDAP_DEBUG_ANY,
511                     "listener ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
512                 return -1;
513         } else {
514                 /* wait for the listener thread to complete */
515                 ldap_pvt_thread_join( listener_tid, (void *) NULL );
516         }
517
518         return 0;
519 }
520
521 void
522 slap_set_shutdown( int sig )
523 {
524         slapd_shutdown = 1;
525         ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
526
527         /* reinstall self */
528         (void) SIGNAL( sig, slap_set_shutdown );
529 }
530
531 void
532 slap_do_nothing( int sig )
533 {
534         /* reinstall self */
535         (void) SIGNAL( sig, slap_do_nothing );
536 }