dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone

Blame SOURCES/0154-UTIL-Allow-dup-ing-child-pipe-to-a-different-FD.patch

905b4d
From df95d21e5253cec8745329567ab8050bfcd52333 Mon Sep 17 00:00:00 2001
905b4d
From: Jakub Hrozek <jhrozek@redhat.com>
905b4d
Date: Wed, 7 Jan 2015 10:36:12 +0100
905b4d
Subject: [PATCH 154/160] UTIL: Allow dup-ing child pipe to a different FD
905b4d
905b4d
Related to:
905b4d
    https://fedorahosted.org/sssd/ticket/2544
905b4d
905b4d
Adds a new function exec_child_ex and moves setting the extra_argv[]
905b4d
to exec_child_ex() along with specifying the input and output fds.
905b4d
905b4d
Reviewed-by: Sumit Bose <sbose@redhat.com>
905b4d
(cherry picked from commit 16cb0969f0a9ea71524d852077d6a480740d4f12)
905b4d
---
905b4d
 src/providers/ad/ad_gpo.c               |  2 +-
905b4d
 src/providers/ipa/ipa_selinux.c         |  3 +--
905b4d
 src/providers/krb5/krb5_child_handler.c |  8 ++++----
905b4d
 src/providers/ldap/sdap_child_helpers.c |  3 +--
905b4d
 src/tests/cmocka/test_child_common.c    | 11 ++++++-----
905b4d
 src/util/child_common.c                 | 22 ++++++++++++++++------
905b4d
 src/util/child_common.h                 | 12 ++++++++++--
905b4d
 7 files changed, 39 insertions(+), 22 deletions(-)
905b4d
905b4d
diff --git a/src/providers/ad/ad_gpo.c b/src/providers/ad/ad_gpo.c
905b4d
index 4f8497809bfe962672a99f26de7b61f9f89ac6fa..1ae62e7c4727702d5338258046e89e4b654904eb 100644
905b4d
--- a/src/providers/ad/ad_gpo.c
905b4d
+++ b/src/providers/ad/ad_gpo.c
905b4d
@@ -3963,7 +3963,7 @@ gpo_fork_child(struct tevent_req *req)
905b4d
     if (pid == 0) { /* child */
905b4d
         err = exec_child(state,
905b4d
                          pipefd_to_child, pipefd_from_child,
905b4d
-                         GPO_CHILD, gpo_child_debug_fd, NULL);
905b4d
+                         GPO_CHILD, gpo_child_debug_fd);
905b4d
         DEBUG(SSSDBG_CRIT_FAILURE, "Could not exec gpo_child: [%d][%s].\n",
905b4d
               err, strerror(err));
905b4d
         return err;
905b4d
diff --git a/src/providers/ipa/ipa_selinux.c b/src/providers/ipa/ipa_selinux.c
905b4d
index c4e70cfcb0748988d91fc1db57cf5a30d5365be4..133b679b6d518704ebb2bd901c64ac48170c9a0b 100644
905b4d
--- a/src/providers/ipa/ipa_selinux.c
905b4d
+++ b/src/providers/ipa/ipa_selinux.c
905b4d
@@ -1049,8 +1049,7 @@ static errno_t selinux_fork_child(struct selinux_child_state *state)
905b4d
     if (pid == 0) { /* child */
905b4d
         ret = exec_child(state,
905b4d
                          pipefd_to_child, pipefd_from_child,
905b4d
-                         SELINUX_CHILD, selinux_child_debug_fd,
905b4d
-                         NULL);
905b4d
+                         SELINUX_CHILD, selinux_child_debug_fd);
905b4d
         DEBUG(SSSDBG_CRIT_FAILURE, "Could not exec selinux_child: [%d][%s].\n",
905b4d
               ret, sss_strerror(ret));
905b4d
         return ret;
905b4d
diff --git a/src/providers/krb5/krb5_child_handler.c b/src/providers/krb5/krb5_child_handler.c
905b4d
index 1454d220fb294abc339df6e862154012a03fdca0..633cd917737d3f39526b049cc3d930b67f8b5c66 100644
905b4d
--- a/src/providers/krb5/krb5_child_handler.c
905b4d
+++ b/src/providers/krb5/krb5_child_handler.c
905b4d
@@ -305,10 +305,10 @@ static errno_t fork_child(struct tevent_req *req)
905b4d
     pid = fork();
