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