1 /* init.c - initialize various things */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
31 #include <ac/socket.h>
32 #include <ac/string.h>
38 #include "slapi/slapi.h"
42 * read-only global variables or variables only written by the listener
43 * thread (after they are initialized) - no need to protect them with a mutex.
47 struct berval AllUser = BER_BVC( LDAP_ALL_USER_ATTRIBUTES );
48 struct berval AllOper = BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES );
49 struct berval NoAttrs = BER_BVC( LDAP_NO_ATTRS );
52 * global variables that need mutex protection
55 int ldap_syslog = LDAP_DEBUG_STATS;
61 int ldap_syslog_level = LOG_DEBUG;
63 int ldap_syslog_level;
67 * these mutexes must be used when calling the entry2str()
68 * routine since it returns a pointer to static data.
70 static const char* slap_name = NULL;
71 int slapMode = SLAP_UNDEFINED_MODE;
74 slap_init( int mode, const char *name )
81 if ( slapMode != SLAP_UNDEFINED_MODE ) {
82 Debug( LDAP_DEBUG_ANY,
83 "%s init: init called twice (old=%d, new=%d)\n",
84 name, slapMode, mode );
91 switch ( slapMode & SLAP_MODE ) {
92 case SLAP_SERVER_MODE:
94 Debug( LDAP_DEBUG_TRACE,
95 "%s init: initiated %s.\n", name,
96 (mode & SLAP_MODE) == SLAP_TOOL_MODE ? "tool" : "server",
102 ldap_pvt_thread_pool_init( &SLAPD_GLOBAL(connection_pool),
103 SLAPD_GLOBAL(connection_pool_max), 0);
105 ldap_pvt_thread_mutex_init( &SLAPD_GLOBAL(entry2str_mutex) );
106 ldap_pvt_thread_mutex_init( &SLAPD_GLOBAL(replog_mutex) );
108 ldap_pvt_thread_mutex_init( &SLAPD_GLOBAL(counters).sc_sent_mutex );
109 ldap_pvt_thread_mutex_init( &SLAPD_GLOBAL(counters).sc_ops_mutex );
110 ldap_pvt_mp_init( SLAPD_GLOBAL(counters).sc_bytes );
111 ldap_pvt_mp_init( SLAPD_GLOBAL(counters).sc_pdu );
112 ldap_pvt_mp_init( SLAPD_GLOBAL(counters).sc_entries );
113 ldap_pvt_mp_init( SLAPD_GLOBAL(counters).sc_refs );
115 ldap_pvt_mp_init( SLAPD_GLOBAL(counters).sc_ops_completed );
116 ldap_pvt_mp_init( SLAPD_GLOBAL(counters).sc_ops_initiated );
119 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
120 ldap_pvt_mp_init( SLAPD_GLOBAL(counters).sc_ops_initiated_[ i ] );
121 ldap_pvt_mp_init( SLAPD_GLOBAL(counters).sc_ops_completed_[ i ] );
123 #endif /* SLAPD_MONITOR */
125 #ifndef HAVE_GMTIME_R
126 ldap_pvt_thread_mutex_init( &SLAPD_GLOBAL(gmtime_mutex) );
128 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
129 ldap_pvt_thread_mutex_init( &passwd_mutex );
132 rc = slap_sasl_init();
135 rc = backend_init( );
141 Debug( LDAP_DEBUG_ANY,
142 "%s init: undefined mode (%d).\n", name, mode, 0 );
151 int slap_startup( Backend *be )
155 Debug( LDAP_DEBUG_TRACE,
156 "%s startup: initiated.\n",
160 rc = backend_startup( be );
164 Slapi_PBlock *pb = slapi_pblock_new();
166 if ( slapi_int_call_plugins( NULL, SLAPI_PLUGIN_START_FN, pb ) < 0 ) {
169 slapi_pblock_destroy( pb );
171 #endif /* LDAP_SLAPI */
176 int slap_shutdown( Backend *be )
183 Debug( LDAP_DEBUG_TRACE,
184 "%s shutdown: initiated\n",
187 /* let backends do whatever cleanup they need to do */
188 rc = backend_shutdown( be );
191 pb = slapi_pblock_new();
192 (void) slapi_int_call_plugins( NULL, SLAPI_PLUGIN_CLOSE_FN, pb );
193 slapi_pblock_destroy( pb );
194 #endif /* LDAP_SLAPI */
199 int slap_destroy(void)
204 Debug( LDAP_DEBUG_TRACE,
205 "%s destroy: freeing system resources.\n",
208 if ( SLAPD_GLOBAL(default_referral) ) {
209 ber_bvarray_free( SLAPD_GLOBAL(default_referral) );
212 rc = backend_destroy();
218 switch ( slapMode & SLAP_MODE ) {
219 case SLAP_SERVER_MODE:
222 ldap_pvt_thread_mutex_destroy( &SLAPD_GLOBAL(counters).sc_sent_mutex );
223 ldap_pvt_thread_mutex_destroy( &SLAPD_GLOBAL(counters).sc_ops_mutex );
224 ldap_pvt_mp_clear( SLAPD_GLOBAL(counters).sc_bytes );
225 ldap_pvt_mp_clear( SLAPD_GLOBAL(counters).sc_pdu );
226 ldap_pvt_mp_clear( SLAPD_GLOBAL(counters).sc_entries );
227 ldap_pvt_mp_clear( SLAPD_GLOBAL(counters).sc_refs );
228 ldap_pvt_mp_clear( SLAPD_GLOBAL(counters).sc_ops_completed );
229 ldap_pvt_mp_clear( SLAPD_GLOBAL(counters).sc_ops_initiated );
232 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
233 ldap_pvt_mp_clear( SLAPD_GLOBAL(counters).sc_ops_initiated_[ i ] );
234 ldap_pvt_mp_clear( SLAPD_GLOBAL(counters).sc_ops_completed_[ i ] );
236 #endif /* SLAPD_MONITOR */
240 Debug( LDAP_DEBUG_ANY,
241 "slap_destroy(): undefined mode (%d).\n", slapMode, 0, 0 );
248 ldap_pvt_thread_destroy();
250 /* should destory the above mutex */