From f6310f1844ba5482dad66f3e5de0126098e17d13 Mon Sep 17 00:00:00 2001 From: Liza Tretyakova Date: Wed, 2 May 2018 10:47:15 +0300 Subject: [PATCH] utils: add shared mount point detection Signed-off-by: Liza Tretyakova --- src/lxc/utils.c | 33 ++++++++++++++++++++------------- src/lxc/utils.h | 1 + 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/lxc/utils.c b/src/lxc/utils.c index dd6cdc91a..bad355265 100644 --- a/src/lxc/utils.c +++ b/src/lxc/utils.c @@ -1219,19 +1219,12 @@ uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval) return hval; } -/* - * Detect whether / is mounted MS_SHARED. The only way I know of to - * check that is through /proc/self/mountinfo. - * I'm only checking for /. If the container rootfs or mount location - * is MS_SHARED, but not '/', then you're out of luck - figuring that - * out would be too much work to be worth it. - */ -int detect_shared_rootfs(void) +bool is_shared_mountpoint(const char *path) { - char buf[LXC_LINELEN], *p; + char buf[LXC_LINELEN]; FILE *f; int i; - char *p2; + char *p, *p2; f = fopen("/proc/self/mountinfo", "r"); if (!f) @@ -1248,17 +1241,31 @@ int detect_shared_rootfs(void) continue; *p2 = '\0'; - if (strcmp(p + 1, "/") == 0) { - /* This is '/'. Is it shared? */ + if (strcmp(p + 1, path) == 0) { + /* This is the path. Is it shared? */ p = strchr(p2 + 1, ' '); if (p && strstr(p, "shared:")) { fclose(f); - return 1; + return true; } } } fclose(f); + return false; +} + +/* + * Detect whether / is mounted MS_SHARED. The only way I know of to + * check that is through /proc/self/mountinfo. + * I'm only checking for /. If the container rootfs or mount location + * is MS_SHARED, but not '/', then you're out of luck - figuring that + * out would be too much work to be worth it. + */ +int detect_shared_rootfs(void) +{ + if (is_shared_mountpoint("/")) + return 1; return 0; } diff --git a/src/lxc/utils.h b/src/lxc/utils.h index 295e7862c..46ef28504 100644 --- a/src/lxc/utils.h +++ b/src/lxc/utils.h @@ -502,6 +502,7 @@ extern bool dir_exists(const char *path); #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL) extern uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval); +extern bool is_shared_mountpoint(const char *path); extern int detect_shared_rootfs(void); extern bool detect_ramfs_rootfs(void); extern char *on_path(const char *cmd, const char *rootfs); -- 2.47.2