]> git.sur5r.net Git - openldap/blob - servers/slapd/daemon.c
Removed o_suffix and o_suffixalias as they were 1) leaked and 2) unused
[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 /* 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_cdn = NULL;
91                 c[i].c_addr = NULL;
92                 c[i].c_domain = NULL;
93                 c[i].c_ops = NULL;
94                 c[i].c_sb.sb_sd = -1;
95                 c[i].c_sb.sb_options = LBER_NO_READ_AHEAD;
96                 c[i].c_sb.sb_naddr = 0;
97                 c[i].c_sb.sb_ber.ber_buf = NULL;
98                 c[i].c_sb.sb_ber.ber_ptr = NULL;
99                 c[i].c_sb.sb_ber.ber_end = NULL;
100                 c[i].c_writewaiter = 0;
101                 c[i].c_connid = 0;
102                 pthread_mutex_init( &c[i].c_dnmutex,
103                     pthread_mutexattr_default );
104                 pthread_mutex_init( &c[i].c_opsmutex,
105                     pthread_mutexattr_default );
106                 pthread_mutex_init( &c[i].c_pdumutex,
107                     pthread_mutexattr_default );
108                 pthread_cond_init( &c[i].c_wcv, pthread_condattr_default );
109         }
110
111         if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
112                 Debug( LDAP_DEBUG_ANY, "socket() failed errno %d (%s)", errno,
113                     errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
114                     "unknown", 0 );
115                 exit( 1 );
116         }
117
118         i = 1;
119         if ( setsockopt( tcps, SOL_SOCKET, SO_REUSEADDR, (char *) &i,
120             sizeof(i) ) == -1 ) {
121                 Debug( LDAP_DEBUG_ANY, "setsockopt() failed errno %d (%s)",
122                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
123                     "unknown", 0 );
124         }
125
126         (void) memset( (void *) &addr, '\0', sizeof(addr) );
127         addr.sin_family = AF_INET;
128         addr.sin_addr.s_addr = INADDR_ANY;
129         addr.sin_port = htons( (int)port );
130         if ( bind( tcps, (struct sockaddr *) &addr, sizeof(addr) ) == -1 ) {
131                 Debug( LDAP_DEBUG_ANY, "bind() failed errno %d (%s)\n",
132                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
133                     "unknown", 0 );
134                 exit( 1 );
135         }
136
137         if ( listen( tcps, 5 ) == -1 ) {
138                 Debug( LDAP_DEBUG_ANY, "listen() failed errno %d (%s)",
139                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
140                     "unknown", 0 );
141                 exit( 1 );
142         }
143
144         (void) SIGNAL( SIGPIPE, SIG_IGN );
145         (void) SIGNAL( LDAP_SIGUSR1, do_nothing );
146         (void) SIGNAL( LDAP_SIGUSR2, set_shutdown );
147         (void) SIGNAL( SIGTERM, set_shutdown );
148         (void) SIGNAL( SIGINT, set_shutdown );
149         (void) SIGNAL( SIGHUP, set_shutdown );
150
151         Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
152
153 #if defined( SLAPD_PIDEXT ) || defined( SLAPD_ARGSEXT )
154     if ( !serverName ) serverName = DEFAULT_SERVERNAME;
155
156 #ifdef SLAPD_PIDEXT
157     sprintf( pidFile, "%s%s%s%s", DEFAULT_RUNDIR, DEFAULT_DIRSEP,
158                 serverName, SLAPD_PIDEXT );
159         if ( (fp = fopen( pidFile, "w" )) != NULL ) {
160                 fprintf( fp, "%d\n", (int) getpid() );
161                 fclose( fp );
162         }
163 #endif
164 #ifdef SLAPD_ARGSEXT
165     sprintf( argsFile, "%s%s%s%s", DEFAULT_RUNDIR, DEFAULT_DIRSEP,
166         serverName, SLAPD_ARGSEXT );
167         if ( (fp = fopen( argsFile, "w" )) != NULL ) {
168                 for ( i = 0; i < g_argc; i++ ) {
169                         fprintf( fp, "%s ", g_argv[i] );
170                 }
171                 fprintf( fp, "\n" );
172                 fclose( fp );
173         }
174 #endif
175 #endif
176
177
178         while ( !slapd_shutdown ) {
179                 struct sockaddr_in      from;
180                 struct hostent          *hp;
181                 struct timeval          zero;
182                 struct timeval          *tvp;
183                 int                     len, pid;
184
185                 char    *client_name;
186                 char    *client_addr;
187
188                 FD_ZERO( &writefds );
189                 FD_ZERO( &readfds );
190                 FD_SET( tcps, &readfds );
191
192                 zero.tv_sec = 0;
193                 zero.tv_usec = 0;
194
195                 pthread_mutex_lock( &active_threads_mutex );
196                 Debug( LDAP_DEBUG_CONNS,
197                     "listening for connections on %d, activity on:",
198                     tcps, 0, 0 );
199
200                 pthread_mutex_lock( &new_conn_mutex );
201                 for ( i = 0; i < dtblsize; i++ ) {
202                         if ( c[i].c_sb.sb_sd != -1 ) {
203                                 FD_SET( c[i].c_sb.sb_sd, &readfds );
204
205                                 if ( c[i].c_writewaiter ) {
206                                         FD_SET( c[i].c_sb.sb_sd, &writefds );
207                                 }
208                                 Debug( LDAP_DEBUG_CONNS, " %dr%s", i,
209                                     c[i].c_writewaiter ? "w" : "", 0 );
210                         }
211                 }
212                 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
213                 pthread_mutex_unlock( &new_conn_mutex );
214
215                 Debug( LDAP_DEBUG_CONNS, "before select active_threads %d\n",
216                     active_threads, 0, 0 );
217 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
218                 tvp = NULL;
219 #else
220                 tvp = active_threads ? &zero : NULL;
221 #endif
222                 pthread_mutex_unlock( &active_threads_mutex );
223
224                 switch ( i = select( dtblsize, &readfds, &writefds, 0, tvp ) ) {
225                 case -1:        /* failure - try again */
226                         Debug( LDAP_DEBUG_CONNS,
227                             "select failed errno %d (%s)\n",
228                             errno, errno > -1 && errno < sys_nerr ?
229                             sys_errlist[errno] : "unknown", 0 );
230                         continue;
231
232                 case 0:         /* timeout - let threads run */
233                         Debug( LDAP_DEBUG_CONNS, "select timeout - yielding\n",
234                             0, 0, 0 );
235                         pthread_yield();
236                         continue;
237
238                 default:        /* something happened - deal with it */
239                         Debug( LDAP_DEBUG_CONNS, "select activity on %d descriptors\n", i, 0, 0 );
240                         ;       /* FALL */
241                 }
242                 pthread_mutex_lock( &currenttime_mutex );
243                 time( &currenttime );
244                 pthread_mutex_unlock( &currenttime_mutex );
245
246                 /* new connection */
247                 pthread_mutex_lock( &new_conn_mutex );
248                 if ( FD_ISSET( tcps, &readfds ) ) {
249                         len = sizeof(from);
250                         if ( (ns = accept( tcps, (struct sockaddr *) &from,
251                             &len )) == -1 ) {
252                                 Debug( LDAP_DEBUG_ANY,
253                                     "accept() failed errno %d (%s)", errno,
254                                     errno > -1 && errno < sys_nerr ?
255                                     sys_errlist[errno] : "unknown", 0 );
256                                 pthread_mutex_unlock( &new_conn_mutex );
257                                 continue;
258                         }
259                         if ( ioctl( ns, FIONBIO, (caddr_t) &on ) == -1 ) {
260                                 Debug( LDAP_DEBUG_ANY,
261                                     "FIONBIO ioctl on %d failed\n", ns, 0, 0 );
262                         }
263
264                         Debug( LDAP_DEBUG_CONNS, "new connection on %d\n", ns,
265                             0, 0 );
266
267                         len = sizeof(from);
268
269                         if ( getpeername( ns, (struct sockaddr *) &from, &len )
270                             == 0 ) {
271                                 char *s;
272                                 client_addr = inet_ntoa( from.sin_addr );
273
274 #if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
275                                 hp = gethostbyaddr( (char *)
276                                     &(from.sin_addr.s_addr),
277                                     sizeof(from.sin_addr.s_addr), AF_INET );
278
279                                 if(hp) {
280                                         client_name = hp->h_name;
281
282                                         /* normalize the domain */
283                                         for ( s = client_name; *s; s++ ) {
284                                                 *s = TOLOWER( *s );
285                                         }
286
287                                 } else {
288                                         client_name = NULL;
289                                 }
290 #else
291                                 client_name = NULL;
292 #endif
293
294                         } else {
295                                 client_name = NULL;;
296                                 client_addr = NULL;
297                         }
298
299 #ifdef HAVE_TCPD
300                         if(!hosts_ctl("slapd",
301                                 client_name != NULL ? client_name : STRING_UNKNOWN,
302                                 client_addr != NULL ? client_addr : STRING_UNKNOWN,
303                                 STRING_UNKNOWN))
304                         {
305                                 /* DENY ACCESS */
306                                 Statslog( LDAP_DEBUG_ANY,
307                                  "fd=%d connection from %s (%s) denied.\n",
308                                         ns,
309                                                 client_name == NULL ? "unknown" : client_name,
310                                                 client_addr == NULL ? "unknown" : client_addr,
311                                   0 );
312
313                                 close(ns);
314                                 pthread_mutex_unlock( &new_conn_mutex );
315                                 continue;
316                         }
317 #endif /* HAVE_TCPD */
318
319                         c[ns].c_sb.sb_sd = ns;
320                         pthread_mutex_lock( &ops_mutex );
321                         c[ns].c_connid = num_conns++;
322                         pthread_mutex_unlock( &ops_mutex );
323
324                         Statslog( LDAP_DEBUG_STATS,
325                             "conn=%d fd=%d connection from %s (%s) accepted.\n",
326                                 c[ns].c_connid, ns,
327                                         client_name == NULL ? "unknown" : client_name,
328                                         client_addr == NULL ? "unknown" : client_addr,
329                              0 );
330
331                         if ( c[ns].c_addr != NULL ) {
332                                 free( c[ns].c_addr );
333                         }
334                         c[ns].c_addr = ch_strdup( client_addr );
335
336                         if ( c[ns].c_domain != NULL ) {
337                                 free( c[ns].c_domain );
338                         }
339
340                         c[ns].c_domain = ch_strdup( client_name == NULL
341                                 ? "" : client_name );
342
343                         pthread_mutex_lock( &c[ns].c_dnmutex );
344                         if ( c[ns].c_dn != NULL ) {
345                                 free( c[ns].c_dn );
346                                 c[ns].c_dn = NULL;
347                         }
348                         if ( c[ns].c_cdn != NULL ) {
349                                 free( c[ns].c_cdn );
350                                 c[ns].c_cdn = NULL;
351                         }
352                         pthread_mutex_unlock( &c[ns].c_dnmutex );
353                         c[ns].c_starttime = currenttime;
354                         c[ns].c_opsinitiated = 0;
355                         c[ns].c_opscompleted = 0;
356                 }
357                 pthread_mutex_unlock( &new_conn_mutex );
358
359                 Debug( LDAP_DEBUG_CONNS, "activity on:", 0, 0, 0 );
360                 for ( i = 0; i < dtblsize; i++ ) {
361                         int     r, w;
362
363                         r = FD_ISSET( i, &readfds );
364                         w = FD_ISSET( i, &writefds );
365                         if ( i != tcps && (r || w) ) {
366                                 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
367                                     r ? "r" : "", w ? "w" : "" );
368                         }
369                 }
370                 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
371
372                 for ( i = 0; i < dtblsize; i++ ) {
373                         if ( i == tcps || (! FD_ISSET( i, &readfds ) &&
374                             ! FD_ISSET( i, &writefds )) ) {
375                                 continue;
376                         }
377
378                         if ( FD_ISSET( i, &writefds ) ) {
379                                 Debug( LDAP_DEBUG_CONNS,
380                                     "signaling write waiter on %d\n", i, 0, 0 );
381
382                                 pthread_mutex_lock( &active_threads_mutex );
383                                 pthread_cond_signal( &c[i].c_wcv );
384                                 c[i].c_writewaiter = 0;
385                                 active_threads++;
386                                 pthread_mutex_unlock( &active_threads_mutex );
387                         }
388
389                         if ( FD_ISSET( i, &readfds ) ) {
390                                 Debug( LDAP_DEBUG_CONNS,
391                                     "read activity on %d\n", i, 0, 0 );
392
393                                 connection_activity( &c[i] );
394                         }
395                 }
396
397                 pthread_yield();
398         }
399
400         close( tcps );
401
402         pthread_mutex_lock( &active_threads_mutex );
403         Debug( LDAP_DEBUG_ANY,
404             "slapd shutting down - waiting for %d threads to terminate\n",
405             active_threads, 0, 0 );
406         while ( active_threads > 0 ) {
407                 pthread_cond_wait(&active_threads_cond, &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         return NULL;
418 }
419
420 static void
421 set_shutdown( int sig )
422 {
423         Debug( LDAP_DEBUG_ANY, "slapd got shutdown signal %d\n", sig, 0, 0 );
424         slapd_shutdown = 1;
425         pthread_kill( listener_tid, LDAP_SIGUSR1 );
426         (void) SIGNAL( LDAP_SIGUSR2, set_shutdown );
427         (void) SIGNAL( SIGTERM, set_shutdown );
428         (void) SIGNAL( SIGINT, set_shutdown );
429         (void) SIGNAL( SIGHUP, set_shutdown );
430 }
431
432 static void
433 do_nothing( int sig )
434 {
435         Debug( LDAP_DEBUG_TRACE, "slapd got do_nothing signal %d\n", sig, 0, 0 );
436         (void) SIGNAL( LDAP_SIGUSR1, do_nothing );
437 }