[CRIU] [PATCH 3/3] lib: add criu_check() to libcriu
Ruslan Kuprieiev
kupruser at gmail.com
Fri Nov 22 04:59:47 PST 2013
On 22.11.2013 09:20, Pavel Emelyanov wrote:
> On 11/22/2013 05:20 AM, Ruslan Kuprieiev wrote:
>> Signed-off-by: Ruslan Kuprieiev <kupruser at gmail.com>
>> ---
>> lib/criu.c | 128 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>> 1 file changed, 128 insertions(+)
>>
>> diff --git a/lib/criu.c b/lib/criu.c
>> index 2bd224e..f154a88 100644
>> --- a/lib/criu.c
>> +++ b/lib/criu.c
>> @@ -1,3 +1,131 @@
>> #include "version.h"
>> +#include <sys/socket.h>
>> +#include <sys/un.h>
>> +#include <limits.h>
>> +#include <stdio.h>
>> +#include <unistd.h>
>> +#include <stdlib.h>
>> +
>> +#include "rpc.pb-c.h"
>> +
>> +#define MAX_MSG_SIZE 1024
>> +#define CR_DEFAULT_SERVICE_ADDRESS "/var/run/criu_service.socket"
>>
>> const char *criu_lib_version = CRIU_VERSION;
>> +
>> +static char *service_address = CR_DEFAULT_SERVICE_ADDRESS;
>> +
>> +void criu_set_service_address(char *path)
>> +{
>> + if (path)
>> + service_address = path;
>> + else
>> + service_address = CR_DEFAULT_SERVICE_ADDRESS;
>> +}
>> +
>> +static CriuResp *recv_resp(int socket_fd)
>> +{
>> + unsigned char buf[MAX_MSG_SIZE];
>> + int len;
>> + CriuResp *msg = 0;
>> +
>> + len = read(socket_fd, buf, MAX_MSG_SIZE);
>> + if (len == -1) {
>> + perror("Can't read response");
>> + return NULL;
>> + }
>> +
>> + msg = criu_resp__unpack(NULL, len, buf);
>> + if (!msg) {
>> + perror("Failed unpacking response");
>> + return NULL;
>> + }
>> +
>> + return msg;
>> +}
>> +
>> +static int send_req(int socket_fd, CriuReq *req)
>> +{
>> + unsigned char buf[MAX_MSG_SIZE];
>> + int len;
>> +
>> + len = criu_req__get_packed_size(req);
>> +
>> + if (criu_req__pack(req, buf) != len) {
>> + perror("Failed packing request");
>> + return -1;
>> + }
>> +
>> + if (write(socket_fd, buf, len) == -1) {
>> + perror("Can't send request");
>> + return -1;
>> + }
>> +
>> + return 0;
>> +}
>> +
>> +static int criu_connect(void)
>> +{
>> + int fd, ret;
>> + struct sockaddr_un addr;
>> + socklen_t addr_len;
>> +
>> + fd = socket(AF_LOCAL, SOCK_SEQPACKET, 0);
>> + if (fd < 0) {
>> + perror("Can't create socket");
>> + return -1;
>> + }
>> +
>> + memset(&addr, 0, sizeof(addr));
>> + addr.sun_family = AF_LOCAL;
>> +
>> + strcpy(addr.sun_path, service_address);
>> +
>> + addr_len = strlen(addr.sun_path) + sizeof(addr.sun_family);
>> +
>> + ret = connect(fd, (struct sockaddr *) &addr, addr_len);
>> + if (ret < 0) {
>> + perror("Can't connect to socket");
>> + close(fd);
>> + }
>> +
>> + return fd;
>> +}
>> +
>> +int criu_check(void)
> Please, define a set of errors in lib.h and use them in
> criu_check().
Can you please explain how it should be done? Error must be returned or
set as errno?
>
> Also provide criu_dump() and criu_restore() functions.
Sure. I'm going to add them by separate patches.
> i
>
>> +{
>> + int fd;
>> + int ret = -1;
>> + CriuReq req = CRIU_REQ__INIT;
>> + CriuResp *resp = NULL;
>> +
>> + fd = criu_connect();
>> + if (fd < 0) {
>> + perror("Can't connect to criu");
>> + return -1;
>> + }
>> +
>> + req.type = CRIU_REQ_TYPE__CHECK;
>> +
>> + if (send_req(fd, &req) < 0)
>> + goto exit;
>> +
>> + resp = recv_resp(fd);
>> + if (!resp) {
>> + perror("Can't receive response");
>> + goto exit;
>> + }
>> +
>> + if (resp->type != CRIU_REQ_TYPE__CHECK) {
>> + perror("Unexpected response type");
>> + goto exit;
>> + }
>> +
>> + ret = resp->success ? 0 : -1;
>> +
>> +exit:
>> + close(fd);
>> + free(resp);
>> +
>> + return ret;
>> +}
>>
>
More information about the CRIU
mailing list