2020-07-23 14:51:22 +00:00
|
|
|
/*
|
|
|
|
* libifupdown/lifecycle.c
|
|
|
|
* Purpose: management of interface lifecycle (bring up, takedown, reload)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* This software is provided 'as is' and without any warranty, express or
|
|
|
|
* implied. In no event shall the authors be liable for any damages arising
|
|
|
|
* from the use of this software.
|
|
|
|
*/
|
|
|
|
|
2020-07-23 17:56:20 +00:00
|
|
|
#include <ctype.h>
|
2020-07-23 14:51:22 +00:00
|
|
|
#include <string.h>
|
2020-08-26 23:15:51 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2020-07-23 14:51:22 +00:00
|
|
|
|
|
|
|
#include "libifupdown/environment.h"
|
|
|
|
#include "libifupdown/execute.h"
|
|
|
|
#include "libifupdown/interface.h"
|
|
|
|
#include "libifupdown/lifecycle.h"
|
|
|
|
#include "libifupdown/state.h"
|
2020-07-24 10:12:42 +00:00
|
|
|
#include "libifupdown/tokenize.h"
|
2020-07-23 14:51:22 +00:00
|
|
|
|
|
|
|
static bool
|
2020-09-07 19:41:11 +00:00
|
|
|
handle_commands_for_phase(const struct lif_execute_opts *opts, char *const envp[], const struct lif_interface *iface, const char *phase)
|
2020-07-23 14:51:22 +00:00
|
|
|
{
|
2020-09-07 19:41:11 +00:00
|
|
|
const struct lif_node *iter;
|
2020-07-23 14:51:22 +00:00
|
|
|
|
|
|
|
LIF_DICT_FOREACH(iter, &iface->vars)
|
|
|
|
{
|
2020-09-07 19:41:11 +00:00
|
|
|
const struct lif_dict_entry *entry = iter->data;
|
2020-07-23 14:51:22 +00:00
|
|
|
|
|
|
|
if (strcmp(entry->key, phase))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const char *cmd = entry->data;
|
|
|
|
if (!lif_execute_fmt(opts, envp, "%s", cmd))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-24 17:04:48 +00:00
|
|
|
static inline bool
|
2020-08-26 08:06:36 +00:00
|
|
|
handle_single_executor_for_phase(const struct lif_dict_entry *entry, const struct lif_execute_opts *opts, char *const envp[], const char *phase)
|
2020-08-24 17:04:48 +00:00
|
|
|
{
|
|
|
|
if (strcmp(entry->key, "use"))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const char *cmd = entry->data;
|
2020-08-26 08:06:36 +00:00
|
|
|
if (!lif_maybe_run_executor(opts, envp, cmd, phase))
|
2020-08-24 17:04:48 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-25 08:19:15 +00:00
|
|
|
static bool
|
2020-09-07 19:41:11 +00:00
|
|
|
handle_executors_for_phase(const struct lif_execute_opts *opts, char *const envp[], const struct lif_interface *iface, bool up, const char *phase)
|
2020-07-25 08:19:15 +00:00
|
|
|
{
|
2020-09-07 19:41:11 +00:00
|
|
|
const struct lif_node *iter;
|
2020-07-25 08:19:15 +00:00
|
|
|
|
2020-08-24 17:04:48 +00:00
|
|
|
if (up)
|
2020-07-25 08:19:15 +00:00
|
|
|
{
|
2020-08-24 17:04:48 +00:00
|
|
|
LIF_DICT_FOREACH(iter, &iface->vars)
|
2020-08-26 08:06:36 +00:00
|
|
|
handle_single_executor_for_phase(iter->data, opts, envp, phase);
|
2020-08-24 17:04:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LIF_DICT_FOREACH_REVERSE(iter, &iface->vars)
|
2020-08-26 08:06:36 +00:00
|
|
|
handle_single_executor_for_phase(iter->data, opts, envp, phase);
|
2020-07-25 08:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-28 19:46:25 +00:00
|
|
|
static bool
|
2020-09-07 19:41:11 +00:00
|
|
|
query_dependents_from_executors(const struct lif_execute_opts *opts, char *const envp[], const struct lif_interface *iface, char *buf, size_t bufsize, const char *phase)
|
2020-07-28 19:46:25 +00:00
|
|
|
{
|
2020-09-07 19:41:11 +00:00
|
|
|
const struct lif_node *iter;
|
2020-07-28 19:46:25 +00:00
|
|
|
|
|
|
|
LIF_DICT_FOREACH(iter, &iface->vars)
|
|
|
|
{
|
|
|
|
char resbuf[1024] = {};
|
2020-09-07 19:41:11 +00:00
|
|
|
const struct lif_dict_entry *entry = iter->data;
|
2020-07-28 19:46:25 +00:00
|
|
|
struct lif_execute_opts exec_opts = {
|
|
|
|
.verbose = opts->verbose,
|
|
|
|
.executor_path = opts->executor_path,
|
|
|
|
.interfaces_file = opts->interfaces_file
|
|
|
|
};
|
|
|
|
|
|
|
|
if (strcmp(entry->key, "use"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const char *cmd = entry->data;
|
2020-08-26 08:06:36 +00:00
|
|
|
if (!lif_maybe_run_executor_with_result(&exec_opts, envp, cmd, resbuf, sizeof resbuf, phase))
|
2020-07-28 19:46:25 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!*resbuf)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
strlcat(buf, " ", bufsize);
|
|
|
|
strlcat(buf, resbuf, bufsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-29 08:24:10 +00:00
|
|
|
static void
|
2020-09-07 19:41:11 +00:00
|
|
|
build_environment(char **envp[], const struct lif_execute_opts *opts, const struct lif_interface *iface, const char *lifname, const char *phase, const char *mode)
|
2020-07-28 19:46:25 +00:00
|
|
|
{
|
|
|
|
if (lifname == NULL)
|
|
|
|
lifname = iface->ifname;
|
|
|
|
|
2020-07-29 08:24:10 +00:00
|
|
|
lif_environment_push(envp, "IFACE", lifname);
|
|
|
|
lif_environment_push(envp, "PHASE", phase);
|
|
|
|
lif_environment_push(envp, "MODE", mode);
|
2020-08-20 09:14:57 +00:00
|
|
|
lif_environment_push(envp, "METHOD", "none");
|
2020-07-23 14:51:22 +00:00
|
|
|
|
2020-07-25 08:18:53 +00:00
|
|
|
if (opts->verbose)
|
2020-07-29 08:24:10 +00:00
|
|
|
lif_environment_push(envp, "VERBOSE", "1");
|
2020-07-25 08:18:53 +00:00
|
|
|
|
2020-07-26 08:50:01 +00:00
|
|
|
if (opts->interfaces_file)
|
2020-07-29 08:24:10 +00:00
|
|
|
lif_environment_push(envp, "INTERFACES_FILE", opts->interfaces_file);
|
2020-07-26 08:50:01 +00:00
|
|
|
|
2020-09-07 19:41:11 +00:00
|
|
|
const struct lif_node *iter;
|
2020-07-23 17:56:20 +00:00
|
|
|
bool did_address = false, did_gateway = false;
|
|
|
|
|
|
|
|
LIF_DICT_FOREACH(iter, &iface->vars)
|
|
|
|
{
|
2020-09-07 19:41:11 +00:00
|
|
|
const struct lif_dict_entry *entry = iter->data;
|
2020-07-23 17:56:20 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2020-07-29 08:24:10 +00:00
|
|
|
lif_environment_push(envp, "IF_ADDRESS", addrbuf);
|
2020-07-23 17:56:20 +00:00
|
|
|
did_address = true;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (!strcmp(entry->key, "gateway"))
|
|
|
|
{
|
|
|
|
if (did_gateway)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
did_gateway = true;
|
|
|
|
}
|
2020-07-24 10:37:14 +00:00
|
|
|
else if (!strcmp(entry->key, "requires"))
|
|
|
|
{
|
|
|
|
if (iface->is_bridge)
|
2020-07-29 08:24:10 +00:00
|
|
|
lif_environment_push(envp, "IF_BRIDGE_PORTS", (const char *) entry->data);
|
2020-07-24 10:37:14 +00:00
|
|
|
|
|
|
|
if (iface->is_bond)
|
2020-07-29 08:24:10 +00:00
|
|
|
lif_environment_push(envp, "IF_BOND_SLAVES", (const char *) entry->data);
|
2020-07-24 10:37:14 +00:00
|
|
|
}
|
2020-07-23 17:56:20 +00:00
|
|
|
|
|
|
|
char envkey[4096] = "IF_";
|
|
|
|
strlcat(envkey, entry->key, sizeof envkey);
|
|
|
|
char *ep = envkey + 2;
|
|
|
|
|
|
|
|
while (*ep++)
|
|
|
|
{
|
|
|
|
*ep = toupper(*ep);
|
2020-07-24 07:03:44 +00:00
|
|
|
|
|
|
|
if (*ep == '-')
|
|
|
|
*ep = '_';
|
2020-07-23 17:56:20 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 08:24:10 +00:00
|
|
|
lif_environment_push(envp, envkey, (const char *) entry->data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
lif_lifecycle_query_dependents(const struct lif_execute_opts *opts, struct lif_interface *iface, const char *lifname)
|
|
|
|
{
|
|
|
|
char deps[4096] = {};
|
|
|
|
char final_deps[4096] = {};
|
|
|
|
|
|
|
|
if (lifname == NULL)
|
|
|
|
lifname = iface->ifname;
|
|
|
|
|
|
|
|
char **envp = NULL;
|
|
|
|
|
|
|
|
build_environment(&envp, opts, iface, lifname, "depend", "depend");
|
|
|
|
|
|
|
|
struct lif_dict_entry *entry = lif_dict_find(&iface->vars, "requires");
|
|
|
|
if (entry != NULL)
|
|
|
|
strlcpy(deps, entry->data, sizeof deps);
|
|
|
|
|
2020-08-26 08:06:36 +00:00
|
|
|
if (!query_dependents_from_executors(opts, envp, iface, deps, sizeof deps, "depend"))
|
2020-07-29 08:24:10 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
char *p = deps;
|
|
|
|
while (*p)
|
|
|
|
{
|
|
|
|
char *token = lif_next_token(&p);
|
|
|
|
|
|
|
|
if (strstr(final_deps, token) != NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
strlcat(final_deps, token, sizeof final_deps);
|
|
|
|
strlcat(final_deps, " ", sizeof final_deps);
|
2020-07-23 17:56:20 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 08:24:10 +00:00
|
|
|
if (entry != NULL)
|
|
|
|
{
|
|
|
|
free(entry->data);
|
|
|
|
entry->data = strdup(final_deps);
|
|
|
|
}
|
2020-07-31 23:12:13 +00:00
|
|
|
else if (*final_deps)
|
2020-07-29 08:24:10 +00:00
|
|
|
lif_dict_add(&iface->vars, "requires", strdup(final_deps));
|
|
|
|
|
|
|
|
lif_environment_free(&envp);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
lif_lifecycle_run_phase(const struct lif_execute_opts *opts, struct lif_interface *iface, const char *phase, const char *lifname, bool up)
|
|
|
|
{
|
|
|
|
char **envp = NULL;
|
|
|
|
|
|
|
|
build_environment(&envp, opts, iface, lifname, phase, up ? "start" : "stop");
|
|
|
|
|
2020-08-26 08:06:36 +00:00
|
|
|
if (!handle_executors_for_phase(opts, envp, iface, up, phase))
|
2020-07-26 09:38:55 +00:00
|
|
|
goto handle_error;
|
2020-07-23 14:51:22 +00:00
|
|
|
|
2020-07-26 09:39:58 +00:00
|
|
|
if (!handle_commands_for_phase(opts, envp, iface, phase))
|
2020-07-26 09:38:55 +00:00
|
|
|
goto handle_error;
|
2020-07-23 14:51:22 +00:00
|
|
|
|
2020-08-26 23:15:51 +00:00
|
|
|
/* Check if scripts dir for this phase is present and bail out if it isn't */
|
|
|
|
struct stat dir_stat;
|
|
|
|
char dir_path[4096];
|
|
|
|
snprintf (dir_path, 4096, "/etc/network/if-%s.d", phase);
|
|
|
|
|
|
|
|
if (stat (dir_path, &dir_stat) != 0 || S_ISDIR (dir_stat.st_mode) == 0) {
|
2020-08-27 10:10:12 +00:00
|
|
|
goto out_free;
|
2020-08-26 23:15:51 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 14:51:22 +00:00
|
|
|
/* we should do error handling here, but ifupdown1 doesn't */
|
2020-08-26 23:15:51 +00:00
|
|
|
lif_execute_fmt(opts, envp, "/bin/run-parts %s", dir_path);
|
2020-07-23 14:51:22 +00:00
|
|
|
|
2020-08-27 10:10:12 +00:00
|
|
|
out_free:
|
2020-07-23 14:51:22 +00:00
|
|
|
lif_environment_free(&envp);
|
|
|
|
return true;
|
|
|
|
|
2020-07-26 09:38:55 +00:00
|
|
|
handle_error:
|
2020-07-23 14:51:22 +00:00
|
|
|
lif_environment_free(&envp);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-08 21:28:36 +00:00
|
|
|
/* this function returns true if we can skip processing the interface for now,
|
|
|
|
* otherwise false.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
handle_refcounting(struct lif_dict *state, struct lif_interface *iface, bool up)
|
|
|
|
{
|
|
|
|
size_t orig_refcount = iface->refcount;
|
|
|
|
|
|
|
|
if (up)
|
|
|
|
lif_state_ref_if(state, iface->ifname, iface);
|
|
|
|
else
|
|
|
|
lif_state_unref_if(state, iface->ifname, iface);
|
|
|
|
|
2020-09-08 21:50:32 +00:00
|
|
|
#ifdef DEBUG_REFCOUNTING
|
|
|
|
fprintf(stderr, "handle_refcounting(): orig_refcount=%zu, refcount=%zu, direction=%s\n",
|
|
|
|
orig_refcount, iface->refcount, up ? "UP" : "DOWN");
|
|
|
|
#endif
|
|
|
|
|
2020-09-08 21:28:36 +00:00
|
|
|
/* if going up and orig_refcount > 0 -- we're already configured. */
|
|
|
|
if (up && orig_refcount > 0)
|
|
|
|
return true;
|
|
|
|
|
2020-09-08 21:50:32 +00:00
|
|
|
/* if going down and iface->refcount > 1 -- we still have other dependents. */
|
|
|
|
if (!up && iface->refcount > 1)
|
2020-09-08 21:28:36 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
/* we can change this interface -- no blocking dependents. */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-24 09:42:34 +00:00
|
|
|
static bool
|
|
|
|
handle_dependents(const struct lif_execute_opts *opts, struct lif_interface *parent, struct lif_dict *collection, struct lif_dict *state, bool up)
|
|
|
|
{
|
|
|
|
struct lif_dict_entry *requires = lif_dict_find(&parent->vars, "requires");
|
|
|
|
|
|
|
|
/* no dependents, nothing to worry about */
|
|
|
|
if (requires == NULL)
|
|
|
|
return true;
|
|
|
|
|
2020-07-24 09:51:31 +00:00
|
|
|
char require_ifs[4096] = {};
|
2020-07-24 09:42:34 +00:00
|
|
|
strlcpy(require_ifs, requires->data, sizeof require_ifs);
|
|
|
|
char *bufp = require_ifs;
|
|
|
|
|
2020-07-24 10:12:42 +00:00
|
|
|
for (char *tokenp = lif_next_token(&bufp); *tokenp; tokenp = lif_next_token(&bufp))
|
2020-07-24 09:42:34 +00:00
|
|
|
{
|
|
|
|
struct lif_interface *iface = lif_interface_collection_find(collection, tokenp);
|
|
|
|
|
2020-09-08 21:28:36 +00:00
|
|
|
/* if handle_refcounting returns true, it means we've already
|
|
|
|
* configured the interface, or it is too soon to deconfigure
|
|
|
|
* the interface.
|
|
|
|
*/
|
|
|
|
if (handle_refcounting(state, iface, up))
|
2020-08-24 16:36:17 +00:00
|
|
|
{
|
2020-08-24 17:33:56 +00:00
|
|
|
if (opts->verbose)
|
2020-09-08 21:28:36 +00:00
|
|
|
fprintf(stderr, "ifupdown: skipping dependent interface %s (of %s) -- %s\n",
|
|
|
|
iface->ifname, parent->ifname,
|
|
|
|
up ? "already configured" : "transient dependencies still exist");
|
|
|
|
|
2020-07-24 09:42:34 +00:00
|
|
|
continue;
|
2020-08-24 16:36:17 +00:00
|
|
|
}
|
2020-07-24 09:42:34 +00:00
|
|
|
|
2020-07-24 09:51:31 +00:00
|
|
|
if (opts->verbose)
|
|
|
|
fprintf(stderr, "ifupdown: changing state of dependent interface %s (of %s) to %s\n",
|
|
|
|
iface->ifname, parent->ifname, up ? "up" : "down");
|
|
|
|
|
2020-07-24 09:42:34 +00:00
|
|
|
if (!lif_lifecycle_run(opts, iface, collection, state, iface->ifname, up))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-23 14:51:22 +00:00
|
|
|
bool
|
2020-07-24 09:42:34 +00:00
|
|
|
lif_lifecycle_run(const struct lif_execute_opts *opts, struct lif_interface *iface, struct lif_dict *collection, struct lif_dict *state, const char *lifname, bool up)
|
2020-07-23 14:51:22 +00:00
|
|
|
{
|
|
|
|
if (lifname == NULL)
|
|
|
|
lifname = iface->ifname;
|
|
|
|
|
2020-07-28 19:46:25 +00:00
|
|
|
if (!lif_lifecycle_query_dependents(opts, iface, lifname))
|
|
|
|
return false;
|
|
|
|
|
2020-07-23 14:51:22 +00:00
|
|
|
if (up)
|
|
|
|
{
|
2020-07-24 09:42:34 +00:00
|
|
|
/* when going up, dependents go up first. */
|
|
|
|
if (!handle_dependents(opts, iface, collection, state, up))
|
|
|
|
return false;
|
|
|
|
|
2020-07-23 14:51:22 +00:00
|
|
|
/* XXX: we should try to recover (take the iface down) if bringing it up fails.
|
|
|
|
* but, right now neither debian ifupdown or busybox ifupdown do any recovery,
|
|
|
|
* so we wont right now.
|
|
|
|
*/
|
2020-09-09 20:36:07 +00:00
|
|
|
if (!lif_lifecycle_run_phase(opts, iface, "create", lifname, up))
|
|
|
|
return false;
|
|
|
|
|
2020-07-23 14:51:22 +00:00
|
|
|
if (!lif_lifecycle_run_phase(opts, iface, "pre-up", lifname, up))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!lif_lifecycle_run_phase(opts, iface, "up", lifname, up))
|
|
|
|
return false;
|
|
|
|
|
2020-07-23 16:43:14 +00:00
|
|
|
if (!lif_lifecycle_run_phase(opts, iface, "post-up", lifname, up))
|
|
|
|
return false;
|
|
|
|
|
2020-07-23 14:51:22 +00:00
|
|
|
lif_state_upsert(state, lifname, iface);
|
2020-07-24 09:42:34 +00:00
|
|
|
|
2020-09-08 21:28:36 +00:00
|
|
|
lif_state_ref_if(state, lifname, iface);
|
2020-07-23 14:51:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-23 16:43:14 +00:00
|
|
|
if (!lif_lifecycle_run_phase(opts, iface, "pre-down", lifname, up))
|
|
|
|
return false;
|
|
|
|
|
2020-07-23 14:51:22 +00:00
|
|
|
if (!lif_lifecycle_run_phase(opts, iface, "down", lifname, up))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!lif_lifecycle_run_phase(opts, iface, "post-down", lifname, up))
|
|
|
|
return false;
|
|
|
|
|
2020-09-09 20:36:07 +00:00
|
|
|
if (!lif_lifecycle_run_phase(opts, iface, "destroy", lifname, up))
|
|
|
|
return false;
|
|
|
|
|
2020-07-24 09:42:34 +00:00
|
|
|
/* when going up, dependents go down last. */
|
|
|
|
if (!handle_dependents(opts, iface, collection, state, up))
|
|
|
|
return false;
|
|
|
|
|
2020-09-08 21:28:36 +00:00
|
|
|
lif_state_unref_if(state, lifname, iface);
|
2020-07-23 14:51:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|