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 "tmpfile-util.h"
28 #include "user-util.h"
31 int device_add_property(sd_device
*device
, const char *key
, const char *value
) {
37 r
= device_add_property_aux(device
, key
, value
, false);
42 r
= device_add_property_aux(device
, key
, value
, true);
50 void device_set_devlink_priority(sd_device
*device
, int priority
) {
53 device
->devlink_priority
= priority
;
56 void device_set_is_initialized(sd_device
*device
) {
59 device
->is_initialized
= true;
62 int device_ensure_usec_initialized(sd_device
*device
, sd_device
*device_old
) {
67 if (device_old
&& device_old
->usec_initialized
> 0)
68 when
= device_old
->usec_initialized
;
70 when
= now(CLOCK_MONOTONIC
);
72 return device_set_usec_initialized(device
, when
);
75 uint64_t device_get_properties_generation(sd_device
*device
) {
78 return device
->properties_generation
;
81 uint64_t device_get_tags_generation(sd_device
*device
) {
84 return device
->tags_generation
;
87 uint64_t device_get_devlinks_generation(sd_device
*device
) {
90 return device
->devlinks_generation
;
93 int device_get_devnode_mode(sd_device
*device
, mode_t
*mode
) {
98 r
= device_read_db(device
);
102 if (device
->devmode
== (mode_t
) -1)
106 *mode
= device
->devmode
;
111 int device_get_devnode_uid(sd_device
*device
, uid_t
*uid
) {
116 r
= device_read_db(device
);
120 if (device
->devuid
== (uid_t
) -1)
124 *uid
= device
->devuid
;
129 static int device_set_devuid(sd_device
*device
, const char *uid
) {
136 r
= safe_atou(uid
, &u
);
140 r
= device_add_property_internal(device
, "DEVUID", uid
);
149 int device_get_devnode_gid(sd_device
*device
, gid_t
*gid
) {
154 r
= device_read_db(device
);
158 if (device
->devgid
== (gid_t
) -1)
162 *gid
= device
->devgid
;
167 static int device_set_devgid(sd_device
*device
, const char *gid
) {
174 r
= safe_atou(gid
, &g
);
178 r
= device_add_property_internal(device
, "DEVGID", gid
);
187 static int device_amend(sd_device
*device
, const char *key
, const char *value
) {
194 if (streq(key
, "DEVPATH")) {
197 path
= strjoina("/sys", value
);
199 /* the caller must verify or trust this data (e.g., if it comes from the kernel) */
200 r
= device_set_syspath(device
, path
, false);
202 return log_device_debug_errno(device
, r
, "sd-device: Failed to set syspath to '%s': %m", path
);
203 } else if (streq(key
, "SUBSYSTEM")) {
204 r
= device_set_subsystem(device
, value
);
206 return log_device_debug_errno(device
, r
, "sd-device: Failed to set subsystem to '%s': %m", value
);
207 } else if (streq(key
, "DEVTYPE")) {
208 r
= device_set_devtype(device
, value
);
210 return log_device_debug_errno(device
, r
, "sd-device: Failed to set devtype to '%s': %m", value
);
211 } else if (streq(key
, "DEVNAME")) {
212 r
= device_set_devname(device
, value
);
214 return log_device_debug_errno(device
, r
, "sd-device: Failed to set devname to '%s': %m", value
);
215 } else if (streq(key
, "USEC_INITIALIZED")) {
218 r
= safe_atou64(value
, &t
);
220 return log_device_debug_errno(device
, r
, "sd-device: Failed to parse timestamp '%s': %m", value
);
222 r
= device_set_usec_initialized(device
, t
);
224 return log_device_debug_errno(device
, r
, "sd-device: Failed to set usec-initialized to '%s': %m", value
);
225 } else if (streq(key
, "DRIVER")) {
226 r
= device_set_driver(device
, value
);
228 return log_device_debug_errno(device
, r
, "sd-device: Failed to set driver to '%s': %m", value
);
229 } else if (streq(key
, "IFINDEX")) {
230 r
= device_set_ifindex(device
, value
);
232 return log_device_debug_errno(device
, r
, "sd-device: Failed to set ifindex to '%s': %m", value
);
233 } else if (streq(key
, "DEVMODE")) {
234 r
= device_set_devmode(device
, value
);
236 return log_device_debug_errno(device
, r
, "sd-device: Failed to set devmode to '%s': %m", value
);
237 } else if (streq(key
, "DEVUID")) {
238 r
= device_set_devuid(device
, value
);
240 return log_device_debug_errno(device
, r
, "sd-device: Failed to set devuid to '%s': %m", value
);
241 } else if (streq(key
, "DEVGID")) {
242 r
= device_set_devgid(device
, value
);
244 return log_device_debug_errno(device
, r
, "sd-device: Failed to set devgid to '%s': %m", value
);
245 } else if (streq(key
, "DEVLINKS")) {
246 const char *word
, *state
;
249 FOREACH_WORD(word
, l
, value
, state
) {
252 strncpy(devlink
, word
, l
);
255 r
= device_add_devlink(device
, devlink
);
257 return log_device_debug_errno(device
, r
, "sd-device: Failed to add devlink '%s': %m", devlink
);
259 } else if (streq(key
, "TAGS")) {
260 const char *word
, *state
;
263 FOREACH_WORD_SEPARATOR(word
, l
, value
, ":", state
) {
266 (void) strncpy(tag
, word
, l
);
269 r
= device_add_tag(device
, tag
);
271 return log_device_debug_errno(device
, r
, "sd-device: Failed to add tag '%s': %m", tag
);
274 r
= device_add_property_internal(device
, key
, value
);
276 return log_device_debug_errno(device
, r
, "sd-device: Failed to add property '%s=%s': %m", key
, value
);
282 static const char* const device_action_table
[_DEVICE_ACTION_MAX
] = {
283 [DEVICE_ACTION_ADD
] = "add",
284 [DEVICE_ACTION_REMOVE
] = "remove",
285 [DEVICE_ACTION_CHANGE
] = "change",
286 [DEVICE_ACTION_MOVE
] = "move",
287 [DEVICE_ACTION_ONLINE
] = "online",
288 [DEVICE_ACTION_OFFLINE
] = "offline",
289 [DEVICE_ACTION_BIND
] = "bind",
290 [DEVICE_ACTION_UNBIND
] = "unbind",
293 DEFINE_STRING_TABLE_LOOKUP(device_action
, DeviceAction
);
295 static int device_append(sd_device
*device
, char *key
, const char **_major
, const char **_minor
, uint64_t *_seqnum
,
296 DeviceAction
*_action
) {
297 DeviceAction action
= _DEVICE_ACTION_INVALID
;
299 const char *major
= NULL
, *minor
= NULL
;
310 value
= strchr(key
, '=');
312 log_device_debug(device
, "sd-device: Not a key-value pair: '%s'", key
);
320 if (streq(key
, "MAJOR"))
322 else if (streq(key
, "MINOR"))
325 if (streq(key
, "ACTION")) {
326 action
= device_action_from_string(value
);
327 if (action
== _DEVICE_ACTION_INVALID
)
329 } else if (streq(key
, "SEQNUM")) {
330 r
= safe_atou64(value
, &seqnum
);
333 else if (seqnum
== 0)
334 /* kernel only sends seqnum > 0 */
338 r
= device_amend(device
, key
, value
);
349 if (action
!= _DEVICE_ACTION_INVALID
)
358 void device_seal(sd_device
*device
) {
361 device
->sealed
= true;
364 static int device_verify(sd_device
*device
, DeviceAction action
, uint64_t seqnum
) {
367 if (!device
->devpath
|| !device
->subsystem
|| action
== _DEVICE_ACTION_INVALID
|| seqnum
== 0) {
368 log_device_debug(device
, "sd-device: Device created from strv or nulstr lacks devpath, subsystem, action or seqnum.");
372 device
->sealed
= true;
377 int device_new_from_strv(sd_device
**ret
, char **strv
) {
378 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
380 const char *major
= NULL
, *minor
= NULL
;
381 DeviceAction action
= _DEVICE_ACTION_INVALID
;
388 r
= device_new_aux(&device
);
392 STRV_FOREACH(key
, strv
) {
393 r
= device_append(device
, *key
, &major
, &minor
, &seqnum
, &action
);
399 r
= device_set_devnum(device
, major
, minor
);
401 return log_device_debug_errno(device
, r
, "sd-device: Failed to set devnum %s:%s: %m", major
, minor
);
404 r
= device_verify(device
, action
, seqnum
);
408 *ret
= TAKE_PTR(device
);
413 int device_new_from_nulstr(sd_device
**ret
, uint8_t *nulstr
, size_t len
) {
414 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
415 const char *major
= NULL
, *minor
= NULL
;
416 DeviceAction action
= _DEVICE_ACTION_INVALID
;
425 r
= device_new_aux(&device
);
433 key
= (char*)&nulstr
[i
];
434 end
= memchr(key
, '\0', len
- i
);
436 log_device_debug(device
, "sd-device: Failed to parse nulstr");
441 r
= device_append(device
, key
, &major
, &minor
, &seqnum
, &action
);
447 r
= device_set_devnum(device
, major
, minor
);
449 return log_device_debug_errno(device
, r
, "sd-device: Failed to set devnum %s:%s: %m", major
, minor
);
452 r
= device_verify(device
, action
, seqnum
);
456 *ret
= TAKE_PTR(device
);
461 static int device_update_properties_bufs(sd_device
*device
) {
462 const char *val
, *prop
;
463 _cleanup_free_
char **buf_strv
= NULL
;
464 _cleanup_free_
uint8_t *buf_nulstr
= NULL
;
465 size_t allocated_nulstr
= 0;
466 size_t nulstr_len
= 0, num
= 0, i
= 0;
470 if (!device
->properties_buf_outdated
)
473 FOREACH_DEVICE_PROPERTY(device
, prop
, val
) {
476 len
= strlen(prop
) + 1 + strlen(val
);
478 buf_nulstr
= GREEDY_REALLOC0(buf_nulstr
, allocated_nulstr
, nulstr_len
+ len
+ 2);
482 strscpyl((char *)buf_nulstr
+ nulstr_len
, len
+ 1, prop
, "=", val
, NULL
);
483 nulstr_len
+= len
+ 1;
487 /* build buf_strv from buf_nulstr */
488 buf_strv
= new0(char *, num
+ 1);
492 NULSTR_FOREACH(val
, (char*) buf_nulstr
) {
493 buf_strv
[i
] = (char *) val
;
498 free_and_replace(device
->properties_nulstr
, buf_nulstr
);
499 device
->properties_nulstr_len
= nulstr_len
;
500 free_and_replace(device
->properties_strv
, buf_strv
);
502 device
->properties_buf_outdated
= false;
507 int device_get_properties_nulstr(sd_device
*device
, const uint8_t **nulstr
, size_t *len
) {
514 r
= device_update_properties_bufs(device
);
518 *nulstr
= device
->properties_nulstr
;
519 *len
= device
->properties_nulstr_len
;
524 int device_get_properties_strv(sd_device
*device
, char ***strv
) {
530 r
= device_update_properties_bufs(device
);
534 *strv
= device
->properties_strv
;
539 int device_get_devlink_priority(sd_device
*device
, int *priority
) {
545 r
= device_read_db(device
);
549 *priority
= device
->devlink_priority
;
554 int device_get_watch_handle(sd_device
*device
, int *handle
) {
559 r
= device_read_db(device
);
563 if (device
->watch_handle
< 0)
567 *handle
= device
->watch_handle
;
572 void device_set_watch_handle(sd_device
*device
, int handle
) {
575 device
->watch_handle
= handle
;
578 int device_rename(sd_device
*device
, const char *name
) {
579 _cleanup_free_
char *dirname
= NULL
;
581 const char *interface
;
587 dirname
= dirname_malloc(device
->syspath
);
591 new_syspath
= strjoina(dirname
, "/", name
);
593 /* the user must trust that the new name is correct */
594 r
= device_set_syspath(device
, new_syspath
, false);
598 r
= sd_device_get_property_value(device
, "INTERFACE", &interface
);
600 /* like DEVPATH_OLD, INTERFACE_OLD is not saved to the db, but only stays around for the current event */
601 r
= device_add_property_internal(device
, "INTERFACE_OLD", interface
);
605 r
= device_add_property_internal(device
, "INTERFACE", name
);
608 } else if (r
!= -ENOENT
)
614 int device_shallow_clone(sd_device
*old_device
, sd_device
**new_device
) {
615 _cleanup_(sd_device_unrefp
) sd_device
*ret
= NULL
;
621 r
= device_new_aux(&ret
);
625 r
= device_set_syspath(ret
, old_device
->syspath
, false);
629 r
= device_set_subsystem(ret
, old_device
->subsystem
);
633 ret
->devnum
= old_device
->devnum
;
635 *new_device
= TAKE_PTR(ret
);
640 int device_clone_with_db(sd_device
*old_device
, sd_device
**new_device
) {
641 _cleanup_(sd_device_unrefp
) sd_device
*ret
= NULL
;
647 r
= device_shallow_clone(old_device
, &ret
);
651 r
= device_read_db(ret
);
657 *new_device
= TAKE_PTR(ret
);
662 int device_new_from_synthetic_event(sd_device
**new_device
, const char *syspath
, const char *action
) {
663 _cleanup_(sd_device_unrefp
) sd_device
*ret
= NULL
;
670 r
= sd_device_new_from_syspath(&ret
, syspath
);
674 r
= device_read_uevent_file(ret
);
678 r
= device_add_property_internal(ret
, "ACTION", action
);
682 *new_device
= TAKE_PTR(ret
);
687 int device_new_from_stat_rdev(sd_device
**ret
, const struct stat
*st
) {
693 if (S_ISBLK(st
->st_mode
))
695 else if (S_ISCHR(st
->st_mode
))
700 return sd_device_new_from_devnum(ret
, type
, st
->st_rdev
);
703 int device_copy_properties(sd_device
*device_dst
, sd_device
*device_src
) {
704 const char *property
, *value
;
711 r
= device_properties_prepare(device_src
);
715 ORDERED_HASHMAP_FOREACH_KEY(value
, property
, device_src
->properties_db
, i
) {
716 r
= device_add_property_aux(device_dst
, property
, value
, true);
721 ORDERED_HASHMAP_FOREACH_KEY(value
, property
, device_src
->properties
, i
) {
722 r
= device_add_property_aux(device_dst
, property
, value
, false);
730 void device_cleanup_tags(sd_device
*device
) {
733 set_free_free(device
->tags
);
735 device
->property_tags_outdated
= true;
736 device
->tags_generation
++;
739 void device_cleanup_devlinks(sd_device
*device
) {
742 set_free_free(device
->devlinks
);
743 device
->devlinks
= NULL
;
744 device
->property_devlinks_outdated
= true;
745 device
->devlinks_generation
++;
748 void device_remove_tag(sd_device
*device
, const char *tag
) {
752 free(set_remove(device
->tags
, tag
));
753 device
->property_tags_outdated
= true;
754 device
->tags_generation
++;
757 static int device_tag(sd_device
*device
, const char *tag
, bool add
) {
765 r
= device_get_id_filename(device
, &id
);
769 path
= strjoina("/run/udev/tags/", tag
, "/", id
);
772 r
= touch_file(path
, true, USEC_INFINITY
, UID_INVALID
, GID_INVALID
, 0444);
777 if (r
< 0 && errno
!= ENOENT
)
784 int device_tag_index(sd_device
*device
, sd_device
*device_old
, bool add
) {
788 if (add
&& device_old
) {
789 /* delete possible left-over tags */
790 FOREACH_DEVICE_TAG(device_old
, tag
) {
791 if (!sd_device_has_tag(device
, tag
)) {
792 k
= device_tag(device_old
, tag
, false);
799 FOREACH_DEVICE_TAG(device
, tag
) {
800 k
= device_tag(device
, tag
, add
);
808 static bool device_has_info(sd_device
*device
) {
811 if (!set_isempty(device
->devlinks
))
814 if (device
->devlink_priority
!= 0)
817 if (!ordered_hashmap_isempty(device
->properties_db
))
820 if (!set_isempty(device
->tags
))
823 if (device
->watch_handle
>= 0)
829 void device_set_db_persist(sd_device
*device
) {
832 device
->db_persist
= true;
835 int device_update_db(sd_device
*device
) {
838 _cleanup_fclose_
FILE *f
= NULL
;
839 _cleanup_free_
char *path_tmp
= NULL
;
845 has_info
= device_has_info(device
);
847 r
= device_get_id_filename(device
, &id
);
851 path
= strjoina("/run/udev/data/", id
);
853 /* do not store anything for otherwise empty devices */
854 if (!has_info
&& major(device
->devnum
) == 0 && device
->ifindex
== 0) {
856 if (r
< 0 && errno
!= ENOENT
)
862 /* write a database file */
863 r
= mkdir_parents(path
, 0755);
867 r
= fopen_temporary(path
, &f
, &path_tmp
);
872 * set 'sticky' bit to indicate that we should not clean the
873 * database when we transition from initramfs to the real root
875 if (device
->db_persist
) {
876 r
= fchmod(fileno(f
), 01644);
882 r
= fchmod(fileno(f
), 0644);
890 const char *property
, *value
, *tag
;
893 if (major(device
->devnum
) > 0) {
896 FOREACH_DEVICE_DEVLINK(device
, devlink
)
897 fprintf(f
, "S:%s\n", devlink
+ STRLEN("/dev/"));
899 if (device
->devlink_priority
!= 0)
900 fprintf(f
, "L:%i\n", device
->devlink_priority
);
902 if (device
->watch_handle
>= 0)
903 fprintf(f
, "W:%i\n", device
->watch_handle
);
906 if (device
->usec_initialized
> 0)
907 fprintf(f
, "I:"USEC_FMT
"\n", device
->usec_initialized
);
909 ORDERED_HASHMAP_FOREACH_KEY(value
, property
, device
->properties_db
, i
)
910 fprintf(f
, "E:%s=%s\n", property
, value
);
912 FOREACH_DEVICE_TAG(device
, tag
)
913 fprintf(f
, "G:%s\n", tag
);
916 r
= fflush_and_check(f
);
920 r
= rename(path_tmp
, path
);
926 log_device_debug(device
, "sd-device: Created %s file '%s' for '%s'", has_info
? "db" : "empty",
927 path
, device
->devpath
);
933 (void) unlink(path_tmp
);
935 return log_device_debug_errno(device
, r
, "sd-device: Failed to create %s file '%s' for '%s'", has_info
? "db" : "empty", path
, device
->devpath
);
938 int device_delete_db(sd_device
*device
) {
945 r
= device_get_id_filename(device
, &id
);
949 path
= strjoina("/run/udev/data/", id
);
952 if (r
< 0 && errno
!= ENOENT
)