未验证 提交 d0be5ab4 编写于 作者: O openharmony_ci 提交者: Gitee

!756 fix dump_service fd 泄露的bug

Merge pull request !756 from Mupceet/init0601_1
......@@ -61,6 +61,7 @@ typedef struct {
void CmdServiceInit(const char *socketPath, CallbackControlFdProcess func);
void CmdClientInit(const char *socketPath, uint16_t type, const char *cmd, const char *fifoName);
void DestroyCmdFifo(int rfd, int wfd, const char *readPath, const char *writePath);
#ifdef __cplusplus
#if __cplusplus
......
......@@ -70,6 +70,9 @@ static void ProcessFifoRead(const WatcherHandle taskHandle, int fd, uint32_t *ev
int readlen = read(fd, buf, FIFO_BUF_SIZE - 1);
if (readlen > 0) {
fprintf(stdout, "%s", buf);
} else {
DestroyCmdFifo(g_FifoReadFd, g_FifoWriteFd, g_FifoReadPath, g_FifoWritePath);
return;
}
int ret = fflush(stdout);
BEGET_ERROR_CHECK(ret == 0, return, "[control_fd] Failed fflush err=%d", errno);
......@@ -79,20 +82,21 @@ static void ProcessFifoRead(const WatcherHandle taskHandle, int fd, uint32_t *ev
*events = Event_Read;
}
static void DestroyCmdFifo(void)
void DestroyCmdFifo(int rfd, int wfd, const char *readPath, const char *writePath)
{
if (g_FifoReadFd >= 0) {
(void)close(g_FifoReadFd);
if (rfd >= 0) {
(void)close(rfd);
}
if (wfd >= 0) {
(void)close(wfd);
}
if (readPath != NULL) {
BEGET_CHECK_ONLY_ELOG(unlink(readPath) == 0, "Failed unlink fifo %s", readPath);
}
if (g_FifoWriteFd >= 0) {
(void)close(g_FifoWriteFd);
if (writePath != NULL) {
BEGET_CHECK_ONLY_ELOG(unlink(writePath) == 0, "Failed unlink fifo %s", writePath);
}
g_FifoReadFd = -1;
g_FifoWriteFd = -1;
int ret = unlink(g_FifoReadPath);
BEGET_CHECK_ONLY_ELOG(ret == 0, "Failed unlink fifo %s", g_FifoReadPath);
ret = unlink(g_FifoWritePath);
BEGET_CHECK_ONLY_ELOG(ret == 0, "Failed unlink fifo %s", g_FifoReadPath);
}
static void CmdOnRecvMessage(const TaskHandle task, const uint8_t *buffer, uint32_t buffLen)
......@@ -108,7 +112,7 @@ static void CmdOnConntectComplete(const TaskHandle client)
static void CmdOnClose(const TaskHandle task)
{
BEGET_LOGI("[control_fd] CmdOnClose");
DestroyCmdFifo();
DestroyCmdFifo(g_FifoReadFd, g_FifoWriteFd, g_FifoReadPath, g_FifoWritePath);
}
static void CmdDisConnectComplete(const TaskHandle client)
......@@ -270,5 +274,5 @@ void CmdClientInit(const char *socketPath, uint16_t type, const char *cmd, const
BEGET_LOGI("Cmd Client exit ");
LE_CloseStreamTask(LE_GetDefaultLoop(), agent->task);
free(agent);
DestroyCmdFifo();
DestroyCmdFifo(g_FifoReadFd, g_FifoWriteFd, g_FifoReadPath, g_FifoWritePath);
}
......@@ -41,24 +41,15 @@ static void CmdOnSendMessageComplete(const TaskHandle task, const BufferHandle h
BEGET_LOGI("[control_fd] CmdOnSendMessageComplete ");
}
static int OpenFifo(int pid, bool read, const char *pipeName)
static int OpenFifo(bool read, const char *pipePath)
{
// create pipe for cmd
if (pipeName == NULL) {
if (pipePath == NULL) {
return -1;
}
char buffer[FIFO_PATH_SIZE] = {0};
int ret = 0;
if (read == true) {
ret = sprintf_s(buffer, sizeof(buffer) - 1, "/dev/fifo/%s1.%d", pipeName, pid);
BEGET_ERROR_CHECK(ret > 0, return -1, "[control_fd] Failed sprintf_s err=%d", errno);
} else {
ret = sprintf_s(buffer, sizeof(buffer) - 1, "/dev/fifo/%s0.%d", pipeName, pid);
BEGET_ERROR_CHECK(ret > 0, return -1, "[control_fd] Failed sprintf_s err=%d", errno);
}
int flags = read ? (O_RDONLY | O_TRUNC | O_NONBLOCK) : (O_WRONLY | O_TRUNC);
return open(buffer, flags);
return open(pipePath, flags);
}
static void CmdOnRecvMessage(const TaskHandle task, const uint8_t *buffer, uint32_t buffLen)
......@@ -73,11 +64,16 @@ static void CmdOnRecvMessage(const TaskHandle task, const uint8_t *buffer, uint3
BEGET_LOGE("[control_fd] Failed msg ");
return;
}
int reader = OpenFifo(msg->pid, true, msg->fifoName); // read
char readPath[FIFO_PATH_SIZE] = {0};
int ret = sprintf_s(readPath, sizeof(readPath) - 1, "/dev/fifo/%s1.%d", msg->fifoName, msg->pid);
BEGET_ERROR_CHECK(ret > 0, return, "[control_fd] Failed sprintf_s err=%d", errno);
int reader = OpenFifo(true, readPath); // read
BEGET_ERROR_CHECK(reader > 0, return, "[control_fd] Failed to open filo");
int writer = OpenFifo(msg->pid, false, msg->fifoName); // write
char writePath[FIFO_PATH_SIZE] = {0};
ret = sprintf_s(writePath, sizeof(writePath) - 1, "/dev/fifo/%s0.%d", msg->fifoName, msg->pid);
BEGET_ERROR_CHECK(ret > 0, return, "[control_fd] Failed sprintf_s err=%d", errno);
int writer = OpenFifo(false, writePath); // write
BEGET_ERROR_CHECK(writer > 0, return, "[control_fd] Failed to open filo");
pid_t pid = fork();
......@@ -85,11 +81,14 @@ static void CmdOnRecvMessage(const TaskHandle task, const uint8_t *buffer, uint3
(void)dup2(reader, STDIN_FILENO);
(void)dup2(writer, STDOUT_FILENO);
(void)dup2(writer, STDERR_FILENO); // Redirect fd to 0, 1, 2
(void)close(reader);
(void)close(writer);
g_controlFdFunc(msg->type, msg->cmd, NULL);
exit(0);
} else if (pid < 0) {
BEGET_LOGE("[control_fd] Failed fork service");
}
DestroyCmdFifo(reader, writer, readPath, writePath);
return;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册