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