Now teamd uses dot symbol to split path string into different nodes
in __teamd_json_path_lite_va() when dumping teamd state. But team
port ifname is a part of path string, if ifname has dot character
inside, it would be parsed as two nodes incorrectly.
Like, if users uses eth1.1 as a port ifname
teamdctl team0 state dump would be:
"ports": {
"eth1": {
"1": {
"ifinfo": {
...
and teamdctl team0 state view even crashs as:
...
ports:
Failed to parse JSON port dump.
command call failed (Invalid argument)
Actually it's common for users to use dot in ifname, especially for
vlan device.
This patch is to replace dot with space in ifname for then, after
being parsed, it would be changed back to dot.
Note that it's safe to change it with space, as space in ifname is
disallowed. Besides, no teamd state attributes/nodes include space
afaik, so no side effects could be caused by this fix.
Signed-off-by: Xin Long <lucien.xin(a)gmail.com>
---
teamd/teamd_json.c | 16 +++++++++++++++-
teamd/teamd_json.h | 1 +
teamd/teamd_state.c | 13 ++++++++++---
3 files changed, 26 insertions(+), 4 deletions(-)
diff --git a/teamd/teamd_json.c b/teamd/teamd_json.c
index 5fd5e8e..7082484 100644
--- a/teamd/teamd_json.c
+++ b/teamd/teamd_json.c
@@ -42,6 +42,19 @@ static char *__strchrs(char *str, char *chars)
return NULL;
}
+char *strchrchg(char *str, char s, char d)
+{
+ char *tmp = str;
+
+ while (*str != '\0') {
+ if (*str == s)
+ *str = d;
+ str++;
+ }
+
+ return tmp;
+}
+
#define TEAMD_JSON_PATH_MAXLEN 128
typedef json_t *(*obj_constructor_t)(void);
@@ -82,7 +95,8 @@ static int __teamd_json_path_lite_va(json_t **p_json_obj, json_t *json_root,
*end = '\0';
}
prev_json_obj = json_obj;
- json_obj = json_object_get(prev_json_obj, ptr);
+ json_obj = json_object_get(prev_json_obj,
+ strchrchg(ptr, ' ', '.'));
if (!json_obj && obj_constructor) {
/* In case new object is not supposed to be
* leaf, use json_object() as a constructor.
diff --git a/teamd/teamd_json.h b/teamd/teamd_json.h
index 30e9eaa..3dffdb5 100644
--- a/teamd/teamd_json.h
+++ b/teamd/teamd_json.h
@@ -24,6 +24,7 @@
#define TEAMD_JSON_DUMPS_FLAGS (JSON_INDENT(4) | JSON_ENSURE_ASCII | JSON_SORT_KEYS)
+char *strchrchg(char *str, char s, char d);
int teamd_json_path_lite_va(json_t **p_json_obj, json_t *json_root,
const char *fmt, va_list ap);
int teamd_json_path_lite(json_t **p_json_obj, json_t *json_root,
diff --git a/teamd/teamd_state.c b/teamd/teamd_state.c
index 51fac8d..d9931dc 100644
--- a/teamd/teamd_state.c
+++ b/teamd/teamd_state.c
@@ -194,11 +194,18 @@ static int teamd_state_build_val_json_subpath(json_t **p_vg_json_obj,
int err;
char *dot;
- if (tdport)
+ if (tdport) {
+ char *tmp;
+
+ if (asprintf(&tmp, tdport->ifname) == -1)
+ return -ENOMEM;
+
ret = asprintf(&path, "$." TEAMD_STATE_PER_PORT_PREFIX "%s%s",
- tdport->ifname, subpath);
- else
+ strchrchg(tmp, '.', ' '), subpath);
+ free(tmp);
+ } else {
ret = asprintf(&path, "$%s", subpath);
+ }
if (ret == -1)
return -ENOMEM;
dot = strrchr(path, '.');
--
2.1.0