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: 's avatarChristian Brauner <christian.brauner@ubuntu.com>
parent 6e3bb289
......@@ -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;
}
......@@ -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 */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment