Skip to content

Commit

Permalink
util: dump fsfd log messages
Browse files Browse the repository at this point in the history
It should help to investigate errors of fsconfig, fsmount and etc.

Signed-off-by: Andrei Vagin <avagin@google.com>
  • Loading branch information
avagin committed Sep 19, 2024
1 parent fe87f3b commit 6aafc9d
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 39 deletions.
18 changes: 11 additions & 7 deletions criu/cgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include "images/cgroup.pb-c.h"
#include "kerndat.h"
#include "linux/mount.h"
#include "syscall.h"

/*
* This structure describes set of controller groups
Expand Down Expand Up @@ -581,37 +580,42 @@ static int __new_open_cgroupfs(struct cg_ctl *cc)
int fsfd, fd;
char *name;

fsfd = sys_fsopen(fstype, 0);
fsfd = cr_fsopen(fstype, 0);
if (fsfd < 0) {
pr_perror("Unable to open the cgroup file system");
return -1;
}

if (strstartswith(cc->name, namestr)) {
if (sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "name", cc->name + strlen(namestr), 0)) {
if (cr_fsconfig(fsfd, FSCONFIG_SET_STRING, "name", cc->name + strlen(namestr), 0)) {
fsfd_dump_messages(fsfd);
pr_perror("Unable to configure the cgroup (%s) file system", cc->name);
goto err;
}
} else if (cc->name[0] != 0) { /* cgroup v1 */
char *saveptr = NULL, *buf = strdupa(cc->name);
name = strtok_r(buf, ",", &saveptr);
while (name) {
if (sys_fsconfig(fsfd, FSCONFIG_SET_FLAG, name, NULL, 0)) {
if (cr_fsconfig(fsfd, FSCONFIG_SET_FLAG, name, NULL, 0)) {
fsfd_dump_messages(fsfd);
pr_perror("Unable to configure the cgroup (%s) file system", name);
goto err;
}
name = strtok_r(NULL, ",", &saveptr);
}
}

if (sys_fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0)) {
if (cr_fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0)) {
fsfd_dump_messages(fsfd);
pr_perror("Unable to create the cgroup (%s) file system", cc->name);
goto err;
}

fd = sys_fsmount(fsfd, 0, 0);
if (fd < 0)
fd = cr_fsmount(fsfd, 0, 0);
if (fd < 0) {
fsfd_dump_messages(fsfd);
pr_perror("Unable to mount the cgroup (%s) file system", cc->name);
}
close(fsfd);

return fd;
Expand Down
21 changes: 10 additions & 11 deletions criu/cr-check.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
#include "restorer.h"
#include "uffd.h"
#include "linux/aio_abi.h"
#include "syscall.h"
#include "mount-v2.h"

#include "images/inventory.pb-c.h"
Expand Down Expand Up @@ -1437,18 +1436,18 @@ static int ovl_mount(void)
{
int tmpfs, fsfd, ovl;

fsfd = sys_fsopen("tmpfs", 0);
fsfd = cr_fsopen("tmpfs", 0);
if (fsfd == -1) {
pr_perror("Unable to fsopen tmpfs");
return -1;
}

if (sys_fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) == -1) {
if (cr_fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) == -1) {
pr_perror("Unable to create tmpfs mount");
return -1;
}

tmpfs = sys_fsmount(fsfd, 0, 0);
tmpfs = cr_fsmount(fsfd, 0, 0);
if (tmpfs == -1) {
pr_perror("Unable to mount tmpfs");
return -1;
Expand All @@ -1475,23 +1474,23 @@ static int ovl_mount(void)
return -1;
}

