summaryrefslogtreecommitdiffstats
path: root/src/basic/chase.c
diff options
context:
space:
mode:
authorDaan De Meyer <daan.j.demeyer@gmail.com>2023-03-30 11:44:33 +0200
committerGitHub <noreply@github.com>2023-03-30 11:44:33 +0200
commit5ccf06074abfd53e7f580764137eb17542830928 (patch)
treea06aae06e104021c9d816f7900331b953199e0e5 /src/basic/chase.c
parentMerge pull request #27063 from yuwata/chase-graceful (diff)
parentchase: Simplify chase_and_open() and chase_and_openat() (diff)
downloadsystemd-5ccf06074abfd53e7f580764137eb17542830928.tar.xz
systemd-5ccf06074abfd53e7f580764137eb17542830928.zip
Merge pull request #27064 from DaanDeMeyer/chase-fixes
Chase fixes
Diffstat (limited to 'src/basic/chase.c')
-rw-r--r--src/basic/chase.c129
1 files changed, 25 insertions, 104 deletions
diff --git a/src/basic/chase.c b/src/basic/chase.c
index 1d6af64dc7..a037b0b9be 100644
--- a/src/basic/chase.c
+++ b/src/basic/chase.c
@@ -72,13 +72,7 @@ static int log_prohibited_symlink(int fd, ChaseFlags flags) {
strna(n1));
}
-int chaseat(
- int dir_fd,
- const char *path,
- ChaseFlags flags,
- char **ret_path,
- int *ret_fd) {
-
+int chaseat(int dir_fd, const char *path, ChaseFlags flags, char **ret_path, int *ret_fd) {
_cleanup_free_ char *buffer = NULL, *done = NULL;
_cleanup_close_ int fd = -EBADF, root_fd = -EBADF;
unsigned max_follow = CHASE_MAX; /* how many symlinks to follow before giving up and returning ELOOP */
@@ -482,13 +476,7 @@ chased_one:
return 0;
}
-int chase(
- const char *path,
- const char *original_root,
- ChaseFlags flags,
- char **ret_path,
- int *ret_fd) {
-
+int chase(const char *path, const char *original_root, ChaseFlags flags, char **ret_path, int *ret_fd) {
_cleanup_free_ char *root = NULL, *absolute = NULL, *p = NULL;
_cleanup_close_ int fd = -EBADF, pfd = -EBADF;
int r;
@@ -572,13 +560,7 @@ int chase(
return r;
}
-int chase_and_open(
- const char *path,
- const char *root,
- ChaseFlags chase_flags,
- int open_flags,
- char **ret_path) {
-
+int chase_and_open(const char *path, const char *root, ChaseFlags chase_flags, int open_flags, char **ret_path) {
_cleanup_close_ int path_fd = -EBADF;
_cleanup_free_ char *p = NULL, *fname = NULL;
mode_t mode = open_flags & O_DIRECTORY ? 0755 : 0644;
@@ -603,14 +585,13 @@ int chase_and_open(
if (isempty(q))
q = ".";
- r = path_extract_filename(q, &fname);
- if (r < 0 && r != -EADDRNOTAVAIL)
- return r;
+ if (!FLAGS_SET(chase_flags, CHASE_PARENT)) {
+ r = path_extract_filename(q, &fname);
+ if (r < 0 && r != -EADDRNOTAVAIL)
+ return r;
+ }
- if (FLAGS_SET(chase_flags, CHASE_PARENT) || r == -EADDRNOTAVAIL)
- r = fd_reopen(path_fd, open_flags);
- else
- r = xopenat(path_fd, fname, open_flags|O_NOFOLLOW, mode);
+ r = xopenat(path_fd, strempty(fname), open_flags|O_NOFOLLOW, mode);
if (r < 0)
return r;
@@ -620,13 +601,7 @@ int chase_and_open(
return r;
}
-int chase_and_opendir(
- const char *path,
- const char *root,
- ChaseFlags chase_flags,
- char **ret_path,
- DIR **ret_dir) {
-
+int chase_and_opendir(const char *path, const char *root, ChaseFlags chase_flags, char **ret_path, DIR **ret_dir) {
_cleanup_close_ int path_fd = -EBADF;
_cleanup_free_ char *p = NULL;
DIR *d;
@@ -662,13 +637,7 @@ int chase_and_opendir(
return 0;
}
-int chase_and_stat(
- const char *path,
- const char *root,
- ChaseFlags chase_flags,
- char **ret_path,
- struct stat *ret_stat) {
-
+int chase_and_stat(const char *path, const char *root, ChaseFlags chase_flags, char **ret_path, struct stat *ret_stat) {
_cleanup_close_ int path_fd = -EBADF;
_cleanup_free_ char *p = NULL;
int r;
@@ -697,13 +666,7 @@ int chase_and_stat(
return 0;
}
-int chase_and_access(
- const char *path,
- const char *root,
- ChaseFlags chase_flags,
- int access_mode,
- char **ret_path) {
-
+int chase_and_access(const char *path, const char *root, ChaseFlags chase_flags, int access_mode, char **ret_path) {
_cleanup_close_ int path_fd = -EBADF;
_cleanup_free_ char *p = NULL;
int r;
@@ -767,13 +730,7 @@ int chase_and_fopen_unlocked(
return 0;
}
-int chase_and_unlink(
- const char *path,
- const char *root,
- ChaseFlags chase_flags,
- int unlink_flags,
- char **ret_path) {
-
+int chase_and_unlink(const char *path, const char *root, ChaseFlags chase_flags, int unlink_flags, char **ret_path) {
_cleanup_free_ char *p = NULL, *fname = NULL;
_cleanup_close_ int fd = -EBADF;
int r;
@@ -810,13 +767,7 @@ int chase_and_open_parent(const char *path, const char *root, ChaseFlags chase_f
return pfd;
}
-int chase_and_openat(
- int dir_fd,
- const char *path,
- ChaseFlags chase_flags,
- int open_flags,
- char **ret_path) {
-
+int chase_and_openat(int dir_fd, const char *path, ChaseFlags chase_flags, int open_flags, char **ret_path) {
_cleanup_close_ int path_fd = -EBADF;
_cleanup_free_ char *p = NULL, *fname = NULL;
mode_t mode = open_flags & O_DIRECTORY ? 0755 : 0644;
@@ -835,14 +786,13 @@ int chase_and_openat(
if (r < 0)
return r;
- r = path_extract_filename(p, &fname);
- if (r < 0 && r != -EDESTADDRREQ)
- return r;
+ if (!FLAGS_SET(chase_flags, CHASE_PARENT)) {
+ r = path_extract_filename(p, &fname);
+ if (r < 0 && r != -EADDRNOTAVAIL)
+ return r;
+ }
- if (FLAGS_SET(chase_flags, CHASE_PARENT) || r == -EDESTADDRREQ)
- r = fd_reopen(path_fd, open_flags);
- else
- r = xopenat(path_fd, fname, open_flags|O_NOFOLLOW, mode);
+ r = xopenat(path_fd, strempty(fname), open_flags|O_NOFOLLOW, mode);
if (r < 0)
return r;
@@ -852,13 +802,7 @@ int chase_and_openat(
return r;
}
-int chase_and_opendirat(
- int dir_fd,
- const char *path,
- ChaseFlags chase_flags,
- char **ret_path,
- DIR **ret_dir) {
-
+int chase_and_opendirat(int dir_fd, const char *path, ChaseFlags chase_flags, char **ret_path, DIR **ret_dir) {
_cleanup_close_ int path_fd = -EBADF;
_cleanup_free_ char *p = NULL;
DIR *d;
@@ -894,13 +838,7 @@ int chase_and_opendirat(
return 0;
}
-int chase_and_statat(
- int dir_fd,
- const char *path,
- ChaseFlags chase_flags,
- char **ret_path,
- struct stat *ret_stat) {
-
+int chase_and_statat(int dir_fd, const char *path, ChaseFlags chase_flags, char **ret_path, struct stat *ret_stat) {
_cleanup_close_ int path_fd = -EBADF;
_cleanup_free_ char *p = NULL;
int r;
@@ -929,13 +867,7 @@ int chase_and_statat(
return 0;
}
-int chase_and_accessat(
- int dir_fd,
- const char *path,
- ChaseFlags chase_flags,
- int access_mode,
- char **ret_path) {
-
+int chase_and_accessat(int dir_fd, const char *path, ChaseFlags chase_flags, int access_mode, char **ret_path) {
_cleanup_close_ int path_fd = -EBADF;
_cleanup_free_ char *p = NULL;
int r;
@@ -999,13 +931,7 @@ int chase_and_fopenat_unlocked(
return 0;
}
-int chase_and_unlinkat(
- int dir_fd,
- const char *path,
- ChaseFlags chase_flags,
- int unlink_flags,
- char **ret_path) {
-
+int chase_and_unlinkat(int dir_fd, const char *path, ChaseFlags chase_flags, int unlink_flags, char **ret_path) {
_cleanup_free_ char *p = NULL, *fname = NULL;
_cleanup_close_ int fd = -EBADF;
int r;
@@ -1030,12 +956,7 @@ int chase_and_unlinkat(
return 0;
}
-int chase_and_open_parent_at(
- int dir_fd,
- const char *path,
- ChaseFlags chase_flags,
- char **ret_filename) {
-
+int chase_and_open_parent_at(int dir_fd, const char *path, ChaseFlags chase_flags, char **ret_filename) {
int pfd, r;
assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));