]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - libiberty/pex-common.c
* remote.c (extended_remote_restart): Pass the correct length
[thirdparty/binutils-gdb.git] / libiberty / pex-common.c
CommitLineData
b109e79a
ILT
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
5This file is part of the libiberty library.
6Libiberty is free software; you can redistribute it and/or
7modify it under the terms of the GNU Library General Public
8License as published by the Free Software Foundation; either
9version 2 of the License, or (at your option) any later version.
10
11Libiberty is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14Library General Public License for more details.
15
16You should have received a copy of the GNU Library General Public
17License along with libiberty; see the file COPYING.LIB. If not,
979c05d3
NC
18write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19Boston, MA 02110-1301, USA. */
b109e79a
ILT
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
28extern 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
40extern 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
46static void pex_add_remove (struct pex_obj *, const char *, int);
47static int pex_get_status_and_time (struct pex_obj *, int, const char **,
48 int *);
49
50/* Initialize a pex_obj structure. */
51
52struct pex_obj *
53pex_init_common (int flags, const char *pname, const char *tempbase,
54 const struct pex_funcs *funcs)
55{
56 struct pex_obj *obj;
57
abf6a75b 58 obj = XNEW (struct pex_obj);
b109e79a
ILT
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;
76}
77
78/* Add a file to be removed when we are done. */
79
80static void
81pex_add_remove (struct pex_obj *obj, const char *name, int allocated)
82{
83 char *add;
84
85 ++obj->remove_count;
abf6a75b 86 obj->remove = XRESIZEVEC (char *, obj->remove, obj->remove_count);
b109e79a
ILT
87 if (allocated)
88 add = (char *) name;
89 else
90 add = xstrdup (name);
91 obj->remove[obj->remove_count - 1] = add;
92}
93
94/* Run a program. */
95
96const char *
97pex_run (struct pex_obj *obj, int flags, const char *executable,
98 char * const * argv, const char *orig_outname, const char *errname,
99 int *err)
100{
101 const char *errmsg;
102 int in, out, errdes;
103 char *outname;
104 int outname_allocated;
105 int p[2];
106 long pid;
107
108 in = -1;
109 out = -1;
110 errdes = -1;
111 outname = (char *) orig_outname;
112 outname_allocated = 0;
113
114 /* Set IN. */
115
116 if (obj->next_input_name != NULL)
117 {
118 /* We have to make sure that the previous process has completed
119 before we try to read the file. */
120 if (!pex_get_status_and_time (obj, 0, &errmsg, err))
121 goto error_exit;
122
123 in = obj->funcs->open_read (obj, obj->next_input_name,
124 (flags & PEX_BINARY_INPUT) != 0);
125 if (in < 0)
126 {
127 *err = errno;
128 errmsg = "open temporary file";
129 goto error_exit;
130 }
131 if (obj->next_input_name_allocated)
132 {
133 free (obj->next_input_name);
134 obj->next_input_name_allocated = 0;
135 }
136 obj->next_input_name = NULL;
137 }
138 else
139 {
140 in = obj->next_input;
141 if (in < 0)
142 {
143 *err = 0;
144 errmsg = "pipeline already complete";
145 goto error_exit;
146 }
147 }
148
149 /* Set OUT and OBJ->NEXT_INPUT/OBJ->NEXT_INPUT_NAME. */
150
151 if ((flags & PEX_LAST) != 0)
152 {
153 if (outname == NULL)
154 out = STDOUT_FILE_NO;
155 else if ((flags & PEX_SUFFIX) != 0)
156 {
157 outname = concat (obj->tempbase, outname, NULL);
158 outname_allocated = 1;
159 }
160 obj->next_input = -1;
161 }
162 else if ((obj->flags & PEX_USE_PIPES) == 0)
163 {
164 if (outname == NULL)
165 {
166 if (obj->tempbase == NULL)
167 {
168 outname = make_temp_file (NULL);
169 outname_allocated = 1;
170 }
171 else
172 {
173 int len = strlen (obj->tempbase);
174
175 if (len >= 6
176 && strcmp (obj->tempbase + len - 6, "XXXXXX") == 0)
177 outname = xstrdup (obj->tempbase);
178 else
179 outname = concat (obj->tempbase, "XXXXXX", NULL);
180
181 outname_allocated = 1;
182
183 out = mkstemps (outname, 0);
184 if (out < 0)
185 {
186 *err = 0;
187 errmsg = "could not create temporary output file";
188 goto error_exit;
189 }
190
191 /* This isn't obj->funcs->close because we got the
192 descriptor from mkstemps, not from a function in
193 obj->funcs. Calling close here is just like what
194 make_temp_file does. */
195 close (out);
196 out = -1;
197 }
198 }
199 else if ((flags & PEX_SUFFIX) != 0)
200 {
201 if (obj->tempbase == NULL)
202 outname = make_temp_file (outname);
203 else
204 outname = concat (obj->tempbase, outname, NULL);
205 outname_allocated = 1;
206 }
207
208 if ((obj->flags & PEX_SAVE_TEMPS) == 0)
209 {
210 pex_add_remove (obj, outname, outname_allocated);
211 outname_allocated = 0;
212 }
213
57b126a3
DD
214 /* Hand off ownership of outname to the next stage. */
215 obj->next_input_name = outname;
216 obj->next_input_name_allocated = outname_allocated;
217 outname_allocated = 0;
b109e79a
ILT
218 }
219 else
220 {
221 if (obj->funcs->pipe (obj, p, (flags & PEX_BINARY_OUTPUT) != 0) < 0)
222 {
223 *err = errno;
224 errmsg = "pipe";
225 goto error_exit;
226 }
227
228 out = p[WRITE_PORT];
229 obj->next_input = p[READ_PORT];
230 }
231
232 if (out < 0)
233 {
234 out = obj->funcs->open_write (obj, outname,
235 (flags & PEX_BINARY_OUTPUT) != 0);
236 if (out < 0)
237 {
238 *err = errno;
239 errmsg = "open temporary output file";
240 goto error_exit;
241 }
242 }
243
244 if (outname_allocated)
245 {
246 free (outname);
247 outname_allocated = 0;
248 }
249
250 /* Set ERRDES. */
251
252 if (errname == NULL)
253 errdes = STDERR_FILE_NO;
254 else
255 {
256 /* We assume that stderr is in text mode--it certainly shouldn't
257 be controlled by PEX_BINARY_OUTPUT. If necessary, we can add
258 a PEX_BINARY_STDERR flag. */
259 errdes = obj->funcs->open_write (obj, errname, 0);
260 if (errdes < 0)
261 {
262 *err = errno;
263 errmsg = "open error file";
264 goto error_exit;
265 }
266 }
267
268 /* Run the program. */
269
270 pid = obj->funcs->exec_child (obj, flags, executable, argv, in, out, errdes,
271 &errmsg, err);
272 if (pid < 0)
273 goto error_exit;
274
275 ++obj->count;
abf6a75b 276 obj->children = XRESIZEVEC (long, obj->children, obj->count);
b109e79a
ILT
277 obj->children[obj->count - 1] = pid;
278
279 return NULL;
280
281 error_exit:
282 if (in >= 0 && in != STDIN_FILE_NO)
283 obj->funcs->close (obj, in);
284 if (out >= 0 && out != STDOUT_FILE_NO)
285 obj->funcs->close (obj, out);
286 if (errdes >= 0 && errdes != STDERR_FILE_NO)
287 obj->funcs->close (obj, errdes);
288 if (outname_allocated)
289 free (outname);
290 return errmsg;
291}
292
293/* Return a FILE pointer for the output of the last program
294 executed. */
295
296FILE *
297pex_read_output (struct pex_obj *obj, int binary)
298{
299 if (obj->next_input_name != NULL)
300 {
301 const char *errmsg;
302 int err;
303
304 /* We have to make sure that the process has completed before we
305 try to read the file. */
306 if (!pex_get_status_and_time (obj, 0, &errmsg, &err))
307 {
308 errno = err;
309 return NULL;
310 }
311
312 obj->read_output = fopen (obj->next_input_name, binary ? "rb" : "r");
313
314 if (obj->next_input_name_allocated)
315 {
316 free (obj->next_input_name);
317 obj->next_input_name_allocated = 0;
318 }
319 obj->next_input_name = NULL;
320 }
321 else
322 {
323 int o;
324
325 o = obj->next_input;
326 if (o < 0 || o == STDIN_FILE_NO)
327 return NULL;
328 obj->read_output = obj->funcs->fdopenr (obj, o, binary);
329 obj->next_input = -1;
330 }
331
332 return obj->read_output;
333}
334
335/* Get the exit status and, if requested, the resource time for all
336 the child processes. Return 0 on failure, 1 on success. */
337
338static int
339pex_get_status_and_time (struct pex_obj *obj, int done, const char **errmsg,
340 int *err)
341{
342 int ret;
343 int i;
344
345 if (obj->number_waited == obj->count)
346 return 1;
347
abf6a75b 348 obj->status = XRESIZEVEC (int, obj->status, obj->count);
b109e79a 349 if ((obj->flags & PEX_RECORD_TIMES) != 0)
abf6a75b 350 obj->time = XRESIZEVEC (struct pex_time, obj->time, obj->count);
b109e79a
ILT
351
352 ret = 1;
353 for (i = obj->number_waited; i < obj->count; ++i)
354 {
355 if (obj->funcs->wait (obj, obj->children[i], &obj->status[i],
356 obj->time == NULL ? NULL : &obj->time[i],
357 done, errmsg, err) < 0)
358 ret = 0;
359 }
360 obj->number_waited = i;
361
362 return ret;
363}
364
365/* Get exit status of executed programs. */
366
367int
368pex_get_status (struct pex_obj *obj, int count, int *vector)
369{
370 if (obj->status == NULL)
371 {
372 const char *errmsg;
373 int err;
374
375 if (!pex_get_status_and_time (obj, 0, &errmsg, &err))
376 return 0;
377 }
378
379 if (count > obj->count)
380 {
381 memset (vector + obj->count, 0, (count - obj->count) * sizeof (int));
382 count = obj->count;
383 }
384
385 memcpy (vector, obj->status, count * sizeof (int));
386
387 return 1;
388}
389
390/* Get process times of executed programs. */
391
392int
393pex_get_times (struct pex_obj *obj, int count, struct pex_time *vector)
394{
395 if (obj->status == NULL)
396 {
397 const char *errmsg;
398 int err;
399
400 if (!pex_get_status_and_time (obj, 0, &errmsg, &err))
401 return 0;
402 }
403
404 if (obj->time == NULL)
405 return 0;
406
407 if (count > obj->count)
408 {
409 memset (vector + obj->count, 0,
410 (count - obj->count) * sizeof (struct pex_time));
411 count = obj->count;
412 }
413
414 memcpy (vector, obj->time, count * sizeof (struct pex_time));
415
416 return 1;
417}
418
419/* Free a pex_obj structure. */
420
421void
422pex_free (struct pex_obj *obj)
423{
424 if (obj->next_input >= 0 && obj->next_input != STDIN_FILE_NO)
425 obj->funcs->close (obj, obj->next_input);
426
427 /* If the caller forgot to wait for the children, we do it here, to
428 avoid zombies. */
429 if (obj->status == NULL)
430 {
431 const char *errmsg;
432 int err;
433
434 obj->flags &= ~ PEX_RECORD_TIMES;
435 pex_get_status_and_time (obj, 1, &errmsg, &err);
436 }
437
438 if (obj->next_input_name_allocated)
439 free (obj->next_input_name);
440 if (obj->children != NULL)
441 free (obj->children);
442 if (obj->status != NULL)
443 free (obj->status);
444 if (obj->time != NULL)
445 free (obj->time);
446 if (obj->read_output != NULL)
447 fclose (obj->read_output);
448
449 if (obj->remove_count > 0)
450 {
451 int i;
452
453 for (i = 0; i < obj->remove_count; ++i)
454 {
455 remove (obj->remove[i]);
456 free (obj->remove[i]);
457 }
458 free (obj->remove);
459 }
460
461 if (obj->funcs->cleanup != NULL)
462 obj->funcs->cleanup (obj);
463
464 free (obj);
465}