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