src/confd: refactor srx_get_bool() to create srx_enabled()

All value functions, except the new srx_enabled(), now return clear
error indication, with the value as a separate argument.

The srx_enabled() predicate function is for cases where the default
value is disabled and code checks enable a subsystem or flag.  I.e.,
when an error can safely be returned as 'false'.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2023-04-17 14:09:24 +02:00
parent 63bc767593
commit ac0b0d520f
4 changed files with 41 additions and 15 deletions
+1 -3
View File
@@ -33,7 +33,6 @@ static int ifchange(sr_session_ctx_t *session, uint32_t sub_id, const char *modu
size_t addrcnt;
char *ifname;
char *ptr;
int ena;
ifname = srx_get_str(session, "%s/name", xpath);
ptr = srx_get_str(session, "%s/description", xpath);
@@ -63,8 +62,7 @@ static int ifchange(sr_session_ctx_t *session, uint32_t sub_id, const char *modu
free(plen);
}
ena = srx_get_bool(session, "%s/enabled", xpath);
systemf("ip link set %s %s", ifname, ena <= 0 ? "down" : "up");
systemf("ip link set %s %s", ifname, srx_enabled(session, "%s/enabled", xpath) ? "up" : "down");
}
sr_free_values(val, cnt);
+3 -3
View File
@@ -346,7 +346,7 @@ static int change_ntp(sr_session_ctx_t *session, uint32_t sub_id, const char *mo
remove(CHRONY_PREV);
rename(CHRONY_CONF, CHRONY_PREV);
rename(CHRONY_NEXT, CHRONY_CONF);
if (srx_get_bool(session, "/ietf-system:system/ntp/enabled") == 0) {
if (srx_enabled(session, "/ietf-system:system/ntp/enabled")) {
systemf("initctl -nbq disable chronyd");
return SR_ERR_OK;
}
@@ -405,9 +405,9 @@ static int change_ntp(sr_session_ctx_t *session, uint32_t sub_id, const char *mo
}
if (server) {
if (srx_get_bool(session, "%s/iburst", xpath) > 0)
if (srx_enabled(session, "%s/iburst", xpath) > 0)
fprintf(fp, " iburst");
if (srx_get_bool(session, "%s/prefer", xpath) > 0)
if (srx_enabled(session, "%s/prefer", xpath) > 0)
fprintf(fp, " prefer");
fprintf(fp, "\n");
+36 -9
View File
@@ -32,21 +32,48 @@ static int srx_vaget(sr_session_ctx_t *session, const char *fmt, va_list ap, sr_
return 0;
}
int srx_get_bool(sr_session_ctx_t *session, const char *fmt, ...)
static int get_vabool(sr_session_ctx_t *session, int *result, const char *fmt, va_list ap)
{
sr_val_t *val = NULL;
int result = -1;
va_list apdup;
int rc;
va_copy(apdup, ap);
rc = srx_vaget(session, fmt, apdup, &val, SR_BOOL_T);
va_end(apdup);
if (rc)
return rc;
*result = val->data.bool_val;
sr_free_val(val);
return 0;
}
int srx_get_bool(sr_session_ctx_t *session, int *result, const char *fmt, ...)
{
va_list ap;
int rc;
va_start(ap, fmt);
if (srx_vaget(session, fmt, ap, &val, SR_BOOL_T))
goto fail;
result = val->data.bool_val;
sr_free_val(val);
fail:
rc = get_vabool(session, result, fmt, ap);
va_end(ap);
return result;
return rc;
}
int srx_enabled(sr_session_ctx_t *session, const char *fmt, ...)
{
va_list ap;
int v = 0;
int rc;
va_start(ap, fmt);
rc = get_vabool(session, &v, fmt, ap);
va_end(ap);
return rc ? 0 : v;
}
int srx_get_int(sr_session_ctx_t *session, int *result, sr_val_type_t type, const char *fmt, ...)
+1
View File
@@ -10,5 +10,6 @@
char *srx_get_str (sr_session_ctx_t *session, const char *fmt, ...);
int srx_get_int (sr_session_ctx_t *session, int *result, sr_val_type_t type, const char *fmt, ...);
int srx_get_bool (sr_session_ctx_t *session, const char *fmt, ...);
int srx_enabled (sr_session_ctx_t *session, const char *fmt, ...);
#endif /* CONFD_SRX_VAL_H_ */