Skip to contentSkip to conversation

Add objset to libsepol

PATCH In review Karl MacMillan 2 participants 4 messages Apr 25, 2007 – Apr 27, 2007 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add objset to libsepol

Add the objset data structure to libsepol. Object sets behave similarly to Python sets.

---

 libsepol/include/sepol/objset.h |  169 +++++++++++++++++++++++++++++++++++++++
 libsepol/src/objset.c           |  154 ++++++++++++++++++++++++++++++++++++
 libsepol/tests/libsepol-tests.c |    2 
 libsepol/tests/test-objset.c    |  147 ++++++++++++++++++++++++++++++++++
 libsepol/tests/test-objset.h    |   12 +++
 5 files changed, 484 insertions(+), 0 deletions(-)
libsepol/include/sepol/objset.h new file +169 -0
new file mode 100644
index 0000000..14f49e8
@@ -0,0 +1,169 @@
+/* Author: Karl MacMillan <[email protected]> */
+
+#ifndef __sepol_objset_h__
+#define __sepol_objset_h__
+
+#include <sepol/iter.h>
+#include <sepol/handle.h>
+
+/**
+ * \defgroup libsepol_objset sepol_objset: sets of unique objects
+ * Object sets store sets of objects unique objects (think sets in
+ * the mathematical sense). The data structure is unordered.
+ * 
+ * For example, consider the following example:
+ *
+ * \code
+ * struct sepol_objset *set;
+ * struct sepol_handle *h;
+ * 
+ * // object set creation ommitted
+ * sepol_objset_add(h, set, "foo");
+ * sepol_objset_add(h, set, "bar");
+ * sepol_objset_add(h, set, "baz");
+ * sepol_objset_add(h, set, "foo");
+ * // at this point the objset contains the strings
+ * // "foo", "bar", and "baz". The duplicate "foo"
+ * // was not added.
+ * \endcode
+ *
+ * In contrast to the objpool data structure, objsets are designed to
+ * be space efficient and hold a relatively small number of objects.
+ * Additionally, the lifecycle management of the objects is not
+ * handled by objsets - it is the responsibility of the caller.
+ *
+ * It is possible to use objsets in conjunction with objpools. In this
+ * mode all of the objects should be added to the pool first so that
+ * the objset comparison function can become a simple pointer
+ * compare. This likely only makes sense when several objsets share a
+ * single objpool (each holding a different subset of the objects
+ * stored in the objpool).
+ */
+struct sepol_objset;
+
+/**
+ * \ingroup libsepol_objset
+ * Function type for comparison of objects stored an objset.
+ */
+typedef int (*sepol_objset_cmp_t)(struct sepol_objset *s, void *a, void *b);
+
+/**
+ * \ingroup libsepol_objset
+ * Create a new objset.
+ *
+ * @param h sepol handle (may be NULL)
+ * @param set out pointer for newly created objset
+ * @param cmp comparison function for objects stored in the set
+ *
+ * \retval SEPOL_ENOMEM out of memory
+ * \retval SEPOL_OK success
+ */
+extern int sepol_objset_create(struct sepol_handle *h, struct sepol_objset **set,
+			       sepol_objset_cmp_t cmp);
+
+/**
+ * \ingroup libsepol_objset
+ *
+ * Free an objset. The contained objects are not freed, only the
+ * objset itself.
+ *
+ * @param s objset to free
+ */
+extern void sepol_objset_free(struct sepol_objset *s);
+
+/**
+ * \ingroup libsepol_objset
+ *
+ * Return the length (number of objects) of the objset. This operation
+ * is constant time.
+ *
+ * @param s objset to determine length
+ *
+ * \retval >=0 number of objects in objset
+ */
+extern unsigned int sepol_objset_length(struct sepol_objset *s);
+
+/**
+ * \ingroup libsepol_objset
+ * Set the compare function.
+ *
+ * @param s objset
+ * @param cmp new compare function
+ *
+ * \retval SEPOL_OK success
+ */
+extern int sepol_objset_set_cmp(struct sepol_objset *s, sepol_objset_cmp_t cmp);
+
+/**
+ * \ingroup libsepol_objset
+ *
+ * Add an object to the object set. The object is only added if an
+ * identical object is not already present.
+ *
+ * @param h sepol handle (may be NULL)
+ * @param s objset to add object
+ * @param obj object to add
+ *
+ * \retval SEPOL_OK success (object was added)
+ * \retval SEPOL_EEXIST object not added because object already exists
+ * \retval SEPOL_ENOMEM out of memory
+ */
+extern int sepol_objset_add(struct sepol_handle *h, struct sepol_objset *s,
+			    void *obj);
+
+/**
+ * \ingroup libsepol_objset
+ *
+ * Delete an object from the object set.
+ *
+ * @param h sepol handle (may be NULL)
+ * @param s objset from which to delete object
+ * @param obj object to delete
+ *
+ * \retval SEPOL_OK success
+ * \retval SEPOL_ENOENT object does not exist in set
+ * \retval SEPOL_ENOMEM out of memory
+ */
+extern int sepol_objset_del(struct sepol_handle *h, struct sepol_objset *s,
+			    void *obj);
+
+/**
+ * \ingroup libsepol_objset
+ *
+ * Iterate over the objects in the set. The objset is unordered so the
+ * order the objects are returned from the iterator should not be
+ * depended upon.
+ *
+ * @param h sepol handle (may be NULL)
+ * @param s objset to iterate over
+ * @param iter already created iterator to set to initial position
+ *
+ * \retval SEPOL_OK success
+ * \retval SEPOL_ITERSTOP stop iteration (set is empty)
+ */
+extern int sepol_objset_iter(struct sepol_handle *h, struct sepol_objset *s,
+			     struct sepol_iter *iter);
+
+/**
+ * \ingroup libsepol_objset
+ *
+ * Mark this set as a compliment. This is simply a flag and does not change
+ * the contents of the set.
+ *
+ * @param s objset to compliment
+ * @param compliment value of compliment flag (0 or 1)
+ */
+extern void sepol_objset_set_compliment(struct sepol_objset *s, char compliment);
+
+/**
+ * \ingroup libsepol_objset
+ *
+ * Return the current value of the compliement flag.
+ *
+ * @param s objset from which to obtain the compliment value
+ *
+ * \retval 0|1 current value of compliment flag
+ */
+extern char sepol_objset_get_compliement(struct sepol_objset *s);
+
+#endif
libsepol/src/objset.c new file +154 -0

