Files
infix/src/statd/shared.c
T
Joachim Wiberg c529f06cf0 statd: fix gcc warning, "format not a string literal"
error: format not a string literal and no format arguments [-Werror=format-security]

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2023-11-17 10:40:17 +01:00

69 lines
1.3 KiB
C

/* SPDX-License-Identifier: BSD-3-Clause */
#include <stdio.h>
#include <stdlib.h>
#include <jansson.h>
#include <srx/common.h>
#include <srx/helpers.h>
json_t *json_get_output(const char *cmd)
{
json_error_t j_err;
json_t *j_root;
FILE *proc;
proc = popenf("re", "%s", cmd);
if (!proc) {
ERROR("Error, running command %s", cmd);
return NULL;
}
j_root = json_loadf(proc, 0, &j_err);
pclose(proc);
if (!j_root) {
ERROR("Error, parsing command JSON (%s)", cmd);
return NULL;
}
return j_root;
}
int ip_link_check_group(const char *ifname, const char *group)
{
char cmd[512] = {}; /* Size is arbitrary */
json_t *j_iface;
json_t *j_root;
json_t *j_val;
snprintf(cmd, sizeof(cmd), "ip -s -d -j link show dev %s 2>/dev/null", ifname);
j_root = json_get_output(cmd);
if (!j_root) {
ERROR("Error, parsing ip-link JSON");
return -1;
}
if (json_array_size(j_root) != 1) {
ERROR("Error, expected JSON array of single iface");
json_decref(j_root);
return -1;
}
j_iface = json_array_get(j_root, 0);
j_val = json_object_get(j_iface, "group");
if (!json_is_string(j_val)) {
ERROR("Error, expected a JSON string for 'group'");
json_decref(j_root);
return -1;
}
if (strcmp(json_string_value(j_val), group) == 0) {
json_decref(j_root);
return 1;
}
json_decref(j_root);
return 0;
}