summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Catanzaro <mcatanzaro@gnome.org>2021-02-25 12:21:38 -0600
committerMichael Catanzaro <mcatanzaro@redhat.com>2021-09-20 13:01:33 -0500
commitd53d7f6105c71c5d8510b64d249a68d2b3137422 (patch)
tree4c0736f7bcf8f8cc4a0ebafc29215a77c7016199
parent1699ca4332d09cab71136d2d17135a005fc121bf (diff)
downloadglib-mcatanzaro/posix-spawn2.tar.gz
gsubprocess: ensure we test fd remapping on the posix_spawn codepathmcatanzaro/posix-spawn2
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.
-rw-r--r--gio/tests/gsubprocess.c30
1 files changed, 28 insertions, 2 deletions
diff --git a/gio/tests/gsubprocess.c b/gio/tests/gsubprocess.c
index 084b77df3..21be47826 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 our testing of
+ * g_subprocess_launcher_take_fd() is less-comprehensive than when
+ * using G_SUBPROCESS_FLAGS_NONE.
+ */
+ do_test_pass_fd (G_SUBPROCESS_FLAGS_INHERIT_FDS, NULL);
+}
+
#endif
static void