[glib/mcatanzaro/posix-spawn2: 2/2] gsubprocess: ensure we test fd remapping on the posix_spawn codepath




commit f12662de6e2f5d0352597047248bc9ac7d519c12
Author: Michael Catanzaro <mcatanzaro gnome org>
Date:   Thu Feb 25 12:21:38 2021 -0600

    gsubprocess: ensure we test fd remapping on the posix_spawn codepath
    
    We should run test_pass_fd twice, once using gspawn's fork/exec codepath
    and once attempting to use its posix_spawn codepath. There's no
    guarantee we'll actually get the posix_spawn codepath, but it works
    for now on Linux.
    
    This causes the test to fail if I break the fd remapping implementation
    in my previous commit. Without this, we fail to test fd remapping on the
    optimized codepath, and have no test that fails if it doesn't work
    properly.

 gio/tests/gsubprocess.c | 30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)
---
diff --git a/gio/tests/gsubprocess.c b/gio/tests/gsubprocess.c
index 7e22678ec..a35863e77 100644
--- a/gio/tests/gsubprocess.c
+++ b/gio/tests/gsubprocess.c
@@ -1697,7 +1697,8 @@ test_child_setup (void)
 }
 
 static void
-test_pass_fd (void)
+do_test_pass_fd (GSubprocessFlags     flags,
+                 GSpawnChildSetupFunc child_setup)
 {
   GError *local_error = NULL;
   GError **error = &local_error;
@@ -1722,9 +1723,11 @@ test_pass_fd (void)
   needdup_fd_str = g_strdup_printf ("%d", needdup_pipefds[1] + 1);
 
   args = get_test_subprocess_args ("write-to-fds", basic_fd_str, needdup_fd_str, NULL);
-  launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_NONE);
+  launcher = g_subprocess_launcher_new (flags);
   g_subprocess_launcher_take_fd (launcher, basic_pipefds[1], basic_pipefds[1]);
   g_subprocess_launcher_take_fd (launcher, needdup_pipefds[1], needdup_pipefds[1] + 1);
+  if (child_setup != NULL)
+    g_subprocess_launcher_set_child_setup (launcher, child_setup, NULL, NULL);
   proc = g_subprocess_launcher_spawnv (launcher, (const gchar * const *) args->pdata, error);
   g_ptr_array_free (args, TRUE);
   g_assert_no_error (local_error);
@@ -1754,6 +1757,29 @@ test_pass_fd (void)
   g_object_unref (proc);
 }
 
+static void
+empty_child_setup (gpointer user_data)
+{
+}
+
+static void
+test_pass_fd (void)
+{
+  /* Using a child setup function forces gspawn to use fork/exec
+   * rather than posix_spawn.
+   */
+  do_test_pass_fd (G_SUBPROCESS_FLAGS_NONE, empty_child_setup);
+
+  /* Try to test the optimized posix_spawn codepath instead of
+   * fork/exec. Currently this requires using INHERIT_FDS since gspawn's
+   * posix_spawn codepath does not currently handle closing
+   * non-inherited fds. Note that using INHERIT_FDS means we don't
+   * fully test g_subprocess_launcher_take_fd() as we do when using
+   * G_SUBPROCESS_FLAGS_NONE.
+   */
+  do_test_pass_fd (G_SUBPROCESS_FLAGS_INHERIT_FDS, NULL);
+}
+
 #endif
 
 static void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]