lxc_attach.c revision 13bc2fd2370f8e3778d81f58ac8dda5746550a4f
/*
* 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 <stdlib.h>
#include "attach.h"
#include "arguments.h"
#include "config.h"
#include "confile.h"
#include "namespace.h"
#include "caps.h"
#include "log.h"
#include "utils.h"
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 for 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 enivornment 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,
};
{
int ret;
ret = lxc_caps_init();
if (ret)
return 1;
if (ret)
return 1;
if (ret)
return 1;
if (remount_sys_proc)
if (elevated_privileges)
ret = lxc_attach(my_args.name, my_args.lxcpath[0], lxc_attach_run_command, &command, &attach_options, &pid);
} else {
ret = lxc_attach(my_args.name, my_args.lxcpath[0], lxc_attach_run_shell, NULL, &attach_options, &pid);
}
if (ret < 0)
return 1;
if (ret < 0)
return 1;
return WEXITSTATUS(ret);
return 1;
}