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