diff --git a/HISTORY b/HISTORY index 198e529..74bc0e4 100644 --- a/HISTORY +++ b/HISTORY @@ -1,6 +1,7 @@ -Version 1.59 2022-06-24 +Version 1.59 2022-06-25 * open file with flag O_CLOEXEC + * add global var g_set_cloexec and macro FC_SET_CLOEXEC Version 1.58 2022-06-04 * add function conn_pool_connect_server_ex1 to support service name diff --git a/src/connection_pool.c b/src/connection_pool.c index 5cf8a3d..6d05941 100644 --- a/src/connection_pool.c +++ b/src/connection_pool.c @@ -125,7 +125,7 @@ int conn_pool_connect_server_ex1(ConnectionInfo *conn, } if ((conn->sock=socketCreateEx2(conn->socket_domain, conn->ip_addr, - O_NONBLOCK | O_CLOEXEC, bind_ipaddr, &result)) < 0) + O_NONBLOCK, bind_ipaddr, &result)) < 0) { return result; } @@ -160,8 +160,8 @@ int conn_pool_async_connect_server_ex(ConnectionInfo *conn, close(conn->sock); } - if ((conn->sock=socketCreateEx2(conn->socket_domain, conn->ip_addr, - O_NONBLOCK | O_CLOEXEC, bind_ipaddr, + if ((conn->sock=socketCreateEx2(conn->socket_domain, + conn->ip_addr, O_NONBLOCK, bind_ipaddr, &result)) < 0) { return result; diff --git a/src/shared_func.c b/src/shared_func.c index 4886683..37fd469 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -45,6 +45,8 @@ #endif #endif +bool g_set_cloexec = false; + char *formatDatetime(const time_t nTime, \ const char *szDateFormat, \ char *buff, const int buff_size) diff --git a/src/shared_func.h b/src/shared_func.h index 2aba889..687d764 100644 --- a/src/shared_func.h +++ b/src/shared_func.h @@ -41,10 +41,20 @@ normalize_path_ex(from, filename, full_filename, size, \ NORMALIZE_FLAGS_URL_ENABLED_AND_APPEND_PARAMS) +#define FC_SET_CLOEXEC(fd) \ + if (g_set_cloexec) fd_set_cloexec(fd) + #ifdef __cplusplus extern "C" { #endif + extern bool g_set_cloexec; + +static inline void fc_enable_fd_cloexec(const bool cloexec) +{ + g_set_cloexec = cloexec; +} + /** lowercase the string * parameters: * src: input string, will be changed @@ -730,7 +740,7 @@ int fd_add_flags(int fd, int adding_flags); * fd: the fd to set * return: error no , 0 success, != 0 fail */ -#define set_nonblock(fd) fd_add_flags(fd, O_NONBLOCK | FD_CLOEXEC) +#define set_nonblock(fd) fd_add_flags(fd, O_NONBLOCK) /** set fd FD_CLOEXEC flags * parameters: diff --git a/src/sockopt.c b/src/sockopt.c index 8d04797..b45b364 100644 --- a/src/sockopt.c +++ b/src/sockopt.c @@ -1012,6 +1012,7 @@ int socketCreateEx2(int af, const char *server_ip, return -1; } + FC_SET_CLOEXEC(sock); SET_SOCKOPT_NOSIGPIPE(sock); if (flags != 0) { @@ -1402,6 +1403,7 @@ int socketServer2(int af, const char *bind_ipaddr, const int port, int *err_no) return -1; } + FC_SET_CLOEXEC(sock); SET_SOCKOPT_NOSIGPIPE(sock); result = 1; @@ -2126,7 +2128,7 @@ int tcpsetnonblockopt(int fd) return errno != 0 ? errno : EACCES; } - if (fcntl(fd, F_SETFL, flags | (O_NONBLOCK | FD_CLOEXEC)) < 0) + if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) { logError("file: "__FILE__", line: %d, " \ "fcntl failed, errno: %d, error info: %s.", \