1 /* $NetBSD: init.c,v 1.1.1.4 2014/05/28 09:58:46 tron Exp $ */ 2 3 /* init.c - initialize various things */ 4 /* $OpenLDAP$ */ 5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 6 * 7 * Copyright 1998-2014 The OpenLDAP Foundation. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted only as authorized by the OpenLDAP 12 * Public License. 13 * 14 * A copy of this license is available in the file LICENSE in the 15 * top-level directory of the distribution or, alternatively, at 16 * <http://www.OpenLDAP.org/license.html>. 17 */ 18 /* Portions Copyright (c) 1995 Regents of the University of Michigan. 19 * All rights reserved. 20 * 21 * Redistribution and use in source and binary forms are permitted 22 * provided that this notice is preserved and that due credit is given 23 * to the University of Michigan at Ann Arbor. The name of the University 24 * may not be used to endorse or promote products derived from this 25 * software without specific prior written permission. This software 26 * is provided ``as is'' without express or implied warranty. 27 */ 28 29 #include "portable.h" 30 31 #include <stdio.h> 32 33 #include <ac/socket.h> 34 #include <ac/string.h> 35 #include <ac/time.h> 36 37 #include "slap.h" 38 #include "lber_pvt.h" 39 40 #include "ldap_rq.h" 41 42 /* 43 * read-only global variables or variables only written by the listener 44 * thread (after they are initialized) - no need to protect them with a mutex. 45 */ 46 int slap_debug = 0; 47 48 #ifdef LDAP_DEBUG 49 int ldap_syslog = LDAP_DEBUG_STATS; 50 #else 51 int ldap_syslog; 52 #endif 53 54 #ifdef LOG_DEBUG 55 int ldap_syslog_level = LOG_DEBUG; 56 #endif 57 58 BerVarray default_referral = NULL; 59 60 /* 61 * global variables that need mutex protection 62 */ 63 ldap_pvt_thread_pool_t connection_pool; 64 int connection_pool_max = SLAP_MAX_WORKER_THREADS; 65 int slap_tool_thread_max = 1; 66 67 slap_counters_t slap_counters, *slap_counters_list; 68 69 static const char* slap_name = NULL; 70 int slapMode = SLAP_UNDEFINED_MODE; 71 72 int 73 slap_init( int mode, const char *name ) 74 { 75 int rc; 76 77 assert( mode ); 78 79 if ( slapMode != SLAP_UNDEFINED_MODE ) { 80 /* Make sure we write something to stderr */ 81 slap_debug |= LDAP_DEBUG_NONE; 82 Debug( LDAP_DEBUG_ANY, 83 "%s init: init called twice (old=%d, new=%d)\n", 84 name, slapMode, mode ); 85 86 return 1; 87 } 88 89 slapMode = mode; 90 91 slap_op_init(); 92 93 #ifdef SLAPD_MODULES 94 if ( module_init() != 0 ) { 95 slap_debug |= LDAP_DEBUG_NONE; 96 Debug( LDAP_DEBUG_ANY, 97 "%s: module_init failed\n", 98 name, 0, 0 ); 99 return 1; 100 } 101 #endif 102 103 if ( slap_schema_init( ) != 0 ) { 104 slap_debug |= LDAP_DEBUG_NONE; 105 Debug( LDAP_DEBUG_ANY, 106 "%s: slap_schema_init failed\n", 107 name, 0, 0 ); 108 return 1; 109 } 110 111 if ( filter_init() != 0 ) { 112 slap_debug |= LDAP_DEBUG_NONE; 113 Debug( LDAP_DEBUG_ANY, 114 "%s: filter_init failed\n", 115 name, 0, 0 ); 116 return 1; 117 } 118 119 if ( entry_init() != 0 ) { 120 slap_debug |= LDAP_DEBUG_NONE; 121 Debug( LDAP_DEBUG_ANY, 122 "%s: entry_init failed\n", 123 name, 0, 0 ); 124 return 1; 125 } 126 127 switch ( slapMode & SLAP_MODE ) { 128 case SLAP_SERVER_MODE: 129 root_dse_init(); 130 131 /* FALLTHRU */ 132 case SLAP_TOOL_MODE: 133 Debug( LDAP_DEBUG_TRACE, 134 "%s init: initiated %s.\n", name, 135 (mode & SLAP_MODE) == SLAP_TOOL_MODE ? "tool" : "server", 136 0 ); 137 138 slap_name = name; 139 140 ldap_pvt_thread_pool_init( &connection_pool, 141 connection_pool_max, 0); 142 143 slap_counters_init( &slap_counters ); 144 145 ldap_pvt_thread_mutex_init( &slapd_rq.rq_mutex ); 146 LDAP_STAILQ_INIT( &slapd_rq.task_list ); 147 LDAP_STAILQ_INIT( &slapd_rq.run_list ); 148 149 slap_passwd_init(); 150 151 rc = slap_sasl_init(); 152 153 if( rc == 0 ) { 154 rc = backend_init( ); 155 } 156 if ( rc ) 157 return rc; 158 159 break; 160 161 default: 162 slap_debug |= LDAP_DEBUG_NONE; 163 Debug( LDAP_DEBUG_ANY, 164 "%s init: undefined mode (%d).\n", name, mode, 0 ); 165 166 rc = 1; 167 break; 168 } 169 170 if ( slap_controls_init( ) != 0 ) { 171 slap_debug |= LDAP_DEBUG_NONE; 172 Debug( LDAP_DEBUG_ANY, 173 "%s: slap_controls_init failed\n", 174 name, 0, 0 ); 175 return 1; 176 } 177 178 if ( frontend_init() ) { 179 slap_debug |= LDAP_DEBUG_NONE; 180 Debug( LDAP_DEBUG_ANY, 181 "%s: frontend_init failed\n", 182 name, 0, 0 ); 183 return 1; 184 } 185 186 if ( overlay_init() ) { 187 slap_debug |= LDAP_DEBUG_NONE; 188 Debug( LDAP_DEBUG_ANY, 189 "%s: overlay_init failed\n", 190 name, 0, 0 ); 191 return 1; 192 } 193 194 if ( glue_sub_init() ) { 195 slap_debug |= LDAP_DEBUG_NONE; 196 Debug( LDAP_DEBUG_ANY, 197 "%s: glue/subordinate init failed\n", 198 name, 0, 0 ); 199 200 return 1; 201 } 202 203 if ( acl_init() ) { 204 slap_debug |= LDAP_DEBUG_NONE; 205 Debug( LDAP_DEBUG_ANY, 206 "%s: acl_init failed\n", 207 name, 0, 0 ); 208 return 1; 209 } 210 211 return rc; 212 } 213 214 int slap_startup( Backend *be ) 215 { 216 int rc; 217 Debug( LDAP_DEBUG_TRACE, 218 "%s startup: initiated.\n", 219 slap_name, 0, 0 ); 220 221 rc = backend_startup( be ); 222 if ( !rc && ( slapMode & SLAP_SERVER_MODE )) 223 slapMode |= SLAP_SERVER_RUNNING; 224 return rc; 225 } 226 227 int slap_shutdown( Backend *be ) 228 { 229 Debug( LDAP_DEBUG_TRACE, 230 "%s shutdown: initiated\n", 231 slap_name, 0, 0 ); 232 233 /* let backends do whatever cleanup they need to do */ 234 return backend_shutdown( be ); 235 } 236 237 int slap_destroy(void) 238 { 239 int rc; 240 241 Debug( LDAP_DEBUG_TRACE, 242 "%s destroy: freeing system resources.\n", 243 slap_name, 0, 0 ); 244 245 if ( default_referral ) { 246 ber_bvarray_free( default_referral ); 247 } 248 249 /* clear out any thread-keys for the main thread */ 250 ldap_pvt_thread_pool_context_reset( ldap_pvt_thread_pool_context()); 251 252 rc = backend_destroy(); 253 254 slap_sasl_destroy(); 255 256 /* rootdse destroy goes before entry_destroy() 257 * because it may use entry_free() */ 258 root_dse_destroy(); 259 entry_destroy(); 260 261 switch ( slapMode & SLAP_MODE ) { 262 case SLAP_SERVER_MODE: 263 case SLAP_TOOL_MODE: 264 slap_counters_destroy( &slap_counters ); 265 break; 266 267 default: 268 Debug( LDAP_DEBUG_ANY, 269 "slap_destroy(): undefined mode (%d).\n", slapMode, 0, 0 ); 270 271 rc = 1; 272 break; 273 274 } 275 276 slap_op_destroy(); 277 278 ldap_pvt_thread_destroy(); 279 280 /* should destroy the above mutex */ 281 return rc; 282 } 283 284 void slap_counters_init( slap_counters_t *sc ) 285 { 286 int i; 287 288 ldap_pvt_thread_mutex_init( &sc->sc_mutex ); 289 ldap_pvt_mp_init( sc->sc_bytes ); 290 ldap_pvt_mp_init( sc->sc_pdu ); 291 ldap_pvt_mp_init( sc->sc_entries ); 292 ldap_pvt_mp_init( sc->sc_refs ); 293 294 ldap_pvt_mp_init( sc->sc_ops_initiated ); 295 ldap_pvt_mp_init( sc->sc_ops_completed ); 296 297 #ifdef SLAPD_MONITOR 298 for ( i = 0; i < SLAP_OP_LAST; i++ ) { 299 ldap_pvt_mp_init( sc->sc_ops_initiated_[ i ] ); 300 ldap_pvt_mp_init( sc->sc_ops_completed_[ i ] ); 301 } 302 #endif /* SLAPD_MONITOR */ 303 } 304 305 void slap_counters_destroy( slap_counters_t *sc ) 306 { 307 int i; 308 309 ldap_pvt_thread_mutex_destroy( &sc->sc_mutex ); 310 ldap_pvt_mp_clear( sc->sc_bytes ); 311 ldap_pvt_mp_clear( sc->sc_pdu ); 312 ldap_pvt_mp_clear( sc->sc_entries ); 313 ldap_pvt_mp_clear( sc->sc_refs ); 314 315 ldap_pvt_mp_clear( sc->sc_ops_initiated ); 316 ldap_pvt_mp_clear( sc->sc_ops_completed ); 317 318 #ifdef SLAPD_MONITOR 319 for ( i = 0; i < SLAP_OP_LAST; i++ ) { 320 ldap_pvt_mp_clear( sc->sc_ops_initiated_[ i ] ); 321 ldap_pvt_mp_clear( sc->sc_ops_completed_[ i ] ); 322 } 323 #endif /* SLAPD_MONITOR */ 324 } 325 326