2013-09-21 02:20:24 +04:00
|
|
|
#include "rpc.pb-c.h"
|
2013-11-08 16:46:05 +04:00
|
|
|
#include <stdlib.h>
|
2013-09-21 02:20:24 +04:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2018-06-21 11:32:53 +03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
2013-09-21 02:20:24 +04:00
|
|
|
#include <stdio.h>
|
2015-08-10 14:37:00 +03:00
|
|
|
#include <unistd.h>
|
2013-09-21 02:20:24 +04:00
|
|
|
|
|
|
|
#define MAX_MSG_SIZE 1024
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-07-19 07:28:38 +00:00
|
|
|
if (write(socket_fd, buf, len) == -1) {
|
2013-09-21 02:20:24 +04:00
|
|
|
perror("Can't send request");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-22 18:59:00 +03:00
|
|
|
int main(int argc, char *argv[])
|
2013-09-21 02:20:24 +04:00
|
|
|
{
|
2021-07-19 07:28:38 +00:00
|
|
|
CriuReq req = CRIU_REQ__INIT;
|
|
|
|
CriuResp *resp = NULL;
|
2013-09-21 02:20:24 +04:00
|
|
|
int fd, dir_fd;
|
|
|
|
int ret = 0;
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
socklen_t addr_len;
|
|
|
|
|
2015-01-22 18:59:00 +03:00
|
|
|
if (argc != 3) {
|
|
|
|
fprintf(stderr, "Usage: test-c criu-service.socket imgs_dir");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-09-21 02:20:24 +04:00
|
|
|
/*
|
|
|
|
* Open a directory, in which criu will
|
|
|
|
* put images
|
|
|
|
*/
|
|
|
|
|
2015-01-22 18:59:00 +03:00
|
|
|
puts(argv[2]);
|
|
|
|
dir_fd = open(argv[2], O_DIRECTORY);
|
2013-09-21 02:20:24 +04:00
|
|
|
if (dir_fd == -1) {
|
2015-01-22 18:59:00 +03:00
|
|
|
perror("Can't open imgs dir");
|
2013-09-21 02:20:24 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set "DUMP" type of request.
|
|
|
|
* Allocate CriuDumpReq.
|
|
|
|
*/
|
2021-07-19 07:28:38 +00:00
|
|
|
req.type = CRIU_REQ_TYPE__DUMP;
|
|
|
|
req.opts = malloc(sizeof(CriuOpts));
|
2013-10-02 16:04:11 +04:00
|
|
|
if (!req.opts) {
|
2021-07-19 07:28:38 +00:00
|
|
|
perror("Can't allocate memory for dump request");
|
|
|
|
return -1;
|
2013-09-21 02:20:24 +04:00
|
|
|
}
|
|
|
|
|
2013-10-02 16:04:11 +04:00
|
|
|
criu_opts__init(req.opts);
|
2013-09-21 02:20:24 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set dump options.
|
|
|
|
* Checkout more in protobuf/rpc.proto.
|
|
|
|
*/
|
2021-07-19 07:28:38 +00:00
|
|
|
req.opts->has_leave_running = true;
|
|
|
|
req.opts->leave_running = true;
|
|
|
|
req.opts->images_dir_fd = dir_fd;
|
|
|
|
req.opts->has_log_level = true;
|
|
|
|
req.opts->log_level = 4;
|
2013-09-21 02:20:24 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Connect to service socket
|
|
|
|
*/
|
|
|
|
fd = socket(AF_LOCAL, SOCK_SEQPACKET, 0);
|
|
|
|
if (fd == -1) {
|
|
|
|
perror("Can't create socket");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.sun_family = AF_LOCAL;
|
|
|
|
|
2015-01-22 18:59:00 +03:00
|
|
|
strcpy(addr.sun_path, argv[1]);
|
2013-09-21 02:20:24 +04:00
|
|
|
|
|
|
|
addr_len = strlen(addr.sun_path) + sizeof(addr.sun_family);
|
|
|
|
|
2021-07-19 07:28:38 +00:00
|
|
|
ret = connect(fd, (struct sockaddr *)&addr, addr_len);
|
2013-09-21 02:20:24 +04:00
|
|
|
if (ret == -1) {
|
2022-03-30 18:45:16 -07:00
|
|
|
perror("Can't connect to socket");
|
2013-09-21 02:20:24 +04:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send request
|
|
|
|
*/
|
|
|
|
ret = send_req(fd, &req);
|
|
|
|
if (ret == -1) {
|
|
|
|
perror("Can't send request");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recv response
|
|
|
|
*/
|
|
|
|
resp = recv_resp(fd);
|
|
|
|
if (!resp) {
|
|
|
|
perror("Can't recv response");
|
|
|
|
ret = -1;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resp->type != CRIU_REQ_TYPE__DUMP) {
|
|
|
|
perror("Unexpected response type");
|
|
|
|
ret = -1;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check response.
|
|
|
|
*/
|
|
|
|
if (resp->success)
|
|
|
|
puts("Success");
|
|
|
|
else {
|
|
|
|
puts("Fail");
|
|
|
|
ret = -1;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resp->dump->has_restored && resp->dump->restored)
|
|
|
|
puts("Restored");
|
|
|
|
|
|
|
|
exit:
|
|
|
|
close(fd);
|
|
|
|
close(dir_fd);
|
2015-08-24 01:26:26 +03:00
|
|
|
if (resp)
|
|
|
|
criu_resp__free_unpacked(resp, NULL);
|
2013-09-21 02:20:24 +04:00
|
|
|
return ret;
|
|
|
|
}
|