]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/udev-util.c
util: make argument "subsystem" in device_wait_for_initialization() optional
[thirdparty/systemd.git] / src / shared / udev-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4
5 #include "alloc-util.h"
6 #include "device-util.h"
7 #include "env-file.h"
8 #include "log.h"
9 #include "parse-util.h"
10 #include "string-table.h"
11 #include "string-util.h"
12 #include "udev-util.h"
13
14 static const char* const resolve_name_timing_table[_RESOLVE_NAME_TIMING_MAX] = {
15 [RESOLVE_NAME_NEVER] = "never",
16 [RESOLVE_NAME_LATE] = "late",
17 [RESOLVE_NAME_EARLY] = "early",
18 };
19
20 DEFINE_STRING_TABLE_LOOKUP(resolve_name_timing, ResolveNameTiming);
21
22 int udev_parse_config_full(
23 unsigned *ret_children_max,
24 usec_t *ret_exec_delay_usec,
25 usec_t *ret_event_timeout_usec,
26 ResolveNameTiming *ret_resolve_name_timing) {
27
28 _cleanup_free_ char *log_val = NULL, *children_max = NULL, *exec_delay = NULL, *event_timeout = NULL, *resolve_names = NULL;
29 int r;
30
31 r = parse_env_file(NULL, "/etc/udev/udev.conf",
32 "udev_log", &log_val,
33 "children_max", &children_max,
34 "exec_delay", &exec_delay,
35 "event_timeout", &event_timeout,
36 "resolve_names", &resolve_names);
37 if (r == -ENOENT)
38 return 0;
39 if (r < 0)
40 return r;
41
42 if (log_val) {
43 const char *log;
44 size_t n;
45
46 /* unquote */
47 n = strlen(log_val);
48 if (n >= 2 &&
49 ((log_val[0] == '"' && log_val[n-1] == '"') ||
50 (log_val[0] == '\'' && log_val[n-1] == '\''))) {
51 log_val[n - 1] = '\0';
52 log = log_val + 1;
53 } else
54 log = log_val;
55
56 /* we set the udev log level here explicitly, this is supposed
57 * to regulate the code in libudev/ and udev/. */
58 r = log_set_max_level_from_string_realm(LOG_REALM_UDEV, log);
59 if (r < 0)
60 log_debug_errno(r, "/etc/udev/udev.conf: failed to set udev log level '%s', ignoring: %m", log);
61 }
62
63 if (ret_children_max && children_max) {
64 r = safe_atou(children_max, ret_children_max);
65 if (r < 0)
66 log_notice_errno(r, "/etc/udev/udev.conf: failed to set parse children_max=%s, ignoring: %m", children_max);
67 }
68
69 if (ret_exec_delay_usec && exec_delay) {
70 r = parse_sec(exec_delay, ret_exec_delay_usec);
71 if (r < 0)
72 log_notice_errno(r, "/etc/udev/udev.conf: failed to set parse exec_delay=%s, ignoring: %m", exec_delay);
73 }
74
75 if (ret_event_timeout_usec && event_timeout) {
76 r = parse_sec(event_timeout, ret_event_timeout_usec);
77 if (r < 0)
78 log_notice_errno(r, "/etc/udev/udev.conf: failed to set parse event_timeout=%s, ignoring: %m", event_timeout);
79 }
80
81 if (ret_resolve_name_timing && resolve_names) {
82 ResolveNameTiming t;
83
84 t = resolve_name_timing_from_string(resolve_names);
85 if (t < 0)
86 log_notice("/etc/udev/udev.conf: failed to set parse resolve_names=%s, ignoring.", resolve_names);
87 else
88 *ret_resolve_name_timing = t;
89 }
90
91 return 0;
92 }
93
94 struct DeviceMonitorData {
95 const char *sysname;
96 sd_device *device;
97 };
98
99 static int device_monitor_handler(sd_device_monitor *monitor, sd_device *device, void *userdata) {
100 struct DeviceMonitorData *data = userdata;
101 const char *sysname;
102
103 assert(device);
104 assert(data);
105 assert(data->sysname);
106 assert(!data->device);
107
108 if (sd_device_get_sysname(device, &sysname) >= 0 && streq(sysname, data->sysname)) {
109 data->device = sd_device_ref(device);
110 return sd_event_exit(sd_device_monitor_get_event(monitor), 0);
111 }
112
113 return 0;
114 }
115
116 int device_wait_for_initialization(sd_device *device, const char *subsystem, sd_device **ret) {
117 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
118 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
119 struct DeviceMonitorData data = {};
120 int r;
121
122 assert(device);
123
124 if (sd_device_get_is_initialized(device) > 0) {
125 if (ret)
126 *ret = sd_device_ref(device);
127 return 0;
128 }
129
130 assert_se(sd_device_get_sysname(device, &data.sysname) >= 0);
131
132 /* Wait until the device is initialized, so that we can get access to the ID_PATH property */
133
134 r = sd_event_new(&event);
135 if (r < 0)
136 return log_error_errno(r, "Failed to get default event: %m");
137
138 r = sd_device_monitor_new(&monitor);
139 if (r < 0)
140 return log_error_errno(r, "Failed to acquire monitor: %m");
141
142 if (!subsystem) {
143 r = sd_device_get_subsystem(device, &subsystem);
144 if (r < 0 && r != -ENOENT)
145 return log_device_error_errno(device, r, "Failed to get subsystem: %m");
146 }
147
148 if (subsystem) {
149 r = sd_device_monitor_filter_add_match_subsystem_devtype(monitor, subsystem, NULL);
150 if (r < 0)
151 return log_error_errno(r, "Failed to add %s subsystem match to monitor: %m", subsystem);
152 }
153
154 r = sd_device_monitor_attach_event(monitor, event);
155 if (r < 0)
156 return log_error_errno(r, "Failed to attach event to device monitor: %m");
157
158 r = sd_device_monitor_start(monitor, device_monitor_handler, &data);
159 if (r < 0)
160 return log_error_errno(r, "Failed to start device monitor: %m");
161
162 /* Check again, maybe things changed. Udev will re-read the db if the device wasn't initialized
163 * yet. */
164 if (sd_device_get_is_initialized(device) > 0) {
165 if (ret)
166 *ret = sd_device_ref(device);
167 return 0;
168 }
169
170 r = sd_event_loop(event);
171 if (r < 0)
172 return log_error_errno(r, "Event loop failed: %m");
173
174 if (ret)
175 *ret = TAKE_PTR(data.device);
176 return 0;
177 }
178
179 int device_is_renaming(sd_device *dev) {
180 int r;
181
182 assert(dev);
183
184 r = sd_device_get_property_value(dev, "ID_RENAMING", NULL);
185 if (r < 0 && r != -ENOENT)
186 return r;
187
188 return r >= 0;
189 }
190
191 bool device_for_action(sd_device *dev, DeviceAction action) {
192 DeviceAction a;
193
194 assert(dev);
195
196 if (device_get_action(dev, &a) < 0)
197 return false;
198
199 return a == action;
200 }