Skip to content

eBPF-based security platform

status, init, config, version, image: parse options with the table

Daniel Borkmann6cbac1cc5520
diff --git a/Documentation/reference/cli/config.rst b/Documentation/reference/cli/config.rst
index 54773231..9b2ca4bf 100644
--- a/Documentation/reference/cli/config.rst
+++ b/Documentation/reference/cli/config.rst
@@ -80,7 +80,7 @@ Exit status
printed ``map_export`` or ``tls.classical_hosts`` settings are
invalid. In the second case the configuration is still printed.
* - 2
- - An unknown argument.
+ - An option or an argument: ``config`` takes none.
Files and environment
---------------------
diff --git a/Documentation/reference/cli/image.rst b/Documentation/reference/cli/image.rst
index 6383e1ce..151ec8c4 100644
--- a/Documentation/reference/cli/image.rst
+++ b/Documentation/reference/cli/image.rst
@@ -34,7 +34,7 @@ Usage
The subcommand names must be spelled out. ``image`` without a subcommand
prints the usage and exits with 2. ``--list-cmds`` prints
-``build verify update stage status`` on one line.
+``build``, ``verify``, ``update``, ``stage`` and ``status``, one a line.
An image is four files that share a prefix: ``<prefix>.img`` (the ext4
file system), ``<prefix>.verity`` (the hash tree), ``<prefix>.roothash``
@@ -209,9 +209,10 @@ Exit status
such device`` and ``status`` with ``image status: no config
activation state on this host``.
* - 2
- - No subcommand, an unknown subcommand, a wrong number of arguments
- to ``update``, ``stage`` or ``status``, or ``verify`` arguments
- in another form.
+ - No subcommand, an unknown subcommand, an unknown option or one
+ given twice, a wrong number of arguments to ``update``, ``stage``
+ or ``status``, ``stage`` with both a prefix and ``--cancel`` or
+ neither, or ``verify`` arguments in another form.
* - 127
- ``build``, ``verify``: ``shirudo`` cannot execute the helper.
diff --git a/Documentation/reference/cli/init.rst b/Documentation/reference/cli/init.rst
index 27bd2a8c..86d64cd7 100644
--- a/Documentation/reference/cli/init.rst
+++ b/Documentation/reference/cli/init.rst
@@ -153,7 +153,8 @@ Exit status
check failed (``init: pre-flight failed, <n> of <m> checks``), or
a phase failed (``init: <phase> failed: <error>``).
* - 2
- - An unknown option, or ``--json`` without ``--check-only``.
+ - A usage error: an unknown option, an option given twice or an
+ argument, or ``--json`` without ``--check-only``.
Files and environment
---------------------
diff --git a/Documentation/reference/cli/status.rst b/Documentation/reference/cli/status.rst
index eea7226a..37fa6b5a 100644
--- a/Documentation/reference/cli/status.rst
+++ b/Documentation/reference/cli/status.rst
@@ -254,8 +254,9 @@ reads ``.status`` and the conditions from the JSON document.
With ``--keepalive``: the body cannot be built. Also an allocation
failure.
* - 2
- - An unknown option, ``--keepalive`` with ``--json`` or ``--otel``,
- or with ``--otel`` an invalid ``map_export`` configuration.
+ - A usage error: an unknown option, an option given twice or an
+ argument, ``--keepalive`` with ``--json`` or ``--otel``. Or, with
+ ``--otel``, an invalid ``map_export`` configuration.
Files and environment
---------------------
diff --git a/Documentation/reference/cli/version.rst b/Documentation/reference/cli/version.rst
index 6368dbcd..2acc0142 100644
--- a/Documentation/reference/cli/version.rst
+++ b/Documentation/reference/cli/version.rst
@@ -53,13 +53,11 @@ Options
- off
- Print only the version id, on one line.
-Only the first argument is read. Any argument other than ``--version-id``
-and ``--help`` is ignored.
-
Exit status
-----------
-0 in every case.
+0, or 2 for a usage error: an unknown option, ``--version-id`` given
+twice, or an argument.
Examples
--------
diff --git a/builtin-config.c b/builtin-config.c
index daa8eddf..945bc33e 100644
--- a/builtin-config.c
+++ b/builtin-config.c
@@ -18,6 +18,7 @@
#include "util/config.h"
#include "util/help.h"
#include "util/log.h"
+#include "util/options.h"
#include "util/paths.h"
static const struct help config_help = {
@@ -29,27 +30,21 @@ static const struct help config_help = {
" shirudo config > " CONF_PATH,
};
-static void usage(void)
-{
- fprintf(stderr, "usage: %s\n", config_help.usage);
-}
-
int cmd_config(int argc, const char **argv)
{
struct config cfg __cleanup(config_clear);
bool image_mode = image_config_active();
+ struct opt_parser p;
bool file_present;
- int ret;
- int i;
+ int ret, opt;
- for (i = 1; i < argc; i++) {
- if (is_help(argv[i])) {
- print_help(&config_help);
+ opt_init(&p, "config", NULL, 0, &config_help, argc, argv, 1);
+ for (opt = opt_next(&p); opt != OPT_END; opt = opt_next(&p)) {
+ if (opt == OPT_DONE)
return 0;
- }
- pr_err("config: unknown option '%s'", argv[i]);
- usage();
- return 2;
+ if (opt == OPT_ARG)
+ opt_usage(&p, "unexpected argument '%s'", p.value);
+ return EXIT_USAGE;
}
file_present = access(CONF_PATH, F_OK) == 0;
diff --git a/builtin-image.c b/builtin-image.c
index c49971bd..f7fa3f68 100644
--- a/builtin-image.c
+++ b/builtin-image.c
@@ -2,62 +2,188 @@
/* Copyright (c) 2026 Authors of shirudo */
#include <errno.h>
+#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "builtin.h"
+#include "util/help.h"
#include "util/image_config.h"
#include "util/image_runtime.h"
#include "util/log.h"
#include "util/malloc.h"
+#include "util/options.h"
-static void image_help(void)
+static const struct help_item image_subcmds[] = {
+ { "build", "build a signed image from a file system image (operator)" },
+ { "verify", "check an image's signature and hash tree (operator)" },
+ { "update", "activate a config image now (target)" },
+ { "stage", "select a config image for the next boot (target)" },
+ { "status", "print the state of config activation (target)" },
+};
+
+enum image_stage_opt {
+ IMAGE_STAGE_CANCEL,
+};
+
+static const struct opt_spec image_stage_opts[] = {
+ [IMAGE_STAGE_CANCEL] = { "--cancel", 0, 0, NULL,
+ "drop the selection for the next boot" },
+};
+
+enum image_status_opt {
+ IMAGE_STATUS_JSON,
+};
+
+static const struct opt_spec image_status_opts[] = {
+ [IMAGE_STATUS_JSON] = { "--json", 0, 0, NULL,
+ "print the state as JSON" },
+};
+
+static const struct help_section image_help_sections[] = {
+ HELP_ITEMS("subcommands:", image_subcmds),
+ HELP_OPTIONS("stage options:", image_stage_opts),
+ HELP_OPTIONS("status options:", image_status_opts),
+};
+
+static const struct help image_help = {
+ .usage = "shirudo image build [image builder options]\n"
+ " shirudo image verify <prefix> --cert <certificate>\n"
+ " shirudo image update <prefix-or-https-prefix>\n"
+ " shirudo image stage <prefix-or-https-prefix>\n"
+ " shirudo image stage --cancel\n"
+ " shirudo image status [--json]",
+ .sections = image_help_sections,
+ .n_sections = ARRAY_SIZE(image_help_sections),
+};
+
+static int image_done(const char *sub, int ret)
{
- fprintf(stderr,
- "usage: shirudo image build [image builder options]\n"
- " shirudo image verify <prefix> --cert <certificate>\n"
- " shirudo image update <prefix-or-https-prefix>\n"
- " shirudo image stage <prefix-or-https-prefix>\n"
- " shirudo image stage --cancel\n"
- " shirudo image status [--json]\n");
+ if (ret == -EINPROGRESS)
+ pr_err("image: activation is pending; further changes are blocked");
+ else if (ret)
+ pr_err("image %s: %s", sub, strerror(-ret));
+ return ret ? 1 : 0;
}
-int cmd_image(int argc, const char **argv)
+/*
+ * The arguments of an update or a stage: OPT_END with *prefix or, where
+ * cancel is given, *cancel set, or OPT_DONE or OPT_USAGE to stop at.
+ */
+static int image_target(struct opt_parser *p, bool *cancel,
+ const char **prefix)
+{
+ int opt;
+
+ *prefix = NULL;
+ for (opt = opt_next(p); opt != OPT_END; opt = opt_next(p)) {
+ switch (opt) {
+ case IMAGE_STAGE_CANCEL:
+ *cancel = true;
+ break;
+ case OPT_ARG:
+ if (*prefix)
+ return opt_usage(p, "unexpected argument '%s'",
+ p->value);
+ *prefix = p->value;
+ break;
+ default:
+ return opt;
+ }
+ }
+ if (cancel && *cancel == !!*prefix)
+ return opt_usage(p, "takes <prefix> or --cancel");
+ if (!cancel && !*prefix)
+ return opt_usage(p, "takes <prefix>");
+ return OPT_END;
+}
+
+static int image_update_cmd(int argc, const char **argv)
{
+ const char *prefix;
+ struct opt_parser p;
int ret;
- if (argc == 2 && !strcmp(argv[1], "--list-cmds")) {
- puts("build verify update stage status");
+ opt_init(&p, "image update", NULL, 0, &image_help, argc, argv, 1);
+ ret = image_target(&p, NULL, &prefix);
+ if (ret != OPT_END)
+ return ret == OPT_DONE ? 0 : EXIT_USAGE;
+ return image_done("update", image_update(prefix));
+}
+
+static int image_stage_cmd(int argc, const char **argv)
+{
+ bool cancel = false;
+ const char *prefix;
+ struct opt_parser p;
+ int ret;
+
+ OPT_INIT(&p, "image stage", image_stage_opts, &image_help, argc, argv,
+ 1);
+ ret = image_target(&p, &cancel, &prefix);
+ if (ret != OPT_END)
+ return ret == OPT_DONE ? 0 : EXIT_USAGE;
+ return image_done("stage", cancel ? image_stage_cancel() :
+ image_stage(prefix));
+}
+
+static int image_status_cmd(int argc, const char **argv)
+{
+ struct opt_parser p;
+ bool json = false;
+ int opt, ret;
+
+ OPT_INIT(&p, "image status", image_status_opts, &image_help, argc,
+ argv, 1);
+ for (opt = opt_next(&p); opt != OPT_END; opt = opt_next(&p)) {
+ switch (opt) {
+ case IMAGE_STATUS_JSON:
+ json = true;
+ break;
+ case OPT_ARG:
+ opt_usage(&p, "unexpected argument '%s'", p.value);
+ return EXIT_USAGE;
+ case OPT_DONE:
+ return 0;
+ default:
+ return EXIT_USAGE;
+ }
+ }
+ ret = image_status(json);
+ if (ret == -ENODEV) {
+ pr_err("image status: no config activation state on this host");
+ return 1;
+ }
+ return image_done("status", ret);
+}
+
+int cmd_image(int argc, const char **argv)
+{
+ const char *names[ARRAY_SIZE(image_subcmds)];
+ size_t i;
+
+ if (argc >= 2 && is_list_cmds(argv[1])) {
+ print_subcmd_list(image_subcmds, ARRAY_SIZE(image_subcmds));
return 0;
}
- if (argc < 2 || !strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")) {
- image_help();
- return argc < 2 ? 2 : 0;
+ if (argc < 2 || is_help(argv[1])) {
+ print_help(&image_help);
+ return argc < 2 ? EXIT_USAGE : 0;
}
if (!strcmp(argv[1], "build") || !strcmp(argv[1], "verify"))
return build_forward(!strcmp(argv[1], "build") ?
"image build" : "image verify",
(const char *const[]){ "image", NULL },
argc - 1, argv + 1);
- if (!strcmp(argv[1], "update") && argc == 3) {
- ret = image_update(argv[2]);
- } else if (!strcmp(argv[1], "stage") && argc == 3) {
- ret = !strcmp(argv[2], "--cancel") ? image_stage_cancel() :
- image_stage(argv[2]);
- } else if (!strcmp(argv[1], "status") &&
- (argc == 2 || (argc == 3 && !strcmp(argv[2], "--json")))) {
- ret = image_status(argc == 3);
- if (ret == -ENODEV) {
- pr_err("image status: no config activation state on this host");
- return 1;
- }
- } else {
- image_help();
- return 2;
- }
- if (ret == -EINPROGRESS)
- pr_err("image: activation is pending; further changes are blocked");
- else if (ret)
- pr_err("image %s: %s", argv[1], strerror(-ret));
- return ret ? 1 : 0;
+ if (!strcmp(argv[1], "update"))
+ return image_update_cmd(argc - 1, argv + 1);
+ if (!strcmp(argv[1], "stage"))
+ return image_stage_cmd(argc - 1, argv + 1);
+ if (!strcmp(argv[1], "status"))
+ return image_status_cmd(argc - 1, argv + 1);
+ pr_err("image: '%s' is not a subcommand", argv[1]);
+ for (i = 0; i < ARRAY_SIZE(image_subcmds); i++)
+ names[i] = image_subcmds[i].name;
+ help_suggest(argv[1], names, ARRAY_SIZE(names));
+ return EXIT_USAGE;
}
diff --git a/builtin-init.c b/builtin-init.c
index ea6dd4bf..462492bf 100644
--- a/builtin-init.c
+++ b/builtin-init.c
@@ -49,6 +49,7 @@
#include "util/keys.h"
#include "util/loader.h"
#include "util/log.h"
+#include "util/options.h"
#include "util/malloc.h"
#include "util/nv_writer.h"
#include "util/paths.h"
@@ -1328,13 +1329,20 @@ static int chain_extend_init(struct measured_upatch *set, size_t n)
return 0;
}
-static const struct help_item init_options[] = {
- { "--check-only", "run the pre-flight only; never touch kernel state" },
- { "--json", "with --check-only, print the checks as JSON" },
+enum init_opt {
+ INIT_CHECK_ONLY,
+ INIT_JSON,
+};
+
+static const struct opt_spec init_opts[] = {
+ [INIT_CHECK_ONLY] = { "--check-only", 0, 0, NULL,
+ "run the pre-flight only; never touch kernel state" },
+ [INIT_JSON] = { "--json", 0, 0, NULL,
+ "with --check-only, print the checks as JSON" },
};
static const struct help_section init_help_sections[] = {
- HELP_ITEMS("options:", init_options),
+ HELP_OPTIONS("options:", init_opts),
};
static const struct help init_help = {
@@ -1345,11 +1353,6 @@ static const struct help init_help = {
.n_sections = ARRAY_SIZE(init_help_sections),
};
-static void usage(void)
-{
- fprintf(stderr, "usage: %s\n", init_help.usage);
-}
-
/* Where the state tree is on the root file system, which the initrd binds. */
static int state_source(char *buf, size_t cap)
{
@@ -1532,28 +1535,28 @@ static int run_init(void)
int cmd_init(int argc, const char **argv)
{
- struct preflight_result res;
bool check_only = false, json = false;
- int i, ret;
-
- for (i = 1; i < argc; i++) {
- const char *a = argv[i];
+ struct preflight_result res;
+ struct opt_parser p;
+ int opt, ret;
- if (strcmp(a, "--check-only") == 0) {
+ OPT_INIT(&p, "init", init_opts, &init_help, argc, argv, 1);
+ for (opt = opt_next(&p); opt != OPT_END; opt = opt_next(&p)) {
+ switch (opt) {
+ case INIT_CHECK_ONLY:
check_only = true;
- continue;
- }
- if (strcmp(a, "--json") == 0) {
+ break;
+ case INIT_JSON:
json = true;
- continue;
- }
- if (is_help(a)) {
- print_help(&init_help);
+ break;
+ case OPT_ARG:
+ opt_usage(&p, "unexpected argument '%s'", p.value);
+ return EXIT_USAGE;
+ case OPT_DONE:
return 0;
+ default:
+ return EXIT_USAGE;
}
- pr_err("init: unknown option '%s'", a);
- usage();
- return 2;
}
initrd_mode = in_initrd();
@@ -1563,8 +1566,8 @@ int cmd_init(int argc, const char **argv)
return 1;
}
if (json && !check_only) {
- pr_err("init: --json takes --check-only");
- return 2;
+ opt_usage(&p, "--json takes --check-only");
+ return EXIT_USAGE;
}
preflight_run(&res);
if (json) {
diff --git a/builtin-status.c b/builtin-status.c
index 0b4f0981..00f88227 100644
--- a/builtin-status.c
+++ b/builtin-status.c
@@ -48,6 +48,7 @@
#include "util/keyring.h"
#include "util/keys.h"
#include "util/log.h"
+#include "util/options.h"
#include "util/malloc.h"
#include "util/paths.h"
#include "util/initrd.h"
@@ -354,21 +355,35 @@ static void print_system_human(const struct sysinfo *si)
printf(" %-22s %s\n", "Boot ID:", si->boot_id);
}
-static const struct help_item status_options[] = {
- { "--json", "print the report as one JSON document" },
- { "--otel", "quote the keepalive body with the TPM and send "
- "it as an OTLP log" },
- { "--keepalive", "print the keepalive body, unquoted and unsent" },
- { "--check", "exit 1 when the verdict is tampered" },
- { "--show-full", "print every section, folded or not, and the "
- "system and eventlog sections" },
- { "--include-system", "add the system section: OS, kernel, machine, "
- "memory, disk, uptime" },
- { "--include-eventlog", "add the eventlog records, all boots" },
+enum status_opt {
+ STATUS_JSON,
+ STATUS_OTEL,
+ STATUS_KEEPALIVE,
+ STATUS_CHECK,
+ STATUS_SHOW_FULL,
+ STATUS_INCLUDE_SYSTEM,
+ STATUS_INCLUDE_EVENTLOG,
+};
+
+static const struct opt_spec status_opts[] = {
+ [STATUS_JSON] = { "--json", 0, 0, NULL,
+ "print the report as one JSON document" },
+ [STATUS_OTEL] = { "--otel", 0, 0, NULL,
+ "quote the keepalive body with the TPM and send it as an OTLP log" },
+ [STATUS_KEEPALIVE] = { "--keepalive", 0, 0, NULL,
+ "print the keepalive body, unquoted and unsent" },
+ [STATUS_CHECK] = { "--check", 0, 0, NULL,
+ "exit 1 when the verdict is tampered" },
+ [STATUS_SHOW_FULL] = { "--show-full", 0, 0, NULL,
+ "print every section, folded or not, and the system and eventlog sections" },
+ [STATUS_INCLUDE_SYSTEM] = { "--include-system", 0, 0, NULL,
+ "add the system section: OS, kernel, machine, memory, disk, uptime" },
+ [STATUS_INCLUDE_EVENTLOG] = { "--include-eventlog", 0, 0, NULL,
+ "add the eventlog records, all boots" },
};
static const struct help_section status_help_sections[] = {
- HELP_ITEMS("options:", status_options),
+ HELP_OPTIONS("options:", status_opts),
};
static const struct help status_help = {
@@ -385,11 +400,6 @@ static const struct help status_help = {
.n_sections = ARRAY_SIZE(status_help_sections),
};
-static void usage(void)
-{
- fprintf(stderr, "usage: %s\n", status_help.usage);
-}
-
static size_t build_upatches_table(struct dirent **ups, int n_ups,
struct upatch_table_row **out)
{
@@ -1180,36 +1190,45 @@ int cmd_status(int argc, const char **argv)
char config_root_hex[65] = "";
__u8 config_root[32];
int unmeasured, lock;
- int config_state;
+ int config_state, opt;
+ struct opt_parser p;
- for (i = 1; i < argc; i++) {
- if (strcmp(argv[i], "--json") == 0) {
+ OPT_INIT(&p, "status", status_opts, &status_help, argc, argv, 1);
+ for (opt = opt_next(&p); opt != OPT_END; opt = opt_next(&p)) {
+ switch (opt) {
+ case STATUS_JSON:
json_stdout = true;
- } else if (strcmp(argv[i], "--otel") == 0) {
+ break;
+ case STATUS_OTEL:
otel = true;
- } else if (strcmp(argv[i], "--keepalive") == 0) {
+ break;
+ case STATUS_KEEPALIVE:
keepalive = true;
- } else if (strcmp(argv[i], "--include-system") == 0) {
- include_system = true;
- } else if (strcmp(argv[i], "--include-eventlog") == 0) {
- include_eventlog = true;
- } else if (strcmp(argv[i], "--check") == 0) {
+ break;
+ case STATUS_CHECK:
check = true;
- } else if (strcmp(argv[i], "--show-full") == 0) {
+ break;
+ case STATUS_SHOW_FULL:
show_full = true;
- } else if (is_help(argv[i])) {
- print_help(&status_help);
+ break;
+ case STATUS_INCLUDE_SYSTEM:
+ include_system = true;
+ break;
+ case STATUS_INCLUDE_EVENTLOG:
+ include_eventlog = true;
+ break;
+ case OPT_ARG:
+ opt_usage(&p, "unexpected argument '%s'", p.value);
+ return EXIT_USAGE;
+ case OPT_DONE:
return 0;
- } else {
- fprintf(stderr, "status: unknown option '%s'\n", argv[i]);
- usage();
- return 2;
+ default:
+ return EXIT_USAGE;
}
}
if (keepalive && (json_stdout || otel)) {
- fprintf(stderr, "status: --keepalive goes without --json and --otel\n");
- usage();
- return 2;
+ opt_usage(&p, "--keepalive goes without --json and --otel");
+ return EXIT_USAGE;
}
if (otel) {
int err = config_load(&otel_cfg);
diff --git a/builtin-version.c b/builtin-version.c
index c908227a..8cfb1ff6 100644
--- a/builtin-version.c
+++ b/builtin-version.c
@@ -12,14 +12,20 @@
#include "builtin.h"
#include "util/help.h"
+#include "util/options.h"
#include "util/version.h"
-static const struct help_item version_options[] = {
- { "--version-id", "print only the raw SHIRUDO_VERSION_ID, one line" },
+enum version_opt {
+ VERSION_ID,
+};
+
+static const struct opt_spec version_opts[] = {
+ [VERSION_ID] = { "--version-id", 0, 0, NULL,
+ "print only the raw SHIRUDO_VERSION_ID, one line" },
};
static const struct help_section version_help_sections[] = {
- HELP_ITEMS("options:", version_options),
+ HELP_OPTIONS("options:", version_opts),
};
static const struct help version_help = {
@@ -33,11 +39,26 @@ static const struct help version_help = {
int cmd_version(int argc, const char **argv)
{
- if (argc >= 2 && is_help(argv[1])) {
- print_help(&version_help);
- return 0;
+ bool id_only = false;
+ struct opt_parser p;
+ int opt;
+
+ OPT_INIT(&p, "version", version_opts, &version_help, argc, argv, 1);
+ for (opt = opt_next(&p); opt != OPT_END; opt = opt_next(&p)) {
+ switch (opt) {
+ case VERSION_ID:
+ id_only = true;
+ break;
+ case OPT_ARG:
+ opt_usage(&p, "unexpected argument '%s'", p.value);
+ return EXIT_USAGE;
+ case OPT_DONE:
+ return 0;
+ default:
+ return EXIT_USAGE;
+ }
}
- if (argc >= 2 && strcmp(argv[1], "--version-id") == 0) {
+ if (id_only) {
printf("%s\n", shirudo_version_id);
return 0;
}