From 1079a51f834cb3b52e0152b9d309cd67fa52cb01 Mon Sep 17 00:00:00 2001 From: Andrei Vagin Date: Thu, 12 Sep 2024 11:19:35 -0700 Subject: [PATCH] util: dump fsfd log messages It should help to investigate errors of fsconfig, fsmount and etc. Signed-off-by: Andrei Vagin --- criu/cgroup.c | 18 +++++++----- criu/cr-check.c | 21 +++++++------- criu/include/syscall.h | 17 ------------ criu/include/util.h | 5 ++++ criu/util.c | 62 +++++++++++++++++++++++++++++++++++++++--- 5 files changed, 84 insertions(+), 39 deletions(-) delete mode 100644 criu/include/syscall.h diff --git a/criu/cgroup.c b/criu/cgroup.c index d90b70bb79..fcaed07080 100644 --- a/criu/cgroup.c +++ b/criu/cgroup.c @@ -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 @@ -581,14 +580,15 @@ 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; } @@ -596,7 +596,8 @@ static int __new_open_cgroupfs(struct cg_ctl *cc) 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; } @@ -604,14 +605,17 @@ static int __new_open_cgroupfs(struct cg_ctl *cc) } } - 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; diff --git a/criu/cr-check.c b/criu/cr-check.c index 507f9915ca..0388cbe7fe 100644 --- a/criu/cr-check.c +++ b/criu/cr-check.c @@ -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" @@ -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; @@ -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; diff --git a/criu/include/syscall.h b/criu/include/syscall.h deleted file mode 100644 index c38d6d971b..0000000000 --- a/criu/include/syscall.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef __CR_SYSCALL_H__ -#define __CR_SYSCALL_H__ - -static inline int sys_fsopen(const char *fsname, unsigned int flags) -{ - return syscall(__NR_fsopen, fsname, flags); -} -static inline int sys_fsconfig(int fd, unsigned int cmd, const char *key, const char *value, int aux) -{ - return syscall(__NR_fsconfig, fd, cmd, key, value, aux); -} -static inline int sys_fsmount(int fd, unsigned int flags, unsigned int attr_flags) -{ - return syscall(__NR_fsmount, fd, flags, attr_flags); -} - -#endif /* __CR_SYSCALL_H__ */ \ No newline at end of file diff --git a/criu/include/util.h b/criu/include/util.h index 435469e1ec..ae293a68c8 100644 --- a/criu/include/util.h +++ b/criu/include/util.h @@ -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); diff --git a/criu/util.c b/criu/util.c index 7dfa1fe424..d2bc9a8657 100644 --- a/criu/util.c +++ b/criu/util.c @@ -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" @@ -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]; + 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);