]> git.sur5r.net Git - openldap/blob - servers/slapd/init.c
c807014185bd5547b4330c5395e7ebc3958471c0
[openldap] / servers / slapd / init.c
1 /* init.c - initialize various things */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/socket.h>
8 #include <ac/string.h>
9 #include <ac/time.h>
10
11 #include "slap.h"
12
13 /*
14  * read-only global variables or variables only written by the listener
15  * thread (after they are initialized) - no need to protect them with a mutex.
16  */
17 int             slap_debug = 0;
18
19 #ifdef LDAP_DEBUG
20 int             ldap_syslog = LDAP_DEBUG_STATS;
21 #else
22 int             ldap_syslog;
23 #endif
24
25 int             ldap_syslog_level = LOG_DEBUG;
26 char            *default_referral;
27 int             g_argc;
28 char            **g_argv;
29
30 /*
31  * global variables that need mutex protection
32  */
33 int                             active_threads;
34 ldap_pvt_thread_mutex_t active_threads_mutex;
35 ldap_pvt_thread_cond_t  active_threads_cond;
36
37 ldap_pvt_thread_mutex_t gmtime_mutex;
38 #ifdef SLAPD_CRYPT
39 ldap_pvt_thread_mutex_t crypt_mutex;
40 #endif
41
42 int                             num_conns;
43 long                    num_ops_initiated;
44 long                    num_ops_completed;
45 ldap_pvt_thread_mutex_t num_ops_mutex;
46
47 long                    num_entries_sent;
48 long                    num_bytes_sent;
49 ldap_pvt_thread_mutex_t num_sent_mutex;
50 /*
51  * these mutexes must be used when calling the entry2str()
52  * routine since it returns a pointer to static data.
53  */
54 ldap_pvt_thread_mutex_t entry2str_mutex;
55 ldap_pvt_thread_mutex_t replog_mutex;
56
57 static char* slap_name;
58 int slapMode = SLAP_UNDEFINED_MODE;
59
60 static ldap_pvt_thread_mutex_t  currenttime_mutex;
61
62 int
63 slap_init( int mode, char *name )
64 {
65         int rc;
66
67         if( slapMode != SLAP_UNDEFINED_MODE ) {
68                 Debug( LDAP_DEBUG_ANY,
69                  "%s init: init called twice (old=%d, new=%d)\n",
70                  name, slapMode, mode );
71                 return 1;
72         }
73
74         slapMode = mode;
75
76         switch ( slapMode ) {
77
78                 case SLAP_SERVER_MODE:
79                 case SLAP_TOOL_MODE:
80 #ifdef SLAPD_BDB2
81                 case SLAP_TIMEDSERVER_MODE:
82                 case SLAP_TOOLID_MODE:
83 #endif
84
85                         Debug( LDAP_DEBUG_TRACE,
86                                 "%s init: initiated %s.\n",
87                                 name, mode == SLAP_TOOL_MODE ? "tool" : "server", 0 );
88
89                         slap_name = name;
90         
91                         (void) ldap_pvt_thread_initialize();
92
93                         ldap_pvt_thread_mutex_init( &active_threads_mutex );
94                         ldap_pvt_thread_cond_init( &active_threads_cond );
95
96                         ldap_pvt_thread_mutex_init( &currenttime_mutex );
97                         ldap_pvt_thread_mutex_init( &entry2str_mutex );
98                         ldap_pvt_thread_mutex_init( &replog_mutex );
99                         ldap_pvt_thread_mutex_init( &num_ops_mutex );
100                         ldap_pvt_thread_mutex_init( &num_sent_mutex );
101
102                         ldap_pvt_thread_mutex_init( &gmtime_mutex );
103 #ifdef SLAPD_CRYPT
104                         ldap_pvt_thread_mutex_init( &crypt_mutex );
105 #endif
106
107                         rc = backend_init();
108                         break;
109
110                 default:
111                         Debug( LDAP_DEBUG_ANY,
112                                 "%s init: undefined mode (%d).\n", name, mode, 0 );
113                         rc = 1;
114                         break;
115         }
116
117         return rc;
118 }
119
120 int slap_startup(int dbnum)
121 {
122         int rc;
123
124         Debug( LDAP_DEBUG_TRACE,
125                 "%s startup: initiated.\n",
126                 slap_name, 0, 0 );
127
128         rc = backend_startup(dbnum);
129
130         return rc;
131 }
132
133 int slap_shutdown(int dbnum)
134 {
135         int rc;
136
137         Debug( LDAP_DEBUG_TRACE,
138                 "%s shutdown: initiated\n",
139                 slap_name, 0, 0 );
140
141         /* let backends do whatever cleanup they need to do */
142         rc = backend_shutdown(dbnum); 
143
144         return rc;
145 }
146
147 int slap_destroy(void)
148 {
149         int rc;
150
151         Debug( LDAP_DEBUG_TRACE,
152                 "%s shutdown: freeing system resources.\n",
153                 slap_name, 0, 0 );
154
155         rc = backend_destroy();
156
157         /* should destory the above mutex */
158         return rc;
159 }
160
161 /* should create a utils.c for these */
162 time_t slap_get_time(void)
163 {
164         time_t t;
165         ldap_pvt_thread_mutex_lock( &currenttime_mutex );
166         time( &t );
167         ldap_pvt_thread_mutex_unlock( &currenttime_mutex );
168         return t;
169 }