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"
22 #include "string-table.h"
23 #include "string-util.h"
26 #include "tmpfile-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 void device_set_devlink_priority(sd_device
*device
, int priority
) {
52 device
->devlink_priority
= priority
;
55 void device_set_is_initialized(sd_device
*device
) {
58 device
->is_initialized
= true;
61 int device_ensure_usec_initialized(sd_device
*device
, sd_device
*device_old
) {
66 if (device_old
&& device_old
->usec_initialized
> 0)
67 when
= device_old
->usec_initialized
;
69 when
= now(CLOCK_MONOTONIC
);
71 return device_set_usec_initialized(device
, when
);
74 uint64_t device_get_properties_generation(sd_device
*device
) {
77 return device
->properties_generation
;
80 uint64_t device_get_tags_generation(sd_device
*device
) {
83 return device
->tags_generation
;
86 uint64_t device_get_devlinks_generation(sd_device
*device
) {
89 return device
->devlinks_generation
;
92 int device_get_devnode_mode(sd_device
*device
, mode_t
*mode
) {
97 r
= device_read_db(device
);
101 if (device
->devmode
== (mode_t
) -1)
105 *mode
= device
->devmode
;
110 int device_get_devnode_uid(sd_device
*device
, uid_t
*uid
) {
115 r
= device_read_db(device
);
119 if (device
->devuid
== (uid_t
) -1)
123 *uid
= device
->devuid
;
128 static int device_set_devuid(sd_device
*device
, const char *uid
) {
135 r
= safe_atou(uid
, &u
);
139 r
= device_add_property_internal(device
, "DEVUID", uid
);
148 int device_get_devnode_gid(sd_device
*device
, gid_t
*gid
) {
153 r
= device_read_db(device
);
157 if (device
->devgid
== (gid_t
) -1)
161 *gid
= device
->devgid
;
166 static int device_set_devgid(sd_device
*device
, const char *gid
) {
173 r
= safe_atou(gid
, &g
);
177 r
= device_add_property_internal(device
, "DEVGID", gid
);
186 static int device_amend(sd_device
*device
, const char *key
, const char *value
) {
193 if (streq(key
, "DEVPATH")) {
196 path
= strjoina("/sys", value
);
198 /* the caller must verify or trust this data (e.g., if it comes from the kernel) */
199 r
= device_set_syspath(device
, path
, false);
201 return log_device_debug_errno(device
, r
, "sd-device: Failed to set syspath to '%s': %m", path
);
202 } else if (streq(key
, "SUBSYSTEM")) {
203 r
= device_set_subsystem(device
, value
);
205 return log_device_debug_errno(device
, r
, "sd-device: Failed to set subsystem to '%s': %m", value
);
206 } else if (streq(key
, "DEVTYPE")) {
207 r
= device_set_devtype(device
, value
);
209 return log_device_debug_errno(device
, r
, "sd-device: Failed to set devtype to '%s': %m", value
);
210 } else if (streq(key
, "DEVNAME")) {
211 r
= device_set_devname(device
, value
);
213 return log_device_debug_errno(device
, r
, "sd-device: Failed to set devname to '%s': %m", value
);
214 } else if (streq(key
, "USEC_INITIALIZED")) {
217 r
= safe_atou64(value
, &t
);
219 return log_device_debug_errno(device
, r
, "sd-device: Failed to parse timestamp '%s': %m", value
);
221 r
= device_set_usec_initialized(device
, t
);
223 return log_device_debug_errno(device
, r
, "sd-device: Failed to set usec-initialized to '%s': %m", value
);
224 } else if (streq(key
, "DRIVER")) {
225 r
= device_set_driver(device
, value
);
227 return log_device_debug_errno(device
, r
, "sd-device: Failed to set driver to '%s': %m", value
);
228 } else if (streq(key
, "IFINDEX")) {
229 r
= device_set_ifindex(device
, value
);
231 return log_device_debug_errno(device
, r
, "sd-device: Failed to set ifindex to '%s': %m", value
);
232 } else if (streq(key
, "DEVMODE")) {
233 r
= device_set_devmode(device
, value
);
235 return log_device_debug_errno(device
, r
, "sd-device: Failed to set devmode to '%s': %m", value
);
236 } else if (streq(key
, "DEVUID")) {
237 r
= device_set_devuid(device
, value
);
239 return log_device_debug_errno(device
, r
, "sd-device: Failed to set devuid to '%s': %m", value
);
240 } else if (streq(key
, "DEVGID")) {
241 r
= device_set_devgid(device
, value
);
243 return log_device_debug_errno(device
, r
, "sd-device: Failed to set devgid to '%s': %m", value
);
244 } else if (streq(key
, "DEVLINKS")) {
245 const char *word
, *state
;
248 FOREACH_WORD(word
, l
, value
, state
) {
251 strncpy(devlink
, word
, l
);
254 r
= device_add_devlink(device
, devlink
);
256 return log_device_debug_errno(device
, r
, "sd-device: Failed to add devlink '%s': %m", devlink
);
258 } else if (streq(key
, "TAGS")) {
259 const char *word
, *state
;
262 FOREACH_WORD_SEPARATOR(word
, l
, value
, ":", state
) {
265 (void) strncpy(tag
, word
, l
);
268 r
= device_add_tag(device
, tag
);
270 return log_device_debug_errno(device
, r
, "sd-device: Failed to add tag '%s': %m", tag
);
273 r
= device_add_property_internal(device
, key
, value
);
275 return log_device_debug_errno(device
, r
, "sd-device: Failed to add property '%s=%s': %m", key
, value
);
281 static const char* const device_action_table
[_DEVICE_ACTION_MAX
] = {
282 [DEVICE_ACTION_ADD
] = "add",
283 [DEVICE_ACTION_REMOVE
] = "remove",
284 [DEVICE_ACTION_CHANGE
] = "change",
285 [DEVICE_ACTION_MOVE
] = "move",
286 [DEVICE_ACTION_ONLINE
] = "online",
287 [DEVICE_ACTION_OFFLINE
] = "offline",
288 [DEVICE_ACTION_BIND
] = "bind",
289 [DEVICE_ACTION_UNBIND
] = "unbind",
292 DEFINE_STRING_TABLE_LOOKUP(device_action
, DeviceAction
);
294 static int device_append(sd_device
*device
, char *key
, const char **_major
, const char **_minor
, uint64_t *_seqnum
,
295 DeviceAction
*_action
) {
296 DeviceAction action
= _DEVICE_ACTION_INVALID
;
298 const char *major
= NULL
, *minor
= NULL
;
309 value
= strchr(key
, '=');
311 log_device_debug(device
, "sd-device: Not a key-value pair: '%s'", key
);
319 if (streq(key
, "MAJOR"))
321 else if (streq(key
, "MINOR"))
324 if (streq(key
, "ACTION")) {
325 action
= device_action_from_string(value
);
326 if (action
== _DEVICE_ACTION_INVALID
)
328 } else if (streq(key
, "SEQNUM")) {
329 r
= safe_atou64(value
, &seqnum
);
332 else if (seqnum
== 0)
333 /* kernel only sends seqnum > 0 */
337 r
= device_amend(device
, key
, value
);
348 if (action
!= _DEVICE_ACTION_INVALID
)
357 void device_seal(sd_device
*device
) {
360 device
->sealed
= true;
363 static int device_verify(sd_device
*device
, DeviceAction action
, uint64_t seqnum
) {
366 if (!device
->devpath
|| !device
->subsystem
|| action
== _DEVICE_ACTION_INVALID
|| seqnum
== 0) {
367 log_device_debug(device
, "sd-device: Device created from strv or nulstr lacks devpath, subsystem, action or seqnum.");
371 device
->sealed
= true;
376 int device_new_from_strv(sd_device
**ret
, char **strv
) {
377 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
379 const char *major
= NULL
, *minor
= NULL
;
380 DeviceAction action
= _DEVICE_ACTION_INVALID
;
387 r
= device_new_aux(&device
);
391 STRV_FOREACH(key
, strv
) {
392 r
= device_append(device
, *key
, &major
, &minor
, &seqnum
, &action
);
398 r
= device_set_devnum(device
, major
, minor
);
400 return log_device_debug_errno(device
, r
, "sd-device: Failed to set devnum %s:%s: %m", major
, minor
);
403 r
= device_verify(device
, action
, seqnum
);
407 *ret
= TAKE_PTR(device
);
412 int device_new_from_nulstr(sd_device
**ret
, uint8_t *nulstr
, size_t len
) {
413 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
414 const char *major
= NULL
, *minor
= NULL
;
415 DeviceAction action
= _DEVICE_ACTION_INVALID
;
424 r
= device_new_aux(&device
);
432 key
= (char*)&nulstr
[i
];
433 end
= memchr(key
, '\0', len
- i
);
435 log_device_debug(device
, "sd-device: Failed to parse nulstr");
440 r
= device_append(device
, key
, &major
, &minor
, &seqnum
, &action
);
446 r
= device_set_devnum(device
, major
, minor
);
448 return log_device_debug_errno(device
, r
, "sd-device: Failed to set devnum %s:%s: %m", major
, minor
);
451 r
= device_verify(device
, action
, seqnum
);
455 *ret
= TAKE_PTR(device
);
460 static int device_update_properties_bufs(sd_device
*device
) {
461 const char *val
, *prop
;
462 _cleanup_free_
char **buf_strv
= NULL
;
463 _cleanup_free_
uint8_t *buf_nulstr
= NULL
;
464 size_t allocated_nulstr
= 0;
465 size_t nulstr_len
= 0, num
= 0, i
= 0;
469 if (!device
->properties_buf_outdated
)
472 FOREACH_DEVICE_PROPERTY(device
, prop
, val
) {
475 len
= strlen(prop
) + 1 + strlen(val
);
477 buf_nulstr
= GREEDY_REALLOC0(buf_nulstr
, allocated_nulstr
, nulstr_len
+ len
+ 2);
481 strscpyl((char *)buf_nulstr
+ nulstr_len
, len
+ 1, prop
, "=", val
, NULL
);
482 nulstr_len
+= len
+ 1;
486 /* build buf_strv from buf_nulstr */
487 buf_strv
= new0(char *, num
+ 1);
491 NULSTR_FOREACH(val
, (char*) buf_nulstr
) {
492 buf_strv
[i
] = (char *) val
;
497 free_and_replace(device
->properties_nulstr
, buf_nulstr
);
498 device
->properties_nulstr_len
= nulstr_len
;
499 free_and_replace(device
->properties_strv
, buf_strv
);
501 device
->properties_buf_outdated
= false;
506 int device_get_properties_nulstr(sd_device
*device
, const uint8_t **nulstr
, size_t *len
) {
513 r
= device_update_properties_bufs(device
);
517 *nulstr
= device
->properties_nulstr
;
518 *len
= device
->properties_nulstr_len
;
523 int device_get_properties_strv(sd_device
*device
, char ***strv
) {
529 r
= device_update_properties_bufs(device
);
533 *strv
= device
->properties_strv
;
538 int device_get_devlink_priority(sd_device
*device
, int *priority
) {
544 r
= device_read_db(device
);
548 *priority
= device
->devlink_priority
;
553 int device_get_watch_handle(sd_device
*device
, int *handle
) {
558 r
= device_read_db(device
);
562 if (device
->watch_handle
< 0)
566 *handle
= device
->watch_handle
;
571 void device_set_watch_handle(sd_device
*device
, int handle
) {
574 device
->watch_handle
= handle
;
577 int device_rename(sd_device
*device
, const char *name
) {
578 _cleanup_free_
char *dirname
= NULL
;
580 const char *interface
;
586 dirname
= dirname_malloc(device
->syspath
);
590 new_syspath
= strjoina(dirname
, "/", name
);
592 /* the user must trust that the new name is correct */
593 r
= device_set_syspath(device
, new_syspath
, false);
597 r
= sd_device_get_property_value(device
, "INTERFACE", &interface
);
599 /* like DEVPATH_OLD, INTERFACE_OLD is not saved to the db, but only stays around for the current event */
600 r
= device_add_property_internal(device
, "INTERFACE_OLD", interface
);
604 r
= device_add_property_internal(device
, "INTERFACE", name
);
607 } else if (r
!= -ENOENT
)
613 int device_shallow_clone(sd_device
*old_device
, sd_device
**new_device
) {
614 _cleanup_(sd_device_unrefp
) sd_device
*ret
= NULL
;
620 r
= device_new_aux(&ret
);
624 r
= device_set_syspath(ret
, old_device
->syspath
, false);
628 r
= device_set_subsystem(ret
, old_device
->subsystem
);
632 ret
->devnum
= old_device
->devnum
;
634 *new_device
= TAKE_PTR(ret
);
639 int device_clone_with_db(sd_device
*old_device
, sd_device
**new_device
) {
640 _cleanup_(sd_device_unrefp
) sd_device
*ret
= NULL
;
646 r
= device_shallow_clone(old_device
, &ret
);
650 r
= device_read_db(ret
);
656 *new_device
= TAKE_PTR(ret
);
661 int device_new_from_synthetic_event(sd_device
**new_device
, const char *syspath
, const char *action
) {
662 _cleanup_(sd_device_unrefp
) sd_device
*ret
= NULL
;
669 r
= sd_device_new_from_syspath(&ret
, syspath
);
673 r
= device_read_uevent_file(ret
);
677 r
= device_add_property_internal(ret
, "ACTION", action
);
681 *new_device
= TAKE_PTR(ret
);
686 int device_new_from_stat_rdev(sd_device
**ret
, const struct stat
*st
) {
692 if (S_ISBLK(st
->st_mode
))
694 else if (S_ISCHR(st
->st_mode
))
699 return sd_device_new_from_devnum(ret
, type
, st
->st_rdev
);
702 int device_copy_properties(sd_device
*device_dst
, sd_device
*device_src
) {
703 const char *property
, *value
;
710 r
= device_properties_prepare(device_src
);
714 ORDERED_HASHMAP_FOREACH_KEY(value
, property
, device_src
->properties_db
, i
) {
715 r
= device_add_property_aux(device_dst
, property
, value
, true);
720 ORDERED_HASHMAP_FOREACH_KEY(value
, property
, device_src
->properties
, i
) {
721 r
= device_add_property_aux(device_dst
, property
, value
, false);
729 void device_cleanup_tags(sd_device
*device
) {
732 set_free_free(device
->tags
);
734 device
->property_tags_outdated
= true;
735 device
->tags_generation
++;
738 void device_cleanup_devlinks(sd_device
*device
) {
741 set_free_free(device
->devlinks
);
742 device
->devlinks
= NULL
;
743 device
->property_devlinks_outdated
= true;
744 device
->devlinks_generation
++;
747 void device_remove_tag(sd_device
*device
, const char *tag
) {
751 free(set_remove(device
->tags
, tag
));
752 device
->property_tags_outdated
= true;
753 device
->tags_generation
++;
756 static int device_tag(sd_device
*device
, const char *tag
, bool add
) {
764 r
= device_get_id_filename(device
, &id
);
768 path
= strjoina("/run/udev/tags/", tag
, "/", id
);
771 r
= touch_file(path
, true, USEC_INFINITY
, UID_INVALID
, GID_INVALID
, 0444);
776 if (r
< 0 && errno
!= ENOENT
)
783 int device_tag_index(sd_device
*device
, sd_device
*device_old
, bool add
) {
787 if (add
&& device_old
) {
788 /* delete possible left-over tags */
789 FOREACH_DEVICE_TAG(device_old
, tag
) {
790 if (!sd_device_has_tag(device
, tag
)) {
791 k
= device_tag(device_old
, tag
, false);
798 FOREACH_DEVICE_TAG(device
, tag
) {
799 k
= device_tag(device
, tag
, add
);
807 static bool device_has_info(sd_device
*device
) {
810 if (!set_isempty(device
->devlinks
))
813 if (device
->devlink_priority
!= 0)
816 if (!ordered_hashmap_isempty(device
->properties_db
))
819 if (!set_isempty(device
->tags
))
822 if (device
->watch_handle
>= 0)
828 void device_set_db_persist(sd_device
*device
) {
831 device
->db_persist
= true;
834 int device_update_db(sd_device
*device
) {
837 _cleanup_fclose_
FILE *f
= NULL
;
838 _cleanup_free_
char *path_tmp
= NULL
;
844 has_info
= device_has_info(device
);
846 r
= device_get_id_filename(device
, &id
);
850 path
= strjoina("/run/udev/data/", id
);
852 /* do not store anything for otherwise empty devices */
853 if (!has_info
&& major(device
->devnum
) == 0 && device
->ifindex
== 0) {
855 if (r
< 0 && errno
!= ENOENT
)
861 /* write a database file */
862 r
= mkdir_parents(path
, 0755);
866 r
= fopen_temporary(path
, &f
, &path_tmp
);
871 * set 'sticky' bit to indicate that we should not clean the
872 * database when we transition from initramfs to the real root
874 if (device
->db_persist
) {
875 r
= fchmod(fileno(f
), 01644);
881 r
= fchmod(fileno(f
), 0644);
889 const char *property
, *value
, *tag
;
892 if (major(device
->devnum
) > 0) {
895 FOREACH_DEVICE_DEVLINK(device
, devlink
)
896 fprintf(f
, "S:%s\n", devlink
+ STRLEN("/dev/"));
898 if (device
->devlink_priority
!= 0)
899 fprintf(f
, "L:%i\n", device
->devlink_priority
);
901 if (device
->watch_handle
>= 0)
902 fprintf(f
, "W:%i\n", device
->watch_handle
);
905 if (device
->usec_initialized
> 0)
906 fprintf(f
, "I:"USEC_FMT
"\n", device
->usec_initialized
);
908 ORDERED_HASHMAP_FOREACH_KEY(value
, property
, device
->properties_db
, i
)
909 fprintf(f
, "E:%s=%s\n", property
, value
);
911 FOREACH_DEVICE_TAG(device
, tag
)
912 fprintf(f
, "G:%s\n", tag
);
915 r
= fflush_and_check(f
);
919 r
= rename(path_tmp
, path
);
925 log_device_debug(device
, "sd-device: Created %s file '%s' for '%s'", has_info
? "db" : "empty",
926 path
, device
->devpath
);
932 (void) unlink(path_tmp
);
934 return log_device_debug_errno(device
, r
, "sd-device: Failed to create %s file '%s' for '%s'", has_info
? "db" : "empty", path
, device
->devpath
);
937 int device_delete_db(sd_device
*device
) {
944 r
= device_get_id_filename(device
, &id
);
948 path
= strjoina("/run/udev/data/", id
);
951 if (r
< 0 && errno
!= ENOENT
)