[CRIU] [PATCH 7/8] lib: add dump/restore

Ruslan Kuprieiev kupruser at gmail.com
Wed Dec 4 05:50:22 PST 2013


On 04.12.2013 14:18, Andrew Vagin wrote:
> On Wed, Dec 04, 2013 at 02:57:30PM +0400, Ruslan Kuprieiev wrote:
>> Signed-off-by: Ruslan Kuprieiev <kupruser at gmail.com>
>> ---
>>   lib/criu.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++----------------
>>   lib/criu.h |  3 +++
>>   2 files changed, 49 insertions(+), 16 deletions(-)
>>
>> diff --git a/lib/criu.c b/lib/criu.c
>> index 75abe10..845ea42 100644
>> --- a/lib/criu.c
>> +++ b/lib/criu.c
>> @@ -93,12 +93,10 @@ static int criu_connect(void)
>>   	return fd;
>>   }
>>   
>> -int criu_check(void)
>> +static int exchange(CriuReq *req, CriuResp **resp)
>>   {
>>   	int fd;
>>   	int ret	= 0;
>> -	CriuReq req	= CRIU_REQ__INIT;
>> -	CriuResp *resp	= NULL;
>>   
>>   	fd = criu_connect();
>>   	if (fd < 0) {
>> @@ -107,34 +105,66 @@ int criu_check(void)
>>   		goto exit;
>>   	}
>>   
>> -	req.type	= CRIU_REQ_TYPE__CHECK;
>> -
>> -	if (send_req(fd, &req) < 0) {
>> +	if (send_req(fd, req) < 0) {
>>   		ret = CRIU_EREQ;
>>   		goto exit;
>>   	}
>>   
>> -	resp = recv_resp(fd);
>> -	if (!resp) {
>> +	*resp = recv_resp(fd);
>> +	if (!*resp) {
>>   		perror("Can't receive response");
>>   		ret = CRIU_ERESP;
>>   		goto exit;
>>   	}
>>   
>> -	if (resp->type != CRIU_REQ_TYPE__CHECK) {
>> -		perror("Unexpected response type");
>> -		ret = CRIU_ERESP;
>> +	if ((*resp)->type != req->type) {
>> +		if ((*resp)->type == CRIU_REQ_TYPE__EMPTY)
>> +			ret = CRIU_EREQ;
>> +		else {
>> +			perror("Unexpected response type");
>> +			ret = CRIU_ERESP;
>> +		}
>>   		goto exit;
>>   	}
>>   
>> -	if (!resp->success)
>> -		ret = CRIU_ECHECK;
>> -
>>   exit:
>>   	if (fd > 0)
>>   		close(fd);
>> -	if (resp)
>> -		free(resp);
>>   
>>   	return ret;
>>   }
>> +
>> +int criu_check(void)
>> +{
>> +	int ret;
>> +	CriuReq req	= CRIU_REQ__INIT;
>> +	CriuResp *resp	= NULL;
>> +
>> +	req.type	= CRIU_REQ_TYPE__CHECK;
>> +
>> +	ret = exchange(&req, &resp);
>> +	if (ret < 0)
>> +		return ret;
>> +
>> +	return resp->success ? 0 : CRIU_ECHECK;
>> +}
>> +
>> +int criu_dump(CriuOpts *opts, CriuResp **resp)
>> +{
> I don't like that we return "CriuResp **resp" to users.

Why? User should pass pointer to CriuResp pointer, it was comfortable 
enough, when i was writing tests. Especially, considering 
not-so-comfortable usage of protobuf structures in c.

>
>> +	CriuReq req	= CRIU_REQ__INIT;
>> +
>> +	req.type	= CRIU_REQ_TYPE__DUMP;
>> +	req.opts	= opts;
>> +
>> +	return exchange(&req, resp);
>> +}
>> +
>> +int criu_restore(CriuOpts *opts, CriuResp **resp)
>> +{
>> +	CriuReq req	= CRIU_REQ__INIT;
>> +
>> +	req.type	= CRIU_REQ_TYPE__RESTORE;
>> +	req.opts	= opts;
>> +
>> +	return exchange(&req, resp);
>> +}
>> diff --git a/lib/criu.h b/lib/criu.h
>> index 02939d3..5b50670 100644
>> --- a/lib/criu.h
>> +++ b/lib/criu.h
>> @@ -1,10 +1,13 @@
>>   #ifndef __CRIU_LIB_H__
>>   #define __CRIU_LIB_H__
>>   
>> +#include <stdbool.h>
>>   #include "rpc.pb-c.h"
>>   
>>   void criu_set_service_address(char *path);
>>   int criu_check(void);
>> +int criu_dump(CriuOpts *opts, CriuResp **resp);
>> +int criu_restore(CriuOpts *opts, CriuResp **resp);
>>   
>>   #define CRIU_ECONNECT	-1
>>   #define CRIU_EREQ	-2
>> -- 
>> 1.8.1.2
>>
>> _______________________________________________
>> CRIU mailing list
>> CRIU at openvz.org
>> https://lists.openvz.org/mailman/listinfo/criu



More information about the CRIU mailing list