From 35880582cac11a312f48fe83b665f9e117558cbd Mon Sep 17 00:00:00 2001 From: Chengwei Yang Date: Tue, 10 Sep 2013 22:29:19 +0800 Subject: [PATCH 1/2] Check EINVAL for socketpair and retry without SOCK_CLOEXEC As the same as _dbus_open_socket() and _dbus_full_duplex_pipe(), socketpair() may fail with EINVAL if call with SOCK_CLOEXEC. Check for the failure and retry without SOCK_CLOEXEC, in addition, only call _dbus_fd_set_close_on_exec() if the socketpair failure happened. --- dbus/dbus-sysdeps-unix.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index 1cb4a58..1face5c 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -892,16 +892,24 @@ _dbus_connect_exec (const char *path, { int fds[2]; pid_t pid; + int retval; + dbus_bool_t cloexec_done = 0; _DBUS_ASSERT_ERROR_IS_CLEAR (error); _dbus_verbose ("connecting to process %s\n", path); - if (socketpair (AF_UNIX, SOCK_STREAM #ifdef SOCK_CLOEXEC - |SOCK_CLOEXEC + retval = socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, fds); + cloexec_done = retval >= 0; + + if (retval < 0 && (errno == EINVAL)) #endif - , 0, fds) < 0) + { + retval = socketpair(AF_UNIX, SOCK_STREAM, 0, fds); + } + + if (retval < 0) { dbus_set_error (error, _dbus_error_from_errno (errno), @@ -910,8 +918,11 @@ _dbus_connect_exec (const char *path, return -1; } - _dbus_fd_set_close_on_exec (fds[0]); - _dbus_fd_set_close_on_exec (fds[1]); + if (!cloexec_done) + { + _dbus_fd_set_close_on_exec (fds[0]); + _dbus_fd_set_close_on_exec (fds[1]); + } pid = fork (); if (pid < 0) -- 1.7.9.5