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 <algorithm>
16 #include <cstring>
17 #include <iostream>
18 #include <memory>
19 #include <mutex>
20 #include <thread>
21 #include <sys/prctl.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24
25 #include <log_utils.h>
26 #include <seq_packet_socket_server.h>
27
28 #include "service_controller.h"
29 #include "cmd_executor.h"
30
31 namespace OHOS {
32 namespace HiviewDFX {
33 static const int MAX_CLIENT_CONNECTIONS = 100;
34
~CmdExecutor()35 CmdExecutor::~CmdExecutor()
36 {
37 std::lock_guard<std::mutex> lg(m_clientAccess);
38 for (auto& client : m_clients) {
39 client->m_stopThread.store(true);
40 }
41 for (auto& client : m_clients) {
42 if (client->m_clientThread.joinable()) {
43 client->m_clientThread.join();
44 }
45 }
46 }
47
MainLoop(const std::string & socketName)48 void CmdExecutor::MainLoop(const std::string& socketName)
49 {
50 SeqPacketSocketServer cmdServer(socketName, MAX_CLIENT_CONNECTIONS);
51 if (cmdServer.Init() < 0) {
52 std::cerr << "Failed to init control socket ! \n";
53 return;
54 }
55 std::cout << "Server started to listen !\n";
56 using namespace std::chrono_literals;
57 cmdServer.StartAcceptingConnection(
58 [this] (std::unique_ptr<Socket> handler) {
59 OnAcceptedConnection(std::move(handler));
60 },
61 3000ms,
62 [this] () {
63 CleanFinishedClients();
64 });
65 }
66
OnAcceptedConnection(std::unique_ptr<Socket> handler)67 void CmdExecutor::OnAcceptedConnection(std::unique_ptr<Socket> handler)
68 {
69 std::lock_guard<std::mutex> lg(m_clientAccess);
70 auto newVal = std::make_unique<ClientThread>();
71 if (newVal != nullptr) {
72 newVal->m_stopThread.store(false);
73 newVal->m_clientThread = std::thread(&CmdExecutor::ClientEventLoop, this, std::move(handler));
74 m_clients.push_back(std::move(newVal));
75 }
76 }
77
ClientEventLoop(std::unique_ptr<Socket> handler)78 void CmdExecutor::ClientEventLoop(std::unique_ptr<Socket> handler)
79 {
80 decltype(m_clients)::iterator clientInfoIt;
81 {
82 std::lock_guard<std::mutex> lg(m_clientAccess);
83 clientInfoIt = std::find_if(m_clients.begin(), m_clients.end(),
84 [](const std::unique_ptr<ClientThread>& ct) {
85 return ct->m_clientThread.get_id() == std::this_thread::get_id();
86 });
87 }
88 if (clientInfoIt == m_clients.end()) {
89 std::cerr << "Failed to find client\n";
90 return;
91 }
92
93 prctl(PR_SET_NAME, m_name.c_str());
94 ServiceController serviceCtrl(std::move(handler), m_logCollector, m_hilogBuffer);
95 serviceCtrl.CommunicationLoop((*clientInfoIt)->m_stopThread, m_cmdList);
96
97 std::lock_guard<std::mutex> ul(m_finishedClientAccess);
98 m_finishedClients.push_back(std::this_thread::get_id());
99 }
100
CleanFinishedClients()101 void CmdExecutor::CleanFinishedClients()
102 {
103 std::list<std::thread> threadsToJoin;
104 {
105 // select clients to clean up - pick threads that we have to be sure are ended
106 std::scoped_lock sl(m_finishedClientAccess, m_clientAccess);
107 for (auto threadId : m_finishedClients) {
108 auto clientInfoIt = std::find_if(m_clients.begin(), m_clients.end(),
109 [&threadId](const std::unique_ptr<ClientThread>& ct) {
110 return ct->m_clientThread.get_id() == threadId;
111 });
112 if (clientInfoIt != m_clients.end()) {
113 threadsToJoin.push_back(std::move((*clientInfoIt)->m_clientThread));
114 m_clients.erase(clientInfoIt);
115 }
116 }
117 m_finishedClients.clear();
118 }
119 for (auto& thread : threadsToJoin) {
120 if (thread.joinable()) {
121 thread.join();
122 }
123 }
124 }
125 } // namespace HiviewDFX
126 } // namespace OHOS
127