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