--- libaitcfg/src/aitcfg.c 2016/05/18 15:18:10 1.13 +++ 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.13 2016/05/18 15:18:10 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 - 2016 +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; @@ -157,6 +157,30 @@ cfgEndConfig(cfg_root_t **pcfg) } /* + * cfgInitConfigExt() - Init existed config root + * + * @cfg = Config root + * return: NULL error or !=NULL inited config root + */ +cfg_root_t * +cfgInitConfigExt(cfg_root_t * __restrict cfg) +{ + 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); + + TAILQ_INIT(cfg); + RB_INIT(cfg); + return cfg; +} + +/* * cfgLoadConfig() - Load config from file * * @cfgName = Config filename @@ -168,11 +192,21 @@ 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 { + 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); @@ -232,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); } /* @@ -257,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; @@ -321,6 +356,8 @@ cfgLoadPasswd(const char *pwdName, pwd_root_t * __rest cfg_SetErr(EINVAL, "Invalid parameter(s)"); return -1; } else { + memset(pwd, 0, sizeof(pwd_root_t)); + pthread_mutex_init(&pwd->pwd_mtx, NULL); SLIST_INIT(pwd);