]>
Commit | Line | Data |
---|---|---|
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 |
17 | static 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 | ||
23 | DEFINE_STRING_TABLE_LOOKUP(resolve_name_timing, ResolveNameTiming); | |
24 | ||
4b3ca79e ZJS |
25 | int 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 */ | |
115 | static 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 | ||
fe79f107 ZJS |
121 | if (stat(devlink, &st) < 0) |
122 | return log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR, errno, | |
123 | "Failed to stat() %s: %m", devlink); | |
030a0d79 LB |
124 | |
125 | if (!S_ISBLK(st.st_mode)) | |
fe79f107 ZJS |
126 | return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK), |
127 | "%s does not point to a block device: %m", devlink); | |
030a0d79 LB |
128 | |
129 | r = sd_device_new_from_devnum(ret_device, 'b', st.st_rdev); | |
130 | if (r < 0) | |
131 | return log_error_errno(r, "Failed to initialize device from %s: %m", devlink); | |
132 | ||
133 | return 0; | |
134 | } | |
135 | ||
ed435031 ZJS |
136 | struct DeviceMonitorData { |
137 | const char *sysname; | |
030a0d79 | 138 | const char *devlink; |
ed435031 ZJS |
139 | sd_device *device; |
140 | }; | |
141 | ||
ce5eef65 LB |
142 | static void device_monitor_data_free(struct DeviceMonitorData *d) { |
143 | assert(d); | |
144 | ||
145 | sd_device_unref(d->device); | |
146 | } | |
147 | ||
ed435031 ZJS |
148 | static int device_monitor_handler(sd_device_monitor *monitor, sd_device *device, void *userdata) { |
149 | struct DeviceMonitorData *data = userdata; | |
150 | const char *sysname; | |
151 | ||
152 | assert(device); | |
153 | assert(data); | |
030a0d79 | 154 | assert(data->sysname || data->devlink); |
ed435031 ZJS |
155 | assert(!data->device); |
156 | ||
030a0d79 LB |
157 | if (data->sysname && sd_device_get_sysname(device, &sysname) >= 0 && streq(sysname, data->sysname)) |
158 | goto found; | |
159 | ||
160 | if (data->devlink) { | |
161 | const char *devlink; | |
162 | ||
163 | FOREACH_DEVICE_DEVLINK(device, devlink) | |
164 | if (path_equal(devlink, data->devlink)) | |
165 | goto found; | |
166 | ||
167 | if (sd_device_get_devname(device, &devlink) >= 0 && path_equal(devlink, data->devlink)) | |
168 | goto found; | |
ed435031 ZJS |
169 | } |
170 | ||
171 | return 0; | |
030a0d79 LB |
172 | |
173 | found: | |
174 | data->device = sd_device_ref(device); | |
175 | return sd_event_exit(sd_device_monitor_get_event(monitor), 0); | |
ed435031 ZJS |
176 | } |
177 | ||
030a0d79 LB |
178 | static int device_wait_for_initialization_internal( |
179 | sd_device *_device, | |
180 | const char *devlink, | |
181 | const char *subsystem, | |
182 | usec_t timeout, | |
183 | sd_device **ret) { | |
ed435031 | 184 | _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL; |
1b47436e | 185 | _cleanup_(sd_event_source_unrefp) sd_event_source *timeout_source = NULL; |
ed435031 | 186 | _cleanup_(sd_event_unrefp) sd_event *event = NULL; |
030a0d79 LB |
187 | /* Ensure that if !_device && devlink, device gets unrefd on errors since it will be new */ |
188 | _cleanup_(sd_device_unrefp) sd_device *device = sd_device_ref(_device); | |
ce5eef65 | 189 | _cleanup_(device_monitor_data_free) struct DeviceMonitorData data = { |
030a0d79 LB |
190 | .devlink = devlink, |
191 | }; | |
ed435031 ZJS |
192 | int r; |
193 | ||
030a0d79 | 194 | assert(device || (subsystem && devlink)); |
ed435031 | 195 | |
030a0d79 LB |
196 | /* Devlink might already exist, if it does get the device to use the sysname filtering */ |
197 | if (!device && devlink) { | |
198 | r = device_new_from_dev_path(devlink, &device); | |
199 | if (r < 0 && r != -ENOENT) | |
200 | return r; | |
ed435031 ZJS |
201 | } |
202 | ||
030a0d79 LB |
203 | if (device) { |
204 | if (sd_device_get_is_initialized(device) > 0) { | |
205 | if (ret) | |
206 | *ret = sd_device_ref(device); | |
207 | return 0; | |
208 | } | |
209 | /* We need either the sysname or the devlink for filtering */ | |
210 | assert_se(sd_device_get_sysname(device, &data.sysname) >= 0 || devlink); | |
211 | } | |
ed435031 ZJS |
212 | |
213 | /* Wait until the device is initialized, so that we can get access to the ID_PATH property */ | |
214 | ||
fc40bfa7 | 215 | r = sd_event_new(&event); |
ed435031 ZJS |
216 | if (r < 0) |
217 | return log_error_errno(r, "Failed to get default event: %m"); | |
218 | ||
219 | r = sd_device_monitor_new(&monitor); | |
220 | if (r < 0) | |
221 | return log_error_errno(r, "Failed to acquire monitor: %m"); | |
222 | ||
030a0d79 | 223 | if (device && !subsystem) { |
f822c5d5 YW |
224 | r = sd_device_get_subsystem(device, &subsystem); |
225 | if (r < 0 && r != -ENOENT) | |
226 | return log_device_error_errno(device, r, "Failed to get subsystem: %m"); | |
227 | } | |
228 | ||
229 | if (subsystem) { | |
230 | r = sd_device_monitor_filter_add_match_subsystem_devtype(monitor, subsystem, NULL); | |
231 | if (r < 0) | |
232 | return log_error_errno(r, "Failed to add %s subsystem match to monitor: %m", subsystem); | |
233 | } | |
ed435031 ZJS |
234 | |
235 | r = sd_device_monitor_attach_event(monitor, event); | |
236 | if (r < 0) | |
237 | return log_error_errno(r, "Failed to attach event to device monitor: %m"); | |
238 | ||
239 | r = sd_device_monitor_start(monitor, device_monitor_handler, &data); | |
240 | if (r < 0) | |
241 | return log_error_errno(r, "Failed to start device monitor: %m"); | |
242 | ||
1b47436e | 243 | if (timeout != USEC_INFINITY) { |
39cf0351 LP |
244 | r = sd_event_add_time_relative( |
245 | event, &timeout_source, | |
246 | CLOCK_MONOTONIC, timeout, 0, | |
bac0bfc1 | 247 | NULL, INT_TO_PTR(-ETIMEDOUT)); |
1b47436e YW |
248 | if (r < 0) |
249 | return log_error_errno(r, "Failed to add timeout event source: %m"); | |
250 | } | |
251 | ||
ed435031 ZJS |
252 | /* Check again, maybe things changed. Udev will re-read the db if the device wasn't initialized |
253 | * yet. */ | |
030a0d79 LB |
254 | if (!device && devlink) { |
255 | r = device_new_from_dev_path(devlink, &device); | |
256 | if (r < 0 && r != -ENOENT) | |
257 | return r; | |
258 | } | |
259 | if (device && sd_device_get_is_initialized(device) > 0) { | |
ed435031 ZJS |
260 | if (ret) |
261 | *ret = sd_device_ref(device); | |
262 | return 0; | |
263 | } | |
264 | ||
265 | r = sd_event_loop(event); | |
266 | if (r < 0) | |
1b47436e | 267 | return log_error_errno(r, "Failed to wait for device to be initialized: %m"); |
ed435031 ZJS |
268 | |
269 | if (ret) | |
270 | *ret = TAKE_PTR(data.device); | |
271 | return 0; | |
272 | } | |
90ba130f | 273 | |
030a0d79 LB |
274 | int device_wait_for_initialization(sd_device *device, const char *subsystem, usec_t timeout, sd_device **ret) { |
275 | return device_wait_for_initialization_internal(device, NULL, subsystem, timeout, ret); | |
276 | } | |
277 | ||
278 | int device_wait_for_devlink(const char *devlink, const char *subsystem, usec_t timeout, sd_device **ret) { | |
279 | return device_wait_for_initialization_internal(NULL, devlink, subsystem, timeout, ret); | |
280 | } | |
281 | ||
90ba130f YW |
282 | int device_is_renaming(sd_device *dev) { |
283 | int r; | |
284 | ||
285 | assert(dev); | |
286 | ||
287 | r = sd_device_get_property_value(dev, "ID_RENAMING", NULL); | |
b9daaedb LP |
288 | if (r == -ENOENT) |
289 | return false; | |
290 | if (r < 0) | |
90ba130f YW |
291 | return r; |
292 | ||
b9daaedb | 293 | return true; |
90ba130f | 294 | } |
a707c65b YW |
295 | |
296 | bool device_for_action(sd_device *dev, DeviceAction action) { | |
297 | DeviceAction a; | |
298 | ||
299 | assert(dev); | |
300 | ||
301 | if (device_get_action(dev, &a) < 0) | |
302 | return false; | |
303 | ||
304 | return a == action; | |
305 | } |