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 #include <cstdlib>
16 #include <sys/mount.h>
17 #include <sys/wait.h>
18 
19 #include "daemon_unity.h"
20 
21 namespace Hdc {
HdcDaemonUnity(HTaskInfo hTaskInfo)22 HdcDaemonUnity::HdcDaemonUnity(HTaskInfo hTaskInfo)
23     : HdcTaskBase(hTaskInfo)
24 {
25     currentDataCommand = CMD_KERNEL_ECHO_RAW;  // Default output to shelldata
26 }
27 
~HdcDaemonUnity()28 HdcDaemonUnity::~HdcDaemonUnity()
29 {
30     WRITE_LOG(LOG_DEBUG, "~HdcDaemonUnity channelId:%u", taskInfo->channelId);
31 }
32 
StopTask()33 void HdcDaemonUnity::StopTask()
34 {
35     // Remove jpid tracker when stopping task
36     RemoveJdwpTracker();
37     asyncCommand.DoRelease();
38 }
39 
ReadyForRelease()40 bool HdcDaemonUnity::ReadyForRelease()
41 {
42     if (!HdcTaskBase::ReadyForRelease() || !asyncCommand.ReadyForRelease()) {
43         WRITE_LOG(LOG_DEBUG, "not ready for release channelId:%u", taskInfo->channelId);
44         return false;
45     }
46     return true;
47 }
48 
AsyncCmdOut(bool finish, int64_t exitStatus, const string result)49 bool HdcDaemonUnity::AsyncCmdOut(bool finish, int64_t exitStatus, const string result)
50 {
51 #ifdef UNIT_TEST
52     Base::WriteBinFile((UT_TMP_PATH + "/execute.result").c_str(), (uint8_t *)result.c_str(), result.size(),
53                        countUt++ == 0);
54 #endif
55     bool ret = false;
56     bool wantFinish = false;
57     do {
58         if (finish) {
59             wantFinish = true;
60             ret = true;
61             --refCount;
62             break;
63         }
64         if (!SendToAnother(currentDataCommand, reinterpret_cast<uint8_t *>(const_cast<char *>(result.c_str())),
65             result.size())) {
66             break;
67         }
68         ret = true;
69     } while (false);
70     if (wantFinish) {
71         TaskFinish();
72     }
73     return ret;
74 }
75 
ExecuteShell(const char *shellCommand)76 int HdcDaemonUnity::ExecuteShell(const char *shellCommand)
77 {
78     do {
79         AsyncCmd::CmdResultCallback funcResultOutput;
80         funcResultOutput = [this](bool finish, int64_t exitStatus, const string result) -> bool {
81             return this->AsyncCmdOut(finish, exitStatus, result);
82         };
83         if (!asyncCommand.Initial(loopTask, funcResultOutput)) {
84             break;
85         }
86         asyncCommand.ExecuteCommand(shellCommand);
87         ++refCount;
88         return RET_SUCCESS;
89     } while (false);
90 
91     TaskFinish();
92     WRITE_LOG(LOG_DEBUG, "Shell failed finish");
93     return -1;
94 }
95 
FindMountDeviceByPath(const char *toQuery, char *dev)96 bool HdcDaemonUnity::FindMountDeviceByPath(const char *toQuery, char *dev)
97 {
98     int ret = false;
99     int len = BUF_SIZE_DEFAULT2;
100     char buf[BUF_SIZE_DEFAULT2];
101 
102     FILE *fp = fopen("/proc/mounts", "r");
103     if (fp == nullptr) {
104         WRITE_LOG(LOG_FATAL, "fopen /proc/mounts error:%d", errno);
105         return false;
106     }
107 
108     while (fgets(buf, len, fp) != nullptr) {
109         char dir[BUF_SIZE_SMALL] = "";
110         int freq;
111         int passnno;
112         int res = 0;
113         // clang-format off
114         res = sscanf_s(buf, "%255s %255s %*s %*s %d %d\n", dev, BUF_SIZE_SMALL - 1,
115                        dir, BUF_SIZE_SMALL - 1, &freq, &passnno);
116         // clang-format on
117         dev[BUF_SIZE_SMALL - 1] = '\0';
118         dir[BUF_SIZE_SMALL - 1] = '\0';
119         if (res == 4 && (strcmp(toQuery, dir) == 0)) {  // 4 : The correct number of parameters
120             WRITE_LOG(LOG_DEBUG, "FindMountDeviceByPath dev:%s dir:%s", dev, dir);
121             ret = true;
122             break;
123         }
124     }
125     int rc = fclose(fp);
126     if (rc != 0) {
127         WRITE_LOG(LOG_WARN, "fclose rc:%d error:%d", rc, errno);
128     }
129     if (!ret) {
130         WRITE_LOG(LOG_FATAL, "FindMountDeviceByPath not found %s", toQuery);
131     }
132     return ret;
133 }
134 
RemountPartition(const char *dir)135 bool HdcDaemonUnity::RemountPartition(const char *dir)
136 {
137     int fd;
138     int off = 0;
139     char dev[BUF_SIZE_SMALL] = "";
140 
141     if (!FindMountDeviceByPath(dir, dev) || strlen(dev) < 4) {  // 4 : file count
142         WRITE_LOG(LOG_FATAL, "FindMountDeviceByPath failed %s", dir);
143         return false;
144     }
145 
146     if ((fd = open(dev, O_RDONLY | O_CLOEXEC)) < 0) {
147         WRITE_LOG(LOG_FATAL, "Open device:%s failed,error:%d", dev, errno);
148         return false;
149     }
150     ioctl(fd, BLKROSET, &off);
151     Base::CloseFd(fd);
152 
153     if (mount(dev, dir, "none", MS_REMOUNT, nullptr) < 0) {
154         WRITE_LOG(LOG_FATAL, "Mount device failed dev:%s dir:%s error:%d", dev, dir, errno);
155         return false;
156     }
157     return true;
158 }
159 
CallRemount()160 bool HdcDaemonUnity::CallRemount()
161 {
162     int pipefd[2];
163     pid_t pid;
164     int exitStatus;
165 
166     if (pipe(pipefd) == -1) {
167         WRITE_LOG(LOG_FATAL, "Failed to create pipe: %s", strerror(errno));
168         return false;
169     }
170     pid = fork();
171     if (pid < 0) {
172         WRITE_LOG(LOG_FATAL, "Failed to fork: %s", strerror(errno));
173         return false;
174     }
175     if (pid == 0) {
176         close(pipefd[0]);
177         signal(SIGCHLD, SIG_DFL);
178         exitStatus = system("remount");
179         write(pipefd[1], &exitStatus, sizeof(int));
180         close(pipefd[1]);
181         _exit(0);
182     } else {
183         close(pipefd[1]);
184         read(pipefd[0], &exitStatus, sizeof(int));
185         close(pipefd[0]);
186         waitpid(pid, nullptr, 0);
187         if (exitStatus == -1) {
188             WRITE_LOG(LOG_FATAL, "Failed to execute /bin/remount: %s", strerror(errno));
189             return false;
190         } else if (WIFEXITED(exitStatus) && WEXITSTATUS(exitStatus) != 0) {
191             WRITE_LOG(LOG_FATAL, "Remount failed with exit code: %d", WEXITSTATUS(exitStatus));
192             return false;
193         }
194     }
195 
196     return true;
197 }
198 
RemountDevice()199 bool HdcDaemonUnity::RemountDevice()
200 {
201     if (getuid() != 0) {
202         LogMsg(MSG_FAIL, "Operate need running as root");
203         return false;
204     }
205     struct stat info;
206     if (!lstat("/vendor", &info) && (info.st_mode & S_IFMT) == S_IFDIR) {
207         if (!RemountPartition("/vendor")) {
208             WRITE_LOG(LOG_FATAL, "Mount failed /vendor (via mount)");
209         }
210     }
211     if (!lstat("/system", &info) && (info.st_mode & S_IFMT) == S_IFDIR) {
212         if (!RemountPartition("/")) {
213             WRITE_LOG(LOG_FATAL, "Mount failed /system (via mount)");
214         }
215     }
216     if (CallRemount()) {
217         LogMsg(MSG_OK, "Mount finish");
218         return true;
219     } else {
220         LogMsg(MSG_FAIL, "Mount failed");
221         return false;
222     }
223 }
224 
RebootDevice(const string &cmd)225 bool HdcDaemonUnity::RebootDevice(const string &cmd)
226 {
227     sync();
228     return SystemDepend::RebootDevice(cmd);
229 }
230 
SetDeviceRunMode(const char *cmd)231 bool HdcDaemonUnity::SetDeviceRunMode(const char *cmd)
232 {
233     WRITE_LOG(LOG_INFO, "Set run mode:%s", cmd);
234     string tmp(cmd);
235     char *ptr = tmp.data();
236     char *token = nullptr;
237 
238 #ifdef HDC_EMULATOR
239     LogMsg(MSG_FAIL, "[E001300]Not support tmode for Emulator");
240     return true;
241 #endif
242 
243     // hdc tmode usb, do nothing
244     if (strcmp(CMDSTR_TMODE_USB.c_str(), cmd) == 0) {
245         LogMsg(MSG_FAIL, "[E001000]For USB debugging, please set it on the device's Settings UI");
246         return true;
247     }
248     // not usb and not tcp
249     if (strncmp("port", cmd, strlen("port")) != 0) {
250         LogMsg(MSG_FAIL, "[E001001]Unknown command");
251         return false;
252     }
253 
254     // bypass port
255     token = strtok_r(ptr, " ", &ptr);
256     // get next token
257     token = strtok_r(ptr, " ", &ptr);
258     // hdc tmode port
259     if (token == nullptr) {
260         LogMsg(MSG_OK, "Set device run mode successful.");
261         SystemDepend::SetDevItem("persist.hdc.mode", "tcp");
262         SystemDepend::SetDevItem("persist.hdc.mode.tcp", "enable");
263     } else {
264         /*
265         * hdc tmode port xxxxxx
266         * hdc tmode port close
267         */
268         if (strcmp(token, "close") == 0) {
269             SystemDepend::SetDevItem("persist.hdc.port", "0");
270             SystemDepend::SetDevItem("persist.hdc.mode.tcp", "disable");
271         } else {
272             string tmp(token);
273             if (tmp.find_first_not_of("0123456789") != string::npos) {
274                 LogMsg(MSG_FAIL, "[E001100]Invalid port");
275                 return false;
276             }
277             LogMsg(MSG_OK, "Set device run mode successful.");
278             SystemDepend::SetDevItem("persist.hdc.mode", "tcp");
279             SystemDepend::SetDevItem("persist.hdc.port", token);
280             SystemDepend::SetDevItem("persist.hdc.mode.tcp", "enable");
281         }
282     }
283     return true;
284 }
285 
GetHiLog(const char *cmd)286 inline bool HdcDaemonUnity::GetHiLog(const char *cmd)
287 {
288     string cmdDo = "hilog";
289     if (cmd && !strcmp(const_cast<char *>(cmd), "h")) {
290         cmdDo += " -h";
291     }
292     ExecuteShell(cmdDo.c_str());
293     return true;
294 }
295 
ListJdwpProcess(void *daemonIn)296 inline bool HdcDaemonUnity::ListJdwpProcess(void *daemonIn)
297 {
298     HdcDaemon *daemon = (HdcDaemon *)daemonIn;
299     string result = ((HdcJdwp *)daemon->clsJdwp)->GetProcessList();
300     if (!result.size()) {
301         result = EMPTY_ECHO;
302     } else {
303         result.erase(result.end() - 1);  // remove tail \n
304     }
305     LogMsg(MSG_OK, result.c_str());
306     return true;
307 }
308 
TrackJdwpProcess(void *daemonIn, const string& param)309 inline bool HdcDaemonUnity::TrackJdwpProcess(void *daemonIn, const string& param)
310 {
311     HdcDaemon *daemon = static_cast<HdcDaemon *>(daemonIn);
312     taskInfo->debugRelease = 1;
313     if (param == "p") {
314         taskInfo->debugRelease = 0;
315     } else if (param == "a") {
316         // allApp with display debug or release
317         constexpr uint8_t allAppWithDr = 3;
318         taskInfo->debugRelease = allAppWithDr;
319     }
320     if (!((static_cast<HdcJdwp *>(daemon->clsJdwp))->CreateJdwpTracker(taskInfo))) {
321         string result = MESSAGE_FAIL;
322         LogMsg(MSG_OK, result.c_str());
323         return false;
324     }
325     return true;
326 }
327 
RemoveJdwpTracker()328 inline void HdcDaemonUnity::RemoveJdwpTracker()
329 {
330     HdcDaemon *daemon = static_cast<HdcDaemon *>(taskInfo->ownerSessionClass);
331     (static_cast<HdcJdwp *>(daemon->clsJdwp))->RemoveJdwpTracker(taskInfo);
332 }
333 
CommandDispatch(const uint16_t command, uint8_t *payload, const int payloadSize)334 bool HdcDaemonUnity::CommandDispatch(const uint16_t command, uint8_t *payload, const int payloadSize)
335 {
336     bool ret = true;
337     HdcDaemon *daemon = (HdcDaemon *)taskInfo->ownerSessionClass;
338     // Both are not executed, do not need to be detected 'childReady'
339     string strPayload = string(reinterpret_cast<char *>(payload), payloadSize);
340 #ifdef HDC_DEBUG
341     WRITE_LOG(LOG_DEBUG, "CommandDispatch command:%d", command);
342 #endif // HDC_DEBUG
343     switch (command) {
344         case CMD_UNITY_EXECUTE: {
345             ExecuteShell(const_cast<char *>(strPayload.c_str()));
346             break;
347         }
348         case CMD_UNITY_REMOUNT: {
349             ret = false;
350             RemountDevice();
351             break;
352         }
353         case CMD_UNITY_REBOOT: {
354             ret = false;
355             RebootDevice(strPayload);
356             break;
357         }
358         case CMD_UNITY_RUNMODE: {
359             ret = false;
360             SetDeviceRunMode(strPayload.c_str());
361             break;
362         }
363         case CMD_UNITY_HILOG: {
364             GetHiLog(strPayload.c_str());
365             break;
366         }
367         case CMD_UNITY_ROOTRUN: {
368             ret = false;
369             string debugMode;
370             // hdcd restart in old pid
371             bool restart = true;
372             SystemDepend::GetDevItem("const.debuggable", debugMode);
373             if (debugMode == "1") {
374                 if (payloadSize != 0 && !strcmp(strPayload.c_str(), "r")) {
375                     SystemDepend::SetDevItem("persist.hdc.root", "0");
376                 } else {
377                     // hdcd restart in new pid
378                     restart = false;
379                     SystemDepend::SetDevItem("persist.hdc.root", "1");
380                 }
381             } else {
382                 LogMsg(MSG_FAIL, "Cannot set root run mode in undebuggable version.");
383                 return false;
384             }
385             daemon->PostStopInstanceMessage(restart);
386             break;
387         }
388         case CMD_UNITY_BUGREPORT_INIT: {
389             currentDataCommand = CMD_UNITY_BUGREPORT_DATA;
390             ExecuteShell("hidumper");
391             break;
392         }
393         case CMD_JDWP_LIST: {
394             ret = false;
395             ListJdwpProcess(daemon);
396             break;
397         }
398         case CMD_JDWP_TRACK: {
399             if (!TrackJdwpProcess(daemon, strPayload)) {
400                 ret = false;
401             }
402             break;
403         }
404         default:
405             break;
406     }
407 #ifdef HDC_DEBUG
408     WRITE_LOG(LOG_DEBUG, "CommandDispatch command:%d finish.", command);
409 #endif // HDC_LOCAL_DEBUG
410     return ret;
411 };
412 }  // namespace Hdc
413