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