init.c revision 1.1.1.1
1/* init.c - initialize various things */
2/* $OpenLDAP: pkg/ldap/servers/slapd/init.c,v 1.97.2.9 2008/02/12 00:46:46 quanah Exp $ */
3/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 1998-2008 The OpenLDAP Foundation.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
10 * Public License.
11 *
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>.
15 */
16/* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
18 *
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.
25 */
26
27#include "portable.h"
28
29#include <stdio.h>
30
31#include <ac/socket.h>
32#include <ac/string.h>
33#include <ac/time.h>
34
35#include "slap.h"
36#include "lber_pvt.h"
37
38#include "ldap_rq.h"
39
40/*
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.
43 */
44int		slap_debug = 0;
45
46#ifdef LDAP_DEBUG
47int		ldap_syslog = LDAP_DEBUG_STATS;
48#else
49int		ldap_syslog;
50#endif
51
52#ifdef LOG_DEBUG
53int		ldap_syslog_level = LOG_DEBUG;
54#endif
55
56BerVarray default_referral = NULL;
57
58struct berval AllUser = BER_BVC( LDAP_ALL_USER_ATTRIBUTES );
59struct berval AllOper = BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES );
60struct berval NoAttrs = BER_BVC( LDAP_NO_ATTRS );
61
62/*
63 * global variables that need mutex protection
64 */
65ldap_pvt_thread_pool_t	connection_pool;
66int			connection_pool_max = SLAP_MAX_WORKER_THREADS;
67int		slap_tool_thread_max = 1;
68ldap_pvt_thread_mutex_t	gmtime_mutex;
69
70slap_counters_t			slap_counters, *slap_counters_list;
71
72static const char* slap_name = NULL;
73int slapMode = SLAP_UNDEFINED_MODE;
74
75int
76slap_init( int mode, const char *name )
77{
78	int rc;
79
80	assert( mode );
81
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 );
88
89		return 1;
90	}
91
92	slapMode = mode;
93
94	slap_op_init();
95
96#ifdef SLAPD_MODULES
97	if ( module_init() != 0 ) {
98		slap_debug |= LDAP_DEBUG_NONE;
99		Debug( LDAP_DEBUG_ANY,
100		    "%s: module_init failed\n",
101			name, 0, 0 );
102		return 1;
103	}
104#endif
105
106	if ( slap_schema_init( ) != 0 ) {
107		slap_debug |= LDAP_DEBUG_NONE;
108		Debug( LDAP_DEBUG_ANY,
109		    "%s: slap_schema_init failed\n",
110		    name, 0, 0 );
111		return 1;
112	}
113
114	if ( filter_init() != 0 ) {
115		slap_debug |= LDAP_DEBUG_NONE;
116		Debug( LDAP_DEBUG_ANY,
117		    "%s: filter_init failed\n",
118		    name, 0, 0 );
119		return 1;
120	}
121
122	if ( entry_init() != 0 ) {
123		slap_debug |= LDAP_DEBUG_NONE;
124		Debug( LDAP_DEBUG_ANY,
125		    "%s: entry_init failed\n",
126		    name, 0, 0 );
127		return 1;
128	}
129
130	switch ( slapMode & SLAP_MODE ) {
131	case SLAP_SERVER_MODE:
132		root_dse_init();
133
134		/* FALLTHRU */
135	case SLAP_TOOL_MODE:
136		Debug( LDAP_DEBUG_TRACE,
137			"%s init: initiated %s.\n",	name,
138			(mode & SLAP_MODE) == SLAP_TOOL_MODE ? "tool" : "server",
139			0 );
140
141		slap_name = name;
142
143		ldap_pvt_thread_pool_init( &connection_pool,
144				connection_pool_max, 0);
145
146		slap_counters_init( &slap_counters );
147
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 );
151
152		ldap_pvt_thread_mutex_init( &gmtime_mutex );
153		slap_passwd_init();
154
155		rc = slap_sasl_init();
156
157		if( rc == 0 ) {
158			rc = backend_init( );
159		}
160		if ( rc )
161			return rc;
162
163		break;
164
165	default:
166		slap_debug |= LDAP_DEBUG_NONE;
167		Debug( LDAP_DEBUG_ANY,
168			"%s init: undefined mode (%d).\n", name, mode, 0 );
169
170		rc = 1;
171		break;
172	}
173
174	if ( slap_controls_init( ) != 0 ) {
175		slap_debug |= LDAP_DEBUG_NONE;
176		Debug( LDAP_DEBUG_ANY,
177		    "%s: slap_controls_init failed\n",
178		    name, 0, 0 );
179		return 1;
180	}
181
182	if ( frontend_init() ) {
183		slap_debug |= LDAP_DEBUG_NONE;
184		Debug( LDAP_DEBUG_ANY,
185		    "%s: frontend_init failed\n",
186		    name, 0, 0 );
187		return 1;
188	}
189
190	if ( overlay_init() ) {
191		slap_debug |= LDAP_DEBUG_NONE;
192		Debug( LDAP_DEBUG_ANY,
193		    "%s: overlay_init failed\n",
194		    name, 0, 0 );
195		return 1;
196	}
197
198	if ( glue_sub_init() ) {
199		slap_debug |= LDAP_DEBUG_NONE;
200		Debug( LDAP_DEBUG_ANY,
201		    "%s: glue/subordinate init failed\n",
202		    name, 0, 0 );
203
204		return 1;
205	}
206
207	if ( acl_init() ) {
208		slap_debug |= LDAP_DEBUG_NONE;
209		Debug( LDAP_DEBUG_ANY,
210		    "%s: acl_init failed\n",
211		    name, 0, 0 );
212		return 1;
213	}
214
215	return rc;
216}
217
218int slap_startup( Backend *be )
219{
220	Debug( LDAP_DEBUG_TRACE,
221		"%s startup: initiated.\n",
222		slap_name, 0, 0 );
223
224
225	return backend_startup( be );
226}
227
228int slap_shutdown( Backend *be )
229{
230	Debug( LDAP_DEBUG_TRACE,
231		"%s shutdown: initiated\n",
232		slap_name, 0, 0 );
233
234	/* let backends do whatever cleanup they need to do */
235	return backend_shutdown( be );
236}
237
238int slap_destroy(void)
239{
240	int rc;
241
242	Debug( LDAP_DEBUG_TRACE,
243		"%s destroy: freeing system resources.\n",
244		slap_name, 0, 0 );
245
246	if ( default_referral ) {
247		ber_bvarray_free( default_referral );
248	}
249
250	/* clear out any thread-keys for the main thread */
251	ldap_pvt_thread_pool_context_reset( ldap_pvt_thread_pool_context());
252
253	rc = backend_destroy();
254
255	slap_sasl_destroy();
256
257	/* rootdse destroy goes before entry_destroy()
258	 * because it may use entry_free() */
259	root_dse_destroy();
260	entry_destroy();
261
262	switch ( slapMode & SLAP_MODE ) {
263	case SLAP_SERVER_MODE:
264	case SLAP_TOOL_MODE:
265		slap_counters_destroy( &slap_counters );
266		break;
267
268	default:
269		Debug( LDAP_DEBUG_ANY,
270			"slap_destroy(): undefined mode (%d).\n", slapMode, 0, 0 );
271
272		rc = 1;
273		break;
274
275	}
276
277	slap_op_destroy();
278
279	ldap_pvt_thread_destroy();
280
281	/* should destroy the above mutex */
282	return rc;
283}
284
285void slap_counters_init( slap_counters_t *sc )
286{
287	int i;
288
289	ldap_pvt_thread_mutex_init( &sc->sc_mutex );
290	ldap_pvt_mp_init( sc->sc_bytes );
291	ldap_pvt_mp_init( sc->sc_pdu );
292	ldap_pvt_mp_init( sc->sc_entries );
293	ldap_pvt_mp_init( sc->sc_refs );
294
295	ldap_pvt_mp_init( sc->sc_ops_initiated );
296	ldap_pvt_mp_init( sc->sc_ops_completed );
297
298#ifdef SLAPD_MONITOR
299	for ( i = 0; i < SLAP_OP_LAST; i++ ) {
300		ldap_pvt_mp_init( sc->sc_ops_initiated_[ i ] );
301		ldap_pvt_mp_init( sc->sc_ops_completed_[ i ] );
302	}
303#endif /* SLAPD_MONITOR */
304}
305
306void slap_counters_destroy( slap_counters_t *sc )
307{
308	int i;
309
310	ldap_pvt_thread_mutex_destroy( &sc->sc_mutex );
311	ldap_pvt_mp_clear( sc->sc_bytes );
312	ldap_pvt_mp_clear( sc->sc_pdu );
313	ldap_pvt_mp_clear( sc->sc_entries );
314	ldap_pvt_mp_clear( sc->sc_refs );
315
316	ldap_pvt_mp_clear( sc->sc_ops_initiated );
317	ldap_pvt_mp_clear( sc->sc_ops_completed );
318
319#ifdef SLAPD_MONITOR
320	for ( i = 0; i < SLAP_OP_LAST; i++ ) {
321		ldap_pvt_mp_clear( sc->sc_ops_initiated_[ i ] );
322		ldap_pvt_mp_clear( sc->sc_ops_completed_[ i ] );
323	}
324#endif /* SLAPD_MONITOR */
325}
326
327