Added to brook option for not redirecting child's STDIN/STDOUT to /dev/null. prototype
authorStanislaw Klekot <dozzie@jarowit.net>
Mon, 7 Aug 2017 21:24:59 +0000 (23:24 +0200)
committerStanislaw Klekot <dozzie@jarowit.net>
Mon, 7 Aug 2017 21:24:59 +0000 (23:24 +0200)
This also solves the bug for when child process was receiving data with STDOUT
redirected to /dev/null (wrong open mode for /dev/null handle).

bin/brook

index aeb8663..bbf5e9c 100755 (executable)
--- a/bin/brook
+++ b/bin/brook
@@ -57,6 +57,11 @@ parser.add_option(
     #help = "(default: 127.0.0.1:5268)",
     metavar = "HOST:PORT",
 )
+parser.add_option(
+    "--stdio", dest = "close_stdio", action = "store_false", default = True,
+    #help = "keep STDIN/STDOUT open in child",
+    metavar = "HOST:PORT",
+)
 
 (options, command) = parser.parse_args()
 if len(command) == 0:
@@ -381,15 +386,30 @@ if command is None: # STDIO mode
         writefh = WriteHandle(sys.stdout)
         sys.stdin.close()
 else:
-    devnull = open("/dev/null", "rw")
+    if options.sub_chan is not None:
+        stdin = subprocess.PIPE
+    elif options.close_stdio:
+        stdin = open("/dev/null", "r")
+    else:
+        stdin = None
+    if options.pub_chan is not None:
+        stdout = subprocess.PIPE
+    elif options.close_stdio:
+        stdout = open("/dev/null", "w")
+    else:
+        stdout = None
+
     child_process = subprocess.Popen(
         command,
         shell = False,
         close_fds = True,
-        stdin  = subprocess.PIPE if options.sub_chan is not None else devnull,
-        stdout = subprocess.PIPE if options.pub_chan is not None else devnull,
+        stdin  = stdin,
+        stdout = stdout,
     )
-    devnull.close()
+    if stdin is not None and stdin is not subprocess.PIPE:
+        stdin.close()
+    if stdout is not None and stdout is not subprocess.PIPE:
+        stdout.close()
     if child_process.stdout is not None:
         readfh = ReadHandle(child_process.stdout)
     if child_process.stdin is not None: