!1627 feat: add error message for pull-up process of hdc server.

Merge pull request !1627 from liurantao/master
This commit is contained in:
openharmony_ci 2024-11-11 07:17:05 +00:00 committed by Gitee
commit 28755363ff
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
11 changed files with 125 additions and 10 deletions

View File

@ -246,7 +246,7 @@ bool ReadKey(const char *file, list<void *> *listPrivateKey)
return ret;
}
int GetUserKeyPath(string &path)
bool GetUserKeyPath(string &path)
{
struct stat status;
const char harmoneyPath[] = ".harmony";

View File

@ -82,6 +82,7 @@ constexpr uint16_t FORWORD_PORT_OTHER_BUF_SIZE = 6;
constexpr uint16_t CMD_REMOTE_SIZE = 8;
constexpr uint16_t PORT_MAX_LEN = 5;
constexpr uint16_t START_SERVER_FOR_CLIENT_TIME = 300;
constexpr uint16_t START_CMD_WAIT_TIME = 3000; // 3 seconds, for client wait for server start
constexpr uint16_t CMD_FILE_PENULT_PARAM = 2;
#ifdef HDC_HOST
constexpr uint16_t MAX_DELETED_SESSION_ID_RECORD_COUNT = 32;

View File

@ -156,6 +156,7 @@ enum HdcCommand {
CMD_CHECK_DEVICE,
CMD_WAIT_FOR,
CMD_SERVICE_KILL,
CMD_SERVICE_START,
// One-pass simple commands
CMD_UNITY_COMMAND_HEAD = 1000, // not use
CMD_UNITY_EXECUTE,

View File

@ -101,6 +101,56 @@ bool HdcClient::StartServer(const string &cmd)
return true;
}
void HdcClient::ChannelCtrlServerStart(const char *listenString)
{
Base::PrintMessage("hdc start server, listening: %s", channelHostPort.c_str());
HdcServer::PullupServer(channelHostPort.c_str());
uv_sleep(START_CMD_WAIT_TIME);
ExecuteCommand(CMDSTR_SERVICE_START.c_str());
}
bool HdcClient::ChannelCtrlServer(const string &cmd, string &connectKey)
{
// new version build channle to send Ctrl command to server
int serverStatus = Base::ProgramMutex(SERVER_NAME.c_str(), true);
if (serverStatus < 0) {
WRITE_LOG(LOG_DEBUG, "get server status failed, serverStatus:%d", serverStatus);
return false;
}
bool isRestart = (cmd.find(" -r") != std::string::npos);
bool isKill = !strncmp(cmd.c_str(), CMDSTR_SERVICE_KILL.c_str(), CMDSTR_SERVICE_KILL.size());
bool isStart = !strncmp(cmd.c_str(), CMDSTR_SERVICE_START.c_str(), CMDSTR_SERVICE_START.size());
Initial(connectKey);
// server is not running, "hdc start [-r]" and "hdc kill -r" will start server directly.
if (serverStatus == 0) {
if ((isRestart && isKill) || isStart) {
ChannelCtrlServerStart(channelHostPort.c_str());
} else if (!isRestart && isKill) { // "hdc kill" will print message and exit directly.
Base::PrintMessage("hdc server process not exists");
}
return true;
}
// server is running
if (isStart) {
if (isRestart) { // "hdc start -r": kill and restart server.
ExecuteCommand(CMDSTR_SERVICE_KILL.c_str());
MallocChannel(&channel);
ChannelCtrlServerStart(channelHostPort.c_str());
} else { // "hdc start": ignore and print message.
Base::PrintMessage("hdc server process already exists");
}
return true;
} else if (isKill) { // "hdc kill": kill server.
ExecuteCommand(CMDSTR_SERVICE_KILL.c_str());
if (isRestart) { // "-r": restart server.
MallocChannel(&channel);
ChannelCtrlServerStart(channelHostPort.c_str());
}
return true;
}
return true;
}
bool HdcClient::KillServer(const string &cmd)
{
int serverStatus = Base::ProgramMutex(SERVER_NAME.c_str(), true);
@ -133,7 +183,12 @@ bool HdcClient::KillServer(const string &cmd)
Base::PrintMessage("Kill server failed %s", buf);
}
if (cmd.find(" -r") != std::string::npos) {
string connectKey;
Base::PrintMessage("hdc start server, listening: %s", channelHostPort.c_str());
HdcServer::PullupServer(channelHostPort.c_str());
uv_sleep(START_CMD_WAIT_TIME);
Initial(connectKey);
ExecuteCommand(CMDSTR_SERVICE_START.c_str());
}
return true;
}
@ -433,7 +488,7 @@ void HdcClient::CommandWorker(uv_timer_t *handle)
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());
thisClass->CtrlServiceWork(thisClass->command.c_str());
return;
}

