• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014 Tresys Technology, LLC. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *    1. Redistributions of source code must retain the above copyright notice,
8  *       this list of conditions and the following disclaimer.
9  *
10  *    2. Redistributions in binary form must reproduce the above copyright notice,
11  *       this list of conditions and the following disclaimer in the documentation
12  *       and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY TRESYS TECHNOLOGY, LLC ``AS IS'' AND ANY EXPRESS
15  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17  * EVENT SHALL TRESYS TECHNOLOGY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * The views and conclusions contained in the software and documentation are those
26  * of the authors and should not be interpreted as representing official policies,
27  * either expressed or implied, of Tresys Technology, LLC.
28  */
29 
30 #include <pthread.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include "cil_mem.h"
35 #include "cil_strpool.h"
36 
37 #include "cil_log.h"
38 #define CIL_STRPOOL_TABLE_SIZE 1 << 15
39 
40 struct cil_strpool_entry {
41 	char *str;
42 };
43 
44 static pthread_mutex_t cil_strpool_mutex = PTHREAD_MUTEX_INITIALIZER;
45 static unsigned int cil_strpool_readers = 0;
46 static hashtab_t cil_strpool_tab = NULL;
47 
cil_strpool_hash(hashtab_t h,const_hashtab_key_t key)48 static unsigned int cil_strpool_hash(hashtab_t h, const_hashtab_key_t key)
49 {
50 	const char *p;
51 	size_t size;
52 	unsigned int val;
53 
54 	val = 0;
55 	size = strlen(key);
56 	for (p = key; ((size_t) (p - key)) < size; p++)
57 		val =
58 		    (val << 4 | (val >> (8 * sizeof(unsigned int) - 4))) ^ (*p);
59 	return val & (h->size - 1);
60 }
61 
cil_strpool_compare(hashtab_t h,const_hashtab_key_t key1,const_hashtab_key_t key2)62 static int cil_strpool_compare(hashtab_t h __attribute__ ((unused)), const_hashtab_key_t key1, const_hashtab_key_t key2)
63 {
64 	return strcmp(key1, key2);
65 }
66 
cil_strpool_add(const char * str)67 char *cil_strpool_add(const char *str)
68 {
69 	struct cil_strpool_entry *strpool_ref = NULL;
70 
71 	pthread_mutex_lock(&cil_strpool_mutex);
72 
73 	strpool_ref = hashtab_search(cil_strpool_tab, str);
74 	if (strpool_ref == NULL) {
75 		int rc;
76 		strpool_ref = cil_malloc(sizeof(*strpool_ref));
77 		strpool_ref->str = cil_strdup(str);
78 		rc = hashtab_insert(cil_strpool_tab, strpool_ref->str, strpool_ref);
79 		if (rc != SEPOL_OK) {
80 			pthread_mutex_unlock(&cil_strpool_mutex);
81 			cil_log(CIL_ERR, "Failed to allocate memory\n");
82 			exit(1);
83 		}
84 	}
85 
86 	pthread_mutex_unlock(&cil_strpool_mutex);
87 	return strpool_ref->str;
88 }
89 
cil_strpool_entry_destroy(hashtab_key_t k,hashtab_datum_t d,void * args)90 static int cil_strpool_entry_destroy(hashtab_key_t k __attribute__ ((unused)), hashtab_datum_t d, void *args __attribute__ ((unused)))
91 {
92 	struct cil_strpool_entry *strpool_ref = (struct cil_strpool_entry*)d;
93 	free(strpool_ref->str);
94 	free(strpool_ref);
95 	return SEPOL_OK;
96 }
97 
cil_strpool_init(void)98 void cil_strpool_init(void)
99 {
100 	pthread_mutex_lock(&cil_strpool_mutex);
101 	if (cil_strpool_tab == NULL) {
102 		cil_strpool_tab = hashtab_create(cil_strpool_hash, cil_strpool_compare, CIL_STRPOOL_TABLE_SIZE);
103 		if (cil_strpool_tab == NULL) {
104 			pthread_mutex_unlock(&cil_strpool_mutex);
105 			cil_log(CIL_ERR, "Failed to allocate memory\n");
106 			exit(1);
107 		}
108 	}
109 	cil_strpool_readers++;
110 	pthread_mutex_unlock(&cil_strpool_mutex);
111 }
112 
cil_strpool_destroy(void)113 void cil_strpool_destroy(void)
114 {
115 	pthread_mutex_lock(&cil_strpool_mutex);
116 	cil_strpool_readers--;
117 	if (cil_strpool_readers == 0) {
118 		hashtab_map(cil_strpool_tab, cil_strpool_entry_destroy, NULL);
119 		hashtab_destroy(cil_strpool_tab);
120 		cil_strpool_tab = NULL;
121 	}
122 	pthread_mutex_unlock(&cil_strpool_mutex);
123 }
124