1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "selinux_parameter.h" 17 #include <fcntl.h> 18 #include <pthread.h> 19 #include <sys/socket.h> 20 #include <unistd.h> 21 #include "errno.h" 22 #include "selinux_error.h" 23 #include "contexts_trie.h" 24 25 static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; 26 static const char DEFAULT_CONTEXT[] = "u:object_r:default_param:s0"; 27 static ParamContextsTrie *g_contextsTrie = NULL; 28 static ParamContextsList *g_contextsList = NULL; 29 ParameterContextsLoad(void)30static int ParameterContextsLoad(void) 31 { 32 if (getpid() == 1) { // process init will load parameter_contexts to shared memory 33 int res = LoadParameterContextsToSharedMem(); 34 if (res != 0) { 35 return res; 36 } 37 } 38 if (!ReadParamFromSharedMem(&g_contextsTrie, &g_contextsList)) { // other process load parameters from shared memory 39 return -SELINUX_CONTEXTS_FILE_LOAD_ERROR; 40 } 41 return 0; 42 } 43 InitParamSelinux(void)44int InitParamSelinux(void) 45 { 46 pthread_mutex_lock(&g_mutex); 47 if (g_contextsTrie != NULL) { 48 return 0; 49 } 50 int res = ParameterContextsLoad(); 51 pthread_mutex_unlock(&g_mutex); 52 return res; 53 } 54 DestroyParamList(ParamContextsList ** list)55void DestroyParamList(ParamContextsList **list) 56 { 57 if (list == NULL) { 58 return; 59 } 60 ParamContextsList *tmpNode; 61 ParamContextsList *listHead = *list; 62 while (listHead != NULL) { 63 tmpNode = listHead->next; 64 free(listHead); 65 listHead = tmpNode; 66 } 67 *list = NULL; 68 return; 69 } 70 GetParamList()71ParamContextsList *GetParamList() 72 { 73 if (g_contextsList == NULL) { 74 return NULL; 75 } 76 return g_contextsList; 77 } 78 GetParamLabel(const char * paraName)79const char *GetParamLabel(const char *paraName) 80 { 81 if (paraName == NULL || g_contextsTrie == NULL) { 82 return DEFAULT_CONTEXT; 83 } 84 return SearchFromParamTrie(g_contextsTrie, paraName); 85 } 86