[CRIU] [PATCH 1/5] tty: notify about orphan tty-s via rpc
Andrei Vagin
avagin at virtuozzo.com
Fri Feb 10 17:08:51 PST 2017
On Fri, Feb 10, 2017 at 01:41:21PM +0300, Pavel Emelyanov wrote:
> On 02/07/2017 11:43 AM, Andrei Vagin wrote:
> > From: Andrei Vagin <avagin at virtuozzo.com>
> >
> > 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
> > Signed-off-by: Andrei Vagin <avagin at virtuozzo.com>
> > ---
> > criu/action-scripts.c | 20 ++++++++++++++++++--
> > criu/cr-restore.c | 1 +
> > criu/cr-service.c | 27 +++++++++++++++++++++------
> > criu/include/action-scripts.h | 6 +++++-
> > criu/include/cr_options.h | 1 +
> > criu/include/servicefd.h | 1 +
> > criu/tty.c | 40 +++++++++++++++++++++++++++++++++++-----
> > images/rpc.proto | 1 +
> > 8 files changed, 83 insertions(+), 14 deletions(-)
> >
> > diff --git a/criu/action-scripts.c b/criu/action-scripts.c
> > index 1a12311..4fa8e28 100644
> > --- a/criu/action-scripts.c
> > +++ b/criu/action-scripts.c
> > @@ -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)
>
> Can we keep this one in cr-service.c? It has nothing to do with
> action scripts...
action_names and scripts_mode are not avaliable out of this file
>
> > +{
> > + 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;
> > }
> > diff --git a/criu/cr-restore.c b/criu/cr-restore.c
> > index ee6b848..9fa9bc3 100644
> > --- a/criu/cr-restore.c
> > +++ b/criu/cr-restore.c
> > @@ -3197,6 +3197,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();
> >
> > diff --git a/criu/cr-service.c b/criu/cr-service.c
> > index 4bac50d..130e2a7 100644
> > --- a/criu/cr-service.c
> > +++ b/criu/cr-service.c
> > @@ -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;
> >
> > @@ -511,6 +523,9 @@ static int setup_opts_from_req(int sk, CriuOpts *req)
> > }
> > }
> >
> > + if (req->orphan_pts_master)
> > + opts.orphan_pts_master = true;
> > +
> > if (check_namespace_opts())
> > goto err;
> >
> > diff --git a/criu/include/action-scripts.h b/criu/include/action-scripts.h
> > index 4421169..40b09b1 100644
> > --- a/criu/include/action-scripts.h
> > +++ b/criu/include/action-scripts.h
> > @@ -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,
> > @@ -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__ */
> > diff --git a/criu/include/cr_options.h b/criu/include/cr_options.h
> > index aaefea3..365315e 100644
> > --- a/criu/include/cr_options.h
> > +++ b/criu/include/cr_options.h
> > @@ -120,6 +120,7 @@ struct cr_options {
> > bool weak_sysctls;
> > bool check_only;
> > int status_fd;
> > + bool orphan_pts_master;
> > };
> >
> > extern struct cr_options opts;
> > diff --git a/criu/include/servicefd.h b/criu/include/servicefd.h
> > index 5152fb6..1b48e90 100644
> > --- a/criu/include/servicefd.h
> > +++ b/criu/include/servicefd.h
> > @@ -21,6 +21,7 @@ enum sfd_type {
> > NS_FD_OFF, /* Node's net namespace fd */
> > TRANSPORT_FD_OFF, /* to transfer file descriptors */
> > LAZY_PAGES_SK_OFF, /* socket for communication with lazy-pages daemon */
> > + RPC_SK_OFF,
>
> Need explaining comment.
Ok
>
> >
> > SERVICE_FD_MAX
> > };
> > diff --git a/criu/tty.c b/criu/tty.c
> > index 17d19f4..5bfce44 100644
> > --- a/criu/tty.c
> > +++ b/criu/tty.c
> > @@ -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;
> > diff --git a/images/rpc.proto b/images/rpc.proto
> > index 4811738..ddc28db 100644
> > --- a/images/rpc.proto
> > +++ b/images/rpc.proto
> > @@ -109,6 +109,7 @@ message criu_opts {
> > optional bool tcp_skip_in_flight = 46;
> > optional bool weak_sysctls = 47;
> > optional bool lazy_pages = 48;
> > + optional bool orphan_pts_master = 49;
> > }
> >
> > message criu_dump_resp {
> >
>
More information about the CRIU
mailing list