xref: /dflybsd-src/contrib/gdb-7/libiberty/pexecute.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /* Utilities to execute a program in a subprocess (possibly linked by pipes
286d7f5d3SJohn Marino    with other subprocesses), and wait for it.
386d7f5d3SJohn Marino    Copyright (C) 2004 Free Software Foundation, Inc.
486d7f5d3SJohn Marino 
586d7f5d3SJohn Marino This file is part of the libiberty library.
686d7f5d3SJohn Marino Libiberty is free software; you can redistribute it and/or
786d7f5d3SJohn Marino modify it under the terms of the GNU Library General Public
886d7f5d3SJohn Marino License as published by the Free Software Foundation; either
986d7f5d3SJohn Marino version 2 of the License, or (at your option) any later version.
1086d7f5d3SJohn Marino 
1186d7f5d3SJohn Marino Libiberty is distributed in the hope that it will be useful,
1286d7f5d3SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
1386d7f5d3SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1486d7f5d3SJohn Marino Library General Public License for more details.
1586d7f5d3SJohn Marino 
1686d7f5d3SJohn Marino You should have received a copy of the GNU Library General Public
1786d7f5d3SJohn Marino License along with libiberty; see the file COPYING.LIB.  If not,
1886d7f5d3SJohn Marino write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
1986d7f5d3SJohn Marino Boston, MA 02110-1301, USA.  */
2086d7f5d3SJohn Marino 
2186d7f5d3SJohn Marino /* pexecute is an old routine.  This implementation uses the newer
2286d7f5d3SJohn Marino    pex_init/pex_run/pex_get_status/pex_free routines.  Don't use
2386d7f5d3SJohn Marino    pexecute in new code.  Use the newer routines instead.  */
2486d7f5d3SJohn Marino 
2586d7f5d3SJohn Marino #include "config.h"
2686d7f5d3SJohn Marino #include "libiberty.h"
2786d7f5d3SJohn Marino 
2886d7f5d3SJohn Marino #ifdef HAVE_STDLIB_H
2986d7f5d3SJohn Marino #include <stdlib.h>
3086d7f5d3SJohn Marino #endif
3186d7f5d3SJohn Marino 
3286d7f5d3SJohn Marino /* We only permit a single pexecute chain to execute at a time.  This
3386d7f5d3SJohn Marino    was always true anyhow, though it wasn't documented.  */
3486d7f5d3SJohn Marino 
3586d7f5d3SJohn Marino static struct pex_obj *pex;
3686d7f5d3SJohn Marino static int idx;
3786d7f5d3SJohn Marino 
3886d7f5d3SJohn Marino int
pexecute(const char * program,char * const * argv,const char * pname,const char * temp_base,char ** errmsg_fmt,char ** errmsg_arg,int flags)3986d7f5d3SJohn Marino pexecute (const char *program, char * const *argv, const char *pname,
4086d7f5d3SJohn Marino 	  const char *temp_base, char **errmsg_fmt, char **errmsg_arg,
4186d7f5d3SJohn Marino 	  int flags)
4286d7f5d3SJohn Marino {
4386d7f5d3SJohn Marino   const char *errmsg;
4486d7f5d3SJohn Marino   int err;
4586d7f5d3SJohn Marino 
4686d7f5d3SJohn Marino   if ((flags & PEXECUTE_FIRST) != 0)
4786d7f5d3SJohn Marino     {
4886d7f5d3SJohn Marino       if (pex != NULL)
4986d7f5d3SJohn Marino 	{
5086d7f5d3SJohn Marino 	  *errmsg_fmt = (char *) "pexecute already in progress";
5186d7f5d3SJohn Marino 	  *errmsg_arg = NULL;
5286d7f5d3SJohn Marino 	  return -1;
5386d7f5d3SJohn Marino 	}
5486d7f5d3SJohn Marino       pex = pex_init (PEX_USE_PIPES, pname, temp_base);
5586d7f5d3SJohn Marino       idx = 0;
5686d7f5d3SJohn Marino     }
5786d7f5d3SJohn Marino   else
5886d7f5d3SJohn Marino     {
5986d7f5d3SJohn Marino       if (pex == NULL)
6086d7f5d3SJohn Marino 	{
6186d7f5d3SJohn Marino 	  *errmsg_fmt = (char *) "pexecute not in progress";
6286d7f5d3SJohn Marino 	  *errmsg_arg = NULL;
6386d7f5d3SJohn Marino 	  return -1;
6486d7f5d3SJohn Marino 	}
6586d7f5d3SJohn Marino     }
6686d7f5d3SJohn Marino 
6786d7f5d3SJohn Marino   errmsg = pex_run (pex,
6886d7f5d3SJohn Marino 		    (((flags & PEXECUTE_LAST) != 0 ? PEX_LAST : 0)
6986d7f5d3SJohn Marino 		     | ((flags & PEXECUTE_SEARCH) != 0 ? PEX_SEARCH : 0)),
7086d7f5d3SJohn Marino 		    program, argv, NULL, NULL, &err);
7186d7f5d3SJohn Marino   if (errmsg != NULL)
7286d7f5d3SJohn Marino     {
7386d7f5d3SJohn Marino       *errmsg_fmt = (char *) errmsg;
7486d7f5d3SJohn Marino       *errmsg_arg = NULL;
7586d7f5d3SJohn Marino       return -1;
7686d7f5d3SJohn Marino     }
7786d7f5d3SJohn Marino 
7886d7f5d3SJohn Marino   /* Instead of a PID, we just return a one-based index into the
7986d7f5d3SJohn Marino      status values.  We avoid zero just because the old pexecute would
8086d7f5d3SJohn Marino      never return it.  */
8186d7f5d3SJohn Marino   return ++idx;
8286d7f5d3SJohn Marino }
8386d7f5d3SJohn Marino 
8486d7f5d3SJohn Marino int
pwait(int pid,int * status,int flags ATTRIBUTE_UNUSED)8586d7f5d3SJohn Marino pwait (int pid, int *status, int flags ATTRIBUTE_UNUSED)
8686d7f5d3SJohn Marino {
8786d7f5d3SJohn Marino   /* The PID returned by pexecute is one-based.  */
8886d7f5d3SJohn Marino   --pid;
8986d7f5d3SJohn Marino 
9086d7f5d3SJohn Marino   if (pex == NULL || pid < 0 || pid >= idx)
9186d7f5d3SJohn Marino     return -1;
9286d7f5d3SJohn Marino 
9386d7f5d3SJohn Marino   if (pid == 0 && idx == 1)
9486d7f5d3SJohn Marino     {
9586d7f5d3SJohn Marino       if (!pex_get_status (pex, 1, status))
9686d7f5d3SJohn Marino 	return -1;
9786d7f5d3SJohn Marino     }
9886d7f5d3SJohn Marino   else
9986d7f5d3SJohn Marino     {
10086d7f5d3SJohn Marino       int *vector;
10186d7f5d3SJohn Marino 
10286d7f5d3SJohn Marino       vector = XNEWVEC (int, idx);
10386d7f5d3SJohn Marino       if (!pex_get_status (pex, idx, vector))
10486d7f5d3SJohn Marino 	{
10586d7f5d3SJohn Marino 	  free (vector);
10686d7f5d3SJohn Marino 	  return -1;
10786d7f5d3SJohn Marino 	}
10886d7f5d3SJohn Marino       *status = vector[pid];
10986d7f5d3SJohn Marino       free (vector);
11086d7f5d3SJohn Marino     }
11186d7f5d3SJohn Marino 
11286d7f5d3SJohn Marino   /* Assume that we are done after the caller has retrieved the last
11386d7f5d3SJohn Marino      exit status.  The original implementation did not require that
11486d7f5d3SJohn Marino      the exit statuses be retrieved in order, but this implementation
11586d7f5d3SJohn Marino      does.  */
11686d7f5d3SJohn Marino   if (pid + 1 == idx)
11786d7f5d3SJohn Marino     {
11886d7f5d3SJohn Marino       pex_free (pex);
11986d7f5d3SJohn Marino       pex = NULL;
12086d7f5d3SJohn Marino       idx = 0;
12186d7f5d3SJohn Marino     }
12286d7f5d3SJohn Marino 
12386d7f5d3SJohn Marino   return pid + 1;
12486d7f5d3SJohn Marino }
125