]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/shared/udev-util.c
tree-wide: make use of new relative time events in sd-event.h
[thirdparty/systemd.git] / src / shared / udev-util.c
index 1ca5080d1c802ebf53a1e3fa05050f7563190a36..7cd7156a892fb57b8aa2b66c817dafb68b9e6cd2 100644 (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)
@@ -57,25 +60,29 @@ int udev_parse_config_full(
                  * to regulate the code in libudev/ and udev/. */
                 r = log_set_max_level_from_string_realm(LOG_REALM_UDEV, log);
                 if (r < 0)
-                        log_debug_errno(r, "/etc/udev/udev.conf: failed to set udev log level '%s', ignoring: %m", log);
+                        log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r,
+                                   "failed to set udev log level '%s', ignoring: %m", log);
         }
 
         if (ret_children_max && children_max) {
                 r = safe_atou(children_max, ret_children_max);
                 if (r < 0)
-                        log_notice_errno(r, "/etc/udev/udev.conf: failed to set parse children_max=%s, ignoring: %m", children_max);
+                        log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r,
+                                   "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_notice_errno(r, "/etc/udev/udev.conf: failed to set parse exec_delay=%s, ignoring: %m", exec_delay);
+                        log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r,
+                                   "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_notice_errno(r, "/etc/udev/udev.conf: failed to set parse event_timeout=%s, ignoring: %m", event_timeout);
+                        log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r,
+                                   "failed to parse event_timeout=%s, ignoring: %m", event_timeout);
         }
 
         if (ret_resolve_name_timing && resolve_names) {
@@ -83,11 +90,21 @@ int udev_parse_config_full(
 
                 t = resolve_name_timing_from_string(resolve_names);
                 if (t < 0)
-                        log_notice("/etc/udev/udev.conf: failed to set parse resolve_names=%s, ignoring.", resolve_names);
+                        log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r,
+                                   "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;
 }
 
@@ -113,8 +130,13 @@ static int device_monitor_handler(sd_device_monitor *monitor, sd_device *device,
         return 0;
 }
 
-int device_wait_for_initialization(sd_device *device, const char *subsystem, sd_device **ret) {
+static int device_timeout_handler(sd_event_source *s, uint64_t usec, void *userdata) {
+        return sd_event_exit(sd_event_source_get_event(s), -ETIMEDOUT);
+}
+
+int device_wait_for_initialization(sd_device *device, const char *subsystem, usec_t timeout, sd_device **ret) {
         _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
+        _cleanup_(sd_event_source_unrefp) sd_event_source *timeout_source = NULL;
         _cleanup_(sd_event_unrefp) sd_event *event = NULL;
         struct DeviceMonitorData data = {};
         int r;
@@ -159,6 +181,15 @@ int device_wait_for_initialization(sd_device *device, const char *subsystem, sd_
         if (r < 0)
                 return log_error_errno(r, "Failed to start device monitor: %m");
 
+        if (timeout != USEC_INFINITY) {
+                r = sd_event_add_time_relative(
+                                event, &timeout_source,
+                                CLOCK_MONOTONIC, timeout, 0,
+                                device_timeout_handler, NULL);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to add timeout event source: %m");
+        }
+
         /* Check again, maybe things changed. Udev will re-read the db if the device wasn't initialized
          * yet. */
         if (sd_device_get_is_initialized(device) > 0) {
@@ -169,7 +200,7 @@ int device_wait_for_initialization(sd_device *device, const char *subsystem, sd_
 
         r = sd_event_loop(event);
         if (r < 0)
-                return log_error_errno(r, "Event loop failed: %m");
+                return log_error_errno(r, "Failed to wait for device to be initialized: %m");
 
         if (ret)
                 *ret = TAKE_PTR(data.device);