util.pposix: Use freopen(3) when replacing standard IO streams (thanks ratchetfreak)
authorKim Alvefur <zash@zash.se>
Thu, 12 Jul 2018 20:42:22 +0200
changeset 9031 d4c2a3060e7e
parent 9030 4028eb4a9f7f
child 9032 c5e738b54d77
util.pposix: Use freopen(3) when replacing standard IO streams (thanks ratchetfreak) Man page says this is what freopen(3) is for.
util-src/pposix.c
--- a/util-src/pposix.c	Thu Jul 12 02:18:46 2018 +0200
+++ b/util-src/pposix.c	Thu Jul 12 20:42:22 2018 +0200
@@ -104,14 +104,10 @@
 		return 2;
 	}
 
-	/* Close stdin, stdout, stderr */
-	close(0);
-	close(1);
-	close(2);
 	/* Make sure accidental use of FDs 0, 1, 2 don't cause weirdness */
-	open("/dev/null", O_RDONLY);
-	open("/dev/null", O_WRONLY);
-	open("/dev/null", O_WRONLY);
+	stdin = freopen("/dev/null", "r", stdin);
+	stdout = freopen("/dev/null", "w", stdout);
+	stderr = freopen("/dev/null", "w", stderr);
 
 	/* Final fork, use it wisely */
 	if(fork()) {