]> git.sur5r.net Git - openldap/blob - servers/slapd/daemon.c
Minor change to signal handler. Changed _shutdown flag to
[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 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                         c[ns].c_sb.sb_sd = ns;
241                         Debug( LDAP_DEBUG_CONNS, "new connection on %d\n", ns,
242                             0, 0 );
243
244                         pthread_mutex_lock( &ops_mutex );
245                         c[ns].c_connid = num_conns++;
246                         pthread_mutex_unlock( &ops_mutex );
247
248                         len = sizeof(from);
249
250                         if ( getpeername( ns, (struct sockaddr *) &from, &len )
251                             == 0 ) {
252                                 char *s;
253                                 client_addr = inet_ntoa( from.sin_addr );
254
255 #if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
256                                 hp = gethostbyaddr( (char *)
257                                     &(from.sin_addr.s_addr),
258                                     sizeof(from.sin_addr.s_addr), AF_INET );
259
260                                 if(hp) {
261                                         client_name = hp->h_name;
262
263                                         /* normalize the domain */
264                                         for ( s = client_name; *s; s++ ) {
265                                                 *s = TOLOWER( *s );
266                                         }
267
268                                 } else {
269                                         client_name = NULL;
270                                 }
271 #else
272                                 client_name = NULL;
273 #endif
274
275                         } else {
276                                 client_name = NULL;;
277                                 client_addr = NULL;
278                         }
279
280 #ifdef HAVE_TCPD
281                         if(!hosts_ctl("slapd", client_name, client_addr,
282                                 STRING_UNKNOWN))
283                         {
284                                 /* DENY ACCESS */
285                                 Statslog( LDAP_DEBUG_STATS,
286                                  "conn=%d fd=%d connection from %s (%s) denied.\n",
287                                         c[ns].c_connid, ns,
288                                                 client_name == NULL ? "unknown" : client_name,
289                                                 client_addr == NULL ? "unknown" : client_addr,
290                                   0 );
291
292                                 close(ns);
293                                 pthread_mutex_unlock( &new_conn_mutex );
294                                 continue;
295                         }
296 #endif /* HAVE_TCPD */
297
298                         Statslog( LDAP_DEBUG_STATS,
299                             "conn=%d fd=%d connection from %s (%s) accepted.\n",
300                                 c[ns].c_connid, ns,
301                                         client_name == NULL ? "unknown" : client_name,
302                                         client_addr == NULL ? "unknown" : client_addr,
303                              0 );
304
305                         if ( c[ns].c_addr != NULL ) {
306                                 free( c[ns].c_addr );
307                         }
308                         c[ns].c_addr = ch_strdup( client_addr );
309
310                         if ( c[ns].c_domain != NULL ) {
311                                 free( c[ns].c_domain );
312                         }
313
314                         c[ns].c_domain = ch_strdup( client_name == NULL
315                                 ? "" : client_name );
316
317                         pthread_mutex_lock( &c[ns].c_dnmutex );
318                         if ( c[ns].c_dn != NULL ) {
319                                 free( c[ns].c_dn );
320                                 c[ns].c_dn = NULL;
321                         }
322                         pthread_mutex_unlock( &c[ns].c_dnmutex );
323                         c[ns].c_starttime = currenttime;
324                         c[ns].c_opsinitiated = 0;
325                         c[ns].c_opscompleted = 0;
326                 }
327                 pthread_mutex_unlock( &new_conn_mutex );
328
329                 Debug( LDAP_DEBUG_CONNS, "activity on:", 0, 0, 0 );
330                 for ( i = 0; i < dtblsize; i++ ) {
331                         int     r, w;
332
333                         r = FD_ISSET( i, &readfds );
334                         w = FD_ISSET( i, &writefds );
335                         if ( i != tcps && (r || w) ) {
336                                 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
337                                     r ? "r" : "", w ? "w" : "" );
338                         }
339                 }
340                 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
341
342                 for ( i = 0; i < dtblsize; i++ ) {
343                         if ( i == tcps || (! FD_ISSET( i, &readfds ) &&
344                             ! FD_ISSET( i, &writefds )) ) {
345                                 continue;
346                         }
347
348                         if ( FD_ISSET( i, &writefds ) ) {
349                                 Debug( LDAP_DEBUG_CONNS,
350                                     "signaling write waiter on %d\n", i, 0, 0 );
351
352                                 pthread_mutex_lock( &active_threads_mutex );
353                                 pthread_cond_signal( &c[i].c_wcv );
354                                 c[i].c_writewaiter = 0;
355                                 active_threads++;
356                                 pthread_mutex_unlock( &active_threads_mutex );
357                         }
358
359                         if ( FD_ISSET( i, &readfds ) ) {
360                                 Debug( LDAP_DEBUG_CONNS,
361                                     "read activity on %d\n", i, 0, 0 );
362
363                                 connection_activity( &c[i] );
364                         }
365                 }
366
367                 pthread_yield();
368         }
369
370         close( tcps );
371         pthread_mutex_lock( &active_threads_mutex );
372         Debug( LDAP_DEBUG_ANY,
373             "slapd shutting down - waiting for %d threads to terminate\n",
374             active_threads, 0, 0 );
375         while ( active_threads > 0 ) {
376                 pthread_mutex_unlock( &active_threads_mutex );
377                 pthread_yield();
378                 pthread_mutex_lock( &active_threads_mutex );
379         }
380         pthread_mutex_unlock( &active_threads_mutex );
381
382         /* let backends do whatever cleanup they need to do */
383         Debug( LDAP_DEBUG_TRACE,
384             "slapd shutting down - waiting for backends to close down\n", 0, 0,
385             0 );
386         be_close();
387         Debug( LDAP_DEBUG_ANY, "slapd stopping\n", 0, 0, 0 );
388         return NULL;
389 }
390
391 static void
392 set_shutdown( int sig )
393 {
394         Debug( LDAP_DEBUG_ANY, "slapd got shutdown signal %d\n", sig, 0, 0 );
395         slapd_shutdown = 1;
396         pthread_kill( listener_tid, LDAP_SIGUSR1 );
397         (void) SIGNAL( LDAP_SIGUSR2, set_shutdown );
398         (void) SIGNAL( SIGTERM, set_shutdown );
399         (void) SIGNAL( SIGINT, set_shutdown );
400         (void) SIGNAL( SIGHUP, set_shutdown );
401 }
402
403 static void
404 do_nothing( int sig )
405 {
406         Debug( LDAP_DEBUG_TRACE, "slapd got do_nothing signal %d\n", sig, 0, 0 );
407         (void) SIGNAL( LDAP_SIGUSR1, do_nothing );
408 }