lxc_attach.c revision e13eeea2db3743bf8d3fe2833e069a80e2c4102c
/*
* lxc: linux Container library
*
* (C) Copyright IBM Corp. 2007, 2010
*
* Authors:
* Daniel Lezcano <dlezcano at fr.ibm.com>
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define _GNU_SOURCE
#include <unistd.h>
#include <errno.h>
#include <pwd.h>
#include <stdlib.h>
#include <sys/personality.h>
#include "attach.h"
#include "commands.h"
#include "arguments.h"
#include "caps.h"
#include "cgroup.h"
#include "confile.h"
#include "start.h"
#include "sync.h"
#include "log.h"
#include "namespace.h"
static const struct option my_longopts[] = {
};
static int elevated_privileges = 0;
static signed long new_personality = -1;
static int namespace_flags = -1;
{
int ret;
switch (c) {
case 'a':
if (new_personality < 0) {
return -1;
}
break;
case 's':
namespace_flags = 0;
if (ret)
return -1;
/* -s implies -e */
elevated_privileges = 1;
break;
}
return 0;
}
static struct lxc_arguments my_args = {
.progname = "lxc-attach",
.help = "\
--name=NAME\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\n\
Use elevated privileges (capabilities, cgroup\n\
restrictions) instead of those of the container.\n\
WARNING: This may leak privleges 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, it may therefore\n\
leak privileges into the container. Use with care.\n",
.options = my_longopts,
};
{
int ret;
struct lxc_proc_context_info *init_ctx;
struct lxc_handler *handler;
void *cgroup_data = NULL;
char *curdir;
ret = lxc_caps_init();
if (ret)
return ret;
if (ret)
return ret;
if (ret)
return ret;
if (init_pid < 0) {
ERROR("failed to get the init pid");
return -1;
}
if (!init_ctx) {
return -1;
}
if (!elevated_privileges) {
* be available inside the container or we may not have
* the required permissions anymore
*/
if (ret < 0) {
ERROR("failed to prepare attaching to cgroup");
return -1;
}
}
/* determine which namespaces the container was created with
* by asking lxc-start
*/
if (namespace_flags == -1) {
/* call failed */
if (namespace_flags == -1) {
ERROR("failed to automatically determine the "
"namespaces which the container unshared");
return -1;
}
}
/* we need to attach before we fork since certain namespaces
* (such as pid namespaces) only really affect children of the
* current process and not the process itself
*/
if (ret < 0) {
ERROR("failed to enter the namespace");
return -1;
}
/* hack: we need sync.h infrastructure - and that needs a handler */
if (lxc_sync_init(handler)) {
ERROR("failed to initialize synchronization socket");
return -1;
}
if (pid < 0) {
SYSERROR("failed to fork");
return -1;
}
if (pid) {
int status;
/* wait until the child has done configuring itself before
* we put it in a cgroup that potentially limits these
* possibilities */
return -1;
/* now that we are done with all privileged operations,
* we can add ourselves to the cgroup. Since we smuggled in
* the fds earlier, we still have write permission
*/
if (!elevated_privileges) {
/* since setns() for pid namespaces only really
* affects child processes, the pid we have is
* still valid outside the container, so this is
* fine
*/
if (ret < 0) {
ERROR("failed to attach process to cgroup");
return -1;
}
}
/* tell the child we are done initializing */
return -1;
goto again;
return -1;
}
return WEXITSTATUS(status);
return -1;
}
if (!pid) {
if (new_personality < 0)
ERROR("could not ensure correct architecture: %s",
return -1;
}
ERROR("could not drop privileges");
return -1;
}
/* tell parent we are done setting up the container and wait
* until we have been put in the container's cgroup, if
* applicable */
return -1;
return -1;
}
if (!passwd) {
SYSERROR("failed to get passwd " \
"entry for uid '%d'", uid);
return -1;
}
{
char *const args[] = {
NULL,
};
return -1;
}
}
return 0;
}