1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-04 13:51:24 +03:00

wait-online: support $REQUIRED_OPER_STATE_FOR_ONLINE= in state file

This also make wait-online optionally take operstate in -i option,
and adds '--operstate' option to wait-online which also takes operstate.

Also, this contains huge refactoring for wait-online.
This commit is contained in:
Yu Watanabe 2019-03-06 23:46:33 +09:00
parent 22eab27caa
commit f023184e6d
5 changed files with 223 additions and 77 deletions

View File

@ -10,6 +10,7 @@
int link_new(Manager *m, Link **ret, int ifindex, const char *ifname) {
_cleanup_(link_freep) Link *l = NULL;
_cleanup_free_ char *n = NULL;
int r;
assert(m);
@ -23,30 +24,33 @@ int link_new(Manager *m, Link **ret, int ifindex, const char *ifname) {
if (r < 0)
return r;
l = new0(Link, 1);
n = strdup(ifname);
if (!n)
return -ENOMEM;
l = new(Link, 1);
if (!l)
return -ENOMEM;
l->manager = m;
l->ifname = strdup(ifname);
if (!l->ifname)
return -ENOMEM;
*l = (Link) {
.manager = m,
.ifname = TAKE_PTR(n),
.ifindex = ifindex,
.required_operstate = LINK_OPERSTATE_DEGRADED,
};
r = hashmap_put(m->links_by_name, l->ifname, l);
if (r < 0)
return r;
l->ifindex = ifindex;
r = hashmap_put(m->links, INT_TO_PTR(ifindex), l);
if (r < 0)
return r;
if (ret)
*ret = l;
l = NULL;
TAKE_PTR(l);
return 0;
}
@ -60,6 +64,7 @@ Link *link_free(Link *l) {
hashmap_remove(l->manager->links_by_name, l->ifname);
}
free(l->state);
free(l->ifname);
return mfree(l);
}
@ -87,9 +92,8 @@ int link_update_rtnl(Link *l, sd_netlink_message *m) {
if (!new_ifname)
return -ENOMEM;
hashmap_remove(l->manager->links_by_name, l->ifname);
free(l->ifname);
l->ifname = new_ifname;
assert_se(hashmap_remove(l->manager->links_by_name, l->ifname) == l);
free_and_replace(l->ifname, new_ifname);
r = hashmap_put(l->manager->links_by_name, l->ifname, l);
if (r < 0)
@ -100,17 +104,55 @@ int link_update_rtnl(Link *l, sd_netlink_message *m) {
}
int link_update_monitor(Link *l) {
_cleanup_free_ char *operstate = NULL, *required_operstate = NULL, *state = NULL;
LinkOperationalState s;
int r, ret = 0;
assert(l);
assert(l->ifname);
l->required_for_online = sd_network_link_get_required_for_online(l->ifindex) != 0;
r = sd_network_link_get_required_for_online(l->ifindex);
if (r < 0)
ret = log_debug_errno(r,
"Failed to get whether link %s is required for online or not, "
"ignoring: %m", l->ifname);
else
l->required_for_online = r > 0;
l->operational_state = mfree(l->operational_state);
r = sd_network_link_get_required_operstate_for_online(l->ifindex, &required_operstate);
if (r < 0)
ret = log_debug_errno(r, "Failed to get required operational state for link %s to be online, "
"ignoring: %m", l->ifname);
else {
s = link_operstate_from_string(required_operstate);
if (s < 0)
ret = log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
"Failed to parse required operational state for link %s to be online, "
"ignoring: %m", l->ifname);
else
l->required_operstate = s;
}
sd_network_link_get_operational_state(l->ifindex, &l->operational_state);
r = sd_network_link_get_operational_state(l->ifindex, &operstate);
if (r < 0)
ret = log_debug_errno(r, "Failed to get operational state of link %s, ignoring: %m",
l->ifname);
else {
s = link_operstate_from_string(operstate);
if (s < 0)
ret = log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
"Failed to parse operational state of link %s, ignoring: %m",
l->ifname);
else
l->operational_state = s;
}
l->state = mfree(l->state);
r = sd_network_link_get_setup_state(l->ifindex, &state);
if (r < 0)
ret = log_debug_errno(r, "Failed to get setup state of link %s, ignoring: %m",
l->ifname);
else
free_and_replace(l->state, state);
sd_network_link_get_setup_state(l->ifindex, &l->state);
return 0;
return ret;
}

