]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/daemon.c
Another concurrency typo.
[openldap] / servers / slapd / daemon.c
index 161fb79cf0e2bf6ce67b05be0ff4d7efba0a67ab..684a9d235d1f76cdce045b30fc11c063a19edd46 100644 (file)
@@ -1,55 +1,52 @@
-#include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <errno.h>
-#include <sys/time.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <netdb.h>
-#include <signal.h>
-#ifdef _AIX
-#include <sys/select.h>
-#endif
-#include "slap.h"
+
+/* Revision history
+ *
+ * 5-Jun-96    hodges
+ *     Added locking of new_conn_mutex when traversing the c[] array.
+ */
+
 #include "portable.h"
+
+#include <stdio.h>
+
+#include <ac/ctype.h>
+#include <ac/errno.h>
+#include <ac/signal.h>
+#include <ac/socket.h>
+#include <ac/string.h>
+#include <ac/time.h>
+#include <ac/unistd.h>
+
 #include "ldapconfig.h"
-#ifdef NEED_FILIO
+#include "slap.h"
+
+#ifdef HAVE_SYS_FILIO_H
 #include <sys/filio.h>
-#else /* NEED_FILIO */
+#elif HAVE_SYS_IOCTL_H
 #include <sys/ioctl.h>
-#endif /* NEED_FILIO */
-#ifdef USE_SYSCONF
-#include <unistd.h>
-#endif /* USE_SYSCONF */
+#endif
 
-extern Operation       *op_add();
+#ifdef HAVE_TCPD
+#include <tcpd.h>
 
-#ifndef SYSERRLIST_IN_STDIO
-extern int             sys_nerr;
-extern char            *sys_errlist[];
-#endif
-extern time_t          currenttime;
-extern pthread_mutex_t currenttime_mutex;
-extern int             active_threads;
-extern pthread_mutex_t active_threads_mutex;
-extern pthread_mutex_t new_conn_mutex;
-extern int             slapd_shutdown;
-extern pthread_t       listener_tid;
-extern int             num_conns;
-extern pthread_mutex_t ops_mutex;
-extern int             g_argc;
-extern char            **g_argv;
+int allow_severity = LOG_INFO;
+int deny_severity = LOG_NOTICE;
+#endif /* TCP Wrappers */
 
 int            dtblsize;
 Connection     *c;
 
-static void    set_shutdown();
-static void    do_nothing();
+static volatile sig_atomic_t slapd_shutdown = 0;
+static void    set_shutdown(int sig);
+static void    do_nothing  (int sig);
+
+/* a link to the slapd.conf configuration parameters */
+extern char *slapd_pid_file;
+extern char *slapd_args_file;
 
