spawn-unix: Move _dbus_unix_make_pipe() to common code

I want to use this in a test-case.

Note that this changes the error code used if pipe() fails (which in
practice should not happen) from DBUS_ERROR_SPAWN_FAILED to some
reasonable interpretation of errno.

Signed-off-by: Simon McVittie <smcv@collabora.com>
This commit is contained in:
Simon McVittie 2023-11-30 14:56:32 +00:00
parent 76380efdbd
commit 256b9dbe1e
3 changed files with 47 additions and 43 deletions

View file

@ -922,48 +922,6 @@ close_and_invalidate (int *fd)
return ret;
}
static dbus_bool_t
make_pipe (int p[2],
DBusError *error)
{
int retval;
#ifdef HAVE_PIPE2
dbus_bool_t cloexec_done;
retval = pipe2 (p, O_CLOEXEC);
cloexec_done = retval >= 0;
/* Check if kernel seems to be too old to know pipe2(). We assume
that if pipe2 is available, O_CLOEXEC is too. */
if (retval < 0 && errno == ENOSYS)
#endif
{
retval = pipe(p);
}
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
if (retval < 0)
{
dbus_set_error (error,
DBUS_ERROR_SPAWN_FAILED,
"Failed to create pipe for communicating with child process (%s)",
_dbus_strerror (errno));
return FALSE;
}
#ifdef HAVE_PIPE2
if (!cloexec_done)
#endif
{
_dbus_fd_set_close_on_exec (p[0]);
_dbus_fd_set_close_on_exec (p[1]);
}
return TRUE;
}
static void
do_write (int fd, const void *buf, size_t count)
{
@ -1317,7 +1275,7 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p,
goto cleanup_and_fail;
}
if (!make_pipe (child_err_report_pipe, error))
if (!_dbus_unix_make_pipe (child_err_report_pipe, error))
goto cleanup_and_fail;
if (!_dbus_socketpair (&babysitter_pipe[0], &babysitter_pipe[1], TRUE, error))

View file

@ -5273,4 +5273,46 @@ _dbus_get_low_level_socket_errno (void)
return errno;
}
dbus_bool_t
_dbus_unix_make_pipe (int p[2],
DBusError *error)
{
int retval;
#ifdef HAVE_PIPE2
dbus_bool_t cloexec_done;
retval = pipe2 (p, O_CLOEXEC);
cloexec_done = retval >= 0;
/* Check if kernel seems to be too old to know pipe2(). We assume
that if pipe2 is available, O_CLOEXEC is too. */
if (retval < 0 && errno == ENOSYS)
#endif
{
retval = pipe(p);
}
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
if (retval < 0)
{
dbus_set_error (error,
_dbus_error_from_errno (errno),
"Failed to create pipe (%s)",
_dbus_strerror (errno));
return FALSE;
}
#ifdef HAVE_PIPE2
if (!cloexec_done)
#endif
{
_dbus_fd_set_close_on_exec (p[0]);
_dbus_fd_set_close_on_exec (p[1]);
}
return TRUE;
}
/* tests in dbus-sysdeps-util.c */

View file

@ -162,6 +162,10 @@ void _dbus_set_signal_handler (int sig,
dbus_bool_t _dbus_reset_oom_score_adj (const char **error_str_p);
DBUS_PRIVATE_EXPORT
dbus_bool_t _dbus_unix_make_pipe (int p[2],
DBusError *error);
/** @} */
DBUS_END_DECLS