[RESEND,v1,26/55] pstree: Split lookup_create_pid()

Submitted by Kirill Tkhai on March 24, 2017, 3:13 p.m.

Details

Message ID 149036839116.23093.4674321127879978908.stgit@localhost.localdomain
State New
Series "Nested pid namespaces support"
Headers show

Commit Message

Kirill Tkhai March 24, 2017, 3:13 p.m.
Extract the function, which seaches for existing pid.
In next patches we will use it.

Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
---
 criu/pstree.c |   36 +++++++++++++++++++++++++-----------
 1 file changed, 25 insertions(+), 11 deletions(-)

Patch hide | download patch | download mbox

diff --git a/criu/pstree.c b/criu/pstree.c
index 601bf136..da2dc9fb 100644
--- a/criu/pstree.c
+++ b/criu/pstree.c
@@ -406,28 +406,42 @@  static int prepare_pstree_for_shell_job(void)
 	return 0;
 }
 
-/*
- * Try to find a pid node in the tree and insert a new one,
- * it is not there yet. If pid_node isn't set, pstree_item
- * is inserted.
- */
-static struct pid *lookup_create_pid(pid_t pid, struct pid *pid_node)
+static struct pid *find_pid_or_place_in_hier(struct rb_node **root, pid_t pid, int level,
+					     struct rb_node **ret_parent, struct rb_node ***ret_place)
 {
-	struct rb_node *node = pid_root_rb.rb_node;
-	struct rb_node **new = &pid_root_rb.rb_node;
+	struct rb_node *node = *root;
+	struct rb_node **new = root;
 	struct rb_node *parent = NULL;
 
 	while (node) {
-		struct pid *this = rb_entry(node, struct pid, ns[0].node);
+		struct pid *this = rb_entry(node, struct pid, ns[level].node);
 
 		parent = *new;
-		if (pid < this->ns[0].virt)
+		if (pid < this->ns[level].virt)
 			node = node->rb_left, new = &((*new)->rb_left);
-		else if (pid > this->ns[0].virt)
+		else if (pid > this->ns[level].virt)
 			node = node->rb_right, new = &((*new)->rb_right);
 		else
 			return this;
 	}
+	*ret_parent = parent;
+	*ret_place = new;
+	return NULL;
+}
+
+/*
+ * Try to find a pid node in the tree and insert a new one,
+ * it is not there yet. If pid_node isn't set, pstree_item
+ * is inserted.
+ */
+static struct pid *lookup_create_pid(pid_t pid, struct pid *pid_node)
+{
+	struct rb_node **new = NULL, *parent = NULL;
+	struct pid *found;
+
+	found = find_pid_or_place_in_hier(&pid_root_rb.rb_node, pid, 0, &parent, &new);
+	if (found)
+		return found;
 
 	if (!pid_node) {
 		struct pstree_item *item;