1
0
mirror of https://github.com/systemd/systemd synced 2026-03-29 11:14:50 +02:00

Compare commits

..

13 Commits

Author SHA1 Message Date
Lennart Poettering
41a978fdb1
Merge pull request #20676 from gogsbread/sysctl-minimize-sideeffect
sysctl: minimize side effects when running `systemd-sysctl`
2021-09-29 09:17:48 +02:00
Antony Deepak Thomas
ab14aa23ae sysctl-util: minimize side-effects when running systemd-sysctl
Currently `systemd-sysctl` binary is used in `systemd-sysctl.service`
which is mostly configured as `oneshot`. There are situations where one
would like to use systemd to maintain Sysctl configurations on a host,
using a configuration managers such as Chef or Puppet, by apply
configurations every X duration.
The problem with using `systemd-sysctl` is that it writes all the Sysctl
settings, even if the values for those settings have not changed. From
experience, we have observed that some Sysctl settings cause actions in
the kernel upon writing(like dropping caches) which in turn cause
undesired side effects.
This patch tries to minimize such side effects by comparing values
before writing.
2021-09-29 13:07:47 +09:00
Antony Deepak Thomas
e565cfd2eb fileio: introduce new mode to suppress writing the same value 2021-09-29 13:06:25 +09:00
Antony Deepak Thomas
8034b42ca6 string-util: introduce streq_skip_trailing_chars() 2021-09-29 12:57:30 +09:00
Antony Deepak Thomas
46a0f5cac8 fileio: introduce read_virtual_file_fd() 2021-09-29 12:47:49 +09:00
Yu Watanabe
83455d0c8b
Merge pull request #20865 from keszybz/meson-net-naming-definitions
Allow defining new naming scheme entries as configuration time
2021-09-29 12:29:14 +09:00
Frantisek Sumsal
ecea250d77 core: fix the return type for xxx_running_timeout() functions
otherwise we might return an invalid value, since `usec_t` is 64-bit,
whereas `int` might not be.

Follow-up to: 5918a93
Fixes: #20872
2021-09-29 12:28:21 +09:00
Yu Watanabe
17373589f3
Merge pull request #20860 from yuwata/libsystemd-network-get-ifname-negative-errno
libsystemd-network: make sd_dhcp_client_get_ifname() or friends return negative errno
2021-09-29 12:27:01 +09:00
Yu Watanabe
5977b71f28 libsystemd-network: make sd_dhcp_client_get_ifname() or friends return negative errno on error 2021-09-29 03:37:09 +09:00
Yu Watanabe
01afd0f7f5 tree-wide: make format_ifname() or friends return negative errno on failure
Also,
- drop unnecessary +1 from buffer size, as IF_NAMESIZE or IFNAMSIZ
  includes the nul at the end.
- format_ifname() does not update buffer on failure,
- introduces format_ifname_alloc(), FORMAT_IFNAME(), and their friends.
2021-09-29 03:37:06 +09:00
Zbigniew Jędrzejewski-Szmek
681cb84a63 meson: allow extra net naming schemes to be defined during configuration
In upstream, we have a linearly-growing list of net-naming-scheme defines;
we add a new one for every release where we make user-visible changes to the
naming scheme.

But the general idea was that downstream distributions could define their
own combinations (or even just their own names for existing combinations),
so provide stability for their users. So far this required patching of the
netif-naming-scheme.c and .h files to add the new lines.

With this patch, patching is not required:

$ meson configure build \
  -Dextra-net-naming-schemes=gargoyle=v238+npar_ari+allow_rerenames,gargoyle2=gargoyle+nspawn_long_hash \
  -Ddefault-net-naming-scheme=gargoyle2

or even

$ meson configure build \
  -Dextra-net-naming-schemes=gargoyle=v238+npar_ari+allow_rerenames,gargoyle2=gargoyle+nspawn_long_hash,latest=v249 \
  -Ddefault-net-naming-scheme=gargoyle2

The syntax is a comma-separated list of NAME=name+name+…
This syntax is a bit scary, but any typos result in compilation errors,
so I think it should be OK in practice.

With this approach, we don't allow users to define arbitrary combinations:
what is allowed is still defined at compilation time, so it's up to the
distribution maintainers to provide reasonable combinations. In this regard,
the only difference from status quo is that it's much easier to do (and harder
to do incorrectly, for example by forgetting to add a name to one of the
maps).
2021-09-28 14:22:40 +02:00
Zbigniew Jędrzejewski-Szmek
77faadfdd3 meson: drop the list of valid net naming schemes
We used 'combo' type for the scheme list. For a while we forgot to add
new names, and recently aa0a23ec86 added v241, v243, v245, and v247.
I want to allow defining new values during configuration, which means
that we can't use meson to verify the list of options. So any value is
allowed, but then two tests are added: one that will fail compilation if some
invalid name is given (other than "latest"), and one that converts
DEFAULT_NET_NAMING_SCHEME to a NamingScheme pointer.
2021-09-28 14:22:37 +02:00
Zbigniew Jędrzejewski-Szmek
acaa636866 netif-naming: inline one iterator variable 2021-09-28 12:26:09 +02:00
53 changed files with 448 additions and 224 deletions

View File

@ -707,8 +707,30 @@ else
conf.set('DEFAULT_HIERARCHY', 'CGROUP_UNIFIED_ALL') conf.set('DEFAULT_HIERARCHY', 'CGROUP_UNIFIED_ALL')
endif endif
extra_net_naming_schemes = []
extra_net_naming_map = []
foreach scheme: get_option('extra-net-naming-schemes').split(',')
if scheme != ''
name = scheme.split('=')[0]
value = scheme.split('=')[1]
NAME = name.underscorify().to_upper()
VALUE = []
foreach field: value.split('+')
VALUE += 'NAMING_' + field.underscorify().to_upper()
endforeach
extra_net_naming_schemes += 'NAMING_@0@ = @1@,'.format(NAME, '|'.join(VALUE))
extra_net_naming_map += '{ "@0@", NAMING_@1@ },'.format(name, NAME)
endif
endforeach
conf.set('EXTRA_NET_NAMING_SCHEMES', ' '.join(extra_net_naming_schemes))
conf.set('EXTRA_NET_NAMING_MAP', ' '.join(extra_net_naming_map))
default_net_naming_scheme = get_option('default-net-naming-scheme') default_net_naming_scheme = get_option('default-net-naming-scheme')
conf.set_quoted('DEFAULT_NET_NAMING_SCHEME', default_net_naming_scheme) conf.set_quoted('DEFAULT_NET_NAMING_SCHEME', default_net_naming_scheme)
if default_net_naming_scheme != 'latest'
conf.set('_DEFAULT_NET_NAMING_SCHEME_TEST',
'NAMING_' + default_net_naming_scheme.underscorify().to_upper())
endif
time_epoch = get_option('time-epoch') time_epoch = get_option('time-epoch')
if time_epoch == -1 if time_epoch == -1

View File

