Unverified Commit e08bdcbc by Serge Hallyn Committed by GitHub

Merge pull request #2061 from brauner/2017-12-23/attach_allocate_pty_in_api

attach: add LXC_ATTACH_ALLOCATE_PTY
parents e7d7dd73 c2229b24
...@@ -58,7 +58,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ...@@ -58,7 +58,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
<arg choice="opt">-R, --remount-sys-proc</arg> <arg choice="opt">-R, --remount-sys-proc</arg>
<arg choice="opt">--keep-env</arg> <arg choice="opt">--keep-env</arg>
<arg choice="opt">--clear-env</arg> <arg choice="opt">--clear-env</arg>
<arg choice="opt">-L, --pty-log <replaceable>file</replaceable></arg>
<arg choice="opt">-v, --set-var <replaceable>variable</replaceable></arg> <arg choice="opt">-v, --set-var <replaceable>variable</replaceable></arg>
<arg choice="opt">--keep-var <replaceable>variable</replaceable></arg> <arg choice="opt">--keep-var <replaceable>variable</replaceable></arg>
<arg choice="opt">-- <replaceable>command</replaceable></arg> <arg choice="opt">-- <replaceable>command</replaceable></arg>
...@@ -258,22 +257,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ...@@ -258,22 +257,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
<varlistentry> <varlistentry>
<term> <term>
<option>-L, --pty-log <replaceable>file</replaceable></option>
</term>
<listitem>
<para>
Specify a file where the output of <command>lxc-attach</command> will be
logged.
</para>
<para>
<emphasis>Important:</emphasis> When a standard file descriptor
does not refer to a pty output produced on it will not be logged.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<option>-v, --set-var <replaceable>variable</replaceable></option> <option>-v, --set-var <replaceable>variable</replaceable></option>
</term> </term>
<listitem> <listitem>
......
...@@ -51,6 +51,7 @@ enum { ...@@ -51,6 +51,7 @@ enum {
LXC_ATTACH_LSM_NOW = 0x00020000, /*!< FIXME: unknown */ LXC_ATTACH_LSM_NOW = 0x00020000, /*!< FIXME: unknown */
/* Set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges. */ /* Set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges. */
LXC_ATTACH_NO_NEW_PRIVS = 0x00040000, /*!< PR_SET_NO_NEW_PRIVS */ LXC_ATTACH_NO_NEW_PRIVS = 0x00040000, /*!< PR_SET_NO_NEW_PRIVS */
LXC_ATTACH_ALLOCATE_PTY = 0x00080000, /*!< Allocate new pty for attached process. */
/* We have 16 bits for things that are on by default and 16 bits that /* We have 16 bits for things that are on by default and 16 bits that
* are off by default, that should be sufficient to keep binary * are off by default, that should be sufficient to keep binary
...@@ -131,6 +132,9 @@ typedef struct lxc_attach_options_t { ...@@ -131,6 +132,9 @@ typedef struct lxc_attach_options_t {
int stdout_fd; /*!< stdout file descriptor */ int stdout_fd; /*!< stdout file descriptor */
int stderr_fd; /*!< stderr file descriptor */ int stderr_fd; /*!< stderr file descriptor */
/**@}*/ /**@}*/
/*! File descriptor to log output. */
int log_fd;
} lxc_attach_options_t; } lxc_attach_options_t;
/*! Default attach options to use */ /*! Default attach options to use */
...@@ -145,7 +149,10 @@ typedef struct lxc_attach_options_t { ...@@ -145,7 +149,10 @@ typedef struct lxc_attach_options_t {
/* .env_policy = */ LXC_ATTACH_KEEP_ENV, \ /* .env_policy = */ LXC_ATTACH_KEEP_ENV, \
/* .extra_env_vars = */ NULL, \ /* .extra_env_vars = */ NULL, \
/* .extra_keep_env = */ NULL, \ /* .extra_keep_env = */ NULL, \
/* .stdin_fd = */ 0, 1, 2 \ /* .stdin_fd = */ 0, \
/* .stdout_fd = */ 1, \
/* .stderr_fd = */ 2, \
/* .log_fd = */ -EBADF, \
} }
/*! /*!
......
...@@ -73,6 +73,7 @@ ...@@ -73,6 +73,7 @@
#include "cgroup.h" #include "cgroup.h"
#include "conf.h" #include "conf.h"
#include "confile_utils.h" #include "confile_utils.h"
#include "console.h"
#include "error.h" #include "error.h"
#include "log.h" #include "log.h"
#include "lxclock.h" #include "lxclock.h"
...@@ -3091,24 +3092,6 @@ int chown_mapped_root(const char *path, struct lxc_conf *conf) ...@@ -3091,24 +3092,6 @@ int chown_mapped_root(const char *path, struct lxc_conf *conf)
return ret; return ret;
} }
int lxc_ttys_shift_ids(struct lxc_conf *c)
{
if (lxc_list_empty(&c->id_map))
return 0;
if (!strcmp(c->console.name, ""))
return 0;
if (chown_mapped_root(c->console.name, c) < 0) {
ERROR("failed to chown console \"%s\"", c->console.name);
return -1;
}
TRACE("chowned console \"%s\"", c->console.name);
return 0;
}
/* NOTE: Must not be called from inside the container namespace! */ /* NOTE: Must not be called from inside the container namespace! */
int lxc_create_tmp_proc_mount(struct lxc_conf *conf) int lxc_create_tmp_proc_mount(struct lxc_conf *conf)
{ {
...@@ -3720,11 +3703,7 @@ void lxc_conf_free(struct lxc_conf *conf) ...@@ -3720,11 +3703,7 @@ void lxc_conf_free(struct lxc_conf *conf)
return; return;
if (current_config == conf) if (current_config == conf)
current_config = NULL; current_config = NULL;
free(conf->console.buffer_log_file); lxc_pty_conf_free(&conf->console);
free(conf->console.log_path);
free(conf->console.path);
if (conf->console.buffer_size > 0 && conf->console.ringbuf.addr)
lxc_ringbuf_release(&conf->console.ringbuf);
free(conf->rootfs.mount); free(conf->rootfs.mount);
free(conf->rootfs.bdev_type); free(conf->rootfs.bdev_type);
free(conf->rootfs.options); free(conf->rootfs.options);
......
...@@ -174,18 +174,19 @@ struct lxc_console { ...@@ -174,18 +174,19 @@ struct lxc_console {
char name[MAXPATHLEN]; char name[MAXPATHLEN];
struct termios *tios; struct termios *tios;
struct lxc_tty_state *tty_state; struct lxc_tty_state *tty_state;
struct /* lxc_console_ringbuf */ {
/* size of the ringbuffer */
uint64_t buffer_size;
/* size of the ringbuffer */ /* path to the log file for the ringbuffer */
uint64_t buffer_size; char *buffer_log_file;
/* path to the log file for the ringbuffer */ /* fd to the log file for the ringbuffer */
char *buffer_log_file; int buffer_log_file_fd;
/* fd to the log file for the ringbuffer */ /* the in-memory ringbuffer */
int buffer_log_file_fd; struct lxc_ringbuf ringbuf;
};
/* the in-memory ringbuffer */
struct lxc_ringbuf ringbuf;
}; };
/* /*
...@@ -451,7 +452,6 @@ extern int find_unmapped_nsid(struct lxc_conf *conf, enum idtype idtype); ...@@ -451,7 +452,6 @@ extern int find_unmapped_nsid(struct lxc_conf *conf, enum idtype idtype);
extern int mapped_hostid(unsigned id, struct lxc_conf *conf, extern int mapped_hostid(unsigned id, struct lxc_conf *conf,
enum idtype idtype); enum idtype idtype);
extern int chown_mapped_root(const char *path, struct lxc_conf *conf); extern int chown_mapped_root(const char *path, struct lxc_conf *conf);
extern int lxc_ttys_shift_ids(struct lxc_conf *c);
extern int userns_exec_1(struct lxc_conf *conf, int (*fn)(void *), void *data, extern int userns_exec_1(struct lxc_conf *conf, int (*fn)(void *), void *data,
const char *fn_name); const char *fn_name);
extern int userns_exec_full(struct lxc_conf *conf, int (*fn)(void *), extern int userns_exec_full(struct lxc_conf *conf, int (*fn)(void *),
......
...@@ -168,7 +168,7 @@ struct lxc_tty_state *lxc_console_signal_init(int srcfd, int dstfd) ...@@ -168,7 +168,7 @@ struct lxc_tty_state *lxc_console_signal_init(int srcfd, int dstfd)
goto on_error; goto on_error;
} }
ts->sigfd = signalfd(-1, &mask, 0); ts->sigfd = signalfd(-1, &mask, SFD_CLOEXEC);
if (ts->sigfd < 0) { if (ts->sigfd < 0) {
WARN("Failed to create signal fd"); WARN("Failed to create signal fd");
sigprocmask(SIG_SETMASK, &ts->oldmask, NULL); sigprocmask(SIG_SETMASK, &ts->oldmask, NULL);
...@@ -285,31 +285,25 @@ static int lxc_console_mainloop_add_peer(struct lxc_console *console) ...@@ -285,31 +285,25 @@ static int lxc_console_mainloop_add_peer(struct lxc_console *console)
return 0; return 0;
} }
extern int lxc_console_mainloop_add(struct lxc_epoll_descr *descr, int lxc_console_mainloop_add(struct lxc_epoll_descr *descr,
struct lxc_conf *conf) struct lxc_console *console)
{ {
int ret; int ret;
struct lxc_console *console = &conf->console;
if (!conf->rootfs.path) {
INFO("no rootfs, no console.");
return 0;
}
if (console->master < 0) { if (console->master < 0) {
INFO("no console"); INFO("no console");
return 0; return 0;
} }
if (lxc_mainloop_add_handler(descr, console->master, ret = lxc_mainloop_add_handler(descr, console->master,
lxc_console_cb_con, console)) { lxc_console_cb_con, console);
ERROR("failed to add to mainloop console handler for '%d'", if (ret < 0) {
console->master); ERROR("Failed to add handler for %d to mainloop", console->master);
return -1; return -1;
} }
/* we cache the descr so that we can add an fd to it when someone /* We cache the descr so that we can add an fd to it when someone
* does attach to it in lxc_console_allocate() * does attach to it in lxc_console_allocate().
*/ */
console->descr = descr; console->descr = descr;
ret = lxc_console_mainloop_add_peer(console); ret = lxc_console_mainloop_add_peer(console);
...@@ -709,24 +703,13 @@ int lxc_console_create_log_file(struct lxc_console *console) ...@@ -709,24 +703,13 @@ int lxc_console_create_log_file(struct lxc_console *console)
return 0; return 0;
} }
int lxc_console_create(struct lxc_conf *conf) int lxc_pty_create(struct lxc_console *console)
{ {
int ret, saved_errno; int ret, saved_errno;
struct lxc_console *console = &conf->console;
if (!conf->rootfs.path) {
INFO("Container does not have a rootfs. The console will be "
"shared with the host");
return 0;
}
if (console->path && !strcmp(console->path, "none")) {
INFO("No console was requested");
return 0;
}
process_lock(); process_lock();
ret = openpty(&console->master, &console->slave, console->name, NULL, NULL); ret = openpty(&console->master, &console->slave, console->name, NULL,
NULL);
saved_errno = errno; saved_errno = errno;
process_unlock(); process_unlock();
if (ret < 0) { if (ret < 0) {
...@@ -752,6 +735,33 @@ int lxc_console_create(struct lxc_conf *conf) ...@@ -752,6 +735,33 @@ int lxc_console_create(struct lxc_conf *conf)
goto err; goto err;
} }
return 0;
err:
lxc_console_delete(console);
return -ENODEV;
}
int lxc_console_create(struct lxc_conf *conf)
{
int ret;
struct lxc_console *console = &conf->console;
if (!conf->rootfs.path) {
INFO("Container does not have a rootfs. The console will be "
"shared with the host");
return 0;
}
if (console->path && !strcmp(console->path, "none")) {
INFO("No console was requested");
return 0;
}
ret = lxc_pty_create(console);
if (ret < 0)
return -1;
/* create console log file */ /* create console log file */
ret = lxc_console_create_log_file(console); ret = lxc_console_create_log_file(console);
if (ret < 0) if (ret < 0)
...@@ -969,3 +979,84 @@ close_fds: ...@@ -969,3 +979,84 @@ close_fds:
return ret; return ret;
} }
int lxc_make_controlling_pty(int fd)
{
int ret;
setsid();
ret = ioctl(fd, TIOCSCTTY, (char *)NULL);
if (ret < 0)
return -1;
return 0;
}
int lxc_login_pty(int fd)
{
int ret;
ret = lxc_make_controlling_pty(fd);
if (ret < 0)
return -1;
ret = lxc_console_set_stdfds(fd);
if (ret < 0)
return -1;
if (fd > STDERR_FILENO)
close(fd);
return 0;
}
void lxc_pty_info_init(struct lxc_pty_info *pty)
{
pty->name[0] = '\0';
pty->master = -EBADF;
pty->slave = -EBADF;
pty->busy = -1;
}
void lxc_pty_init(struct lxc_console *pty)
{
memset(pty, 0, sizeof(*pty));
pty->slave = -EBADF;
pty->master = -EBADF;
pty->peer = -EBADF;
pty->log_fd = -EBADF;
pty->buffer_log_file_fd = -EBADF;
lxc_pty_info_init(&pty->peerpty);
}
void lxc_pty_conf_free(struct lxc_console *console)
{
free(console->buffer_log_file);
free(console->log_path);
free(console->path);
if (console->buffer_size > 0 && console->ringbuf.addr)
lxc_ringbuf_release(&console->ringbuf);
}
int lxc_pty_map_ids(struct lxc_conf *c, struct lxc_console *pty)
{
int ret;
if (lxc_list_empty(&c->id_map))
return 0;
ret = strcmp(pty->name, "");
if (ret == 0)
return 0;
ret = chown_mapped_root(pty->name, c);
if (ret < 0) {
ERROR("Failed to chown \"%s\"", pty->name);
return -1;
}
TRACE("Chowned \"%s\"", pty->name);
return 0;
}
...@@ -82,6 +82,12 @@ extern int lxc_console_allocate(struct lxc_conf *conf, int sockfd, int *ttynum) ...@@ -82,6 +82,12 @@ extern int lxc_console_allocate(struct lxc_conf *conf, int sockfd, int *ttynum)
* automatically chowned to the uid/gid of the unprivileged user. For this * automatically chowned to the uid/gid of the unprivileged user. For this
* ttys_shift_ids() can be called.) * ttys_shift_ids() can be called.)
*/ */
extern int lxc_pty_create(struct lxc_console *console);
/**
* lxc_console_create: Create a new pty.
* - In addition to lxc_pty_create() also sets up all pty logs.
*/
extern int lxc_console_create(struct lxc_conf *); extern int lxc_console_create(struct lxc_conf *);
/* /*
...@@ -108,7 +114,7 @@ extern void lxc_console_free(struct lxc_conf *conf, int fd); ...@@ -108,7 +114,7 @@ extern void lxc_console_free(struct lxc_conf *conf, int fd);
/* /*
* Register pty event handlers in an open mainloop * Register pty event handlers in an open mainloop
*/ */
extern int lxc_console_mainloop_add(struct lxc_epoll_descr *, struct lxc_conf *); extern int lxc_console_mainloop_add(struct lxc_epoll_descr *, struct lxc_console *);
/* /*
* Handle SIGWINCH events on the allocated ptys. * Handle SIGWINCH events on the allocated ptys.
...@@ -226,4 +232,11 @@ extern int lxc_console_create_log_file(struct lxc_console *console); ...@@ -226,4 +232,11 @@ extern int lxc_console_create_log_file(struct lxc_console *console);
extern int lxc_console_cb_con(int fd, uint32_t events, void *data, extern int lxc_console_cb_con(int fd, uint32_t events, void *data,
struct lxc_epoll_descr *descr); struct lxc_epoll_descr *descr);
extern int lxc_make_controlling_pty(int fd);
extern int lxc_login_pty(int fd);
extern void lxc_pty_conf_free(struct lxc_console *console);
extern void lxc_pty_info_init(struct lxc_pty_info *pty);
extern void lxc_pty_init(struct lxc_console *pty);
extern int lxc_pty_map_ids(struct lxc_conf *c, struct lxc_console *pty);
#endif #endif
...@@ -266,35 +266,36 @@ restart: ...@@ -266,35 +266,36 @@ restart:
static int setup_signal_fd(sigset_t *oldmask) static int setup_signal_fd(sigset_t *oldmask)
{ {
int ret, sig;
sigset_t mask; sigset_t mask;
int fd; int signals[] = {SIGBUS, SIGILL, SIGSEGV, SIGWINCH};
/* Block everything except serious error signals. */ /* Block everything except serious error signals. */
if (sigfillset(&mask) || ret = sigfillset(&mask);
sigdelset(&mask, SIGILL) || if (ret < 0)
sigdelset(&mask, SIGSEGV) || return -EBADF;
sigdelset(&mask, SIGBUS) ||
sigdelset(&mask, SIGWINCH) || for (sig = 0; sig < (sizeof(signals) / sizeof(signals[0])); sig++) {
sigprocmask(SIG_BLOCK, &mask, oldmask)) { ret = sigdelset(&mask, signals[sig]);
SYSERROR("Failed to set signal mask."); if (ret < 0)
return -1; return -EBADF;
} }
fd = signalfd(-1, &mask, 0); ret = sigprocmask(SIG_BLOCK, &mask, oldmask);
if (fd < 0) { if (ret < 0) {
SYSERROR("Failed to create signal file descriptor."); SYSERROR("Failed to set signal mask");
return -1; return -EBADF;
} }
if (fcntl(fd, F_SETFD, FD_CLOEXEC)) { ret = signalfd(-1, &mask, SFD_CLOEXEC);
SYSERROR("Failed to set FD_CLOEXEC on the signal file descriptor: %d.", fd); if (ret < 0) {
close(fd); SYSERROR("Failed to create signal file descriptor");
return -1; return -EBADF;
} }
DEBUG("Set SIGCHLD handler with file descriptor: %d.", fd); TRACE("Created signal file descriptor %d", ret);
return fd; return ret;
} }
static int signal_handler(int fd, uint32_t events, void *data, static int signal_handler(int fd, uint32_t events, void *data,
...@@ -468,6 +469,7 @@ int lxc_set_state(const char *name, struct lxc_handler *handler, ...@@ -468,6 +469,7 @@ int lxc_set_state(const char *name, struct lxc_handler *handler,
int lxc_poll(const char *name, struct lxc_handler *handler) int lxc_poll(const char *name, struct lxc_handler *handler)
{ {
int ret; int ret;
bool has_console = (handler->conf->rootfs.path != NULL);
struct lxc_epoll_descr descr, descr_console; struct lxc_epoll_descr descr, descr_console;
ret = lxc_mainloop_open(&descr); ret = lxc_mainloop_open(&descr);
...@@ -476,10 +478,12 @@ int lxc_poll(const char *name, struct lxc_handler *handler) ...@@ -476,10 +478,12 @@ int lxc_poll(const char *name, struct lxc_handler *handler)
goto out_sigfd; goto out_sigfd;
} }
ret = lxc_mainloop_open(&descr_console); if (has_console) {
if (ret < 0) { ret = lxc_mainloop_open(&descr_console);
ERROR("Failed to create console mainloop"); if (ret < 0) {
goto out_mainloop; ERROR("Failed to create console mainloop");
goto out_mainloop;
}
} }
ret = lxc_mainloop_add_handler(&descr, handler->sigfd, signal_handler, handler); ret = lxc_mainloop_add_handler(&descr, handler->sigfd, signal_handler, handler);
...@@ -488,16 +492,20 @@ int lxc_poll(const char *name, struct lxc_handler *handler) ...@@ -488,16 +492,20 @@ int lxc_poll(const char *name, struct lxc_handler *handler)
goto out_mainloop_console; goto out_mainloop_console;
} }
ret = lxc_console_mainloop_add(&descr, handler->conf); if (has_console) {
if (ret < 0) { struct lxc_console *console = &handler->conf->console;
ERROR("Failed to add console handlers to mainloop");
goto out_mainloop_console;
}
ret = lxc_console_mainloop_add(&descr_console, handler->conf); ret = lxc_console_mainloop_add(&descr, console);
if (ret < 0) { if (ret < 0) {
ERROR("Failed to add console handlers to console mainloop"); ERROR("Failed to add console handlers to mainloop");
goto out_mainloop_console; goto out_mainloop_console;
}
ret = lxc_console_mainloop_add(&descr_console, console);
if (ret < 0) {
ERROR("Failed to add console handlers to console mainloop");
goto out_mainloop_console;
}
} }
ret = lxc_cmd_mainloop_add(name, &descr, handler); ret = lxc_cmd_mainloop_add(name, &descr, handler);
...@@ -514,15 +522,19 @@ int lxc_poll(const char *name, struct lxc_handler *handler) ...@@ -514,15 +522,19 @@ int lxc_poll(const char *name, struct lxc_handler *handler)
if (ret < 0 || !handler->init_died) if (ret < 0 || !handler->init_died)
goto out_mainloop; goto out_mainloop;
ret = lxc_mainloop(&descr_console, 0); if (has_console)
ret = lxc_mainloop(&descr_console, 0);
out_mainloop: out_mainloop:
lxc_mainloop_close(&descr); lxc_mainloop_close(&descr);
TRACE("Closed mainloop"); TRACE("Closed mainloop");
out_mainloop_console: out_mainloop_console:
lxc_mainloop_close(&descr_console); if (has_console) {
TRACE("Closed console mainloop"); lxc_mainloop_close(&descr_console);
TRACE("Closed console mainloop");
}
out_sigfd: out_sigfd:
close(handler->sigfd); close(handler->sigfd);
...@@ -739,13 +751,15 @@ int lxc_init(const char *name, struct lxc_handler *handler) ...@@ -739,13 +751,15 @@ int lxc_init(const char *name, struct lxc_handler *handler)
TRACE("set up signal fd"); TRACE("set up signal fd");
/* Do this after setting up signals since it might unblock SIGWINCH. */ /* Do this after setting up signals since it might unblock SIGWINCH. */
if (lxc_console_create(conf)) { ret = lxc_console_create(conf);
ERROR("Failed to create console for container \"%s\".", name); if (ret < 0) {
ERROR("Failed to create console");
goto out_restore_sigmask; goto out_restore_sigmask;
} }
TRACE("created console"); TRACE("Created console");
if (lxc_ttys_shift_ids(conf) < 0) { ret = lxc_pty_map_ids(conf, &conf->console);
if (ret < 0) {
ERROR("Failed to shift tty into container."); ERROR("Failed to shift tty into container.");
goto out_restore_sigmask; goto out_restore_sigmask;
} }
......
...@@ -27,11 +27,12 @@ ...@@ -27,11 +27,12 @@
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <termios.h>
#include <unistd.h>
#include <lxc/lxccontainer.h> #include <lxc/lxccontainer.h>
...@@ -46,12 +47,6 @@ ...@@ -46,12 +47,6 @@
#include "mainloop.h" #include "mainloop.h"
#include "utils.h" #include "utils.h"
#if HAVE_PTY_H
#include <pty.h>
#else
#include <../include/openpty.h>
#endif
static const struct option my_longopts[] = { static const struct option my_longopts[] = {
{"elevated-privileges", optional_argument, 0, 'e'}, {"elevated-privileges", optional_argument, 0, 'e'},
{"arch", required_argument, 0, 'a'}, {"arch", required_argument, 0, 'a'},
...@@ -241,154 +236,29 @@ Options :\n\ ...@@ -241,154 +236,29 @@ Options :\n\
.checker = NULL, .checker = NULL,
}; };
struct wrapargs { static bool stdfd_is_pty(void)
lxc_attach_options_t *options;
lxc_attach_command_t *command;
struct lxc_console *console;
int ptyfd;
};
/* Minimalistic login_tty() implementation. */
static int login_pty(int fd)
{
setsid();
if (ioctl(fd, TIOCSCTTY, NULL) < 0)
return -1;
if (lxc_console_set_stdfds(fd) < 0)
return -1;
if (fd > STDERR_FILENO)
close(fd);
return 0;
}
static int get_pty_on_host_callback(void *p)
{ {
struct wrapargs *wrap = p; if (isatty(STDIN_FILENO))
return true;
close(wrap->console->master); if (isatty(STDOUT_FILENO))
if (login_pty(wrap->console->slave) < 0) return true;
return -1; if (isatty(STDERR_FILENO))
return true;
if (wrap->command->program) return false;
lxc_attach_run_command(wrap->command);
else
lxc_attach_run_shell(NULL);
return -1;
} }
static int get_pty_on_host(struct lxc_container *c, struct wrapargs *wrap, int *pid) int lxc_attach_create_log_file(const char *log_file)
{ {
struct lxc_epoll_descr descr; int fd;
struct lxc_conf *conf;
struct lxc_tty_state *ts;
int ret = -1;
struct wrapargs *args = wrap;
if (!isatty(args->ptyfd)) {
fprintf(stderr, "Standard file descriptor does not refer to a pty\n");
return -1;
}
if (c->lxc_conf) {
conf = c->lxc_conf;
} else {
/* If the container is not defined and the user didn't specify a
* config file to load we will simply init a dummy config here.
*/
conf = lxc_conf_init();
if (!conf) {
fprintf(stderr, "Failed to allocate dummy config file for the container\n");
return -1;
}
/* We also need a dummy rootfs path otherwise fd = open(log_file, O_CLOEXEC | O_RDWR | O_CREAT | O_APPEND, 0600);
* lxc_console_create() will not let us create a console. Note, if (fd < 0) {
* I don't want this change to make it into fprintf(stderr, "Failed to open log file \"%s\"\n", log_file);
* lxc_console_create()'s since this function will only be
* responsible for proper /dev/{console,tty<n>} devices.
* lxc-attach is just abusing it to also handle the pty case
* because it is very similar. However, with LXC 3.0 lxc-attach
* will need to move away from using lxc_console_create() since
* this is actually an internal symbol and we only want the
* tools to use the API with LXC 3.0.
*/
conf->rootfs.path = strdup("dummy");
if (!conf->rootfs.path)
return -1;
}
free(conf->console.log_path);
if (my_args.console_log)
conf->console.log_path = strdup(my_args.console_log);
else
conf->console.log_path = NULL;
/* In the case of lxc-attach our peer pty will always be the current
* controlling terminal. We clear whatever was set by the user for
* lxc.console.path here and set it NULL. lxc_console_peer_default()
* will then try to open /dev/tty. If the process doesn't have a
* controlling terminal we should still proceed.
*/
free(conf->console.path);
conf->console.path = NULL;
/* Create pty on the host. */
if (lxc_console_create(conf) < 0)
return -1; return -1;
ts = conf->console.tty_state;
conf->console.descr = &descr;
/* Shift ttys to container. */
if (lxc_ttys_shift_ids(conf) < 0) {
fprintf(stderr, "Failed to shift tty into container\n");
goto err1;
}
/* Send wrapper function on its way. */
wrap->console = &conf->console;
if (c->attach(c, get_pty_on_host_callback, wrap, wrap->options, pid) < 0)
goto err1;
close(conf->console.slave); /* Close slave side. */
conf->console.slave = -1;
ret = lxc_mainloop_open(&descr);
if (ret) {
fprintf(stderr, "failed to create mainloop\n");
goto err2;
} }
if (lxc_console_mainloop_add(&descr, conf) < 0) { return fd;
fprintf(stderr, "Failed to add handlers to lxc mainloop.\n");
goto err3;
}
ret = lxc_mainloop(&descr, -1);
if (ret) {
fprintf(stderr, "mainloop returned an error\n");
goto err3;
}
ret = 0;
err3:
lxc_mainloop_close(&descr);
err2:
if (ts && ts->sigfd != -1)
lxc_console_signal_fini(ts);
err1:
lxc_console_delete(&conf->console);
return ret;
}
static int stdfd_is_pty(void)
{
if (isatty(STDIN_FILENO))
return STDIN_FILENO;
if (isatty(STDOUT_FILENO))
return STDOUT_FILENO;
if (isatty(STDERR_FILENO))
return STDERR_FILENO;
return -1;
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
...@@ -462,6 +332,8 @@ int main(int argc, char *argv[]) ...@@ -462,6 +332,8 @@ int main(int argc, char *argv[])
attach_options.attach_flags |= LXC_ATTACH_REMOUNT_PROC_SYS; attach_options.attach_flags |= LXC_ATTACH_REMOUNT_PROC_SYS;
if (elevated_privileges) if (elevated_privileges)
attach_options.attach_flags &= ~(elevated_privileges); attach_options.attach_flags &= ~(elevated_privileges);
if (stdfd_is_pty())
attach_options.attach_flags |= LXC_ATTACH_ALLOCATE_PTY;
attach_options.namespaces = namespace_flags; attach_options.namespaces = namespace_flags;
attach_options.personality = new_personality; attach_options.personality = new_personality;
attach_options.env_policy = env_policy; attach_options.env_policy = env_policy;
...@@ -473,29 +345,17 @@ int main(int argc, char *argv[]) ...@@ -473,29 +345,17 @@ int main(int argc, char *argv[])
command.argv = (char**)my_args.argv; command.argv = (char**)my_args.argv;
} }
struct wrapargs wrap = (struct wrapargs){ if (my_args.console_log) {
.command = &command, attach_options.log_fd = lxc_attach_create_log_file(my_args.console_log);
.options = &attach_options if (attach_options.log_fd < 0)
};
wrap.ptyfd = stdfd_is_pty();
if (wrap.ptyfd >= 0) {
if ((!isatty(STDOUT_FILENO) || !isatty(STDERR_FILENO)) && my_args.console_log) {
fprintf(stderr, "-L/--pty-log can only be used when stdout and stderr refer to a pty.\n");
goto out;
}
ret = get_pty_on_host(c, &wrap, &pid);
} else {
if (my_args.console_log) {
fprintf(stderr, "-L/--pty-log can only be used when stdout and stderr refer to a pty.\n");
goto out; goto out;
}
if (command.program)
ret = c->attach(c, lxc_attach_run_command, &command, &attach_options, &pid);
else
ret = c->attach(c, lxc_attach_run_shell, NULL, &attach_options, &pid);
} }
if (command.program)
ret = c->attach(c, lxc_attach_run_command, &command, &attach_options, &pid);
else
ret = c->attach(c, lxc_attach_run_shell, NULL, &attach_options, &pid);
if (ret < 0) if (ret < 0)
goto out; goto out;
......
...@@ -278,6 +278,14 @@ int main(int argc, char *argv[]) ...@@ -278,6 +278,14 @@ int main(int argc, char *argv[])
} }
} }
if (my_args.console)
if (!c->set_config_item(c, "lxc.console.path", my_args.console))
goto out;
if (my_args.console_log)
if (!c->set_config_item(c, "lxc.console.logfile", my_args.console_log))
goto out;
if (!lxc_setup_shared_ns(&my_args, c)) if (!lxc_setup_shared_ns(&my_args, c))
goto out; goto out;
......
...@@ -2396,7 +2396,7 @@ int parse_byte_size_string(const char *s, int64_t *converted) ...@@ -2396,7 +2396,7 @@ int parse_byte_size_string(const char *s, int64_t *converted)
int64_t mltpl, overflow; int64_t mltpl, overflow;
char *end; char *end;
char dup[LXC_NUMSTRLEN64 + 2]; char dup[LXC_NUMSTRLEN64 + 2];
char suffix[3]; char suffix[3] = {0};
if (!s || !strcmp(s, "")) if (!s || !strcmp(s, ""))
return -EINVAL; return -EINVAL;
...@@ -2412,10 +2412,10 @@ int parse_byte_size_string(const char *s, int64_t *converted) ...@@ -2412,10 +2412,10 @@ int parse_byte_size_string(const char *s, int64_t *converted)
else else
return -EINVAL; return -EINVAL;
if ((end - 2) == dup && !isdigit(*(end - 2))) if (suffix_len > 0 && (end - 2) == dup && !isdigit(*(end - 2)))
return -EINVAL; return -EINVAL;
if (isalpha(*(end - 2))) { if (suffix_len > 0 && isalpha(*(end - 2))) {
if (suffix_len == 1) if (suffix_len == 1)
suffix_len++; suffix_len++;
else else
......
...@@ -19,16 +19,18 @@ ...@@ -19,16 +19,18 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/ */
#include <lxc/lxccontainer.h> #include <errno.h>
#include "lxc/utils.h"
#include "lxc/lsm/lsm.h"
#include <sys/types.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/syscall.h> #include <sys/syscall.h>
#include <errno.h> #include <sys/types.h>
#include "lxctest.h"
#include "utils.h"
#include "lsm/lsm.h"
#include <lxc/lxccontainer.h>
#define TSTNAME "lxc-attach-test" #define TSTNAME "lxc-attach-test"
#define TSTOUT(fmt, ...) do { \ #define TSTOUT(fmt, ...) do { \
...@@ -392,19 +394,60 @@ err1: ...@@ -392,19 +394,60 @@ err1:
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int ret; int i, ret;
struct lxc_log log;
char template[sizeof(P_tmpdir"/attach_XXXXXX")];
int fret = EXIT_FAILURE;
strcpy(template, P_tmpdir"/attach_XXXXXX");
i = lxc_make_tmpfile(template, false);
if (i < 0) {
lxc_error("Failed to create temporary log file for container %s\n", TSTNAME);
exit(EXIT_FAILURE);
} else {
lxc_debug("Using \"%s\" as temporary log file for container %s\n", template, TSTNAME);
close(i);
}
log.name = TSTNAME;
log.file = template;
log.level = "TRACE";
log.prefix = "attach";
log.quiet = false;
log.lxcpath = NULL;
if (lxc_log_init(&log))
goto on_error;
test_lsm_detect(); test_lsm_detect();
ret = test_attach(NULL, TSTNAME, "busybox"); ret = test_attach(NULL, TSTNAME, "busybox");
if (ret < 0) if (ret < 0)
return EXIT_FAILURE; goto on_error;
TSTOUT("\n"); TSTOUT("\n");
ret = test_attach(LXCPATH "/alternate-path-test", TSTNAME, "busybox"); ret = test_attach(LXCPATH "/alternate-path-test", TSTNAME, "busybox");
if (ret < 0) if (ret < 0)
return EXIT_FAILURE; goto on_error;
(void)rmdir(LXCPATH "/alternate-path-test");
TSTOUT("All tests passed\n"); TSTOUT("All tests passed\n");
return EXIT_SUCCESS; fret = EXIT_SUCCESS;
on_error:
if (fret != EXIT_SUCCESS) {
int fd;
fd = open(template, O_RDONLY);
if (fd >= 0) {
char buf[4096];
ssize_t buflen;
while ((buflen = read(fd, buf, 1024)) > 0) {
buflen = write(STDERR_FILENO, buf, buflen);
if (buflen <= 0)
break;
}
close(fd);
}
}
(void)rmdir(LXCPATH "/alternate-path-test");
(void)unlink(template);
exit(fret);
} }
...@@ -190,17 +190,6 @@ fi ...@@ -190,17 +190,6 @@ fi
rm -f $out $err rm -f $out $err
if [ $allocate_pty = "pty" ]; then
# Test whether logging pty output to a file works.
trap "rm -f /tmp/ptylog" EXIT INT QUIT PIPE
lxc-attach -n busy -L /tmp/ptylog -- hostname || FAIL "to allocate or setup pty"
if [ ! -s /tmp/ptylog ]; then
FAIL "lxc-attach -n busy -L /tmp/ptylog -- hostname"
fi
rm -f /tmp/ptylog
fi
lxc-destroy -n busy -f lxc-destroy -n busy -f
exit 0 exit 0
...@@ -180,6 +180,7 @@ void test_detect_ramfs_rootfs(void) ...@@ -180,6 +180,7 @@ void test_detect_ramfs_rootfs(void)
} }
fclose(fp1); fclose(fp1);
fp1 = NULL; fp1 = NULL;
fd1 = -1;
/* Test if it correctly fails to detect when no - rootfs rootfs */ /* Test if it correctly fails to detect when no - rootfs rootfs */
for (i = 0; i < sizeof(mountinfo) / sizeof(mountinfo[0]); i++) { for (i = 0; i < sizeof(mountinfo) / sizeof(mountinfo[0]); i++) {
...@@ -192,6 +193,7 @@ void test_detect_ramfs_rootfs(void) ...@@ -192,6 +193,7 @@ void test_detect_ramfs_rootfs(void)
} }
fclose(fp2); fclose(fp2);
fp2 = NULL; fp2 = NULL;
fd2 = -1;
if (mount(tmpf1, "/proc/self/mountinfo", NULL, MS_BIND, 0) < 0) { if (mount(tmpf1, "/proc/self/mountinfo", NULL, MS_BIND, 0) < 0) {
lxc_error("%s\n", "Could not overmount \"/proc/self/mountinfo\"."); lxc_error("%s\n", "Could not overmount \"/proc/self/mountinfo\".");
...@@ -386,80 +388,108 @@ void test_parse_byte_size_string(void) ...@@ -386,80 +388,108 @@ void test_parse_byte_size_string(void)
int64_t n; int64_t n;
ret = parse_byte_size_string("0", &n); ret = parse_byte_size_string("0", &n);
if (ret < 0) if (ret < 0) {
lxc_error("%s\n", "Failed to parse \"0\"");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
if (n != 0) }
if (n != 0) {
lxc_error("%s\n", "Failed to parse \"0\"");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
}
ret = parse_byte_size_string("1", &n); ret = parse_byte_size_string("1", &n);
if (ret < 0) if (ret < 0) {
lxc_error("%s\n", "Failed to parse \"1\"");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
if (n != 1) }
if (n != 1) {
lxc_error("%s\n", "Failed to parse \"1\"");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
}
ret = parse_byte_size_string("1 ", &n); ret = parse_byte_size_string("1 ", &n);
if (ret == 0) if (ret == 0) {
lxc_error("%s\n", "Failed to parse \"1 \"");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
}
ret = parse_byte_size_string("1B", &n); ret = parse_byte_size_string("1B", &n);
if (ret < 0) if (ret < 0) {
lxc_error("%s\n", "Failed to parse \"1B\"");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
if (n != 1) }
if (n != 1) {
lxc_error("%s\n", "Failed to parse \"1B\"");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
}
ret = parse_byte_size_string("1kB", &n); ret = parse_byte_size_string("1kB", &n);
if (ret < 0) if (ret < 0) {
lxc_error("%s\n", "Failed to parse \"1kB\"");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
if (n != 1024) }
if (n != 1024) {
lxc_error("%s\n", "Failed to parse \"1kB\"");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
}
ret = parse_byte_size_string("1MB", &n); ret = parse_byte_size_string("1MB", &n);
if (ret < 0) if (ret < 0) {
exit(EXIT_FAILURE); lxc_error("%s\n", "Failed to parse \"1MB\"");
if (n != 1048576)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
}
ret = parse_byte_size_string("1GB", &n); if (n != 1048576) {
if (ret < 0) lxc_error("%s\n", "Failed to parse \"1MB\"");
exit(EXIT_FAILURE);
if (n != 1073741824)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
}
ret = parse_byte_size_string("1TB", &n); ret = parse_byte_size_string("1TB", &n);
if (ret == 0) if (ret == 0) {
lxc_error("%s\n", "Failed to parse \"1TB\"");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
}
ret = parse_byte_size_string("1 B", &n); ret = parse_byte_size_string("1 B", &n);
if (ret < 0) if (ret < 0) {
lxc_error("%s\n", "Failed to parse \"1 B\"");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
if (n != 1) }
if (n != 1) {
lxc_error("%s\n", "Failed to parse \"1 B\"");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
}
ret = parse_byte_size_string("1 kB", &n); ret = parse_byte_size_string("1 kB", &n);
if (ret < 0) if (ret < 0) {
lxc_error("%s\n", "Failed to parse \"1 kB\"");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
if (n != 1024) }
if (n != 1024) {
lxc_error("%s\n", "Failed to parse \"1 kB\"");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
}
ret = parse_byte_size_string("1 MB", &n); ret = parse_byte_size_string("1 MB", &n);
if (ret < 0) if (ret < 0) {
exit(EXIT_FAILURE); lxc_error("%s\n", "Failed to parse \"1 MB\"");
if (n != 1048576)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
}
ret = parse_byte_size_string("1 GB", &n); if (n != 1048576) {
if (ret < 0) lxc_error("%s\n", "Failed to parse \"1 MB\"");
exit(EXIT_FAILURE);
if (n != 1073741824)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
}
ret = parse_byte_size_string("1 TB", &n); ret = parse_byte_size_string("1 TB", &n);
if (ret == 0) if (ret == 0) {
lxc_error("%s\n", "Failed to parse \"1 TB\"");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
}
ret = parse_byte_size_string("asdf", &n); ret = parse_byte_size_string("asdf", &n);
if (ret == 0) if (ret == 0) {
lxc_error("%s\n", "Failed to parse \"asdf\"");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
}
} }
void test_lxc_config_net_hwaddr(void) void test_lxc_config_net_hwaddr(void)
......
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