--- libaitcfg/src/aitcfg.c 2012/09/19 15:22:32 1.8 +++ libaitcfg/src/aitcfg.c 2021/11/26 01:15:03 1.15 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitcfg.c,v 1.8 2012/09/19 15:22:32 misho Exp $ +* $Id: aitcfg.c,v 1.15 2021/11/26 01:15:03 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 +Copyright 2004 - 2021 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -44,8 +44,6 @@ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF TH SUCH DAMAGE. */ #include "global.h" -#include "aitcfg.h" -#include "aitpwd.h" #pragma GCC visibility push(hidden) @@ -53,7 +51,7 @@ SUCH DAMAGE. int cfg_Errno; char cfg_Error[STRSIZ]; -inline int +int cfg_Write(FILE *f, char *fmt, ...) { int ret = 0; @@ -66,15 +64,15 @@ cfg_Write(FILE *f, char *fmt, ...) return ret; } -inline int +int cfg_tree_cmp(struct tagCfg *a, struct tagCfg *b) { int ret; assert(a && b); - ret = ((AIT_KEY(&a->cfg_sec) << 16) | AIT_KEY(&a->cfg_attr)) - - ((AIT_KEY(&b->cfg_sec) << 16) | AIT_KEY(&b->cfg_attr)); + ret = ((AIT_KEY(&a->cfg_sec) << 15) | AIT_KEY(&a->cfg_attr)) - + ((AIT_KEY(&b->cfg_sec) << 15) | AIT_KEY(&b->cfg_attr)); if (ret < 0) return -1; @@ -90,21 +88,21 @@ RB_GENERATE(tagRC, tagCfg, cfg_node, cfg_tree_cmp); // cfg_GetErrno() Get error code of last operation -inline int +int cfg_GetErrno() { return cfg_Errno; } // cfg_GetError() Get error text of last operation -inline const char * +const char * cfg_GetError() { return cfg_Error; } // cfg_SetErr() Set error to variables for internal use!!! -inline void +void cfg_SetErr(int eno, char *estr, ...) { va_list lst; @@ -120,23 +118,45 @@ cfg_SetErr(int eno, char *estr, ...) /* * cfgInitConfig() - Init config root * - * @cfg = Config root - * return: -1 error or 0 ok + * return: NULL error or !=NULL allocated config root */ -int -cfgInitConfig(cfg_root_t * __restrict cfg) +cfg_root_t * +cfgInitConfig() { - if (!cfg) - return -1; + cfg_root_t *cfg = NULL; + cfg = e_malloc(sizeof(cfg_root_t)); + if (!cfg) { + cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError()); + return NULL; + } else + memset(cfg, 0, sizeof(cfg_root_t)); + pthread_mutex_init(&cfg->rc_mtx, NULL); - SLIST_INIT(cfg); + TAILQ_INIT(cfg); RB_INIT(cfg); - return 0; + return cfg; } /* + * cfgEndConfig() - Free resources & config root + * + * @pcfg = Config root + * return: none + */ +void +cfgEndConfig(cfg_root_t **pcfg) +{ + if (pcfg && *pcfg) { + cfgClearConfig(*pcfg); + pthread_mutex_destroy(&(*pcfg)->rc_mtx); + e_free(*pcfg); + *pcfg = NULL; + } +} + +/* * cfgLoadConfig() - Load config from file * * @cfgName = Config filename @@ -152,9 +172,15 @@ cfgLoadConfig(const char *cfgName, cfg_root_t * __rest if (!cfgName || !cfg) { cfg_SetErr(EINVAL, "Invalid parameter(s)"); return -1; - } else - cfgInitConfig(cfg); + } else { + memset(cfg, 0, sizeof(cfg_root_t)); + pthread_mutex_init(&cfg->rc_mtx, NULL); + + TAILQ_INIT(cfg); + RB_INIT(cfg); + } + f = fopen(cfgName, "r"); if (!f) { LOGERR; @@ -182,13 +208,13 @@ cfgClearConfig(cfg_root_t * __restrict cfg) return; CFG_RC_LOCK(cfg); - while ((av = SLIST_FIRST(cfg))) { - SLIST_REMOVE_HEAD(cfg, cfg_next); + while ((av = TAILQ_FIRST(cfg))) { + TAILQ_REMOVE(cfg, av, cfg_next); AIT_FREE_VAL(&av->cfg_val); AIT_FREE_VAL(&av->cfg_attr); AIT_FREE_VAL(&av->cfg_sec); - io_free(av); + e_free(av); } cfg->rbh_root = NULL; CFG_RC_UNLOCK(cfg); @@ -242,15 +268,20 @@ cfgCreateConfig(const char *csConfigName, cfg_root_t * /* * cfgInitPasswd() - Init password root * - * @pwd = Password root - * return: -1 error or 0 ok + * return: NULL error or !=NULL allocated password root */ -int -cfgInitPasswd(pwd_root_t * __restrict pwd) +pwd_root_t * +cfgInitPasswd() { - if (!pwd) - return -1; + pwd_root_t *pwd = NULL; + pwd = e_malloc(sizeof(pwd_root_t)); + if (!pwd) { + cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError()); + return NULL; + } else + memset(pwd, 0, sizeof(pwd_root_t)); + pthread_mutex_init(&pwd->pwd_mtx, NULL); SLIST_INIT(pwd); @@ -259,6 +290,23 @@ cfgInitPasswd(pwd_root_t * __restrict pwd) } /* + * cfgEndPasswd() - Free resources & password root + * + * @ppwd = Password root + * return: none + */ +void +cfgEndPasswd(pwd_root_t **ppwd) +{ + if (ppwd && *ppwd) { + cfgClearPasswd(*ppwd); + pthread_mutex_destroy(&(*ppwd)->pwd_mtx); + e_free(*ppwd); + *ppwd = NULL; + } +} + +/* * cfgLoadPasswd() - Load passwords from file * * @pwdName = Passwords filename @@ -274,9 +322,15 @@ cfgLoadPasswd(const char *pwdName, pwd_root_t * __rest if (!pwdName || !pwd) { cfg_SetErr(EINVAL, "Invalid parameter(s)"); return -1; - } else - cfgInitPasswd(pwd); + } else { + memset(pwd, 0, sizeof(pwd_root_t)); + pthread_mutex_init(&pwd->pwd_mtx, NULL); + + SLIST_INIT(pwd); + RB_INIT(pwd); + } + f = fopen(pwdName, "r"); if (!f) { LOGERR; @@ -317,7 +371,7 @@ cfgClearPasswd(pwd_root_t * __restrict pwd) AIT_FREE_VAL(&p->usr_realm); AIT_FREE_VAL(&p->usr_home); AIT_FREE_VAL(&p->usr_shell); - io_free(p); + e_free(p); } pwd->rbh_root = NULL; PWD_UNLOCK(pwd);