HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/libsystemd/sd-device/sd-device.c
Git blob: 5bba62e3e0d2a79e44042a3074bf19ca5ccbec3e
Lines 2001–2953 of 2,953
(void) device_read_db(device);
if (device->current_tags_iterator_generation != device->tags_generation)
return NULL;
(void) set_iterate(device->current_tags, &device->current_tags_iterator, &v);
return v;
}
_public_ const char* sd_device_get_devlink_first(sd_device *device) {
void *v;
assert_return(device, NULL);
(void) device_read_db(device);
device->devlinks_iterator_generation = device->devlinks_generation;
device->devlinks_iterator = ITERATOR_FIRST;
(void) set_iterate(device->devlinks, &device->devlinks_iterator, &v);
return v;
}
_public_ const char* sd_device_get_devlink_next(sd_device *device) {
void *v;
assert_return(device, NULL);
(void) device_read_db(device);
if (device->devlinks_iterator_generation != device->devlinks_generation)
return NULL;
(void) set_iterate(device->devlinks, &device->devlinks_iterator, &v);
return v;
}
int device_properties_prepare(sd_device *device) {
int r;
assert(device);
r = device_read_uevent_file(device);
if (r < 0)
return r;
r = device_read_db(device);
if (r < 0)
return r;
if (device->property_devlinks_outdated) {
_cleanup_free_ char *devlinks = NULL;
r = set_strjoin(device->devlinks, " ", false, &devlinks);
if (r < 0)
return r;
if (!isempty(devlinks)) {
r = device_add_property_internal(device, "DEVLINKS", devlinks);
if (r < 0)
return r;
}
device->property_devlinks_outdated = false;
}
if (device->property_tags_outdated) {
_cleanup_free_ char *tags = NULL;
r = set_strjoin(device->all_tags, ":", true, &tags);
if (r < 0)
return r;
if (!isempty(tags)) {
r = device_add_property_internal(device, "TAGS", tags);
if (r < 0)
return r;
}
tags = mfree(tags);
r = set_strjoin(device->current_tags, ":", true, &tags);
if (r < 0)
return r;
if (!isempty(tags)) {
r = device_add_property_internal(device, "CURRENT_TAGS", tags);
if (r < 0)
return r;
}
device->property_tags_outdated = false;
}
return 0;
}
_public_ const char* sd_device_get_property_first(sd_device *device, const char **_value) {
const char *key;
int r;
assert_return(device, NULL);
r = device_properties_prepare(device);
if (r < 0)
return NULL;
device->properties_iterator_generation = device->properties_generation;
device->properties_iterator = ITERATOR_FIRST;
(void) ordered_hashmap_iterate(device->properties, &device->properties_iterator, (void**)_value, (const void**)&key);
return key;
}
_public_ const char* sd_device_get_property_next(sd_device *device, const char **_value) {
const char *key;
int r;
assert_return(device, NULL);
r = device_properties_prepare(device);
if (r < 0)
return NULL;
if (device->properties_iterator_generation != device->properties_generation)
return NULL;
(void) ordered_hashmap_iterate(device->properties, &device->properties_iterator, (void**)_value, (const void**)&key);
return key;
}
static int device_sysattrs_read_all_internal(sd_device *device, const char *subdir, Set **stack) {
_cleanup_closedir_ DIR *dir = NULL;
int r;
assert(device);
assert(stack);
r = device_opendir(device, subdir, &dir);
if (r == -ENOENT && subdir)
return 0; /* Maybe, this is a child device, and is already removed. */
if (r < 0)
return r;
if (subdir) {
if (faccessat(dirfd(dir), "uevent", F_OK, 0) >= 0)
return 0; /* this is a child device, skipping */
if (errno != ENOENT) {
log_device_debug_errno(device, errno,
"sd-device: Failed to access %s/uevent, ignoring sub-directory %s: %m",
subdir, subdir);
return 0;
}
}
FOREACH_DIRENT_ALL(de, dir, return -errno) {
_cleanup_free_ char *p = NULL;
struct stat statbuf;
if (dot_or_dot_dot(de->d_name))
continue;
/* only handle symlinks, regular files, and directories */
if (!IN_SET(de->d_type, DT_LNK, DT_REG, DT_DIR))
continue;
if (subdir) {
p = path_join(subdir, de->d_name);
if (!p)
return -ENOMEM;
}
if (de->d_type == DT_DIR) {
/* push the sub-directory into the stack, and read it later. */
if (p)
r = set_ensure_consume(stack, &path_hash_ops_free, TAKE_PTR(p));
else
r = set_put_strdup_full(stack, &path_hash_ops_free, de->d_name);
if (r < 0)
return r;
continue;
}
if (fstatat(dirfd(dir), de->d_name, &statbuf, AT_SYMLINK_NOFOLLOW) < 0)
continue;
if ((statbuf.st_mode & (S_IRUSR | S_IWUSR)) == 0)
continue;
if (p)
r = set_ensure_consume(&device->sysattrs, &path_hash_ops_free, TAKE_PTR(p));
else
r = set_put_strdup_full(&device->sysattrs, &path_hash_ops_free, de->d_name);
if (r < 0)
return r;
}
return 0;
}
static int device_sysattrs_read_all(sd_device *device) {
_cleanup_set_free_ Set *stack = NULL;
int r;
assert(device);
if (device->sysattrs_read)
return 0;
r = device_sysattrs_read_all_internal(device, NULL, &stack);
if (r < 0)
return r;
for (;;) {
_cleanup_free_ char *subdir = NULL;
subdir = set_steal_first(stack);
if (!subdir)
break;
r = device_sysattrs_read_all_internal(device, subdir, &stack);
if (r < 0)
return r;
}
device->sysattrs_read = true;
return 0;
}
_public_ const char* sd_device_get_sysattr_first(sd_device *device) {
void *v;
assert_return(device, NULL);
if (!device->sysattrs_read &&
device_sysattrs_read_all(device) < 0)
return NULL;
device->sysattrs_iterator = ITERATOR_FIRST;
(void) set_iterate(device->sysattrs, &device->sysattrs_iterator, &v);
return v;
}
_public_ const char* sd_device_get_sysattr_next(sd_device *device) {
void *v;
assert_return(device, NULL);
if (!device->sysattrs_read)
return NULL;
(void) set_iterate(device->sysattrs, &device->sysattrs_iterator, &v);
return v;
}
_public_ int sd_device_has_tag(sd_device *device, const char *tag) {
assert_return(device, -EINVAL);
assert_return(tag, -EINVAL);
(void) device_read_db(device);
return set_contains(device->all_tags, tag);
}
_public_ int sd_device_has_current_tag(sd_device *device, const char *tag) {
assert_return(device, -EINVAL);
assert_return(tag, -EINVAL);
(void) device_read_db(device);
return set_contains(device->current_tags, tag);
}
_public_ int sd_device_get_property_value(sd_device *device, const char *key, const char **ret) {
const char *value;
int r;
assert_return(device, -EINVAL);
assert_return(key, -EINVAL);
r = device_properties_prepare(device);
if (r < 0)
return r;
value = ordered_hashmap_get(device->properties, key);
if (!value)
return -ENOENT;
if (ret)
*ret = value;
return 0;
}
int device_get_property_bool(sd_device *device, const char *key) {
const char *value;
int r;
assert(device);
assert(key);
r = sd_device_get_property_value(device, key, &value);
if (r < 0)
return r;
return parse_boolean(value);
}
int device_get_property_int(sd_device *device, const char *key, int *ret) {
const char *value;
int r, v;
assert(device);
assert(key);
r = sd_device_get_property_value(device, key, &value);
if (r < 0)
return r;
r = safe_atoi(value, &v);
if (r < 0)
return r;
if (ret)
*ret = v;
return 0;
}
int device_get_property_uint(sd_device *device, const char *key, unsigned *ret) {
const char *value;
int r;
assert(device);
assert(key);
r = sd_device_get_property_value(device, key, &value);
if (r < 0)
return r;
unsigned v;
r = safe_atou(value, &v);
if (r < 0)
return r;
if (ret)
*ret = v;
return 0;
}
_public_ int sd_device_get_trigger_uuid(sd_device *device, sd_id128_t *ret) {
const char *s;
sd_id128_t id;
int r;
assert_return(device, -EINVAL);
/* Retrieves the UUID attached to a uevent when triggering it from userspace via
* sd_device_trigger_with_uuid() or an equivalent interface. Returns -ENOENT if the record is not
* caused by a synthetic event and -ENODATA if it was but no UUID was specified */
r = sd_device_get_property_value(device, "SYNTH_UUID", &s);
if (r < 0)
return r;
if (streq(s, "0")) /* SYNTH_UUID=0 is set whenever a device is triggered by userspace without specifying a UUID */
return -ENODATA;
r = sd_id128_from_string(s, &id);
if (r < 0)
return r;
if (ret)
*ret = id;
return 0;
}
void device_clear_sysattr_cache(sd_device *device) {
device->sysattr_values = hashmap_free(device->sysattr_values);
}
typedef struct SysAttrCacheEntry {
char *key;
char *value;
char *value_stripped;
size_t size;
int error;
} SysAttrCacheEntry;
static SysAttrCacheEntry* sysattr_cache_entry_free(SysAttrCacheEntry *p) {
if (!p)
return NULL;
free(p->key);
free(p->value);
free(p->value_stripped);
return mfree(p);
}
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
sysattr_cache_hash_ops,
char, path_hash_func, path_compare,
SysAttrCacheEntry, sysattr_cache_entry_free);
static int device_cache_sysattr_value_full(sd_device *device, char *key, char *value, size_t size, int error, bool ignore_uevent) {
int r;
assert(device);
assert(key);
assert(value || error > 0);
/* This takes the reference of the input arguments when cached, hence the caller must not free them
* when a positive return value is returned. The input value may be NULL. This replaces an already
* existing entry. */
if (ignore_uevent && streq(last_path_component(key), "uevent"))
return 0; /* not cached */
/* Remove the old cache entry. So, we do not need to clear cache on error. */
sysattr_cache_entry_free(hashmap_remove(device->sysattr_values, key));
/* We use ENOANO as a recognizable error code when we have not read the attribute. */
if (error == ENOANO)
error = ESTALE;
_cleanup_free_ SysAttrCacheEntry *entry = new(SysAttrCacheEntry, 1);
if (!entry)
return -ENOMEM;
_cleanup_free_ char *value_stripped = NULL;
if (value) {
value_stripped = memdup_suffix0(value, size);
if (!value_stripped)
return -ENOMEM;
delete_trailing_chars(value_stripped, NEWLINE);
}
*entry = (SysAttrCacheEntry) {
.key = key,
.value = value,
.value_stripped = value_stripped,
.size = size,
.error = error,
};
r = hashmap_ensure_put(&device->sysattr_values, &sysattr_cache_hash_ops, entry->key, entry);
if (r < 0)
return r;
TAKE_PTR(entry);
TAKE_PTR(value_stripped);
return 1; /* cached */
}
int device_cache_sysattr_value(sd_device *device, char *key, char *value, int error) {
return device_cache_sysattr_value_full(device, key, value, strlen(value), error, /* ignore_uevent= */ true);
}
static int device_get_cached_sysattr_value(sd_device *device, const char *key, const char **ret_value, size_t *ret_size) {
SysAttrCacheEntry *entry;
assert(device);
assert(key);
entry = hashmap_get(device->sysattr_values, key);
if (!entry)
return -ENOANO; /* We have not read the attribute. */
if (!entry->value) {
/* We have looked up the attribute before and failed. Return the cached error code. */
assert(entry->error > 0);
return -entry->error;
}
if (ret_value)
*ret_value = ret_size ? entry->value : entry->value_stripped;
if (ret_size)
*ret_size = entry->size;
return 0;
}
int device_chase(sd_device *device, const char *path, ChaseFlags flags, char **ret_resolved, int *ret_fd) {
int r;
assert(device);
assert(path);
const char *syspath;
r = sd_device_get_syspath(device, &syspath);
if (r < 0)
return r;
/* Here, CHASE_PREFIX_ROOT is borrowed. If the flag is set or the specified path is relative, then
* the path will be prefixed with the syspath. Note, we do not pass CHASE_PREFIX_ROOT flag with
* syspath as root to chase(), but we manually concatenate the specified path with syspath before
* calling chase(). Otherwise, we cannot set/get attributes of parent or sibling devices. */
_cleanup_free_ char *prefixed = NULL;
if (FLAGS_SET(flags, CHASE_PREFIX_ROOT) || !path_is_absolute(path)) {
prefixed = path_join(syspath, path);
if (!prefixed)
return -ENOMEM;
path = prefixed;
flags &= ~CHASE_PREFIX_ROOT;
}
_cleanup_free_ char *resolved = NULL;
_cleanup_close_ int fd = -EBADF;
r = chase(path, /* root= */ NULL, CHASE_NO_AUTOFS | flags, &resolved, ret_fd ? &fd : NULL);
if (r < 0)
return r;
/* Refuse to reading/writing files outside of sysfs. */
if (!path_startswith(resolved, "/sys/"))
return -EINVAL;
if (ret_resolved) {
/* Always return relative path. */
r = path_make_relative(syspath, resolved, ret_resolved);
if (r < 0)
return r;
}
if (ret_fd)
*ret_fd = TAKE_FD(fd);
return 0;
}
_public_ int sd_device_get_sysattr_value_with_size(sd_device *device, const char *sysattr, const char **ret_value, size_t *ret_size) {
_cleanup_free_ char *resolved = NULL, *value = NULL;
_cleanup_close_ int fd = -EBADF;
size_t size = 0;
int r;
assert_return(device, -EINVAL);
assert_return(sysattr, -EINVAL);
/* Look for possibly already cached result. */
r = device_get_cached_sysattr_value(device, sysattr, ret_value, ret_size);
if (r != -ENOANO)
return r;
/* Special cases: read the symlink and return the last component of the value. Some core links return
* only the last element of the target path, these are just values, the paths should not be exposed. */
if (STR_IN_SET(sysattr, "driver", "subsystem", "module")) {
_cleanup_free_ char *prefixed = NULL;
const char *syspath;
r = sd_device_get_syspath(device, &syspath);
if (r < 0)
return r;
prefixed = path_join(syspath, sysattr);
if (!prefixed)
return -ENOMEM;
r = readlink_value(prefixed, &value);
if (r >= 0)
size = strlen(value);
if (r != -EINVAL) /* -EINVAL means the path is not a symlink. */
goto cache_result;
}
r = device_chase(device, sysattr, CHASE_PREFIX_ROOT, &resolved, &fd);
if (r < 0)
goto cache_result;
/* Look for cached result again with the resolved path. */
r = device_get_cached_sysattr_value(device, resolved, ret_value, ret_size);
if (r != -ENOANO)
return r;
/* Read attribute value, Some attributes contain embedded '\0'. So, it is necessary to also get the
* size of the result. See issue #20025. */
r = read_virtual_file_fd(fd, SIZE_MAX, &value, &size);
if (r < 0)
goto cache_result;
r = 0;
cache_result:
if (r == -ENOMEM)
return r; /* Do not cache -ENOMEM, as the failure may be transient. */
if (!resolved) {
/* If we have not or could not chase the path, assume 'sysattr' is normalized. */
resolved = strdup(sysattr);
if (!resolved)
return RET_GATHER(r, -ENOMEM);
}
int k = device_cache_sysattr_value_full(device, resolved, value, size, -r, /* ignore_uevent= */ false);
if (k < 0) {
if (r < 0)
log_device_debug_errno(device, k,
"sd-device: failed to cache error code (%i) in reading attribute '%s', ignoring: %m",
-r, resolved);
else {
/* Unfortunately, we need to return 'const char*' instead of 'char*'. Hence, failure in caching
* sysattr value is critical unlike the other places. */
log_device_debug_errno(device, k,
"sd-device: failed to cache attribute '%s' with '%s'%s: %m",
resolved, value, ret_value ? "" : ", ignoring");
if (ret_value)
return k;
}
return r;
}
assert(k > 0);
/* device_cache_sysattr_value_full() takes 'resolved' and 'value' on success. */
sysattr = TAKE_PTR(resolved);
TAKE_PTR(value);
if (r < 0)
return r;
return device_get_cached_sysattr_value(device, sysattr, ret_value, ret_size);
}
_public_ int sd_device_get_sysattr_value(sd_device *device, const char *sysattr, const char **ret) {
return sd_device_get_sysattr_value_with_size(device, sysattr, ret, NULL);
}
int device_get_sysattr_streq(sd_device *device, const char *sysattr, const char *expected) {
const char *value;
int r;
assert(expected);
r = sd_device_get_sysattr_value(device, sysattr, &value);
if (r < 0)
return r;
return streq(value, expected);
}
int device_get_sysattr_safe_string(sd_device *device, const char *sysattr, const char **ret) {
const char *value;
int r;
r = sd_device_get_sysattr_value(device, sysattr, &value);
if (r < 0)
return r;
if (!string_is_safe(value,
STRING_ALLOW_EMPTY |
STRING_ALLOW_NEWLINES |
STRING_ALLOW_BACKSLASHES |
STRING_ALLOW_QUOTES |
STRING_ALLOW_GLOBS)) {
if (DEBUG_LOGGING) {
_cleanup_free_ char *escaped = cescape(value);
log_device_debug(device, "sd-device: '%s' sysattr contains invalid characters, refusing: %s",
sysattr, strnull(escaped));
}
return -ENXIO;
}
if (ret)
*ret = value;
return 0;
}
#define DEFINE_DEVICE_GET_SYSATTR_PARSE(name, type, parser) \
int device_get_sysattr_##name(sd_device *device, const char *sysattr, type *ret) { \
const char *value; \
int r; \
\
r = sd_device_get_sysattr_value(device, sysattr, &value); \
if (r < 0) \
return r; \
\
type v; \
r = parser(value, &v); \
if (r < 0) \
return log_device_debug_errno(device, r, "Failed to parse '%s' attribute: %m", sysattr); \
\
if (ret) \
*ret = v; \
/* We return "true" if the value is positive. */ \
return v > 0; \
}
#define DEFINE_DEVICE_GET_SYSATTR_PARSE_BASE(name, type, parser) \
int device_get_sysattr_##name##_full(sd_device *device, const char *sysattr, unsigned base, type *ret) { \
const char *value; \
int r; \
\
r = sd_device_get_sysattr_value(device, sysattr, &value); \
if (r < 0) \
return r; \
\
type v; \
r = parser(value, base, &v); \
if (r < 0) \
return log_device_debug_errno(device, r, "Failed to parse '%s' attribute: %m", sysattr); \
\
if (ret) \
*ret = v; \
/* We return "true" if the value is positive. */ \
return v > 0; \
}
DEFINE_DEVICE_GET_SYSATTR_PARSE(int, int, safe_atoi);
DEFINE_DEVICE_GET_SYSATTR_PARSE_BASE(unsigned, unsigned, safe_atou_full);
DEFINE_DEVICE_GET_SYSATTR_PARSE_BASE(u8, uint8_t, safe_atou8_full);
DEFINE_DEVICE_GET_SYSATTR_PARSE_BASE(u16, uint16_t, safe_atou16_full);
DEFINE_DEVICE_GET_SYSATTR_PARSE_BASE(u32, uint32_t, safe_atou32_full);
DEFINE_DEVICE_GET_SYSATTR_PARSE(u64, uint64_t, safe_atou64);
int device_get_sysattr_bool(sd_device *device, const char *sysattr) {
const char *value;
int r;
assert(device);
assert(sysattr);
r = sd_device_get_sysattr_value(device, sysattr, &value);
if (r < 0)
return r;
return parse_boolean(value);
}
static int device_remove_cached_sysattr_value(sd_device *device, const char *sysattr) {
int r;
assert(device);
assert(sysattr);
_cleanup_free_ char *resolved = NULL;
r = device_chase(device, sysattr, CHASE_PREFIX_ROOT | CHASE_NONEXISTENT, &resolved, /* ret_fd= */ NULL);
if (r < 0)
return r;
sysattr_cache_entry_free(hashmap_remove(device->sysattr_values, resolved));
return 0;
}
_public_ int sd_device_set_sysattr_value(sd_device *device, const char *sysattr, const char *value) {
int r;
assert_return(device, -EINVAL);
assert_return(sysattr, -EINVAL);
/* Set the attribute and save it in the cache. */
if (!value)
/* If input value is NULL, then clear cache and not write anything. */
return device_remove_cached_sysattr_value(device, sysattr);
_cleanup_free_ char *resolved = NULL;
_cleanup_close_ int fd = -EBADF;
r = device_chase(device, sysattr, CHASE_PREFIX_ROOT, &resolved, &fd);
if (r < 0) {
/* On failure, clear cache entry, hopefully, 'sysattr' is normalized. */
sysattr_cache_entry_free(hashmap_remove(device->sysattr_values, sysattr));
return r;
}
/* value length is limited to 4k */
_cleanup_free_ char *copied = strndup(value, 4096);
if (!copied)
return -ENOMEM;
/* drop trailing newlines */
delete_trailing_chars(copied, NEWLINE);
r = write_string_file_fd(fd, copied, WRITE_STRING_FILE_DISABLE_BUFFER | WRITE_STRING_FILE_AVOID_NEWLINE);
if (r < 0) {
/* On failure, clear cache entry, as we do not know how it fails. */
sysattr_cache_entry_free(hashmap_remove(device->sysattr_values, resolved));
return r;
}
r = device_cache_sysattr_value(device, resolved, copied, 0);
if (r < 0)
log_device_debug_errno(device, r,
"sd-device: failed to cache written attribute '%s' with '%s', ignoring: %m",
resolved, copied);
else if (r > 0) {
TAKE_PTR(resolved);
TAKE_PTR(copied);
}
return 0;
}
_public_ int sd_device_set_sysattr_valuef(sd_device *device, const char *sysattr, const char *format, ...) {
_cleanup_free_ char *value = NULL;
va_list ap;
int r;
assert_return(device, -EINVAL);
assert_return(sysattr, -EINVAL);
if (!format)
return device_remove_cached_sysattr_value(device, sysattr);
va_start(ap, format);
r = vasprintf(&value, format, ap);
va_end(ap);
if (r < 0)
return -ENOMEM;
return sd_device_set_sysattr_value(device, sysattr, value);
}
_public_ int sd_device_trigger(sd_device *device, sd_device_action_t action) {
return sd_device_trigger_with_uuid(device, action, NULL);
}
_public_ int sd_device_trigger_with_uuid(
sd_device *device,
sd_device_action_t action,
sd_id128_t *ret_uuid) {
const char *s, *j;
sd_id128_t u;
int r;
assert_return(device, -EINVAL);
s = device_action_to_string(action);
if (!s)
return -EINVAL;
r = sd_id128_randomize(&u);
if (r < 0)
return r;
j = strjoina(s, " ", SD_ID128_TO_UUID_STRING(u));
r = sd_device_set_sysattr_value(device, "uevent", j);
if (r < 0)
return r;
if (ret_uuid)
*ret_uuid = u;
return 0;
}
_public_ int sd_device_open(sd_device *device, int flags) {
_cleanup_close_ int fd = -EBADF, fd2 = -EBADF;
const char *devname;
uint64_t q, diskseq = 0;
struct stat st;
dev_t devnum;
int r;
assert_return(device, -EINVAL);
assert_return(FLAGS_SET(flags, O_PATH) || !FLAGS_SET(flags, O_NOFOLLOW), -EINVAL);
r = sd_device_get_devname(device, &devname);
if (r == -ENOENT)
return -ENOEXEC;
if (r < 0)
return r;
r = sd_device_get_devnum(device, &devnum);
if (r == -ENOENT)
return -ENOEXEC;
if (r < 0)
return r;
fd = open(devname, FLAGS_SET(flags, O_PATH) ? flags : O_CLOEXEC|O_NOFOLLOW|O_PATH);
if (fd < 0)
return -errno;
if (fstat(fd, &st) < 0)
return -errno;
if (st.st_rdev != devnum)
return -ENXIO;
r = device_in_subsystem(device, "block");
if (r < 0)
return r;
if (r > 0 ? !S_ISBLK(st.st_mode) : !S_ISCHR(st.st_mode))
return -ENXIO;
/* If flags has O_PATH, then we cannot check diskseq. Let's return earlier. */
if (FLAGS_SET(flags, O_PATH))
return TAKE_FD(fd);
/* If the device is not initialized, then we cannot determine if we should check diskseq through
* ID_IGNORE_DISKSEQ property. Let's skip to check diskseq in that case. */
r = sd_device_get_is_initialized(device);
if (r < 0)
return r;
if (r > 0) {
r = device_get_property_bool(device, "ID_IGNORE_DISKSEQ");
if (r < 0 && r != -ENOENT)
return r;
if (r <= 0) {
r = sd_device_get_diskseq(device, &diskseq);
if (r < 0 && r != -ENOENT)
return r;
}
}
fd2 = fd_reopen(fd, flags);
if (fd2 < 0)
return fd2;
if (diskseq == 0)
return TAKE_FD(fd2);
r = fd_get_diskseq(fd2, &q);
if (r < 0)
return r;
if (q != diskseq)
return -ENXIO;
return TAKE_FD(fd2);
}
int device_opendir(sd_device *device, const char *subdir, DIR **ret) {
_cleanup_closedir_ DIR *d = NULL;
_cleanup_free_ char *path = NULL;
const char *syspath;
int r;
assert(device);
assert(ret);
r = sd_device_get_syspath(device, &syspath);
if (r < 0)
return r;
if (subdir) {
if (!path_is_safe(subdir))
return -EINVAL;
path = path_join(syspath, subdir);
if (!path)
return -ENOMEM;
}
d = opendir(path ?: syspath);
if (!d)
return -errno;
*ret = TAKE_PTR(d);
return 0;
}