]> git.sur5r.net Git - openldap/blob - servers/slapd/daemon.c
Protoized, moved extern definitions to .h files, fixed related bugs.
[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 #ifdef HAVE_LINUX_THREADS
131         /*
132          * LinuxThreads are implemented using SIGUSR1/USR2,
133          * so we'll use SIGSTKFLT and SIGUNUSED
134          */
135         (void) SIGNAL( SIGSTKFLT, do_nothing );
136         (void) SIGNAL( SIGUNUSED, set_shutdown );
137 #else  /* !linux */
138         (void) SIGNAL( SIGUSR1, do_nothing );
139         (void) SIGNAL( SIGUSR2, set_shutdown );
140 #endif /* !linux */
141         (void) SIGNAL( SIGTERM, set_shutdown );
142         (void) SIGNAL( SIGINT, set_shutdown );
143         (void) SIGNAL( SIGHUP, set_shutdown );
144
145         Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
146 #ifdef SLAPD_PIDFILE
147         if ( (fp = fopen( SLAPD_PIDFILE, "w" )) != NULL ) {
148                 fprintf( fp, "%d\n", (int) getpid() );
149                 fclose( fp );
150         }
151 #endif
152 #ifdef SLAPD_ARGSFILE
153         if ( (fp = fopen( SLAPD_ARGSFILE, "w" )) != NULL ) {
154                 for ( i = 0; i < g_argc; i++ ) {
155                         fprintf( fp, "%s ", g_argv[i] );
156                 }
157                 fprintf( fp, "\n" );
158                 fclose( fp );
159         }
160 #endif
161
162         while ( !slapd_shutdown ) {
163                 struct sockaddr_in      from;
164                 struct hostent          *hp;
165                 struct timeval          zero;
166                 struct timeval          *tvp;
167                 int                     len, pid;
168
169                 char    *client_name;
170                 char    *client_addr;
171
172                 FD_ZERO( &writefds );
173                 FD_ZERO( &readfds );
174                 FD_SET( tcps, &readfds );
175
176                 zero.tv_sec = 0;
177                 zero.tv_usec = 0;
178
179                 pthread_mutex_lock( &active_threads_mutex );
180                 Debug( LDAP_DEBUG_CONNS,
181                     "listening for connections on %d, activity on:",
182                     tcps, 0, 0 );
183
184                 pthread_mutex_lock( &new_conn_mutex );
185                 for ( i = 0; i < dtblsize; i++ ) {
186                         if ( c[i].c_sb.sb_sd != -1 ) {
187                                 FD_SET( c[i].c_sb.sb_sd, &readfds );
188
189                                 if ( c[i].c_writewaiter ) {
190                                         FD_SET( c[i].c_sb.sb_sd, &writefds );
191                                 }
192                                 Debug( LDAP_DEBUG_CONNS, " %dr%s", i,
193                                     c[i].c_writewaiter ? "w" : "", 0 );
194                         }
195                 }
196                 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
197                 pthread_mutex_unlock( &new_conn_mutex );
198
199                 Debug( LDAP_DEBUG_CONNS, "before select active_threads %d\n",
200                     active_threads, 0, 0 );
201 #ifdef PREEMPTIVE_THREADS
202                 tvp = NULL;
203 #else
204                 tvp = active_threads ? &zero : NULL;
205 #endif
206                 pthread_mutex_unlock( &active_threads_mutex );
207
208                 switch ( i = select( dtblsize, &readfds, &writefds, 0, tvp ) ) {
209                 case -1:        /* failure - try again */
210                         Debug( LDAP_DEBUG_CONNS,
211                             "select failed errno %d (%s)\n",
212                             errno, errno > -1 && errno < sys_nerr ?
213                             sys_errlist[errno] : "unknown", 0 );
214                         continue;
215
216                 case 0:         /* timeout - let threads run */
217                         Debug( LDAP_DEBUG_CONNS, "select timeout - yielding\n",
218                             0, 0, 0 );
219                         pthread_yield();
220                         continue;
221
222                 default:        /* something happened - deal with it */
223                         Debug( LDAP_DEBUG_CONNS, "select activity on %d descriptors\n", i, 0, 0 );
224                         ;       /* FALL */
225                 }
226                 pthread_mutex_lock( &currenttime_mutex );
227                 time( &currenttime );
228                 pthread_mutex_unlock( &currenttime_mutex );
229
230                 /* new connection */
231                 pthread_mutex_lock( &new_conn_mutex );
232                 if ( FD_ISSET( tcps, &readfds ) ) {
233                         len = sizeof(from);
234                         if ( (ns = accept( tcps, (struct sockaddr *) &from,
235                             &len )) == -1 ) {
236                                 Debug( LDAP_DEBUG_ANY,
237                                     "accept() failed errno %d (%s)", errno,
238                                     errno > -1 && errno < sys_nerr ?
239                                     sys_errlist[errno] : "unknown", 0 );
240                                 pthread_mutex_unlock( &new_conn_mutex );
241                                 continue;
242                         }
243                         if ( ioctl( ns, FIONBIO, (caddr_t) &on ) == -1 ) {
244                                 Debug( LDAP_DEBUG_ANY,
245                                     "FIONBIO ioctl on %d failed\n", ns, 0, 0 );
246                         }
247
248                         c[ns].c_sb.sb_sd = ns;
249                         Debug( LDAP_DEBUG_CONNS, "new connection on %d\n", ns,
250                             0, 0 );
251
252                         pthread_mutex_lock( &ops_mutex );
253                         c[ns].c_connid = num_conns++;
254                         pthread_mutex_unlock( &ops_mutex );
255
256                         len = sizeof(from);
257
258                         if ( getpeername( ns, (struct sockaddr *) &from, &len )
259                             == 0 ) {
260                                 char *s;
261                                 client_addr = inet_ntoa( from.sin_addr );
262
263 #if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
264                                 hp = gethostbyaddr( (char *)
265                                     &(from.sin_addr.s_addr),
266                                     sizeof(from.sin_addr.s_addr), AF_INET );
267
268                                 if(hp) {
269                                         client_name = hp->h_name;
270
271                                         /* normalize the domain */
272                                         for ( s = client_name; *s; s++ ) {
273                                                 *s = TOLOWER( *s );
274                                         }
275
276                                 } else {
277                                         client_name = NULL;
278                                 }
279 #else
280                                 client_name = NULL;
281 #endif
282
283                         } else {
284                                 client_name = NULL;;
285                                 client_addr = NULL;
286                         }
287
288 #ifdef HAVE_TCPD
289                         if(!hosts_ctl("slapd", client_name, client_addr,
290                                 STRING_UNKNOWN))
291                         {
292                                 /* DENY ACCESS */
293                                 Statslog( LDAP_DEBUG_STATS,
294                                  "conn=%d fd=%d connection from %s (%s) denied.\n",
295                                         c[ns].c_connid, ns,
296                                                 client_name == NULL ? "unknown" : client_name,
297                                                 client_addr == NULL ? "unknown" : client_addr,
298                                   0 );
299
300                                 close(ns);
301                                 pthread_mutex_unlock( &new_conn_mutex );
302                                 continue;
303                         }
304 #endif /* HAVE_TCPD */
305
306                         Statslog( LDAP_DEBUG_STATS,
307                             "conn=%d fd=%d connection from %s (%s) accepted.\n",
308                                 c[ns].c_connid, ns,
309                                         client_name == NULL ? "unknown" : client_name,
310                                         client_addr == NULL ? "unknown" : client_addr,
311                              0 );
312
313                         if ( c[ns].c_addr != NULL ) {
314                                 free( c[ns].c_addr );
315                         }
316                         c[ns].c_addr = strdup( client_addr );
317
318                         if ( c[ns].c_domain != NULL ) {
319                                 free( c[ns].c_domain );
320                         }
321
322                         c[ns].c_domain = strdup( client_name == NULL
323                                 ? "" : client_name );
324
325                         pthread_mutex_lock( &c[ns].c_dnmutex );
326                         if ( c[ns].c_dn != NULL ) {
327                                 free( c[ns].c_dn );
328                                 c[ns].c_dn = NULL;
329                         }
330                         pthread_mutex_unlock( &c[ns].c_dnmutex );
331                         c[ns].c_starttime = currenttime;
332                         c[ns].c_opsinitiated = 0;
333                         c[ns].c_opscompleted = 0;
334                 }
335                 pthread_mutex_unlock( &new_conn_mutex );
336
337                 Debug( LDAP_DEBUG_CONNS, "activity on:", 0, 0, 0 );
338                 for ( i = 0; i < dtblsize; i++ ) {
339                         int     r, w;
340
341                         r = FD_ISSET( i, &readfds );
342                         w = FD_ISSET( i, &writefds );
343                         if ( i != tcps && (r || w) ) {
344                                 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
345                                     r ? "r" : "", w ? "w" : "" );
346                         }
347                 }
348                 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
349
350                 for ( i = 0; i < dtblsize; i++ ) {
351                         if ( i == tcps || (! FD_ISSET( i, &readfds ) &&
352                             ! FD_ISSET( i, &writefds )) ) {
353                                 continue;
354                         }
355
356                         if ( FD_ISSET( i, &writefds ) ) {
357                                 Debug( LDAP_DEBUG_CONNS,
358                                     "signaling write waiter on %d\n", i, 0, 0 );
359
360                                 pthread_mutex_lock( &active_threads_mutex );
361                                 pthread_cond_signal( &c[i].c_wcv );
362                                 c[i].c_writewaiter = 0;
363                                 active_threads++;
364                                 pthread_mutex_unlock( &active_threads_mutex );
365                         }
366
367                         if ( FD_ISSET( i, &readfds ) ) {
368                                 Debug( LDAP_DEBUG_CONNS,
369                                     "read activity on %d\n", i, 0, 0 );
370
371                                 connection_activity( &c[i] );
372                         }
373                 }
374
375                 pthread_yield();
376         }
377
378         close( tcps );
379         pthread_mutex_lock( &active_threads_mutex );
380         Debug( LDAP_DEBUG_ANY,
381             "slapd shutting down - waiting for %d threads to terminate\n",
382             active_threads, 0, 0 );
383         while ( active_threads > 0 ) {
384                 pthread_mutex_unlock( &active_threads_mutex );
385                 pthread_yield();
386                 pthread_mutex_lock( &active_threads_mutex );
387         }
388         pthread_mutex_unlock( &active_threads_mutex );
389
390         /* let backends do whatever cleanup they need to do */
391         Debug( LDAP_DEBUG_TRACE,
392             "slapd shutting down - waiting for backends to close down\n", 0, 0,
393             0 );
394         be_close();
395         Debug( LDAP_DEBUG_ANY, "slapd stopping\n", 0, 0, 0 );
396         return NULL;
397 }
398
399 static void
400 set_shutdown( int sig )
401 {
402         Debug( LDAP_DEBUG_ANY, "slapd got shutdown signal %d\n", sig, 0, 0 );
403         slapd_shutdown = 1;
404 #ifdef HAVE_LINUX_THREADS
405         /*
406          * LinuxThreads are implemented using SIGUSR1/USR2,
407          * so we'll use SIGSTKFLT and SIGUNUSED
408          */
409         pthread_kill( listener_tid, SIGSTKFLT );
410         (void) SIGNAL( SIGUNUSED, set_shutdown );
411 #else /* !linux */
412         pthread_kill( listener_tid, SIGUSR1 );
413         (void) SIGNAL( SIGUSR2, set_shutdown );
414 #endif /* !linux */
415         (void) SIGNAL( SIGTERM, set_shutdown );
416         (void) SIGNAL( SIGINT, set_shutdown );
417         (void) SIGNAL( SIGHUP, set_shutdown );
418 }
419
420 static void
421 do_nothing( int sig )
422 {
423         Debug( LDAP_DEBUG_TRACE, "slapd got do_nothing signal %d\n", sig, 0, 0 );
424 #ifdef HAVE_LINUX_THREADS
425         /*
426          * LinuxThreads are implemented using SIGUSR1/USR2,
427          * so we'll use SIGSTKFLT and SIGUNUSED
428          */
429         (void) SIGNAL( SIGSTKFLT, do_nothing );
430 #else /* !linux */
431         (void) SIGNAL( SIGUSR1, do_nothing );
432 #endif /* !linux */
433 }