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