chiark / gitweb /
main: log to the console in a container
[elogind.git] / src / nspawn.c
index 4e4d40ea6f5c7f85e1fae4e3baaf69f267507327..f340805f38339835d8162d291dfd33a73961ef9d 100644 (file)
@@ -36,6 +36,7 @@
 
 #include "log.h"
 #include "util.h"
 
 #include "log.h"
 #include "util.h"
+#include "missing.h"
 
 static char *arg_directory = NULL;
 
 
 static char *arg_directory = NULL;
 
@@ -175,6 +176,9 @@ static int copy_devnodes(const char *dest) {
         int r = 0, k;
         char *tty = NULL;
         dev_t tty_devnum;
         int r = 0, k;
         char *tty = NULL;
         dev_t tty_devnum;
+        mode_t u;
+
+        u = umask(0000);
 
         NULSTR_FOREACH(d, devnodes) {
                 char *from = NULL, *to = NULL;
 
         NULSTR_FOREACH(d, devnodes) {
                 char *from = NULL, *to = NULL;
@@ -250,7 +254,7 @@ static int copy_devnodes(const char *dest) {
                                         r = -errno;
                         }
 
                                         r = -errno;
                         }
 
-                        if (mount(from, to, "bind", MS_BIND, NULL) < 0) {
+                        if (mount(from, to, "bind", MS_BIND|MS_RDONLY, NULL) < 0) {
                                 log_error("bind mount for /dev/console failed: %m");
 
                                 if (r == 0)
                                 log_error("bind mount for /dev/console failed: %m");
 
                                 if (r == 0)
@@ -264,6 +268,8 @@ static int copy_devnodes(const char *dest) {
 
         free(tty);
 
 
         free(tty);
 
+        umask(u);
+
         return r;
 }
 
         return r;
 }
 
@@ -366,7 +372,7 @@ int main(int argc, char *argv[]) {
         }
 
         if (path_equal(arg_directory, "/")) {
         }
 
         if (path_equal(arg_directory, "/")) {
-                log_error("Spawning constainer on root directory not supported.");
+                log_error("Spawning container on root directory not supported.");
                 goto finish;
         }
 
                 goto finish;
         }
 
@@ -384,6 +390,11 @@ int main(int argc, char *argv[]) {
 
         if (pid == 0) {
                 const char *hn;
 
         if (pid == 0) {
                 const char *hn;
+                const char *envp[] = {
+                        "HOME=/root",
+                        "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
+                        NULL
+                };
 
                 /* child */
 
 
                 /* child */
 
@@ -419,9 +430,11 @@ int main(int argc, char *argv[]) {
                         sethostname(hn, strlen(hn));
 
                 if (argc > optind)
                         sethostname(hn, strlen(hn));
 
                 if (argc > optind)
-                        execvp(argv[optind], argv + optind);
-                else
-                        execl("/bin/bash", "/bin/bash", NULL);
+                        execvpe(argv[optind], argv + optind, (char**) envp);
+                else {
+                        chdir("/root");
+                        execle("/bin/bash", "-bash", NULL, (char**) envp);
+                }
 
                 log_error("execv() failed: %m");
 
 
                 log_error("execv() failed: %m");
 
@@ -429,7 +442,7 @@ int main(int argc, char *argv[]) {
                 _exit(EXIT_FAILURE);
         }
 
                 _exit(EXIT_FAILURE);
         }
 
-        r = wait_for_terminate_and_warn("container", pid);
+        r = wait_for_terminate_and_warn(argc > optind ? argv[optind] : "bash", pid);
 
         if (r < 0)
                 r = EXIT_FAILURE;
 
         if (r < 0)
                 r = EXIT_FAILURE;