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