1 /* init.c - initialize various things */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2010 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;
64 ldap_pvt_thread_mutex_t gmtime_mutex;
66 slap_counters_t slap_counters, *slap_counters_list;
68 static const char* slap_name = NULL;
69 int slapMode = SLAP_UNDEFINED_MODE;
72 slap_init( int mode, const char *name )
78 if ( slapMode != SLAP_UNDEFINED_MODE ) {
79 /* Make sure we write something to stderr */
80 slap_debug |= LDAP_DEBUG_NONE;
81 Debug( LDAP_DEBUG_ANY,
82 "%s init: init called twice (old=%d, new=%d)\n",
83 name, slapMode, mode );
93 if ( module_init() != 0 ) {
94 slap_debug |= LDAP_DEBUG_NONE;
95 Debug( LDAP_DEBUG_ANY,
96 "%s: module_init failed\n",
102 if ( slap_schema_init( ) != 0 ) {
103 slap_debug |= LDAP_DEBUG_NONE;
104 Debug( LDAP_DEBUG_ANY,
105 "%s: slap_schema_init failed\n",
110 if ( filter_init() != 0 ) {
111 slap_debug |= LDAP_DEBUG_NONE;
112 Debug( LDAP_DEBUG_ANY,
113 "%s: filter_init failed\n",
118 if ( entry_init() != 0 ) {
119 slap_debug |= LDAP_DEBUG_NONE;
120 Debug( LDAP_DEBUG_ANY,
121 "%s: entry_init failed\n",
126 switch ( slapMode & SLAP_MODE ) {
127 case SLAP_SERVER_MODE:
132 Debug( LDAP_DEBUG_TRACE,
133 "%s init: initiated %s.\n", name,
134 (mode & SLAP_MODE) == SLAP_TOOL_MODE ? "tool" : "server",
139 ldap_pvt_thread_pool_init( &connection_pool,
140 connection_pool_max, 0);
142 slap_counters_init( &slap_counters );
144 ldap_pvt_thread_mutex_init( &slapd_rq.rq_mutex );
145 LDAP_STAILQ_INIT( &slapd_rq.task_list );
146 LDAP_STAILQ_INIT( &slapd_rq.run_list );
148 ldap_pvt_thread_mutex_init( &gmtime_mutex );
151 rc = slap_sasl_init();
154 rc = backend_init( );
162 slap_debug |= LDAP_DEBUG_NONE;
163 Debug( LDAP_DEBUG_ANY,
164 "%s init: undefined mode (%d).\n", name, mode, 0 );
170 if ( slap_controls_init( ) != 0 ) {
171 slap_debug |= LDAP_DEBUG_NONE;
172 Debug( LDAP_DEBUG_ANY,
173 "%s: slap_controls_init failed\n",
178 if ( frontend_init() ) {
179 slap_debug |= LDAP_DEBUG_NONE;
180 Debug( LDAP_DEBUG_ANY,
181 "%s: frontend_init failed\n",
186 if ( overlay_init() ) {
187 slap_debug |= LDAP_DEBUG_NONE;
188 Debug( LDAP_DEBUG_ANY,
189 "%s: overlay_init failed\n",
194 if ( glue_sub_init() ) {
195 slap_debug |= LDAP_DEBUG_NONE;
196 Debug( LDAP_DEBUG_ANY,
197 "%s: glue/subordinate init failed\n",
204 slap_debug |= LDAP_DEBUG_NONE;
205 Debug( LDAP_DEBUG_ANY,
206 "%s: acl_init failed\n",
214 int slap_startup( Backend *be )
216 Debug( LDAP_DEBUG_TRACE,
217 "%s startup: initiated.\n",
221 return backend_startup( be );
224 int slap_shutdown( Backend *be )
226 Debug( LDAP_DEBUG_TRACE,
227 "%s shutdown: initiated\n",
230 /* let backends do whatever cleanup they need to do */
231 return backend_shutdown( be );
234 int slap_destroy(void)
238 Debug( LDAP_DEBUG_TRACE,
239 "%s destroy: freeing system resources.\n",
242 if ( default_referral ) {
243 ber_bvarray_free( default_referral );
246 /* clear out any thread-keys for the main thread */
247 ldap_pvt_thread_pool_context_reset( ldap_pvt_thread_pool_context());
249 rc = backend_destroy();
253 /* rootdse destroy goes before entry_destroy()
254 * because it may use entry_free() */
258 switch ( slapMode & SLAP_MODE ) {
259 case SLAP_SERVER_MODE:
261 slap_counters_destroy( &slap_counters );
265 Debug( LDAP_DEBUG_ANY,
266 "slap_destroy(): undefined mode (%d).\n", slapMode, 0, 0 );
275 ldap_pvt_thread_destroy();
277 /* should destroy the above mutex */
281 void slap_counters_init( slap_counters_t *sc )
285 ldap_pvt_thread_mutex_init( &sc->sc_mutex );
286 ldap_pvt_mp_init( sc->sc_bytes );
287 ldap_pvt_mp_init( sc->sc_pdu );
288 ldap_pvt_mp_init( sc->sc_entries );
289 ldap_pvt_mp_init( sc->sc_refs );
291 ldap_pvt_mp_init( sc->sc_ops_initiated );
292 ldap_pvt_mp_init( sc->sc_ops_completed );
295 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
296 ldap_pvt_mp_init( sc->sc_ops_initiated_[ i ] );
297 ldap_pvt_mp_init( sc->sc_ops_completed_[ i ] );
299 #endif /* SLAPD_MONITOR */
302 void slap_counters_destroy( slap_counters_t *sc )
306 ldap_pvt_thread_mutex_destroy( &sc->sc_mutex );
307 ldap_pvt_mp_clear( sc->sc_bytes );
308 ldap_pvt_mp_clear( sc->sc_pdu );
309 ldap_pvt_mp_clear( sc->sc_entries );
310 ldap_pvt_mp_clear( sc->sc_refs );
312 ldap_pvt_mp_clear( sc->sc_ops_initiated );
313 ldap_pvt_mp_clear( sc->sc_ops_completed );
316 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
317 ldap_pvt_mp_clear( sc->sc_ops_initiated_[ i ] );
318 ldap_pvt_mp_clear( sc->sc_ops_completed_[ i ] );
320 #endif /* SLAPD_MONITOR */