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