]> git.sur5r.net Git - openldap/blob - servers/slapd/init.c
Forced commit, partially revert prev commit
[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 static const char* slap_name = NULL;
73 int slapMode = SLAP_UNDEFINED_MODE;
74
75 int
76 slap_init( int mode, const char *name )
77 {
78         int rc;
79         int i;
80
81         assert( mode );
82
83         if ( slapMode != SLAP_UNDEFINED_MODE ) {
84                 /* Make sure we write something to stderr */
85                 slap_debug |= LDAP_DEBUG_NONE;
86                 Debug( LDAP_DEBUG_ANY,
87                  "%s init: init called twice (old=%d, new=%d)\n",
88                  name, slapMode, mode );
89
90                 return 1;
91         }
92
93         slapMode = mode;
94
95         slap_op_init();
96
97 #ifdef SLAPD_MODULES
98         if ( module_init() != 0 ) {
99                 slap_debug |= LDAP_DEBUG_NONE;
100                 Debug( LDAP_DEBUG_ANY,
101                     "%s: module_init failed\n",
102                         name, 0, 0 );
103                 return 1;
104         }
105 #endif
106
107         if ( slap_schema_init( ) != 0 ) {
108                 slap_debug |= LDAP_DEBUG_NONE;
109                 Debug( LDAP_DEBUG_ANY,
110                     "%s: slap_schema_init failed\n",
111                     name, 0, 0 );
112                 return 1;
113         }
114
115         if ( filter_init() != 0 ) {
116                 slap_debug |= LDAP_DEBUG_NONE;
117                 Debug( LDAP_DEBUG_ANY,
118                     "%s: filter_init failed\n",
119                     name, 0, 0 );
120                 return 1;
121         }
122
123         if ( entry_init() != 0 ) {
124                 slap_debug |= LDAP_DEBUG_NONE;
125                 Debug( LDAP_DEBUG_ANY,
126                     "%s: entry_init failed\n",
127                     name, 0, 0 );
128                 return 1;
129         }
130
131         switch ( slapMode & SLAP_MODE ) {
132         case SLAP_SERVER_MODE:
133                 root_dse_init();
134
135                 /* FALLTHRU */
136         case SLAP_TOOL_MODE:
137                 Debug( LDAP_DEBUG_TRACE,
138                         "%s init: initiated %s.\n",     name,
139                         (mode & SLAP_MODE) == SLAP_TOOL_MODE ? "tool" : "server",
140                         0 );
141
142                 slap_name = name;
143
144                 ldap_pvt_thread_pool_init( &connection_pool,
145                                 connection_pool_max, 0);
146
147                 ldap_pvt_thread_mutex_init( &slap_counters.sc_sent_mutex );
148                 ldap_pvt_thread_mutex_init( &slap_counters.sc_ops_mutex );
149                 ldap_pvt_mp_init( slap_counters.sc_bytes );
150                 ldap_pvt_mp_init( slap_counters.sc_pdu );
151                 ldap_pvt_mp_init( slap_counters.sc_entries );
152                 ldap_pvt_mp_init( slap_counters.sc_refs );
153
154                 ldap_pvt_mp_init( slap_counters.sc_ops_initiated );
155                 ldap_pvt_mp_init( slap_counters.sc_ops_completed );
156
157                 ldap_pvt_thread_mutex_init( &slapd_rq.rq_mutex );
158                 LDAP_STAILQ_INIT( &slapd_rq.task_list );
159                 LDAP_STAILQ_INIT( &slapd_rq.run_list );
160
161 #ifdef SLAPD_MONITOR
162                 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
163                         ldap_pvt_mp_init( slap_counters.sc_ops_initiated_[ i ] );
164                         ldap_pvt_mp_init( slap_counters.sc_ops_completed_[ i ] );
165                 }
166 #endif /* SLAPD_MONITOR */
167
168                 ldap_pvt_thread_mutex_init( &gmtime_mutex );
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                 slap_debug |= LDAP_DEBUG_NONE;
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                 slap_debug |= LDAP_DEBUG_NONE;
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                 slap_debug |= LDAP_DEBUG_NONE;
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                 slap_debug |= LDAP_DEBUG_NONE;
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                 slap_debug |= LDAP_DEBUG_NONE;
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                 slap_debug |= LDAP_DEBUG_NONE;
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         Debug( LDAP_DEBUG_TRACE,
247                 "%s startup: initiated.\n",
248                 slap_name, 0, 0 );
249
250
251         return backend_startup( be );
252 }
253
254 int slap_shutdown( Backend *be )
255 {
256         Debug( LDAP_DEBUG_TRACE,
257                 "%s shutdown: initiated\n",
258                 slap_name, 0, 0 );
259
260         /* let backends do whatever cleanup they need to do */
261         return backend_shutdown( be ); 
262 }
263
264 int slap_destroy(void)
265 {
266         int rc;
267         int i;
268
269         Debug( LDAP_DEBUG_TRACE,
270                 "%s destroy: freeing system resources.\n",
271                 slap_name, 0, 0 );
272
273         if ( default_referral ) {
274                 ber_bvarray_free( default_referral );
275         }
276
277         /* clear out any thread-keys for the main thread */
278         ldap_pvt_thread_pool_context_reset( ldap_pvt_thread_pool_context());
279
280         rc = backend_destroy();
281
282         slap_sasl_destroy();
283
284         /* rootdse destroy goes before entry_destroy()
285          * because it may use entry_free() */
286         root_dse_destroy();
287         entry_destroy();
288
289         switch ( slapMode & SLAP_MODE ) {
290         case SLAP_SERVER_MODE:
291
292         case SLAP_TOOL_MODE:
293
294                 ldap_pvt_thread_mutex_destroy( &slap_counters.sc_sent_mutex );
295                 ldap_pvt_thread_mutex_destroy( &slap_counters.sc_ops_mutex );
296                 ldap_pvt_mp_clear( slap_counters.sc_bytes );
297                 ldap_pvt_mp_clear( slap_counters.sc_pdu );
298                 ldap_pvt_mp_clear( slap_counters.sc_entries );
299                 ldap_pvt_mp_clear( slap_counters.sc_refs );
300                 ldap_pvt_mp_clear( slap_counters.sc_ops_initiated );
301                 ldap_pvt_mp_clear( slap_counters.sc_ops_completed );
302
303 #ifdef SLAPD_MONITOR
304                 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
305                         ldap_pvt_mp_clear( slap_counters.sc_ops_initiated_[ i ] );
306                         ldap_pvt_mp_clear( slap_counters.sc_ops_completed_[ i ] );
307                 }
308 #endif /* SLAPD_MONITOR */
309                 break;
310
311         default:
312                 Debug( LDAP_DEBUG_ANY,
313                         "slap_destroy(): undefined mode (%d).\n", slapMode, 0, 0 );
314
315                 rc = 1;
316                 break;
317
318         }
319
320         slap_op_destroy();
321
322         ldap_pvt_thread_destroy();
323
324         /* should destroy the above mutex */
325         return rc;
326 }