Previously, monitor mode wrapped all events in a single JSON array inside a top-level object, which made piping the output to external tools (such as `jq`) impossible. Send a separate JSON object for each event in monitor mode, making the output suitable for line-by-line consumers. Skip the global JSON wrapper for monitor mode. Signed-off-by: Vitaly Grinberg --- dpll/dpll.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/dpll/dpll.c b/dpll/dpll.c index 7b05bf86..6736c85e 100644 --- a/dpll/dpll.c +++ b/dpll/dpll.c @@ -571,8 +571,15 @@ int main(int argc, char **argv) argc -= optind; argv += optind; - new_json_obj_plain(json); - open_json_object(NULL); + /* Monitor emits one JSON object per event for streaming; + * other commands use a single JSON wrapper object. + */ + bool is_monitor = argc > 0 && strcmp(argv[0], "monitor") == 0; + + if (!is_monitor) { + new_json_obj_plain(json); + open_json_object(NULL); + } /* Skip netlink init for help commands */ bool need_nl = true; @@ -602,8 +609,10 @@ dpll_fini: if (need_nl) dpll_fini(dpll); json_cleanup: - close_json_object(); - delete_json_obj_plain(); + if (!is_monitor) { + close_json_object(); + delete_json_obj_plain(); + } dpll_free: dpll_free(dpll); return ret; @@ -1840,6 +1849,7 @@ static int cmd_monitor_cb(const struct nlmsghdr *nlh, void *data) mnl_attr_parse(nlh, sizeof(struct genlmsghdr), attr_cb, tb); + new_json_obj_plain(json); open_json_object(NULL); print_string(PRINT_JSON, "name", NULL, json_name); open_json_object("msg"); @@ -1849,6 +1859,7 @@ static int cmd_monitor_cb(const struct nlmsghdr *nlh, void *data) close_json_object(); close_json_object(); + delete_json_obj_plain(); break; } case DPLL_CMD_PIN_CREATE_NTF: @@ -1867,6 +1878,7 @@ static int cmd_monitor_cb(const struct nlmsghdr *nlh, void *data) json_name = "pin-delete-ntf"; } + new_json_obj_plain(json); open_json_object(NULL); print_string(PRINT_JSON, "name", NULL, json_name); open_json_object("msg"); @@ -1876,6 +1888,7 @@ static int cmd_monitor_cb(const struct nlmsghdr *nlh, void *data) close_json_object(); close_json_object(); + delete_json_obj_plain(); break; } default: @@ -1941,8 +1954,6 @@ static int cmd_monitor(struct dpll *dpll) goto err_signalfd; } - open_json_array(PRINT_JSON, "monitor"); - pfds[0].fd = signal_fd; pfds[0].events = POLLIN; pfds[1].fd = netlink_fd; @@ -1975,8 +1986,6 @@ static int cmd_monitor(struct dpll *dpll) } } - close_json_array(PRINT_JSON, NULL); - err_signalfd: if (signal_fd >= 0) close(signal_fd); -- 2.52.0