1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-22 13:33:56 +03:00

network: introduce FORMAT_LIFETIME()

Fixes a bug introduced by 5291f26d4a.

Fixes #20227.
This commit is contained in:
Yu Watanabe 2021-07-15 18:48:56 +09:00
parent 6c39b39aa8
commit 0fd97a2533
3 changed files with 35 additions and 29 deletions

View File

@ -16,6 +16,7 @@
#include "parse-util.h"
#include "string-util.h"
#include "strv.h"
#include "strxcpyx.h"
#define ADDRESSES_PER_LINK_MAX 2048U
#define STATIC_ADDRESSES_PER_NETWORK_MAX 1024U
@ -620,9 +621,23 @@ int manager_has_address(Manager *manager, int family, const union in_addr_union
return false;
}
char *format_lifetime(char *buf, size_t l, uint32_t lifetime) {
char *p = buf;
assert(buf);
assert(l > 0);
if (lifetime == CACHE_INFO_INFINITY_LIFE_TIME) {
strscpy(buf, l, "forever");
return buf;
}
l -= strpcpy(&p, l, "for ");
return format_timespan(p, l, lifetime * USEC_PER_SEC, USEC_PER_SEC);
}
static void log_address_debug(const Address *address, const char *str, const Link *link) {
_cleanup_free_ char *addr = NULL, *peer = NULL, *flags_str = NULL;
const char *valid_str = NULL, *preferred_str = NULL;
bool has_peer;
assert(address);
@ -637,19 +652,13 @@ static void log_address_debug(const Address *address, const char *str, const Lin
if (has_peer)
(void) in_addr_to_string(address->family, &address->in_addr_peer, &peer);
if (address->cinfo.ifa_valid != CACHE_INFO_INFINITY_LIFE_TIME)
valid_str = FORMAT_TIMESPAN(address->cinfo.ifa_valid * USEC_PER_SEC, USEC_PER_SEC);
if (address->cinfo.ifa_prefered != CACHE_INFO_INFINITY_LIFE_TIME)
preferred_str = FORMAT_TIMESPAN(address->cinfo.ifa_prefered * USEC_PER_SEC, USEC_PER_SEC);
(void) address_flags_to_string_alloc(address->flags, address->family, &flags_str);
log_link_debug(link, "%s address: %s%s%s/%u (valid %s%s, preferred %s%s), flags: %s",
log_link_debug(link, "%s address: %s%s%s/%u (valid %s, preferred %s), flags: %s",
str, strnull(addr), has_peer ? " peer " : "",
has_peer ? strnull(peer) : "", address->prefixlen,
valid_str ? "for " : "forever", strempty(valid_str),
preferred_str ? "for " : "forever", strempty(preferred_str),
FORMAT_LIFETIME(address->cinfo.ifa_valid),
FORMAT_LIFETIME(address->cinfo.ifa_prefered),
strna(flags_str));
}

View File

@ -50,6 +50,13 @@ typedef struct Address {
address_ready_callback_t callback;
} Address;
char *format_lifetime(char *buf, size_t l, uint32_t lifetime) _warn_unused_result_;
/* Note: the lifetime of the compound literal is the immediately surrounding block,
* see C11 §6.5.2.5, and
* https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks */
#define FORMAT_LIFETIME(lifetime) \
format_lifetime((char[FORMAT_TIMESPAN_MAX+STRLEN("for ")]){}, FORMAT_TIMESPAN_MAX+STRLEN("for "), lifetime)
int address_new(Address **ret);
Address *address_free(Address *address);
int address_get(Link *link, const Address *in, Address **ret);

View File

@ -373,7 +373,6 @@ static int dhcp6_pd_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Lin
}
static void log_dhcp6_pd_address(Link *link, const Address *address) {
const char *valid_str = NULL, *preferred_str = NULL;
_cleanup_free_ char *buffer = NULL;
int log_level;
@ -386,15 +385,11 @@ static void log_dhcp6_pd_address(Link *link, const Address *address) {
return;
(void) in6_addr_prefix_to_string(&address->in_addr.in6, address->prefixlen, &buffer);
if (address->cinfo.ifa_valid != CACHE_INFO_INFINITY_LIFE_TIME)
valid_str = FORMAT_TIMESPAN(address->cinfo.ifa_valid * USEC_PER_SEC, USEC_PER_SEC);
if (address->cinfo.ifa_prefered != CACHE_INFO_INFINITY_LIFE_TIME)
preferred_str = FORMAT_TIMESPAN(address->cinfo.ifa_prefered * USEC_PER_SEC, USEC_PER_SEC);
log_link_full(link, log_level, "DHCPv6-PD address %s (valid %s%s, preferred %s%s)",
log_link_full(link, log_level, "DHCPv6-PD address %s (valid %s, preferred %s)",
strna(buffer),
valid_str ? "for " : "forever", strempty(valid_str),
preferred_str ? "for " : "forever", strempty(preferred_str));
FORMAT_LIFETIME(address->cinfo.ifa_valid),
FORMAT_LIFETIME(address->cinfo.ifa_prefered));
}
static int dhcp6_pd_after_address_configure(Request *req, void *object) {
@ -1068,11 +1063,6 @@ static void log_dhcp6_address(Link *link, const Address *address, char **ret) {
(void) in6_addr_prefix_to_string(&address->in_addr.in6, address->prefixlen, &buffer);
const char *valid_str = address->cinfo.ifa_valid == CACHE_INFO_INFINITY_LIFE_TIME ? NULL :
FORMAT_TIMESPAN(address->cinfo.ifa_valid * USEC_PER_SEC, USEC_PER_SEC);
const char *preferred_str = address->cinfo.ifa_prefered == CACHE_INFO_INFINITY_LIFE_TIME ? NULL :
FORMAT_TIMESPAN(address->cinfo.ifa_prefered * USEC_PER_SEC, USEC_PER_SEC);
r = address_get(link, address, &existing);
if (r < 0) {
/* New address. */
@ -1095,20 +1085,20 @@ static void log_dhcp6_address(Link *link, const Address *address, char **ret) {
break;
}
log_link_warning(link, "DHCPv6 address %s (valid %s%s, preferred %s%s) conflicts the existing address %s %s.",
log_link_warning(link, "DHCPv6 address %s (valid %s, preferred %s) conflicts the existing address %s %s.",
strna(buffer),
valid_str ? "for " : "forever", strempty(valid_str),
preferred_str ? "for " : "forever", strempty(preferred_str),
FORMAT_LIFETIME(address->cinfo.ifa_valid),
FORMAT_LIFETIME(address->cinfo.ifa_prefered),
strna(buffer),
by_ndisc ? "assigned by NDISC. Please try to use or update IPv6Token= setting "
"to change the address generated by NDISC, or disable UseAutonomousPrefix=" : "");
goto finalize;
simple_log:
log_link_full(link, log_level, "DHCPv6 address %s (valid %s%s, preferred %s%s)",
log_link_full(link, log_level, "DHCPv6 address %s (valid %s, preferred %s)",
strna(buffer),
valid_str ? "for " : "forever", strempty(valid_str),
preferred_str ? "for " : "forever", strempty(preferred_str));
FORMAT_LIFETIME(address->cinfo.ifa_valid),
FORMAT_LIFETIME(address->cinfo.ifa_prefered));
finalize:
if (ret)