]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/udev-util.c
update TODO
[thirdparty/systemd.git] / src / shared / udev-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
b237a168 2
152d0efa 3#include <errno.h>
030a0d79 4#include <unistd.h>
b237a168 5
152d0efa 6#include "alloc-util.h"
f822c5d5 7#include "device-util.h"
686d13b9 8#include "env-file.h"
b237a168 9#include "log.h"
4b3ca79e 10#include "parse-util.h"
030a0d79 11#include "path-util.h"
e2099267 12#include "signal-util.h"
bc768f04 13#include "string-table.h"
b237a168
ZJS
14#include "string-util.h"
15#include "udev-util.h"
16
bc768f04
ZJS
17static const char* const resolve_name_timing_table[_RESOLVE_NAME_TIMING_MAX] = {
18 [RESOLVE_NAME_NEVER] = "never",
19 [RESOLVE_NAME_LATE] = "late",
20 [RESOLVE_NAME_EARLY] = "early",
21};
22
23DEFINE_STRING_TABLE_LOOKUP(resolve_name_timing, ResolveNameTiming);
24
4b3ca79e
ZJS
25int udev_parse_config_full(
26 unsigned *ret_children_max,
27 usec_t *ret_exec_delay_usec,
a14e7af1 28 usec_t *ret_event_timeout_usec,
e2099267
MS
29 ResolveNameTiming *ret_resolve_name_timing,
30 int *ret_timeout_signal) {
4b3ca79e 31
e2099267 32 _cleanup_free_ char *log_val = NULL, *children_max = NULL, *exec_delay = NULL, *event_timeout = NULL, *resolve_names = NULL, *timeout_signal = NULL;
b237a168
ZJS
33 int r;
34
aa8fbc74 35 r = parse_env_file(NULL, "/etc/udev/udev.conf",
4b3ca79e
ZJS
36 "udev_log", &log_val,
37 "children_max", &children_max,
38 "exec_delay", &exec_delay,
9b2934cb 39 "event_timeout", &event_timeout,
e2099267
MS
40 "resolve_names", &resolve_names,
41 "timeout_signal", &timeout_signal);
4b3ca79e 42 if (r == -ENOENT)
b237a168
ZJS
43 return 0;
44 if (r < 0)
45 return r;
46
4b3ca79e
ZJS
47 if (log_val) {
48 const char *log;
49 size_t n;
50
51 /* unquote */
52 n = strlen(log_val);
53 if (n >= 2 &&
54 ((log_val[0] == '"' && log_val[n-1] == '"') ||
55 (log_val[0] == '\'' && log_val[n-1] == '\''))) {
56 log_val[n - 1] = '\0';
57 log = log_val + 1;
58 } else
59 log = log_val;
60
61 /* we set the udev log level here explicitly, this is supposed
62 * to regulate the code in libudev/ and udev/. */
63 r = log_set_max_level_from_string_realm(LOG_REALM_UDEV, log);
64 if (r < 0)
d7921114
ZJS
65 log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r,
66 "failed to set udev log level '%s', ignoring: %m", log);
4b3ca79e
ZJS
67 }
68
69 if (ret_children_max && children_max) {
70 r = safe_atou(children_max, ret_children_max);
71 if (r < 0)
d7921114 72 log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r,
e2099267 73 "failed to parse children_max=%s, ignoring: %m", children_max);
4b3ca79e
ZJS
74 }
75
76 if (ret_exec_delay_usec && exec_delay) {
77 r = parse_sec(exec_delay, ret_exec_delay_usec);
78 if (r < 0)
d7921114 79 log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r,
e2099267 80 "failed to parse exec_delay=%s, ignoring: %m", exec_delay);
4b3ca79e
ZJS
81 }
82
83 if (ret_event_timeout_usec && event_timeout) {
84 r = parse_sec(event_timeout, ret_event_timeout_usec);
85 if (r < 0)
d7921114 86 log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r,
e2099267 87 "failed to parse event_timeout=%s, ignoring: %m", event_timeout);
4b3ca79e 88 }
b237a168 89
a14e7af1
ZJS
90 if (ret_resolve_name_timing && resolve_names) {
91 ResolveNameTiming t;
92
93 t = resolve_name_timing_from_string(resolve_names);
94 if (t < 0)
d7921114 95 log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r,
e2099267 96 "failed to parse resolve_names=%s, ignoring.", resolve_names);
a14e7af1
ZJS
97 else
98 *ret_resolve_name_timing = t;
99 }
e2099267
MS
100
101 if (ret_timeout_signal && timeout_signal) {
102 r = signal_from_string(timeout_signal);
103 if (r < 0)
104 log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r,
105 "failed to parse timeout_signal=%s, ignoring: %m", timeout_signal);
106 else
107 *ret_timeout_signal = r;
108 }
a14e7af1 109
b237a168
ZJS
110 return 0;
111}
ed435031 112
030a0d79
LB
113/* Note that if -ENOENT is returned, it will be logged at debug level rather than error,
114 * because it's an expected, common occurrence that the caller will handle with a fallback */
115static int device_new_from_dev_path(const char *devlink, sd_device **ret_device) {
116 struct stat st;
117 int r;
118
119 assert(devlink);
120
121 r = stat(devlink, &st);
122 if (r < 0)
123 return log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR, errno, "Failed to stat() %s: %m", devlink);
124
125 if (!S_ISBLK(st.st_mode))
126 return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK), "%s does not point to a block device: %m", devlink);
127
128 r = sd_device_new_from_devnum(ret_device, 'b', st.st_rdev);
129 if (r < 0)
130 return log_error_errno(r, "Failed to initialize device from %s: %m", devlink);
131
132 return 0;
133}
134
ed435031
ZJS
135struct DeviceMonitorData {
136 const char *sysname;
030a0d79 137 const char *devlink;
ed435031
ZJS
138 sd_device *device;
139};
140
ce5eef65
LB
141static void device_monitor_data_free(struct DeviceMonitorData *d) {
142 assert(d);
143
144 sd_device_unref(d->device);
145}
146
ed435031
ZJS
147static int device_monitor_handler(sd_device_monitor *monitor, sd_device *device, void *userdata) {
148 struct DeviceMonitorData *data = userdata;
149 const char *sysname;
150
151 assert(device);
152 assert(data);
030a0d79 153 assert(data->sysname || data->devlink);
ed435031
ZJS
154 assert(!data->device);
155
030a0d79
LB
156 if (data->sysname && sd_device_get_sysname(device, &sysname) >= 0 && streq(sysname, data->sysname))
157 goto found;
158
159 if (data->devlink) {
160 const char *devlink;
161
162 FOREACH_DEVICE_DEVLINK(device, devlink)
163 if (path_equal(devlink, data->devlink))
164 goto found;
165
166 if (sd_device_get_devname(device, &devlink) >= 0 && path_equal(devlink, data->devlink))
167 goto found;
ed435031
ZJS
168 }
169
170 return 0;
030a0d79
LB
171
172found:
173 data->device = sd_device_ref(device);
174 return sd_event_exit(sd_device_monitor_get_event(monitor), 0);
ed435031
ZJS
175}
176
1b47436e
YW
177static int device_timeout_handler(sd_event_source *s, uint64_t usec, void *userdata) {
178 return sd_event_exit(sd_event_source_get_event(s), -ETIMEDOUT);
179}
180
030a0d79
LB
181static int device_wait_for_initialization_internal(
182 sd_device *_device,
183 const char *devlink,
184 const char *subsystem,
185 usec_t timeout,
186 sd_device **ret) {
ed435031 187 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
1b47436e 188 _cleanup_(sd_event_source_unrefp) sd_event_source *timeout_source = NULL;
ed435031 189 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
030a0d79
LB
190 /* Ensure that if !_device && devlink, device gets unrefd on errors since it will be new */
191 _cleanup_(sd_device_unrefp) sd_device *device = sd_device_ref(_device);
ce5eef65 192 _cleanup_(device_monitor_data_free) struct DeviceMonitorData data = {
030a0d79
LB
193 .devlink = devlink,
194 };
ed435031
ZJS
195 int r;
196
030a0d79 197 assert(device || (subsystem && devlink));
ed435031 198
030a0d79
LB
199 /* Devlink might already exist, if it does get the device to use the sysname filtering */
200 if (!device && devlink) {
201 r = device_new_from_dev_path(devlink, &device);
202 if (r < 0 && r != -ENOENT)
203 return r;
ed435031
ZJS
204 }
205
030a0d79
LB
206 if (device) {
207 if (sd_device_get_is_initialized(device) > 0) {
208 if (ret)
209 *ret = sd_device_ref(device);
210 return 0;
211 }
212 /* We need either the sysname or the devlink for filtering */
213 assert_se(sd_device_get_sysname(device, &data.sysname) >= 0 || devlink);
214 }
ed435031
ZJS
215
216 /* Wait until the device is initialized, so that we can get access to the ID_PATH property */
217
fc40bfa7 218 r = sd_event_new(&event);
ed435031
ZJS
219 if (r < 0)
220 return log_error_errno(r, "Failed to get default event: %m");
221
222 r = sd_device_monitor_new(&monitor);
223 if (r < 0)
224 return log_error_errno(r, "Failed to acquire monitor: %m");
225
030a0d79 226 if (device && !subsystem) {
f822c5d5
YW
227 r = sd_device_get_subsystem(device, &subsystem);
228 if (r < 0 && r != -ENOENT)
229 return log_device_error_errno(device, r, "Failed to get subsystem: %m");
230 }
231
232 if (subsystem) {
233 r = sd_device_monitor_filter_add_match_subsystem_devtype(monitor, subsystem, NULL);
234 if (r < 0)
235 return log_error_errno(r, "Failed to add %s subsystem match to monitor: %m", subsystem);
236 }
ed435031
ZJS
237
238 r = sd_device_monitor_attach_event(monitor, event);
239 if (r < 0)
240 return log_error_errno(r, "Failed to attach event to device monitor: %m");
241
242 r = sd_device_monitor_start(monitor, device_monitor_handler, &data);
243 if (r < 0)
244 return log_error_errno(r, "Failed to start device monitor: %m");
245
1b47436e 246 if (timeout != USEC_INFINITY) {
39cf0351
LP
247 r = sd_event_add_time_relative(
248 event, &timeout_source,
249 CLOCK_MONOTONIC, timeout, 0,
250 device_timeout_handler, NULL);
1b47436e
YW
251 if (r < 0)
252 return log_error_errno(r, "Failed to add timeout event source: %m");
253 }
254
ed435031
ZJS
255 /* Check again, maybe things changed. Udev will re-read the db if the device wasn't initialized
256 * yet. */
030a0d79
LB
257 if (!device && devlink) {
258 r = device_new_from_dev_path(devlink, &device);
259 if (r < 0 && r != -ENOENT)
260 return r;
261 }
262 if (device && sd_device_get_is_initialized(device) > 0) {
ed435031
ZJS
263 if (ret)
264 *ret = sd_device_ref(device);
265 return 0;
266 }
267
268 r = sd_event_loop(event);
269 if (r < 0)
1b47436e 270 return log_error_errno(r, "Failed to wait for device to be initialized: %m");
ed435031
ZJS
271
272 if (ret)
273 *ret = TAKE_PTR(data.device);
274 return 0;
275}
90ba130f 276
030a0d79
LB
277int device_wait_for_initialization(sd_device *device, const char *subsystem, usec_t timeout, sd_device **ret) {
278 return device_wait_for_initialization_internal(device, NULL, subsystem, timeout, ret);
279}
280
281int device_wait_for_devlink(const char *devlink, const char *subsystem, usec_t timeout, sd_device **ret) {
282 return device_wait_for_initialization_internal(NULL, devlink, subsystem, timeout, ret);
283}
284
90ba130f
YW
285int device_is_renaming(sd_device *dev) {
286 int r;
287
288 assert(dev);
289
290 r = sd_device_get_property_value(dev, "ID_RENAMING", NULL);
291 if (r < 0 && r != -ENOENT)
292 return r;
293
294 return r >= 0;
295}
a707c65b
YW
296
297bool device_for_action(sd_device *dev, DeviceAction action) {
298 DeviceAction a;
299
300 assert(dev);
301
302 if (device_get_action(dev, &a) < 0)
303 return false;
304
305 return a == action;
306}