daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
65878a
From b6fbffe3d4a9435cc8560ee0271c7e6e763e89c1 Mon Sep 17 00:00:00 2001
65878a
From: Harald Hoyer <harald@redhat.com>
65878a
Date: Thu, 6 Mar 2014 09:12:57 +0100
65878a
Subject: [PATCH] util: add files_same() helper function
65878a
65878a
files_same() returns
65878a
     1, if the files are the same
65878a
     0, if the files have different inode/dev numbers
65878a
 errno, for any stat error
65878a
65878a
 (cherry picked from commit 9d9951a460a90ef0e1e0384742cefdcf85193f8c)
65878a
65878a
 Related: #1111199
65878a
---
65878a
 src/shared/util.c | 24 ++++++++++++++++--------
65878a
 src/shared/util.h |  2 ++
65878a
 2 files changed, 18 insertions(+), 8 deletions(-)
65878a
65878a
diff --git a/src/shared/util.c b/src/shared/util.c
65878a
index 2974c2a..fb1e6d1 100644
65878a
--- a/src/shared/util.c
65878a
+++ b/src/shared/util.c
65878a
@@ -3311,19 +3311,27 @@ bool on_tty(void) {
65878a
         return cached_on_tty;
65878a
 }
65878a
 
65878a
-int running_in_chroot(void) {
65878a
-        struct stat a = {}, b = {};
65878a
+int files_same(const char *filea, const char *fileb) {
65878a
+        struct stat a, b;
65878a
 
65878a
-        /* Only works as root */
65878a
-        if (stat("/proc/1/root", &a) < 0)
65878a
+        if (stat(filea, &a) < 0)
65878a
                 return -errno;
65878a
 
65878a
-        if (stat("/", &b) < 0)
65878a
+        if (stat(fileb, &b) < 0)
65878a
                 return -errno;
65878a
 
65878a
-        return
65878a
-                a.st_dev != b.st_dev ||
65878a
-                a.st_ino != b.st_ino;
65878a
+        return a.st_dev == b.st_dev &&
65878a
+               a.st_ino == b.st_ino;
65878a
+}
65878a
+
65878a
+int running_in_chroot(void) {
65878a
+        int ret;
65878a
+
65878a
+        ret = files_same("/proc/1/root", "/");
65878a
+        if (ret < 0)
65878a
+                return ret;
65878a
+
65878a
+        return ret == 0;
65878a
 }
65878a
 
65878a
 char *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
65878a
diff --git a/src/shared/util.h b/src/shared/util.h
65878a
index e577ef0..631a385 100644
65878a
--- a/src/shared/util.h
65878a
+++ b/src/shared/util.h
65878a
@@ -403,6 +403,8 @@ static inline const char *ansi_highlight_off(void) {
65878a
         return on_tty() ? ANSI_HIGHLIGHT_OFF : "";
65878a
 }
65878a
 
65878a
+int files_same(const char *filea, const char *fileb);
65878a
+
65878a
 int running_in_chroot(void);
65878a
 
65878a
 char *ellipsize(const char *s, size_t length, unsigned percent);