2
0
mirror of https://github.com/checkpoint-restore/criu synced 2025-08-23 02:17:22 +00:00
criu/test/unix-callback/syslog-lib.c
Andrey Vagin 1e4b8c8c23 tests: check callback-s for dumping and restoring sockets (v2)
Here are client, server programs and two libraries for dumping client
sockets and syslog socket.

The client can ask server to save a value and then request it later.
We suppose that after dumping and restoring the client will get
the same value.

So the dump callback requests the value and save it in a file.
The restore callback creates a new socket and ask server to save the
value from the file.

v2: open a syslog socket

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
2013-12-20 15:28:53 +04:00

67 lines
1.3 KiB
C

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "criu-plugin.h"
#include "criu-log.h"
extern cr_plugin_dump_unix_sk_t cr_plugin_dump_unix_sk;
extern cr_plugin_restore_unix_sk_t cr_plugin_restore_unix_sk;
int cr_plugin_dump_unix_sk(int sk, int id)
{
struct sockaddr_un addr;
socklen_t addr_len = sizeof(addr);
char buf[4096];
int fd;
if (getsockname(sk, (struct sockaddr *) &addr, &addr_len) < 0)
return -1;
if (strncmp(addr.sun_path, "/dev/log", addr_len - sizeof(addr.sun_family)))
return -ENOTSUP;
snprintf(buf, sizeof(buf), "syslog-%x.img", id);
fd = open(buf, O_WRONLY | O_CREAT);
if (fd < 0)
return -1;
close(fd);
return 0;
}
int cr_plugin_restore_unix_sk(int id)
{
struct sockaddr_un addr;
socklen_t addr_len;
char buf[4096];
int sk, fd;
snprintf(buf, sizeof(buf), "syslog-%x.img", id);
fd = open(buf, O_RDONLY);
if (fd < 0)
return -ENOTSUP;
close(fd);
sk = socket(AF_FILE, SOCK_DGRAM|SOCK_CLOEXEC, 0);
if (sk == -1)
return sk;
addr.sun_family = AF_FILE;
addr_len = strlen("/dev/log");
strncpy(addr.sun_path, "/dev/log", addr_len);
addr_len += sizeof(addr.sun_family);
if (connect(sk, (struct sockaddr *) &addr, addr_len) == -1) {
close(sk);
return -1;
}
return sk;
}