From b2b933e5a7fbccb33481f0c7a1fa0b560b22a961 Mon Sep 17 00:00:00 2001 From: yuqing Date: Thu, 31 Mar 2016 15:51:58 +0800 Subject: [PATCH] add function fd_set_cloexec --- HISTORY | 4 ++++ src/shared_func.c | 16 +++++++++++++--- src/shared_func.h | 19 ++++++++++++++++++- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/HISTORY b/HISTORY index 65cad8e..18d7183 100644 --- a/HISTORY +++ b/HISTORY @@ -1,3 +1,7 @@ + +Version 1.27 2016-03-31 + * add function fd_set_cloexec + Version 1.26 2016-03-16 * add logger parameter: compress_log_days_before diff --git a/src/shared_func.c b/src/shared_func.c index fa5766c..29c9c3a 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -1392,11 +1392,11 @@ void set_log_level(char *pLogLevel) } } -int fd_add_flags(int fd, int adding_flags) +int fcntl_add_flags(int fd, int get_cmd, int set_cmd, int adding_flags) { int flags; - flags = fcntl(fd, F_GETFL, 0); + flags = fcntl(fd, get_cmd, 0); if (flags < 0) { logError("file: "__FILE__", line: %d, " \ @@ -1405,7 +1405,7 @@ int fd_add_flags(int fd, int adding_flags) return errno != 0 ? errno : EACCES; } - if (fcntl(fd, F_SETFL, flags | adding_flags) == -1) + if (fcntl(fd, set_cmd, flags | adding_flags) == -1) { logError("file: "__FILE__", line: %d, " \ "fcntl fail, errno: %d, error info: %s.", \ @@ -1416,6 +1416,16 @@ int fd_add_flags(int fd, int adding_flags) return 0; } +int fd_add_flags(int fd, int adding_flags) +{ + return fcntl_add_flags(fd, F_GETFL, F_SETFL, adding_flags); +} + +int fd_set_cloexec(int fd) +{ + return fcntl_add_flags(fd, F_GETFD, F_SETFD, FD_CLOEXEC); +} + int set_run_by(const char *group_name, const char *username) { #ifndef WIN32 diff --git a/src/shared_func.h b/src/shared_func.h index 13a7e16..441732d 100644 --- a/src/shared_func.h +++ b/src/shared_func.h @@ -444,7 +444,17 @@ int fd_gets(int fd, char *buff, const int size, int once_bytes); */ int set_rlimit(int resource, const rlim_t value); -/** set non block mode +/** fcntl add flags such as O_NONBLOCK or FD_CLOEXEC + * parameters: + * fd: the fd to set + * get_cmd: the get command + * set_cmd: the set command + * adding_flags: the flags to add + * return: error no , 0 success, != 0 fail +*/ +int fcntl_add_flags(int fd, int get_cmd, int set_cmd, int adding_flags); + +/** set fd flags such as O_NONBLOCK * parameters: * fd: the fd to set * adding_flags: the flags to add @@ -459,6 +469,13 @@ int fd_add_flags(int fd, int adding_flags); */ #define set_nonblock(fd) fd_add_flags(fd, O_NONBLOCK) +/** set fd FD_CLOEXEC flags + * parameters: + * fd: the fd to set + * return: error no , 0 success, != 0 fail +*/ +int fd_set_cloexec(int fd); + /** set run by group and user * parameters: * group_name: the group name, can be NULL or empty