1 /*
2 * Copyright (c) 2021 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 "common_tool.h"
17
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <unistd.h>
21
22 #include <cstdlib>
23
24 #include "string_ex.h"
25
26 namespace OHOS {
27 namespace AppExecFwk {
ExcuteCmd(const std::string & cmd,std::vector<std::string> & resvec,const int32_t size)28 int32_t CommonTool::ExcuteCmd(const std::string &cmd, std::vector<std::string> &resvec, const int32_t size)
29 {
30 if (size <= 0) {
31 return 0;
32 }
33 resvec.clear();
34 FILE *file = popen(cmd.c_str(), "r");
35 if (file == nullptr) {
36 return 0;
37 }
38 char tmp[size];
39 tmp[0] = '\0';
40 while (fgets(tmp, sizeof(tmp), file) != nullptr) {
41 if (tmp[strlen(tmp) - 1] == '\n') {
42 tmp[strlen(tmp) - 1] = '\0';
43 }
44 std::string iem(tmp);
45 resvec.push_back(iem);
46 }
47 pclose(file);
48 return resvec.size();
49 }
50
ExcuteCmd(const std::string & cmd)51 bool CommonTool::ExcuteCmd(const std::string &cmd)
52 {
53 FILE *file = popen(cmd.c_str(), "r");
54 if (file == nullptr) {
55 return false;
56 }
57 pclose(file);
58 return true;
59 }
60
CheckFilePathISExist(const std::string & filePath)61 bool CommonTool::CheckFilePathISExist(const std::string &filePath)
62 {
63 if (access(filePath.c_str(), F_OK) == 0) {
64 return true;
65 }
66 return false;
67 }
68
CopyTmpFileToSystemPath(const std::string & srcPath,const std::string & dstPath)69 bool CommonTool::CopyTmpFileToSystemPath(const std::string &srcPath, const std::string &dstPath)
70 {
71 if (access(srcPath.c_str(), F_OK) != 0) {
72 return false;
73 }
74 FILE *srcFile = fopen(srcPath.c_str(), "r");
75 if (srcFile == nullptr) {
76 return false;
77 }
78 FILE *dstFile = fopen(dstPath.c_str(), "w");
79 if (dstFile == nullptr) {
80 fclose(srcFile);
81 return false;
82 }
83
84 constexpr int READ_SIZE = 1024;
85 std::string buff;
86 buff.reserve(READ_SIZE);
87 buff.resize(READ_SIZE - 1);
88 size_t count = 0;
89 while ((count = fread(&(buff[0]), 1, READ_SIZE - 1, srcFile)) != 0) {
90 fwrite(&(buff[0]), 1, count, dstFile);
91 }
92
93 fclose(srcFile);
94 fclose(dstFile);
95 return true;
96 }
97
GetFileBuildTime(const std::string & path)98 long CommonTool::GetFileBuildTime(const std::string &path)
99 {
100 long buildTime = 0;
101
102 if (!CheckFilePathISExist(path)) {
103 return buildTime;
104 }
105
106 struct stat buf;
107 FILE *file = fopen(path.c_str(), "r");
108 if (file == nullptr) {
109 return buildTime;
110 }
111 int fd = fileno(file);
112 if (fd == -1) {
113 fclose(file);
114 return buildTime;
115 }
116 fstat(fd, &buf);
117 buildTime = buf.st_ctime;
118 fclose(file);
119 return buildTime;
120 }
121
GetPid(const std::string & processName)122 int32_t CommonTool::GetPid(const std::string &processName)
123 {
124 std::string cmd = "ps -A | grep " + processName;
125 int32_t size = 1024;
126 std::vector<std::string> echoContents;
127 int32_t queryResult = ExcuteCmd(cmd, echoContents, size);
128 if (queryResult == 0) {
129 return 0;
130 }
131
132 for (std::string echoContent : echoContents) {
133 auto index = echoContent.find(processName);
134 if (index != std::string::npos) {
135 int32_t pid = 0;
136 std::vector<std::string> strsRet;
137 OHOS::SplitStr(echoContent, " ", strsRet);
138 for (std::string item : strsRet) {
139 if (OHOS::StrToInt(item, pid)) {
140 break;
141 }
142 }
143 return pid;
144 }
145 }
146 return 0;
147 }
148
VectorToStr(const std::vector<std::string> & strVector)149 std::string CommonTool::VectorToStr(const std::vector<std::string> &strVector)
150 {
151 std::string str;
152 for (auto it = strVector.begin(); it != strVector.end(); it++) {
153 str.append(*it);
154 }
155 return str;
156 }
157
StartExecutable(const std::string & serviceName,const std::string & args)158 bool CommonTool::StartExecutable(const std::string &serviceName, const std::string &args)
159 {
160 std::string cmd = "chmod +x /system/bin/" + serviceName;
161 int res = system(cmd.c_str());
162 if (res != 0) {
163 return false;
164 }
165
166 int32_t pid = GetPid(serviceName);
167 if (pid != 0) {
168 return true;
169 }
170
171 std::string exeCmd = "/system/bin/" + serviceName + " " + args + "&";
172 int cmdRes = system(exeCmd.c_str());
173 if (cmdRes != 0) {
174 return false;
175 }
176
177 std::vector<std::string> resvec;
178 int32_t size = 1024;
179 int32_t contentSize = ExcuteCmd("ps -A |grep " + serviceName, resvec, size);
180 if (contentSize == 0) {
181 return false;
182 }
183 return true;
184 }
185 } // namespace AppExecFwk
186 } // namespace OHOS