[systemd-devel] [PATCH] nspawn: fix buggy mount_binds, now works for bind-mounted files
Shawn Landden
shawn at churchofgit.com
Thu Dec 5 06:20:08 PST 2013
---
src/nspawn/nspawn.c | 42 ++++++++++++++++++++++++++----------------
1 file changed, 26 insertions(+), 16 deletions(-)
diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
index dd7337b..c1212c0 100644
--- a/src/nspawn/nspawn.c
+++ b/src/nspawn/nspawn.c
@@ -416,6 +416,7 @@ static int mount_all(const char *dest) {
static int mount_binds(const char *dest, char **l, unsigned long flags) {
char **x, **y;
+ int r;
STRV_FOREACH_PAIR(x, y, l) {
_cleanup_free_ char *where = NULL;
@@ -426,31 +427,40 @@ static int mount_binds(const char *dest, char **l, unsigned long flags) {
return -errno;
}
- where = strjoin(dest, "/", *y, NULL);
+ where = strjoin(dest, *y, NULL);
if (!where)
return log_oom();
- if (stat(where, &dest_st) == 0) {
+ r = stat(where, &dest_st);
+ if (r == 0) {
if ((source_st.st_mode & S_IFMT) != (dest_st.st_mode & S_IFMT)) {
log_error("The file types of %s and %s do not match. Refusing bind mount",
*x, where);
return -EINVAL;
}
- } else {
- /* Create the mount point, but be conservative -- refuse to create block
- * and char devices. */
- if (S_ISDIR(source_st.st_mode))
- mkdir_p_label(where, 0755);
- else if (S_ISFIFO(source_st.st_mode))
- mkfifo(where, 0644);
- else if (S_ISSOCK(source_st.st_mode))
- mknod(where, 0644 | S_IFSOCK, 0);
- else if (S_ISREG(source_st.st_mode))
- touch(where);
- else {
- log_error("Refusing to create mountpoint for file: %s", *x);
- return -ENOTSUP;
+ } else if (errno == ENOENT) {
+ r = mkdir_parents_label(where, 0755);
+ if (r < 0) {
+ log_error("Failed to bind mount %s: %s", *x, strerror(-r));
+ return r;
}
+ } else {
+ log_error("Failed to bind mount %s: %s", *x, strerror(errno));
+ return -errno;
+ }
+ /* Create the mount point, but be conservative -- refuse to create block
+ * and char devices. */
+ if (S_ISDIR(source_st.st_mode))
+ mkdir_label(where, 0755);
+ else if (S_ISFIFO(source_st.st_mode))
+ mkfifo(where, 0644);
+ else if (S_ISSOCK(source_st.st_mode))
+ mknod(where, 0644 | S_IFSOCK, 0);
+ else if (S_ISREG(source_st.st_mode))
+ touch(where);
+ else {
+ log_error("Refusing to create mountpoint for file: %s", *x);
+ return -ENOTSUP;
}
if (mount(*x, where, "bind", MS_BIND, NULL) < 0) {
--
1.8.5.1
More information about the systemd-devel
mailing list