mirror of
https://github.com/kernelkit/infix.git
synced 2026-07-31 13:03:02 +02:00
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:
@@ -33,7 +33,6 @@ static int ifchange(sr_session_ctx_t *session, uint32_t sub_id, const char *modu
|
|||||||
size_t addrcnt;
|
size_t addrcnt;
|
||||||
char *ifname;
|
char *ifname;
|
||||||
char *ptr;
|
char *ptr;
|
||||||
int ena;
|
|
||||||
|
|
||||||
ifname = srx_get_str(session, "%s/name", xpath);
|
ifname = srx_get_str(session, "%s/name", xpath);
|
||||||
ptr = srx_get_str(session, "%s/description", 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);
|
free(plen);
|
||||||
}
|
}
|
||||||
|
|
||||||
ena = srx_get_bool(session, "%s/enabled", xpath);
|
systemf("ip link set %s %s", ifname, srx_enabled(session, "%s/enabled", xpath) ? "up" : "down");
|
||||||
systemf("ip link set %s %s", ifname, ena <= 0 ? "down" : "up");
|
|
||||||
}
|
}
|
||||||
sr_free_values(val, cnt);
|
sr_free_values(val, cnt);
|
||||||
|
|
||||||
|
|||||||
@@ -346,7 +346,7 @@ static int change_ntp(sr_session_ctx_t *session, uint32_t sub_id, const char *mo
|
|||||||
remove(CHRONY_PREV);
|
remove(CHRONY_PREV);
|
||||||
rename(CHRONY_CONF, CHRONY_PREV);
|
rename(CHRONY_CONF, CHRONY_PREV);
|
||||||
rename(CHRONY_NEXT, CHRONY_CONF);
|
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");
|
systemf("initctl -nbq disable chronyd");
|
||||||
return SR_ERR_OK;
|
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 (server) {
|
||||||
if (srx_get_bool(session, "%s/iburst", xpath) > 0)
|
if (srx_enabled(session, "%s/iburst", xpath) > 0)
|
||||||
fprintf(fp, " iburst");
|
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, " prefer");
|
||||||
|
|
||||||
fprintf(fp, "\n");
|
fprintf(fp, "\n");
|
||||||
|
|||||||
+36
-9
@@ -32,21 +32,48 @@ static int srx_vaget(sr_session_ctx_t *session, const char *fmt, va_list ap, sr_
|
|||||||
return 0;
|
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;
|
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;
|
va_list ap;
|
||||||
|
int rc;
|
||||||
|
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
if (srx_vaget(session, fmt, ap, &val, SR_BOOL_T))
|
rc = get_vabool(session, result, fmt, ap);
|
||||||
goto fail;
|
|
||||||
|
|
||||||
result = val->data.bool_val;
|
|
||||||
sr_free_val(val);
|
|
||||||
fail:
|
|
||||||
va_end(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, ...)
|
int srx_get_int(sr_session_ctx_t *session, int *result, sr_val_type_t type, const char *fmt, ...)
|
||||||
|
|||||||
@@ -10,5 +10,6 @@
|
|||||||
char *srx_get_str (sr_session_ctx_t *session, const char *fmt, ...);
|
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_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_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_ */
|
#endif /* CONFD_SRX_VAL_H_ */
|
||||||
|
|||||||
Reference in New Issue
Block a user