Back to index

cell-binutils  2.17cvs20070401
pex-common.c
Go to the documentation of this file.
00001 /* Common code for executing a program in a sub-process.
00002    Copyright (C) 2005 Free Software Foundation, Inc.
00003    Written by Ian Lance Taylor <ian@airs.com>.
00004 
00005 This file is part of the libiberty library.
00006 Libiberty is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU Library General Public
00008 License as published by the Free Software Foundation; either
00009 version 2 of the License, or (at your option) any later version.
00010 
00011 Libiberty is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 Library General Public License for more details.
00015 
00016 You should have received a copy of the GNU Library General Public
00017 License along with libiberty; see the file COPYING.LIB.  If not,
00018 write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
00019 Boston, MA 02110-1301, USA.  */
00020 
00021 #include "config.h"
00022 #include "libiberty.h"
00023 #include "pex-common.h"
00024 
00025 #include <stdio.h>
00026 #include <errno.h>
00027 #ifdef NEED_DECLARATION_ERRNO
00028 extern int errno;
00029 #endif
00030 #ifdef HAVE_STDLIB_H
00031 #include <stdlib.h>
00032 #endif
00033 #ifdef HAVE_STRING_H
00034 #include <string.h>
00035 #endif
00036 #ifdef HAVE_UNISTD_H
00037 #include <unistd.h>
00038 #endif
00039 
00040 extern int mkstemps (char *, int);
00041 
00042 /* This file contains subroutines for the program execution routines
00043    (pex_init, pex_run, etc.).  This file is compiled on all
00044    systems.  */
00045 
00046 static void pex_add_remove (struct pex_obj *, const char *, int);
00047 static int pex_get_status_and_time (struct pex_obj *, int, const char **,
00048                                 int *);
00049 
00050 /* Initialize a pex_obj structure.  */
00051 
00052 struct pex_obj *
00053 pex_init_common (int flags, const char *pname, const char *tempbase,
00054                const struct pex_funcs *funcs)
00055 {
00056   struct pex_obj *obj;
00057 
00058   obj = XNEW (struct pex_obj);
00059   obj->flags = flags;
00060   obj->pname = pname;
00061   obj->tempbase = tempbase;
00062   obj->next_input = STDIN_FILE_NO;
00063   obj->next_input_name = NULL;
00064   obj->next_input_name_allocated = 0;
00065   obj->stderr_pipe = -1;
00066   obj->count = 0;
00067   obj->children = NULL;
00068   obj->status = NULL;
00069   obj->time = NULL;
00070   obj->number_waited = 0;
00071   obj->input_file = NULL;
00072   obj->read_output = NULL;
00073   obj->read_err = NULL;
00074   obj->remove_count = 0;
00075   obj->remove = NULL;
00076   obj->funcs = funcs;
00077   obj->sysdep = NULL;
00078   return obj;
00079 }
00080 
00081 /* Add a file to be removed when we are done.  */
00082 
00083 static void
00084 pex_add_remove (struct pex_obj *obj, const char *name, int allocated)
00085 {
00086   char *add;
00087 
00088   ++obj->remove_count;
00089   obj->remove = XRESIZEVEC (char *, obj->remove, obj->remove_count);
00090   if (allocated)
00091     add = (char *) name;
00092   else
00093     add = xstrdup (name);
00094   obj->remove[obj->remove_count - 1] = add;
00095 }
00096 
00097 /* Generate a temporary file name based on OBJ, FLAGS, and NAME.
00098    Return NULL if we were unable to reserve a temporary filename.
00099 
00100    If non-NULL, the result is either allocated with malloc, or the
00101    same pointer as NAME.  */
00102 static char *
00103 temp_file (struct pex_obj *obj, int flags, char *name)
00104 {
00105   if (name == NULL)
00106     {
00107       if (obj->tempbase == NULL)
00108         {
00109           name = make_temp_file (NULL);
00110         }
00111       else
00112         {
00113           int len = strlen (obj->tempbase);
00114           int out;
00115 
00116           if (len >= 6
00117               && strcmp (obj->tempbase + len - 6, "XXXXXX") == 0)
00118             name = xstrdup (obj->tempbase);
00119           else
00120             name = concat (obj->tempbase, "XXXXXX", NULL);
00121 
00122           out = mkstemps (name, 0);
00123           if (out < 0)
00124             {
00125               free (name);
00126               return NULL;
00127             }
00128 
00129           /* This isn't obj->funcs->close because we got the
00130              descriptor from mkstemps, not from a function in
00131              obj->funcs.  Calling close here is just like what
00132              make_temp_file does.  */
00133           close (out);
00134         }
00135     }
00136   else if ((flags & PEX_SUFFIX) != 0)
00137     {
00138       if (obj->tempbase == NULL)
00139         name = make_temp_file (name);
00140       else
00141         name = concat (obj->tempbase, name, NULL);
00142     }
00143 
00144   return name;
00145 }
00146 
00147 
00148 /* As for pex_run (), but permits the environment for the child process
00149    to be specified. */
00150 
00151 const char *
00152 pex_run_in_environment (struct pex_obj *obj, int flags, const char *executable,
00153                               char * const * argv, char * const * env,
00154                         const char *orig_outname, const char *errname,
00155                      int *err)
00156 {
00157   const char *errmsg;
00158   int in, out, errdes;
00159   char *outname;
00160   int outname_allocated;
00161   int p[2];
00162   int toclose;
00163   long pid;
00164 
00165   in = -1;
00166   out = -1;
00167   errdes = -1;
00168   outname = (char *) orig_outname;
00169   outname_allocated = 0;
00170 
00171   /* If the user called pex_input_file, close the file now.  */
00172   if (obj->input_file)
00173     {
00174       if (fclose (obj->input_file) == EOF)
00175         {
00176           errmsg = "closing pipeline input file";
00177           goto error_exit;
00178         }
00179       obj->input_file = NULL;
00180     }
00181 
00182   /* Set IN.  */
00183 
00184   if (obj->next_input_name != NULL)
00185     {
00186       /* We have to make sure that the previous process has completed
00187         before we try to read the file.  */
00188       if (!pex_get_status_and_time (obj, 0, &errmsg, err))
00189        goto error_exit;
00190 
00191       in = obj->funcs->open_read (obj, obj->next_input_name,
00192                               (flags & PEX_BINARY_INPUT) != 0);
00193       if (in < 0)
00194        {
00195          *err = errno;
00196          errmsg = "open temporary file";
00197          goto error_exit;
00198        }
00199       if (obj->next_input_name_allocated)
00200        {
00201          free (obj->next_input_name);
00202          obj->next_input_name_allocated = 0;
00203        }
00204       obj->next_input_name = NULL;
00205     }
00206   else
00207     {
00208       in = obj->next_input;
00209       if (in < 0)
00210        {
00211          *err = 0;
00212          errmsg = "pipeline already complete";
00213          goto error_exit;
00214        }
00215     }
00216 
00217   /* Set OUT and OBJ->NEXT_INPUT/OBJ->NEXT_INPUT_NAME.  */
00218 
00219   if ((flags & PEX_LAST) != 0)
00220     {
00221       if (outname == NULL)
00222        out = STDOUT_FILE_NO;
00223       else if ((flags & PEX_SUFFIX) != 0)
00224        {
00225          outname = concat (obj->tempbase, outname, NULL);
00226          outname_allocated = 1;
00227        }
00228       obj->next_input = -1;
00229     }
00230   else if ((obj->flags & PEX_USE_PIPES) == 0)
00231     {
00232       outname = temp_file (obj, flags, outname);
00233       if (! outname)
00234         {
00235           *err = 0;
00236           errmsg = "could not create temporary file";
00237           goto error_exit;
00238         }
00239 
00240       if (outname != orig_outname)
00241         outname_allocated = 1;
00242 
00243       if ((obj->flags & PEX_SAVE_TEMPS) == 0)
00244        {
00245          pex_add_remove (obj, outname, outname_allocated);
00246          outname_allocated = 0;
00247        }
00248 
00249       /* Hand off ownership of outname to the next stage.  */
00250       obj->next_input_name = outname;
00251       obj->next_input_name_allocated = outname_allocated;
00252       outname_allocated = 0;
00253     }
00254   else
00255     {
00256       if (obj->funcs->pipe (obj, p, (flags & PEX_BINARY_OUTPUT) != 0) < 0)
00257        {
00258          *err = errno;
00259          errmsg = "pipe";
00260          goto error_exit;
00261        }
00262 
00263       out = p[WRITE_PORT];
00264       obj->next_input = p[READ_PORT];
00265     }
00266 
00267   if (out < 0)
00268     {
00269       out = obj->funcs->open_write (obj, outname,
00270                                 (flags & PEX_BINARY_OUTPUT) != 0);
00271       if (out < 0)
00272        {
00273          *err = errno;
00274          errmsg = "open temporary output file";
00275          goto error_exit;
00276        }
00277     }
00278 
00279   if (outname_allocated)
00280     {
00281       free (outname);
00282       outname_allocated = 0;
00283     }
00284 
00285   /* Set ERRDES.  */
00286 
00287   if (errname != NULL && (flags & PEX_STDERR_TO_PIPE) != 0)
00288     {
00289       *err = 0;
00290       errmsg = "both ERRNAME and PEX_STDERR_TO_PIPE specified.";
00291       goto error_exit;
00292     }
00293 
00294   if (obj->stderr_pipe != -1)
00295     {
00296       *err = 0;
00297       errmsg = "PEX_STDERR_TO_PIPE used in the middle of pipeline";
00298       goto error_exit;
00299     }
00300 
00301   if (errname == NULL)
00302     {
00303       if (flags & PEX_STDERR_TO_PIPE)
00304        {
00305          if (obj->funcs->pipe (obj, p, (flags & PEX_BINARY_ERROR) != 0) < 0)
00306            {
00307              *err = errno;
00308              errmsg = "pipe";
00309              goto error_exit;
00310            }
00311          
00312          errdes = p[WRITE_PORT];
00313          obj->stderr_pipe = p[READ_PORT];   
00314        }
00315       else
00316        {
00317          errdes = STDERR_FILE_NO;
00318        }
00319     }
00320   else
00321     {
00322       errdes = obj->funcs->open_write (obj, errname, 
00323                                    (flags & PEX_BINARY_ERROR) != 0);
00324       if (errdes < 0)
00325        {
00326          *err = errno;
00327          errmsg = "open error file";
00328          goto error_exit;
00329        }
00330     }
00331 
00332   /* If we are using pipes, the child process has to close the next
00333      input pipe.  */
00334 
00335   if ((obj->flags & PEX_USE_PIPES) == 0)
00336     toclose = -1;
00337   else
00338     toclose = obj->next_input;
00339 
00340   /* Run the program.  */
00341 
00342   pid = obj->funcs->exec_child (obj, flags, executable, argv, env,
00343                             in, out, errdes, toclose, &errmsg, err);
00344   if (pid < 0)
00345     goto error_exit;
00346 
00347   ++obj->count;
00348   obj->children = XRESIZEVEC (long, obj->children, obj->count);
00349   obj->children[obj->count - 1] = pid;
00350 
00351   return NULL;
00352 
00353  error_exit:
00354   if (in >= 0 && in != STDIN_FILE_NO)
00355     obj->funcs->close (obj, in);
00356   if (out >= 0 && out != STDOUT_FILE_NO)
00357     obj->funcs->close (obj, out);
00358   if (errdes >= 0 && errdes != STDERR_FILE_NO)
00359     obj->funcs->close (obj, errdes);
00360   if (outname_allocated)
00361     free (outname);
00362   return errmsg;
00363 }
00364 
00365 /* Run a program.  */
00366 
00367 const char *
00368 pex_run (struct pex_obj *obj, int flags, const char *executable,
00369                char * const * argv, const char *orig_outname, const char *errname,
00370          int *err)
00371 {
00372   return pex_run_in_environment (obj, flags, executable, argv, NULL,
00373                              orig_outname, errname, err);
00374 }
00375 
00376 /* Return a FILE pointer for a temporary file to fill with input for
00377    the pipeline.  */
00378 FILE *
00379 pex_input_file (struct pex_obj *obj, int flags, const char *in_name)
00380 {
00381   char *name = (char *) in_name;
00382   FILE *f;
00383 
00384   /* This must be called before the first pipeline stage is run, and
00385      there must not have been any other input selected.  */
00386   if (obj->count != 0
00387       || (obj->next_input >= 0 && obj->next_input != STDIN_FILE_NO)
00388       || obj->next_input_name)
00389     {
00390       errno = EINVAL;
00391       return NULL;
00392     }
00393 
00394   name = temp_file (obj, flags, name);
00395   if (! name)
00396     return NULL;
00397 
00398   f = fopen (name, (flags & PEX_BINARY_OUTPUT) ? "wb" : "w");
00399   if (! f)
00400     {
00401       free (name);
00402       return NULL;
00403     }
00404 
00405   obj->input_file = f;
00406   obj->next_input_name = name;
00407   obj->next_input_name_allocated = (name != in_name);
00408 
00409   return f;
00410 }
00411 
00412 /* Return a stream for a pipe connected to the standard input of the
00413    first stage of the pipeline.  */
00414 FILE *
00415 pex_input_pipe (struct pex_obj *obj, int binary)
00416 {
00417   int p[2];
00418   FILE *f;
00419 
00420   /* You must call pex_input_pipe before the first pex_run or pex_one.  */
00421   if (obj->count > 0)
00422     goto usage_error;
00423 
00424   /* You must be using pipes.  Implementations that don't support
00425      pipes clear this flag before calling pex_init_common.  */
00426   if (! (obj->flags & PEX_USE_PIPES))
00427     goto usage_error;
00428 
00429   /* If we have somehow already selected other input, that's a
00430      mistake.  */
00431   if ((obj->next_input >= 0 && obj->next_input != STDIN_FILE_NO)
00432       || obj->next_input_name)
00433     goto usage_error;
00434 
00435   if (obj->funcs->pipe (obj, p, binary != 0) < 0)
00436     return NULL;
00437 
00438   f = obj->funcs->fdopenw (obj, p[WRITE_PORT], binary != 0);
00439   if (! f)
00440     {
00441       int saved_errno = errno;
00442       obj->funcs->close (obj, p[READ_PORT]);
00443       obj->funcs->close (obj, p[WRITE_PORT]);
00444       errno = saved_errno;
00445       return NULL;
00446     }
00447 
00448   obj->next_input = p[READ_PORT];
00449 
00450   return f;
00451 
00452  usage_error:
00453   errno = EINVAL;
00454   return NULL;
00455 }
00456 
00457 /* Return a FILE pointer for the output of the last program
00458    executed.  */
00459 
00460 FILE *
00461 pex_read_output (struct pex_obj *obj, int binary)
00462 {
00463   if (obj->next_input_name != NULL)
00464     {
00465       const char *errmsg;
00466       int err;
00467 
00468       /* We have to make sure that the process has completed before we
00469         try to read the file.  */
00470       if (!pex_get_status_and_time (obj, 0, &errmsg, &err))
00471        {
00472          errno = err;
00473          return NULL;
00474        }
00475 
00476       obj->read_output = fopen (obj->next_input_name, binary ? "rb" : "r");
00477 
00478       if (obj->next_input_name_allocated)
00479        {
00480          free (obj->next_input_name);
00481          obj->next_input_name_allocated = 0;
00482        }
00483       obj->next_input_name = NULL;
00484     }
00485   else
00486     {
00487       int o;
00488 
00489       o = obj->next_input;
00490       if (o < 0 || o == STDIN_FILE_NO)
00491        return NULL;
00492       obj->read_output = obj->funcs->fdopenr (obj, o, binary);
00493       obj->next_input = -1;
00494     }
00495 
00496   return obj->read_output;
00497 }
00498 
00499 FILE *
00500 pex_read_err (struct pex_obj *obj, int binary)
00501 {
00502   int o;
00503   
00504   o = obj->stderr_pipe;
00505   if (o < 0 || o == STDIN_FILE_NO)
00506     return NULL;
00507   obj->read_err = obj->funcs->fdopenr (obj, o, binary);
00508   return obj->read_err;    
00509 }
00510 
00511 /* Get the exit status and, if requested, the resource time for all
00512    the child processes.  Return 0 on failure, 1 on success.  */
00513 
00514 static int
00515 pex_get_status_and_time (struct pex_obj *obj, int done, const char **errmsg,
00516                       int *err)
00517 {
00518   int ret;
00519   int i;
00520 
00521   if (obj->number_waited == obj->count)
00522     return 1;
00523 
00524   obj->status = XRESIZEVEC (int, obj->status, obj->count);
00525   if ((obj->flags & PEX_RECORD_TIMES) != 0)
00526     obj->time = XRESIZEVEC (struct pex_time, obj->time, obj->count);
00527 
00528   ret = 1;
00529   for (i = obj->number_waited; i < obj->count; ++i)
00530     {
00531       if (obj->funcs->wait (obj, obj->children[i], &obj->status[i],
00532                          obj->time == NULL ? NULL : &obj->time[i],
00533                          done, errmsg, err) < 0)
00534        ret = 0;
00535     }
00536   obj->number_waited = i;
00537 
00538   return ret;
00539 }
00540 
00541 /* Get exit status of executed programs.  */
00542 
00543 int
00544 pex_get_status (struct pex_obj *obj, int count, int *vector)
00545 {
00546   if (obj->status == NULL)
00547     {
00548       const char *errmsg;
00549       int err;
00550 
00551       if (!pex_get_status_and_time (obj, 0, &errmsg, &err))
00552        return 0;
00553     }
00554 
00555   if (count > obj->count)
00556     {
00557       memset (vector + obj->count, 0, (count - obj->count) * sizeof (int));
00558       count = obj->count;
00559     }
00560 
00561   memcpy (vector, obj->status, count * sizeof (int));
00562 
00563   return 1;
00564 }
00565 
00566 /* Get process times of executed programs.  */
00567 
00568 int
00569 pex_get_times (struct pex_obj *obj, int count, struct pex_time *vector)
00570 {
00571   if (obj->status == NULL)
00572     {
00573       const char *errmsg;
00574       int err;
00575 
00576       if (!pex_get_status_and_time (obj, 0, &errmsg, &err))
00577        return 0;
00578     }
00579 
00580   if (obj->time == NULL)
00581     return 0;
00582 
00583   if (count > obj->count)
00584     {
00585       memset (vector + obj->count, 0,
00586              (count - obj->count) * sizeof (struct pex_time));
00587       count = obj->count;
00588     }
00589 
00590   memcpy (vector, obj->time, count * sizeof (struct pex_time));
00591 
00592   return 1;
00593 }
00594 
00595 /* Free a pex_obj structure.  */
00596 
00597 void
00598 pex_free (struct pex_obj *obj)
00599 {
00600   if (obj->next_input >= 0 && obj->next_input != STDIN_FILE_NO)
00601     obj->funcs->close (obj, obj->next_input);
00602 
00603   /* If the caller forgot to wait for the children, we do it here, to
00604      avoid zombies.  */
00605   if (obj->status == NULL)
00606     {
00607       const char *errmsg;
00608       int err;
00609 
00610       obj->flags &= ~ PEX_RECORD_TIMES;
00611       pex_get_status_and_time (obj, 1, &errmsg, &err);
00612     }
00613 
00614   if (obj->next_input_name_allocated)
00615     free (obj->next_input_name);
00616   if (obj->children != NULL)
00617     free (obj->children);
00618   if (obj->status != NULL)
00619     free (obj->status);
00620   if (obj->time != NULL)
00621     free (obj->time);
00622   if (obj->read_output != NULL)
00623     fclose (obj->read_output);
00624   if (obj->read_err != NULL)
00625     fclose (obj->read_err);
00626 
00627   if (obj->remove_count > 0)
00628     {
00629       int i;
00630 
00631       for (i = 0; i < obj->remove_count; ++i)
00632        {
00633          remove (obj->remove[i]);
00634          free (obj->remove[i]);
00635        }
00636       free (obj->remove);
00637     }
00638 
00639   if (obj->funcs->cleanup != NULL)
00640     obj->funcs->cleanup (obj);
00641 
00642   free (obj);
00643 }