1 /* 2 * Copyright (c) 2024 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 "test_server_service.h" 17 18 #include "iremote_object.h" 19 #include "system_ability_definition.h" 20 #include "hilog/log.h" 21 #include "parameters.h" 22 #include "iservice_registry.h" 23 #include "test_server_error_code.h" 24 #include "pasteboard_client.h" 25 #include "socperf_client.h" 26 #include <sstream> 27 namespace OHOS::testserver { 28 // TEST_SERVER_SA_ID 29 REGISTER_SYSTEM_ABILITY_BY_ID(TestServerService, TEST_SERVER_SA_ID, false); // SA run on demand 30 31 using namespace std; 32 using namespace OHOS::HiviewDFX; 33 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL_SERVICE = {LOG_CORE, 0xD003110, "TestServerService"}; 34 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL_TIMER = {LOG_CORE, 0xD003110, "CallerDetectTimer"}; 35 static const int CALLER_DETECT_DURING = 10000; 36 static const int START_SPDAEMON_PROCESS = 1; 37 static const int KILL_SPDAEMON_PROCESS = 2; 38 TestServerService(int32_t saId,bool runOnCreate)39 TestServerService::TestServerService(int32_t saId, bool runOnCreate) : SystemAbility(saId, runOnCreate) 40 { 41 HiLog::Info(LABEL_SERVICE, "%{public}s called. saId=%{public}d, runOnCreate=%{public}d", 42 __func__, saId, runOnCreate); 43 StartCallerDetectTimer(); 44 } 45 ~TestServerService()46 TestServerService::~TestServerService() 47 { 48 HiLog::Info(LABEL_SERVICE, "%{public}s called.", __func__); 49 if (callerDetectTimer_ == nullptr) { 50 HiLog::Error(LABEL_SERVICE, "%{public}s. callerDetectTimer_ is nullptr.", __func__); 51 return; 52 } 53 callerDetectTimer_->Cancel(); 54 } 55 OnStart()56 void TestServerService::OnStart() 57 { 58 HiLog::Info(LABEL_SERVICE, "%{public}s called.", __func__); 59 if (!IsRootVersion() && !IsDeveloperMode()) { 60 HiLog::Error(LABEL_SERVICE, "%{public}s. System mode is unsatisfied.", __func__); 61 return; 62 } 63 bool res = Publish(this); 64 if (!res) { 65 HiLog::Error(LABEL_SERVICE, "%{public}s. Publish failed", __func__); 66 } 67 } 68 OnStop()69 void TestServerService::OnStop() 70 { 71 HiLog::Info(LABEL_SERVICE, "%{public}s called.", __func__); 72 IsDeveloperMode(); 73 } 74 IsRootVersion()75 bool TestServerService::IsRootVersion() 76 { 77 bool debugmode = OHOS::system::GetBoolParameter("const.debuggable", false); 78 HiLog::Info(LABEL_SERVICE, "%{public}s. debugmode=%{public}d", __func__, debugmode); 79 return debugmode; 80 } 81 IsDeveloperMode()82 bool TestServerService::IsDeveloperMode() 83 { 84 bool developerMode = OHOS::system::GetBoolParameter("const.security.developermode.state", false); 85 HiLog::Info(LABEL_SERVICE, "%{public}s. developerMode=%{public}d", __func__, developerMode); 86 return developerMode; 87 } 88 StartCallerDetectTimer()89 void TestServerService::StartCallerDetectTimer() 90 { 91 HiLog::Info(LABEL_SERVICE, "%{public}s called.", __func__); 92 callerDetectTimer_ = new CallerDetectTimer(this); 93 callerDetectTimer_->Start(); 94 } 95 CreateSession(const SessionToken & sessionToken)96 ErrCode TestServerService::CreateSession(const SessionToken &sessionToken) 97 { 98 HiLog::Info(LABEL_SERVICE, "%{public}s called.", __func__); 99 bool result = true; 100 try { 101 result = sessionToken.AddDeathRecipient( 102 sptr<TestServerProxyDeathRecipient>(new TestServerProxyDeathRecipient(this))); 103 } catch(...) { 104 result = false; 105 } 106 if (!result) { 107 HiLog::Error(LABEL_SERVICE, "%{public}s. AddDeathRecipient FAILD.", __func__); 108 DestorySession(); 109 return TEST_SERVER_ADD_DEATH_RECIPIENT_FAILED; 110 } 111 AddCaller(); 112 HiLog::Info(LABEL_SERVICE, "%{public}s. Create session SUCCESS. callerCount=%{public}d", 113 __func__, GetCallerCount()); 114 return TEST_SERVER_OK; 115 } 116 SetPasteData(const std::string & text)117 ErrCode TestServerService::SetPasteData(const std::string& text) 118 { 119 HiLog::Info(LABEL_SERVICE, "%{public}s called.", __func__); 120 auto pasteBoardMgr = MiscServices::PasteboardClient::GetInstance(); 121 pasteBoardMgr->Clear(); 122 auto pasteData = pasteBoardMgr->CreatePlainTextData(text); 123 if (pasteData == nullptr) { 124 return TEST_SERVER_CREATE_PASTE_DATA_FAILED; 125 } 126 int32_t ret = pasteBoardMgr->SetPasteData(*pasteData); 127 int32_t successErrCode = 27787264; 128 return ret == successErrCode ? TEST_SERVER_OK : TEST_SERVER_SET_PASTE_DATA_FAILED; 129 } 130 PublishCommonEvent(const EventFwk::CommonEventData & event,bool & re)131 ErrCode TestServerService::PublishCommonEvent(const EventFwk::CommonEventData &event, bool &re) 132 { 133 if (!EventFwk::CommonEventManager::PublishCommonEvent(event)) { 134 HiLog::Info(LABEL_SERVICE, "%{public}s Pulbish commonEvent.", __func__); 135 re = false; 136 } 137 re = true; 138 int32_t ret = re ? TEST_SERVER_OK : TEST_SERVER_PUBLISH_EVENT_FAILED; 139 return ret; 140 } 141 AddCaller()142 void TestServerService::AddCaller() 143 { 144 callerCount_++; 145 } 146 RemoveCaller()147 void TestServerService::RemoveCaller() 148 { 149 callerCount_--; 150 } 151 GetCallerCount()152 int TestServerService::GetCallerCount() 153 { 154 return callerCount_.load(); 155 } 156 DestorySession()157 void TestServerService::DestorySession() 158 { 159 HiLog::Info(LABEL_SERVICE, "%{public}s called.", __func__); 160 if (callerCount_ == 0) { 161 HiLog::Info(LABEL_SERVICE, "%{public}s. No proxy exists. Remove the TestServer", __func__); 162 RemoveTestServer(); 163 } else { 164 HiLog::Info(LABEL_SERVICE, "%{public}s. Other proxys exist. Can not remove the TestServer", __func__); 165 } 166 } 167 RemoveTestServer()168 bool TestServerService::RemoveTestServer() 169 { 170 HiLog::Info(LABEL_SERVICE, "%{public}s called. ", __func__); 171 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 172 if (samgr == nullptr) { 173 HiLog::Error(LABEL_SERVICE, "%{public}s. Get SystemAbility Manager failed!", __func__); 174 return false; 175 } 176 auto res = samgr->UnloadSystemAbility(TEST_SERVER_SA_ID); 177 return res == ERR_OK; 178 } 179 OnRemoteDied(const wptr<IRemoteObject> & object)180 void TestServerService::TestServerProxyDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &object) 181 { 182 HiLog::Info(LABEL_SERVICE, "%{public}s called.", __func__); 183 if (object == nullptr) { 184 HiLog::Error(LABEL_SERVICE, "%{public}s. IRemoteObject is NULL.", __func__); 185 return; 186 } 187 testServerService_->RemoveCaller(); 188 testServerService_->DestorySession(); 189 } 190 Start()191 void TestServerService::CallerDetectTimer::Start() 192 { 193 HiLog::Info(LABEL_TIMER, "%{public}s called.", __func__); 194 thread_ = thread([this] { 195 this_thread::sleep_for(chrono::milliseconds(CALLER_DETECT_DURING)); 196 HiLog::Info(LABEL_TIMER, "%{public}s. Timer is done.", __func__); 197 if (!testServerExit_) { 198 testServerService_->DestorySession(); 199 } 200 }); 201 thread_.detach(); 202 } 203 Cancel()204 void TestServerService::CallerDetectTimer::Cancel() 205 { 206 HiLog::Info(LABEL_TIMER, "%{public}s called.", __func__); 207 testServerExit_ = true; 208 } 209 FrequencyLock()210 ErrCode TestServerService::FrequencyLock() 211 { 212 HiLog::Info(LABEL_SERVICE, "%{public}s called.", __func__); 213 int performanceModeId = 9100; 214 OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequest(performanceModeId, ""); 215 return TEST_SERVER_OK; 216 } 217 SpDaemonProcess(int daemonCommand)218 ErrCode TestServerService::SpDaemonProcess(int daemonCommand) 219 { 220 HiLog::Info(LABEL_SERVICE, "%{public}s called. daemonCommand: %{public}d", __func__, daemonCommand); 221 if (daemonCommand == START_SPDAEMON_PROCESS) { 222 std::system("./system/bin/SP_daemon &"); 223 } else if (daemonCommand == KILL_SPDAEMON_PROCESS) { 224 const std::string spDaemonProcessName = "SP_daemon"; 225 KillProcess(spDaemonProcessName); 226 } 227 return TEST_SERVER_OK; 228 } 229 KillProcess(const std::string & processName)230 void TestServerService::KillProcess(const std::string& processName) 231 { 232 std::string cmd = "ps -ef | grep -v grep | grep " + processName; 233 if (cmd.empty()) { 234 return; 235 } 236 FILE *fd = popen(cmd.c_str(), "r"); 237 if (fd == nullptr) { 238 return; 239 } 240 char buf[4096] = {'\0'}; 241 while ((fgets(buf, sizeof(buf), fd)) != nullptr) { 242 std::string line(buf); 243 HiLog::Info(LABEL_SERVICE, "line %s", line.c_str()); 244 std::istringstream iss(line); 245 std::string field; 246 std::string pid = "-1"; 247 int count = 0; 248 while (iss >> field) { 249 if (count == 1) { 250 pid = field; 251 break; 252 } 253 count++; 254 } 255 HiLog::Info(LABEL_SERVICE, "pid %s", pid.c_str()); 256 cmd = "kill " + pid; 257 FILE *fpd = popen(cmd.c_str(), "r"); 258 if (pclose(fpd) == -1) { 259 HiLog::Info(LABEL_SERVICE, "Error: Failed to close file"); 260 return; 261 } 262 } 263 if (pclose(fd) == -1) { 264 HiLog::Info(LABEL_SERVICE, "Error: Failed to close file"); 265 return; 266 } 267 } 268 269 } // namespace OHOS::testserver