src/confd: refactor, add first file function helper: writesf()

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2023-04-16 14:45:34 +02:00
parent 31f4f09bfa
commit ba7ee1e358
6 changed files with 53 additions and 29 deletions
+1 -1
View File
@@ -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
+2
View File
@@ -19,6 +19,8 @@
#include <sysrepo/values.h>
#include <sysrepo/xpath.h>
#include "helpers.h"
#ifndef HAVE_VASPRINTF
int vasprintf(char **strp, const char *fmt, va_list ap);
#endif
+25
View File
@@ -0,0 +1,25 @@
/* SPDX-License-Identifier: BSD-3-Clause */
#include <stdarg.h>
#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;
}
+8
View File
@@ -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_ */
+3 -10
View File
@@ -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);
+14 -18
View File
@@ -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;
}