]> git.sur5r.net Git - openldap/blob - servers/slapd/init.c
ITS#4911 move slap_op_init/destroy to slap_init/destroy
[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-2007 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 struct berval AllUser = BER_BVC( LDAP_ALL_USER_ATTRIBUTES );
59 struct berval AllOper = BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES );
60 struct berval NoAttrs = BER_BVC( LDAP_NO_ATTRS );
61
62 /*
63  * global variables that need mutex protection
64  */
65 ldap_pvt_thread_pool_t  connection_pool;
66 int                     connection_pool_max = SLAP_MAX_WORKER_THREADS;
67 int             slap_tool_thread_max = 1;
68 ldap_pvt_thread_mutex_t gmtime_mutex;
69
70 slap_counters_t                 slap_counters;
71
72 ldap_pvt_thread_mutex_t replog_mutex;
73
74 static const char* slap_name = NULL;
75 int slapMode = SLAP_UNDEFINED_MODE;
76
77 int
78 slap_init( int mode, const char *name )
79 {
80         int rc;
81         int i;
82
83         assert( mode );
84
85         if ( slapMode != SLAP_UNDEFINED_MODE ) {
86                 /* Make sure we write something to stderr */
87                 slap_debug |= LDAP_DEBUG_NONE;
88                 Debug( LDAP_DEBUG_ANY,
89                  "%s init: init called twice (old=%d, new=%d)\n",
90                  name, slapMode, mode );
91
92                 return 1;
93         }
94
95         slapMode = mode;
96
97         slap_op_init();
98
99 #ifdef SLAPD_MODULES
100         if ( module_init() != 0 ) {
101                 slap_debug |= LDAP_DEBUG_NONE;
102                 Debug( LDAP_DEBUG_ANY,
103                     "%s: module_init failed\n",
104                         name, 0, 0 );
105                 return 1;
106         }
107 #endif
108
109         if ( slap_schema_init( ) != 0 ) {
110                 slap_debug |= LDAP_DEBUG_NONE;
111                 Debug( LDAP_DEBUG_ANY,
112                     "%s: slap_schema_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                 ldap_pvt_thread_mutex_init( &replog_mutex );
141
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 );
148
149                 ldap_pvt_mp_init( slap_counters.sc_ops_initiated );
150                 ldap_pvt_mp_init( slap_counters.sc_ops_completed );
151
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 );
155
156 #ifdef SLAPD_MONITOR
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 ] );
160                 }
161 #endif /* SLAPD_MONITOR */
162
163                 ldap_pvt_thread_mutex_init( &gmtime_mutex );
164                 slap_passwd_init();
165
166                 rc = slap_sasl_init();
167
168                 if( rc == 0 ) {
169                         rc = backend_init( );
170                 }
171                 if ( rc )
172                         return rc;
173
174                 break;
175
176         default:
177                 slap_debug |= LDAP_DEBUG_NONE;
178                 Debug( LDAP_DEBUG_ANY,
179                         "%s init: undefined mode (%d).\n", name, mode, 0 );
180
181                 rc = 1;
182                 break;
183         }
184
185         if ( slap_controls_init( ) != 0 ) {
186                 slap_debug |= LDAP_DEBUG_NONE;
187                 Debug( LDAP_DEBUG_ANY,
188                     "%s: slap_controls_init failed\n",
189                     name, 0, 0 );
190                 return 1;
191         }
192
193 #ifdef HAVE_TLS
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.
198          */
199         rc = 0;
200         (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
201 #endif
202
203         if ( frontend_init() ) {
204                 slap_debug |= LDAP_DEBUG_NONE;
205                 Debug( LDAP_DEBUG_ANY,
206                     "%s: frontend_init failed\n",
207                     name, 0, 0 );
208                 return 1;
209         }
210
211         if ( overlay_init() ) {
212                 slap_debug |= LDAP_DEBUG_NONE;
213                 Debug( LDAP_DEBUG_ANY,
214                     "%s: overlay_init failed\n",
215                     name, 0, 0 );
216                 return 1;
217         }
218
219         if ( glue_sub_init() ) {
220                 slap_debug |= LDAP_DEBUG_NONE;
221                 Debug( LDAP_DEBUG_ANY,
222                     "%s: glue/subordinate init failed\n",
223                     name, 0, 0 );
224
225                 return 1;
226         }
227
228         if ( acl_init() ) {
229                 slap_debug |= LDAP_DEBUG_NONE;
230                 Debug( LDAP_DEBUG_ANY,
231                     "%s: acl_init failed\n",
232                     name, 0, 0 );
233                 return 1;
234         }
235
236         return rc;
237 }
238
239 int slap_startup( Backend *be )
240 {
241         Debug( LDAP_DEBUG_TRACE,
242                 "%s startup: initiated.\n",
243                 slap_name, 0, 0 );
244
245
246         return backend_startup( be );
247 }
248
249 int slap_shutdown( Backend *be )
250 {
251         Debug( LDAP_DEBUG_TRACE,
252                 "%s shutdown: initiated\n",
253                 slap_name, 0, 0 );
254
255         /* let backends do whatever cleanup they need to do */
256         return backend_shutdown( be ); 
257 }
258
259 int slap_destroy(void)
260 {
261         int rc;
262         int i;
263
264         Debug( LDAP_DEBUG_TRACE,
265                 "%s destroy: freeing system resources.\n",
266                 slap_name, 0, 0 );
267
268         if ( default_referral ) {
269                 ber_bvarray_free( default_referral );
270         }
271
272         /* clear out any thread-keys for the main thread */
273         ldap_pvt_thread_pool_context_reset( ldap_pvt_thread_pool_context());
274
275         rc = backend_destroy();
276
277         slap_sasl_destroy();
278
279         /* rootdse destroy goes before entry_destroy()
280          * because it may use entry_free() */
281         root_dse_destroy();
282         entry_destroy();
283
284         switch ( slapMode & SLAP_MODE ) {
285         case SLAP_SERVER_MODE:
286
287         case SLAP_TOOL_MODE:
288
289                 ldap_pvt_thread_mutex_destroy( &slap_counters.sc_sent_mutex );
290                 ldap_pvt_thread_mutex_destroy( &slap_counters.sc_ops_mutex );
291                 ldap_pvt_mp_clear( slap_counters.sc_bytes );
292                 ldap_pvt_mp_clear( slap_counters.sc_pdu );
293                 ldap_pvt_mp_clear( slap_counters.sc_entries );
294                 ldap_pvt_mp_clear( slap_counters.sc_refs );
295                 ldap_pvt_mp_clear( slap_counters.sc_ops_initiated );
296                 ldap_pvt_mp_clear( slap_counters.sc_ops_completed );
297
298 #ifdef SLAPD_MONITOR
299                 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
300                         ldap_pvt_mp_clear( slap_counters.sc_ops_initiated_[ i ] );
301                         ldap_pvt_mp_clear( slap_counters.sc_ops_completed_[ i ] );
302                 }
303 #endif /* SLAPD_MONITOR */
304                 break;
305
306         default:
307                 Debug( LDAP_DEBUG_ANY,
308                         "slap_destroy(): undefined mode (%d).\n", slapMode, 0, 0 );
309
310                 rc = 1;
311                 break;
312
313         }
314
315         slap_op_destroy();
316
317         ldap_pvt_thread_destroy();
318
319         /* should destroy the above mutex */
320         return rc;
321 }