chiark / gitweb /
bus: connect directly via kdbus in sd_bus_open_system_container()
[elogind.git] / src / nspawn / nspawn.c
index dd7337bc91351e80632fb57fdad84ab0d5986d2f..b3ca10ea9116c67168e904592f55e532d194e1a3 100644 (file)
@@ -418,39 +418,46 @@ static int mount_binds(const char *dest, char **l, unsigned long flags) {
         char **x, **y;
 
         STRV_FOREACH_PAIR(x, y, l) {
-                _cleanup_free_ char *where = NULL;
+                char *where;
                 struct stat source_st, dest_st;
+                int r;
 
                 if (stat(*x, &source_st) < 0) {
                         log_error("failed to stat %s: %m", *x);
                         return -errno;
                 }
 
-                where = strjoin(dest, "/", *y, NULL);
-                if (!where)
-                        return log_oom();
-
-                if (stat(where, &dest_st) == 0) {
+                where = strappenda(dest, *y);
+                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) {
@@ -1060,6 +1067,7 @@ int main(int argc, char *argv[]) {
         _cleanup_close_pipe_ int kmsg_socket_pair[2] = { -1, -1 };
         _cleanup_fdset_free_ FDSet *fds = NULL;
         _cleanup_free_ char *kdbus_namespace = NULL;
+        const char *ns;
 
         log_parse_environment();
         log_open();
@@ -1089,7 +1097,7 @@ int main(int argc, char *argv[]) {
         path_kill_slashes(arg_directory);
 
         if (!arg_machine) {
-                arg_machine = strdup(path_get_file_name(arg_directory));
+                arg_machine = strdup(basename(arg_directory));
                 if (!arg_machine) {
                         log_oom();
                         goto finish;
@@ -1160,7 +1168,8 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
-        kdbus_fd = bus_kernel_create_namespace(arg_machine, &kdbus_namespace);
+        ns = strappenda("machine-", arg_machine);
+        kdbus_fd = bus_kernel_create_namespace(ns, &kdbus_namespace);
         if (r < 0)
                 log_debug("Failed to create kdbus namespace: %s", strerror(-r));
         else