154 lines not on this page · raw message

libsepol/tests/libsepol-tests.c +2 -0
index 2fee01d..5156ada 100644
@@ -25,6 +25,7 @@
 #include "test-list.h"
 #include "test-hashtab.h"
 #include "test-objpool.h"
+#include "test-objset.h"
 
 #include <CUnit/Basic.h>
 #include <CUnit/Console.h>
@@ -67,6 +68,7 @@ static int do_tests(int interactive, int verbose)
 	DECLARE_SUITE(list);
 	DECLARE_SUITE(hashtab);
 	DECLARE_SUITE(objpool);
+	DECLARE_SUITE(objset);
 
 	if (verbose)
 		CU_basic_set_mode(CU_BRM_VERBOSE);
libsepol/tests/test-objset.c new file +147 -0

147 lines not on this page · raw message

libsepol/tests/test-objset.h new file +12 -0
new file mode 100644
index 0000000..d6ab340
@@ -0,0 +1,12 @@
+/* Author : Karl MacMillan <[email protected]> */
+
+#ifndef __test_objset_h__
+#define __test_objset_h__
+
+#include <CUnit/Basic.h>
+
+int objset_test_init(void);
+int objset_test_cleanup(void);
+int objset_add_tests(CU_pSuite suite);
+
+#endif

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#1 lore raw
Quoted 2 lines · Karl MacMillan

Add the objset data structure to libsepol. Object sets behave similarly to Python sets.

Umm, From: http://docs.python.org/lib/types-set.html

A set object is an unordered collection of immutable values.

...AIUI this will be an ordered collection of mutable values, though documented to maybe be unordered at some point?

Quoted 5 lines

---

 libsepol/include/sepol/objset.h |  169 +++++++++++++++++++++++++++++++++++++++
 libsepol/src/objset.c           |  154 ++++++++++++++++++++++++++++++++++++

One thing I thought when reviewing Josh's mamoth patch is that it might
be nice for review to post the unit tests seperately and mark them clearly. Just a thought.

