Skip to content

Commit 1082c9d

Browse files
committed
Do not close-on-exec standard streams
When disabling inheritance. Closes r-lib/callr#236
1 parent 0a06c32 commit 1082c9d

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

src/client.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ SEXP processx_write(SEXP fd, SEXP data) {
102102
#include <fcntl.h>
103103
#include <errno.h>
104104
#include <unistd.h>
105+
#include <stdlib.h>
105106

106107
#include <R_ext/Rdynload.h>
107108
#include <Rinternals.h>
@@ -130,8 +131,13 @@ SEXP processx_disable_inheritance() {
130131

131132
/* Set the CLOEXEC flag on all open descriptors. Unconditionally try the
132133
* first 16 file descriptors. After that, bail out after the first error.
133-
*/
134-
for (fd = 0; ; fd++) {
134+
* We skip the standard streams, because R and `system()` is not prepared
135+
* to not inheriting stdin and eg. an R subprocess does not even start in
136+
* system(). See https://github.com/r-lib/callr/issues/236. */
137+
138+
int firstfd = 3;
139+
if (getenv("PROCESSX_CLOEXEC_STDIO")) firstfd = 0;
140+
for (fd = firstfd; ; fd++) {
135141
if (processx__cloexec_fcntl(fd, 1) && fd > 15) break;
136142
}
137143

src/processx-connection.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <errno.h>
77
#include <sys/types.h>
88
#include <unistd.h>
9+
#include <stdlib.h>
910

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

732733
/* Set the CLOEXEC flag on all open descriptors. Unconditionally try the
733734
* first 16 file descriptors. After that, bail out after the first error.
734-
*/
735-
for (fd = 0; ; fd++) {
735+
* We skip the standard streams, because R and `system()` is not prepared
736+
* to not inheriting stdin and eg. an R subprocess does not even start in
737+
* system(). See https://github.com/r-lib/callr/issues/236. */
738+
739+
int firstfd = 3;
740+
if (getenv("PROCESSX_CLOEXEC_STDIO")) firstfd = 0;
741+
for (fd = firstfd; ; fd++) {
736742
if (processx__cloexec_fcntl(fd, 1) && fd > 15) break;
737743
}
738744

0 commit comments

Comments
 (0)