From f3d91181d4ee9da3f8bbf4ddf8782951c0ae46c1 Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Mon, 8 Dec 2014 17:39:57 +0100 Subject: UTIL: Unify the fd_nonblocking implementation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The responder and child_common modules each had their own implementation. Unify it instead and add a unit test. Reviewed-by: Pavel Březina --- src/util/child_common.c | 23 ----------------------- src/util/util.c | 24 ++++++++++++++++++++++++ src/util/util.h | 12 ++++++++++++ 3 files changed, 36 insertions(+), 23 deletions(-) (limited to 'src/util') diff --git a/src/util/child_common.c b/src/util/child_common.c index 0afd3a61..b1af0233 100644 --- a/src/util/child_common.c +++ b/src/util/child_common.c @@ -518,29 +518,6 @@ int read_pipe_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx, return EOK; } -/* The pipes to communicate with the child must be nonblocking */ -void fd_nonblocking(int fd) -{ - int flags; - int ret; - - flags = fcntl(fd, F_GETFL, 0); - if (flags == -1) { - ret = errno; - DEBUG(SSSDBG_CRIT_FAILURE, - "F_GETFL failed [%d][%s].\n", ret, strerror(ret)); - return; - } - - if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) { - ret = errno; - DEBUG(SSSDBG_CRIT_FAILURE, - "F_SETFL failed [%d][%s].\n", ret, strerror(ret)); - } - - return; -} - static void child_invoke_callback(struct tevent_context *ev, struct tevent_immediate *imm, void *pvt); diff --git a/src/util/util.c b/src/util/util.c index 2acb8604..613c559b 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -880,3 +880,27 @@ done: return ret; } + +/* Set the nonblocking flag to the fd */ +errno_t sss_fd_nonblocking(int fd) +{ + int flags; + int ret; + + flags = fcntl(fd, F_GETFL, 0); + if (flags == -1) { + ret = errno; + DEBUG(SSSDBG_CRIT_FAILURE, + "F_GETFL failed [%d][%s].\n", ret, strerror(ret)); + return ret; + } + + if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) { + ret = errno; + DEBUG(SSSDBG_CRIT_FAILURE, + "F_SETFL failed [%d][%s].\n", ret, strerror(ret)); + return ret; + } + + return EOK; +} diff --git a/src/util/util.h b/src/util/util.h index 45efd1ae..60dbf938 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -444,6 +445,17 @@ errno_t sss_hash_create_ex(TALLOC_CTX *mem_ctx, errno_t add_strings_lists(TALLOC_CTX *mem_ctx, const char **l1, const char **l2, bool copy_strings, char ***_new_list); +/** + * @brief set file descriptor as nonblocking + * + * Set the O_NONBLOCK flag for the input fd + * + * @param[in] fd The file descriptor to set as nonblocking + * + * @return EOK on success, errno code otherwise + */ +errno_t sss_fd_nonblocking(int fd); + /* Copy a NULL-terminated string list * Returns NULL on out of memory error or invalid input */ -- cgit