HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/libsystemd/sd-device/sd-device.c
Git blob: 5bba62e3e0d2a79e44042a3074bf19ca5ccbec3e
Lines 1–1000 of 2,953
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <linux/magic.h>
#include <unistd.h>
#include "sd-device.h"
#include "sd-id128.h"
#include "alloc-util.h"
#include "chase.h"
#include "device-internal.h"
#include "device-private.h"
#include "device-util.h"
#include "devnum-util.h"
#include "dirent-util.h"
#include "env-util.h"
#include "errno-util.h"
#include "escape.h"
#include "extract-word.h"
#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
#include "hashmap.h"
#include "netlink-util.h"
#include "parse-util.h"
#include "path-util.h"
#include "set.h"
#include "socket-util.h"
#include "stat-util.h"
#include "stdio-util.h"
#include "string-util.h"
#include "strv.h"
#include "time-util.h"
int device_new_aux(sd_device **ret) {
sd_device *device;
assert(ret);
device = new(sd_device, 1);
if (!device)
return -ENOMEM;
*device = (sd_device) {
.n_ref = 1,
.devmode = MODE_INVALID,
.devuid = UID_INVALID,
.devgid = GID_INVALID,
.action = _SD_DEVICE_ACTION_INVALID,
.database_version = OLDEST_UDEV_DATABASE_VERSION,
};
*ret = device;
return 0;
}
static sd_device* device_free(sd_device *device) {
assert(device);
sd_device_unref(device->parent);
free(device->syspath);
free(device->sysname);
free(device->devtype);
free(device->devname);
free(device->subsystem);
free(device->driver_subsystem);
free(device->driver);
free(device->device_id);
free(device->properties_strv);
free(device->properties_nulstr);
ordered_hashmap_free(device->properties);
ordered_hashmap_free(device->properties_db);
hashmap_free(device->sysattr_values);
set_free(device->sysattrs);
set_free(device->all_tags);
set_free(device->current_tags);
set_free(device->devlinks);
hashmap_free(device->children);
return mfree(device);
}
DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_device, sd_device, device_free);
static bool property_is_valid(const char *key, const char *value) {
/* Device properties may be saved to database file, then may be parsed from the file. When if a
* property contains spurious characters, then the parser may be confused. Let's refuse spurious
* properties, even if it is internal, which will not be saved to database file, for consistency. */
if (isempty(key) || !in_charset(key, ALPHANUMERICAL "_."))
return false;
/* an empty value means unset the property, hence that's fine. */
if (isempty(value))
return true;
/* refuse invalid UTF8 and control characters */
return string_is_safe(value,
STRING_ALLOW_BACKSLASHES |
STRING_ALLOW_QUOTES |
STRING_ALLOW_GLOBS);
}
int device_add_property_aux(sd_device *device, const char *key, const char *value, bool db) {
OrderedHashmap **properties;
assert(device);
assert(key);
if (!property_is_valid(key, value)) {
if (DEBUG_LOGGING) {
_cleanup_free_ char *escaped_key = cescape(key),
*escaped_value = cescape(strempty(value));
log_device_debug(device, "sd-device: Refusing invalid property: %s=%s",
strnull(escaped_key), strnull(escaped_value));
}
return -EINVAL;
}
if (db)
properties = &device->properties_db;
else
properties = &device->properties;
if (!isempty(value)) {
_unused_ _cleanup_free_ char *old_value = NULL;
_cleanup_free_ char *new_key = NULL, *new_value = NULL, *old_key = NULL;
int r;
r = ordered_hashmap_ensure_allocated(properties, &string_hash_ops_free_free);
if (r < 0)
return r;
new_key = strdup(key);
if (!new_key)
return -ENOMEM;
new_value = strdup(value);
if (!new_value)
return -ENOMEM;
old_value = ordered_hashmap_get2(*properties, key, (void**) &old_key);
/* ordered_hashmap_replace() does not fail when the hashmap already has the entry. */
r = ordered_hashmap_replace(*properties, new_key, new_value);
if (r < 0)
return r;
TAKE_PTR(new_key);
TAKE_PTR(new_value);
} else {
_unused_ _cleanup_free_ char *old_value = NULL;
_cleanup_free_ char *old_key = NULL;
old_value = ordered_hashmap_remove2(*properties, key, (void**) &old_key);
}
if (!db) {
device->properties_generation++;
device->properties_buf_outdated = true;
}
return 0;
}
int device_set_syspath(sd_device *device, const char *_syspath, bool verify) {
_cleanup_free_ char *syspath = NULL;
const char *devpath;
int r;
assert(device);
assert(_syspath);
if (verify) {
_cleanup_close_ int fd = -EBADF;
/* The input path maybe a symlink located outside of /sys. Let's try to chase the symlink at first.
* The primary use case is that e.g. /proc/device-tree is a symlink to /sys/firmware/devicetree/base.
* By chasing symlinks in the path at first, we can call sd_device_new_from_path() with such path. */
r = chase(_syspath, NULL, 0, &syspath, &fd);
if (r == -ENOENT)
/* the device does not exist (any more?) */
return log_trace_errno(SYNTHETIC_ERRNO(ENODEV),
"sd-device: Device \"%s\" not found.", _syspath);
if (r < 0)
return log_debug_errno(r, "sd-device: Failed to get target of '%s': %m", _syspath);
if (!path_startswith(syspath, "/sys")) {
_cleanup_free_ char *real_sys = NULL, *new_syspath = NULL;
char *p;
/* /sys is a symlink to somewhere sysfs is mounted on? In that case, we convert the path to real sysfs to "/sys". */
r = chase("/sys", NULL, 0, &real_sys, NULL);
if (r < 0)
return log_debug_errno(r, "sd-device: Failed to chase symlink /sys: %m");
p = path_startswith(syspath, real_sys);
if (!p)
return log_debug_errno(SYNTHETIC_ERRNO(ENODEV),
"sd-device: Canonicalized path '%s' does not starts with sysfs mount point '%s'",
syspath, real_sys);
new_syspath = path_join("/sys", p);
if (!new_syspath)
return log_oom_debug();
free_and_replace(syspath, new_syspath);
path_simplify(syspath);
}
if (path_startswith(syspath, "/sys/devices/")) {
/* For proper devices, stricter rules apply: they must have a 'uevent' file,
* otherwise we won't allow them */
if (faccessat(fd, "uevent", F_OK, 0) < 0) {
if (errno == ENOENT)
/* This is not a valid device. Note, this condition is quite often
* satisfied when enumerating devices or finding a parent device.
* Hence, use log_trace_errno() here. */
return log_trace_errno(SYNTHETIC_ERRNO(ENODEV),
"sd-device: the uevent file \"%s/uevent\" does not exist.", syspath);
if (errno == ENOTDIR)
/* Not actually a directory. */
return log_debug_errno(SYNTHETIC_ERRNO(ENODEV),
"sd-device: the syspath \"%s\" is not a directory.", syspath);
return log_debug_errno(errno, "sd-device: cannot find uevent file for %s: %m", syspath);
}
} else {
struct stat st;
/* For everything else lax rules apply: they just need to be a directory */
if (fstat(fd, &st) < 0)
return log_debug_errno(errno, "sd-device: failed to check if syspath \"%s\" is a directory: %m", syspath);
if (!S_ISDIR(st.st_mode))
return log_debug_errno(SYNTHETIC_ERRNO(ENODEV),
"sd-device: the syspath \"%s\" is not a directory.", syspath);
}
/* Only operate on sysfs, i.e. refuse going down into /sys/fs/cgroup/ or similar places where
* things are not arranged as kobjects in kernel, and hence don't necessarily have
* kobject/attribute structure. */
r = secure_getenv_bool("SYSTEMD_DEVICE_VERIFY_SYSFS");
if (r < 0 && r != -ENXIO)
log_debug_errno(r, "Failed to parse $SYSTEMD_DEVICE_VERIFY_SYSFS value: %m");
if (r != 0) {
r = fd_is_fs_type(fd, SYSFS_MAGIC);
if (r < 0)
return log_debug_errno(r, "sd-device: failed to check if syspath \"%s\" is backed by sysfs.", syspath);
if (r == 0)
return log_debug_errno(SYNTHETIC_ERRNO(ENODEV),
"sd-device: the syspath \"%s\" is outside of sysfs, refusing.", syspath);
}
} else {
/* must be a subdirectory of /sys */
if (!path_startswith(_syspath, "/sys/"))
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
"sd-device: Syspath '%s' is not a subdirectory of /sys",
_syspath);
r = path_simplify_alloc(_syspath, &syspath);
if (r < 0)
return log_oom_debug();
}
assert_se(devpath = startswith(syspath, "/sys"));
if (devpath[0] != '/')
return log_debug_errno(SYNTHETIC_ERRNO(ENODEV), "sd-device: \"/sys\" alone is not a valid device path.");
r = device_add_property_internal(device, "DEVPATH", devpath);
if (r < 0)
return log_debug_errno(r, "sd-device: Failed to add \"DEVPATH\" property for device \"%s\": %m", syspath);
free_and_replace(device->syspath, syspath);
device->devpath = devpath;
/* Unset sysname and sysnum, they will be assigned when requested. */
device->sysnum = NULL;
device->sysname = mfree(device->sysname);
return 0;
}
static int device_new_from_syspath(sd_device **ret, const char *syspath, bool strict) {
_cleanup_(sd_device_unrefp) sd_device *device = NULL;
int r;
assert_return(ret, -EINVAL);
assert_return(syspath, -EINVAL);
if (strict && !path_startswith(syspath, "/sys/"))
return -EINVAL;
r = device_new_aux(&device);
if (r < 0)
return r;
r = device_set_syspath(device, syspath, /* verify= */ true);
if (r < 0)
return r;
*ret = TAKE_PTR(device);
return 0;
}
_public_ int sd_device_new_from_syspath(sd_device **ret, const char *syspath) {
return device_new_from_syspath(ret, syspath, /* strict= */ true);
}
int device_new_from_mode_and_devnum(sd_device **ret, mode_t mode, dev_t devnum) {
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
_cleanup_free_ char *syspath = NULL;
const char *t;
dev_t n;
int r;
assert(ret);
if (S_ISCHR(mode))
t = "char";
else if (S_ISBLK(mode))
t = "block";
else
return -ENOTTY;
if (major(devnum) == 0)
return -ENODEV;
if (asprintf(&syspath, "/sys/dev/%s/" DEVNUM_FORMAT_STR, t, DEVNUM_FORMAT_VAL(devnum)) < 0)
return -ENOMEM;
r = sd_device_new_from_syspath(&dev, syspath);
if (r < 0)
return r;
r = sd_device_get_devnum(dev, &n);
if (r == -ENOENT)
return -ENXIO;
if (r < 0)
return r;
if (n != devnum)
return -ENXIO;
r = device_in_subsystem(dev, "block");
if (r < 0)
return r;
if (r > 0 ? !S_ISBLK(mode) : !S_ISCHR(mode))
return -ENXIO;
*ret = TAKE_PTR(dev);
return 0;
}
_public_ int sd_device_new_from_devnum(sd_device **ret, char type, dev_t devnum) {
assert_return(ret, -EINVAL);
assert_return(IN_SET(type, 'b', 'c'), -EINVAL);
return device_new_from_mode_and_devnum(ret, type == 'b' ? S_IFBLK : S_IFCHR, devnum);
}
static int device_new_from_main_ifname(sd_device **ret, const char *ifname) {
const char *syspath;
assert(ret);
assert(ifname);
syspath = strjoina("/sys/class/net/", ifname);
return sd_device_new_from_syspath(ret, syspath);
}
_public_ int sd_device_new_from_ifname(sd_device **ret, const char *ifname) {
_cleanup_free_ char *main_name = NULL;
int r;
assert_return(ret, -EINVAL);
assert_return(ifname, -EINVAL);
if (ifname_valid(ifname)) {
r = device_new_from_main_ifname(ret, ifname);
if (r >= 0)
return r;
}
r = rtnl_resolve_ifname_full(NULL, RESOLVE_IFNAME_ALTERNATIVE | RESOLVE_IFNAME_NUMERIC, ifname, &main_name, NULL);
if (r < 0)
return r;
return device_new_from_main_ifname(ret, main_name);
}
_public_ int sd_device_new_from_ifindex(sd_device **ret, int ifindex) {
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
_cleanup_free_ char *ifname = NULL;
int r, i;
assert_return(ret, -EINVAL);
assert_return(ifindex > 0, -EINVAL);
r = rtnl_get_ifname(NULL, ifindex, &ifname);
if (r < 0)
return r;
r = device_new_from_main_ifname(&dev, ifname);
if (r < 0)
return r;
r = sd_device_get_ifindex(dev, &i);
if (r == -ENOENT)
return -ENXIO;
if (r < 0)
return r;
if (i != ifindex)
return -ENXIO;
*ret = TAKE_PTR(dev);
return 0;
}
static int device_new_from_path_join(
sd_device **device,
const char *subsystem,
const char *driver_subsystem,
const char *sysname,
const char *a,
const char *b,
const char *c,
const char *d) {
_cleanup_(sd_device_unrefp) sd_device *new_device = NULL;
_cleanup_free_ char *p = NULL;
int r;
assert(device);
assert(sysname);
p = path_join(a, b, c, d);
if (!p)
return -ENOMEM;
r = sd_device_new_from_syspath(&new_device, p);
if (r == -ENODEV)
return 0;
if (r < 0)
return r;
/* Check if the found device really has the expected subsystem and sysname, for safety. */
r = device_in_subsystem(new_device, subsystem);
if (r <= 0)
return r;
const char *new_driver_subsystem = NULL;
(void) sd_device_get_driver_subsystem(new_device, &new_driver_subsystem);
if (!streq_ptr(driver_subsystem, new_driver_subsystem))
return 0;
const char *new_sysname;
r = sd_device_get_sysname(new_device, &new_sysname);
if (r < 0)
return r;
if (!streq(sysname, new_sysname))
return 0;
/* If this is the first device we found, then take it. */
if (!*device) {
*device = TAKE_PTR(new_device);
return 1;
}
/* Unfortunately, (subsystem, sysname) pair is not unique. For examples,
* - /sys/bus/gpio and /sys/class/gpio, both have gpiochip%N. However, these point to different devpaths.
* - /sys/bus/mdio_bus and /sys/class/mdio_bus,
* - /sys/bus/mei and /sys/class/mei,
* - /sys/bus/typec and /sys/class/typec, and so on.
* Hence, if we already know a device, then we need to check if it is equivalent to the newly found one. */
const char *devpath, *new_devpath;
r = sd_device_get_devpath(*device, &devpath);
if (r < 0)
return r;
r = sd_device_get_devpath(new_device, &new_devpath);
if (r < 0)
return r;
if (!streq(devpath, new_devpath))
return log_debug_errno(SYNTHETIC_ERRNO(ETOOMANYREFS),
"sd-device: found multiple devices for subsystem=%s and sysname=%s, refusing: %s, %s",
subsystem, sysname, devpath, new_devpath);
return 1; /* Fortunately, they are consistent. */
}
_public_ int sd_device_new_from_subsystem_sysname(
sd_device **ret,
const char *subsystem,
const char *sysname) {
_cleanup_(sd_device_unrefp) sd_device *device = NULL;
char *name;
int r;
assert_return(ret, -EINVAL);
assert_return(subsystem, -EINVAL);
assert_return(sysname, -EINVAL);
if (!path_is_normalized(subsystem))
return -EINVAL;
if (!path_is_normalized(sysname))
return -EINVAL;
/* translate sysname back to sysfs filename */
name = strdupa_safe(sysname);
string_replace_char(name, '/', '!');
if (streq(subsystem, "subsystem")) {
FOREACH_STRING(s, "/sys/bus/", "/sys/class/") {
r = device_new_from_path_join(&device, subsystem, /* driver_subsystem= */ NULL, sysname, s, name, NULL, NULL);
if (r < 0)
return r;
}
} else if (streq(subsystem, "module")) {
r = device_new_from_path_join(&device, subsystem, /* driver_subsystem= */ NULL, sysname, "/sys/module/", name, NULL, NULL);
if (r < 0)
return r;
} else if (streq(subsystem, "drivers")) {
const char *sep;
sep = strchr(name, ':');
if (sep && sep[1] != '\0') { /* Require ":" and something non-empty after that. */
const char *subsys = memdupa_suffix0(name, sep - name);
sep++;
if (streq(sep, "drivers")) /* If the sysname is "drivers", then it's the drivers directory itself that is meant. */
r = device_new_from_path_join(&device, subsystem, subsys, "drivers", "/sys/bus/", subsys, "/drivers", NULL);
else
r = device_new_from_path_join(&device, subsystem, subsys, sysname + (sep - name), "/sys/bus/", subsys, "/drivers/", sep);
if (r < 0)
return r;
}
}
r = device_new_from_path_join(&device, subsystem, /* driver_subsystem= */ NULL, sysname, "/sys/bus/", subsystem, "/devices/", name);
if (r < 0)
return r;
r = device_new_from_path_join(&device, subsystem, /* driver_subsystem= */ NULL, sysname, "/sys/class/", subsystem, name, NULL);
if (r < 0)
return r;
/* Note that devices under /sys/firmware/ (e.g. /sys/firmware/devicetree/base/) do not have
* subsystem. Hence, pass NULL for subsystem. See issue #35861. */
r = device_new_from_path_join(&device, /* subsystem= */ NULL, /* driver_subsystem= */ NULL, sysname, "/sys/firmware/", subsystem, name, NULL);
if (r < 0)
return r;
if (!device)
return -ENODEV;
*ret = TAKE_PTR(device);
return 0;
}
_public_ int sd_device_new_from_stat_rdev(sd_device **ret, const struct stat *st) {
assert_return(ret, -EINVAL);
assert_return(st, -EINVAL);
return device_new_from_mode_and_devnum(ret, st->st_mode, st->st_rdev);
}
static int device_new_from_devname(sd_device **ret, const char *devname, bool strict) {
int r;
assert_return(ret, -EINVAL);
assert_return(devname, -EINVAL);
/* This function actually accepts both devlinks and devnames, i.e. both symlinks and device
* nodes below /dev/. */
if (strict && isempty(path_startswith(devname, "/dev/")))
return -EINVAL;
dev_t devnum;
mode_t mode;
if (device_path_parse_major_minor(devname, &mode, &devnum) >= 0)
/* Let's shortcut when "/dev/block/maj:min" or "/dev/char/maj:min" is specified.
* In that case, we can directly convert the path to syspath, hence it is not necessary
* that the specified path exists. So, this works fine without udevd being running. */
return device_new_from_mode_and_devnum(ret, mode, devnum);
_cleanup_free_ char *resolved = NULL;
struct stat st;
r = chase_and_stat(devname, /* root= */ NULL, /* chase_flags= */ 0, &resolved, &st);
if (ERRNO_IS_NEG_DEVICE_ABSENT(r))
return -ENODEV;
if (r < 0)
return r;
if (isempty(path_startswith(resolved, "/dev/")))
return -EINVAL;
return sd_device_new_from_stat_rdev(ret, &st);
}
_public_ int sd_device_new_from_devname(sd_device **ret, const char *devname) {
return device_new_from_devname(ret, devname, /* strict= */ true);
}
_public_ int sd_device_new_from_path(sd_device **ret, const char *path) {
assert_return(ret, -EINVAL);
assert_return(path, -EINVAL);
if (device_new_from_devname(ret, path, /* strict= */ false) >= 0)
return 0;
return device_new_from_syspath(ret, path, /* strict= */ false);
}
int device_set_devtype(sd_device *device, const char *devtype) {
_cleanup_free_ char *t = NULL;
int r;
assert(device);
assert(devtype);
t = strdup(devtype);
if (!t)
return -ENOMEM;
r = device_add_property_internal(device, "DEVTYPE", t);
if (r < 0)
return r;
return free_and_replace(device->devtype, t);
}
int device_set_ifindex(sd_device *device, const char *name) {
int r, ifindex;
assert(device);
assert(name);
ifindex = parse_ifindex(name);
if (ifindex < 0)
return ifindex;
r = device_add_property_internal(device, "IFINDEX", name);
if (r < 0)
return r;
device->ifindex = ifindex;
return 0;
}
static int mangle_devname(const char *p, char **ret) {
char *q;
assert(p);
assert(ret);
if (!path_is_safe(p))
return -EINVAL;
/* When the path is absolute, it must start with "/dev/", but ignore "/dev/" itself. */
if (path_is_absolute(p)) {
if (isempty(path_startswith(p, "/dev/")))
return -EINVAL;
q = strdup(p);
} else
q = path_join("/dev/", p);
if (!q)
return -ENOMEM;
path_simplify(q);
*ret = q;
return 0;
}
int device_set_devname(sd_device *device, const char *devname) {
_cleanup_free_ char *t = NULL;
int r;
assert(device);
assert(devname);
r = mangle_devname(devname, &t);
if (r < 0)
return r;
r = device_add_property_internal(device, "DEVNAME", t);
if (r < 0)
return r;
return free_and_replace(device->devname, t);
}
int device_set_devmode(sd_device *device, const char *_devmode) {
unsigned devmode;
int r;
assert(device);
assert(_devmode);
r = safe_atou(_devmode, &devmode);
if (r < 0)
return r;
if (devmode > 07777)
return -EINVAL;
r = device_add_property_internal(device, "DEVMODE", _devmode);
if (r < 0)
return r;
device->devmode = devmode;
return 0;
}
int device_set_devnum(sd_device *device, const char *major, const char *minor) {
unsigned maj, min = 0;
int r;
assert(device);
assert(major);
r = safe_atou(major, &maj);
if (r < 0)
return r;
if (maj == 0)
return 0;
if (!DEVICE_MAJOR_VALID(maj))
return -EINVAL;
if (minor) {
r = safe_atou(minor, &min);
if (r < 0)
return r;
if (!DEVICE_MINOR_VALID(min))
return -EINVAL;
}
r = device_add_property_internal(device, "MAJOR", major);
if (r < 0)
return r;
if (minor) {
r = device_add_property_internal(device, "MINOR", minor);
if (r < 0)
return r;
}
device->devnum = makedev(maj, min);
return 0;
}
int device_set_diskseq(sd_device *device, const char *str) {
uint64_t diskseq;
int r;
assert(device);
assert(str);
r = safe_atou64(str, &diskseq);
if (r < 0)
return r;
if (diskseq == 0)
return -EINVAL;
r = device_add_property_internal(device, "DISKSEQ", str);
if (r < 0)
return r;
device->diskseq = diskseq;
return 0;
}
static int handle_uevent_line(
sd_device *device,
const char *key,
const char *value,
const char **major,
const char **minor) {
assert(device);
assert(key);
assert(value);
assert(major);
assert(minor);
if (streq(key, "SUBSYSTEM"))
return device_set_subsystem(device, value);
if (streq(key, "DEVTYPE"))
return device_set_devtype(device, value);
if (streq(key, "IFINDEX"))
return device_set_ifindex(device, value);
if (streq(key, "DEVNAME"))
return device_set_devname(device, value);
if (streq(key, "DEVUID"))
return device_set_devuid(device, value);
if (streq(key, "DEVGID"))
return device_set_devgid(device, value);
if (streq(key, "DEVMODE"))
return device_set_devmode(device, value);
if (streq(key, "DISKSEQ"))
return device_set_diskseq(device, value);
if (streq(key, "DRIVER"))
return device_set_driver(device, value);
if (streq(key, "MAJOR"))
*major = value;
else if (streq(key, "MINOR"))
*minor = value;
else
return device_add_property_internal(device, key, value);
return 0;
}
int device_read_uevent_file(sd_device *device) {
int r;
assert(device);
if (device->uevent_loaded || device->sealed)
return 0;
device->uevent_loaded = true;
const char *uevent;
r = sd_device_get_sysattr_value(device, "uevent", &uevent);
if (ERRNO_IS_NEG_PRIVILEGE(r) || ERRNO_IS_NEG_DEVICE_ABSENT(r))
/* The uevent files may be write-only, the device may be already removed, or the device
* may not have the uevent file. */
return 0;
if (r < 0)
return log_device_debug_errno(device, r, "sd-device: Failed to read uevent file: %m");
_cleanup_strv_free_ char **v = NULL;
r = strv_split_newlines_full(&v, uevent, EXTRACT_RETAIN_ESCAPE);
if (r < 0)
return log_device_debug_errno(device, r, "sd-device: Failed to parse uevent file: %m");
const char *major = NULL, *minor = NULL;
STRV_FOREACH(s, v) {
char *eq = strchr(*s, '=');
if (!eq) {
_cleanup_free_ char *escaped = cescape(*s);
log_device_debug(device, "sd-device: Invalid uevent line, ignoring: %s", strnull(escaped));
continue;
}
*eq = '\0';
r = handle_uevent_line(device, *s, eq + 1, &major, &minor);
if (r < 0) {
_cleanup_free_ char *escaped_key = cescape(*s), *escaped_value = cescape(eq + 1);
log_device_debug_errno(device, r,
"sd-device: Failed to handle uevent entry '%s=%s', ignoring: %m",
strnull(escaped_key), strnull(escaped_value));
}
}
if (major) {
r = device_set_devnum(device, major, minor);
if (r < 0) {
_cleanup_free_ char *escaped_major = cescape(major), *escaped_minor = minor ? cescape(minor) : NULL;
log_device_debug_errno(device, r,
"sd-device: Failed to set 'MAJOR=%s' and/or 'MINOR=%s' from uevent, ignoring: %m",
strnull(escaped_major), minor ? strnull(escaped_minor) : "n/a");
}
}
r = device_in_subsystem(device, "drivers");
if (r < 0)
log_device_debug_errno(device, r, "Failed to check if the device is a driver, ignoring: %m");
if (r > 0) {
r = device_set_drivers_subsystem(device);
if (r < 0)
log_device_debug_errno(device, r,
"sd-device: Failed to set driver subsystem, ignoring: %m");
}
return 0;
}
_public_ int sd_device_get_ifindex(sd_device *device, int *ret) {
int r;
assert_return(device, -EINVAL);
r = device_read_uevent_file(device);
if (r < 0)
return r;
if (device->ifindex <= 0)
return -ENOENT;
if (ret)
*ret = device->ifindex;
return 0;
}
int device_get_ifname(sd_device *device, const char **ret) {
int r;
assert_return(device, -EINVAL);
/* First, check if the device is a network interface. */
r = sd_device_get_ifindex(device, NULL);
if (r < 0)
return r;
/* The sysname and ifname may be different, as '!' in sysname are replaced with '/'.
* For network interfaces, we can use INTERFACE property. */
return sd_device_get_property_value(device, "INTERFACE", ret);
}
_public_ int sd_device_new_from_device_id(sd_device **ret, const char *id) {
int r;
assert_return(ret, -EINVAL);
assert_return(id, -EINVAL);
switch (id[0]) {
case 'b':
case 'c': {
dev_t devt;
if (isempty(id))
return -EINVAL;
r = parse_devnum(id + 1, &devt);
if (r < 0)
return r;
return sd_device_new_from_devnum(ret, id[0], devt);
}
case 'n': {
int ifindex;
ifindex = parse_ifindex(id + 1);
if (ifindex < 0)
return ifindex;
return sd_device_new_from_ifindex(ret, ifindex);
}
case '+': {
const char *sep = strchr(id + 1, ':');
if (!sep || sep[1] == '\0')
return -EINVAL;
_cleanup_free_ char *subsystem = strndup(id + 1, sep - id - 1);
if (!subsystem)
return -ENOMEM;
_cleanup_free_ char *sysname = strdup(sep + 1);
if (!sysname)
return -ENOMEM;
/* Device ID uses device directory name as is, hence may contain '!', but
* sd_device_new_from_subsystem_sysname() expects that the input is sysname,
* that is, '!' must be replaced with '/'. */
string_replace_char(sysname, '!', '/');
return sd_device_new_from_subsystem_sysname(ret, subsystem, sysname);
}
default:
return -EINVAL;
}
}
_public_ int sd_device_get_syspath(sd_device *device, const char **ret) {
assert_return(device, -EINVAL);
assert(path_startswith(device->syspath, "/sys/"));
if (ret)
*ret = device->syspath;
return 0;
}
DEFINE_PRIVATE_HASH_OPS_FULL(
device_by_path_hash_ops,
char, path_hash_func, path_compare, free,
sd_device, sd_device_unref);