]> git.sur5r.net Git - openldap/blob - servers/slapd/daemon.c
Import ITS#44 tcp_wrapper connections bug fix from devel.
[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 int slapd_shutdown = 0;
40 static void     set_shutdown(int sig);
41 static void     do_nothing  (int sig);
42
43 void *
44 slapd_daemon(
45     void *port
46 )
47 {
48         Operation               *o;
49         BerElement              ber;
50         unsigned long           len, tag, msgid;
51         int                     i;
52         int                     tcps, ns;
53         struct sockaddr_in      addr;
54         fd_set                  readfds;
55         fd_set                  writefds;
56         FILE                    *fp;
57         int                     on = 1;
58
59 #ifdef HAVE_SYSCONF
60         dtblsize = sysconf( _SC_OPEN_MAX );
61 #elif HAVE_GETDTABLESIZE
62         dtblsize = getdtablesize();
63 #else
64         dtblsize = FD_SETSIZE
65 #endif
66
67 #ifdef FD_SETSIZE
68         if(dtblsize > FD_SETSIZE) {
69                 dtblsize = FD_SETSIZE;
70         }
71 #endif  /* !FD_SETSIZE */
72
73         c = (Connection *) ch_calloc( 1, dtblsize * sizeof(Connection) );
74
75         for ( i = 0; i < dtblsize; i++ ) {
76                 c[i].c_dn = NULL;
77                 c[i].c_addr = NULL;
78                 c[i].c_domain = NULL;
79                 c[i].c_ops = NULL;
80                 c[i].c_sb.sb_sd = -1;
81                 c[i].c_sb.sb_options = LBER_NO_READ_AHEAD;
82                 c[i].c_sb.sb_naddr = 0;
83                 c[i].c_sb.sb_ber.ber_buf = NULL;
84                 c[i].c_sb.sb_ber.ber_ptr = NULL;
85                 c[i].c_sb.sb_ber.ber_end = NULL;
86                 c[i].c_writewaiter = 0;
87                 c[i].c_connid = 0;
88                 pthread_mutex_init( &c[i].c_dnmutex,
89                     pthread_mutexattr_default );
90                 pthread_mutex_init( &c[i].c_opsmutex,
91                     pthread_mutexattr_default );
92                 pthread_mutex_init( &c[i].c_pdumutex,
93                     pthread_mutexattr_default );
94                 pthread_cond_init( &c[i].c_wcv, pthread_condattr_default );
95         }
96
97         if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
98                 Debug( LDAP_DEBUG_ANY, "socket() failed errno %d (%s)", errno,
99                     errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
100                     "unknown", 0 );
101                 exit( 1 );
102         }
103
104         i = 1;
105         if ( setsockopt( tcps, SOL_SOCKET, SO_REUSEADDR, (char *) &i,
106             sizeof(i) ) == -1 ) {
107                 Debug( LDAP_DEBUG_ANY, "setsockopt() failed errno %d (%s)",
108                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
109                     "unknown", 0 );
110         }
111
112         (void) memset( (void *) &addr, '\0', sizeof(addr) );
113         addr.sin_family = AF_INET;
114         addr.sin_addr.s_addr = INADDR_ANY;
115         addr.sin_port = htons( (int)port );
116         if ( bind( tcps, (struct sockaddr *) &addr, sizeof(addr) ) == -1 ) {
117                 Debug( LDAP_DEBUG_ANY, "bind() failed errno %d (%s)\n",
118                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
119                     "unknown", 0 );
120                 exit( 1 );
121         }
122
123         if ( listen( tcps, 5 ) == -1 ) {
124                 Debug( LDAP_DEBUG_ANY, "listen() failed errno %d (%s)",
125                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
126                     "unknown", 0 );
127                 exit( 1 );
128         }
129
130         (void) SIGNAL( SIGPIPE, SIG_IGN );
131         (void) SIGNAL( LDAP_SIGUSR1, do_nothing );
132         (void) SIGNAL( LDAP_SIGUSR2, set_shutdown );
133         (void) SIGNAL( SIGTERM, set_shutdown );
134         (void) SIGNAL( SIGINT, set_shutdown );
135         (void) SIGNAL( SIGHUP, set_shutdown );
136
137         Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
138 #ifdef SLAPD_PIDFILE
139         if ( (fp = fopen( SLAPD_PIDFILE, "w" )) != NULL ) {
140                 fprintf( fp, "%d\n", (int) getpid() );
141                 fclose( fp );
142         }
143 #endif
144 #ifdef SLAPD_ARGSFILE
145         if ( (fp = fopen( SLAPD_ARGSFILE, "w" )) != NULL ) {
146                 for ( i = 0; i < g_argc; i++ ) {
147                         fprintf( fp, "%s ", g_argv[i] );
148                 }
149                 fprintf( fp, "\n" );
150                 fclose( fp );
151         }
152 #endif
153
154         while ( !slapd_shutdown ) {
155                 struct sockaddr_in      from;
156                 struct hostent          *hp;
157                 struct timeval          zero;
158                 struct timeval          *tvp;
159                 int                     len, pid;
160
161                 char    *client_name;
162                 char    *client_addr;
163
164                 FD_ZERO( &writefds );
165                 FD_ZERO( &readfds );
166                 FD_SET( tcps, &readfds );
167
168                 zero.tv_sec = 0;
169                 zero.tv_usec = 0;
170
171                 pthread_mutex_lock( &active_threads_mutex );
172                 Debug( LDAP_DEBUG_CONNS,
173                     "listening for connections on %d, activity on:",
174                     tcps, 0, 0 );
175
176                 pthread_mutex_lock( &new_conn_mutex );
177                 for ( i = 0; i < dtblsize; i++ ) {
178                         if ( c[i].c_sb.sb_sd != -1 ) {
179                                 FD_SET( c[i].c_sb.sb_sd, &readfds );
180
181                                 if ( c[i].c_writewaiter ) {
182                                         FD_SET( c[i].c_sb.sb_sd, &writefds );
183                                 }
184                                 Debug( LDAP_DEBUG_CONNS, " %dr%s", i,
185                                     c[i].c_writewaiter ? "w" : "", 0 );
186                         }
187                 }
188                 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
189                 pthread_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 = NULL;
195 #else
196                 tvp = active_threads ? &zero : NULL;
197 #endif
198                 pthread_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                         pthread_yield();
212                         continue;
213
214                 default:        /* something happened - deal with it */
215                         Debug( LDAP_DEBUG_CONNS, "select activity on %d descriptors\n", i, 0, 0 );
216                         ;       /* FALL */
217                 }
218                 pthread_mutex_lock( &currenttime_mutex );
219                 time( &currenttime );
220                 pthread_mutex_unlock( &currenttime_mutex );
221
222                 /* new connection */
223                 pthread_mutex_lock( &new_conn_mutex );
224                 if ( FD_ISSET( tcps, &readfds ) ) {
225                         len = sizeof(from);
226                         if ( (ns = accept( tcps, (struct sockaddr *) &from,
227                             &len )) == -1 ) {
228                                 Debug( LDAP_DEBUG_ANY,
229                                     "accept() failed errno %d (%s)", errno,
230                                     errno > -1 && errno < sys_nerr ?
231                                     sys_errlist[errno] : "unknown", 0 );
232                                 pthread_mutex_unlock( &new_conn_mutex );
233                                 continue;
234                         }
235                         if ( ioctl( ns, FIONBIO, (caddr_t) &on ) == -1 ) {
236                                 Debug( LDAP_DEBUG_ANY,
237                                     "FIONBIO ioctl on %d failed\n", ns, 0, 0 );
238                         }
239
240                         Debug( LDAP_DEBUG_CONNS, "new connection on %d\n", ns,
241                             0, 0 );
242
243                         len = sizeof(from);
244
245                         if ( getpeername( ns, (struct sockaddr *) &from, &len )
246                             == 0 ) {
247                                 char *s;
248                                 client_addr = inet_ntoa( from.sin_addr );
249
250 #if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
251                                 hp = gethostbyaddr( (char *)
252                                     &(from.sin_addr.s_addr),
253                                     sizeof(from.sin_addr.s_addr), AF_INET );
254
255                                 if(hp) {
256                                         client_name = hp->h_name;
257
258                                         /* normalize the domain */
259                                         for ( s = client_name; *s; s++ ) {
260                                                 *s = TOLOWER( *s );
261                                         }
262
263                                 } else {
264                                         client_name = NULL;
265                                 }
266 #else
267                                 client_name = NULL;
268 #endif
269
270                         } else {
271                                 client_name = NULL;;
272                                 client_addr = NULL;
273                         }
274
275 #ifdef HAVE_TCPD
276                         if(!hosts_ctl("slapd",
277                                 client_name != NULL ? client_name : STRING_UNKNOWN,
278                                 client_addr != NULL ? client_addr : STRING_UNKNOWN,
279                                 STRING_UNKNOWN))
280                         {
281                                 /* DENY ACCESS */
282                                 Statslog( LDAP_DEBUG_ANY,
283                                  "fd=%d connection from %s (%s) denied.\n",
284                                         ns,
285                                                 client_name == NULL ? "unknown" : client_name,
286                                                 client_addr == NULL ? "unknown" : client_addr,
287                                   0 );
288
289                                 close(ns);
290                                 pthread_mutex_unlock( &new_conn_mutex );
291                                 continue;
292                         }
293 #endif /* HAVE_TCPD */
294
295                         c[ns].c_sb.sb_sd = ns;
296                         pthread_mutex_lock( &ops_mutex );
297                         c[ns].c_connid = num_conns++;
298                         pthread_mutex_unlock( &ops_mutex );
299
300                         Statslog( LDAP_DEBUG_STATS,
301                             "conn=%d fd=%d connection from %s (%s) accepted.\n",
302                                 c[ns].c_connid, ns,
303                                         client_name == NULL ? "unknown" : client_name,
304                                         client_addr == NULL ? "unknown" : client_addr,
305                              0 );
306
307                         if ( c[ns].c_addr != NULL ) {
308                                 free( c[ns].c_addr );
309                         }
310                         c[ns].c_addr = ch_strdup( client_addr );
311
312                         if ( c[ns].c_domain != NULL ) {
313                                 free( c[ns].c_domain );
314                         }
315
316                         c[ns].c_domain = ch_strdup( client_name == NULL
317                                 ? "" : client_name );
318
319                         pthread_mutex_lock( &c[ns].c_dnmutex );
320                         if ( c[ns].c_dn != NULL ) {
321                                 free( c[ns].c_dn );
322                                 c[ns].c_dn = NULL;
323                         }
324                         pthread_mutex_unlock( &c[ns].c_dnmutex );
325                         c[ns].c_starttime = currenttime;
326                         c[ns].c_opsinitiated = 0;
327                         c[ns].c_opscompleted = 0;
328                 }
329                 pthread_mutex_unlock( &new_conn_mutex );
330
331                 Debug( LDAP_DEBUG_CONNS, "activity on:", 0, 0, 0 );
332                 for ( i = 0; i < dtblsize; i++ ) {
333                         int     r, w;
334
335                         r = FD_ISSET( i, &readfds );
336                         w = FD_ISSET( i, &writefds );
337                         if ( i != tcps && (r || w) ) {
338                                 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
339                                     r ? "r" : "", w ? "w" : "" );
340                         }
341                 }
342                 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
343
344                 for ( i = 0; i < dtblsize; i++ ) {
345                         if ( i == tcps || (! FD_ISSET( i, &readfds ) &&
346                             ! FD_ISSET( i, &writefds )) ) {
347                                 continue;
348                         }
349
350                         if ( FD_ISSET( i, &writefds ) ) {
351                                 Debug( LDAP_DEBUG_CONNS,
352                                     "signaling write waiter on %d\n", i, 0, 0 );
353
354                                 pthread_mutex_lock( &active_threads_mutex );
355                                 pthread_cond_signal( &c[i].c_wcv );
356                                 c[i].c_writewaiter = 0;
357                                 active_threads++;
358                                 pthread_mutex_unlock( &active_threads_mutex );
359                         }
360
361                         if ( FD_ISSET( i, &readfds ) ) {
362                                 Debug( LDAP_DEBUG_CONNS,
363                                     "read activity on %d\n", i, 0, 0 );
364
365                                 connection_activity( &c[i] );
366                         }
367                 }
368
369                 pthread_yield();
370         }
371
372         close( tcps );
373
374         pthread_mutex_lock( &active_threads_mutex );
375         Debug( LDAP_DEBUG_ANY,
376             "slapd shutting down - waiting for %d threads to terminate\n",
377             active_threads, 0, 0 );
378         while ( active_threads > 0 ) {
379                 pthread_cond_wait(&active_threads_cond, &active_threads_mutex);
380         }
381         pthread_mutex_unlock( &active_threads_mutex );
382
383         /* let backends do whatever cleanup they need to do */
384         Debug( LDAP_DEBUG_TRACE,
385             "slapd shutting down - waiting for backends to close down\n", 0, 0,
386             0 );
387         be_close();
388         Debug( LDAP_DEBUG_ANY, "slapd stopping\n", 0, 0, 0 );
389         return NULL;
390 }
391
392 static void
393 set_shutdown( int sig )
394 {
395         Debug( LDAP_DEBUG_ANY, "slapd got shutdown signal %d\n", sig, 0, 0 );
396         slapd_shutdown = 1;
397         pthread_kill( listener_tid, LDAP_SIGUSR1 );
398         (void) SIGNAL( LDAP_SIGUSR2, set_shutdown );
399         (void) SIGNAL( SIGTERM, set_shutdown );
400         (void) SIGNAL( SIGINT, set_shutdown );
401         (void) SIGNAL( SIGHUP, set_shutdown );
402 }
403
404 static void
405 do_nothing( int sig )
406 {
407         Debug( LDAP_DEBUG_TRACE, "slapd got do_nothing signal %d\n", sig, 0, 0 );
408         (void) SIGNAL( LDAP_SIGUSR1, do_nothing );
409 }