1 /* init.c - initialize various things */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 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"
44 * read-only global variables or variables only written by the listener
45 * thread (after they are initialized) - no need to protect them with a mutex.
50 int ldap_syslog = LDAP_DEBUG_STATS;
56 int ldap_syslog_level = LOG_DEBUG;
59 BerVarray default_referral = NULL;
61 struct berval AllUser = BER_BVC( LDAP_ALL_USER_ATTRIBUTES );
62 struct berval AllOper = BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES );
63 struct berval NoAttrs = BER_BVC( LDAP_NO_ATTRS );
66 * global variables that need mutex protection
68 ldap_pvt_thread_pool_t connection_pool;
69 int connection_pool_max = SLAP_MAX_WORKER_THREADS;
71 ldap_pvt_thread_mutex_t gmtime_mutex;
74 slap_counters_t slap_counters;
77 * these mutexes must be used when calling the entry2str()
78 * routine since it returns a pointer to static data.
80 ldap_pvt_thread_mutex_t entry2str_mutex;
81 ldap_pvt_thread_mutex_t replog_mutex;
83 static const char* slap_name = NULL;
84 int slapMode = SLAP_UNDEFINED_MODE;
87 slap_init( int mode, const char *name )
94 if ( slapMode != SLAP_UNDEFINED_MODE ) {
95 /* Make sure we write something to stderr */
97 Debug( LDAP_DEBUG_ANY,
98 "%s init: init called twice (old=%d, new=%d)\n",
99 name, slapMode, mode );
107 if ( module_init() != 0 ) {
109 Debug( LDAP_DEBUG_ANY,
110 "%s: module_init failed\n",
116 if ( slap_schema_init( ) != 0 ) {
118 Debug( LDAP_DEBUG_ANY,
119 "%s: slap_schema_init failed\n",
125 switch ( slapMode & SLAP_MODE ) {
126 case SLAP_SERVER_MODE:
127 ldap_pvt_thread_pool_init( &connection_pool,
128 connection_pool_max, 0);
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_mutex_init( &entry2str_mutex );
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( );
178 Debug( LDAP_DEBUG_ANY,
179 "%s init: undefined mode (%d).\n", name, mode, 0 );
185 if ( slap_controls_init( ) != 0 ) {
187 Debug( LDAP_DEBUG_ANY,
188 "%s: slap_controls_init failed\n",
194 /* Library defaults to full certificate checking. This is correct when
195 * a client is verifying a server because all servers should have a
196 * valid cert. But few clients have valid certs, so we want our default
197 * to be no checking. The config file can override this as usual.
200 (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
203 if ( frontend_init() ) {
205 Debug( LDAP_DEBUG_ANY,
206 "%s: frontend_init failed\n",
211 if ( overlay_init() ) {
213 Debug( LDAP_DEBUG_ANY,
214 "%s: overlay_init failed\n",
221 Debug( LDAP_DEBUG_ANY,
222 "%s: acl_init failed\n",
230 int slap_startup( Backend *be )
234 Debug( LDAP_DEBUG_TRACE,
235 "%s startup: initiated.\n",
239 rc = backend_startup( be );
243 Slapi_PBlock *pb = slapi_pblock_new();
245 if ( slapi_int_call_plugins( frontendDB, SLAPI_PLUGIN_START_FN, pb ) < 0 ) {
248 slapi_pblock_destroy( pb );
250 #endif /* LDAP_SLAPI */
255 int slap_shutdown( Backend *be )
262 Debug( LDAP_DEBUG_TRACE,
263 "%s shutdown: initiated\n",
266 /* let backends do whatever cleanup they need to do */
267 rc = backend_shutdown( be );
270 pb = slapi_pblock_new();
271 (void) slapi_int_call_plugins( frontendDB, SLAPI_PLUGIN_CLOSE_FN, pb );
272 slapi_pblock_destroy( pb );
273 #endif /* LDAP_SLAPI */
278 int slap_destroy(void)
283 Debug( LDAP_DEBUG_TRACE,
284 "%s destroy: freeing system resources.\n",
287 if ( default_referral ) {
288 ber_bvarray_free( default_referral );
291 rc = backend_destroy();
297 switch ( slapMode & SLAP_MODE ) {
298 case SLAP_SERVER_MODE:
301 ldap_pvt_thread_mutex_destroy( &slap_counters.sc_sent_mutex );
302 ldap_pvt_thread_mutex_destroy( &slap_counters.sc_ops_mutex );
303 ldap_pvt_mp_clear( slap_counters.sc_bytes );
304 ldap_pvt_mp_clear( slap_counters.sc_pdu );
305 ldap_pvt_mp_clear( slap_counters.sc_entries );
306 ldap_pvt_mp_clear( slap_counters.sc_refs );
307 ldap_pvt_mp_clear( slap_counters.sc_ops_initiated );
308 ldap_pvt_mp_clear( slap_counters.sc_ops_completed );
311 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
312 ldap_pvt_mp_clear( slap_counters.sc_ops_initiated_[ i ] );
313 ldap_pvt_mp_clear( slap_counters.sc_ops_completed_[ i ] );
315 #endif /* SLAPD_MONITOR */
319 Debug( LDAP_DEBUG_ANY,
320 "slap_destroy(): undefined mode (%d).\n", slapMode, 0, 0 );
327 ldap_pvt_thread_destroy();
329 /* should destory the above mutex */