Skip to content

Commit 8a89ab6

Browse files
Piotr LesnickiRalph Castain
authored andcommitted
odls: close only used file descriptors at fork/exec
(cherry picked from commit open-mpi/ompi@1dd5487) (cherry picked from commit open-mpi/ompi@c167acc) (cherry picked from commit open-mpi/ompi@92ae386) (cherry picked from commit open-mpi/ompi@f284487)
1 parent 9900543 commit 8a89ab6

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

orte/mca/odls/default/odls_default_module.c

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Copyright (c) 2010 IBM Corporation. All rights reserved.
1616
* Copyright (c) 2011-2013 Los Alamos National Security, LLC. All rights
1717
* reserved.
18-
* Copyright (c) 2013-2014 Intel, Inc. All rights reserved
18+
* Copyright (c) 2013-2015 Intel, Inc. All rights reserved
1919
*
2020
* $COPYRIGHT$
2121
*
@@ -101,6 +101,10 @@
101101
#ifdef HAVE_SYS_SELECT_H
102102
#include <sys/select.h>
103103
#endif
104+
#ifdef HAVE_DIRENT_H
105+
#include <dirent.h>
106+
#endif
107+
#include <ctype.h>
104108

105109
#include "opal/mca/hwloc/hwloc.h"
106110
#include "opal/mca/hwloc/base/base.h"
@@ -350,6 +354,33 @@ static void send_error_show_help(int fd, int exit_status,
350354
exit(exit_status);
351355
}
352356

357+
/* close all open file descriptors w/ exception of stdin/stdout/stderr,
358+
the pipe used for the IOF INTERNAL messages, and the pipe up to
359+
the parent. */
360+
static int close_open_file_descriptors(int write_fd,
361+
orte_iof_base_io_conf_t opts) {
362+
DIR *dir = opendir("/proc/self/fd");
363+
if (NULL == dir) {
364+
return ORTE_ERR_FILE_OPEN_FAILURE;
365+
}
366+
struct dirent *files;
367+
while (NULL != (files = readdir(dir))) {
368+
if (!isdigit(files->d_name[0])) {
369+
continue;
370+
}
371+
int fd = strtol(files->d_name, NULL, 10);
372+
if (errno == EINVAL || errno == ERANGE) {
373+
closedir(dir);
374+
return ORTE_ERR_TYPE_MISMATCH;
375+
}
376+
if (fd >=3 && fd != opts.p_internal[1] && fd != write_fd) {
377+
close(fd);
378+
}
379+
}
380+
closedir(dir);
381+
return ORTE_SUCCESS;
382+
}
383+
353384
static int do_child(orte_app_context_t* context,
354385
orte_proc_t *child,
355386
char **environ_copy,
@@ -427,12 +458,15 @@ static int do_child(orte_app_context_t* context,
427458
opal_unsetenv(param, &environ_copy);
428459
free(param);
429460

430-
/* close all file descriptors w/ exception of stdin/stdout/stderr,
461+
/* close all open file descriptors w/ exception of stdin/stdout/stderr,
431462
the pipe used for the IOF INTERNAL messages, and the pipe up to
432463
the parent. */
433-
for(fd=3; fd<fdmax; fd++) {
434-
if (fd != opts.p_internal[1] && fd != write_fd) {
435-
close(fd);
464+
if (ORTE_SUCCESS != close_open_file_descriptors(write_fd, opts)) {
465+
// close *all* file descriptors -- slow
466+
for(fd=3; fd<fdmax; fd++) {
467+
if (fd != opts.p_internal[1] && fd != write_fd) {
468+
close(fd);
469+
}
436470
}
437471
}
438472

0 commit comments

Comments
 (0)