1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
6 Copyright 2014 Tom Gundersen <teg@jklm.no>
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include <sys/types.h>
26 #include "sd-device.h"
28 #include "alloc-util.h"
29 #include "device-internal.h"
30 #include "device-private.h"
31 #include "device-util.h"
38 #include "parse-util.h"
39 #include "path-util.h"
42 #include "string-table.h"
43 #include "string-util.h"
46 #include "user-util.h"
49 int device_add_property(sd_device
*device
, const char *key
, const char *value
) {
55 r
= device_add_property_aux(device
, key
, value
, false);
60 r
= device_add_property_aux(device
, key
, value
, true);
68 static int device_add_property_internal_from_string(sd_device
*device
, const char *str
) {
69 _cleanup_free_
char *key
= NULL
;
79 value
= strchr(key
, '=');
88 return device_add_property_internal(device
, key
, value
);
91 static int handle_db_line(sd_device
*device
, char key
, const char *value
) {
100 path
= strjoina("/dev/", value
);
101 r
= device_add_devlink(device
, path
);
107 r
= safe_atoi(value
, &device
->devlink_priority
);
113 r
= device_add_property_internal_from_string(device
, value
);
119 r
= device_add_tag(device
, value
);
125 r
= safe_atoi(value
, &device
->watch_handle
);
131 r
= device_set_usec_initialized(device
, value
);
137 log_debug("device db: unknown key '%c'", key
);
143 void device_set_devlink_priority(sd_device
*device
, int priority
) {
146 device
->devlink_priority
= priority
;
149 void device_set_is_initialized(sd_device
*device
) {
152 device
->is_initialized
= true;
155 int device_ensure_usec_initialized(sd_device
*device
, sd_device
*device_old
) {
156 char num
[DECIMAL_STR_MAX(usec_t
)];
157 usec_t usec_initialized
;
162 if (device_old
&& device_old
->usec_initialized
> 0)
163 usec_initialized
= device_old
->usec_initialized
;
165 usec_initialized
= now(CLOCK_MONOTONIC
);
167 r
= snprintf(num
, sizeof(num
), USEC_FMT
, usec_initialized
);
171 r
= device_set_usec_initialized(device
, num
);
178 static int device_read_db(sd_device
*device
) {
179 _cleanup_free_
char *db
= NULL
;
181 const char *id
, *value
;
197 if (device
->db_loaded
|| device
->sealed
)
200 r
= device_get_id_filename(device
, &id
);
204 path
= strjoina("/run/udev/data/", id
);
206 r
= read_full_file(path
, &db
, &db_len
);
211 return log_debug_errno(r
, "sd-device: failed to read db '%s': %m", path
);
214 /* devices with a database entry are initialized */
215 device_set_is_initialized(device
);
217 for (i
= 0; i
< db_len
; i
++) {
220 if (!strchr(NEWLINE
, db
[i
])) {
229 log_debug("sd-device: ignoring invalid db entry with key '%c'", key
);
231 state
= INVALID_LINE
;
246 if (strchr(NEWLINE
, db
[i
]))
251 if (strchr(NEWLINE
, db
[i
])) {
253 r
= handle_db_line(device
, key
, value
);
255 log_debug_errno(r
, "sd-device: failed to handle db entry '%c:%s': %m", key
, value
);
262 assert_not_reached("invalid state when parsing db");
266 device
->db_loaded
= true;
271 uint64_t device_get_properties_generation(sd_device
*device
) {
274 return device
->properties_generation
;
277 uint64_t device_get_tags_generation(sd_device
*device
) {
280 return device
->tags_generation
;
283 uint64_t device_get_devlinks_generation(sd_device
*device
) {
286 return device
->devlinks_generation
;
289 int device_get_devnode_mode(sd_device
*device
, mode_t
*mode
) {
295 r
= device_read_db(device
);
299 *mode
= device
->devmode
;
304 int device_get_devnode_uid(sd_device
*device
, uid_t
*uid
) {
310 r
= device_read_db(device
);
314 *uid
= device
->devuid
;
319 static int device_set_devuid(sd_device
*device
, const char *uid
) {
326 r
= safe_atou(uid
, &u
);
330 r
= device_add_property_internal(device
, "DEVUID", uid
);
339 int device_get_devnode_gid(sd_device
*device
, gid_t
*gid
) {
345 r
= device_read_db(device
);
349 *gid
= device
->devgid
;
354 static int device_set_devgid(sd_device
*device
, const char *gid
) {
361 r
= safe_atou(gid
, &g
);
365 r
= device_add_property_internal(device
, "DEVGID", gid
);
374 static int device_amend(sd_device
*device
, const char *key
, const char *value
) {
381 if (streq(key
, "DEVPATH")) {
384 path
= strjoina("/sys", value
);
386 /* the caller must verify or trust this data (e.g., if it comes from the kernel) */
387 r
= device_set_syspath(device
, path
, false);
389 return log_debug_errno(r
, "sd-device: could not set syspath to '%s': %m", path
);
390 } else if (streq(key
, "SUBSYSTEM")) {
391 r
= device_set_subsystem(device
, value
);
393 return log_debug_errno(r
, "sd-device: could not set subsystem to '%s': %m", value
);
394 } else if (streq(key
, "DEVTYPE")) {
395 r
= device_set_devtype(device
, value
);
397 return log_debug_errno(r
, "sd-device: could not set devtype to '%s': %m", value
);
398 } else if (streq(key
, "DEVNAME")) {
399 r
= device_set_devname(device
, value
);
401 return log_debug_errno(r
, "sd-device: could not set devname to '%s': %m", value
);
402 } else if (streq(key
, "USEC_INITIALIZED")) {
403 r
= device_set_usec_initialized(device
, value
);
405 return log_debug_errno(r
, "sd-device: could not set usec-initialized to '%s': %m", value
);
406 } else if (streq(key
, "DRIVER")) {
407 r
= device_set_driver(device
, value
);
409 return log_debug_errno(r
, "sd-device: could not set driver to '%s': %m", value
);
410 } else if (streq(key
, "IFINDEX")) {
411 r
= device_set_ifindex(device
, value
);
413 return log_debug_errno(r
, "sd-device: could not set ifindex to '%s': %m", value
);
414 } else if (streq(key
, "DEVMODE")) {
415 r
= device_set_devmode(device
, value
);
417 return log_debug_errno(r
, "sd-device: could not set devmode to '%s': %m", value
);
418 } else if (streq(key
, "DEVUID")) {
419 r
= device_set_devuid(device
, value
);
421 return log_debug_errno(r
, "sd-device: could not set devuid to '%s': %m", value
);
422 } else if (streq(key
, "DEVGID")) {
423 r
= device_set_devgid(device
, value
);
425 return log_debug_errno(r
, "sd-device: could not set devgid to '%s': %m", value
);
426 } else if (streq(key
, "DEVLINKS")) {
427 const char *word
, *state
;
430 FOREACH_WORD(word
, l
, value
, state
) {
433 strncpy(devlink
, word
, l
);
436 r
= device_add_devlink(device
, devlink
);
438 return log_debug_errno(r
, "sd-device: could not add devlink '%s': %m", devlink
);
440 } else if (streq(key
, "TAGS")) {
441 const char *word
, *state
;
444 FOREACH_WORD_SEPARATOR(word
, l
, value
, ":", state
) {
447 (void)strncpy(tag
, word
, l
);
450 r
= device_add_tag(device
, tag
);
452 return log_debug_errno(r
, "sd-device: could not add tag '%s': %m", tag
);
455 r
= device_add_property_internal(device
, key
, value
);
457 return log_debug_errno(r
, "sd-device: could not add property '%s=%s': %m", key
, value
);
463 static const char* const device_action_table
[_DEVICE_ACTION_MAX
] = {
464 [DEVICE_ACTION_ADD
] = "add",
465 [DEVICE_ACTION_REMOVE
] = "remove",
466 [DEVICE_ACTION_CHANGE
] = "change",
467 [DEVICE_ACTION_MOVE
] = "move",
468 [DEVICE_ACTION_ONLINE
] = "online",
469 [DEVICE_ACTION_OFFLINE
] = "offline",
470 [DEVICE_ACTION_BIND
] = "bind",
471 [DEVICE_ACTION_UNBIND
] = "unbind",
474 DEFINE_STRING_TABLE_LOOKUP(device_action
, DeviceAction
);
476 static int device_append(sd_device
*device
, char *key
, const char **_major
, const char **_minor
, uint64_t *_seqnum
,
477 DeviceAction
*_action
) {
478 DeviceAction action
= _DEVICE_ACTION_INVALID
;
480 const char *major
= NULL
, *minor
= NULL
;
491 value
= strchr(key
, '=');
493 log_debug("sd-device: not a key-value pair: '%s'", key
);
501 if (streq(key
, "MAJOR"))
503 else if (streq(key
, "MINOR"))
506 if (streq(key
, "ACTION")) {
507 action
= device_action_from_string(value
);
508 if (action
== _DEVICE_ACTION_INVALID
)
510 } else if (streq(key
, "SEQNUM")) {
511 r
= safe_atou64(value
, &seqnum
);
514 else if (seqnum
== 0)
515 /* kernel only sends seqnum > 0 */
519 r
= device_amend(device
, key
, value
);
530 if (action
!= _DEVICE_ACTION_INVALID
)
539 void device_seal(sd_device
*device
) {
542 device
->sealed
= true;
545 static int device_verify(sd_device
*device
, DeviceAction action
, uint64_t seqnum
) {
548 if (!device
->devpath
|| !device
->subsystem
|| action
== _DEVICE_ACTION_INVALID
|| seqnum
== 0) {
549 log_debug("sd-device: device created from strv lacks devpath, subsystem, action or seqnum");
553 device
->sealed
= true;
558 int device_new_from_strv(sd_device
**ret
, char **strv
) {
559 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
561 const char *major
= NULL
, *minor
= NULL
;
562 DeviceAction action
= _DEVICE_ACTION_INVALID
;
569 r
= device_new_aux(&device
);
573 STRV_FOREACH(key
, strv
) {
574 r
= device_append(device
, *key
, &major
, &minor
, &seqnum
, &action
);
580 r
= device_set_devnum(device
, major
, minor
);
582 return log_debug_errno(r
, "sd-device: could not set devnum %s:%s: %m", major
, minor
);
585 r
= device_verify(device
, action
, seqnum
);
589 *ret
= TAKE_PTR(device
);
594 int device_new_from_nulstr(sd_device
**ret
, uint8_t *nulstr
, size_t len
) {
595 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
596 const char *major
= NULL
, *minor
= NULL
;
597 DeviceAction action
= _DEVICE_ACTION_INVALID
;
606 r
= device_new_aux(&device
);
614 key
= (char*)&nulstr
[i
];
615 end
= memchr(key
, '\0', len
- i
);
617 log_debug("sd-device: failed to parse nulstr");
622 r
= device_append(device
, key
, &major
, &minor
, &seqnum
, &action
);
628 r
= device_set_devnum(device
, major
, minor
);
630 return log_debug_errno(r
, "sd-device: could not set devnum %s:%s: %m", major
, minor
);
633 r
= device_verify(device
, action
, seqnum
);
637 *ret
= TAKE_PTR(device
);
642 static int device_update_properties_bufs(sd_device
*device
) {
643 const char *val
, *prop
;
644 _cleanup_free_
char **buf_strv
= NULL
;
645 _cleanup_free_
uint8_t *buf_nulstr
= NULL
;
646 size_t allocated_nulstr
= 0;
647 size_t nulstr_len
= 0, num
= 0, i
= 0;
651 if (!device
->properties_buf_outdated
)
654 FOREACH_DEVICE_PROPERTY(device
, prop
, val
) {
657 len
= strlen(prop
) + 1 + strlen(val
);
659 buf_nulstr
= GREEDY_REALLOC0(buf_nulstr
, allocated_nulstr
, nulstr_len
+ len
+ 2);
663 strscpyl((char *)buf_nulstr
+ nulstr_len
, len
+ 1, prop
, "=", val
, NULL
);
664 nulstr_len
+= len
+ 1;
668 /* build buf_strv from buf_nulstr */
669 buf_strv
= new0(char *, num
+ 1);
673 NULSTR_FOREACH(val
, (char*) buf_nulstr
) {
674 buf_strv
[i
] = (char *) val
;
679 free_and_replace(device
->properties_nulstr
, buf_nulstr
);
680 device
->properties_nulstr_len
= nulstr_len
;
681 free_and_replace(device
->properties_strv
, buf_strv
);
683 device
->properties_buf_outdated
= false;
688 int device_get_properties_nulstr(sd_device
*device
, const uint8_t **nulstr
, size_t *len
) {
695 r
= device_update_properties_bufs(device
);
699 *nulstr
= device
->properties_nulstr
;
700 *len
= device
->properties_nulstr_len
;
705 int device_get_properties_strv(sd_device
*device
, char ***strv
) {
711 r
= device_update_properties_bufs(device
);
715 *strv
= device
->properties_strv
;
720 int device_get_devlink_priority(sd_device
*device
, int *priority
) {
726 r
= device_read_db(device
);
730 *priority
= device
->devlink_priority
;
735 int device_get_watch_handle(sd_device
*device
, int *handle
) {
741 r
= device_read_db(device
);
745 *handle
= device
->watch_handle
;
750 void device_set_watch_handle(sd_device
*device
, int handle
) {
753 device
->watch_handle
= handle
;
756 int device_rename(sd_device
*device
, const char *name
) {
757 _cleanup_free_
char *dirname
= NULL
;
759 const char *interface
;
765 dirname
= dirname_malloc(device
->syspath
);
769 new_syspath
= strjoina(dirname
, "/", name
);
771 /* the user must trust that the new name is correct */
772 r
= device_set_syspath(device
, new_syspath
, false);
776 r
= sd_device_get_property_value(device
, "INTERFACE", &interface
);
778 /* like DEVPATH_OLD, INTERFACE_OLD is not saved to the db, but only stays around for the current event */
779 r
= device_add_property_internal(device
, "INTERFACE_OLD", interface
);
783 r
= device_add_property_internal(device
, "INTERFACE", name
);
786 } else if (r
!= -ENOENT
)
792 int device_shallow_clone(sd_device
*old_device
, sd_device
**new_device
) {
793 _cleanup_(sd_device_unrefp
) sd_device
*ret
= NULL
;
799 r
= device_new_aux(&ret
);
803 r
= device_set_syspath(ret
, old_device
->syspath
, false);
807 r
= device_set_subsystem(ret
, old_device
->subsystem
);
811 ret
->devnum
= old_device
->devnum
;
813 *new_device
= TAKE_PTR(ret
);
818 int device_clone_with_db(sd_device
*old_device
, sd_device
**new_device
) {
819 _cleanup_(sd_device_unrefp
) sd_device
*ret
= NULL
;
825 r
= device_shallow_clone(old_device
, &ret
);
829 r
= device_read_db(ret
);
835 *new_device
= TAKE_PTR(ret
);
840 int device_new_from_synthetic_event(sd_device
**new_device
, const char *syspath
, const char *action
) {
841 _cleanup_(sd_device_unrefp
) sd_device
*ret
= NULL
;
848 r
= sd_device_new_from_syspath(&ret
, syspath
);
852 r
= device_read_uevent_file(ret
);
856 r
= device_add_property_internal(ret
, "ACTION", action
);
860 *new_device
= TAKE_PTR(ret
);
865 int device_copy_properties(sd_device
*device_dst
, sd_device
*device_src
) {
866 const char *property
, *value
;
872 FOREACH_DEVICE_PROPERTY(device_src
, property
, value
) {
873 r
= device_add_property(device_dst
, property
, value
);
881 void device_cleanup_tags(sd_device
*device
) {
884 set_free_free(device
->tags
);
886 device
->property_tags_outdated
= true;
887 device
->tags_generation
++;
890 void device_cleanup_devlinks(sd_device
*device
) {
893 set_free_free(device
->devlinks
);
894 device
->devlinks
= NULL
;
895 device
->property_devlinks_outdated
= true;
896 device
->devlinks_generation
++;
899 void device_remove_tag(sd_device
*device
, const char *tag
) {
903 free(set_remove(device
->tags
, tag
));
904 device
->property_tags_outdated
= true;
905 device
->tags_generation
++;
908 static int device_tag(sd_device
*device
, const char *tag
, bool add
) {
916 r
= device_get_id_filename(device
, &id
);
920 path
= strjoina("/run/udev/tags/", tag
, "/", id
);
923 r
= touch_file(path
, true, USEC_INFINITY
, UID_INVALID
, GID_INVALID
, 0444);
928 if (r
< 0 && errno
!= ENOENT
)
935 int device_tag_index(sd_device
*device
, sd_device
*device_old
, bool add
) {
939 if (add
&& device_old
) {
940 /* delete possible left-over tags */
941 FOREACH_DEVICE_TAG(device_old
, tag
) {
942 if (!sd_device_has_tag(device
, tag
)) {
943 k
= device_tag(device_old
, tag
, false);
950 FOREACH_DEVICE_TAG(device
, tag
) {
951 k
= device_tag(device
, tag
, add
);
959 static bool device_has_info(sd_device
*device
) {
962 if (!set_isempty(device
->devlinks
))
965 if (device
->devlink_priority
!= 0)
968 if (!ordered_hashmap_isempty(device
->properties_db
))
971 if (!set_isempty(device
->tags
))
974 if (device
->watch_handle
>= 0)
980 void device_set_db_persist(sd_device
*device
) {
983 device
->db_persist
= true;
986 int device_update_db(sd_device
*device
) {
989 _cleanup_fclose_
FILE *f
= NULL
;
990 _cleanup_free_
char *path_tmp
= NULL
;
996 has_info
= device_has_info(device
);
998 r
= device_get_id_filename(device
, &id
);
1002 path
= strjoina("/run/udev/data/", id
);
1004 /* do not store anything for otherwise empty devices */
1005 if (!has_info
&& major(device
->devnum
) == 0 && device
->ifindex
== 0) {
1007 if (r
< 0 && errno
!= ENOENT
)
1013 /* write a database file */
1014 r
= mkdir_parents(path
, 0755);
1018 r
= fopen_temporary(path
, &f
, &path_tmp
);
1023 * set 'sticky' bit to indicate that we should not clean the
1024 * database when we transition from initramfs to the real root
1026 if (device
->db_persist
) {
1027 r
= fchmod(fileno(f
), 01644);
1033 r
= fchmod(fileno(f
), 0644);
1041 const char *property
, *value
, *tag
;
1044 if (major(device
->devnum
) > 0) {
1045 const char *devlink
;
1047 FOREACH_DEVICE_DEVLINK(device
, devlink
)
1048 fprintf(f
, "S:%s\n", devlink
+ STRLEN("/dev/"));
1050 if (device
->devlink_priority
!= 0)
1051 fprintf(f
, "L:%i\n", device
->devlink_priority
);
1053 if (device
->watch_handle
>= 0)
1054 fprintf(f
, "W:%i\n", device
->watch_handle
);
1057 if (device
->usec_initialized
> 0)
1058 fprintf(f
, "I:"USEC_FMT
"\n", device
->usec_initialized
);
1060 ORDERED_HASHMAP_FOREACH_KEY(value
, property
, device
->properties_db
, i
)
1061 fprintf(f
, "E:%s=%s\n", property
, value
);
1063 FOREACH_DEVICE_TAG(device
, tag
)
1064 fprintf(f
, "G:%s\n", tag
);
1067 r
= fflush_and_check(f
);
1071 r
= rename(path_tmp
, path
);
1077 log_debug("created %s file '%s' for '%s'", has_info
? "db" : "empty",
1078 path
, device
->devpath
);
1083 (void) unlink(path
);
1084 (void) unlink(path_tmp
);
1086 return log_error_errno(r
, "failed to create %s file '%s' for '%s'", has_info
? "db" : "empty", path
, device
->devpath
);
1089 int device_delete_db(sd_device
*device
) {
1096 r
= device_get_id_filename(device
, &id
);
1100 path
= strjoina("/run/udev/data/", id
);
1103 if (r
< 0 && errno
!= ENOENT
)
1109 int device_read_db_force(sd_device
*device
) {
1112 return device_read_db_aux(device
, true);