1
0
mirror of https://github.com/systemd/systemd synced 2025-09-29 08:44:45 +02:00

Compare commits

...

14 Commits

Author SHA1 Message Date
Luca Boccassi
e953dcab39
Merge pull request #18384 from poettering/mangle-os-fix
import: two fixes to OS mangling logic
2021-01-26 20:23:52 +00:00
Yu Watanabe
f0d87798ea test-network: retry several times if expected LLDP info is not obtained
As LLDP thing does not get involved in the link status, `networkctl lldp`
may not provide an expected information even if the link is in
'configured' state.

Fixes #17360.
2021-01-26 18:48:44 +01:00
Lennart Poettering
0de405873c
Merge pull request #18377 from yuwata/sd-device-cleanups
sd-device: several tiny cleanups
2021-01-26 17:39:54 +01:00
Lennart Poettering
04a853848e
Merge pull request #18382 from yuwata/fix-downgrade-to-bool
sd-device,sd-netlink: trivial cleanups
2021-01-26 17:38:19 +01:00
Lennart Poettering
0ca04e5fdc import-common: when mangling OS trees, propagate ownership/mode from subdir to parent
After all we want to remove the top-level dir, and make it look like the
only subdir, hence propagate the attributes from the subdir to the
top-level dir.
2021-01-26 17:33:43 +01:00
Lennart Poettering
dda859f84c import-common: fix log message string
The trees do look like directory trees, obviously. But they don't like
OS trees.
2021-01-26 17:31:31 +01:00
Yu Watanabe
49f2369377 sd-device: do not use downgrade-to-bool feature 2021-01-27 00:01:02 +09:00
Yu Watanabe
d6ae82bdd9 sd-netlink: fix indentation 2021-01-26 23:58:08 +09:00
Yu Watanabe
22d33e8090 sd-netlink: do not use downgrade-to-bool feature 2021-01-26 23:57:59 +09:00
Yu Watanabe
ce634c4a4d sd-device: use size_t for index in the loop 2021-01-26 20:35:53 +09:00
Yu Watanabe
61c0972dad sd-device: use string_hash_ops_free_free 2021-01-26 20:26:33 +09:00
Yu Watanabe
327379f5f2 sd-device: add a short comment why we simply return negative errno here on failure 2021-01-26 20:07:38 +09:00
Yu Watanabe
4f1ef7f9e0 sd-device: rename variables and use TAKE_PTR() 2021-01-26 19:56:13 +09:00
Yu Watanabe
dd75bbee43 sd-device: use string_hash_ops_free_free 2021-01-26 19:52:56 +09:00
5 changed files with 71 additions and 50 deletions

View File