View File

@ -3,6 +3,8 @@
#include "sd-netlink.h"
#include "network-util.h"
typedef struct Link Link;
typedef struct Manager Manager;
@ -14,7 +16,8 @@ struct Link {
unsigned flags;
bool required_for_online;
char *operational_state;
LinkOperationalState required_operstate;
LinkOperationalState operational_state;
char *state;
};

View File

@ -13,7 +13,7 @@
#include "time-util.h"
#include "util.h"
bool manager_ignore_link(Manager *m, Link *link) {
static bool manager_ignore_link(Manager *m, Link *link) {
assert(m);
assert(link);
@ -22,7 +22,7 @@ bool manager_ignore_link(Manager *m, Link *link) {
return true;
/* if interfaces are given on the command line, ignore all others */
if (m->interfaces && !strv_contains(m->interfaces, link->ifname))
if (m->interfaces && !hashmap_contains(m->interfaces, link->ifname))
return true;
if (!link->required_for_online)
@ -32,45 +32,76 @@ bool manager_ignore_link(Manager *m, Link *link) {
return strv_fnmatch(m->ignore, link->ifname, 0);
}
bool manager_all_configured(Manager *m) {
Iterator i;
Link *l;
char **ifname;
bool one_ready = false;
static int manager_link_is_online(Manager *m, Link *l, LinkOperationalState s) {
/* This returns the following:
* -EAGAIN: not processed by udev or networkd
* 0: operstate is not enough
* 1: online */
/* wait for all the links given on the command line to appear */
STRV_FOREACH(ifname, m->interfaces) {
l = hashmap_get(m->links_by_name, *ifname);
if (!l) {
log_debug("still waiting for %s", *ifname);
return false;
}
if (!l->state) {
log_debug("link %s has not yet been processed by udev",
l->ifname);
return -EAGAIN;
}
if (STR_IN_SET(l->state, "configuring", "pending")) {
log_debug("link %s is being processed by networkd",
l->ifname);
return -EAGAIN;
}
if (s < 0)
s = m->required_operstate >= 0 ? m->required_operstate : l->required_operstate;
if (l->operational_state < s) {
log_debug("Operational state of link %s does not reach to %s",
l->ifname, link_operstate_to_string(s));
return 0;
}
return 1;
}
bool manager_all_configured(Manager *m) {
bool one_ready = false;
Iterator i;
const char *ifname;
void *p;
Link *l;
int r;
/* wait for all the links given on the command line to appear */
HASHMAP_FOREACH_KEY(p, ifname, m->interfaces, i) {
LinkOperationalState s = PTR_TO_INT(p);
l = hashmap_get(m->links_by_name, ifname);
if (!l) {
log_debug("still waiting for %s", ifname);
return false;
}
if (manager_link_is_online(m, l, s) <= 0)
return false;
}
if (!hashmap_isempty(m->interfaces))
/* all interfaces given by the command line are online. */
return true;
/* wait for all links networkd manages to be in admin state 'configured'
and at least one link to gain a carrier */
* and at least one link to gain a carrier */
HASHMAP_FOREACH(l, m->links, i) {
if (manager_ignore_link(m, l)) {
log_info("ignoring: %s", l->ifname);
continue;
}
if (!l->state) {
log_debug("link %s has not yet been processed by udev",
l->ifname);
r = manager_link_is_online(m, l, _LINK_OPERSTATE_INVALID);
if (r < 0)
return false;
}
if (STR_IN_SET(l->state, "configuring", "pending")) {
log_debug("link %s is being processed by networkd",
l->ifname);
return false;
}
if (l->operational_state &&
STR_IN_SET(l->operational_state, "degraded", "routable"))
if (r > 0)
/* we wait for at least one link to be ready,
regardless of who manages it */
* regardless of who manages it */
one_ready = true;
}
@ -120,15 +151,15 @@ static int manager_process_link(sd_netlink *rtnl, sd_netlink_message *mm, void *
r = link_new(m, &l, ifindex, ifname);
if (r < 0)
return log_error_errno(r, "Failed to create link object: %m");
r = link_update_monitor(l);
if (r < 0)
return log_error_errno(r, "Failed to initialize link object: %m");
}
r = link_update_rtnl(l, mm);
if (r < 0)
return log_warning_errno(r, "Failed to process RTNL link message: %m");;
log_warning_errno(r, "Failed to process RTNL link message, ignoring: %m");
r = link_update_monitor(l);
if (r < 0)
log_warning_errno(r, "Failed to initialize link object, ignoring: %m");
break;
@ -251,18 +282,22 @@ static int manager_network_monitor_listen(Manager *m) {
return 0;
}
int manager_new(Manager **ret, char **interfaces, char **ignore, usec_t timeout) {
int manager_new(Manager **ret, Hashmap *interfaces, char **ignore,
LinkOperationalState required_operstate, usec_t timeout) {
_cleanup_(manager_freep) Manager *m = NULL;
int r;
assert(ret);
m = new0(Manager, 1);
m = new(Manager, 1);
if (!m)
return -ENOMEM;
m->interfaces = interfaces;
m->ignore = ignore;
*m = (Manager) {
.interfaces = interfaces,
.ignore = ignore,
.required_operstate = required_operstate,
};
r = sd_event_default(&m->event);
if (r < 0)

View File

@ -6,6 +6,7 @@
#include "sd-network.h"
#include "hashmap.h"
#include "network-util.h"
typedef struct Manager Manager;
typedef struct Link Link;
@ -14,9 +15,12 @@ struct Manager {
Hashmap *links;
Hashmap *links_by_name;
char **interfaces;
/* Do not free the two members below. */
Hashmap *interfaces;
char **ignore;
LinkOperationalState required_operstate;
sd_netlink *rtnl;
sd_event_source *rtnl_event_source;
@ -27,9 +31,9 @@ struct Manager {
};
void manager_free(Manager *m);
int manager_new(Manager **ret, char **interfaces, char **ignore, usec_t timeout);
int manager_new(Manager **ret, Hashmap *interfaces, char **ignore,
LinkOperationalState required_operstate, usec_t timeout);
DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
bool manager_all_configured(Manager *m);
bool manager_ignore_link(Manager *m, Link *link);

View File

@ -9,14 +9,16 @@
#include "manager.h"
#include "pretty-print.h"
#include "signal-util.h"
#include "socket-util.h"
#include "strv.h"
static bool arg_quiet = false;
static usec_t arg_timeout = 120 * USEC_PER_SEC;
static char **arg_interfaces = NULL;
static Hashmap *arg_interfaces = NULL;
static char **arg_ignore = NULL;
static LinkOperationalState arg_required_operstate = _LINK_OPERSTATE_INVALID;
STATIC_DESTRUCTOR_REGISTER(arg_interfaces, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_interfaces, hashmap_free_free_keyp);
STATIC_DESTRUCTOR_REGISTER(arg_ignore, strv_freep);
static int help(void) {
@ -32,8 +34,11 @@ static int help(void) {
" -h --help Show this help\n"
" --version Print version string\n"
" -q --quiet Do not show status information\n"
" -i --interface=INTERFACE Block until at least these interfaces have appeared\n"
" -i --interface=INTERFACE[:OPERSTATE]\n"
" Block until at least these interfaces have appeared\n"
" --ignore=INTERFACE Don't take these interfaces into account\n"
" -o --operational-state=OPERSTATE\n"
" Required operational state\n"
" --timeout=SECS Maximum time to wait for network connectivity\n"
"\nSee the %s for details.\n"
, program_invocation_short_name
@ -43,6 +48,52 @@ static int help(void) {
return 0;
}
static int parse_interface_with_operstate(const char *str) {
_cleanup_free_ char *ifname = NULL;
LinkOperationalState s;
const char *p;
int r;
assert(str);
p = strchr(str, ':');
if (p) {
if (isempty(p + 1))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Operational state is empty.");
s = link_operstate_from_string(p + 1);
if (s < 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Invalid operational state '%s'", p + 1);
ifname = strndup(optarg, p - optarg);
} else {
s = _LINK_OPERSTATE_INVALID;
ifname = strdup(str);
}
if (!ifname)
return log_oom();
if (!ifname_valid(ifname))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Invalid interface name '%s'", ifname);
r = hashmap_ensure_allocated(&arg_interfaces, &string_hash_ops);
if (r < 0)
return log_oom();
r = hashmap_put(arg_interfaces, ifname, INT_TO_PTR(s));
if (r < 0)
return log_error_errno(r, "Failed to store interface name: %m");
if (r == 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Interface name %s is already specified", ifname);
TAKE_PTR(ifname);
return 0;
}
static int parse_argv(int argc, char *argv[]) {
enum {
@ -52,12 +103,13 @@ static int parse_argv(int argc, char *argv[]) {
};
static const struct option options[] = {
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, ARG_VERSION },
{ "quiet", no_argument, NULL, 'q' },
{ "interface", required_argument, NULL, 'i' },
{ "ignore", required_argument, NULL, ARG_IGNORE },
{ "timeout", required_argument, NULL, ARG_TIMEOUT },
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, ARG_VERSION },
{ "quiet", no_argument, NULL, 'q' },
{ "interface", required_argument, NULL, 'i' },
{ "ignore", required_argument, NULL, ARG_IGNORE },
{ "operational-state", required_argument, NULL, 'o' },
{ "timeout", required_argument, NULL, ARG_TIMEOUT },
{}
};
@ -66,7 +118,7 @@ static int parse_argv(int argc, char *argv[]) {
assert(argc >= 0);
assert(argv);
while ((c = getopt_long(argc, argv, "+hi:q", options, NULL)) >= 0)
while ((c = getopt_long(argc, argv, "hi:qo:", options, NULL)) >= 0)
switch (c) {
@ -82,9 +134,9 @@ static int parse_argv(int argc, char *argv[]) {
return version();
case 'i':
if (strv_extend(&arg_interfaces, optarg) < 0)
return log_oom();
r = parse_interface_with_operstate(optarg);
if (r < 0)
return r;
break;
case ARG_IGNORE:
@ -93,11 +145,21 @@ static int parse_argv(int argc, char *argv[]) {
break;
case 'o': {
LinkOperationalState s;
s = link_operstate_from_string(optarg);
if (s < 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Invalid operational state '%s'", optarg);
arg_required_operstate = s;
break;
}
case ARG_TIMEOUT:
r = parse_sec(optarg, &arg_timeout);
if (r < 0)
return r;
break;
case '?':
@ -124,11 +186,11 @@ static int run(int argc, char *argv[]) {
return r;
if (arg_quiet)
log_set_max_level(LOG_WARNING);
log_set_max_level(LOG_ERR);
assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
r = manager_new(&m, arg_interfaces, arg_ignore, arg_timeout);
r = manager_new(&m, arg_interfaces, arg_ignore, arg_required_operstate, arg_timeout);
if (r < 0)
return log_error_errno(r, "Could not create manager: %m");