| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "device-private.h" |
| 4 |
#include "device-util.h" |
| 5 |
#include "devnum-util.h" |
| 6 |
#include "fd-util.h" |
| 7 |
#include "string-util.h" |
| 8 |
#include "strv.h" |
| 9 |
|
| 10 |
int devname_from_devnum(mode_t mode, dev_t devnum, char **ret) { |
| 11 |
_cleanup_(sd_device_unrefp) sd_device *dev = NULL; |
| 12 |
const char *devname; |
| 13 |
int r; |
| 14 |
|
| 15 |
assert(ret); |
| 16 |
|
| 17 |
if (devnum_is_zero(devnum)) |
| 18 |
return device_path_make_inaccessible(mode, ret); |
| 19 |
|
| 20 |
r = device_new_from_mode_and_devnum(&dev, mode, devnum); |
| 21 |
if (r < 0) |
| 22 |
return r; |
| 23 |
|
| 24 |
r = sd_device_get_devname(dev, &devname); |
| 25 |
if (r < 0) |
| 26 |
return r; |
| 27 |
|
| 28 |
return strdup_to(ret, devname); |
| 29 |
} |
| 30 |
|
| 31 |
int devname_from_stat_rdev(const struct stat *st, char **ret) { |
| 32 |
assert(st); |
| 33 |
return devname_from_devnum(st->st_mode, st->st_rdev, ret); |
| 34 |
} |
| 35 |
|
| 36 |
int device_open_from_devnum(mode_t mode, dev_t devnum, int flags, char **ret_devname) { |
| 37 |
_cleanup_(sd_device_unrefp) sd_device *dev = NULL; |
| 38 |
_cleanup_close_ int fd = -EBADF; |
| 39 |
int r; |
| 40 |
|
| 41 |
r = device_new_from_mode_and_devnum(&dev, mode, devnum); |
| 42 |
if (r < 0) |
| 43 |
return r; |
| 44 |
|
| 45 |
fd = sd_device_open(dev, flags); |
| 46 |
if (fd < 0) |
| 47 |
return fd; |
| 48 |
|
| 49 |
if (ret_devname) { |
| 50 |
const char *devname; |
| 51 |
|
| 52 |
r = sd_device_get_devname(dev, &devname); |
| 53 |
if (r < 0) |
| 54 |
return r; |
| 55 |
|
| 56 |
r = strdup_to(ret_devname, devname); |
| 57 |
if (r < 0) |
| 58 |
return r; |
| 59 |
} |
| 60 |
|
| 61 |
return TAKE_FD(fd); |
| 62 |
} |
| 63 |
|
| 64 |
static int add_string_field( |
| 65 |
sd_device *device, |
| 66 |
const char *field, |
| 67 |
int (*func)(sd_device *dev, const char **s), |
| 68 |
char ***strv) { |
| 69 |
|
| 70 |
const char *s; |
| 71 |
int r; |
| 72 |
|
| 73 |
assert(device); |
| 74 |
assert(field); |
| 75 |
assert(func); |
| 76 |
assert(strv); |
| 77 |
|
| 78 |
r = func(device, &s); |
| 79 |
if (r < 0 && r != -ENOENT) |
| 80 |
log_device_debug_errno(device, r, "Failed to get device \"%s\" property, ignoring: %m", field); |
| 81 |
if (r >= 0) |
| 82 |
(void) strv_extend_assignment(strv, field, s); |
| 83 |
|
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
char** device_make_log_fields(sd_device *device) { |
| 88 |
_cleanup_strv_free_ char **strv = NULL; |
| 89 |
dev_t devnum; |
| 90 |
int ifindex; |
| 91 |
sd_device_action_t action; |
| 92 |
uint64_t seqnum, diskseq; |
| 93 |
int r; |
| 94 |
|
| 95 |
assert(device); |
| 96 |
|
| 97 |
(void) add_string_field(device, "SYSPATH", sd_device_get_syspath, &strv); |
| 98 |
(void) add_string_field(device, "SUBSYSTEM", sd_device_get_subsystem, &strv); |
| 99 |
(void) add_string_field(device, "DEVTYPE", sd_device_get_devtype, &strv); |
| 100 |
(void) add_string_field(device, "DRIVER", sd_device_get_driver, &strv); |
| 101 |
(void) add_string_field(device, "DEVPATH", sd_device_get_devpath, &strv); |
| 102 |
(void) add_string_field(device, "DEVNAME", sd_device_get_devname, &strv); |
| 103 |
(void) add_string_field(device, "SYSNAME", sd_device_get_sysname, &strv); |
| 104 |
(void) add_string_field(device, "SYSNUM", sd_device_get_sysnum, &strv); |
| 105 |
|
| 106 |
r = sd_device_get_devnum(device, &devnum); |
| 107 |
if (r < 0 && r != -ENOENT) |
| 108 |
log_device_debug_errno(device, r, "Failed to get device \"%s\" property, ignoring: %m", "DEVNUM"); |
| 109 |
if (r >= 0) |
| 110 |
(void) strv_extendf(&strv, "DEVNUM="DEVNUM_FORMAT_STR, DEVNUM_FORMAT_VAL(devnum)); |
| 111 |
|
| 112 |
r = sd_device_get_ifindex(device, &ifindex); |
| 113 |
if (r < 0 && r != -ENOENT) |
| 114 |
log_device_debug_errno(device, r, "Failed to get device \"%s\" property, ignoring: %m", "IFINDEX"); |
| 115 |
if (r >= 0) |
| 116 |
(void) strv_extendf(&strv, "IFINDEX=%i", ifindex); |
| 117 |
|
| 118 |
r = sd_device_get_action(device, &action); |
| 119 |
if (r < 0 && r != -ENOENT) |
| 120 |
log_device_debug_errno(device, r, "Failed to get device \"%s\" property, ignoring: %m", "ACTION"); |
| 121 |
if (r >= 0) |
| 122 |
(void) strv_extendf(&strv, "ACTION=%s", device_action_to_string(action)); |
| 123 |
|
| 124 |
r = sd_device_get_seqnum(device, &seqnum); |
| 125 |
if (r < 0 && r != -ENOENT) |
| 126 |
log_device_debug_errno(device, r, "Failed to get device \"%s\" property, ignoring: %m", "SEQNUM"); |
| 127 |
if (r >= 0) |
| 128 |
(void) strv_extendf(&strv, "SEQNUM=%"PRIu64, seqnum); |
| 129 |
|
| 130 |
r = sd_device_get_diskseq(device, &diskseq); |
| 131 |
if (r < 0 && r != -ENOENT) |
| 132 |
log_device_debug_errno(device, r, "Failed to get device \"%s\" property, ignoring: %m", "DISKSEQ"); |
| 133 |
if (r >= 0) |
| 134 |
(void) strv_extendf(&strv, "DISKSEQ=%"PRIu64, diskseq); |
| 135 |
|
| 136 |
return TAKE_PTR(strv); |
| 137 |
} |
| 138 |
|
| 139 |
int device_in_subsystem_strv(sd_device *device, char * const *subsystems) { |
| 140 |
const char *s; |
| 141 |
int r; |
| 142 |
|
| 143 |
assert(device); |
| 144 |
|
| 145 |
r = sd_device_get_subsystem(device, &s); |
| 146 |
if (r == -ENOENT) |
| 147 |
return strv_isempty(subsystems); |
| 148 |
if (r < 0) |
| 149 |
return r; |
| 150 |
return strv_contains(subsystems, s); |
| 151 |
} |
| 152 |
|
| 153 |
int device_is_devtype(sd_device *device, const char *devtype) { |
| 154 |
const char *s; |
| 155 |
int r; |
| 156 |
|
| 157 |
assert(device); |
| 158 |
|
| 159 |
r = sd_device_get_devtype(device, &s); |
| 160 |
if (r == -ENOENT) |
| 161 |
return !devtype; |
| 162 |
if (r < 0) |
| 163 |
return r; |
| 164 |
return streq_ptr(s, devtype); |
| 165 |
} |
| 166 |
|
| 167 |
int device_is_subsystem_devtype(sd_device *device, const char *subsystem, const char *devtype) { |
| 168 |
int r; |
| 169 |
|
| 170 |
assert(device); |
| 171 |
|
| 172 |
r = device_in_subsystem(device, subsystem); |
| 173 |
if (r <= 0) |
| 174 |
return r; |
| 175 |
|
| 176 |
if (!devtype) |
| 177 |
return true; |
| 178 |
|
| 179 |
return device_is_devtype(device, devtype); |
| 180 |
} |
| 181 |
|
| 182 |
int device_sysname_startswith_strv(sd_device *device, char * const *prefixes, const char **ret_suffix) { |
| 183 |
const char *sysname; |
| 184 |
int r; |
| 185 |
|
| 186 |
assert(device); |
| 187 |
|
| 188 |
r = sd_device_get_sysname(device, &sysname); |
| 189 |
if (r < 0) |
| 190 |
return r; |
| 191 |
|
| 192 |
const char *suffix = startswith_strv(sysname, prefixes); |
| 193 |
if (ret_suffix) |
| 194 |
*ret_suffix = suffix; |
| 195 |
return !!suffix; |
| 196 |
} |
| 197 |
|
| 198 |
int device_get_seat(sd_device *device, const char **ret) { |
| 199 |
const char *seat = NULL; |
| 200 |
int r; |
| 201 |
|
| 202 |
assert(device); |
| 203 |
assert(ret); |
| 204 |
|
| 205 |
r = sd_device_get_property_value(device, "ID_SEAT", &seat); |
| 206 |
if (r < 0 && r != -ENOENT) |
| 207 |
return r; |
| 208 |
|
| 209 |
*ret = isempty(seat) ? "seat0" : seat; |
| 210 |
return 0; |
| 211 |
} |
| 212 |
|
| 213 |
bool device_property_can_set(const char *property) { |
| 214 |
return property && |
| 215 |
!STR_IN_SET(property, |
| 216 |
/* basic properties set by kernel, only in netlink event */ |
| 217 |
"ACTION", "SEQNUM", "SYNTH_UUID", |
| 218 |
/* basic properties set by kernel, both in netlink event and uevent file */ |
| 219 |
"DEVPATH", "DEVPATH_OLD", "SUBSYSTEM", "DEVTYPE", "DRIVER", "MODALIAS", |
| 220 |
/* device node */ |
| 221 |
"DEVNAME", "DEVMODE", "DEVUID", "DEVGID", "MAJOR", "MINOR", |
| 222 |
/* block device */ |
| 223 |
"DISKSEQ", "PARTN", |
| 224 |
/* network interface (INTERFACE_OLD is set by udevd) */ |
| 225 |
"IFINDEX", "INTERFACE", "INTERFACE_OLD", |
| 226 |
/* basic properties set by udevd */ |
| 227 |
"DEVLINKS", "TAGS", "CURRENT_TAGS", "USEC_INITIALIZED", "UDEV_DATABASE_VERSION") && |
| 228 |
/* Similar to SYNTH_UUID, but set based on KEY=VALUE arguments passed by userspace. |
| 229 |
* See kernel's f36776fafbaa0094390dd4e7e3e29805e0b82730 (v4.13) */ |
| 230 |
!startswith(property, "SYNTH_ARG_"); |
| 231 |
} |