Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/confd/src/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
CFLAGS = -Wall -Wextra -Werror -Wno-unused-parameter
AM_CPPFLAGS = -D_DEFAULT_SOURCE -D_XOPEN_SOURCE -DYANG_PATH_=\"$(YANGDIR)/\"

plugindir = $(srpdplugindir)
plugin_LTLIBRARIES = confd-plugin.la

confd_plugin_la_CFLAGS = $(augeas_CFLAGS) $(sysrepo_CFLAGS) $(CFLAGS)
confd_plugin_la_LIBADD = $(augeas_LIBS) $(sysrepo_LIBS) $(CFLAGS)
confd_plugin_la_LIBADD = $(augeas_LIBS) $(sysrepo_LIBS)
confd_plugin_la_LDFLAGS = -module -avoid-version -shared
confd_plugin_la_SOURCES = core.c core.h run.c ietf-system.c ietf-interfaces.c
25 changes: 15 additions & 10 deletions src/confd/src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,36 @@ int sr_plugin_init_cb(sr_session_ctx_t *session, void **priv)
if (!confd.aug)
goto err;

if (rc = ietf_system_init(&confd))
rc = ietf_interfaces_init(&confd);
if (rc)
goto err;
rc = ietf_system_init(&confd);
if (rc)
goto err;

/* if (rc = ietf_interfaces_init(&confd)) */
/* goto err; */

/* YOUR_INIT GOES HERE */

/* Set up hook to save startup-config to persisten backend store */
if (rc = sr_session_start(confd.conn, SR_DS_STARTUP, &startup))
rc = sr_session_start(confd.conn, SR_DS_STARTUP, &startup);
if (rc)
goto err;

