Skip to content

Commit

Permalink
tty: notify about orphan tty-s via rpc
Browse files Browse the repository at this point in the history
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.

opencontainers/runc#1202
travis-ci: success for A few fixes to c/r a docker container with a console (rev3)
Signed-off-by: Andrei Vagin <avagin@virtuozzo.com>
Signed-off-by: Pavel Emelyanov <xemul@virtuozzo.com>
  • Loading branch information
avagin committed Mar 1, 2017
1 parent e4f60ee commit 6afe523
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 14 deletions.
20 changes: 18 additions & 2 deletions criu/action-scripts.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions criu/cr-restore.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
27 changes: 21 additions & 6 deletions criu/cr-service.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -84,10 +87,10 @@ static int recv_criu_msg(int socket_fd, CriuReq **req)
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);

Expand All @@ -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;
}
Expand All @@ -112,6 +119,11 @@ static int send_criu_msg(int socket_fd, CriuResp *msg)
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();
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
6 changes: 5 additions & 1 deletion criu/include/action-scripts.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef __CR_ACTION_SCRIPTS_H__
#define __CR_ACTION_SCRIPTS_H__

#include "asm/int.h"

enum script_actions {
ACT_PRE_DUMP,
ACT_POST_DUMP,
Expand All @@ -12,13 +14,15 @@ enum script_actions {
ACT_POST_SETUP_NS,
ACT_POST_RESUME,
ACT_PRE_RESUME,
ACT_ORPHAN_PTS_MASTER,

ACT_MAX
};

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__ */
1 change: 1 addition & 0 deletions criu/include/cr_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions criu/include/servicefd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down
40 changes: 35 additions & 5 deletions criu/tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "files-reg.h"
#include "namespaces.h"
#include "external.h"
#include "action-scripts.h"

#include "protobuf.h"
#include "util.h"
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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;
}

/*
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions images/rpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6afe523

Please sign in to comment.