]> git.tuebingen.mpg.de Git - micoforia.git/blobdiff - micoforia.c
Improve the default command for entering a container.
[micoforia.git] / micoforia.c
index 4d267ec8f56a0912fb897bc930af328142b8420c..699354202debf3ab1ac640cdd3cb3e6bfd90fb19 100644 (file)
@@ -890,6 +890,7 @@ static void create_standard_device_nodes(struct container_runtime *cr)
                {.major = 1, .minor = 8, .mode = 0666, .name = "random"},
                {.major = 1, .minor = 9, .mode = 0666, .name = "urandom"},
                {.major = 4, .minor = 0, .mode = 0620, .name = "tty0"},
+               {.major = 5, .minor = 0, .mode = 0666, .name = "tty"},
                {.major = 5, .minor = 1, .mode = 0600, .name = "console"},
                {.major = 5, .minor = 2, .mode = 0666, .name = "ptmx"},
        };
@@ -943,7 +944,7 @@ static void shutdown_console(struct container_runtime *cr)
        char *console;
 
        for (n = 0; n < cr->num_ttys; n++) {
-               char *tty = msg("%s/tty1", cr->dev);
+               char *tty = msg("%s/tty%u", cr->dev, n);
                if (umount2(tty, MNT_DETACH) < 0)
                        DEBUG_LOG("umount %s: %m\n", tty);
                free(tty);
@@ -1921,13 +1922,17 @@ static bool com_enter(void)
        char **argv;
        char *nsenter_args[] = {"nsenter", "-w", "-a", "-r", "-t"};
        const unsigned nna = ARRAY_SIZE(nsenter_args); /* num nsenter args */
-       char *dflt_cmd[] = {"login", "-f", "root"};
+       char *dflt_cmd[] = {"bash", "-c", "[[ -f /etc/os-release ]] && "
+               ". /etc/os-release && printf '%s\n' \"$PRETTY_NAME\"; "
+               "command cd; exec bash"};
        unsigned n, N, ni = lls_num_inputs(sublpr);
        unsigned nea = ni > 1? ni - 1 : ARRAY_SIZE(dflt_cmd); /* num extra args */
        const char *arg;
        bool success;
        int ret, pid;
        char *errctx;
+       struct termios tios;
+       bool tty;
 
        ret = lls_check_arg_count(sublpr, 1, INT_MAX, &errctx);
        if (ret < 0)
@@ -1950,7 +1955,21 @@ static bool com_enter(void)
                        : dflt_cmd[n];
        argv[N - 1] = NULL;
        clean_env();
+       tty = false;
+       if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) {
+               if (tcgetattr(STDIN_FILENO, &tios) >= 0)
+                       tty = true;
+       }
        success = xexec(argv, NULL);
+       if (tty) { /* reset terminal settings */
+               /*
+                * First give up the controlling terminal. Without this, the
+                * command gets SIGSTOP and goes to the background. We ignore
+                * errors here because nobody cares about a messed up terminal.
+                */
+               ioctl(STDIN_FILENO, TIOCNOTTY);
+               tcsetattr(STDIN_FILENO, TCSAFLUSH, &tios);
+       }
        free(argv);
        return success;
 }