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