diff --git a/cmd/ifquery.c b/cmd/ifquery.c index 621b9d7..dd17ddc 100644 --- a/cmd/ifquery.c +++ b/cmd/ifquery.c @@ -159,6 +159,7 @@ list_interfaces(struct lif_dict *collection, struct match_options *opts) } static bool listing = false, listing_stat = false, listing_running = false; +static bool allow_undefined = false; void list_state(struct lif_dict *state, struct match_options *opts) @@ -228,6 +229,13 @@ set_property(const char *opt_arg) match_opts.property = opt_arg; } +static void +set_allow_undefined(const char *opt_arg) +{ + (void) opt_arg; + allow_undefined = true; +} + static struct if_option local_options[] = { {'r', "running", NULL, "show configured (running) interfaces", false, set_show_running}, {'s', "state", NULL, "show configured state", false, set_show_state}, @@ -235,6 +243,7 @@ static struct if_option local_options[] = { {'D', "dot", NULL, "generate a dependency graph", false, set_output_dot}, {'L', "list", NULL, "list matching interfaces", false, set_listing}, {'P', "pretty-print", NULL, "pretty print the interfaces instead of just listing", false, set_pretty_print}, + {'U', "allow-undefined", NULL, "allow querying undefined (virtual) interfaces", false, set_allow_undefined}, }; static struct if_option_group local_option_group = { @@ -307,6 +316,9 @@ ifquery_main(int argc, char *argv[]) if (entry != NULL) iface = entry->data; + + if (entry == NULL && allow_undefined) + iface = lif_interface_collection_find(&collection, argv[idx]); } if (iface == NULL) diff --git a/doc/ifquery.scd b/doc/ifquery.scd index 6e2b6bd..83c8480 100644 --- a/doc/ifquery.scd +++ b/doc/ifquery.scd @@ -54,6 +54,11 @@ configuration file to the current format. When listing interfaces, print their configuration in a format that is compatible with *interfaces*(5) files. +*-U, --allow-undefined* + Create virtual interfaces for any interfaces not explicitly + defined in the configuration file. This is primarily useful + for property queries. + *-S, --state-file* _FILE_ Use _FILE_ as the state database. diff --git a/tests/ifquery_test b/tests/ifquery_test index e095255..92a4b6b 100755 --- a/tests/ifquery_test +++ b/tests/ifquery_test @@ -34,7 +34,9 @@ tests_init \ vlan_explicit_learned_dependency \ vlan_guessed_learned_dependency \ vlan_complex_learned_dependency \ - wireguard + wireguard \ + allow_undefined_positive \ + allow_undefined_negative noargs_body() { atf_check -s exit:1 -e ignore ifquery -S/dev/null @@ -226,3 +228,17 @@ wireguard_body() { -o match:"use wireguard" \ ifquery -E $EXECUTORS_LINUX -i $FIXTURES/wireguard.interfaces wg0 } + +allow_undefined_positive_body() { + atf_check -s exit:0 \ + -o ignore \ + -e ignore \ + ifquery -U -i /dev/null -p address foo +} + +allow_undefined_negative_body() { + atf_check -s exit:1 \ + -o ignore \ + -e ignore \ + ifquery -i /dev/null -p address foo +}