• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "zip_helper.h"
17 
18 #include <algorithm>
19 #include <cerrno>
20 #include <cstdio>
21 #include <cstdlib>
22 #include <cstring>
23 #include <ctime>
24 #include <fcntl.h>
25 #include <file_util.h>
26 #include <regex>
27 #include <sys/ioctl.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31 #include "string_ex.h"
32 #include "securec.h"
33 #include "limits.h"
34 #include "bundle_mgr_client.h"
35 #include "sanitizerd_log.h"
36 #include "parameters.h"
37 
38 namespace OHOS {
39 namespace HiviewDFX {
40 using namespace OHOS::AppExecFwk;
SplitString(const std::string & input,const std::string & regex)41 std::vector<std::string> SplitString(const std::string& input, const std::string& regex)
42 {
43     std::regex re(regex);
44     std::sregex_token_iterator first {input.begin(), input.end(), re, -1}, last;
45     return {first, last};
46 }
47 
HashString(const std::string & input)48 unsigned HashString(const std::string& input)
49 {
50     unsigned hash = 0;
51     for (size_t i = 0; i < input.length(); ++i) {
52         hash = hash * HASH_FACTOR + input[i];
53     }
54     return hash;
55 }
56 
IsNameValid(const std::string & name,const std::string & sep,bool canEmpty)57 bool IsNameValid(const std::string& name, const std::string& sep, bool canEmpty)
58 {
59     std::vector<std::string> nameVec;
60     SplitStr(name, sep, nameVec, canEmpty, false);
61     std::regex re("^[a-zA-Z][a-zA-Z0-9_]*$");
62     for (auto const& splitName : nameVec) {
63         if (!std::regex_match(splitName, re)) {
64             SANITIZERD_LOGI("Invalid splitName:%{public}s", splitName.c_str());
65             return false;
66         }
67     }
68     return true;
69 }
70 
IsModuleNameValid(const std::string & name)71 bool IsModuleNameValid(const std::string& name)
72 {
73     if (name.empty() || name.size() > MAX_NAME_LENGTH) {
74         SANITIZERD_LOGI("invalid log name.");
75         return false;
76     }
77 
78     if (name.find("/") != std::string::npos || name.find(".") == std::string::npos) {
79         std::string path = name.substr(1); // may skip first .
80         path.erase(path.find_last_not_of(" \n\r\t") + 1);
81         SANITIZERD_LOGI("module name:%{public}s", name.c_str());
82         return IsNameValid(path, "/", false);
83     }
84 
85     return IsNameValid(name, ".", true);
86 }
87 
GetApplicationNameById(int32_t uid)88 std::string GetApplicationNameById(int32_t uid)
89 {
90     std::string bundleName;
91     AppExecFwk::BundleMgrClient client;
92     if (client.GetNameForUid(uid, bundleName) != ERR_OK) {
93         SANITIZERD_LOGW("Failed to query bundleName from bms, uid:%{public}d.", uid);
94     } else {
95         SANITIZERD_LOGI("bundleName of uid:%{public}d is %{public}s", uid, bundleName.c_str());
96     }
97     return bundleName;
98 }
99 
GetDfxBundleInfo(const std::string & bundleName,DfxBundleInfo & bundleInfo)100 bool GetDfxBundleInfo(const std::string& bundleName, DfxBundleInfo& bundleInfo)
101 {
102     AppExecFwk::BundleInfo info;
103     AppExecFwk::BundleMgrClient client;
104     if (!client.GetBundleInfo(bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT,
105                               info, Constants::ALL_USERID)) {
106         SANITIZERD_LOGW("Failed to query BundleInfo from bms, bundle:%{public}s.", bundleName.c_str());
107         return false;
108     } else {
109         SANITIZERD_LOGI("The version of %{public}s is %{public}s", bundleName.c_str(),
110                         info.versionName.c_str());
111     }
112     bundleInfo.isPreInstalled = info.isPreInstallApp;
113     bundleInfo.versionName = info.versionName;
114     bundleInfo.versionCode = info.versionCode;
115     return true;
116 }
117 
CreateMultiTierDirectory(const std::string & directoryPath,const std::string & rootDirPath,const uid_t dirOwner,const gid_t dirGroup)118 int32_t CreateMultiTierDirectory(const std::string &directoryPath, const std::string &rootDirPath,
119                                  const uid_t dirOwner, const gid_t dirGroup)
120 {
121     int32_t ret = -1;
122     uint32_t dirPathLen = directoryPath.length();
123     if (dirPathLen > PATH_MAX) {
124         return ret;
125     }
126     char tmpDirPath[PATH_MAX] = { 0 };
127     for (uint32_t i = 0; i < dirPathLen; ++i) {
128         tmpDirPath[i] = directoryPath[i];
129         if (i < rootDirPath.length() && directoryPath[i] != rootDirPath[i]) {
130             return ret;
131         } else if (i < rootDirPath.length() || tmpDirPath[i] != '/') {
132             continue;
133         }
134         if (access(tmpDirPath, 0) != 0) {
135             ret = mkdir(tmpDirPath, DEFAULT_LOG_DIR_MODE);
136             ret += chown(tmpDirPath, dirOwner, dirGroup);
137             if (ret != 0) {
138                 SANITIZERD_LOGE("Fail to create dir %{public}s,  err: %{public}s.",
139                                 tmpDirPath, strerror(errno));
140                 return ret;
141             }
142         }
143     }
144     return 0;
145 }
146 
GetCollectedDataSavePath(const T_SANITIZERD_PARAMS * params)147 static std::string GetCollectedDataSavePath(const T_SANITIZERD_PARAMS *params)
148 {
149     std::string faultLogPath = std::string(ROOT_FAULTLOG_LOG_PATH);
150     std::string filePath = std::string(CUSTOM_SANITIZER_LOG_PATH);
151     return filePath;
152 }
153 
CalcCollectedLogName(T_SANITIZERD_PARAMS * params)154 static std::string CalcCollectedLogName(T_SANITIZERD_PARAMS *params)
155 {
156     std::string filePath = GetCollectedDataSavePath(params);
157     if (filePath.size() == 0) {
158         return filePath;
159     }
160     std::string prefix = std::string(SANITIZERD_TYPE_STR[params->type][PREFIXFILENAME]);
161     std::string name = params->procName;
162     if (name.find("/") != std::string::npos) {
163         name = params->procName.substr(params->procName.find_last_of("/") + 1);
164     }
165 
166     std::string fileName = "";
167     fileName.append(prefix);
168     fileName.append("-");
169     fileName.append(name);
170     fileName.append("-");
171     fileName.append(std::to_string(params->uid));
172     fileName.append("-");
173     fileName.append(std::to_string(params->happenTime));
174 
175     std::string fullName = filePath + fileName;
176 
177     params->logName = fileName;
178 
179     return fullName;
180 }
181 
CreateLogFile(const std::string & name)182 static int32_t CreateLogFile(const std::string& name)
183 {
184     int32_t fd = -1;
185     if (!FileUtil::FileExists(name)) {
186         SANITIZERD_LOGW("file %{public}s is creating now.", name.c_str());
187     }
188     fd = open(name.c_str(), O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_LOG_FILE_MODE);
189     return fd;
190 }
191 
WriteNewFile(const int32_t fd,const T_SANITIZERD_PARAMS * params)192 static bool WriteNewFile(const int32_t fd, const T_SANITIZERD_PARAMS *params)
193 {
194     if (fd < 0) {
195         return false;
196     }
197 
198     FileUtil::SaveStringToFd(fd, "Generated by HiviewDFX @OpenHarmony " +
199             system::GetParameter(DEVICE_OHOS_VERSION_PARAM, EMPTY_PARAM) + "\n" +
200             "=================================================================\n" +
201             "TIMESTAMP:" + std::to_string(params->happenTime) + "\n" +
202             "Pid:" + std::to_string(params->pid) + "\n" +
203             "Uid:" + std::to_string(params->uid) + "\n" +
204             "Process name:" + params->procName + "\n" +
205             "Reason:" + std::string(SANITIZERD_TYPE_STR[params->type][ORISANITIZERTYPE]) + ":" +
206             params->errType + "\n" +
207             "Fault thread Info:\n" +
208             params->description);
209 
210     close(fd);
211     return true;
212 }
213 
WriteCollectedData(T_SANITIZERD_PARAMS * params)214 void WriteCollectedData(T_SANITIZERD_PARAMS *params)
215 {
216     std::string fullName = CalcCollectedLogName(params);
217     if (fullName.size() == 0) {
218         return;
219     }
220     int32_t fd = CreateLogFile(fullName);
221     if (fd < 0) {
222         return;
223     }
224 
225     if (!WriteNewFile(fd, params)) {
226         SANITIZERD_LOGE("Fail to write %{public}s,  err: %{public}s.", fullName.c_str(), strerror(errno));
227     }
228 }
229 } // namespace HiviewDFX
230 } // namespace OHOS
231