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