diff --git a/BUILD.gn b/BUILD.gn index 7190448132b0e4b4ffc4510f4d52bf5737a8e0d9..adc62b0b89aa5e19eb29f97a538bd3b0ee9cf5a5 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -398,6 +398,7 @@ ohos_executable("hdc") { sources += [ "src/common/password.cpp" ] sources += [ "src/common/hdc_huks.cpp" ] defines += [ "HDC_SUPPORT_ENCRYPT_PRIVATE_KEY" ] + defines += [ "HOST_OHOS" ] external_deps += [ "huks:libhukssdk" ] } diff --git a/src/common/base.cpp b/src/common/base.cpp index fac3bbe4aa330873a1611ebc4fb3169b5e15fb2f..b2d6366591e8538271080fc957f53709aef97195 100644 --- a/src/common/base.cpp +++ b/src/common/base.cpp @@ -195,7 +195,12 @@ namespace Base { #else int flags = UV_FS_O_RDWR | UV_FS_O_APPEND | UV_FS_O_CREAT; uv_fs_t req; - int fd = uv_fs_open(nullptr, &req, path, flags, S_IWUSR | S_IRUSR, nullptr); +#ifdef HOST_OHOS + mode_t mode = (S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP); +#else + mode_t mode = (S_IWUSR | S_IRUSR); +#endif + int fd = uv_fs_open(nullptr, &req, path, flags, mode, nullptr); if (fd < 0) { char buffer[BUF_SIZE_DEFAULT] = { 0 }; uv_strerror_r((int)req.result, buffer, BUF_SIZE_DEFAULT); @@ -662,9 +667,13 @@ static void EchoLog(string &buf) string debugInfo = functionName; GetLogDebugFunctionName(debugInfo, line, threadIdString); GetLogLevelAndTime(logLevel, logLevelString, timeString); +#ifdef OHOS_HOST + string logBuf = StringFormat("[%s][%s][%u]%s%s %s%s", logLevelString.c_str(), timeString.c_str(), + getuid(), threadIdString.c_str(), debugInfo.c_str(), buf, sep.c_str()); +#else string logBuf = StringFormat("[%s][%s]%s%s %s%s", logLevelString.c_str(), timeString.c_str(), threadIdString.c_str(), debugInfo.c_str(), buf, sep.c_str()); - +#endif EchoLog(logBuf); if (!g_logCache) { @@ -1347,7 +1356,12 @@ static void EchoLog(string &buf) resolvedPath = CanonicalizeSpecPath(srcPath); } uv_fs_t req; - int fd = uv_fs_open(nullptr, &req, resolvedPath.c_str(), flags, S_IWUSR | S_IRUSR, nullptr); +#ifdef HOST_OHOS + mode_t mode = (S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP); +#else + mode_t mode = (S_IWUSR | S_IRUSR); +#endif + int fd = uv_fs_open(nullptr, &req, resolvedPath.c_str(), flags, mode, nullptr); if (fd < 0) { char buffer[BUF_SIZE_DEFAULT] = { 0 }; uv_strerror_r((int)req.result, buffer, BUF_SIZE_DEFAULT); @@ -1387,10 +1401,17 @@ static void EchoLog(string &buf) char buf[BUF_SIZE_DEFAULT] = ""; char pidBuf[BUF_SIZE_TINY] = ""; size_t size = sizeof(buf); +#ifdef HOST_OHOS + if (uv_os_homedir(buf, &size) < 0) { + WRITE_LOG(LOG_FATAL, "Homepath failed"); + return ERR_API_FAIL; + } +#else if (uv_os_tmpdir(buf, &size) < 0) { WRITE_LOG(LOG_FATAL, "Tmppath failed"); return ERR_API_FAIL; } +#endif if (snprintf_s(bufPath, sizeof(bufPath), sizeof(bufPath) - 1, "%s%c.%s.pid", buf, Base::GetPathSep(), procname) < 0) { return ERR_BUF_OVERFLOW; @@ -2144,7 +2165,11 @@ static void EchoLog(string &buf) int value = -1; char path[PATH_MAX] = ""; size_t size = sizeof(path); +#ifdef HOST_OHOS + value = uv_os_homedir(path, &size); +#else value = uv_os_tmpdir(path, &size); +#endif if (value < 0) { constexpr int bufSize = 1024; char buf[bufSize] = { 0 }; @@ -2905,7 +2930,12 @@ void CloseOpenFd(void) { int flags = UV_FS_O_RDWR | UV_FS_O_CREAT | UV_FS_O_APPEND; uv_fs_t req; - int fd = uv_fs_open(nullptr, &req, path.c_str(), flags, S_IWUSR | S_IRUSR, nullptr); +#ifdef HOST_OHOS + mode_t mode = (S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP); +#else + mode_t mode = (S_IWUSR | S_IRUSR); +#endif + int fd = uv_fs_open(nullptr, &req, path.c_str(), flags, mode, nullptr); if (fd < 0) { char buffer[BUF_SIZE_DEFAULT] = { 0 }; uv_strerror_r((int)req.result, buffer, BUF_SIZE_DEFAULT); diff --git a/src/common/define.h b/src/common/define.h index 9c3fcb1f049097bccc816082035f865a9f328337..450a9951a2aaee5d66331b89aee5270f290879a9 100644 --- a/src/common/define.h +++ b/src/common/define.h @@ -147,6 +147,10 @@ const char HUGE_BUF_TAG = 'H'; // support huge buffer const size_t BANNER_FEATURE_TAG_OFFSET = 11; const char WAIT_DEVICE_TAG = 'W'; const size_t WAIT_TAG_OFFSET = 11; +#ifdef HOST_OHOS +const size_t SERVICE_KILL_OFFSET = 10; +const char SERVICE_KILL_TAG = 'K'; +#endif // input command const string CMDSTR_SOFTWARE_VERSION = "version"; const string CMDSTR_SOFTWARE_HELP = "help"; diff --git a/src/common/define_plus.h b/src/common/define_plus.h index 3c31ba29b3c0b49e3fdf0500bd375e259122aac6..c327ce6947b66f6e32b3c1f2f0804a9af764cd60 100644 --- a/src/common/define_plus.h +++ b/src/common/define_plus.h @@ -385,6 +385,9 @@ struct HdcChannel { bool connectLocalDevice = false; bool isStableBuf = false; std::atomic writeFailedTimes; +#ifdef HOST_OHOS + bool isSupportedKillServerCmd = false; +#endif #ifdef HDC_HOST uint64_t startTime = 0; uint64_t endTime = 0; diff --git a/src/host/client.cpp b/src/host/client.cpp index 4cf0db854dae8132263f1701e981d62c25d6df33..a5b5c2401e1147ffeab8487edae9edf5bea47cb0 100755 --- a/src/host/client.cpp +++ b/src/host/client.cpp @@ -57,13 +57,20 @@ void HdcClient::NotifyInstanceChannelFree(HChannel hChannel) uint32_t HdcClient::GetLastPID() { char bufPath[BUF_SIZE_MEDIUM] = ""; - size_t size = BUF_SIZE_MEDIUM; char pidBuf[BUF_SIZE_TINY] = ""; // get running pid to kill it + size_t size = BUF_SIZE_MEDIUM; +#ifdef HOST_OHOS + if (uv_os_homedir(bufPath, &size) < 0) { + WRITE_LOG(LOG_FATAL, "Homepath failed"); + return 0; + } +#else if (uv_os_tmpdir(bufPath, &size) < 0) { WRITE_LOG(LOG_FATAL, "Tmppath failed"); return 0; } +#endif string path = Base::StringFormat("%s%c.%s.pid", bufPath, Base::GetPathSep(), SERVER_NAME.c_str()); Base::ReadBinFile(path.c_str(), reinterpret_cast(&pidBuf), BUF_SIZE_TINY); int pid = atoi(pidBuf); // pid maybe 0 @@ -228,6 +235,9 @@ string HdcClient::AutoConnectKey(string &doCommand, const string &preConnectKey) vecNoConnectKeyCommand.push_back(CMDSTR_SOFTWARE_VERSION); vecNoConnectKeyCommand.push_back(CMDSTR_SOFTWARE_HELP); vecNoConnectKeyCommand.push_back(CMDSTR_TARGET_DISCOVER); +#ifdef HOST_OHOS + vecNoConnectKeyCommand.push_back(CMDSTR_SERVICE_KILL); +#endif vecNoConnectKeyCommand.push_back(CMDSTR_LIST_TARGETS); vecNoConnectKeyCommand.push_back(CMDSTR_CHECK_SERVER); vecNoConnectKeyCommand.push_back(CMDSTR_CONNECT_TARGET); @@ -518,6 +528,14 @@ void HdcClient::CommandWorker(uv_timer_t *handle) return; } uv_timer_stop(handle); +#ifdef HOST_OHOS + if (!strncmp(thisClass->command.c_str(), CMDSTR_SERVICE_KILL.c_str(), + CMDSTR_SERVICE_KILL.size()) && !thisClass->channel->isSupportedKillServerCmd) { + WRITE_LOG(LOG_DEBUG, "uv_kill server"); + thisClass->CtrlServiceWork(CMDSTR_SERVICE_KILL.c_str()); + return; + } +#endif WRITE_LOG(LOG_DEBUG, "Connect server successful"); bool closeInput = false; if (!HostUpdater::ConfirmCommand(thisClass->command, closeInput)) { @@ -693,8 +711,13 @@ int HdcClient::PreHandshake(HChannel hChannel, const uint8_t *buf) return ERR_BUF_CHECK; } hChannel->isStableBuf = (hShake->banner[BANNER_FEATURE_TAG_OFFSET] != HUGE_BUF_TAG); - WRITE_LOG(LOG_DEBUG, "Channel PreHandshake isStableBuf:%d", - hChannel->isStableBuf); +#ifdef HOST_OHOS + hChannel->isSupportedKillServerCmd = (hShake->banner[SERVICE_KILL_OFFSET] == SERVICE_KILL_TAG); + WRITE_LOG(LOG_DEBUG, "Channel PreHandshake isStableBuf:%d, killflag:%d", + hChannel->isStableBuf, hChannel->isSupportedKillServerCmd); +#else + WRITE_LOG(LOG_DEBUG, "Channel PreHandshake isStableBuf:%d", hChannel->isStableBuf); +#endif if (this->command == CMDSTR_WAIT_FOR && !connectKey.empty()) { hShake->banner[WAIT_TAG_OFFSET] = WAIT_DEVICE_TAG; } @@ -712,13 +735,11 @@ int HdcClient::PreHandshake(HChannel hChannel, const uint8_t *buf) WRITE_LOG(LOG_DEBUG, "Channel Hello failed"); return ERR_BUF_COPY; } - #ifdef HDC_VERSION_CHECK // add check version if (!isCheckVersionCmd) { // do not check version cause user want to get server version string clientVer = Base::GetVersion() + HDC_MSG_HASH; string serverVer(hShake->version); - if (clientVer != serverVer) { serverVer = serverVer.substr(0, Base::GetVersion().size()); WRITE_LOG(LOG_FATAL, "Client version:%s, server version:%s", clientVer.c_str(), serverVer.c_str()); diff --git a/src/host/host_usb.h b/src/host/host_usb.h index 21493c81ba7b257524601523a6abfced408777f2..15c1eafa5d760c33a34132299a9ed8eabbf29f1c 100644 --- a/src/host/host_usb.h +++ b/src/host/host_usb.h @@ -36,6 +36,7 @@ public: void RemoveIgnoreDevice(string &mountInfo); static libusb_log_level GetLibusbLogLevel(void); static void SetLibusbLogLevelEnv(libusb_log_level logLevel); + void SendSoftResetToDaemon(HSession hSession, uint32_t sessionIdOld); private: enum UsbCheckStatus { @@ -69,7 +70,6 @@ private: void CancelUsbIo(HSession hSession) override; int UsbToHdcProtocol(uv_stream_t *stream, uint8_t *appendData, int dataSize) override; int SubmitUsbBio(HSession hSession, bool sendOrRecv, uint8_t *buf, int bufSize); - void SendSoftResetToDaemon(HSession hSession, uint32_t sessionIdOld); libusb_context *ctxUSB; uv_timer_t devListWatcher; diff --git a/src/host/main.cpp b/src/host/main.cpp index 817e2b4082fd746b1ab205d31a6484c99531656f..8935b2b38a7f68a6c67398efb2f2872e1e01e65f 100644 --- a/src/host/main.cpp +++ b/src/host/main.cpp @@ -211,8 +211,12 @@ int RunClientMode(string &commands, string &serverListenString, string &connectK std::cerr << TranslateCommand::Usage(); return 0; } +#ifdef HOST_OHOS + if (!strncmp(commands.c_str(), CMDSTR_GENERATE_KEY.c_str(), CMDSTR_GENERATE_KEY.size())) { +#else if (!strncmp(commands.c_str(), CMDSTR_GENERATE_KEY.c_str(), CMDSTR_GENERATE_KEY.size()) || !strncmp(commands.c_str(), CMDSTR_SERVICE_KILL.c_str(), CMDSTR_SERVICE_KILL.size())) { +#endif client.CtrlServiceWork(commands.c_str()); return 0; } @@ -221,12 +225,29 @@ int RunClientMode(string &commands, string &serverListenString, string &connectK return 0; } if (isPullServer && Base::ProgramMutex(SERVER_NAME.c_str(), true) == 0) { +#ifdef HOST_OHOS + if (!strncmp(commands.c_str(), CMDSTR_SERVICE_KILL.c_str(), + CMDSTR_SERVICE_KILL.size())) { + WRITE_LOG(LOG_DEBUG, "kill server, but server not exist, so do nothing"); + return 0; + } +#endif // default pullup, just default listenstr.If want to customer listen-string, please use 'hdc -m -s lanip:port' HdcServer::PullupServer(serverListenString.c_str()); uv_sleep(START_SERVER_FOR_CLIENT_TIME); // give time to start serverForClient,at least 200ms } client.Initial(connectKey); client.ExecuteCommand(commands.c_str()); +#ifdef HOST_OHOS + if (!strncmp(commands.c_str(), CMDSTR_SERVICE_KILL.c_str(), CMDSTR_SERVICE_KILL.size())) { + //server need restart + string &cmd = commands; + if (cmd.find("-r") != std::string::npos) { + HdcServer::PullupServer(serverListenString.c_str()); + uv_sleep(START_SERVER_FOR_CLIENT_TIME); + } + } +#endif return 0; } diff --git a/src/host/server.cpp b/src/host/server.cpp index 499944d521ccf40bb9d171ee1f9d447756742d96..a72692c50ac7e6898835c5c835fc43e9dfa1bfa7 100644 --- a/src/host/server.cpp +++ b/src/host/server.cpp @@ -1106,4 +1106,29 @@ void HdcServer::PrintCmdLogEx(const string& cmdStr) Hdc::ServerCmdLog::GetInstance().PushCmdLogStr(cmdStr); } +#ifdef HOST_OHOS +void HdcServer::SessionSoftReset() +{ + uv_rwlock_rdlock(&daemonAdmin); + map::iterator iter; + for (iter = mapDaemon.begin(); iter != mapDaemon.end(); ++iter) { + HDaemonInfo di = iter->second; + if (di == nullptr) { + continue; + } + string devname = di->devName; + if (devname.empty()) { + continue; + } + if (di->connType == CONN_USB) { + HSession hSession = di->hSession; + if (hSession == nullptr) { + continue; + } + clsUSBClt->SendSoftResetToDaemon(hSession, 0); + } + } + uv_rwlock_rdunlock(&daemonAdmin); +} +#endif } // namespace Hdc diff --git a/src/host/server.h b/src/host/server.h index 13d77e34c960ea92b630ea6886416ef60ef5ba30..289321b99d49c1590dd78c14a43fc929d3ffa37b 100644 --- a/src/host/server.h +++ b/src/host/server.h @@ -35,6 +35,9 @@ public: static bool PullupServer(const char *listenString); static void UsbPreConnect(uv_timer_t *handle); void NotifyInstanceSessionFree(HSession hSession, bool freeOrClear) override; +#ifdef HOST_OHOS + void SessionSoftReset(); +#endif HdcHostTCP *clsTCPClt; HdcHostUSB *clsUSBClt; diff --git a/src/host/server_for_client.cpp b/src/host/server_for_client.cpp index dd6167c3efc2f1deb263705e79f3870176dd5ffc..bf5f312d633a58f1b05c0dbfbb6a12bed677526b 100644 --- a/src/host/server_for_client.cpp +++ b/src/host/server_for_client.cpp @@ -83,6 +83,9 @@ void HdcServerForClient::AcceptClient(uv_stream_t *server, int status) struct ChannelHandShake handShake = {}; if (EOK == strcpy_s(handShake.banner, sizeof(handShake.banner), HANDSHAKE_MESSAGE.c_str())) { handShake.banner[BANNER_FEATURE_TAG_OFFSET] = HUGE_BUF_TAG; // set feature tag for huge buf size +#ifdef HOST_OHOS + handShake.banner[SERVICE_KILL_OFFSET] = SERVICE_KILL_TAG; +#endif handShake.channelId = htonl(hChannel->channelId); string ver = Base::GetVersion() + HDC_MSG_HASH; WRITE_LOG(LOG_DEBUG, "Server ver:%s", ver.c_str()); @@ -512,6 +515,15 @@ bool HdcServerForClient::DoCommandLocal(HChannel hChannel, void *formatCommandIn ret = false; break; } +#ifdef HOST_OHOS + case CMD_SERVER_KILL: { + WRITE_LOG(LOG_FATAL, "CMD_SERVER_KILL command"); + ptrServer->SessionSoftReset(); + hChannel->isSuccess = true; + EchoClient(hChannel, MSG_OK, "Kill server finish"); + _exit(0); + } +#endif case CMD_CHECK_SERVER: { WRITE_LOG(LOG_DEBUG, "CMD_CHECK_SERVER command"); ReportServerVersion(hChannel); @@ -729,6 +741,9 @@ bool HdcServerForClient::DoCommand(HChannel hChannel, void *formatCommandInput, TranslateCommand::FormatCommand *formatCommand = (TranslateCommand::FormatCommand *)formatCommandInput; if (!hChannel->hChildWorkTCP.loop || formatCommand->cmdFlag == CMD_FORWARD_REMOVE || +#ifdef HOST_OHOS + formatCommand->cmdFlag == CMD_SERVER_KILL || +#endif formatCommand->cmdFlag == CMD_SERVICE_START) { hChannel->commandFlag = formatCommand->cmdFlag; hChannel->commandParameters = formatCommand->parameters; diff --git a/src/host/translate.cpp b/src/host/translate.cpp index db2cd0d0680d632953528ba63ddaebd2b1323217..33d07e1635371cb4eb07ff1573c53c24b2406437 100644 --- a/src/host/translate.cpp +++ b/src/host/translate.cpp @@ -368,7 +368,13 @@ namespace TranslateCommand { } } else if (!strncmp(input.c_str(), CMDSTR_SERVICE_START.c_str(), CMDSTR_SERVICE_START.size())) { outCmd->cmdFlag = CMD_SERVICE_START; - } else if (!strncmp(input.c_str(), CMDSTR_CHECK_SERVER.c_str(), CMDSTR_CHECK_SERVER.size())) { + } +#ifdef HOST_OHOS + else if (!strncmp(input.c_str(), CMDSTR_SERVICE_KILL.c_str(), CMDSTR_SERVICE_KILL.size())) { + outCmd->cmdFlag = CMD_SERVER_KILL; + } +#endif + else if (!strncmp(input.c_str(), CMDSTR_CHECK_SERVER.c_str(), CMDSTR_CHECK_SERVER.size())) { outCmd->cmdFlag = CMD_CHECK_SERVER; } else if (!strncmp(input.c_str(), CMDSTR_CHECK_DEVICE.c_str(), CMDSTR_CHECK_DEVICE.size())) { outCmd->parameters = input.c_str() + CMDSTR_CHECK_DEVICE.size() + 1; // with ' '