]> git.sur5r.net Git - openldap/blob - servers/slapd/init.c
fix format (ITS#2640?)
[openldap] / servers / slapd / init.c
1 /* init.c - initialize various things */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/socket.h>
13 #include <ac/string.h>
14 #include <ac/time.h>
15
16 #include "slap.h"
17 #include "lber_pvt.h"
18 #ifdef LDAP_SLAPI
19 #include "slapi.h"
20 #endif
21
22 /*
23  * read-only global variables or variables only written by the listener
24  * thread (after they are initialized) - no need to protect them with a mutex.
25  */
26 int             slap_debug = 0;
27
28 #ifdef LDAP_DEBUG
29 int             ldap_syslog = LDAP_DEBUG_STATS;
30 #else
31 int             ldap_syslog;
32 #endif
33
34 #ifdef LOG_DEBUG
35 int             ldap_syslog_level = LOG_DEBUG;
36 #endif
37
38 BerVarray default_referral = NULL;
39
40 struct berval AllUser = BER_BVC( LDAP_ALL_USER_ATTRIBUTES );
41 struct berval AllOper = BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES );
42 struct berval NoAttrs = BER_BVC( LDAP_NO_ATTRS );
43
44 /*
45  * global variables that need mutex protection
46  */
47 ldap_pvt_thread_pool_t  connection_pool;
48 int                     connection_pool_max = SLAP_MAX_WORKER_THREADS;
49 ldap_pvt_thread_mutex_t gmtime_mutex;
50 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
51 ldap_pvt_thread_mutex_t passwd_mutex;
52 #endif
53
54 unsigned long                   num_ops_initiated = 0;
55 unsigned long                   num_ops_completed = 0;
56 #ifdef SLAPD_MONITOR
57 unsigned long                   num_ops_initiated_[SLAP_OP_LAST];
58 unsigned long                   num_ops_completed_[SLAP_OP_LAST];
59 #endif /* SLAPD_MONITOR */
60 ldap_pvt_thread_mutex_t num_ops_mutex;
61
62 unsigned long                   num_entries_sent;
63 unsigned long                   num_refs_sent;
64 unsigned long                   num_bytes_sent;
65 unsigned long                   num_pdu_sent;
66 ldap_pvt_thread_mutex_t num_sent_mutex;
67 /*
68  * these mutexes must be used when calling the entry2str()
69  * routine since it returns a pointer to static data.
70  */
71 ldap_pvt_thread_mutex_t entry2str_mutex;
72 ldap_pvt_thread_mutex_t replog_mutex;
73
74 static const char* slap_name = NULL;
75 int slapMode = SLAP_UNDEFINED_MODE;
76
77 int
78 slap_init( int mode, const char *name )
79 {
80         int rc;
81
82         assert( mode );
83
84         if( slapMode != SLAP_UNDEFINED_MODE ) {
85 #ifdef NEW_LOGGING
86                 LDAP_LOG( OPERATION, CRIT, 
87                         "init: %s init called twice (old=%d, new=%d)\n",
88                         name, slapMode, mode );
89 #else
90                 Debug( LDAP_DEBUG_ANY,
91                  "%s init: init called twice (old=%d, new=%d)\n",
92                  name, slapMode, mode );
93 #endif
94
95                 return 1;
96         }
97
98         slapMode = mode;
99
100         switch ( slapMode & SLAP_MODE ) {
101                 case SLAP_SERVER_MODE:
102                 case SLAP_TOOL_MODE:
103 #ifdef NEW_LOGGING
104                         LDAP_LOG( OPERATION, DETAIL1, 
105                                 "init: %s initiation, initiated %s.\n",
106                                 name, (mode & SLAP_MODE) == SLAP_TOOL_MODE ? 
107                                   "tool" : "server", 0 );
108 #else
109                         Debug( LDAP_DEBUG_TRACE,
110                                 "%s init: initiated %s.\n",     name,
111                                 (mode & SLAP_MODE) == SLAP_TOOL_MODE ? "tool" : "server",
112                                 0 );
113 #endif
114
115
116                         slap_name = name;
117         
118                         (void) ldap_pvt_thread_initialize();
119
120                         ldap_pvt_thread_pool_init(&connection_pool, connection_pool_max, 0);
121
122                         ldap_pvt_thread_mutex_init( &entry2str_mutex );
123                         ldap_pvt_thread_mutex_init( &replog_mutex );
124                         ldap_pvt_thread_mutex_init( &num_ops_mutex );
125                         ldap_pvt_thread_mutex_init( &num_sent_mutex );
126
127 #ifdef SLAPD_MONITOR
128                         {
129                                 int i;
130                                 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
131                                         num_ops_initiated_[ i ] = 0;
132                                         num_ops_completed_[ i ] = 0;
133                                 }
134                         }
135 #endif
136
137                         ldap_pvt_thread_mutex_init( &gmtime_mutex );
138 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
139                         ldap_pvt_thread_mutex_init( &passwd_mutex );
140 #endif
141
142                         rc = slap_sasl_init();
143
144                         if( rc == 0 ) {
145                                 rc = backend_init( );
146                         }
147
148                         break;
149
150                 default:
151 #ifdef NEW_LOGGING
152                         LDAP_LOG( OPERATION, ERR, 
153                                 "init: %s init, undefined mode (%d).\n", name, mode, 0 );
154 #else
155                         Debug( LDAP_DEBUG_ANY,
156                                 "%s init: undefined mode (%d).\n", name, mode, 0 );
157 #endif
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 #ifdef NEW_LOGGING
171         LDAP_LOG( OPERATION, CRIT, "slap_startup: %s started\n", slap_name, 0, 0 );
172 #else
173         Debug( LDAP_DEBUG_TRACE,
174                 "%s startup: initiated.\n",
175                 slap_name, 0, 0 );
176 #endif
177
178
179         rc = backend_startup( be );
180
181 #ifdef LDAP_SLAPI
182         if( rc == 0 ) {
183                 Slapi_PBlock *pb = slapi_pblock_new();
184
185                 if ( doPluginFNs( NULL, SLAPI_PLUGIN_START_FN, pb ) < 0 ) {
186                         rc = -1;
187                 }
188                 slapi_pblock_destroy( pb );
189         }
190 #endif /* LDAP_SLAPI */
191
192         return rc;
193 }
194
195 int slap_shutdown( Backend *be )
196 {
197         int rc;
198 #ifdef LDAP_SLAPI
199         Slapi_PBlock *pb;
200 #endif
201
202 #ifdef NEW_LOGGING
203         LDAP_LOG( OPERATION, CRIT, 
204                 "slap_shutdown: %s shutdown initiated.\n", slap_name, 0, 0);
205 #else
206         Debug( LDAP_DEBUG_TRACE,
207                 "%s shutdown: initiated\n",
208                 slap_name, 0, 0 );
209 #endif
210
211
212         slap_sasl_destroy();
213
214         /* let backends do whatever cleanup they need to do */
215         rc = backend_shutdown( be ); 
216
217 #ifdef LDAP_SLAPI
218         pb = slapi_pblock_new( );
219         (void) doPluginFNs( NULL, SLAPI_PLUGIN_CLOSE_FN, pb );
220         slapi_pblock_destroy( pb );
221 #endif /* LDAP_SLAPI */
222
223         return rc;
224 }
225
226 int slap_destroy(void)
227 {
228         int rc;
229
230 #ifdef NEW_LOGGING
231         LDAP_LOG( OPERATION, INFO, 
232                 "slap_destroy: %s freeing system resources.\n", slap_name, 0, 0);
233 #else
234         Debug( LDAP_DEBUG_TRACE,
235                 "%s shutdown: freeing system resources.\n",
236                 slap_name, 0, 0 );
237 #endif
238
239
240         rc = backend_destroy();
241
242         entry_destroy();
243
244         ldap_pvt_thread_destroy();
245
246         /* should destory the above mutex */
247         return rc;
248 }