dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone

Blame SOURCES/0069-UTIL-Remove-code-duplication-of-struct-io.patch

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