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