1 /*
2 * Copyright (c) 2022 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 "shell_command_executor.h"
17
18 #include <chrono>
19 #include <cinttypes>
20 #include <iostream>
21
22 #include "hilog_wrapper.h"
23
24 using namespace std::chrono_literals;
25 namespace OHOS {
26 namespace AAFwk {
ShellCommandExecutor(const std::string & cmd,const int64_t timeoutSec)27 ShellCommandExecutor::ShellCommandExecutor(const std::string &cmd, const int64_t timeoutSec)
28 : cmd_(cmd), timeoutSec_(timeoutSec)
29 {
30 handler_ = std::make_shared<AppExecFwk::EventHandler>(AppExecFwk::EventRunner::Create());
31 }
32
WaitWorkDone()33 ShellCommandResult ShellCommandExecutor::WaitWorkDone()
34 {
35 HILOG_INFO("enter");
36
37 std::unique_lock<std::mutex> syncLock(mtxSyncWork_);
38
39 if (!DoWork()) {
40 HILOG_INFO("Failed to execute command : \"%{public}s\"", cmd_.data());
41 return cmdResult_;
42 }
43
44 std::unique_lock<std::mutex> workLock(mtxWork_);
45 syncLock.unlock();
46
47 if (timeoutSec_ <= 0) {
48 cvWork_.wait(workLock);
49 } else if (cvWork_.wait_for(workLock, timeoutSec_ * 1s) == std::cv_status::timeout) {
50 HILOG_WARN("Command execution timed out! cmd : \"%{public}s\", timeoutSec : %{public}" PRId64,
51 cmd_.data(), timeoutSec_);
52 std::cout << "Warning! Command execution timed out! cmd : " << cmd_ << ", timeoutSec : " << timeoutSec_
53 << std::endl;
54
55 ShellCommandResult realResult;
56 realResult.exitCode = -1;
57 {
58 std::lock_guard<std::mutex> copyLock(mtxCopy_);
59 realResult.stdResult = cmdResult_.stdResult;
60 }
61 return realResult;
62 }
63
64 HILOG_INFO("Command execution complete, cmd : \"%{public}s\", exitCode : %{public}d",
65 cmd_.data(), cmdResult_.exitCode);
66 return cmdResult_;
67 }
68
DoWork()69 bool ShellCommandExecutor::DoWork()
70 {
71 HILOG_INFO("enter");
72
73 if (cmd_.empty()) {
74 HILOG_ERROR("Invalid command");
75 return false;
76 }
77
78 if (!handler_) {
79 HILOG_ERROR("Invalid event handler");
80 return false;
81 }
82
83 auto self(shared_from_this());
84 handler_->PostTask([this, self]() {
85 HILOG_INFO("DoWork async task begin, cmd : \"%{public}s\"", cmd_.data());
86
87 std::unique_lock<std::mutex> syncLock(mtxSyncWork_);
88
89 FILE *file = popen(cmd_.c_str(), "r");
90 if (!file) {
91 HILOG_ERROR("Failed to call popen, cmd : \"%{public}s\"", cmd_.data());
92
93 cvWork_.notify_one();
94 HILOG_INFO("DoWork async task end, cmd : \"%{public}s\"", cmd_.data());
95 return;
96 }
97
98 char commandResult[1024] = {'\0'};
99 while ((fgets(commandResult, sizeof(commandResult), file)) != nullptr) {
100 {
101 std::lock_guard<std::mutex> copyLock(mtxCopy_);
102 cmdResult_.stdResult.append(commandResult);
103 }
104 std::cout << commandResult;
105 }
106
107 cmdResult_.exitCode = pclose(file);
108 file = nullptr;
109
110 cvWork_.notify_one();
111 HILOG_INFO("DoWork async task end, cmd : \"%{public}s\"", cmd_.data());
112 });
113
114 return true;
115 }
116 } // namespace AAFwk
117 } // namespace OHOS