]> git.sur5r.net Git - openldap/blob - servers/slapd/init.c
ee01fe9b48e7807618a579113a2035297f8afc80
[openldap] / servers / slapd / init.c
1 /* init.c - initialize various things */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2015 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  */
44 int             slap_debug = 0;
45
46 #ifdef LDAP_DEBUG
47 int             ldap_syslog = LDAP_DEBUG_STATS;
48 #else
49 int             ldap_syslog;
50 #endif
51
52 #ifdef LOG_DEBUG
53 int             ldap_syslog_level = LOG_DEBUG;
54 #endif
55
56 BerVarray default_referral = NULL;
57
58 /*
59  * global variables that need mutex protection
60  */
61 ldap_pvt_thread_pool_t  connection_pool;
62 int             connection_pool_max = SLAP_MAX_WORKER_THREADS;
63 int             connection_pool_queues = 1;
64 int             slap_tool_thread_max = 1;
65
66 slap_counters_t                 slap_counters, *slap_counters_list;
67
68 static const char* slap_name = NULL;
69 int slapMode = SLAP_UNDEFINED_MODE;
70
71 int
72 slap_init( int mode, const char *name )
73 {
74         int rc;
75
76         assert( mode );
77
78         if ( slapMode != SLAP_UNDEFINED_MODE ) {
79                 /* Make sure we write something to stderr */
80                 slap_debug |= LDAP_DEBUG_NONE;
81                 Debug( LDAP_DEBUG_ANY,
82                  "%s init: init called twice (old=%d, new=%d)\n",
83                  name, slapMode, mode );
84
85                 return 1;
86         }
87
88         slapMode = mode;
89
90         slap_op_init();
91
92 #ifdef SLAPD_MODULES
93         if ( module_init() != 0 ) {
94                 slap_debug |= LDAP_DEBUG_NONE;
95                 Debug( LDAP_DEBUG_ANY,
96                     "%s: module_init failed\n",
97                         name, 0, 0 );
98                 return 1;
99         }
100 #endif
101
102         if ( slap_schema_init( ) != 0 ) {
103                 slap_debug |= LDAP_DEBUG_NONE;
104                 Debug( LDAP_DEBUG_ANY,
105                     "%s: slap_schema_init failed\n",
106                     name, 0, 0 );
107                 return 1;
108         }
109
110         if ( filter_init() != 0 ) {
111                 slap_debug |= LDAP_DEBUG_NONE;
112                 Debug( LDAP_DEBUG_ANY,
113                     "%s: filter_init failed\n",
114                     name, 0, 0 );
115                 return 1;
116         }
117
118         if ( entry_init() != 0 ) {
119                 slap_debug |= LDAP_DEBUG_NONE;
120                 Debug( LDAP_DEBUG_ANY,
121                     "%s: entry_init failed\n",
122                     name, 0, 0 );
123                 return 1;
124         }
125
126         switch ( slapMode & SLAP_MODE ) {
127         case SLAP_SERVER_MODE:
128                 root_dse_init();
129
130                 /* FALLTHRU */
131         case SLAP_TOOL_MODE:
132                 Debug( LDAP_DEBUG_TRACE,
133                         "%s init: initiated %s.\n",     name,
134                         (mode & SLAP_MODE) == SLAP_TOOL_MODE ? "tool" : "server",
135                         0 );
136
137                 slap_name = name;
138
139                 ldap_pvt_thread_pool_init_q( &connection_pool,
140                                 connection_pool_max, 0, connection_pool_queues);
141
142                 slap_counters_init( &slap_counters );
143
144                 ldap_pvt_thread_mutex_init( &slapd_rq.rq_mutex );
145                 LDAP_STAILQ_INIT( &slapd_rq.task_list );
146                 LDAP_STAILQ_INIT( &slapd_rq.run_list );
147
148                 slap_passwd_init();
149
150                 rc = slap_sasl_init();
151
152                 if( rc == 0 ) {
153                         rc = backend_init( );
154                 }
155                 if ( rc )
156                         return rc;
157
158                 break;
159
160         default:
161                 slap_debug |= LDAP_DEBUG_NONE;
162                 Debug( LDAP_DEBUG_ANY,
163                         "%s init: undefined mode (%d).\n", name, mode, 0 );
164
165                 rc = 1;
166                 break;
167         }
168
169         if ( slap_controls_init( ) != 0 ) {
170                 slap_debug |= LDAP_DEBUG_NONE;
171                 Debug( LDAP_DEBUG_ANY,
172                     "%s: slap_controls_init failed\n",
173                     name, 0, 0 );
174                 return 1;
175         }
176
177         if ( frontend_init() ) {
178                 slap_debug |= LDAP_DEBUG_NONE;
179                 Debug( LDAP_DEBUG_ANY,
180                     "%s: frontend_init failed\n",
181                     name, 0, 0 );
182                 return 1;
183         }
184
185         if ( overlay_init() ) {
186                 slap_debug |= LDAP_DEBUG_NONE;
187                 Debug( LDAP_DEBUG_ANY,
188                     "%s: overlay_init failed\n",
189                     name, 0, 0 );
190                 return 1;
191         }
192
193         if ( glue_sub_init() ) {
194                 slap_debug |= LDAP_DEBUG_NONE;
195                 Debug( LDAP_DEBUG_ANY,
196                     "%s: glue/subordinate init failed\n",
197                     name, 0, 0 );
198
199                 return 1;
200         }
201
202         if ( acl_init() ) {
203                 slap_debug |= LDAP_DEBUG_NONE;
204                 Debug( LDAP_DEBUG_ANY,
205                     "%s: acl_init failed\n",
206                     name, 0, 0 );
207                 return 1;
208         }
209
210         return rc;
211 }
212
213 int slap_startup( Backend *be )
214 {
215         int rc;
216         Debug( LDAP_DEBUG_TRACE,
217                 "%s startup: initiated.\n",
218                 slap_name, 0, 0 );
219
220         rc = backend_startup( be );
221         if ( !rc && ( slapMode & SLAP_SERVER_MODE ))
222                 slapMode |= SLAP_SERVER_RUNNING;
223         return rc;
224 }
225
226 int slap_shutdown( Backend *be )
227 {
228         Debug( LDAP_DEBUG_TRACE,
229                 "%s shutdown: initiated\n",
230                 slap_name, 0, 0 );
231
232         /* let backends do whatever cleanup they need to do */
233         return backend_shutdown( be ); 
234 }
235
236 int slap_destroy(void)
237 {
238         int rc;
239
240         Debug( LDAP_DEBUG_TRACE,
241                 "%s destroy: freeing system resources.\n",
242                 slap_name, 0, 0 );
243
244         if ( default_referral ) {
245                 ber_bvarray_free( default_referral );
246         }
247
248         /* clear out any thread-keys for the main thread */
249         ldap_pvt_thread_pool_context_reset( ldap_pvt_thread_pool_context());
250
251         rc = backend_destroy();
252
253         slap_sasl_destroy();
254
255         /* rootdse destroy goes before entry_destroy()
256          * because it may use entry_free() */
257         root_dse_destroy();
258         entry_destroy();
259
260         switch ( slapMode & SLAP_MODE ) {
261         case SLAP_SERVER_MODE:
262         case SLAP_TOOL_MODE:
263                 slap_counters_destroy( &slap_counters );
264                 break;
265
266         default:
267                 Debug( LDAP_DEBUG_ANY,
268                         "slap_destroy(): undefined mode (%d).\n", slapMode, 0, 0 );
269
270                 rc = 1;
271                 break;
272
273         }
274
275         slap_op_destroy();
276
277         ldap_pvt_thread_destroy();
278
279         /* should destroy the above mutex */
280         return rc;
281 }
282
283 void slap_counters_init( slap_counters_t *sc )
284 {
285         int i;
286
287         ldap_pvt_thread_mutex_init( &sc->sc_mutex );
288         ldap_pvt_mp_init( sc->sc_bytes );
289         ldap_pvt_mp_init( sc->sc_pdu );
290         ldap_pvt_mp_init( sc->sc_entries );
291         ldap_pvt_mp_init( sc->sc_refs );
292
293         ldap_pvt_mp_init( sc->sc_ops_initiated );
294         ldap_pvt_mp_init( sc->sc_ops_completed );
295
296 #ifdef SLAPD_MONITOR
297         for ( i = 0; i < SLAP_OP_LAST; i++ ) {
298                 ldap_pvt_mp_init( sc->sc_ops_initiated_[ i ] );
299                 ldap_pvt_mp_init( sc->sc_ops_completed_[ i ] );
300         }
301 #endif /* SLAPD_MONITOR */
302 }
303
304 void slap_counters_destroy( slap_counters_t *sc )
305 {
306         int i;
307
308         ldap_pvt_thread_mutex_destroy( &sc->sc_mutex );
309         ldap_pvt_mp_clear( sc->sc_bytes );
310         ldap_pvt_mp_clear( sc->sc_pdu );
311         ldap_pvt_mp_clear( sc->sc_entries );
312         ldap_pvt_mp_clear( sc->sc_refs );
313
314         ldap_pvt_mp_clear( sc->sc_ops_initiated );
315         ldap_pvt_mp_clear( sc->sc_ops_completed );
316
317 #ifdef SLAPD_MONITOR
318         for ( i = 0; i < SLAP_OP_LAST; i++ ) {
319                 ldap_pvt_mp_clear( sc->sc_ops_initiated_[ i ] );
320                 ldap_pvt_mp_clear( sc->sc_ops_completed_[ i ] );
321         }
322 #endif /* SLAPD_MONITOR */
323 }
324