]> git.sur5r.net Git - openldap/blob - servers/slapd/init.c
ACIs almost entirely factored out of slapd
[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                 Debug( LDAP_DEBUG_ANY,
96                  "%s init: init called twice (old=%d, new=%d)\n",
97                  name, slapMode, mode );
98
99                 return 1;
100         }
101
102         slapMode = mode;
103
104         switch ( slapMode & SLAP_MODE ) {
105         case SLAP_SERVER_MODE:
106                 ldap_pvt_thread_pool_init( &connection_pool,
107                                 connection_pool_max, 0);
108
109                 /* FALLTHRU */
110         case SLAP_TOOL_MODE:
111                 Debug( LDAP_DEBUG_TRACE,
112                         "%s init: initiated %s.\n",     name,
113                         (mode & SLAP_MODE) == SLAP_TOOL_MODE ? "tool" : "server",
114                         0 );
115
116                 slap_name = name;
117
118                 ldap_pvt_thread_mutex_init( &entry2str_mutex );
119                 ldap_pvt_thread_mutex_init( &replog_mutex );
120
121                 ldap_pvt_thread_mutex_init( &slap_counters.sc_sent_mutex );
122                 ldap_pvt_thread_mutex_init( &slap_counters.sc_ops_mutex );
123                 ldap_pvt_mp_init( slap_counters.sc_bytes );
124                 ldap_pvt_mp_init( slap_counters.sc_pdu );
125                 ldap_pvt_mp_init( slap_counters.sc_entries );
126                 ldap_pvt_mp_init( slap_counters.sc_refs );
127
128                 ldap_pvt_mp_init( slap_counters.sc_ops_initiated );
129                 ldap_pvt_mp_init( slap_counters.sc_ops_completed );
130
131                 ldap_pvt_thread_mutex_init( &slapd_rq.rq_mutex );
132                 LDAP_STAILQ_INIT( &slapd_rq.task_list );
133                 LDAP_STAILQ_INIT( &slapd_rq.run_list );
134
135 #ifdef SLAPD_MONITOR
136                 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
137                         ldap_pvt_mp_init( slap_counters.sc_ops_initiated_[ i ] );
138                         ldap_pvt_mp_init( slap_counters.sc_ops_completed_[ i ] );
139                 }
140 #endif /* SLAPD_MONITOR */
141
142 #ifndef HAVE_GMTIME_R
143                 ldap_pvt_thread_mutex_init( &gmtime_mutex );
144 #endif
145                 slap_passwd_init();
146
147                 rc = slap_sasl_init();
148
149                 if( rc == 0 ) {
150                         rc = backend_init( );
151                 }
152
153                 break;
154
155         default:
156                 Debug( LDAP_DEBUG_ANY,
157                         "%s init: undefined mode (%d).\n", name, mode, 0 );
158
159                 rc = 1;
160                 break;
161         }
162
163         return rc;
164 }
165
166 int slap_startup( Backend *be )
167 {
168         int rc;
169
170         Debug( LDAP_DEBUG_TRACE,
171                 "%s startup: initiated.\n",
172                 slap_name, 0, 0 );
173
174
175         rc = backend_startup( be );
176
177 #ifdef LDAP_SLAPI
178         if( rc == 0 ) {
179                 Slapi_PBlock *pb = slapi_pblock_new();
180
181                 if ( slapi_int_call_plugins( frontendDB, SLAPI_PLUGIN_START_FN, pb ) < 0 ) {
182                         rc = -1;
183                 }
184                 slapi_pblock_destroy( pb );
185         }
186 #endif /* LDAP_SLAPI */
187
188         return rc;
189 }
190
191 int slap_shutdown( Backend *be )
192 {
193         int rc;
194 #ifdef LDAP_SLAPI
195         Slapi_PBlock *pb;
196 #endif
197
198         Debug( LDAP_DEBUG_TRACE,
199                 "%s shutdown: initiated\n",
200                 slap_name, 0, 0 );
201
202         /* let backends do whatever cleanup they need to do */
203         rc = backend_shutdown( be ); 
204
205 #ifdef LDAP_SLAPI
206         pb = slapi_pblock_new();
207         (void) slapi_int_call_plugins( frontendDB, SLAPI_PLUGIN_CLOSE_FN, pb );
208         slapi_pblock_destroy( pb );
209 #endif /* LDAP_SLAPI */
210
211         return rc;
212 }
213
214 int slap_destroy(void)
215 {
216         int rc;
217         int i;
218
219         Debug( LDAP_DEBUG_TRACE,
220                 "%s destroy: freeing system resources.\n",
221                 slap_name, 0, 0 );
222
223         if ( default_referral ) {
224                 ber_bvarray_free( default_referral );
225         }
226
227         rc = backend_destroy();
228
229         slap_sasl_destroy();
230
231         entry_destroy();
232
233         switch ( slapMode & SLAP_MODE ) {
234         case SLAP_SERVER_MODE:
235         case SLAP_TOOL_MODE:
236
237                 ldap_pvt_thread_mutex_destroy( &slap_counters.sc_sent_mutex );
238                 ldap_pvt_thread_mutex_destroy( &slap_counters.sc_ops_mutex );
239                 ldap_pvt_mp_clear( slap_counters.sc_bytes );
240                 ldap_pvt_mp_clear( slap_counters.sc_pdu );
241                 ldap_pvt_mp_clear( slap_counters.sc_entries );
242                 ldap_pvt_mp_clear( slap_counters.sc_refs );
243                 ldap_pvt_mp_clear( slap_counters.sc_ops_initiated );
244                 ldap_pvt_mp_clear( slap_counters.sc_ops_completed );
245
246 #ifdef SLAPD_MONITOR
247                 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
248                         ldap_pvt_mp_clear( slap_counters.sc_ops_initiated_[ i ] );
249                         ldap_pvt_mp_clear( slap_counters.sc_ops_completed_[ i ] );
250                 }
251 #endif /* SLAPD_MONITOR */
252                 break;
253
254         default:
255                 Debug( LDAP_DEBUG_ANY,
256                         "slap_destroy(): undefined mode (%d).\n", slapMode, 0, 0 );
257
258                 rc = 1;
259                 break;
260
261         }
262
263         ldap_pvt_thread_destroy();
264
265         /* should destory the above mutex */
266         return rc;
267 }