@ -200,8 +200,9 @@ option('fallback-hostname', type : 'string', value : 'localhost',
option('default-hierarchy', type : 'combo', option('default-hierarchy', type : 'combo',
choices : ['legacy', 'hybrid', 'unified'], value : 'unified', choices : ['legacy', 'hybrid', 'unified'], value : 'unified',
description : 'default cgroup hierarchy') description : 'default cgroup hierarchy')
option('default-net-naming-scheme', type : 'combo', option('extra-net-naming-schemes', type : 'string',
choices : ['latest', 'v238', 'v239', 'v240', 'v241', 'v243', 'v245', 'v247', 'v249'], description : 'comma-separated list of extra net.naming-scheme= definitions')
option('default-net-naming-scheme', type : 'string', value : 'latest',
description : 'default net.naming-scheme= value') description : 'default net.naming-scheme= value')
option('status-unit-format-default', type : 'combo', option('status-unit-format-default', type : 'combo',
choices : ['description', 'name', 'combined'], choices : ['description', 'name', 'combined'],

View File

@ -148,6 +148,30 @@ int write_string_stream_ts(
return -EBADF; return -EBADF;
} }
if (flags & WRITE_STRING_FILE_SUPPRESS_REDUNDANT_VIRTUAL) {
_cleanup_free_ char *t = NULL;
/* If value to be written is same as that of the existing value, then suppress the write. */
if (fd < 0) {
fd = fileno(f);
if (fd < 0)
return -EBADF;
}
/* Read an additional byte to detect cases where the prefix matches but the rest
* doesn't. Also, 0 returned by read_virtual_file_fd() means the read was truncated and
* it won't be equal to the new value. */
if (read_virtual_file_fd(fd, strlen(line)+1, &t, NULL) > 0 &&
streq_skip_trailing_chars(line, t, NEWLINE)) {
log_debug("No change in value '%s', supressing write", line);
return 0;
}
if (lseek(fd, 0, SEEK_SET) < 0)
return -errno;
}
needs_nl = !(flags & WRITE_STRING_FILE_AVOID_NEWLINE) && !endswith(line, "\n"); needs_nl = !(flags & WRITE_STRING_FILE_AVOID_NEWLINE) && !endswith(line, "\n");
if (needs_nl && (flags & WRITE_STRING_FILE_DISABLE_BUFFER)) { if (needs_nl && (flags & WRITE_STRING_FILE_DISABLE_BUFFER)) {
@ -263,10 +287,11 @@ int write_string_file_ts(
assert(!ts); assert(!ts);
/* We manually build our own version of fopen(..., "we") that works without O_CREAT and with O_NOFOLLOW if needed. */ /* We manually build our own version of fopen(..., "we") that works without O_CREAT and with O_NOFOLLOW if needed. */
fd = open(fn, O_WRONLY|O_CLOEXEC|O_NOCTTY | fd = open(fn, O_CLOEXEC|O_NOCTTY |
(FLAGS_SET(flags, WRITE_STRING_FILE_NOFOLLOW) ? O_NOFOLLOW : 0) | (FLAGS_SET(flags, WRITE_STRING_FILE_NOFOLLOW) ? O_NOFOLLOW : 0) |
(FLAGS_SET(flags, WRITE_STRING_FILE_CREATE) ? O_CREAT : 0) | (FLAGS_SET(flags, WRITE_STRING_FILE_CREATE) ? O_CREAT : 0) |
(FLAGS_SET(flags, WRITE_STRING_FILE_TRUNCATE) ? O_TRUNC : 0), (FLAGS_SET(flags, WRITE_STRING_FILE_TRUNCATE) ? O_TRUNC : 0) |
(FLAGS_SET(flags, WRITE_STRING_FILE_SUPPRESS_REDUNDANT_VIRTUAL) ? O_RDWR : O_WRONLY),
(FLAGS_SET(flags, WRITE_STRING_FILE_MODE_0600) ? 0600 : 0666)); (FLAGS_SET(flags, WRITE_STRING_FILE_MODE_0600) ? 0600 : 0666));
if (fd < 0) { if (fd < 0) {
r = -errno; r = -errno;
@ -375,9 +400,8 @@ int verify_file(const char *fn, const char *blob, bool accept_extra_nl) {
return 1; return 1;
} }
int read_virtual_file(const char *filename, size_t max_size, char **ret_contents, size_t *ret_size) { int read_virtual_file_fd(int fd, size_t max_size, char **ret_contents, size_t *ret_size) {
_cleanup_free_ char *buf = NULL; _cleanup_free_ char *buf = NULL;
_cleanup_close_ int fd = -1;
size_t n, size; size_t n, size;
int n_retries; int n_retries;
bool truncated = false; bool truncated = false;
@ -395,10 +419,7 @@ int read_virtual_file(const char *filename, size_t max_size, char **ret_contents
* contents* may be returned. (Though the read is still done using one syscall.) Returns 0 on * contents* may be returned. (Though the read is still done using one syscall.) Returns 0 on
* partial success, 1 if untruncated contents were read. */ * partial success, 1 if untruncated contents were read. */
fd = open(filename, O_RDONLY|O_NOCTTY|O_CLOEXEC); assert(fd >= 0);
if (fd < 0)
return -errno;
assert(max_size <= READ_VIRTUAL_BYTES_MAX || max_size == SIZE_MAX); assert(max_size <= READ_VIRTUAL_BYTES_MAX || max_size == SIZE_MAX);
/* Limit the number of attempts to read the number of bytes returned by fstat(). */ /* Limit the number of attempts to read the number of bytes returned by fstat(). */
@ -434,8 +455,8 @@ int read_virtual_file(const char *filename, size_t max_size, char **ret_contents
n_retries--; n_retries--;
} else if (n_retries > 1) { } else if (n_retries > 1) {
/* Files in /proc are generally smaller than the page size so let's start with a page size /* Files in /proc are generally smaller than the page size so let's start with
* buffer from malloc and only use the max buffer on the final try. */ * a page size buffer from malloc and only use the max buffer on the final try. */
size = MIN3(page_size() - 1, READ_VIRTUAL_BYTES_MAX, max_size); size = MIN3(page_size() - 1, READ_VIRTUAL_BYTES_MAX, max_size);
n_retries = 1; n_retries = 1;
} else { } else {
@ -524,6 +545,18 @@ int read_virtual_file(const char *filename, size_t max_size, char **ret_contents
return !truncated; return !truncated;
} }
int read_virtual_file(const char *filename, size_t max_size, char **ret_contents, size_t *ret_size) {
_cleanup_close_ int fd = -1;
assert(filename);
fd = open(filename, O_RDONLY | O_NOCTTY | O_CLOEXEC);
if (fd < 0)
return -errno;
return read_virtual_file_fd(fd, max_size, ret_contents, ret_size);
}
int read_full_stream_full( int read_full_stream_full(
FILE *f, FILE *f,
const char *filename, const char *filename,

View File

@ -26,6 +26,7 @@ typedef enum {
WRITE_STRING_FILE_NOFOLLOW = 1 << 8, WRITE_STRING_FILE_NOFOLLOW = 1 << 8,
WRITE_STRING_FILE_MKDIR_0755 = 1 << 9, WRITE_STRING_FILE_MKDIR_0755 = 1 << 9,
WRITE_STRING_FILE_MODE_0600 = 1 << 10, WRITE_STRING_FILE_MODE_0600 = 1 << 10,
WRITE_STRING_FILE_SUPPRESS_REDUNDANT_VIRTUAL = 1 << 11,
/* And before you wonder, why write_string_file_atomic_label_ts() is a separate function instead of just one /* And before you wonder, why write_string_file_atomic_label_ts() is a separate function instead of just one
more flag here: it's about linking: we don't want to pull -lselinux into all users of write_string_file() more flag here: it's about linking: we don't want to pull -lselinux into all users of write_string_file()
@ -67,6 +68,7 @@ static inline int read_full_file(const char *filename, char **ret_contents, size
return read_full_file_full(AT_FDCWD, filename, UINT64_MAX, SIZE_MAX, 0, NULL, ret_contents, ret_size); return read_full_file_full(AT_FDCWD, filename, UINT64_MAX, SIZE_MAX, 0, NULL, ret_contents, ret_size);
} }
int read_virtual_file_fd(int fd, size_t max_size, char **ret_contents, size_t *ret_size);
int read_virtual_file(const char *filename, size_t max_size, char **ret_contents, size_t *ret_size); int read_virtual_file(const char *filename, size_t max_size, char **ret_contents, size_t *ret_size);
static inline int read_full_virtual_file(const char *filename, char **ret_contents, size_t *ret_size) { static inline int read_full_virtual_file(const char *filename, char **ret_contents, size_t *ret_size) {
return read_virtual_file(filename, SIZE_MAX, ret_contents, ret_size); return read_virtual_file(filename, SIZE_MAX, ret_contents, ret_size);

View File

@ -3,23 +3,43 @@
#include "format-util.h" #include "format-util.h"
#include "memory-util.h" #include "memory-util.h"
#include "stdio-util.h" #include "stdio-util.h"
#include "strxcpyx.h"
assert_cc(STRLEN("%") + DECIMAL_STR_MAX(int) <= IF_NAMESIZE);
int format_ifname_full(int ifindex, FormatIfnameFlag flag, char buf[static IF_NAMESIZE]) {
if (ifindex <= 0)
return -EINVAL;
assert_cc(DECIMAL_STR_MAX(int) + 1 <= IF_NAMESIZE + 1);
char *format_ifname_full(int ifindex, char buf[static IF_NAMESIZE + 1], FormatIfnameFlag flag) {
/* Buffer is always cleared */
memzero(buf, IF_NAMESIZE + 1);
if (if_indextoname(ifindex, buf)) if (if_indextoname(ifindex, buf))
return buf; return 0;
if (!FLAGS_SET(flag, FORMAT_IFNAME_IFINDEX)) if (!FLAGS_SET(flag, FORMAT_IFNAME_IFINDEX))
return NULL; return -errno;
if (FLAGS_SET(flag, FORMAT_IFNAME_IFINDEX_WITH_PERCENT)) if (FLAGS_SET(flag, FORMAT_IFNAME_IFINDEX_WITH_PERCENT))
assert(snprintf_ok(buf, IF_NAMESIZE + 1, "%%%d", ifindex)); assert(snprintf_ok(buf, IF_NAMESIZE, "%%%d", ifindex));
else else
assert(snprintf_ok(buf, IF_NAMESIZE + 1, "%d", ifindex)); assert(snprintf_ok(buf, IF_NAMESIZE, "%d", ifindex));
return buf; return 0;
}
int format_ifname_full_alloc(int ifindex, FormatIfnameFlag flag, char **ret) {
char buf[IF_NAMESIZE], *copy;
int r;
assert(ret);
r = format_ifname_full(ifindex, flag, buf);
if (r < 0)
return r;
copy = strdup(buf);
if (!copy)
return -ENOMEM;
*ret = copy;
return 0;
} }
char *format_bytes_full(char *buf, size_t l, uint64_t t, FormatBytesFlag flag) { char *format_bytes_full(char *buf, size_t l, uint64_t t, FormatBytesFlag flag) {

View File

@ -61,10 +61,23 @@ typedef enum {
FORMAT_IFNAME_IFINDEX_WITH_PERCENT = (1 << 1) | FORMAT_IFNAME_IFINDEX, FORMAT_IFNAME_IFINDEX_WITH_PERCENT = (1 << 1) | FORMAT_IFNAME_IFINDEX,
} FormatIfnameFlag; } FormatIfnameFlag;
char *format_ifname_full(int ifindex, char buf[static IF_NAMESIZE + 1], FormatIfnameFlag flag); int format_ifname_full(int ifindex, FormatIfnameFlag flag, char buf[static IF_NAMESIZE]);
static inline char *format_ifname(int ifindex, char buf[static IF_NAMESIZE + 1]) { int format_ifname_full_alloc(int ifindex, FormatIfnameFlag flag, char **ret);
return format_ifname_full(ifindex, buf, 0);
static inline int format_ifname(int ifindex, char buf[static IF_NAMESIZE]) {
return format_ifname_full(ifindex, 0, buf);
} }
static inline int format_ifname_alloc(int ifindex, char **ret) {
return format_ifname_full_alloc(ifindex, 0, ret);
}
static inline char *_format_ifname_full(int ifindex, FormatIfnameFlag flag, char buf[static IF_NAMESIZE]) {
(void) format_ifname_full(ifindex, flag, buf);
return buf;
}
#define FORMAT_IFNAME_FULL(index, flag) _format_ifname_full(index, flag, (char[IF_NAMESIZE]){})
#define FORMAT_IFNAME(index) _format_ifname_full(index, 0, (char[IF_NAMESIZE]){})
typedef enum { typedef enum {
FORMAT_BYTES_USE_IEC = 1 << 0, FORMAT_BYTES_USE_IEC = 1 << 0,

View File

@ -455,23 +455,23 @@ int sockaddr_pretty(
if (r < 0) if (r < 0)
return -ENOMEM; return -ENOMEM;
} else { } else {
char a[INET6_ADDRSTRLEN], ifname[IF_NAMESIZE + 1]; char a[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, &sa->in6.sin6_addr, a, sizeof(a)); inet_ntop(AF_INET6, &sa->in6.sin6_addr, a, sizeof(a));
if (sa->in6.sin6_scope_id != 0)
format_ifname_full(sa->in6.sin6_scope_id, ifname, FORMAT_IFNAME_IFINDEX);
if (include_port) { if (include_port) {
r = asprintf(&p, if (asprintf(&p,
"[%s]:%u%s%s", "[%s]:%u%s%s",
a, a,
be16toh(sa->in6.sin6_port), be16toh(sa->in6.sin6_port),
sa->in6.sin6_scope_id != 0 ? "%" : "", sa->in6.sin6_scope_id != 0 ? "%" : "",
sa->in6.sin6_scope_id != 0 ? ifname : ""); FORMAT_IFNAME_FULL(sa->in6.sin6_scope_id, FORMAT_IFNAME_IFINDEX)) < 0)
if (r < 0)
return -ENOMEM; return -ENOMEM;
} else { } else {
p = sa->in6.sin6_scope_id != 0 ? strjoin(a, "%", ifname) : strdup(a); if (sa->in6.sin6_scope_id != 0)
p = strjoin(a, "%", FORMAT_IFNAME_FULL(sa->in6.sin6_scope_id, FORMAT_IFNAME_IFINDEX));
else
p = strdup(a);
if (!p) if (!p)
return -ENOMEM; return -ENOMEM;
} }
@ -1233,7 +1233,7 @@ int socket_bind_to_ifname(int fd, const char *ifname) {
} }
int socket_bind_to_ifindex(int fd, int ifindex) { int socket_bind_to_ifindex(int fd, int ifindex) {
char ifname[IF_NAMESIZE + 1]; char ifname[IF_NAMESIZE];
int r; int r;
assert(fd >= 0); assert(fd >= 0);
@ -1251,8 +1251,9 @@ int socket_bind_to_ifindex(int fd, int ifindex) {
return r; return r;
/* Fall back to SO_BINDTODEVICE on kernels < 5.0 which didn't have SO_BINDTOIFINDEX */ /* Fall back to SO_BINDTODEVICE on kernels < 5.0 which didn't have SO_BINDTOIFINDEX */
if (!format_ifname(ifindex, ifname)) r = format_ifname(ifindex, ifname);
return -errno; if (r < 0)
return r;
return socket_bind_to_ifname(fd, ifname); return socket_bind_to_ifname(fd, ifname);
} }

View File

@ -1146,3 +1146,19 @@ int string_contains_word_strv(const char *string, const char *separators, char *
*ret_word = found; *ret_word = found;
return !!found; return !!found;
} }
bool streq_skip_trailing_chars(const char *s1, const char *s2, const char *ok) {
if (!s1 && !s2)
return true;
if (!s1 || !s2)
return false;
if (!ok)
ok = WHITESPACE;
for (; *s1 && *s2; s1++, s2++)
if (*s1 != *s2)
break;
return in_charset(s1, ok) && in_charset(s2, ok);
}

View File

@ -226,3 +226,5 @@ int string_contains_word_strv(const char *string, const char *separators, char *
static inline int string_contains_word(const char *string, const char *separators, const char *word) { static inline int string_contains_word(const char *string, const char *separators, const char *word) {
return string_contains_word_strv(string, separators, STRV_MAKE(word), NULL); return string_contains_word_strv(string, separators, STRV_MAKE(word), NULL);
} }
bool streq_skip_trailing_chars(const char *s1, const char *s2, const char *ok);

View File

@ -58,7 +58,7 @@ int sysctl_write(const char *property, const char *value) {
log_debug("Setting '%s' to '%s'", p, value); log_debug("Setting '%s' to '%s'", p, value);
return write_string_file(p, value, WRITE_STRING_FILE_VERIFY_ON_FAILURE | WRITE_STRING_FILE_DISABLE_BUFFER); return write_string_file(p, value, WRITE_STRING_FILE_VERIFY_ON_FAILURE | WRITE_STRING_FILE_DISABLE_BUFFER | WRITE_STRING_FILE_SUPPRESS_REDUNDANT_VIRTUAL);
} }
int sysctl_writef(const char *property, const char *format, ...) { int sysctl_writef(const char *property, const char *format, ...) {

View File

@ -52,7 +52,7 @@ static void scope_done(Unit *u) {
s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source);
} }
static int scope_running_timeout(Scope *s) { static usec_t scope_running_timeout(Scope *s) {
usec_t delta = 0; usec_t delta = 0;
assert(s); assert(s);

View File

@ -515,7 +515,7 @@ static void service_remove_fd_store(Service *s, const char *name) {
} }
} }
static int service_running_timeout(Service *s) { static usec_t service_running_timeout(Service *s) {
usec_t delta = 0; usec_t delta = 0;
assert(s); assert(s);

View File

@ -12,7 +12,7 @@
#include "sd-dhcp-client.h" #include "sd-dhcp-client.h"
#include "dhcp-protocol.h" #include "dhcp-protocol.h"
#include "log-link.h" #include "network-common.h"
#include "socket-util.h" #include "socket-util.h"
typedef struct sd_dhcp_option { typedef struct sd_dhcp_option {
@ -75,10 +75,10 @@ void dhcp_client_set_test_mode(sd_dhcp_client *client, bool test_mode);
#define log_dhcp_client_errno(client, error, fmt, ...) \ #define log_dhcp_client_errno(client, error, fmt, ...) \
log_interface_prefix_full_errno( \ log_interface_prefix_full_errno( \
"DHCPv4 client: ", \ "DHCPv4 client: ", \
sd_dhcp_client_get_ifname(client), \ sd_dhcp_client, client, \
error, fmt, ##__VA_ARGS__) error, fmt, ##__VA_ARGS__)
#define log_dhcp_client(client, fmt, ...) \ #define log_dhcp_client(client, fmt, ...) \
log_interface_prefix_full_errno_zerook( \ log_interface_prefix_full_errno_zerook( \
"DHCPv4 client: ", \ "DHCPv4 client: ", \
sd_dhcp_client_get_ifname(client), \ sd_dhcp_client, client, \
0, fmt, ##__VA_ARGS__) 0, fmt, ##__VA_ARGS__)

View File

@ -9,8 +9,8 @@
#include "sd-event.h" #include "sd-event.h"
#include "dhcp-internal.h" #include "dhcp-internal.h"
#include "network-common.h"
#include "ordered-set.h" #include "ordered-set.h"
#include "log-link.h"
#include "time-util.h" #include "time-util.h"
typedef enum DHCPRawOption { typedef enum DHCPRawOption {
@ -112,10 +112,10 @@ int client_id_compare_func(const DHCPClientId *a, const DHCPClientId *b);
#define log_dhcp_server_errno(server, error, fmt, ...) \ #define log_dhcp_server_errno(server, error, fmt, ...) \
log_interface_prefix_full_errno( \ log_interface_prefix_full_errno( \
"DHCPv4 server: ", \ "DHCPv4 server: ", \
sd_dhcp_server_get_ifname(server), \ sd_dhcp_server, server, \
error, fmt, ##__VA_ARGS__) error, fmt, ##__VA_ARGS__)
#define log_dhcp_server(server, fmt, ...) \ #define log_dhcp_server(server, fmt, ...) \
log_interface_prefix_full_errno_zerook( \ log_interface_prefix_full_errno_zerook( \
"DHCPv4 server: ", \ "DHCPv4 server: ", \
sd_dhcp_server_get_ifname(server), \ sd_dhcp_server, server, \
0, fmt, ##__VA_ARGS__) 0, fmt, ##__VA_ARGS__)

View File

@ -13,8 +13,8 @@
#include "hashmap.h" #include "hashmap.h"
#include "list.h" #include "list.h"
#include "log-link.h"
#include "macro.h" #include "macro.h"
#include "network-common.h"
#include "sparse-endian.h" #include "sparse-endian.h"
typedef struct sd_dhcp6_option { typedef struct sd_dhcp6_option {
@ -125,10 +125,10 @@ void dhcp6_client_set_test_mode(sd_dhcp6_client *client, bool test_mode);
#define log_dhcp6_client_errno(client, error, fmt, ...) \ #define log_dhcp6_client_errno(client, error, fmt, ...) \
log_interface_prefix_full_errno( \ log_interface_prefix_full_errno( \
"DHCPv6 client: ", \ "DHCPv6 client: ", \
sd_dhcp6_client_get_ifname(client), \ sd_dhcp6_client, client, \
error, fmt, ##__VA_ARGS__) error, fmt, ##__VA_ARGS__)
#define log_dhcp6_client(client, fmt, ...) \ #define log_dhcp6_client(client, fmt, ...) \
log_interface_prefix_full_errno_zerook( \ log_interface_prefix_full_errno_zerook( \
"DHCPv6 client: ", \ "DHCPv6 client: ", \
sd_dhcp6_client_get_ifname(client), \ sd_dhcp6_client, client, \
0, fmt, ##__VA_ARGS__) 0, fmt, ##__VA_ARGS__)

View File

@ -5,7 +5,7 @@
#include "sd-lldp-rx.h" #include "sd-lldp-rx.h"
#include "hashmap.h" #include "hashmap.h"
#include "log-link.h" #include "network-common.h"
#include "prioq.h" #include "prioq.h"
struct sd_lldp_rx { struct sd_lldp_rx {
@ -39,10 +39,10 @@ sd_lldp_rx_event_t lldp_rx_event_from_string(const char *s) _pure_;
#define log_lldp_rx_errno(lldp_rx, error, fmt, ...) \ #define log_lldp_rx_errno(lldp_rx, error, fmt, ...) \
log_interface_prefix_full_errno( \ log_interface_prefix_full_errno( \
"LLDP Rx: ", \ "LLDP Rx: ", \
sd_lldp_rx_get_ifname(lldp_rx), \ sd_lldp_rx, lldp_rx, \
error, fmt, ##__VA_ARGS__) error, fmt, ##__VA_ARGS__)
#define log_lldp_rx(lldp_rx, fmt, ...) \ #define log_lldp_rx(lldp_rx, fmt, ...) \
log_interface_prefix_full_errno_zerook( \ log_interface_prefix_full_errno_zerook( \
"LLDP Rx: ", \ "LLDP Rx: ", \
sd_lldp_rx_get_ifname(lldp_rx), \ sd_lldp_rx, lldp_rx, \
0, fmt, ##__VA_ARGS__) 0, fmt, ##__VA_ARGS__)

View File

@ -7,7 +7,7 @@
#include "sd-ndisc.h" #include "sd-ndisc.h"
#include "log-link.h" #include "network-common.h"
#include "time-util.h" #include "time-util.h"
#define NDISC_ROUTER_SOLICITATION_INTERVAL (4U * USEC_PER_SEC) #define NDISC_ROUTER_SOLICITATION_INTERVAL (4U * USEC_PER_SEC)
@ -44,10 +44,10 @@ sd_ndisc_event_t ndisc_event_from_string(const char *s) _pure_;
#define log_ndisc_errno(ndisc, error, fmt, ...) \ #define log_ndisc_errno(ndisc, error, fmt, ...) \
log_interface_prefix_full_errno( \ log_interface_prefix_full_errno( \
"NDISC: ", \ "NDISC: ", \
sd_ndisc_get_ifname(ndisc), \ sd_ndisc, ndisc, \
error, fmt, ##__VA_ARGS__) error, fmt, ##__VA_ARGS__)
#define log_ndisc(ndisc, fmt, ...) \ #define log_ndisc(ndisc, fmt, ...) \
log_interface_prefix_full_errno_zerook( \ log_interface_prefix_full_errno_zerook( \
"NDISC: ", \ "NDISC: ", \
sd_ndisc_get_ifname(ndisc), \ sd_ndisc, ndisc, \
0, fmt, ##__VA_ARGS__) 0, fmt, ##__VA_ARGS__)

View File

@ -2,23 +2,14 @@
#include "format-util.h" #include "format-util.h"
#include "network-common.h" #include "network-common.h"
#include "string-util.h"
const char *get_ifname(int ifindex, char **ifname) {
char buf[IF_NAMESIZE + 1];
int get_ifname(int ifindex, char **ifname) {
assert(ifname); assert(ifname);
/* This sets ifname only when it is not set yet. */ /* This sets ifname only when it is not set yet. */
if (*ifname) if (*ifname)
return *ifname; return 0;
if (ifindex <= 0) return format_ifname_alloc(ifindex, ifname);
return NULL;
if (!format_ifname(ifindex, buf))
return NULL;
return *ifname = strdup(buf);
} }

View File

@ -1,4 +1,28 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */ /* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once #pragma once
const char *get_ifname(int ifindex, char **ifname); #include "log-link.h"
#define log_interface_prefix_full_errno_zerook(prefix, type, val, error, fmt, ...) \
({ \
int _e = (error); \
if (DEBUG_LOGGING) { \
const char *_n = NULL; \
\
(void) type##_get_ifname(val, &_n); \
log_interface_full_errno_zerook( \
_n, LOG_DEBUG, _e, prefix fmt, \
##__VA_ARGS__); \
} \
-ERRNO_VALUE(_e); \
})
#define log_interface_prefix_full_errno(prefix, type, val, error, fmt, ...) \
({ \
int _error = (error); \
ASSERT_NON_ZERO(_error); \
log_interface_prefix_full_errno_zerook( \
prefix, type, val, _error, fmt, ##__VA_ARGS__); \
})
int get_ifname(int ifindex, char **ifname);

View File

@ -7,8 +7,8 @@
#include "sd-radv.h" #include "sd-radv.h"
#include "log-link.h"
#include "list.h" #include "list.h"
#include "network-common.h"
#include "sparse-endian.h" #include "sparse-endian.h"
assert_cc(SD_RADV_DEFAULT_MIN_TIMEOUT_USEC <= SD_RADV_DEFAULT_MAX_TIMEOUT_USEC); assert_cc(SD_RADV_DEFAULT_MIN_TIMEOUT_USEC <= SD_RADV_DEFAULT_MAX_TIMEOUT_USEC);
@ -128,10 +128,10 @@ struct sd_radv_route_prefix {
#define log_radv_errno(radv, error, fmt, ...) \ #define log_radv_errno(radv, error, fmt, ...) \
log_interface_prefix_full_errno( \ log_interface_prefix_full_errno( \
"RADV: ", \ "RADV: ", \
sd_radv_get_ifname(radv), \ sd_radv, radv, \
error, fmt, ##__VA_ARGS__) error, fmt, ##__VA_ARGS__)
#define log_radv(radv, fmt, ...) \ #define log_radv(radv, fmt, ...) \
log_interface_prefix_full_errno_zerook( \ log_interface_prefix_full_errno_zerook( \
"RADV: ", \ "RADV: ", \
sd_radv_get_ifname(radv), \ sd_radv, radv, \
0, fmt, ##__VA_ARGS__) 0, fmt, ##__VA_ARGS__)

View File

@ -297,11 +297,19 @@ int sd_dhcp_client_set_ifname(sd_dhcp_client *client, const char *ifname) {
return free_and_strdup(&client->ifname, ifname); return free_and_strdup(&client->ifname, ifname);
} }
const char *sd_dhcp_client_get_ifname(sd_dhcp_client *client) { int sd_dhcp_client_get_ifname(sd_dhcp_client *client, const char **ret) {
if (!client) int r;
return NULL;
return get_ifname(client->ifindex, &client->ifname); assert_return(client, -EINVAL);
r = get_ifname(client->ifindex, &client->ifname);
if (r < 0)
return r;
if (ret)
*ret = client->ifname;
return 0;
} }
int sd_dhcp_client_set_mac( int sd_dhcp_client_set_mac(

View File

@ -230,11 +230,19 @@ int sd_dhcp_server_set_ifname(sd_dhcp_server *server, const char *ifname) {
return free_and_strdup(&server->ifname, ifname); return free_and_strdup(&server->ifname, ifname);
} }
const char *sd_dhcp_server_get_ifname(sd_dhcp_server *server) { int sd_dhcp_server_get_ifname(sd_dhcp_server *server, const char **ret) {
if (!server) int r;
return NULL;
return get_ifname(server->ifindex, &server->ifname); assert_return(server, -EINVAL);
r = get_ifname(server->ifindex, &server->ifname);
if (r < 0)
return r;
if (ret)
*ret = server->ifname;
return 0;
} }
int sd_dhcp_server_attach_event(sd_dhcp_server *server, sd_event *event, int64_t priority) { int sd_dhcp_server_attach_event(sd_dhcp_server *server, sd_event *event, int64_t priority) {

View File

@ -179,11 +179,19 @@ int sd_dhcp6_client_set_ifname(sd_dhcp6_client *client, const char *ifname) {
return free_and_strdup(&client->ifname, ifname); return free_and_strdup(&client->ifname, ifname);
} }
const char *sd_dhcp6_client_get_ifname(sd_dhcp6_client *client) { int sd_dhcp6_client_get_ifname(sd_dhcp6_client *client, const char **ret) {
if (!client) int r;
return NULL;
return get_ifname(client->ifindex, &client->ifname); assert_return(client, -EINVAL);
r = get_ifname(client->ifindex, &client->ifname);
if (r < 0)
return r;
if (ret)
*ret = client->ifname;
return 0;
} }
int sd_dhcp6_client_set_local_address( int sd_dhcp6_client_set_local_address(

View File

@ -17,7 +17,6 @@
#include "event-util.h" #include "event-util.h"
#include "fd-util.h" #include "fd-util.h"
#include "in-addr-util.h" #include "in-addr-util.h"
#include "log-link.h"
#include "memory-util.h" #include "memory-util.h"
#include "network-common.h" #include "network-common.h"
#include "random-util.h" #include "random-util.h"
@ -81,12 +80,12 @@ struct sd_ipv4acd {
#define log_ipv4acd_errno(acd, error, fmt, ...) \ #define log_ipv4acd_errno(acd, error, fmt, ...) \
log_interface_prefix_full_errno( \ log_interface_prefix_full_errno( \
"IPv4ACD: ", \ "IPv4ACD: ", \
sd_ipv4acd_get_ifname(acd), \ sd_ipv4acd, acd, \
error, fmt, ##__VA_ARGS__) error, fmt, ##__VA_ARGS__)
#define log_ipv4acd(acd, fmt, ...) \ #define log_ipv4acd(acd, fmt, ...) \
log_interface_prefix_full_errno_zerook( \ log_interface_prefix_full_errno_zerook( \
"IPv4ACD: ", \ "IPv4ACD: ", \
sd_ipv4acd_get_ifname(acd), \ sd_ipv4acd, acd, \
0, fmt, ##__VA_ARGS__) 0, fmt, ##__VA_ARGS__)
static const char * const ipv4acd_state_table[_IPV4ACD_STATE_MAX] = { static const char * const ipv4acd_state_table[_IPV4ACD_STATE_MAX] = {
@ -445,11 +444,19 @@ int sd_ipv4acd_set_ifname(sd_ipv4acd *acd, const char *ifname) {
return free_and_strdup(&acd->ifname, ifname); return free_and_strdup(&acd->ifname, ifname);
} }
const char *sd_ipv4acd_get_ifname(sd_ipv4acd *acd) { int sd_ipv4acd_get_ifname(sd_ipv4acd *acd, const char **ret) {
if (!acd) int r;
return NULL;
return get_ifname(acd->ifindex, &acd->ifname); assert_return(acd, -EINVAL);
r = get_ifname(acd->ifindex, &acd->ifname);
if (r < 0)
return r;
if (ret)
*ret = acd->ifname;
return 0;
} }
int sd_ipv4acd_set_mac(sd_ipv4acd *acd, const struct ether_addr *addr) { int sd_ipv4acd_set_mac(sd_ipv4acd *acd, const struct ether_addr *addr) {

View File

@ -15,7 +15,7 @@
#include "alloc-util.h" #include "alloc-util.h"
#include "ether-addr-util.h" #include "ether-addr-util.h"
#include "in-addr-util.h" #include "in-addr-util.h"
#include "log-link.h" #include "network-common.h"
#include "random-util.h" #include "random-util.h"
#include "siphash24.h" #include "siphash24.h"
#include "sparse-endian.h" #include "sparse-endian.h"
@ -55,12 +55,12 @@ struct sd_ipv4ll {
#define log_ipv4ll_errno(ll, error, fmt, ...) \ #define log_ipv4ll_errno(ll, error, fmt, ...) \
log_interface_prefix_full_errno( \ log_interface_prefix_full_errno( \
"IPv4LL: ", \ "IPv4LL: ", \
sd_ipv4ll_get_ifname(ll), \ sd_ipv4ll, ll, \
error, fmt, ##__VA_ARGS__) error, fmt, ##__VA_ARGS__)
#define log_ipv4ll(ll, fmt, ...) \ #define log_ipv4ll(ll, fmt, ...) \
log_interface_prefix_full_errno_zerook( \ log_interface_prefix_full_errno_zerook( \
"IPv4LL: ", \ "IPv4LL: ", \
sd_ipv4ll_get_ifname(ll), \ sd_ipv4ll, ll, \
0, fmt, ##__VA_ARGS__) 0, fmt, ##__VA_ARGS__)
static void ipv4ll_on_acd(sd_ipv4acd *acd, int event, void *userdata); static void ipv4ll_on_acd(sd_ipv4acd *acd, int event, void *userdata);
@ -133,11 +133,10 @@ int sd_ipv4ll_set_ifname(sd_ipv4ll *ll, const char *ifname) {
return sd_ipv4acd_set_ifname(ll->acd, ifname); return sd_ipv4acd_set_ifname(ll->acd, ifname);
} }
const char *sd_ipv4ll_get_ifname(sd_ipv4ll *ll) { int sd_ipv4ll_get_ifname(sd_ipv4ll *ll, const char **ret) {
if (!ll) assert_return(ll, -EINVAL);
return NULL;
return sd_ipv4acd_get_ifname(ll->acd); return sd_ipv4acd_get_ifname(ll->acd, ret);
} }
int sd_ipv4ll_set_mac(sd_ipv4ll *ll, const struct ether_addr *addr) { int sd_ipv4ll_set_mac(sd_ipv4ll *ll, const struct ether_addr *addr) {

View File

@ -359,11 +359,19 @@ int sd_lldp_rx_set_ifname(sd_lldp_rx *lldp_rx, const char *ifname) {
return free_and_strdup(&lldp_rx->ifname, ifname); return free_and_strdup(&lldp_rx->ifname, ifname);
} }
const char *sd_lldp_rx_get_ifname(sd_lldp_rx *lldp_rx) { int sd_lldp_rx_get_ifname(sd_lldp_rx *lldp_rx, const char **ret) {
if (!lldp_rx) int r;
return NULL;
return get_ifname(lldp_rx->ifindex, &lldp_rx->ifname); assert_return(lldp_rx, -EINVAL);
r = get_ifname(lldp_rx->ifindex, &lldp_rx->ifname);
if (r < 0)
return r;
if (ret)
*ret = lldp_rx->ifname;
return 0;
} }
static sd_lldp_rx *lldp_rx_free(sd_lldp_rx *lldp_rx) { static sd_lldp_rx *lldp_rx_free(sd_lldp_rx *lldp_rx) {

View File

@ -12,7 +12,6 @@
#include "ether-addr-util.h" #include "ether-addr-util.h"
#include "fd-util.h" #include "fd-util.h"
#include "hostname-util.h" #include "hostname-util.h"
#include "log-link.h"
#include "network-common.h" #include "network-common.h"
#include "random-util.h" #include "random-util.h"
#include "socket-util.h" #include "socket-util.h"
@ -70,12 +69,12 @@ struct sd_lldp_tx {
#define log_lldp_tx_errno(lldp_tx, error, fmt, ...) \ #define log_lldp_tx_errno(lldp_tx, error, fmt, ...) \
log_interface_prefix_full_errno( \ log_interface_prefix_full_errno( \
"LLDP Tx: ", \ "LLDP Tx: ", \
sd_lldp_tx_get_ifname(lldp_tx), \ sd_lldp_tx, lldp_tx, \
error, fmt, ##__VA_ARGS__) error, fmt, ##__VA_ARGS__)
#define log_lldp_tx(lldp_tx, fmt, ...) \ #define log_lldp_tx(lldp_tx, fmt, ...) \
log_interface_prefix_full_errno_zerook( \ log_interface_prefix_full_errno_zerook( \
"LLDP Tx: ", \ "LLDP Tx: ", \
sd_lldp_tx_get_ifname(lldp_tx), \ sd_lldp_tx, lldp_tx, \
0, fmt, ##__VA_ARGS__) 0, fmt, ##__VA_ARGS__)
static sd_lldp_tx *lldp_tx_free(sd_lldp_tx *lldp_tx) { static sd_lldp_tx *lldp_tx_free(sd_lldp_tx *lldp_tx) {
@ -131,11 +130,19 @@ int sd_lldp_tx_set_ifname(sd_lldp_tx *lldp_tx, const char *ifname) {
return free_and_strdup(&lldp_tx->ifname, ifname); return free_and_strdup(&lldp_tx->ifname, ifname);
} }
const char *sd_lldp_tx_get_ifname(sd_lldp_tx *lldp_tx) { int sd_lldp_tx_get_ifname(sd_lldp_tx *lldp_tx, const char **ret) {
if (!lldp_tx) int r;
return NULL;
return get_ifname(lldp_tx->ifindex, &lldp_tx->ifname); assert_return(lldp_tx, -EINVAL);
r = get_ifname(lldp_tx->ifindex, &lldp_tx->ifname);
if (r < 0)
return r;
if (ret)
*ret = lldp_tx->ifname;
return 0;
} }
int sd_lldp_tx_set_multicast_mode(sd_lldp_tx *lldp_tx, sd_lldp_multicast_mode_t mode) { int sd_lldp_tx_set_multicast_mode(sd_lldp_tx *lldp_tx, sd_lldp_multicast_mode_t mode) {
@ -222,7 +229,7 @@ static size_t lldp_tx_calculate_maximum_packet_size(sd_lldp_tx *lldp_tx, const c
/* Chassis ID */ /* Chassis ID */
2 + 1 + (SD_ID128_STRING_MAX - 1) + 2 + 1 + (SD_ID128_STRING_MAX - 1) +
/* Port ID */ /* Port ID */
2 + 1 + strlen_ptr(sd_lldp_tx_get_ifname(lldp_tx)) + 2 + 1 + strlen_ptr(lldp_tx->ifname) +
/* Port description */ /* Port description */
2 + strlen_ptr(lldp_tx->port_description) + 2 + strlen_ptr(lldp_tx->port_description) +
/* System name */ /* System name */
@ -334,6 +341,11 @@ static int lldp_tx_create_packet(sd_lldp_tx *lldp_tx, size_t *ret_packet_size, u
assert(ret_packet_size); assert(ret_packet_size);
assert(ret_packet); assert(ret_packet);
/* If ifname is not set yet, set ifname from ifindex. */
r = sd_lldp_tx_get_ifname(lldp_tx, NULL);
if (r < 0)
return r;
r = sd_id128_get_machine(&machine_id); r = sd_id128_get_machine(&machine_id);
if (r < 0) if (r < 0)
return r; return r;
@ -365,7 +377,7 @@ static int lldp_tx_create_packet(sd_lldp_tx *lldp_tx, size_t *ret_packet_size, u
r = packet_append_prefixed_string(packet, packet_size, &offset, SD_LLDP_TYPE_PORT_ID, r = packet_append_prefixed_string(packet, packet_size, &offset, SD_LLDP_TYPE_PORT_ID,
1, (const uint8_t[]) { SD_LLDP_PORT_SUBTYPE_INTERFACE_NAME }, 1, (const uint8_t[]) { SD_LLDP_PORT_SUBTYPE_INTERFACE_NAME },
sd_lldp_tx_get_ifname(lldp_tx)); lldp_tx->ifname);
if (r < 0) if (r < 0)
return r; return r;

View File

@ -74,11 +74,19 @@ int sd_ndisc_set_ifname(sd_ndisc *nd, const char *ifname) {
return free_and_strdup(&nd->ifname, ifname); return free_and_strdup(&nd->ifname, ifname);
} }
const char *sd_ndisc_get_ifname(sd_ndisc *nd) { int sd_ndisc_get_ifname(sd_ndisc *nd, const char **ret) {
if (!nd) int r;
return NULL;
return get_ifname(nd->ifindex, &nd->ifname); assert_return(nd, -EINVAL);
r = get_ifname(nd->ifindex, &nd->ifname);
if (r < 0)
return r;
if (ret)
*ret = nd->ifname;
return 0;
} }
_public_ int sd_ndisc_set_mac(sd_ndisc *nd, const struct ether_addr *mac_addr) { _public_ int sd_ndisc_set_mac(sd_ndisc *nd, const struct ether_addr *mac_addr) {

View File

@ -438,11 +438,19 @@ int sd_radv_set_ifname(sd_radv *ra, const char *ifname) {
return free_and_strdup(&ra->ifname, ifname); return free_and_strdup(&ra->ifname, ifname);
} }
const char *sd_radv_get_ifname(sd_radv *ra) { int sd_radv_get_ifname(sd_radv *ra, const char **ret) {
if (!ra) int r;
return NULL;
return get_ifname(ra->ifindex, &ra->ifname); assert_return(ra, -EINVAL);
r = get_ifname(ra->ifindex, &ra->ifname);
if (r < 0)
return r;
if (ret)
*ret = ra->ifname;
return 0;
} }
_public_ int sd_radv_set_mac(sd_radv *ra, const struct ether_addr *mac_addr) { _public_ int sd_radv_set_mac(sd_radv *ra, const struct ether_addr *mac_addr) {

View File

@ -284,12 +284,12 @@ _public_ int sd_device_new_from_ifname(sd_device **ret, const char *ifname) {
} }
_public_ int sd_device_new_from_ifindex(sd_device **ret, int ifindex) { _public_ int sd_device_new_from_ifindex(sd_device **ret, int ifindex) {
char ifname[IF_NAMESIZE + 1]; char ifname[IF_NAMESIZE];
assert_return(ret, -EINVAL); assert_return(ret, -EINVAL);
assert_return(ifindex > 0, -EINVAL); assert_return(ifindex > 0, -EINVAL);
if (!format_ifname(ifindex, ifname)) if (format_ifname(ifindex, ifname) < 0)
return -ENODEV; return -ENODEV;
return device_new_from_main_ifname(ret, ifname); return device_new_from_main_ifname(ret, ifname);

View File

@ -12,7 +12,7 @@
int rtnl_set_link_name(sd_netlink **rtnl, int ifindex, const char *name) { int rtnl_set_link_name(sd_netlink **rtnl, int ifindex, const char *name) {
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL; _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL;
_cleanup_strv_free_ char **alternative_names = NULL; _cleanup_strv_free_ char **alternative_names = NULL;
char old_name[IF_NAMESIZE + 1] = {}; char old_name[IF_NAMESIZE] = {};
int r; int r;
assert(rtnl); assert(rtnl);
@ -33,7 +33,9 @@ int rtnl_set_link_name(sd_netlink **rtnl, int ifindex, const char *name) {
return log_debug_errno(r, "Failed to remove '%s' from alternative names on network interface %i: %m", return log_debug_errno(r, "Failed to remove '%s' from alternative names on network interface %i: %m",
name, ifindex); name, ifindex);
format_ifname(ifindex, old_name); r = format_ifname(ifindex, old_name);
if (r < 0)
return log_debug_errno(r, "Failed to get current name of network interface %i: %m", ifindex);
} }
r = sd_rtnl_message_new_link(*rtnl, &message, RTM_SETLINK, ifindex); r = sd_rtnl_message_new_link(*rtnl, &message, RTM_SETLINK, ifindex);

View File

@ -557,9 +557,9 @@ static void print_machine_status_info(sd_bus *bus, MachineStatusInfo *i) {
fputs("\t Iface:", stdout); fputs("\t Iface:", stdout);
for (size_t c = 0; c < i->n_netif; c++) { for (size_t c = 0; c < i->n_netif; c++) {
char name[IF_NAMESIZE+1]; char name[IF_NAMESIZE];
if (format_ifname(i->netif[c], name)) { if (format_ifname(i->netif[c], name) >= 0) {
fputc(' ', stdout); fputc(' ', stdout);
fputs(name, stdout); fputs(name, stdout);

View File

@ -1044,7 +1044,6 @@ static int dump_gateways(
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
_cleanup_free_ char *gateway = NULL, *description = NULL; _cleanup_free_ char *gateway = NULL, *description = NULL;
char name[IF_NAMESIZE+1];
r = in_addr_to_string(local[i].family, &local[i].address, &gateway); r = in_addr_to_string(local[i].family, &local[i].address, &gateway);
if (r < 0) if (r < 0)
@ -1063,7 +1062,7 @@ static int dump_gateways(
r = strv_extendf(&buf, "%s%s%s", r = strv_extendf(&buf, "%s%s%s",
gateway, gateway,
ifindex <= 0 ? " on " : "", ifindex <= 0 ? " on " : "",
ifindex <= 0 ? format_ifname_full(local[i].ifindex, name, FORMAT_IFNAME_IFINDEX_WITH_PERCENT) : ""); ifindex <= 0 ? FORMAT_IFNAME_FULL(local[i].ifindex, FORMAT_IFNAME_IFINDEX_WITH_PERCENT) : "");
if (r < 0) if (r < 0)
return log_oom(); return log_oom();
} }
@ -1094,7 +1093,6 @@ static int dump_addresses(
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
_cleanup_free_ char *pretty = NULL; _cleanup_free_ char *pretty = NULL;
char name[IF_NAMESIZE+1];
r = in_addr_to_string(local[i].family, &local[i].address, &pretty); r = in_addr_to_string(local[i].family, &local[i].address, &pretty);
if (r < 0) if (r < 0)
@ -1118,7 +1116,7 @@ static int dump_addresses(
r = strv_extendf(&buf, "%s%s%s", r = strv_extendf(&buf, "%s%s%s",
pretty, pretty,
ifindex <= 0 ? " on " : "", ifindex <= 0 ? " on " : "",
ifindex <= 0 ? format_ifname_full(local[i].ifindex, name, FORMAT_IFNAME_IFINDEX_WITH_PERCENT) : ""); ifindex <= 0 ? FORMAT_IFNAME_FULL(local[i].ifindex, FORMAT_IFNAME_IFINDEX_WITH_PERCENT) : "");
if (r < 0) if (r < 0)
return log_oom(); return log_oom();
} }
@ -2682,12 +2680,9 @@ static int link_up_down(int argc, char *argv[], void *userdata) {
SET_FOREACH(p, indexes) { SET_FOREACH(p, indexes) {
index = PTR_TO_INT(p); index = PTR_TO_INT(p);
r = link_up_down_send_message(rtnl, argv[0], index); r = link_up_down_send_message(rtnl, argv[0], index);
if (r < 0) { if (r < 0)
char ifname[IF_NAMESIZE + 1];
return log_error_errno(r, "Failed to bring %s interface %s: %m", return log_error_errno(r, "Failed to bring %s interface %s: %m",
argv[0], format_ifname_full(index, ifname, FORMAT_IFNAME_IFINDEX)); argv[0], FORMAT_IFNAME_FULL(index, FORMAT_IFNAME_IFINDEX));
}
} }
return r; return r;
@ -2720,12 +2715,9 @@ static int link_delete(int argc, char *argv[], void *userdata) {
SET_FOREACH(p, indexes) { SET_FOREACH(p, indexes) {
index = PTR_TO_INT(p); index = PTR_TO_INT(p);
r = link_delete_send_message(rtnl, index); r = link_delete_send_message(rtnl, index);
if (r < 0) { if (r < 0)
char ifname[IF_NAMESIZE + 1];
return log_error_errno(r, "Failed to delete interface %s: %m", return log_error_errno(r, "Failed to delete interface %s: %m",
format_ifname_full(index, ifname, FORMAT_IFNAME_IFINDEX)); FORMAT_IFNAME_FULL(index, FORMAT_IFNAME_IFINDEX));
}
} }
return r; return r;
@ -2844,12 +2836,9 @@ static int verb_reconfigure(int argc, char *argv[], void *userdata) {
SET_FOREACH(p, indexes) { SET_FOREACH(p, indexes) {
index = PTR_TO_INT(p); index = PTR_TO_INT(p);
r = bus_call_method(bus, bus_network_mgr, "ReconfigureLink", &error, NULL, "i", index); r = bus_call_method(bus, bus_network_mgr, "ReconfigureLink", &error, NULL, "i", index);
if (r < 0) { if (r < 0)
char ifname[IF_NAMESIZE + 1];
return log_error_errno(r, "Failed to reconfigure network interface %s: %m", return log_error_errno(r, "Failed to reconfigure network interface %s: %m",
format_ifname_full(index, ifname, FORMAT_IFNAME_IFINDEX)); FORMAT_IFNAME_FULL(index, FORMAT_IFNAME_IFINDEX));
}
} }
return 0; return 0;

View File

@ -2212,7 +2212,7 @@ static int link_update_alternative_names(Link *link, sd_netlink_message *message
} }
static int link_update_name(Link *link, sd_netlink_message *message) { static int link_update_name(Link *link, sd_netlink_message *message) {
char ifname_from_index[IF_NAMESIZE + 1]; char ifname_from_index[IF_NAMESIZE];
const char *ifname; const char *ifname;
int r; int r;
@ -2229,8 +2229,9 @@ static int link_update_name(Link *link, sd_netlink_message *message) {
if (streq(ifname, link->ifname)) if (streq(ifname, link->ifname))
return 0; return 0;
if (!format_ifname(link->ifindex, ifname_from_index)) r = format_ifname(link->ifindex, ifname_from_index);
return log_link_debug_errno(link, SYNTHETIC_ERRNO(ENXIO), "Could not get interface name for index %i.", link->ifindex); if (r < 0)
return log_link_debug_errno(link, r, "Could not get interface name for index %i.", link->ifindex);
if (!streq(ifname, ifname_from_index)) { if (!streq(ifname, ifname_from_index)) {
log_link_debug(link, "New interface name '%s' received from the kernel does not correspond " log_link_debug(link, "New interface name '%s' received from the kernel does not correspond "

View File

@ -187,14 +187,16 @@ static void print_source(uint64_t flags, usec_t rtt) {
} }
static void print_ifindex_comment(int printed_so_far, int ifindex) { static void print_ifindex_comment(int printed_so_far, int ifindex) {
char ifname[IF_NAMESIZE + 1]; char ifname[IF_NAMESIZE];
int r;
if (ifindex <= 0) if (ifindex <= 0)
return; return;
if (!format_ifname(ifindex, ifname)) r = format_ifname(ifindex, ifname);
log_warning_errno(errno, "Failed to resolve interface name for index %i, ignoring: %m", ifindex); if (r < 0)
else return (void) log_warning_errno(r, "Failed to resolve interface name for index %i, ignoring: %m", ifindex);
printf("%*s%s-- link: %s%s", printf("%*s%s-- link: %s%s",
60 > printed_so_far ? 60 - printed_so_far : 0, " ", /* Align comment to the 60th column */ 60 > printed_so_far ? 60 - printed_so_far : 0, " ", /* Align comment to the 60th column */
ansi_grey(), ifname, ansi_normal()); ansi_grey(), ifname, ansi_normal());
@ -1555,15 +1557,16 @@ static int status_ifindex(sd_bus *bus, int ifindex, const char *name, StatusMode
_cleanup_(link_info_clear) LinkInfo link_info = {}; _cleanup_(link_info_clear) LinkInfo link_info = {};
_cleanup_(table_unrefp) Table *table = NULL; _cleanup_(table_unrefp) Table *table = NULL;
_cleanup_free_ char *p = NULL; _cleanup_free_ char *p = NULL;
char ifi[DECIMAL_STR_MAX(int)], ifname[IF_NAMESIZE + 1] = ""; char ifi[DECIMAL_STR_MAX(int)], ifname[IF_NAMESIZE];
int r; int r;
assert(bus); assert(bus);
assert(ifindex > 0); assert(ifindex > 0);
if (!name) { if (!name) {
if (!format_ifname(ifindex, ifname)) r = format_ifname(ifindex, ifname);
return log_error_errno(errno, "Failed to resolve interface name for %i: %m", ifindex); if (r < 0)
return log_error_errno(r, "Failed to resolve interface name for %i: %m", ifindex);
name = ifname; name = ifname;
} }

View File

@ -504,7 +504,6 @@ static int dns_cache_put_positive(
if (DEBUG_LOGGING) { if (DEBUG_LOGGING) {
_cleanup_free_ char *t = NULL; _cleanup_free_ char *t = NULL;
char ifname[IF_NAMESIZE + 1];
(void) in_addr_to_string(i->owner_family, &i->owner_address, &t); (void) in_addr_to_string(i->owner_family, &i->owner_address, &t);
@ -514,7 +513,7 @@ static int dns_cache_put_positive(
i->shared_owner ? " shared" : "", i->shared_owner ? " shared" : "",
dns_resource_key_to_string(i->key, key_str, sizeof key_str), dns_resource_key_to_string(i->key, key_str, sizeof key_str),
(i->until - timestamp) / USEC_PER_SEC, (i->until - timestamp) / USEC_PER_SEC,
i->ifindex == 0 ? "*" : strna(format_ifname(i->ifindex, ifname)), i->ifindex == 0 ? "*" : FORMAT_IFNAME(i->ifindex),
af_to_name_short(i->owner_family), af_to_name_short(i->owner_family),
strna(t)); strna(t));
} }

View File

@ -1665,16 +1665,9 @@ static const char *table_data_format(Table *t, TableData *d, bool avoid_uppercas
case TABLE_IFINDEX: { case TABLE_IFINDEX: {
_cleanup_free_ char *p = NULL; _cleanup_free_ char *p = NULL;
char name[IF_NAMESIZE + 1];
if (format_ifname(d->ifindex, name)) { if (format_ifname_full_alloc(d->ifindex, FORMAT_IFNAME_IFINDEX, &p) < 0)
p = strdup(name);
if (!p)
return NULL; return NULL;
} else {
if (asprintf(&p, "%i" , d->ifindex) < 0)
return NULL;
}
d->formatted = TAKE_PTR(p); d->formatted = TAKE_PTR(p);
break; break;

View File

@ -17,24 +17,6 @@
log_interface_full_errno_zerook(ifname, level, _error, __VA_ARGS__); \ log_interface_full_errno_zerook(ifname, level, _error, __VA_ARGS__); \
}) })
#define log_interface_prefix_full_errno_zerook(prefix, ifname_expr, error, fmt, ...) \
({ \
int _e = (error); \
if (DEBUG_LOGGING) \
log_interface_full_errno_zerook( \
ifname_expr, \
LOG_DEBUG, _e, prefix fmt, \
##__VA_ARGS__); \
-ERRNO_VALUE(_e); \
})
#define log_interface_prefix_full_errno(prefix, ifname_expr, error, fmt, ...) \
({ \
int _error = (error); \
ASSERT_NON_ZERO(_error); \
log_interface_prefix_full_errno_zerook(prefix, ifname_expr, _error, fmt, ##__VA_ARGS__); \
})
/* /*
* The following macros append INTERFACE= to the message. * The following macros append INTERFACE= to the message.
* The macros require a struct named 'Link' which contains 'char *ifname': * The macros require a struct named 'Link' which contains 'char *ifname':

View File

@ -5,6 +5,13 @@
#include "proc-cmdline.h" #include "proc-cmdline.h"
#include "string-util.h" #include "string-util.h"
#ifdef _DEFAULT_NET_NAMING_SCHEME_TEST
/* The primary purpose of this check is to verify that _DEFAULT_NET_NAMING_SCHEME_TEST
* is a valid identifier. If an invalid name is given during configuration, this will
* fail with a name error. */
assert_cc(_DEFAULT_NET_NAMING_SCHEME_TEST >= 0);
#endif
static const NamingScheme naming_schemes[] = { static const NamingScheme naming_schemes[] = {
{ "v238", NAMING_V238 }, { "v238", NAMING_V238 },
{ "v239", NAMING_V239 }, { "v239", NAMING_V239 },
@ -15,19 +22,22 @@ static const NamingScheme naming_schemes[] = {
{ "v247", NAMING_V247 }, { "v247", NAMING_V247 },
{ "v249", NAMING_V249 }, { "v249", NAMING_V249 },
/* … add more schemes here, as the logic to name devices is updated … */ /* … add more schemes here, as the logic to name devices is updated … */
/* also remember to update the list of options in meson_options.txt */
EXTRA_NET_NAMING_MAP
}; };
static const NamingScheme* naming_scheme_from_name(const char *name) { const NamingScheme* naming_scheme_from_name(const char *name) {
size_t i; /* "latest" may either be defined explicitly by the extra map, in which case we we will find it in
* the table like any other name. After iterating through the table, we check for "latest" again,
* which means that if not mapped explicitly, it maps to the last defined entry, whatever that is. */
for (size_t i = 0; i < ELEMENTSOF(naming_schemes); i++)
if (streq(naming_schemes[i].name, name))
return naming_schemes + i;
if (streq(name, "latest")) if (streq(name, "latest"))
return naming_schemes + ELEMENTSOF(naming_schemes) - 1; return naming_schemes + ELEMENTSOF(naming_schemes) - 1;
for (i = 0; i < ELEMENTSOF(naming_schemes); i++)
if (streq(naming_schemes[i].name, name))
return naming_schemes + i;
return NULL; return NULL;
} }

View File

@ -46,6 +46,8 @@ typedef enum NamingSchemeFlags {
NAMING_V247 = NAMING_V245 | NAMING_BRIDGE_NO_SLOT, NAMING_V247 = NAMING_V245 | NAMING_BRIDGE_NO_SLOT,
NAMING_V249 = NAMING_V247 | NAMING_SLOT_FUNCTION_ID | NAMING_16BIT_INDEX | NAMING_REPLACE_STRICTLY, NAMING_V249 = NAMING_V247 | NAMING_SLOT_FUNCTION_ID | NAMING_16BIT_INDEX | NAMING_REPLACE_STRICTLY,
EXTRA_NET_NAMING_SCHEMES
_NAMING_SCHEME_FLAGS_INVALID = -EINVAL, _NAMING_SCHEME_FLAGS_INVALID = -EINVAL,
} NamingSchemeFlags; } NamingSchemeFlags;
@ -54,6 +56,7 @@ typedef struct NamingScheme {
NamingSchemeFlags flags; NamingSchemeFlags flags;
} NamingScheme; } NamingScheme;
const NamingScheme* naming_scheme_from_name(const char *name);
const NamingScheme* naming_scheme(void); const NamingScheme* naming_scheme(void);
static inline bool naming_scheme_has(NamingSchemeFlags flags) { static inline bool naming_scheme_has(NamingSchemeFlags flags) {

View File

@ -135,7 +135,7 @@ int sd_dhcp_client_set_ifindex(
int sd_dhcp_client_set_ifname( int sd_dhcp_client_set_ifname(
sd_dhcp_client *client, sd_dhcp_client *client,
const char *interface_name); const char *interface_name);
const char *sd_dhcp_client_get_ifname(sd_dhcp_client *client); int sd_dhcp_client_get_ifname(sd_dhcp_client *client, const char **ret);
int sd_dhcp_client_set_mac( int sd_dhcp_client_set_mac(
sd_dhcp_client *client, sd_dhcp_client *client,
const uint8_t *addr, const uint8_t *addr,

View File

@ -38,7 +38,7 @@ enum {
int sd_dhcp_server_new(sd_dhcp_server **ret, int ifindex); int sd_dhcp_server_new(sd_dhcp_server **ret, int ifindex);
int sd_dhcp_server_set_ifname(sd_dhcp_server *server, const char *ifname); int sd_dhcp_server_set_ifname(sd_dhcp_server *server, const char *ifname);
const char *sd_dhcp_server_get_ifname(sd_dhcp_server *server); int sd_dhcp_server_get_ifname(sd_dhcp_server *server, const char **ret);
sd_dhcp_server *sd_dhcp_server_ref(sd_dhcp_server *server); sd_dhcp_server *sd_dhcp_server_ref(sd_dhcp_server *server);
sd_dhcp_server *sd_dhcp_server_unref(sd_dhcp_server *server); sd_dhcp_server *sd_dhcp_server_unref(sd_dhcp_server *server);

View File

@ -94,7 +94,7 @@ int sd_dhcp6_client_set_ifindex(
int sd_dhcp6_client_set_ifname( int sd_dhcp6_client_set_ifname(
sd_dhcp6_client *client, sd_dhcp6_client *client,
const char *interface_name); const char *interface_name);
const char * sd_dhcp6_client_get_ifname(sd_dhcp6_client *client); int sd_dhcp6_client_get_ifname(sd_dhcp6_client *client, const char **ret);
int sd_dhcp6_client_set_local_address( int sd_dhcp6_client_set_local_address(
sd_dhcp6_client *client, sd_dhcp6_client *client,
const struct in6_addr *local_address); const struct in6_addr *local_address);

View File

@ -47,7 +47,7 @@ int sd_ipv4acd_set_mac(sd_ipv4acd *acd, const struct ether_addr *addr);
int sd_ipv4acd_set_ifindex(sd_ipv4acd *acd, int interface_index); int sd_ipv4acd_set_ifindex(sd_ipv4acd *acd, int interface_index);
int sd_ipv4acd_get_ifindex(sd_ipv4acd *acd); int sd_ipv4acd_get_ifindex(sd_ipv4acd *acd);
int sd_ipv4acd_set_ifname(sd_ipv4acd *acd, const char *interface_name); int sd_ipv4acd_set_ifname(sd_ipv4acd *acd, const char *interface_name);
const char *sd_ipv4acd_get_ifname(sd_ipv4acd *acd); int sd_ipv4acd_get_ifname(sd_ipv4acd *acd, const char **ret);
int sd_ipv4acd_set_address(sd_ipv4acd *acd, const struct in_addr *address); int sd_ipv4acd_set_address(sd_ipv4acd *acd, const struct in_addr *address);
int sd_ipv4acd_is_running(sd_ipv4acd *acd); int sd_ipv4acd_is_running(sd_ipv4acd *acd);
int sd_ipv4acd_start(sd_ipv4acd *acd, bool reset_conflicts); int sd_ipv4acd_start(sd_ipv4acd *acd, bool reset_conflicts);

View File

@ -47,7 +47,7 @@ int sd_ipv4ll_set_mac(sd_ipv4ll *ll, const struct ether_addr *addr);
int sd_ipv4ll_set_ifindex(sd_ipv4ll *ll, int interface_index); int sd_ipv4ll_set_ifindex(sd_ipv4ll *ll, int interface_index);
int sd_ipv4ll_get_ifindex(sd_ipv4ll *ll); int sd_ipv4ll_get_ifindex(sd_ipv4ll *ll);
int sd_ipv4ll_set_ifname(sd_ipv4ll *ll, const char *interface_name); int sd_ipv4ll_set_ifname(sd_ipv4ll *ll, const char *interface_name);
const char *sd_ipv4ll_get_ifname(sd_ipv4ll *ll); int sd_ipv4ll_get_ifname(sd_ipv4ll *ll, const char **ret);
int sd_ipv4ll_set_address(sd_ipv4ll *ll, const struct in_addr *address); int sd_ipv4ll_set_address(sd_ipv4ll *ll, const struct in_addr *address);
int sd_ipv4ll_set_address_seed(sd_ipv4ll *ll, uint64_t seed); int sd_ipv4ll_set_address_seed(sd_ipv4ll *ll, uint64_t seed);
int sd_ipv4ll_is_running(sd_ipv4ll *ll); int sd_ipv4ll_is_running(sd_ipv4ll *ll);

View File

@ -59,7 +59,7 @@ sd_event *sd_lldp_rx_get_event(sd_lldp_rx *lldp_rx);
int sd_lldp_rx_set_callback(sd_lldp_rx *lldp_rx, sd_lldp_rx_callback_t cb, void *userdata); int sd_lldp_rx_set_callback(sd_lldp_rx *lldp_rx, sd_lldp_rx_callback_t cb, void *userdata);
int sd_lldp_rx_set_ifindex(sd_lldp_rx *lldp_rx, int ifindex); int sd_lldp_rx_set_ifindex(sd_lldp_rx *lldp_rx, int ifindex);
int sd_lldp_rx_set_ifname(sd_lldp_rx *lldp_rx, const char *ifname); int sd_lldp_rx_set_ifname(sd_lldp_rx *lldp_rx, const char *ifname);
const char *sd_lldp_rx_get_ifname(sd_lldp_rx *lldp_rx); int sd_lldp_rx_get_ifname(sd_lldp_rx *lldp_rx, const char **ret);
/* Controls how much and what to store in the neighbors database */ /* Controls how much and what to store in the neighbors database */
int sd_lldp_rx_set_neighbors_max(sd_lldp_rx *lldp_rx, uint64_t n); int sd_lldp_rx_set_neighbors_max(sd_lldp_rx *lldp_rx, uint64_t n);

View File

@ -54,7 +54,7 @@ sd_event *sd_lldp_tx_get_event(sd_lldp_tx *lldp_tx);
int sd_lldp_tx_set_ifindex(sd_lldp_tx *lldp_tx, int ifindex); int sd_lldp_tx_set_ifindex(sd_lldp_tx *lldp_tx, int ifindex);
int sd_lldp_tx_set_ifname(sd_lldp_tx *lldp_tx, const char *ifname); int sd_lldp_tx_set_ifname(sd_lldp_tx *lldp_tx, const char *ifname);
const char *sd_lldp_tx_get_ifname(sd_lldp_tx *lldp_tx); int sd_lldp_tx_get_ifname(sd_lldp_tx *lldp_tx, const char **ret);
int sd_lldp_tx_set_multicast_mode(sd_lldp_tx *lldp_tx, sd_lldp_multicast_mode_t mode); int sd_lldp_tx_set_multicast_mode(sd_lldp_tx *lldp_tx, sd_lldp_multicast_mode_t mode);
int sd_lldp_tx_set_hwaddr(sd_lldp_tx *lldp_tx, const struct ether_addr *hwaddr); int sd_lldp_tx_set_hwaddr(sd_lldp_tx *lldp_tx, const struct ether_addr *hwaddr);

View File

@ -79,7 +79,7 @@ sd_event *sd_ndisc_get_event(sd_ndisc *nd);
int sd_ndisc_set_callback(sd_ndisc *nd, sd_ndisc_callback_t cb, void *userdata); int sd_ndisc_set_callback(sd_ndisc *nd, sd_ndisc_callback_t cb, void *userdata);
int sd_ndisc_set_ifindex(sd_ndisc *nd, int interface_index); int sd_ndisc_set_ifindex(sd_ndisc *nd, int interface_index);
int sd_ndisc_set_ifname(sd_ndisc *nd, const char *interface_name); int sd_ndisc_set_ifname(sd_ndisc *nd, const char *interface_name);
const char *sd_ndisc_get_ifname(sd_ndisc *nd); int sd_ndisc_get_ifname(sd_ndisc *nd, const char **ret);
int sd_ndisc_set_mac(sd_ndisc *nd, const struct ether_addr *mac_addr); int sd_ndisc_set_mac(sd_ndisc *nd, const struct ether_addr *mac_addr);
int sd_ndisc_get_mtu(sd_ndisc *nd, uint32_t *ret); int sd_ndisc_get_mtu(sd_ndisc *nd, uint32_t *ret);

View File

@ -54,7 +54,7 @@ int sd_radv_is_running(sd_radv *ra);
int sd_radv_set_ifindex(sd_radv *ra, int interface_index); int sd_radv_set_ifindex(sd_radv *ra, int interface_index);
int sd_radv_set_ifname(sd_radv *ra, const char *interface_name); int sd_radv_set_ifname(sd_radv *ra, const char *interface_name);
const char *sd_radv_get_ifname(sd_radv *ra); int sd_radv_get_ifname(sd_radv *ra, const char **ret);
int sd_radv_set_mac(sd_radv *ra, const struct ether_addr *mac_addr); int sd_radv_set_mac(sd_radv *ra, const struct ether_addr *mac_addr);
int sd_radv_set_mtu(sd_radv *ra, uint32_t mtu); int sd_radv_set_mtu(sd_radv *ra, uint32_t mtu);
int sd_radv_set_hop_limit(sd_radv *ra, uint8_t hop_limit); int sd_radv_set_hop_limit(sd_radv *ra, uint8_t hop_limit);

View File

@ -429,6 +429,8 @@ tests += [
[['src/test/test-firewall-util.c']], [['src/test/test-firewall-util.c']],
[['src/test/test-net-naming-scheme.c']],
[['src/test/test-netlink-manual.c'], [['src/test/test-netlink-manual.c'],
[], [],
[libkmod], [libkmod],

View File

@ -0,0 +1,31 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "netif-naming-scheme.h"
#include "string-util.h"
#include "tests.h"
static void test_default_net_naming_scheme(void) {
log_info("/* %s */", __func__);
const NamingScheme *n;
assert_se(n = naming_scheme_from_name(DEFAULT_NET_NAMING_SCHEME));
log_info("default → %s", n->name);
}
static void test_naming_scheme_conversions(void) {
log_info("/* %s */", __func__);
const NamingScheme *n;
assert_se(n = naming_scheme_from_name("latest"));
log_info("latest → %s", n->name);
assert_se(n = naming_scheme_from_name("v238"));
assert_se(streq(n->name, "v238"));
}
int main(int argc, char **argv) {
test_setup_logging(LOG_INFO);
test_default_net_naming_scheme();
test_naming_scheme_conversions();
}

View File

@ -41,14 +41,12 @@ static const char* af_to_string(int family, char *buf, size_t buf_len) {
} }
static int print_gaih_addrtuples(const struct gaih_addrtuple *tuples) { static int print_gaih_addrtuples(const struct gaih_addrtuple *tuples) {
int n = 0; int r, n = 0;
for (const struct gaih_addrtuple *it = tuples; it; it = it->next) { for (const struct gaih_addrtuple *it = tuples; it; it = it->next) {
_cleanup_free_ char *a = NULL; _cleanup_free_ char *a = NULL;
union in_addr_union u; union in_addr_union u;
int r;
char family_name[DECIMAL_STR_MAX(int)]; char family_name[DECIMAL_STR_MAX(int)];
char ifname[IF_NAMESIZE + 1];
memcpy(&u, it->addr, 16); memcpy(&u, it->addr, 16);
r = in_addr_to_string(it->family, &u, &a); r = in_addr_to_string(it->family, &u, &a);
@ -56,20 +54,12 @@ static int print_gaih_addrtuples(const struct gaih_addrtuple *tuples) {
if (r == -EAFNOSUPPORT) if (r == -EAFNOSUPPORT)
assert_se(a = hexmem(it->addr, 16)); assert_se(a = hexmem(it->addr, 16));
if (it->scopeid == 0) log_info(" \"%s\" %s %s %s",
goto numerical_index;
if (!format_ifname(it->scopeid, ifname)) {
log_warning_errno(errno, "if_indextoname(%d) failed: %m", it->scopeid);
numerical_index:
xsprintf(ifname, "%i", it->scopeid);
};
log_info(" \"%s\" %s %s %%%s",
it->name, it->name,
af_to_string(it->family, family_name, sizeof family_name), af_to_string(it->family, family_name, sizeof family_name),
a, a,
ifname); FORMAT_IFNAME_FULL(it->scopeid, FORMAT_IFNAME_IFINDEX_WITH_PERCENT));
n++; n++;
} }
return n; return n;

View File

@ -1000,6 +1000,33 @@ static void test_strextendf(void) {
assert_se(streq(p, "<77>,<99>,< 88>,<00001234>")); assert_se(streq(p, "<77>,<99>,< 88>,<00001234>"));
} }
static void test_streq_skip_trailing_chars(void) {
log_info("/* %s */", __func__);
/* NULL is WHITESPACE by default*/
assert_se(streq_skip_trailing_chars("foo bar", "foo bar", NULL));
assert_se(streq_skip_trailing_chars("foo", "foo", NULL));
assert_se(streq_skip_trailing_chars("foo bar ", "foo bar", NULL));
assert_se(streq_skip_trailing_chars("foo bar", "foo bar\t\t", NULL));
assert_se(streq_skip_trailing_chars("foo bar ", "foo bar\t\t", NULL));
assert_se(streq_skip_trailing_chars("foo\nbar", "foo\nbar", NULL));
assert_se(streq_skip_trailing_chars("\t\tfoo bar", "\t\tfoo bar", NULL));
assert_se(streq_skip_trailing_chars(" foo bar\t", " foo bar\n", NULL));
assert_se(!streq_skip_trailing_chars("foobar", "foo bar", NULL));
assert_se(!streq_skip_trailing_chars("foo\nbar", "foo\tbar", NULL));
assert_se(!streq_skip_trailing_chars("\t\nfoo bar", "\t foo bar", NULL));
assert_se(streq_skip_trailing_chars("foo bar ", "foo bar", WHITESPACE));
assert_se(!streq_skip_trailing_chars("foo bar ", "foo bar", NEWLINE));
assert_se(streq_skip_trailing_chars(NULL, NULL, NULL));
assert_se(streq_skip_trailing_chars("", "", NULL));
assert_se(!streq_skip_trailing_chars(NULL, "foo bar", NULL));
assert_se(!streq_skip_trailing_chars("foo", NULL, NULL));
assert_se(!streq_skip_trailing_chars("", "f", NULL));
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
test_setup_logging(LOG_DEBUG); test_setup_logging(LOG_DEBUG);
@ -1039,6 +1066,7 @@ int main(int argc, char *argv[]) {
test_string_contains_word(); test_string_contains_word();
test_strverscmp_improved(); test_strverscmp_improved();
test_strextendf(); test_strextendf();
test_streq_skip_trailing_chars();
return 0; return 0;
} }