lib, mgmtd: fix commit history location

Both of these belong in `/var/lib`, not `/var/run`.

Rather hilariously, the history read in
`mgmt_history_read_cmt_record_index` was always failing, because it was
doing a `file_exists(MGMTD_COMMIT_FILE_PATH)` check.  Which is the wrong
macro - it's `.../commit-%s.json`, including the unprocessed `%s`, which
would never exist.

I guess noone ever tried if this actually works.  Cool.

On the plus side, this means I don't have to implement legacy
compatibility for this, since it never worked to begin with.

(SQLite3 DB location is also changed in this commit since it also uses
`DAEMON_DB_DIR`.)

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
This commit is contained in:
David Lamparter 2024-01-25 19:59:22 +01:00
parent 567f5702c0
commit bfd6d8ee19
4 changed files with 22 additions and 22 deletions

View File

@ -2741,8 +2741,6 @@ AC_DEFINE_UNQUOTED([FRR_LIBSTATE_PATH], ["$CFG_LIBSTATE"], [/var/lib/frr equival
AC_DEFINE_UNQUOTED([YANG_MODELS_PATH], ["$CFG_YANGMODELS"], [path to YANG data models]) AC_DEFINE_UNQUOTED([YANG_MODELS_PATH], ["$CFG_YANGMODELS"], [path to YANG data models])
AC_DEFINE_UNQUOTED([WATCHFRR_SH_PATH], ["${CFG_SBIN%/}/watchfrr.sh"], [path to watchfrr.sh]) AC_DEFINE_UNQUOTED([WATCHFRR_SH_PATH], ["${CFG_SBIN%/}/watchfrr.sh"], [path to watchfrr.sh])
AC_DEFINE_UNQUOTED([DAEMON_DB_DIR], ["$CFG_STATE"], [daemon database directory])
dnl various features dnl various features
AM_CONDITIONAL([SUPPORT_REALMS], [test "$enable_realms" = "yes"]) AM_CONDITIONAL([SUPPORT_REALMS], [test "$enable_realms" = "yes"])
AM_CONDITIONAL([ENABLE_BGP_VNC], [test "$enable_bgp_vnc" != "no"]) AM_CONDITIONAL([ENABLE_BGP_VNC], [test "$enable_bgp_vnc" != "no"])

View File

@ -47,9 +47,6 @@ DEFINE_KOOH(frr_fini, (), ());
const char frr_sysconfdir[] = SYSCONFDIR; const char frr_sysconfdir[] = SYSCONFDIR;
char frr_runstatedir[256] = FRR_RUNSTATE_PATH; char frr_runstatedir[256] = FRR_RUNSTATE_PATH;
char frr_libstatedir[256] = FRR_LIBSTATE_PATH; char frr_libstatedir[256] = FRR_LIBSTATE_PATH;
#ifdef HAVE_SQLITE3
const char frr_dbdir[] = DAEMON_DB_DIR;
#endif
const char frr_moduledir[] = MODULE_PATH; const char frr_moduledir[] = MODULE_PATH;
const char frr_scriptdir[] = SCRIPT_PATH; const char frr_scriptdir[] = SCRIPT_PATH;
@ -347,7 +344,7 @@ void frr_preinit(struct frr_daemon_info *daemon, int argc, char **argv)
snprintf(frr_zclientpath, sizeof(frr_zclientpath), ZAPI_SOCK_NAME); snprintf(frr_zclientpath, sizeof(frr_zclientpath), ZAPI_SOCK_NAME);
#ifdef HAVE_SQLITE3 #ifdef HAVE_SQLITE3
snprintf(dbfile_default, sizeof(dbfile_default), "%s/%s.db", snprintf(dbfile_default, sizeof(dbfile_default), "%s/%s.db",
frr_dbdir, di->name); frr_libstatedir, di->name);
#endif #endif
strlcpy(frr_protoname, di->logname, sizeof(frr_protoname)); strlcpy(frr_protoname, di->logname, sizeof(frr_protoname));
@ -728,7 +725,7 @@ struct event_loop *frr_init(void)
frr_runstatedir, di->name, p_instance); frr_runstatedir, di->name, p_instance);
#ifdef HAVE_SQLITE3 #ifdef HAVE_SQLITE3
snprintf(dbfile_default, sizeof(dbfile_default), "%s/%s%s%s.db", snprintf(dbfile_default, sizeof(dbfile_default), "%s/%s%s%s.db",
frr_dbdir, p_pathspace, di->name, p_instance); frr_libstatedir, p_pathspace, di->name, p_instance);
#endif #endif
zprivs_preinit(di->privs); zprivs_preinit(di->privs);

View File

