]> git.sur5r.net Git - openldap/blob - servers/slapd/init.c
8cf3c7ae0e1fe3fc1874f783b5fe47cf1f68c278
[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 #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                 slap_debug |= LDAP_DEBUG_NONE;
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 #ifdef SLAPD_MODULES
108         if ( module_init() != 0 ) {
109                 slap_debug |= LDAP_DEBUG_NONE;
110                 Debug( LDAP_DEBUG_ANY,
111                     "%s: module_init failed\n",
112                         name, 0, 0 );
113                 return 1;
114         }
115 #endif
116
117         if ( slap_schema_init( ) != 0 ) {
118                 slap_debug |= LDAP_DEBUG_NONE;
119                 Debug( LDAP_DEBUG_ANY,
120                     "%s: slap_schema_init failed\n",
121                     name, 0, 0 );
122                 return 1;
123         }
124
125
126         switch ( slapMode & SLAP_MODE ) {
127         case SLAP_SERVER_MODE:
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                 ldap_pvt_thread_mutex_init( &entry2str_mutex );
142                 ldap_pvt_thread_mutex_init( &replog_mutex );
143
144                 ldap_pvt_thread_mutex_init( &slap_counters.sc_sent_mutex );
145                 ldap_pvt_thread_mutex_init( &slap_counters.sc_ops_mutex );
146                 ldap_pvt_mp_init( slap_counters.sc_bytes );
147                 ldap_pvt_mp_init( slap_counters.sc_pdu );
148                 ldap_pvt_mp_init( slap_counters.sc_entries );
149                 ldap_pvt_mp_init( slap_counters.sc_refs );
150
151                 ldap_pvt_mp_init( slap_counters.sc_ops_initiated );
152                 ldap_pvt_mp_init( slap_counters.sc_ops_completed );
153
154                 ldap_pvt_thread_mutex_init( &slapd_rq.rq_mutex );
155                 LDAP_STAILQ_INIT( &slapd_rq.task_list );
156                 LDAP_STAILQ_INIT( &slapd_rq.run_list );
157
158 #ifdef SLAPD_MONITOR
159                 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
160                         ldap_pvt_mp_init( slap_counters.sc_ops_initiated_[ i ] );
161                         ldap_pvt_mp_init( slap_counters.sc_ops_completed_[ i ] );
162                 }
163 #endif /* SLAPD_MONITOR */
164
165 #ifndef HAVE_GMTIME_R
166                 ldap_pvt_thread_mutex_init( &gmtime_mutex );
167 #endif
168                 slap_passwd_init();
169
170                 rc = slap_sasl_init();
171
172                 if( rc == 0 ) {
173                         rc = backend_init( );
174                 }
175
176                 break;
177
178         default:
179                 slap_debug |= LDAP_DEBUG_NONE;
180                 Debug( LDAP_DEBUG_ANY,
181                         "%s init: undefined mode (%d).\n", name, mode, 0 );
182
183                 rc = 1;
184                 break;
185         }
186
187         if ( slap_controls_init( ) != 0 ) {
188                 slap_debug |= LDAP_DEBUG_NONE;
189                 Debug( LDAP_DEBUG_ANY,
190                     "%s: slap_controls_init failed\n",
191                     name, 0, 0 );
192                 return 1;
193         }
194
195 #ifdef HAVE_TLS
196         /* Library defaults to full certificate checking. This is correct when
197          * a client is verifying a server because all servers should have a
198          * valid cert. But few clients have valid certs, so we want our default
199          * to be no checking. The config file can override this as usual.
200          */
201         rc = 0;
202         (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
203 #endif
204
205         if ( frontend_init() ) {
206                 slap_debug |= LDAP_DEBUG_NONE;
207                 Debug( LDAP_DEBUG_ANY,
208                     "%s: frontend_init failed\n",
209                     name, 0, 0 );
210                 return 1;
211         }
212
213         if ( overlay_init() ) {
214                 slap_debug |= LDAP_DEBUG_NONE;
215                 Debug( LDAP_DEBUG_ANY,
216                     "%s: overlay_init failed\n",
217                     name, 0, 0 );
218                 return 1;
219         }
220
221         if ( glue_sub_init() ) {
222                 slap_debug |= LDAP_DEBUG_NONE;
223                 Debug( LDAP_DEBUG_ANY,
224                     "%s: glue/subordinate init failed\n",
225                     name, 0, 0 );
226
227                 return 1;
228         }
229
230         if ( acl_init() ) {
231                 slap_debug |= LDAP_DEBUG_NONE;
232                 Debug( LDAP_DEBUG_ANY,
233                     "%s: acl_init failed\n",
234                     name, 0, 0 );
235                 return 1;
236         }
237
238         return rc;
239 }
240
241 int slap_startup( Backend *be )
242 {
243         int rc;
244
245         Debug( LDAP_DEBUG_TRACE,
246                 "%s startup: initiated.\n",
247                 slap_name, 0, 0 );
248
249
250         rc = backend_startup( be );
251
252 #ifdef LDAP_SLAPI
253         if( rc == 0 ) {
254                 Slapi_PBlock *pb = slapi_pblock_new();
255
256                 if ( slapi_int_call_plugins( frontendDB, SLAPI_PLUGIN_START_FN, pb ) < 0 ) {
257                         rc = -1;
258                 }
259                 slapi_pblock_destroy( pb );
260         }
261 #endif /* LDAP_SLAPI */
262
263         return rc;
264 }
265
266 int slap_shutdown( Backend *be )
267 {
268         int rc;
269 #ifdef LDAP_SLAPI
270         Slapi_PBlock *pb;
271 #endif
272
273         Debug( LDAP_DEBUG_TRACE,
274                 "%s shutdown: initiated\n",
275                 slap_name, 0, 0 );
276
277         /* let backends do whatever cleanup they need to do */
278         rc = backend_shutdown( be ); 
279
280 #ifdef LDAP_SLAPI
281         pb = slapi_pblock_new();
282         (void) slapi_int_call_plugins( frontendDB, SLAPI_PLUGIN_CLOSE_FN, pb );
283         slapi_pblock_destroy( pb );
284 #endif /* LDAP_SLAPI */
285
286         return rc;
287 }
288
289 int slap_destroy(void)
290 {
291         int rc;
292         int i;
293
294         Debug( LDAP_DEBUG_TRACE,
295                 "%s destroy: freeing system resources.\n",
296                 slap_name, 0, 0 );
297
298         if ( default_referral ) {
299                 ber_bvarray_free( default_referral );
300         }
301
302         rc = backend_destroy();
303
304         slap_sasl_destroy();
305
306         entry_destroy();
307
308         switch ( slapMode & SLAP_MODE ) {
309         case SLAP_SERVER_MODE:
310         case SLAP_TOOL_MODE:
311
312                 ldap_pvt_thread_mutex_destroy( &slap_counters.sc_sent_mutex );
313                 ldap_pvt_thread_mutex_destroy( &slap_counters.sc_ops_mutex );
314                 ldap_pvt_mp_clear( slap_counters.sc_bytes );
315                 ldap_pvt_mp_clear( slap_counters.sc_pdu );
316                 ldap_pvt_mp_clear( slap_counters.sc_entries );
317                 ldap_pvt_mp_clear( slap_counters.sc_refs );
318                 ldap_pvt_mp_clear( slap_counters.sc_ops_initiated );
319                 ldap_pvt_mp_clear( slap_counters.sc_ops_completed );
320
321 #ifdef SLAPD_MONITOR
322                 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
323                         ldap_pvt_mp_clear( slap_counters.sc_ops_initiated_[ i ] );
324                         ldap_pvt_mp_clear( slap_counters.sc_ops_completed_[ i ] );
325                 }
326 #endif /* SLAPD_MONITOR */
327                 break;
328
329         default:
330                 Debug( LDAP_DEBUG_ANY,
331                         "slap_destroy(): undefined mode (%d).\n", slapMode, 0, 0 );
332
333                 rc = 1;
334                 break;
335
336         }
337
338         ldap_pvt_thread_destroy();
339
340         /* should destory the above mutex */
341         return rc;
342 }