Merge pull request #15378 from msekletar/udev-kill-signal

udev:  make signal that we use to kill workers on timeout configurable
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2020-06-05 16:33:14 +02:00 committed by GitHub
commit ea9f36ac83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 190 additions and 38 deletions

5
NEWS
View File

@ -100,6 +100,11 @@ CHANGES WITH 246 in spe:
can now be suspended or resumed either using new systemctl verbs,
freeze and thaw respectively, or via D-Bus.
* systemd-udevd gained new configuration option timeout_signal= as well
as coresponding kernel command line option udev.timeout_signal.
The option can be used to configure the UNIX signal that the main
daemon sends to the worker processes on timeout.
* A new sd-path.h API has been added to libsystemd. It provides a
simple API for retrieving various search paths and primary
directories for various resources.

View File

@ -271,6 +271,9 @@
<term><varname>rd.udev.exec_delay=</varname></term>
<term><varname>udev.event_timeout=</varname></term>
<term><varname>rd.udev.event_timeout=</varname></term>
<term><varname>udev.timeout_signal=</varname></term>
<term><varname>rd.udev.timeout_signal=</varname></term>
<term><varname>net.ifnames=</varname></term>
<term><varname>net.naming-scheme=</varname></term>

View File

@ -105,6 +105,21 @@
</listitem>
</varlistentry>
<varlistentry>
<term><option>-s</option></term>
<term><option>--timeout-signal=</option></term>
<listitem>
<para>Set the signal which <filename>systemd-udevd</filename> will send to
forked off processes after reaching event timeout. The setting can be overriden
at boot time with the kernel command line option
<varname>udev.timeout_signal=</varname>. Setting to <constant>SIGABRT</constant>
may be helpful in order to debug worker timeouts. Defaults to
<constant>SIGKILL</constant>. Note that setting the option on the command line
overrides the setting from the configuration file.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>-N=</option></term>
<term><option>--resolve-names=</option></term>
@ -160,6 +175,15 @@
terminated due to kernel drivers taking too long to initialize.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><varname>udev.timeout_signal=</varname></term>
<term><varname>rd.udev.timeout_signal=</varname></term>
<listitem>
<para>Specifies a signal that <filename>systemd-udevd</filename> will send to
workers on timeout. Note that kernel command line option overrides both the
setting in the configuration file and the one on the program command line.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><varname>net.ifnames=</varname></term>
<listitem>

View File

@ -97,6 +97,16 @@
<para>This is the same as the <option>--resolve-names=</option> option.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><varname>timeout_signal=</varname></term>
<listitem>
<para>Specifies a signal that <filename>systemd-udevd</filename> will send on worker
timeouts. Note that both workers and spawned processes will be killed using this
signal. Defaults to <option>SIGKILL</option>.</para>
</listitem>
</varlistentry>
</variablelist>
<para>

View File

@ -7,6 +7,7 @@
#include "env-file.h"
#include "log.h"
#include "parse-util.h"
#include "signal-util.h"
#include "string-table.h"
#include "string-util.h"
#include "udev-util.h"
@ -23,9 +24,10 @@ int udev_parse_config_full(
unsigned *ret_children_max,
usec_t *ret_exec_delay_usec,
usec_t *ret_event_timeout_usec,
ResolveNameTiming *ret_resolve_name_timing) {
ResolveNameTiming *ret_resolve_name_timing,
int *ret_timeout_signal) {
_cleanup_free_ char *log_val = NULL, *children_max = NULL, *exec_delay = NULL, *event_timeout = NULL, *resolve_names = NULL;
_cleanup_free_ char *log_val = NULL, *children_max = NULL, *exec_delay = NULL, *event_timeout = NULL, *resolve_names = NULL, *timeout_signal = NULL;
int r;
r = parse_env_file(NULL, "/etc/udev/udev.conf",
@ -33,7 +35,8 @@ int udev_parse_config_full(
"children_max", &children_max,
"exec_delay", &exec_delay,
"event_timeout", &event_timeout,
"resolve_names", &resolve_names);
"resolve_names", &resolve_names,
"timeout_signal", &timeout_signal);
if (r == -ENOENT)
return 0;
if (r < 0)
@ -65,21 +68,21 @@ int udev_parse_config_full(
r = safe_atou(children_max, ret_children_max);
if (r < 0)
log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r,
"failed to set parse children_max=%s, ignoring: %m", children_max);
"failed to parse children_max=%s, ignoring: %m", children_max);
}
if (ret_exec_delay_usec && exec_delay) {
r = parse_sec(exec_delay, ret_exec_delay_usec);
if (r < 0)
log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r,
"failed to set parse exec_delay=%s, ignoring: %m", exec_delay);
"failed to parse exec_delay=%s, ignoring: %m", exec_delay);
}
if (ret_event_timeout_usec && event_timeout) {
r = parse_sec(event_timeout, ret_event_timeout_usec);
if (r < 0)
log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r,
"failed to set parse event_timeout=%s, ignoring: %m", event_timeout);
"failed to parse event_timeout=%s, ignoring: %m", event_timeout);
}
if (ret_resolve_name_timing && resolve_names) {
@ -88,11 +91,20 @@ int udev_parse_config_full(
t = resolve_name_timing_from_string(resolve_names);
if (t < 0)
log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r,
"failed to set parse resolve_names=%s, ignoring.", resolve_names);
"failed to parse resolve_names=%s, ignoring.", resolve_names);
else
*ret_resolve_name_timing = t;
}
if (ret_timeout_signal && timeout_signal) {
r = signal_from_string(timeout_signal);
if (r < 0)
log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r,
"failed to parse timeout_signal=%s, ignoring: %m", timeout_signal);
else
*ret_timeout_signal = r;
}
return 0;
}

