[systemd-devel] [PATCH 1/2] util: add files_same() helper function
harald at redhat.com
harald at redhat.com
Wed Mar 5 05:37:46 PST 2014
From: Harald Hoyer <harald at redhat.com>
files_same() returns
1, if the files are the same
0, if the files have different inode/dev numbers
errno, for any stat error
---
src/shared/util.c | 20 +++++++++++++-------
src/shared/util.h | 2 ++
2 files changed, 15 insertions(+), 7 deletions(-)
diff --git a/src/shared/util.c b/src/shared/util.c
index 8c7cfbd..b1341ad 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -3198,19 +3198,25 @@ bool on_tty(void) {
return cached_on_tty;
}
-int running_in_chroot(void) {
+int files_same(const char *filea, const char *fileb) {
struct stat a = {}, b = {};
- /* Only works as root */
- if (stat("/proc/1/root", &a) < 0)
+ if (stat(filea, &a) < 0)
return -errno;
- if (stat("/", &b) < 0)
+ if (stat(fileb, &b) < 0)
return -errno;
- return
- a.st_dev != b.st_dev ||
- a.st_ino != b.st_ino;
+ return (a.st_dev == b.st_dev && a.st_ino == b.st_ino);
+}
+
+int running_in_chroot(void) {
+ int ret = files_same("/proc/1/root", "/");
+
+ if (ret < 0)
+ return ret;
+
+ return (ret == 0);
}
static char *ascii_ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
diff --git a/src/shared/util.h b/src/shared/util.h
index e430071..eab8cfd 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -451,6 +451,8 @@ static inline const char *ansi_highlight_off(void) {
return on_tty() ? ANSI_HIGHLIGHT_OFF : "";
}
+int files_same(const char *filea, const char *fileb);
+
int running_in_chroot(void);
char *ellipsize(const char *s, size_t length, unsigned percent);
--
1.8.5.3
More information about the systemd-devel
mailing list