]> git.sur5r.net Git - openldap/blob - servers/slapd/init.c
Don't pass NULL DN to rewrite_session(), causes assertion failure
[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
19 /*
20  * read-only global variables or variables only written by the listener
21  * thread (after they are initialized) - no need to protect them with a mutex.
22  */
23 int             slap_debug = 0;
24
25 #ifdef LDAP_DEBUG
26 int             ldap_syslog = LDAP_DEBUG_STATS;
27 #else
28 int             ldap_syslog;
29 #endif
30
31 #ifdef LOG_DEBUG
32 int             ldap_syslog_level = LOG_DEBUG;
33 #endif
34
35 BerVarray default_referral = NULL;
36
37 struct berval AllUser = BER_BVC( LDAP_ALL_USER_ATTRIBUTES );
38 struct berval AllOper = BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES );
39 struct berval NoAttrs = BER_BVC( LDAP_NO_ATTRS );
40
41 /*
42  * global variables that need mutex protection
43  */
44 ldap_pvt_thread_pool_t  connection_pool;
45 int                     connection_pool_max = SLAP_MAX_WORKER_THREADS;
46 ldap_pvt_thread_mutex_t gmtime_mutex;
47 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
48 ldap_pvt_thread_mutex_t passwd_mutex;
49 #endif
50
51 unsigned long                   num_ops_initiated = 0;
52 unsigned long                   num_ops_completed = 0;
53 #ifdef SLAPD_MONITOR
54 unsigned long                   num_ops_initiated_[SLAP_OP_LAST];
55 unsigned long                   num_ops_completed_[SLAP_OP_LAST];
56 #endif /* SLAPD_MONITOR */
57 ldap_pvt_thread_mutex_t num_ops_mutex;
58
59 unsigned long                   num_entries_sent;
60 unsigned long                   num_refs_sent;
61 unsigned long                   num_bytes_sent;
62 unsigned long                   num_pdu_sent;
63 ldap_pvt_thread_mutex_t num_sent_mutex;
64 /*
65  * these mutexes must be used when calling the entry2str()
66  * routine since it returns a pointer to static data.
67  */
68 ldap_pvt_thread_mutex_t entry2str_mutex;
69 ldap_pvt_thread_mutex_t replog_mutex;
70
71 static const char* slap_name = NULL;
72 int slapMode = SLAP_UNDEFINED_MODE;
73
74 int
75 slap_init( int mode, const char *name )
76 {
77         int rc;
78
79         assert( mode );
80
81         if( slapMode != SLAP_UNDEFINED_MODE ) {
82 #ifdef NEW_LOGGING
83                 LDAP_LOG( OPERATION, CRIT, 
84                         "init: %s init called twice (old=%d, new=%d)\n",
85                         name, slapMode, mode );
86 #else
87                 Debug( LDAP_DEBUG_ANY,
88                  "%s init: init called twice (old=%d, new=%d)\n",
89                  name, slapMode, mode );
90 #endif
91
92                 return 1;
93         }
94
95         slapMode = mode;
96
97         switch ( slapMode & SLAP_MODE ) {
98                 case SLAP_SERVER_MODE:
99                 case SLAP_TOOL_MODE:
100 #ifdef NEW_LOGGING
101                         LDAP_LOG( OPERATION, DETAIL1, 
102                                 "init: %s initiation, initiated %s.\n",
103                                 name, (mode & SLAP_MODE) == SLAP_TOOL_MODE ? 
104                                   "tool" : "server", 0 );
105 #else
106                         Debug( LDAP_DEBUG_TRACE,
107                                 "%s init: initiated %s.\n",     name,
108                                 (mode & SLAP_MODE) == SLAP_TOOL_MODE ? "tool" : "server",
109                                 0 );
110 #endif
111
112
113                         slap_name = name;
114         
115                         (void) ldap_pvt_thread_initialize();
116
117                         ldap_pvt_thread_pool_init(&connection_pool, connection_pool_max, 0);
118
119                         ldap_pvt_thread_mutex_init( &entry2str_mutex );
120                         ldap_pvt_thread_mutex_init( &replog_mutex );
121                         ldap_pvt_thread_mutex_init( &num_ops_mutex );
122                         ldap_pvt_thread_mutex_init( &num_sent_mutex );
123
124 #ifdef SLAPD_MONITOR
125                         {
126                                 int i;
127                                 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
128                                         num_ops_initiated_[ i ] = 0;
129                                         num_ops_completed_[ i ] = 0;
130                                 }
131                         }
132 #endif
133
134                         ldap_pvt_thread_mutex_init( &gmtime_mutex );
135 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
136                         ldap_pvt_thread_mutex_init( &passwd_mutex );
137 #endif
138
139                         rc = slap_sasl_init();
140
141                         if( rc == 0 ) {
142                                 rc = backend_init( );
143                         }
144                         break;
145
146                 default:
147 #ifdef NEW_LOGGING
148                         LDAP_LOG( OPERATION, ERR, 
149                                 "init: %s init, undefined mode (%d).\n", name, mode, 0 );
150 #else
151                         Debug( LDAP_DEBUG_ANY,
152                                 "%s init: undefined mode (%d).\n", name, mode, 0 );
153 #endif
154
155                         rc = 1;
156                         break;
157         }
158
159         return rc;
160 }
161
162 int slap_startup( Backend *be )
163 {
164         int rc;
165
166 #ifdef NEW_LOGGING
167         LDAP_LOG( OPERATION, CRIT, "slap_startup: %s started\n", slap_name, 0, 0 );
168 #else
169         Debug( LDAP_DEBUG_TRACE,
170                 "%s startup: initiated.\n",
171                 slap_name, 0, 0 );
172 #endif
173
174
175         rc = backend_startup( be );
176
177         return rc;
178 }
179
180 int slap_shutdown( Backend *be )
181 {
182         int rc;
183
184 #ifdef NEW_LOGGING
185         LDAP_LOG( OPERATION, CRIT, 
186                 "slap_shutdown: %s shutdown initiated.\n", slap_name, 0, 0);
187 #else
188         Debug( LDAP_DEBUG_TRACE,
189                 "%s shutdown: initiated\n",
190                 slap_name, 0, 0 );
191 #endif
192
193
194         slap_sasl_destroy();
195
196         /* let backends do whatever cleanup they need to do */
197         rc = backend_shutdown( be ); 
198
199         return rc;
200 }
201
202 int slap_destroy(void)
203 {
204         int rc;
205
206 #ifdef NEW_LOGGING
207         LDAP_LOG( OPERATION, INFO, 
208                 "slap_destroy: %s freeing system resources.\n", slap_name, 0, 0);
209 #else
210         Debug( LDAP_DEBUG_TRACE,
211                 "%s shutdown: freeing system resources.\n",
212                 slap_name, 0, 0 );
213 #endif
214
215
216         rc = backend_destroy();
217
218         entry_destroy();
219
220         ldap_pvt_thread_destroy();
221
222         /* should destory the above mutex */
223         return rc;
224 }