1158115Sume/*- 2158115Sume * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru> 3158115Sume * All rights reserved. 4158115Sume * 5158115Sume * Redistribution and use in source and binary forms, with or without 6158115Sume * modification, are permitted provided that the following conditions 7158115Sume * are met: 8158115Sume * 1. Redistributions of source code must retain the above copyright 9158115Sume * notice, this list of conditions and the following disclaimer. 10158115Sume * 2. Redistributions in binary form must reproduce the above copyright 11158115Sume * notice, this list of conditions and the following disclaimer in the 12158115Sume * documentation and/or other materials provided with the distribution. 13158115Sume * 14158115Sume * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15158115Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16158115Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17158115Sume * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18158115Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19158115Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20158115Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21158115Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22158115Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23158115Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24158115Sume * SUCH DAMAGE. 25158115Sume * 26158115Sume */ 27158115Sume 28158115Sume#include <sys/cdefs.h> 29158115Sume__FBSDID("$FreeBSD: stable/11/usr.sbin/nscd/config.c 315599 2017-03-20 00:54:45Z pfg $"); 30158115Sume 31194089Sdes#include <sys/stat.h> 32194089Sdes#include <sys/time.h> 33194089Sdes 34158115Sume#include <assert.h> 35158115Sume#include <math.h> 36194089Sdes#include <nsswitch.h> 37194089Sdes#include <pthread.h> 38158115Sume#include <stdio.h> 39158115Sume#include <stdlib.h> 40158115Sume#include <string.h> 41194089Sdes 42158115Sume#include "config.h" 43158115Sume#include "debug.h" 44158115Sume#include "log.h" 45158115Sume 46158115Sume/* 47158115Sume * Default entries, which always exist in the configuration 48158115Sume */ 49158115Sumeconst char *c_default_entries[6] = { 50158115Sume NSDB_PASSWD, 51158115Sume NSDB_GROUP, 52158115Sume NSDB_HOSTS, 53158115Sume NSDB_SERVICES, 54158115Sume NSDB_PROTOCOLS, 55158115Sume NSDB_RPC 56158115Sume }; 57158115Sume 58158115Sumestatic int configuration_entry_cmp(const void *, const void *); 59158115Sumestatic int configuration_entry_sort_cmp(const void *, const void *); 60158115Sumestatic int configuration_entry_cache_mp_sort_cmp(const void *, const void *); 61158115Sumestatic int configuration_entry_cache_mp_cmp(const void *, const void *); 62158115Sumestatic int configuration_entry_cache_mp_part_cmp(const void *, const void *); 63158115Sumestatic struct configuration_entry *create_configuration_entry(const char *, 64158115Sume struct timeval const *, struct timeval const *, 65158115Sume struct common_cache_entry_params const *, 66158115Sume struct common_cache_entry_params const *, 67158115Sume struct mp_cache_entry_params const *); 68158115Sume 69158115Sumestatic int 70158115Sumeconfiguration_entry_sort_cmp(const void *e1, const void *e2) 71158115Sume{ 72158115Sume return (strcmp((*((struct configuration_entry **)e1))->name, 73158115Sume (*((struct configuration_entry **)e2))->name 74158115Sume )); 75158115Sume} 76158115Sume 77158115Sumestatic int 78158115Sumeconfiguration_entry_cmp(const void *e1, const void *e2) 79158115Sume{ 80158115Sume return (strcmp((const char *)e1, 81158115Sume (*((struct configuration_entry **)e2))->name 82158115Sume )); 83158115Sume} 84158115Sume 85158115Sumestatic int 86158115Sumeconfiguration_entry_cache_mp_sort_cmp(const void *e1, const void *e2) 87158115Sume{ 88158115Sume return (strcmp((*((cache_entry *)e1))->params->entry_name, 89158115Sume (*((cache_entry *)e2))->params->entry_name 90158115Sume )); 91158115Sume} 92158115Sume 93158115Sumestatic int 94158115Sumeconfiguration_entry_cache_mp_cmp(const void *e1, const void *e2) 95158115Sume{ 96158115Sume return (strcmp((const char *)e1, 97158115Sume (*((cache_entry *)e2))->params->entry_name 98158115Sume )); 99158115Sume} 100158115Sume 101158115Sumestatic int 102158115Sumeconfiguration_entry_cache_mp_part_cmp(const void *e1, const void *e2) 103158115Sume{ 104158115Sume return (strncmp((const char *)e1, 105158115Sume (*((cache_entry *)e2))->params->entry_name, 106158115Sume strlen((const char *)e1) 107158115Sume )); 108158115Sume} 109158115Sume 110158115Sumestatic struct configuration_entry * 111158115Sumecreate_configuration_entry(const char *name, 112158115Sume struct timeval const *common_timeout, 113158115Sume struct timeval const *mp_timeout, 114158115Sume struct common_cache_entry_params const *positive_params, 115158115Sume struct common_cache_entry_params const *negative_params, 116158115Sume struct mp_cache_entry_params const *mp_params) 117158115Sume{ 118158115Sume struct configuration_entry *retval; 119158115Sume size_t size; 120158115Sume int res; 121158115Sume 122158115Sume TRACE_IN(create_configuration_entry); 123158115Sume assert(name != NULL); 124158115Sume assert(positive_params != NULL); 125158115Sume assert(negative_params != NULL); 126158115Sume assert(mp_params != NULL); 127158115Sume 128194104Sdes retval = calloc(1, 129194104Sdes sizeof(*retval)); 130158115Sume assert(retval != NULL); 131158115Sume 132158115Sume res = pthread_mutex_init(&retval->positive_cache_lock, NULL); 133158115Sume if (res != 0) { 134158115Sume free(retval); 135158115Sume LOG_ERR_2("create_configuration_entry", 136158115Sume "can't create positive cache lock"); 137158115Sume TRACE_OUT(create_configuration_entry); 138158115Sume return (NULL); 139158115Sume } 140158115Sume 141158115Sume res = pthread_mutex_init(&retval->negative_cache_lock, NULL); 142158115Sume if (res != 0) { 143158115Sume pthread_mutex_destroy(&retval->positive_cache_lock); 144158115Sume free(retval); 145158115Sume LOG_ERR_2("create_configuration_entry", 146158115Sume "can't create negative cache lock"); 147158115Sume TRACE_OUT(create_configuration_entry); 148158115Sume return (NULL); 149158115Sume } 150158115Sume 151158115Sume res = pthread_mutex_init(&retval->mp_cache_lock, NULL); 152158115Sume if (res != 0) { 153158115Sume pthread_mutex_destroy(&retval->positive_cache_lock); 154158115Sume pthread_mutex_destroy(&retval->negative_cache_lock); 155158115Sume free(retval); 156158115Sume LOG_ERR_2("create_configuration_entry", 157158115Sume "can't create negative cache lock"); 158158115Sume TRACE_OUT(create_configuration_entry); 159158115Sume return (NULL); 160158115Sume } 161158115Sume 162158115Sume memcpy(&retval->positive_cache_params, positive_params, 163158115Sume sizeof(struct common_cache_entry_params)); 164158115Sume memcpy(&retval->negative_cache_params, negative_params, 165158115Sume sizeof(struct common_cache_entry_params)); 166158115Sume memcpy(&retval->mp_cache_params, mp_params, 167158115Sume sizeof(struct mp_cache_entry_params)); 168158115Sume 169158115Sume size = strlen(name); 170194104Sdes retval->name = calloc(1, size + 1); 171158115Sume assert(retval->name != NULL); 172158115Sume memcpy(retval->name, name, size); 173158115Sume 174158115Sume memcpy(&retval->common_query_timeout, common_timeout, 175158115Sume sizeof(struct timeval)); 176158115Sume memcpy(&retval->mp_query_timeout, mp_timeout, 177158115Sume sizeof(struct timeval)); 178158115Sume 179194097Sdes asprintf(&retval->positive_cache_params.cep.entry_name, "%s+", name); 180194097Sdes assert(retval->positive_cache_params.cep.entry_name != NULL); 181158115Sume 182194097Sdes asprintf(&retval->negative_cache_params.cep.entry_name, "%s-", name); 183194097Sdes assert(retval->negative_cache_params.cep.entry_name != NULL); 184158115Sume 185194097Sdes asprintf(&retval->mp_cache_params.cep.entry_name, "%s*", name); 186194097Sdes assert(retval->mp_cache_params.cep.entry_name != NULL); 187158115Sume 188158115Sume TRACE_OUT(create_configuration_entry); 189158115Sume return (retval); 190158115Sume} 191158115Sume 192158115Sume/* 193158115Sume * Creates configuration entry and fills it with default values 194158115Sume */ 195158115Sumestruct configuration_entry * 196158115Sumecreate_def_configuration_entry(const char *name) 197158115Sume{ 198158115Sume struct common_cache_entry_params positive_params, negative_params; 199158115Sume struct mp_cache_entry_params mp_params; 200158115Sume struct timeval default_common_timeout, default_mp_timeout; 201158115Sume 202158115Sume struct configuration_entry *res = NULL; 203158115Sume 204158115Sume TRACE_IN(create_def_configuration_entry); 205158115Sume memset(&positive_params, 0, 206158115Sume sizeof(struct common_cache_entry_params)); 207194097Sdes positive_params.cep.entry_type = CET_COMMON; 208158115Sume positive_params.cache_entries_size = DEFAULT_CACHE_HT_SIZE; 209158115Sume positive_params.max_elemsize = DEFAULT_POSITIVE_ELEMENTS_SIZE; 210158115Sume positive_params.satisf_elemsize = DEFAULT_POSITIVE_ELEMENTS_SIZE / 2; 211158115Sume positive_params.max_lifetime.tv_sec = DEFAULT_POSITIVE_LIFETIME; 212238094Sse positive_params.confidence_threshold = DEFAULT_POSITIVE_CONF_THRESH; 213158115Sume positive_params.policy = CPT_LRU; 214158115Sume 215158115Sume memcpy(&negative_params, &positive_params, 216158115Sume sizeof(struct common_cache_entry_params)); 217158115Sume negative_params.max_elemsize = DEFAULT_NEGATIVE_ELEMENTS_SIZE; 218158115Sume negative_params.satisf_elemsize = DEFAULT_NEGATIVE_ELEMENTS_SIZE / 2; 219158115Sume negative_params.max_lifetime.tv_sec = DEFAULT_NEGATIVE_LIFETIME; 220238094Sse negative_params.confidence_threshold = DEFAULT_NEGATIVE_CONF_THRESH; 221158115Sume negative_params.policy = CPT_FIFO; 222158115Sume 223158115Sume memset(&default_common_timeout, 0, sizeof(struct timeval)); 224158115Sume default_common_timeout.tv_sec = DEFAULT_COMMON_ENTRY_TIMEOUT; 225158115Sume 226158115Sume memset(&default_mp_timeout, 0, sizeof(struct timeval)); 227158115Sume default_mp_timeout.tv_sec = DEFAULT_MP_ENTRY_TIMEOUT; 228158115Sume 229158115Sume memset(&mp_params, 0, 230158115Sume sizeof(struct mp_cache_entry_params)); 231194097Sdes mp_params.cep.entry_type = CET_MULTIPART; 232158115Sume mp_params.max_elemsize = DEFAULT_MULTIPART_ELEMENTS_SIZE; 233158115Sume mp_params.max_sessions = DEFAULT_MULITPART_SESSIONS_SIZE; 234158115Sume mp_params.max_lifetime.tv_sec = DEFAULT_MULITPART_LIFETIME; 235158115Sume 236158115Sume res = create_configuration_entry(name, &default_common_timeout, 237158115Sume &default_mp_timeout, &positive_params, &negative_params, 238158115Sume &mp_params); 239158115Sume 240158115Sume TRACE_OUT(create_def_configuration_entry); 241158115Sume return (res); 242158115Sume} 243158115Sume 244158115Sumevoid 245158115Sumedestroy_configuration_entry(struct configuration_entry *entry) 246158115Sume{ 247158115Sume TRACE_IN(destroy_configuration_entry); 248158115Sume assert(entry != NULL); 249158115Sume pthread_mutex_destroy(&entry->positive_cache_lock); 250158115Sume pthread_mutex_destroy(&entry->negative_cache_lock); 251158115Sume pthread_mutex_destroy(&entry->mp_cache_lock); 252158115Sume free(entry->name); 253194097Sdes free(entry->positive_cache_params.cep.entry_name); 254194097Sdes free(entry->negative_cache_params.cep.entry_name); 255194097Sdes free(entry->mp_cache_params.cep.entry_name); 256158115Sume free(entry->mp_cache_entries); 257158115Sume free(entry); 258158115Sume TRACE_OUT(destroy_configuration_entry); 259158115Sume} 260158115Sume 261158115Sumeint 262158115Sumeadd_configuration_entry(struct configuration *config, 263158115Sume struct configuration_entry *entry) 264158115Sume{ 265158115Sume TRACE_IN(add_configuration_entry); 266158115Sume assert(entry != NULL); 267158115Sume assert(entry->name != NULL); 268158115Sume if (configuration_find_entry(config, entry->name) != NULL) { 269158115Sume TRACE_OUT(add_configuration_entry); 270158115Sume return (-1); 271158115Sume } 272158115Sume 273158115Sume if (config->entries_size == config->entries_capacity) { 274158115Sume struct configuration_entry **new_entries; 275158115Sume 276158115Sume config->entries_capacity *= 2; 277315599Spfg new_entries = calloc(config->entries_capacity, 278315599Spfg sizeof(*new_entries)); 279158115Sume assert(new_entries != NULL); 280158115Sume memcpy(new_entries, config->entries, 281158115Sume sizeof(struct configuration_entry *) * 282158115Sume config->entries_size); 283158115Sume 284158115Sume free(config->entries); 285158115Sume config->entries = new_entries; 286158115Sume } 287158115Sume 288158115Sume config->entries[config->entries_size++] = entry; 289158115Sume qsort(config->entries, config->entries_size, 290158115Sume sizeof(struct configuration_entry *), 291158115Sume configuration_entry_sort_cmp); 292158115Sume 293158115Sume TRACE_OUT(add_configuration_entry); 294158115Sume return (0); 295158115Sume} 296158115Sume 297158115Sumesize_t 298158115Sumeconfiguration_get_entries_size(struct configuration *config) 299158115Sume{ 300158115Sume TRACE_IN(configuration_get_entries_size); 301158115Sume assert(config != NULL); 302158115Sume TRACE_OUT(configuration_get_entries_size); 303158115Sume return (config->entries_size); 304158115Sume} 305158115Sume 306158115Sumestruct configuration_entry * 307158115Sumeconfiguration_get_entry(struct configuration *config, size_t index) 308158115Sume{ 309158115Sume TRACE_IN(configuration_get_entry); 310158115Sume assert(config != NULL); 311158115Sume assert(index < config->entries_size); 312158115Sume TRACE_OUT(configuration_get_entry); 313158115Sume return (config->entries[index]); 314158115Sume} 315158115Sume 316158115Sumestruct configuration_entry * 317158115Sumeconfiguration_find_entry(struct configuration *config, 318158115Sume const char *name) 319158115Sume{ 320158115Sume struct configuration_entry **retval; 321158115Sume 322158115Sume TRACE_IN(configuration_find_entry); 323158115Sume 324158115Sume retval = bsearch(name, config->entries, config->entries_size, 325158115Sume sizeof(struct configuration_entry *), configuration_entry_cmp); 326158115Sume TRACE_OUT(configuration_find_entry); 327158115Sume 328158115Sume return ((retval != NULL) ? *retval : NULL); 329158115Sume} 330158115Sume 331158115Sume/* 332158115Sume * All multipart cache entries are stored in the configuration_entry in the 333158115Sume * sorted array (sorted by names). The 3 functions below manage this array. 334158115Sume */ 335158115Sume 336158115Sumeint 337158115Sumeconfiguration_entry_add_mp_cache_entry(struct configuration_entry *config_entry, 338158115Sume cache_entry c_entry) 339158115Sume{ 340158115Sume cache_entry *new_mp_entries, *old_mp_entries; 341158115Sume 342158115Sume TRACE_IN(configuration_entry_add_mp_cache_entry); 343158115Sume ++config_entry->mp_cache_entries_size; 344194104Sdes new_mp_entries = malloc(sizeof(*new_mp_entries) * 345158115Sume config_entry->mp_cache_entries_size); 346158115Sume assert(new_mp_entries != NULL); 347158115Sume new_mp_entries[0] = c_entry; 348158115Sume 349158115Sume if (config_entry->mp_cache_entries_size - 1 > 0) { 350158115Sume memcpy(new_mp_entries + 1, 351158115Sume config_entry->mp_cache_entries, 352158115Sume (config_entry->mp_cache_entries_size - 1) * 353158115Sume sizeof(cache_entry)); 354158115Sume } 355158115Sume 356158115Sume old_mp_entries = config_entry->mp_cache_entries; 357158115Sume config_entry->mp_cache_entries = new_mp_entries; 358158115Sume free(old_mp_entries); 359158115Sume 360158115Sume qsort(config_entry->mp_cache_entries, 361158115Sume config_entry->mp_cache_entries_size, 362158115Sume sizeof(cache_entry), 363158115Sume configuration_entry_cache_mp_sort_cmp); 364158115Sume 365158115Sume TRACE_OUT(configuration_entry_add_mp_cache_entry); 366158115Sume return (0); 367158115Sume} 368158115Sume 369158115Sumecache_entry 370158115Sumeconfiguration_entry_find_mp_cache_entry( 371158115Sume struct configuration_entry *config_entry, const char *mp_name) 372158115Sume{ 373158115Sume cache_entry *result; 374158115Sume 375158115Sume TRACE_IN(configuration_entry_find_mp_cache_entry); 376158115Sume result = bsearch(mp_name, config_entry->mp_cache_entries, 377158115Sume config_entry->mp_cache_entries_size, 378158115Sume sizeof(cache_entry), configuration_entry_cache_mp_cmp); 379158115Sume 380158115Sume if (result == NULL) { 381158115Sume TRACE_OUT(configuration_entry_find_mp_cache_entry); 382158115Sume return (NULL); 383158115Sume } else { 384158115Sume TRACE_OUT(configuration_entry_find_mp_cache_entry); 385158115Sume return (*result); 386158115Sume } 387158115Sume} 388158115Sume 389158115Sume/* 390158115Sume * Searches for all multipart entries with names starting with mp_name. 391158115Sume * Needed for cache flushing. 392158115Sume */ 393158115Sumeint 394158115Sumeconfiguration_entry_find_mp_cache_entries( 395158115Sume struct configuration_entry *config_entry, const char *mp_name, 396158115Sume cache_entry **start, cache_entry **finish) 397158115Sume{ 398158115Sume cache_entry *result; 399158115Sume 400158115Sume TRACE_IN(configuration_entry_find_mp_cache_entries); 401158115Sume result = bsearch(mp_name, config_entry->mp_cache_entries, 402158115Sume config_entry->mp_cache_entries_size, 403158115Sume sizeof(cache_entry), configuration_entry_cache_mp_part_cmp); 404158115Sume 405158115Sume if (result == NULL) { 406158115Sume TRACE_OUT(configuration_entry_find_mp_cache_entries); 407158115Sume return (-1); 408158115Sume } 409158115Sume 410158115Sume *start = result; 411158115Sume *finish = result + 1; 412158115Sume 413158115Sume while (*start != config_entry->mp_cache_entries) { 414158115Sume if (configuration_entry_cache_mp_part_cmp(mp_name, *start - 1) == 0) 415158115Sume *start = *start - 1; 416158115Sume else 417158115Sume break; 418158115Sume } 419158115Sume 420158115Sume while (*finish != config_entry->mp_cache_entries + 421158115Sume config_entry->mp_cache_entries_size) { 422158115Sume 423158115Sume if (configuration_entry_cache_mp_part_cmp( 424158115Sume mp_name, *finish) == 0) 425158115Sume *finish = *finish + 1; 426158115Sume else 427158115Sume break; 428158115Sume } 429158115Sume 430158115Sume TRACE_OUT(configuration_entry_find_mp_cache_entries); 431158115Sume return (0); 432158115Sume} 433158115Sume 434158115Sume/* 435158115Sume * Configuration entry uses rwlock to handle access to its fields. 436158115Sume */ 437158115Sumevoid 438158115Sumeconfiguration_lock_rdlock(struct configuration *config) 439158115Sume{ 440158115Sume TRACE_IN(configuration_lock_rdlock); 441158115Sume pthread_rwlock_rdlock(&config->rwlock); 442158115Sume TRACE_OUT(configuration_lock_rdlock); 443158115Sume} 444158115Sume 445158115Sumevoid 446158115Sumeconfiguration_lock_wrlock(struct configuration *config) 447158115Sume{ 448158115Sume TRACE_IN(configuration_lock_wrlock); 449158115Sume pthread_rwlock_wrlock(&config->rwlock); 450158115Sume TRACE_OUT(configuration_lock_wrlock); 451158115Sume} 452158115Sume 453158115Sumevoid 454158115Sumeconfiguration_unlock(struct configuration *config) 455158115Sume{ 456158115Sume TRACE_IN(configuration_unlock); 457158115Sume pthread_rwlock_unlock(&config->rwlock); 458158115Sume TRACE_OUT(configuration_unlock); 459158115Sume} 460158115Sume 461158115Sume/* 462158115Sume * Configuration entry uses 3 mutexes to handle cache operations. They are 463158115Sume * acquired by configuration_lock_entry and configuration_unlock_entry 464158115Sume * functions. 465158115Sume */ 466158115Sumevoid 467158115Sumeconfiguration_lock_entry(struct configuration_entry *entry, 468158115Sume enum config_entry_lock_type lock_type) 469158115Sume{ 470158115Sume TRACE_IN(configuration_lock_entry); 471158115Sume assert(entry != NULL); 472158115Sume 473158115Sume switch (lock_type) { 474158115Sume case CELT_POSITIVE: 475158115Sume pthread_mutex_lock(&entry->positive_cache_lock); 476158115Sume break; 477158115Sume case CELT_NEGATIVE: 478158115Sume pthread_mutex_lock(&entry->negative_cache_lock); 479158115Sume break; 480158115Sume case CELT_MULTIPART: 481158115Sume pthread_mutex_lock(&entry->mp_cache_lock); 482158115Sume break; 483158115Sume default: 484158115Sume /* should be unreachable */ 485158115Sume break; 486158115Sume } 487158115Sume TRACE_OUT(configuration_lock_entry); 488158115Sume} 489158115Sume 490158115Sumevoid 491158115Sumeconfiguration_unlock_entry(struct configuration_entry *entry, 492158115Sume enum config_entry_lock_type lock_type) 493158115Sume{ 494158115Sume TRACE_IN(configuration_unlock_entry); 495158115Sume assert(entry != NULL); 496158115Sume 497158115Sume switch (lock_type) { 498158115Sume case CELT_POSITIVE: 499158115Sume pthread_mutex_unlock(&entry->positive_cache_lock); 500158115Sume break; 501158115Sume case CELT_NEGATIVE: 502158115Sume pthread_mutex_unlock(&entry->negative_cache_lock); 503158115Sume break; 504158115Sume case CELT_MULTIPART: 505158115Sume pthread_mutex_unlock(&entry->mp_cache_lock); 506158115Sume break; 507158115Sume default: 508158115Sume /* should be unreachable */ 509158115Sume break; 510158115Sume } 511158115Sume TRACE_OUT(configuration_unlock_entry); 512158115Sume} 513158115Sume 514158115Sumestruct configuration * 515158115Sumeinit_configuration(void) 516158115Sume{ 517158115Sume struct configuration *retval; 518158115Sume 519158115Sume TRACE_IN(init_configuration); 520194104Sdes retval = calloc(1, sizeof(*retval)); 521158115Sume assert(retval != NULL); 522158115Sume 523158115Sume retval->entries_capacity = INITIAL_ENTRIES_CAPACITY; 524315599Spfg retval->entries = calloc(retval->entries_capacity, 525315599Spfg sizeof(*retval->entries)); 526158115Sume assert(retval->entries != NULL); 527158115Sume 528158115Sume pthread_rwlock_init(&retval->rwlock, NULL); 529158115Sume 530158115Sume TRACE_OUT(init_configuration); 531158115Sume return (retval); 532158115Sume} 533158115Sume 534158115Sumevoid 535158115Sumefill_configuration_defaults(struct configuration *config) 536158115Sume{ 537158115Sume size_t len, i; 538158115Sume 539158115Sume TRACE_IN(fill_configuration_defaults); 540158115Sume assert(config != NULL); 541158115Sume 542158115Sume if (config->socket_path != NULL) 543158115Sume free(config->socket_path); 544158115Sume 545158115Sume len = strlen(DEFAULT_SOCKET_PATH); 546194104Sdes config->socket_path = calloc(1, len + 1); 547158115Sume assert(config->socket_path != NULL); 548158115Sume memcpy(config->socket_path, DEFAULT_SOCKET_PATH, len); 549158115Sume 550158115Sume len = strlen(DEFAULT_PIDFILE_PATH); 551194104Sdes config->pidfile_path = calloc(1, len + 1); 552158115Sume assert(config->pidfile_path != NULL); 553158115Sume memcpy(config->pidfile_path, DEFAULT_PIDFILE_PATH, len); 554158115Sume 555158115Sume config->socket_mode = S_IFSOCK | S_IRUSR | S_IWUSR | 556158115Sume S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; 557158115Sume config->force_unlink = 1; 558158115Sume 559158115Sume config->query_timeout = DEFAULT_QUERY_TIMEOUT; 560158115Sume config->threads_num = DEFAULT_THREADS_NUM; 561158115Sume 562158115Sume for (i = 0; i < config->entries_size; ++i) 563158115Sume destroy_configuration_entry(config->entries[i]); 564158115Sume config->entries_size = 0; 565158115Sume 566158115Sume TRACE_OUT(fill_configuration_defaults); 567158115Sume} 568158115Sume 569158115Sumevoid 570158115Sumedestroy_configuration(struct configuration *config) 571158115Sume{ 572194096Sdes unsigned int i; 573194096Sdes 574158115Sume TRACE_IN(destroy_configuration); 575158115Sume assert(config != NULL); 576158115Sume free(config->pidfile_path); 577158115Sume free(config->socket_path); 578158115Sume 579158115Sume for (i = 0; i < config->entries_size; ++i) 580158115Sume destroy_configuration_entry(config->entries[i]); 581158115Sume free(config->entries); 582158115Sume 583158115Sume pthread_rwlock_destroy(&config->rwlock); 584158115Sume free(config); 585158115Sume TRACE_OUT(destroy_configuration); 586158115Sume} 587