提交 52e5af66 编写于 作者: C cheng_jinsong

fix control_fd bugs

Signed-off-by: Ncheng_jinsong <chengjinsong2@huawei.com>
上级 6141a59c
...@@ -77,9 +77,10 @@ typedef struct { ...@@ -77,9 +77,10 @@ typedef struct {
char cmd[0]; char cmd[0];
} CmdMessage; } CmdMessage;
void CmdServiceInit(const char *socketPath, CallbackControlFdProcess func); void CmdServiceInit(const char *socketPath, CallbackControlFdProcess func, LoopHandle loop);
void CmdClientInit(const char *socketPath, uint16_t type, const char *cmd); void CmdClientInit(const char *socketPath, uint16_t type, const char *cmd);
void CmdServiceProcessDelClient(pid_t pid); void CmdServiceProcessDelClient(pid_t pid);
void CmdServiceProcessDestroyClient(void);
#ifdef __cplusplus #ifdef __cplusplus
#if __cplusplus #if __cplusplus
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <sys/stat.h>
#include <termios.h> #include <termios.h>
#include <unistd.h> #include <unistd.h>
...@@ -167,6 +168,8 @@ CONTROL_FD_STATIC int InitPtyInterface(CmdAgent *agent, uint16_t type, const cha ...@@ -167,6 +168,8 @@ CONTROL_FD_STATIC int InitPtyInterface(CmdAgent *agent, uint16_t type, const cha
ret = ptsname_r(pfd, ptsbuffer, sizeof(ptsbuffer)); ret = ptsname_r(pfd, ptsbuffer, sizeof(ptsbuffer));
BEGET_ERROR_CHECK(ret >= 0, close(pfd); return -1, "Failed to get pts name err=%d", errno); BEGET_ERROR_CHECK(ret >= 0, close(pfd); return -1, "Failed to get pts name err=%d", errno);
BEGET_LOGI("ptsbuffer is %s", ptsbuffer); BEGET_LOGI("ptsbuffer is %s", ptsbuffer);
BEGET_ERROR_CHECK(chmod(ptsbuffer, S_IRWXU | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) == 0,
close(pfd); return -1, "Failed to chmod %s, err=%d", ptsbuffer, errno);
agent->ptyFd = pfd; agent->ptyFd = pfd;
LE_WatchInfo info = {}; LE_WatchInfo info = {};
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "securec.h" #include "securec.h"
static CmdService g_cmdService; static CmdService g_cmdService;
static LoopHandle g_controlFdLoop = NULL;
CallbackControlFdProcess g_controlFdFunc = NULL; CallbackControlFdProcess g_controlFdFunc = NULL;
...@@ -52,12 +53,12 @@ CONTROL_FD_STATIC void CmdOnRecvMessage(const TaskHandle task, const uint8_t *bu ...@@ -52,12 +53,12 @@ CONTROL_FD_STATIC void CmdOnRecvMessage(const TaskHandle task, const uint8_t *bu
if (agent->pid == 0) { if (agent->pid == 0) {
OpenConsole(); OpenConsole();
char *realPath = GetRealPath(msg->ptyName); char *realPath = GetRealPath(msg->ptyName);
BEGET_ERROR_CHECK(realPath != NULL, return, "Failed get realpath, err=%d", errno); BEGET_ERROR_CHECK(realPath != NULL, _exit(1), "Failed get realpath, err=%d", errno);
char *strl = strstr(realPath, "/dev/pts"); char *strl = strstr(realPath, "/dev/pts");
BEGET_ERROR_CHECK(strl != NULL, return, "pty slave path %s is invaild", realPath); BEGET_ERROR_CHECK(strl != NULL, free(realPath); _exit(1), "pts path %s is invaild", realPath);
int fd = open(realPath, O_RDWR); int fd = open(realPath, O_RDWR);
free(realPath); free(realPath);
BEGET_ERROR_CHECK(fd >= 0, return, "Failed open %s, err=%d", msg->ptyName, errno); BEGET_ERROR_CHECK(fd >= 0, _exit(1), "Failed open %s, err=%d", msg->ptyName, errno);
(void)dup2(fd, STDIN_FILENO); (void)dup2(fd, STDIN_FILENO);
(void)dup2(fd, STDOUT_FILENO); (void)dup2(fd, STDOUT_FILENO);
(void)dup2(fd, STDERR_FILENO); // Redirect fd to 0, 1, 2 (void)dup2(fd, STDERR_FILENO); // Redirect fd to 0, 1, 2
...@@ -65,7 +66,7 @@ CONTROL_FD_STATIC void CmdOnRecvMessage(const TaskHandle task, const uint8_t *bu ...@@ -65,7 +66,7 @@ CONTROL_FD_STATIC void CmdOnRecvMessage(const TaskHandle task, const uint8_t *bu
if (g_controlFdFunc != NULL) { if (g_controlFdFunc != NULL) {
g_controlFdFunc(msg->type, msg->cmd, NULL); g_controlFdFunc(msg->type, msg->cmd, NULL);
} }
exit(0); _exit(0);
} else if (agent->pid < 0) { } else if (agent->pid < 0) {
BEGET_LOGE("[control_fd] Failed fork service"); BEGET_LOGE("[control_fd] Failed fork service");
} }
...@@ -85,7 +86,7 @@ CONTROL_FD_STATIC int SendMessage(LoopHandle loop, TaskHandle task, const char * ...@@ -85,7 +86,7 @@ CONTROL_FD_STATIC int SendMessage(LoopHandle loop, TaskHandle task, const char *
char *buff = (char *)LE_GetBufferInfo(handle, NULL, &bufferSize); char *buff = (char *)LE_GetBufferInfo(handle, NULL, &bufferSize);
BEGET_ERROR_CHECK(buff != NULL, return -1, "[control_fd] Failed get buffer info"); BEGET_ERROR_CHECK(buff != NULL, return -1, "[control_fd] Failed get buffer info");
int ret = memcpy_s(buff, bufferSize, message, strlen(message) + 1); int ret = memcpy_s(buff, bufferSize, message, strlen(message) + 1);
BEGET_ERROR_CHECK(ret == 0, LE_FreeBuffer(LE_GetDefaultLoop(), task, handle); BEGET_ERROR_CHECK(ret == 0, LE_FreeBuffer(g_controlFdLoop, task, handle);
return -1, "[control_fd] Failed memcpy_s err=%d", errno); return -1, "[control_fd] Failed memcpy_s err=%d", errno);
LE_STATUS status = LE_Send(loop, task, handle, strlen(message) + 1); LE_STATUS status = LE_Send(loop, task, handle, strlen(message) + 1);
BEGET_ERROR_CHECK(status == LE_SUCCESS, return -1, "[control_fd] Failed le send msg"); BEGET_ERROR_CHECK(status == LE_SUCCESS, return -1, "[control_fd] Failed le send msg");
...@@ -106,21 +107,21 @@ CONTROL_FD_STATIC int CmdOnIncommingConnect(const LoopHandle loop, const TaskHan ...@@ -106,21 +107,21 @@ CONTROL_FD_STATIC int CmdOnIncommingConnect(const LoopHandle loop, const TaskHan
info.disConnectComplete = NULL; info.disConnectComplete = NULL;
info.sendMessageComplete = NULL; info.sendMessageComplete = NULL;
info.recvMessage = CmdOnRecvMessage; info.recvMessage = CmdOnRecvMessage;
int ret = LE_AcceptStreamClient(LE_GetDefaultLoop(), server, &client, &info); int ret = LE_AcceptStreamClient(g_controlFdLoop, server, &client, &info);
BEGET_ERROR_CHECK(ret == 0, return -1, "[control_fd] Failed accept stream") BEGET_ERROR_CHECK(ret == 0, return -1, "[control_fd] Failed accept stream")
CmdTask *agent = (CmdTask *)LE_GetUserData(client); CmdTask *agent = (CmdTask *)LE_GetUserData(client);
BEGET_ERROR_CHECK(agent != NULL, return -1, "[control_fd] Invalid agent"); BEGET_ERROR_CHECK(agent != NULL, return -1, "[control_fd] Invalid agent");
agent->task = client; agent->task = client;
OH_ListInit(&agent->item); OH_ListInit(&agent->item);
ret = SendMessage(LE_GetDefaultLoop(), agent->task, "connect success."); ret = SendMessage(g_controlFdLoop, agent->task, "connect success.");
BEGET_ERROR_CHECK(ret == 0, return -1, "[control_fd] Failed send msg"); BEGET_ERROR_CHECK(ret == 0, return -1, "[control_fd] Failed send msg");
OH_ListAddTail(&g_cmdService.head, &agent->item); OH_ListAddTail(&g_cmdService.head, &agent->item);
return 0; return 0;
} }
void CmdServiceInit(const char *socketPath, CallbackControlFdProcess func) void CmdServiceInit(const char *socketPath, CallbackControlFdProcess func, LoopHandle loop)
{ {
if ((socketPath == NULL) || (func == NULL)) { if ((socketPath == NULL) || (func == NULL) || (loop == NULL)) {
BEGET_LOGE("[control_fd] Invalid parameter"); BEGET_LOGE("[control_fd] Invalid parameter");
return; return;
} }
...@@ -135,7 +136,10 @@ void CmdServiceInit(const char *socketPath, CallbackControlFdProcess func) ...@@ -135,7 +136,10 @@ void CmdServiceInit(const char *socketPath, CallbackControlFdProcess func)
info.sendMessageComplete = NULL; info.sendMessageComplete = NULL;
info.recvMessage = NULL; info.recvMessage = NULL;
g_controlFdFunc = func; g_controlFdFunc = func;
(void)LE_CreateStreamServer(LE_GetDefaultLoop(), &g_cmdService.serverTask, &info); if (g_controlFdLoop == NULL) {
g_controlFdLoop = loop;
}
(void)LE_CreateStreamServer(g_controlFdLoop, &g_cmdService.serverTask, &info);
} }
static int ClientTraversalProc(ListNode *node, void *data) static int ClientTraversalProc(ListNode *node, void *data)
...@@ -152,6 +156,23 @@ void CmdServiceProcessDelClient(pid_t pid) ...@@ -152,6 +156,23 @@ void CmdServiceProcessDelClient(pid_t pid)
CmdTask *agent = ListEntry(node, CmdTask, item); CmdTask *agent = ListEntry(node, CmdTask, item);
OH_ListRemove(&agent->item); OH_ListRemove(&agent->item);
OH_ListInit(&agent->item); OH_ListInit(&agent->item);
LE_CloseTask(LE_GetDefaultLoop(), agent->task); LE_CloseTask(g_controlFdLoop, agent->task);
}
}
static void CmdServiceDestroyProc(ListNode *node)
{
if (node == NULL) {
return;
} }
CmdTask *agent = ListEntry(node, CmdTask, item);
OH_ListRemove(&agent->item);
OH_ListInit(&agent->item);
LE_CloseTask(g_controlFdLoop, agent->task);
}
void CmdServiceProcessDestroyClient(void)
{
OH_ListRemoveAll(&g_cmdService.head, CmdServiceDestroyProc);
LE_StopLoop(g_controlFdLoop);
} }
...@@ -260,6 +260,6 @@ void ProcessControlFd(uint16_t type, const char *serviceCmd, const void *context ...@@ -260,6 +260,6 @@ void ProcessControlFd(uint16_t type, const char *serviceCmd, const void *context
void InitControlFd(void) void InitControlFd(void)
{ {
CmdServiceInit(INIT_CONTROL_FD_SOCKET_PATH, ProcessControlFd); CmdServiceInit(INIT_CONTROL_FD_SOCKET_PATH, ProcessControlFd, LE_GetDefaultLoop());
return; return;
} }
...@@ -28,7 +28,7 @@ namespace OHOS { ...@@ -28,7 +28,7 @@ namespace OHOS {
{ {
std::string str(reinterpret_cast<const char*>(data), size); std::string str(reinterpret_cast<const char*>(data), size);
CloseStdout(); CloseStdout();
CmdServiceInit(str.c_str(), Func); CmdServiceInit(str.c_str(), Func, LE_GetDefaultLoop());
return true; return true;
} }
} }
......
...@@ -354,12 +354,12 @@ HWTEST_F(InnerkitsUnitTest, TestControlFd, TestSize.Level1) ...@@ -354,12 +354,12 @@ HWTEST_F(InnerkitsUnitTest, TestControlFd, TestSize.Level1)
HWTEST_F(InnerkitsUnitTest, TestControlFdServer, TestSize.Level1) HWTEST_F(InnerkitsUnitTest, TestControlFdServer, TestSize.Level1)
{ {
CmdServiceInit(nullptr, nullptr); CmdServiceInit(nullptr, nullptr, nullptr);
CmdServiceInit("/data/testSock1", [](uint16_t type, const char *serviceCmd, const void *context) { CmdServiceInit("/data/testSock1", [](uint16_t type, const char *serviceCmd, const void *context) {
UNUSED(type); UNUSED(type);
UNUSED(serviceCmd); UNUSED(serviceCmd);
UNUSED(context); UNUSED(context);
}); }, LE_GetDefaultLoop());
TaskHandle testServer; TaskHandle testServer;
LE_StreamServerInfo info = {}; LE_StreamServerInfo info = {};
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册