[1/3] service: allow to execute page-server as a child process

Submitted by Andrei Vagin on April 24, 2017, 9:37 p.m.

Details

Message ID 20170424213751.31374-2-avagin@openvz.org
State New
Series "service: allow to run page-server as a child process"
Headers show

Commit Message

Andrei Vagin April 24, 2017, 9:37 p.m.
From: Andrei Vagin <avagin@virtuozzo.com>

In this case we can wait it and get an exit code.

For example, it will be useful for p.haul where one connection
is used several times, so we need a way how to understand  that
page-server exited unexpectedly.

Signed-off-by: Andrei Vagin <avagin@virtuozzo.com>
---
 criu/cr-service.c | 85 ++++++++++++++++++++++++++++++++++++++++++-------------
 images/rpc.proto  |  8 ++++++
 2 files changed, 74 insertions(+), 19 deletions(-)

Patch hide | download patch | download mbox

diff --git a/criu/cr-service.c b/criu/cr-service.c
index d59ab8b..515d040 100644
--- a/criu/cr-service.c
+++ b/criu/cr-service.c
@@ -710,7 +710,7 @@  struct ps_info {
 	unsigned short port;
 };
 
-static int start_page_server_req(int sk, CriuOpts *req)
+static int start_page_server_req(int sk, CriuOpts *req, bool daemon_mode)
 {
 	int ret = -1, pid, start_pipe[2];
 	ssize_t count;
@@ -735,35 +735,46 @@  static int start_page_server_req(int sk, CriuOpts *req)
 
 		pr_debug("Starting page server\n");
 
-		pid = cr_page_server(true, false, start_pipe[1]);
+		if (!daemon_mode) {
+			count = write(start_pipe[1], &info, sizeof(info));
+			if (count != sizeof(info))
+				goto out_ch;
+		}
+
+		pid = cr_page_server(daemon_mode, false, start_pipe[1]);
 		if (pid < 0)
 			goto out_ch;
 
-		info.pid = pid;
-		info.port = opts.port;
+		if (daemon_mode) {
+			info.pid = pid;
+			info.port = opts.port;
 
-		count = write(start_pipe[1], &info, sizeof(info));
-		if (count != sizeof(info))
-			goto out_ch;
+			count = write(start_pipe[1], &info, sizeof(info));
+			if (count != sizeof(info))
+				goto out_ch;
+		}
 
 		ret = 0;
 out_ch:
-		if (ret < 0 && pid > 0)
+		if (daemon_mode && ret < 0 && pid > 0)
 			kill(pid, SIGKILL);
 		close(start_pipe[1]);
 		exit(ret);
 	}
 
 	close(start_pipe[1]);
