--- libaitcfg/src/aitcfg.c 2014/01/30 08:30:47 1.11 +++ libaitcfg/src/aitcfg.c 2014/03/03 09:41:09 1.12 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitcfg.c,v 1.11 2014/01/30 08:30:47 misho Exp $ +* $Id: aitcfg.c,v 1.12 2014/03/03 09:41:09 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -118,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); 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 @@ -150,9 +172,13 @@ cfgLoadConfig(const char *cfgName, cfg_root_t * __rest if (!cfgName || !cfg) { cfg_SetErr(EINVAL, "Invalid parameter(s)"); return -1; - } else - cfgInitConfig(cfg); + } else { + pthread_mutex_init(&cfg->rc_mtx, NULL); + TAILQ_INIT(cfg); + RB_INIT(cfg); + } + f = fopen(cfgName, "r"); if (!f) { LOGERR; @@ -240,15 +266,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); @@ -257,6 +288,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 @@ -272,8 +320,12 @@ cfgLoadPasswd(const char *pwdName, pwd_root_t * __rest if (!pwdName || !pwd) { cfg_SetErr(EINVAL, "Invalid parameter(s)"); return -1; - } else - cfgInitPasswd(pwd); + } else { + pthread_mutex_init(&pwd->pwd_mtx, NULL); + + SLIST_INIT(pwd); + RB_INIT(pwd); + } f = fopen(pwdName, "r"); if (!f) {