1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "alloc-util.h"
10 #include "device-internal.h"
11 #include "device-private.h"
12 #include "device-util.h"
19 #include "parse-util.h"
20 #include "path-util.h"
23 #include "string-table.h"
24 #include "string-util.h"
27 #include "user-util.h"
30 int device_add_property(sd_device
*device
, const char *key
, const char *value
) {
36 r
= device_add_property_aux(device
, key
, value
, false);
41 r
= device_add_property_aux(device
, key
, value
, true);
49 static int device_add_property_internal_from_string(sd_device
*device
, const char *str
) {
50 _cleanup_free_
char *key
= NULL
;
60 value
= strchr(key
, '=');
69 return device_add_property_internal(device
, key
, value
);
72 static int handle_db_line(sd_device
*device
, char key
, const char *value
) {
81 path
= strjoina("/dev/", value
);
82 r
= device_add_devlink(device
, path
);
88 r
= safe_atoi(value
, &device
->devlink_priority
);
94 r
= device_add_property_internal_from_string(device
, value
);
100 r
= device_add_tag(device
, value
);
106 r
= safe_atoi(value
, &device
->watch_handle
);
112 r
= device_set_usec_initialized(device
, value
);
118 log_device_debug(device
, "sd-device: Unknown key '%c' in device db, ignoring", key
);
124 void device_set_devlink_priority(sd_device
*device
, int priority
) {
127 device
->devlink_priority
= priority
;
130 void device_set_is_initialized(sd_device
*device
) {
133 device
->is_initialized
= true;
136 int device_ensure_usec_initialized(sd_device
*device
, sd_device
*device_old
) {
137 char num
[DECIMAL_STR_MAX(usec_t
)];
138 usec_t usec_initialized
;
143 if (device_old
&& device_old
->usec_initialized
> 0)
144 usec_initialized
= device_old
->usec_initialized
;
146 usec_initialized
= now(CLOCK_MONOTONIC
);
148 r
= snprintf(num
, sizeof(num
), USEC_FMT
, usec_initialized
);
152 r
= device_set_usec_initialized(device
, num
);
159 static int device_read_db(sd_device
*device
) {
160 _cleanup_free_
char *db
= NULL
;
162 const char *id
, *value
;
178 if (device
->db_loaded
|| device
->sealed
)
181 r
= device_get_id_filename(device
, &id
);
185 path
= strjoina("/run/udev/data/", id
);
187 r
= read_full_file(path
, &db
, &db_len
);
192 return log_device_debug_errno(device
, r
, "sd-device: Failed to read db '%s': %m", path
);
195 /* devices with a database entry are initialized */
196 device_set_is_initialized(device
);
198 for (i
= 0; i
< db_len
; i
++) {
201 if (!strchr(NEWLINE
, db
[i
])) {
210 log_device_debug(device
, "sd-device: Invalid db entry with key '%c', ignoring", key
);
212 state
= INVALID_LINE
;
227 if (strchr(NEWLINE
, db
[i
]))
232 if (strchr(NEWLINE
, db
[i
])) {
234 r
= handle_db_line(device
, key
, value
);
236 log_device_debug_errno(device
, r
, "sd-device: Failed to handle db entry '%c:%s', ignoring: %m", key
, value
);
243 assert_not_reached("Invalid state when parsing db");
247 device
->db_loaded
= true;
252 uint64_t device_get_properties_generation(sd_device
*device
) {
255 return device
->properties_generation
;
258 uint64_t device_get_tags_generation(sd_device
*device
) {
261 return device
->tags_generation
;
264 uint64_t device_get_devlinks_generation(sd_device
*device
) {
267 return device
->devlinks_generation
;
270 int device_get_devnode_mode(sd_device
*device
, mode_t
*mode
) {
275 r
= device_read_db(device
);
279 if (device
->devmode
== (mode_t
) -1)
283 *mode
= device
->devmode
;
288 int device_get_devnode_uid(sd_device
*device
, uid_t
*uid
) {
293 r
= device_read_db(device
);
297 if (device
->devuid
== (uid_t
) -1)
301 *uid
= device
->devuid
;
306 static int device_set_devuid(sd_device
*device
, const char *uid
) {
313 r
= safe_atou(uid
, &u
);
317 r
= device_add_property_internal(device
, "DEVUID", uid
);
326 int device_get_devnode_gid(sd_device
*device
, gid_t
*gid
) {
331 r
= device_read_db(device
);
335 if (device
->devgid
== (gid_t
) -1)
339 *gid
= device
->devgid
;
344 static int device_set_devgid(sd_device
*device
, const char *gid
) {
351 r
= safe_atou(gid
, &g
);
355 r
= device_add_property_internal(device
, "DEVGID", gid
);
364 static int device_amend(sd_device
*device
, const char *key
, const char *value
) {
371 if (streq(key
, "DEVPATH")) {
374 path
= strjoina("/sys", value
);
376 /* the caller must verify or trust this data (e.g., if it comes from the kernel) */
377 r
= device_set_syspath(device
, path
, false);
379 return log_device_debug_errno(device
, r
, "sd-device: Failed to set syspath to '%s': %m", path
);
380 } else if (streq(key
, "SUBSYSTEM")) {
381 r
= device_set_subsystem(device
, value
);
383 return log_device_debug_errno(device
, r
, "sd-device: Failed to set subsystem to '%s': %m", value
);
384 } else if (streq(key
, "DEVTYPE")) {
385 r
= device_set_devtype(device
, value
);
387 return log_device_debug_errno(device
, r
, "sd-device: Failed to set devtype to '%s': %m", value
);
388 } else if (streq(key
, "DEVNAME")) {
389 r
= device_set_devname(device
, value
);
391 return log_device_debug_errno(device
, r
, "sd-device: Failed to set devname to '%s': %m", value
);
392 } else if (streq(key
, "USEC_INITIALIZED")) {
393 r
= device_set_usec_initialized(device
, value
);
395 return log_device_debug_errno(device
, r
, "sd-device: Failed to set usec-initialized to '%s': %m", value
);
396 } else if (streq(key
, "DRIVER")) {
397 r
= device_set_driver(device
, value
);
399 return log_device_debug_errno(device
, r
, "sd-device: Failed to set driver to '%s': %m", value
);
400 } else if (streq(key
, "IFINDEX")) {
401 r
= device_set_ifindex(device
, value
);
403 return log_device_debug_errno(device
, r
, "sd-device: Failed to set ifindex to '%s': %m", value
);
404 } else if (streq(key
, "DEVMODE")) {
405 r
= device_set_devmode(device
, value
);
407 return log_device_debug_errno(device
, r
, "sd-device: Failed to set devmode to '%s': %m", value
);
408 } else if (streq(key
, "DEVUID")) {
409 r
= device_set_devuid(device
, value
);
411 return log_device_debug_errno(device
, r
, "sd-device: Failed to set devuid to '%s': %m", value
);
412 } else if (streq(key
, "DEVGID")) {
413 r
= device_set_devgid(device
, value
);
415 return log_device_debug_errno(device
, r
, "sd-device: Failed to set devgid to '%s': %m", value
);
416 } else if (streq(key
, "DEVLINKS")) {
417 const char *word
, *state
;
420 FOREACH_WORD(word
, l
, value
, state
) {
423 strncpy(devlink
, word
, l
);
426 r
= device_add_devlink(device
, devlink
);
428 return log_device_debug_errno(device
, r
, "sd-device: Failed to add devlink '%s': %m", devlink
);
430 } else if (streq(key
, "TAGS")) {
431 const char *word
, *state
;
434 FOREACH_WORD_SEPARATOR(word
, l
, value
, ":", state
) {
437 (void) strncpy(tag
, word
, l
);
440 r
= device_add_tag(device
, tag
);
442 return log_device_debug_errno(device
, r
, "sd-device: Failed to add tag '%s': %m", tag
);
445 r
= device_add_property_internal(device
, key
, value
);
447 return log_device_debug_errno(device
, r
, "sd-device: Failed to add property '%s=%s': %m", key
, value
);
453 static const char* const device_action_table
[_DEVICE_ACTION_MAX
] = {
454 [DEVICE_ACTION_ADD
] = "add",
455 [DEVICE_ACTION_REMOVE
] = "remove",
456 [DEVICE_ACTION_CHANGE
] = "change",
457 [DEVICE_ACTION_MOVE
] = "move",
458 [DEVICE_ACTION_ONLINE
] = "online",
459 [DEVICE_ACTION_OFFLINE
] = "offline",
460 [DEVICE_ACTION_BIND
] = "bind",
461 [DEVICE_ACTION_UNBIND
] = "unbind",
464 DEFINE_STRING_TABLE_LOOKUP(device_action
, DeviceAction
);
466 static int device_append(sd_device
*device
, char *key
, const char **_major
, const char **_minor
, uint64_t *_seqnum
,
467 DeviceAction
*_action
) {
468 DeviceAction action
= _DEVICE_ACTION_INVALID
;
470 const char *major
= NULL
, *minor
= NULL
;
481 value
= strchr(key
, '=');
483 log_device_debug(device
, "sd-device: Not a key-value pair: '%s'", key
);
491 if (streq(key
, "MAJOR"))
493 else if (streq(key
, "MINOR"))
496 if (streq(key
, "ACTION")) {
497 action
= device_action_from_string(value
);
498 if (action
== _DEVICE_ACTION_INVALID
)
500 } else if (streq(key
, "SEQNUM")) {
501 r
= safe_atou64(value
, &seqnum
);
504 else if (seqnum
== 0)
505 /* kernel only sends seqnum > 0 */
509 r
= device_amend(device
, key
, value
);
520 if (action
!= _DEVICE_ACTION_INVALID
)
529 void device_seal(sd_device
*device
) {
532 device
->sealed
= true;
535 static int device_verify(sd_device
*device
, DeviceAction action
, uint64_t seqnum
) {
538 if (!device
->devpath
|| !device
->subsystem
|| action
== _DEVICE_ACTION_INVALID
|| seqnum
== 0) {
539 log_device_debug(device
, "sd-device: Device created from strv or nulstr lacks devpath, subsystem, action or seqnum.");
543 device
->sealed
= true;
548 int device_new_from_strv(sd_device
**ret
, char **strv
) {
549 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
551 const char *major
= NULL
, *minor
= NULL
;
552 DeviceAction action
= _DEVICE_ACTION_INVALID
;
559 r
= device_new_aux(&device
);
563 STRV_FOREACH(key
, strv
) {
564 r
= device_append(device
, *key
, &major
, &minor
, &seqnum
, &action
);
570 r
= device_set_devnum(device
, major
, minor
);
572 return log_device_debug_errno(device
, r
, "sd-device: Failed to set devnum %s:%s: %m", major
, minor
);
575 r
= device_verify(device
, action
, seqnum
);
579 *ret
= TAKE_PTR(device
);
584 int device_new_from_nulstr(sd_device
**ret
, uint8_t *nulstr
, size_t len
) {
585 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
586 const char *major
= NULL
, *minor
= NULL
;
587 DeviceAction action
= _DEVICE_ACTION_INVALID
;
596 r
= device_new_aux(&device
);
604 key
= (char*)&nulstr
[i
];
605 end
= memchr(key
, '\0', len
- i
);
607 log_device_debug(device
, "sd-device: Failed to parse nulstr");
612 r
= device_append(device
, key
, &major
, &minor
, &seqnum
, &action
);
618 r
= device_set_devnum(device
, major
, minor
);
620 return log_device_debug_errno(device
, r
, "sd-device: Failed to set devnum %s:%s: %m", major
, minor
);
623 r
= device_verify(device
, action
, seqnum
);
627 *ret
= TAKE_PTR(device
);
632 static int device_update_properties_bufs(sd_device
*device
) {
633 const char *val
, *prop
;
634 _cleanup_free_
char **buf_strv
= NULL
;
635 _cleanup_free_
uint8_t *buf_nulstr
= NULL
;
636 size_t allocated_nulstr
= 0;
637 size_t nulstr_len
= 0, num
= 0, i
= 0;
641 if (!device
->properties_buf_outdated
)
644 FOREACH_DEVICE_PROPERTY(device
, prop
, val
) {
647 len
= strlen(prop
) + 1 + strlen(val
);
649 buf_nulstr
= GREEDY_REALLOC0(buf_nulstr
, allocated_nulstr
, nulstr_len
+ len
+ 2);
653 strscpyl((char *)buf_nulstr
+ nulstr_len
, len
+ 1, prop
, "=", val
, NULL
);
654 nulstr_len
+= len
+ 1;
658 /* build buf_strv from buf_nulstr */
659 buf_strv
= new0(char *, num
+ 1);
663 NULSTR_FOREACH(val
, (char*) buf_nulstr
) {
664 buf_strv
[i
] = (char *) val
;
669 free_and_replace(device
->properties_nulstr
, buf_nulstr
);
670 device
->properties_nulstr_len
= nulstr_len
;
671 free_and_replace(device
->properties_strv
, buf_strv
);
673 device
->properties_buf_outdated
= false;
678 int device_get_properties_nulstr(sd_device
*device
, const uint8_t **nulstr
, size_t *len
) {
685 r
= device_update_properties_bufs(device
);
689 *nulstr
= device
->properties_nulstr
;
690 *len
= device
->properties_nulstr_len
;
695 int device_get_properties_strv(sd_device
*device
, char ***strv
) {
701 r
= device_update_properties_bufs(device
);
705 *strv
= device
->properties_strv
;
710 int device_get_devlink_priority(sd_device
*device
, int *priority
) {
716 r
= device_read_db(device
);
720 *priority
= device
->devlink_priority
;
725 int device_get_watch_handle(sd_device
*device
, int *handle
) {
730 r
= device_read_db(device
);
734 if (device
->watch_handle
< 0)
738 *handle
= device
->watch_handle
;
743 void device_set_watch_handle(sd_device
*device
, int handle
) {
746 device
->watch_handle
= handle
;
749 int device_rename(sd_device
*device
, const char *name
) {
750 _cleanup_free_
char *dirname
= NULL
;
752 const char *interface
;
758 dirname
= dirname_malloc(device
->syspath
);
762 new_syspath
= strjoina(dirname
, "/", name
);
764 /* the user must trust that the new name is correct */
765 r
= device_set_syspath(device
, new_syspath
, false);
769 r
= sd_device_get_property_value(device
, "INTERFACE", &interface
);
771 /* like DEVPATH_OLD, INTERFACE_OLD is not saved to the db, but only stays around for the current event */
772 r
= device_add_property_internal(device
, "INTERFACE_OLD", interface
);
776 r
= device_add_property_internal(device
, "INTERFACE", name
);
779 } else if (r
!= -ENOENT
)
785 int device_shallow_clone(sd_device
*old_device
, sd_device
**new_device
) {
786 _cleanup_(sd_device_unrefp
) sd_device
*ret
= NULL
;
792 r
= device_new_aux(&ret
);
796 r
= device_set_syspath(ret
, old_device
->syspath
, false);
800 r
= device_set_subsystem(ret
, old_device
->subsystem
);
804 ret
->devnum
= old_device
->devnum
;
806 *new_device
= TAKE_PTR(ret
);
811 int device_clone_with_db(sd_device
*old_device
, sd_device
**new_device
) {
812 _cleanup_(sd_device_unrefp
) sd_device
*ret
= NULL
;
818 r
= device_shallow_clone(old_device
, &ret
);
822 r
= device_read_db(ret
);
828 *new_device
= TAKE_PTR(ret
);
833 int device_new_from_synthetic_event(sd_device
**new_device
, const char *syspath
, const char *action
) {
834 _cleanup_(sd_device_unrefp
) sd_device
*ret
= NULL
;
841 r
= sd_device_new_from_syspath(&ret
, syspath
);
845 r
= device_read_uevent_file(ret
);
849 r
= device_add_property_internal(ret
, "ACTION", action
);
853 *new_device
= TAKE_PTR(ret
);
858 int device_new_from_stat_rdev(sd_device
**ret
, const struct stat
*st
) {
864 if (S_ISBLK(st
->st_mode
))
866 else if (S_ISCHR(st
->st_mode
))
871 return sd_device_new_from_devnum(ret
, type
, st
->st_rdev
);
874 int device_copy_properties(sd_device
*device_dst
, sd_device
*device_src
) {
875 const char *property
, *value
;
881 FOREACH_DEVICE_PROPERTY(device_src
, property
, value
) {
882 r
= device_add_property(device_dst
, property
, value
);
890 void device_cleanup_tags(sd_device
*device
) {
893 set_free_free(device
->tags
);
895 device
->property_tags_outdated
= true;
896 device
->tags_generation
++;
899 void device_cleanup_devlinks(sd_device
*device
) {
902 set_free_free(device
->devlinks
);
903 device
->devlinks
= NULL
;
904 device
->property_devlinks_outdated
= true;
905 device
->devlinks_generation
++;
908 void device_remove_tag(sd_device
*device
, const char *tag
) {
912 free(set_remove(device
->tags
, tag
));
913 device
->property_tags_outdated
= true;
914 device
->tags_generation
++;
917 static int device_tag(sd_device
*device
, const char *tag
, bool add
) {
925 r
= device_get_id_filename(device
, &id
);
929 path
= strjoina("/run/udev/tags/", tag
, "/", id
);
932 r
= touch_file(path
, true, USEC_INFINITY
, UID_INVALID
, GID_INVALID
, 0444);
937 if (r
< 0 && errno
!= ENOENT
)
944 int device_tag_index(sd_device
*device
, sd_device
*device_old
, bool add
) {
948 if (add
&& device_old
) {
949 /* delete possible left-over tags */
950 FOREACH_DEVICE_TAG(device_old
, tag
) {
951 if (!sd_device_has_tag(device
, tag
)) {
952 k
= device_tag(device_old
, tag
, false);
959 FOREACH_DEVICE_TAG(device
, tag
) {
960 k
= device_tag(device
, tag
, add
);
968 static bool device_has_info(sd_device
*device
) {
971 if (!set_isempty(device
->devlinks
))
974 if (device
->devlink_priority
!= 0)
977 if (!ordered_hashmap_isempty(device
->properties_db
))
980 if (!set_isempty(device
->tags
))
983 if (device
->watch_handle
>= 0)
989 void device_set_db_persist(sd_device
*device
) {
992 device
->db_persist
= true;
995 int device_update_db(sd_device
*device
) {
998 _cleanup_fclose_
FILE *f
= NULL
;
999 _cleanup_free_
char *path_tmp
= NULL
;
1005 has_info
= device_has_info(device
);
1007 r
= device_get_id_filename(device
, &id
);
1011 path
= strjoina("/run/udev/data/", id
);
1013 /* do not store anything for otherwise empty devices */
1014 if (!has_info
&& major(device
->devnum
) == 0 && device
->ifindex
== 0) {
1016 if (r
< 0 && errno
!= ENOENT
)
1022 /* write a database file */
1023 r
= mkdir_parents(path
, 0755);
1027 r
= fopen_temporary(path
, &f
, &path_tmp
);
1032 * set 'sticky' bit to indicate that we should not clean the
1033 * database when we transition from initramfs to the real root
1035 if (device
->db_persist
) {
1036 r
= fchmod(fileno(f
), 01644);
1042 r
= fchmod(fileno(f
), 0644);
1050 const char *property
, *value
, *tag
;
1053 if (major(device
->devnum
) > 0) {
1054 const char *devlink
;
1056 FOREACH_DEVICE_DEVLINK(device
, devlink
)
1057 fprintf(f
, "S:%s\n", devlink
+ STRLEN("/dev/"));
1059 if (device
->devlink_priority
!= 0)
1060 fprintf(f
, "L:%i\n", device
->devlink_priority
);
1062 if (device
->watch_handle
>= 0)
1063 fprintf(f
, "W:%i\n", device
->watch_handle
);
1066 if (device
->usec_initialized
> 0)
1067 fprintf(f
, "I:"USEC_FMT
"\n", device
->usec_initialized
);
1069 ORDERED_HASHMAP_FOREACH_KEY(value
, property
, device
->properties_db
, i
)
1070 fprintf(f
, "E:%s=%s\n", property
, value
);
1072 FOREACH_DEVICE_TAG(device
, tag
)
1073 fprintf(f
, "G:%s\n", tag
);
1076 r
= fflush_and_check(f
);
1080 r
= rename(path_tmp
, path
);
1086 log_device_debug(device
, "sd-device: Created %s file '%s' for '%s'", has_info
? "db" : "empty",
1087 path
, device
->devpath
);
1092 (void) unlink(path
);
1093 (void) unlink(path_tmp
);
1095 return log_device_debug_errno(device
, r
, "sd-device: Failed to create %s file '%s' for '%s'", has_info
? "db" : "empty", path
, device
->devpath
);
1098 int device_delete_db(sd_device
*device
) {
1105 r
= device_get_id_filename(device
, &id
);
1109 path
= strjoina("/run/udev/data/", id
);
1112 if (r
< 0 && errno
!= ENOENT
)
1118 int device_read_db_force(sd_device
*device
) {
1121 return device_read_db_aux(device
, true);