OSDN Git Service

Patch to provide pex_run_in_environment.
[pf3gnuchains/gcc-fork.git] / libiberty / pex-common.c
1 /* Common code for executing a program in a sub-process.
2    Copyright (C) 2005 Free Software Foundation, Inc.
3    Written by Ian Lance Taylor <ian@airs.com>.
4
5 This file is part of the libiberty library.
6 Libiberty is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 Libiberty is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public
17 License along with libiberty; see the file COPYING.LIB.  If not,
18 write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19 Boston, MA 02110-1301, USA.  */
20
21 #include "config.h"
22 #include "libiberty.h"
23 #include "pex-common.h"
24
25 #include <stdio.h>
26 #include <errno.h>
27 #ifdef NEED_DECLARATION_ERRNO
28 extern int errno;
29 #endif
30 #ifdef HAVE_STDLIB_H
31 #include <stdlib.h>
32 #endif
33 #ifdef HAVE_STRING_H
34 #include <string.h>
35 #endif
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39
40 extern int mkstemps (char *, int);
41
42 /* This file contains subroutines for the program execution routines
43    (pex_init, pex_run, etc.).  This file is compiled on all
44    systems.  */
45
46 static void pex_add_remove (struct pex_obj *, const char *, int);
47 static int pex_get_status_and_time (struct pex_obj *, int, const char **,
48                                     int *);
49
50 /* Initialize a pex_obj structure.  */
51
52 struct pex_obj *
53 pex_init_common (int flags, const char *pname, const char *tempbase,
54                  const struct pex_funcs *funcs)
55 {
56   struct pex_obj *obj;
57
58   obj = XNEW (struct pex_obj);
59   obj->flags = flags;
60   obj->pname = pname;
61   obj->tempbase = tempbase;
62   obj->next_input = STDIN_FILE_NO;
63   obj->next_input_name = NULL;
64   obj->next_input_name_allocated = 0;
65   obj->count = 0;
66   obj->children = NULL;
67   obj->status = NULL;
68   obj->time = NULL;
69   obj->number_waited = 0;
70   obj->input_file = NULL;
71   obj->read_output = NULL;
72   obj->remove_count = 0;
73   obj->remove = NULL;
74   obj->funcs = funcs;
75   obj->sysdep = NULL;
76   return obj;
77 }
78
79 /* Add a file to be removed when we are done.  */
80
81 static void
82 pex_add_remove (struct pex_obj *obj, const char *name, int allocated)
83 {
84   char *add;
85
86   ++obj->remove_count;
87   obj->remove = XRESIZEVEC (char *, obj->remove, obj->remove_count);
88   if (allocated)
89     add = (char *) name;
90   else
91     add = xstrdup (name);
92   obj->remove[obj->remove_count - 1] = add;
93 }
94
95 /* Generate a temporary file name based on OBJ, FLAGS, and NAME.
96    Return NULL if we were unable to reserve a temporary filename.
97
98    If non-NULL, the result is either allocated with malloc, or the
99    same pointer as NAME.  */
100 static char *
101 temp_file (struct pex_obj *obj, int flags, char *name)
102 {
103   if (name == NULL)
104     {
105       if (obj->tempbase == NULL)
106         {
107           name = make_temp_file (NULL);
108         }
109       else
110         {
111           int len = strlen (obj->tempbase);
112           int out;
113
114           if (len >= 6
115               && strcmp (obj->tempbase + len - 6, "XXXXXX") == 0)
116             name = xstrdup (obj->tempbase);
117           else
118             name = concat (obj->tempbase, "XXXXXX", NULL);
119
120           out = mkstemps (name, 0);
121           if (out < 0)
122             {
123               free (name);
124               return NULL;
125             }
126
127           /* This isn't obj->funcs->close because we got the
128              descriptor from mkstemps, not from a function in
129              obj->funcs.  Calling close here is just like what
130              make_temp_file does.  */
131           close (out);
132         }
133     }
134   else if ((flags & PEX_SUFFIX) != 0)
135     {
136       if (obj->tempbase == NULL)
137         name = make_temp_file (name);
138       else
139         name = concat (obj->tempbase, name, NULL);
140     }
141
142   return name;
143 }
144
145
146 /* As for pex_run (), but permits the environment for the child process
147    to be specified. */
148
149 const char *
150 pex_run_in_environment (struct pex_obj *obj, int flags, const char *executable,
151                         char * const * argv, char * const * env,
152                         const char *orig_outname, const char *errname,
153                         int *err)
154 {
155   const char *errmsg;
156   int in, out, errdes;
157   char *outname;
158   int outname_allocated;
159   int p[2];
160   long pid;
161
162   in = -1;
163   out = -1;
164   errdes = -1;
165   outname = (char *) orig_outname;
166   outname_allocated = 0;
167
168   /* If the user called pex_input_file, close the file now.  */
169   if (obj->input_file)
170     {
171       if (fclose (obj->input_file) == EOF)
172         {
173           errmsg = "closing pipeline input file";
174           goto error_exit;
175         }
176       obj->input_file = NULL;
177     }
178
179   /* Set IN.  */
180
181   if (obj->next_input_name != NULL)
182     {
183       /* We have to make sure that the previous process has completed
184          before we try to read the file.  */
185       if (!pex_get_status_and_time (obj, 0, &errmsg, err))
186         goto error_exit;
187
188       in = obj->funcs->open_read (obj, obj->next_input_name,
189                                   (flags & PEX_BINARY_INPUT) != 0);
190       if (in < 0)
191         {
192           *err = errno;
193           errmsg = "open temporary file";
194           goto error_exit;
195         }
196       if (obj->next_input_name_allocated)
197         {
198           free (obj->next_input_name);
199           obj->next_input_name_allocated = 0;
200         }
201       obj->next_input_name = NULL;
202     }
203   else
204     {
205       in = obj->next_input;
206       if (in < 0)
207         {
208           *err = 0;
209           errmsg = "pipeline already complete";
210           goto error_exit;
211         }
212     }
213
214   /* Set OUT and OBJ->NEXT_INPUT/OBJ->NEXT_INPUT_NAME.  */
215
216   if ((flags & PEX_LAST) != 0)
217     {
218       if (outname == NULL)
219         out = STDOUT_FILE_NO;
220       else if ((flags & PEX_SUFFIX) != 0)
221         {
222           outname = concat (obj->tempbase, outname, NULL);
223           outname_allocated = 1;
224         }
225       obj->next_input = -1;
226     }
227   else if ((obj->flags & PEX_USE_PIPES) == 0)
228     {
229       outname = temp_file (obj, flags, outname);
230       if (! outname)
231         {
232           *err = 0;
233           errmsg = "could not create temporary file";
234           goto error_exit;
235         }
236
237       if (outname != orig_outname)
238         outname_allocated = 1;
239
240       if ((obj->flags & PEX_SAVE_TEMPS) == 0)
241         {
242           pex_add_remove (obj, outname, outname_allocated);
243           outname_allocated = 0;
244         }
245
246       /* Hand off ownership of outname to the next stage.  */
247       obj->next_input_name = outname;
248       obj->next_input_name_allocated = outname_allocated;
249       outname_allocated = 0;
250     }
251   else
252     {
253       if (obj->funcs->pipe (obj, p, (flags & PEX_BINARY_OUTPUT) != 0) < 0)
254         {
255           *err = errno;
256           errmsg = "pipe";
257           goto error_exit;
258         }
259
260       out = p[WRITE_PORT];
261       obj->next_input = p[READ_PORT];
262     }
263
264   if (out < 0)
265     {
266       out = obj->funcs->open_write (obj, outname,
267                                     (flags & PEX_BINARY_OUTPUT) != 0);
268       if (out < 0)
269         {
270           *err = errno;
271           errmsg = "open temporary output file";
272           goto error_exit;
273         }
274     }
275
276   if (outname_allocated)
277     {
278       free (outname);
279       outname_allocated = 0;
280     }
281
282   /* Set ERRDES.  */
283
284   if (errname == NULL)
285     errdes = STDERR_FILE_NO;
286   else
287     {
288       /* We assume that stderr is in text mode--it certainly shouldn't
289          be controlled by PEX_BINARY_OUTPUT.  If necessary, we can add
290          a PEX_BINARY_STDERR flag.  */
291       errdes = obj->funcs->open_write (obj, errname, 0);
292       if (errdes < 0)
293         {
294           *err = errno;
295           errmsg = "open error file";
296           goto error_exit;
297         }
298     }
299
300   /* Run the program.  */
301
302   pid = obj->funcs->exec_child (obj, flags, executable, argv, env,
303                                 in, out, errdes, &errmsg, err);
304   if (pid < 0)
305     goto error_exit;
306
307   ++obj->count;
308   obj->children = XRESIZEVEC (long, obj->children, obj->count);
309   obj->children[obj->count - 1] = pid;
310
311   return NULL;
312
313  error_exit:
314   if (in >= 0 && in != STDIN_FILE_NO)
315     obj->funcs->close (obj, in);
316   if (out >= 0 && out != STDOUT_FILE_NO)
317     obj->funcs->close (obj, out);
318   if (errdes >= 0 && errdes != STDERR_FILE_NO)
319     obj->funcs->close (obj, errdes);
320   if (outname_allocated)
321     free (outname);
322   return errmsg;
323 }
324
325 /* Run a program.  */
326
327 const char *
328 pex_run (struct pex_obj *obj, int flags, const char *executable,
329          char * const * argv, const char *orig_outname, const char *errname,
330          int *err)
331 {
332   return pex_run_in_environment (obj, flags, executable, argv, NULL,
333                                  orig_outname, errname, err);
334 }
335
336 /* Return a FILE pointer for a temporary file to fill with input for
337    the pipeline.  */
338 FILE *
339 pex_input_file (struct pex_obj *obj, int flags, const char *in_name)
340 {
341   char *name = (char *) in_name;
342   FILE *f;
343
344   /* This must be called before the first pipeline stage is run, and
345      there must not have been any other input selected.  */
346   if (obj->count != 0
347       || (obj->next_input >= 0 && obj->next_input != STDIN_FILE_NO)
348       || obj->next_input_name)
349     {
350       errno = EINVAL;
351       return NULL;
352     }
353
354   name = temp_file (obj, flags, name);
355   if (! name)
356     return NULL;
357
358   f = fopen (name, (flags & PEX_BINARY_OUTPUT) ? "wb" : "w");
359   if (! f)
360     {
361       free (name);
362       return NULL;
363     }
364
365   obj->input_file = f;
366   obj->next_input_name = name;
367   obj->next_input_name_allocated = (name != in_name);
368
369   return f;
370 }
371
372 /* Return a stream for a pipe connected to the standard input of the
373    first stage of the pipeline.  */
374 FILE *
375 pex_input_pipe (struct pex_obj *obj, int binary)
376 {
377   int p[2];
378   FILE *f;
379
380   /* You must call pex_input_pipe before the first pex_run or pex_one.  */
381   if (obj->count > 0)
382     goto usage_error;
383
384   /* You must be using pipes.  Implementations that don't support
385      pipes clear this flag before calling pex_init_common.  */
386   if (! (obj->flags & PEX_USE_PIPES))
387     goto usage_error;
388
389   /* If we have somehow already selected other input, that's a
390      mistake.  */
391   if ((obj->next_input >= 0 && obj->next_input != STDIN_FILE_NO)
392       || obj->next_input_name)
393     goto usage_error;
394
395   if (obj->funcs->pipe (obj, p, binary != 0) < 0)
396     return NULL;
397
398   f = obj->funcs->fdopenw (obj, p[WRITE_PORT], binary != 0);
399   if (! f)
400     {
401       int saved_errno = errno;
402       obj->funcs->close (obj, p[READ_PORT]);
403       obj->funcs->close (obj, p[WRITE_PORT]);
404       errno = saved_errno;
405       return NULL;
406     }
407
408   obj->next_input = p[READ_PORT];
409
410   return f;
411
412  usage_error:
413   errno = EINVAL;
414   return NULL;
415 }
416
417 /* Return a FILE pointer for the output of the last program
418    executed.  */
419
420 FILE *
421 pex_read_output (struct pex_obj *obj, int binary)
422 {
423   if (obj->next_input_name != NULL)
424     {
425       const char *errmsg;
426       int err;
427
428       /* We have to make sure that the process has completed before we
429          try to read the file.  */
430       if (!pex_get_status_and_time (obj, 0, &errmsg, &err))
431         {
432           errno = err;
433           return NULL;
434         }
435
436       obj->read_output = fopen (obj->next_input_name, binary ? "rb" : "r");
437
438       if (obj->next_input_name_allocated)
439         {
440           free (obj->next_input_name);
441           obj->next_input_name_allocated = 0;
442         }
443       obj->next_input_name = NULL;
444     }
445   else
446     {
447       int o;
448
449       o = obj->next_input;
450       if (o < 0 || o == STDIN_FILE_NO)
451         return NULL;
452       obj->read_output = obj->funcs->fdopenr (obj, o, binary);
453       obj->next_input = -1;
454     }
455
456   return obj->read_output;
457 }
458
459 /* Get the exit status and, if requested, the resource time for all
460    the child processes.  Return 0 on failure, 1 on success.  */
461
462 static int
463 pex_get_status_and_time (struct pex_obj *obj, int done, const char **errmsg,
464                          int *err)
465 {
466   int ret;
467   int i;
468
469   if (obj->number_waited == obj->count)
470     return 1;
471
472   obj->status = XRESIZEVEC (int, obj->status, obj->count);
473   if ((obj->flags & PEX_RECORD_TIMES) != 0)
474     obj->time = XRESIZEVEC (struct pex_time, obj->time, obj->count);
475
476   ret = 1;
477   for (i = obj->number_waited; i < obj->count; ++i)
478     {
479       if (obj->funcs->wait (obj, obj->children[i], &obj->status[i],
480                             obj->time == NULL ? NULL : &obj->time[i],
481                             done, errmsg, err) < 0)
482         ret = 0;
483     }
484   obj->number_waited = i;
485
486   return ret;
487 }
488
489 /* Get exit status of executed programs.  */
490
491 int
492 pex_get_status (struct pex_obj *obj, int count, int *vector)
493 {
494   if (obj->status == NULL)
495     {
496       const char *errmsg;
497       int err;
498
499       if (!pex_get_status_and_time (obj, 0, &errmsg, &err))
500         return 0;
501     }
502
503   if (count > obj->count)
504     {
505       memset (vector + obj->count, 0, (count - obj->count) * sizeof (int));
506       count = obj->count;
507     }
508
509   memcpy (vector, obj->status, count * sizeof (int));
510
511   return 1;
512 }
513
514 /* Get process times of executed programs.  */
515
516 int
517 pex_get_times (struct pex_obj *obj, int count, struct pex_time *vector)
518 {
519   if (obj->status == NULL)
520     {
521       const char *errmsg;
522       int err;
523
524       if (!pex_get_status_and_time (obj, 0, &errmsg, &err))
525         return 0;
526     }
527
528   if (obj->time == NULL)
529     return 0;
530
531   if (count > obj->count)
532     {
533       memset (vector + obj->count, 0,
534               (count - obj->count) * sizeof (struct pex_time));
535       count = obj->count;
536     }
537
538   memcpy (vector, obj->time, count * sizeof (struct pex_time));
539
540   return 1;
541 }
542
543 /* Free a pex_obj structure.  */
544
545 void
546 pex_free (struct pex_obj *obj)
547 {
548   if (obj->next_input >= 0 && obj->next_input != STDIN_FILE_NO)
549     obj->funcs->close (obj, obj->next_input);
550
551   /* If the caller forgot to wait for the children, we do it here, to
552      avoid zombies.  */
553   if (obj->status == NULL)
554     {
555       const char *errmsg;
556       int err;
557
558       obj->flags &= ~ PEX_RECORD_TIMES;
559       pex_get_status_and_time (obj, 1, &errmsg, &err);
560     }
561
562   if (obj->next_input_name_allocated)
563     free (obj->next_input_name);
564   if (obj->children != NULL)
565     free (obj->children);
566   if (obj->status != NULL)
567     free (obj->status);
568   if (obj->time != NULL)
569     free (obj->time);
570   if (obj->read_output != NULL)
571     fclose (obj->read_output);
572
573   if (obj->remove_count > 0)
574     {
575       int i;
576
577       for (i = 0; i < obj->remove_count; ++i)
578         {
579           remove (obj->remove[i]);
580           free (obj->remove[i]);
581         }
582       free (obj->remove);
583     }
584
585   if (obj->funcs->cleanup != NULL)
586     obj->funcs->cleanup (obj);
587
588   free (obj);
589 }