27
27
#include <sys/types.h>
28
28
#include <sys/un.h>
32
#include <lxc/start.h> /* for struct lxc_handler */
30
35
#include "af_unix.h"
35
37
lxc_log_define(lxc_console, lxc);
37
39
extern int lxc_console(const char *name, int ttynum, int *fd)
39
struct sockaddr_un addr = { 0 };
42
snprintf(addr.sun_path, sizeof(addr.sun_path), "@%s", name);
43
addr.sun_path[0] = '\0';
45
sock = lxc_af_unix_connect(addr.sun_path);
47
ERROR("failed to connect to the tty service");
51
ret = lxc_af_unix_send_credential(sock, &ttynum, sizeof(ttynum));
53
SYSERROR("failed to send credentials");
57
ret = lxc_af_unix_recv_fd(sock, fd, &ttynum, sizeof(ttynum));
59
ERROR("failed to connect to the tty");
63
INFO("tty %d allocated", ttynum);
42
struct lxc_command command = {
43
.request = { .type = LXC_COMMAND_TTY, .data = ttynum },
46
ret = lxc_command(name, &command, &stopped);
47
if (ret < 0 && stopped) {
48
ERROR("'%s' is stopped", name);
53
ERROR("failed to send command");
66
58
ERROR("console denied by '%s'", name);
62
if (command.answer.ret) {
63
ERROR("console access denied: %s",
64
strerror(-command.answer.ret));
68
*fd = command.answer.fd;
70
ERROR("unable to allocate fd for tty %d", ttynum);
74
INFO("tty %d allocated", ttynum);
78
/*----------------------------------------------------------------------------
79
* functions used by lxc-start mainloop
80
* to handle above command request.
81
*--------------------------------------------------------------------------*/
82
extern void lxc_console_remove_fd(int fd, struct lxc_tty_info *tty_info)
86
for (i = 0; i < tty_info->nbtty; i++) {
88
if (tty_info->pty_info[i].busy != fd)
91
tty_info->pty_info[i].busy = 0;
97
extern int lxc_console_callback(int fd, struct lxc_request *request,
98
struct lxc_handler *handler)
100
int ttynum = request->data;
101
struct lxc_tty_info *tty_info = &handler->conf.tty_info;
104
if (ttynum > tty_info->nbtty)
107
if (tty_info->pty_info[ttynum - 1].busy)
113
/* fixup index tty1 => [0] */
115
ttynum <= tty_info->nbtty && tty_info->pty_info[ttynum - 1].busy;
118
/* we didn't find any available slot for tty */
119
if (ttynum > tty_info->nbtty)
123
if (lxc_af_unix_send_fd(fd, tty_info->pty_info[ttynum - 1].master,
124
&ttynum, sizeof(ttynum)) < 0) {
125
ERROR("failed to send tty to client");
129
tty_info->pty_info[ttynum - 1].busy = fd;
134
/* the close fd and related cleanup will be done by caller */