[CRIU] [PATCH 08/14] pstree: allocate fdt shared data

Andrey Vagin avagin at openvz.org
Fri Jan 11 04:22:39 EST 2013


fdt shared data contains PID of process, which will restore file
descriptors and a futex for synchronization.

A process with mimimal pid restores file descriptors.

Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
 include/crtools.h |  9 +++++++++
 include/pstree.h  |  8 ++++++++
 pstree.c          | 34 ++++++++++++++++++++++++++++++++++
 3 files changed, 51 insertions(+)

diff --git a/include/crtools.h b/include/crtools.h
index a51998d..0afa2a1 100644
--- a/include/crtools.h
+++ b/include/crtools.h
@@ -8,6 +8,7 @@
 #include "list.h"
 #include "util.h"
 #include "image.h"
+#include "lock.h"
 
 #include "../protobuf/vma.pb-c.h"
 
@@ -229,6 +230,12 @@ struct vma_area {
 #define vma_area_is(vma_area, s)	vma_entry_is(&((vma_area)->vma), s)
 #define vma_area_len(vma_area)		vma_entry_len(&((vma_area)->vma))
 
+struct fdt {
+	futex_t			fdt_lock;
+	int			nr;
+	pid_t			pid;
+};
+
 struct rst_info {
 	struct list_head	fds;
 	struct list_head	eventpoll;
@@ -236,6 +243,8 @@ struct rst_info {
 
 	void			*premmapped_addr;
 	unsigned long		premmapped_len;
+
+	struct fdt		*fdt;
 };
 
 static inline int in_vma_area(struct vma_area *vma, unsigned long addr)
diff --git a/include/pstree.h b/include/pstree.h
index 7e1a40c..f53d1f6 100644
--- a/include/pstree.h
+++ b/include/pstree.h
@@ -46,6 +46,14 @@ struct pstree_item {
 	struct rst_info		rst[0];
 };
 
+static inline int shared_fdtable(struct pstree_item *item) {
+	return (item->parent && item->parent->state != TASK_HELPER &&
+		item->ids &&
+		item->parent->ids &&
+		item->ids->files_id &&
+		item->ids->files_id == item->parent->ids->files_id);
+}
+
 extern void free_pstree(struct pstree_item *root_item);
 extern struct pstree_item *__alloc_pstree_item(bool rst);
 #define alloc_pstree_item() __alloc_pstree_item(false)
diff --git a/pstree.c b/pstree.c
index 2e2171b..85371d7 100644
--- a/pstree.c
+++ b/pstree.c
@@ -5,6 +5,7 @@
 #include "pstree.h"
 #include "restorer.h"
 #include "util.h"
+#include "lock.h"
 
 #include "protobuf.h"
 #include "protobuf/pstree.pb-c.h"
@@ -434,6 +435,39 @@ static int prepare_pstree_ids(void)
 				helper->pid.virt, helper->pgid);
 	}
 
+	/* Find a process with minimal pid for shared fd tables */
+	for_each_pstree_item(item) {
+		struct pstree_item *parent = item->parent;
+		struct fdt *fdt;
+
+		if (item->state == TASK_HELPER)
+			continue;
+
+		if (parent == NULL)
+			continue;
+
+		if (!shared_fdtable(item))
+			continue;
+
+		if (!parent->rst->fdt) {
+			fdt = shmalloc(sizeof(*item->rst->fdt));
+			if (fdt == NULL)
+				return -1;
+
+			parent->rst->fdt = fdt;
+
+			futex_init(&fdt->fdt_lock);
+			fdt->nr = 1;
+			fdt->pid = parent->pid.virt;
+		} else
+			fdt = parent->rst->fdt;
+
+		item->rst->fdt = fdt;
+		fdt->nr++;
+		if (fdt->pid > item->pid.virt)
+			fdt->pid = item->pid.virt;
+	}
+
 	return 0;
 }
 
-- 
1.7.11.7



More information about the CRIU mailing list