]> git.sur5r.net Git - openldap/blob - servers/slapd/init.c
40def39a95e2206e6d5b19bbb1f99a86e104e923
[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 unsigned long                   num_ops_initiated = 0;
76 unsigned long                   num_ops_completed = 0;
77 #ifdef SLAPD_MONITOR
78 unsigned long                   num_ops_initiated_[SLAP_OP_LAST];
79 unsigned long                   num_ops_completed_[SLAP_OP_LAST];
80 #endif /* SLAPD_MONITOR */
81 ldap_pvt_thread_mutex_t num_ops_mutex;
82
83 unsigned long                   num_entries_sent;
84 unsigned long                   num_refs_sent;
85 unsigned long                   num_bytes_sent;
86 unsigned long                   num_pdu_sent;
87 ldap_pvt_thread_mutex_t num_sent_mutex;
88 /*
89  * these mutexes must be used when calling the entry2str()
90  * routine since it returns a pointer to static data.
91  */
92 ldap_pvt_thread_mutex_t entry2str_mutex;
93 ldap_pvt_thread_mutex_t replog_mutex;
94
95 static const char* slap_name = NULL;
96 int slapMode = SLAP_UNDEFINED_MODE;
97
98 int
99 slap_init( int mode, const char *name )
100 {
101         int rc;
102
103         assert( mode );
104
105         if( slapMode != SLAP_UNDEFINED_MODE ) {
106                 Debug( LDAP_DEBUG_ANY,
107                  "%s init: init called twice (old=%d, new=%d)\n",
108                  name, slapMode, mode );
109
110                 return 1;
111         }
112
113         slapMode = mode;
114
115         switch ( slapMode & SLAP_MODE ) {
116                 case SLAP_SERVER_MODE:
117                 case SLAP_TOOL_MODE:
118                         Debug( LDAP_DEBUG_TRACE,
119                                 "%s init: initiated %s.\n",     name,
120                                 (mode & SLAP_MODE) == SLAP_TOOL_MODE ? "tool" : "server",
121                                 0 );
122
123
124                         slap_name = name;
125         
126                         (void) ldap_pvt_thread_initialize();
127
128                         ldap_pvt_thread_pool_init( &connection_pool,
129                                 connection_pool_max, 0);
130
131                         ldap_pvt_thread_mutex_init( &entry2str_mutex );
132                         ldap_pvt_thread_mutex_init( &replog_mutex );
133                         ldap_pvt_thread_mutex_init( &num_ops_mutex );
134                         ldap_pvt_thread_mutex_init( &num_sent_mutex );
135
136 #ifdef SLAPD_MONITOR
137                         {
138                                 int i;
139                                 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
140                                         num_ops_initiated_[ i ] = 0;
141                                         num_ops_completed_[ i ] = 0;
142                                 }
143                         }
144 #endif
145
146 #ifndef HAVE_GMTIME_R
147                         ldap_pvt_thread_mutex_init( &gmtime_mutex );
148 #endif
149 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
150                         ldap_pvt_thread_mutex_init( &passwd_mutex );
151 #endif
152
153                         rc = slap_sasl_init();
154
155                         if( rc == 0 ) {
156                                 rc = backend_init( );
157                         }
158
159                         break;
160
161                 default:
162                         Debug( LDAP_DEBUG_ANY,
163                                 "%s init: undefined mode (%d).\n", name, mode, 0 );
164
165                         rc = 1;
166                         break;
167         }
168
169         return rc;
170 }
171
172 int slap_startup( Backend *be )
173 {
174         int rc;
175
176         Debug( LDAP_DEBUG_TRACE,
177                 "%s startup: initiated.\n",
178                 slap_name, 0, 0 );
179
180
181         rc = backend_startup( be );
182
183 #ifdef LDAP_SLAPI
184         if( rc == 0 ) {
185                 Slapi_PBlock *pb = slapi_pblock_new();
186
187                 if ( slapi_int_call_plugins( NULL, SLAPI_PLUGIN_START_FN, pb ) < 0 ) {
188                         rc = -1;
189                 }
190                 slapi_pblock_destroy( pb );
191         }
192 #endif /* LDAP_SLAPI */
193
194         return rc;
195 }
196
197 int slap_shutdown( Backend *be )
198 {
199         int rc;
200 #ifdef LDAP_SLAPI
201         Slapi_PBlock *pb;
202 #endif
203
204         Debug( LDAP_DEBUG_TRACE,
205                 "%s shutdown: initiated\n",
206                 slap_name, 0, 0 );
207
208         /* let backends do whatever cleanup they need to do */
209         rc = backend_shutdown( be ); 
210
211 #ifdef LDAP_SLAPI
212         pb = slapi_pblock_new();
213         (void) slapi_int_call_plugins( NULL, SLAPI_PLUGIN_CLOSE_FN, pb );
214         slapi_pblock_destroy( pb );
215 #endif /* LDAP_SLAPI */
216
217         return rc;
218 }
219
220 int slap_destroy(void)
221 {
222         int rc;
223
224         Debug( LDAP_DEBUG_TRACE,
225                 "%s destroy: freeing system resources.\n",
226                 slap_name, 0, 0 );
227
228
229         rc = backend_destroy();
230
231         slap_sasl_destroy();
232
233         entry_destroy();
234
235         ldap_pvt_thread_destroy();
236
237         /* should destory the above mutex */
238         return rc;
239 }