>From 97bcc4166307c374c7f3f110f8c0bee37e1c3320 Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Tue, 21 Oct 2014 12:29:55 +0200 Subject: [PATCH 1/3] UTIL: Remove code duplication of struct io We had struct io and the associated destructor copied twice in the code already and need it again in the SELinux provider. Instead of adding another copy, move the code to a shared subtree under util/ --- src/providers/ad/ad_gpo.c | 43 +++------------------------------ src/providers/krb5/krb5_child_handler.c | 38 ++--------------------------- src/util/child_common.c | 29 ++++++++++++++++++++++ src/util/child_common.h | 7 ++++++ 4 files changed, 41 insertions(+), 76 deletions(-) diff --git a/src/providers/ad/ad_gpo.c b/src/providers/ad/ad_gpo.c index 4dfbd4b6943b477bd93fdd730dfa5b1c5828a10a..80b0d45c2861a64f01fe7835cccee4348084c7da 100644 --- a/src/providers/ad/ad_gpo.c +++ b/src/providers/ad/ad_gpo.c @@ -3756,46 +3756,9 @@ struct ad_gpo_process_cse_state { pid_t child_pid; uint8_t *buf; ssize_t len; - struct io *io; + struct child_io_fds *io; }; -struct io { - int read_from_child_fd; - int write_to_child_fd; -}; - -static errno_t -gpo_child_io_destructor(void *ptr) -{ - int ret; - struct io *io; - - io = talloc_get_type(ptr, struct io); - if (io == NULL) return EOK; - - if (io->write_to_child_fd != -1) { - ret = close(io->write_to_child_fd); - io->write_to_child_fd = -1; - if (ret != EOK) { - ret = errno; - DEBUG(SSSDBG_CRIT_FAILURE, - "close failed [%d][%s].\n", ret, strerror(ret)); - } - } - - if (io->read_from_child_fd != -1) { - ret = close(io->read_from_child_fd); - io->read_from_child_fd = -1; - if (ret != EOK) { - ret = errno; - DEBUG(SSSDBG_CRIT_FAILURE, - "close failed [%d][%s].\n", ret, strerror(ret)); - } - } - - return EOK; -} - static errno_t gpo_fork_child(struct tevent_req *req); static void gpo_cse_step(struct tevent_req *subreq); static void gpo_cse_done(struct tevent_req *subreq); @@ -3849,7 +3812,7 @@ ad_gpo_process_cse_send(TALLOC_CTX *mem_ctx, state->gpo_guid = gpo_guid; state->smb_path = smb_path; state->smb_cse_suffix = smb_cse_suffix; - state->io = talloc(state, struct io); + state->io = talloc(state, struct child_io_fds); if (state->io == NULL) { DEBUG(SSSDBG_CRIT_FAILURE, "talloc failed.\n"); ret = ENOMEM; @@ -3858,7 +3821,7 @@ ad_gpo_process_cse_send(TALLOC_CTX *mem_ctx, state->io->write_to_child_fd = -1; state->io->read_from_child_fd = -1; - talloc_set_destructor((void *) state->io, gpo_child_io_destructor); + talloc_set_destructor((void *) state->io, child_io_destructor); /* prepare the data to pass to child */ ret = create_cse_send_buffer(state, smb_server, smb_share, smb_path, diff --git a/src/providers/krb5/krb5_child_handler.c b/src/providers/krb5/krb5_child_handler.c index 114e72a33e48da299647c2fe3096b9bf61cf294a..4ba939deb3e0e282b3ca9b2b21226ea7e64e311b 100644 --- a/src/providers/krb5/krb5_child_handler.c +++ b/src/providers/krb5/krb5_child_handler.c @@ -41,11 +41,6 @@ #define TIME_T_MAX LONG_MAX #define int64_to_time_t(val) ((time_t)((val) < TIME_T_MAX ? val : TIME_T_MAX)) -struct io { - int read_from_child_fd; - int write_to_child_fd; -}; - struct handle_child_state { struct tevent_context *ev; struct krb5child_req *kr; @@ -55,38 +50,9 @@ struct handle_child_state { struct tevent_timer *timeout_handler; pid_t child_pid; - struct io *io; + struct child_io_fds *io; }; -static int child_io_destructor(void *ptr) -{ - int ret; - struct io *io = talloc_get_type(ptr, struct io); - if (io == NULL) return EOK; - - if (io->write_to_child_fd != -1) { - ret = close(io->write_to_child_fd); - io->write_to_child_fd = -1; - if (ret != EOK) { - ret = errno; - DEBUG(SSSDBG_CRIT_FAILURE, - "close failed [%d][%s].\n", ret, strerror(ret)); - } - } - - if (io->read_from_child_fd != -1) { - ret = close(io->read_from_child_fd); - io->read_from_child_fd = -1; - if (ret != EOK) { - ret = errno; - DEBUG(SSSDBG_CRIT_FAILURE, - "close failed [%d][%s].\n", ret, strerror(ret)); - } - } - - return EOK; -} - static errno_t pack_authtok(struct io_buffer *buf, size_t *rp, struct sss_auth_token *tok) { @@ -391,7 +357,7 @@ struct tevent_req *handle_child_send(TALLOC_CTX *mem_ctx, state->child_pid = -1; state->timeout_handler = NULL; - state->io = talloc(state, struct io); + state->io = talloc(state, struct child_io_fds); if (state->io == NULL) { DEBUG(SSSDBG_CRIT_FAILURE, "talloc failed.\n"); ret = ENOMEM; diff --git a/src/util/child_common.c b/src/util/child_common.c index 81bbab70ed44a6c0a4410909924aec195c643bea..e4a885b6e6e4a1a8a0cabd12ba1544a7c8f0f160 100644 --- a/src/util/child_common.c +++ b/src/util/child_common.c @@ -772,3 +772,32 @@ void child_cleanup(int readfd, int writefd) } } } + +int child_io_destructor(void *ptr) +{ + int ret; + struct child_io_fds *io = talloc_get_type(ptr, struct child_io_fds); + if (io == NULL) return EOK; + + if (io->write_to_child_fd != -1) { + ret = close(io->write_to_child_fd); + io->write_to_child_fd = -1; + if (ret != EOK) { + ret = errno; + DEBUG(SSSDBG_CRIT_FAILURE, + "close failed [%d][%s].\n", ret, strerror(ret)); + } + } + + if (io->read_from_child_fd != -1) { + ret = close(io->read_from_child_fd); + io->read_from_child_fd = -1; + if (ret != EOK) { + ret = errno; + DEBUG(SSSDBG_CRIT_FAILURE, + "close failed [%d][%s].\n", ret, strerror(ret)); + } + } + + return EOK; +} diff --git a/src/util/child_common.h b/src/util/child_common.h index 95865bb529b6b282a57b138d7a85ce93649faa2e..261da7f9c546ddfdb38506e5285024ad201bdd4d 100644 --- a/src/util/child_common.h +++ b/src/util/child_common.h @@ -45,6 +45,11 @@ struct io_buffer { size_t size; }; +struct child_io_fds { + int read_from_child_fd; + int write_to_child_fd; +}; + /* COMMON SIGCHLD HANDLING */ typedef void (*sss_child_fn_t)(int pid, int wait_status, void *pvt); @@ -113,4 +118,6 @@ errno_t exec_child(TALLOC_CTX *mem_ctx, void child_cleanup(int readfd, int writefd); +int child_io_destructor(void *ptr); + #endif /* __CHILD_COMMON_H__ */ -- 1.9.3