if (rc = sr_module_change_subscribe(startup, "ietf-system", "/ietf-system:system//.",
startup_save_hook, NULL, 0, SR_SUBSCR_PASSIVE | SR_SUBSCR_DONE_ONLY, &confd.sub)) {
rc = sr_module_change_subscribe(startup, "ietf-system", "/ietf-system:system//.",
startup_save_hook, NULL, 0, SR_SUBSCR_PASSIVE | SR_SUBSCR_DONE_ONLY, &confd.sub);
if (rc) {
ERROR("failed setting up startup-config hook: %s", sr_strerror(rc));
goto err;
}

if (rc = sr_module_change_subscribe(session, "ietf-system", "/ietf-system:system//.",
commit_done_hook, NULL, 0, SR_SUBSCR_PASSIVE | SR_SUBSCR_DONE_ONLY, &confd.sub)) {
rc = sr_module_change_subscribe(session, "ietf-system", "/ietf-system:system//.",
commit_done_hook, NULL, 0, SR_SUBSCR_PASSIVE | SR_SUBSCR_DONE_ONLY, &confd.sub);
if (rc) {
ERROR("failed setting up startup-config hook: %s", sr_strerror(rc));
goto err;
}

if (rc = sr_install_module(confd.conn, YANG_PATH_"kernelkit-infix-deviations.yang", NULL, NULL))
rc = sr_install_module(confd.conn, YANG_PATH_"kernelkit-infix-deviations.yang", NULL, NULL);
if (rc)
goto err;

return SR_ERR_OK;
Expand Down
21 changes: 17 additions & 4 deletions src/confd/src/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,22 @@
#define ERROR(frmt, ...) syslog(LOG_ERR, "%s: " frmt, __func__, ##__VA_ARGS__)
#define ERRNO(frmt, ...) syslog(LOG_ERR, "%s: " frmt ": %s", __func__, ##__VA_ARGS__, strerror(errno))

#define REGISTER_CHANGE(s,m,x,f,c,a,u) \
if (rc = register_change(s, m, x, f, c, a, u)) \
static inline void print_val(sr_val_t *val)
{
char *str;

if (sr_print_val_mem(&str, val))
return;
ERROR("%s", str);
free(str);
}

#define REGISTER_CHANGE(s,m,x,f,c,a,u) \
if ((rc = register_change(s, m, x, f, c, a, u))) \
goto err

#define REGISTER_RPC(s,x,c,a,u) \
if (rc = register_rpc(s, x, c, a, u)) \
if ((rc = register_rpc(s, x, c, a, u))) \
goto err

struct confd {
Expand Down Expand Up @@ -63,7 +73,10 @@ static inline int register_rpc(sr_session_ctx_t *session, const char *xpath,
/* core.c */
int run(const char *fmt, ...);

/* ietf-syste.c */
/* ietf-interfaces.c */
int ietf_interfaces_init(struct confd *confd);

/* ietf-system.c */
int ietf_system_init(struct confd *confd);

#endif /* CONFD_CORE_H_ */
18 changes: 1 addition & 17 deletions src/confd/src/ietf-interfaces.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,7 @@

#include "core.h"

int ietf_system_init(struct confd *confd)
int ietf_interfaces_init(struct confd *confd)
{
char *model[] = {
YANG_PATH_"ietf-interfaces@2018-02-20.yang",
YANG_PATH_"ietf-ip@2018-02-22.yang",
};
int rc, i;

for (i = 0; i < NELEMS(model); i++) {
if (rc = sr_install_module(confd->conn, model[i], NULL, NULL))
goto err;
}

return SR_ERR_OK;
err:
ERROR("init failed: %s", sr_strerror(rc));
sr_unsubscribe(confd->sub);

return rc;
}
67 changes: 25 additions & 42 deletions src/confd/src/ietf-system.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ static int platform_cb(sr_session_ctx_t *session, uint32_t sub_id, const char *m
struct lyd_node **parent, void *priv)
{
const struct ly_ctx *ctx;
struct utsname data;
char *buf;
int rc;

Expand Down Expand Up @@ -250,16 +249,6 @@ static int sys_reload_services(void)
return run("initctl -nbq touch sysklogd lldpd");
}

static void print_val(sr_val_t *val)
{
char *str;

if (sr_print_val_mem(&str, val))
return;
ERROR("%s", str);
free(str);
}

int sr_get_int(sr_session_ctx_t *session, const char *fmt, ...)
{
sr_val_t *val = NULL;
Expand Down Expand Up @@ -391,11 +380,8 @@ char *sr_get_str(sr_session_ctx_t *session, const char *fmt, ...)
static int change_clock(sr_session_ctx_t *session, uint32_t sub_id, const char *module,
const char *xpath, sr_event_t event, unsigned request_id, void *priv)
{
struct confd *confd = (struct confd *)priv;
const char *fn = TIMEZONE_CONF;
char *timezone;
FILE *fp;
int rc;

switch (event) {
case SR_EV_ENABLED: /* first time, on register. */
Expand All @@ -421,6 +407,9 @@ static int change_clock(sr_session_ctx_t *session, uint32_t sub_id, const char *
rename("/etc/localtime+", "/etc/localtime");

return SR_ERR_OK;

default:
return SR_ERR_OK;
}

/* XXX: add support also for /ietf-system:system/clock/timezone-utc-offset (deviation) */
Expand Down Expand Up @@ -454,10 +443,8 @@ static int change_clock(sr_session_ctx_t *session, uint32_t sub_id, const char *
static int change_ntp(sr_session_ctx_t *session, uint32_t sub_id, const char *module,
const char *xpath, sr_event_t event, unsigned request_id, void *priv)
{
struct confd *confd = (struct confd *)priv;
const char *fn = CHRONY_NEXT;
int valid = -1;
sr_data_t *sdt;
sr_val_t *val;
size_t cnt;
FILE *fp;
Expand Down Expand Up @@ -492,13 +479,17 @@ static int change_ntp(sr_session_ctx_t *session, uint32_t sub_id, const char *mo
run("initctl -nbq touch chronyd");
run("initctl -nbq enable chronyd");
return SR_ERR_OK;

default:
return SR_ERR_OK;
}

if (rc = sr_get_items(session, "/ietf-system:system/ntp/server", 0, 0, &val, &cnt)) {
rc = sr_get_items(session, "/ietf-system:system/ntp/server", 0, 0, &val, &cnt);
if (rc) {
remove(CHRONY_NEXT);
return rc;
}

fp = fopen(fn, "w");
if (!fp) {
ERROR("failed updating %s: %s", fn, strerror(errno));
Expand All @@ -511,7 +502,7 @@ static int change_ntp(sr_session_ctx_t *session, uint32_t sub_id, const char *mo
char *type, *ptr;
int server = 0;

/*
/*
* Handle empty startup-config on SR_EV_ENABLED,
* prevents subscribe failure due to false invalid.
*/
Expand All @@ -528,7 +519,8 @@ static int change_ntp(sr_session_ctx_t *session, uint32_t sub_id, const char *mo
free(type);
free(ptr);

if (ptr = sr_get_str(session, "%s/udp/port", xpath)) {
ptr = sr_get_str(session, "%s/udp/port", xpath);
if (ptr) {
fprintf(fp, " port %s", ptr);
free(ptr);
}
Expand Down Expand Up @@ -568,12 +560,9 @@ static int change_ntp(sr_session_ctx_t *session, uint32_t sub_id, const char *mo
static int change_dns(sr_session_ctx_t *session, uint32_t sub_id, const char *module,
const char *xpath, sr_event_t event, unsigned request_id, void *priv)
{
struct confd *confd = (struct confd *)priv;
const char *fn = RESOLV_NEXT;
int timeout, attempts;
int rc = SR_ERR_SYS;
int valid = -1;
sr_data_t *sdt;
sr_val_t *val;
size_t cnt;
FILE *fp;
Expand Down Expand Up @@ -603,6 +592,9 @@ static int change_dns(sr_session_ctx_t *session, uint32_t sub_id, const char *mo

run("resolvconf -u");
return SR_ERR_OK;

default:
return SR_ERR_OK;
}

fp = fopen(fn, "w");
Expand All @@ -623,7 +615,8 @@ static int change_dns(sr_session_ctx_t *session, uint32_t sub_id, const char *mo
fprintf(fp, "\n");
}

if (rc = sr_get_items(session, "/ietf-system:system/dns-resolver/search", 0, 0, &val, &cnt))
rc = sr_get_items(session, "/ietf-system:system/dns-resolver/search", 0, 0, &val, &cnt);
if (rc)
goto fail;

if (cnt) {
Expand All @@ -634,13 +627,13 @@ static int change_dns(sr_session_ctx_t *session, uint32_t sub_id, const char *mo
}
sr_free_values(val, cnt);

if (rc = sr_get_items(session, "/ietf-system:system/dns-resolver/server", 0, 0, &val, &cnt))
rc = sr_get_items(session, "/ietf-system:system/dns-resolver/server", 0, 0, &val, &cnt);
if (rc)
goto fail;

for (size_t i = 0; i < cnt; i++) {
const char *xpath = val[i].xpath;
char *type, *ptr;
int server = 0;
char *ptr;

/* Get /ietf-system:system/dns-resolver/server[name='foo'] */
ptr = sr_get_str(session, "%s/udp-and-tcp/address", xpath);
Expand Down Expand Up @@ -693,23 +686,11 @@ static int change_hostname(sr_session_ctx_t *session, uint32_t sub_id, const cha
const char *host, *tmp = NULL;
char **hosts, *current;
int err, i, nhosts;
int rc = SR_ERR_SYS;
char *nm;

switch (event) {
case SR_EV_ENABLED: /* first time, on register. */
case SR_EV_CHANGE: /* regular change (copy cand running) */
/* Wait for DONE to activate changes */
return SR_ERR_OK;

case SR_EV_ABORT: /* User abort, or other plugin failed */
if (event != SR_EV_DONE)
return SR_ERR_OK;

case SR_EV_DONE:
/* Activate changes */
break;
}

nm = sr_get_str(session, xpath);
if (!nm) {
/* XXX: derive from global "options.h" or /usr/share/factory/ */
Expand Down Expand Up @@ -777,10 +758,12 @@ int ietf_system_init(struct confd *confd)
goto err;
}

if (rc = sr_install_module(confd->conn, YANG_PATH_"ietf-system@2014-08-06.yang", NULL, features))
rc = sr_install_module(confd->conn, YANG_PATH_"ietf-system@2014-08-06.yang", NULL, features);
if (rc)
goto err;
/* Augment to ietf-systems */
if (rc = sr_install_module(confd->conn, YANG_PATH_"infix-system@2014-08-06.yang", NULL, NULL))
rc = sr_install_module(confd->conn, YANG_PATH_"infix-system@2014-08-06.yang", NULL, NULL);
if (rc)
goto err;

rc = sr_oper_get_subscribe(confd->session, "ietf-system", CLOCK_PATH_,
Expand Down