1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-31 05:47:30 +03:00

Merge pull request #9668 from poettering/open-parent

introduce open_parent() helper
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2018-07-25 12:58:45 +02:00 committed by GitHub
commit 17086a40c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 64 additions and 61 deletions

View File

@ -28,6 +28,7 @@
#include "device-nodes.h" #include "device-nodes.h"
#include "fd-util.h" #include "fd-util.h"
#include "fileio.h" #include "fileio.h"
#include "fs-util.h"
#include "io-util.h" #include "io-util.h"
#include "macro.h" #include "macro.h"
#include "missing.h" #include "missing.h"
@ -59,23 +60,6 @@ static int validate_subvolume_name(const char *name) {
return 0; return 0;
} }
static int open_parent(const char *path, int flags) {
_cleanup_free_ char *parent = NULL;
int fd;
assert(path);
parent = dirname_malloc(path);
if (!parent)
return -ENOMEM;
fd = open(parent, flags);
if (fd < 0)
return -errno;
return fd;
}
static int extract_subvolume_name(const char *path, const char **subvolume) { static int extract_subvolume_name(const char *path, const char **subvolume) {
const char *fn; const char *fn;
int r; int r;
@ -144,7 +128,7 @@ int btrfs_subvol_make(const char *path) {
if (r < 0) if (r < 0)
return r; return r;
fd = open_parent(path, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY); fd = open_parent(path, O_CLOEXEC, 0);
if (fd < 0) if (fd < 0)
return fd; return fd;
@ -1283,7 +1267,7 @@ int btrfs_subvol_remove(const char *path, BtrfsRemoveFlags flags) {
if (r < 0) if (r < 0)
return r; return r;
fd = open_parent(path, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY); fd = open_parent(path, O_CLOEXEC, 0);
if (fd < 0) if (fd < 0)
return fd; return fd;
@ -1723,7 +1707,7 @@ int btrfs_subvol_snapshot_fd(int old_fd, const char *new_path, BtrfsSnapshotFlag
if (r < 0) if (r < 0)
return r; return r;
new_fd = open_parent(new_path, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY); new_fd = open_parent(new_path, O_CLOEXEC, 0);
if (new_fd < 0) if (new_fd < 0)
return new_fd; return new_fd;

View File

@ -1225,9 +1225,13 @@ int tempfn_xxxxxx(const char *p, const char *extra, char **ret) {
const char *fn; const char *fn;
char *t; char *t;
assert(p);
assert(ret); assert(ret);
if (isempty(p))
return -EINVAL;
if (path_equal(p, "/"))
return -EINVAL;
/* /*
* Turns this: * Turns this:
* /foo/bar/waldo * /foo/bar/waldo
@ -1258,9 +1262,13 @@ int tempfn_random(const char *p, const char *extra, char **ret) {
uint64_t u; uint64_t u;
unsigned i; unsigned i;
assert(p);
assert(ret); assert(ret);
if (isempty(p))
return -EINVAL;
if (path_equal(p, "/"))
return -EINVAL;
/* /*
* Turns this: * Turns this:
* /foo/bar/waldo * /foo/bar/waldo
@ -1311,7 +1319,8 @@ int tempfn_random_child(const char *p, const char *extra, char **ret) {
r = tmp_dir(&p); r = tmp_dir(&p);
if (r < 0) if (r < 0)
return r; return r;
} } else if (isempty(p))
return -EINVAL;
extra = strempty(extra); extra = strempty(extra);
@ -1404,7 +1413,8 @@ int open_tmpfile_unlinkable(const char *directory, int flags) {
r = tmp_dir(&directory); r = tmp_dir(&directory);
if (r < 0) if (r < 0)
return r; return r;
} } else if (isempty(directory))
return -EINVAL;
/* Returns an unlinked temporary file that cannot be linked into the file system anymore */ /* Returns an unlinked temporary file that cannot be linked into the file system anymore */
@ -1439,21 +1449,13 @@ int open_tmpfile_linkable(const char *target, int flags, char **ret_path) {
* which case "ret_path" will be returned as NULL. If not possible a the tempoary path name used is returned in * which case "ret_path" will be returned as NULL. If not possible a the tempoary path name used is returned in
* "ret_path". Use link_tmpfile() below to rename the result after writing the file in full. */ * "ret_path". Use link_tmpfile() below to rename the result after writing the file in full. */
{ fd = open_parent(target, O_TMPFILE|flags, 0640);
_cleanup_free_ char *dn = NULL;
dn = dirname_malloc(target);
if (!dn)
return -ENOMEM;
fd = open(dn, O_TMPFILE|flags, 0640);
if (fd >= 0) { if (fd >= 0) {
*ret_path = NULL; *ret_path = NULL;
return fd; return fd;
} }
log_debug_errno(errno, "Failed to use O_TMPFILE on %s: %m", dn); log_debug_errno(fd, "Failed to use O_TMPFILE for %s: %m", target);
}
r = tempfn_random(target, NULL, &tmp); r = tempfn_random(target, NULL, &tmp);
if (r < 0) if (r < 0)

View File

@ -1156,7 +1156,7 @@ int unlinkat_deallocate(int fd, const char *name, int flags) {
} }
int fsync_directory_of_file(int fd) { int fsync_directory_of_file(int fd) {
_cleanup_free_ char *path = NULL, *dn = NULL; _cleanup_free_ char *path = NULL;
_cleanup_close_ int dfd = -1; _cleanup_close_ int dfd = -1;
int r; int r;
@ -1182,16 +1182,40 @@ int fsync_directory_of_file(int fd) {
if (!path_is_absolute(path)) if (!path_is_absolute(path))
return -EINVAL; return -EINVAL;
dn = dirname_malloc(path); dfd = open_parent(path, O_CLOEXEC, 0);
if (!dn)
return -ENOMEM;
dfd = open(dn, O_RDONLY|O_CLOEXEC|O_DIRECTORY);
if (dfd < 0) if (dfd < 0)
return -errno; return dfd;
if (fsync(dfd) < 0) if (fsync(dfd) < 0)
return -errno; return -errno;
return 0; return 0;
} }
int open_parent(const char *path, int flags, mode_t mode) {
_cleanup_free_ char *parent = NULL;
int fd;
if (isempty(path))
return -EINVAL;
if (path_equal(path, "/")) /* requesting the parent of the root dir is fishy, let's prohibit that */
return -EINVAL;
parent = dirname_malloc(path);
if (!parent)
return -ENOMEM;
/* Let's insist on O_DIRECTORY since the parent of a file or directory is a directory. Except if we open an
* O_TMPFILE file, because in that case we are actually create a regular file below the parent directory. */
if ((flags & O_PATH) == O_PATH)
flags |= O_DIRECTORY;
else if ((flags & O_TMPFILE) != O_TMPFILE)
flags |= O_DIRECTORY|O_RDONLY;
fd = open(parent, flags, mode);
if (fd < 0)
return -errno;
return fd;
}

View File

@ -103,3 +103,5 @@ void unlink_tempfilep(char (*p)[]);
int unlinkat_deallocate(int fd, const char *name, int flags); int unlinkat_deallocate(int fd, const char *name, int flags);
int fsync_directory_of_file(int fd); int fsync_directory_of_file(int fd);
int open_parent(const char *path, int flags, mode_t mode);

View File

@ -261,7 +261,7 @@ fallback_fstat:
/* flags can be AT_SYMLINK_FOLLOW or 0 */ /* 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(const char *t, const char *root, int flags) {
_cleanup_free_ char *canonical = NULL, *parent = NULL; _cleanup_free_ char *canonical = NULL;
_cleanup_close_ int fd = -1; _cleanup_close_ int fd = -1;
int r; int r;
@ -283,11 +283,7 @@ int path_is_mount_point(const char *t, const char *root, int flags) {
t = canonical; t = canonical;
} }
parent = dirname_malloc(t); fd = open_parent(t, O_PATH|O_CLOEXEC, 0);
if (!parent)
return -ENOMEM;
fd = openat(AT_FDCWD, parent, O_DIRECTORY|O_CLOEXEC|O_PATH);
if (fd < 0) if (fd < 0)
return -errno; return -errno;

View File

@ -16,6 +16,7 @@
#include "export-raw.h" #include "export-raw.h"
#include "fd-util.h" #include "fd-util.h"
#include "fileio.h" #include "fileio.h"
#include "fs-util.h"
#include "import-common.h" #include "import-common.h"
#include "missing.h" #include "missing.h"
#include "ratelimit.h" #include "ratelimit.h"
@ -244,13 +245,9 @@ static int raw_export_on_defer(sd_event_source *s, void *userdata) {
} }
static int reflink_snapshot(int fd, const char *path) { static int reflink_snapshot(int fd, const char *path) {
char *p, *d;
int new_fd, r; int new_fd, r;
p = strdupa(path); new_fd = open_parent(path, O_TMPFILE|O_CLOEXEC|O_RDWR, 0600);
d = dirname(p);
new_fd = open(d, O_TMPFILE|O_CLOEXEC|O_NOCTTY|O_RDWR, 0600);
if (new_fd < 0) { if (new_fd < 0) {
_cleanup_free_ char *t = NULL; _cleanup_free_ char *t = NULL;

View File

@ -1058,7 +1058,7 @@ finish:
} }
int bus_machine_method_copy(sd_bus_message *message, void *userdata, sd_bus_error *error) { int bus_machine_method_copy(sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *src, *dest, *host_path, *container_path, *host_basename, *host_dirname, *container_basename, *container_dirname; const char *src, *dest, *host_path, *container_path, *host_basename, *container_basename, *container_dirname;
_cleanup_close_pair_ int errno_pipe_fd[2] = { -1, -1 }; _cleanup_close_pair_ int errno_pipe_fd[2] = { -1, -1 };
CopyFlags copy_flags = COPY_REFLINK|COPY_MERGE; CopyFlags copy_flags = COPY_REFLINK|COPY_MERGE;
_cleanup_close_ int hostfd = -1; _cleanup_close_ int hostfd = -1;
@ -1119,16 +1119,14 @@ int bus_machine_method_copy(sd_bus_message *message, void *userdata, sd_bus_erro
} }
host_basename = basename(host_path); host_basename = basename(host_path);
t = strdupa(host_path);
host_dirname = dirname(t);
container_basename = basename(container_path); container_basename = basename(container_path);
t = strdupa(container_path); t = strdupa(container_path);
container_dirname = dirname(t); container_dirname = dirname(t);
hostfd = open(host_dirname, O_CLOEXEC|O_RDONLY|O_NOCTTY|O_DIRECTORY); hostfd = open_parent(host_path, O_CLOEXEC, 0);
if (hostfd < 0) if (hostfd < 0)
return sd_bus_error_set_errnof(error, errno, "Failed to open host directory %s: %m", host_dirname); return sd_bus_error_set_errnof(error, hostfd, "Failed to open host directory %s: %m", host_path);
if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0) if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0)
return sd_bus_error_set_errnof(error, errno, "Failed to create pipe: %m"); return sd_bus_error_set_errnof(error, errno, "Failed to create pipe: %m");