[CRIU] [PATCH 04/11] pstree: add helper for_each_pssubtree_item to walk item's subtree

Pavel Tikhomirov ptikhomirov at virtuozzo.com
Fri May 26 10:02:47 PDT 2017


Need it to lookup adoptive children of pidns init. Also add
skip_descendants flag to be able to skip unneeded subtrees.

Signed-off-by: Pavel Tikhomirov <ptikhomirov at virtuozzo.com>
---
 criu/include/pstree.h |  4 ++++
 criu/pstree.c         | 15 +++++++++++----
 2 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/criu/include/pstree.h b/criu/include/pstree.h
index bae5ce6..85679d6 100644
--- a/criu/include/pstree.h
+++ b/criu/include/pstree.h
@@ -106,8 +106,12 @@ static inline struct pid *pstree_pid_by_virt(pid_t pid)
 
 extern struct pstree_item *root_item;
 extern struct pstree_item *pstree_item_next(struct pstree_item *item);
+extern struct pstree_item *pssubtree_item_next(struct pstree_item *item,
+		struct pstree_item *root, bool skip_descendants);
 #define for_each_pstree_item(pi) \
 	for (pi = root_item; pi != NULL; pi = pstree_item_next(pi))
+#define for_each_pssubtree_item(pi, root) \
+	for (pi = root; pi != NULL; pi = pssubtree_item_next(pi, root, false))
 
 extern bool restore_before_setsid(struct pstree_item *child);
 extern int prepare_pstree(void);
diff --git a/criu/pstree.c b/criu/pstree.c
index 1fe5f2d..2f183f7 100644
--- a/criu/pstree.c
+++ b/criu/pstree.c
@@ -267,13 +267,14 @@ int init_pstree_helper(struct pstree_item *ret)
 	return 0;
 }
 
-/* Deep first search on children */
-struct pstree_item *pstree_item_next(struct pstree_item *item)
+/* Search only root's subtree, possibly skipping descendants */
+struct pstree_item *pssubtree_item_next(struct pstree_item *item,
+		struct pstree_item *root, bool skip_descendants)
 {
-	if (!list_empty(&item->children))
+	if (!skip_descendants && !list_empty(&item->children))
 		return list_first_entry(&item->children, struct pstree_item, sibling);
 
-	while (item->parent) {
+	while (item->parent && item != root) {
 		if (item->sibling.next != &item->parent->children)
 			return list_entry(item->sibling.next, struct pstree_item, sibling);
 		item = item->parent;
@@ -282,6 +283,12 @@ struct pstree_item *pstree_item_next(struct pstree_item *item)
 	return NULL;
 }
 
+/* Deep first search on children */
+struct pstree_item *pstree_item_next(struct pstree_item *item)
+{
+	return pssubtree_item_next(item, NULL, false);
+}
+
 /* Preorder traversal of pstree item */
 int preorder_pstree_traversal(struct pstree_item *item, int (*f)(struct pstree_item *))
 {
-- 
2.9.3



More information about the CRIU mailing list