Received: from jazzdrum.ncsc.mil (zombie.ncsc.mil [144.51.88.131]) by tarius.tycho.ncsc.mil (8.13.1/8.13.1) with SMTP id l3QIZ9kp025007 for ; Thu, 26 Apr 2007 14:35:09 -0400 Received: from mx1.redhat.com (jazzdrum.ncsc.mil [144.51.5.7]) by jazzdrum.ncsc.mil (8.12.10/8.12.10) with ESMTP id l3QIZ7SH014276 for ; Thu, 26 Apr 2007 18:35:07 GMT Received: from int-mx1.corp.redhat.com (int-mx1.corp.redhat.com [172.16.52.254]) by mx1.redhat.com (8.13.1/8.13.1) with ESMTP id l3QIZ7LY009812 for ; Thu, 26 Apr 2007 14:35:07 -0400 Received: from pobox-2.corp.redhat.com (pobox-2.corp.redhat.com [10.11.255.15]) by int-mx1.corp.redhat.com (8.13.1/8.13.1) with ESMTP id l3QIZ7OT014950 for ; Thu, 26 Apr 2007 14:35:07 -0400 Received: from localhost.localdomain (vpn-14-41.rdu.redhat.com [10.11.14.41]) by pobox-2.corp.redhat.com (8.13.1/8.13.1) with ESMTP id l3QIZ66n007281 for ; Thu, 26 Apr 2007 14:35:06 -0400 From: Karl MacMillan Subject: [POLICYREP PATCH] Add objset to libsepol To: selinux@tycho.nsa.gov Date: Thu, 26 Apr 2007 14:35:06 -0400 Message-ID: <20070426183506.9811.32600.stgit@localhost.localdomain> In-Reply-To: 1177608900.3999.34.camel@code.and.org References: 1177608900.3999.34.camel@code.and.org MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Sender: owner-selinux@tycho.nsa.gov List-Id: selinux@tycho.nsa.gov Add the objset data structure to libsepol. Object sets behave similarly to Python sets. Updated based on comments from James Antill. Signed-off-by: Karl MacMillan --- 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(-) diff --git a/libsepol/include/sepol/objset.h b/libsepol/include/sepol/objset.h new file mode 100644 index 0000000..9690c41 --- /dev/null +++ b/libsepol/include/sepol/objset.h @@ -0,0 +1,177 @@ +/* Author: Karl MacMillan */ + +#ifndef __sepol_objset_h__ +#define __sepol_objset_h__ + +#include +#include + +/** + * \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 diff --git a/libsepol/src/objset.c b/libsepol/src/objset.c new file mode 100644 index 0000000..e3911c2 --- /dev/null +++ b/libsepol/src/objset.c @@ -0,0 +1,182 @@ +/* + * Author : Karl MacMillan + * + * Copyright (C) 2007 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include + +#include "debug.h" + +#include + +struct sepol_objset +{ + struct sepol_list *objs; + sepol_objset_cmp_t cmp; + 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; + + (*set)->cmp = cmp; + + ret = sepol_list_create(h, &(*set)->objs); + if (ret < 0) { + free(*set); + return ret; + } + + return SEPOL_OK; +} + +void sepol_objset_free(struct sepol_objset *s) +{ + if (s == NULL) + return; + sepol_list_free(s->objs); + free(s); +} + +unsigned int sepol_objset_length(struct sepol_objset *s) +{ + return sepol_list_length(s->objs); +} + +static int sepol_objset_seek(struct sepol_handle *h, struct sepol_objset *s, + struct sepol_iter **out, void *obj) +{ + int ret, cmp; + struct sepol_iter *iter; + void *cur; + + ret = sepol_iter_create(h, &iter); + if (ret < 0) + return ret; + + *out = iter; + + ret = sepol_list_begin(h, s->objs, iter); + sepol_foreach(h, ret, cur, iter) { + cmp = s->cmp(s, cur, obj); + if (cmp == 0) { + return SEPOL_EEXIST; + } else if (cmp > 0) { + return SEPOL_ENOENT; + } + } + if (ret != SEPOL_ITERSTOP) { + ERR(h, "iteration ended abnormally"); + } + + sepol_iter_free(h, iter); + *out = NULL; + return ret; +} + +int sepol_objset_add(struct sepol_handle *h, struct sepol_objset *s, void *obj) +{ + int ret; + struct sepol_iter *iter; + + ret = sepol_objset_seek(h, s, &iter, obj); + switch (ret) { + case SEPOL_EEXIST: + sepol_iter_free(h, iter); + break; + case SEPOL_ENOENT: + ret = sepol_list_insert(h, s->objs, iter, obj); + sepol_iter_free(h, iter); + break; + case SEPOL_ITERSTOP: + ret = sepol_list_append(h, s->objs, obj); + break; + } + + return ret; +} + +int sepol_objset_del(struct sepol_handle *h, struct sepol_objset *s, void *obj) +{ + int ret; + struct sepol_iter *iter; + + ret = sepol_objset_seek(h, s, &iter, obj); + switch (ret) { + case SEPOL_EEXIST: + ret = sepol_list_del(h, s->objs, iter); + sepol_iter_free(h, iter); + break; + case SEPOL_ENOENT: + sepol_iter_free(h, iter); + break; + case SEPOL_ITERSTOP: + ret = SEPOL_ENOENT; + break; + } + + return ret; +} + +int sepol_objset_contains(struct sepol_handle *h, struct sepol_objset *s, void *obj) +{ + int ret; + struct sepol_iter *iter; + int found = !s->compliment; + + ret = sepol_objset_seek(h, s, &iter, obj); + switch (ret) { + case SEPOL_EEXIST: + sepol_iter_free(h, iter); + ret = found; + break; + case SEPOL_ENOENT: + sepol_iter_free(h, iter); + ret = !found; + break; + case SEPOL_ITERSTOP: + ret = !found; + break; + } + + return ret; +} + +int sepol_objset_iter(struct sepol_handle *h, struct sepol_objset *s, + struct sepol_iter *iter) +{ + return sepol_list_begin(h, s->objs, iter); +} + +void sepol_objset_set_compliment(struct sepol_objset *s, char compliment) +{ + s->compliment = compliment; +} + +char sepol_objset_get_compliment(struct sepol_objset *s) +{ + return s->compliment; +} diff --git a/libsepol/tests/libsepol-tests.c b/libsepol/tests/libsepol-tests.c index 2fee01d..5156ada 100644 --- a/libsepol/tests/libsepol-tests.c +++ b/libsepol/tests/libsepol-tests.c @@ -25,6 +25,7 @@ #include "test-list.h" #include "test-hashtab.h" #include "test-objpool.h" +#include "test-objset.h" #include #include @@ -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); diff --git a/libsepol/tests/test-objset.c b/libsepol/tests/test-objset.c new file mode 100644 index 0000000..f064638 --- /dev/null +++ b/libsepol/tests/test-objset.c @@ -0,0 +1,150 @@ +/* + * Author : Karl MacMillan + * + * Copyright (C) 2007 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "test-objset.h" + +#include +#include +#include +#include +#include + +#include +#include + +int objset_test_init(void) +{ + return 0; +} + +int objset_test_cleanup(void) +{ + return 0; +} + +static int ptrcmp(struct sepol_objset *s, void *a, void *b) +{ + if (a == b) + return 0; + else if (a < b) + return -1; + else + return 1; +} + +static void test_objset(void) +{ + int ret; + struct sepol_objset *s; + struct sepol_objpool *objpool; + struct sepol_handle *h; + char *strs[3]; + char *str; + + h = sepol_handle_create(); + CU_ASSERT(h != NULL); + + ret = sepol_objset_create(h, &s, (sepol_objset_cmp_t)sepol_symcmp); + CU_ASSERT(ret == SEPOL_OK); + + ret = sepol_objset_add(h, s, "foo"); + CU_ASSERT(ret == SEPOL_OK); + + ret = sepol_objset_add(h, s, "foo"); + CU_ASSERT(ret == SEPOL_EEXIST); + + CU_ASSERT(sepol_objset_length(s) == 1); + + ret = sepol_objset_add(h, s, "bar"); + CU_ASSERT(ret == SEPOL_OK); + + CU_ASSERT(sepol_objset_length(s) == 2); + + CU_ASSERT(sepol_objset_contains(h, s, "bar")); + CU_ASSERT(sepol_objset_contains(h, s, "baz") == 0); + + ret = sepol_objset_del(h, s, "bar"); + CU_ASSERT(ret == SEPOL_OK); + + CU_ASSERT(sepol_objset_length(s) == 1); + + ret = sepol_objset_del(h, s, "bar"); + CU_ASSERT(ret == SEPOL_ENOENT); + + ret = sepol_objset_del(h, s, "foo"); + CU_ASSERT(ret == SEPOL_OK); + + CU_ASSERT(sepol_objset_length(s) == 0); + + sepol_objset_free(s); + + + /* obj pool test */ + ret = sepol_objset_create(h, &s, (sepol_objset_cmp_t)ptrcmp); + CU_ASSERT(ret == SEPOL_OK); + + strs[0] = strdup("foo"); + CU_ASSERT(strs[0] != NULL); + strs[1] = strdup("foo"); + CU_ASSERT(strs[1] != NULL); + strs[2] = strdup("bar"); + CU_ASSERT(strs[2] != NULL); + + ret = sepol_objpool_create(h, &objpool, (sepol_objpool_hash_t)sepol_symhash, + (sepol_objpool_cmp_t)sepol_symcmp, free, 64); + CU_ASSERT(ret == SEPOL_OK); + + str = strs[0]; + str = sepol_objpool_add(h, objpool, (void*)str); + CU_ASSERT(str != NULL); + str = strs[1]; + str = sepol_objpool_add(h, objpool,(void*)str); + CU_ASSERT(str != NULL); + str = strs[2]; + str = sepol_objpool_add(h, objpool, (void*)str); + CU_ASSERT(str != NULL); + + ret = sepol_objset_add(h, s, strs[0]); + CU_ASSERT(ret == SEPOL_OK); + + ret = sepol_objset_add(h, s, strs[0]); + CU_ASSERT(ret == SEPOL_EEXIST); + + CU_ASSERT(sepol_objset_length(s) == 1); + + ret = sepol_objset_add(h, s, strs[2]); + CU_ASSERT(ret == SEPOL_OK); + + CU_ASSERT(sepol_objset_length(s) == 2); + + sepol_objpool_free(h, objpool); + sepol_objset_free(s); + sepol_handle_destroy(h); +} + +int objset_add_tests(CU_pSuite suite) +{ + if (NULL == CU_add_test(suite, "test_objset", + test_objset)) { + return -1; + } + + return 0; +} diff --git a/libsepol/tests/test-objset.h b/libsepol/tests/test-objset.h new file mode 100644 index 0000000..d6ab340 --- /dev/null +++ b/libsepol/tests/test-objset.h @@ -0,0 +1,12 @@ +/* Author : Karl MacMillan */ + +#ifndef __test_objset_h__ +#define __test_objset_h__ + +#include + +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 majordomo@tycho.nsa.gov with the words "unsubscribe selinux" without quotes as the message.