• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include <iostream>
17 #include "ext_client.h"
18 #include "forward.h"
19 #include "server.h"
20 #include "server_for_client.h"
21 
22 #ifdef _WIN32
23 #include <windows.h>
24 #endif
25 #ifdef __OHOS__
26 #include <sys/xattr.h>
27 #endif
28 
29 #ifndef HARMONY_PROJECT
30 #include "ut_command.h"
31 using namespace HdcTest;
32 #endif
33 
34 using namespace Hdc;
35 
36 namespace {
37     bool g_isServerMode = false;
38     bool g_isPullServer = true;
39     bool g_isPcDebugRun = false;
40     bool g_isTCPorUSB = false;
41     bool g_isCustomLoglevel = false;
42     bool g_externalCmd = false;
43     int g_isTestMethod = 0;
44     string g_connectKey = "";
45     string g_serverListenString = "";
46     string g_containerInOut = "";
47 }
48 
49 namespace Hdc {
50 // return value: 0 == not command, 1 == one command, 2 == double command
IsRegisterCommand(string & outCommand,const char * cmd,const char * cmdnext)51 int IsRegisterCommand(string &outCommand, const char *cmd, const char *cmdnext)
52 {
53     string sCmdNext = cmdnext == nullptr ? string("") : string(cmdnext);
54     string doubleCommand = cmd + string(" ") + sCmdNext;
55     vector<string> registerCommand;
56     registerCommand.push_back(CMDSTR_SOFTWARE_VERSION);
57     registerCommand.push_back(CMDSTR_SOFTWARE_HELP);
58     registerCommand.push_back(CMDSTR_TARGET_DISCOVER);
59     registerCommand.push_back(CMDSTR_LIST_TARGETS);
60     registerCommand.push_back(CMDSTR_CHECK_SERVER);
61     registerCommand.push_back(CMDSTR_CHECK_DEVICE);
62     registerCommand.push_back(CMDSTR_WAIT_FOR);
63     registerCommand.push_back(CMDSTR_CONNECT_ANY);
64     registerCommand.push_back(CMDSTR_CONNECT_TARGET);
65     registerCommand.push_back(CMDSTR_SHELL);
66     registerCommand.push_back(CMDSTR_SHELL_EX);
67     registerCommand.push_back(CMDSTR_FILE_SEND);
68     registerCommand.push_back(CMDSTR_FILE_RECV);
69     registerCommand.push_back(CMDSTR_FORWARD_FPORT);
70     registerCommand.push_back(CMDSTR_FORWARD_RPORT);
71     registerCommand.push_back(CMDSTR_SERVICE_KILL);
72     registerCommand.push_back(CMDSTR_SERVICE_START);
73     registerCommand.push_back(CMDSTR_GENERATE_KEY);
74     registerCommand.push_back(CMDSTR_APP_INSTALL);
75     registerCommand.push_back(CMDSTR_APP_UNINSTALL);
76     registerCommand.push_back(CMDSTR_TARGET_MOUNT);
77     registerCommand.push_back(CMDSTR_HILOG);
78     registerCommand.push_back(CMDSTR_STARTUP_MODE);
79     registerCommand.push_back(CMDSTR_BUGREPORT);
80     registerCommand.push_back(CMDSTR_TARGET_MODE);
81     registerCommand.push_back(CMDSTR_APP_SIDELOAD);
82     registerCommand.push_back(CMDSTR_TARGET_REBOOT);
83     registerCommand.push_back(CMDSTR_LIST_JDWP);
84     registerCommand.push_back(CMDSTR_TRACK_JDWP);
85     registerCommand.push_back(CMDSTR_FLASHD_UPDATE);
86     registerCommand.push_back(CMDSTR_FLASHD_FLASH);
87     registerCommand.push_back(CMDSTR_FLASHD_ERASE);
88     registerCommand.push_back(CMDSTR_FLASHD_FORMAT);
89 
90     for (string v : registerCommand) {
91         if (doubleCommand == v) {
92             outCommand = doubleCommand;
93             return CMD_ARG1_COUNT;
94         }
95         if (cmd == v || !strncmp(cmd, CMDSTR_WAIT_FOR.c_str(), CMDSTR_WAIT_FOR.size())) {
96             outCommand = cmd;
97             return 1;
98         }
99     }
100     return 0;
101 }
102 
AppendCwdWhenTransfer(string & outCommand)103 void AppendCwdWhenTransfer(string &outCommand)
104 {
105     if (outCommand != CMDSTR_FILE_SEND && outCommand != CMDSTR_FILE_RECV && outCommand != CMDSTR_APP_INSTALL &&
106         outCommand != CMDSTR_APP_SIDELOAD) {
107         return;
108     }
109     int value = -1;
110     char path[PATH_MAX] = "";
111     size_t size = sizeof(path);
112     value = uv_cwd(path, &size);
113     if (value < 0) {
114         constexpr int bufSize = 1024;
115         char buf[bufSize] = { 0 };
116         uv_strerror_r(value, buf, bufSize);
117         WRITE_LOG(LOG_FATAL, "append cwd path failed: %s", buf);
118         return;
119     }
120     if (strlen(path) >= PATH_MAX - 1) {
121         WRITE_LOG(LOG_FATAL, "append cwd path failed: buffer space max");
122         return;
123     }
124     if (path[strlen(path) - 1] != Base::GetPathSep()) {
125         path[strlen(path)] = Base::GetPathSep();
126     }
127     outCommand += outCommand.size() ? " " : "";
128     outCommand += CMDSTR_REMOTE_PARAMETER;
129     outCommand += outCommand.size() ? " -cwd " : "-cwd ";
130     outCommand += Base::StringFormat("\"%s\"", path);
131 }
132 
SplitOptionAndCommand(int argc,const char ** argv,string & outOption,string & outCommand)133 int SplitOptionAndCommand(int argc, const char **argv, string &outOption, string &outCommand)
134 {
135     bool foundCommand = false;
136     int resultChild = 0;
137     // we want to start from 1, ignore argv[0], but it has issue
138     for (int i = 0; i < argc; ++i) {
139         if (!foundCommand) {
140             resultChild = IsRegisterCommand(outCommand, argv[i], (i == argc - 1) ? nullptr : argv[i + 1]);
141             if (resultChild > 0) {
142                 foundCommand = true;
143                 if (resultChild == 2) {
144                     ++i;
145                 }
146                 AppendCwdWhenTransfer(outCommand);
147                 continue;
148             }
149         }
150         if (foundCommand) {
151             outCommand += outCommand.size() ? " " : "";
152             string rawCmd = Base::UnicodeToUtf8(argv[i]);
153             outCommand += rawCmd.find(" ") == string::npos ? rawCmd : Base::StringFormat("\"%s\"", rawCmd.c_str());
154         } else {
155             outOption += outOption.size() ? " " : "";
156             string rawOption = Base::UnicodeToUtf8(argv[i]);
157             outOption += (i == 0) ? Base::StringFormat("\"%s\"", rawOption.c_str()) : rawOption;
158         }
159     }
160     return 0;
161 }
162 
RunServerMode(string & serverListenString)163 int RunServerMode(string &serverListenString)
164 {
165 #ifndef __OHOS__
166     if (serverListenString.empty()) {
167         return -1;
168     }
169 #endif
170     /*
171      * Notice !!!!!!
172      * For hdc server, all setenv must befor Base::RemoveLogFile()
173      * RemoveLogFile will create thread to run ThreadCompressLog and RemoveOlderLogFiles which will
174      * call uv_os_tmpdir and libuv inner wiil call getenv
175      * setenv and getenv concurrent calling wiil cause crash
176      * NOW, for hdc server setenv are SetLibusbLogLevelEnv and HdcServer construct
177     */
178     HdcHostUSB::SetLibusbLogLevelEnv(HdcHostUSB::GetLibusbLogLevel());
179     HdcServer server(true);
180     if (!server.Initial(serverListenString.c_str())) {
181         Base::PrintMessage("Initial failed");
182         return -1;
183     }
184     server.WorkerPendding();
185     return 0;
186 }
187 
RunPcDebugMode(bool isPullServer,bool isTCPorUSB,int isTestMethod)188 int RunPcDebugMode(bool isPullServer, bool isTCPorUSB, int isTestMethod)
189 {
190 #ifdef HARMONY_PROJECT
191     Base::PrintMessage("Not support command...");
192 #else
193     pthread_t pt;
194     if (isPullServer) {
195         pthread_create(&pt, nullptr, TestBackgroundServerForClient, nullptr);
196         uv_sleep(200);  // give time to start serverForClient,at least 200ms
197     }
198     TestRuntimeCommandSimple(isTCPorUSB, isTestMethod, true);
199     if (isPullServer) {
200         pthread_join(pt, nullptr);
201         WRITE_LOG(LOG_DEBUG, "!!!!!!!!!Server finish");
202     }
203 #endif
204     return 0;
205 }
206 
RunClientMode(string & commands,string & serverListenString,string & connectKey,bool isPullServer)207 int RunClientMode(string &commands, string &serverListenString, string &connectKey, bool isPullServer)
208 {
209     if (serverListenString.empty()) {
210         return -1;
211     }
212     uv_loop_t loopMain;
213     uv_loop_init(&loopMain);
214     HdcClient client(false, serverListenString, &loopMain, commands == CMDSTR_CHECK_SERVER);
215     if (!commands.size()) {
216         Base::PrintMessage("Unknown operation command...");
217         std::cerr << TranslateCommand::Usage();
218         return 0;
219     }
220 #ifdef HOST_OHOS
221     if (!strncmp(commands.c_str(), CMDSTR_GENERATE_KEY.c_str(), CMDSTR_GENERATE_KEY.size())) {
222 #else
223     if (!strncmp(commands.c_str(), CMDSTR_GENERATE_KEY.c_str(), CMDSTR_GENERATE_KEY.size()) ||
224         !strncmp(commands.c_str(), CMDSTR_SERVICE_KILL.c_str(), CMDSTR_SERVICE_KILL.size())) {
225 #endif
226         client.CtrlServiceWork(commands.c_str());
227         return 0;
228     }
229     if (!strncmp(commands.c_str(), CMDSTR_SERVICE_START.c_str(), CMDSTR_SERVICE_START.size())) {
230         client.ChannelCtrlServer(commands, connectKey);
231         return 0;
232     }
233     if (isPullServer && Base::ProgramMutex(SERVER_NAME.c_str(), true) == 0) {
234 #ifdef HOST_OHOS
235         if (!strncmp(commands.c_str(), CMDSTR_SERVICE_KILL.c_str(),
236             CMDSTR_SERVICE_KILL.size())) {
237             WRITE_LOG(LOG_DEBUG, "kill server, but server not exist, so do nothing");
238             return 0;
239         }
240 #endif
241         // default pullup, just default listenstr.If want to customer listen-string, please use 'hdc -m -s lanip:port'
242         HdcServer::PullupServer(serverListenString.c_str());
243         uv_sleep(START_SERVER_FOR_CLIENT_TIME);  // give time to start serverForClient,at least 200ms
244     }
245     client.Initial(connectKey);
246     client.ExecuteCommand(commands.c_str());
247 #ifdef HOST_OHOS
248     if (!strncmp(commands.c_str(), CMDSTR_SERVICE_KILL.c_str(), CMDSTR_SERVICE_KILL.size())) {
249         //server need restart
250         string &cmd = commands;
251         if (cmd.find("-r") != std::string::npos) {
252             HdcServer::PullupServer(serverListenString.c_str());
253             uv_sleep(START_SERVER_FOR_CLIENT_TIME);
254         }
255     }
256 #endif
257     return 0;
258 }
259 
260 #ifdef __OHOS__
261 bool IsHiShellLabel()
262 {
263     pid_t pid = getpid();
264     char pathBuf[BUF_SIZE_DEFAULT] = "";
265     if (snprintf_s(pathBuf, sizeof(pathBuf), sizeof(pathBuf) - 1, "/proc/%d/attr/current", pid) < 0) {
266         WRITE_LOG(LOG_FATAL, "get pathBuf failed, pid is %d", pid);
267         return false;
268     }
269 
270     const char* attrName = "security.selinux";
271     // get attribute size
272     ssize_t attrSize = getxattr(pathBuf, attrName, nullptr, 0);
273     if (attrSize == 0 || attrSize == - 1) {
274         return false;
275     }
276     char* attrValue = new(std::nothrow) char[attrSize];
277     if (attrValue == nullptr) {
278         return false;
279     }
280     // get attribute value
281     if (getxattr(pathBuf, attrName, attrValue, attrSize) == -1) {
282         delete []attrValue;
283         return false;
284     }
285     string label(attrValue, attrSize - 1);
286     delete []attrValue;
287     return label == "u:r:hishell_hap:s0";
288 }
289 #endif
290 
291 bool ParseServerListenString(string &serverListenString, char *optarg)
292 {
293 #ifdef __OHOS__
294     string temp = optarg;
295     if (temp == UDS_STR) {
296         serverListenString = temp;
297         return true;
298     }
299     if (!IsHiShellLabel()) {
300         Base::PrintMessage("[E001105] Unsupport option [s], please try command in HiShell.");
301         return false;
302     }
303 #endif
304     if (strlen(optarg) > strlen("0000::0000:0000:0000:0000%interfacename:65535")) {
305         Base::PrintMessage("Unknown content of parament '-s'");
306         return false;
307     }
308     char buf[BUF_SIZE_TINY] = "";
309     if (strcpy_s(buf, sizeof(buf), optarg) != 0) {
310         Base::PrintMessage("strcpy_s error %d", errno);
311         return false;
312     }
313     char *p = strrchr(buf, ':');
314     if (!p) {  // Only port
315         if (strlen(buf) > PORT_MAX_LEN) {
316             Base::PrintMessage("The port-string's length must < 5");
317             return false;
318         }
319         size_t len = strlen(buf);
320         for (size_t i = 0; i < len; i++) {
321             if (isdigit(buf[i]) == 0) {
322                 Base::PrintMessage("The port must be digit buf:%s", buf);
323                 return false;
324             }
325         }
326         int port = atoi(buf);
327         if (port <= 0 || port > MAX_IP_PORT) {
328             Base::PrintMessage("Port range incorrect");
329             return false;
330         }
331         (void)snprintf_s(buf, sizeof(buf), sizeof(buf) - 1, "::ffff:127.0.0.1:%d", port);
332         serverListenString = buf;
333     } else {
334         *p = '\0';
335         char *str = p + 1;
336         size_t len = strlen(str);
337         for (size_t i = 0; i < len; i++) {
338             if (isdigit(str[i]) == 0) {
339                 Base::PrintMessage("The port must be digit str:%s", str);
340                 return false;
341             }
342         }
343         int port = atoi(p + 1);
344         sockaddr_in addrv4;
345         sockaddr_in6 addrv6;
346 
347         if ((port <= 0 || port > MAX_IP_PORT)) {
348             Base::PrintMessage("-s content port incorrect.");
349             return false;
350         }
351 
352         if (uv_ip4_addr(buf, port, &addrv4) == 0) {
353             serverListenString = IPV4_MAPPING_PREFIX;
354             serverListenString += optarg;
355         } else if (uv_ip6_addr(buf, port, &addrv6) == 0) {
356             serverListenString = optarg;
357         } else {
358             Base::PrintMessage("-s content IP incorrect.");
359             return false;
360         }
361     }
362     return true;
363 }
364 
365 bool ParseForwardListenIP(char *optarg)
366 {
367     if (optarg == nullptr) {
368         return false;
369     }
370     sockaddr_in addrv4;
371     sockaddr_in6 addrv6;
372     string forwardListenIP;
373 
374     const int testPort = 8888;
375     if (uv_ip4_addr(optarg, testPort, &addrv4) == 0) {
376         forwardListenIP = IPV4_MAPPING_PREFIX;
377         forwardListenIP += optarg;
378         HdcForwardBase::SetForwardListenIP(forwardListenIP);
379     } else if (uv_ip6_addr(optarg, testPort, &addrv6) == 0) {
380         forwardListenIP = optarg;
381         HdcForwardBase::SetForwardListenIP(forwardListenIP);
382     } else {
383         Base::PrintMessage("[E001106]-e content IP incorrect.");
384         return false;
385     }
386     return true;
387 }
388 
389 bool GetCommandlineOptions(int optArgc, const char *optArgv[])
390 {
391     int ch = 0;
392     bool needExit = false;
393     opterr = 0;
394     // get option parameters first
395     while ((ch = getopt(optArgc, const_cast<char *const*>(optArgv), "hvpfmncs:Sd:e:t:l:")) != -1) {
396         switch (ch) {
397             case 'h': {
398                 string usage = Hdc::TranslateCommand::Usage();
399                 if (optind < optArgc && optind >= 0 && string(optArgv[optind]) == "verbose") {
400                     usage = Hdc::TranslateCommand::Verbose();
401                 }
402                 fprintf(stderr, "%s", usage.c_str());
403                 needExit = true;
404                 return needExit;
405             }
406             case 'v': {
407                 string ver = Base::GetVersion();
408                 fprintf(stdout, "%s\n", ver.c_str());
409                 needExit = true;
410                 return needExit;
411             }
412             case 'e': {
413                 if (!ParseForwardListenIP(optarg)) {
414                     needExit = true;
415                     return needExit;
416                 }
417                 break;
418             }
419             case 'f': {  // [not-publish]
420                 break;
421             }
422             case 'l': {
423                 int logLevel = atoi(optarg);
424                 if (logLevel < 0 || logLevel > LOG_LAST) {
425                     Base::PrintMessage("Loglevel error!");
426                     needExit = true;
427                     return needExit;
428                 }
429                 g_isCustomLoglevel = true;
430                 Base::SetLogLevel(logLevel);
431                 break;
432             }
433             case 'm': {  // [not-publish] is server mode,or client mode
434                 g_isServerMode = true;
435                 break;
436             }
437             case 'n': {
438                 g_containerInOut = "-n";
439                 break;
440             }
441             case 'c': {
442                 g_containerInOut = "-c";
443                 break;
444             }
445             case 'p': {  // [not-publish]  not pullup server
446                 g_isPullServer = false;
447                 break;
448             }
449             case 't': {  // key
450                 if (strlen(optarg) > MAX_CONNECTKEY_SIZE) {
451                     Base::PrintMessage("Sizeo of of parament '-t' %d is too long", strlen(optarg));
452                     needExit = true;
453                     return needExit;
454                 }
455                 g_connectKey = optarg;
456                 break;
457             }
458             case 's': {
459                 if (!Hdc::ParseServerListenString(g_serverListenString, optarg)) {
460                     needExit = true;
461                     return needExit;
462                 }
463                 break;
464             }
465             case 'S': {
466                 g_externalCmd = true;
467                 break;
468             }
469             case 'd':  // [Undisclosed parameters] debug mode
470                 g_isPcDebugRun = true;
471                 if (optarg[0] == 't') {
472                     g_isTCPorUSB = true;
473                 } else if (optarg[0] == 'u') {
474                     g_isTCPorUSB = false;
475                 } else {
476                     Base::PrintMessage("Unknown debug parameters");
477                     needExit = true;
478                     return needExit;
479                 }
480                 g_isTestMethod = atoi(optarg + 1);
481                 break;
482             case '?':
483                 break;
484             default: {
485                 Base::PrintMessage("Unknown parameters");
486                 needExit = true;
487                 return needExit;
488             }
489         }
490     }
491     return needExit;
492 }
493 
494 void InitServerAddr(void)
495 {
496     int port;
497     do {
498         char *env = getenv(ENV_SERVER_PORT.c_str());
499         if (!env) {
500             port = DEFAULT_PORT;
501             break;
502         }
503 
504         size_t len = strlen(env);
505         size_t maxLen = 5;
506         if (len > maxLen) {
507             fprintf(stderr, "OHOS_HDC_SERVER_PORT %s is not in (0, 65535] range\n", env);
508             return;
509         }
510 
511         for (size_t i = 0; i < len; i++) {
512             if (isdigit(env[i]) == 0) {
513                 fprintf(stderr, "OHOS_HDC_SERVER_PORT %s is not digit\n", env);
514                 return;
515             }
516         }
517 
518         port = atoi(env);
519         if (port > MAX_IP_PORT || port <= 0) {
520             fprintf(stderr, "OHOS_HDC_SERVER_PORT %s is not in (0, 65535] range\n", env);
521             return;
522         }
523     } while (0);
524     g_serverListenString = DEFAULT_SERVER_ADDR_IP;
525     g_serverListenString += ":";
526     g_serverListenString += std::to_string(port);
527 }
528 
529 void RunExternalClient(string &str, string &connectKey, string &containerInOut)
530 {
531     ExtClient extClient;
532     extClient.connectKey = connectKey;
533     extClient.containerInOut = containerInOut;
534     extClient.Init();
535     extClient.ExecuteCommand(str);
536 }
537 }
538 
539 #ifndef UNIT_TEST
540 
541 // hdc -l4 -m -s ip:port|hdc -l4 -m
542 // hdc -l4 - s ip:port list targets
main(int argc,const char * argv[])543 int main(int argc, const char *argv[])
544 {
545     Base::UpdateEnvCache();
546 #ifdef _WIN32
547     SetConsoleOutputCP(CP_UTF8);
548 #endif
549     string options;
550     string commands;
551     Hdc::SplitOptionAndCommand(argc, argv, options, commands);
552     uv_setup_args(argc, const_cast<char **>(argv));
553     int optArgc = 0;
554     char **optArgv = Base::SplitCommandToArgs(options.c_str(), &optArgc);
555     bool cmdOptionResult;
556 #ifndef __OHOS__
557     InitServerAddr();
558 #endif
559     cmdOptionResult = GetCommandlineOptions(optArgc, const_cast<const char **>(optArgv));
560     delete[](reinterpret_cast<char*>(optArgv));
561     if (cmdOptionResult) {
562         return 0;
563     }
564     Base::InitProcess();
565     if (g_isServerMode) {
566 #ifdef FEATURE_HOST_LOG_COMPRESS
567         Base::CreateLogDir();
568 #endif
569         // -m server.Run alone in the background, no -s will be listen loopback address
570         Hdc::RunServerMode(g_serverListenString);
571     } else if (g_isPcDebugRun) {
572         Hdc::RunPcDebugMode(g_isPullServer, g_isTCPorUSB, g_isTestMethod);
573     } else {
574 #ifdef __OHOS__
575         if (g_serverListenString.empty()) {
576             g_serverListenString = UDS_STR;
577         }
578 #endif
579         if (!g_isCustomLoglevel) {
580             Base::SetLogLevel(LOG_INFO);
581         }
582 
583         if (!ExtClient::SharedLibraryExist()) {
584             Hdc::RunClientMode(commands, g_serverListenString, g_connectKey, g_isPullServer);
585             Hdc::Base::RemoveLogCache();
586             _exit(0);
587         }
588         string str = "list targets";
589         if (!strncmp(commands.c_str(), CMDSTR_LIST_TARGETS.c_str(), CMDSTR_LIST_TARGETS.size())) {
590             string lista = "list targets -a";
591             if (!strncmp(commands.c_str(), lista.c_str(), lista.size())) {
592                 str = "list targets -v";
593             } else {
594                 str = commands;
595             }
596             Hdc::RunExternalClient(str, g_connectKey, g_containerInOut);
597             Hdc::RunClientMode(str, g_serverListenString, g_connectKey, g_isPullServer);
598         } else if (!strncmp(commands.c_str(), CMDSTR_SOFTWARE_VERSION.c_str(), CMDSTR_SOFTWARE_VERSION.size()) ||
599                    !strncmp(commands.c_str(), CMDSTR_SOFTWARE_HELP.c_str(), CMDSTR_SOFTWARE_HELP.size()) ||
600                    !strncmp(commands.c_str(), CMDSTR_TARGET_DISCOVER.c_str(), CMDSTR_TARGET_DISCOVER.size()) ||
601                    !strncmp(commands.c_str(), CMDSTR_SERVICE_START.c_str(), CMDSTR_SERVICE_START.size()) ||
602                    !strncmp(commands.c_str(), CMDSTR_SERVICE_KILL.c_str(), CMDSTR_SERVICE_KILL.size()) ||
603                    !strncmp(commands.c_str(), CMDSTR_WAIT_FOR.c_str(), CMDSTR_WAIT_FOR.size())) {
604             Hdc::RunExternalClient(commands, g_connectKey, g_containerInOut);
605             Hdc::RunClientMode(commands, g_serverListenString, g_connectKey, g_isPullServer);
606         } else if (!strncmp(commands.c_str(), CMDSTR_CONNECT_TARGET.c_str(), CMDSTR_CONNECT_TARGET.size()) ||
607                    !strncmp(commands.c_str(), CMDSTR_TARGET_MODE.c_str(), CMDSTR_TARGET_MODE.size()) || g_externalCmd) {
608             Hdc::RunExternalClient(commands, g_connectKey, g_containerInOut);
609         } else {
610             g_show = false;
611             Hdc::RunExternalClient(str, g_connectKey, g_containerInOut);
612             Hdc::RunClientMode(str, g_serverListenString, g_connectKey, g_isPullServer);
613             g_show = true;
614             if (g_connectKey.empty()) {
615                 if (g_lists.size() == 0) {
616                     Base::PrintMessage("No any target");
617                 } else if (g_lists.size() == 1) {
618                     auto iter = g_lists.begin();
619                     g_connectKey = iter->first;
620                 } else {
621                     Base::PrintMessage("Specify one target");
622                 }
623             }
624             if (g_lists[g_connectKey] == "external") {
625                 Hdc::RunExternalClient(commands, g_connectKey, g_containerInOut);
626             } else if (g_lists[g_connectKey] == "hdc") {
627                 Hdc::RunClientMode(commands, g_serverListenString, g_connectKey, g_isPullServer);
628             }
629         }
630     }
631     WRITE_LOG(LOG_DEBUG, "!!!!!!!!!Main finish main");
632     Hdc::Base::RemoveLogCache();
633     return 0;
634 }
635 #endif  // no UNIT_TEST
636