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.
48 int ldap_syslog = LDAP_DEBUG_STATS;
54 int ldap_syslog_level = LOG_DEBUG;
57 BerVarray default_referral = NULL;
59 struct berval AllUser = BER_BVC( LDAP_ALL_USER_ATTRIBUTES );
60 struct berval AllOper = BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES );
61 struct berval NoAttrs = BER_BVC( LDAP_NO_ATTRS );
64 * global variables that need mutex protection
66 ldap_pvt_thread_pool_t connection_pool;
67 int connection_pool_max = SLAP_MAX_WORKER_THREADS;
69 ldap_pvt_thread_mutex_t gmtime_mutex;
71 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
72 ldap_pvt_thread_mutex_t passwd_mutex;
75 slap_counters_t slap_counters;
78 * these mutexes must be used when calling the entry2str()
79 * routine since it returns a pointer to static data.
81 ldap_pvt_thread_mutex_t entry2str_mutex;
82 ldap_pvt_thread_mutex_t replog_mutex;
84 static const char* slap_name = NULL;
85 int slapMode = SLAP_UNDEFINED_MODE;
88 slap_init( int mode, const char *name )
95 if ( slapMode != SLAP_UNDEFINED_MODE ) {
96 Debug( LDAP_DEBUG_ANY,
97 "%s init: init called twice (old=%d, new=%d)\n",
98 name, slapMode, mode );
105 switch ( slapMode & SLAP_MODE ) {
106 case SLAP_SERVER_MODE:
108 Debug( LDAP_DEBUG_TRACE,
109 "%s init: initiated %s.\n", name,
110 (mode & SLAP_MODE) == SLAP_TOOL_MODE ? "tool" : "server",
116 (void) ldap_pvt_thread_initialize();
118 ldap_pvt_thread_pool_init( &connection_pool,
119 connection_pool_max, 0);
121 ldap_pvt_thread_mutex_init( &entry2str_mutex );
122 ldap_pvt_thread_mutex_init( &replog_mutex );
124 ldap_pvt_thread_mutex_init( &slap_counters.sc_sent_mutex );
125 ldap_pvt_thread_mutex_init( &slap_counters.sc_ops_mutex );
127 mpz_init( slap_counters.sc_bytes );
128 mpz_init( slap_counters.sc_pdu );
129 mpz_init( slap_counters.sc_entries );
130 mpz_init( slap_counters.sc_refs );
132 mpz_init( slap_counters.sc_ops_completed );
133 mpz_init( slap_counters.sc_ops_initiated );
136 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
137 mpz_init( slap_counters.sc_ops_initiated_[ i ] );
138 mpz_init( slap_counters.sc_ops_completed_[ i ] );
140 #endif /* SLAPD_MONITOR */
141 #else /* ! HAVE_GMP */
142 slap_counters.sc_bytes = 0;
143 slap_counters.sc_pdu = 0;
144 slap_counters.sc_entries = 0;
145 slap_counters.sc_refs = 0;
147 slap_counters.sc_ops_completed = 0;
148 slap_counters.sc_ops_initiated = 0;
150 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
151 slap_counters.sc_ops_initiated_[ i ] = 0;
152 slap_counters.sc_ops_completed_[ i ] = 0;
154 #endif /* SLAPD_MONITOR */
155 #endif /* ! HAVE_GMP */
157 #ifndef HAVE_GMTIME_R
158 ldap_pvt_thread_mutex_init( &gmtime_mutex );
160 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
161 ldap_pvt_thread_mutex_init( &passwd_mutex );
164 rc = slap_sasl_init();
167 rc = backend_init( );
173 Debug( LDAP_DEBUG_ANY,
174 "%s init: undefined mode (%d).\n", name, mode, 0 );
183 int slap_startup( Backend *be )
187 Debug( LDAP_DEBUG_TRACE,
188 "%s startup: initiated.\n",
192 rc = backend_startup( be );
196 Slapi_PBlock *pb = slapi_pblock_new();
198 if ( slapi_int_call_plugins( NULL, SLAPI_PLUGIN_START_FN, pb ) < 0 ) {
201 slapi_pblock_destroy( pb );
203 #endif /* LDAP_SLAPI */
208 int slap_shutdown( Backend *be )
215 Debug( LDAP_DEBUG_TRACE,
216 "%s shutdown: initiated\n",
219 /* let backends do whatever cleanup they need to do */
220 rc = backend_shutdown( be );
223 pb = slapi_pblock_new();
224 (void) slapi_int_call_plugins( NULL, SLAPI_PLUGIN_CLOSE_FN, pb );
225 slapi_pblock_destroy( pb );
226 #endif /* LDAP_SLAPI */
231 int slap_destroy(void)
236 Debug( LDAP_DEBUG_TRACE,
237 "%s destroy: freeing system resources.\n",
240 if ( default_referral ) {
241 ber_bvarray_free( default_referral );
244 rc = backend_destroy();
250 switch ( slapMode & SLAP_MODE ) {
251 case SLAP_SERVER_MODE:
254 ldap_pvt_thread_mutex_destroy( &slap_counters.sc_sent_mutex );
255 ldap_pvt_thread_mutex_destroy( &slap_counters.sc_ops_mutex );
257 mpz_clear( slap_counters.sc_bytes );
258 mpz_clear( slap_counters.sc_pdu );
259 mpz_clear( slap_counters.sc_entries );
260 mpz_clear( slap_counters.sc_refs );
261 mpz_clear( slap_counters.sc_ops_completed );
262 mpz_clear( slap_counters.sc_ops_initiated );
265 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
266 mpz_clear( slap_counters.sc_ops_initiated_[ i ] );
267 mpz_clear( slap_counters.sc_ops_completed_[ i ] );
269 #endif /* SLAPD_MONITOR */
270 #endif /* HAVE_GMP */
274 Debug( LDAP_DEBUG_ANY,
275 "slap_destroy(): undefined mode (%d).\n", slapMode, 0, 0 );
282 ldap_pvt_thread_destroy();
284 /* should destory the above mutex */