gspawn: treat all negative fds as unset

Philip Withnall suggests that glib should treat all negative
file descriptors as unset/invalid, rather than explicitly requiring
them to be -1.

This can simplify the logic for some users of this code.

https://gitlab.gnome.org/GNOME/gnome-shell/merge_requests/132
This commit is contained in:
Daniel Drake 2018-06-29 11:38:53 -05:00
parent 95082691d2
commit 16c3409888
2 changed files with 15 additions and 9 deletions

View File

@ -843,12 +843,12 @@ g_spawn_async_with_fds (const gchar *working_directory,
GError **error)
{
g_return_val_if_fail (argv != NULL, FALSE);
g_return_val_if_fail (stdout_fd == -1 ||
g_return_val_if_fail (stdout_fd < 0 ||
!(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
g_return_val_if_fail (stderr_fd == -1 ||
g_return_val_if_fail (stderr_fd < 0 ||
!(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
/* can't inherit stdin if we have an input pipe. */
g_return_val_if_fail (stdin_fd == -1 ||
g_return_val_if_fail (stdin_fd < 0 ||
!(flags & G_SPAWN_CHILD_INHERITS_STDIN), FALSE);
return fork_exec_with_fds (!(flags & G_SPAWN_DO_NOT_REAP_CHILD),

View File

@ -183,13 +183,15 @@ test_spawn_async_with_fds (void)
/* Each test has 3 variable parameters: stdin, stdout, stderr */
enum fd_type {
NO_FD, /* don't pass a fd */
NO_FD, /* pass fd -1 (unset) */
FD_NEGATIVE, /* pass fd of negative value (equivalent to unset) */
PIPE, /* pass fd of new/unique pipe */
STDOUT_PIPE, /* pass the same pipe as stdout */
} tests[][3] = {
{ NO_FD, NO_FD, NO_FD }, /* Test with no fds passed */
{ PIPE, PIPE, PIPE }, /* Test with unique fds passed */
{ NO_FD, PIPE, STDOUT_PIPE }, /* Test the same fd for stdout + stderr */
{ NO_FD, NO_FD, NO_FD }, /* Test with no fds passed */
{ NO_FD, FD_NEGATIVE, NO_FD }, /* Test another negative fd value */
{ PIPE, PIPE, PIPE }, /* Test with unique fds passed */
{ NO_FD, PIPE, STDOUT_PIPE }, /* Test the same fd for stdout + stderr */
};
arg = g_strdup_printf ("thread %d", tnum);
@ -220,6 +222,10 @@ test_spawn_async_with_fds (void)
test_pipe[j][0] = -1;
test_pipe[j][1] = -1;
break;
case FD_NEGATIVE:
test_pipe[j][0] = -5;
test_pipe[j][1] = -5;
break;
case PIPE:
#ifdef G_OS_UNIX
g_unix_open_pipe (test_pipe[j], FD_CLOEXEC, &error);
@ -261,7 +267,7 @@ test_spawn_async_with_fds (void)
g_source_attach (source, context);
g_source_unref (source);
if (test_pipe[1][0] != -1)
if (test_pipe[1][0] >= 0)
{
channel = g_io_channel_unix_new (test_pipe[1][0]);
source = g_io_create_watch (channel, G_IO_IN | G_IO_HUP | G_IO_ERR);
@ -280,7 +286,7 @@ test_spawn_async_with_fds (void)
g_assert_true (data.child_exited);
if (test_pipe[1][0] != -1)
if (test_pipe[1][0] >= 0)
{
/* Check for echo on stdout */
g_assert_true (data.stdout_done);