fsfd = sys_fsopen("overlay", 0);
fsfd = cr_fsopen("overlay", 0);
if (fsfd == -1) {
pr_perror("Unable to fsopen overlayfs");
return -1;
}
if (sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "source", "test", 0) == -1 ||
sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "lowerdir", "/tmp/l", 0) == -1 ||
sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "upperdir", "/tmp/u", 0) == -1 ||
sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "workdir", "/tmp/w", 0) == -1) {
if (cr_fsconfig(fsfd, FSCONFIG_SET_STRING, "source", "test", 0) == -1 ||
cr_fsconfig(fsfd, FSCONFIG_SET_STRING, "lowerdir", "/tmp/l", 0) == -1 ||
cr_fsconfig(fsfd, FSCONFIG_SET_STRING, "upperdir", "/tmp/u", 0) == -1 ||
cr_fsconfig(fsfd, FSCONFIG_SET_STRING, "workdir", "/tmp/w", 0) == -1) {
pr_perror("Unable to configure overlayfs");
return -1;
}
if (sys_fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) == -1) {
if (cr_fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) == -1) {
pr_perror("Unable to create overlayfs");
return -1;
}
ovl = sys_fsmount(fsfd, 0, 0);
ovl = cr_fsmount(fsfd, 0, 0);
if (ovl == -1) {
pr_perror("Unable to mount overlayfs");
return -1;
Expand Down
17 changes: 0 additions & 17 deletions criu/include/syscall.h

This file was deleted.

5 changes: 5 additions & 0 deletions criu/include/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,11 @@ static inline void print_stack_trace(pid_t pid)

extern int mount_detached_fs(const char *fsname);

extern int cr_fsopen(const char *fsname, unsigned int flags);
extern int cr_fsconfig(int fd, unsigned int cmd, const char *key, const char *value, int aux);
extern int cr_fsmount(int fd, unsigned int flags, unsigned int attr_flags);
extern void fsfd_dump_messages(int fd);

extern char *get_legacy_iptables_bin(bool ipv6, bool restore);

extern int set_opts_cap_eff(void);
Expand Down
62 changes: 58 additions & 4 deletions criu/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#include "mem.h"
#include "namespaces.h"
#include "criu-log.h"
#include "syscall.h"
#include "util-caps.h"

#include "clone-noasan.h"
Expand Down Expand Up @@ -1556,23 +1555,78 @@ void print_stack_trace(pid_t pid)
}
#endif

int cr_fsopen(const char *fsname, unsigned int flags)
{
return syscall(__NR_fsopen, fsname, flags);
}

int cr_fsconfig(int fd, unsigned int cmd, const char *key, const char *value, int aux)
{
int ret = syscall(__NR_fsconfig, fd, cmd, key, value, aux);
if (ret)
fsfd_dump_messages(fd);
return ret;
}

int cr_fsmount(int fd, unsigned int flags, unsigned int attr_flags)
{
int ret = syscall(__NR_fsmount, fd, flags, attr_flags);
if (ret)
fsfd_dump_messages(fd);
return ret;
}

void fsfd_dump_messages(int fd)
{
char buf[4096];

Check warning on line 1581 in criu/util.c

View workflow job for this annotation

GitHub Actions / build

int err, n;

err = errno;

for (;;) {
n = read(fd, buf, sizeof(buf) - 1);
if (n < 0) {
if (errno != ENODATA)
pr_perror("Unable to read from fs descriptor");
break;
}
buf[n] = 0;

switch (buf[0]) {
case 'w':
pr_warn("%s\n", buf);
break;
case 'i':
pr_info("%s\n", buf);
break;
case 'e':
/* fallthrough */
default:
pr_err("%s\n", buf);
break;
}
}

errno = err;
}

int mount_detached_fs(const char *fsname)
{
int fsfd, fd;

fsfd = sys_fsopen(fsname, 0);
fsfd = cr_fsopen(fsname, 0);
if (fsfd < 0) {
pr_perror("Unable to open the %s file system", fsname);
return -1;
}

if (sys_fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) < 0) {
if (cr_fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) < 0) {
pr_perror("Unable to create the %s file system", fsname);
close(fsfd);
return -1;
}

fd = sys_fsmount(fsfd, 0, 0);
fd = cr_fsmount(fsfd, 0, 0);
if (fd < 0)
pr_perror("Unable to mount the %s file system", fsname);
close(fsfd);
Expand Down

0 comments on commit 6aafc9d

Please sign in to comment.