diff --git a/src/cm_defines/cm_error.h b/src/cm_defines/cm_error.h index 9bb2a20dac0fb1e6b77b0332fc50d9011ca80f78..dda0aedb432f441ea015aa5dc5f32e157b4e2e4b 100644 --- a/src/cm_defines/cm_error.h +++ b/src/cm_defines/cm_error.h @@ -250,6 +250,14 @@ static inline void cm_panic(bool32 condition) } \ } while (0) +#define CM_CONTINUE_IF_EAGAIN(err) \ + do { \ + if ((err) == EAGAIN) { \ + LOG_DEBUG_INF("Linux return EAGAIN errno, try again."); \ + continue; \ + } \ + } while (0) + #define CM_THROW_ERROR(error_no, ...) \ do { \ cm_set_error((char *)__FILE_NAME__, (uint32)__LINE__, (cm_errno_t)error_no, \ diff --git a/src/cm_utils/cm_file.c b/src/cm_utils/cm_file.c index 331fe0b24c08802f65b1b598b894e134d4b2bf8a..725b27ed7b25875a981fdc8f22a353dc7a6b9a50 100644 --- a/src/cm_utils/cm_file.c +++ b/src/cm_utils/cm_file.c @@ -816,10 +816,14 @@ status_t cm_lock_fd(int32 fd) lk.l_whence = SEEK_SET; lk.l_start = lk.l_len = 0; - if (fcntl(fd, F_SETLK, &lk) != 0) { + do { + if (fcntl(fd, F_SETLK, &lk) == 0) { + break; + } + CM_CONTINUE_IF_EAGAIN(cm_get_os_error()); CM_THROW_ERROR(ERR_LOCK_FILE, errno); return CM_ERROR; - } + } while (CM_TRUE); return CM_SUCCESS; #endif @@ -836,10 +840,14 @@ status_t cm_unlock_fd(int32 fd) lk.l_whence = SEEK_SET; lk.l_start = lk.l_len = 0; - if (fcntl(fd, F_SETLK, &lk) != 0) { + do { + if (fcntl(fd, F_SETLK, &lk) == 0) { + break; + } + CM_CONTINUE_IF_EAGAIN(cm_get_os_error()); CM_THROW_ERROR(ERR_UNLOCK_FILE, errno); return CM_ERROR; - } + } while (CM_TRUE); return CM_SUCCESS; #endif diff --git a/src/ddes_cap/ddes_cap_exec.c b/src/ddes_cap/ddes_cap_exec.c index f28ad5b20a72b02053cd56221ebfa6409016caa1..c78b298d8226ecb189ca159ddfdc06190c6de237 100644 --- a/src/ddes_cap/ddes_cap_exec.c +++ b/src/ddes_cap/ddes_cap_exec.c @@ -218,17 +218,24 @@ status_t cap_agent_init(cap_agent_t *agent, const char *agent_name) // application server send cap command as a request to cap agent // after executing, cap agent send a response if (0 == agent->pid) { // child process - if (-1 == fcntl(agent->req_pipe.hread, F_SETFL, O_NONBLOCK)) { + do { + if (fcntl(agent->req_pipe.hread, F_SETFL, O_NONBLOCK) != -1) { + break; + } + CM_CONTINUE_IF_EAGAIN(cm_get_os_error()); send_fail_response(CM_ERROR, "failed to set reading end of request pipe to non-block. errno = %d.", errno); return CM_ERROR; - } - if (-1 == fcntl(agent->res_pipe.hwrite, F_SETFL, O_NONBLOCK)) { + } while (CM_TRUE); + do { + if (fcntl(agent->res_pipe.hwrite, F_SETFL, O_NONBLOCK) != -1) { + break; + } + CM_CONTINUE_IF_EAGAIN(cm_get_os_error()); send_fail_response(CM_ERROR, "failed to set writing end of response pipe to non-block. errno = %d.", errno); return CM_ERROR; - } - + } while (CM_TRUE); char req_rfd[MAX_FD_LEN + 1], res_wfd[MAX_FD_LEN + 1]; int32 size = vsnprintf_s(req_rfd, MAX_FD_LEN, MAX_FD_LEN, agent->req_pipe.hread); if (SECUREC_UNLIKELY(size == -1)) { @@ -261,16 +268,24 @@ status_t cap_agent_init(cap_agent_t *agent, const char *agent_name) // close the write end of response pipe close(agent->res_pipe.hwrite); - - if (-1 == fcntl(agent->req_pipe.hwrite, F_SETFL, O_NONBLOCK)) { - // log error; + do { + if (fcntl(agent->req_pipe.hwrite, F_SETFL, O_NONBLOCK) != -1) { + break; + } + CM_CONTINUE_IF_EAGAIN(cm_get_os_error()); + send_fail_response(CM_ERROR, + "failed to set writing end of request pipe to non-block. errno = %d.", errno); return CM_ERROR; - } - if (-1 == fcntl(agent->res_pipe.hread, F_SETFL, O_NONBLOCK)) { - // log error; + } while (CM_TRUE); + do { + if (fcntl(agent->res_pipe.hread, F_SETFL, O_NONBLOCK) != -1) { + break; + } + CM_CONTINUE_IF_EAGAIN(cm_get_os_error()); + send_fail_response(CM_ERROR, + "failed to set reading end of response pipe to non-block. errno = %d.", errno); return CM_ERROR; - } - + } while (CM_TRUE); return check_exec_response(agent->res_pipe.hread, NULL, 0); } }