eBPF-based security platform
upatch map: Change --json to print to stdout by default
Michi Mutsuzaki24073703c163
diff --git a/Documentation/telemetry.rst b/Documentation/telemetry.rstindex a7d7213a..330b79f6 100644--- a/Documentation/telemetry.rst+++ b/Documentation/telemetry.rst@@ -244,9 +244,9 @@ signed with SigV4 from ``AWS_ACCESS_KEY_ID``, ``AWS_SECRET_ACCESS_KEY``,``AWS_CA_BUNDLE``. Large snapshots bypass the collector's record sizelimits this way while the collector still learns that they exist.-JSON lines. ``--json=<path>`` (or ``map_export.json_path``) appends-the map listing and the decoded records as JSON lines, for a log-shipper that tails files.+JSON lines. ``--json`` prints the map listing or decoded records to+stdout. ``--json=<path>`` (or ``map_export.json_path``) appends them+as JSON lines to a file for a log shipper that tails files.5) Transport@@ -450,6 +450,7 @@ Air-gapped fleets use this path.On a target::sudo shirudo upatch map --all # decoded, to stdout+ sudo shirudo upatch map --all --json # decoded NDJSONsudo shirudo upatch map --all --json=/tmp/maps.ndjson # JSON linessudo shirudo upatch map --all --otel --otel-endpoint=http://127.0.0.1:4318sudo shirudo status --otel --include-systemdiff --git a/builtin-upatch.c b/builtin-upatch.cindex 14669e65..a9bb4dad 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -192,7 +192,7 @@ static const struct shirudo_help upatch_unload_help = {static const struct shirudo_help_item upatch_map_options[] = {{ "--all", "export every map from every loaded upatch" },- { "--json=<path>", "append the signed Lua decoder's NDJSON record to path" },+ { "--json[=<path>]", "print JSON or append it to path" },{ "--show-full", "lift the per-map 256-entry truncation cap" },{ "--peek", "read serializer-backed maps without draining them" },{ "--otel", "export through OTLP using each decoder's signed signal" },@@ -209,7 +209,7 @@ static const struct shirudo_help_section upatch_map_sections[] = {static const struct shirudo_help upatch_map_help = {.usage = "shirudo upatch map [--all] [<upatch>] [<map>] "- "[--json=<path>] [--show-full] [--peek]\n"+ "[--json[=<path>]] [--show-full] [--peek]\n"" [--otel] ""[--otel-endpoint=<url>]\n"" [--otel-insecure] "@@ -230,6 +230,7 @@ static const struct shirudo_help upatch_map_help = {"Examples:\n"" shirudo upatch map\n"" shirudo upatch map --all\n"+" shirudo upatch map kernel-obs exec --json\n"" shirudo upatch map cve-2026-31431-counter\n"" shirudo upatch map cve-2026-31431-counter blocked_af_alg\n"" shirudo upatch map cve-2026-31431-counter blocked_af_alg \\\n"@@ -2323,6 +2324,22 @@ out:return rc;}+static int print_json_record(const char *json, size_t len)+{+ int rc;++ rc = shirudo_write_all(STDOUT_FILENO, json, len);+ if (!rc)+ rc = shirudo_write_all(STDOUT_FILENO, "\n", 1);+ return rc;+}++static int emit_json_record(const char *path, const char *json, size_t len)+{+ return path ? append_json_record(path, json, len) :+ print_json_record(json, len);+}+static int copy_file_atomic(const char *src, const char *dst){char tmp[PATH_MAX + 8], buf[65536];@@ -2945,7 +2962,7 @@ static int append_one_map_json(const char *path,while (decoded_len && (decoded[decoded_len - 1] == '\n' ||decoded[decoded_len - 1] == '\r'))decoded_len--;- rc = append_json_record(path, decoded, decoded_len);+ rc = emit_json_record(path, decoded, decoded_len);xfree(decoded, decoded_len + 1);return rc;}@@ -2953,12 +2970,12 @@ static int append_one_map_json(const char *path,}static int export_one_map(const char *upatch, const char *map,- const char *json_path, bool otel,+ bool json_output, const char *json_path, bool otel,const struct shirudo_otel_opts *otel_opts,bool show_full, bool peek, bool print_detail){struct shirudo_map_view v = {};- uint32_t cap = (show_full || json_path || otel) ? 0 :+ uint32_t cap = (show_full || json_output || otel) ? 0 :SHIRUDO_MAP_ENTRIES_DEFAULT;int rc, ret = 0;@@ -2971,11 +2988,15 @@ static int export_one_map(const char *upatch, const char *map,shirudo_map_view_free(&v);return 1;}- if (json_path) {+ if (json_output) {rc = append_one_map_json(json_path, &v);if (rc) {- pr_err("upatch map: append JSON to %s: %s",- json_path, strerror(-rc));+ if (json_path)+ pr_err("upatch map: append JSON to %s: %s",+ json_path, strerror(-rc));+ else+ pr_err("upatch map: print JSON: %s",+ strerror(-rc));ret = 1;}}@@ -3009,7 +3030,8 @@ static int cmd_upatch_map(int argc, const char **argv).retries = 3,};const char *upatch = NULL, *map = NULL, *json_path = NULL;- bool all = false, show_full = false, otel = false, peek = false;+ bool all = false, json_output = false, show_full = false;+ bool otel = false, peek = false;bool insecure_set = false;bool retries_set = false;size_t n = 0;@@ -3029,15 +3051,19 @@ static int cmd_upatch_map(int argc, const char **argv)}all = true;} else if (strncmp(a, "--json=", 7) == 0) {- if (json_path || !a[7]) {+ if (json_output || !a[7]) {fprintf(stderr,"upatch map: invalid or duplicate --json\n");return 2;}+ json_output = true;json_path = a + 7;} else if (strcmp(a, "--json") == 0) {- fprintf(stderr, "upatch map: --json requires a path\n");- return 2;+ if (json_output) {+ fprintf(stderr, "upatch map: duplicate --json\n");+ return 2;+ }+ json_output = true;} else if (strcmp(a, "--show-full") == 0) {show_full = true;} else if (strcmp(a, "--peek") == 0) {@@ -3120,8 +3146,10 @@ static int cmd_upatch_map(int argc, const char **argv)pr_err("upatch map: invalid map_export configuration");return 2;}- if (!json_path && export_cfg.map_export_json_path[0])+ if (!json_output && export_cfg.map_export_json_path[0]) {json_path = export_cfg.map_export_json_path;+ json_output = true;+ }if (!otel && export_cfg.map_export_otel_endpoint[0]) {otel = true;}@@ -3166,17 +3194,14 @@ static int cmd_upatch_map(int argc, const char **argv)return 2;}}- if (all && !json_path && !otel)- return 0;-if (upatch && map) {if (!safe_upatch_name(upatch)) {pr_err("upatch map: invalid upatch name '%s'", upatch);return 2;}- return export_one_map(upatch, map, json_path, otel,+ return export_one_map(upatch, map, json_output, json_path, otel,&otel_opts, show_full, peek,- !json_path && !otel);+ !json_output && !otel);}if (all) {@@ -3185,8 +3210,9 @@ static int cmd_upatch_map(int argc, const char **argv)shirudo_upatch_maps_list_all(&list, &n);for (k = 0; k < n; k++) {if (export_one_map(list[k].upatch, list[k].name,- json_path, otel, &otel_opts,- show_full, peek, false) != 0)+ json_output, json_path, otel,+ &otel_opts, show_full, peek,+ !json_output && !otel) != 0)ret = 1;}shirudo_map_summary_free(list, n);@@ -3203,7 +3229,7 @@ static int cmd_upatch_map(int argc, const char **argv)shirudo_upatch_maps_list_all(&list, &n);}- if (json_path) {+ if (json_output) {struct shirudo_json_emit e = {};size_t cap = 8192 + 256 * n;char *buf;@@ -3237,11 +3263,15 @@ static int cmd_upatch_map(int argc, const char **argv)if (e.err) {ret = 1;} else {- int rc = append_json_record(json_path, buf, e.len);+ int rc = emit_json_record(json_path, buf, e.len);if (rc) {- pr_err("upatch map: append JSON to %s: %s",- json_path, strerror(-rc));+ if (json_path)+ pr_err("upatch map: append JSON to %s: %s",+ json_path, strerror(-rc));+ else+ pr_err("upatch map: print JSON: %s",+ strerror(-rc));ret = 1;}}diff --git a/testing/t0008-upatch-serializer-pages.sh b/testing/t0008-upatch-serializer-pages.shindex 65bd3ab0..7552b3df 100755--- a/testing/t0008-upatch-serializer-pages.sh+++ b/testing/t0008-upatch-serializer-pages.sh@@ -19,8 +19,13 @@ test_expect_success 'paged serializer JSON exposes pages and one data object' '"$H"'-test_expect_success 'upatch map requires a JSON output path' '- ! "$S" upatch map --json >/dev/null 2>&1 &&+test_expect_success 'upatch map prints JSON to stdout by default' '+ "$S" upatch map --json >"$WORK/maps.stdout" &&+ python3 -c '\''+import json, sys+doc = json.load(open(sys.argv[1], encoding="utf-8"))+assert doc["schema"] == 1 and isinstance(doc["maps"], list)+'\'' "$WORK/maps.stdout" &&! "$S" upatch map --json= >/dev/null 2>&1'diff --git a/upatches/kernel-obs/README.md b/upatches/kernel-obs/README.mdindex af80e891..7152b132 100644--- a/upatches/kernel-obs/README.md+++ b/upatches/kernel-obs/README.md@@ -91,6 +91,7 @@ rebuild the package. There is nothing to configure on the target.```shsudo shirudo upatch map kernel-obssudo shirudo upatch map kernel-obs exec+sudo shirudo upatch map kernel-obs exec --jsonsudo shirudo upatch map kernel-obs net --show-fullsudo shirudo upatch map kernel-obs priv --json=./priv.ndjson```