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