]> git.sur5r.net Git - openldap/blob - servers/slapd/daemon.c
include portable.h
[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 #include <ac/string.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <errno.h>
15 #include <ac/time.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
18 #include <netdb.h>
19 #include <signal.h>
20 #ifdef _AIX
21 #include <sys/select.h>
22 #endif
23 #include "slap.h"
24 #include "ldapconfig.h"
25 #ifdef NEED_FILIO
26 #include <sys/filio.h>
27 #else /* NEED_FILIO */
28 #include <sys/ioctl.h>
29 #endif /* NEED_FILIO */
30 #ifdef USE_SYSCONF
31 #include <unistd.h>
32 #endif /* USE_SYSCONF */
33
34 #ifdef TCP_WRAPPERS
35 #include <tcpd.h>
36
37 int allow_severity = LOG_INFO;
38 int deny_severity = LOG_NOTICE;
39 #endif /* TCP_WRAPPERS */
40
41 extern Operation        *op_add();
42
43 #ifdef DECL_SYS_ERRLIST
44 extern int              sys_nerr;
45 extern char             *sys_errlist[];
46 #endif
47
48 extern time_t           currenttime;
49 extern pthread_mutex_t  currenttime_mutex;
50 extern int              active_threads;
51 extern pthread_mutex_t  active_threads_mutex;
52 extern pthread_mutex_t  new_conn_mutex;
53 extern int              slapd_shutdown;
54 extern pthread_t        listener_tid;
55 extern int              num_conns;
56 extern pthread_mutex_t  ops_mutex;
57 extern int              g_argc;
58 extern char             **g_argv;
59
60 int             dtblsize;
61 Connection      *c;
62
63 static void     set_shutdown();
64 static void     do_nothing();
65
66 void
67 slapd_daemon(
68     int port
69 )
70 {
71         Operation               *o;
72         BerElement              ber;
73         unsigned long           len, tag, msgid;
74         int                     i;
75         int                     tcps, ns;
76         struct sockaddr_in      addr;
77         fd_set                  readfds;
78         fd_set                  writefds;
79         FILE                    *fp;
80         int                     on = 1;
81
82 #ifdef USE_SYSCONF
83         dtblsize = sysconf( _SC_OPEN_MAX );
84 #else /* USE_SYSCONF */
85         dtblsize = getdtablesize();
86 #endif /* USE_SYSCONF */
87         /*
88          * Add greg@greg.rim.or.jp
89          */
90 #ifdef FD_SETSIZE
91         if(dtblsize > FD_SETSIZE) {
92                 dtblsize = FD_SETSIZE;
93         }
94 #endif  /* !FD_SETSIZE */
95
96         c = (Connection *) ch_calloc( 1, dtblsize * sizeof(Connection) );
97
98         for ( i = 0; i < dtblsize; i++ ) {
99                 c[i].c_dn = NULL;
100                 c[i].c_addr = NULL;
101                 c[i].c_domain = NULL;
102                 c[i].c_ops = NULL;
103                 c[i].c_sb.sb_sd = -1;
104                 c[i].c_sb.sb_options = LBER_NO_READ_AHEAD;
105                 c[i].c_sb.sb_naddr = 0;
106                 c[i].c_sb.sb_ber.ber_buf = NULL;
107                 c[i].c_sb.sb_ber.ber_ptr = NULL;
108                 c[i].c_sb.sb_ber.ber_end = NULL;
109                 c[i].c_writewaiter = 0;
110                 c[i].c_connid = 0;
111                 pthread_mutex_init( &c[i].c_dnmutex,
112                     pthread_mutexattr_default );
113                 pthread_mutex_init( &c[i].c_opsmutex,
114                     pthread_mutexattr_default );
115                 pthread_mutex_init( &c[i].c_pdumutex,
116                     pthread_mutexattr_default );
117                 pthread_cond_init( &c[i].c_wcv, pthread_condattr_default );
118         }
119
120         if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
121                 Debug( LDAP_DEBUG_ANY, "socket() failed errno %d (%s)", errno,
122                     errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
123                     "unknown", 0 );
124                 exit( 1 );
125         }
126
127         i = 1;
128         if ( setsockopt( tcps, SOL_SOCKET, SO_REUSEADDR, (char *) &i,
129             sizeof(i) ) == -1 ) {
130                 Debug( LDAP_DEBUG_ANY, "setsockopt() failed errno %d (%s)",
131                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
132                     "unknown", 0 );
133         }
134
135         (void) memset( (void *) &addr, '\0', sizeof(addr) );
136         addr.sin_family = AF_INET;
137         addr.sin_addr.s_addr = INADDR_ANY;
138         addr.sin_port = htons( port );
139         if ( bind( tcps, (struct sockaddr *) &addr, sizeof(addr) ) == -1 ) {
140                 Debug( LDAP_DEBUG_ANY, "bind() failed errno %d (%s)\n",
141                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
142                     "unknown", 0 );
143                 exit( 1 );
144         }
145
146         if ( listen( tcps, 5 ) == -1 ) {
147                 Debug( LDAP_DEBUG_ANY, "listen() failed errno %d (%s)",
148                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
149                     "unknown", 0 );
150                 exit( 1 );
151         }
152
153         (void) SIGNAL( SIGPIPE, SIG_IGN );
154 #ifdef linux
155         /*
156          * LinuxThreads are implemented using SIGUSR1/USR2,
157          * so we'll use SIGSTKFLT and SIGUNUSED
158          */
159         (void) SIGNAL( SIGSTKFLT, (void *) do_nothing );
160         (void) SIGNAL( SIGUNUSED, (void *) set_shutdown );
161 #else /* !linux */
162         (void) SIGNAL( SIGUSR1, (void *) do_nothing );
163         (void) SIGNAL( SIGUSR2, (void *) set_shutdown );
164 #endif /* !linux */
165         (void) SIGNAL( SIGTERM, (void *) set_shutdown );
166         (void) SIGNAL( SIGINT, (void *) set_shutdown );
167         (void) SIGNAL( SIGHUP, (void *) set_shutdown );
168
169         Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
170 #ifdef SLAPD_PIDFILE
171         if ( (fp = fopen( SLAPD_PIDFILE, "w" )) != NULL ) {
172                 fprintf( fp, "%d\n", getpid() );
173                 fclose( fp );
174         }
175 #endif
176 #ifdef SLAPD_ARGSFILE
177         if ( (fp = fopen( SLAPD_ARGSFILE, "w" )) != NULL ) {
178                 for ( i = 0; i < g_argc; i++ ) {
179                         fprintf( fp, "%s ", g_argv[i] );
180                 }
181                 fprintf( fp, "\n" );
182                 fclose( fp );
183         }
184 #endif
185
186         while ( !slapd_shutdown ) {
187                 struct sockaddr_in      from;
188                 struct hostent          *hp;
189                 struct timeval          zero;
190                 struct timeval          *tvp;
191                 int                     len, pid;
192
193                 char    *client_name;
194                 char    *client_addr;
195
196                 FD_ZERO( &writefds );
197                 FD_ZERO( &readfds );
198                 FD_SET( tcps, &readfds );
199
200                 zero.tv_sec = 0;
201                 zero.tv_usec = 0;
202
203                 pthread_mutex_lock( &active_threads_mutex );
204                 Debug( LDAP_DEBUG_CONNS,
205                     "listening for connections on %d, activity on:",
206                     tcps, 0, 0 );
207
208                 pthread_mutex_lock( &new_conn_mutex );
209                 for ( i = 0; i < dtblsize; i++ ) {
210                         if ( c[i].c_sb.sb_sd != -1 ) {
211                                 FD_SET( c[i].c_sb.sb_sd, &readfds );
212
213                                 if ( c[i].c_writewaiter ) {
214                                         FD_SET( c[i].c_sb.sb_sd, &writefds );
215                                 }
216                                 Debug( LDAP_DEBUG_CONNS, " %dr%s", i,
217                                     c[i].c_writewaiter ? "w" : "", 0 );
218                         }
219                 }
220                 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
221                 pthread_mutex_unlock( &new_conn_mutex );
222
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 }