]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic: move chase_symlinks_and_fopen_unlocked() → chase-symlinks.[ch] 20937/head
authorLennart Poettering <lennart@poettering.net>
Tue, 5 Oct 2021 13:23:29 +0000 (15:23 +0200)
committerLuca Boccassi <luca.boccassi@microsoft.com>
Tue, 5 Oct 2021 18:37:30 +0000 (19:37 +0100)
src/basic/chase-symlinks.c
src/basic/chase-symlinks.h
src/basic/fileio.c
src/basic/fileio.h
src/nspawn/nspawn-bind-user.c
src/shared/user-record.c

index dd3a217203b0a1d9ddcf2ccd018bc3c508e60e30..344e7e1bb747b492819ed8b8880c3824d03815bd 100644 (file)
@@ -5,6 +5,7 @@
 #include "alloc-util.h"
 #include "chase-symlinks.h"
 #include "fd-util.h"
+#include "fileio.h"
 #include "fs-util.h"
 #include "glyph-util.h"
 #include "log.h"
@@ -515,3 +516,37 @@ int chase_symlinks_and_stat(
 
         return 1;
 }
+
+int chase_symlinks_and_fopen_unlocked(
+                const char *path,
+                const char *root,
+                unsigned chase_flags,
+                const char *open_flags,
+                char **ret_path,
+                FILE **ret_file) {
+
+        _cleanup_free_ char *final_path = NULL;
+        _cleanup_close_ int fd = -1;
+        int mode_flags, r;
+
+        assert(path);
+        assert(open_flags);
+        assert(ret_file);
+
+        mode_flags = fopen_mode_to_flags(open_flags);
+        if (mode_flags < 0)
+                return mode_flags;
+
+        fd = chase_symlinks_and_open(path, root, chase_flags, mode_flags, ret_path ? &final_path : NULL);
+        if (fd < 0)
+                return fd;
+
+        r = take_fdopen_unlocked(&fd, open_flags, ret_file);
+        if (r < 0)
+                return r;
+
+        if (ret_path)
+                *ret_path = TAKE_PTR(final_path);
+
+        return 0;
+}
index 7191a457687d37d80d3f4b9e5c068e870ecec7c7..e2b22fe0412ee22c4c74de859dbcaeac65ae4bf4 100644 (file)
@@ -28,3 +28,5 @@ int chase_symlinks(const char *path_with_prefix, const char *root, unsigned flag
 int chase_symlinks_and_open(const char *path, const char *root, unsigned chase_flags, int open_flags, char **ret_path);
 int chase_symlinks_and_opendir(const char *path, const char *root, unsigned chase_flags, char **ret_path, DIR **ret_dir);
 int chase_symlinks_and_stat(const char *path, const char *root, unsigned chase_flags, char **ret_path, struct stat *ret_stat, int *ret_fd);
+
+int chase_symlinks_and_fopen_unlocked(const char *path, const char *root, unsigned chase_flags, const char *open_flags, char **ret_path, FILE **ret_file);
index 4911ac7c6dc0f3e872e9455c1a4931587d5cd70a..cced1dd564b26bf02e28bf4097f5c0fdf6b96540 100644 (file)
@@ -941,10 +941,12 @@ DIR *xopendirat(int fd, const char *name, int flags) {
         return d;
 }
 
-static int mode_to_flags(const char *mode) {
+int fopen_mode_to_flags(const char *mode) {
         const char *p;
         int flags;
 
+        assert(mode);
+
         if ((p = startswith(mode, "r+")))
                 flags = O_RDWR;
         else if ((p = startswith(mode, "r")))
@@ -997,7 +999,7 @@ int xfopenat(int dir_fd, const char *path, const char *mode, int flags, FILE **r
         } else {
                 int fd, mode_flags;
 
-                mode_flags = mode_to_flags(mode);
+                mode_flags = fopen_mode_to_flags(mode);
                 if (mode_flags < 0)
                         return mode_flags;
 
@@ -1137,39 +1139,6 @@ int search_and_fopen_nulstr(
         return search_and_fopen_internal(filename, mode, root, s, ret, ret_path);
 }
 
-int chase_symlinks_and_fopen_unlocked(
-                const char *path,
-                const char *root,
-                unsigned chase_flags,
-                const char *open_flags,
-                FILE **ret_file,
-                char **ret_path) {
-
-        _cleanup_close_ int fd = -1;
-        _cleanup_free_ char *final_path = NULL;
-        int mode_flags, r;
-
-        assert(path);
-        assert(open_flags);
-        assert(ret_file);
-
-        mode_flags = mode_to_flags(open_flags);
-        if (mode_flags < 0)
-                return mode_flags;
-
-        fd = chase_symlinks_and_open(path, root, chase_flags, mode_flags, ret_path ? &final_path : NULL);
-        if (fd < 0)
-                return fd;
-
-        r = take_fdopen_unlocked(&fd, open_flags, ret_file);
-        if (r < 0)
-                return r;
-
-        if (ret_path)
-                *ret_path = TAKE_PTR(final_path);
-        return 0;
-}
-
 int fflush_and_check(FILE *f) {
         assert(f);
 
index c25a638104677ba26f51deb389b37b9311ecfd8e..899def946bd4c4c8ce14b49ca287d10ffab83425 100644 (file)
@@ -91,14 +91,6 @@ int xfopenat(int dir_fd, const char *path, const char *mode, int flags, FILE **r
 int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **ret, char **ret_path);
 int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **ret, char **ret_path);
 
-int chase_symlinks_and_fopen_unlocked(
-                const char *path,
-                const char *root,
-                unsigned chase_flags,
-                const char *open_flags,
-                FILE **ret_file,
-                char **ret_path);
-
 int fflush_and_check(FILE *f);
 int fflush_sync_and_check(FILE *f);
 
@@ -126,3 +118,5 @@ static inline int read_nul_string(FILE *f, size_t limit, char **ret) {
 int safe_fgetc(FILE *f, char *ret);
 
 int warn_file_is_world_accessible(const char *filename, struct stat *st, const char *unit, unsigned line);
+
+int fopen_mode_to_flags(const char *mode);
index 0220d71c2b93c69b736f9b242399d007988555db..a5d866da959688adb282120291a459f92fb01602 100644 (file)
@@ -24,7 +24,7 @@ static int check_etc_passwd_collisions(
         assert(directory);
         assert(name || uid_is_valid(uid));
 
-        r = chase_symlinks_and_fopen_unlocked("/etc/passwd", directory, CHASE_PREFIX_ROOT, "re", &f, NULL);
+        r = chase_symlinks_and_fopen_unlocked("/etc/passwd", directory, CHASE_PREFIX_ROOT, "re", NULL, &f);
         if (r == -ENOENT)
                 return 0; /* no user database? then no user, hence no collision */
         if (r < 0)
@@ -57,7 +57,7 @@ static int check_etc_group_collisions(
         assert(directory);
         assert(name || gid_is_valid(gid));
 
-        r = chase_symlinks_and_fopen_unlocked("/etc/group", directory, CHASE_PREFIX_ROOT, "re", &f, NULL);
+        r = chase_symlinks_and_fopen_unlocked("/etc/group", directory, CHASE_PREFIX_ROOT, "re", NULL, &f);
         if (r == -ENOENT)
                 return 0; /* no group database? then no group, hence no collision */
         if (r < 0)
index f34a48b6567ccb0d14dabcd3c10cf57157fae102..fd6b37b908308b8be6110b207c3f43169512c4f8 100644 (file)
@@ -55,7 +55,7 @@ int read_login_defs(UGIDAllocationRange *ret_defs, const char *path, const char
         if (!path)
                 path = "/etc/login.defs";
 
-        r = chase_symlinks_and_fopen_unlocked(path, root, CHASE_PREFIX_ROOT, "re", &f, NULL);
+        r = chase_symlinks_and_fopen_unlocked(path, root, CHASE_PREFIX_ROOT, "re", NULL, &f);
         if (r == -ENOENT)
                 goto assign;
         if (r < 0)