From 9d9951a460a90ef0e1e0384742cefdcf85193f8c Mon Sep 17 00:00:00 2001 From: Harald Hoyer Date: Thu, 6 Mar 2014 09:12:57 +0100 Subject: util: add files_same() helper function 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 | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'src/shared/util.c') diff --git a/src/shared/util.c b/src/shared/util.c index d28caae6c2..10f113bd24 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -3198,19 +3198,27 @@ bool on_tty(void) { return cached_on_tty; } -int running_in_chroot(void) { - struct stat a = {}, b = {}; +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; + + 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) { -- cgit v1.2.3-54-g00ecf