* gdb/remote.c (remote_open_1): Do preopen tasks before
[binutils.git] / libiberty / pex-common.c
blobfd0c6b22e9bf86d03d5c64a07b615ed84e00ab85
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>.
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.
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.
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. */
21 #include "config.h"
22 #include "libiberty.h"
23 #include "pex-common.h"
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
40 extern int mkstemps (char *, int);
42 /* This file contains subroutines for the program execution routines
43 (pex_init, pex_run, etc.). This file is compiled on all
44 systems. */
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 *);
50 /* Initialize a pex_obj structure. */
52 struct pex_obj *
53 pex_init_common (int flags, const char *pname, const char *tempbase,
54 const struct pex_funcs *funcs)
56 struct pex_obj *obj;
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->read_output = NULL;
71 obj->remove_count = 0;
72 obj->remove = NULL;
73 obj->funcs = funcs;
74 obj->sysdep = NULL;
75 return obj;
78 /* Add a file to be removed when we are done. */
80 static void
81 pex_add_remove (struct pex_obj *obj, const char *name, int allocated)
83 char *add;
85 ++obj->remove_count;
86 obj->remove = XRESIZEVEC (char *, obj->remove, obj->remove_count);
87 if (allocated)
88 add = (char *) name;
89 else
90 add = xstrdup (name);
91 obj->remove[obj->remove_count - 1] = add;
94 /* Run a program. */
96 const char *
97 pex_run (struct pex_obj *obj, int flags, const char *executable,
98 char * const * argv, const char *orig_outname, const char *errname,
99 int *err)
101 const char *errmsg;
102 int in, out, errdes;
103 char *outname;
104 int outname_allocated;
105 int p[2];
106 long pid;
108 in = -1;
109 out = -1;
110 errdes = -1;
111 p[READ_PORT] = -1;
112 p[WRITE_PORT] = -1;
113 outname = (char *) orig_outname;
114 outname_allocated = 0;
116 /* Set IN. */
118 if (obj->next_input_name != NULL)
120 /* We have to make sure that the previous process has completed
121 before we try to read the file. */
122 if (!pex_get_status_and_time (obj, 0, &errmsg, err))
123 goto error_exit;
125 in = obj->funcs->open_read (obj, obj->next_input_name,
126 (flags & PEX_BINARY_INPUT) != 0);
127 if (in < 0)
129 *err = errno;
130 errmsg = "open temporary file";
131 goto error_exit;
133 if (obj->next_input_name_allocated)
135 free (obj->next_input_name);
136 obj->next_input_name_allocated = 0;
138 obj->next_input_name = NULL;
140 else
142 in = obj->next_input;
143 if (in < 0)
145 *err = 0;
146 errmsg = "pipeline already complete";
147 goto error_exit;
151 /* Set OUT and OBJ->NEXT_INPUT/OBJ->NEXT_INPUT_NAME. */
153 if ((flags & PEX_LAST) != 0)
155 if (outname == NULL)
156 out = STDOUT_FILE_NO;
157 else if ((flags & PEX_SUFFIX) != 0)
159 outname = concat (obj->tempbase, outname, NULL);
160 outname_allocated = 1;
162 obj->next_input = -1;
164 else if ((obj->flags & PEX_USE_PIPES) == 0)
166 if (outname == NULL)
168 if (obj->tempbase == NULL)
170 outname = make_temp_file (NULL);
171 outname_allocated = 1;
173 else
175 int len = strlen (obj->tempbase);
177 if (len >= 6
178 && strcmp (obj->tempbase + len - 6, "XXXXXX") == 0)
179 outname = xstrdup (obj->tempbase);
180 else
181 outname = concat (obj->tempbase, "XXXXXX", NULL);
183 outname_allocated = 1;
185 out = mkstemps (outname, 0);
186 if (out < 0)
188 *err = 0;
189 errmsg = "could not create temporary output file";
190 goto error_exit;
193 /* This isn't obj->funcs->close because we got the
194 descriptor from mkstemps, not from a function in
195 obj->funcs. Calling close here is just like what
196 make_temp_file does. */
197 close (out);
198 out = -1;
201 else if ((flags & PEX_SUFFIX) != 0)
203 if (obj->tempbase == NULL)
204 outname = make_temp_file (outname);
205 else
206 outname = concat (obj->tempbase, outname, NULL);
207 outname_allocated = 1;
210 if ((obj->flags & PEX_SAVE_TEMPS) == 0)
212 pex_add_remove (obj, outname, outname_allocated);
213 outname_allocated = 0;
216 if (!outname_allocated)
218 obj->next_input_name = outname;
219 obj->next_input_name_allocated = 0;
221 else
223 obj->next_input_name = outname;
224 outname_allocated = 0;
225 obj->next_input_name_allocated = 1;
228 else
230 if (obj->funcs->pipe (obj, p, (flags & PEX_BINARY_OUTPUT) != 0) < 0)
232 *err = errno;
233 errmsg = "pipe";
234 goto error_exit;
237 out = p[WRITE_PORT];
238 obj->next_input = p[READ_PORT];
241 if (out < 0)
243 out = obj->funcs->open_write (obj, outname,
244 (flags & PEX_BINARY_OUTPUT) != 0);
245 if (out < 0)
247 *err = errno;
248 errmsg = "open temporary output file";
249 goto error_exit;
253 if (outname_allocated)
255 free (outname);
256 outname_allocated = 0;
259 /* Set ERRDES. */
261 if (errname == NULL)
262 errdes = STDERR_FILE_NO;
263 else
265 /* We assume that stderr is in text mode--it certainly shouldn't
266 be controlled by PEX_BINARY_OUTPUT. If necessary, we can add
267 a PEX_BINARY_STDERR flag. */
268 errdes = obj->funcs->open_write (obj, errname, 0);
269 if (errdes < 0)
271 *err = errno;
272 errmsg = "open error file";
273 goto error_exit;
277 /* Run the program. */
279 pid = obj->funcs->exec_child (obj, flags, executable, argv, in, out, errdes,
280 &errmsg, err);
281 if (p[WRITE_PORT] != -1)
282 obj->funcs->close (obj, p[WRITE_PORT]);
283 if (pid < 0)
284 goto error_exit;
286 ++obj->count;
287 obj->children = XRESIZEVEC (long, obj->children, obj->count);
288 obj->children[obj->count - 1] = pid;
290 return NULL;
292 error_exit:
293 if (in >= 0 && in != STDIN_FILE_NO)
294 obj->funcs->close (obj, in);
295 if (out >= 0 && out != STDOUT_FILE_NO)
296 obj->funcs->close (obj, out);
297 if (errdes >= 0 && errdes != STDERR_FILE_NO)
298 obj->funcs->close (obj, errdes);
299 if (outname_allocated)
300 free (outname);
301 return errmsg;
304 /* Return a FILE pointer for the input of the first program
305 executed. */
307 FILE *
308 pex_write_input (struct pex_obj *obj, int binary)
310 int p[2];
311 FILE *write_input;
313 /* You must call pex_write_input before the first pex_run or pex_one. */
314 if (obj->count > 0)
315 goto usage_error;
317 /* You must be using pipes. Implementations that don't support
318 pipes clear this flag before calling pex_init_common. */
319 if (! (obj->flags & PEX_USE_PIPES))
320 goto usage_error;
322 /* If we have somehow already selected other input, that's a
323 mistake. */
324 if ((obj->next_input >= 0 && obj->next_input != STDIN_FILE_NO)
325 || obj->next_input_name)
326 goto usage_error;
328 if (obj->funcs->pipe (obj, p, binary != 0) < 0)
329 return NULL;
331 write_input = obj->funcs->fdopenw (obj, p[WRITE_PORT], binary != 0);
332 if (! write_input)
334 int saved_errno = errno;
335 obj->funcs->close (obj, p[READ_PORT]);
336 obj->funcs->close (obj, p[WRITE_PORT]);
337 errno = saved_errno;
338 return NULL;
341 obj->next_input = p[READ_PORT];
343 return write_input;
345 usage_error:
346 errno = EINVAL;
347 return NULL;
350 /* Return a FILE pointer for the output of the last program
351 executed. */
353 FILE *
354 pex_read_output (struct pex_obj *obj, int binary)
356 if (obj->next_input_name != NULL)
358 const char *errmsg;
359 int err;
361 /* We have to make sure that the process has completed before we
362 try to read the file. */
363 if (!pex_get_status_and_time (obj, 0, &errmsg, &err))
365 errno = err;
366 return NULL;
369 obj->read_output = fopen (obj->next_input_name, binary ? "rb" : "r");
371 if (obj->next_input_name_allocated)
373 free (obj->next_input_name);
374 obj->next_input_name_allocated = 0;
376 obj->next_input_name = NULL;
378 else
380 int o;
382 o = obj->next_input;
383 if (o < 0 || o == STDIN_FILE_NO)
384 return NULL;
385 obj->read_output = obj->funcs->fdopenr (obj, o, binary);
386 obj->next_input = -1;
389 return obj->read_output;
392 /* Get the exit status and, if requested, the resource time for all
393 the child processes. Return 0 on failure, 1 on success. */
395 static int
396 pex_get_status_and_time (struct pex_obj *obj, int done, const char **errmsg,
397 int *err)
399 int ret;
400 int i;
402 if (obj->number_waited == obj->count)
403 return 1;
405 obj->status = XRESIZEVEC (int, obj->status, obj->count);
406 if ((obj->flags & PEX_RECORD_TIMES) != 0)
407 obj->time = XRESIZEVEC (struct pex_time, obj->time, obj->count);
409 ret = 1;
410 for (i = obj->number_waited; i < obj->count; ++i)
412 if (obj->funcs->wait (obj, obj->children[i], &obj->status[i],
413 obj->time == NULL ? NULL : &obj->time[i],
414 done, errmsg, err) < 0)
415 ret = 0;
417 obj->number_waited = i;
419 return ret;
422 /* Get exit status of executed programs. */
425 pex_get_status (struct pex_obj *obj, int count, int *vector)
427 if (obj->status == NULL)
429 const char *errmsg;
430 int err;
432 if (!pex_get_status_and_time (obj, 0, &errmsg, &err))
433 return 0;
436 if (count > obj->count)
438 memset (vector + obj->count, 0, (count - obj->count) * sizeof (int));
439 count = obj->count;
442 memcpy (vector, obj->status, count * sizeof (int));
444 return 1;
447 /* Get process times of executed programs. */
450 pex_get_times (struct pex_obj *obj, int count, struct pex_time *vector)
452 if (obj->status == NULL)
454 const char *errmsg;
455 int err;
457 if (!pex_get_status_and_time (obj, 0, &errmsg, &err))
458 return 0;
461 if (obj->time == NULL)
462 return 0;
464 if (count > obj->count)
466 memset (vector + obj->count, 0,
467 (count - obj->count) * sizeof (struct pex_time));
468 count = obj->count;
471 memcpy (vector, obj->time, count * sizeof (struct pex_time));
473 return 1;
476 /* Free a pex_obj structure. */
478 void
479 pex_free (struct pex_obj *obj)
481 if (obj->next_input >= 0 && obj->next_input != STDIN_FILE_NO)
482 obj->funcs->close (obj, obj->next_input);
484 /* If the caller forgot to wait for the children, we do it here, to
485 avoid zombies. */
486 if (obj->status == NULL)
488 const char *errmsg;
489 int err;
491 obj->flags &= ~ PEX_RECORD_TIMES;
492 pex_get_status_and_time (obj, 1, &errmsg, &err);
495 if (obj->next_input_name_allocated)
496 free (obj->next_input_name);
497 if (obj->children != NULL)
498 free (obj->children);
499 if (obj->status != NULL)
500 free (obj->status);
501 if (obj->time != NULL)
502 free (obj->time);
503 if (obj->read_output != NULL)
504 fclose (obj->read_output);
506 if (obj->remove_count > 0)
508 int i;
510 for (i = 0; i < obj->remove_count; ++i)
512 remove (obj->remove[i]);
513 free (obj->remove[i]);
515 free (obj->remove);
518 if (obj->funcs->cleanup != NULL)
519 obj->funcs->cleanup (obj);
521 free (obj);