-void
-daemon(
-    int        port
+void *
+slapd_daemon(
+    void *port
 )
 {
        Operation               *o;
@@ -63,16 +60,25 @@ daemon(
        FILE                    *fp;
        int                     on = 1;
 
-#ifdef USE_SYSCONF
-        dtblsize = sysconf( _SC_OPEN_MAX );
-#else /* USE_SYSCONF */
-        dtblsize = getdtablesize();
-#endif /* USE_SYSCONF */
+#ifdef HAVE_SYSCONF
+       dtblsize = sysconf( _SC_OPEN_MAX );
+#elif HAVE_GETDTABLESIZE
+       dtblsize = getdtablesize();
+#else
+       dtblsize = FD_SETSIZE
+#endif
+
+#ifdef FD_SETSIZE
+       if(dtblsize > FD_SETSIZE) {
+               dtblsize = FD_SETSIZE;
+       }
+#endif /* !FD_SETSIZE */
 
        c = (Connection *) ch_calloc( 1, dtblsize * sizeof(Connection) );
 
        for ( i = 0; i < dtblsize; i++ ) {
                c[i].c_dn = NULL;
+               c[i].c_cdn = NULL;
                c[i].c_addr = NULL;
                c[i].c_domain = NULL;
                c[i].c_ops = NULL;
@@ -84,13 +90,10 @@ daemon(
                c[i].c_sb.sb_ber.ber_end = NULL;
                c[i].c_writewaiter = 0;
                c[i].c_connid = 0;
-               pthread_mutex_init( &c[i].c_dnmutex,
-                   pthread_mutexattr_default );
-               pthread_mutex_init( &c[i].c_opsmutex,
-                   pthread_mutexattr_default );
-               pthread_mutex_init( &c[i].c_pdumutex,
-                   pthread_mutexattr_default );
-               pthread_cond_init( &c[i].c_wcv, pthread_condattr_default );
+               ldap_pvt_thread_mutex_init( &c[i].c_dnmutex );
+               ldap_pvt_thread_mutex_init( &c[i].c_opsmutex );
+               ldap_pvt_thread_mutex_init( &c[i].c_pdumutex );
+               ldap_pvt_thread_cond_init( &c[i].c_wcv );
        }
 
        if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
@@ -111,7 +114,7 @@ daemon(
        (void) memset( (void *) &addr, '\0', sizeof(addr) );
        addr.sin_family = AF_INET;
        addr.sin_addr.s_addr = INADDR_ANY;
-       addr.sin_port = htons( port );
+       addr.sin_port = htons( (int)port );
        if ( bind( tcps, (struct sockaddr *) &addr, sizeof(addr) ) == -1 ) {
                Debug( LDAP_DEBUG_ANY, "bind() failed errno %d (%s)\n",
                    errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
@@ -127,27 +130,28 @@ daemon(
        }
 
        (void) SIGNAL( SIGPIPE, SIG_IGN );
-       (void) SIGNAL( SIGUSR1, (void *) do_nothing );
-       (void) SIGNAL( SIGUSR2, (void *) set_shutdown );
-       (void) SIGNAL( SIGTERM, (void *) set_shutdown );
-       (void) SIGNAL( SIGHUP, (void *) set_shutdown );
+       (void) SIGNAL( LDAP_SIGUSR1, do_nothing );
+       (void) SIGNAL( LDAP_SIGUSR2, set_shutdown );
+       (void) SIGNAL( SIGTERM, set_shutdown );
+       (void) SIGNAL( SIGINT, set_shutdown );
+       (void) SIGNAL( SIGHUP, set_shutdown );
 
        Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
-#ifdef SLAPD_PIDFILE
-       if ( (fp = fopen( SLAPD_PIDFILE, "w" )) != NULL ) {
-               fprintf( fp, "%d\n", getpid() );
+
+       if (( slapd_pid_file != NULL ) &&
+                       (( fp = fopen( slapd_pid_file, "w" )) != NULL )) {
+               fprintf( fp, "%d\n", (int) getpid() );
                fclose( fp );
        }
-#endif
-#ifdef SLAPD_ARGSFILE
-       if ( (fp = fopen( SLAPD_ARGSFILE, "w" )) != NULL ) {
+
+       if (( slapd_args_file != NULL ) &&
+                       (( fp = fopen( slapd_args_file, "w" )) != NULL )) {
                for ( i = 0; i < g_argc; i++ ) {
                        fprintf( fp, "%s ", g_argv[i] );
                }
                fprintf( fp, "\n" );
                fclose( fp );
        }
-#endif
 
        while ( !slapd_shutdown ) {
                struct sockaddr_in      from;
@@ -156,14 +160,22 @@ daemon(
                struct timeval          *tvp;
                int                     len, pid;
 
+               char    *client_name;
+               char    *client_addr;
+
                FD_ZERO( &writefds );
                FD_ZERO( &readfds );
                FD_SET( tcps, &readfds );
 
-               pthread_mutex_lock( &active_threads_mutex );
+               zero.tv_sec = 0;
+               zero.tv_usec = 0;
+
+               ldap_pvt_thread_mutex_lock( &active_threads_mutex );
                Debug( LDAP_DEBUG_CONNS,
                    "listening for connections on %d, activity on:",
                    tcps, 0, 0 );
+
+               ldap_pvt_thread_mutex_lock( &new_conn_mutex );
                for ( i = 0; i < dtblsize; i++ ) {
                        if ( c[i].c_sb.sb_sd != -1 ) {
                                FD_SET( c[i].c_sb.sb_sd, &readfds );
@@ -176,19 +188,18 @@ daemon(
                        }
                }
                Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
+               ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
 
-               zero.tv_sec = 0;
-               zero.tv_usec = 0;
                Debug( LDAP_DEBUG_CONNS, "before select active_threads %d\n",
                    active_threads, 0, 0 );
-#ifdef PTHREAD_PREEMPTIVE
+#if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
                tvp = NULL;
 #else
                tvp = active_threads ? &zero : NULL;
 #endif
-               pthread_mutex_unlock( &active_threads_mutex );
+               ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
 
-               switch ( select( dtblsize, &readfds, &writefds, 0, tvp ) ) {
+               switch ( i = select( dtblsize, &readfds, &writefds, 0, tvp ) ) {
                case -1:        /* failure - try again */
                        Debug( LDAP_DEBUG_CONNS,
                            "select failed errno %d (%s)\n",
@@ -199,19 +210,19 @@ daemon(
                case 0:         /* timeout - let threads run */
                        Debug( LDAP_DEBUG_CONNS, "select timeout - yielding\n",
                            0, 0, 0 );
-                       pthread_yield();
+                       ldap_pvt_thread_yield();
                        continue;
 
                default:        /* something happened - deal with it */
-                       Debug( LDAP_DEBUG_CONNS, "select activity\n", 0, 0, 0 );
+                       Debug( LDAP_DEBUG_CONNS, "select activity on %d descriptors\n", i, 0, 0 );
                        ;       /* FALL */
                }
-               pthread_mutex_lock( &currenttime_mutex );
+               ldap_pvt_thread_mutex_lock( &currenttime_mutex );
                time( &currenttime );
-               pthread_mutex_unlock( &currenttime_mutex );
+               ldap_pvt_thread_mutex_unlock( &currenttime_mutex );
 
                /* new connection */
-               pthread_mutex_lock( &new_conn_mutex );
+               ldap_pvt_thread_mutex_lock( &new_conn_mutex );
                if ( FD_ISSET( tcps, &readfds ) ) {
                        len = sizeof(from);
                        if ( (ns = accept( tcps, (struct sockaddr *) &from,
@@ -220,68 +231,108 @@ daemon(
                                    "accept() failed errno %d (%s)", errno,
                                    errno > -1 && errno < sys_nerr ?
                                    sys_errlist[errno] : "unknown", 0 );
-                               pthread_mutex_unlock( &new_conn_mutex );
+                               ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
                                continue;
                        }
                        if ( ioctl( ns, FIONBIO, (caddr_t) &on ) == -1 ) {
                                Debug( LDAP_DEBUG_ANY,
-                                   "FIONBIO ioctl on %d faled\n", ns, 0, 0 );
+                                   "FIONBIO ioctl on %d failed\n", ns, 0, 0 );
                        }
-                       c[ns].c_sb.sb_sd = ns;
+
                        Debug( LDAP_DEBUG_CONNS, "new connection on %d\n", ns,
                            0, 0 );
 
-                       pthread_mutex_lock( &ops_mutex );
-                       c[ns].c_connid = num_conns++;
-                       pthread_mutex_unlock( &ops_mutex );
                        len = sizeof(from);
+
                        if ( getpeername( ns, (struct sockaddr *) &from, &len )
                            == 0 ) {
-                               char    *s;
-#ifdef REVERSE_LOOKUP
+                               char *s;
+                               client_addr = inet_ntoa( from.sin_addr );
+
+#if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
                                hp = gethostbyaddr( (char *)
                                    &(from.sin_addr.s_addr),
                                    sizeof(from.sin_addr.s_addr), AF_INET );
-#else
-                               hp = NULL;
-#endif
 
-                               Statslog( LDAP_DEBUG_STATS,
-                                   "conn=%d fd=%d connection from %s (%s)\n",
-                                   c[ns].c_connid, ns, hp == NULL ? "unknown"
-                                   : hp->h_name, inet_ntoa( from.sin_addr ),
-                                   0 );
+                               if(hp) {
+                                       client_name = hp->h_name;
 
-                               if ( c[ns].c_addr != NULL ) {
-                                       free( c[ns].c_addr );
-                               }
-                               c[ns].c_addr = strdup( inet_ntoa(
-                                   from.sin_addr ) );
-                               if ( c[ns].c_domain != NULL ) {
-                                       free( c[ns].c_domain );
-                               }
-                               c[ns].c_domain = strdup( hp == NULL ? "" :
-                                   hp->h_name );
-                               /* normalize the domain */
-                               for ( s = c[ns].c_domain; *s; s++ ) {
-                                       *s = TOLOWER( *s );
+                                       /* normalize the domain */
+                                       for ( s = client_name; *s; s++ ) {
+                                               *s = TOLOWER( *s );
+                                       }
+
+                               } else {
+                                       client_name = NULL;
                                }
+#else
+                               client_name = NULL;
+#endif
+
                        } else {
-                               Statslog( LDAP_DEBUG_STATS,
-                                   "conn=%d fd=%d connection from unknown\n",
-                                   c[ns].c_connid, ns, 0, 0, 0 );
+                               client_name = NULL;;
+                               client_addr = NULL;
+                       }
+
+#ifdef HAVE_TCPD
+                       if(!hosts_ctl("slapd",
+                               client_name != NULL ? client_name : STRING_UNKNOWN,
+                               client_addr != NULL ? client_addr : STRING_UNKNOWN,
+                               STRING_UNKNOWN))
+                       {
+                               /* DENY ACCESS */
+                               Statslog( LDAP_DEBUG_ANY,
+                                "fd=%d connection from %s (%s) denied.\n",
+                                       ns,
+                                               client_name == NULL ? "unknown" : client_name,
+                                               client_addr == NULL ? "unknown" : client_addr,
+                                 0, 0 );
+
+                               close(ns);
+                               ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
+                               continue;
+                       }
+#endif /* HAVE_TCPD */
+
+                       c[ns].c_sb.sb_sd = ns;
+                       ldap_pvt_thread_mutex_lock( &ops_mutex );
+                       c[ns].c_connid = num_conns++;
+                       ldap_pvt_thread_mutex_unlock( &ops_mutex );
+
+                       Statslog( LDAP_DEBUG_STATS,
+                           "conn=%d fd=%d connection from %s (%s) accepted.\n",
+                               c[ns].c_connid, ns,
+                                       client_name == NULL ? "unknown" : client_name,
+                                       client_addr == NULL ? "unknown" : client_addr,
+                            0 );
+
+                       if ( c[ns].c_addr != NULL ) {
+                               free( c[ns].c_addr );
+                       }
+                       c[ns].c_addr = ch_strdup( client_addr );
+
+                       if ( c[ns].c_domain != NULL ) {
+                               free( c[ns].c_domain );
                        }
-                       pthread_mutex_lock( &c[ns].c_dnmutex );
+
+                       c[ns].c_domain = ch_strdup( client_name == NULL
+                               ? "" : client_name );
+
+                       ldap_pvt_thread_mutex_lock( &c[ns].c_dnmutex );
                        if ( c[ns].c_dn != NULL ) {
                                free( c[ns].c_dn );
                                c[ns].c_dn = NULL;
                        }
-                       pthread_mutex_unlock( &c[ns].c_dnmutex );
+                       if ( c[ns].c_cdn != NULL ) {
+                               free( c[ns].c_cdn );
+                               c[ns].c_cdn = NULL;
+                       }
+                       ldap_pvt_thread_mutex_unlock( &c[ns].c_dnmutex );
                        c[ns].c_starttime = currenttime;
                        c[ns].c_opsinitiated = 0;
                        c[ns].c_opscompleted = 0;
                }
-               pthread_mutex_unlock( &new_conn_mutex );
+               ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
 
                Debug( LDAP_DEBUG_CONNS, "activity on:", 0, 0, 0 );
                for ( i = 0; i < dtblsize; i++ ) {
@@ -306,11 +357,11 @@ daemon(
                                Debug( LDAP_DEBUG_CONNS,
                                    "signaling write waiter on %d\n", i, 0, 0 );
 
-                               pthread_mutex_lock( &active_threads_mutex );
-                               pthread_cond_signal( &c[i].c_wcv );
-                               c[i].c_writewaiter = 0;
+                               ldap_pvt_thread_mutex_lock( &active_threads_mutex );
                                active_threads++;
-                               pthread_mutex_unlock( &active_threads_mutex );
+                               c[i].c_writewaiter = 0;
+                               ldap_pvt_thread_cond_signal( &c[i].c_wcv );
+                               ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
                        }
 
                        if ( FD_ISSET( i, &readfds ) ) {
@@ -321,43 +372,54 @@ daemon(
                        }
                }
 
-               pthread_yield();
+               ldap_pvt_thread_yield();
        }
 
+       Debug( LDAP_DEBUG_TRACE,
+           "slapd shutdown: shutdown initiated.\n",
+           0, 0, 0 );
+
        close( tcps );
-       pthread_mutex_lock( &active_threads_mutex );
+
+       ldap_pvt_thread_mutex_lock( &active_threads_mutex );
        Debug( LDAP_DEBUG_ANY,
-           "slapd shutting down - waiting for %d threads to terminate\n",
+           "slapd shutdown: waiting for %d threads to terminate\n",
            active_threads, 0, 0 );
        while ( active_threads > 0 ) {
-               pthread_mutex_unlock( &active_threads_mutex );
-               pthread_yield();
-               pthread_mutex_lock( &active_threads_mutex );
+               ldap_pvt_thread_cond_wait(&active_threads_cond, &active_threads_mutex);
        }
-       pthread_mutex_unlock( &active_threads_mutex );
+       ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
 
        /* let backends do whatever cleanup they need to do */
        Debug( LDAP_DEBUG_TRACE,
-           "slapd shutting down - waiting for backends to close down\n", 0, 0,
-           0 );
+           "slapd shutdown: closing each backends.\n",
+           0, 0, 0 );
        be_close();
-       Debug( LDAP_DEBUG_ANY, "slapd stopping\n", 0, 0, 0 );
+
+       Debug( LDAP_DEBUG_TRACE,
+           "slapd shutdown: shutdown backends.\n",
+           0, 0, 0 );
+       be_shutdown();
+       Debug( LDAP_DEBUG_ANY, "slapd: stopped\n", 0, 0, 0 );
+
+       return NULL;
 }
 
 static void
-set_shutdown()
+set_shutdown( int sig )
 {
-       Debug( LDAP_DEBUG_ANY, "slapd got shutdown signal\n", 0, 0, 0 );
+       Debug( LDAP_DEBUG_ANY, "slapd got shutdown signal %d\n", sig, 0, 0 );
        slapd_shutdown = 1;
-       pthread_kill( listener_tid, SIGUSR1 );
-       (void) SIGNAL( SIGUSR2, (void *) set_shutdown );
-       (void) SIGNAL( SIGTERM, (void *) set_shutdown );
-       (void) SIGNAL( SIGHUP, (void *) set_shutdown );
+       ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
+       (void) SIGNAL( LDAP_SIGUSR2, set_shutdown );
+       (void) SIGNAL( SIGTERM, set_shutdown );
+       (void) SIGNAL( SIGINT, set_shutdown );
+       (void) SIGNAL( SIGHUP, set_shutdown );
 }
 
 static void
-do_nothing()
+do_nothing( int sig )
 {
-       Debug( LDAP_DEBUG_TRACE, "slapd got SIGUSR1\n", 0, 0, 0 );
-       (void) SIGNAL( SIGUSR1, (void *) do_nothing );
+       Debug( LDAP_DEBUG_TRACE, "slapd got do_nothing signal %d\n", sig, 0, 0 );
+       (void) SIGNAL( LDAP_SIGUSR1, do_nothing );
 }