]> git.sur5r.net Git - openldap/blob - servers/slapd/init.c
- setup framework for monitoring of back-bdb/back-hdb stuff in their
[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-2006 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 #ifndef HAVE_GMTIME_R
69 ldap_pvt_thread_mutex_t gmtime_mutex;
70 #endif
71
72 slap_counters_t                 slap_counters;
73
74 ldap_pvt_thread_mutex_t replog_mutex;
75
76 static const char* slap_name = NULL;
77 int slapMode = SLAP_UNDEFINED_MODE;
78
79 int
80 slap_init( int mode, const char *name )
81 {
82         int rc;
83         int i;
84
85         assert( mode );
86
87         if ( slapMode != SLAP_UNDEFINED_MODE ) {
88                 /* Make sure we write something to stderr */
89                 slap_debug |= LDAP_DEBUG_NONE;
90                 Debug( LDAP_DEBUG_ANY,
91                  "%s init: init called twice (old=%d, new=%d)\n",
92                  name, slapMode, mode );
93
94                 return 1;
95         }
96
97         slapMode = mode;
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
128                 /* FALLTHRU */
129         case SLAP_TOOL_MODE:
130                 Debug( LDAP_DEBUG_TRACE,
131                         "%s init: initiated %s.\n",     name,
132                         (mode & SLAP_MODE) == SLAP_TOOL_MODE ? "tool" : "server",
133                         0 );
134
135                 slap_name = name;
136
137                 ldap_pvt_thread_pool_init( &connection_pool,
138                                 connection_pool_max, 0);
139                 ldap_pvt_thread_mutex_init( &replog_mutex );
140
141                 ldap_pvt_thread_mutex_init( &slap_counters.sc_sent_mutex );
142                 ldap_pvt_thread_mutex_init( &slap_counters.sc_ops_mutex );
143                 ldap_pvt_mp_init( slap_counters.sc_bytes );
144                 ldap_pvt_mp_init( slap_counters.sc_pdu );
145                 ldap_pvt_mp_init( slap_counters.sc_entries );
146                 ldap_pvt_mp_init( slap_counters.sc_refs );
147
148                 ldap_pvt_mp_init( slap_counters.sc_ops_initiated );
149                 ldap_pvt_mp_init( slap_counters.sc_ops_completed );
150
151                 ldap_pvt_thread_mutex_init( &slapd_rq.rq_mutex );
152                 LDAP_STAILQ_INIT( &slapd_rq.task_list );
153                 LDAP_STAILQ_INIT( &slapd_rq.run_list );
154
155 #ifdef SLAPD_MONITOR
156                 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
157                         ldap_pvt_mp_init( slap_counters.sc_ops_initiated_[ i ] );
158                         ldap_pvt_mp_init( slap_counters.sc_ops_completed_[ i ] );
159                 }
160 #endif /* SLAPD_MONITOR */
161
162 #ifndef HAVE_GMTIME_R
163                 ldap_pvt_thread_mutex_init( &gmtime_mutex );
164 #endif
165                 slap_passwd_init();
166
167                 rc = slap_sasl_init();
168
169                 if( rc == 0 ) {
170                         rc = backend_init( );
171                 }
172
173                 break;
174
175         default:
176                 slap_debug |= LDAP_DEBUG_NONE;
177                 Debug( LDAP_DEBUG_ANY,
178                         "%s init: undefined mode (%d).\n", name, mode, 0 );
179
180                 rc = 1;
181                 break;
182         }
183
184         if ( slap_controls_init( ) != 0 ) {
185                 slap_debug |= LDAP_DEBUG_NONE;
186                 Debug( LDAP_DEBUG_ANY,
187                     "%s: slap_controls_init failed\n",
188                     name, 0, 0 );
189                 return 1;
190         }
191
192 #ifdef HAVE_TLS
193         /* Library defaults to full certificate checking. This is correct when
194          * a client is verifying a server because all servers should have a
195          * valid cert. But few clients have valid certs, so we want our default
196          * to be no checking. The config file can override this as usual.
197          */
198         rc = 0;
199         (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
200 #endif
201
202         if ( frontend_init() ) {
203                 slap_debug |= LDAP_DEBUG_NONE;
204                 Debug( LDAP_DEBUG_ANY,
205                     "%s: frontend_init failed\n",
206                     name, 0, 0 );
207                 return 1;
208         }
209
210         if ( overlay_init() ) {
211                 slap_debug |= LDAP_DEBUG_NONE;
212                 Debug( LDAP_DEBUG_ANY,
213                     "%s: overlay_init failed\n",
214                     name, 0, 0 );
215                 return 1;
216         }
217
218         if ( glue_sub_init() ) {
219                 slap_debug |= LDAP_DEBUG_NONE;
220                 Debug( LDAP_DEBUG_ANY,
221                     "%s: glue/subordinate init failed\n",
222                     name, 0, 0 );
223
224                 return 1;
225         }
226
227         if ( acl_init() ) {
228                 slap_debug |= LDAP_DEBUG_NONE;
229                 Debug( LDAP_DEBUG_ANY,
230                     "%s: acl_init failed\n",
231                     name, 0, 0 );
232                 return 1;
233         }
234
235         return rc;
236 }
237
238 int slap_startup( Backend *be )
239 {
240         Debug( LDAP_DEBUG_TRACE,
241                 "%s startup: initiated.\n",
242                 slap_name, 0, 0 );
243
244
245         return backend_startup( be );
246 }
247
248 int slap_shutdown( Backend *be )
249 {
250         Debug( LDAP_DEBUG_TRACE,
251                 "%s shutdown: initiated\n",
252                 slap_name, 0, 0 );
253
254         /* let backends do whatever cleanup they need to do */
255         return backend_shutdown( be ); 
256 }
257
258 int slap_destroy(void)
259 {
260         int rc;
261         int i;
262
263         Debug( LDAP_DEBUG_TRACE,
264                 "%s destroy: freeing system resources.\n",
265                 slap_name, 0, 0 );
266
267         if ( default_referral ) {
268                 ber_bvarray_free( default_referral );
269         }
270
271         rc = backend_destroy();
272
273         slap_sasl_destroy();
274
275         entry_destroy();
276
277         switch ( slapMode & SLAP_MODE ) {
278         case SLAP_SERVER_MODE:
279         case SLAP_TOOL_MODE:
280
281                 ldap_pvt_thread_mutex_destroy( &slap_counters.sc_sent_mutex );
282                 ldap_pvt_thread_mutex_destroy( &slap_counters.sc_ops_mutex );
283                 ldap_pvt_mp_clear( slap_counters.sc_bytes );
284                 ldap_pvt_mp_clear( slap_counters.sc_pdu );
285                 ldap_pvt_mp_clear( slap_counters.sc_entries );
286                 ldap_pvt_mp_clear( slap_counters.sc_refs );
287                 ldap_pvt_mp_clear( slap_counters.sc_ops_initiated );
288                 ldap_pvt_mp_clear( slap_counters.sc_ops_completed );
289
290 #ifdef SLAPD_MONITOR
291                 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
292                         ldap_pvt_mp_clear( slap_counters.sc_ops_initiated_[ i ] );
293                         ldap_pvt_mp_clear( slap_counters.sc_ops_completed_[ i ] );
294                 }
295 #endif /* SLAPD_MONITOR */
296                 break;
297
298         default:
299                 Debug( LDAP_DEBUG_ANY,
300                         "slap_destroy(): undefined mode (%d).\n", slapMode, 0, 0 );
301
302                 rc = 1;
303                 break;
304
305         }
306
307         ldap_pvt_thread_destroy();
308
309         /* should destory the above mutex */
310         return rc;
311 }