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