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 #include "dynlink_rand.h"
16 #include <sys/mman.h>
17 #include <unistd.h>
18
19 #ifdef OHOS_ENABLE_PARAMETER
20 #include "sys_param.h"
21 #endif
22
23 #ifdef UNIT_TEST_STATIC
free_task(struct loadtask * task)24 void free_task(struct loadtask *task)
25 {
26 if (task == NULL) {
27 return;
28 }
29 if (task->name) {
30 free((void *)task->name);
31 task->name = NULL;
32 }
33 if (task->allocated_buf) {
34 free(task->allocated_buf);
35 task->allocated_buf = NULL;
36 }
37 if (task->dyn_map_len) {
38 munmap(task->dyn_map, task->dyn_map_len);
39 task->dyn_map = NULL;
40 task->dyn_map_len = 0;
41 }
42 if (task->str_map_len) {
43 munmap(task->str_map, task->str_map_len);
44 task->str_map = NULL;
45 task->str_map_len = 0;
46 }
47 if (task->fd != -1 && task->fd) {
48 close(task->fd);
49 task->fd = -1;
50 }
51 free(task);
52 }
53
get_loadtask(struct loadtasks * tasks,size_t index)54 struct loadtask *get_loadtask(struct loadtasks *tasks, size_t index)
55 {
56 if (tasks && tasks->array && (index < tasks->length)) {
57 return tasks->array[index];
58 } else {
59 return NULL;
60 }
61 }
62
free_loadtasks(struct loadtasks * tasks)63 void free_loadtasks(struct loadtasks *tasks)
64 {
65 if (tasks) {
66 if (tasks->length) {
67 for (size_t i = 0; i < tasks->length; i++) {
68 free_task(get_loadtask(tasks, i));
69 }
70 tasks->length = 0;
71 }
72 if (tasks->array) {
73 free(tasks->array);
74 tasks->array = NULL;
75 }
76 tasks->capacity = 0;
77 free(tasks);
78 }
79 }
80 #endif
81
get_ld_debug_dlclose_value()82 bool get_ld_debug_dlclose_value()
83 {
84 #ifdef OHOS_ENABLE_PARAMETER
85 static CachedHandle param_handle = NULL;
86 if (param_handle == NULL) {
87 param_handle = CachedParameterCreate("musl.ld.debug.dlclose", "false");
88 }
89
90 const char *param_value = CachedParameterGet(param_handle);
91 if (param_value != NULL) {
92 if (strcmp(param_value, "true") == 0) {
93 return true;
94 }
95 }
96 #endif
97 return false;
98 }