Commit 6afe523d authored by Andrei Vagin's avatar Andrei Vagin

tty: notify about orphan tty-s via rpc

Now Docker creates a pty pair from a container devpts to use is as console.
A slave tty is set as a control tty for the init process and bind-mounted
into /dev/console. The master tty is handled externelly.

Now CRIU can handle external resources, but here we have internal resources
which are used externaly.

https://github.com/opencontainers/runc/issues/1202
travis-ci: success for A few fixes to c/r a docker container with a console (rev3)
Signed-off-by: 's avatarAndrei Vagin <avagin@virtuozzo.com>
Signed-off-by: 's avatarPavel Emelyanov <xemul@virtuozzo.com>
parent e4f60eee
......@@ -13,6 +13,9 @@
#include "pstree.h"
#include "common/bug.h"
#include "util.h"
#include <sys/un.h>
#include <sys/socket.h>
#include "common/scm.h"
static const char *action_names[ACT_MAX] = {
[ ACT_PRE_DUMP ] = "pre-dump",
......@@ -25,6 +28,7 @@ static const char *action_names[ACT_MAX] = {
[ ACT_POST_SETUP_NS ] = "post-setup-namespaces",
[ ACT_PRE_RESUME ] = "pre-resume",
[ ACT_POST_RESUME ] = "post-resume",
[ ACT_ORPHAN_PTS_MASTER ] = "orphan-pts-master",
};
struct script {
......@@ -96,6 +100,17 @@ static int run_shell_scripts(const char *action)
return retval;
}
int rpc_send_fd(enum script_actions act, int fd)
{
const char *action = action_names[act];
if (scripts_mode != SCRIPTS_RPC)
return -1;
pr_debug("\tRPC\n");
return send_criu_rpc_script(act, (char *)action, rpc_sk, fd);
}
int run_scripts(enum script_actions act)
{
int ret = 0;
......@@ -108,7 +123,7 @@ int run_scripts(enum script_actions act)
if (scripts_mode == SCRIPTS_RPC) {
pr_debug("\tRPC\n");
ret = send_criu_rpc_script(act, (char *)action, rpc_sk);
ret = send_criu_rpc_script(act, (char *)action, rpc_sk, -1);
goto out;
}
......@@ -146,6 +161,7 @@ int add_rpc_notify(int sk)
BUG_ON(scripts_mode == SCRIPTS_SHELL);
scripts_mode = SCRIPTS_RPC;
rpc_sk = sk;
rpc_sk = install_service_fd(RPC_SK_OFF, sk);
return 0;
}
......@@ -3045,6 +3045,7 @@ static int sigreturn_restore(pid_t pid, struct task_restore_args *task_args, uns
close_proc();
close_service_fd(ROOT_FD_OFF);
close_service_fd(USERNSD_SK);
close_service_fd(RPC_SK_OFF);
__gcov_flush();
......
......@@ -36,6 +36,9 @@
#include "irmap.h"
#include "kerndat.h"
#include "proc_parse.h"
#include <sys/un.h>
#include <sys/socket.h>
#include "common/scm.h"
#include "setproctitle.h"
......@@ -84,10 +87,10 @@ err:
return -1;
}
static int send_criu_msg(int socket_fd, CriuResp *msg)
static int send_criu_msg_with_fd(int socket_fd, CriuResp *msg, int fd)
{
unsigned char *buf;
int len;
int len, ret;
len = criu_resp__get_packed_size(msg);
......@@ -100,7 +103,11 @@ static int send_criu_msg(int socket_fd, CriuResp *msg)
goto err;
}
if (write(socket_fd, buf, len) == -1) {
if (fd >= 0) {
ret = send_fds(socket_fd, NULL, 0, &fd, 1, buf, len);
} else
ret = write(socket_fd, buf, len);
if (ret < 0) {
pr_perror("Can't send response");
goto err;
}
......@@ -112,6 +119,11 @@ err:
return -1;
}
static int send_criu_msg(int socket_fd, CriuResp *msg)
{
return send_criu_msg_with_fd(socket_fd, msg, -1);
}
static void set_resp_err(CriuResp *resp)
{
resp->cr_errno = get_cr_errno();
......@@ -174,7 +186,7 @@ int send_criu_restore_resp(int socket_fd, bool success, int pid)
return send_criu_msg(socket_fd, &msg);
}
int send_criu_rpc_script(enum script_actions act, char *name, int fd)
int send_criu_rpc_script(enum script_actions act, char *name, int sk, int fd)
{
int ret;
CriuResp msg = CRIU_RESP__INIT;
......@@ -201,11 +213,11 @@ int send_criu_rpc_script(enum script_actions act, char *name, int fd)
break;
}
ret = send_criu_msg(fd, &msg);
ret = send_criu_msg_with_fd(sk, &msg, fd);
if (ret < 0)
return ret;
ret = recv_criu_msg(fd, &req);
ret = recv_criu_msg(sk, &req);
if (ret < 0)
return ret;
......@@ -512,6 +524,9 @@ static int setup_opts_from_req(int sk, CriuOpts *req)
goto err;
}
if (req->orphan_pts_master)
opts.orphan_pts_master = true;
if (check_namespace_opts())
goto err;
......
#ifndef __CR_ACTION_SCRIPTS_H__
#define __CR_ACTION_SCRIPTS_H__
#include "asm/int.h"
enum script_actions {
ACT_PRE_DUMP,
ACT_POST_DUMP,
......@@ -12,6 +14,7 @@ enum script_actions {
ACT_POST_SETUP_NS,
ACT_POST_RESUME,
ACT_PRE_RESUME,
ACT_ORPHAN_PTS_MASTER,
ACT_MAX
};
......@@ -19,6 +22,7 @@ enum script_actions {
extern int add_script(char *path);
extern int add_rpc_notify(int sk);
extern int run_scripts(enum script_actions);
extern int send_criu_rpc_script(enum script_actions act, char *name, int arg);
extern int rpc_send_fd(enum script_actions, int fd);
extern int send_criu_rpc_script(enum script_actions act, char *name, int sk, int fd);
#endif /* __CR_ACTION_SCRIPTS_H__ */
......@@ -118,6 +118,7 @@ struct cr_options {
bool display_stats;
bool weak_sysctls;
int status_fd;
bool orphan_pts_master;
};
extern struct cr_options opts;
......
......@@ -20,6 +20,7 @@ enum sfd_type {
USERNSD_SK, /* Socket for usernsd */
NS_FD_OFF, /* Node's net namespace fd */
TRANSPORT_FD_OFF, /* to transfer file descriptors */
RPC_SK_OFF,
SERVICE_FD_MAX
};
......
......@@ -28,6 +28,7 @@
#include "files-reg.h"
#include "namespaces.h"
#include "external.h"
#include "action-scripts.h"
#include "protobuf.h"
#include "util.h"
......@@ -376,7 +377,7 @@ static int tty_verify_active_pairs(void * unused)
continue;
}
if (!opts.shell_job) {
if (!opts.shell_job && !opts.orphan_pts_master) {
pr_err("Found slave peer index %d without "
"correspond master peer\n",
tty_get_index(i));
......@@ -688,7 +689,7 @@ int tty_restore_ctl_terminal(struct file_desc *d, int fd)
else
index = driver->index;
if (is_pty(info->driver)) {
if (is_pty(info->driver) && tty_is_master(info)) {
fake = pty_alloc_fake_slave(info);
if (!fake)
goto err;
......@@ -956,6 +957,32 @@ static int pty_open_unpaired_slave(struct file_desc *d, struct tty_info *slave)
*/
if (likely(slave->inherit)) {
if (opts.orphan_pts_master) {
fake = pty_alloc_fake_master(slave);
if (!fake)
goto err;
master = pty_open_ptmx_index(&fake->d, slave, O_RDWR);
if (master < 0) {
pr_err("Can't open master pty %x (index %d)\n",
slave->tfe->id, slave->tie->pty->index);
goto err;
}
unlock_pty(master);
if (opts.orphan_pts_master &&
rpc_send_fd(ACT_ORPHAN_PTS_MASTER, master) == 0) {
fd = open_tty_reg(slave->reg_d, slave->tfe->flags);
if (fd < 0) {
pr_err("Can't open slave pty %s\n", path_from_reg(slave->reg_d));
goto err;
}
goto out;
}
}
if (!stdin_isatty) {
pr_err("Don't have tty to inherit session from, aborting\n");
return -1;
......@@ -990,6 +1017,7 @@ static int pty_open_unpaired_slave(struct file_desc *d, struct tty_info *slave)
}
out:
if (restore_tty_params(fd, slave))
goto err;
......@@ -1002,7 +1030,7 @@ static int pty_open_unpaired_slave(struct file_desc *d, struct tty_info *slave)
* be already restored properly thus we can simply
* use syscalls instead of lookup via process tree.
*/
if (likely(slave->inherit)) {
if (slave->inherit && opts.shell_job) {
/*
* The restoration procedure only works if we're
* migrating not a session leader, otherwise it's
......@@ -1241,8 +1269,10 @@ static int tty_find_restoring_task(struct tty_info *info)
if (!tty_is_master(info)) {
if (tty_has_active_pair(info))
return 0;
else
else if (!opts.orphan_pts_master)
goto shell_job;
else
info->inherit = true;
}
/*
......@@ -1366,7 +1396,7 @@ static int tty_setup_slavery(void * unused)
info->driver->type == TTY_TYPE__CTTY)
continue;
if (!tty_is_master(info))
if (!tty_is_master(info) && info->link)
continue;
info->ctl_tty = info;
......
......@@ -109,6 +109,7 @@ message criu_opts {
optional bool tcp_skip_in_flight = 46;
optional bool weak_sysctls = 47;
optional int32 status_fd = 49;
optional bool orphan_pts_master = 50;
}
message criu_dump_resp {
......
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