Quoted 18 lines
libsepol/src/objset.c new file +12 -0
new file mode 100644
index 0000000..7b86e6b
@@ -0,0 +1,154 @@
+struct sepol_objset
+{
+	struct sepol_list *objs;
+	sepol_objset_cmp_t cmp;
+	unsigned int len;
+	char compliment;
+};
+
+int sepol_objset_create(struct sepol_handle *h, struct sepol_objset **set,
+			sepol_objset_cmp_t cmp)
+{
+	int ret;

libsepol/src/objset.c line 22

Quoted 10 lines
libsepol/src/objset.c new file +10 -0
+
+	*set = calloc(1, sizeof(struct sepol_objset));
+	if (*set == NULL)
+		return SEPOL_ENOMEM;
+
+	(*set)->cmp = cmp;
+
+	ret = sepol_list_create(h, &(*set)->objs);
+	if (ret < 0) {
+		free(set);

This should be free(*set).

Quoted 16 lines
libsepol/src/objset.c new file +16 -0
+		return ret;
+	}
+	
+	return SEPOL_OK;
+}
+
+int sepol_objset_add(struct sepol_handle *h, struct sepol_objset *s, void *obj)
+{
+	int ret, ret2, cmp;
+	struct sepol_iter *iter;
+	void *cur;
+
+	ret = sepol_iter_create(h, &iter);
+	if (ret < 0)
+		return ret;
+

libsepol/src/objset.c line 96

Quoted 10 lines
libsepol/src/objset.c new file +10 -0
+	ret = sepol_list_begin(h, s->objs, iter);
+	sepol_foreach(h, ret, cur, iter) {
+		cmp = s->cmp(s, cur, obj);
+		if (cmp == 0) {
+			ret = SEPOL_EEXIST;
+			goto out;
+		} else if (cmp > 0) {
+			ret = sepol_list_insert(h, s->objs, iter, obj);
+			goto out;
+		}

This implies an ordering, so you can't for Eg. add some elements and
then change the cmp function.

libsepol/src/objset.c line 108

Quoted 12 lines
libsepol/src/objset.c new file +12 -0
+	}
+	if (ret != SEPOL_ITERSTOP) {
+		ERR(h, "iteration ended abnormally");
+		goto out;
+	}
+	ret = sepol_list_append(h, s->objs, obj);
+out:
+	ret2 = sepol_iter_free(h, iter);
+	if (ret2 != SEPOL_OK) {
+		ERR(h, "error freeing iterator");
+		return ret2;
+	}

I think it's much more likely that you want to know ret and not ret2
here, if ret==SEPOL_EEXIST etc.

Quoted 13 lines
libsepol/src/objset.c new file +13 -0
+	return ret;
+}
+
+int sepol_objset_del(struct sepol_handle *h, struct sepol_objset *s, void *obj)
+{
+	int ret, ret2, cmp;
+	struct sepol_iter *iter;
+	void *cur;
+
+	ret = sepol_iter_create(h, &iter);
+	if (ret < 0)
+		return ret;
+

libsepol/src/objset.c line 131

Quoted 10 lines
libsepol/src/objset.c new file +10 -0
+	ret = sepol_list_begin(h, s->objs, iter);
+	sepol_foreach(h, ret, cur, iter) {
+		cmp = s->cmp(s, cur, obj);
+		if (cmp == 0) {
+			ret = sepol_list_del(h, s->objs, iter);
+			goto out;
+		}
+	}
+	if (ret == SEPOL_ITERSTOP)
+		ret = SEPOL_ENOENT;

You aren't using the ordering here, why is that?

libsepol/src/objset.c line 136

Quoted 5 lines
libsepol/src/objset.c new file +5 -0
+		
+out:
+	ret2 = sepol_iter_free(h, iter);
+	if (ret2 != SEPOL_OK)
+		return ret2;

Dito. Also no ERR() call?

libsepol/src/objset.c line 154

Quoted 11 lines
libsepol/src/objset.c new file +11 -0
+	return ret;	
+}
+void sepol_objset_set_compliment(struct sepol_objset *s, char compliment)
+{
+	s->compliment = compliment;
+}
+
+char sepol_objset_get_compliement(struct sepol_objset *s)
+{
+	return s->compliment;
+}

Spillink not soo god?;)

Also if you are going to add these, I think you want at least
sepol_objset_contains_obj() which does the right thing.

Signature · 1 line
James Antill <[email protected]>
#2 lore raw
Quoted 11 lines · James Antill
Quoted 2 lines · Karl MacMillan

Add the objset data structure to libsepol. Object sets behave similarly to Python sets.

Umm, From: http://docs.python.org/lib/types-set.html

A set object is an unordered collection of immutable values.

...AIUI this will be an ordered collection of mutable values, though documented to maybe be unordered at some point?

Hmm - I have been assuming that the values are immutable. If they change then the ordering and uniqueness may be lost.

Quoted 10 lines
Quoted 5 lines

---

 libsepol/include/sepol/objset.h |  169 +++++++++++++++++++++++++++++++++++++++
 libsepol/src/objset.c           |  154 ++++++++++++++++++++++++++++++++++++

One thing I thought when reviewing Josh's mamoth patch is that it might
be nice for review to post the unit tests seperately and mark them clearly. Just a thought.

Ok.

