Unverified Commit 2ed797da by Serge Hallyn Committed by GitHub

Merge pull request #1884 from brauner/2017-10-28/move_tools_to_api_only

confile: add lxc.namespace.<namespace-key> + add user namespace sharing + rework start logic
parents 190f9aee 686dd5d1
......@@ -835,7 +835,7 @@ static int lxc_cmd_get_name_callback(int fd, struct lxc_cmd_req *req,
memset(&rsp, 0, sizeof(rsp));
rsp.data = handler->name;
rsp.data = (char *)handler->name;
rsp.datalen = strlen(handler->name) + 1;
rsp.ret = 0;
......
......@@ -2461,21 +2461,19 @@ struct lxc_conf *lxc_conf_init(void)
lxc_list_init(&new->aliens);
lxc_list_init(&new->environment);
lxc_list_init(&new->limits);
for (i=0; i<NUM_LXC_HOOKS; i++)
for (i = 0; i < NUM_LXC_HOOKS; i++)
lxc_list_init(&new->hooks[i]);
lxc_list_init(&new->groups);
new->lsm_aa_profile = NULL;
new->lsm_se_context = NULL;
new->tmp_umount_proc = 0;
for (i = 0; i < LXC_NS_MAX; i++)
new->inherit_ns_fd[i] = -1;
/* if running in a new user namespace, init and COMMAND
* default to running as UID/GID 0 when using lxc-execute */
new->init_uid = 0;
new->init_gid = 0;
memset(&new->cgroup_meta, 0, sizeof(struct lxc_cgroup));
memset(&new->inherit_ns, 0, sizeof(char *) * LXC_NS_MAX);
return new;
}
......@@ -3155,7 +3153,7 @@ int lxc_setup_child(struct lxc_handler *handler)
return -1;
}
if (lxc_conf->inherit_ns_fd[LXC_NS_UTS] == -1) {
if (handler->nsfd[LXC_NS_UTS] == -1) {
if (setup_utsname(lxc_conf->utsname)) {
ERROR("failed to setup the utsname for '%s'", name);
return -1;
......@@ -3674,7 +3672,7 @@ int userns_exec_1(struct lxc_conf *conf, int (*fn)(void *), void *data,
struct lxc_list *it;
struct id_map *map;
char c = '1';
int ret = -1;
int ret = -1, status = -1;
struct lxc_list *idmap = NULL, *tmplist = NULL;
struct id_map *container_root_uid = NULL, *container_root_gid = NULL,
*host_uid_map = NULL, *host_gid_map = NULL;
......@@ -3844,10 +3842,11 @@ int userns_exec_1(struct lxc_conf *conf, int (*fn)(void *), void *data,
goto on_error;
}
on_error:
/* Wait for child to finish. */
ret = wait_for_pid(pid);
if (pid > 0)
status = wait_for_pid(pid);
on_error:
if (idmap)
lxc_free_idmap(idmap);
if (container_root_uid)
......@@ -3863,6 +3862,9 @@ on_error:
close(p[0]);
close(p[1]);
if (status < 0)
ret = -1;
return ret;
}
......@@ -4026,10 +4028,11 @@ int userns_exec_full(struct lxc_conf *conf, int (*fn)(void *), void *data,
goto on_error;
}
on_error:
/* Wait for child to finish. */
ret = wait_for_pid(pid);
if (pid > 0)
ret = wait_for_pid(pid);
on_error:
if (idmap)
lxc_free_idmap(idmap);
if (host_uid_map && (host_uid_map != container_root_uid))
......
......@@ -285,8 +285,6 @@ struct lxc_conf {
int loglevel; /* loglevel as specifed in config (if any) */
int logfd;
int inherit_ns_fd[LXC_NS_MAX];
unsigned int start_auto;
unsigned int start_delay;
int start_order;
......@@ -348,6 +346,8 @@ struct lxc_conf {
* that union.
*/
struct lxc_cgroup cgroup_meta;
char *inherit_ns[LXC_NS_MAX];
};
#ifdef HAVE_TLS
......
......@@ -102,6 +102,7 @@ lxc_config_define(monitor);
lxc_config_define(mount);
lxc_config_define(mount_auto);
lxc_config_define(mount_fstab);
lxc_config_define(namespace);
lxc_config_define(net);
lxc_config_define(net_flags);
lxc_config_define(net_hwaddr);
......@@ -178,6 +179,7 @@ static struct lxc_config_t config[] = {
{ "lxc.mount.auto", false, set_config_mount_auto, get_config_mount_auto, clr_config_mount_auto, },
{ "lxc.mount.entry", false, set_config_mount, get_config_mount, clr_config_mount, },
{ "lxc.mount.fstab", false, set_config_mount_fstab, get_config_mount_fstab, clr_config_mount_fstab, },
{ "lxc.namespace", false, set_config_namespace, get_config_namespace, clr_config_namespace, },
/* [START]: REMOVE IN LXC 3.0 */
{ "lxc.network.type", true, set_config_network_legacy_type, get_config_network_legacy_item, clr_config_network_legacy_item, },
......@@ -1976,6 +1978,23 @@ static int set_config_uts_name(const char *key, const char *value,
return 0;
}
static int set_config_namespace(const char *key, const char *value,
struct lxc_conf *lxc_conf, void *data)
{
int ns_idx;
const char *namespace;
if (lxc_config_value_empty(value))
return clr_config_namespace(key, lxc_conf, data);
namespace = key + sizeof("lxc.namespace.") - 1;
ns_idx = lxc_namespace_2_ns_idx(namespace);
if (ns_idx < 0)
return ns_idx;
return set_config_string_item(&lxc_conf->inherit_ns[ns_idx], value);
}
struct parse_line_conf {
struct lxc_conf *conf;
bool from_include;
......@@ -3268,6 +3287,28 @@ static int get_config_noop(const char *key, char *retv, int inlen,
return 0;
}
static int get_config_namespace(const char *key, char *retv, int inlen,
struct lxc_conf *c, void *data)
{
int len, ns_idx;
const char *namespace;
int fulllen = 0;
if (!retv)
inlen = 0;
else
memset(retv, 0, inlen);
namespace = key + sizeof("lxc.namespace.") - 1;
ns_idx = lxc_namespace_2_ns_idx(namespace);
if (ns_idx < 0)
return ns_idx;
strprint(retv, inlen, "%s", c->inherit_ns[ns_idx]);
return fulllen;
}
/* Callbacks to clear config items. */
static inline int clr_config_personality(const char *key, struct lxc_conf *c,
void *data)
......@@ -3606,6 +3647,23 @@ static inline int clr_config_noop(const char *key, struct lxc_conf *c,
return 0;
}
static int clr_config_namespace(const char *key, struct lxc_conf *lxc_conf,
void *data)
{
int ns_idx;
const char *namespace;
namespace = key + sizeof("lxc.namespace.") - 1;
ns_idx = lxc_namespace_2_ns_idx(namespace);
if (ns_idx < 0)
return ns_idx;
free(lxc_conf->inherit_ns[ns_idx]);
lxc_conf->inherit_ns[ns_idx] = NULL;
return 0;
}
static int get_config_includefiles(const char *key, char *retv, int inlen,
struct lxc_conf *c, void *data)
{
......
......@@ -29,8 +29,9 @@
#include "confile.h"
#include "confile_utils.h"
#include "error.h"
#include "log.h"
#include "list.h"
#include "log.h"
#include "lxccontainer.h"
#include "network.h"
#include "parse.h"
#include "utils.h"
......@@ -700,3 +701,77 @@ bool parse_limit_value(const char **value, rlim_t *res)
return true;
}
static int lxc_container_name_to_pid(const char *lxcname_or_pid,
const char *lxcpath)
{
int ret;
signed long int pid;
char *err = NULL;
pid = strtol(lxcname_or_pid, &err, 10);
if (*err != '\0' || pid < 1) {
struct lxc_container *c;
c = lxc_container_new(lxcname_or_pid, lxcpath);
if (!c) {
ERROR("\"%s\" is not a valid pid nor a container name",
lxcname_or_pid);
return -1;
}
if (!c->may_control(c)) {
ERROR("Insufficient privileges to control container "
"\"%s\"", c->name);
lxc_container_put(c);
return -1;
}
pid = c->init_pid(c);
if (pid < 1) {
ERROR("Container \"%s\" is not running", c->name);
lxc_container_put(c);
return -1;
}
lxc_container_put(c);
}
ret = kill(pid, 0);
if (ret < 0) {
ERROR("%s - Failed to send signal to pid %d", strerror(errno),
(int)pid);
return -EPERM;
}
return pid;
}
int lxc_inherit_namespace(const char *lxcname_or_pid, const char *lxcpath,
const char *namespace)
{
int fd, pid;
char *dup, *lastslash;
lastslash = strrchr(lxcname_or_pid, '/');
if (lastslash) {
dup = strdup(lxcname_or_pid);
if (!dup)
return -ENOMEM;
*lastslash = '\0';
pid = lxc_container_name_to_pid(lastslash, dup);
free(dup);
} else {
pid = lxc_container_name_to_pid(lxcname_or_pid, lxcpath);
}
if (pid < 0)
return -EINVAL;
fd = lxc_preserve_ns(pid, namespace);
if (fd < 0)
return -EINVAL;
return fd;
}
......@@ -86,5 +86,7 @@ extern int lxc_get_conf_str(char *retv, int inlen, const char *value);
extern int lxc_get_conf_int(struct lxc_conf *c, char *retv, int inlen, int v);
extern int lxc_get_conf_uint64(struct lxc_conf *c, char *retv, int inlen, uint64_t v);
extern bool parse_limit_value(const char **value, rlim_t *res);
extern int lxc_inherit_namespace(const char *lxcname_or_pid,
const char *lxcpath, const char *namespace);
#endif /* __LXC_CONFILE_UTILS_H */
......@@ -105,10 +105,10 @@ static void lxc_monitor_fifo_send(struct lxc_msg *msg, const char *lxcpath)
/* It is normal for this open() to fail with ENXIO when there is
* no monitor running, so we don't log it.
*/
if (errno == ENXIO)
if (errno == ENXIO || errno == ENOENT)
return;
WARN("Failed to open fifo to send message: %s.", strerror(errno));
WARN("%s - Failed to open fifo to send message", strerror(errno));
return;
}
......
......@@ -96,15 +96,26 @@ const struct ns_info ns_info[LXC_NS_MAX] = {
[LXC_NS_CGROUP] = {"cgroup", CLONE_NEWCGROUP, "CLONE_NEWCGROUP"}
};
int lxc_namespace_2_cloneflag(char *namespace)
int lxc_namespace_2_cloneflag(const char *namespace)
{
int i;
for (i = 0; i < LXC_NS_MAX; i++)
if (!strcasecmp(ns_info[i].proc_name, namespace))
return ns_info[i].clone_flag;
ERROR("Invalid namespace name: %s.", namespace);
return -1;
ERROR("Invalid namespace name \"%s\"", namespace);
return -EINVAL;
}
int lxc_namespace_2_ns_idx(const char *namespace)
{
int i;
for (i = 0; i < LXC_NS_MAX; i++)
if (!strcmp(ns_info[i].proc_name, namespace))
return i;
ERROR("Invalid namespace name \"%s\"", namespace);
return -EINVAL;
}
int lxc_fill_namespace_flags(char *flaglist, int *flags)
......
......@@ -81,7 +81,8 @@ int clone(int (*fn)(void *), void *child_stack,
extern pid_t lxc_clone(int (*fn)(void *), void *arg, int flags);
extern int lxc_namespace_2_cloneflag(char *namespace);
extern int lxc_namespace_2_cloneflag(const char *namespace);
extern int lxc_namespace_2_ns_idx(const char *namespace);
extern int lxc_fill_namespace_flags(char *flaglist, int *flags);
#endif
......@@ -2050,7 +2050,7 @@ int lxc_find_gateway_addresses(struct lxc_handler *handler)
}
#define LXC_USERNIC_PATH LIBEXECDIR "/lxc/lxc-user-nic"
static int lxc_create_network_unpriv_exec(const char *lxcpath, char *lxcname,
static int lxc_create_network_unpriv_exec(const char *lxcpath, const char *lxcname,
struct lxc_netdev *netdev, pid_t pid)
{
int ret;
......@@ -2126,13 +2126,13 @@ static int lxc_create_network_unpriv_exec(const char *lxcpath, char *lxcname,
if (bytes < 0) {
SYSERROR("Failed to read from pipe file descriptor");
close(pipefd[0]);
return -1;
} else {
buffer[bytes - 1] = '\0';
}
buffer[bytes - 1] = '\0';
ret = wait_for_pid(child);
close(pipefd[0]);
if (ret != 0) {
if (ret != 0 || bytes < 0) {
ERROR("lxc-user-nic failed to configure requested network: %s",
buffer[0] != '\0' ? buffer : "(null)");
return -1;
......@@ -2194,7 +2194,7 @@ static int lxc_create_network_unpriv_exec(const char *lxcpath, char *lxcname,
return 0;
}
static int lxc_delete_network_unpriv_exec(const char *lxcpath, char *lxcname,
static int lxc_delete_network_unpriv_exec(const char *lxcpath, const char *lxcname,
struct lxc_netdev *netdev,
const char *netns_path)
{
......@@ -2267,19 +2267,18 @@ static int lxc_delete_network_unpriv_exec(const char *lxcpath, char *lxcname,
if (bytes < 0) {
SYSERROR("Failed to read from pipe file descriptor.");
close(pipefd[0]);
return -1;
} else {
buffer[bytes - 1] = '\0';
}
buffer[bytes - 1] = '\0';
if (wait_for_pid(child) != 0) {
ret = wait_for_pid(child);
close(pipefd[0]);
if (ret != 0 || bytes < 0) {
ERROR("lxc-user-nic failed to delete requested network: %s",
buffer[0] != '\0' ? buffer : "(null)");
close(pipefd[0]);
return -1;
}
close(pipefd[0]);
return 0;
}
......@@ -2302,14 +2301,14 @@ bool lxc_delete_network_unpriv(struct lxc_handler *handler)
*netns_path = '\0';
if (handler->netnsfd < 0) {
if (handler->nsfd[LXC_NS_NET] < 0) {
DEBUG("Cannot not guarantee safe deletion of network devices. "
"Manual cleanup maybe needed");
return false;
}
ret = snprintf(netns_path, sizeof(netns_path), "/proc/%d/fd/%d",
getpid(), handler->netnsfd);
getpid(), handler->nsfd[LXC_NS_NET]);
if (ret < 0 || ret >= sizeof(netns_path))
return false;
......@@ -2408,7 +2407,7 @@ int lxc_create_network_priv(struct lxc_handler *handler)
return 0;
}
int lxc_network_move_created_netdev_priv(const char *lxcpath, char *lxcname,
int lxc_network_move_created_netdev_priv(const char *lxcpath, const char *lxcname,
struct lxc_list *network, pid_t pid)
{
int ret;
......@@ -2448,7 +2447,7 @@ int lxc_network_move_created_netdev_priv(const char *lxcpath, char *lxcname,
return 0;
}
int lxc_create_network_unpriv(const char *lxcpath, char *lxcname,
int lxc_create_network_unpriv(const char *lxcpath, const char *lxcname,
struct lxc_list *network, pid_t pid)
{
struct lxc_list *iterator;
......@@ -2621,7 +2620,7 @@ int lxc_restore_phys_nics_to_netns(struct lxc_handler *handler)
int oldfd;
char ifname[IFNAMSIZ];
struct lxc_list *iterator;
int netnsfd = handler->netnsfd;
int netnsfd = handler->nsfd[LXC_NS_NET];
struct lxc_conf *conf = handler->conf;
/* We need CAP_NET_ADMIN in the parent namespace in order to setns() to
......
......@@ -263,12 +263,12 @@ extern int setup_private_host_hw_addr(char *veth1);
extern int netdev_get_mtu(int ifindex);
extern int lxc_create_network_priv(struct lxc_handler *handler);
extern int lxc_network_move_created_netdev_priv(const char *lxcpath,
char *lxcname,
const char *lxcname,
struct lxc_list *network,
pid_t pid);
extern void lxc_delete_network(struct lxc_handler *handler);
extern int lxc_find_gateway_addresses(struct lxc_handler *handler);
extern int lxc_create_network_unpriv(const char *lxcpath, char *lxcname,
extern int lxc_create_network_unpriv(const char *lxcpath, const char *lxcname,
struct lxc_list *network, pid_t pid);
extern int lxc_requests_empty_network(struct lxc_handler *handler);
extern int lxc_restore_phys_nics_to_netns(struct lxc_handler *handler);
......
......@@ -40,9 +40,6 @@ struct lxc_handler {
/* The clone flags that were requested. */
int clone_flags;
/* File descriptors referring to the network namespace of the container. */
int netnsfd;
/* File descriptor to pin the rootfs for privileged containers. */
int pinfd;
......@@ -67,10 +64,10 @@ struct lxc_handler {
/* Socketpair to synchronize processes during container creation. */
int sync_sock[2];
/* The name of the container. */
char *name;
/* Pointer to the name of the container. Do not free! */
const char *name;
/* The path the container is running in. */
/* Pointer to the path the container. Do not free! */
const char *lxcpath;
/* Whether the container's startup process euid is 0. */
......
......@@ -89,53 +89,6 @@ err:
return err;
}
static int pid_from_lxcname(const char *lxcname_or_pid, const char *lxcpath) {
char *eptr;
int pid = strtol(lxcname_or_pid, &eptr, 10);
if (*eptr != '\0' || pid < 1) {
struct lxc_container *s;
s = lxc_container_new(lxcname_or_pid, lxcpath);
if (!s) {
SYSERROR("'%s' is not a valid pid nor a container name", lxcname_or_pid);
return -1;
}
if (!s->may_control(s)) {
SYSERROR("Insufficient privileges to control container '%s'", s->name);
lxc_container_put(s);
return -1;
}
pid = s->init_pid(s);
if (pid < 1) {
SYSERROR("Is container '%s' running?", s->name);
lxc_container_put(s);
return -1;
}
lxc_container_put(s);
}
if (kill(pid, 0) < 0) {
SYSERROR("Can't send signal to pid %d", pid);
return -1;
}
return pid;
}
static int open_ns(int pid, const char *ns_proc_name) {
int fd;
char path[MAXPATHLEN];
snprintf(path, MAXPATHLEN, "/proc/%d/ns/%s", pid, ns_proc_name);
fd = open(path, O_RDONLY);
if (fd < 0) {
SYSERROR("failed to open %s", path);
return -1;
}
return fd;
}
static int my_parser(struct lxc_arguments* args, int c, char* arg)
{
switch (c) {
......@@ -201,16 +154,18 @@ Options :\n\
int main(int argc, char *argv[])
{
int err = EXIT_FAILURE;
int i;
struct lxc_conf *conf;
struct lxc_log log;
const char *lxcpath;
char *const *args;
struct lxc_container *c;
int err = EXIT_FAILURE;
char *rcfile = NULL;
char *const default_args[] = {
"/sbin/init",
NULL,
};
struct lxc_container *c;
lxc_list_init(&defines);
......@@ -236,14 +191,13 @@ int main(int argc, char *argv[])
exit(err);
lxc_log_options_no_override();
if (access(my_args.lxcpath[0], O_RDONLY) < 0) {
lxcpath = my_args.lxcpath[0];
if (access(lxcpath, O_RDONLY) < 0) {
if (!my_args.quiet)
fprintf(stderr, "You lack access to %s\n", my_args.lxcpath[0]);
fprintf(stderr, "You lack access to %s\n", lxcpath);
exit(err);
}
const char *lxcpath = my_args.lxcpath[0];
/* REMOVE IN LXC 3.0 */
setenv("LXC_UPDATE_CONFIG_FORMAT", "1", 0);
......@@ -326,16 +280,6 @@ int main(int argc, char *argv[])
goto out;
}
if (ensure_path(&conf->console.path, my_args.console) < 0) {
ERROR("failed to ensure console path '%s'", my_args.console);
goto out;
}
if (ensure_path(&conf->console.log_path, my_args.console_log) < 0) {
ERROR("failed to ensure console log '%s'", my_args.console_log);
goto out;
}
if (my_args.pidfile != NULL) {
if (ensure_path(&c->pidfile, my_args.pidfile) < 0) {
ERROR("failed to ensure pidfile '%s'", my_args.pidfile);
......@@ -343,19 +287,26 @@ int main(int argc, char *argv[])
}
}
int i;
for (i = 0; i < LXC_NS_MAX; i++) {
if (my_args.share_ns[i] == NULL)
const char *key, *value;
value = my_args.share_ns[i];
if (!value)
continue;
int pid = pid_from_lxcname(my_args.share_ns[i], lxcpath);
if (pid < 1)
goto out;
if (i == LXC_NS_NET)
key = "lxc.namespace.net";
else if (i == LXC_NS_IPC)
key = "lxc.namespace.ipc";
else if (i == LXC_NS_UTS)
key = "lxc.namespace.uts";
else if (i == LXC_NS_PID)
key = "lxc.namespace.pid";
else
continue;
int fd = open_ns(pid, ns_info[i].proc_name);
if (fd < 0)
if (!c->set_config_item(c, key, value))
goto out;
conf->inherit_ns_fd[i] = fd;
}
if (!my_args.daemonize) {
......
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