Skip to content

eBPF-based security platform

options: read an option's value from a set of names

Daniel Borkmannf5b6add81517
diff --git a/.agents/skills/coding/SKILL.md b/.agents/skills/coding/SKILL.md
index 701c3120..607b4d95 100644
--- a/.agents/skills/coding/SKILL.md
+++ b/.agents/skills/coding/SKILL.md
@@ -282,7 +282,7 @@ already, or should. The basics:
| SHA-256 | `sha256_buffer()`, or `sha256_init()`, `sha256_update()` and `sha256_final()` over pieces (`util/crypto.h`); `sha256_digest_encode()` and `sha256_digest_decode()` for the `sha256:<hex>` form (`util/hex.h`), `bpm_sha256_digest()` to hash and write it at once (`util/bpm_oci.h`) |
| JSON | `util/json.h`: `json_parse()` with a schema, `json_walk()`, `struct json_emit` |
| Messages | `pr_err()`, `pr_warn()`, `pr_info()`, `pr_audit()` (`util/log.h`) |
-| A command's options | a table of `struct opt_spec` indexed by the command's enum, `OPT_INIT()`, a switch over `opt_next()`, `opt_u64()`, `opt_usage()` and `EXIT_USAGE` (`util/options.h`); `HELP_OPTIONS()` lists the same table in help, and `--list-opts` for completion; never compare `argv[]` by hand |
+| A command's options | a table of `struct opt_spec` indexed by the command's enum, `OPT_INIT()`, a switch over `opt_next()`, `opt_u64()`, `OPT_CHOICE()` for a value from a set of names, `opt_usage()` and `EXIT_USAGE` (`util/options.h`); `HELP_OPTIONS()` lists the same table in help, and `--list-opts` for completion; never compare `argv[]` by hand |
Code that a second place needs moves into `util/<m>.c`, declared in
`util/<m>.h`, or in `include/shirudo/` for the project's basics, with
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index a2db9925..ed89e11b 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -1357,7 +1357,9 @@ of :src:`util/upatch_maps.c` that puts a page together from the chunks
limit of 64 CBMC ends a run on the code as it is and none on the code
broken, so no proof of the limit is kept. ``options-next`` takes
``parse_u64()`` by the contract ``string-number`` proves and leaves out
-the text of the usage messages, which only ``pr_err()`` reads. Of :src:`util/sysinfo.c` the
+the text of the usage messages, which only ``pr_err()`` reads, and
+``opt_choice()``, a comparison of the value with a fixed table, to the
+unit cases. Of :src:`util/sysinfo.c` the
proofs cover the image identity's parsers; reading the files they parse,
and the readers of ``/proc`` and the root file system, go to the kernel
and have none.
diff --git a/testing/unit/options.c b/testing/unit/options.c
index 34907378..c9e2ab73 100644
--- a/testing/unit/options.c
+++ b/testing/unit/options.c
@@ -189,6 +189,28 @@ static void a_number_is_bounded(void)
EXPECT_EQ(opt_u64(&p, 1, 512, &n), OPT_USAGE);
}
+static void a_choice_names_its_value(void)
+{
+ static const struct opt_name ops[] = {
+ { "add", 0 },
+ { "delete", 1 },
+ };
+ static const char *const del[] = { "--tag=delete" };
+ static const char *const bad[] = { "--tag=remove" };
+ struct opt_parser p;
+ int op = -1;
+
+ parse(&p, del, 1);
+ ASSERT_EQ(opt_next(&p), T_TAG);
+ ASSERT_EQ(OPT_CHOICE(&p, ops, &op), 0);
+ EXPECT_EQ(op, 1);
+ op = -1;
+ parse(&p, bad, 1);
+ ASSERT_EQ(opt_next(&p), T_TAG);
+ EXPECT_EQ(OPT_CHOICE(&p, ops, &op), OPT_USAGE);
+ EXPECT_EQ(op, -1);
+}
+
static void a_malformed_table_is_refused_before_any_argument(void)
{
static const struct opt_spec hole[] = {
@@ -227,6 +249,7 @@ static const struct unit_case cases[] = {
UNIT_CASE(an_optional_value_comes_after_equals_alone),
UNIT_CASE(help_and_the_option_list_end_parsing),
UNIT_CASE(a_number_is_bounded),
+ UNIT_CASE(a_choice_names_its_value),
UNIT_CASE(a_malformed_table_is_refused_before_any_argument),
{}
};
diff --git a/util/options.c b/util/options.c
index c2b329d6..23dfec46 100644
--- a/util/options.c
+++ b/util/options.c
@@ -220,3 +220,29 @@ int opt_u64(const struct opt_parser *p, __u64 min, __u64 max, __u64 *out)
*out = v;
return 0;
}
+
+int opt_choice(const struct opt_parser *p, const struct opt_name *names,
+ size_t n, int *out)
+{
+ char list[256] = "";
+ size_t i, len = 0;
+
+ if (p->opt < 0 || !p->value)
+ return opt_usage(p, "no option value to read as a name");
+ for (i = 0; i < n; i++) {
+ if (!strcmp(p->value, names[i].name)) {
+ *out = names[i].value;
+ return 0;
+ }
+ }
+ for (i = 0; i < n; i++) {
+ const char *sep = !i ? "" : i + 1 < n ? ", " : " or ";
+ int ret = strsprintf(list + len, sizeof(list) - len, "%s%s",
+ sep, names[i].name);
+
+ if (ret < 0)
+ break;
+ len += (size_t)ret;
+ }
+ return opt_usage(p, "%s takes %s", p->specs[p->opt].name, list);
+}
diff --git a/util/options.h b/util/options.h
index 5781e39d..feae7f3d 100644
--- a/util/options.h
+++ b/util/options.h
@@ -98,6 +98,24 @@ static inline bool opt_given(const struct opt_parser *p, int opt)
int opt_u64(const struct opt_parser *p, __u64 min, __u64 max,
__u64 *out) __must_check;
+/* A name an option's value may be, and the value it stands for. */
+struct opt_name {
+ const char *name;
+ int value;
+};
+
+/*
+ * The value of the name the option last returned was given, from the n
+ * names of names: 0, or OPT_USAGE, reported with the names it takes.
+ */
+int opt_choice(const struct opt_parser *p, const struct opt_name *names,
+ size_t n, int *out) __must_check;
+
+#define OPT_CHOICE(p, names, out) \
+ opt_choice(p, names, \
+ sizeof(names) / sizeof((names)[0]) + __must_be_array(names), \
+ out)
+
/* A usage error the command finds, as "<command>: <message>": OPT_USAGE. */
int opt_usage(const struct opt_parser *p, const char *fmt, ...)
__printf(2, 3);