View File

@ -21,10 +21,11 @@ int udev_parse_config_full(
unsigned *ret_children_max,
usec_t *ret_exec_delay_usec,
usec_t *ret_event_timeout_usec,
ResolveNameTiming *ret_resolve_name_timing);
ResolveNameTiming *ret_resolve_name_timing,
int *ret_timeout_signal);
static inline int udev_parse_config(void) {
return udev_parse_config_full(NULL, NULL, NULL, NULL);
return udev_parse_config_full(NULL, NULL, NULL, NULL, NULL);
}
int device_wait_for_initialization(sd_device *device, const char *subsystem, usec_t timeout, sd_device **ret);

View File

@ -122,8 +122,8 @@ static int run(int argc, char *argv[]) {
}
}
udev_event_execute_rules(event, 3 * USEC_PER_SEC, NULL, rules);
udev_event_execute_run(event, 3 * USEC_PER_SEC);
udev_event_execute_rules(event, 3 * USEC_PER_SEC, SIGKILL, NULL, rules);
udev_event_execute_run(event, 3 * USEC_PER_SEC, SIGKILL);
return 0;
}

View File

@ -41,6 +41,7 @@ typedef struct Spawn {
pid_t pid;
usec_t timeout_warn_usec;
usec_t timeout_usec;
int timeout_signal;
usec_t event_birth_usec;
bool accept_failure;
int fd_stdout;
@ -596,7 +597,7 @@ static int on_spawn_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
assert(spawn);
kill_and_sigcont(spawn->pid, SIGKILL);
kill_and_sigcont(spawn->pid, spawn->timeout_signal);
log_device_error(spawn->device, "Spawned process '%s' ["PID_FMT"] timed out after %s, killing",
spawn->cmd, spawn->pid,
@ -717,6 +718,7 @@ static int spawn_wait(Spawn *spawn) {
int udev_event_spawn(UdevEvent *event,
usec_t timeout_usec,
int timeout_signal,
bool accept_failure,
const char *cmd,
char *result, size_t ressize) {
@ -793,6 +795,7 @@ int udev_event_spawn(UdevEvent *event,
.accept_failure = accept_failure,
.timeout_warn_usec = udev_warn_timeout(timeout_usec),
.timeout_usec = timeout_usec,
.timeout_signal = timeout_signal,
.event_birth_usec = event->birth_usec,
.fd_stdout = outpipe[READ_END],
.fd_stderr = errpipe[READ_END],
@ -896,6 +899,7 @@ static int update_devnode(UdevEvent *event) {
static void event_execute_rules_on_remove(
UdevEvent *event,
usec_t timeout_usec,
int timeout_signal,
Hashmap *properties_list,
UdevRules *rules) {
@ -917,7 +921,7 @@ static void event_execute_rules_on_remove(
if (sd_device_get_devnum(dev, NULL) >= 0)
(void) udev_watch_end(dev);
(void) udev_rules_apply_to_event(rules, event, timeout_usec, properties_list);
(void) udev_rules_apply_to_event(rules, event, timeout_usec, timeout_signal, properties_list);
if (sd_device_get_devnum(dev, NULL) >= 0)
(void) udev_node_remove(dev);
@ -944,6 +948,7 @@ static int udev_event_on_move(UdevEvent *event) {
int udev_event_execute_rules(UdevEvent *event,
usec_t timeout_usec,
int timeout_signal,
Hashmap *properties_list,
UdevRules *rules) {
const char *subsystem;
@ -965,7 +970,7 @@ int udev_event_execute_rules(UdevEvent *event,
return log_device_error_errno(dev, r, "Failed to get ACTION: %m");
if (action == DEVICE_ACTION_REMOVE) {
event_execute_rules_on_remove(event, timeout_usec, properties_list, rules);
event_execute_rules_on_remove(event, timeout_usec, timeout_signal, properties_list, rules);
return 0;
}
@ -983,7 +988,7 @@ int udev_event_execute_rules(UdevEvent *event,
return r;
}
r = udev_rules_apply_to_event(rules, event, timeout_usec, properties_list);
r = udev_rules_apply_to_event(rules, event, timeout_usec, timeout_signal, properties_list);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to apply udev rules: %m");
@ -1016,7 +1021,7 @@ int udev_event_execute_rules(UdevEvent *event,
return 0;
}
void udev_event_execute_run(UdevEvent *event, usec_t timeout_usec) {
void udev_event_execute_run(UdevEvent *event, usec_t timeout_usec, int timeout_signal) {
const char *command;
void *val;
Iterator i;
@ -1040,7 +1045,8 @@ void udev_event_execute_run(UdevEvent *event, usec_t timeout_usec) {
}
log_device_debug(event->dev, "Running command \"%s\"", command);
r = udev_event_spawn(event, timeout_usec, false, command, NULL, 0);
r = udev_event_spawn(event, timeout_usec, timeout_signal, false, command, NULL, 0);
if (r < 0)
log_device_warning_errno(event->dev, r, "Failed to execute '%s', ignoring: %m", command);
else if (r > 0) /* returned value is positive when program fails */

View File

@ -54,13 +54,15 @@ ssize_t udev_event_apply_format(UdevEvent *event,
int udev_check_format(const char *value, size_t *offset, const char **hint);
int udev_event_spawn(UdevEvent *event,
usec_t timeout_usec,
int timeout_signal,
bool accept_failure,
const char *cmd, char *result, size_t ressize);
int udev_event_execute_rules(UdevEvent *event,
usec_t timeout_usec,
int timeout_signal,
Hashmap *properties_list,
UdevRules *rules);
void udev_event_execute_run(UdevEvent *event, usec_t timeout_usec);
void udev_event_execute_run(UdevEvent *event, usec_t timeout_usec, int timeout_signal);
static inline usec_t udev_warn_timeout(usec_t timeout_usec) {
return DIV_ROUND_UP(timeout_usec, 3);

View File

@ -1520,6 +1520,7 @@ static int udev_rule_apply_token_to_event(
sd_device *dev,
UdevEvent *event,
usec_t timeout_usec,
int timeout_signal,
Hashmap *properties_list) {
UdevRuleToken *token;
@ -1662,7 +1663,7 @@ static int udev_rule_apply_token_to_event(
(void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false);
log_rule_debug(dev, rules, "Running PROGRAM '%s'", buf);
r = udev_event_spawn(event, timeout_usec, true, buf, result, sizeof(result));
r = udev_event_spawn(event, timeout_usec, timeout_signal, true, buf, result, sizeof(result));
if (r != 0) {
if (r < 0)
log_rule_warning_errno(dev, rules, r, "Failed to execute '%s', ignoring: %m", buf);
@ -1732,7 +1733,7 @@ static int udev_rule_apply_token_to_event(
(void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false);
log_rule_debug(dev, rules, "Importing properties from results of '%s'", buf);
r = udev_event_spawn(event, timeout_usec, true, buf, result, sizeof result);
r = udev_event_spawn(event, timeout_usec, timeout_signal, true, buf, result, sizeof result);
if (r != 0) {
if (r < 0)
log_rule_warning_errno(dev, rules, r, "Failed to execute '%s', ignoring: %m", buf);
@ -2162,7 +2163,8 @@ static bool token_is_for_parents(UdevRuleToken *token) {
static int udev_rule_apply_parent_token_to_event(
UdevRules *rules,
UdevEvent *event) {
UdevEvent *event,
int timeout_signal) {
UdevRuleLine *line;
UdevRuleToken *head;
@ -2175,7 +2177,7 @@ static int udev_rule_apply_parent_token_to_event(
LIST_FOREACH(tokens, line->current_token, head) {
if (!token_is_for_parents(line->current_token))
return true; /* All parent tokens match. */
r = udev_rule_apply_token_to_event(rules, event->dev_parent, event, 0, NULL);
r = udev_rule_apply_token_to_event(rules, event->dev_parent, event, 0, timeout_signal, NULL);
if (r < 0)
return r;
if (r == 0)
@ -2196,6 +2198,7 @@ static int udev_rule_apply_line_to_event(
UdevRules *rules,
UdevEvent *event,
usec_t timeout_usec,
int timeout_signal,
Hashmap *properties_list,
UdevRuleLine **next_line) {
@ -2229,7 +2232,7 @@ static int udev_rule_apply_line_to_event(
if (parents_done)
continue;
r = udev_rule_apply_parent_token_to_event(rules, event);
r = udev_rule_apply_parent_token_to_event(rules, event, timeout_signal);
if (r <= 0)
return r;
@ -2237,7 +2240,7 @@ static int udev_rule_apply_line_to_event(
continue;
}
r = udev_rule_apply_token_to_event(rules, event->dev, event, timeout_usec, properties_list);
r = udev_rule_apply_token_to_event(rules, event->dev, event, timeout_usec, timeout_signal, properties_list);
if (r <= 0)
return r;
}
@ -2252,6 +2255,7 @@ int udev_rules_apply_to_event(
UdevRules *rules,
UdevEvent *event,
usec_t timeout_usec,
int timeout_signal,
Hashmap *properties_list) {
UdevRuleFile *file;
@ -2264,7 +2268,7 @@ int udev_rules_apply_to_event(
LIST_FOREACH(rule_files, file, rules->rule_files) {
rules->current_file = file;
LIST_FOREACH_SAFE(rule_lines, file->current_line, next_line, file->rule_lines) {
r = udev_rule_apply_line_to_event(rules, event, timeout_usec, properties_list, &next_line);
r = udev_rule_apply_line_to_event(rules, event, timeout_usec, timeout_signal, properties_list, &next_line);
if (r < 0)
return r;
}

View File

@ -23,5 +23,6 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(UdevRules*, udev_rules_free);
bool udev_rules_check_timestamp(UdevRules *rules);
int udev_rules_apply_to_event(UdevRules *rules, UdevEvent *event,
usec_t timeout_usec,
int timeout_signal,
Hashmap *properties_list);
int udev_rules_apply_static_dev_perms(UdevRules *rules);

View File

@ -7,4 +7,5 @@
#children_max=
#exec_delay=
#event_timeout=180
#timeout_signal=SIGKILL
#resolve_names=early

View File

@ -143,7 +143,7 @@ int test_main(int argc, char *argv[], void *userdata) {
assert_se(sigfillset(&mask) >= 0);
assert_se(sigprocmask(SIG_SETMASK, &mask, &sigmask_orig) >= 0);
udev_event_execute_rules(event, 60 * USEC_PER_SEC, NULL, rules);
udev_event_execute_rules(event, 60 * USEC_PER_SEC, SIGKILL, NULL, rules);
FOREACH_DEVICE_PROPERTY(dev, key, value)
printf("%s=%s\n", key, value);

View File

@ -75,6 +75,7 @@ static ResolveNameTiming arg_resolve_name_timing = RESOLVE_NAME_EARLY;
static unsigned arg_children_max = 0;
static usec_t arg_exec_delay_usec = 0;
static usec_t arg_event_timeout_usec = 180 * USEC_PER_SEC;
static int arg_timeout_signal = SIGKILL;
typedef struct Manager {
sd_event *event;
@ -228,7 +229,7 @@ static int on_event_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
assert(event);
assert(event->worker);
kill_and_sigcont(event->worker->pid, SIGKILL);
kill_and_sigcont(event->worker->pid, arg_timeout_signal);
event->worker->state = WORKER_KILLED;
log_device_error(event->dev, "Worker ["PID_FMT"] processing SEQNUM=%"PRIu64" killed", event->worker->pid, event->seqnum);
@ -412,11 +413,11 @@ static int worker_process_device(Manager *manager, sd_device *dev) {
return r;
/* apply rules, create node, symlinks */
r = udev_event_execute_rules(udev_event, arg_event_timeout_usec, manager->properties, manager->rules);
r = udev_event_execute_rules(udev_event, arg_event_timeout_usec, arg_timeout_signal, manager->properties, manager->rules);
if (r < 0)
return r;
udev_event_execute_run(udev_event, arg_event_timeout_usec);
udev_event_execute_run(udev_event, arg_event_timeout_usec, arg_timeout_signal);
if (!manager->rtnl)
/* in case rtnl was initialized */
@ -1455,6 +1456,13 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
r = parse_sec(value, &arg_exec_delay_usec);
} else if (proc_cmdline_key_streq(key, "udev.timeout_signal")) {
if (proc_cmdline_value_missing(key, value))
return 0;
r = signal_from_string(value);
if (r > 0)
arg_timeout_signal = r;
} else if (startswith(key, "udev."))
log_warning("Unknown udev kernel command line option \"%s\", ignoring", key);
@ -1492,15 +1500,20 @@ static int help(void) {
}
static int parse_argv(int argc, char *argv[]) {
enum {
ARG_TIMEOUT_SIGNAL,
};
static const struct option options[] = {
{ "daemon", no_argument, NULL, 'd' },
{ "debug", no_argument, NULL, 'D' },
{ "children-max", required_argument, NULL, 'c' },
{ "exec-delay", required_argument, NULL, 'e' },
{ "event-timeout", required_argument, NULL, 't' },
{ "resolve-names", required_argument, NULL, 'N' },
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'V' },
{ "daemon", no_argument, NULL, 'd' },
{ "debug", no_argument, NULL, 'D' },
{ "children-max", required_argument, NULL, 'c' },
{ "exec-delay", required_argument, NULL, 'e' },
{ "event-timeout", required_argument, NULL, 't' },
{ "resolve-names", required_argument, NULL, 'N' },
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'V' },
{ "timeout-signal", required_argument, NULL, ARG_TIMEOUT_SIGNAL },
{}
};
@ -1525,6 +1538,14 @@ static int parse_argv(int argc, char *argv[]) {
if (r < 0)
log_warning_errno(r, "Failed to parse --exec-delay= value '%s', ignoring: %m", optarg);
break;
case ARG_TIMEOUT_SIGNAL:
r = signal_from_string(optarg);
if (r <= 0)
log_warning_errno(r, "Failed to parse --timeout-signal= value '%s', ignoring: %m", optarg);
else
arg_timeout_signal = r;
break;
case 't':
r = parse_sec(optarg, &arg_event_timeout_usec);
if (r < 0)
@ -1722,7 +1743,7 @@ int run_udevd(int argc, char *argv[]) {
log_set_target(LOG_TARGET_AUTO);
log_open();
udev_parse_config_full(&arg_children_max, &arg_exec_delay_usec, &arg_event_timeout_usec, &arg_resolve_name_timing);
udev_parse_config_full(&arg_children_max, &arg_exec_delay_usec, &arg_event_timeout_usec, &arg_resolve_name_timing, &arg_timeout_signal);
log_parse_environment();
log_open(); /* Done again to update after reading configuration. */

View File

@ -0,0 +1 @@
../TEST-01-BASIC/Makefile

View File

@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -e
TEST_DESCRIPTION="test udev's event-timeout and timeout-signal options"
TEST_NO_NSPAWN=1
. $TEST_BASE_DIR/test-functions
do_test "$@" 49

View File

@ -0,0 +1,6 @@
[Unit]
Description=TEST-49-UDEV-EVENT-TIMEOUT
[Service]
ExecStart=/usr/lib/systemd/tests/testdata/units/%N.sh
Type=oneshot

47
test/units/testsuite-49.sh Executable file
View File

@ -0,0 +1,47 @@
#!/usr/bin/env bash
set -ex
test_rule="/run/udev/rules.d/49-test.rules"
setup() {
mkdir -p "${test_rule%/*}"
cp -f /etc/udev/udev.conf /etc/udev/udev.conf.bckp
echo 'KERNEL=="lo", SUBSYSTEM=="net", PROGRAM=="/bin/sleep 60"' > "${test_rule}"
echo "event_timeout=30" >> /etc/udev/udev.conf
echo "timeout_signal=SIGABRT" >> /etc/udev/udev.conf
systemctl restart systemd-udevd.service
}
teardown() {
set +e
mv -f /etc/udev/udev.conf.bckp /etc/udev/udev.conf
rm -f "$test_rule"
systemctl restart systemd-udevd.service
}
run_test() {
since="$(date +%T)"
echo add > /sys/class/net/lo/uevent
for n in {1..20}; do
sleep 5
if coredumpctl --since "$since" --no-legend --no-pager | grep /bin/udevadm ; then
return 0
fi
done
return 1
}
trap teardown EXIT
setup
run_test
echo OK > /testok
exit 0