View File

@ -30,6 +30,7 @@ public:
int Initial(const string &connectKeyIn);
int ExecuteCommand(const string &commandIn);
int CtrlServiceWork(const char *commandIn);
bool ChannelCtrlServer(const string &cmd, string &connectKey);
protected:
private:
@ -46,6 +47,7 @@ private:
uint32_t GetLastPID();
bool StartServer(const string &cmd);
bool KillServer(const string &cmd);
void ChannelCtrlServerStart(const char *listenString);
void BindLocalStd();
void BindLocalStd(HChannel hChannel);
void ModifyTty(bool setOrRestore, uv_tty_t *tty);

View File

@ -201,18 +201,17 @@ int RunClientMode(string &commands, string &serverListenString, string &connectK
std::cerr << TranslateCommand::Usage();
return 0;
}
if (!strncmp(commands.c_str(), CMDSTR_SERVICE_START.c_str(), CMDSTR_SERVICE_START.size()) ||
!strncmp(commands.c_str(), CMDSTR_GENERATE_KEY.c_str(), CMDSTR_GENERATE_KEY.size())) {
if (!strncmp(commands.c_str(), CMDSTR_GENERATE_KEY.c_str(), CMDSTR_GENERATE_KEY.size())) {
client.CtrlServiceWork(commands.c_str());
return 0;
}
if (!strncmp(commands.c_str(), CMDSTR_SERVICE_KILL.c_str(), CMDSTR_SERVICE_KILL.size()) ||
!strncmp(commands.c_str(), CMDSTR_SERVICE_START.c_str(), CMDSTR_SERVICE_START.size())) {
client.ChannelCtrlServer(commands, connectKey);
return 0;
}
if (isPullServer && Base::ProgramMutex(SERVER_NAME.c_str(), true) == 0) {
// default pullup, just default listenstr.If want to customer listen-string, please use 'hdc -m -s lanip:port'
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;
}
HdcServer::PullupServer(serverListenString.c_str());
uv_sleep(START_SERVER_FOR_CLIENT_TIME); // give time to start serverForClient,at least 200ms
}

View File

