/src/binutils-gdb/libiberty/pexecute.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Utilities to execute a program in a subprocess (possibly linked by pipes |
2 | | with other subprocesses), and wait for it. |
3 | | Copyright (C) 2004-2025 Free Software Foundation, Inc. |
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 | | /* pexecute is an old routine. This implementation uses the newer |
22 | | pex_init/pex_run/pex_get_status/pex_free routines. Don't use |
23 | | pexecute in new code. Use the newer routines instead. */ |
24 | | |
25 | | #include "config.h" |
26 | | #include "libiberty.h" |
27 | | |
28 | | #ifdef HAVE_STDLIB_H |
29 | | #include <stdlib.h> |
30 | | #endif |
31 | | |
32 | | /* We only permit a single pexecute chain to execute at a time. This |
33 | | was always true anyhow, though it wasn't documented. */ |
34 | | |
35 | | static struct pex_obj *pex; |
36 | | static int idx; |
37 | | |
38 | | int |
39 | | pexecute (const char *program, char * const *argv, const char *pname, |
40 | | const char *temp_base, char **errmsg_fmt, char **errmsg_arg, |
41 | | int flags) |
42 | 0 | { |
43 | 0 | const char *errmsg; |
44 | 0 | int err; |
45 | |
|
46 | 0 | if ((flags & PEXECUTE_FIRST) != 0) |
47 | 0 | { |
48 | 0 | if (pex != NULL) |
49 | 0 | { |
50 | 0 | *errmsg_fmt = (char *) "pexecute already in progress"; |
51 | 0 | *errmsg_arg = NULL; |
52 | 0 | return -1; |
53 | 0 | } |
54 | 0 | pex = pex_init (PEX_USE_PIPES, pname, temp_base); |
55 | 0 | idx = 0; |
56 | 0 | } |
57 | 0 | else |
58 | 0 | { |
59 | 0 | if (pex == NULL) |
60 | 0 | { |
61 | 0 | *errmsg_fmt = (char *) "pexecute not in progress"; |
62 | 0 | *errmsg_arg = NULL; |
63 | 0 | return -1; |
64 | 0 | } |
65 | 0 | } |
66 | | |
67 | 0 | errmsg = pex_run (pex, |
68 | 0 | (((flags & PEXECUTE_LAST) != 0 ? PEX_LAST : 0) |
69 | 0 | | ((flags & PEXECUTE_SEARCH) != 0 ? PEX_SEARCH : 0)), |
70 | 0 | program, argv, NULL, NULL, &err); |
71 | 0 | if (errmsg != NULL) |
72 | 0 | { |
73 | 0 | *errmsg_fmt = (char *) errmsg; |
74 | 0 | *errmsg_arg = NULL; |
75 | 0 | return -1; |
76 | 0 | } |
77 | | |
78 | | /* Instead of a PID, we just return a one-based index into the |
79 | | status values. We avoid zero just because the old pexecute would |
80 | | never return it. */ |
81 | 0 | return ++idx; |
82 | 0 | } |
83 | | |
84 | | int |
85 | | pwait (int pid, int *status, int flags ATTRIBUTE_UNUSED) |
86 | 0 | { |
87 | | /* The PID returned by pexecute is one-based. */ |
88 | 0 | --pid; |
89 | |
|
90 | 0 | if (pex == NULL || pid < 0 || pid >= idx) |
91 | 0 | return -1; |
92 | | |
93 | 0 | if (pid == 0 && idx == 1) |
94 | 0 | { |
95 | 0 | if (!pex_get_status (pex, 1, status)) |
96 | 0 | return -1; |
97 | 0 | } |
98 | 0 | else |
99 | 0 | { |
100 | 0 | int *vector; |
101 | |
|
102 | 0 | vector = XNEWVEC (int, idx); |
103 | 0 | if (!pex_get_status (pex, idx, vector)) |
104 | 0 | { |
105 | 0 | free (vector); |
106 | 0 | return -1; |
107 | 0 | } |
108 | 0 | *status = vector[pid]; |
109 | 0 | free (vector); |
110 | 0 | } |
111 | | |
112 | | /* Assume that we are done after the caller has retrieved the last |
113 | | exit status. The original implementation did not require that |
114 | | the exit statuses be retrieved in order, but this implementation |
115 | | does. */ |
116 | 0 | if (pid + 1 == idx) |
117 | 0 | { |
118 | 0 | pex_free (pex); |
119 | 0 | pex = NULL; |
120 | 0 | idx = 0; |
121 | 0 | } |
122 | |
|
123 | 0 | return pid + 1; |
124 | 0 | } |