]> git.sur5r.net Git - openldap/blob - servers/slapd/init.c
Exit loop after matching command is found in openldap_ldap_init_w_conf
[openldap] / servers / slapd / init.c
1 /* init.c - initialize various things */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-1999 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 int                             active_threads;
42 ldap_pvt_thread_mutex_t active_threads_mutex;
43 ldap_pvt_thread_cond_t  active_threads_cond;
44
45 ldap_pvt_thread_mutex_t gmtime_mutex;
46 #ifdef SLAPD_CRYPT
47 ldap_pvt_thread_mutex_t crypt_mutex;
48 #endif
49
50 int                             num_conns;
51 long                    num_ops_initiated;
52 long                    num_ops_completed;
53 ldap_pvt_thread_mutex_t num_ops_mutex;
54
55 long                    num_entries_sent;
56 long                    num_refs_sent;
57 long                    num_bytes_sent;
58 long                    num_pdu_sent;
59 ldap_pvt_thread_mutex_t num_sent_mutex;
60 /*
61  * these mutexes must be used when calling the entry2str()
62  * routine since it returns a pointer to static data.
63  */
64 ldap_pvt_thread_mutex_t entry2str_mutex;
65 ldap_pvt_thread_mutex_t replog_mutex;
66
67 static char* slap_name = NULL;
68 int slapMode = SLAP_UNDEFINED_MODE;
69
70 static ldap_pvt_thread_mutex_t  currenttime_mutex;
71
72 int
73 slap_init( int mode, char *name )
74 {
75         int rc;
76
77         assert( mode );
78
79         if( slapMode != SLAP_UNDEFINED_MODE ) {
80                 Debug( LDAP_DEBUG_ANY,
81                  "%s init: init called twice (old=%d, new=%d)\n",
82                  name, slapMode, mode );
83                 return 1;
84         }
85
86         slapMode = mode;
87
88         switch ( slapMode & SLAP_MODE ) {
89                 case SLAP_SERVER_MODE:
90                 case SLAP_TOOL_MODE:
91                         Debug( LDAP_DEBUG_TRACE,
92                                 "%s init: initiated %s.\n",
93                                 name, ( mode & SLAP_TOOL_MODE ) ? "tool" : "server", 0 );
94
95                         slap_name = name;
96         
97                         (void) ldap_pvt_thread_initialize();
98
99                         ldap_pvt_thread_mutex_init( &active_threads_mutex );
100                         ldap_pvt_thread_cond_init( &active_threads_cond );
101
102                         ldap_pvt_thread_mutex_init( &currenttime_mutex );
103                         ldap_pvt_thread_mutex_init( &entry2str_mutex );
104                         ldap_pvt_thread_mutex_init( &replog_mutex );
105                         ldap_pvt_thread_mutex_init( &num_ops_mutex );
106                         ldap_pvt_thread_mutex_init( &num_sent_mutex );
107
108                         ldap_pvt_thread_mutex_init( &gmtime_mutex );
109 #ifdef SLAPD_CRYPT
110                         ldap_pvt_thread_mutex_init( &crypt_mutex );
111 #endif
112
113                         rc = backend_init( );
114                         break;
115
116                 default:
117                         Debug( LDAP_DEBUG_ANY,
118                                 "%s init: undefined mode (%d).\n", name, mode, 0 );
119                         rc = 1;
120                         break;
121         }
122
123         return rc;
124 }
125
126 int slap_startup( Backend *be )
127 {
128         int rc;
129
130         Debug( LDAP_DEBUG_TRACE,
131                 "%s startup: initiated.\n",
132                 slap_name, 0, 0 );
133
134         rc = backend_startup( be );
135
136         if( rc == 0 ) {
137                 rc = sasl_init();
138         }
139
140         return rc;
141 }
142
143 int slap_shutdown( Backend *be )
144 {
145         int rc;
146
147         Debug( LDAP_DEBUG_TRACE,
148                 "%s shutdown: initiated\n",
149                 slap_name, 0, 0 );
150
151         sasl_destroy();
152
153         /* let backends do whatever cleanup they need to do */
154         rc = backend_shutdown( be ); 
155
156         return rc;
157 }
158
159 int slap_destroy(void)
160 {
161         int rc;
162
163         Debug( LDAP_DEBUG_TRACE,
164                 "%s shutdown: freeing system resources.\n",
165                 slap_name, 0, 0 );
166
167         rc = backend_destroy();
168
169         entry_destroy();
170
171         ldap_pvt_thread_destroy();
172
173         /* should destory the above mutex */
174         return rc;
175 }
176
177 /* should create a utils.c for these */
178 time_t slap_get_time(void)
179 {
180         time_t t;
181         ldap_pvt_thread_mutex_lock( &currenttime_mutex );
182         time( &t );
183         ldap_pvt_thread_mutex_unlock( &currenttime_mutex );
184         return t;
185 }