From 856db27d970f6ce663f91621508fb8adb13a7989 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 6 Apr 2025 01:25:53 +0900 Subject: [PATCH] udev: move extra_timeout_usec() to udev-config.c Then, introduce manager_kill_worker_timeout() helper function. No functional change, just refactoring. --- src/udev/udev-config.c | 38 ++++++++++++++++++++++++++++++++++++++ src/udev/udev-config.h | 2 ++ src/udev/udev-manager.c | 34 +--------------------------------- 3 files changed, 41 insertions(+), 33 deletions(-) diff --git a/src/udev/udev-config.c b/src/udev/udev-config.c index 6562b34d2f8..feca748f9ec 100644 --- a/src/udev/udev-config.c +++ b/src/udev/udev-config.c @@ -483,3 +483,41 @@ UdevReloadFlags manager_reload_config(Manager *manager) { return 0; } + +static usec_t extra_timeout_usec(void) { + static usec_t saved = 10 * USEC_PER_SEC; + static bool parsed = false; + usec_t timeout; + const char *e; + int r; + + if (parsed) + return saved; + + parsed = true; + + e = getenv("SYSTEMD_UDEV_EXTRA_TIMEOUT_SEC"); + if (!e) + return saved; + + r = parse_sec(e, &timeout); + if (r < 0) + log_debug_errno(r, "Failed to parse $SYSTEMD_UDEV_EXTRA_TIMEOUT_SEC=%s, ignoring: %m", e); + + if (timeout > 5 * USEC_PER_HOUR) /* Add an arbitrary upper bound */ + log_debug("Parsed $SYSTEMD_UDEV_EXTRA_TIMEOUT_SEC=%s is too large, ignoring.", e); + else + saved = timeout; + + return saved; +} + +usec_t manager_kill_worker_timeout(Manager *manager) { + assert(manager); + + /* Manager.timeout_usec is also used as the timeout for running programs specified in + * IMPORT{program}=, PROGRAM=, or RUN=. Here, let's add an extra time before the manager + * kills a worker, to make it possible that the worker detects timed out of spawned programs, + * kills them, and finalizes the event. */ + return usec_add(manager->config.timeout_usec, extra_timeout_usec()); +} diff --git a/src/udev/udev-config.h b/src/udev/udev-config.h index fd31979f6d7..339d426a0f8 100644 --- a/src/udev/udev-config.h +++ b/src/udev/udev-config.h @@ -34,3 +34,5 @@ void manager_set_environment(Manager *manager, char * const *v); int manager_load(Manager *manager, int argc, char *argv[]); UdevReloadFlags manager_reload_config(Manager *manager); + +usec_t manager_kill_worker_timeout(Manager *manager); diff --git a/src/udev/udev-manager.c b/src/udev/udev-manager.c index eddd07854d6..f053c67c8b7 100644 --- a/src/udev/udev-manager.c +++ b/src/udev/udev-manager.c @@ -342,34 +342,6 @@ static int on_event_timeout_warning(sd_event_source *s, uint64_t usec, void *use return 1; } -static usec_t extra_timeout_usec(void) { - static usec_t saved = 10 * USEC_PER_SEC; - static bool parsed = false; - usec_t timeout; - const char *e; - int r; - - if (parsed) - return saved; - - parsed = true; - - e = getenv("SYSTEMD_UDEV_EXTRA_TIMEOUT_SEC"); - if (!e) - return saved; - - r = parse_sec(e, &timeout); - if (r < 0) - log_debug_errno(r, "Failed to parse $SYSTEMD_UDEV_EXTRA_TIMEOUT_SEC=%s, ignoring: %m", e); - - if (timeout > 5 * USEC_PER_HOUR) /* Add an arbitrary upper bound */ - log_debug("Parsed $SYSTEMD_UDEV_EXTRA_TIMEOUT_SEC=%s is too large, ignoring.", e); - else - saved = timeout; - - return saved; -} - static void worker_attach_event(Worker *worker, Event *event) { Manager *manager = ASSERT_PTR(ASSERT_PTR(worker)->manager); sd_event *e = ASSERT_PTR(manager->event); @@ -387,12 +359,8 @@ static void worker_attach_event(Worker *worker, Event *event) { udev_warn_timeout(manager->config.timeout_usec), USEC_PER_SEC, on_event_timeout_warning, event); - /* Manager.timeout_usec is also used as the timeout for running programs specified in - * IMPORT{program}=, PROGRAM=, or RUN=. Here, let's add an extra time before the manager - * kills a worker, to make it possible that the worker detects timed out of spawned programs, - * kills them, and finalizes the event. */ (void) sd_event_add_time_relative(e, &event->timeout_event, CLOCK_MONOTONIC, - usec_add(manager->config.timeout_usec, extra_timeout_usec()), USEC_PER_SEC, + manager_kill_worker_timeout(manager), USEC_PER_SEC, on_event_timeout, event); } -- 2.47.3