1 /* init.c - initialize various things */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
31 #include <ac/socket.h>
32 #include <ac/string.h>
38 #include "slapi/slapi.h"
42 * read-only global variables or variables only written by the listener
43 * thread (after they are initialized) - no need to protect them with a mutex.
48 int ldap_syslog = LDAP_DEBUG_STATS;
54 int ldap_syslog_level = LOG_DEBUG;
57 BerVarray default_referral = NULL;
59 struct berval AllUser = BER_BVC( LDAP_ALL_USER_ATTRIBUTES );
60 struct berval AllOper = BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES );
61 struct berval NoAttrs = BER_BVC( LDAP_NO_ATTRS );
64 * global variables that need mutex protection
66 ldap_pvt_thread_pool_t connection_pool;
67 int connection_pool_max = SLAP_MAX_WORKER_THREADS;
69 ldap_pvt_thread_mutex_t gmtime_mutex;
71 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
72 ldap_pvt_thread_mutex_t passwd_mutex;
75 unsigned long num_ops_initiated = 0;
76 unsigned long num_ops_completed = 0;
78 unsigned long num_ops_initiated_[SLAP_OP_LAST];
79 unsigned long num_ops_completed_[SLAP_OP_LAST];
80 #endif /* SLAPD_MONITOR */
81 ldap_pvt_thread_mutex_t num_ops_mutex;
83 unsigned long num_entries_sent;
84 unsigned long num_refs_sent;
85 unsigned long num_bytes_sent;
86 unsigned long num_pdu_sent;
87 ldap_pvt_thread_mutex_t num_sent_mutex;
89 * these mutexes must be used when calling the entry2str()
90 * routine since it returns a pointer to static data.
92 ldap_pvt_thread_mutex_t entry2str_mutex;
93 ldap_pvt_thread_mutex_t replog_mutex;
95 static const char* slap_name = NULL;
96 int slapMode = SLAP_UNDEFINED_MODE;
99 slap_init( int mode, const char *name )
105 if( slapMode != SLAP_UNDEFINED_MODE ) {
107 LDAP_LOG( OPERATION, CRIT,
108 "init: %s init called twice (old=%d, new=%d)\n",
109 name, slapMode, mode );
111 Debug( LDAP_DEBUG_ANY,
112 "%s init: init called twice (old=%d, new=%d)\n",
113 name, slapMode, mode );
121 switch ( slapMode & SLAP_MODE ) {
122 case SLAP_SERVER_MODE:
125 LDAP_LOG( OPERATION, DETAIL1,
126 "init: %s initiation, initiated %s.\n",
127 name, (mode & SLAP_MODE) == SLAP_TOOL_MODE ?
128 "tool" : "server", 0 );
130 Debug( LDAP_DEBUG_TRACE,
131 "%s init: initiated %s.\n", name,
132 (mode & SLAP_MODE) == SLAP_TOOL_MODE ? "tool" : "server",
139 (void) ldap_pvt_thread_initialize();
141 ldap_pvt_thread_pool_init(&connection_pool, connection_pool_max, 0);
143 ldap_pvt_thread_mutex_init( &entry2str_mutex );
144 ldap_pvt_thread_mutex_init( &replog_mutex );
145 ldap_pvt_thread_mutex_init( &num_ops_mutex );
146 ldap_pvt_thread_mutex_init( &num_sent_mutex );
151 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
152 num_ops_initiated_[ i ] = 0;
153 num_ops_completed_[ i ] = 0;
158 #ifndef HAVE_GMTIME_R
159 ldap_pvt_thread_mutex_init( &gmtime_mutex );
161 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
162 ldap_pvt_thread_mutex_init( &passwd_mutex );
165 rc = slap_sasl_init();
168 rc = backend_init( );
175 LDAP_LOG( OPERATION, ERR,
176 "init: %s init, undefined mode (%d).\n", name, mode, 0 );
178 Debug( LDAP_DEBUG_ANY,
179 "%s init: undefined mode (%d).\n", name, mode, 0 );
189 int slap_startup( Backend *be )
194 LDAP_LOG( OPERATION, CRIT, "slap_startup: %s started\n", slap_name, 0, 0 );
196 Debug( LDAP_DEBUG_TRACE,
197 "%s startup: initiated.\n",
202 rc = backend_startup( be );
206 Slapi_PBlock *pb = slapi_pblock_new();
208 if ( slapi_int_call_plugins( NULL, SLAPI_PLUGIN_START_FN, pb ) < 0 ) {
211 slapi_pblock_destroy( pb );
213 #endif /* LDAP_SLAPI */
218 int slap_shutdown( Backend *be )
226 LDAP_LOG( OPERATION, CRIT,
227 "slap_shutdown: %s shutdown initiated.\n", slap_name, 0, 0);
229 Debug( LDAP_DEBUG_TRACE,
230 "%s shutdown: initiated\n",
237 /* let backends do whatever cleanup they need to do */
238 rc = backend_shutdown( be );
241 pb = slapi_pblock_new();
242 (void) slapi_int_call_plugins( NULL, SLAPI_PLUGIN_CLOSE_FN, pb );
243 slapi_pblock_destroy( pb );
244 #endif /* LDAP_SLAPI */
249 int slap_destroy(void)
254 LDAP_LOG( OPERATION, INFO,
255 "slap_destroy: %s freeing system resources.\n", slap_name, 0, 0);
257 Debug( LDAP_DEBUG_TRACE,
258 "%s shutdown: freeing system resources.\n",
263 rc = backend_destroy();
267 ldap_pvt_thread_destroy();
269 /* should destory the above mutex */