]> git.sur5r.net Git - openldap/blob - servers/slapd/init.c
c20bb4bbd64ce8eb4bf65517d362568f2196352c
[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-2004 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 /*
42  * read-only global variables or variables only written by the listener
43  * thread (after they are initialized) - no need to protect them with a mutex.
44  */
45 int             slap_debug = 0;
46
47 #ifdef LDAP_DEBUG
48 int             ldap_syslog = LDAP_DEBUG_STATS;
49 #else
50 int             ldap_syslog;
51 #endif
52
53 #ifdef LOG_DEBUG
54 int             ldap_syslog_level = LOG_DEBUG;
55 #endif
56
57 BerVarray default_referral = NULL;
58
59 struct berval AllUser = BER_BVC( LDAP_ALL_USER_ATTRIBUTES );
60 struct berval AllOper = BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES );
61 struct berval NoAttrs = BER_BVC( LDAP_NO_ATTRS );
62
63 /*
64  * global variables that need mutex protection
65  */
66 ldap_pvt_thread_pool_t  connection_pool;
67 int                     connection_pool_max = SLAP_MAX_WORKER_THREADS;
68 #ifndef HAVE_GMTIME_R
69 ldap_pvt_thread_mutex_t gmtime_mutex;
70 #endif
71 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
72 ldap_pvt_thread_mutex_t passwd_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                 Debug( LDAP_DEBUG_ANY,
97                  "%s init: init called twice (old=%d, new=%d)\n",
98                  name, slapMode, mode );
99
100                 return 1;
101         }
102
103         slapMode = mode;
104
105         switch ( slapMode & SLAP_MODE ) {
106         case SLAP_SERVER_MODE:
107         case SLAP_TOOL_MODE:
108                 Debug( LDAP_DEBUG_TRACE,
109                         "%s init: initiated %s.\n",     name,
110                         (mode & SLAP_MODE) == SLAP_TOOL_MODE ? "tool" : "server",
111                         0 );
112
113
114                 slap_name = name;
115
116                 ldap_pvt_thread_pool_init( &connection_pool,
117                                 connection_pool_max, 0);
118
119                 ldap_pvt_thread_mutex_init( &entry2str_mutex );
120                 ldap_pvt_thread_mutex_init( &replog_mutex );
121
122                 ldap_pvt_thread_mutex_init( &slap_counters.sc_sent_mutex );
123                 ldap_pvt_thread_mutex_init( &slap_counters.sc_ops_mutex );
124 #ifdef HAVE_GMP
125                 mpz_init( slap_counters.sc_bytes );
126                 mpz_init( slap_counters.sc_pdu );
127                 mpz_init( slap_counters.sc_entries );
128                 mpz_init( slap_counters.sc_refs );
129
130                 mpz_init( slap_counters.sc_ops_completed );
131                 mpz_init( slap_counters.sc_ops_initiated );
132
133 #ifdef SLAPD_MONITOR
134                 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
135                         mpz_init( slap_counters.sc_ops_initiated_[ i ] );
136                         mpz_init( slap_counters.sc_ops_completed_[ i ] );
137                 }
138 #endif /* SLAPD_MONITOR */
139 #else /* ! HAVE_GMP */
140                 slap_counters.sc_bytes = 0;
141                 slap_counters.sc_pdu = 0;
142                 slap_counters.sc_entries = 0;
143                 slap_counters.sc_refs = 0;
144
145                 slap_counters.sc_ops_completed = 0;
146                 slap_counters.sc_ops_initiated = 0;
147 #ifdef SLAPD_MONITOR
148                 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
149                         slap_counters.sc_ops_initiated_[ i ] = 0;
150                         slap_counters.sc_ops_completed_[ i ] = 0;
151                 }
152 #endif /* SLAPD_MONITOR */
153 #endif /* ! HAVE_GMP */
154
155 #ifndef HAVE_GMTIME_R
156                 ldap_pvt_thread_mutex_init( &gmtime_mutex );
157 #endif
158 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
159                 ldap_pvt_thread_mutex_init( &passwd_mutex );
160 #endif
161
162                 rc = slap_sasl_init();
163
164                 if( rc == 0 ) {
165                         rc = backend_init( );
166                 }
167
168                 break;
169
170         default:
171                 Debug( LDAP_DEBUG_ANY,
172                         "%s init: undefined mode (%d).\n", name, mode, 0 );
173
174                 rc = 1;
175                 break;
176         }
177
178         return rc;
179 }
180
181 int slap_startup( Backend *be )
182 {
183         int rc;
184
185         Debug( LDAP_DEBUG_TRACE,
186                 "%s startup: initiated.\n",
187                 slap_name, 0, 0 );
188
189
190         rc = backend_startup( be );
191
192 #ifdef LDAP_SLAPI
193         if( rc == 0 ) {
194                 Slapi_PBlock *pb = slapi_pblock_new();
195
196                 if ( slapi_int_call_plugins( NULL, SLAPI_PLUGIN_START_FN, pb ) < 0 ) {
197                         rc = -1;
198                 }
199                 slapi_pblock_destroy( pb );
200         }
201 #endif /* LDAP_SLAPI */
202
203         return rc;
204 }
205
206 int slap_shutdown( Backend *be )
207 {
208         int rc;
209 #ifdef LDAP_SLAPI
210         Slapi_PBlock *pb;
211 #endif
212
213         Debug( LDAP_DEBUG_TRACE,
214                 "%s shutdown: initiated\n",
215                 slap_name, 0, 0 );
216
217         /* let backends do whatever cleanup they need to do */
218         rc = backend_shutdown( be ); 
219
220 #ifdef LDAP_SLAPI
221         pb = slapi_pblock_new();
222         (void) slapi_int_call_plugins( NULL, SLAPI_PLUGIN_CLOSE_FN, pb );
223         slapi_pblock_destroy( pb );
224 #endif /* LDAP_SLAPI */
225
226         return rc;
227 }
228
229 int slap_destroy(void)
230 {
231         int rc;
232         int i;
233
234         Debug( LDAP_DEBUG_TRACE,
235                 "%s destroy: freeing system resources.\n",
236                 slap_name, 0, 0 );
237
238         if ( default_referral ) {
239                 ber_bvarray_free( default_referral );
240         }
241
242         rc = backend_destroy();
243
244         slap_sasl_destroy();
245
246         entry_destroy();
247
248         switch ( slapMode & SLAP_MODE ) {
249         case SLAP_SERVER_MODE:
250         case SLAP_TOOL_MODE:
251
252                 ldap_pvt_thread_mutex_destroy( &slap_counters.sc_sent_mutex );
253                 ldap_pvt_thread_mutex_destroy( &slap_counters.sc_ops_mutex );
254 #ifdef HAVE_GMP
255                 mpz_clear( slap_counters.sc_bytes );
256                 mpz_clear( slap_counters.sc_pdu );
257                 mpz_clear( slap_counters.sc_entries );
258                 mpz_clear( slap_counters.sc_refs );
259                 mpz_clear( slap_counters.sc_ops_completed );
260                 mpz_clear( slap_counters.sc_ops_initiated );
261
262 #ifdef SLAPD_MONITOR
263                 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
264                         mpz_clear( slap_counters.sc_ops_initiated_[ i ] );
265                         mpz_clear( slap_counters.sc_ops_completed_[ i ] );
266                 }
267 #endif /* SLAPD_MONITOR */
268 #endif /* HAVE_GMP */
269                 break;
270
271         default:
272                 Debug( LDAP_DEBUG_ANY,
273                         "slap_destroy(): undefined mode (%d).\n", slapMode, 0, 0 );
274
275                 rc = 1;
276                 break;
277
278         }
279
280         ldap_pvt_thread_destroy();
281
282         /* should destory the above mutex */
283         return rc;
284 }