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