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