1
1

opal/util/fd.c: add new convenience function for setting FD_CLOEXEC

Paul Hargrove pointed out that Stevens tells us that we should
FD_GETFL before FD_SETFL.  And so we shall.

Make a new convenience function to do this (opal_fd_set_cloexec()),
just so that we don't have to litter this 2-step process throughout
the code.

Refs trac:4550

This commit was SVN r31513.

The following Trac tickets were found above:
  Ticket 4550 --> https://svn.open-mpi.org/trac/ompi/ticket/4550
Этот коммит содержится в:
Jeff Squyres 2014-04-24 13:04:49 +00:00
родитель 410f5bfb91
Коммит e1655ae68d
5 изменённых файлов: 39 добавлений и 4 удалений

Просмотреть файл

@ -69,3 +69,22 @@ int opal_fd_write(int fd, int len, const void *buffer)
} }
int opal_fd_set_cloexec(int fd)
{
#ifdef FD_CLOEXEC
int flags;
/* Stevens says that we should get the fd's flags before we set
them. So say we all. */
flags = fcntl(sd, F_GETFD, 0);
if (-1 == flags) {
return OPAL_ERR_IN_ERRNO;
}
if (fcntl(sd, F_SETFD, FD_CLOEXEC | flags) == -1) {
return OPAL_ERR_IN_ERRNO;
}
#endif
return OPAL_SUCCESS;
}

Просмотреть файл

@ -49,6 +49,20 @@ OPAL_DECLSPEC int opal_fd_read(int fd, int len, void *buffer);
*/ */
OPAL_DECLSPEC int opal_fd_write(int fd, int len, const void *buffer); OPAL_DECLSPEC int opal_fd_write(int fd, int len, const void *buffer);
/**
* Convenience function to set a file descriptor to be close-on-exec.
*
* @param fd File descriptor
*
* @returns OPAL_SUCCESS upon success (or if the system does not
* support close-on-exec behavior).
* @returns OPAL_ERR_IN_ERRNO otherwise.
*
* This is simply a convenience function because there's a few steps
* to setting a file descriptor to be close-on-exec.
*/
OPAL_DECLSPEC int opal_fd_set_cloexec(int fd);
END_C_DECLS END_C_DECLS
#endif #endif

Просмотреть файл

@ -419,7 +419,7 @@ static int do_child(orte_app_context_t* context,
} }
/* Setup the pipe to be close-on-exec */ /* Setup the pipe to be close-on-exec */
fcntl(write_fd, F_SETFD, FD_CLOEXEC); opal_fd_set_cloexec(write_fd);
if (NULL != child) { if (NULL != child) {
/* setup stdout/stderr so that any error messages that we /* setup stdout/stderr so that any error messages that we

Просмотреть файл

@ -55,6 +55,7 @@
#include "opal/util/if.h" #include "opal/util/if.h"
#include "opal/util/net.h" #include "opal/util/net.h"
#include "opal/util/argv.h" #include "opal/util/argv.h"
#include "opal/util/fd.h"
#include "opal/class/opal_hash_table.h" #include "opal/class/opal_hash_table.h"
#include "opal/class/opal_list.h" #include "opal/class/opal_list.h"
@ -132,8 +133,8 @@ int orte_oob_tcp_start_listening(void)
/* Make sure the pipe FDs are set to close-on-exec so that /* Make sure the pipe FDs are set to close-on-exec so that
they don't leak into children */ they don't leak into children */
if (fcntl(mca_oob_tcp_component.stop_thread[0], F_SETFD, FD_CLOEXEC) == -1 || if (opal_fd_set_cloexec(mca_oob_tcp_component.stop_thread[0]) != OPAL_SUCCESS ||
fcntl(mca_oob_tcp_component.stop_thread[1], F_SETFD, FD_CLOEXEC) == -1) { opal_fd_set_cloexec(mca_oob_tcp_component.stop_thread[1]) != OPAL_SUCCESS) {
close(mca_oob_tcp_component.stop_thread[0]); close(mca_oob_tcp_component.stop_thread[0]);
close(mca_oob_tcp_component.stop_thread[1]); close(mca_oob_tcp_component.stop_thread[1]);
ORTE_ERROR_LOG(ORTE_ERR_IN_ERRNO); ORTE_ERROR_LOG(ORTE_ERR_IN_ERRNO);

Просмотреть файл

@ -69,6 +69,7 @@
#include "opal/util/opal_environ.h" #include "opal/util/opal_environ.h"
#include "opal/util/opal_getcwd.h" #include "opal/util/opal_getcwd.h"
#include "opal/util/show_help.h" #include "opal/util/show_help.h"
#include "opal/util/fd.h"
#include "opal/sys/atomic.h" #include "opal/sys/atomic.h"
#if OPAL_ENABLE_FT_CR == 1 #if OPAL_ENABLE_FT_CR == 1
#include "opal/runtime/opal_cr.h" #include "opal/runtime/opal_cr.h"
@ -3001,7 +3002,7 @@ static void open_fifo (void)
} }
/* Set this fd to be close-on-exec so that children don't see it */ /* Set this fd to be close-on-exec so that children don't see it */
if (fcntl(attach_fd, F_SETFD, FD_CLOEXEC) == -1) { if (opal_fd_set_cloexec(attach_fd) != OPAL_SUCCESS) {
opal_output(0, "%s unable to set debugger attach fifo to CLOEXEC", opal_output(0, "%s unable to set debugger attach fifo to CLOEXEC",
ORTE_NAME_PRINT(ORTE_PROC_MY_NAME)); ORTE_NAME_PRINT(ORTE_PROC_MY_NAME));
close(attach_fd); close(attach_fd);