Add objset to libsepol
PATCH 1/1 Add objset to libsepol
Add the objset data structure to libsepol. Object sets behave similarly to Python sets.
Updated based on comments from James Antill.
---
libsepol/include/sepol/objset.h | 174 +++++++++++++++++++++++++++++++++++++++ libsepol/src/objset.c | 172 +++++++++++++++++++++++++++++++++++++++ libsepol/tests/libsepol-tests.c | 2 libsepol/tests/test-objset.c | 150 ++++++++++++++++++++++++++++++++++ libsepol/tests/test-objset.h | 12 +++ 5 files changed, 510 insertions(+), 0 deletions(-)
libsepol/include/sepol/objset.h new file +174 -0
@@ -0,0 +1,174 @@ +/* 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 +* +* 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 +* +* Determine whether an object is contained in the set. +* +* @param h sepol handle (may be NULL) +* @param s object set +* @param obj obj to check for in objset +* +* \retval 1 obj found in objset +* \retval 0 obj not found in objset +* \retval <0 error +*/ +extern int sepol_objset_contains (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_compliment (struct sepol_objset *s); + +#endif
libsepol/src/objset.c new file +172 -0
172 lines not on this page · raw message
libsepol/tests/libsepol-tests.c +2 -0
@@ -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 +150 -0
150 lines not on this page · raw message
libsepol/tests/test-objset.h new file +12 -0
@@ -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.
Quoted 2 lines · Karl MacMillan
Add the objset data structure to libsepol. Object sets behave similarly to Python sets.
[...]
libsepol/src/objset.c line 35
Quoted 8 lines
libsepol/src/objset.c new file +7
-0+struct sepol_objset +{ +struct sepol_list *objs; +sepol_objset_cmp_t cmp; +unsigned int len; +char compliment; +};
What is len for?
Quoted 6 lines
libsepol/src/objset.c new file +5
-0+int sepol_objset_contains (struct sepol_handle *h,struct sepol_objset *s,void *obj) +{ +int ret; +struct sepol_iter *iter; +
libsepol/src/objset.c line 153
Quoted 10 lines
libsepol/src/objset.c new file +10
-0+ ret =sepol_objset_seek (h, s, &iter, obj); +if (ret ==SEPOL_EEXIST ) { +sepol_iter_free (h, iter); +return 1 ; + }else if (ret ==SEPOL_ENOENT ) { +sepol_iter_free (h, iter); +return 0 ; + }else if (ret ==SEPOL_ITERSTOP ) { +return 0 ; + }
This should be:
int ret;
struct sepol_iter *iter;
int found = !s->compliment;
ret = sepol_objset_seek(h, s, &iter, obj);
if (ret == SEPOL_EEXIST) {
sepol_iter_free(h, iter);
return found;
} else if (ret == SEPOL_ENOENT) {
sepol_iter_free(h, iter);
return !found;
} else if (ret == SEPOL_ITERSTOP) {
return !found;
}...or something similar, no? Also C has switch, unlike python ;).
libsepol/src/objset.c line 52
Quoted 3 lines
libsepol/src/objset.c new file +3
-0+ +return ret; +}
Having the iter errors bubble up the API like this sucks and will make
eq/subset/etc. much more complicated :(.
Signature · 1 line
James Antill <[email protected]>
Quoted 4 lines · James Antill
Quoted 2 lines · Karl MacMillan
Add the objset data structure to libsepol. Object sets behave similarly to Python sets.
[...]
Quoted 9 lines
libsepol/src/objset.c line 35
Quoted 8 lines
libsepol/src/objset.c new file +7
-0+struct sepol_objset +{ +struct sepol_list *objs; +sepol_objset_cmp_t cmp; +unsigned int len; +char compliment; +};What is len for?
It's vestigial - removed.
Quoted 34 lines
Quoted 6 lines
libsepol/src/objset.c new file +5
-0+int sepol_objset_contains (struct sepol_handle *h,struct sepol_objset *s,void *obj) +{ +int ret; +struct sepol_iter *iter; +libsepol/src/objset.c line 153
Quoted 10 lines
libsepol/src/objset.c new file +10
-0+ ret =sepol_objset_seek (h, s, &iter, obj); +if (ret ==SEPOL_EEXIST ) { +sepol_iter_free (h, iter); +return 1 ; + }else if (ret ==SEPOL_ENOENT ) { +sepol_iter_free (h, iter); +return 0 ; + }else if (ret ==SEPOL_ITERSTOP ) { +return 0 ; + }This should be:
int ret; struct sepol_iter *iter; int found = !s->compliment; ret = sepol_objset_seek(h, s, &iter, obj); if (ret == SEPOL_EEXIST) { sepol_iter_free(h, iter); return found; } else if (ret == SEPOL_ENOENT) { sepol_iter_free(h, iter); return !found; } else if (ret == SEPOL_ITERSTOP) { return !found; }
Quoted 1 line
...or something similar, no?
I was undecided - essentially, compliment is only useful when the set represents a subset of a larger set. Unfortunately, we don't know what is in that larger set. So, given the set { "bar", "baz" } that is complimented, testing for "foo" is unknowable.
When I use the sets in the policy rep I do the only thing possible - the caller interprets what compliment means. To do that I need to know what is literally in the set. So unless someone objects I will leave as is with a note in the docs.
Quoted 2 lines
Also C has switch, unlike python ;).
I normally hate switches, but it definitely made the code easier to read in this case.
Quoted 7 lines
libsepol/src/objset.c line 52
Quoted 3 lines
libsepol/src/objset.c new file +3
-0+ +return ret; +}Having the iter errors bubble up the API like this sucks and will make
eq/subset/etc. much more complicated :(.
I know - suggestions about how to avoid this though?
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.
PATCH 1/1 Add objset to libsepol
In reply to James AntillAdd the objset data structure to libsepol. Object sets behave similarly to Python sets.
Updated based on comments from James Antill.
---
libsepol/include/sepol/objset.h | 177 ++++++++++++++++++++++++++++++++++++++ libsepol/src/objset.c | 182 +++++++++++++++++++++++++++++++++++++++ libsepol/tests/libsepol-tests.c | 2 libsepol/tests/test-objset.c | 150 ++++++++++++++++++++++++++++++++ libsepol/tests/test-objset.h | 12 +++ 5 files changed, 523 insertions(+), 0 deletions(-)
libsepol/include/sepol/objset.h new file +177 -0
@@ -0,0 +1,177 @@ +/* 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: +* +* \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 +* +* 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 +* +* Determine whether an object is contained in the set. The current value +* of compliment is respected, so for the set { "bar", "baz" } testing +* if "foo" is contained will be 0 if compliment is 0 or 1 if compliment +* is 1. +* +* @param h sepol handle (may be NULL) +* @param s object set +* @param obj obj to check for in objset +* +* \retval 1 obj found in objset +* \retval 0 obj not found in objset +* \retval <0 error +*/ +extern int sepol_objset_contains (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_compliment (struct sepol_objset *s); + +#endif
libsepol/src/objset.c new file +182 -0
182 lines not on this page · raw message
libsepol/tests/libsepol-tests.c +2 -0
@@ -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 +150 -0
150 lines not on this page · raw message
libsepol/tests/test-objset.h new file +12 -0
@@ -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.