Back to index

openldap  2.4.31
init.c
Go to the documentation of this file.
00001 /* init.c - initialize various things */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 1998-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
00017  * All rights reserved.
00018  *
00019  * Redistribution and use in source and binary forms are permitted
00020  * provided that this notice is preserved and that due credit is given
00021  * to the University of Michigan at Ann Arbor. The name of the University
00022  * may not be used to endorse or promote products derived from this
00023  * software without specific prior written permission. This software
00024  * is provided ``as is'' without express or implied warranty.
00025  */
00026 
00027 #include "portable.h"
00028 
00029 #include <stdio.h>
00030 
00031 #include <ac/socket.h>
00032 #include <ac/string.h>
00033 #include <ac/time.h>
00034 
00035 #include "slap.h"
00036 #include "lber_pvt.h"
00037 
00038 #include "ldap_rq.h"
00039 
00040 /*
00041  * read-only global variables or variables only written by the listener
00042  * thread (after they are initialized) - no need to protect them with a mutex.
00043  */
00044 int           slap_debug = 0;
00045 
00046 #ifdef LDAP_DEBUG
00047 int           ldap_syslog = LDAP_DEBUG_STATS;
00048 #else
00049 int           ldap_syslog;
00050 #endif
00051 
00052 #ifdef LOG_DEBUG
00053 int           ldap_syslog_level = LOG_DEBUG;
00054 #endif
00055 
00056 BerVarray default_referral = NULL;
00057 
00058 /*
00059  * global variables that need mutex protection
00060  */
00061 ldap_pvt_thread_pool_t      connection_pool;
00062 int                  connection_pool_max = SLAP_MAX_WORKER_THREADS;
00063 int           slap_tool_thread_max = 1;
00064 
00065 slap_counters_t                    slap_counters, *slap_counters_list;
00066 
00067 static const char* slap_name = NULL;
00068 int slapMode = SLAP_UNDEFINED_MODE;
00069 
00070 int
00071 slap_init( int mode, const char *name )
00072 {
00073        int rc;
00074 
00075        assert( mode );
00076 
00077        if ( slapMode != SLAP_UNDEFINED_MODE ) {
00078               /* Make sure we write something to stderr */
00079               slap_debug |= LDAP_DEBUG_NONE;
00080               Debug( LDAP_DEBUG_ANY,
00081                "%s init: init called twice (old=%d, new=%d)\n",
00082                name, slapMode, mode );
00083 
00084               return 1;
00085        }
00086 
00087        slapMode = mode;
00088 
00089        slap_op_init();
00090 
00091 #ifdef SLAPD_MODULES
00092        if ( module_init() != 0 ) {
00093               slap_debug |= LDAP_DEBUG_NONE;
00094               Debug( LDAP_DEBUG_ANY,
00095                   "%s: module_init failed\n",
00096                      name, 0, 0 );
00097               return 1;
00098        }
00099 #endif
00100 
00101        if ( slap_schema_init( ) != 0 ) {
00102               slap_debug |= LDAP_DEBUG_NONE;
00103               Debug( LDAP_DEBUG_ANY,
00104                   "%s: slap_schema_init failed\n",
00105                   name, 0, 0 );
00106               return 1;
00107        }
00108 
00109        if ( filter_init() != 0 ) {
00110               slap_debug |= LDAP_DEBUG_NONE;
00111               Debug( LDAP_DEBUG_ANY,
00112                   "%s: filter_init failed\n",
00113                   name, 0, 0 );
00114               return 1;
00115        }
00116 
00117        if ( entry_init() != 0 ) {
00118               slap_debug |= LDAP_DEBUG_NONE;
00119               Debug( LDAP_DEBUG_ANY,
00120                   "%s: entry_init failed\n",
00121                   name, 0, 0 );
00122               return 1;
00123        }
00124 
00125        switch ( slapMode & SLAP_MODE ) {
00126        case SLAP_SERVER_MODE:
00127               root_dse_init();
00128 
00129               /* FALLTHRU */
00130        case SLAP_TOOL_MODE:
00131               Debug( LDAP_DEBUG_TRACE,
00132                      "%s init: initiated %s.\n", name,
00133                      (mode & SLAP_MODE) == SLAP_TOOL_MODE ? "tool" : "server",
00134                      0 );
00135 
00136               slap_name = name;
00137 
00138               ldap_pvt_thread_pool_init( &connection_pool,
00139                             connection_pool_max, 0);
00140 
00141               slap_counters_init( &slap_counters );
00142 
00143               ldap_pvt_thread_mutex_init( &slapd_rq.rq_mutex );
00144               LDAP_STAILQ_INIT( &slapd_rq.task_list );
00145               LDAP_STAILQ_INIT( &slapd_rq.run_list );
00146 
00147               slap_passwd_init();
00148 
00149               rc = slap_sasl_init();
00150 
00151               if( rc == 0 ) {
00152                      rc = backend_init( );
00153               }
00154               if ( rc )
00155                      return rc;
00156 
00157               break;
00158 
00159        default:
00160               slap_debug |= LDAP_DEBUG_NONE;
00161               Debug( LDAP_DEBUG_ANY,
00162                      "%s init: undefined mode (%d).\n", name, mode, 0 );
00163 
00164               rc = 1;
00165               break;
00166        }
00167 
00168        if ( slap_controls_init( ) != 0 ) {
00169               slap_debug |= LDAP_DEBUG_NONE;
00170               Debug( LDAP_DEBUG_ANY,
00171                   "%s: slap_controls_init failed\n",
00172                   name, 0, 0 );
00173               return 1;
00174        }
00175 
00176        if ( frontend_init() ) {
00177               slap_debug |= LDAP_DEBUG_NONE;
00178               Debug( LDAP_DEBUG_ANY,
00179                   "%s: frontend_init failed\n",
00180                   name, 0, 0 );
00181               return 1;
00182        }
00183 
00184        if ( overlay_init() ) {
00185               slap_debug |= LDAP_DEBUG_NONE;
00186               Debug( LDAP_DEBUG_ANY,
00187                   "%s: overlay_init failed\n",
00188                   name, 0, 0 );
00189               return 1;
00190        }
00191 
00192        if ( glue_sub_init() ) {
00193               slap_debug |= LDAP_DEBUG_NONE;
00194               Debug( LDAP_DEBUG_ANY,
00195                   "%s: glue/subordinate init failed\n",
00196                   name, 0, 0 );
00197 
00198               return 1;
00199        }
00200 
00201        if ( acl_init() ) {
00202               slap_debug |= LDAP_DEBUG_NONE;
00203               Debug( LDAP_DEBUG_ANY,
00204                   "%s: acl_init failed\n",
00205                   name, 0, 0 );
00206               return 1;
00207        }
00208 
00209        return rc;
00210 }
00211 
00212 int slap_startup( Backend *be )
00213 {
00214        Debug( LDAP_DEBUG_TRACE,
00215               "%s startup: initiated.\n",
00216               slap_name, 0, 0 );
00217 
00218 
00219        return backend_startup( be );
00220 }
00221 
00222 int slap_shutdown( Backend *be )
00223 {
00224        Debug( LDAP_DEBUG_TRACE,
00225               "%s shutdown: initiated\n",
00226               slap_name, 0, 0 );
00227 
00228        /* let backends do whatever cleanup they need to do */
00229        return backend_shutdown( be ); 
00230 }
00231 
00232 int slap_destroy(void)
00233 {
00234        int rc;
00235 
00236        Debug( LDAP_DEBUG_TRACE,
00237               "%s destroy: freeing system resources.\n",
00238               slap_name, 0, 0 );
00239 
00240        if ( default_referral ) {
00241               ber_bvarray_free( default_referral );
00242        }
00243 
00244        /* clear out any thread-keys for the main thread */
00245        ldap_pvt_thread_pool_context_reset( ldap_pvt_thread_pool_context());
00246 
00247        rc = backend_destroy();
00248 
00249        slap_sasl_destroy();
00250 
00251        /* rootdse destroy goes before entry_destroy()
00252         * because it may use entry_free() */
00253        root_dse_destroy();
00254        entry_destroy();
00255 
00256        switch ( slapMode & SLAP_MODE ) {
00257        case SLAP_SERVER_MODE:
00258        case SLAP_TOOL_MODE:
00259               slap_counters_destroy( &slap_counters );
00260               break;
00261 
00262        default:
00263               Debug( LDAP_DEBUG_ANY,
00264                      "slap_destroy(): undefined mode (%d).\n", slapMode, 0, 0 );
00265 
00266               rc = 1;
00267               break;
00268 
00269        }
00270 
00271        slap_op_destroy();
00272 
00273        ldap_pvt_thread_destroy();
00274 
00275        /* should destroy the above mutex */
00276        return rc;
00277 }
00278 
00279 void slap_counters_init( slap_counters_t *sc )
00280 {
00281        int i;
00282 
00283        ldap_pvt_thread_mutex_init( &sc->sc_mutex );
00284        ldap_pvt_mp_init( sc->sc_bytes );
00285        ldap_pvt_mp_init( sc->sc_pdu );
00286        ldap_pvt_mp_init( sc->sc_entries );
00287        ldap_pvt_mp_init( sc->sc_refs );
00288 
00289        ldap_pvt_mp_init( sc->sc_ops_initiated );
00290        ldap_pvt_mp_init( sc->sc_ops_completed );
00291 
00292 #ifdef SLAPD_MONITOR
00293        for ( i = 0; i < SLAP_OP_LAST; i++ ) {
00294               ldap_pvt_mp_init( sc->sc_ops_initiated_[ i ] );
00295               ldap_pvt_mp_init( sc->sc_ops_completed_[ i ] );
00296        }
00297 #endif /* SLAPD_MONITOR */
00298 }
00299 
00300 void slap_counters_destroy( slap_counters_t *sc )
00301 {
00302        int i;
00303 
00304        ldap_pvt_thread_mutex_destroy( &sc->sc_mutex );
00305        ldap_pvt_mp_clear( sc->sc_bytes );
00306        ldap_pvt_mp_clear( sc->sc_pdu );
00307        ldap_pvt_mp_clear( sc->sc_entries );
00308        ldap_pvt_mp_clear( sc->sc_refs );
00309 
00310        ldap_pvt_mp_clear( sc->sc_ops_initiated );
00311        ldap_pvt_mp_clear( sc->sc_ops_completed );
00312 
00313 #ifdef SLAPD_MONITOR
00314        for ( i = 0; i < SLAP_OP_LAST; i++ ) {
00315               ldap_pvt_mp_clear( sc->sc_ops_initiated_[ i ] );
00316               ldap_pvt_mp_clear( sc->sc_ops_completed_[ i ] );
00317        }
00318 #endif /* SLAPD_MONITOR */
00319 }
00320