mountpoint-util: introduce path_is_mount_point_full

This commit is contained in:
Mike Yuan
2024-02-05 04:17:32 +08:00
parent 561d879305
commit b409aacb0a
29 changed files with 97 additions and 94 deletions

View File

@@ -329,34 +329,33 @@ fallback_fstat:
}
/* flags can be AT_SYMLINK_FOLLOW or 0 */
int path_is_mount_point(const char *t, const char *root, int flags) {
int path_is_mount_point_full(const char *path, const char *root, int flags) {
_cleanup_free_ char *canonical = NULL;
_cleanup_close_ int fd = -EBADF;
int r;
assert(t);
assert(path);
assert((flags & ~AT_SYMLINK_FOLLOW) == 0);
if (path_equal(t, "/"))
if (path_equal(path, "/"))
return 1;
/* we need to resolve symlinks manually, we can't just rely on
* fd_is_mount_point() to do that for us; if we have a structure like
* /bin -> /usr/bin/ and /usr is a mount point, then the parent that we
/* we need to resolve symlinks manually, we can't just rely on fd_is_mount_point() to do that for us;
* if we have a structure like /bin -> /usr/bin/ and /usr is a mount point, then the parent that we
* look at needs to be /usr, not /. */
if (flags & AT_SYMLINK_FOLLOW) {
r = chase(t, root, CHASE_TRAIL_SLASH, &canonical, NULL);
if (FLAGS_SET(flags, AT_SYMLINK_FOLLOW)) {
r = chase(path, root, CHASE_TRAIL_SLASH, &canonical, NULL);
if (r < 0)
return r;
t = canonical;
path = canonical;
}
fd = open_parent(t, O_PATH|O_CLOEXEC, 0);
fd = open_parent(path, O_PATH|O_CLOEXEC, 0);
if (fd < 0)
return fd;
return fd_is_mount_point(fd, last_path_component(t), flags);
return fd_is_mount_point(fd, last_path_component(path), flags);
}
int path_get_mnt_id_at_fallback(int dir_fd, const char *path, int *ret) {

View File

@@ -3,6 +3,7 @@
#include <fcntl.h>
#include <stdbool.h>
#include <stddef.h>
#include <sys/types.h>
/* The limit used for /dev itself. 4MB should be enough since device nodes and symlinks don't
@@ -44,7 +45,10 @@ static inline int path_get_mnt_id(const char *path, int *ret) {
}
int fd_is_mount_point(int fd, const char *filename, int flags);
int path_is_mount_point(const char *path, const char *root, int flags);
int path_is_mount_point_full(const char *path, const char *root, int flags);
static inline int path_is_mount_point(const char *path) {
return path_is_mount_point_full(path, NULL, 0);
}
bool fstype_is_network(const char *fstype);
bool fstype_needs_quota(const char *fstype);

View File

@@ -821,7 +821,7 @@ static int automount_start(Unit *u) {
assert(a);
assert(IN_SET(a->state, AUTOMOUNT_DEAD, AUTOMOUNT_FAILED));
if (path_is_mount_point(a->where, NULL, 0) > 0)
if (path_is_mount_point(a->where) > 0)
return log_unit_error_errno(u, SYNTHETIC_ERRNO(EEXIST), "Path %s is already a mount point, refusing start.", a->where);
r = unit_test_trigger_loaded(u);

View File

@@ -777,7 +777,7 @@ static int setup_credentials_internal(
assert(workspace);
if (reuse_workspace) {
r = path_is_mount_point(workspace, NULL, 0);
r = path_is_mount_point(workspace);
if (r < 0)
return r;
if (r > 0)
@@ -788,7 +788,7 @@ static int setup_credentials_internal(
} else
workspace_mounted = -1; /* ditto */
r = path_is_mount_point(final, NULL, 0);
r = path_is_mount_point(final);
if (r < 0)
return r;
if (r > 0) {

View File

@@ -80,7 +80,7 @@ static int acquire_credential_directory(ImportCredentialContext *c, const char *
if (c->target_dir_fd >= 0)
return c->target_dir_fd;
r = path_is_mount_point(path, NULL, 0);
r = path_is_mount_point(path);
if (r < 0) {
if (r != -ENOENT)
return log_error_errno(r, "Failed to determine if %s is a mount point: %m", path);

View File

@@ -1117,7 +1117,7 @@ static int mount_bind_dev(const MountEntry *m) {
(void) mkdir_p_label(mount_entry_path(m), 0755);
r = path_is_mount_point(mount_entry_path(m), NULL, 0);
r = path_is_mount_point(mount_entry_path(m));
if (r < 0)
return log_debug_errno(r, "Unable to determine whether /dev is already mounted: %m");
if (r > 0) /* make this a NOP if /dev is already a mount point */
@@ -1137,7 +1137,7 @@ static int mount_bind_sysfs(const MountEntry *m) {
(void) mkdir_p_label(mount_entry_path(m), 0755);
r = path_is_mount_point(mount_entry_path(m), NULL, 0);
r = path_is_mount_point(mount_entry_path(m));
if (r < 0)
return log_debug_errno(r, "Unable to determine whether /sys is already mounted: %m");
if (r > 0) /* make this a NOP if /sys is already a mount point */
@@ -1184,7 +1184,7 @@ static int mount_private_apivfs(
/* When we do not have enough privileges to mount a new instance, fall back to use an
* existing mount. */
r = path_is_mount_point(entry_path, /* root = */ NULL, /* flags = */ 0);
r = path_is_mount_point(entry_path);
if (r < 0)
return log_debug_errno(r, "Unable to determine whether '%s' is already mounted: %m", entry_path);
if (r > 0)
@@ -1299,7 +1299,7 @@ static int mount_run(const MountEntry *m) {
assert(m);
r = path_is_mount_point(mount_entry_path(m), NULL, 0);
r = path_is_mount_point(mount_entry_path(m));
if (r < 0 && r != -ENOENT)
return log_debug_errno(r, "Unable to determine whether /run is already mounted: %m");
if (r > 0) /* make this a NOP if /run is already a mount point */
@@ -1533,7 +1533,7 @@ static int apply_one_mount(
case MOUNT_READ_WRITE_IMPLICIT:
case MOUNT_EXEC:
case MOUNT_NOEXEC:
r = path_is_mount_point(mount_entry_path(m), root_directory, 0);
r = path_is_mount_point_full(mount_entry_path(m), root_directory, /* flags = */ 0);
if (r == -ENOENT && m->ignore)
return 0;
if (r < 0)
@@ -2537,7 +2537,7 @@ int setup_namespace(const NamespaceParameters *p, char **error_path) {
} else if (p->root_directory) {
/* A root directory is specified. Turn its directory into bind mount, if it isn't one yet. */
r = path_is_mount_point(root, NULL, AT_SYMLINK_FOLLOW);
r = path_is_mount_point_full(root, /* root = */ NULL, AT_SYMLINK_FOLLOW);
if (r < 0)
return log_debug_errno(r, "Failed to detect that %s is a mount point or not: %m", root);
if (r == 0) {

View File

@@ -280,7 +280,7 @@ static int path_is_busy(const char *where) {
assert(where);
/* already a mountpoint; generators run during reload */
r = path_is_mount_point(where, NULL, AT_SYMLINK_FOLLOW);
r = path_is_mount_point_full(where, /* root = */ NULL, AT_SYMLINK_FOLLOW);
if (r > 0)
return false;
/* The directory will be created by the mount or automount unit when it is started. */

View File

@@ -428,7 +428,7 @@ int user_record_test_home_directory(UserRecord *h) {
if (r == 0)
return -ENOTDIR;
r = path_is_mount_point(hd, NULL, 0);
r = path_is_mount_point(hd);
if (r < 0)
return r;
if (r > 0)

View File

@@ -82,7 +82,7 @@ TEST(device_is_devtype) {
}
static int intro(void) {
if (path_is_mount_point("/sys", NULL, 0) <= 0)
if (path_is_mount_point("/sys") <= 0)
return log_tests_skipped("/sys is not mounted");
return EXIT_SUCCESS;

View File

@@ -299,7 +299,7 @@ int main(int argc, char *argv[]) {
if (getuid() != 0)
return log_tests_skipped("not root");
if (path_is_mount_point("/sys", NULL, 0) <= 0)
if (path_is_mount_point("/sys") <= 0)
return log_tests_skipped("/sys is not mounted");
if (path_is_read_only_fs("/sys") > 0)

View File

@@ -677,7 +677,7 @@ TEST(devname_from_devnum) {
}
static int intro(void) {
if (path_is_mount_point("/sys", NULL, 0) <= 0)
if (path_is_mount_point("/sys") <= 0)
return log_tests_skipped("/sys is not mounted");
return EXIT_SUCCESS;

View File

@@ -67,7 +67,7 @@ static int user_mkdir_runtime_path(
if (r < 0)
return log_error_errno(r, "Failed to create /run/user: %m");
if (path_is_mount_point(runtime_path, NULL, 0) > 0)
if (path_is_mount_point(runtime_path) > 0)
log_debug("%s is already a mount point", runtime_path);
else {
char options[sizeof("mode=0700,uid=,gid=,size=,nr_inodes=,smackfsroot=*")

View File

@@ -265,7 +265,7 @@ static int mount_legacy_cgroup_hierarchy(
to = strjoina(strempty(dest), "/sys/fs/cgroup/", hierarchy);
r = path_is_mount_point(to, dest, 0);
r = path_is_mount_point_full(to, dest, /* flags = */ 0);
if (r < 0 && r != -ENOENT)
return log_error_errno(r, "Failed to determine if %s is mounted already: %m", to);
if (r > 0)
@@ -317,7 +317,7 @@ static int mount_legacy_cgns_supported(
(void) mkdir_p(cgroup_root, 0755);
/* Mount a tmpfs to /sys/fs/cgroup if it's not mounted there yet. */
r = path_is_mount_point(cgroup_root, dest, AT_SYMLINK_FOLLOW);
r = path_is_mount_point_full(cgroup_root, dest, AT_SYMLINK_FOLLOW);
if (r < 0)
return log_error_errno(r, "Failed to determine if /sys/fs/cgroup is already mounted: %m");
if (r == 0) {
@@ -427,7 +427,7 @@ static int mount_legacy_cgns_unsupported(
(void) mkdir_p(cgroup_root, 0755);
/* Mount a tmpfs to /sys/fs/cgroup if it's not mounted there yet. */
r = path_is_mount_point(cgroup_root, dest, AT_SYMLINK_FOLLOW);
r = path_is_mount_point_full(cgroup_root, dest, AT_SYMLINK_FOLLOW);
if (r < 0)
return log_error_errno(r, "Failed to determine if /sys/fs/cgroup is already mounted: %m");
if (r == 0) {
@@ -529,7 +529,7 @@ static int mount_unified_cgroups(const char *dest) {
(void) mkdir_p(p, 0755);
r = path_is_mount_point(p, dest, AT_SYMLINK_FOLLOW);
r = path_is_mount_point_full(p, dest, AT_SYMLINK_FOLLOW);
if (r < 0)
return log_error_errno(r, "Failed to determine if %s is mounted already: %m", p);
if (r > 0) {

View File

@@ -642,7 +642,7 @@ int mount_all(const char *dest,
/* Skip this entry if it is not a remount. */
if (mount_table[k].what) {
r = path_is_mount_point(where, NULL, 0);
r = path_is_mount_point(where);
if (r < 0 && r != -ENOENT)
return log_error_errno(r, "Failed to detect whether %s is a mount point: %m", where);
if (r > 0)

View File

@@ -2560,7 +2560,7 @@ static int setup_journal(const char *directory) {
p = strjoina("/var/log/journal/", SD_ID128_TO_STRING(arg_uuid));
q = prefix_roota(directory, p);
if (path_is_mount_point(p, NULL, 0) > 0) {
if (path_is_mount_point(p) > 0) {
if (try)
return 0;
@@ -2568,7 +2568,7 @@ static int setup_journal(const char *directory) {
"%s: already a mount point, refusing to use for journal", p);
}
if (path_is_mount_point(q, NULL, 0) > 0) {
if (path_is_mount_point(q) > 0) {
if (try)
return 0;
@@ -3633,7 +3633,7 @@ static int setup_unix_export_dir_outside(char **ret) {
if (!p)
return log_oom();
r = path_is_mount_point(p, /* root= */ NULL, 0);
r = path_is_mount_point(p);
if (r > 0)
return log_error_errno(SYNTHETIC_ERRNO(EEXIST), "Mount point '%s' exists already, refusing.", p);
if (r < 0 && r != -ENOENT)
@@ -5680,7 +5680,7 @@ static int run(int argc, char *argv[]) {
/* If the specified path is a mount point we generate the new snapshot immediately
* inside it under a random name. However if the specified is not a mount point we
* create the new snapshot in the parent directory, just next to it. */
r = path_is_mount_point(arg_directory, NULL, 0);
r = path_is_mount_point(arg_directory);
if (r < 0) {
log_error_errno(r, "Failed to determine whether directory %s is mount point: %m", arg_directory);
goto finish;

View File

@@ -224,7 +224,7 @@ static int run(int argc, char *argv[]) {
if (r <= 0)
return r;
r = path_is_mount_point(arg_target, NULL, 0);
r = path_is_mount_point(arg_target);
if (r < 0)
return log_error_errno(r, "Failed to check if \"%s\" is a mount point: %m", arg_target);
if (r == 0)

View File

@@ -5828,7 +5828,7 @@ static int find_backing_devno(
if (r < 0)
return r;
r = path_is_mount_point(resolved, NULL, 0);
r = path_is_mount_point(resolved);
if (r < 0)
return r;
if (r == 0) /* Not a mount point, then it's not a partition of its own, let's not automatically use it. */
@@ -7037,7 +7037,7 @@ static int parse_argv(int argc, char *argv[]) {
/* By default operate on /sysusr/ or /sysroot/ when invoked in the initrd. We prefer the
* former, if it is mounted, so that we have deterministic behaviour on systems where /usr/
* is vendor-supplied but the root fs formatted on first boot. */
r = path_is_mount_point("/sysusr/usr", NULL, 0);
r = path_is_mount_point("/sysusr/usr");
if (r <= 0) {
if (r < 0 && r != -ENOENT)
log_debug_errno(r, "Unable to determine whether /sysusr/usr is a mount point, assuming it is not: %m");

View File

@@ -931,7 +931,7 @@ static int condition_test_path_is_mount_point(Condition *c, char **env) {
assert(c->parameter);
assert(c->type == CONDITION_PATH_IS_MOUNT_POINT);
return path_is_mount_point(c->parameter, NULL, AT_SYMLINK_FOLLOW) > 0;
return path_is_mount_point_full(c->parameter, /* root = */ NULL, AT_SYMLINK_FOLLOW) > 0;
}
static int condition_test_path_is_read_write(Condition *c, char **env) {

View File

@@ -237,7 +237,7 @@ int machine_id_commit(const char *root) {
etc_machine_id = prefix_roota(root, "/etc/machine-id");
r = path_is_mount_point(etc_machine_id, NULL, 0);
r = path_is_mount_point(etc_machine_id);
if (r < 0)
return log_error_errno(r, "Failed to determine whether %s is a mount point: %m", etc_machine_id);
if (r == 0) {

View File

@@ -177,7 +177,7 @@ static int mount_one(const MountPoint *p, bool relabel) {
if (relabel)
(void) label_fix(p->where, LABEL_IGNORE_ENOENT|LABEL_IGNORE_EROFS);
r = path_is_mount_point(p->where, NULL, AT_SYMLINK_FOLLOW);
r = path_is_mount_point_full(p->where, /* root = */ NULL, AT_SYMLINK_FOLLOW);
if (r < 0 && r != -ENOENT) {
log_full_errno(priority, r, "Failed to determine whether %s is a mount point: %m", p->where);
return (p->mode & MNT_FATAL) ? r : 0;

Some files were not shown because too many files have changed in this diff Show More