[CRIU] [PATCH 1/6] tty: notify about orphan tty-s via rpc

Pavel Emelyanov xemul at virtuozzo.com
Tue Feb 7 02:24:39 PST 2017


On 02/07/2017 08:51 AM, Andrei Vagin wrote:
> On Mon, Feb 06, 2017 at 01:01:12PM +0300, Pavel Emelyanov wrote:
>> On 01/04/2017 02:40 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              |  2 ++
>>>  8 files changed, 84 insertions(+), 14 deletions(-)
>>>
>>> diff --git a/criu/action-scripts.c b/criu/action-scripts.c
>>> index bfa1f82..c18d1aa 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",
>>> @@ -24,6 +27,7 @@ static const char *action_names[ACT_MAX] = {
>>>  	[ ACT_SETUP_NS ]	= "setup-namespaces",
>>>  	[ ACT_POST_SETUP_NS ]	= "post-setup-namespaces",
>>>  	[ ACT_POST_RESUME ]	= "post-resume",
>>> +	[ ACT_ORPHAN_PTS_MASTER ] = "orphan-pts-master",
>>>  };
>>>  
>>>  struct script {
>>> @@ -95,6 +99,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;
>>> @@ -107,7 +122,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;
>>>  	}
>>>  
>>> @@ -145,6 +160,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 1c87c6e..db296c6 100644
>>> --- a/criu/cr-restore.c
>>> +++ b/criu/cr-restore.c
>>> @@ -3192,6 +3192,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 00a2d07..97195b2 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);
>>
>> O_O How about the message itself? How will client know what was just sent to him?
> 
> I don't understand a question. This part isn't changed.
> 
> As for the whole function, we add an ability to attach a file destriptor to a message.

I mean -- when I call send_criu_msg_with_fd and set fd != -1 _only_ the
descriptor is sent, while the message itself is created and initialized,
but is NOT sent.

Is that intentional? Are callers supposed to call this routine twice -- first
time for the message itself and the 2nd time for the descriptor to be sent?

>>
>>> +	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;
>>>  
>>> @@ -504,6 +516,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 2e3b25e..9161621 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,
>>> @@ -11,6 +13,7 @@ enum script_actions {
>>>  	ACT_SETUP_NS,
>>>  	ACT_POST_SETUP_NS,
>>>  	ACT_POST_RESUME,
>>> +	ACT_ORPHAN_PTS_MASTER,
>>>  
>>>  	ACT_MAX
>>>  };
>>> @@ -18,6 +21,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 4f70b41..ee16573 100644
>>> --- a/criu/include/cr_options.h
>>> +++ b/criu/include/cr_options.h
>>> @@ -119,6 +119,7 @@ struct cr_options {
>>>  	bool			display_stats;
>>>  	bool			weak_sysctls;
>>>  	bool			check_only;
>>> +	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,
>>
>> Unused in the set.
> 
> It will be used in the next patches. Will move. Thanks
>>
>>>  
>>>  	SERVICE_FD_MAX
>>>  };
>>> diff --git a/criu/tty.c b/criu/tty.c
>>> index 0f3d1c9..ae2d463 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));
>>> @@ -681,7 +682,7 @@ static 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;
>>> @@ -947,6 +948,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->tie->pty->index, 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;
>>> @@ -981,6 +1008,7 @@ static int pty_open_unpaired_slave(struct file_desc *d, struct tty_info *slave)
>>>  
>>>  	}
>>>  
>>> +out:
>>>  	if (restore_tty_params(fd, slave))
>>>  		goto err;
>>>  
>>> @@ -993,7 +1021,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
>>> @@ -1224,8 +1252,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;
>>>  		}
>>>  
>>>  		/*
>>> @@ -1349,7 +1379,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 7cf958c..832800c 100644
>>> --- a/images/rpc.proto
>>> +++ b/images/rpc.proto
>>> @@ -108,6 +108,8 @@ message criu_opts {
>>>  	optional uint32			timeout			= 45;
>>>  	optional bool			tcp_skip_in_flight	= 46;
>>>  	optional bool			weak_sysctls		= 47;
>>> +
>>> +	optional bool			orphan_pts_master	= 48;
>>>  }
>>>  
>>>  message criu_dump_resp {
>>>
>>
> .
> 



More information about the CRIU mailing list