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