From: Christian Brauner Date: Tue, 9 May 2017 20:04:21 +0000 (+0200) Subject: utils: add lxc_unstack_mountpoint() X-Git-Tag: lxc-2.1.0~139^2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=74251e49bba58feac8b3b84814b8f86348af0b82;p=thirdparty%2Flxc.git utils: add lxc_unstack_mountpoint() lxc_unstack_mountpoint() tries to clear all mountpoints from a given path. It return the number of successful umounts on success and -errno on error. Signed-off-by: Christian Brauner --- diff --git a/src/lxc/utils.c b/src/lxc/utils.c index 6458bbdce..ae23d4049 100644 --- a/src/lxc/utils.c +++ b/src/lxc/utils.c @@ -2204,3 +2204,29 @@ on_error: return fd_loop; } + +int lxc_unstack_mountpoint(const char *path, bool lazy) +{ + int ret; + int umounts = 0; + +pop_stack: + ret = umount2(path, lazy ? MNT_DETACH : 0); + if (ret < 0) { + /* We consider anything else than EINVAL deadly to prevent going + * into an infinite loop. (The other alternative is constantly + * parsing /proc/self/mountinfo which is yucky and probably + * racy.) + */ + if (errno != EINVAL) + return -errno; + } else { + /* We succeeded in umounting. Make sure that there's no other + * mountpoint stacked underneath. + */ + umounts++; + goto pop_stack; + } + + return umounts; +} diff --git a/src/lxc/utils.h b/src/lxc/utils.h index 161788f98..320aa6bf7 100644 --- a/src/lxc/utils.h +++ b/src/lxc/utils.h @@ -348,4 +348,11 @@ int lxc_setgroups(int size, gid_t list[]); /* Find an unused loop device and associate it with source. */ int lxc_prepare_loop_dev(const char *source, char *loop_dev, int flags); +/* Clear all mounts on a given node. + * >= 0 successfully cleared. The number returned is the number of umounts + * performed. + * < 0 error umounting. Return -errno. + */ +int lxc_unstack_mountpoint(const char *path, bool lazy); + #endif /* __LXC_UTILS_H */