Automatic date update in version.in
[deliverable/binutils-gdb.git] / libiberty / pex-common.c
index 7f355365edafd192fa2a0d46f8a733ba95fda2ad..5f3a9628ed68a7e7dfdb8b5df8a2d6fb5fcb02f7 100644 (file)
@@ -1,5 +1,5 @@
 /* Common code for executing a program in a sub-process.
-   Copyright (C) 2005 Free Software Foundation, Inc.
+   Copyright (C) 2005-2019 Free Software Foundation, Inc.
    Written by Ian Lance Taylor <ian@airs.com>.
 
 This file is part of the libiberty library.
@@ -160,7 +160,7 @@ pex_run_in_environment (struct pex_obj *obj, int flags, const char *executable,
   int outname_allocated;
   int p[2];
   int toclose;
-  long pid;
+  pid_t pid;
 
   in = -1;
   out = -1;
@@ -267,7 +267,8 @@ pex_run_in_environment (struct pex_obj *obj, int flags, const char *executable,
   if (out < 0)
     {
       out = obj->funcs->open_write (obj, outname,
-                                   (flags & PEX_BINARY_OUTPUT) != 0);
+                                   (flags & PEX_BINARY_OUTPUT) != 0,
+                                   (flags & PEX_STDOUT_APPEND) != 0);
       if (out < 0)
        {
          *err = errno;
@@ -319,8 +320,9 @@ pex_run_in_environment (struct pex_obj *obj, int flags, const char *executable,
     }
   else
     {
-      errdes = obj->funcs->open_write (obj, errname, 
-                                      (flags & PEX_BINARY_ERROR) != 0);
+      errdes = obj->funcs->open_write (obj, errname,
+                                      (flags & PEX_BINARY_ERROR) != 0,
+                                      (flags & PEX_STDERR_APPEND) != 0);
       if (errdes < 0)
        {
          *err = errno;
@@ -345,7 +347,7 @@ pex_run_in_environment (struct pex_obj *obj, int flags, const char *executable,
     goto error_exit;
 
   ++obj->count;
-  obj->children = XRESIZEVEC (long, obj->children, obj->count);
+  obj->children = XRESIZEVEC (pid_t, obj->children, obj->count);
   obj->children[obj->count - 1] = pid;
 
   return NULL;
@@ -505,6 +507,7 @@ pex_read_err (struct pex_obj *obj, int binary)
   if (o < 0 || o == STDIN_FILE_NO)
     return NULL;
   obj->read_err = obj->funcs->fdopenr (obj, o, binary);
+  obj->stderr_pipe = -1;
   return obj->read_err;    
 }
 
@@ -597,8 +600,17 @@ pex_get_times (struct pex_obj *obj, int count, struct pex_time *vector)
 void
 pex_free (struct pex_obj *obj)
 {
+  /* Close pipe file descriptors corresponding to child's stdout and
+     stderr so that the child does not hang trying to output something
+     while we're waiting for it.  */
   if (obj->next_input >= 0 && obj->next_input != STDIN_FILE_NO)
     obj->funcs->close (obj, obj->next_input);
+  if (obj->stderr_pipe >= 0 && obj->stderr_pipe != STDIN_FILE_NO)
+    obj->funcs->close (obj, obj->stderr_pipe);
+  if (obj->read_output != NULL)
+    fclose (obj->read_output);
+  if (obj->read_err != NULL)
+    fclose (obj->read_err);
 
   /* If the caller forgot to wait for the children, we do it here, to
      avoid zombies.  */
@@ -613,16 +625,9 @@ pex_free (struct pex_obj *obj)
 
   if (obj->next_input_name_allocated)
     free (obj->next_input_name);
-  if (obj->children != NULL)
-    free (obj->children);
-  if (obj->status != NULL)
-    free (obj->status);
-  if (obj->time != NULL)
-    free (obj->time);
-  if (obj->read_output != NULL)
-    fclose (obj->read_output);
-  if (obj->read_err != NULL)
-    fclose (obj->read_err);
+  free (obj->children);
+  free (obj->status);
+  free (obj->time);
 
   if (obj->remove_count > 0)
     {
This page took 0.024539 seconds and 4 git commands to generate.