]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
autoassign tty number
authorDaniel Lezcano <dlezcano@fr.ibm.com>
Thu, 7 May 2009 17:36:33 +0000 (19:36 +0200)
committerDaniel Lezcano <dlezcano@fr.ibm.com>
Thu, 7 May 2009 17:36:33 +0000 (19:36 +0200)
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: Daniel Lezcano <dlezcano@fr.ibm.com>
configure.ac
doc/lxc-console.sgml.in
src/lxc/console.c
src/lxc/lxc.h
src/lxc/lxc_console.c
src/lxc/start.c

index 05a271f3e9073bf65a48bcc6f51de26d0a7887e2..bc27fd88eb74cb7d7951b5e5304d6360cb6429ce 100644 (file)
@@ -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 <linux/types.h>
index 4d06deee81ad70996678c3ad26a15a73a6d8f03d..f86b8508436b3ce4c1c994c33b4e6d10caa13c3b 100644 (file)
@@ -45,7 +45,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
   <refsynopsisdiv>
     <cmdsynopsis>
       <command>lxc-console <replaceable>-n name</replaceable>
-       <replaceable>-t ttynum</replaceable>
+       <optional>-t ttynum</optional>
       </command>
     </cmdsynopsis>
   </refsynopsisdiv>
@@ -56,7 +56,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
     <para>
       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.
     </para>
 
     <para>
@@ -91,11 +91,12 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
       <varlistentry>
        <term>
-         <option>-t <replaceable>ttynum</replaceable></option>
+         <option>-t <optional>ttynum</optional></option>
        </term>
        <listitem>
          <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>
        </listitem>
       </varlistentry>
index d8c8a1066de8031c1c2a565b3b4ab1d05b693336..ca328aa81a1182cef3de2def3ca2551cc4141e4a 100644 (file)
@@ -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;
 }
index 2f45523ff58c26e93f535d9543d73fc9371227b1..5908fb72b24105e3f53eb8dae8b2318c7bce0b6c 100644 (file)
@@ -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);
index 0b13a54991f65dccecbe9317e0aced937891f8ef..fa261892cad2e1d485c15a7141366467af0aaba7 100644 (file)
@@ -47,7 +47,7 @@ void usage(char *cmd)
 {
        fprintf(stderr, "%s <command>\n", basename(cmd));
        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);
 }
 
@@ -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 */
index 7c6e4faee36946450cb41f41393b216af63c4d83..3d10153a8511d1f27e0e9e5d4b74d37b26534f12 100644 (file)
@@ -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)
-               goto out_close;
+       if (ttynum > 0) {
+               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) */
-       ttynum--;
+               goto out_send;
+       }
 
-       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;
 
-       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: