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