[CRIU] Re: on servie fd handling code
Cyrill Gorcunov
gorcunov at openvz.org
Thu Sep 6 06:32:43 EDT 2012
Does the one attached look better?
-------------- next part --------------
>From ef4458603b318a2295de2414aaedbd9ecab5ab75 Mon Sep 17 00:00:00 2001
From: Cyrill Gorcunov <gorcunov at openvz.org>
Date: Thu, 6 Sep 2012 12:07:56 +0400
Subject: [PATCH] service-fd: Rework helpers fd handling v2
While being addressing feedback from tty
c/r patch I found that service fd code could
be transferred to own .ch files and a bit reworked.
With this patch we move all code related to service
fd handling into service-fd.[ch] and add init_service_fd
helper which will fetch current limits from the tool
startup time. Thus instead of calling getrlimit
everytime we call for get_service_fd, we just do
that once.
v2 (by xemul@):
- no need to carry the whole rlim structure
- add is_service_fd helper
Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
Makefile | 1 +
crtools.c | 3 +++
include/crtools.h | 10 ----------
include/service-fd.h | 22 ++++++++++++++++++++++
include/util.h | 2 ++
service-fd.c | 43 +++++++++++++++++++++++++++++++++++++++++++
util.c | 17 -----------------
7 files changed, 71 insertions(+), 27 deletions(-)
create mode 100644 include/service-fd.h
create mode 100644 service-fd.c
diff --git a/Makefile b/Makefile
index c5ee775..aea0a8e 100644
--- a/Makefile
+++ b/Makefile
@@ -42,6 +42,7 @@ OBJS += inotify.o
OBJS += signalfd.o
OBJS += pstree.o
OBJS += protobuf.o
+OBJS += service-fd.o
PROTOBUF-LIB := protobuf/protobuf-lib.o
diff --git a/crtools.c b/crtools.c
index 901a6c7..1a5eb6d 100644
--- a/crtools.c
+++ b/crtools.c
@@ -73,6 +73,9 @@ int main(int argc, char *argv[])
opts.final_state = TASK_DEAD;
INIT_LIST_HEAD(&opts.veth_pairs);
+ if (init_service_fd())
+ return -1;
+
while (1) {
static struct option long_opts[] = {
{ "tree", required_argument, 0, 't' },
diff --git a/include/crtools.h b/include/crtools.h
index 8ad8ce8..a7ce246 100644
--- a/include/crtools.h
+++ b/include/crtools.h
@@ -98,16 +98,6 @@ struct cr_options {
extern struct cr_options opts;
-enum {
- LOG_FD_OFF = 1,
- LOG_DIR_FD_OFF,
- IMG_FD_OFF,
- SELF_EXE_FD_OFF,
- PROC_FD_OFF,
-};
-
-int get_service_fd(int type);
-
/* file descriptors template */
struct cr_fd_desc_tmpl {
const char *fmt; /* format for the name */
diff --git a/include/service-fd.h b/include/service-fd.h
new file mode 100644
index 0000000..836f8fd
--- /dev/null
+++ b/include/service-fd.h
@@ -0,0 +1,22 @@
+#ifndef SERVICE_FD_H__
+#define SERVICE_FD_H__
+
+#include "compiler.h"
+#include "types.h"
+
+enum {
+ LOG_FD_OFF = 1,
+ LOG_DIR_FD_OFF,
+ IMG_FD_OFF,
+ SELF_EXE_FD_OFF,
+ PROC_FD_OFF,
+ CTL_TTY_OFF,
+
+ SERVICE_FD_OFF__MAX,
+};
+
+extern int init_service_fd(void);
+extern int get_service_fd(int type);
+extern bool is_service_fd(int fd, int type);
+
+#endif /* SERVICE_FD_H__ */
diff --git a/include/util.h b/include/util.h
index 1daf451..eaa3baf 100644
--- a/include/util.h
+++ b/include/util.h
@@ -16,6 +16,8 @@
#include "types.h"
#include "log.h"
+#include "service-fd.h"
+
#include "../protobuf/vma.pb-c.h"
#define PREF_SHIFT_OP(pref, op, size) ((size) op (pref ##BYTES_SHIFT))
diff --git a/service-fd.c b/service-fd.c
new file mode 100644
index 0000000..817b490
--- /dev/null
+++ b/service-fd.c
@@ -0,0 +1,43 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <limits.h>
+
+#include <sys/time.h>
+#include <sys/resource.h>
+
+#include <fcntl.h>
+
+#include "service-fd.h"
+#include "log.h"
+
+static int rlim_cur;
+static int rlim_min;
+
+int init_service_fd(void)
+{
+ struct rlimit rlimit;
+
+ /*
+ * Service FDs are thouse that most likely won't
+ * conflict with any 'real-life' ones
+ */
+ if (getrlimit(RLIMIT_NOFILE, &rlimit)) {
+ pr_perror("Can't get rlimit");
+ return -1;
+ }
+
+ rlim_cur = (int)rlimit.rlim_cur;
+ rlim_min = rlim_cur - SERVICE_FD_OFF__MAX;
+ return 0;
+}
+
+int get_service_fd(int type)
+{
+ return rlim_cur - type;
+}
+
+bool is_service_fd(int fd, int type)
+{
+ return fd > rlim_min && fd == get_service_fd(type);
+}
diff --git a/util.c b/util.c
index 1fad14c..6f6cf6d 100644
--- a/util.c
+++ b/util.c
@@ -224,23 +224,6 @@ int do_open_proc(pid_t pid, int flags, const char *fmt, ...)
return openat(dirfd, path, flags);
}
-int get_service_fd(int type)
-{
- struct rlimit rlimit;
-
- /*
- * Service FDs are thouse that most likely won't
- * conflict with any 'real-life' ones
- */
-
- if (getrlimit(RLIMIT_NOFILE, &rlimit)) {
- pr_perror("Can't get rlimit");
- return -1;
- }
-
- return rlimit.rlim_cur - type;
-}
-
int copy_file(int fd_in, int fd_out, size_t bytes)
{
ssize_t written = 0;
--
1.7.7.6
More information about the CRIU
mailing list