1
0
mirror of https://github.com/systemd/systemd.git synced 2025-03-24 14:50:17 +03:00

Merge pull request #23991 from yuwata/udev-reload

udev: reload rules and builtins only when mtime of a config changed
This commit is contained in:
Lennart Poettering 2022-07-25 11:20:08 +02:00 committed by GitHub
commit 8f304b991c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 159 additions and 85 deletions

View File

@ -445,7 +445,7 @@ int config_parse(
return 1;
}
static int hashmap_put_stats_by_path(Hashmap **stats_by_path, const char *path, const struct stat *st) {
int hashmap_put_stats_by_path(Hashmap **stats_by_path, const char *path, const struct stat *st) {
_cleanup_free_ struct stat *st_copy = NULL;
_cleanup_free_ char *path_copy = NULL;
int r;
@ -605,22 +605,19 @@ int config_parse_many(
return config_parse_many_files(conf_files, files, sections, lookup, table, flags, userdata, ret_stats_by_path);
}
static int config_get_stats_by_path_one(
static int dropins_get_stats_by_path(
const char* conf_file,
const char* const* conf_file_dirs,
Hashmap *stats_by_path) {
Hashmap **stats_by_path) {
_cleanup_strv_free_ char **files = NULL;
_cleanup_free_ char *dropin_dirname = NULL;
struct stat st;
int r;
assert(conf_file);
assert(conf_file_dirs);
assert(stats_by_path);
/* Unlike config_parse(), this does not support stream. */
r = path_extract_filename(conf_file, &dropin_dirname);
if (r < 0)
return r;
@ -634,17 +631,9 @@ static int config_get_stats_by_path_one(
if (r < 0)
return r;
/* First read the main config file. */
r = RET_NERRNO(stat(conf_file, &st));
if (r >= 0) {
r = hashmap_put_stats_by_path(&stats_by_path, conf_file, &st);
if (r < 0)
return r;
} else if (r != -ENOENT)
return r;
/* Then read all the drop-ins. */
STRV_FOREACH(fn, files) {
struct stat st;
if (stat(*fn, &st) < 0) {
if (errno == ENOENT)
continue;
@ -652,7 +641,7 @@ static int config_get_stats_by_path_one(
return -errno;
}
r = hashmap_put_stats_by_path(&stats_by_path, *fn, &st);
r = hashmap_put_stats_by_path(stats_by_path, *fn, &st);
if (r < 0)
return r;
}
@ -665,6 +654,7 @@ int config_get_stats_by_path(
const char *root,
unsigned flags,
const char* const* dirs,
bool check_dropins,
Hashmap **ret) {
_cleanup_hashmap_free_ Hashmap *stats_by_path = NULL;
@ -675,16 +665,32 @@ int config_get_stats_by_path(
assert(dirs);
assert(ret);
/* Unlike config_parse(), this does not support stream. */
r = conf_files_list_strv(&files, suffix, root, flags, dirs);
if (r < 0)
return r;
stats_by_path = hashmap_new(&path_hash_ops_free_free);
if (!stats_by_path)
return -ENOMEM;
STRV_FOREACH(f, files) {
r = config_get_stats_by_path_one(*f, dirs, stats_by_path);
struct stat st;
/* First read the main config file. */
if (stat(*f, &st) < 0) {
if (errno == ENOENT)
continue;
return -errno;
}
r = hashmap_put_stats_by_path(&stats_by_path, *f, &st);
if (r < 0)
return r;
if (!check_dropins)
continue;
/* Then read all the drop-ins if requested. */
r = dropins_get_stats_by_path(*f, dirs, &stats_by_path);
if (r < 0)
return r;
}

View File

@ -119,8 +119,10 @@ int config_get_stats_by_path(
const char *root,
unsigned flags,
const char* const* dirs,
bool check_dropins,
Hashmap **ret);
int hashmap_put_stats_by_path(Hashmap **stats_by_path, const char *path, const struct stat *st);
bool stats_by_path_equal(Hashmap *a, Hashmap *b);
typedef struct ConfigSection {

View File

@ -347,9 +347,9 @@ bool link_config_should_reload(LinkConfigContext *ctx) {
assert(ctx);
r = config_get_stats_by_path(".link", NULL, 0, NETWORK_DIRS, &stats_by_path);
r = config_get_stats_by_path(".link", NULL, 0, NETWORK_DIRS, /* check_dropins = */ true, &stats_by_path);
if (r < 0) {
log_warning_errno(r, "Failed to get stats of .link files: %m");
log_warning_errno(r, "Failed to get stats of .link files, ignoring: %m");
return true;
}

View File

@ -208,7 +208,12 @@ static void builtin_hwdb_exit(void) {
/* called every couple of seconds during event activity; 'true' if config has changed */
static bool builtin_hwdb_validate(void) {
return hwdb_validate(hwdb);
if (hwdb_validate(hwdb)) {
log_debug("hwdb needs reloading.");
return true;
}
return false;
}
const UdevBuiltin udev_builtin_hwdb = {

View File

@ -43,7 +43,7 @@ static int builtin_kmod_init(void) {
if (!ctx)
return -ENOMEM;
log_debug("Load module index");
log_debug("Loading kernel module index.");
kmod_set_log_fn(ctx, udev_kmod_log, NULL);
kmod_load_resources(ctx);
return 0;
@ -51,16 +51,21 @@ static int builtin_kmod_init(void) {
/* called on udev shutdown and reload request */
static void builtin_kmod_exit(void) {
log_debug("Unload module index");
log_debug("Unload kernel module index.");
ctx = kmod_unref(ctx);
}
/* called every couple of seconds during event activity; 'true' if config has changed */
static bool builtin_kmod_validate(void) {
log_debug("Validate module index");
if (!ctx)
return false;
return (kmod_validate_resources(ctx) != KMOD_RESOURCES_OK);
if (kmod_validate_resources(ctx) != KMOD_RESOURCES_OK) {
log_debug("Kernel module index needs reloading.");
return true;
}
return false;
}
const UdevBuiltin udev_builtin_kmod = {

View File

@ -75,11 +75,15 @@ static void builtin_net_setup_link_exit(void) {
}
static bool builtin_net_setup_link_validate(void) {
log_debug("Check if link configuration needs reloading.");
if (!ctx)
return false;
return link_config_should_reload(ctx);
if (link_config_should_reload(ctx)) {
log_debug("Link configuration context needs reloading.");
return true;
}
return false;
}
const UdevBuiltin udev_builtin_net_setup_link = {

View File

@ -5,6 +5,7 @@
#include "alloc-util.h"
#include "architecture.h"
#include "conf-files.h"
#include "conf-parser.h"
#include "def.h"
#include "device-private.h"
#include "device-util.h"
@ -177,10 +178,10 @@ struct UdevRuleFile {
};
struct UdevRules {
usec_t dirs_ts_usec;
ResolveNameTiming resolve_name_timing;
Hashmap *known_users;
Hashmap *known_groups;
Hashmap *stats_by_path;
UdevRuleFile *current_file;
LIST_HEAD(UdevRuleFile, rule_files);
};
@ -315,6 +316,7 @@ UdevRules *udev_rules_free(UdevRules *rules) {
hashmap_free_free_key(rules->known_users);
hashmap_free_free_key(rules->known_groups);
hashmap_free(rules->stats_by_path);
return mfree(rules);
}
@ -1176,6 +1178,7 @@ int udev_rules_parse_file(UdevRules *rules, const char *filename) {
UdevRuleFile *rule_file;
bool ignore_line = false;
unsigned line_nr = 0;
struct stat st;
int r;
f = fopen(filename, "re");
@ -1183,16 +1186,23 @@ int udev_rules_parse_file(UdevRules *rules, const char *filename) {
if (errno == ENOENT)
return 0;
return -errno;
return log_warning_errno(errno, "Failed to open %s, ignoring: %m", filename);
}
(void) fd_warn_permissions(filename, fileno(f));
if (fstat(fileno(f), &st) < 0)
return log_warning_errno(errno, "Failed to stat %s, ignoring: %m", filename);
if (null_or_empty_fd(fileno(f))) {
if (null_or_empty(&st)) {
log_debug("Skipping empty file: %s", filename);
return 0;
}
r = hashmap_put_stats_by_path(&rules->stats_by_path, filename, &st);
if (r < 0)
return log_warning_errno(errno, "Failed to save stat for %s, ignoring: %m", filename);
(void) fd_warn_permissions(filename, fileno(f));
log_debug("Reading rules file: %s", filename);
name = strdup(filename);
@ -1296,8 +1306,6 @@ int udev_rules_load(UdevRules **ret_rules, ResolveNameTiming resolve_name_timing
if (!rules)
return -ENOMEM;
(void) udev_rules_check_timestamp(rules);
r = conf_files_list_strv(&files, ".rules", NULL, 0, RULES_DIRS);
if (r < 0)
return log_debug_errno(r, "Failed to enumerate rules files: %m");
@ -1312,11 +1320,25 @@ int udev_rules_load(UdevRules **ret_rules, ResolveNameTiming resolve_name_timing
return 0;
}
bool udev_rules_check_timestamp(UdevRules *rules) {
if (!rules)
return false;
bool udev_rules_should_reload(UdevRules *rules) {
_cleanup_hashmap_free_ Hashmap *stats_by_path = NULL;
int r;
return paths_check_timestamp(RULES_DIRS, &rules->dirs_ts_usec, true);
if (!rules)
return true;
r = config_get_stats_by_path(".rules", NULL, 0, RULES_DIRS, /* check_dropins = */ false, &stats_by_path);
if (r < 0) {
log_warning_errno(r, "Failed to get stats of udev rules, ignoring: %m");
return true;
}
if (!stats_by_path_equal(rules->stats_by_path, stats_by_path)) {
log_debug("Udev rules need reloading");
return true;
}
return false;
}
static bool token_match_string(UdevRuleToken *token, const char *str) {

View File

@ -1,6 +1,7 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
#pragma once
#include "alloc-util.h"
#include "hashmap.h"
#include "time-util.h"
#include "udev-util.h"
@ -21,8 +22,9 @@ UdevRules* udev_rules_new(ResolveNameTiming resolve_name_timing);
int udev_rules_load(UdevRules **ret_rules, ResolveNameTiming resolve_name_timing);
UdevRules *udev_rules_free(UdevRules *rules);
DEFINE_TRIVIAL_CLEANUP_FUNC(UdevRules*, udev_rules_free);
#define udev_rules_free_and_replace(a, b) free_and_replace_full(a, b, udev_rules_free)
bool udev_rules_check_timestamp(UdevRules *rules);
bool udev_rules_should_reload(UdevRules *rules);
int udev_rules_apply_to_event(UdevRules *rules, UdevEvent *event,
usec_t timeout_usec,
int timeout_signal,

View File

@ -351,16 +351,40 @@ static void notify_ready(void) {
}
/* reload requested, HUP signal received, rules changed, builtin changed */
static void manager_reload(Manager *manager) {
static void manager_reload(Manager *manager, bool force) {
_cleanup_(udev_rules_freep) UdevRules *rules = NULL;
usec_t now_usec;
int r;
assert(manager);
assert_se(sd_event_now(manager->event, CLOCK_MONOTONIC, &now_usec) >= 0);
if (!force && now_usec < usec_add(manager->last_usec, 3 * USEC_PER_SEC))
/* check for changed config, every 3 seconds at most */
return;
manager->last_usec = now_usec;
/* Reload SELinux label database, to make the child inherit the up-to-date database. */
mac_selinux_maybe_reload();
/* Nothing changed. It is not necessary to reload. */
if (!udev_rules_should_reload(manager->rules) && !udev_builtin_validate())
return;
sd_notify(false,
"RELOADING=1\n"
"STATUS=Flushing configuration...");
manager_kill_workers(manager, false);
manager->rules = udev_rules_free(manager->rules);
udev_builtin_exit();
udev_builtin_init();
r = udev_rules_load(&rules, arg_resolve_name_timing);
if (r < 0)
log_warning_errno(r, "Failed to read udev rules, using the previously loaded rules, ignoring: %m");
else
udev_rules_free_and_replace(manager->rules, rules);
notify_ready();
}
@ -937,7 +961,6 @@ no_blocker:
}
static int event_queue_start(Manager *manager) {
usec_t usec;
int r;
assert(manager);
@ -945,32 +968,11 @@ static int event_queue_start(Manager *manager) {
if (!manager->events || manager->exit || manager->stop_exec_queue)
return 0;
assert_se(sd_event_now(manager->event, CLOCK_MONOTONIC, &usec) >= 0);
/* check for changed config, every 3 seconds at most */
if (manager->last_usec == 0 ||
usec > usec_add(manager->last_usec, 3 * USEC_PER_SEC)) {
if (udev_rules_check_timestamp(manager->rules) ||
udev_builtin_validate())
manager_reload(manager);
manager->last_usec = usec;
}
r = event_source_disable(manager->kill_workers_event);
if (r < 0)
log_warning_errno(r, "Failed to disable event source for cleaning up idle workers, ignoring: %m");
udev_builtin_init();
if (!manager->rules) {
r = udev_rules_load(&manager->rules, arg_resolve_name_timing);
if (r < 0)
return log_warning_errno(r, "Failed to read udev rules: %m");
}
/* fork with up-to-date SELinux label database, so the child inherits the up-to-date db
* and, until the next SELinux policy changes, we safe further reloads in future children */
mac_selinux_maybe_reload();
manager_reload(manager, /* force = */ false);
LIST_FOREACH(event, event, manager->events) {
if (event->state != EVENT_QUEUED)
@ -1248,11 +1250,11 @@ static int on_ctrl_msg(UdevCtrl *uctrl, UdevCtrlMessageType type, const UdevCtrl
case UDEV_CTRL_START_EXEC_QUEUE:
log_debug("Received udev control message (START_EXEC_QUEUE)");
manager->stop_exec_queue = false;
event_queue_start(manager);
/* It is not necessary to call event_queue_start() here, as it will be called in on_post() if necessary. */
break;
case UDEV_CTRL_RELOAD:
log_debug("Received udev control message (RELOAD)");
manager_reload(manager);
manager_reload(manager, /* force = */ true);
break;
case UDEV_CTRL_SET_ENV: {
_unused_ _cleanup_free_ char *old_val = NULL;
@ -1501,7 +1503,7 @@ static int on_sighup(sd_event_source *s, const struct signalfd_siginfo *si, void
assert(manager);
manager_reload(manager);
manager_reload(manager, /* force = */ true);
return 1;
}
@ -1585,9 +1587,10 @@ static int on_post(sd_event_source *s, void *userdata) {
if (!hashmap_isempty(manager->workers)) {
/* There are idle workers */
(void) event_reset_time(manager->event, &manager->kill_workers_event, CLOCK_MONOTONIC,
now(CLOCK_MONOTONIC) + 3 * USEC_PER_SEC, USEC_PER_SEC,
on_kill_workers_event, manager, 0, "kill-workers-event", false);
(void) event_reset_time_relative(manager->event, &manager->kill_workers_event,
CLOCK_MONOTONIC, 3 * USEC_PER_SEC, USEC_PER_SEC,
on_kill_workers_event, manager,
0, "kill-workers-event", false);
return 1;
}
@ -2010,15 +2013,17 @@ static int main_loop(Manager *manager) {
if (r < 0)
return log_error_errno(r, "Failed to create post event source: %m");
manager->last_usec = now(CLOCK_MONOTONIC);
udev_builtin_init();
r = udev_rules_load(&manager->rules, arg_resolve_name_timing);
if (!manager->rules)
if (r < 0)
return log_error_errno(r, "Failed to read udev rules: %m");
r = udev_rules_apply_static_dev_perms(manager->rules);
if (r < 0)
log_error_errno(r, "Failed to apply permissions on static device nodes: %m");
log_warning_errno(r, "Failed to apply permissions on static device nodes, ignoring: %m");
notify_ready();

View File

@ -276,6 +276,9 @@ def expectedFailureIfFQPIEIsNotAvailable():
return f
def udev_reload():
check_output('udevadm control --reload')
def copy_network_unit(*units, copy_dropins=True):
"""
Copy networkd unit files into the testbed.
@ -287,31 +290,57 @@ def copy_network_unit(*units, copy_dropins=True):
When a drop-in file is specified, its unit file is also copied in automatically.
"""
has_link = False
mkdir_p(network_unit_dir)
for unit in units:
if copy_dropins and os.path.exists(os.path.join(networkd_ci_temp_dir, unit + '.d')):
cp_r(os.path.join(networkd_ci_temp_dir, unit + '.d'), os.path.join(network_unit_dir, unit + '.d'))
if unit.endswith('.conf'):
dropin = unit
unit = os.path.dirname(dropin).rstrip('.d')
dropindir = os.path.join(network_unit_dir, unit + '.d')
mkdir_p(dropindir)
cp(os.path.join(networkd_ci_temp_dir, dropin), dropindir)
cp(os.path.join(networkd_ci_temp_dir, unit), network_unit_dir)
if unit.endswith('.link'):
has_link = True
if has_link:
udev_reload()
def remove_network_unit(*units):
"""
Remove previously copied unit files from the testbed.
Drop-ins will be removed automatically.
"""
has_link = False
for unit in units:
rm_f(os.path.join(network_unit_dir, unit))
rm_rf(os.path.join(network_unit_dir, unit + '.d'))
if unit.endswith('.link') or unit.endswith('.link.d'):
has_link = True
if has_link:
udev_reload()
def clear_network_units():
has_link = False
if os.path.exists(network_unit_dir):
units = os.listdir(network_unit_dir)
for unit in units:
if unit.endswith('.link') or unit.endswith('.link.d'):
has_link = True
rm_rf(network_unit_dir)
if has_link:
udev_reload()
def copy_networkd_conf_dropin(*dropins):
"""Copy networkd.conf dropin files into the testbed."""
mkdir_p(networkd_conf_dropin_dir)
@ -896,7 +925,6 @@ class NetworkctlTests(unittest.TestCase, Utilities):
@expectedFailureIfAlternativeNameIsNotAvailable()
def test_altname(self):
copy_network_unit('26-netdev-link-local-addressing-yes.network', '12-dummy.netdev', '12-dummy.link')
check_output('udevadm control --reload')
start_networkd()
self.wait_online(['dummy98:degraded'])
@ -3891,7 +3919,6 @@ class NetworkdSRIOVTests(unittest.TestCase, Utilities):
@expectedFailureIfNetdevsimWithSRIOVIsNotAvailable()
def test_sriov_udev(self):
copy_network_unit('25-sriov.link', '25-sriov-udev.network')
call('udevadm control --reload')
call('modprobe netdevsim')
@ -3914,7 +3941,7 @@ class NetworkdSRIOVTests(unittest.TestCase, Utilities):
with open(os.path.join(network_unit_dir, '25-sriov.link'), mode='a', encoding='utf-8') as f:
f.write('[Link]\nSR-IOVVirtualFunctions=4\n')
call('udevadm control --reload')
udev_reload()
call('udevadm trigger --action add --settle /sys/devices/netdevsim99/net/eni99np1')
output = check_output('ip link show dev eni99np1')
@ -3930,7 +3957,7 @@ class NetworkdSRIOVTests(unittest.TestCase, Utilities):
with open(os.path.join(network_unit_dir, '25-sriov.link'), mode='a', encoding='utf-8') as f:
f.write('[Link]\nSR-IOVVirtualFunctions=\n')
call('udevadm control --reload')
udev_reload()
call('udevadm trigger --action add --settle /sys/devices/netdevsim99/net/eni99np1')
output = check_output('ip link show dev eni99np1')
@ -3946,7 +3973,7 @@ class NetworkdSRIOVTests(unittest.TestCase, Utilities):
with open(os.path.join(network_unit_dir, '25-sriov.link'), mode='a', encoding='utf-8') as f:
f.write('[Link]\nSR-IOVVirtualFunctions=2\n')
call('udevadm control --reload')
udev_reload()
call('udevadm trigger --action add --settle /sys/devices/netdevsim99/net/eni99np1')
output = check_output('ip link show dev eni99np1')
@ -3962,7 +3989,7 @@ class NetworkdSRIOVTests(unittest.TestCase, Utilities):
with open(os.path.join(network_unit_dir, '25-sriov.link'), mode='a', encoding='utf-8') as f:
f.write('[Link]\nSR-IOVVirtualFunctions=\n')
call('udevadm control --reload')
udev_reload()
call('udevadm trigger --action add --settle /sys/devices/netdevsim99/net/eni99np1')
output = check_output('ip link show dev eni99np1')
@ -5236,8 +5263,6 @@ class NetworkdMTUTests(unittest.TestCase, Utilities):
def test_mtu_link(self):
copy_network_unit('12-dummy.netdev', '12-dummy-mtu.link', '12-dummy.network', copy_dropins=False)
# must reload udev because it only picks up new files after 3 second delay
call('udevadm control --reload')
# note - MTU set by .link happens ONLY at udev processing of device 'add' uevent!
self.check_mtu('1600', reset=False)
@ -5264,8 +5289,6 @@ class NetworkdMTUTests(unittest.TestCase, Utilities):
def test_mtu_link_ipv6_mtu(self):
''' set ipv6 mtu and set device mtu via link file '''
copy_network_unit('12-dummy.netdev', '12-dummy-mtu.link', '12-dummy.network.d/ipv6-mtu-1550.conf')
# must reload udev because it only picks up new files after 3 second delay
call('udevadm control --reload')
self.check_mtu('1600', '1550', reset=False)