From ba7ee1e3583956b785cfc0373e63c3c139271ea6 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Sat, 15 Apr 2023 10:12:19 +0200 Subject: [PATCH] src/confd: refactor, add first file function helper: writesf() Signed-off-by: Joachim Wiberg --- src/confd/src/Makefile.am | 2 +- src/confd/src/core.h | 2 ++ src/confd/src/helpers.c | 25 +++++++++++++++++++++++++ src/confd/src/helpers.h | 8 ++++++++ src/confd/src/ietf-interfaces.c | 13 +++---------- src/confd/src/ietf-system.c | 32 ++++++++++++++------------------ 6 files changed, 53 insertions(+), 29 deletions(-) create mode 100644 src/confd/src/helpers.c create mode 100644 src/confd/src/helpers.h diff --git a/src/confd/src/Makefile.am b/src/confd/src/Makefile.am index 649a72c1..1c4609f7 100644 --- a/src/confd/src/Makefile.am +++ b/src/confd/src/Makefile.am @@ -7,5 +7,5 @@ plugin_LTLIBRARIES = confd-plugin.la confd_plugin_la_CFLAGS = $(augeas_CFLAGS) $(libite_CFLAGS) $(sysrepo_CFLAGS) $(CFLAGS) confd_plugin_la_LIBADD = $(augeas_LIBS) $(libite_LIBS) $(sysrepo_LIBS) confd_plugin_la_LDFLAGS = -module -avoid-version -shared -confd_plugin_la_SOURCES = core.c core.h srx_module.c srx_module.h srx_val.c srx_val.h \ +confd_plugin_la_SOURCES = core.c core.h helpers.c helpers.h srx_module.c srx_module.h srx_val.c srx_val.h \ ietf-system.c ietf-interfaces.c diff --git a/src/confd/src/core.h b/src/confd/src/core.h index 45726150..02b96d4a 100644 --- a/src/confd/src/core.h +++ b/src/confd/src/core.h @@ -19,6 +19,8 @@ #include #include +#include "helpers.h" + #ifndef HAVE_VASPRINTF int vasprintf(char **strp, const char *fmt, va_list ap); #endif diff --git a/src/confd/src/helpers.c b/src/confd/src/helpers.c new file mode 100644 index 00000000..f08fd0bf --- /dev/null +++ b/src/confd/src/helpers.c @@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ + +#include +#include "core.h" + +/* + * Write str to a file composed from fmt and optional args. + */ +int writesf(const char *str, const char *fmt, ...) +{ + va_list ap; + int rc = -1; + FILE *fp; + + va_start(ap, fmt); + fp = fopenf("w", fmt, ap); + if (fp) { + fprintf(fp, "%s\n", str); + rc = fclose(fp); + } + va_end(ap); + + return rc; +} + diff --git a/src/confd/src/helpers.h b/src/confd/src/helpers.h new file mode 100644 index 00000000..05a2fa22 --- /dev/null +++ b/src/confd/src/helpers.h @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ + +#ifndef CONFD_HELPERS_H_ +#define CONFD_HELPERS_H_ + +int writesf(const char *str, const char *fmt, ...); + +#endif /* CONFD_HELPERS_H_ */ diff --git a/src/confd/src/ietf-interfaces.c b/src/confd/src/ietf-interfaces.c index aa1a3f96..a9802fff 100644 --- a/src/confd/src/ietf-interfaces.c +++ b/src/confd/src/ietf-interfaces.c @@ -57,16 +57,9 @@ static int ifchange(sr_session_ctx_t *session, uint32_t sub_id, const char *modu ifname = srx_get_str(session, "%s/name", xpath); ptr = srx_get_str(session, "%s/description", xpath); - if (ptr) { - FILE *fp; - - fp = fopenf("w", "/sys/class/net/%s/ifalias", ifname); - if (fp) { - fprintf(fp, "%s\n", ptr); - fclose(fp); - } - free(ptr); - } + if (ptr) + writesf(ptr, "/sys/class/net/%s/ifalias", ifname); + free(ptr); systemf("ip addr flush dev %s", ifname); diff --git a/src/confd/src/ietf-system.c b/src/confd/src/ietf-system.c index 78f565ea..4d01de73 100644 --- a/src/confd/src/ietf-system.c +++ b/src/confd/src/ietf-system.c @@ -259,7 +259,6 @@ static int change_clock(sr_session_ctx_t *session, uint32_t sub_id, const char * const char *xpath, sr_event_t event, unsigned request_id, void *priv) { char *timezone; - FILE *fp; switch (event) { case SR_EV_ENABLED: /* first time, on register. */ @@ -303,13 +302,10 @@ static int change_clock(sr_session_ctx_t *session, uint32_t sub_id, const char * return SR_ERR_VALIDATION_FAILED; } - fp = fopen(TIMEZONE_NEXT, "w"); - if (!fp) { + if (writesf(timezone, TIMEZONE_NEXT)) { ERRNO("Failed preparing %s", TIMEZONE_NEXT); return SR_ERR_SYS; } - fprintf(fp, "%s\n", timezone); - fclose(fp); return SR_ERR_OK; } @@ -533,26 +529,26 @@ fail: static int change_motd(sr_session_ctx_t *session, uint32_t sub_id, const char *module, const char *xpath, sr_event_t event, unsigned request_id, void *priv) { - char *nm; - FILE *fp; + /* XXX: derive from global "options.h" or /usr/share/factory/ */ + const char *msg = "\033[1;90mNote:\033[0m" + "\033[0;90m use help, show, and setup commands to set up and diagnose the system\033[0m"; + char *str; + int rc; /* Ignore all events except SR_EV_DONE */ if (event != SR_EV_DONE) return SR_ERR_OK; - fp = fopen("/etc/motd", "w"); - if (!fp) - return SR_ERR_SYS; - - nm = srx_get_str(session, xpath); - if (nm) { - fprintf(fp, "%s\n", nm); + str = srx_get_str(session, xpath); + if (str) { + rc = writesf(str, "/etc/motd"); + free(str); } else { - /* XXX: derive from global "options.h" or /usr/share/factory/ */ - fprintf(fp, "\033[1;90mNote:\033[0m "); - fprintf(fp, "\033[0;90m use help, show, and setup commands to set up and diagnose the system\033[0m\n"); + rc = writesf(msg, "/etc/motd"); } - fclose(fp); + + if (rc) + return SR_ERR_SYS; return SR_ERR_OK; }