• 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 "hidebug_base.h"
17 
18 #include <cerrno>
19 #include <cinttypes>
20 #include <cstdio>
21 #include <cstdlib>
22 #include <cstring>
23 
24 #include <parameter.h>
25 #include <sysparam_errno.h>
26 
27 #include "hilog/log.h"
28 #include "securec.h"
29 
30 #undef LOG_DOMAIN
31 #undef LOG_TAG
32 #define LOG_DOMAIN 0xD002D0A
33 #define LOG_TAG "HiDebug_Native"
34 
35 namespace {
36 const int MAX_PARA_LEN = 50;
37 const int MAX_PARA_CNT = 20;
38 const int PARAM_BUF_LEN = 128;
39 const int QUERYNAME_LEN = 80;
40 const char COLON_CHR = ':';
41 
42 struct Params {
43     char key[MAX_PARA_LEN];
44     char value[MAX_PARA_LEN];
45 } g_params[MAX_PARA_CNT];
46 
47 int g_paramCnt = 0;
48 
ParseKeyValue(const char * input)49 void ParseKeyValue(const char *input)
50 {
51     if (g_paramCnt >= MAX_PARA_CNT) {
52         HILOG_ERROR(LOG_CORE, "Parameters is Full.");
53         return;
54     }
55     const char *colonPos = strchr(input, COLON_CHR);
56     if (colonPos == nullptr) {
57         HILOG_ERROR(LOG_CORE, "params is illegal.");
58         return;
59     }
60     errno_t err = strncpy_s(g_params[g_paramCnt].key, MAX_PARA_LEN, input, colonPos - input);
61     if (err != EOK) {
62         HILOG_ERROR(LOG_CORE, "memcpy_s copy key strings failed.");
63         return;
64     }
65     err = strncpy_s(g_params[g_paramCnt].value, MAX_PARA_LEN, colonPos + 1, strlen(colonPos + 1));
66     if (err != EOK) {
67         HILOG_ERROR(LOG_CORE, "memcpy_s copy value strings failed.");
68         return;
69     }
70     g_paramCnt++;
71 }
72 
SplitParams(char * input)73 void SplitParams(char *input)
74 {
75     g_paramCnt = 0;
76     const char space[] = " ";
77     char *param;
78     char *next = nullptr;
79     param = strtok_s(input, space, &next);
80     while (param != nullptr) {
81         ParseKeyValue(param);
82         param = strtok_s(nullptr, space, &next);
83     }
84 }
85 
QueryParams(const char * queryName)86 int QueryParams(const char *queryName)
87 {
88     g_paramCnt = 0;
89     char paramOutBuf[PARAM_BUF_LEN] = { 0 };
90     char defStrValue[PARAM_BUF_LEN] = { 0 };
91     int retLen = GetParameter(queryName, defStrValue, paramOutBuf, PARAM_BUF_LEN);
92     if (retLen == 0) {
93         HILOG_ERROR(LOG_CORE, "get %{public}s parameters failed.", queryName);
94         return 0;
95     }
96     paramOutBuf[retLen] = '\0';
97     SplitParams(paramOutBuf);
98     return g_paramCnt;
99 }
100 }
101 
InitEnvironmentParam(const char * serviceName)102 bool InitEnvironmentParam(const char *serviceName)
103 {
104     if (serviceName == nullptr) {
105         HILOG_ERROR(LOG_CORE, "input service name is null.");
106         return false;
107     }
108     errno_t err = 0;
109     char persistName[QUERYNAME_LEN] = "persist.hiviewdfx.debugenv.";
110     char onceName[QUERYNAME_LEN] = "hiviewdfx.debugenv.";
111     err = strcat_s(onceName, sizeof(onceName), serviceName);
112     if (err != EOK) {
113         HILOG_ERROR(LOG_CORE, "strcat_s query name failed.");
114         return 0;
115     }
116     err = strcat_s(persistName, sizeof(persistName), serviceName);
117     if (err != EOK) {
118         HILOG_ERROR(LOG_CORE, "strcat_s persist query name failed.");
119         return 0;
120     }
121     if (QueryParams(onceName) == 0 && QueryParams(persistName) == 0) {
122         HILOG_ERROR(LOG_CORE, "failed to capture %{public}s environment params.", serviceName);
123         return false;
124     }
125     for (int i = 0; i < g_paramCnt; ++i) {
126         if (setenv(g_params[i].key, g_params[i].value, 1) != 0) { // 1 : overwrite
127             HILOG_ERROR(LOG_CORE, "setenv failed, errno = %{public}d.", errno);
128         }
129     }
130     return true;
131 }
132