]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
utils: add lxc_unstack_mountpoint()
authorChristian Brauner <christian.brauner@ubuntu.com>
Tue, 9 May 2017 20:04:21 +0000 (22:04 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Tue, 9 May 2017 21:27:47 +0000 (23:27 +0200)
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 <christian.brauner@ubuntu.com>
src/lxc/utils.c
src/lxc/utils.h

index 6458bbdce40bcea8ba21b946723f07e682ccd0dc..ae23d404994e0268112d890397b227d1f4cc1801 100644 (file)
@@ -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;
+}
index 161788f9891b95546a3e66dba9336c70016452c0..320aa6bf7e9e459dc34519919e478008f836fcea 100644 (file)
@@ -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 */