1*fae548d3Szrj /* Utilities to execute a program in a subprocess (possibly linked by pipes
2*fae548d3Szrj with other subprocesses), and wait for it.
3*fae548d3Szrj Copyright (C) 2004-2020 Free Software Foundation, Inc.
4*fae548d3Szrj
5*fae548d3Szrj This file is part of the libiberty library.
6*fae548d3Szrj Libiberty is free software; you can redistribute it and/or
7*fae548d3Szrj modify it under the terms of the GNU Library General Public
8*fae548d3Szrj License as published by the Free Software Foundation; either
9*fae548d3Szrj version 2 of the License, or (at your option) any later version.
10*fae548d3Szrj
11*fae548d3Szrj Libiberty is distributed in the hope that it will be useful,
12*fae548d3Szrj but WITHOUT ANY WARRANTY; without even the implied warranty of
13*fae548d3Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14*fae548d3Szrj Library General Public License for more details.
15*fae548d3Szrj
16*fae548d3Szrj You should have received a copy of the GNU Library General Public
17*fae548d3Szrj License along with libiberty; see the file COPYING.LIB. If not,
18*fae548d3Szrj write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19*fae548d3Szrj Boston, MA 02110-1301, USA. */
20*fae548d3Szrj
21*fae548d3Szrj /* pexecute is an old routine. This implementation uses the newer
22*fae548d3Szrj pex_init/pex_run/pex_get_status/pex_free routines. Don't use
23*fae548d3Szrj pexecute in new code. Use the newer routines instead. */
24*fae548d3Szrj
25*fae548d3Szrj #include "config.h"
26*fae548d3Szrj #include "libiberty.h"
27*fae548d3Szrj
28*fae548d3Szrj #ifdef HAVE_STDLIB_H
29*fae548d3Szrj #include <stdlib.h>
30*fae548d3Szrj #endif
31*fae548d3Szrj
32*fae548d3Szrj /* We only permit a single pexecute chain to execute at a time. This
33*fae548d3Szrj was always true anyhow, though it wasn't documented. */
34*fae548d3Szrj
35*fae548d3Szrj static struct pex_obj *pex;
36*fae548d3Szrj static int idx;
37*fae548d3Szrj
38*fae548d3Szrj int
pexecute(const char * program,char * const * argv,const char * pname,const char * temp_base,char ** errmsg_fmt,char ** errmsg_arg,int flags)39*fae548d3Szrj pexecute (const char *program, char * const *argv, const char *pname,
40*fae548d3Szrj const char *temp_base, char **errmsg_fmt, char **errmsg_arg,
41*fae548d3Szrj int flags)
42*fae548d3Szrj {
43*fae548d3Szrj const char *errmsg;
44*fae548d3Szrj int err;
45*fae548d3Szrj
46*fae548d3Szrj if ((flags & PEXECUTE_FIRST) != 0)
47*fae548d3Szrj {
48*fae548d3Szrj if (pex != NULL)
49*fae548d3Szrj {
50*fae548d3Szrj *errmsg_fmt = (char *) "pexecute already in progress";
51*fae548d3Szrj *errmsg_arg = NULL;
52*fae548d3Szrj return -1;
53*fae548d3Szrj }
54*fae548d3Szrj pex = pex_init (PEX_USE_PIPES, pname, temp_base);
55*fae548d3Szrj idx = 0;
56*fae548d3Szrj }
57*fae548d3Szrj else
58*fae548d3Szrj {
59*fae548d3Szrj if (pex == NULL)
60*fae548d3Szrj {
61*fae548d3Szrj *errmsg_fmt = (char *) "pexecute not in progress";
62*fae548d3Szrj *errmsg_arg = NULL;
63*fae548d3Szrj return -1;
64*fae548d3Szrj }
65*fae548d3Szrj }
66*fae548d3Szrj
67*fae548d3Szrj errmsg = pex_run (pex,
68*fae548d3Szrj (((flags & PEXECUTE_LAST) != 0 ? PEX_LAST : 0)
69*fae548d3Szrj | ((flags & PEXECUTE_SEARCH) != 0 ? PEX_SEARCH : 0)),
70*fae548d3Szrj program, argv, NULL, NULL, &err);
71*fae548d3Szrj if (errmsg != NULL)
72*fae548d3Szrj {
73*fae548d3Szrj *errmsg_fmt = (char *) errmsg;
74*fae548d3Szrj *errmsg_arg = NULL;
75*fae548d3Szrj return -1;
76*fae548d3Szrj }
77*fae548d3Szrj
78*fae548d3Szrj /* Instead of a PID, we just return a one-based index into the
79*fae548d3Szrj status values. We avoid zero just because the old pexecute would
80*fae548d3Szrj never return it. */
81*fae548d3Szrj return ++idx;
82*fae548d3Szrj }
83*fae548d3Szrj
84*fae548d3Szrj int
pwait(int pid,int * status,int flags ATTRIBUTE_UNUSED)85*fae548d3Szrj pwait (int pid, int *status, int flags ATTRIBUTE_UNUSED)
86*fae548d3Szrj {
87*fae548d3Szrj /* The PID returned by pexecute is one-based. */
88*fae548d3Szrj --pid;
89*fae548d3Szrj
90*fae548d3Szrj if (pex == NULL || pid < 0 || pid >= idx)
91*fae548d3Szrj return -1;
92*fae548d3Szrj
93*fae548d3Szrj if (pid == 0 && idx == 1)
94*fae548d3Szrj {
95*fae548d3Szrj if (!pex_get_status (pex, 1, status))
96*fae548d3Szrj return -1;
97*fae548d3Szrj }
98*fae548d3Szrj else
99*fae548d3Szrj {
100*fae548d3Szrj int *vector;
101*fae548d3Szrj
102*fae548d3Szrj vector = XNEWVEC (int, idx);
103*fae548d3Szrj if (!pex_get_status (pex, idx, vector))
104*fae548d3Szrj {
105*fae548d3Szrj free (vector);
106*fae548d3Szrj return -1;
107*fae548d3Szrj }
108*fae548d3Szrj *status = vector[pid];
109*fae548d3Szrj free (vector);
110*fae548d3Szrj }
111*fae548d3Szrj
112*fae548d3Szrj /* Assume that we are done after the caller has retrieved the last
113*fae548d3Szrj exit status. The original implementation did not require that
114*fae548d3Szrj the exit statuses be retrieved in order, but this implementation
115*fae548d3Szrj does. */
116*fae548d3Szrj if (pid + 1 == idx)
117*fae548d3Szrj {
118*fae548d3Szrj pex_free (pex);
119*fae548d3Szrj pex = NULL;
120*fae548d3Szrj idx = 0;
121*fae548d3Szrj }
122*fae548d3Szrj
123*fae548d3Szrj return pid + 1;
124*fae548d3Szrj }
125