]> git.sur5r.net Git - openldap/blob - servers/slapd/daemon.c
very bad typo.
[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 <stdio.h>
9 #include <string.h>
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <errno.h>
13 #include <sys/time.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
16 #include <netdb.h>
17 #include <signal.h>
18 #ifdef _AIX
19 #include <sys/select.h>
20 #endif
21 #include "slap.h"
22 #include "portable.h"
23 #include "ldapconfig.h"
24 #ifdef NEED_FILIO
25 #include <sys/filio.h>
26 #else /* NEED_FILIO */
27 #include <sys/ioctl.h>
28 #endif /* NEED_FILIO */
29 #ifdef USE_SYSCONF
30 #include <unistd.h>
31 #endif /* USE_SYSCONF */
32
33 #ifdef TCP_WRAPPERS
34 #include <tcpd.h>
35
36 int allow_severity = LOG_INFO;
37 int deny_severity = LOG_NOTICE;
38 #endif /* TCP_WRAPPERS */
39
40 extern Operation        *op_add();
41
42 #ifndef SYSERRLIST_IN_STDIO
43 extern int              sys_nerr;
44 extern char             *sys_errlist[];
45 #endif
46 extern time_t           currenttime;
47 extern pthread_mutex_t  currenttime_mutex;
48 extern int              active_threads;
49 extern pthread_mutex_t  active_threads_mutex;
50 extern pthread_mutex_t  new_conn_mutex;
51 extern int              slapd_shutdown;
52 extern pthread_t        listener_tid;
53 extern int              num_conns;
54 extern pthread_mutex_t  ops_mutex;
55 extern int              g_argc;
56 extern char             **g_argv;
57
58 int             dtblsize;
59 Connection      *c;
60
61 static void     set_shutdown();
62 static void     do_nothing();
63
64 void
65 slapd_daemon(
66     int port
67 )
68 {
69         Operation               *o;
70         BerElement              ber;
71         unsigned long           len, tag, msgid;
72         int                     i;
73         int                     tcps, ns;
74         struct sockaddr_in      addr;
75         fd_set                  readfds;
76         fd_set                  writefds;
77         FILE                    *fp;
78         int                     on = 1;
79
80 #ifdef USE_SYSCONF
81         dtblsize = sysconf( _SC_OPEN_MAX );
82 #else /* USE_SYSCONF */
83         dtblsize = getdtablesize();
84 #endif /* USE_SYSCONF */
85         /*
86          * Add greg@greg.rim.or.jp
87          */
88 #ifdef FD_SETSIZE
89         if(dtblsize > FD_SETSIZE) {
90                 dtblsize = FD_SETSIZE;
91         }
92 #endif  /* !FD_SETSIZE */
93
94         c = (Connection *) ch_calloc( 1, dtblsize * sizeof(Connection) );
95
96         for ( i = 0; i < dtblsize; i++ ) {
97                 c[i].c_dn = NULL;
98                 c[i].c_addr = NULL;
99                 c[i].c_domain = NULL;
100                 c[i].c_ops = NULL;
101                 c[i].c_sb.sb_sd = -1;
102                 c[i].c_sb.sb_options = LBER_NO_READ_AHEAD;
103                 c[i].c_sb.sb_naddr = 0;
104                 c[i].c_sb.sb_ber.ber_buf = NULL;
105                 c[i].c_sb.sb_ber.ber_ptr = NULL;
106                 c[i].c_sb.sb_ber.ber_end = NULL;
107                 c[i].c_writewaiter = 0;
108                 c[i].c_connid = 0;
109                 pthread_mutex_init( &c[i].c_dnmutex,
110                     pthread_mutexattr_default );
111                 pthread_mutex_init( &c[i].c_opsmutex,
112                     pthread_mutexattr_default );
113                 pthread_mutex_init( &c[i].c_pdumutex,
114                     pthread_mutexattr_default );
115                 pthread_cond_init( &c[i].c_wcv, pthread_condattr_default );
116         }
117
118         if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
119                 Debug( LDAP_DEBUG_ANY, "socket() failed errno %d (%s)", errno,
120                     errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
121                     "unknown", 0 );
122                 exit( 1 );
123         }
124
125         i = 1;
126         if ( setsockopt( tcps, SOL_SOCKET, SO_REUSEADDR, (char *) &i,
127             sizeof(i) ) == -1 ) {
128                 Debug( LDAP_DEBUG_ANY, "setsockopt() failed errno %d (%s)",
129                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
130                     "unknown", 0 );
131         }
132
133         (void) memset( (void *) &addr, '\0', sizeof(addr) );
134         addr.sin_family = AF_INET;
135         addr.sin_addr.s_addr = INADDR_ANY;
136         addr.sin_port = htons( port );
137         if ( bind( tcps, (struct sockaddr *) &addr, sizeof(addr) ) == -1 ) {
138                 Debug( LDAP_DEBUG_ANY, "bind() failed errno %d (%s)\n",
139                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
140                     "unknown", 0 );
141                 exit( 1 );
142         }
143
144         if ( listen( tcps, 5 ) == -1 ) {
145                 Debug( LDAP_DEBUG_ANY, "listen() failed errno %d (%s)",
146                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
147                     "unknown", 0 );
148                 exit( 1 );
149         }
150
151         (void) SIGNAL( SIGPIPE, SIG_IGN );
152 #ifdef linux
153         /*
154          * LinuxThreads are implemented using SIGUSR1/USR2,
155          * so we'll use SIGSTKFLT and SIGUNUSED
156          */
157         (void) SIGNAL( SIGSTKFLT, (void *) do_nothing );
158         (void) SIGNAL( SIGUNUSED, (void *) set_shutdown );
159 #else /* !linux */
160         (void) SIGNAL( SIGUSR1, (void *) do_nothing );
161         (void) SIGNAL( SIGUSR2, (void *) set_shutdown );
162 #endif /* !linux */
163         (void) SIGNAL( SIGTERM, (void *) set_shutdown );
164         (void) SIGNAL( SIGINT, (void *) set_shutdown );
165         (void) SIGNAL( SIGHUP, (void *) set_shutdown );
166
167         Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
168 #ifdef SLAPD_PIDFILE
169         if ( (fp = fopen( SLAPD_PIDFILE, "w" )) != NULL ) {
170                 fprintf( fp, "%d\n", getpid() );
171                 fclose( fp );
172         }
173 #endif
174 #ifdef SLAPD_ARGSFILE
175         if ( (fp = fopen( SLAPD_ARGSFILE, "w" )) != NULL ) {
176                 for ( i = 0; i < g_argc; i++ ) {
177                         fprintf( fp, "%s ", g_argv[i] );
178                 }
179                 fprintf( fp, "\n" );
180                 fclose( fp );
181         }
182 #endif
183
184         while ( !slapd_shutdown ) {
185                 struct sockaddr_in      from;
186                 struct hostent          *hp;
187                 struct timeval          zero;
188                 struct timeval          *tvp;
189                 int                     len, pid;
190
191                 char    *client_name;
192                 char    *client_addr;
193
194                 FD_ZERO( &writefds );
195                 FD_ZERO( &readfds );
196                 FD_SET( tcps, &readfds );
197
198                 zero.tv_sec = 0;
199                 zero.tv_usec = 0;
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                 Debug( LDAP_DEBUG_CONNS, "before select active_threads %d\n",
222                     active_threads, 0, 0 );
223 #if     defined(PTHREAD_PREEMPTIVE) || defined(NO_THREADS)
224                 tvp = NULL;
225 #else
226                 tvp = active_threads ? &zero : NULL;
227 #endif
228                 pthread_mutex_unlock( &active_threads_mutex );
229
230                 switch ( i = select( dtblsize, &readfds, &writefds, 0, tvp ) ) {
231                 case -1:        /* failure - try again */
232                         Debug( LDAP_DEBUG_CONNS,
233                             "select failed errno %d (%s)\n",
234                             errno, errno > -1 && errno < sys_nerr ?
235                             sys_errlist[errno] : "unknown", 0 );
236                         continue;
237
238                 case 0:         /* timeout - let threads run */
239                         Debug( LDAP_DEBUG_CONNS, "select timeout - yielding\n",
240                             0, 0, 0 );
241                         pthread_yield();
242                         continue;
243
244                 default:        /* something happened - deal with it */
245                         Debug( LDAP_DEBUG_CONNS, "select activity on %d descriptors\n", i, 0, 0 );
246                         ;       /* FALL */
247                 }
248                 pthread_mutex_lock( &currenttime_mutex );
249                 time( &currenttime );
250                 pthread_mutex_unlock( &currenttime_mutex );
251
252                 /* new connection */
253                 pthread_mutex_lock( &new_conn_mutex );
254                 if ( FD_ISSET( tcps, &readfds ) ) {
255                         len = sizeof(from);
256                         if ( (ns = accept( tcps, (struct sockaddr *) &from,
257                             &len )) == -1 ) {
258                                 Debug( LDAP_DEBUG_ANY,
259                                     "accept() failed errno %d (%s)", errno,
260                                     errno > -1 && errno < sys_nerr ?
261                                     sys_errlist[errno] : "unknown", 0 );
262                                 pthread_mutex_unlock( &new_conn_mutex );
263                                 continue;
264                         }
265                         if ( ioctl( ns, FIONBIO, (caddr_t) &on ) == -1 ) {
266                                 Debug( LDAP_DEBUG_ANY,
267                                     "FIONBIO ioctl on %d failed\n", ns, 0, 0 );
268                         }
269
270                         c[ns].c_sb.sb_sd = ns;
271                         Debug( LDAP_DEBUG_CONNS, "new connection on %d\n", ns,
272                             0, 0 );
273
274                         pthread_mutex_lock( &ops_mutex );
275                         c[ns].c_connid = num_conns++;
276                         pthread_mutex_unlock( &ops_mutex );
277
278                         len = sizeof(from);
279
280                         if ( getpeername( ns, (struct sockaddr *) &from, &len )
281                             == 0 ) {
282                                 char *s;
283                                 client_addr = inet_ntoa( from.sin_addr );
284
285 #if defined(REVERSE_LOOKUP) || defined(TCP_WRAPPERS)
286                                 hp = gethostbyaddr( (char *)
287                                     &(from.sin_addr.s_addr),
288                                     sizeof(from.sin_addr.s_addr), AF_INET );
289
290                                 if(hp) {
291                                         client_name = hp->h_name;
292
293                                         /* normalize the domain */
294                                         for ( s = client_name; *s; s++ ) {
295                                                 *s = TOLOWER( *s );
296                                         }
297
298                                 } else {
299                                         client_name = NULL;
300                                 }
301 #else
302                                 client_name = NULL;
303 #endif
304
305                         } else {
306                                 client_name = NULL;;
307                                 client_addr = NULL;
308                         }
309
310 #ifdef TCP_WRAPPERS
311                         if(!hosts_ctl("slapd", client_name, client_addr,
312                                 STRING_UNKNOWN))
313                         {
314                                 /* DENY ACCESS */
315                                 Statslog( LDAP_DEBUG_STATS,
316                                  "conn=%d fd=%d connection from %s (%s) denied.\n",
317                                         c[ns].c_connid, ns,
318                                                 client_name == NULL ? "unknown" : client_name,
319                                                 client_addr == NULL ? "unknown" : client_addr,
320                                   0 );
321
322                                 close(ns);
323                                 pthread_mutex_unlock( &new_conn_mutex );
324                                 continue;
325                         }
326 #endif /* TCP_WRAPPERS */
327
328                         Statslog( LDAP_DEBUG_STATS,
329                             "conn=%d fd=%d connection from %s (%s) accepted.\n",
330                                 c[ns].c_connid, ns,
331                                         client_name == NULL ? "unknown" : client_name,
332                                         client_addr == NULL ? "unknown" : client_addr,
333                              0 );
334
335                         if ( c[ns].c_addr != NULL ) {
336                                 free( c[ns].c_addr );
337                         }
338                         c[ns].c_addr = strdup( client_addr );
339
340                         if ( c[ns].c_domain != NULL ) {
341                                 free( c[ns].c_domain );
342                         }
343
344                         c[ns].c_domain = strdup( client_name == NULL
345                                 ? "" : client_name );
346
347                         pthread_mutex_lock( &c[ns].c_dnmutex );
348                         if ( c[ns].c_dn != NULL ) {
349                                 free( c[ns].c_dn );
350                                 c[ns].c_dn = 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         pthread_mutex_lock( &active_threads_mutex );
402         Debug( LDAP_DEBUG_ANY,
403             "slapd shutting down - waiting for %d threads to terminate\n",
404             active_threads, 0, 0 );
405         while ( active_threads > 0 ) {
406                 pthread_mutex_unlock( &active_threads_mutex );
407                 pthread_yield();
408                 pthread_mutex_lock( &active_threads_mutex );
409         }
410         pthread_mutex_unlock( &active_threads_mutex );
411
412         /* let backends do whatever cleanup they need to do */
413         Debug( LDAP_DEBUG_TRACE,
414             "slapd shutting down - waiting for backends to close down\n", 0, 0,
415             0 );
416         be_close();
417         Debug( LDAP_DEBUG_ANY, "slapd stopping\n", 0, 0, 0 );
418 }
419
420 static void
421 set_shutdown()
422 {
423         Debug( LDAP_DEBUG_ANY, "slapd got shutdown signal\n", 0, 0, 0 );
424         slapd_shutdown = 1;
425 #ifdef linux
426         /*
427          * LinuxThreads are implemented using SIGUSR1/USR2,
428          * so we'll use SIGSTKFLT and SIGUNUSED
429          */
430         pthread_kill( listener_tid, SIGSTKFLT );
431         (void) SIGNAL( SIGUNUSED, (void *) set_shutdown );
432 #else /* !linux */
433         pthread_kill( listener_tid, SIGUSR1 );
434         (void) SIGNAL( SIGUSR2, (void *) set_shutdown );
435 #endif /* !linux */
436         (void) SIGNAL( SIGTERM, (void *) set_shutdown );
437         (void) SIGNAL( SIGINT, (void *) set_shutdown );
438         (void) SIGNAL( SIGHUP, (void *) set_shutdown );
439 }
440
441 static void
442 do_nothing()
443 {
444         Debug( LDAP_DEBUG_TRACE, "slapd got do_nothing signal\n", 0, 0, 0 );
445 #ifdef linux
446         /*
447          * LinuxThreads are implemented using SIGUSR1/USR2,
448          * so we'll use SIGSTKFLT and SIGUNUSED
449          */
450         (void) SIGNAL( SIGSTKFLT, (void *) do_nothing );
451 #else /* !linux */
452         (void) SIGNAL( SIGUSR1, (void *) do_nothing );
453 #endif /* !linux */
454 }