@ -26,6 +26,7 @@ HdcServer::HdcServer(bool serverOrDaemonIn)
clsUARTClt = nullptr;
#endif
clsServerForClient = nullptr;
lastErrorNum = 0;
uv_rwlock_init(&daemonAdmin);
uv_rwlock_init(&forwardAdmin);
}
@ -43,13 +44,16 @@ void HdcServer::ClearInstanceResource()
Base::TryCloseLoop(&loopMain, "HdcServer::~HdcServer");
if (clsTCPClt) {
delete clsTCPClt;
clsTCPClt = nullptr;
}
if (clsUSBClt) {
delete clsUSBClt;
clsUSBClt = nullptr;
}
#ifdef HDC_SUPPORT_UART
if (clsUARTClt) {
delete clsUARTClt;
clsUARTClt = nullptr;
}
#endif
if (clsServerForClient) {
@ -456,6 +460,7 @@ bool HdcServer::HandServerAuth(HSession hSession, SessionHandShake &handshake)
GetDaemonAuthType(hSession, handshake);
if (!HdcAuth::GetPublicKeyinfo(handshake.buf)) {
WRITE_LOG(LOG_FATAL, "load public key failed");
lastErrorNum = 0x000005; // E000005: load public key failed
return false;
}
handshake.authType = AUTH_PUBLICKEY;
@ -1071,6 +1076,8 @@ void HdcServer::SessionSoftReset()
string devname = di->devName;
if (devname.empty()) {
continue;
} else if ((di->connStatus != STATUS_CONNECTED)) {
continue;
}
if (di->connType == CONN_USB) {
HSession hSession = di->hSession;

View File

@ -45,6 +45,7 @@ public:
HdcHostUART *clsUARTClt = nullptr;
#endif
void *clsServerForClient;
std::atomic<uint32_t> lastErrorNum;
private:
void ClearInstanceResource() override;

View File

@ -486,6 +486,12 @@ bool HdcServerForClient::DoCommandLocal(HChannel hChannel, void *formatCommandIn
ret = false;
break;
}
case CMD_SERVICE_START: {
PrintLastError(hChannel);
EchoClient(hChannel, MSG_OK, "Start server finish");
ret = false;
break;
}
case CMD_SERVICE_KILL: {
ptrServer->SessionSoftReset();
WRITE_LOG(LOG_DEBUG, "server kill");
@ -692,7 +698,9 @@ bool HdcServerForClient::DoCommand(HChannel hChannel, void *formatCommandInput)
{
bool ret = false;
TranslateCommand::FormatCommand *formatCommand = (TranslateCommand::FormatCommand *)formatCommandInput;
if (!hChannel->hChildWorkTCP.loop || formatCommand->cmdFlag == CMD_FORWARD_REMOVE) {
if (!hChannel->hChildWorkTCP.loop ||
formatCommand->cmdFlag == CMD_FORWARD_REMOVE ||
formatCommand->cmdFlag == CMD_SERVICE_START) {
// Main thread command, direct Listen main thread
ret = DoCommandLocal(hChannel, formatCommandInput);
} else { // CONNECT DAEMON's work thread command, non-primary thread
@ -917,4 +925,24 @@ bool HdcServerForClient::ChannelSendSessionCtrlMsg(vector<uint8_t> &ctrlMsg, uin
}
return rc > 0;
}
void HdcServerForClient::PrintLastError(HChannel HChannel)
{
HdcServer *ptrServer = (HdcServer *)clsServer;
uint32_t errorCode = ptrServer->lastErrorNum;
if (errorCode > 0) {
string errorString = GetErrorString(errorCode);
EchoClient(HChannel, MSG_FAIL, "[E%06x]%s", errorCode, errorString.c_str());
ptrServer->lastErrorNum = 0;
}
}
string HdcServerForClient::GetErrorString(uint32_t errorCode)
{
auto map = ErrorStringEnglish.find(errorCode);
if (map != ErrorStringEnglish.end()) {
return map->second;
}
return ErrorStringEnglish.at(0xFFFFFF); // 0xFFFFFF: Unknown error
}
} // namespace Hdc

View File

@ -58,9 +58,28 @@ private:
bool ChannelSendSessionCtrlMsg(vector<uint8_t> &ctrlMsg, uint32_t sessionId) override;
HSession FindAliveSession(uint32_t sessionId);
HSession FindAliveSessionFromDaemonMap(const HChannel hChannel);
string GetErrorString(uint32_t errorCode);
void PrintLastError(HChannel HChannel);
uv_tcp_t tcpListen;
void *clsServer;
const std::unordered_map<uint32_t, std::string> ErrorStringEnglish = {
#ifdef _WIN32
{0x000005, "Failed to load the authorization pub key!\r\n"
"Please check the public key directory:[%USERPROFILE%\\.harmony]\r\n"
"Alternatively, delete the public key directory and re-run the command.\r\n"
"Delete command: \"del %USERPROFILE%\\.harmony\""},
#else
{0x000005, "Failed to load the authorization pub key!\r\n"
"Please check the public key directory:[~/.harmony]\r\n"
"Alternatively, delete the public key directory and re-run the command.\r\n"
"Delete command: \"rm -r ~/.harmony\""},
#endif
{0x002103, "Failed to start the HDC server process!\r\n"
"Please check the HDC server process port is occupied or used as an exception port.\r\n"
"Alternatively, change the OHOS_HDC_SERVER_PORT environment variable and re-run the command."},
{0xFFFFFF, "Unknown error"},
};
};
} // namespace Hdc
#endif

View File

@ -330,6 +330,8 @@ namespace TranslateCommand {
}
} else if (!strncmp(input.c_str(), CMDSTR_SERVICE_KILL.c_str(), CMDSTR_SERVICE_KILL.size())) {
outCmd->cmdFlag = CMD_SERVICE_KILL;
} 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())) {
outCmd->cmdFlag = CMD_CHECK_SERVER;
} else if (!strncmp(input.c_str(), CMDSTR_CHECK_DEVICE.c_str(), CMDSTR_CHECK_DEVICE.size())) {