diff --git a/configure.ac b/configure.ac index 05a271f3e..bc27fd88e 100644 --- a/configure.ac +++ b/configure.ac @@ -19,6 +19,7 @@ AS_AC_EXPAND(LIBEXECDIR, $libexecdir) AS_AC_EXPAND(SYSCONFDIR, $sysconfdir) AS_AC_EXPAND(LOCALSTATEDIR, $localstatedir) AS_AC_EXPAND(LXCPATH, "${localstatedir}/lib/lxc") +AS_AC_EXPAND(LXC_GENERATE_DATE, "$(date)") AC_CHECK_HEADERS([linux/netlink.h linux/genetlink.h], [], AC_MSG_ERROR([netlink headers not found]), [#include diff --git a/doc/lxc-console.sgml.in b/doc/lxc-console.sgml.in index 4d06deee8..f86b85084 100644 --- a/doc/lxc-console.sgml.in +++ b/doc/lxc-console.sgml.in @@ -45,7 +45,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA lxc-console -n name - -t ttynum + -t ttynum @@ -56,7 +56,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA If the tty service has been configured and is available for the container specified as parameter, this command will launch a - console allowing to log to the container. + console allowing to log on the container. @@ -91,11 +91,12 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - + - Specify the tty number to connect. + Specify the tty number to connect, if not specified a tty + number will be automatically choosen by the container. diff --git a/src/lxc/console.c b/src/lxc/console.c index d8c8a1066..ca328aa81 100644 --- a/src/lxc/console.c +++ b/src/lxc/console.c @@ -45,30 +45,34 @@ extern int lxc_console(const char *name, int ttynum, int *fd) sock = lxc_af_unix_connect(addr.sun_path); if (sock < 0) { ERROR("failed to connect to the tty service"); - goto out_err; + goto out; } ret = lxc_af_unix_send_credential(sock, &ttynum, sizeof(ttynum)); if (ret < 0) { SYSERROR("failed to send credentials"); - goto out_err; + goto out_close; } - ret = lxc_af_unix_recv_fd(sock, fd, NULL, 0); + ret = lxc_af_unix_recv_fd(sock, fd, &ttynum, sizeof(ttynum)); if (ret < 0) { ERROR("failed to connect to the tty"); - goto out_err; + goto out_close; } + INFO("tty %d allocated", ttynum); + if (!ret) { - ERROR("tty%d denied by '%s'", ttynum, name); + ERROR("console denied by '%s'", name); ret = -LXC_ERROR_TTY_DENIED; - goto out_err; + goto out_close; } ret = 0; -out_err: - close(sock); +out: return ret; +out_close: + close(sock); + goto out; } diff --git a/src/lxc/lxc.h b/src/lxc/lxc.h index 2f45523ff..5908fb72b 100644 --- a/src/lxc/lxc.h +++ b/src/lxc/lxc.h @@ -120,6 +120,8 @@ extern int lxc_monitor_close(int fd); /* * Show the console of the container. * @name : the name of container + * @tty : the tty number + * @fd : a pointer to a tty file descriptor * Returns 0 on sucess, < 0 otherwise */ extern int lxc_console(const char *name, int ttynum, int *fd); diff --git a/src/lxc/lxc_console.c b/src/lxc/lxc_console.c index 0b13a5499..fa261892c 100644 --- a/src/lxc/lxc_console.c +++ b/src/lxc/lxc_console.c @@ -47,7 +47,7 @@ void usage(char *cmd) { fprintf(stderr, "%s \n", basename(cmd)); fprintf(stderr, "\t -n : name of the container\n"); - fprintf(stderr, "\t -t : tty number\n"); + fprintf(stderr, "\t [-t ] : tty number\n"); _exit(1); } @@ -55,9 +55,9 @@ int main(int argc, char *argv[]) { char *name = NULL; int opt; - int ttynum = 0; int nbargs = 0; int master = -1; + int ttynum = -1; int wait4q = 0; int err = LXC_ERROR_INTERNAL; struct termios tios, oldtios; @@ -67,6 +67,7 @@ int main(int argc, char *argv[]) case 'n': name = optarg; break; + case 't': ttynum = atoi(optarg); break; @@ -75,7 +76,7 @@ int main(int argc, char *argv[]) nbargs++; } - if (!name || !ttynum) + if (!name) usage(argv[0]); /* Get current termios */ diff --git a/src/lxc/start.c b/src/lxc/start.c index 7c6e4faee..3d10153a8 100644 --- a/src/lxc/start.c +++ b/src/lxc/start.c @@ -197,31 +197,41 @@ static int ttyservice_handler(int fd, void *data, if (lxc_af_unix_rcv_credential(conn, &ttynum, sizeof(ttynum))) goto out_close; - if (ttynum <= 0 || ttynum > tty_info->nbtty) + if (ttynum > 0) { + if (ttynum > tty_info->nbtty) + goto out_close; + + if (tty_info->pty_info[ttynum - 1].busy) + goto out_close; + + goto out_send; + } + + /* fixup index tty1 => [0] */ + for (ttynum = 1; + ttynum <= tty_info->nbtty && tty_info->pty_info[ttynum - 1].busy; + ttynum++); + + /* we didn't find any available slot for tty */ + if (ttynum > tty_info->nbtty) goto out_close; - /* fixup index array (eg. tty1 is index 0) */ - ttynum--; - - if (tty_info->pty_info[ttynum].busy) - goto out_close; - - if (lxc_af_unix_send_fd(conn, tty_info->pty_info[ttynum].master, - NULL, 0) < 0) { +out_send: + if (lxc_af_unix_send_fd(conn, tty_info->pty_info[ttynum - 1].master, + &ttynum, sizeof(ttynum)) < 0) { ERROR("failed to send tty to client"); goto out_close; } - if (lxc_mainloop_add_handler(descr, conn, + if (lxc_mainloop_add_handler(descr, conn, ttyclient_handler, tty_info)) { ERROR("failed to add tty client handler"); goto out_close; } - tty_info->pty_info[ttynum].busy = conn; + tty_info->pty_info[ttynum - 1].busy = conn; ret = 0; - out: return ret; out_close: