Commit be43f17e by Daniel Lezcano

autoassign tty number

When no tty number is specified in the command line, let the tty service to provide choose one available tty and provide this one. The documentation is updated wrt this modification and I did a little fix to generate the date of the documentation. Signed-off-by: 's avatarDaniel Lezcano <dlezcano@fr.ibm.com>
parent 1bc5cc8c
...@@ -19,6 +19,7 @@ AS_AC_EXPAND(LIBEXECDIR, $libexecdir) ...@@ -19,6 +19,7 @@ AS_AC_EXPAND(LIBEXECDIR, $libexecdir)
AS_AC_EXPAND(SYSCONFDIR, $sysconfdir) AS_AC_EXPAND(SYSCONFDIR, $sysconfdir)
AS_AC_EXPAND(LOCALSTATEDIR, $localstatedir) AS_AC_EXPAND(LOCALSTATEDIR, $localstatedir)
AS_AC_EXPAND(LXCPATH, "${localstatedir}/lib/lxc") 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]), AC_CHECK_HEADERS([linux/netlink.h linux/genetlink.h], [], AC_MSG_ERROR([netlink headers not found]),
[#include <linux/types.h> [#include <linux/types.h>
......
...@@ -45,7 +45,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ...@@ -45,7 +45,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
<refsynopsisdiv> <refsynopsisdiv>
<cmdsynopsis> <cmdsynopsis>
<command>lxc-console <replaceable>-n name</replaceable> <command>lxc-console <replaceable>-n name</replaceable>
<replaceable>-t ttynum</replaceable> <optional>-t ttynum</optional>
</command> </command>
</cmdsynopsis> </cmdsynopsis>
</refsynopsisdiv> </refsynopsisdiv>
...@@ -56,7 +56,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ...@@ -56,7 +56,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
<para> <para>
If the tty service has been configured and is available for the If the tty service has been configured and is available for the
container specified as parameter, this command will launch a container specified as parameter, this command will launch a
console allowing to log to the container. console allowing to log on the container.
</para> </para>
<para> <para>
...@@ -91,11 +91,12 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ...@@ -91,11 +91,12 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
<varlistentry> <varlistentry>
<term> <term>
<option>-t <replaceable>ttynum</replaceable></option> <option>-t <optional>ttynum</optional></option>
</term> </term>
<listitem> <listitem>
<para> <para>
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.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
......
...@@ -45,30 +45,34 @@ extern int lxc_console(const char *name, int ttynum, int *fd) ...@@ -45,30 +45,34 @@ extern int lxc_console(const char *name, int ttynum, int *fd)
sock = lxc_af_unix_connect(addr.sun_path); sock = lxc_af_unix_connect(addr.sun_path);
if (sock < 0) { if (sock < 0) {
ERROR("failed to connect to the tty service"); ERROR("failed to connect to the tty service");
goto out_err; goto out;
} }
ret = lxc_af_unix_send_credential(sock, &ttynum, sizeof(ttynum)); ret = lxc_af_unix_send_credential(sock, &ttynum, sizeof(ttynum));
if (ret < 0) { if (ret < 0) {
SYSERROR("failed to send credentials"); 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) { if (ret < 0) {
ERROR("failed to connect to the tty"); ERROR("failed to connect to the tty");
goto out_err; goto out_close;
} }
INFO("tty %d allocated", ttynum);
if (!ret) { if (!ret) {
ERROR("tty%d denied by '%s'", ttynum, name); ERROR("console denied by '%s'", name);
ret = -LXC_ERROR_TTY_DENIED; ret = -LXC_ERROR_TTY_DENIED;
goto out_err; goto out_close;
} }
ret = 0; ret = 0;
out_err: out:
close(sock);
return ret; return ret;
out_close:
close(sock);
goto out;
} }
...@@ -120,6 +120,8 @@ extern int lxc_monitor_close(int fd); ...@@ -120,6 +120,8 @@ extern int lxc_monitor_close(int fd);
/* /*
* Show the console of the container. * Show the console of the container.
* @name : the name of container * @name : the name of container
* @tty : the tty number
* @fd : a pointer to a tty file descriptor
* Returns 0 on sucess, < 0 otherwise * Returns 0 on sucess, < 0 otherwise
*/ */
extern int lxc_console(const char *name, int ttynum, int *fd); extern int lxc_console(const char *name, int ttynum, int *fd);
......
...@@ -47,7 +47,7 @@ void usage(char *cmd) ...@@ -47,7 +47,7 @@ void usage(char *cmd)
{ {
fprintf(stderr, "%s <command>\n", basename(cmd)); fprintf(stderr, "%s <command>\n", basename(cmd));
fprintf(stderr, "\t -n <name> : name of the container\n"); fprintf(stderr, "\t -n <name> : name of the container\n");
fprintf(stderr, "\t -t <tty#> : tty number\n"); fprintf(stderr, "\t [-t <tty#>] : tty number\n");
_exit(1); _exit(1);
} }
...@@ -55,9 +55,9 @@ int main(int argc, char *argv[]) ...@@ -55,9 +55,9 @@ int main(int argc, char *argv[])
{ {
char *name = NULL; char *name = NULL;
int opt; int opt;
int ttynum = 0;
int nbargs = 0; int nbargs = 0;
int master = -1; int master = -1;
int ttynum = -1;
int wait4q = 0; int wait4q = 0;
int err = LXC_ERROR_INTERNAL; int err = LXC_ERROR_INTERNAL;
struct termios tios, oldtios; struct termios tios, oldtios;
...@@ -67,6 +67,7 @@ int main(int argc, char *argv[]) ...@@ -67,6 +67,7 @@ int main(int argc, char *argv[])
case 'n': case 'n':
name = optarg; name = optarg;
break; break;
case 't': case 't':
ttynum = atoi(optarg); ttynum = atoi(optarg);
break; break;
...@@ -75,7 +76,7 @@ int main(int argc, char *argv[]) ...@@ -75,7 +76,7 @@ int main(int argc, char *argv[])
nbargs++; nbargs++;
} }
if (!name || !ttynum) if (!name)
usage(argv[0]); usage(argv[0]);
/* Get current termios */ /* Get current termios */
......
...@@ -197,31 +197,41 @@ static int ttyservice_handler(int fd, void *data, ...@@ -197,31 +197,41 @@ static int ttyservice_handler(int fd, void *data,
if (lxc_af_unix_rcv_credential(conn, &ttynum, sizeof(ttynum))) if (lxc_af_unix_rcv_credential(conn, &ttynum, sizeof(ttynum)))
goto out_close; goto out_close;
if (ttynum <= 0 || ttynum > tty_info->nbtty) if (ttynum > 0) {
goto out_close; if (ttynum > tty_info->nbtty)
goto out_close;
if (tty_info->pty_info[ttynum - 1].busy)
goto out_close;
/* fixup index array (eg. tty1 is index 0) */ goto out_send;
ttynum--; }
if (tty_info->pty_info[ttynum].busy) /* 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; goto out_close;
if (lxc_af_unix_send_fd(conn, tty_info->pty_info[ttynum].master, out_send:
NULL, 0) < 0) { 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"); ERROR("failed to send tty to client");
goto out_close; goto out_close;
} }
if (lxc_mainloop_add_handler(descr, conn, if (lxc_mainloop_add_handler(descr, conn,
ttyclient_handler, tty_info)) { ttyclient_handler, tty_info)) {
ERROR("failed to add tty client handler"); ERROR("failed to add tty client handler");
goto out_close; goto out_close;
} }
tty_info->pty_info[ttynum].busy = conn; tty_info->pty_info[ttynum - 1].busy = conn;
ret = 0; ret = 0;
out: out:
return ret; return ret;
out_close: out_close:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment