All of lore.kernel.org
 help / color / mirror / Atom feed
From: James Carter <jwcart2@gmail.com>
To: selinux@vger.kernel.org
Cc: dburgener@linux.microsoft.com, James Carter <jwcart2@gmail.com>
Subject: [PATCH 3/9 v3] libsepol/cil: Add cil_tree_node_remove function
Date: Thu, 13 Apr 2023 15:34:39 -0400	[thread overview]
Message-ID: <20230413193445.588395-4-jwcart2@gmail.com> (raw)
In-Reply-To: <20230413193445.588395-1-jwcart2@gmail.com>

Add the function cil_tree_node_remove() which takes a node pointer
as an input, finds the parent, walks the list of nodes to the node
prior to the given node, updates that node's next pointer to remove
the given node from the tree, and then destroys the node.

Signed-off-by: James Carter <jwcart2@gmail.com>
---
v3:
 - Check for cl_tail pointing to the node
 - Destroy the node before returning if cl_head points to the node

 libsepol/cil/src/cil_tree.c | 35 +++++++++++++++++++++++++++++++++++
 libsepol/cil/src/cil_tree.h |  1 +
 2 files changed, 36 insertions(+)

diff --git a/libsepol/cil/src/cil_tree.c b/libsepol/cil/src/cil_tree.c
index 6376c208..1155e311 100644
--- a/libsepol/cil/src/cil_tree.c
+++ b/libsepol/cil/src/cil_tree.c
@@ -248,6 +248,41 @@ void cil_tree_node_destroy(struct cil_tree_node **node)
 	*node = NULL;
 }
 
+void cil_tree_node_remove(struct cil_tree_node *node)
+{
+	struct cil_tree_node *parent, *curr;
+
+	if (node == NULL || node->parent == NULL) {
+		return;
+	}
+
+	parent = node->parent;
+
+	if (parent->cl_head == node) {
+		if (parent->cl_tail == node) {
+			parent->cl_tail = NULL;
+		}
+		parent->cl_head = node->next;
+		cil_tree_node_destroy(&node);
+		return;
+	}
+
+	curr = parent->cl_head;
+	while (curr && curr->next != node) {
+		curr = curr->next;
+	}
+
+	if (curr == NULL) {
+		return;
+	}
+
+	if (parent->cl_tail == node) {
+		parent->cl_tail = curr;
+	}
+	curr->next = node->next;
+	cil_tree_node_destroy(&node);
+}
+
 /* Perform depth-first walk of the tree
    Parameters:
    start_node:          root node to start walking from
diff --git a/libsepol/cil/src/cil_tree.h b/libsepol/cil/src/cil_tree.h
index 5a98da55..cb6a0d24 100644
--- a/libsepol/cil/src/cil_tree.h
+++ b/libsepol/cil/src/cil_tree.h
@@ -63,6 +63,7 @@ void cil_tree_children_destroy(struct cil_tree_node *node);
 
 void cil_tree_node_init(struct cil_tree_node **node);
 void cil_tree_node_destroy(struct cil_tree_node **node);
+void cil_tree_node_remove(struct cil_tree_node *node);
 
 //finished values
 #define CIL_TREE_SKIP_NOTHING	0
-- 
2.39.2


  parent reply	other threads:[~2023-04-13 19:34 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-13 19:34 [PATCH 0/9 v3] Add CIL Deny Rule James Carter
2023-04-13 19:34 ` [PATCH 1/9 v3] libsepol/cil: Parse and add deny rule to AST, but do not process James Carter
2023-04-13 19:34 ` [PATCH 2/9 v3] libsepol/cil: Add cil_list_is_empty macro James Carter
2023-04-13 19:34 ` James Carter [this message]
2023-04-13 19:34 ` [PATCH 4/9 v3] libsepol/cil: Process deny rules James Carter
2023-04-13 19:34 ` [PATCH 5/9 v3] libsepol/cil: Add cil_write_post_ast function James Carter
2023-04-13 19:34 ` [PATCH 6/9 v3] libsepol: Export the " James Carter
2023-04-13 19:34 ` [PATCH 7/9 v3] secilc/secil2tree: Add option to write CIL AST after post processing James Carter
2023-04-13 19:34 ` [PATCH 8/9 v3] secilc/test: Add deny rule tests James Carter
2023-04-13 19:34 ` [PATCH 9/9 v3] secilc/docs: Add deny rule to CIL documentation James Carter
2023-07-26 15:55   ` Daniel Burgener
2023-07-26 15:56 ` [PATCH 0/9 v3] Add CIL Deny Rule Daniel Burgener

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230413193445.588395-4-jwcart2@gmail.com \
    --to=jwcart2@gmail.com \
    --cc=dburgener@linux.microsoft.com \
    --cc=selinux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.