Skip to content

Commit

Permalink
commands: s/LXC_CMD_CONSOLE/LXC_CMD_GET_TTY_FD/g
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
  • Loading branch information
Christian Brauner committed Feb 24, 2021
1 parent abb6f65 commit 7e85a2c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
55 changes: 27 additions & 28 deletions src/lxc/commands.c
Expand Up @@ -65,7 +65,7 @@ lxc_log_define(commands, lxc);
static const char *lxc_cmd_str(lxc_cmd_t cmd)
{
static const char *const cmdname[LXC_CMD_MAX] = {
[LXC_CMD_CONSOLE] = "console",
[LXC_CMD_GET_TTY_FD] = "get_tty_fd",
[LXC_CMD_TERMINAL_WINCH] = "terminal_winch",
[LXC_CMD_STOP] = "stop",
[LXC_CMD_GET_STATE] = "get_state",
Expand Down Expand Up @@ -131,14 +131,13 @@ static int __transfer_cgroup_fd(struct unix_fds *fds, struct cgroup_fd *fd)
* the response data is <= a void * worth of data, it will be
* stored directly in data and datalen will be 0.
*
* As a special case, the response for LXC_CMD_CONSOLE is created
* here as it contains an fd for the ptx pty passed through the
* unix socket.
* As a special case, the response for LXC_CMD_GET_TTY_FD is created here as
* it contains an fd for the ptx pty passed through the unix socket.
*/
static int lxc_cmd_rsp_recv(int sock, struct lxc_cmd_rr *cmd)
{
__do_free void *__private_ptr = NULL;
struct lxc_cmd_console_rsp_data *data_console = NULL;
struct lxc_cmd_tty_rsp_data *data_console = NULL;
call_cleaner(put_unix_fds) struct unix_fds *fds = &(struct unix_fds){};
struct lxc_cmd_rsp *rsp = &cmd->rsp;
int cur_cmd = cmd->req.cmd, fret = 0;
Expand All @@ -165,7 +164,7 @@ static int lxc_cmd_rsp_recv(int sock, struct lxc_cmd_rr *cmd)
__fallthrough;
case LXC_CMD_GET_DEVPTS_FD:
__fallthrough;
case LXC_CMD_CONSOLE:
case LXC_CMD_GET_TTY_FD:
fds->fd_count_max = 1;
break;
case LXC_CMD_GET_CGROUP_CTX:
Expand Down Expand Up @@ -238,18 +237,18 @@ static int lxc_cmd_rsp_recv(int sock, struct lxc_cmd_rr *cmd)
/* Don't pointlessly allocate. */
rsp->data = (void *)cmd->req.data;
break;
case LXC_CMD_CONSOLE: /* data */
case LXC_CMD_GET_TTY_FD: /* data */
/*
* recv() returns 0 bytes when a tty cannot be allocated,
* rsp->ret is < 0 when the peer permission check failed
*/
if (ret == 0 || rsp->ret < 0)
return 0;

__private_ptr = malloc(sizeof(struct lxc_cmd_console_rsp_data));
__private_ptr = malloc(sizeof(struct lxc_cmd_tty_rsp_data));
if (!__private_ptr)
return syserrno_set(fret ?: -ENOMEM, "Failed to receive response for command \"%s\"", cur_cmdstr);
data_console = (struct lxc_cmd_console_rsp_data *)__private_ptr;
data_console = (struct lxc_cmd_tty_rsp_data *)__private_ptr;
data_console->ptxfd = move_fd(fds->fd[0]);
data_console->ttynum = PTR_TO_INT(rsp->data);

Expand All @@ -271,9 +270,9 @@ static int lxc_cmd_rsp_recv(int sock, struct lxc_cmd_rr *cmd)
if (rsp->datalen == 0) {
DEBUG("Command \"%s\" requested no additional data", cur_cmdstr);
/*
* Note that LXC_CMD_CONSOLE historically allocates memory to
* return info to the caller. That's why we jump to no_data so
* we ensure that the allocated data is wiped if we return
* Note that LXC_CMD_GET_TTY_FD historically allocates memory
* to return info to the caller. That's why we jump to no_data
* so we ensure that the allocated data is wiped if we return
* early here.
*/
goto no_data;
Expand Down Expand Up @@ -449,13 +448,13 @@ static int lxc_cmd_send(const char *name, struct lxc_cmd_rr *cmd,
*
* Returns the size of the response message on success, < 0 on failure
*
* Note that there is a special case for LXC_CMD_CONSOLE. For this command
* the fd cannot be closed because it is used as a placeholder to indicate
* that a particular tty slot is in use. The fd is also used as a signal to
* the container that when the caller dies or closes the fd, the container
* will notice the fd on its side of the socket in its mainloop select and
* then free the slot with lxc_cmd_fd_cleanup(). The socket fd will be
* returned in the cmd response structure.
* Note that there is a special case for LXC_CMD_GET_TTY_FD. For this command
* the fd cannot be closed because it is used as a placeholder to indicate that
* a particular tty slot is in use. The fd is also used as a signal to the
* container that when the caller dies or closes the fd, the container will
* notice the fd on its side of the socket in its mainloop select and then free
* the slot with lxc_cmd_fd_cleanup(). The socket fd will be returned in the
* cmd response structure.
*/
static int lxc_cmd(const char *name, struct lxc_cmd_rr *cmd, int *stopped,
const char *lxcpath, const char *hashed_sock_name)
Expand All @@ -464,7 +463,7 @@ static int lxc_cmd(const char *name, struct lxc_cmd_rr *cmd, int *stopped,
int ret = -1;
bool stay_connected = false;

if (cmd->req.cmd == LXC_CMD_CONSOLE ||
if (cmd->req.cmd == LXC_CMD_GET_TTY_FD ||
cmd->req.cmd == LXC_CMD_ADD_STATE_CLIENT)
stay_connected = true;

Expand Down Expand Up @@ -1133,7 +1132,7 @@ static int lxc_cmd_terminal_winch_callback(int fd, struct lxc_cmd_req *req,
}

/*
* lxc_cmd_console: Open an fd to a tty in the container
* lxc_cmd_get_tty_fd: Open an fd to a tty in the container
*
* @name : name of container to connect to
* @ttynum : in: the tty to open or -1 for next available
Expand All @@ -1143,13 +1142,13 @@ static int lxc_cmd_terminal_winch_callback(int fd, struct lxc_cmd_req *req,
*
* Returns fd holding tty allocated on success, < 0 on failure
*/
int lxc_cmd_console(const char *name, int *ttynum, int *fd, const char *lxcpath)
int lxc_cmd_get_tty_fd(const char *name, int *ttynum, int *fd, const char *lxcpath)
{
__do_free struct lxc_cmd_console_rsp_data *rspdata = NULL;
__do_free struct lxc_cmd_tty_rsp_data *rspdata = NULL;
int ret, stopped;
struct lxc_cmd_rr cmd = {
.req = {
.cmd = LXC_CMD_CONSOLE,
.cmd = LXC_CMD_GET_TTY_FD,
.data = INT_TO_PTR(*ttynum),
},
};
Expand All @@ -1175,9 +1174,9 @@ int lxc_cmd_console(const char *name, int *ttynum, int *fd, const char *lxcpath)
return log_info(ret, "Alloced fd %d for tty %d via socket %d", *fd, rspdata->ttynum, ret);
}

static int lxc_cmd_console_callback(int fd, struct lxc_cmd_req *req,
struct lxc_handler *handler,
struct lxc_epoll_descr *descr)
static int lxc_cmd_get_tty_fd_callback(int fd, struct lxc_cmd_req *req,
struct lxc_handler *handler,
struct lxc_epoll_descr *descr)
{
int ptxfd, ret;
struct lxc_cmd_rsp rsp = {
Expand Down Expand Up @@ -1846,7 +1845,7 @@ static int lxc_cmd_process(int fd, struct lxc_cmd_req *req,
struct lxc_epoll_descr *);

callback cb[LXC_CMD_MAX] = {
[LXC_CMD_CONSOLE] = lxc_cmd_console_callback,
[LXC_CMD_GET_TTY_FD] = lxc_cmd_get_tty_fd_callback,
[LXC_CMD_TERMINAL_WINCH] = lxc_cmd_terminal_winch_callback,
[LXC_CMD_STOP] = lxc_cmd_stop_callback,
[LXC_CMD_GET_STATE] = lxc_cmd_get_state_callback,
Expand Down
7 changes: 4 additions & 3 deletions src/lxc/commands.h
Expand Up @@ -21,7 +21,7 @@
#define LXC_CMD_REAP_CLIENT_FD 1

typedef enum {
LXC_CMD_CONSOLE = 0,
LXC_CMD_GET_TTY_FD = 0,
LXC_CMD_TERMINAL_WINCH = 1,
LXC_CMD_STOP = 2,
LXC_CMD_GET_STATE = 3,
Expand Down Expand Up @@ -67,7 +67,7 @@ struct lxc_cmd_rr {
struct lxc_cmd_rsp rsp;
};

struct lxc_cmd_console_rsp_data {
struct lxc_cmd_tty_rsp_data {
int ptxfd;
int ttynum;
};
Expand All @@ -81,7 +81,8 @@ struct lxc_cmd_console_log {
};

__hidden extern int lxc_cmd_terminal_winch(const char *name, const char *lxcpath);
__hidden extern int lxc_cmd_console(const char *name, int *ttynum, int *fd, const char *lxcpath);
__hidden extern int lxc_cmd_get_tty_fd(const char *name, int *ttynum, int *fd,
const char *lxcpath);
/*
* Get the 'real' cgroup path (as seen in /proc/self/cgroup) for a container
* for a particular subsystem
Expand Down
4 changes: 2 additions & 2 deletions src/lxc/terminal.c
Expand Up @@ -1078,7 +1078,7 @@ int lxc_terminal_ptx_cb(int fd, uint32_t events, void *cbdata,

int lxc_terminal_getfd(struct lxc_container *c, int *ttynum, int *ptxfd)
{
return lxc_cmd_console(c->name, ttynum, ptxfd, c->config_path);
return lxc_cmd_get_tty_fd(c->name, ttynum, ptxfd, c->config_path);
}

int lxc_console(struct lxc_container *c, int ttynum,
Expand All @@ -1094,7 +1094,7 @@ int lxc_console(struct lxc_container *c, int ttynum,
};
int istty = 0;

ttyfd = lxc_cmd_console(c->name, &ttynum, &ptxfd, c->config_path);
ttyfd = lxc_cmd_get_tty_fd(c->name, &ttynum, &ptxfd, c->config_path);
if (ttyfd < 0)
return -1;

Expand Down

0 comments on commit 7e85a2c

Please sign in to comment.