905b4d
 
905b4d
     if (pid == 0) { /* child */
905b4d
-        err = exec_child(state,
905b4d
-                         pipefd_to_child, pipefd_from_child,
905b4d
-                         KRB5_CHILD, state->kr->krb5_ctx->child_debug_fd,
905b4d
-                         k5c_extra_args);
905b4d
+        err = exec_child_ex(state,
905b4d
+                            pipefd_to_child, pipefd_from_child,
905b4d
+                            KRB5_CHILD, state->kr->krb5_ctx->child_debug_fd,
905b4d
+                            k5c_extra_args, STDIN_FILENO, STDOUT_FILENO);
905b4d
         if (err != EOK) {
905b4d
             DEBUG(SSSDBG_CRIT_FAILURE, "Could not exec KRB5 child: [%d][%s].\n",
905b4d
                       err, strerror(err));
905b4d
diff --git a/src/providers/ldap/sdap_child_helpers.c b/src/providers/ldap/sdap_child_helpers.c
905b4d
index b60891d2b41f9a359856eb22174128d7f07559fb..40010989021eb7cf77b96876b2d1c4119ed39163 100644
905b4d
--- a/src/providers/ldap/sdap_child_helpers.c
905b4d
+++ b/src/providers/ldap/sdap_child_helpers.c
905b4d
@@ -108,8 +108,7 @@ static errno_t sdap_fork_child(struct tevent_context *ev,
905b4d
     if (pid == 0) { /* child */
905b4d
         err = exec_child(child,
905b4d
                          pipefd_to_child, pipefd_from_child,
905b4d
-                         LDAP_CHILD, ldap_child_debug_fd,
905b4d
-                         NULL);
905b4d
+                         LDAP_CHILD, ldap_child_debug_fd);
905b4d
         DEBUG(SSSDBG_CRIT_FAILURE, "Could not exec LDAP child: [%d][%s].\n",
905b4d
                                     err, strerror(err));
905b4d
         return err;
905b4d
diff --git a/src/tests/cmocka/test_child_common.c b/src/tests/cmocka/test_child_common.c
905b4d
index 112ed0ad97294bc45eac7c2124155e6b1908ad92..348b3e6c354d724fac12939c8bd785bbb993e667 100644
905b4d
--- a/src/tests/cmocka/test_child_common.c
905b4d
+++ b/src/tests/cmocka/test_child_common.c
905b4d
@@ -89,7 +89,7 @@ void test_exec_child(void **state)
905b4d
         ret = exec_child(child_tctx,
905b4d
                          child_tctx->pipefd_to_child,
905b4d
                          child_tctx->pipefd_from_child,
905b4d
-                         CHILD_DIR"/"TEST_BIN, 2, NULL);
905b4d
+                         CHILD_DIR"/"TEST_BIN, 2);
905b4d
         assert_int_equal(ret, EOK);
905b4d
     } else {
905b4d
             do {
905b4d
@@ -128,10 +128,11 @@ void test_exec_child_extra_args(void **state)
905b4d
     child_pid = fork();
905b4d
     assert_int_not_equal(child_pid, -1);
905b4d
     if (child_pid == 0) {
905b4d
-        ret = exec_child(child_tctx,
905b4d
-                         child_tctx->pipefd_to_child,
905b4d
-                         child_tctx->pipefd_from_child,
905b4d
-                         CHILD_DIR"/"TEST_BIN, 2, extra_args);
905b4d
+        ret = exec_child_ex(child_tctx,
905b4d
+                            child_tctx->pipefd_to_child,
905b4d
+                            child_tctx->pipefd_from_child,
905b4d
+                            CHILD_DIR"/"TEST_BIN, 2, extra_args,
905b4d
+                            STDIN_FILENO, STDOUT_FILENO);
905b4d
         assert_int_equal(ret, EOK);
905b4d
     } else {
905b4d
             do {
905b4d
diff --git a/src/util/child_common.c b/src/util/child_common.c
905b4d
index 9710630f9773ae02258e4f0dd609a3d74978c8f4..7975a839499370a4a29d9fbc59f815d7da1f63dd 100644
905b4d
--- a/src/util/child_common.c
905b4d
+++ b/src/util/child_common.c
905b4d
@@ -729,17 +729,18 @@ fail:
905b4d
     return ret;
905b4d
 }
905b4d
 
905b4d
-errno_t exec_child(TALLOC_CTX *mem_ctx,
905b4d
-                   int *pipefd_to_child, int *pipefd_from_child,
905b4d
-                   const char *binary, int debug_fd,
905b4d
-                   const char *extra_argv[])
905b4d
+errno_t exec_child_ex(TALLOC_CTX *mem_ctx,
905b4d
+                      int *pipefd_to_child, int *pipefd_from_child,
905b4d
+                      const char *binary, int debug_fd,
905b4d
+                      const char *extra_argv[],
905b4d
+                      int child_in_fd, int child_out_fd)
905b4d
 {
905b4d
     int ret;
905b4d
     errno_t err;
905b4d
     char **argv;
905b4d
 
905b4d
     close(pipefd_to_child[1]);
905b4d
-    ret = dup2(pipefd_to_child[0], STDIN_FILENO);
905b4d
+    ret = dup2(pipefd_to_child[0], child_in_fd);
905b4d
     if (ret == -1) {
905b4d
         err = errno;
905b4d
         DEBUG(SSSDBG_CRIT_FAILURE,
905b4d
@@ -748,7 +749,7 @@ errno_t exec_child(TALLOC_CTX *mem_ctx,
905b4d
     }
905b4d
 
905b4d
     close(pipefd_from_child[0]);
905b4d
-    ret = dup2(pipefd_from_child[1], STDOUT_FILENO);
905b4d
+    ret = dup2(pipefd_from_child[1], child_out_fd);
905b4d
     if (ret == -1) {
905b4d
         err = errno;
905b4d
         DEBUG(SSSDBG_CRIT_FAILURE,
905b4d
@@ -770,6 +771,15 @@ errno_t exec_child(TALLOC_CTX *mem_ctx,
905b4d
     return err;
905b4d
 }
905b4d
 
905b4d
+errno_t exec_child(TALLOC_CTX *mem_ctx,
905b4d
+                   int *pipefd_to_child, int *pipefd_from_child,
905b4d
+                   const char *binary, int debug_fd)
905b4d
+{
905b4d
+    return exec_child_ex(mem_ctx, pipefd_to_child, pipefd_from_child,
905b4d
+                         binary, debug_fd, NULL,
905b4d
+                         STDIN_FILENO, STDOUT_FILENO);
905b4d
+}
905b4d
+
905b4d
 void child_cleanup(int readfd, int writefd)
905b4d
 {
905b4d
     int ret;
905b4d
diff --git a/src/util/child_common.h b/src/util/child_common.h
905b4d
index e659388ece3677b7746c159d7de3e86171bb4146..369de71a13449beb185e5bc682c8871625fe6027 100644
905b4d
--- a/src/util/child_common.h
905b4d
+++ b/src/util/child_common.h
905b4d
@@ -112,10 +112,18 @@ void child_sig_handler(struct tevent_context *ev,
905b4d
                        int count, void *__siginfo, void *pvt);
905b4d
 
905b4d
 /* Never returns EOK, ether returns an error, or doesn't return on success */
905b4d
+errno_t exec_child_ex(TALLOC_CTX *mem_ctx,
905b4d
+                      int *pipefd_to_child, int *pipefd_from_child,
905b4d
+                      const char *binary, int debug_fd,
905b4d
+                      const char *extra_argv[],
905b4d
+                      int child_in_fd, int child_out_fd);
905b4d
+
905b4d
+/* Same as exec_child_ex() except child_in_fd is set to STDIN_FILENO and
905b4d
+ * child_out_fd is set to STDOUT_FILENO and extra_argv is always NULL.
905b4d
+ */
905b4d
 errno_t exec_child(TALLOC_CTX *mem_ctx,
905b4d
                    int *pipefd_to_child, int *pipefd_from_child,
905b4d
-                   const char *binary, int debug_fd,
905b4d
-                   const char *extra_argv[]);
905b4d
+                   const char *binary, int debug_fd);
905b4d
 
905b4d
 void child_cleanup(int readfd, int writefd);
905b4d
 
905b4d
-- 
905b4d
2.1.0
905b4d