init.c revision 1.2
1/*	$NetBSD: init.c,v 1.2 2020/08/11 13:15:39 christos Exp $	*/
2
3/* init.c - initialize various things */
4/* $OpenLDAP$ */
5/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6 *
7 * Copyright 1998-2020 The OpenLDAP Foundation.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
12 * Public License.
13 *
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
17 */
18/* Portions Copyright (c) 1995 Regents of the University of Michigan.
19 * All rights reserved.
20 *
21 * Redistribution and use in source and binary forms are permitted
22 * provided that this notice is preserved and that due credit is given
23 * to the University of Michigan at Ann Arbor. The name of the University
24 * may not be used to endorse or promote products derived from this
25 * software without specific prior written permission. This software
26 * is provided ``as is'' without express or implied warranty.
27 */
28
29#include <sys/cdefs.h>
30__RCSID("$NetBSD: init.c,v 1.2 2020/08/11 13:15:39 christos Exp $");
31
32#include "portable.h"
33
34#include <stdio.h>
35
36#include <ac/socket.h>
37#include <ac/string.h>
38#include <ac/time.h>
39
40#include "slap.h"
41#include "lber_pvt.h"
42
43#include "ldap_rq.h"
44
45/*
46 * read-only global variables or variables only written by the listener
47 * thread (after they are initialized) - no need to protect them with a mutex.
48 */
49int		slap_debug = 0;
50
51#ifdef LDAP_DEBUG
52int		ldap_syslog = LDAP_DEBUG_STATS;
53#else
54int		ldap_syslog;
55#endif
56
57#ifdef LOG_DEBUG
58int		ldap_syslog_level = LOG_DEBUG;
59#endif
60
61BerVarray default_referral = NULL;
62
63/*
64 * global variables that need mutex protection
65 */
66ldap_pvt_thread_pool_t	connection_pool;
67int			connection_pool_max = SLAP_MAX_WORKER_THREADS;
68int		slap_tool_thread_max = 1;
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		slap_passwd_init();
153
154		rc = slap_sasl_init();
155
156		if( rc == 0 ) {
157			rc = backend_init( );
158		}
159		if ( rc )
160			return rc;
161
162		break;
163
164	default:
165		slap_debug |= LDAP_DEBUG_NONE;
166		Debug( LDAP_DEBUG_ANY,
167			"%s init: undefined mode (%d).\n", name, mode, 0 );
168
169		rc = 1;
170		break;
171	}
172
173	if ( slap_controls_init( ) != 0 ) {
174		slap_debug |= LDAP_DEBUG_NONE;
175		Debug( LDAP_DEBUG_ANY,
176		    "%s: slap_controls_init failed\n",
177		    name, 0, 0 );
178		return 1;
179	}
180
181	if ( frontend_init() ) {
182		slap_debug |= LDAP_DEBUG_NONE;
183		Debug( LDAP_DEBUG_ANY,
184		    "%s: frontend_init failed\n",
185		    name, 0, 0 );
186		return 1;
187	}
188
189	if ( overlay_init() ) {
190		slap_debug |= LDAP_DEBUG_NONE;
191		Debug( LDAP_DEBUG_ANY,
192		    "%s: overlay_init failed\n",
193		    name, 0, 0 );
194		return 1;
195	}
196
197	if ( glue_sub_init() ) {
198		slap_debug |= LDAP_DEBUG_NONE;
199		Debug( LDAP_DEBUG_ANY,
200		    "%s: glue/subordinate init failed\n",
201		    name, 0, 0 );
202
203		return 1;
204	}
205
206	if ( acl_init() ) {
207		slap_debug |= LDAP_DEBUG_NONE;
208		Debug( LDAP_DEBUG_ANY,
209		    "%s: acl_init failed\n",
210		    name, 0, 0 );
211		return 1;
212	}
213
214	return rc;
215}
216
217int slap_startup( Backend *be )
218{
219	int rc;
220	Debug( LDAP_DEBUG_TRACE,
221		"%s startup: initiated.\n",
222		slap_name, 0, 0 );
223
224	rc = backend_startup( be );
225	if ( !rc && ( slapMode & SLAP_SERVER_MODE ))
226		slapMode |= SLAP_SERVER_RUNNING;
227	return rc;
228}
229
230int slap_shutdown( Backend *be )
231{
232	Debug( LDAP_DEBUG_TRACE,
233		"%s shutdown: initiated\n",
234		slap_name, 0, 0 );
235
236	/* let backends do whatever cleanup they need to do */
237	return backend_shutdown( be );
238}
239
240int slap_destroy(void)
241{
242	int rc;
243
244	Debug( LDAP_DEBUG_TRACE,
245		"%s destroy: freeing system resources.\n",
246		slap_name, 0, 0 );
247
248	if ( default_referral ) {
249		ber_bvarray_free( default_referral );
250	}
251
252	/* clear out any thread-keys for the main thread */
253	ldap_pvt_thread_pool_context_reset( ldap_pvt_thread_pool_context());
254
255	rc = backend_destroy();
256
257	slap_sasl_destroy();
258
259	/* rootdse destroy goes before entry_destroy()
260	 * because it may use entry_free() */
261	root_dse_destroy();
262	entry_destroy();
263
264	switch ( slapMode & SLAP_MODE ) {
265	case SLAP_SERVER_MODE:
266	case SLAP_TOOL_MODE:
267		slap_counters_destroy( &slap_counters );
268		break;
269
270	default:
271		Debug( LDAP_DEBUG_ANY,
272			"slap_destroy(): undefined mode (%d).\n", slapMode, 0, 0 );
273
274		rc = 1;
275		break;
276
277	}
278
279	slap_op_destroy();
280
281	ldap_pvt_thread_destroy();
282
283	/* should destroy the above mutex */
284	return rc;
285}
286
287void slap_counters_init( slap_counters_t *sc )
288{
289	int i;
290
291	ldap_pvt_thread_mutex_init( &sc->sc_mutex );
292	ldap_pvt_mp_init( sc->sc_bytes );
293	ldap_pvt_mp_init( sc->sc_pdu );
294	ldap_pvt_mp_init( sc->sc_entries );
295	ldap_pvt_mp_init( sc->sc_refs );
296
297	ldap_pvt_mp_init( sc->sc_ops_initiated );
298	ldap_pvt_mp_init( sc->sc_ops_completed );
299
300#ifdef SLAPD_MONITOR
301	for ( i = 0; i < SLAP_OP_LAST; i++ ) {
302		ldap_pvt_mp_init( sc->sc_ops_initiated_[ i ] );
303		ldap_pvt_mp_init( sc->sc_ops_completed_[ i ] );
304	}
305#endif /* SLAPD_MONITOR */
306}
307
308void slap_counters_destroy( slap_counters_t *sc )
309{
310	int i;
311
312	ldap_pvt_thread_mutex_destroy( &sc->sc_mutex );
313	ldap_pvt_mp_clear( sc->sc_bytes );
314	ldap_pvt_mp_clear( sc->sc_pdu );
315	ldap_pvt_mp_clear( sc->sc_entries );
316	ldap_pvt_mp_clear( sc->sc_refs );
317
318	ldap_pvt_mp_clear( sc->sc_ops_initiated );
319	ldap_pvt_mp_clear( sc->sc_ops_completed );
320
321#ifdef SLAPD_MONITOR
322	for ( i = 0; i < SLAP_OP_LAST; i++ ) {
323		ldap_pvt_mp_clear( sc->sc_ops_initiated_[ i ] );
324		ldap_pvt_mp_clear( sc->sc_ops_completed_[ i ] );
325	}
326#endif /* SLAPD_MONITOR */
327}
328
329