add global var g_set_cloexec and macro FC_SET_CLOEXEC

replication_quorum
YuQing 2022-06-25 11:22:25 +08:00
parent 4a7d852409
commit 599d0f1446
5 changed files with 21 additions and 6 deletions

View File

@ -1,6 +1,7 @@
Version 1.59 2022-06-24 Version 1.59 2022-06-25
* open file with flag O_CLOEXEC * open file with flag O_CLOEXEC
* add global var g_set_cloexec and macro FC_SET_CLOEXEC
Version 1.58 2022-06-04 Version 1.58 2022-06-04
* add function conn_pool_connect_server_ex1 to support service name * add function conn_pool_connect_server_ex1 to support service name

View File

@ -125,7 +125,7 @@ int conn_pool_connect_server_ex1(ConnectionInfo *conn,
} }
if ((conn->sock=socketCreateEx2(conn->socket_domain, conn->ip_addr, 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; return result;
} }
@ -160,8 +160,8 @@ int conn_pool_async_connect_server_ex(ConnectionInfo *conn,
close(conn->sock); close(conn->sock);
} }
if ((conn->sock=socketCreateEx2(conn->socket_domain, conn->ip_addr, if ((conn->sock=socketCreateEx2(conn->socket_domain,
O_NONBLOCK | O_CLOEXEC, bind_ipaddr, conn->ip_addr, O_NONBLOCK, bind_ipaddr,
&result)) < 0) &result)) < 0)
{ {
return result; return result;

View File

@ -45,6 +45,8 @@
#endif #endif
#endif #endif
bool g_set_cloexec = false;
char *formatDatetime(const time_t nTime, \ char *formatDatetime(const time_t nTime, \
const char *szDateFormat, \ const char *szDateFormat, \
char *buff, const int buff_size) char *buff, const int buff_size)

View File

@ -41,10 +41,20 @@
normalize_path_ex(from, filename, full_filename, size, \ normalize_path_ex(from, filename, full_filename, size, \
NORMALIZE_FLAGS_URL_ENABLED_AND_APPEND_PARAMS) NORMALIZE_FLAGS_URL_ENABLED_AND_APPEND_PARAMS)
#define FC_SET_CLOEXEC(fd) \
if (g_set_cloexec) fd_set_cloexec(fd)
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
extern bool g_set_cloexec;
static inline void fc_enable_fd_cloexec(const bool cloexec)
{
g_set_cloexec = cloexec;
}
/** lowercase the string /** lowercase the string
* parameters: * parameters:
* src: input string, will be changed * src: input string, will be changed
@ -730,7 +740,7 @@ int fd_add_flags(int fd, int adding_flags);
* fd: the fd to set * fd: the fd to set
* return: error no , 0 success, != 0 fail * 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 /** set fd FD_CLOEXEC flags
* parameters: * parameters:

View File

@ -1012,6 +1012,7 @@ int socketCreateEx2(int af, const char *server_ip,
return -1; return -1;
} }
FC_SET_CLOEXEC(sock);
SET_SOCKOPT_NOSIGPIPE(sock); SET_SOCKOPT_NOSIGPIPE(sock);
if (flags != 0) if (flags != 0)
{ {
@ -1402,6 +1403,7 @@ int socketServer2(int af, const char *bind_ipaddr, const int port, int *err_no)
return -1; return -1;
} }
FC_SET_CLOEXEC(sock);
SET_SOCKOPT_NOSIGPIPE(sock); SET_SOCKOPT_NOSIGPIPE(sock);
result = 1; result = 1;
@ -2126,7 +2128,7 @@ int tcpsetnonblockopt(int fd)
return errno != 0 ? errno : EACCES; 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, " \ logError("file: "__FILE__", line: %d, " \
"fcntl failed, errno: %d, error info: %s.", \ "fcntl failed, errno: %d, error info: %s.", \