]> git.sur5r.net Git - openldap/blob - servers/slapd/init.c
ITS#5262 fixes from HEAD
[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 #ifdef LDAP_SLAPI
38 #include "slapi/slapi.h"
39 #endif
40
41 #include "ldap_rq.h"
42
43 /*
44  * read-only global variables or variables only written by the listener
45  * thread (after they are initialized) - no need to protect them with a mutex.
46  */
47 int             slap_debug = 0;
48
49 #ifdef LDAP_DEBUG
50 int             ldap_syslog = LDAP_DEBUG_STATS;
51 #else
52 int             ldap_syslog;
53 #endif
54
55 #ifdef LOG_DEBUG
56 int             ldap_syslog_level = LOG_DEBUG;
57 #endif
58
59 BerVarray default_referral = NULL;
60
61 struct berval AllUser = BER_BVC( LDAP_ALL_USER_ATTRIBUTES );
62 struct berval AllOper = BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES );
63 struct berval NoAttrs = BER_BVC( LDAP_NO_ATTRS );
64
65 /*
66  * global variables that need mutex protection
67  */
68 ldap_pvt_thread_pool_t  connection_pool;
69 int                     connection_pool_max = SLAP_MAX_WORKER_THREADS;
70 int             slap_tool_thread_max = 1;
71 #ifndef HAVE_GMTIME_R
72 ldap_pvt_thread_mutex_t gmtime_mutex;
73 #endif
74
75 slap_counters_t                 slap_counters;
76
77 /*
78  * these mutexes must be used when calling the entry2str()
79  * routine since it returns a pointer to static data.
80  */
81 ldap_pvt_thread_mutex_t entry2str_mutex;
82 ldap_pvt_thread_mutex_t replog_mutex;
83
84 static const char* slap_name = NULL;
85 int slapMode = SLAP_UNDEFINED_MODE;
86
87 int
88 slap_init( int mode, const char *name )
89 {
90         int rc;
91         int i;
92
93         assert( mode );
94
95         if ( slapMode != SLAP_UNDEFINED_MODE ) {
96                 /* Make sure we write something to stderr */
97                 ldap_debug |= 1;
98                 Debug( LDAP_DEBUG_ANY,
99                  "%s init: init called twice (old=%d, new=%d)\n",
100                  name, slapMode, mode );
101
102                 return 1;
103         }
104
105         slapMode = mode;
106
107         slap_op_init();
108
109 #ifdef SLAPD_MODULES
110         if ( module_init() != 0 ) {
111                 ldap_debug |= 1;
112                 Debug( LDAP_DEBUG_ANY,
113                     "%s: module_init failed\n",
114                         name, 0, 0 );
115                 return 1;
116         }
117 #endif
118
119         if ( slap_schema_init( ) != 0 ) {
120                 ldap_debug |= 1;
121                 Debug( LDAP_DEBUG_ANY,
122                     "%s: slap_schema_init failed\n",
123                     name, 0, 0 );
124                 return 1;
125         }
126
127
128         switch ( slapMode & SLAP_MODE ) {
129         case SLAP_SERVER_MODE:
130
131                 /* FALLTHRU */
132         case SLAP_TOOL_MODE:
133                 Debug( LDAP_DEBUG_TRACE,
134                         "%s init: initiated %s.\n",     name,
135                         (mode & SLAP_MODE) == SLAP_TOOL_MODE ? "tool" : "server",
136                         0 );
137
138                 slap_name = name;
139
140                 ldap_pvt_thread_pool_init( &connection_pool,
141                                 connection_pool_max, 0);
142                 ldap_pvt_thread_mutex_init( &entry2str_mutex );
143                 ldap_pvt_thread_mutex_init( &replog_mutex );
144
145                 ldap_pvt_thread_mutex_init( &slap_counters.sc_sent_mutex );
146                 ldap_pvt_thread_mutex_init( &slap_counters.sc_ops_mutex );
147                 ldap_pvt_mp_init( slap_counters.sc_bytes );
148                 ldap_pvt_mp_init( slap_counters.sc_pdu );
149                 ldap_pvt_mp_init( slap_counters.sc_entries );
150                 ldap_pvt_mp_init( slap_counters.sc_refs );
151
152                 ldap_pvt_mp_init( slap_counters.sc_ops_initiated );
153                 ldap_pvt_mp_init( slap_counters.sc_ops_completed );
154
155                 ldap_pvt_thread_mutex_init( &slapd_rq.rq_mutex );
156                 LDAP_STAILQ_INIT( &slapd_rq.task_list );
157                 LDAP_STAILQ_INIT( &slapd_rq.run_list );
158
159 #ifdef SLAPD_MONITOR
160                 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
161                         ldap_pvt_mp_init( slap_counters.sc_ops_initiated_[ i ] );
162                         ldap_pvt_mp_init( slap_counters.sc_ops_completed_[ i ] );
163                 }
164 #endif /* SLAPD_MONITOR */
165
166 #ifndef HAVE_GMTIME_R
167                 ldap_pvt_thread_mutex_init( &gmtime_mutex );
168 #endif
169                 slap_passwd_init();
170
171                 rc = slap_sasl_init();
172
173                 if( rc == 0 ) {
174                         rc = backend_init( );
175                 }
176                 if ( rc )
177                         return rc;
178
179                 break;
180
181         default:
182                 ldap_debug |= 1;
183                 Debug( LDAP_DEBUG_ANY,
184                         "%s init: undefined mode (%d).\n", name, mode, 0 );
185
186                 rc = 1;
187                 break;
188         }
189
190         if ( slap_controls_init( ) != 0 ) {
191                 ldap_debug |= 1;
192                 Debug( LDAP_DEBUG_ANY,
193                     "%s: slap_controls_init failed\n",
194                     name, 0, 0 );
195                 return 1;
196         }
197
198 #ifdef HAVE_TLS
199         /* Library defaults to full certificate checking. This is correct when
200          * a client is verifying a server because all servers should have a
201          * valid cert. But few clients have valid certs, so we want our default
202          * to be no checking. The config file can override this as usual.
203          */
204         rc = 0;
205         (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
206 #endif
207
208         if ( frontend_init() ) {
209                 ldap_debug |= 1;
210                 Debug( LDAP_DEBUG_ANY,
211                     "%s: frontend_init failed\n",
212                     name, 0, 0 );
213                 return 1;
214         }
215
216         if ( overlay_init() ) {
217                 ldap_debug |= 1;
218                 Debug( LDAP_DEBUG_ANY,
219                     "%s: overlay_init failed\n",
220                     name, 0, 0 );
221                 return 1;
222         }
223
224         if ( glue_sub_init() ) {
225                 ldap_debug |= 1;
226                 Debug( LDAP_DEBUG_ANY,
227                     "%s: glue/subordinate init failed\n",
228                     name, 0, 0 );
229
230                 return 1;
231         }
232
233         if ( acl_init() ) {
234                 ldap_debug |= 1;
235                 Debug( LDAP_DEBUG_ANY,
236                     "%s: acl_init failed\n",
237                     name, 0, 0 );
238                 return 1;
239         }
240
241         return rc;
242 }
243
244 int slap_startup( Backend *be )
245 {
246         int rc;
247
248         Debug( LDAP_DEBUG_TRACE,
249                 "%s startup: initiated.\n",
250                 slap_name, 0, 0 );
251
252
253         rc = backend_startup( be );
254
255 #ifdef LDAP_SLAPI
256         if( rc == 0 ) {
257                 Slapi_PBlock *pb = slapi_pblock_new();
258
259                 if ( slapi_int_call_plugins( frontendDB, SLAPI_PLUGIN_START_FN, pb ) < 0 ) {
260                         rc = -1;
261                 }
262                 slapi_pblock_destroy( pb );
263         }
264 #endif /* LDAP_SLAPI */
265
266         return rc;
267 }
268
269 int slap_shutdown( Backend *be )
270 {
271         int rc;
272 #ifdef LDAP_SLAPI
273         Slapi_PBlock *pb;
274 #endif
275
276         Debug( LDAP_DEBUG_TRACE,
277                 "%s shutdown: initiated\n",
278                 slap_name, 0, 0 );
279
280         /* let backends do whatever cleanup they need to do */
281         rc = backend_shutdown( be ); 
282
283 #ifdef LDAP_SLAPI
284         pb = slapi_pblock_new();
285         (void) slapi_int_call_plugins( frontendDB, SLAPI_PLUGIN_CLOSE_FN, pb );
286         slapi_pblock_destroy( pb );
287 #endif /* LDAP_SLAPI */
288
289         return rc;
290 }
291
292 int slap_destroy(void)
293 {
294         int rc;
295         int i;
296
297         Debug( LDAP_DEBUG_TRACE,
298                 "%s destroy: freeing system resources.\n",
299                 slap_name, 0, 0 );
300
301         if ( default_referral ) {
302                 ber_bvarray_free( default_referral );
303         }
304
305         /* clear out any thread-keys for the main thread */
306         ldap_pvt_thread_pool_context_reset( ldap_pvt_thread_pool_context());
307
308         rc = backend_destroy();
309
310         slap_sasl_destroy();
311
312         entry_destroy();
313
314         switch ( slapMode & SLAP_MODE ) {
315         case SLAP_SERVER_MODE:
316         case SLAP_TOOL_MODE:
317
318                 ldap_pvt_thread_mutex_destroy( &slap_counters.sc_sent_mutex );
319                 ldap_pvt_thread_mutex_destroy( &slap_counters.sc_ops_mutex );
320                 ldap_pvt_mp_clear( slap_counters.sc_bytes );
321                 ldap_pvt_mp_clear( slap_counters.sc_pdu );
322                 ldap_pvt_mp_clear( slap_counters.sc_entries );
323                 ldap_pvt_mp_clear( slap_counters.sc_refs );
324                 ldap_pvt_mp_clear( slap_counters.sc_ops_initiated );
325                 ldap_pvt_mp_clear( slap_counters.sc_ops_completed );
326
327 #ifdef SLAPD_MONITOR
328                 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
329                         ldap_pvt_mp_clear( slap_counters.sc_ops_initiated_[ i ] );
330                         ldap_pvt_mp_clear( slap_counters.sc_ops_completed_[ i ] );
331                 }
332 #endif /* SLAPD_MONITOR */
333                 break;
334
335         default:
336                 Debug( LDAP_DEBUG_ANY,
337                         "slap_destroy(): undefined mode (%d).\n", slapMode, 0, 0 );
338
339                 rc = 1;
340                 break;
341
342         }
343
344         slap_op_destroy();
345
346         ldap_pvt_thread_destroy();
347
348         /* should destroy the above mutex */
349         return rc;
350 }