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