diff --git a/src/shared/util.c b/src/shared/util.c index c3f1ed244..64680561e 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -3204,19 +3204,27 @@ bool on_tty(void) { return cached_on_tty; } +int files_same(const char *filea, const char *fileb) { + struct stat a, b; + + if (stat(filea, &a) < 0) + return -errno; + + if (stat(fileb, &b) < 0) + return -errno; + + return a.st_dev == b.st_dev && + a.st_ino == b.st_ino; +} + int running_in_chroot(void) { - struct stat a = {}, b = {}; + int ret; - /* Only works as root */ - if (stat("/proc/1/root", &a) < 0) - return -errno; + ret = files_same("/proc/1/root", "/"); + if (ret < 0) + return ret; - if (stat("/", &b) < 0) - return -errno; - - return - a.st_dev != b.st_dev || - a.st_ino != b.st_ino; + 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 38d649401..5d2bbfb44 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -445,6 +445,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);