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