lxc_attach.c revision 478dda766ac987ac54df79a126209bbd30792b49
/*
* lxc: linux Container library
*
* (C) Copyright IBM Corp. 2007, 2010
*
* Authors:
* Daniel Lezcano <daniel.lezcano at free.fr>
*
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _GNU_SOURCE
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <lxc/lxccontainer.h>
#include "attach.h"
#include "arguments.h"
#include "caps.h"
#include "config.h"
#include "confile.h"
#include "console.h"
#include "log.h"
#include "list.h"
#include "mainloop.h"
#include "utils.h"
#if HAVE_PTY_H
#include <pty.h>
#else
#include <../include/openpty.h>
#endif
static const struct option my_longopts[] = {
/* TODO: decide upon short option names */
};
static int elevated_privileges = 0;
static signed long new_personality = -1;
static int namespace_flags = -1;
static int remount_sys_proc = 0;
static ssize_t extra_env_size = 0;
static char **extra_keep = NULL;
static ssize_t extra_keep_size = 0;
{
if (*array)
/* we have to reallocate */
if (!new_array)
return -1;
*capacity = new_capacity;
}
return 0;
}
{
int ret;
switch (c) {
case 'e':
if (ret)
return -1;
break;
case 'a':
if (new_personality < 0) {
return -1;
}
break;
case 's':
namespace_flags = 0;
if (ret)
return -1;
/* -s implies -e */
break;
case 500: /* clear-env */
break;
case 501: /* keep-env */
break;
case 502: /* keep-var */
if (ret < 0) {
return -1;
}
break;
case 'v':
if (ret < 0) {
return -1;
}
break;
}
return 0;
}
static struct lxc_arguments my_args = {
.progname = "lxc-attach",
.help = "\
--name=NAME [-- COMMAND]\n\
\n\
Execute the specified COMMAND - enter the container NAME\n\
\n\
Options :\n\
-n, --name=NAME NAME of the container\n\
-e, --elevated-privileges=PRIVILEGES\n\
Use elevated privileges instead of those of the\n\
container. If you don't specify privileges to be\n\
elevated as OR'd list: CAP, CGROUP and LSM (capabilities,\n\
cgroup and restrictions, respectively) then all of them\n\
will be elevated.\n\
WARNING: This may leak privileges into the container.\n\
Use with care.\n\
-a, --arch=ARCH Use ARCH for program instead of container's own\n\
architecture.\n\
-s, --namespaces=FLAGS\n\
Don't attach to all the namespaces of the container\n\
but just to the following OR'd list of flags:\n\
MOUNT, PID, UTSNAME, IPC, USER or NETWORK.\n\
WARNING: Using -s implies -e with all privileges\n\
elevated, it may therefore leak privileges into the\n\
container. Use with care.\n\
-R, --remount-sys-proc\n\
Remount /sys and /proc if not attaching to the\n\
mount namespace when using -s in order to properly\n\
reflect the correct namespace context. See the\n\
lxc-attach(1) manual page for details.\n\
--clear-env Clear all environment variables before attaching.\n\
container=lxc set.\n\
--keep-env Keep all current environment variables. This\n\
is the current default behaviour, but is likely to\n\
change in the future.\n\
-v, --set-var Set an additional variable that is seen by the\n\
attached program in the container. May be specified\n\
multiple times.\n\
--keep-var Keep an additional environment variable. Only\n\
applicable if --clear-env is specified. May be used\n\
multiple times.\n",
.options = my_longopts,
};
struct wrapargs {
struct lxc_console *console;
int ptyfd;
};
/* Minimalistic login_tty() implementation. */
{
setsid();
return -1;
if (lxc_console_set_stdfds(fd) < 0)
return -1;
if (fd > STDERR_FILENO)
return 0;
}
static int get_pty_on_host_callback(void *p)
{
return -1;
else
return -1;
}
{
int ret = -1;
struct lxc_epoll_descr descr;
struct lxc_tty_state *ts;
INFO("Trying to allocate a pty on the host");
ERROR("stdin is not a tty");
return -1;
}
/* Create pty on the host. */
if (lxc_console_create(conf) < 0)
return -1;
/* Shift ttys to container. */
if (ttys_shift_ids(conf) < 0) {
ERROR("Failed to shift tty into container");
goto err1;
}
/* Send wrapper function on its way. */
goto err1;
if (ret) {
ERROR("failed to create mainloop");
goto err2;
}
/* Register sigwinch handler in mainloop. */
if (ret) {
ERROR("failed to add handler for SIGWINCH fd");
goto err3;
}
/* Register i/o callbacks in mainloop. */
if (ret) {
ERROR("failed to add handler for stdinfd");
goto err3;
}
if (ret) {
ERROR("failed to add handler for masterfd");
goto err3;
}
if (ret) {
ERROR("mainloop returned an error");
goto err3;
}
ret = 0;
err3:
err2:
err1:
return ret;
}
{
int ret = -1;
int wexit = 0;
ret = lxc_caps_init();
if (ret)
if (ret)
if (ret)
if (geteuid()) {
}
}
if (!c)
if (!c->may_control(c)) {
}
if (!c->is_defined(c)) {
}
if (remount_sys_proc)
if (elevated_privileges)
.options = &attach_options};
}
if (isatty(STDIN_FILENO))
else if (isatty(STDOUT_FILENO))
else if (isatty(STDERR_FILENO))
} else {
else
}
if (ret < 0)
goto out;
if (ret < 0)
goto out;
out:
if (ret >= 0)
}