[CRIU] [PATCH 1/4] sfd: Move service fd helpers into separate file
Cyrill Gorcunov
gorcunov at openvz.org
Tue May 30 11:13:30 PDT 2017
util.c is too heavy already.
Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
criu/Makefile.crtools | 1 +
criu/servicefd.c | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++
criu/util.c | 123 ---------------------------------------------
3 files changed, 135 insertions(+), 123 deletions(-)
create mode 100644 criu/servicefd.c
diff --git a/criu/Makefile.crtools b/criu/Makefile.crtools
index 9ce0652757ad..1af35669f7bc 100644
--- a/criu/Makefile.crtools
+++ b/criu/Makefile.crtools
@@ -87,6 +87,7 @@ obj-y += path.o
obj-y += autofs.o
obj-y += fdstore.o
obj-y += uffd.o
+obj-y += servicefd.o
ifeq ($(VDSO),y)
obj-y += pie-util-vdso.o
diff --git a/criu/servicefd.c b/criu/servicefd.c
new file mode 100644
index 000000000000..185021d2ef25
--- /dev/null
+++ b/criu/servicefd.c
@@ -0,0 +1,134 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include <sys/time.h>
+#include <sys/syscall.h>
+#include <sys/resource.h>
+
+#include "servicefd.h"
+#include "bitops.h"
+#include "log.h"
+
+#include "common/bug.h"
+
+#undef LOG_PREFIX
+#define LOG_PREFIX "sfd: "
+
+static DECLARE_BITMAP(sfd_map, SERVICE_FD_MAX);
+
+static int service_fd_rlim_cur;
+static int service_fd_id;
+
+int init_service_fd(void)
+{
+ struct rlimit64 rlimit;
+
+ /*
+ * Service FDs are those that most likely won't
+ * conflict with any 'real-life' ones
+ */
+ if (syscall(__NR_prlimit64, getpid(), RLIMIT_NOFILE, NULL, &rlimit)) {
+ pr_perror("Can't get rlimit");
+ return -1;
+ }
+
+ service_fd_rlim_cur = (int)rlimit.rlim_cur;
+ BUG_ON(service_fd_rlim_cur < SERVICE_FD_MAX);
+
+ return 0;
+}
+
+static int __get_service_fd(enum sfd_type type, int service_fd_id)
+{
+ return service_fd_rlim_cur - type - SERVICE_FD_MAX * service_fd_id;
+}
+
+int service_fd_min_fd(void)
+{
+ return service_fd_rlim_cur - (SERVICE_FD_MAX - 1) - SERVICE_FD_MAX * service_fd_id;
+}
+
+int reserve_service_fd(enum sfd_type type)
+{
+ int sfd = __get_service_fd(type, service_fd_id);
+
+ BUG_ON((int)type <= SERVICE_FD_MIN || (int)type >= SERVICE_FD_MAX);
+
+ set_bit(type, sfd_map);
+ return sfd;
+}
+
+int install_service_fd(enum sfd_type type, int fd)
+{
+ int sfd = __get_service_fd(type, service_fd_id);
+
+ BUG_ON((int)type <= SERVICE_FD_MIN || (int)type >= SERVICE_FD_MAX);
+
+ if (dup3(fd, sfd, O_CLOEXEC) != sfd) {
+ pr_perror("Dup %d -> %d failed", fd, sfd);
+ return -1;
+ }
+
+ set_bit(type, sfd_map);
+ return sfd;
+}
+
+int get_service_fd(enum sfd_type type)
+{
+ BUG_ON((int)type <= SERVICE_FD_MIN || (int)type >= SERVICE_FD_MAX);
+
+ if (!test_bit(type, sfd_map))
+ return -1;
+
+ return __get_service_fd(type, service_fd_id);
+}
+
+int close_service_fd(enum sfd_type type)
+{
+ int fd = get_service_fd(type);
+ if (fd < 0)
+ return 0;
+
+ close(fd);
+ clear_bit(type, sfd_map);
+ return 0;
+}
+
+int clone_service_fd(int id)
+{
+ int ret = -1, i;
+
+ if (service_fd_id == id)
+ return 0;
+
+ for (i = SERVICE_FD_MIN + 1; i < SERVICE_FD_MAX; i++) {
+ int old = get_service_fd(i);
+ int new = __get_service_fd(i, id);
+
+ if (old < 0)
+ continue;
+ ret = dup2(old, new);
+ if (ret == -1) {
+ if (errno == EBADF)
+ continue;
+ pr_perror("Unable to clone %d->%d", old, new);
+ }
+ }
+
+ service_fd_id = id;
+ ret = 0;
+
+ return ret;
+}
+
+bool is_any_service_fd(int fd)
+{
+ return fd > __get_service_fd(SERVICE_FD_MAX, service_fd_id) &&
+ fd < __get_service_fd(SERVICE_FD_MIN, service_fd_id);
+}
+
+bool is_service_fd(int fd, enum sfd_type type)
+{
+ return fd == get_service_fd(type);
+}
diff --git a/criu/util.c b/criu/util.c
index 3363eef99f6b..cbff76c948b5 100644
--- a/criu/util.c
+++ b/criu/util.c
@@ -388,134 +388,11 @@ int do_open_proc(pid_t pid, int flags, const char *fmt, ...)
return openat(dirfd, path, flags);
}
-static int service_fd_rlim_cur;
-static int service_fd_id = 0;
-
-int init_service_fd(void)
-{
- struct rlimit64 rlimit;
-
- /*
- * Service FDs are those that most likely won't
- * conflict with any 'real-life' ones
- */
-
- if (syscall(__NR_prlimit64, getpid(), RLIMIT_NOFILE, NULL, &rlimit)) {
- pr_perror("Can't get rlimit");
- return -1;
- }
-
- service_fd_rlim_cur = (int)rlimit.rlim_cur;
- BUG_ON(service_fd_rlim_cur < SERVICE_FD_MAX);
-
- return 0;
-}
-
-static int __get_service_fd(enum sfd_type type, int service_fd_id)
-{
- return service_fd_rlim_cur - type - SERVICE_FD_MAX * service_fd_id;
-}
-
-int service_fd_min_fd(void)
-{
- return service_fd_rlim_cur - (SERVICE_FD_MAX - 1) - SERVICE_FD_MAX * service_fd_id;
-}
-
-static DECLARE_BITMAP(sfd_map, SERVICE_FD_MAX);
-
-int reserve_service_fd(enum sfd_type type)
-{
- int sfd = __get_service_fd(type, service_fd_id);
-
- BUG_ON((int)type <= SERVICE_FD_MIN || (int)type >= SERVICE_FD_MAX);
-
- set_bit(type, sfd_map);
- return sfd;
-}
-
-int install_service_fd(enum sfd_type type, int fd)
-{
- int sfd = __get_service_fd(type, service_fd_id);
-
- BUG_ON((int)type <= SERVICE_FD_MIN || (int)type >= SERVICE_FD_MAX);
-
- if (dup3(fd, sfd, O_CLOEXEC) != sfd) {
- pr_perror("Dup %d -> %d failed", fd, sfd);
- return -1;
- }
-
- set_bit(type, sfd_map);
- return sfd;
-}
-
-int get_service_fd(enum sfd_type type)
-{
- BUG_ON((int)type <= SERVICE_FD_MIN || (int)type >= SERVICE_FD_MAX);
-
- if (!test_bit(type, sfd_map))
- return -1;
-
- return __get_service_fd(type, service_fd_id);
-}
-
int criu_get_image_dir(void)
{
return get_service_fd(IMG_FD_OFF);
}
-int close_service_fd(enum sfd_type type)
-{
- int fd;
-
- fd = get_service_fd(type);
- if (fd < 0)
- return 0;
-
- if (close_safe(&fd))
- return -1;
-
- clear_bit(type, sfd_map);
- return 0;
-}
-
-int clone_service_fd(int id)
-{
- int ret = -1, i;
-
- if (service_fd_id == id)
- return 0;
-
- for (i = SERVICE_FD_MIN + 1; i < SERVICE_FD_MAX; i++) {
- int old = get_service_fd(i);
- int new = __get_service_fd(i, id);
-
- if (old < 0)
- continue;
- ret = dup2(old, new);
- if (ret == -1) {
- if (errno == EBADF)
- continue;
- pr_perror("Unable to clone %d->%d", old, new);
- }
- }
-
- service_fd_id = id;
- ret = 0;
-
- return ret;
-}
-
-bool is_any_service_fd(int fd)
-{
- return fd > __get_service_fd(SERVICE_FD_MAX, service_fd_id) &&
- fd < __get_service_fd(SERVICE_FD_MIN, service_fd_id);
-}
-
-bool is_service_fd(int fd, enum sfd_type type)
-{
- return fd == get_service_fd(type);
-}
-
int copy_file(int fd_in, int fd_out, size_t bytes)
{
ssize_t written = 0;
--
2.7.4
More information about the CRIU
mailing list