]> git.sur5r.net Git - openldap/blob - servers/slapd/init.c
ITS#3353 consolidate slapd globals into a single struct
[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 struct berval AllUser = BER_BVC( LDAP_ALL_USER_ATTRIBUTES );
48 struct berval AllOper = BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES );
49 struct berval NoAttrs = BER_BVC( LDAP_NO_ATTRS );
50
51 /*
52  * global variables that need mutex protection
53  */
54 #ifdef LDAP_DEBUG
55 int ldap_syslog = LDAP_DEBUG_STATS;
56 #else
57 int ldap_syslog;
58 #endif
59
60 #ifdef LOG_DEBUG
61 int ldap_syslog_level = LOG_DEBUG;
62 #else
63 int ldap_syslog_level;
64 #endif
65
66 /*
67  * these mutexes must be used when calling the entry2str()
68  * routine since it returns a pointer to static data.
69  */
70 static const char* slap_name = NULL;
71 int slapMode = SLAP_UNDEFINED_MODE;
72
73 int
74 slap_init( int mode, const char *name )
75 {
76         int rc;
77         int i;
78
79         assert( mode );
80
81         if ( slapMode != SLAP_UNDEFINED_MODE ) {
82                 Debug( LDAP_DEBUG_ANY,
83                  "%s init: init called twice (old=%d, new=%d)\n",
84                  name, slapMode, mode );
85
86                 return 1;
87         }
88
89         slapMode = mode;
90
91         switch ( slapMode & SLAP_MODE ) {
92         case SLAP_SERVER_MODE:
93         case SLAP_TOOL_MODE:
94                 Debug( LDAP_DEBUG_TRACE,
95                         "%s init: initiated %s.\n",     name,
96                         (mode & SLAP_MODE) == SLAP_TOOL_MODE ? "tool" : "server",
97                         0 );
98
99
100                 slap_name = name;
101
102                 ldap_pvt_thread_pool_init( &SLAPD_GLOBAL(connection_pool),
103                                 SLAPD_GLOBAL(connection_pool_max), 0);
104
105                 ldap_pvt_thread_mutex_init( &SLAPD_GLOBAL(entry2str_mutex) );
106                 ldap_pvt_thread_mutex_init( &SLAPD_GLOBAL(replog_mutex) );
107
108                 ldap_pvt_thread_mutex_init( &SLAPD_GLOBAL(counters).sc_sent_mutex );
109                 ldap_pvt_thread_mutex_init( &SLAPD_GLOBAL(counters).sc_ops_mutex );
110                 ldap_pvt_mp_init( SLAPD_GLOBAL(counters).sc_bytes );
111                 ldap_pvt_mp_init( SLAPD_GLOBAL(counters).sc_pdu );
112                 ldap_pvt_mp_init( SLAPD_GLOBAL(counters).sc_entries );
113                 ldap_pvt_mp_init( SLAPD_GLOBAL(counters).sc_refs );
114
115                 ldap_pvt_mp_init( SLAPD_GLOBAL(counters).sc_ops_completed );
116                 ldap_pvt_mp_init( SLAPD_GLOBAL(counters).sc_ops_initiated );
117
118 #ifdef SLAPD_MONITOR
119                 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
120                         ldap_pvt_mp_init( SLAPD_GLOBAL(counters).sc_ops_initiated_[ i ] );
121                         ldap_pvt_mp_init( SLAPD_GLOBAL(counters).sc_ops_completed_[ i ] );
122                 }
123 #endif /* SLAPD_MONITOR */
124
125 #ifndef HAVE_GMTIME_R
126                 ldap_pvt_thread_mutex_init( &SLAPD_GLOBAL(gmtime_mutex) );
127 #endif
128 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
129                 ldap_pvt_thread_mutex_init( &passwd_mutex );
130 #endif
131
132                 rc = slap_sasl_init();
133
134                 if( rc == 0 ) {
135                         rc = backend_init( );
136                 }
137
138                 break;
139
140         default:
141                 Debug( LDAP_DEBUG_ANY,
142                         "%s init: undefined mode (%d).\n", name, mode, 0 );
143
144                 rc = 1;
145                 break;
146         }
147
148         return rc;
149 }
150
151 int slap_startup( Backend *be )
152 {
153         int rc;
154
155         Debug( LDAP_DEBUG_TRACE,
156                 "%s startup: initiated.\n",
157                 slap_name, 0, 0 );
158
159
160         rc = backend_startup( be );
161
162 #ifdef LDAP_SLAPI
163         if( rc == 0 ) {
164                 Slapi_PBlock *pb = slapi_pblock_new();
165
166                 if ( slapi_int_call_plugins( NULL, SLAPI_PLUGIN_START_FN, pb ) < 0 ) {
167                         rc = -1;
168                 }
169                 slapi_pblock_destroy( pb );
170         }
171 #endif /* LDAP_SLAPI */
172
173         return rc;
174 }
175
176 int slap_shutdown( Backend *be )
177 {
178         int rc;
179 #ifdef LDAP_SLAPI
180         Slapi_PBlock *pb;
181 #endif
182
183         Debug( LDAP_DEBUG_TRACE,
184                 "%s shutdown: initiated\n",
185                 slap_name, 0, 0 );
186
187         /* let backends do whatever cleanup they need to do */
188         rc = backend_shutdown( be ); 
189
190 #ifdef LDAP_SLAPI
191         pb = slapi_pblock_new();
192         (void) slapi_int_call_plugins( NULL, SLAPI_PLUGIN_CLOSE_FN, pb );
193         slapi_pblock_destroy( pb );
194 #endif /* LDAP_SLAPI */
195
196         return rc;
197 }
198
199 int slap_destroy(void)
200 {
201         int rc;
202         int i;
203
204         Debug( LDAP_DEBUG_TRACE,
205                 "%s destroy: freeing system resources.\n",
206                 slap_name, 0, 0 );
207
208         if ( SLAPD_GLOBAL(default_referral) ) {
209                 ber_bvarray_free( SLAPD_GLOBAL(default_referral) );
210         }
211
212         rc = backend_destroy();
213
214         slap_sasl_destroy();
215
216         entry_destroy();
217
218         switch ( slapMode & SLAP_MODE ) {
219         case SLAP_SERVER_MODE:
220         case SLAP_TOOL_MODE:
221
222                 ldap_pvt_thread_mutex_destroy( &SLAPD_GLOBAL(counters).sc_sent_mutex );
223                 ldap_pvt_thread_mutex_destroy( &SLAPD_GLOBAL(counters).sc_ops_mutex );
224                 ldap_pvt_mp_clear( SLAPD_GLOBAL(counters).sc_bytes );
225                 ldap_pvt_mp_clear( SLAPD_GLOBAL(counters).sc_pdu );
226                 ldap_pvt_mp_clear( SLAPD_GLOBAL(counters).sc_entries );
227                 ldap_pvt_mp_clear( SLAPD_GLOBAL(counters).sc_refs );
228                 ldap_pvt_mp_clear( SLAPD_GLOBAL(counters).sc_ops_completed );
229                 ldap_pvt_mp_clear( SLAPD_GLOBAL(counters).sc_ops_initiated );
230
231 #ifdef SLAPD_MONITOR
232                 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
233                         ldap_pvt_mp_clear( SLAPD_GLOBAL(counters).sc_ops_initiated_[ i ] );
234                         ldap_pvt_mp_clear( SLAPD_GLOBAL(counters).sc_ops_completed_[ i ] );
235                 }
236 #endif /* SLAPD_MONITOR */
237                 break;
238
239         default:
240                 Debug( LDAP_DEBUG_ANY,
241                         "slap_destroy(): undefined mode (%d).\n", slapMode, 0, 0 );
242
243                 rc = 1;
244                 break;
245
246         }
247
248         ldap_pvt_thread_destroy();
249
250         /* should destory the above mutex */
251         return rc;
252 }