]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ | |
2 | ||
3 | #include <sys/stat.h> | |
4 | #include <sys/sysmacros.h> | |
5 | #include <unistd.h> | |
6 | ||
7 | #include "sd-device.h" | |
8 | ||
9 | #include "alloc-util.h" | |
10 | #include "device-internal.h" | |
11 | #include "device-private.h" | |
12 | #include "device-util.h" | |
13 | #include "extract-word.h" | |
14 | #include "fd-util.h" | |
15 | #include "fileio.h" | |
16 | #include "fs-util.h" | |
17 | #include "hashmap.h" | |
18 | #include "mkdir.h" | |
19 | #include "nulstr-util.h" | |
20 | #include "parse-util.h" | |
21 | #include "path-util.h" | |
22 | #include "set.h" | |
23 | #include "string-table.h" | |
24 | #include "string-util.h" | |
25 | #include "strv.h" | |
26 | #include "strxcpyx.h" | |
27 | #include "time-util.h" | |
28 | #include "tmpfile-util.h" | |
29 | #include "user-util.h" | |
30 | ||
31 | int device_add_property(sd_device *device, const char *key, const char *value) { | |
32 | int r; | |
33 | ||
34 | assert(device); | |
35 | assert(key); | |
36 | ||
37 | r = device_add_property_aux(device, key, value, false); | |
38 | if (r < 0) | |
39 | return r; | |
40 | ||
41 | if (key[0] != '.') { | |
42 | r = device_add_property_aux(device, key, value, true); | |
43 | if (r < 0) | |
44 | return r; | |
45 | } | |
46 | ||
47 | return 0; | |
48 | } | |
49 | ||
50 | int device_add_propertyf(sd_device *device, const char *key, const char *format, ...) { | |
51 | _cleanup_free_ char *value = NULL; | |
52 | va_list ap; | |
53 | int r; | |
54 | ||
55 | assert(device); | |
56 | assert(key); | |
57 | ||
58 | if (!format) | |
59 | return device_add_property(device, key, NULL); | |
60 | ||
61 | va_start(ap, format); | |
62 | r = vasprintf(&value, format, ap); | |
63 | va_end(ap); | |
64 | ||
65 | if (r < 0) | |
66 | return -ENOMEM; | |
67 | ||
68 | return device_add_property(device, key, value); | |
69 | } | |
70 | ||
71 | void device_set_devlink_priority(sd_device *device, int priority) { | |
72 | assert(device); | |
73 | ||
74 | device->devlink_priority = priority; | |
75 | } | |
76 | ||
77 | void device_set_is_initialized(sd_device *device) { | |
78 | assert(device); | |
79 | ||
80 | device->is_initialized = true; | |
81 | } | |
82 | ||
83 | int device_ensure_usec_initialized(sd_device *device, sd_device *device_old) { | |
84 | usec_t when; | |
85 | ||
86 | assert(device); | |
87 | ||
88 | if (device_old && device_old->usec_initialized > 0) | |
89 | when = device_old->usec_initialized; | |
90 | else | |
91 | when = now(CLOCK_MONOTONIC); | |
92 | ||
93 | return device_set_usec_initialized(device, when); | |
94 | } | |
95 | ||
96 | uint64_t device_get_properties_generation(sd_device *device) { | |
97 | assert(device); | |
98 | ||
99 | return device->properties_generation; | |
100 | } | |
101 | ||
102 | uint64_t device_get_tags_generation(sd_device *device) { | |
103 | assert(device); | |
104 | ||
105 | return device->tags_generation; | |
106 | } | |
107 | ||
108 | uint64_t device_get_devlinks_generation(sd_device *device) { | |
109 | assert(device); | |
110 | ||
111 | return device->devlinks_generation; | |
112 | } | |
113 | ||
114 | int device_get_devnode_mode(sd_device *device, mode_t *ret) { | |
115 | int r; | |
116 | ||
117 | assert(device); | |
118 | ||
119 | r = device_read_uevent_file(device); | |
120 | if (r < 0) | |
121 | return r; | |
122 | ||
123 | r = device_read_db(device); | |
124 | if (r < 0) | |
125 | return r; | |
126 | ||
127 | if (device->devmode == MODE_INVALID) | |
128 | return -ENOENT; | |
129 | ||
130 | if (ret) | |
131 | *ret = device->devmode; | |
132 | ||
133 | return 0; | |
134 | } | |
135 | ||
136 | int device_get_devnode_uid(sd_device *device, uid_t *ret) { | |
137 | int r; | |
138 | ||
139 | assert(device); | |
140 | ||
141 | r = device_read_uevent_file(device); | |
142 | if (r < 0) | |
143 | return r; | |
144 | ||
145 | r = device_read_db(device); | |
146 | if (r < 0) | |
147 | return r; | |
148 | ||
149 | if (device->devuid == UID_INVALID) | |
150 | return -ENOENT; | |
151 | ||
152 | if (ret) | |
153 | *ret = device->devuid; | |
154 | ||
155 | return 0; | |
156 | } | |
157 | ||
158 | int device_set_devuid(sd_device *device, const char *uid) { | |
159 | uid_t u; | |
160 | int r; | |
161 | ||
162 | assert(device); | |
163 | assert(uid); | |
164 | ||
165 | r = parse_uid(uid, &u); | |
166 | if (r < 0) | |
167 | return r; | |
168 | ||
169 | r = device_add_property_internal(device, "DEVUID", uid); | |
170 | if (r < 0) | |
171 | return r; | |
172 | ||
173 | device->devuid = u; | |
174 | ||
175 | return 0; | |
176 | } | |
177 | ||
178 | int device_get_devnode_gid(sd_device *device, gid_t *ret) { | |
179 | int r; | |
180 | ||
181 | assert(device); | |
182 | ||
183 | r = device_read_uevent_file(device); | |
184 | if (r < 0) | |
185 | return r; | |
186 | ||
187 | r = device_read_db(device); | |
188 | if (r < 0) | |
189 | return r; | |
190 | ||
191 | if (device->devgid == GID_INVALID) | |
192 | return -ENOENT; | |
193 | ||
194 | if (ret) | |
195 | *ret = device->devgid; | |
196 | ||
197 | return 0; | |
198 | } | |
199 | ||
200 | int device_set_devgid(sd_device *device, const char *gid) { | |
201 | gid_t g; | |
202 | int r; | |
203 | ||
204 | assert(device); | |
205 | assert(gid); | |
206 | ||
207 | r = parse_gid(gid, &g); | |
208 | if (r < 0) | |
209 | return r; | |
210 | ||
211 | r = device_add_property_internal(device, "DEVGID", gid); | |
212 | if (r < 0) | |
213 | return r; | |
214 | ||
215 | device->devgid = g; | |
216 | ||
217 | return 0; | |
218 | } | |
219 | ||
220 | int device_set_action(sd_device *device, sd_device_action_t a) { | |
221 | int r; | |
222 | ||
223 | assert(device); | |
224 | assert(a >= 0 && a < _SD_DEVICE_ACTION_MAX); | |
225 | ||
226 | r = device_add_property_internal(device, "ACTION", device_action_to_string(a)); | |
227 | if (r < 0) | |
228 | return r; | |
229 | ||
230 | device->action = a; | |
231 | ||
232 | return 0; | |
233 | } | |
234 | ||
235 | static int device_set_action_from_string(sd_device *device, const char *action) { | |
236 | sd_device_action_t a; | |
237 | ||
238 | assert(device); | |
239 | assert(action); | |
240 | ||
241 | a = device_action_from_string(action); | |
242 | if (a < 0) | |
243 | return a; | |
244 | ||
245 | return device_set_action(device, a); | |
246 | } | |
247 | ||
248 | static int device_set_seqnum(sd_device *device, const char *str) { | |
249 | uint64_t seqnum; | |
250 | int r; | |
251 | ||
252 | assert(device); | |
253 | assert(str); | |
254 | ||
255 | r = safe_atou64(str, &seqnum); | |
256 | if (r < 0) | |
257 | return r; | |
258 | if (seqnum == 0) | |
259 | return -EINVAL; | |
260 | ||
261 | r = device_add_property_internal(device, "SEQNUM", str); | |
262 | if (r < 0) | |
263 | return r; | |
264 | ||
265 | device->seqnum = seqnum; | |
266 | ||
267 | return 0; | |
268 | } | |
269 | ||
270 | static int device_amend(sd_device *device, const char *key, const char *value) { | |
271 | int r; | |
272 | ||
273 | assert(device); | |
274 | assert(key); | |
275 | assert(value); | |
276 | ||
277 | if (streq(key, "DEVPATH")) { | |
278 | char *path; | |
279 | ||
280 | path = strjoina("/sys", value); | |
281 | ||
282 | /* the caller must verify or trust this data (e.g., if it comes from the kernel) */ | |
283 | r = device_set_syspath(device, path, false); | |
284 | if (r < 0) | |
285 | return log_device_debug_errno(device, r, "sd-device: Failed to set syspath to '%s': %m", path); | |
286 | } else if (streq(key, "SUBSYSTEM")) { | |
287 | r = device_set_subsystem(device, value); | |
288 | if (r < 0) | |
289 | return log_device_debug_errno(device, r, "sd-device: Failed to set subsystem to '%s': %m", value); | |
290 | } else if (streq(key, "DEVTYPE")) { | |
291 | r = device_set_devtype(device, value); | |
292 | if (r < 0) | |
293 | return log_device_debug_errno(device, r, "sd-device: Failed to set devtype to '%s': %m", value); | |
294 | } else if (streq(key, "DEVNAME")) { | |
295 | r = device_set_devname(device, value); | |
296 | if (r < 0) | |
297 | return log_device_debug_errno(device, r, "sd-device: Failed to set devname to '%s': %m", value); | |
298 | } else if (streq(key, "USEC_INITIALIZED")) { | |
299 | usec_t t; | |
300 | ||
301 | r = safe_atou64(value, &t); | |
302 | if (r < 0) | |
303 | return log_device_debug_errno(device, r, "sd-device: Failed to parse timestamp '%s': %m", value); | |
304 | ||
305 | r = device_set_usec_initialized(device, t); | |
306 | if (r < 0) | |
307 | return log_device_debug_errno(device, r, "sd-device: Failed to set usec-initialized to '%s': %m", value); | |
308 | } else if (streq(key, "DRIVER")) { | |
309 | r = device_set_driver(device, value); | |
310 | if (r < 0) | |
311 | return log_device_debug_errno(device, r, "sd-device: Failed to set driver to '%s': %m", value); | |
312 | } else if (streq(key, "IFINDEX")) { | |
313 | r = device_set_ifindex(device, value); | |
314 | if (r < 0) | |
315 | return log_device_debug_errno(device, r, "sd-device: Failed to set ifindex to '%s': %m", value); | |
316 | } else if (streq(key, "DEVMODE")) { | |
317 | r = device_set_devmode(device, value); | |
318 | if (r < 0) | |
319 | return log_device_debug_errno(device, r, "sd-device: Failed to set devmode to '%s': %m", value); | |
320 | } else if (streq(key, "DEVUID")) { | |
321 | r = device_set_devuid(device, value); | |
322 | if (r < 0) | |
323 | return log_device_debug_errno(device, r, "sd-device: Failed to set devuid to '%s': %m", value); | |
324 | } else if (streq(key, "DEVGID")) { | |
325 | r = device_set_devgid(device, value); | |
326 | if (r < 0) | |
327 | return log_device_debug_errno(device, r, "sd-device: Failed to set devgid to '%s': %m", value); | |
328 | } else if (streq(key, "ACTION")) { | |
329 | r = device_set_action_from_string(device, value); | |
330 | if (r < 0) | |
331 | return log_device_debug_errno(device, r, "sd-device: Failed to set action to '%s': %m", value); | |
332 | } else if (streq(key, "SEQNUM")) { | |
333 | r = device_set_seqnum(device, value); | |
334 | if (r < 0) | |
335 | return log_device_debug_errno(device, r, "sd-device: Failed to set SEQNUM to '%s': %m", value); | |
336 | } else if (streq(key, "DISKSEQ")) { | |
337 | r = device_set_diskseq(device, value); | |
338 | if (r < 0) | |
339 | return log_device_debug_errno(device, r, "sd-device: Failed to set DISKSEQ to '%s': %m", value); | |
340 | } else if (streq(key, "DEVLINKS")) { | |
341 | for (const char *p = value;;) { | |
342 | _cleanup_free_ char *word = NULL; | |
343 | ||
344 | /* udev rules may set escaped strings, and sd-device does not modify the input | |
345 | * strings. So, it is also necessary to keep the strings received through | |
346 | * sd-device-monitor. */ | |
347 | r = extract_first_word(&p, &word, NULL, EXTRACT_RETAIN_ESCAPE); | |
348 | if (r < 0) | |
349 | return r; | |
350 | if (r == 0) | |
351 | break; | |
352 | ||
353 | r = device_add_devlink(device, word); | |
354 | if (r < 0) | |
355 | return log_device_debug_errno(device, r, "sd-device: Failed to add devlink '%s': %m", word); | |
356 | } | |
357 | } else if (STR_IN_SET(key, "TAGS", "CURRENT_TAGS")) { | |
358 | for (const char *p = value;;) { | |
359 | _cleanup_free_ char *word = NULL; | |
360 | ||
361 | r = extract_first_word(&p, &word, ":", EXTRACT_DONT_COALESCE_SEPARATORS); | |
362 | if (r < 0) | |
363 | return r; | |
364 | if (r == 0) | |
365 | break; | |
366 | if (isempty(word)) | |
367 | continue; | |
368 | ||
369 | r = device_add_tag(device, word, streq(key, "CURRENT_TAGS")); | |
370 | if (r < 0) | |
371 | return log_device_debug_errno(device, r, "sd-device: Failed to add tag '%s': %m", word); | |
372 | } | |
373 | } else if (streq(key, "UDEV_DATABASE_VERSION")) { | |
374 | r = safe_atou(value, &device->database_version); | |
375 | if (r < 0) | |
376 | return log_device_debug_errno(device, r, "sd-device: Failed to parse udev database version '%s': %m", value); | |
377 | } else { | |
378 | r = device_add_property_internal(device, key, value); | |
379 | if (r < 0) | |
380 | return log_device_debug_errno(device, r, "sd-device: Failed to add property '%s=%s': %m", key, value); | |
381 | } | |
382 | ||
383 | return 0; | |
384 | } | |
385 | ||
386 | static int device_append( | |
387 | sd_device *device, | |
388 | char *key, | |
389 | const char **_major, | |
390 | const char **_minor) { | |
391 | ||
392 | const char *major = NULL, *minor = NULL; | |
393 | char *value; | |
394 | int r; | |
395 | ||
396 | assert(device); | |
397 | assert(key); | |
398 | assert(_major); | |
399 | assert(_minor); | |
400 | ||
401 | value = strchr(key, '='); | |
402 | if (!value) | |
403 | return log_device_debug_errno(device, SYNTHETIC_ERRNO(EINVAL), | |
404 | "sd-device: Not a key-value pair: '%s'", key); | |
405 | ||
406 | *value = '\0'; | |
407 | ||
408 | value++; | |
409 | ||
410 | if (streq(key, "MAJOR")) | |
411 | major = value; | |
412 | else if (streq(key, "MINOR")) | |
413 | minor = value; | |
414 | else { | |
415 | r = device_amend(device, key, value); | |
416 | if (r < 0) | |
417 | return r; | |
418 | } | |
419 | ||
420 | if (major) | |
421 | *_major = major; | |
422 | ||
423 | if (minor) | |
424 | *_minor = minor; | |
425 | ||
426 | return 0; | |
427 | } | |
428 | ||
429 | void device_seal(sd_device *device) { | |
430 | assert(device); | |
431 | ||
432 | device->sealed = true; | |
433 | } | |
434 | ||
435 | static int device_verify(sd_device *device) { | |
436 | int r; | |
437 | ||
438 | assert(device); | |
439 | ||
440 | if (!device->devpath || !device->subsystem || device->action < 0 || device->seqnum == 0) | |
441 | return log_device_debug_errno(device, SYNTHETIC_ERRNO(EINVAL), | |
442 | "sd-device: Device created from strv or nulstr lacks devpath, subsystem, action or seqnum."); | |
443 | ||
444 | r = device_in_subsystem(device, "drivers"); | |
445 | if (r < 0) | |
446 | return log_device_debug_errno(device, r, "sd-device: Failed to check if the device is a driver: %m"); | |
447 | if (r > 0) { | |
448 | r = device_set_drivers_subsystem(device); | |
449 | if (r < 0) | |
450 | return log_device_debug_errno(device, r, | |
451 | "sd-device: Failed to set driver subsystem: %m"); | |
452 | } | |
453 | ||
454 | device->sealed = true; | |
455 | ||
456 | return 0; | |
457 | } | |
458 | ||
459 | int device_new_from_strv(sd_device **ret, char **strv) { | |
460 | _cleanup_(sd_device_unrefp) sd_device *device = NULL; | |
461 | const char *major = NULL, *minor = NULL; | |
462 | int r; | |
463 | ||
464 | assert(ret); | |
465 | assert(strv); | |
466 | ||
467 | r = device_new_aux(&device); | |
468 | if (r < 0) | |
469 | return r; | |
470 | ||
471 | STRV_FOREACH(key, strv) { | |
472 | r = device_append(device, *key, &major, &minor); | |
473 | if (r < 0) | |
474 | return r; | |
475 | } | |
476 | ||
477 | if (major) { | |
478 | r = device_set_devnum(device, major, minor); | |
479 | if (r < 0) | |
480 | return log_device_debug_errno(device, r, "sd-device: Failed to set devnum %s:%s: %m", major, minor); | |
481 | } | |
482 | ||
483 | r = device_verify(device); | |
484 | if (r < 0) | |
485 | return r; | |
486 | ||
487 | *ret = TAKE_PTR(device); | |
488 | ||
489 | return 0; | |
490 | } | |
491 | ||
492 | int device_new_from_nulstr(sd_device **ret, char *nulstr, size_t len) { | |
493 | _cleanup_(sd_device_unrefp) sd_device *device = NULL; | |
494 | const char *major = NULL, *minor = NULL; | |
495 | int r; | |
496 | ||
497 | assert(ret); | |
498 | assert(nulstr); | |
499 | assert(len); | |
500 | ||
501 | r = device_new_aux(&device); | |
502 | if (r < 0) | |
503 | return r; | |
504 | ||
505 | for (size_t i = 0; i < len; ) { | |
506 | char *key; | |
507 | const char *end; | |
508 | ||
509 | key = nulstr + i; | |
510 | end = memchr(key, '\0', len - i); | |
511 | if (!end) | |
512 | return log_device_debug_errno(device, SYNTHETIC_ERRNO(EINVAL), | |
513 | "sd-device: Failed to parse nulstr"); | |
514 | ||
515 | i += end - key + 1; | |
516 | ||
517 | /* netlink messages for some devices contain an unwanted newline at the end of value. | |
518 | * Let's drop the newline and remaining characters after the newline. */ | |
519 | truncate_nl(key); | |
520 | ||
521 | r = device_append(device, key, &major, &minor); | |
522 | if (r < 0) | |
523 | return r; | |
524 | } | |
525 | ||
526 | if (major) { | |
527 | r = device_set_devnum(device, major, minor); | |
528 | if (r < 0) | |
529 | return log_device_debug_errno(device, r, "sd-device: Failed to set devnum %s:%s: %m", major, minor); | |
530 | } | |
531 | ||
532 | r = device_verify(device); | |
533 | if (r < 0) | |
534 | return r; | |
535 | ||
536 | *ret = TAKE_PTR(device); | |
537 | ||
538 | return 0; | |
539 | } | |
540 | ||
541 | static int device_update_properties_bufs(sd_device *device) { | |
542 | _cleanup_free_ char **buf_strv = NULL, *buf_nulstr = NULL; | |
543 | size_t nulstr_len = 0, num = 0; | |
544 | ||
545 | assert(device); | |
546 | ||
547 | if (!device->properties_buf_outdated) | |
548 | return 0; | |
549 | ||
550 | /* append udev database version */ | |
551 | buf_nulstr = newdup(char, "UDEV_DATABASE_VERSION=" STRINGIFY(LATEST_UDEV_DATABASE_VERSION) "\0", | |
552 | STRLEN("UDEV_DATABASE_VERSION=" STRINGIFY(LATEST_UDEV_DATABASE_VERSION)) + 2); | |
553 | if (!buf_nulstr) | |
554 | return -ENOMEM; | |
555 | ||
556 | nulstr_len += STRLEN("UDEV_DATABASE_VERSION=" STRINGIFY(LATEST_UDEV_DATABASE_VERSION)) + 1; | |
557 | num++; | |
558 | ||
559 | FOREACH_DEVICE_PROPERTY(device, prop, val) { | |
560 | size_t len = 0; | |
561 | ||
562 | len = strlen(prop) + 1 + strlen(val); | |
563 | ||
564 | buf_nulstr = GREEDY_REALLOC0(buf_nulstr, nulstr_len + len + 2); | |
565 | if (!buf_nulstr) | |
566 | return -ENOMEM; | |
567 | ||
568 | strscpyl(buf_nulstr + nulstr_len, len + 1, prop, "=", val, NULL); | |
569 | nulstr_len += len + 1; | |
570 | num++; | |
571 | } | |
572 | ||
573 | /* build buf_strv from buf_nulstr */ | |
574 | buf_strv = new0(char*, num + 1); | |
575 | if (!buf_strv) | |
576 | return -ENOMEM; | |
577 | ||
578 | size_t i = 0; | |
579 | NULSTR_FOREACH(p, buf_nulstr) | |
580 | buf_strv[i++] = p; | |
581 | assert(i == num); | |
582 | ||
583 | free_and_replace(device->properties_nulstr, buf_nulstr); | |
584 | device->properties_nulstr_len = nulstr_len; | |
585 | free_and_replace(device->properties_strv, buf_strv); | |
586 | ||
587 | device->properties_buf_outdated = false; | |
588 | return 0; | |
589 | } | |
590 | ||
591 | int device_get_properties_nulstr(sd_device *device, const char **ret_nulstr, size_t *ret_len) { | |
592 | int r; | |
593 | ||
594 | assert(device); | |
595 | ||
596 | r = device_update_properties_bufs(device); | |
597 | if (r < 0) | |
598 | return r; | |
599 | ||
600 | if (ret_nulstr) | |
601 | *ret_nulstr = device->properties_nulstr; | |
602 | if (ret_len) | |
603 | *ret_len = device->properties_nulstr_len; | |
604 | ||
605 | return 0; | |
606 | } | |
607 | ||
608 | int device_get_properties_strv(sd_device *device, char ***ret) { | |
609 | int r; | |
610 | ||
611 | assert(device); | |
612 | ||
613 | r = device_update_properties_bufs(device); | |
614 | if (r < 0) | |
615 | return r; | |
616 | ||
617 | if (ret) | |
618 | *ret = device->properties_strv; | |
619 | ||
620 | return 0; | |
621 | } | |
622 | ||
623 | int device_get_devlink_priority(sd_device *device, int *ret) { | |
624 | int r; | |
625 | ||
626 | assert(device); | |
627 | ||
628 | r = device_read_db(device); | |
629 | if (r < 0) | |
630 | return r; | |
631 | ||
632 | if (ret) | |
633 | *ret = device->devlink_priority; | |
634 | ||
635 | return 0; | |
636 | } | |
637 | ||
638 | int device_clone_with_db(sd_device *device, sd_device **ret) { | |
639 | _cleanup_(sd_device_unrefp) sd_device *dest = NULL; | |
640 | const char *key, *val; | |
641 | int r; | |
642 | ||
643 | assert(device); | |
644 | assert(ret); | |
645 | ||
646 | /* The device may be already removed. Let's copy minimal set of information that was obtained through | |
647 | * netlink socket. */ | |
648 | ||
649 | r = device_new_aux(&dest); | |
650 | if (r < 0) | |
651 | return r; | |
652 | ||
653 | /* Seal device to prevent reading the uevent file, as the device may have been already removed. */ | |
654 | dest->sealed = true; | |
655 | ||
656 | /* Copy syspath, then also devname, sysname or sysnum can be obtained. */ | |
657 | r = device_set_syspath(dest, device->syspath, false); | |
658 | if (r < 0) | |
659 | return r; | |
660 | ||
661 | /* Copy other information stored in database. Here, do not use FOREACH_DEVICE_PROPERTY() and | |
662 | * sd_device_get_property_value(), as they calls device_properties_prepare() -> | |
663 | * device_read_uevent_file(), but as commented in the above, the device may be already removed and | |
664 | * reading uevent file may fail. */ | |
665 | ORDERED_HASHMAP_FOREACH_KEY(val, key, device->properties) { | |
666 | if (streq(key, "MINOR")) | |
667 | continue; | |
668 | ||
669 | if (streq(key, "MAJOR")) { | |
670 | const char *minor = NULL; | |
671 | ||
672 | minor = ordered_hashmap_get(device->properties, "MINOR"); | |
673 | r = device_set_devnum(dest, val, minor); | |
674 | } else | |
675 | r = device_amend(dest, key, val); | |
676 | if (r < 0) | |
677 | return r; | |
678 | ||
679 | if (streq(key, "SUBSYSTEM") && streq(val, "drivers")) { | |
680 | r = free_and_strdup(&dest->driver_subsystem, device->driver_subsystem); | |
681 | if (r < 0) | |
682 | return r; | |
683 | } | |
684 | } | |
685 | ||
686 | /* Finally, read the udev database. */ | |
687 | r = device_read_db_internal(dest, /* force = */ true); | |
688 | if (r < 0) | |
689 | return r; | |
690 | ||
691 | *ret = TAKE_PTR(dest); | |
692 | return 0; | |
693 | } | |
694 | ||
695 | void device_cleanup_tags(sd_device *device) { | |
696 | assert(device); | |
697 | ||
698 | device->all_tags = set_free(device->all_tags); | |
699 | device->current_tags = set_free(device->current_tags); | |
700 | device->property_tags_outdated = true; | |
701 | device->tags_generation++; | |
702 | } | |
703 | ||
704 | void device_cleanup_devlinks(sd_device *device) { | |
705 | assert(device); | |
706 | ||
707 | set_free(device->devlinks); | |
708 | device->devlinks = NULL; | |
709 | device->property_devlinks_outdated = true; | |
710 | device->devlinks_generation++; | |
711 | } | |
712 | ||
713 | void device_remove_tag(sd_device *device, const char *tag) { | |
714 | assert(device); | |
715 | assert(tag); | |
716 | ||
717 | free(set_remove(device->current_tags, tag)); | |
718 | device->property_tags_outdated = true; | |
719 | device->tags_generation++; | |
720 | } | |
721 | ||
722 | static int device_tag(sd_device *device, const char *tag, bool add) { | |
723 | const char *id; | |
724 | char *path; | |
725 | int r; | |
726 | ||
727 | assert(device); | |
728 | assert(tag); | |
729 | ||
730 | r = sd_device_get_device_id(device, &id); | |
731 | if (r < 0) | |
732 | return r; | |
733 | ||
734 | path = strjoina("/run/udev/tags/", tag, "/", id); | |
735 | ||
736 | if (add) | |
737 | return touch_file(path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, 0444); | |
738 | ||
739 | if (unlink(path) < 0 && errno != ENOENT) | |
740 | return -errno; | |
741 | ||
742 | return 0; | |
743 | } | |
744 | ||
745 | int device_tag_index(sd_device *device, sd_device *device_old, bool add) { | |
746 | int r = 0, k; | |
747 | ||
748 | if (add && device_old) | |
749 | /* delete possible left-over tags */ | |
750 | FOREACH_DEVICE_TAG(device_old, tag) | |
751 | if (!sd_device_has_tag(device, tag)) { | |
752 | k = device_tag(device_old, tag, false); | |
753 | if (r >= 0 && k < 0) | |
754 | r = k; | |
755 | } | |
756 | ||
757 | FOREACH_DEVICE_TAG(device, tag) { | |
758 | k = device_tag(device, tag, add); | |
759 | if (r >= 0 && k < 0) | |
760 | r = k; | |
761 | } | |
762 | ||
763 | return r; | |
764 | } | |
765 | ||
766 | static bool device_has_info(sd_device *device) { | |
767 | assert(device); | |
768 | ||
769 | if (!set_isempty(device->devlinks)) | |
770 | return true; | |
771 | ||
772 | if (device->devlink_priority != 0) | |
773 | return true; | |
774 | ||
775 | if (!ordered_hashmap_isempty(device->properties_db)) | |
776 | return true; | |
777 | ||
778 | if (!set_isempty(device->all_tags)) | |
779 | return true; | |
780 | ||
781 | if (!set_isempty(device->current_tags)) | |
782 | return true; | |
783 | ||
784 | return false; | |
785 | } | |
786 | ||
787 | bool device_should_have_db(sd_device *device) { | |
788 | assert(device); | |
789 | ||
790 | if (device_has_info(device)) | |
791 | return true; | |
792 | ||
793 | if (major(device->devnum) != 0) | |
794 | return true; | |
795 | ||
796 | if (device->ifindex != 0) | |
797 | return true; | |
798 | ||
799 | return false; | |
800 | } | |
801 | ||
802 | void device_set_db_persist(sd_device *device) { | |
803 | assert(device); | |
804 | ||
805 | device->db_persist = true; | |
806 | } | |
807 | ||
808 | static int device_get_db_path(sd_device *device, char **ret) { | |
809 | const char *id; | |
810 | char *path; | |
811 | int r; | |
812 | ||
813 | assert(device); | |
814 | assert(ret); | |
815 | ||
816 | r = sd_device_get_device_id(device, &id); | |
817 | if (r < 0) | |
818 | return r; | |
819 | ||
820 | path = path_join("/run/udev/data/", id); | |
821 | if (!path) | |
822 | return -ENOMEM; | |
823 | ||
824 | *ret = path; | |
825 | return 0; | |
826 | } | |
827 | ||
828 | int device_has_db(sd_device *device) { | |
829 | _cleanup_free_ char *path = NULL; | |
830 | int r; | |
831 | ||
832 | assert(device); | |
833 | ||
834 | r = device_get_db_path(device, &path); | |
835 | if (r < 0) | |
836 | return r; | |
837 | ||
838 | return access(path, F_OK) >= 0; | |
839 | } | |
840 | ||
841 | int device_update_db(sd_device *device) { | |
842 | _cleanup_(unlink_and_freep) char *path = NULL, *path_tmp = NULL; | |
843 | _cleanup_fclose_ FILE *f = NULL; | |
844 | int r; | |
845 | ||
846 | assert(device); | |
847 | ||
848 | /* do not store anything for otherwise empty devices */ | |
849 | if (!device_should_have_db(device)) | |
850 | return device_delete_db(device); | |
851 | ||
852 | r = device_get_db_path(device, &path); | |
853 | if (r < 0) | |
854 | return r; | |
855 | ||
856 | /* write a database file */ | |
857 | r = mkdir_parents(path, 0755); | |
858 | if (r < 0) | |
859 | return log_device_debug_errno(device, r, | |
860 | "sd-device: Failed to create parent directories of '%s': %m", | |
861 | path); | |
862 | ||
863 | r = fopen_temporary(path, &f, &path_tmp); | |
864 | if (r < 0) | |
865 | return log_device_debug_errno(device, r, | |
866 | "sd-device: Failed to create temporary file for database file '%s': %m", | |
867 | path); | |
868 | ||
869 | /* set 'sticky' bit to indicate that we should not clean the database when we transition from initrd | |
870 | * to the real root */ | |
871 | if (fchmod(fileno(f), device->db_persist ? 01644 : 0644) < 0) | |
872 | return log_device_debug_errno(device, errno, | |
873 | "sd-device: Failed to chmod temporary database file '%s': %m", | |
874 | path_tmp); | |
875 | ||
876 | if (device_has_info(device)) { | |
877 | const char *property, *value, *ct; | |
878 | ||
879 | if (major(device->devnum) > 0) { | |
880 | FOREACH_DEVICE_DEVLINK(device, devlink) | |
881 | fprintf(f, "S:%s\n", devlink + STRLEN("/dev/")); | |
882 | ||
883 | if (device->devlink_priority != 0) | |
884 | fprintf(f, "L:%i\n", device->devlink_priority); | |
885 | } | |
886 | ||
887 | if (device->usec_initialized > 0) | |
888 | fprintf(f, "I:"USEC_FMT"\n", device->usec_initialized); | |
889 | ||
890 | ORDERED_HASHMAP_FOREACH_KEY(value, property, device->properties_db) | |
891 | fprintf(f, "E:%s=%s\n", property, value); | |
892 | ||
893 | FOREACH_DEVICE_TAG(device, tag) | |
894 | fprintf(f, "G:%s\n", tag); /* Any tag */ | |
895 | ||
896 | SET_FOREACH(ct, device->current_tags) | |
897 | fprintf(f, "Q:%s\n", ct); /* Current tag */ | |
898 | ||
899 | /* Always write the latest database version here, instead of the value stored in | |
900 | * device->database_version, as which may be 0. */ | |
901 | fputs("V:" STRINGIFY(LATEST_UDEV_DATABASE_VERSION) "\n", f); | |
902 | } | |
903 | ||
904 | r = fflush_and_check(f); | |
905 | if (r < 0) | |
906 | return log_device_debug_errno(device, r, | |
907 | "sd-device: Failed to flush temporary database file '%s': %m", | |
908 | path_tmp); | |
909 | ||
910 | if (rename(path_tmp, path) < 0) | |
911 | return log_device_debug_errno(device, errno, | |
912 | "sd-device: Failed to rename temporary database file '%s' to '%s': %m", | |
913 | path_tmp, path); | |
914 | ||
915 | log_device_debug(device, "sd-device: Created database file '%s' for '%s'.", path, device->devpath); | |
916 | ||
917 | path_tmp = mfree(path_tmp); | |
918 | path = mfree(path); | |
919 | ||
920 | return 0; | |
921 | } | |
922 | ||
923 | int device_delete_db(sd_device *device) { | |
924 | _cleanup_free_ char *path = NULL; | |
925 | int r; | |
926 | ||
927 | assert(device); | |
928 | ||
929 | r = device_get_db_path(device, &path); | |
930 | if (r < 0) | |
931 | return r; | |
932 | ||
933 | if (unlink(path) < 0 && errno != ENOENT) | |
934 | return -errno; | |
935 | ||
936 | return 0; | |
937 | } | |
938 | ||
939 | int device_read_db_internal(sd_device *device, bool force) { | |
940 | _cleanup_free_ char *path = NULL; | |
941 | int r; | |
942 | ||
943 | assert(device); | |
944 | ||
945 | if (device->db_loaded || (!force && device->sealed)) | |
946 | return 0; | |
947 | ||
948 | r = device_get_db_path(device, &path); | |
949 | if (r < 0) | |
950 | return r; | |
951 | ||
952 | return device_read_db_internal_filename(device, path); | |
953 | } | |
954 | ||
955 | static const char* const device_action_table[_SD_DEVICE_ACTION_MAX] = { | |
956 | [SD_DEVICE_ADD] = "add", | |
957 | [SD_DEVICE_REMOVE] = "remove", | |
958 | [SD_DEVICE_CHANGE] = "change", | |
959 | [SD_DEVICE_MOVE] = "move", | |
960 | [SD_DEVICE_ONLINE] = "online", | |
961 | [SD_DEVICE_OFFLINE] = "offline", | |
962 | [SD_DEVICE_BIND] = "bind", | |
963 | [SD_DEVICE_UNBIND] = "unbind", | |
964 | }; | |
965 | ||
966 | DEFINE_STRING_TABLE_LOOKUP(device_action, sd_device_action_t); |