From a4b325c4e12914170748adcebebde03a7a9b04fb Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 23 Jan 2026 18:45:51 +0100 Subject: [PATCH] confd: refactor and code cleanup - Simplify, prefer readability - Fix nftw() 'nopenfd' argument - Constify The most interesting change is the argument to nftw(), 0 -> 20, to ensure it is allowed to open enough file descriptors to descend. In POSIX it is undefined if nopenfd is < 1. Signed-off-by: Joachim Wiberg --- src/confd/src/hardware.c | 65 ++++++++++++++++++-------------------- src/confd/src/interfaces.c | 10 +++--- 2 files changed, 37 insertions(+), 38 deletions(-) diff --git a/src/confd/src/hardware.c b/src/confd/src/hardware.c index c1534cb6..b7d0cbc5 100644 --- a/src/confd/src/hardware.c +++ b/src/confd/src/hardware.c @@ -1,13 +1,13 @@ /* SPDX-License-Identifier: BSD-3-Clause */ +#include +#include #include +#include +#include #include #include #include -#include -#include -#include -#include #include "core.h" #include "interfaces.h" @@ -17,10 +17,10 @@ #define HOSTAPD_CONF "/etc/hostapd-%s.conf" #define HOSTAPD_CONF_NEXT HOSTAPD_CONF"+" -static int dir_cb(const char *fpath, const struct stat *sb, - int typeflag, struct FTW *ftwbuf) +static int dir_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) { - char *filename; + const char *filename; + if (typeflag == FTW_DP) return 0; @@ -31,59 +31,55 @@ static int dir_cb(const char *fpath, const struct stat *sb, return FTW_STOP; } } + return 0; } static bool usb_authorize(struct json_t *root, const char *name, int enabled) { - json_t *usb_port, *usb_ports; + json_t *port, *ports; int index; - usb_ports = json_object_get(root, "usb-ports"); - if (!usb_ports) /* No Infix controlled USB ports is ok */ + ports = json_object_get(root, "usb-ports"); + if (!ports) return 0; - json_array_foreach(usb_ports, index, usb_port) { - struct json_t *jname; + json_array_foreach(ports, index, port) { + struct json_t *obj; - jname = json_object_get(usb_port, "name"); - if (!jname || !json_is_string(jname)) { + obj = json_object_get(port, "name"); + if (!obj || !json_is_string(obj)) { ERROR("Did not find USB hardware port (name) for %s", name); continue; } - if (!strcmp(name, json_string_value(jname))) { - struct json_t *jpath = json_object_get(usb_port, "path"); - char authorized_default_path[PATH_MAX]; + + if (!strcmp(name, json_string_value(obj))) { + char apath[PATH_MAX]; const char *path; - if (!jpath || !json_is_string(jpath)) { + obj = json_object_get(port, "path"); + if (!obj || !json_is_string(obj)) { ERROR("Did not find USB hardware port (path) for %s", name); continue; } - /* Path now points to USB device directory, not the attribute file */ - path = json_string_value(jpath); - snprintf(authorized_default_path, sizeof(authorized_default_path), - "%s/authorized_default", path); + path = json_string_value(obj); + snprintf(apath, sizeof(apath), "%s/authorized_default", path); if (!enabled) { - if (fexist(authorized_default_path)) { - if (writedf(0, "w", "%s", authorized_default_path)) { - ERROR("Failed to unauthorize %s", authorized_default_path); + if (fexist(apath)) { + if (writedf(0, "w", "%s", apath)) { + ERROR("Failed to unauthorize %s", apath); return 1; } } } else { - char *rpath; - - rpath = realpath(path, NULL); - if (rpath) { - nftw(rpath, dir_cb, 0, FTW_DEPTH | FTW_PHYS); - free(rpath); - } + if (realpath(path, apath)) + nftw(apath, dir_cb, 20, FTW_DEPTH | FTW_PHYS); } } } + return 0; } @@ -584,7 +580,8 @@ static int hardware_cand(sr_session_ctx_t *session, uint32_t sub_id, const char return err; } -int hardware_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd) +int hardware_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, + sr_event_t event, struct confd *confd) { struct lyd_node *difs = NULL, *dif = NULL; int rc = SR_ERR_OK; @@ -623,7 +620,7 @@ int hardware_change(sr_session_ctx_t *session, struct lyd_node *config, struct l } continue; } - state = lydx_get_child(dif, "state"); + state = lydx_get_child(cif, "state"); admin_state = lydx_get_cattr(state, "admin-state"); if (usb_authorize(confd->root, name, !strcmp(admin_state, "unlocked"))) { rc = SR_ERR_INTERNAL; diff --git a/src/confd/src/interfaces.c b/src/confd/src/interfaces.c index 7df74980..f16aa8ee 100644 --- a/src/confd/src/interfaces.c +++ b/src/confd/src/interfaces.c @@ -599,14 +599,16 @@ static int netdag_gen_iface_del(struct dagger *net, struct lyd_node *dif, static sr_error_t netdag_gen_iface_timeout(struct dagger *net, const char *ifname, const char *iftype) { if (!strcmp(iftype, "infix-if-type:ethernet")) { - FILE *wait = dagger_fopen_net_init(net, ifname, NETDAG_INIT_TIMEOUT, "wait-interface.sh"); - if (!wait) { + FILE *wait; + + wait = dagger_fopen_net_init(net, ifname, NETDAG_INIT_TIMEOUT, "wait-interface.sh"); + if (!wait) return -EIO; - } fprintf(wait, "/usr/libexec/confd/wait-interface %s %d\n", ifname, IFACE_PROBE_TIMEOUT); fclose(wait); - } + } + return SR_ERR_OK; }