]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/main.c
Fix backend_destroy to call bi_destroy instead of bi_close
[openldap] / servers / slapd / main.c
index f529cb5eca015777d194a49e7e301836a773e104..894f834a9fed90c966f1089c8c135e9bf28bb8ce 100644 (file)
@@ -2,6 +2,7 @@
 
 #include <stdio.h>
 
+#include <ac/signal.h>
 #include <ac/socket.h>
 #include <ac/string.h>
 #include <ac/time.h>
@@ -11,7 +12,6 @@
 #include "slap.h"
 #include "lutil.h"                     /* Get lutil_detach() */
 
-
 /*
  * when more than one slapd is running on one machine, each one might have
  * it's own LOCAL for syslogging and must have its own pid/args files
 #define DEFAULT_SYSLOG_USER  LOG_LOCAL4
 
 typedef struct _str2intDispatch {
-
-        char    *stringVal;
-        int      abbr;
-        int      intVal;
-
+       char    *stringVal;
+       int      abbr;
+       int      intVal;
 } STRDISP, *STRDISP_P;
 
 
 /* table to compute syslog-options to integer */
 static STRDISP  syslog_types[] = {
-
     { "LOCAL0",         6, LOG_LOCAL0 },
     { "LOCAL1",         6, LOG_LOCAL1 },
     { "LOCAL2",         6, LOG_LOCAL2 },
@@ -42,7 +39,6 @@ static STRDISP  syslog_types[] = {
     { "LOCAL6",         6, LOG_LOCAL6 },
     { "LOCAL7",         6, LOG_LOCAL7 },
     NULL
-
 };
 
 static int   cnvt_str2int();
@@ -65,10 +61,9 @@ main( int argc, char **argv )
 {
        int             i;
        int             inetd = 0;
+       int             rc = 0;
        int             port;
        int             udp;
-       Backend         *be = NULL;
-       FILE            *fp = NULL;
 #ifdef LOG_LOCAL4
     int     syslogUser = DEFAULT_SYSLOG_USER;
 #endif
@@ -171,45 +166,61 @@ main( int argc, char **argv )
                serverName = ch_strdup( serverName + 1 );
        }
 
-       if ( ! inetd ) {
-               /* pre-open config file before detach in case it is a relative path */
-               fp = fopen( configfile, "r" );
-#ifdef LDAP_DEBUG
-               lutil_detach( ldap_debug, 0 );
-#else
-               lutil_detach( 0, 0 );
-#endif
-       }
-
 #ifdef LOG_LOCAL4
        openlog( serverName, OPENLOG_OPTIONS, syslogUser );
 #else
        openlog( serverName, OPENLOG_OPTIONS );
 #endif
 
-       init();
-       read_config( configfile, &be, fp );
+#ifdef SLAPD_BDB2
+       bdb2i_do_timing = 1;
+#endif
+
+       if ( slap_init( SLAP_SERVER_MODE, serverName ) != 0 ) {
+               rc = 1;
+               goto destroy;
+       }
+
+       if ( read_config( configfile ) != 0 ) {
+               rc = 1;
+               goto destroy;
+       }
+
+       if ( slap_startup(-1)  != 0 ) {
+               rc = 1;
+               goto shutdown;
+       }
 
        if ( ! inetd ) {
                int             status;
 
+               (void) SIGNAL( SIGPIPE, SIG_IGN );
+               (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
+               (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
+               (void) SIGNAL( SIGTERM, slap_set_shutdown );
+               (void) SIGNAL( SIGINT, slap_set_shutdown );
+               (void) SIGNAL( SIGHUP, slap_set_shutdown );
+
+#ifdef LDAP_DEBUG
+               lutil_detach( ldap_debug, 0 );
+#else
+               lutil_detach( 0, 0 );
+#endif
+
                time( &starttime );
 
-               if ( status = pthread_create( &listener_tid, NULL,
+               if ( status = ldap_pvt_thread_create( &listener_tid, 0,
                        slapd_daemon, (void *) port ) != 0 )
                {
                        Debug( LDAP_DEBUG_ANY,
-                           "listener pthread_create failed (%d)\n", status, 0, 0 );
-                       exit( 1 );
-               }
+                           "listener ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
 
-#ifdef HAVE_PTHREADS_FINAL
-               pthread_join( listener_tid, (void *) NULL );
-#else
-               pthread_join( listener_tid, (void *) &status );
-#endif
+                       rc = 1;
 
-               return 0;
+               } else {
+                       /* wait for the listener thread to complete */
+                       ldap_pvt_thread_join( listener_tid, (void *) NULL );
+               }
 
        } else {
                Connection              c;
@@ -230,9 +241,9 @@ main( int argc, char **argv )
                c.c_sb.sb_ber.ber_buf = NULL;
                c.c_sb.sb_ber.ber_ptr = NULL;
                c.c_sb.sb_ber.ber_end = NULL;
-               pthread_mutex_init( &c.c_dnmutex, pthread_mutexattr_default );
-               pthread_mutex_init( &c.c_opsmutex, pthread_mutexattr_default );
-               pthread_mutex_init( &c.c_pdumutex, pthread_mutexattr_default );
+               ldap_pvt_thread_mutex_init( &c.c_dnmutex );
+               ldap_pvt_thread_mutex_init( &c.c_opsmutex );
+               ldap_pvt_thread_mutex_init( &c.c_pdumutex );
 #ifdef notdefcldap
                c.c_sb.sb_addrs = (void **) saddrlist;
                c.c_sb.sb_fromaddr = &faddr;
@@ -262,9 +273,9 @@ main( int argc, char **argv )
 
                while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
                    == LDAP_TAG_MESSAGE ) {
-                       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 );
 
                        if ( (tag = ber_get_int( &ber, &msgid ))
                            != LDAP_TAG_MSGID ) {
@@ -291,7 +302,12 @@ main( int argc, char **argv )
                        ber_free( &ber, 1 );
                }
        }
-       return 1;
+
+shutdown:
+       slap_shutdown(-1);
+destroy:
+       slap_destroy();
+       return rc;
 }