@ -29,8 +29,8 @@
#define MGMTD_MAX_COMMIT_LIST 10 #define MGMTD_MAX_COMMIT_LIST 10
#define MGMTD_COMMIT_FILE_PATH DAEMON_DB_DIR "/commit-%s.json" #define MGMTD_COMMIT_FILE_PATH(id) "%s/commit-%s.json", frr_libstatedir, id
#define MGMTD_COMMIT_INDEX_FILE_NAME DAEMON_DB_DIR "/commit-index.dat" #define MGMTD_COMMIT_INDEX_FILE_PATH "%s/commit-index.dat", frr_libstatedir
extern struct nb_config *running_config; extern struct nb_config *running_config;

View File

@ -63,7 +63,7 @@ static struct mgmt_cmt_info_t *mgmt_history_new_cmt_info(void)
mgmt_time_to_string(&tv, true, new->time_str, sizeof(new->time_str)); mgmt_time_to_string(&tv, true, new->time_str, sizeof(new->time_str));
mgmt_time_to_string(&tv, false, new->cmtid_str, sizeof(new->cmtid_str)); mgmt_time_to_string(&tv, false, new->cmtid_str, sizeof(new->cmtid_str));
snprintf(new->cmt_json_file, sizeof(new->cmt_json_file), snprintf(new->cmt_json_file, sizeof(new->cmt_json_file),
MGMTD_COMMIT_FILE_PATH, new->cmtid_str); MGMTD_COMMIT_FILE_PATH(new->cmtid_str));
return new; return new;
} }
@ -104,18 +104,21 @@ mgmt_history_find_cmt_record(const char *cmtid_str)
static bool mgmt_history_read_cmt_record_index(void) static bool mgmt_history_read_cmt_record_index(void)
{ {
char index_path[MAXPATHLEN];
FILE *fp; FILE *fp;
struct mgmt_cmt_info_t cmt_info; struct mgmt_cmt_info_t cmt_info;
struct mgmt_cmt_info_t *new; struct mgmt_cmt_info_t *new;
int cnt = 0; int cnt = 0;
if (!file_exists(MGMTD_COMMIT_FILE_PATH)) snprintf(index_path, sizeof(index_path), MGMTD_COMMIT_INDEX_FILE_PATH);
return false;
fp = fopen(MGMTD_COMMIT_INDEX_FILE_NAME, "rb"); fp = fopen(index_path, "rb");
if (!fp) { if (!fp) {
zlog_err("Failed to open commit history %s for reading: %s", if (errno == ENOENT || errno == ENOTDIR)
MGMTD_COMMIT_INDEX_FILE_NAME, safe_strerror(errno)); return false;
zlog_err("Failed to open commit history %pSQq for reading: %m",
index_path);
return false; return false;
} }
@ -132,9 +135,8 @@ static bool mgmt_history_read_cmt_record_index(void)
memcpy(new, &cmt_info, sizeof(struct mgmt_cmt_info_t)); memcpy(new, &cmt_info, sizeof(struct mgmt_cmt_info_t));
mgmt_cmt_infos_add_tail(&mm->cmts, new); mgmt_cmt_infos_add_tail(&mm->cmts, new);
} else { } else {
zlog_warn( zlog_warn("More records found in commit history file %pSQq than expected",
"More records found in commit history file %s than expected", index_path);
MGMTD_COMMIT_INDEX_FILE_NAME);
fclose(fp); fclose(fp);
return false; return false;
} }
@ -148,16 +150,19 @@ static bool mgmt_history_read_cmt_record_index(void)
static bool mgmt_history_dump_cmt_record_index(void) static bool mgmt_history_dump_cmt_record_index(void)
{ {
char index_path[MAXPATHLEN];
FILE *fp; FILE *fp;
int ret = 0; int ret = 0;
struct mgmt_cmt_info_t *cmt_info; struct mgmt_cmt_info_t *cmt_info;
struct mgmt_cmt_info_t cmt_info_set[10]; struct mgmt_cmt_info_t cmt_info_set[10];
int cnt = 0; int cnt = 0;
fp = fopen(MGMTD_COMMIT_INDEX_FILE_NAME, "wb"); snprintf(index_path, sizeof(index_path), MGMTD_COMMIT_INDEX_FILE_PATH);
fp = fopen(index_path, "wb");
if (!fp) { if (!fp) {
zlog_err("Failed to open commit history %s for writing: %s", zlog_err("Failed to open commit history %pSQq for writing: %m",
MGMTD_COMMIT_INDEX_FILE_NAME, safe_strerror(errno)); index_path);
return false; return false;
} }
@ -176,7 +181,7 @@ static bool mgmt_history_dump_cmt_record_index(void)
fclose(fp); fclose(fp);
if (ret != cnt) { if (ret != cnt) {
zlog_err("Failed to write full commit history, removing file"); zlog_err("Failed to write full commit history, removing file");
remove_file(MGMTD_COMMIT_INDEX_FILE_NAME); remove_file(index_path);
return false; return false;
} }
return true; return true;