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