shithub: trueawk

Download patch

ref: 5a18f63b8dfc35fb7bcda4688661e354783d2bb7
parent: de6284e0377e1c10f6249586df1a67311e9c5b2f
author: Arnold D. Robbins <arnold@skeeve.com>
date: Tue Jan 21 21:10:59 EST 2020

Set the close-on-exec flag for file and pipe redirections.

--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2020-01-20         Arnold D. Robbins     <arnold@skeeve.com>
+
+	* run.c (openfile): Set the close-on-exec flag for file
+	and pipe redirections that aren't stdin/stdout/stderr.
+
 2020-01-06         Arnold D. Robbins     <arnold@skeeve.com>
 
 	Minor fixes.
--- a/TODO
+++ b/TODO
@@ -1,4 +1,4 @@
-Fri Jan 17 14:04:29 IST 2020
+Wed Jan 22 02:10:35 MST 2020
 ============================
 
 Here are some things that it'd be nice to have volunteer
@@ -17,6 +17,3 @@
    a test suite target that runs valgrind on all the tests and
    reports if there are any definite losses or any invalid reads
    or writes (similar to gawk's test of this nature).
-
-4. Set the "close on exec" flag for file and pipe redirection
-   file descriptors.
--- a/run.c
+++ b/run.c
@@ -25,6 +25,7 @@
 #define DEBUG
 #include <stdio.h>
 #include <ctype.h>
+#include <fcntl.h>
 #include <setjmp.h>
 #include <limits.h>
 #include <math.h>
@@ -1744,6 +1745,8 @@
 		files[i].fname = tostring(s);
 		files[i].fp = fp;
 		files[i].mode = m;
+		if (fp != stdin && fp != stdout && fp != stderr)
+			(void) fcntl(fileno(fp), F_SETFD, FD_CLOEXEC);
 	}
 	return fp;
 }
--