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 "dfx_bundle_util.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 <time_util.h>
31 #include <unistd.h>
32
33 #include "bundle_mgr_client.h"
34 #include "climits"
35 #include "event_publish.h"
36 #include "hilog/log.h"
37 #include "hisysevent.h"
38 #include "json/json.h"
39 #include "securec.h"
40 #include "string_ex.h"
41 #include "parameters.h"
42 #include "parameter_ex.h"
43
44 #undef LOG_DOMAIN
45 #define LOG_DOMAIN 0xD002D11
46
47 #undef LOG_TAG
48 #define LOG_TAG "DfxBundleUtil"
49
50 namespace OHOS {
51 namespace HiviewDFX {
52 using namespace OHOS::AppExecFwk;
IsNameValid(const std::string & name,const std::string & sep,bool canEmpty)53 bool IsNameValid(const std::string& name, const std::string& sep, bool canEmpty)
54 {
55 std::vector<std::string> nameVec;
56 SplitStr(name, sep, nameVec, canEmpty, false);
57 std::regex re("^[a-zA-Z0-9][a-zA-Z0-9_]{0,127}$");
58 for (auto const& splitName : nameVec) {
59 if (!std::regex_match(splitName, re)) {
60 HILOG_INFO(LOG_CORE, "Invalid splitName:%{public}s", splitName.c_str());
61 return false;
62 }
63 }
64 return true;
65 }
66
IsModuleNameValid(const std::string & name)67 bool IsModuleNameValid(const std::string& name)
68 {
69 if (name.empty() || name.size() > MAX_NAME_LENGTH) {
70 HILOG_INFO(LOG_CORE, "invalid log name.");
71 return false;
72 }
73
74 if (name.find("/") != std::string::npos || name.find(".") == std::string::npos) {
75 std::string path = name.substr(1); // may skip first .
76 path.erase(path.find_last_not_of(" \n\r\t") + 1);
77 HILOG_INFO(LOG_CORE, "module name:%{public}s", name.c_str());
78 return IsNameValid(path, "/", false);
79 }
80
81 return IsNameValid(name, ".", true);
82 }
83
GetApplicationNameById(int32_t uid)84 std::string GetApplicationNameById(int32_t uid)
85 {
86 std::string bundleName;
87 AppExecFwk::BundleMgrClient client;
88 if (client.GetNameForUid(uid, bundleName) != ERR_OK) {
89 HILOG_WARN(LOG_CORE, "Failed to query bundleName from bms, uid:%{public}d.", uid);
90 } else {
91 HILOG_INFO(LOG_CORE, "bundleName of uid:%{public}d is %{public}s", uid, bundleName.c_str());
92 }
93 return bundleName;
94 }
95
GetDfxBundleInfo(const std::string & bundleName,DfxBundleInfo & bundleInfo)96 bool GetDfxBundleInfo(const std::string& bundleName, DfxBundleInfo& bundleInfo)
97 {
98 AppExecFwk::BundleInfo info;
99 AppExecFwk::BundleMgrClient client;
100 if (!client.GetBundleInfo(bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT,
101 info, Constants::ALL_USERID)) {
102 HILOG_WARN(LOG_CORE, "Failed to query BundleInfo from bms, bundle:%{public}s.", bundleName.c_str());
103 return false;
104 } else {
105 HILOG_INFO(LOG_CORE, "The version of %{public}s is %{public}s", bundleName.c_str(),
106 info.versionName.c_str());
107 }
108 bundleInfo.isPreInstalled = info.isPreInstallApp;
109 bundleInfo.versionName = info.versionName;
110 bundleInfo.versionCode = info.versionCode;
111 return true;
112 }
113 } // namespace HiviewDFX
114 } // namespace OHOS