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