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