-	wait(&ret);
-	if (WIFEXITED(ret)) {
-		if (WEXITSTATUS(ret)) {
-			pr_err("Child exited with an error\n");
+
+	if (daemon_mode) {
+		wait(&ret);
+		if (WIFEXITED(ret)) {
+			if (WEXITSTATUS(ret)) {
+				pr_err("Child exited with an error\n");
+				goto out;
+			}
+		} else {
+			pr_err("Child wasn't terminated normally\n");
 			goto out;
 		}
-	} else {
-		pr_err("Child wasn't terminated normally\n");
-		goto out;
 	}
 
 	count = read(start_pipe[0], &info, sizeof(info));
@@ -771,11 +782,15 @@  out_ch:
 	if (count != sizeof(info))
 		goto out;
 
+	if (daemon_mode) {
+		ps.pid = info.pid;
+		ps.has_port = true;
+		ps.port = info.port;
+	} else
+		ps.pid = pid;
+
 	success = true;
 	ps.has_pid = true;
-	ps.pid = info.pid;
-	ps.has_port = true;
-	ps.port = info.port;
 	resp.ps = &ps;
 
 	pr_debug("Page server started\n");
@@ -802,6 +817,9 @@  static int chk_keepopen_req(CriuReq *msg)
 	if (msg->type == CRIU_REQ_TYPE__PAGE_SERVER)
 		/* This just fork()-s so no leaks */
 		return 0;
+	if (msg->type == CRIU_REQ_TYPE__PAGE_SERVER_CHLD)
+		/* This just fork()-s so no leaks */
+		return 0;
 	else if (msg->type == CRIU_REQ_TYPE__CPUINFO_DUMP ||
 		 msg->type == CRIU_REQ_TYPE__CPUINFO_CHECK)
 		return 0;
@@ -946,6 +964,29 @@  out:
 	return send_criu_msg(sk, &resp);
 }
 
+static int handle_wait_pid(int sk, int pid)
+{
+	CriuResp resp = CRIU_RESP__INIT;
+	bool success = false;
+	int status;
+
+	if (waitpid(pid, &status, 0) == -1) {
+		resp.cr_errno = errno;
+		pr_perror("Unable to wait %d", pid);
+		goto out;
+	}
+
+	resp.status = status;
+	resp.has_status = true;
+
+	success = true;
+out:
+	resp.type = CRIU_REQ_TYPE__WAIT_PID;
+	resp.success = success;
+
+	return send_criu_msg(sk, &resp);
+}
+
 static int handle_cpuinfo(int sk, CriuReq *msg)
 {
 	CriuResp resp = CRIU_RESP__INIT;
@@ -1031,7 +1072,13 @@  more:
 		ret = pre_dump_loop(sk, msg);
 		break;
 	case CRIU_REQ_TYPE__PAGE_SERVER:
-		ret =  start_page_server_req(sk, msg->opts);
+		ret = start_page_server_req(sk, msg->opts, true);
+		break;
+	case CRIU_REQ_TYPE__PAGE_SERVER_CHLD:
+		ret = start_page_server_req(sk, msg->opts, false);
+		break;
+	case CRIU_REQ_TYPE__WAIT_PID:
+		ret =  handle_wait_pid(sk, msg->pid);
 		break;
 	case CRIU_REQ_TYPE__CPUINFO_DUMP:
 	case CRIU_REQ_TYPE__CPUINFO_CHECK:
diff --git a/images/rpc.proto b/images/rpc.proto
index 48e42e2..71f47d5 100644
--- a/images/rpc.proto
+++ b/images/rpc.proto
@@ -142,6 +142,9 @@  enum criu_req_type {
 	FEATURE_CHECK	= 9;
 
 	VERSION		= 10;
+
+	WAIT_PID	= 11;
+	PAGE_SERVER_CHLD = 12;
 }
 
 /*
@@ -176,6 +179,9 @@  message criu_req {
 	 * via RPC.
 	 */
 	optional criu_features		features	= 5;
+
+	/* 'pid' is used for WAIT_PID */
+	optional uint32			pid		= 6;
 }
 
 /*
@@ -196,6 +202,8 @@  message criu_resp {
 	optional criu_features		features	= 8;
 	optional string			cr_errmsg	= 9;
 	optional criu_version		version		= 10;
+
+	optional int32			status		= 11;
 }
 
 /* Answer for criu_req_type.VERSION requests */

Comments

Pavel Emelianov April 25, 2017, 9:13 a.m.
On 04/25/2017 12:37 AM, Andrei Vagin wrote:
> From: Andrei Vagin <avagin@virtuozzo.com>

How is it executed now? Not as a child?

> In this case we can wait it and get an exit code.
> 
> For example, it will be useful for p.haul where one connection
> is used several times, so we need a way how to understand  that
> page-server exited unexpectedly.
> 
> Signed-off-by: Andrei Vagin <avagin@virtuozzo.com>
> ---
>  criu/cr-service.c | 85 ++++++++++++++++++++++++++++++++++++++++++-------------
>  images/rpc.proto  |  8 ++++++
>  2 files changed, 74 insertions(+), 19 deletions(-)
> 
> diff --git a/criu/cr-service.c b/criu/cr-service.c
> index d59ab8b..515d040 100644
> --- a/criu/cr-service.c
> +++ b/criu/cr-service.c
> @@ -710,7 +710,7 @@ struct ps_info {
>  	unsigned short port;
>  };
>  
> -static int start_page_server_req(int sk, CriuOpts *req)
> +static int start_page_server_req(int sk, CriuOpts *req, bool daemon_mode)
>  {
>  	int ret = -1, pid, start_pipe[2];
>  	ssize_t count;
> @@ -735,35 +735,46 @@ static int start_page_server_req(int sk, CriuOpts *req)
>  
>  		pr_debug("Starting page server\n");
>  
> -		pid = cr_page_server(true, false, start_pipe[1]);
> +		if (!daemon_mode) {
> +			count = write(start_pipe[1], &info, sizeof(info));
> +			if (count != sizeof(info))
> +				goto out_ch;
> +		}
> +
> +		pid = cr_page_server(daemon_mode, false, start_pipe[1]);
>  		if (pid < 0)
>  			goto out_ch;
>  
> -		info.pid = pid;
> -		info.port = opts.port;
> +		if (daemon_mode) {
> +			info.pid = pid;
> +			info.port = opts.port;
>  
> -		count = write(start_pipe[1], &info, sizeof(info));
> -		if (count != sizeof(info))
> -			goto out_ch;
> +			count = write(start_pipe[1], &info, sizeof(info));
> +			if (count != sizeof(info))
> +				goto out_ch;
> +		}
>  
>  		ret = 0;
>  out_ch:
> -		if (ret < 0 && pid > 0)
> +		if (daemon_mode && ret < 0 && pid > 0)
>  			kill(pid, SIGKILL);
>  		close(start_pipe[1]);
>  		exit(ret);
>  	}
>  
>  	close(start_pipe[1]);
> -	wait(&ret);
> -	if (WIFEXITED(ret)) {
> -		if (WEXITSTATUS(ret)) {
> -			pr_err("Child exited with an error\n");
> +
> +	if (daemon_mode) {
> +		wait(&ret);
> +		if (WIFEXITED(ret)) {
> +			if (WEXITSTATUS(ret)) {
> +				pr_err("Child exited with an error\n");
> +				goto out;
> +			}
> +		} else {
> +			pr_err("Child wasn't terminated normally\n");
>  			goto out;
>  		}
> -	} else {
> -		pr_err("Child wasn't terminated normally\n");
> -		goto out;
>  	}
>  
>  	count = read(start_pipe[0], &info, sizeof(info));
> @@ -771,11 +782,15 @@ out_ch:
>  	if (count != sizeof(info))
>  		goto out;
>  
> +	if (daemon_mode) {
> +		ps.pid = info.pid;
> +		ps.has_port = true;
> +		ps.port = info.port;
> +	} else
> +		ps.pid = pid;
> +
>  	success = true;
>  	ps.has_pid = true;
> -	ps.pid = info.pid;
> -	ps.has_port = true;
> -	ps.port = info.port;
>  	resp.ps = &ps;
>  
>  	pr_debug("Page server started\n");
> @@ -802,6 +817,9 @@ static int chk_keepopen_req(CriuReq *msg)
>  	if (msg->type == CRIU_REQ_TYPE__PAGE_SERVER)
>  		/* This just fork()-s so no leaks */
>  		return 0;
> +	if (msg->type == CRIU_REQ_TYPE__PAGE_SERVER_CHLD)
> +		/* This just fork()-s so no leaks */
> +		return 0;
>  	else if (msg->type == CRIU_REQ_TYPE__CPUINFO_DUMP ||
>  		 msg->type == CRIU_REQ_TYPE__CPUINFO_CHECK)
>  		return 0;
> @@ -946,6 +964,29 @@ out:
>  	return send_criu_msg(sk, &resp);
>  }
>  
> +static int handle_wait_pid(int sk, int pid)
> +{
> +	CriuResp resp = CRIU_RESP__INIT;
> +	bool success = false;
> +	int status;
> +
> +	if (waitpid(pid, &status, 0) == -1) {
> +		resp.cr_errno = errno;
> +		pr_perror("Unable to wait %d", pid);
> +		goto out;
> +	}
> +
> +	resp.status = status;
> +	resp.has_status = true;
> +
> +	success = true;
> +out:
> +	resp.type = CRIU_REQ_TYPE__WAIT_PID;
> +	resp.success = success;
> +
> +	return send_criu_msg(sk, &resp);
> +}
> +
>  static int handle_cpuinfo(int sk, CriuReq *msg)
>  {
>  	CriuResp resp = CRIU_RESP__INIT;
> @@ -1031,7 +1072,13 @@ more:
>  		ret = pre_dump_loop(sk, msg);
>  		break;
>  	case CRIU_REQ_TYPE__PAGE_SERVER:
> -		ret =  start_page_server_req(sk, msg->opts);
> +		ret = start_page_server_req(sk, msg->opts, true);
> +		break;
> +	case CRIU_REQ_TYPE__PAGE_SERVER_CHLD:
> +		ret = start_page_server_req(sk, msg->opts, false);
> +		break;
> +	case CRIU_REQ_TYPE__WAIT_PID:
> +		ret =  handle_wait_pid(sk, msg->pid);
>  		break;
>  	case CRIU_REQ_TYPE__CPUINFO_DUMP:
>  	case CRIU_REQ_TYPE__CPUINFO_CHECK:
> diff --git a/images/rpc.proto b/images/rpc.proto
> index 48e42e2..71f47d5 100644
> --- a/images/rpc.proto
> +++ b/images/rpc.proto
> @@ -142,6 +142,9 @@ enum criu_req_type {
>  	FEATURE_CHECK	= 9;
>  
>  	VERSION		= 10;
> +
> +	WAIT_PID	= 11;
> +	PAGE_SERVER_CHLD = 12;
>  }
>  
>  /*
> @@ -176,6 +179,9 @@ message criu_req {
>  	 * via RPC.
>  	 */
>  	optional criu_features		features	= 5;
> +
> +	/* 'pid' is used for WAIT_PID */
> +	optional uint32			pid		= 6;
>  }
>  
>  /*
> @@ -196,6 +202,8 @@ message criu_resp {
>  	optional criu_features		features	= 8;
>  	optional string			cr_errmsg	= 9;
>  	optional criu_version		version		= 10;
> +
> +	optional int32			status		= 11;
>  }
>  
>  /* Answer for criu_req_type.VERSION requests */
>
Andrey Vagin April 25, 2017, 6:53 p.m.
On Tue, Apr 25, 2017 at 12:13:32PM +0300, Pavel Emelyanov wrote:
> On 04/25/2017 12:37 AM, Andrei Vagin wrote:
> > From: Andrei Vagin <avagin@virtuozzo.com>
> 
> How is it executed now? Not as a child?

as a daemon

> 
> > In this case we can wait it and get an exit code.
> > 
> > For example, it will be useful for p.haul where one connection
> > is used several times, so we need a way how to understand  that
> > page-server exited unexpectedly.
> > 
> > Signed-off-by: Andrei Vagin <avagin@virtuozzo.com>
> > ---
> >  criu/cr-service.c | 85 ++++++++++++++++++++++++++++++++++++++++++-------------
> >  images/rpc.proto  |  8 ++++++
> >  2 files changed, 74 insertions(+), 19 deletions(-)
> > 
> > diff --git a/criu/cr-service.c b/criu/cr-service.c
> > index d59ab8b..515d040 100644
> > --- a/criu/cr-service.c
> > +++ b/criu/cr-service.c
> > @@ -710,7 +710,7 @@ struct ps_info {
> >  	unsigned short port;
> >  };
> >  
> > -static int start_page_server_req(int sk, CriuOpts *req)
> > +static int start_page_server_req(int sk, CriuOpts *req, bool daemon_mode)
> >  {
> >  	int ret = -1, pid, start_pipe[2];
> >  	ssize_t count;
> > @@ -735,35 +735,46 @@ static int start_page_server_req(int sk, CriuOpts *req)
> >  
> >  		pr_debug("Starting page server\n");
> >  
> > -		pid = cr_page_server(true, false, start_pipe[1]);
> > +		if (!daemon_mode) {
> > +			count = write(start_pipe[1], &info, sizeof(info));
> > +			if (count != sizeof(info))
> > +				goto out_ch;
> > +		}
> > +
> > +		pid = cr_page_server(daemon_mode, false, start_pipe[1]);
> >  		if (pid < 0)
> >  			goto out_ch;
> >  
> > -		info.pid = pid;
> > -		info.port = opts.port;
> > +		if (daemon_mode) {
> > +			info.pid = pid;
> > +			info.port = opts.port;
> >  
> > -		count = write(start_pipe[1], &info, sizeof(info));
> > -		if (count != sizeof(info))
> > -			goto out_ch;
> > +			count = write(start_pipe[1], &info, sizeof(info));
> > +			if (count != sizeof(info))
> > +				goto out_ch;
> > +		}
> >  
> >  		ret = 0;
> >  out_ch:
> > -		if (ret < 0 && pid > 0)
> > +		if (daemon_mode && ret < 0 && pid > 0)
> >  			kill(pid, SIGKILL);
> >  		close(start_pipe[1]);
> >  		exit(ret);
> >  	}
> >  
> >  	close(start_pipe[1]);
> > -	wait(&ret);
> > -	if (WIFEXITED(ret)) {
> > -		if (WEXITSTATUS(ret)) {
> > -			pr_err("Child exited with an error\n");
> > +
> > +	if (daemon_mode) {
> > +		wait(&ret);
> > +		if (WIFEXITED(ret)) {
> > +			if (WEXITSTATUS(ret)) {
> > +				pr_err("Child exited with an error\n");
> > +				goto out;
> > +			}
> > +		} else {
> > +			pr_err("Child wasn't terminated normally\n");
> >  			goto out;
> >  		}
> > -	} else {
> > -		pr_err("Child wasn't terminated normally\n");
> > -		goto out;
> >  	}
> >  
> >  	count = read(start_pipe[0], &info, sizeof(info));
> > @@ -771,11 +782,15 @@ out_ch:
> >  	if (count != sizeof(info))
> >  		goto out;
> >  
> > +	if (daemon_mode) {
> > +		ps.pid = info.pid;
> > +		ps.has_port = true;
> > +		ps.port = info.port;
> > +	} else
> > +		ps.pid = pid;
> > +
> >  	success = true;
> >  	ps.has_pid = true;
> > -	ps.pid = info.pid;
> > -	ps.has_port = true;
> > -	ps.port = info.port;
> >  	resp.ps = &ps;
> >  
> >  	pr_debug("Page server started\n");
> > @@ -802,6 +817,9 @@ static int chk_keepopen_req(CriuReq *msg)
> >  	if (msg->type == CRIU_REQ_TYPE__PAGE_SERVER)
> >  		/* This just fork()-s so no leaks */
> >  		return 0;
> > +	if (msg->type == CRIU_REQ_TYPE__PAGE_SERVER_CHLD)
> > +		/* This just fork()-s so no leaks */
> > +		return 0;
> >  	else if (msg->type == CRIU_REQ_TYPE__CPUINFO_DUMP ||
> >  		 msg->type == CRIU_REQ_TYPE__CPUINFO_CHECK)
> >  		return 0;
> > @@ -946,6 +964,29 @@ out:
> >  	return send_criu_msg(sk, &resp);
> >  }
> >  
> > +static int handle_wait_pid(int sk, int pid)
> > +{
> > +	CriuResp resp = CRIU_RESP__INIT;
> > +	bool success = false;
> > +	int status;
> > +
> > +	if (waitpid(pid, &status, 0) == -1) {
> > +		resp.cr_errno = errno;
> > +		pr_perror("Unable to wait %d", pid);
> > +		goto out;
> > +	}
> > +
> > +	resp.status = status;
> > +	resp.has_status = true;
> > +
> > +	success = true;
> > +out:
> > +	resp.type = CRIU_REQ_TYPE__WAIT_PID;
> > +	resp.success = success;
> > +
> > +	return send_criu_msg(sk, &resp);
> > +}
> > +
> >  static int handle_cpuinfo(int sk, CriuReq *msg)
> >  {
> >  	CriuResp resp = CRIU_RESP__INIT;
> > @@ -1031,7 +1072,13 @@ more:
> >  		ret = pre_dump_loop(sk, msg);
> >  		break;
> >  	case CRIU_REQ_TYPE__PAGE_SERVER:
> > -		ret =  start_page_server_req(sk, msg->opts);
> > +		ret = start_page_server_req(sk, msg->opts, true);
> > +		break;
> > +	case CRIU_REQ_TYPE__PAGE_SERVER_CHLD:
> > +		ret = start_page_server_req(sk, msg->opts, false);
> > +		break;
> > +	case CRIU_REQ_TYPE__WAIT_PID:
> > +		ret =  handle_wait_pid(sk, msg->pid);
> >  		break;
> >  	case CRIU_REQ_TYPE__CPUINFO_DUMP:
> >  	case CRIU_REQ_TYPE__CPUINFO_CHECK:
> > diff --git a/images/rpc.proto b/images/rpc.proto
> > index 48e42e2..71f47d5 100644
> > --- a/images/rpc.proto
> > +++ b/images/rpc.proto
> > @@ -142,6 +142,9 @@ enum criu_req_type {
> >  	FEATURE_CHECK	= 9;
> >  
> >  	VERSION		= 10;
> > +
> > +	WAIT_PID	= 11;
> > +	PAGE_SERVER_CHLD = 12;
> >  }
> >  
> >  /*
> > @@ -176,6 +179,9 @@ message criu_req {
> >  	 * via RPC.
> >  	 */
> >  	optional criu_features		features	= 5;
> > +
> > +	/* 'pid' is used for WAIT_PID */
> > +	optional uint32			pid		= 6;
> >  }
> >  
> >  /*
> > @@ -196,6 +202,8 @@ message criu_resp {
> >  	optional criu_features		features	= 8;
> >  	optional string			cr_errmsg	= 9;
> >  	optional criu_version		version		= 10;
> > +
> > +	optional int32			status		= 11;
> >  }
> >  
> >  /* Answer for criu_req_type.VERSION requests */
> > 
>