|
15 | 15 | * Copyright (c) 2010 IBM Corporation. All rights reserved. |
16 | 16 | * Copyright (c) 2011-2013 Los Alamos National Security, LLC. All rights |
17 | 17 | * reserved. |
18 | | - * Copyright (c) 2013-2014 Intel, Inc. All rights reserved |
| 18 | + * Copyright (c) 2013-2015 Intel, Inc. All rights reserved |
19 | 19 | * |
20 | 20 | * $COPYRIGHT$ |
21 | 21 | * |
|
101 | 101 | #ifdef HAVE_SYS_SELECT_H |
102 | 102 | #include <sys/select.h> |
103 | 103 | #endif |
| 104 | +#ifdef HAVE_DIRENT_H |
| 105 | +#include <dirent.h> |
| 106 | +#endif |
| 107 | +#include <ctype.h> |
104 | 108 |
|
105 | 109 | #include "opal/mca/hwloc/hwloc.h" |
106 | 110 | #include "opal/mca/hwloc/base/base.h" |
@@ -350,6 +354,33 @@ static void send_error_show_help(int fd, int exit_status, |
350 | 354 | exit(exit_status); |
351 | 355 | } |
352 | 356 |
|
| 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 | + |
353 | 384 | static int do_child(orte_app_context_t* context, |
354 | 385 | orte_proc_t *child, |
355 | 386 | char **environ_copy, |
@@ -427,12 +458,15 @@ static int do_child(orte_app_context_t* context, |
427 | 458 | opal_unsetenv(param, &environ_copy); |
428 | 459 | free(param); |
429 | 460 |
|
430 | | - /* close all file descriptors w/ exception of stdin/stdout/stderr, |
| 461 | + /* close all open file descriptors w/ exception of stdin/stdout/stderr, |
431 | 462 | the pipe used for the IOF INTERNAL messages, and the pipe up to |
432 | 463 | 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 | + } |
436 | 470 | } |
437 | 471 | } |
438 | 472 |
|
|
0 commit comments