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;
69 ldap_pvt_thread_mutex_t gmtime_mutex;
72 slap_counters_t slap_counters;
74 ldap_pvt_thread_mutex_t replog_mutex;
76 static const char* slap_name = NULL;
77 int slapMode = SLAP_UNDEFINED_MODE;
80 slap_init( int mode, const char *name )
87 if ( slapMode != SLAP_UNDEFINED_MODE ) {
88 /* Make sure we write something to stderr */
89 slap_debug |= LDAP_DEBUG_NONE;
90 Debug( LDAP_DEBUG_ANY,
91 "%s init: init called twice (old=%d, new=%d)\n",
92 name, slapMode, mode );
100 if ( module_init() != 0 ) {
101 slap_debug |= LDAP_DEBUG_NONE;
102 Debug( LDAP_DEBUG_ANY,
103 "%s: module_init failed\n",
109 if ( slap_schema_init( ) != 0 ) {
110 slap_debug |= LDAP_DEBUG_NONE;
111 Debug( LDAP_DEBUG_ANY,
112 "%s: slap_schema_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);
140 ldap_pvt_thread_mutex_init( &replog_mutex );
142 ldap_pvt_thread_mutex_init( &slap_counters.sc_sent_mutex );
143 ldap_pvt_thread_mutex_init( &slap_counters.sc_ops_mutex );
144 ldap_pvt_mp_init( slap_counters.sc_bytes );
145 ldap_pvt_mp_init( slap_counters.sc_pdu );
146 ldap_pvt_mp_init( slap_counters.sc_entries );
147 ldap_pvt_mp_init( slap_counters.sc_refs );
149 ldap_pvt_mp_init( slap_counters.sc_ops_initiated );
150 ldap_pvt_mp_init( slap_counters.sc_ops_completed );
152 ldap_pvt_thread_mutex_init( &slapd_rq.rq_mutex );
153 LDAP_STAILQ_INIT( &slapd_rq.task_list );
154 LDAP_STAILQ_INIT( &slapd_rq.run_list );
157 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
158 ldap_pvt_mp_init( slap_counters.sc_ops_initiated_[ i ] );
159 ldap_pvt_mp_init( slap_counters.sc_ops_completed_[ i ] );
161 #endif /* SLAPD_MONITOR */
163 #ifndef HAVE_GMTIME_R
164 ldap_pvt_thread_mutex_init( &gmtime_mutex );
168 rc = slap_sasl_init();
171 rc = backend_init( );
179 slap_debug |= LDAP_DEBUG_NONE;
180 Debug( LDAP_DEBUG_ANY,
181 "%s init: undefined mode (%d).\n", name, mode, 0 );
187 if ( slap_controls_init( ) != 0 ) {
188 slap_debug |= LDAP_DEBUG_NONE;
189 Debug( LDAP_DEBUG_ANY,
190 "%s: slap_controls_init failed\n",
196 /* Library defaults to full certificate checking. This is correct when
197 * a client is verifying a server because all servers should have a
198 * valid cert. But few clients have valid certs, so we want our default
199 * to be no checking. The config file can override this as usual.
202 (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
205 if ( frontend_init() ) {
206 slap_debug |= LDAP_DEBUG_NONE;
207 Debug( LDAP_DEBUG_ANY,
208 "%s: frontend_init failed\n",
213 if ( overlay_init() ) {
214 slap_debug |= LDAP_DEBUG_NONE;
215 Debug( LDAP_DEBUG_ANY,
216 "%s: overlay_init failed\n",
221 if ( glue_sub_init() ) {
222 slap_debug |= LDAP_DEBUG_NONE;
223 Debug( LDAP_DEBUG_ANY,
224 "%s: glue/subordinate init failed\n",
231 slap_debug |= LDAP_DEBUG_NONE;
232 Debug( LDAP_DEBUG_ANY,
233 "%s: acl_init failed\n",
241 int slap_startup( Backend *be )
243 Debug( LDAP_DEBUG_TRACE,
244 "%s startup: initiated.\n",
248 return backend_startup( be );
251 int slap_shutdown( Backend *be )
253 Debug( LDAP_DEBUG_TRACE,
254 "%s shutdown: initiated\n",
257 /* let backends do whatever cleanup they need to do */
258 return backend_shutdown( be );
261 int slap_destroy(void)
266 Debug( LDAP_DEBUG_TRACE,
267 "%s destroy: freeing system resources.\n",
270 if ( default_referral ) {
271 ber_bvarray_free( default_referral );
274 /* clear out any thread-keys for the main thread */
275 ldap_pvt_thread_pool_context_reset( ldap_pvt_thread_pool_context());
277 rc = backend_destroy();
281 /* rootdse destroy goes before entry_destroy()
282 * because it may use entry_free() */
286 switch ( slapMode & SLAP_MODE ) {
287 case SLAP_SERVER_MODE:
291 ldap_pvt_thread_mutex_destroy( &slap_counters.sc_sent_mutex );
292 ldap_pvt_thread_mutex_destroy( &slap_counters.sc_ops_mutex );
293 ldap_pvt_mp_clear( slap_counters.sc_bytes );
294 ldap_pvt_mp_clear( slap_counters.sc_pdu );
295 ldap_pvt_mp_clear( slap_counters.sc_entries );
296 ldap_pvt_mp_clear( slap_counters.sc_refs );
297 ldap_pvt_mp_clear( slap_counters.sc_ops_initiated );
298 ldap_pvt_mp_clear( slap_counters.sc_ops_completed );
301 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
302 ldap_pvt_mp_clear( slap_counters.sc_ops_initiated_[ i ] );
303 ldap_pvt_mp_clear( slap_counters.sc_ops_completed_[ i ] );
305 #endif /* SLAPD_MONITOR */
309 Debug( LDAP_DEBUG_ANY,
310 "slap_destroy(): undefined mode (%d).\n", slapMode, 0, 0 );
317 ldap_pvt_thread_destroy();
319 /* should destory the above mutex */