]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/daemon.c
Another concurrency typo.
[openldap] / servers / slapd / daemon.c
index ae28246e5facf868377b9397b784c700801eae53..684a9d235d1f76cdce045b30fc11c063a19edd46 100644 (file)
@@ -40,11 +40,9 @@ static volatile sig_atomic_t slapd_shutdown = 0;
 static void    set_shutdown(int sig);
 static void    do_nothing  (int sig);
 
-/* we need the server's name for constructing the pid/args file names */
-#if defined( SLAPD_PIDFILE ) || defined( SLAPD_ARGSFILE )
-extern char  *serverName;
-#define DEFAULT_SERVERNAME  "slapd"
-#endif
+/* a link to the slapd.conf configuration parameters */
+extern char *slapd_pid_file;
+extern char *slapd_args_file;
 
 void *
 slapd_daemon(
@@ -62,13 +60,6 @@ slapd_daemon(
        FILE                    *fp;
        int                     on = 1;
 
-#ifdef SLAPD_PIDFILE
-    char            pidFile[BUFSIZ];
-#endif
-#ifdef SLAPD_ARGSFILE
-    char            argsFile[BUFSIZ];
-#endif
-
 #ifdef HAVE_SYSCONF
        dtblsize = sysconf( _SC_OPEN_MAX );
 #elif HAVE_GETDTABLESIZE
@@ -87,6 +78,7 @@ slapd_daemon(
 
        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;
@@ -98,13 +90,10 @@ slapd_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 ) {
@@ -149,27 +138,20 @@ slapd_daemon(
 
        Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
 
-#if defined( SLAPD_PIDFILE ) || defined( SLAPD_ARGSFILE )
-    if ( !serverName ) serverName = DEFAULT_SERVERNAME;
-#endif
-
-#ifdef SLAPD_PIDFILE
-    sprintf( pidFile, "%s%s%s", SLAPD_PIDDIR, serverName, SLAPD_PIDEXT );
-       if ( (fp = fopen( pidFile, "w" )) != NULL ) {
+       if (( slapd_pid_file != NULL ) &&
+                       (( fp = fopen( slapd_pid_file, "w" )) != NULL )) {
                fprintf( fp, "%d\n", (int) getpid() );
                fclose( fp );
        }
-#endif
-#ifdef SLAPD_ARGSFILE
-    sprintf( argsFile, "%s%s%s", SLAPD_ARGSDIR, serverName, SLAPD_ARGSEXT );
-       if ( (fp = fopen( 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;
@@ -188,12 +170,12 @@ slapd_daemon(
                zero.tv_sec = 0;
                zero.tv_usec = 0;
 
-               pthread_mutex_lock( &active_threads_mutex );
+               ldap_pvt_thread_mutex_lock( &active_threads_mutex );
                Debug( LDAP_DEBUG_CONNS,
                    "listening for connections on %d, activity on:",
                    tcps, 0, 0 );
 
-               pthread_mutex_lock( &new_conn_mutex );
+               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 );
@@ -206,7 +188,7 @@ slapd_daemon(
                        }
                }
                Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
-               pthread_mutex_unlock( &new_conn_mutex );
+               ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
 
                Debug( LDAP_DEBUG_CONNS, "before select active_threads %d\n",
                    active_threads, 0, 0 );
@@ -215,7 +197,7 @@ slapd_daemon(
 #else
                tvp = active_threads ? &zero : NULL;
 #endif
-               pthread_mutex_unlock( &active_threads_mutex );
+               ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
 
                switch ( i = select( dtblsize, &readfds, &writefds, 0, tvp ) ) {
                case -1:        /* failure - try again */
@@ -228,19 +210,19 @@ slapd_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 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,
@@ -249,7 +231,7 @@ slapd_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 ) {
@@ -257,14 +239,9 @@ slapd_daemon(
                                    "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 )
@@ -298,23 +275,30 @@ slapd_daemon(
                        }
 
 #ifdef HAVE_TCPD
-                       if(!hosts_ctl("slapd", client_name, client_addr,
+                       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_STATS,
-                                "conn=%d fd=%d connection from %s (%s) denied.\n",
-                                       c[ns].c_connid, ns,
+                               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, 0 );
 
                                close(ns);
-                               pthread_mutex_unlock( &new_conn_mutex );
+                               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,
@@ -334,17 +318,21 @@ slapd_daemon(
                        c[ns].c_domain = ch_strdup( client_name == NULL
                                ? "" : client_name );
 
-                       pthread_mutex_lock( &c[ns].c_dnmutex );
+                       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++ ) {
@@ -369,11 +357,11 @@ slapd_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 ) ) {
@@ -384,27 +372,36 @@ slapd_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;
 }
 
@@ -413,7 +410,7 @@ set_shutdown( int sig )
 {
        Debug( LDAP_DEBUG_ANY, "slapd got shutdown signal %d\n", sig, 0, 0 );
        slapd_shutdown = 1;
-       pthread_kill( listener_tid, LDAP_SIGUSR1 );
+       ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
        (void) SIGNAL( LDAP_SIGUSR2, set_shutdown );
        (void) SIGNAL( SIGTERM, set_shutdown );
        (void) SIGNAL( SIGINT, set_shutdown );