]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
b237a168 | 2 | |
5953d8b9 | 3 | #include <ctype.h> |
152d0efa | 4 | #include <errno.h> |
bee33d05 | 5 | #include <sys/inotify.h> |
030a0d79 | 6 | #include <unistd.h> |
b237a168 | 7 | |
152d0efa | 8 | #include "alloc-util.h" |
393fcaf7 | 9 | #include "device-nodes.h" |
a1130022 | 10 | #include "device-private.h" |
f822c5d5 | 11 | #include "device-util.h" |
686d13b9 | 12 | #include "env-file.h" |
acfc2a1d | 13 | #include "errno-util.h" |
aea3253e | 14 | #include "escape.h" |
bee33d05 | 15 | #include "fd-util.h" |
e9162760 | 16 | #include "id128-util.h" |
b237a168 | 17 | #include "log.h" |
aea3253e | 18 | #include "macro.h" |
4b3ca79e | 19 | #include "parse-util.h" |
030a0d79 | 20 | #include "path-util.h" |
e2099267 | 21 | #include "signal-util.h" |
e1ecfef1 | 22 | #include "socket-util.h" |
f92c5bb1 | 23 | #include "stat-util.h" |
bc768f04 | 24 | #include "string-table.h" |
b237a168 | 25 | #include "string-util.h" |
1223227f | 26 | #include "strxcpyx.h" |
b237a168 | 27 | #include "udev-util.h" |
aea3253e | 28 | #include "utf8.h" |
b237a168 | 29 | |
bc768f04 ZJS |
30 | static const char* const resolve_name_timing_table[_RESOLVE_NAME_TIMING_MAX] = { |
31 | [RESOLVE_NAME_NEVER] = "never", | |
1c6e17e5 | 32 | [RESOLVE_NAME_LATE] = "late", |
bc768f04 ZJS |
33 | [RESOLVE_NAME_EARLY] = "early", |
34 | }; | |
35 | ||
36 | DEFINE_STRING_TABLE_LOOKUP(resolve_name_timing, ResolveNameTiming); | |
37 | ||
4b3ca79e ZJS |
38 | int udev_parse_config_full( |
39 | unsigned *ret_children_max, | |
40 | usec_t *ret_exec_delay_usec, | |
a14e7af1 | 41 | usec_t *ret_event_timeout_usec, |
e2099267 MS |
42 | ResolveNameTiming *ret_resolve_name_timing, |
43 | int *ret_timeout_signal) { | |
4b3ca79e | 44 | |
e2099267 | 45 | _cleanup_free_ char *log_val = NULL, *children_max = NULL, *exec_delay = NULL, *event_timeout = NULL, *resolve_names = NULL, *timeout_signal = NULL; |
b237a168 ZJS |
46 | int r; |
47 | ||
aa8fbc74 | 48 | r = parse_env_file(NULL, "/etc/udev/udev.conf", |
4b3ca79e ZJS |
49 | "udev_log", &log_val, |
50 | "children_max", &children_max, | |
51 | "exec_delay", &exec_delay, | |
9b2934cb | 52 | "event_timeout", &event_timeout, |
e2099267 MS |
53 | "resolve_names", &resolve_names, |
54 | "timeout_signal", &timeout_signal); | |
4b3ca79e | 55 | if (r == -ENOENT) |
b237a168 ZJS |
56 | return 0; |
57 | if (r < 0) | |
58 | return r; | |
59 | ||
4b3ca79e ZJS |
60 | if (log_val) { |
61 | const char *log; | |
62 | size_t n; | |
63 | ||
64 | /* unquote */ | |
65 | n = strlen(log_val); | |
66 | if (n >= 2 && | |
67 | ((log_val[0] == '"' && log_val[n-1] == '"') || | |
68 | (log_val[0] == '\'' && log_val[n-1] == '\''))) { | |
69 | log_val[n - 1] = '\0'; | |
70 | log = log_val + 1; | |
71 | } else | |
72 | log = log_val; | |
73 | ||
74 | /* we set the udev log level here explicitly, this is supposed | |
75 | * to regulate the code in libudev/ and udev/. */ | |
3cc6b14a | 76 | r = log_set_max_level_from_string(log); |
4b3ca79e | 77 | if (r < 0) |
d7921114 ZJS |
78 | log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r, |
79 | "failed to set udev log level '%s', ignoring: %m", log); | |
4b3ca79e ZJS |
80 | } |
81 | ||
82 | if (ret_children_max && children_max) { | |
83 | r = safe_atou(children_max, ret_children_max); | |
84 | if (r < 0) | |
d7921114 | 85 | log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r, |
e2099267 | 86 | "failed to parse children_max=%s, ignoring: %m", children_max); |
4b3ca79e ZJS |
87 | } |
88 | ||
89 | if (ret_exec_delay_usec && exec_delay) { | |
90 | r = parse_sec(exec_delay, ret_exec_delay_usec); | |
91 | if (r < 0) | |
d7921114 | 92 | log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r, |
e2099267 | 93 | "failed to parse exec_delay=%s, ignoring: %m", exec_delay); |
4b3ca79e ZJS |
94 | } |
95 | ||
96 | if (ret_event_timeout_usec && event_timeout) { | |
97 | r = parse_sec(event_timeout, ret_event_timeout_usec); | |
98 | if (r < 0) | |
d7921114 | 99 | log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r, |
e2099267 | 100 | "failed to parse event_timeout=%s, ignoring: %m", event_timeout); |
4b3ca79e | 101 | } |
b237a168 | 102 | |
a14e7af1 ZJS |
103 | if (ret_resolve_name_timing && resolve_names) { |
104 | ResolveNameTiming t; | |
105 | ||
106 | t = resolve_name_timing_from_string(resolve_names); | |
107 | if (t < 0) | |
d7921114 | 108 | log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r, |
e2099267 | 109 | "failed to parse resolve_names=%s, ignoring.", resolve_names); |
a14e7af1 ZJS |
110 | else |
111 | *ret_resolve_name_timing = t; | |
112 | } | |
e2099267 MS |
113 | |
114 | if (ret_timeout_signal && timeout_signal) { | |
115 | r = signal_from_string(timeout_signal); | |
116 | if (r < 0) | |
117 | log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r, | |
118 | "failed to parse timeout_signal=%s, ignoring: %m", timeout_signal); | |
119 | else | |
120 | *ret_timeout_signal = r; | |
121 | } | |
a14e7af1 | 122 | |
b237a168 ZJS |
123 | return 0; |
124 | } | |
ed435031 ZJS |
125 | |
126 | struct DeviceMonitorData { | |
127 | const char *sysname; | |
030a0d79 | 128 | const char *devlink; |
ed435031 ZJS |
129 | sd_device *device; |
130 | }; | |
131 | ||
ce5eef65 LB |
132 | static void device_monitor_data_free(struct DeviceMonitorData *d) { |
133 | assert(d); | |
134 | ||
135 | sd_device_unref(d->device); | |
136 | } | |
137 | ||
ed435031 | 138 | static int device_monitor_handler(sd_device_monitor *monitor, sd_device *device, void *userdata) { |
99534007 | 139 | struct DeviceMonitorData *data = ASSERT_PTR(userdata); |
ed435031 ZJS |
140 | const char *sysname; |
141 | ||
142 | assert(device); | |
030a0d79 | 143 | assert(data->sysname || data->devlink); |
ed435031 ZJS |
144 | assert(!data->device); |
145 | ||
e13d96ca LP |
146 | /* Ignore REMOVE events here. We are waiting for initialization after all, not de-initialization. We |
147 | * might see a REMOVE event from an earlier use of the device (devices by the same name are recycled | |
148 | * by the kernel after all), which we should not get confused by. After all we cannot distinguish use | |
149 | * cycles of the devices, as the udev queue is entirely asynchronous. | |
150 | * | |
151 | * If we see a REMOVE event here for the use cycle we actually care about then we won't notice of | |
152 | * course, but that should be OK, given the timeout logic used on the wait loop: this will be noticed | |
153 | * by means of -ETIMEDOUT. Thus we won't notice immediately, but eventually, and that should be | |
154 | * sufficient for an error path that should regularly not happen. | |
155 | * | |
156 | * (And yes, we only need to special case REMOVE. It's the only "negative" event type, where a device | |
157 | * ceases to exist. All other event types are "positive": the device exists and is registered in the | |
158 | * udev database, thus whenever we see the event, we can consider it initialized.) */ | |
a1130022 | 159 | if (device_for_action(device, SD_DEVICE_REMOVE)) |
e13d96ca LP |
160 | return 0; |
161 | ||
030a0d79 LB |
162 | if (data->sysname && sd_device_get_sysname(device, &sysname) >= 0 && streq(sysname, data->sysname)) |
163 | goto found; | |
164 | ||
165 | if (data->devlink) { | |
166 | const char *devlink; | |
167 | ||
168 | FOREACH_DEVICE_DEVLINK(device, devlink) | |
169 | if (path_equal(devlink, data->devlink)) | |
170 | goto found; | |
171 | ||
172 | if (sd_device_get_devname(device, &devlink) >= 0 && path_equal(devlink, data->devlink)) | |
173 | goto found; | |
ed435031 ZJS |
174 | } |
175 | ||
176 | return 0; | |
030a0d79 LB |
177 | |
178 | found: | |
179 | data->device = sd_device_ref(device); | |
180 | return sd_event_exit(sd_device_monitor_get_event(monitor), 0); | |
ed435031 ZJS |
181 | } |
182 | ||
030a0d79 LB |
183 | static int device_wait_for_initialization_internal( |
184 | sd_device *_device, | |
185 | const char *devlink, | |
186 | const char *subsystem, | |
4f89ce0c | 187 | usec_t timeout_usec, |
030a0d79 | 188 | sd_device **ret) { |
9e3d9067 | 189 | |
ed435031 ZJS |
190 | _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL; |
191 | _cleanup_(sd_event_unrefp) sd_event *event = NULL; | |
030a0d79 LB |
192 | /* Ensure that if !_device && devlink, device gets unrefd on errors since it will be new */ |
193 | _cleanup_(sd_device_unrefp) sd_device *device = sd_device_ref(_device); | |
ce5eef65 | 194 | _cleanup_(device_monitor_data_free) struct DeviceMonitorData data = { |
030a0d79 LB |
195 | .devlink = devlink, |
196 | }; | |
ed435031 ZJS |
197 | int r; |
198 | ||
030a0d79 | 199 | assert(device || (subsystem && devlink)); |
ed435031 | 200 | |
030a0d79 LB |
201 | /* Devlink might already exist, if it does get the device to use the sysname filtering */ |
202 | if (!device && devlink) { | |
f81b3e90 YW |
203 | r = sd_device_new_from_devname(&device, devlink); |
204 | if (r < 0 && !ERRNO_IS_DEVICE_ABSENT(r)) | |
205 | return log_error_errno(r, "Failed to create sd-device object from %s: %m", devlink); | |
ed435031 ZJS |
206 | } |
207 | ||
030a0d79 LB |
208 | if (device) { |
209 | if (sd_device_get_is_initialized(device) > 0) { | |
210 | if (ret) | |
211 | *ret = sd_device_ref(device); | |
212 | return 0; | |
213 | } | |
214 | /* We need either the sysname or the devlink for filtering */ | |
215 | assert_se(sd_device_get_sysname(device, &data.sysname) >= 0 || devlink); | |
216 | } | |
ed435031 ZJS |
217 | |
218 | /* Wait until the device is initialized, so that we can get access to the ID_PATH property */ | |
219 | ||
fc40bfa7 | 220 | r = sd_event_new(&event); |
ed435031 ZJS |
221 | if (r < 0) |
222 | return log_error_errno(r, "Failed to get default event: %m"); | |
223 | ||
224 | r = sd_device_monitor_new(&monitor); | |
225 | if (r < 0) | |
226 | return log_error_errno(r, "Failed to acquire monitor: %m"); | |
227 | ||
030a0d79 | 228 | if (device && !subsystem) { |
f822c5d5 YW |
229 | r = sd_device_get_subsystem(device, &subsystem); |
230 | if (r < 0 && r != -ENOENT) | |
231 | return log_device_error_errno(device, r, "Failed to get subsystem: %m"); | |
232 | } | |
233 | ||
234 | if (subsystem) { | |
235 | r = sd_device_monitor_filter_add_match_subsystem_devtype(monitor, subsystem, NULL); | |
236 | if (r < 0) | |
237 | return log_error_errno(r, "Failed to add %s subsystem match to monitor: %m", subsystem); | |
238 | } | |
ed435031 ZJS |
239 | |
240 | r = sd_device_monitor_attach_event(monitor, event); | |
241 | if (r < 0) | |
242 | return log_error_errno(r, "Failed to attach event to device monitor: %m"); | |
243 | ||
244 | r = sd_device_monitor_start(monitor, device_monitor_handler, &data); | |
245 | if (r < 0) | |
246 | return log_error_errno(r, "Failed to start device monitor: %m"); | |
247 | ||
4f89ce0c YW |
248 | if (timeout_usec != USEC_INFINITY) { |
249 | r = sd_event_add_time_relative( | |
250 | event, NULL, | |
251 | CLOCK_MONOTONIC, timeout_usec, 0, | |
bac0bfc1 | 252 | NULL, INT_TO_PTR(-ETIMEDOUT)); |
1b47436e YW |
253 | if (r < 0) |
254 | return log_error_errno(r, "Failed to add timeout event source: %m"); | |
255 | } | |
256 | ||
4f89ce0c | 257 | /* Check again, maybe things changed. Udev will re-read the db if the device wasn't initialized yet. */ |
030a0d79 | 258 | if (!device && devlink) { |
f81b3e90 YW |
259 | r = sd_device_new_from_devname(&device, devlink); |
260 | if (r < 0 && !ERRNO_IS_DEVICE_ABSENT(r)) | |
261 | return log_error_errno(r, "Failed to create sd-device object from %s: %m", devlink); | |
030a0d79 LB |
262 | } |
263 | if (device && sd_device_get_is_initialized(device) > 0) { | |
ed435031 ZJS |
264 | if (ret) |
265 | *ret = sd_device_ref(device); | |
266 | return 0; | |
267 | } | |
268 | ||
269 | r = sd_event_loop(event); | |
270 | if (r < 0) | |
1b47436e | 271 | return log_error_errno(r, "Failed to wait for device to be initialized: %m"); |
ed435031 ZJS |
272 | |
273 | if (ret) | |
274 | *ret = TAKE_PTR(data.device); | |
275 | return 0; | |
276 | } | |
90ba130f | 277 | |
4f89ce0c YW |
278 | int device_wait_for_initialization(sd_device *device, const char *subsystem, usec_t timeout_usec, sd_device **ret) { |
279 | return device_wait_for_initialization_internal(device, NULL, subsystem, timeout_usec, ret); | |
030a0d79 LB |
280 | } |
281 | ||
4f89ce0c YW |
282 | int device_wait_for_devlink(const char *devlink, const char *subsystem, usec_t timeout_usec, sd_device **ret) { |
283 | return device_wait_for_initialization_internal(NULL, devlink, subsystem, timeout_usec, ret); | |
030a0d79 LB |
284 | } |
285 | ||
90ba130f YW |
286 | int device_is_renaming(sd_device *dev) { |
287 | int r; | |
288 | ||
289 | assert(dev); | |
290 | ||
291 | r = sd_device_get_property_value(dev, "ID_RENAMING", NULL); | |
b9daaedb LP |
292 | if (r == -ENOENT) |
293 | return false; | |
294 | if (r < 0) | |
90ba130f YW |
295 | return r; |
296 | ||
b9daaedb | 297 | return true; |
90ba130f | 298 | } |
a707c65b | 299 | |
a1130022 LP |
300 | bool device_for_action(sd_device *dev, sd_device_action_t a) { |
301 | sd_device_action_t b; | |
a707c65b YW |
302 | |
303 | assert(dev); | |
304 | ||
a1130022 | 305 | if (a < 0) |
a707c65b YW |
306 | return false; |
307 | ||
a1130022 LP |
308 | if (sd_device_get_action(dev, &b) < 0) |
309 | return false; | |
310 | ||
311 | return a == b; | |
a707c65b | 312 | } |
aea3253e | 313 | |
b2d9e58f | 314 | void log_device_uevent(sd_device *device, const char *str) { |
a1130022 | 315 | sd_device_action_t action = _SD_DEVICE_ACTION_INVALID; |
e9162760 | 316 | sd_id128_t event_id = SD_ID128_NULL; |
b2d9e58f YW |
317 | uint64_t seqnum = 0; |
318 | ||
319 | if (!DEBUG_LOGGING) | |
320 | return; | |
321 | ||
a1130022 LP |
322 | (void) sd_device_get_seqnum(device, &seqnum); |
323 | (void) sd_device_get_action(device, &action); | |
e9162760 YW |
324 | (void) sd_device_get_trigger_uuid(device, &event_id); |
325 | log_device_debug(device, "%s%s(SEQNUM=%"PRIu64", ACTION=%s%s%s)", | |
b2d9e58f | 326 | strempty(str), isempty(str) ? "" : " ", |
e9162760 YW |
327 | seqnum, strna(device_action_to_string(action)), |
328 | sd_id128_is_null(event_id) ? "" : ", UUID=", | |
b7416360 | 329 | sd_id128_is_null(event_id) ? "" : SD_ID128_TO_UUID_STRING(event_id)); |
b2d9e58f YW |
330 | } |
331 | ||
aea3253e YLY |
332 | int udev_rule_parse_value(char *str, char **ret_value, char **ret_endpos) { |
333 | char *i, *j; | |
aea3253e YLY |
334 | bool is_escaped; |
335 | ||
336 | /* value must be double quotated */ | |
337 | is_escaped = str[0] == 'e'; | |
338 | str += is_escaped; | |
339 | if (str[0] != '"') | |
340 | return -EINVAL; | |
341 | str++; | |
342 | ||
343 | if (!is_escaped) { | |
344 | /* unescape double quotation '\"'->'"' */ | |
345 | for (i = j = str; *i != '"'; i++, j++) { | |
346 | if (*i == '\0') | |
347 | return -EINVAL; | |
348 | if (i[0] == '\\' && i[1] == '"') | |
349 | i++; | |
350 | *j = *i; | |
351 | } | |
352 | j[0] = '\0'; | |
353 | } else { | |
354 | _cleanup_free_ char *unescaped = NULL; | |
e437538f | 355 | ssize_t l; |
aea3253e YLY |
356 | |
357 | /* find the end position of value */ | |
358 | for (i = str; *i != '"'; i++) { | |
359 | if (i[0] == '\\') | |
360 | i++; | |
361 | if (*i == '\0') | |
362 | return -EINVAL; | |
363 | } | |
364 | i[0] = '\0'; | |
365 | ||
e437538f ZJS |
366 | l = cunescape_length(str, i - str, 0, &unescaped); |
367 | if (l < 0) | |
368 | return l; | |
369 | ||
370 | assert(l <= i - str); | |
371 | memcpy(str, unescaped, l + 1); | |
aea3253e YLY |
372 | } |
373 | ||
374 | *ret_value = str; | |
375 | *ret_endpos = i + 1; | |
376 | return 0; | |
377 | } | |
5953d8b9 YW |
378 | |
379 | size_t udev_replace_whitespace(const char *str, char *to, size_t len) { | |
380 | bool is_space = false; | |
381 | size_t i, j; | |
382 | ||
383 | assert(str); | |
384 | assert(to); | |
385 | ||
386 | /* Copy from 'str' to 'to', while removing all leading and trailing whitespace, and replacing | |
387 | * each run of consecutive whitespace with a single underscore. The chars from 'str' are copied | |
388 | * up to the \0 at the end of the string, or at most 'len' chars. This appends \0 to 'to', at | |
389 | * the end of the copied characters. | |
390 | * | |
391 | * If 'len' chars are copied into 'to', the final \0 is placed at len+1 (i.e. 'to[len] = \0'), | |
392 | * so the 'to' buffer must have at least len+1 chars available. | |
393 | * | |
394 | * Note this may be called with 'str' == 'to', i.e. to replace whitespace in-place in a buffer. | |
395 | * This function can handle that situation. | |
396 | * | |
397 | * Note that only 'len' characters are read from 'str'. */ | |
398 | ||
399 | i = strspn(str, WHITESPACE); | |
400 | ||
401 | for (j = 0; j < len && i < len && str[i] != '\0'; i++) { | |
402 | if (isspace(str[i])) { | |
403 | is_space = true; | |
404 | continue; | |
405 | } | |
406 | ||
407 | if (is_space) { | |
408 | if (j + 1 >= len) | |
409 | break; | |
410 | ||
411 | to[j++] = '_'; | |
412 | is_space = false; | |
413 | } | |
414 | to[j++] = str[i]; | |
415 | } | |
416 | ||
417 | to[j] = '\0'; | |
418 | return j; | |
419 | } | |
393fcaf7 | 420 | |
e1ecfef1 YW |
421 | size_t udev_replace_ifname(char *str) { |
422 | size_t replaced = 0; | |
423 | ||
424 | assert(str); | |
425 | ||
426 | /* See ifname_valid_full(). */ | |
427 | ||
428 | for (char *p = str; *p != '\0'; p++) | |
429 | if (!ifname_valid_char(*p)) { | |
430 | *p = '_'; | |
431 | replaced++; | |
432 | } | |
433 | ||
434 | return replaced; | |
435 | } | |
436 | ||
393fcaf7 YW |
437 | size_t udev_replace_chars(char *str, const char *allow) { |
438 | size_t i = 0, replaced = 0; | |
439 | ||
440 | assert(str); | |
441 | ||
442 | /* allow chars in allow list, plain ascii, hex-escaping and valid utf8. */ | |
443 | ||
444 | while (str[i] != '\0') { | |
445 | int len; | |
446 | ||
447 | if (allow_listed_char_for_devnode(str[i], allow)) { | |
448 | i++; | |
449 | continue; | |
450 | } | |
451 | ||
452 | /* accept hex encoding */ | |
453 | if (str[i] == '\\' && str[i+1] == 'x') { | |
454 | i += 2; | |
455 | continue; | |
456 | } | |
457 | ||
458 | /* accept valid utf8 */ | |
f5fbe71d | 459 | len = utf8_encoded_valid_unichar(str + i, SIZE_MAX); |
393fcaf7 YW |
460 | if (len > 1) { |
461 | i += len; | |
462 | continue; | |
463 | } | |
464 | ||
465 | /* if space is allowed, replace whitespace with ordinary space */ | |
466 | if (isspace(str[i]) && allow && strchr(allow, ' ')) { | |
467 | str[i] = ' '; | |
468 | i++; | |
469 | replaced++; | |
470 | continue; | |
471 | } | |
472 | ||
473 | /* everything else is replaced with '_' */ | |
474 | str[i] = '_'; | |
475 | i++; | |
476 | replaced++; | |
477 | } | |
478 | return replaced; | |
479 | } | |
1223227f YW |
480 | |
481 | int udev_resolve_subsys_kernel(const char *string, char *result, size_t maxsize, bool read_value) { | |
482 | _cleanup_(sd_device_unrefp) sd_device *dev = NULL; | |
483 | _cleanup_free_ char *temp = NULL; | |
484 | char *subsys, *sysname, *attr; | |
485 | const char *val; | |
486 | int r; | |
487 | ||
488 | assert(string); | |
489 | assert(result); | |
490 | ||
491 | /* handle "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */ | |
492 | ||
493 | if (string[0] != '[') | |
494 | return -EINVAL; | |
495 | ||
496 | temp = strdup(string); | |
497 | if (!temp) | |
498 | return -ENOMEM; | |
499 | ||
500 | subsys = &temp[1]; | |
501 | ||
502 | sysname = strchr(subsys, '/'); | |
503 | if (!sysname) | |
504 | return -EINVAL; | |
505 | sysname[0] = '\0'; | |
506 | sysname = &sysname[1]; | |
507 | ||
508 | attr = strchr(sysname, ']'); | |
509 | if (!attr) | |
510 | return -EINVAL; | |
511 | attr[0] = '\0'; | |
512 | attr = &attr[1]; | |
513 | if (attr[0] == '/') | |
514 | attr = &attr[1]; | |
515 | if (attr[0] == '\0') | |
516 | attr = NULL; | |
517 | ||
518 | if (read_value && !attr) | |
519 | return -EINVAL; | |
520 | ||
521 | r = sd_device_new_from_subsystem_sysname(&dev, subsys, sysname); | |
522 | if (r < 0) | |
523 | return r; | |
524 | ||
525 | if (read_value) { | |
526 | r = sd_device_get_sysattr_value(dev, attr, &val); | |
acfc2a1d | 527 | if (r < 0 && !ERRNO_IS_PRIVILEGE(r) && r != -ENOENT) |
1223227f | 528 | return r; |
acfc2a1d | 529 | if (r >= 0) |
1223227f | 530 | strscpy(result, maxsize, val); |
acfc2a1d YW |
531 | else |
532 | result[0] = '\0'; | |
1223227f YW |
533 | log_debug("value '[%s/%s]%s' is '%s'", subsys, sysname, attr, result); |
534 | } else { | |
535 | r = sd_device_get_syspath(dev, &val); | |
536 | if (r < 0) | |
537 | return r; | |
538 | ||
539 | strscpyl(result, maxsize, val, attr ? "/" : NULL, attr ?: NULL, NULL); | |
540 | log_debug("path '[%s/%s]%s' is '%s'", subsys, sysname, strempty(attr), result); | |
541 | } | |
542 | return 0; | |
543 | } | |
bee33d05 | 544 | |
a1af9668 YW |
545 | bool devpath_conflict(const char *a, const char *b) { |
546 | /* This returns true when two paths are equivalent, or one is a child of another. */ | |
547 | ||
548 | if (!a || !b) | |
549 | return false; | |
550 | ||
551 | for (; *a != '\0' && *b != '\0'; a++, b++) | |
552 | if (*a != *b) | |
553 | return false; | |
554 | ||
555 | return *a == '/' || *b == '/' || *a == *b; | |
556 | } | |
557 | ||
bee33d05 YW |
558 | int udev_queue_is_empty(void) { |
559 | return access("/run/udev/queue", F_OK) < 0 ? | |
560 | (errno == ENOENT ? true : -errno) : false; | |
561 | } | |
562 | ||
563 | int udev_queue_init(void) { | |
564 | _cleanup_close_ int fd = -1; | |
565 | ||
566 | fd = inotify_init1(IN_CLOEXEC); | |
567 | if (fd < 0) | |
568 | return -errno; | |
569 | ||
570 | if (inotify_add_watch(fd, "/run/udev" , IN_DELETE) < 0) | |
571 | return -errno; | |
572 | ||
573 | return TAKE_FD(fd); | |
574 | } | |
06795b02 | 575 | |
795e86b4 YW |
576 | static int device_is_power_sink(sd_device *device) { |
577 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; | |
578 | bool found_source = false, found_sink = false; | |
579 | sd_device *parent, *d; | |
580 | int r; | |
581 | ||
582 | assert(device); | |
583 | ||
584 | /* USB-C power supply device has two power roles: source or sink. See, | |
0e685823 | 585 | * https://docs.kernel.org/admin-guide/abi-testing.html#abi-file-testing-sysfs-class-typec */ |
795e86b4 YW |
586 | |
587 | r = sd_device_enumerator_new(&e); | |
588 | if (r < 0) | |
589 | return r; | |
590 | ||
591 | r = sd_device_enumerator_allow_uninitialized(e); | |
592 | if (r < 0) | |
593 | return r; | |
594 | ||
595 | r = sd_device_enumerator_add_match_subsystem(e, "typec", true); | |
596 | if (r < 0) | |
597 | return r; | |
598 | ||
599 | r = sd_device_get_parent(device, &parent); | |
600 | if (r < 0) | |
601 | return r; | |
602 | ||
603 | r = sd_device_enumerator_add_match_parent(e, parent); | |
604 | if (r < 0) | |
605 | return r; | |
606 | ||
607 | FOREACH_DEVICE(e, d) { | |
608 | const char *val; | |
609 | ||
610 | r = sd_device_get_sysattr_value(d, "power_role", &val); | |
611 | if (r < 0) { | |
612 | if (r != -ENOENT) | |
613 | log_device_debug_errno(d, r, "Failed to read 'power_role' sysfs attribute, ignoring: %m"); | |
614 | continue; | |
615 | } | |
616 | ||
617 | if (strstr(val, "[source]")) { | |
618 | found_source = true; | |
619 | log_device_debug(d, "The USB type-C port is in power source mode."); | |
620 | } else if (strstr(val, "[sink]")) { | |
621 | found_sink = true; | |
622 | log_device_debug(d, "The USB type-C port is in power sink mode."); | |
623 | } | |
624 | } | |
625 | ||
626 | if (found_sink) | |
627 | log_device_debug(device, "The USB type-C device has at least one port in power sink mode."); | |
628 | else if (!found_source) | |
629 | log_device_debug(device, "The USB type-C device has no port in power source mode, assuming the device is in power sink mode."); | |
630 | else | |
631 | log_device_debug(device, "All USB type-C ports are in power source mode."); | |
632 | ||
633 | return found_sink || !found_source; | |
634 | } | |
635 | ||
06795b02 | 636 | int on_ac_power(void) { |
01d4ad3b | 637 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
4a52514b | 638 | bool found_ac_online = false, found_battery = false; |
01d4ad3b | 639 | sd_device *d; |
06795b02 YW |
640 | int r; |
641 | ||
01d4ad3b YW |
642 | r = sd_device_enumerator_new(&e); |
643 | if (r < 0) | |
644 | return r; | |
06795b02 | 645 | |
01d4ad3b YW |
646 | r = sd_device_enumerator_allow_uninitialized(e); |
647 | if (r < 0) | |
648 | return r; | |
06795b02 | 649 | |
01d4ad3b YW |
650 | r = sd_device_enumerator_add_match_subsystem(e, "power_supply", true); |
651 | if (r < 0) | |
652 | return r; | |
06795b02 | 653 | |
01d4ad3b | 654 | FOREACH_DEVICE(e, d) { |
4a52514b ZJS |
655 | /* See |
656 | * https://github.com/torvalds/linux/blob/4eef766b7d4d88f0b984781bc1bcb574a6eafdc7/include/linux/power_supply.h#L176 | |
657 | * for defined power source types. Also see: | |
658 | * https://docs.kernel.org/admin-guide/abi-testing.html#abi-file-testing-sysfs-class-power */ | |
06795b02 | 659 | |
4a52514b | 660 | const char *val; |
01d4ad3b YW |
661 | r = sd_device_get_sysattr_value(d, "type", &val); |
662 | if (r < 0) { | |
406fbeca | 663 | log_device_debug_errno(d, r, "Failed to read 'type' sysfs attribute, ignoring device: %m"); |
06795b02 | 664 | continue; |
01d4ad3b | 665 | } |
06795b02 | 666 | |
795e86b4 YW |
667 | /* Ignore USB-C power supply in source mode. See issue #21988. */ |
668 | if (streq(val, "USB")) { | |
669 | r = device_is_power_sink(d); | |
670 | if (r <= 0) { | |
671 | if (r < 0) | |
406fbeca | 672 | log_device_debug_errno(d, r, "Failed to determine the current power role, ignoring device: %m"); |
795e86b4 | 673 | else |
406fbeca | 674 | log_device_debug(d, "USB power supply is in source mode, ignoring device."); |
795e86b4 YW |
675 | continue; |
676 | } | |
677 | } | |
678 | ||
8676bdb7 | 679 | if (streq(val, "Battery")) { |
3c69e94a | 680 | r = sd_device_get_sysattr_value(d, "scope", &val); |
8676bdb7 YW |
681 | if (r < 0) { |
682 | if (r != -ENOENT) | |
683 | log_device_debug_errno(d, r, "Failed to read 'scope' sysfs attribute, ignoring: %m"); | |
684 | } else if (streq(val, "Device")) { | |
685 | log_device_debug(d, "The power supply is a device battery, ignoring device."); | |
3c69e94a ZJS |
686 | continue; |
687 | } | |
688 | ||
4a52514b ZJS |
689 | found_battery = true; |
690 | log_device_debug(d, "The power supply is battery."); | |
06795b02 | 691 | continue; |
01d4ad3b | 692 | } |
06795b02 | 693 | |
4a52514b | 694 | r = device_get_sysattr_unsigned(d, "online", NULL); |
01d4ad3b | 695 | if (r < 0) { |
8676bdb7 | 696 | log_device_debug_errno(d, r, "Failed to query 'online' sysfs attribute, ignoring device: %m"); |
01d4ad3b | 697 | continue; |
4a52514b ZJS |
698 | } else if (r > 0) /* At least 1 and 2 are defined as different types of 'online' */ |
699 | found_ac_online = true; | |
01d4ad3b | 700 | |
4a52514b | 701 | log_device_debug(d, "The power supply is currently %s.", r > 0 ? "online" : "offline"); |
06795b02 YW |
702 | } |
703 | ||
4a52514b ZJS |
704 | if (found_ac_online) { |
705 | log_debug("Found at least one online non-battery power supply, system is running on AC."); | |
706 | return true; | |
707 | } else if (found_battery) { | |
708 | log_debug("Found battery and no online power sources, assuming system is running from battery."); | |
709 | return false; | |
710 | } else { | |
711 | log_debug("No power supply reported online and no battery, assuming system is running on AC."); | |
712 | return true; | |
713 | } | |
06795b02 | 714 | } |
f92c5bb1 YW |
715 | |
716 | bool udev_available(void) { | |
717 | static int cache = -1; | |
718 | ||
719 | /* The service systemd-udevd is started only when /sys is read write. | |
720 | * See systemd-udevd.service: ConditionPathIsReadWrite=/sys | |
721 | * Also, our container interface (http://systemd.io/CONTAINER_INTERFACE/) states that /sys must | |
722 | * be mounted in read-only mode in containers. */ | |
723 | ||
724 | if (cache >= 0) | |
725 | return cache; | |
726 | ||
9fa31df6 | 727 | return (cache = (path_is_read_only_fs("/sys/") <= 0)); |
f92c5bb1 | 728 | } |