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