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