--- libaitcfg/src/aitcfg.c 2014/01/29 23:48:34 1.10 +++ libaitcfg/src/aitcfg.c 2025/08/19 11:22:09 1.16.6.3 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitcfg.c,v 1.10 2014/01/29 23:48:34 misho Exp $ +* $Id: aitcfg.c,v 1.16.6.3 2025/08/19 11:22:09 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 - 2014 +Copyright 2004 - 2025 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -71,8 +71,8 @@ cfg_tree_cmp(struct tagCfg *a, struct tagCfg *b) 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; @@ -118,20 +118,66 @@ cfg_SetErr(int eno, char *estr, ...) /* * cfgInitConfig() - Init config root * + * return: NULL error or !=NULL allocated config root + */ +cfg_root_t * +cfgInitConfig() +{ + 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 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; + } +} + +/* + * cfgInitConfigExt() - Init existed config root + * * @cfg = Config root - * return: -1 error or 0 ok + * return: NULL error or !=NULL inited config root */ -int -cfgInitConfig(cfg_root_t * __restrict cfg) +cfg_root_t * +cfgInitConfigExt(cfg_root_t * __restrict cfg) { - if (!cfg) - return -1; + pthread_mutex_t mtx = { 0 }; + if (!TAILQ_EMPTY(cfg) || !RB_EMPTY(cfg) || + memcmp(&cfg->rc_mtx, &mtx, sizeof mtx)) + cfgUnloadConfig(cfg); + + 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; } /* @@ -146,13 +192,27 @@ cfgLoadConfig(const char *cfgName, cfg_root_t * __rest { FILE *f; int ret; + pthread_mutex_t mtx = { 0 }; if (!cfgName || !cfg) { cfg_SetErr(EINVAL, "Invalid parameter(s)"); return -1; - } else - cfgInitConfig(cfg); + } else { + if (!TAILQ_EMPTY(cfg) || !RB_EMPTY(cfg) || memcmp(&cfg->rc_mtx, &mtx, sizeof mtx)) + cfgUnloadConfig(cfg); + /* + if (memcmp(&cfg->rc_mtx, &mtx, sizeof mtx)) + pthread_mutex_destroy(&cfg->rc_mtx); + */ + 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; @@ -180,8 +240,8 @@ 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); @@ -206,6 +266,7 @@ cfgUnloadConfig(cfg_root_t * __restrict cfg) cfgClearConfig(cfg); pthread_mutex_destroy(&cfg->rc_mtx); + memset(&cfg->rc_mtx, 0, sizeof cfg->rc_mtx); } /* @@ -231,7 +292,7 @@ cfgCreateConfig(const char *csConfigName, cfg_root_t * return -1; } - ret = cfgWriteConfig(f, cfg, whitespace); + ret = cfgWriteConfigRaw(f, cfg, whitespace); fclose(f); return ret; @@ -240,15 +301,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 +323,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 +355,14 @@ 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) {