[CRIU] [PATCH 04/15] util: clone service descriptors, if fd tables are shared for tasks

Andrey Vagin avagin at openvz.org
Thu Jan 10 05:00:29 EST 2013


It looks like a namespace for service descriptors.
It will be used for restoring tasks with shared fd tables.
Service descriptors should be own for each process.

v2: clone_service_fd doesn't know about sub-systems like log, proc, etc

Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
 include/crtools.h |  1 +
 util.c            | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 66 insertions(+), 5 deletions(-)

diff --git a/include/crtools.h b/include/crtools.h
index 08c9655..766f1c0 100644
--- a/include/crtools.h
+++ b/include/crtools.h
@@ -121,6 +121,7 @@ enum sfd_type {
 	SERVICE_FD_MAX
 };
 
+extern int clone_service_fd(bool to_init);
 extern int init_service_fd(void);
 extern int get_service_fd(enum sfd_type type);
 extern bool is_service_fd(int fd, enum sfd_type type);
diff --git a/util.c b/util.c
index 90c82f7..1d11478 100644
--- a/util.c
+++ b/util.c
@@ -33,6 +33,7 @@
 #include "asm/types.h"
 #include "list.h"
 #include "util.h"
+#include "lock.h"
 
 #include "crtools.h"
 
@@ -280,6 +281,8 @@ int do_open_proc(pid_t pid, int flags, const char *fmt, ...)
 }
 
 static int service_fd_rlim_cur;
+static int service_fd_id = 0;
+static mutex_t *service_fd_lock;
 
 int init_service_fd(void)
 {
@@ -298,24 +301,81 @@ int init_service_fd(void)
 	service_fd_rlim_cur = (int)rlimit.rlim_cur;
 	BUG_ON(service_fd_rlim_cur < SERVICE_FD_MAX);
 
+	service_fd_lock = shmalloc(sizeof(mutex_t));
+	if (service_fd_lock == NULL)
+		return -1;
+
+	mutex_init(service_fd_lock);
+
 	return 0;
 }
 
-static int __get_service_fd(enum sfd_type type)
+static int __get_service_fd(enum sfd_type type, int service_fd_id)
 {
-	return service_fd_rlim_cur - type;
+	return service_fd_rlim_cur - type - SERVICE_FD_MAX * service_fd_id;
 }
 
 int get_service_fd(enum sfd_type type)
 {
 	BUG_ON((int)type <= SERVICE_FD_MIN || (int)type >= SERVICE_FD_MAX);
-	return __get_service_fd(type);
+	return __get_service_fd(type, service_fd_id);
+}
+
+int clone_service_fd(bool to_init)
+{
+	int ret = -1, i;
+	int service_fd_old_id = service_fd_id;
+
+	if (to_init && service_fd_id == 0)
+		return 0;
+
+	mutex_lock(service_fd_lock);
+
+	if (to_init)
+		service_fd_id = 0;
+	else {
+		/*
+		 * Find a free service fd namespace. We suppose,
+		 * that LOG_FD_OFF should be opened all time, so
+		 * if LOG_FD_OFF isn't busy, a service fd namespace is free.
+		 */
+		while (1) {
+			service_fd_id++;
+
+			if (fcntl(get_service_fd(LOG_FD_OFF), F_GETFD) != -1)
+				continue;
+
+			if (errno == EBADF)
+				break;
+
+			pr_perror("fcntl failed\n");
+			goto out;
+		}
+	}
+
+	for (i = SERVICE_FD_MIN + 1; i < SERVICE_FD_MAX; i++) {
+		int old = __get_service_fd(i, service_fd_old_id);
+		int new = __get_service_fd(i, service_fd_id);
+
+		ret = dup2(old, new);
+		if (ret == -1) {
+			if (errno == EBADF)
+				continue;
+			pr_perror("Unalbe to clone %d->%d\n", old, new);
+		}
+	}
+
+	ret = 0;
+out:
+	mutex_unlock(service_fd_lock);
+
+	return ret;
 }
 
 bool is_any_service_fd(int fd)
 {
-	return fd > __get_service_fd(SERVICE_FD_MAX) &&
-		fd < __get_service_fd(SERVICE_FD_MIN);
+	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)
-- 
1.7.11.7



More information about the CRIU mailing list