From 1824d05bd56203a1935317ca55db9172df3be2f5 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Thu, 23 Jul 2020 11:56:20 -0600 Subject: [PATCH] lifecycle: implement support for IF_ env vars based on triples --- libifupdown/lifecycle.c | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/libifupdown/lifecycle.c b/libifupdown/lifecycle.c index fd030f1..33c6308 100644 --- a/libifupdown/lifecycle.c +++ b/libifupdown/lifecycle.c @@ -13,6 +13,7 @@ * from the use of this software. */ +#include #include #include "libifupdown/environment.h" @@ -183,6 +184,49 @@ lif_lifecycle_run_phase(const struct lif_execute_opts *opts, struct lif_interfac else lif_environment_push(&envp, "MODE", "stop"); + struct lif_node *iter; + bool did_address = false, did_gateway = false; + + LIF_DICT_FOREACH(iter, &iface->vars) + { + struct lif_dict_entry *entry = iter->data; + + if (!strcmp(entry->key, "address")) + { + if (did_address) + continue; + + struct lif_address *addr = entry->data; + char addrbuf[4096]; + + if (!lif_address_unparse(addr, addrbuf, sizeof addrbuf, true)) + continue; + + lif_environment_push(&envp, "IF_ADDRESS", addrbuf); + did_address = true; + + continue; + } + else if (!strcmp(entry->key, "gateway")) + { + if (did_gateway) + continue; + + did_gateway = true; + } + + char envkey[4096] = "IF_"; + strlcat(envkey, entry->key, sizeof envkey); + char *ep = envkey + 2; + + while (*ep++) + { + *ep = toupper(*ep); + } + + lif_environment_push(&envp, envkey, (const char *) entry->data); + } + if (!strcmp(phase, "pre-up")) { if (!handle_pre_up(opts, iface, lifname))