Skip to content

Commit

Permalink
Do not close-on-exec standard streams
Browse files Browse the repository at this point in the history
When disabling inheritance.

Closes r-lib/callr#236
  • Loading branch information
gaborcsardi committed Oct 8, 2022
1 parent 0a06c32 commit 1082c9d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ SEXP processx_write(SEXP fd, SEXP data) {
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>

#include <R_ext/Rdynload.h>
#include <Rinternals.h>
Expand Down Expand Up @@ -130,8 +131,13 @@ SEXP processx_disable_inheritance() {

/* Set the CLOEXEC flag on all open descriptors. Unconditionally try the
* first 16 file descriptors. After that, bail out after the first error.
*/
for (fd = 0; ; fd++) {
* We skip the standard streams, because R and `system()` is not prepared
* to not inheriting stdin and eg. an R subprocess does not even start in
* system(). See https://github.com/r-lib/callr/issues/236. */

int firstfd = 3;
if (getenv("PROCESSX_CLOEXEC_STDIO")) firstfd = 0;
for (fd = firstfd; ; fd++) {
if (processx__cloexec_fcntl(fd, 1) && fd > 15) break;
}

Expand Down
10 changes: 8 additions & 2 deletions src/processx-connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>

#ifndef _WIN32
#include <sys/uio.h>
Expand Down Expand Up @@ -731,8 +732,13 @@ SEXP processx_connection_disable_inheritance() {

/* Set the CLOEXEC flag on all open descriptors. Unconditionally try the
* first 16 file descriptors. After that, bail out after the first error.
*/
for (fd = 0; ; fd++) {
* We skip the standard streams, because R and `system()` is not prepared
* to not inheriting stdin and eg. an R subprocess does not even start in
* system(). See https://github.com/r-lib/callr/issues/236. */

int firstfd = 3;
if (getenv("PROCESSX_CLOEXEC_STDIO")) firstfd = 0;
for (fd = firstfd; ; fd++) {
if (processx__cloexec_fcntl(fd, 1) && fd > 15) break;
}

Expand Down

0 comments on commit 1082c9d

Please sign in to comment.