1 /* init.c - initialize various things */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2007 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;
58 struct berval AllUser = BER_BVC( LDAP_ALL_USER_ATTRIBUTES );
59 struct berval AllOper = BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES );
60 struct berval NoAttrs = BER_BVC( LDAP_NO_ATTRS );
63 * global variables that need mutex protection
65 ldap_pvt_thread_pool_t connection_pool;
66 int connection_pool_max = SLAP_MAX_WORKER_THREADS;
67 int slap_tool_thread_max = 1;
68 ldap_pvt_thread_mutex_t gmtime_mutex;
70 slap_counters_t slap_counters, *slap_counters_list;
72 static const char* slap_name = NULL;
73 int slapMode = SLAP_UNDEFINED_MODE;
76 slap_init( int mode, const char *name )
82 if ( slapMode != SLAP_UNDEFINED_MODE ) {
83 /* Make sure we write something to stderr */
84 slap_debug |= LDAP_DEBUG_NONE;
85 Debug( LDAP_DEBUG_ANY,
86 "%s init: init called twice (old=%d, new=%d)\n",
87 name, slapMode, mode );
97 if ( module_init() != 0 ) {
98 slap_debug |= LDAP_DEBUG_NONE;
99 Debug( LDAP_DEBUG_ANY,
100 "%s: module_init failed\n",
106 if ( slap_schema_init( ) != 0 ) {
107 slap_debug |= LDAP_DEBUG_NONE;
108 Debug( LDAP_DEBUG_ANY,
109 "%s: slap_schema_init failed\n",
114 if ( filter_init() != 0 ) {
115 slap_debug |= LDAP_DEBUG_NONE;
116 Debug( LDAP_DEBUG_ANY,
117 "%s: filter_init failed\n",
122 if ( entry_init() != 0 ) {
123 slap_debug |= LDAP_DEBUG_NONE;
124 Debug( LDAP_DEBUG_ANY,
125 "%s: entry_init failed\n",
130 switch ( slapMode & SLAP_MODE ) {
131 case SLAP_SERVER_MODE:
136 Debug( LDAP_DEBUG_TRACE,
137 "%s init: initiated %s.\n", name,
138 (mode & SLAP_MODE) == SLAP_TOOL_MODE ? "tool" : "server",
143 ldap_pvt_thread_pool_init( &connection_pool,
144 connection_pool_max, 0);
146 slap_counters_init( &slap_counters );
148 ldap_pvt_thread_mutex_init( &slapd_rq.rq_mutex );
149 LDAP_STAILQ_INIT( &slapd_rq.task_list );
150 LDAP_STAILQ_INIT( &slapd_rq.run_list );
152 ldap_pvt_thread_mutex_init( &gmtime_mutex );
155 rc = slap_sasl_init();
158 rc = backend_init( );
166 slap_debug |= LDAP_DEBUG_NONE;
167 Debug( LDAP_DEBUG_ANY,
168 "%s init: undefined mode (%d).\n", name, mode, 0 );
174 if ( slap_controls_init( ) != 0 ) {
175 slap_debug |= LDAP_DEBUG_NONE;
176 Debug( LDAP_DEBUG_ANY,
177 "%s: slap_controls_init failed\n",
183 /* Library defaults to full certificate checking. This is correct when
184 * a client is verifying a server because all servers should have a
185 * valid cert. But few clients have valid certs, so we want our default
186 * to be no checking. The config file can override this as usual.
189 (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
192 if ( frontend_init() ) {
193 slap_debug |= LDAP_DEBUG_NONE;
194 Debug( LDAP_DEBUG_ANY,
195 "%s: frontend_init failed\n",
200 if ( overlay_init() ) {
201 slap_debug |= LDAP_DEBUG_NONE;
202 Debug( LDAP_DEBUG_ANY,
203 "%s: overlay_init failed\n",
208 if ( glue_sub_init() ) {
209 slap_debug |= LDAP_DEBUG_NONE;
210 Debug( LDAP_DEBUG_ANY,
211 "%s: glue/subordinate init failed\n",
218 slap_debug |= LDAP_DEBUG_NONE;
219 Debug( LDAP_DEBUG_ANY,
220 "%s: acl_init failed\n",
228 int slap_startup( Backend *be )
230 Debug( LDAP_DEBUG_TRACE,
231 "%s startup: initiated.\n",
235 return backend_startup( be );
238 int slap_shutdown( Backend *be )
240 Debug( LDAP_DEBUG_TRACE,
241 "%s shutdown: initiated\n",
244 /* let backends do whatever cleanup they need to do */
245 return backend_shutdown( be );
248 int slap_destroy(void)
252 Debug( LDAP_DEBUG_TRACE,
253 "%s destroy: freeing system resources.\n",
256 if ( default_referral ) {
257 ber_bvarray_free( default_referral );
260 /* clear out any thread-keys for the main thread */
261 ldap_pvt_thread_pool_context_reset( ldap_pvt_thread_pool_context());
263 rc = backend_destroy();
267 /* rootdse destroy goes before entry_destroy()
268 * because it may use entry_free() */
272 switch ( slapMode & SLAP_MODE ) {
273 case SLAP_SERVER_MODE:
275 slap_counters_destroy( &slap_counters );
279 Debug( LDAP_DEBUG_ANY,
280 "slap_destroy(): undefined mode (%d).\n", slapMode, 0, 0 );
289 ldap_pvt_thread_destroy();
291 /* should destroy the above mutex */
295 void slap_counters_init( slap_counters_t *sc )
299 ldap_pvt_thread_mutex_init( &sc->sc_mutex );
300 ldap_pvt_mp_init( sc->sc_bytes );
301 ldap_pvt_mp_init( sc->sc_pdu );
302 ldap_pvt_mp_init( sc->sc_entries );
303 ldap_pvt_mp_init( sc->sc_refs );
305 ldap_pvt_mp_init( sc->sc_ops_initiated );
306 ldap_pvt_mp_init( sc->sc_ops_completed );
309 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
310 ldap_pvt_mp_init( sc->sc_ops_initiated_[ i ] );
311 ldap_pvt_mp_init( sc->sc_ops_completed_[ i ] );
313 #endif /* SLAPD_MONITOR */
316 void slap_counters_destroy( slap_counters_t *sc )
320 ldap_pvt_thread_mutex_destroy( &sc->sc_mutex );
321 ldap_pvt_mp_clear( sc->sc_bytes );
322 ldap_pvt_mp_clear( sc->sc_pdu );
323 ldap_pvt_mp_clear( sc->sc_entries );
324 ldap_pvt_mp_clear( sc->sc_refs );
326 ldap_pvt_mp_clear( sc->sc_ops_initiated );
327 ldap_pvt_mp_clear( sc->sc_ops_completed );
330 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
331 ldap_pvt_mp_clear( sc->sc_ops_initiated_[ i ] );
332 ldap_pvt_mp_clear( sc->sc_ops_completed_[ i ] );
334 #endif /* SLAPD_MONITOR */