]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic/path-util: allow flags for path_equal_or_files_same
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 17 Jun 2017 16:37:16 +0000 (12:37 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 17 Jun 2017 16:37:16 +0000 (12:37 -0400)
No functional change, just a new parameters and the tests that
AT_SYMLINK_NOFOLLOW works as expected.

14 files changed:
src/basic/path-util.c
src/basic/path-util.h
src/basic/process-util.c
src/basic/rm-rf.c
src/basic/socket-util.c
src/basic/stat-util.c
src/basic/stat-util.h
src/basic/util.c
src/basic/virt.c
src/core/socket.c
src/shared/install.c
src/systemctl/systemctl.c
src/test/test-path-util.c
src/test/test-stat-util.c

index 1313a52c9cab551b682a3977f566b32405f0d7a4..80fdda170f10c42472ca2ea98bc821f0b681b509 100644 (file)
@@ -442,8 +442,8 @@ bool path_equal(const char *a, const char *b) {
         return path_compare(a, b) == 0;
 }
 
-bool path_equal_or_files_same(const char *a, const char *b) {
-        return path_equal(a, b) || files_same(a, b) > 0;
+bool path_equal_or_files_same(const char *a, const char *b, int flags) {
+        return path_equal(a, b) || files_same(a, b, flags) > 0;
 }
 
 char* path_join(const char *root, const char *path, const char *rest) {
index 35aef3adc83b5ff13364be969c067cf279ddd169..26f165fc386791cebfa6a4f60932c63e8852e559 100644 (file)
@@ -46,7 +46,7 @@ char* path_kill_slashes(char *path);
 char* path_startswith(const char *path, const char *prefix) _pure_;
 int path_compare(const char *a, const char *b) _pure_;
 bool path_equal(const char *a, const char *b) _pure_;
-bool path_equal_or_files_same(const char *a, const char *b);
+bool path_equal_or_files_same(const char *a, const char *b, int flags);
 char* path_join(const char *root, const char *path, const char *rest);
 
 static inline bool path_equal_ptr(const char *a, const char *b) {
index 0df3fed64064e0f9b171e4ed38fc2bd268dd944c..a21dc35baa59b54df83d6be3af3843c74464a2e0 100644 (file)
@@ -807,7 +807,7 @@ int pid_from_same_root_fs(pid_t pid) {
 
         root = procfs_file_alloca(pid, "root");
 
-        return files_same(root, "/proc/1/root");
+        return files_same(root, "/proc/1/root", 0);
 }
 
 bool is_main_thread(void) {
index ff040e7a5516c24a158597b7e2e01e5e9c051203..421289e11bb179702237cde02aabbb009eef4ee0 100644 (file)
@@ -182,7 +182,7 @@ int rm_rf(const char *path, RemoveFlags flags) {
         /* We refuse to clean the root file system with this
          * call. This is extra paranoia to never cause a really
          * seriously broken system. */
-        if (path_equal_or_files_same(path, "/")) {
+        if (path_equal_or_files_same(path, "/", 0)) {
                 log_error("Attempted to remove entire root file system, and we can't allow that.");
                 return -EPERM;
         }
index 49642c23474a9c29e57a1f1438fc1b9dd08b5e93..016e64aa03442d4aca97cc862e292cee28cbf5d4 100644 (file)
@@ -412,7 +412,7 @@ bool socket_address_equal(const SocketAddress *a, const SocketAddress *b) {
                         return false;
 
                 if (a->sockaddr.un.sun_path[0]) {
-                        if (!path_equal_or_files_same(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path))
+                        if (!path_equal_or_files_same(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, 0))
                                 return false;
                 } else {
                         if (a->size != b->size)
index 7e1914aa140fae46a0c8c85078586b33befabccd..d87370e672985a53f7f91278aa3b96920534c803 100644 (file)
@@ -169,16 +169,16 @@ int path_is_os_tree(const char *path) {
         return 1;
 }
 
-int files_same(const char *filea, const char *fileb) {
+int files_same(const char *filea, const char *fileb, int flags) {
         struct stat a, b;
 
         assert(filea);
         assert(fileb);
 
-        if (stat(filea, &a) < 0)
+        if (fstatat(AT_FDCWD, filea, &a, flags) < 0)
                 return -errno;
 
-        if (stat(fileb, &b) < 0)
+        if (fstatat(AT_FDCWD, fileb, &b, flags) < 0)
                 return -errno;
 
         return a.st_dev == b.st_dev &&
index 5d571efe186fb43f394282f72018522776e23404..cd204ac6cbda9368c5725d986a4f3876598ffd64 100644 (file)
@@ -49,7 +49,7 @@ int null_or_empty_fd(int fd);
 int path_is_read_only_fs(const char *path);
 int path_is_os_tree(const char *path);
 
-int files_same(const char *filea, const char *fileb);
+int files_same(const char *filea, const char *fileb, int flags);
 
 /* The .f_type field of struct statfs is really weird defined on
  * different archs. Let's give its type a name. */
index 3dce0ea92eb79044a7b3ee39f88ccd0e9ee1c67c..b52a5db31b9e8f997a2a3d4992e779b8110001dd 100644 (file)
@@ -541,7 +541,7 @@ int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int
                 if (asprintf(&userns_fd_path, "/proc/self/fd/%d", userns_fd) < 0)
                         return -ENOMEM;
 
-                r = files_same(userns_fd_path, "/proc/self/ns/user");
+                r = files_same(userns_fd_path, "/proc/self/ns/user", 0);
                 if (r < 0)
                         return r;
                 if (r)
index ff4491d6d61f64c04b0885a21e4811b0e3d405de..60117445238f395b8015dcb73b0ac5a17933d1ed 100644 (file)
@@ -574,7 +574,7 @@ int running_in_chroot(void) {
         if (getenv_bool("SYSTEMD_IGNORE_CHROOT") > 0)
                 return 0;
 
-        ret = files_same("/proc/1/root", "/");
+        ret = files_same("/proc/1/root", "/", 0);
         if (ret < 0)
                 return ret;
 
index c4da227e0931144cd74c2e143bbbd78f68b869d1..8750643d926c3dcc357aa0aaa9e4ea2817e373a8 100644 (file)
@@ -2528,7 +2528,7 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
                 else
                         LIST_FOREACH(port, p, s->ports)
                                 if (p->type == SOCKET_FIFO &&
-                                    path_equal_or_files_same(p->path, value+skip)) {
+                                    path_equal_or_files_same(p->path, value+skip, 0)) {
                                         socket_port_take_fd(p, fds, fd);
                                         break;
                                 }
@@ -2542,7 +2542,7 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
                 else
                         LIST_FOREACH(port, p, s->ports)
                                 if (p->type == SOCKET_SPECIAL &&
-                                    path_equal_or_files_same(p->path, value+skip)) {
+                                    path_equal_or_files_same(p->path, value+skip, 0)) {
                                         socket_port_take_fd(p, fds, fd);
                                         break;
                                 }
@@ -2596,7 +2596,7 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
                 else
                         LIST_FOREACH(port, p, s->ports)
                                 if (p->type == SOCKET_USB_FUNCTION &&
-                                    path_equal_or_files_same(p->path, value+skip)) {
+                                    path_equal_or_files_same(p->path, value+skip, 0)) {
                                         socket_port_take_fd(p, fds, fd);
                                         break;
                                 }
index 7f3bc0d8132785f5141c0ddf4ee9edd134159e86..d0a291b819b6714bc97c1a0217749a9d9184c69c 100644 (file)
@@ -422,7 +422,7 @@ static bool chroot_symlinks_same(const char *root, const char *wd, const char *a
 
         a = strjoina(path_is_absolute(a) ? root : wd, "/", a);
         b = strjoina(path_is_absolute(b) ? root : wd, "/", b);
-        return path_equal_or_files_same(a, b);
+        return path_equal_or_files_same(a, b, 0);
 }
 
 static int create_symlink(
index 2f69e863c5e8ae0dee8935b0f4e47f47da6aaf97..9ad74b0695085156609885853d906b1a123c73fc 100644 (file)
@@ -5654,7 +5654,7 @@ static int switch_root(int argc, char *argv[], void *userdata) {
 
                 /* If the passed init is actually the same as the
                  * systemd binary, then let's suppress it. */
-                if (files_same(root_init_path, root_systemd_path) > 0)
+                if (files_same(root_init_path, root_systemd_path, 0) > 0)
                         init = NULL;
         }
 
index ab62d0dad2f043da6674b1973bb1e364554616f2..e5644246c287ab5b33c6ebe6c14cdce8708aee4d 100644 (file)
@@ -118,23 +118,33 @@ static void test_path_equal_root(void) {
 
         /* Make sure that files_same works as expected. */
 
-        assert_se(files_same("/", "/") > 0);
-        assert_se(files_same("/", "//") > 0);
+        assert_se(files_same("/", "/", 0) > 0);
+        assert_se(files_same("/", "/", AT_SYMLINK_NOFOLLOW) > 0);
+        assert_se(files_same("/", "//", 0) > 0);
+        assert_se(files_same("/", "//", AT_SYMLINK_NOFOLLOW) > 0);
 
-        assert_se(files_same("/", "/./") > 0);
-        assert_se(files_same("/", "/../") > 0);
+        assert_se(files_same("/", "/./", 0) > 0);
+        assert_se(files_same("/", "/./", AT_SYMLINK_NOFOLLOW) > 0);
+        assert_se(files_same("/", "/../", 0) > 0);
+        assert_se(files_same("/", "/../", AT_SYMLINK_NOFOLLOW) > 0);
 
-        assert_se(files_same("/", "/.../") == -ENOENT);
+        assert_se(files_same("/", "/.../", 0) == -ENOENT);
+        assert_se(files_same("/", "/.../", AT_SYMLINK_NOFOLLOW) == -ENOENT);
 
         /* The same for path_equal_or_files_same. */
 
-        assert_se(path_equal_or_files_same("/", "/"));
-        assert_se(path_equal_or_files_same("/", "//"));
+        assert_se(path_equal_or_files_same("/", "/", 0));
+        assert_se(path_equal_or_files_same("/", "/", AT_SYMLINK_NOFOLLOW));
+        assert_se(path_equal_or_files_same("/", "//", 0));
+        assert_se(path_equal_or_files_same("/", "//", AT_SYMLINK_NOFOLLOW));
 
-        assert_se(path_equal_or_files_same("/", "/./"));
-        assert_se(path_equal_or_files_same("/", "/../"));
+        assert_se(path_equal_or_files_same("/", "/./", 0));
+        assert_se(path_equal_or_files_same("/", "/./", AT_SYMLINK_NOFOLLOW));
+        assert_se(path_equal_or_files_same("/", "/../", 0));
+        assert_se(path_equal_or_files_same("/", "/../", AT_SYMLINK_NOFOLLOW));
 
-        assert_se(!path_equal_or_files_same("/", "/.../"));
+        assert_se(!path_equal_or_files_same("/", "/.../", 0));
+        assert_se(!path_equal_or_files_same("/", "/.../", AT_SYMLINK_NOFOLLOW));
 }
 
 static void test_find_binary(const char *self) {
index 3ff2aadea48fe404304e61cbf52047d0a8bfa5d8..8e027ff26cf60a7b8ea3ec1d081baabe634194c1 100644 (file)
@@ -38,8 +38,10 @@ static void test_files_same(void) {
         assert_se(fd >= 0);
         assert_se(symlink(name, name_alias) >= 0);
 
-        assert_se(files_same(name, name));
-        assert_se(files_same(name, name_alias));
+        assert_se(files_same(name, name, 0));
+        assert_se(files_same(name, name, AT_SYMLINK_NOFOLLOW));
+        assert_se(files_same(name, name_alias, 0));
+        assert_se(!files_same(name, name_alias, AT_SYMLINK_NOFOLLOW));
 
         unlink(name);
         unlink(name_alias);