Quoted 22 lines
Quoted 22 lines
libsepol/src/objset.c new file +16 -0
new file mode 100644
index 0000000..7b86e6b
@@ -0,0 +1,154 @@
+struct sepol_objset
+{
+	struct sepol_list *objs;
+	sepol_objset_cmp_t cmp;
+	unsigned int len;
+	char compliment;
+};
+
+int sepol_objset_create(struct sepol_handle *h, struct sepol_objset **set,
+			sepol_objset_cmp_t cmp)
+{
+	int ret;
+
+	*set = calloc(1, sizeof(struct sepol_objset));
+	if (*set == NULL)
+		return SEPOL_ENOMEM;
Quoted 9 lines

libsepol/src/objset.c line 50

Quoted 6 lines
libsepol/src/objset.c new file +6 -0
+
+	(*set)->cmp = cmp;
+
+	ret = sepol_list_create(h, &(*set)->objs);
+	if (ret < 0) {
+		free(set);

This should be free(*set).

Good catch (as usual).

Quoted 21 lines
Quoted 21 lines
libsepol/src/objset.c new file +21 -0
+		return ret;
+	}
+	
+	return SEPOL_OK;
+}
+
+int sepol_objset_add(struct sepol_handle *h, struct sepol_objset *s, void *obj)
+{
+	int ret, ret2, cmp;
+	struct sepol_iter *iter;
+	void *cur;
+
+	ret = sepol_iter_create(h, &iter);
+	if (ret < 0)
+		return ret;
+
+	ret = sepol_list_begin(h, s->objs, iter);
+	sepol_foreach(h, ret, cur, iter) {
+		cmp = s->cmp(s, cur, obj);
+		if (cmp == 0) {
+			ret = SEPOL_EEXIST;
Quoted 9 lines

libsepol/src/objset.c line 96

Quoted 5 lines
libsepol/src/objset.c new file +5 -0
+			goto out;
+		} else if (cmp > 0) {
+			ret = sepol_list_insert(h, s->objs, iter, obj);
+			goto out;
+		}

This implies an ordering, so you can't for Eg. add some elements and
then change the cmp function.

Yes - perhaps I should remove objset_set_cmp?

Quoted 7 lines
Quoted 7 lines
libsepol/src/objset.c new file +7 -0
+	}
+	if (ret != SEPOL_ITERSTOP) {
+		ERR(h, "iteration ended abnormally");
+		goto out;
+	}
+	ret = sepol_list_append(h, s->objs, obj);
+out:
Quoted 9 lines

libsepol/src/objset.c line 108

Quoted 5 lines
libsepol/src/objset.c new file +5 -0
+	ret2 = sepol_iter_free(h, iter);
+	if (ret2 != SEPOL_OK) {
+		ERR(h, "error freeing iterator");
+		return ret2;
+	}

I think it's much more likely that you want to know ret and not ret2
here, if ret==SEPOL_EEXIST etc.

Yeah - a previous version had just ignored errors from freeing the iterator. Does that seem more reasonable? These errors on iterator freeing are really irritating.

Quoted 17 lines
Quoted 17 lines
libsepol/src/objset.c new file +17 -0
+	return ret;
+}
+
+int sepol_objset_del(struct sepol_handle *h, struct sepol_objset *s, void *obj)
+{
+	int ret, ret2, cmp;
+	struct sepol_iter *iter;
+	void *cur;
+
+	ret = sepol_iter_create(h, &iter);
+	if (ret < 0)
+		return ret;
+
+	ret = sepol_list_begin(h, s->objs, iter);
+	sepol_foreach(h, ret, cur, iter) {
+		cmp = s->cmp(s, cur, obj);
+		if (cmp == 0) {
Quoted 9 lines

libsepol/src/objset.c line 131

Quoted 6 lines
libsepol/src/objset.c new file +6 -0
+			ret = sepol_list_del(h, s->objs, iter);
+			goto out;
+		}
+	}
+	if (ret == SEPOL_ITERSTOP)
+		ret = SEPOL_ENOENT;

You aren't using the ordering here, why is that?

No idea - fixed.

Quoted 8 lines

libsepol/src/objset.c line 136

Quoted 5 lines
libsepol/src/objset.c new file +5 -0
+		
+out:
+	ret2 = sepol_iter_free(h, iter);
+	if (ret2 != SEPOL_OK)
+		return ret2;

Dito. Also no ERR() call?

Quoted 14 lines

libsepol/src/objset.c line 154

Quoted 11 lines
libsepol/src/objset.c new file +11 -0
+	return ret;	
+}
+void sepol_objset_set_compliment(struct sepol_objset *s, char compliment)
+{
+	s->compliment = compliment;
+}
+
+char sepol_objset_get_compliement(struct sepol_objset *s)
+{
+	return s->compliment;
+}

Spillink not soo god?;)

Oops.

Quoted 3 lines

Also if you are going to add these, I think you want at least
sepol_objset_contains_obj() which does the right thing.

Added - and refactored a bit. At some future date subset, union, and other set operations should be added.

Karl

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#3 lore raw
In reply to Karl MacMillan
Quoted 3 lines · Karl MacMillan

Add the objset data structure to libsepol. Object sets behave similarly to Python sets.

After many revisions this has been merged into the policyrep branch.

Karl

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.