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 "ld_log.h"
17
18 static bool ld_log_enable = false;
19 static bool ld_dlclose_debug = false;
20
21 #ifdef OHOS_ENABLE_PARAMETER
22 #include <fcntl.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include <sys/prctl.h>
26 #include <unistd.h>
27
28 #include "sys_param.h"
29
30 #define SYSPARAM_LENGTH 32
31 #define PROCESS_NAME_LEN 1024
32
get_app_name(char * buf,size_t length)33 static char *get_app_name(char *buf, size_t length)
34 {
35 char *app = NULL;
36 int fd = open("/proc/self/cmdline", O_RDONLY);
37 if (fd != -1) {
38 ssize_t ret = read(fd, buf, length - 1);
39 if (ret != -1) {
40 buf[ret] = 0;
41 app = strrchr(buf, '/');
42 if (app) {
43 app++;
44 } else {
45 app = buf;
46 }
47 char *app_end = strchr(app, ':');
48 if (app_end) {
49 *app_end = 0;
50 }
51 }
52 close(fd);
53 }
54 return app;
55 }
56
get_ld_log_value()57 static bool get_ld_log_value()
58 {
59 char buf[PROCESS_NAME_LEN];
60 char *path = get_app_name(buf, PROCESS_NAME_LEN);
61 if (!path) {
62 return false;
63 }
64
65 char app_param_name[PROCESS_NAME_LEN] = "musl.log.ld.app.";
66 strcat(app_param_name, path);
67 static CachedHandle app_param_handle = NULL;
68 static CachedHandle all_param_handle = NULL;
69 if (app_param_handle == NULL) {
70 app_param_handle = CachedParameterCreate(app_param_name, "false");
71 }
72 if (all_param_handle == NULL) {
73 all_param_handle = CachedParameterCreate("musl.log.ld.all", "false");
74 }
75 return (get_bool_sysparam(app_param_handle) || get_bool_sysparam(all_param_handle));
76 }
77
get_ld_debug_dlclose_value()78 static bool get_ld_debug_dlclose_value()
79 {
80 static CachedHandle param_handle = NULL;
81 if (param_handle == NULL) {
82 param_handle = CachedParameterCreate("musl.ld.debug.dlclose", "false");
83 }
84 return get_bool_sysparam(param_handle);
85 }
86 #endif
87
ld_log_reset()88 void ld_log_reset()
89 {
90 #if (defined(OHOS_ENABLE_PARAMETER))
91 ld_dlclose_debug = get_ld_debug_dlclose_value();
92 if (!is_musl_log_enable()) {
93 ld_log_enable = false;
94 return;
95 }
96 ld_log_enable = get_ld_log_value();
97 #else
98 ld_log_enable = is_musl_log_enable();
99 #endif
100 }
101
get_ld_log_enable()102 bool get_ld_log_enable()
103 {
104 return ld_log_enable;
105 }
106
is_dlclose_debug_enable()107 bool is_dlclose_debug_enable()
108 {
109 return ld_dlclose_debug;
110 }
111