@ -17,6 +17,7 @@
#include "process-util.h"
#include "selinux-util.h"
#include "signal-util.h"
#include "stat-util.h"
#include "tmpfile-util.h"
#include "util.h"
@ -195,10 +196,10 @@ int import_fork_tar_c(const char *path, pid_t *ret) {
}
int import_mangle_os_tree(const char *path) {
_cleanup_free_ char *child = NULL, *t = NULL, *joined = NULL;
_cleanup_closedir_ DIR *d = NULL, *cd = NULL;
_cleanup_free_ char *child = NULL, *t = NULL;
const char *joined;
struct dirent *de;
struct stat st;
int r;
assert(path);
@ -240,14 +241,24 @@ int import_mangle_os_tree(const char *path) {
if (errno != 0)
return log_error_errno(errno, "Failed to iterate through directory '%s': %m", path);
log_debug("Directory '%s' does not look like a directory tree, and has multiple children, leaving as it is.", path);
log_debug("Directory '%s' does not look like an OS tree, and has multiple children, leaving as it is.", path);
return 0;
}
joined = prefix_roota(path, child);
if (fstatat(dirfd(d), child, &st, AT_SYMLINK_NOFOLLOW) < 0)
return log_debug_errno(errno, "Failed to stat file '%s/%s': %m", path, child);
r = stat_verify_directory(&st);
if (r < 0) {
log_debug_errno(r, "Child '%s' of directory '%s' is not a directory, leaving things as they are.", child, path);
return 0;
}
joined = path_join(path, child);
if (!joined)
return log_oom();
r = path_is_os_tree(joined);
if (r == -ENOTDIR) {
log_debug("Directory '%s' does not look like a directory tree, and contains a single regular file only, leaving as it is.", path);
log_debug("Directory '%s' does not look like an OS tree, and contains a single regular file only, leaving as it is.", path);
return 0;
}
if (r < 0)
@ -293,6 +304,14 @@ int import_mangle_os_tree(const char *path) {
if (unlinkat(dirfd(d), t, AT_REMOVEDIR) < 0)
return log_error_errno(errno, "Failed to remove temporary directory '%s/%s': %m", path, t);
r = futimens(dirfd(d), (struct timespec[2]) { st.st_atim, st.st_mtim });
if (r < 0)
log_debug_errno(r, "Failed to adjust top-level timestamps '%s', ignoring: %m", path);
r = fchmod_and_chown(dirfd(d), st.st_mode, st.st_uid, st.st_gid);
if (r < 0)
return log_error_errno(r, "Failed to adjust top-level directory mode/ownership '%s': %m", path);
log_info("Successfully rearranged OS tree.");
return 0;

View File

@ -448,7 +448,6 @@ int device_new_from_strv(sd_device **ret, char **strv) {
int device_new_from_nulstr(sd_device **ret, uint8_t *nulstr, size_t len) {
_cleanup_(sd_device_unrefp) sd_device *device = NULL;
const char *major = NULL, *minor = NULL;
unsigned i = 0;
int r;
assert(ret);
@ -459,11 +458,11 @@ int device_new_from_nulstr(sd_device **ret, uint8_t *nulstr, size_t len) {
if (r < 0)
return r;
while (i < len) {
for (size_t i = 0; i < len; ) {
char *key;
const char *end;
key = (char*)&nulstr[i];
key = (char*) &nulstr[i];
end = memchr(key, '\0', len - i);
if (!end)
return log_device_debug_errno(device, SYNTHETIC_ERRNO(EINVAL),

View File

@ -65,9 +65,9 @@ static sd_device *device_free(sd_device *device) {
free(device->properties_strv);
free(device->properties_nulstr);
ordered_hashmap_free_free_free(device->properties);
ordered_hashmap_free_free_free(device->properties_db);
hashmap_free_free_free(device->sysattr_values);
ordered_hashmap_free(device->properties);
ordered_hashmap_free(device->properties_db);
hashmap_free(device->sysattr_values);
set_free(device->sysattrs);
set_free(device->all_tags);
set_free(device->current_tags);
@ -78,46 +78,46 @@ static sd_device *device_free(sd_device *device) {
DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_device, sd_device, device_free);
int device_add_property_aux(sd_device *device, const char *_key, const char *_value, bool db) {
int device_add_property_aux(sd_device *device, const char *key, const char *value, bool db) {
OrderedHashmap **properties;
assert(device);
assert(_key);
assert(key);
if (db)
properties = &device->properties_db;
else
properties = &device->properties;
if (_value) {
_cleanup_free_ char *key = NULL, *value = NULL, *old_key = NULL, *old_value = NULL;
if (value) {
_cleanup_free_ char *new_key = NULL, *new_value = NULL, *old_key = NULL, *old_value = NULL;
int r;
r = ordered_hashmap_ensure_allocated(properties, &string_hash_ops);
r = ordered_hashmap_ensure_allocated(properties, &string_hash_ops_free_free);
if (r < 0)
return r;
key = strdup(_key);
if (!key)
new_key = strdup(key);
if (!new_key)
return -ENOMEM;
value = strdup(_value);
if (!value)
new_value = strdup(value);
if (!new_value)
return -ENOMEM;
old_value = ordered_hashmap_get2(*properties, key, (void**) &old_key);
r = ordered_hashmap_replace(*properties, key, value);
/* ordered_hashmap_replace() does not fail when the hashmap already has the entry. */
r = ordered_hashmap_replace(*properties, new_key, new_value);
if (r < 0)
return r;
key = NULL;
value = NULL;
TAKE_PTR(new_key);
TAKE_PTR(new_value);
} else {
_cleanup_free_ char *key = NULL;
_cleanup_free_ char *value = NULL;
_cleanup_free_ char *old_key = NULL, *old_value = NULL;
value = ordered_hashmap_remove2(*properties, _key, (void**) &key);
old_value = ordered_hashmap_remove2(*properties, key, (void**) &old_key);
}
if (!db) {
@ -231,8 +231,7 @@ _public_ int sd_device_new_from_syspath(sd_device **ret, const char *syspath) {
}
_public_ int sd_device_new_from_devnum(sd_device **ret, char type, dev_t devnum) {
char *syspath;
char id[DECIMAL_STR_MAX(unsigned) * 2 + 1];
char id[DECIMAL_STR_MAX(unsigned) * 2 + 1], *syspath;
assert_return(ret, -EINVAL);
assert_return(IN_SET(type, 'b', 'c'), -EINVAL);
@ -399,7 +398,7 @@ int device_set_devmode(sd_device *device, const char *_devmode) {
}
int device_set_devnum(sd_device *device, const char *major, const char *minor) {
unsigned maj = 0, min = 0;
unsigned maj, min = 0;
int r;
assert(device);
@ -408,7 +407,7 @@ int device_set_devnum(sd_device *device, const char *major, const char *minor) {
r = safe_atou(major, &maj);
if (r < 0)
return r;
if (!maj)
if (maj == 0)
return 0;
if (minor) {
@ -1780,30 +1779,28 @@ _public_ int sd_device_get_property_value(sd_device *device, const char *key, co
return 0;
}
/* replaces the value if it already exists */
static int device_add_sysattr_value(sd_device *device, const char *_key, char *value) {
_cleanup_free_ char *key = NULL;
_cleanup_free_ char *value_old = NULL;
static int device_add_sysattr_value(sd_device *device, const char *key, char *value) {
_cleanup_free_ char *new_key = NULL, *old_value = NULL;
int r;
assert(device);
assert(_key);
assert(key);
r = hashmap_ensure_allocated(&device->sysattr_values, &string_hash_ops);
if (r < 0)
return r;
/* This takes the reference of the input value. The input value may be NULL.
* This replaces the value if it already exists. */
value_old = hashmap_remove2(device->sysattr_values, _key, (void **)&key);
if (!key) {
key = strdup(_key);
if (!key)
old_value = hashmap_remove2(device->sysattr_values, key, (void **) &new_key);
if (!new_key) {
new_key = strdup(key);
if (!new_key)
return -ENOMEM;
}
r = hashmap_put(device->sysattr_values, key, value);
r = hashmap_ensure_put(&device->sysattr_values, &string_hash_ops_free_free, new_key, value);
if (r < 0)
return r;
TAKE_PTR(key);
TAKE_PTR(new_key);
return 0;
}

View File

@ -462,7 +462,7 @@ int sd_rtnl_message_link_set_flags(sd_netlink_message *m, unsigned flags, unsign
assert_return(m, -EINVAL);
assert_return(m->hdr, -EINVAL);
assert_return(rtnl_message_type_is_link(m->hdr->nlmsg_type), -EINVAL);
assert_return(change, -EINVAL);
assert_return(change != 0, -EINVAL);
ifi = NLMSG_DATA(m->hdr);
@ -679,7 +679,7 @@ int sd_rtnl_message_new_addr(sd_netlink *rtnl, sd_netlink_message **ret,
}
int sd_rtnl_message_new_addr_update(sd_netlink *rtnl, sd_netlink_message **ret,
int index, int family) {
int index, int family) {
int r;
r = sd_rtnl_message_new_addr(rtnl, ret, RTM_NEWADDR, index, family);

View File

@ -3429,10 +3429,16 @@ class NetworkdLLDPTests(unittest.TestCase, Utilities):
start_networkd()
self.wait_online(['veth99:degraded', 'veth-peer:degraded'])
output = check_output(*networkctl_cmd, 'lldp', env=env)
print(output)
self.assertRegex(output, 'veth-peer')
self.assertRegex(output, 'veth99')
for trial in range(10):
if trial > 0:
time.sleep(1)
output = check_output(*networkctl_cmd, 'lldp', env=env)
print(output)
if re.search(r'veth99 .* veth-peer', output):
break
else:
self.fail()
class NetworkdRATests(unittest.TestCase, Utilities):
links = ['veth99']