1 /* init.c - initialize various things */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2011 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>
41 * read-only global variables or variables only written by the listener
42 * thread (after they are initialized) - no need to protect them with a mutex.
47 int ldap_syslog = LDAP_DEBUG_STATS;
53 int ldap_syslog_level = LOG_DEBUG;
56 BerVarray default_referral = NULL;
59 * global variables that need mutex protection
61 ldap_pvt_thread_pool_t connection_pool;
62 int connection_pool_max = SLAP_MAX_WORKER_THREADS;
63 int slap_tool_thread_max = 1;
65 slap_counters_t slap_counters, *slap_counters_list;
67 static const char* slap_name = NULL;
68 int slapMode = SLAP_UNDEFINED_MODE;
71 slap_init( int mode, const char *name )
77 if ( slapMode != SLAP_UNDEFINED_MODE ) {
78 /* Make sure we write something to stderr */
79 slap_debug |= LDAP_DEBUG_NONE;
80 Debug( LDAP_DEBUG_ANY,
81 "%s init: init called twice (old=%d, new=%d)\n",
82 name, slapMode, mode );
92 if ( module_init() != 0 ) {
93 slap_debug |= LDAP_DEBUG_NONE;
94 Debug( LDAP_DEBUG_ANY,
95 "%s: module_init failed\n",
101 if ( slap_schema_init( ) != 0 ) {
102 slap_debug |= LDAP_DEBUG_NONE;
103 Debug( LDAP_DEBUG_ANY,
104 "%s: slap_schema_init failed\n",
109 if ( filter_init() != 0 ) {
110 slap_debug |= LDAP_DEBUG_NONE;
111 Debug( LDAP_DEBUG_ANY,
112 "%s: filter_init failed\n",
117 if ( entry_init() != 0 ) {
118 slap_debug |= LDAP_DEBUG_NONE;
119 Debug( LDAP_DEBUG_ANY,
120 "%s: entry_init failed\n",
125 switch ( slapMode & SLAP_MODE ) {
126 case SLAP_SERVER_MODE:
131 Debug( LDAP_DEBUG_TRACE,
132 "%s init: initiated %s.\n", name,
133 (mode & SLAP_MODE) == SLAP_TOOL_MODE ? "tool" : "server",
138 ldap_pvt_thread_pool_init( &connection_pool,
139 connection_pool_max, 0);
141 slap_counters_init( &slap_counters );
143 ldap_pvt_thread_mutex_init( &slapd_rq.rq_mutex );
144 LDAP_STAILQ_INIT( &slapd_rq.task_list );
145 LDAP_STAILQ_INIT( &slapd_rq.run_list );
149 rc = slap_sasl_init();
152 rc = backend_init( );
160 slap_debug |= LDAP_DEBUG_NONE;
161 Debug( LDAP_DEBUG_ANY,
162 "%s init: undefined mode (%d).\n", name, mode, 0 );
168 if ( slap_controls_init( ) != 0 ) {
169 slap_debug |= LDAP_DEBUG_NONE;
170 Debug( LDAP_DEBUG_ANY,
171 "%s: slap_controls_init failed\n",
176 if ( frontend_init() ) {
177 slap_debug |= LDAP_DEBUG_NONE;
178 Debug( LDAP_DEBUG_ANY,
179 "%s: frontend_init failed\n",
184 if ( overlay_init() ) {
185 slap_debug |= LDAP_DEBUG_NONE;
186 Debug( LDAP_DEBUG_ANY,
187 "%s: overlay_init failed\n",
192 if ( glue_sub_init() ) {
193 slap_debug |= LDAP_DEBUG_NONE;
194 Debug( LDAP_DEBUG_ANY,
195 "%s: glue/subordinate init failed\n",
202 slap_debug |= LDAP_DEBUG_NONE;
203 Debug( LDAP_DEBUG_ANY,
204 "%s: acl_init failed\n",
212 int slap_startup( Backend *be )
214 Debug( LDAP_DEBUG_TRACE,
215 "%s startup: initiated.\n",
219 return backend_startup( be );
222 int slap_shutdown( Backend *be )
224 Debug( LDAP_DEBUG_TRACE,
225 "%s shutdown: initiated\n",
228 /* let backends do whatever cleanup they need to do */
229 return backend_shutdown( be );
232 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 /* clear out any thread-keys for the main thread */
245 ldap_pvt_thread_pool_context_reset( ldap_pvt_thread_pool_context());
247 rc = backend_destroy();
251 /* rootdse destroy goes before entry_destroy()
252 * because it may use entry_free() */
256 switch ( slapMode & SLAP_MODE ) {
257 case SLAP_SERVER_MODE:
259 slap_counters_destroy( &slap_counters );
263 Debug( LDAP_DEBUG_ANY,
264 "slap_destroy(): undefined mode (%d).\n", slapMode, 0, 0 );
273 ldap_pvt_thread_destroy();
275 /* should destroy the above mutex */
279 void slap_counters_init( slap_counters_t *sc )
283 ldap_pvt_thread_mutex_init( &sc->sc_mutex );
284 ldap_pvt_mp_init( sc->sc_bytes );
285 ldap_pvt_mp_init( sc->sc_pdu );
286 ldap_pvt_mp_init( sc->sc_entries );
287 ldap_pvt_mp_init( sc->sc_refs );
289 ldap_pvt_mp_init( sc->sc_ops_initiated );
290 ldap_pvt_mp_init( sc->sc_ops_completed );
293 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
294 ldap_pvt_mp_init( sc->sc_ops_initiated_[ i ] );
295 ldap_pvt_mp_init( sc->sc_ops_completed_[ i ] );
297 #endif /* SLAPD_MONITOR */
300 void slap_counters_destroy( slap_counters_t *sc )
304 ldap_pvt_thread_mutex_destroy( &sc->sc_mutex );
305 ldap_pvt_mp_clear( sc->sc_bytes );
306 ldap_pvt_mp_clear( sc->sc_pdu );
307 ldap_pvt_mp_clear( sc->sc_entries );
308 ldap_pvt_mp_clear( sc->sc_refs );
310 ldap_pvt_mp_clear( sc->sc_ops_initiated );
311 ldap_pvt_mp_clear( sc->sc_ops_completed );
314 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
315 ldap_pvt_mp_clear( sc->sc_ops_initiated_[ i ] );
316 ldap_pvt_mp_clear( sc->sc_ops_completed_[ i ] );
318 #endif /* SLAPD_MONITOR */