1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-06 17:18:12 +03:00

dissect-image: introduce probe_filesystem_full() which can take file descriptor of device node

In dissect_loop_device(), we have opened the device node. Let's reuse
the file descriptor.
This commit is contained in:
Yu Watanabe 2022-10-01 11:02:40 +09:00
parent f7725647bb
commit d2c6e79d89
2 changed files with 40 additions and 12 deletions

View File

@ -74,19 +74,44 @@
/* how many times to wait for the device nodes to appear */ /* how many times to wait for the device nodes to appear */
#define N_DEVICE_NODE_LIST_ATTEMPTS 10 #define N_DEVICE_NODE_LIST_ATTEMPTS 10
int probe_filesystem(const char *node, char **ret_fstype) { int probe_filesystem_full(int fd, const char *path, char **ret_fstype) {
/* Try to find device content type and return it in *ret_fstype. If nothing is found, /* Try to find device content type and return it in *ret_fstype. If nothing is found,
* 0/NULL will be returned. -EUCLEAN will be returned for ambiguous results, and an * 0/NULL will be returned. -EUCLEAN will be returned for ambiguous results, and an
* different error otherwise. */ * different error otherwise. */
#if HAVE_BLKID #if HAVE_BLKID
_cleanup_(blkid_free_probep) blkid_probe b = NULL; _cleanup_(blkid_free_probep) blkid_probe b = NULL;
_cleanup_free_ char *path_by_fd = NULL;
_cleanup_close_ int fd_close = -1;
const char *fstype; const char *fstype;
int r; int r;
errno = 0; assert(fd >= 0 || path);
b = blkid_new_probe_from_filename(node); assert(ret_fstype);
if (fd < 0) {
fd_close = open(path, O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY);
if (fd_close < 0)
return -errno;
fd = fd_close;
}
if (!path) {
r = fd_get_path(fd, &path_by_fd);
if (r < 0)
return r;
path = path_by_fd;
}
b = blkid_new_probe();
if (!b) if (!b)
return -ENOMEM;
errno = 0;
r = blkid_probe_set_device(b, fd, 0, 0);
if (r != 0)
return errno_or_else(ENOMEM); return errno_or_else(ENOMEM);
blkid_probe_enable_superblocks(b, 1); blkid_probe_enable_superblocks(b, 1);
@ -98,16 +123,16 @@ int probe_filesystem(const char *node, char **ret_fstype) {
goto not_found; goto not_found;
if (r == -2) if (r == -2)
return log_debug_errno(SYNTHETIC_ERRNO(EUCLEAN), return log_debug_errno(SYNTHETIC_ERRNO(EUCLEAN),
"Results ambiguous for partition %s", node); "Results ambiguous for partition %s", path);
if (r != 0) if (r != 0)
return log_debug_errno(errno_or_else(EIO), "Failed to probe partition %s: %m", node); return log_debug_errno(errno_or_else(EIO), "Failed to probe partition %s: %m", path);
(void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL); (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL);
if (fstype) { if (fstype) {
char *t; char *t;
log_debug("Probed fstype '%s' on partition %s.", fstype, node); log_debug("Probed fstype '%s' on partition %s.", fstype, path);
t = strdup(fstype); t = strdup(fstype);
if (!t) if (!t)
@ -118,7 +143,7 @@ int probe_filesystem(const char *node, char **ret_fstype) {
} }
not_found: not_found:
log_debug("No type detected on partition %s", node); log_debug("No type detected on partition %s", path);
*ret_fstype = NULL; *ret_fstype = NULL;
return 0; return 0;
#else #else
@ -140,8 +165,8 @@ static int dissected_image_probe_filesystem(DissectedImage *m) {
if (!p->found) if (!p->found)
continue; continue;
if (!p->fstype && p->node) { if (!p->fstype && p->mount_node_fd >= 0 && !p->decrypted_node) {
r = probe_filesystem(p->node, &p->fstype); r = probe_filesystem_full(p->mount_node_fd, p->node, &p->fstype);
if (r < 0 && r != -EUCLEAN) if (r < 0 && r != -EUCLEAN)
return r; return r;
} }
@ -2326,8 +2351,8 @@ int dissected_image_decrypt(
return r; return r;
} }
if (!p->decrypted_fstype && p->decrypted_node) { if (!p->decrypted_fstype && p->mount_node_fd >= 0 && p->decrypted_node) {
r = probe_filesystem(p->decrypted_node, &p->decrypted_fstype); r = probe_filesystem_full(p->mount_node_fd, p->decrypted_node, &p->decrypted_fstype);
if (r < 0 && r != -EUCLEAN) if (r < 0 && r != -EUCLEAN)
return r; return r;
} }

View File

@ -267,7 +267,10 @@ MountOptions* mount_options_free_all(MountOptions *options);
DEFINE_TRIVIAL_CLEANUP_FUNC(MountOptions*, mount_options_free_all); DEFINE_TRIVIAL_CLEANUP_FUNC(MountOptions*, mount_options_free_all);
const char* mount_options_from_designator(const MountOptions *options, PartitionDesignator designator); const char* mount_options_from_designator(const MountOptions *options, PartitionDesignator designator);
int probe_filesystem(const char *node, char **ret_fstype); int probe_filesystem_full(int fd, const char *path, char **ret_fstype);
static inline int probe_filesystem(const char *path, char **ret_fstype) {
return probe_filesystem_full(-1, path, ret_fstype);
}
int dissect_image_file( int dissect_image_file(
const char *path, const char *path,
const VeritySettings *verity, const VeritySettings *verity,