]> git.sur5r.net Git - openldap/blob - servers/slapd/init.c
Add OpenLDAP RCSid to *.[ch] in clients, libraries, and servers.
[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 int             ldap_syslog_level = LOG_DEBUG;
31 struct berval **default_referral = NULL;
32 int             g_argc;
33 char            **g_argv;
34
35 /*
36  * global variables that need mutex protection
37  */
38 int                             active_threads;
39 ldap_pvt_thread_mutex_t active_threads_mutex;
40 ldap_pvt_thread_cond_t  active_threads_cond;
41
42 ldap_pvt_thread_mutex_t gmtime_mutex;
43 #ifdef SLAPD_CRYPT
44 ldap_pvt_thread_mutex_t crypt_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 char* slap_name;
65 int slapMode = SLAP_UNDEFINED_MODE;
66
67 static ldap_pvt_thread_mutex_t  currenttime_mutex;
68
69 int
70 slap_init( int mode, char *name )
71 {
72         int rc;
73
74         assert( mode );
75
76         if( slapMode != SLAP_UNDEFINED_MODE ) {
77                 Debug( LDAP_DEBUG_ANY,
78                  "%s init: init called twice (old=%d, new=%d)\n",
79                  name, slapMode, mode );
80                 return 1;
81         }
82
83         slapMode = mode;
84
85         switch ( slapMode & SLAP_MODE ) {
86                 case SLAP_SERVER_MODE:
87                 case SLAP_TOOL_MODE:
88                         Debug( LDAP_DEBUG_TRACE,
89                                 "%s init: initiated %s.\n",
90                                 name, ( mode & SLAP_TOOL_MODE ) ? "tool" : "server", 0 );
91
92                         slap_name = name;
93         
94                         (void) ldap_pvt_thread_initialize();
95
96                         ldap_pvt_thread_mutex_init( &active_threads_mutex );
97                         ldap_pvt_thread_cond_init( &active_threads_cond );
98
99                         ldap_pvt_thread_mutex_init( &currenttime_mutex );
100                         ldap_pvt_thread_mutex_init( &entry2str_mutex );
101                         ldap_pvt_thread_mutex_init( &replog_mutex );
102                         ldap_pvt_thread_mutex_init( &num_ops_mutex );
103                         ldap_pvt_thread_mutex_init( &num_sent_mutex );
104
105                         ldap_pvt_thread_mutex_init( &gmtime_mutex );
106 #ifdef SLAPD_CRYPT
107                         ldap_pvt_thread_mutex_init( &crypt_mutex );
108 #endif
109
110                         rc = backend_init( );
111                         break;
112
113                 default:
114                         Debug( LDAP_DEBUG_ANY,
115                                 "%s init: undefined mode (%d).\n", name, mode, 0 );
116                         rc = 1;
117                         break;
118         }
119
120         return rc;
121 }
122
123 int slap_startup( Backend *be )
124 {
125         int rc;
126
127         Debug( LDAP_DEBUG_TRACE,
128                 "%s startup: initiated.\n",
129                 slap_name, 0, 0 );
130
131         rc = backend_startup( be );
132
133         if( rc == 0 ) {
134                 rc = sasl_init();
135         }
136
137         return rc;
138 }
139
140 int slap_shutdown( Backend *be )
141 {
142         int rc;
143
144         Debug( LDAP_DEBUG_TRACE,
145                 "%s shutdown: initiated\n",
146                 slap_name, 0, 0 );
147
148         sasl_destroy();
149
150         /* let backends do whatever cleanup they need to do */
151         rc = backend_shutdown( be ); 
152
153         return rc;
154 }
155
156 int slap_destroy(void)
157 {
158         int rc;
159
160         Debug( LDAP_DEBUG_TRACE,
161                 "%s shutdown: freeing system resources.\n",
162                 slap_name, 0, 0 );
163
164         rc = backend_destroy();
165
166         entry_destroy();
167
168         ldap_pvt_thread_destroy();
169
170         /* should destory the above mutex */
171         return rc;
172 }
173
174 /* should create a utils.c for these */
175 time_t slap_get_time(void)
176 {
177         time_t t;
178         ldap_pvt_thread_mutex_lock( &currenttime_mutex );
179         time( &t );
180         ldap_pvt_thread_mutex_unlock( &currenttime_mutex );
181         return t;
182 }