| 1 | /* Utilities to execute a program in a subprocess (possibly linked by pipes
|
|---|
| 2 | with other subprocesses), and wait for it. Generic Win32 specialization.
|
|---|
| 3 | Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003
|
|---|
| 4 | Free Software Foundation, Inc.
|
|---|
| 5 |
|
|---|
| 6 | This file is part of the libiberty library.
|
|---|
| 7 | Libiberty is free software; you can redistribute it and/or
|
|---|
| 8 | modify it under the terms of the GNU Library General Public
|
|---|
| 9 | License as published by the Free Software Foundation; either
|
|---|
| 10 | version 2 of the License, or (at your option) any later version.
|
|---|
| 11 |
|
|---|
| 12 | Libiberty is distributed in the hope that it will be useful,
|
|---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 15 | Library General Public License for more details.
|
|---|
| 16 |
|
|---|
| 17 | You should have received a copy of the GNU Library General Public
|
|---|
| 18 | License along with libiberty; see the file COPYING.LIB. If not,
|
|---|
| 19 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|---|
| 20 | Boston, MA 02111-1307, USA. */
|
|---|
| 21 |
|
|---|
| 22 | #include "pex-common.h"
|
|---|
| 23 |
|
|---|
| 24 | #ifdef HAVE_STRING_H
|
|---|
| 25 | #include <string.h>
|
|---|
| 26 | #endif
|
|---|
| 27 | #ifdef HAVE_UNISTD_H
|
|---|
| 28 | #include <unistd.h>
|
|---|
| 29 | #endif
|
|---|
| 30 | #ifdef HAVE_SYS_WAIT_H
|
|---|
| 31 | #include <sys/wait.h>
|
|---|
| 32 | #endif
|
|---|
| 33 |
|
|---|
| 34 | #include <process.h>
|
|---|
| 35 | #include <io.h>
|
|---|
| 36 | #include <fcntl.h>
|
|---|
| 37 | #include <signal.h>
|
|---|
| 38 |
|
|---|
| 39 | /* mingw32 headers may not define the following. */
|
|---|
| 40 |
|
|---|
| 41 | #ifndef _P_WAIT
|
|---|
| 42 | # define _P_WAIT 0
|
|---|
| 43 | # define _P_NOWAIT 1
|
|---|
| 44 | # define _P_OVERLAY 2
|
|---|
| 45 | # define _P_NOWAITO 3
|
|---|
| 46 | # define _P_DETACH 4
|
|---|
| 47 |
|
|---|
| 48 | # define WAIT_CHILD 0
|
|---|
| 49 | # define WAIT_GRANDCHILD 1
|
|---|
| 50 | #endif
|
|---|
| 51 |
|
|---|
| 52 | /* This is a kludge to get around the Microsoft C spawn functions' propensity
|
|---|
| 53 | to remove the outermost set of double quotes from all arguments. */
|
|---|
| 54 |
|
|---|
| 55 | static const char * const *
|
|---|
| 56 | fix_argv (argvec)
|
|---|
| 57 | char **argvec;
|
|---|
| 58 | {
|
|---|
| 59 | int i;
|
|---|
| 60 |
|
|---|
| 61 | for (i = 1; argvec[i] != 0; i++)
|
|---|
| 62 | {
|
|---|
| 63 | int len, j;
|
|---|
| 64 | char *temp, *newtemp;
|
|---|
| 65 |
|
|---|
| 66 | temp = argvec[i];
|
|---|
| 67 | len = strlen (temp);
|
|---|
| 68 | for (j = 0; j < len; j++)
|
|---|
| 69 | {
|
|---|
| 70 | if (temp[j] == '"')
|
|---|
| 71 | {
|
|---|
| 72 | newtemp = xmalloc (len + 2);
|
|---|
| 73 | strncpy (newtemp, temp, j);
|
|---|
| 74 | newtemp [j] = '\\';
|
|---|
| 75 | strncpy (&newtemp [j+1], &temp [j], len-j);
|
|---|
| 76 | newtemp [len+1] = 0;
|
|---|
| 77 | temp = newtemp;
|
|---|
| 78 | len++;
|
|---|
| 79 | j++;
|
|---|
| 80 | }
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | argvec[i] = temp;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | for (i = 0; argvec[i] != 0; i++)
|
|---|
| 87 | {
|
|---|
| 88 | if (strpbrk (argvec[i], " \t"))
|
|---|
| 89 | {
|
|---|
| 90 | int len, trailing_backslash;
|
|---|
| 91 | char *temp;
|
|---|
| 92 |
|
|---|
| 93 | len = strlen (argvec[i]);
|
|---|
| 94 | trailing_backslash = 0;
|
|---|
| 95 |
|
|---|
| 96 | /* There is an added complication when an arg with embedded white
|
|---|
| 97 | space ends in a backslash (such as in the case of -iprefix arg
|
|---|
| 98 | passed to cpp). The resulting quoted strings gets misinterpreted
|
|---|
| 99 | by the command interpreter -- it thinks that the ending quote
|
|---|
| 100 | is escaped by the trailing backslash and things get confused.
|
|---|
| 101 | We handle this case by escaping the trailing backslash, provided
|
|---|
| 102 | it was not escaped in the first place. */
|
|---|
| 103 | if (len > 1
|
|---|
| 104 | && argvec[i][len-1] == '\\'
|
|---|
| 105 | && argvec[i][len-2] != '\\')
|
|---|
| 106 | {
|
|---|
| 107 | trailing_backslash = 1;
|
|---|
| 108 | ++len; /* to escape the final backslash. */
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | len += 2; /* and for the enclosing quotes. */
|
|---|
| 112 |
|
|---|
| 113 | temp = xmalloc (len + 1);
|
|---|
| 114 | temp[0] = '"';
|
|---|
| 115 | strcpy (temp + 1, argvec[i]);
|
|---|
| 116 | if (trailing_backslash)
|
|---|
| 117 | temp[len-2] = '\\';
|
|---|
| 118 | temp[len-1] = '"';
|
|---|
| 119 | temp[len] = '\0';
|
|---|
| 120 |
|
|---|
| 121 | argvec[i] = temp;
|
|---|
| 122 | }
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | return (const char * const *) argvec;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /* Win32 supports pipes */
|
|---|
| 129 | int
|
|---|
| 130 | pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
|
|---|
| 131 | const char *program;
|
|---|
| 132 | char * const *argv;
|
|---|
| 133 | const char *this_pname;
|
|---|
| 134 | const char *temp_base;
|
|---|
| 135 | char **errmsg_fmt, **errmsg_arg;
|
|---|
| 136 | int flags;
|
|---|
| 137 | {
|
|---|
| 138 | int pid;
|
|---|
| 139 | int pdes[2], org_stdin, org_stdout;
|
|---|
| 140 | int input_desc, output_desc;
|
|---|
| 141 | int retries, sleep_interval;
|
|---|
| 142 |
|
|---|
| 143 | /* Pipe waiting from last process, to be used as input for the next one.
|
|---|
| 144 | Value is STDIN_FILE_NO if no pipe is waiting
|
|---|
| 145 | (i.e. the next command is the first of a group). */
|
|---|
| 146 | static int last_pipe_input;
|
|---|
| 147 |
|
|---|
| 148 | /* If this is the first process, initialize. */
|
|---|
| 149 | if (flags & PEXECUTE_FIRST)
|
|---|
| 150 | last_pipe_input = STDIN_FILE_NO;
|
|---|
| 151 |
|
|---|
| 152 | input_desc = last_pipe_input;
|
|---|
| 153 |
|
|---|
| 154 | /* If this isn't the last process, make a pipe for its output,
|
|---|
| 155 | and record it as waiting to be the input to the next process. */
|
|---|
| 156 | if (! (flags & PEXECUTE_LAST))
|
|---|
| 157 | {
|
|---|
| 158 | if (_pipe (pdes, 256, O_BINARY) < 0)
|
|---|
| 159 | {
|
|---|
| 160 | *errmsg_fmt = "pipe";
|
|---|
| 161 | *errmsg_arg = NULL;
|
|---|
| 162 | return -1;
|
|---|
| 163 | }
|
|---|
| 164 | output_desc = pdes[WRITE_PORT];
|
|---|
| 165 | last_pipe_input = pdes[READ_PORT];
|
|---|
| 166 | }
|
|---|
| 167 | else
|
|---|
| 168 | {
|
|---|
| 169 | /* Last process. */
|
|---|
| 170 | output_desc = STDOUT_FILE_NO;
|
|---|
| 171 | last_pipe_input = STDIN_FILE_NO;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | if (input_desc != STDIN_FILE_NO)
|
|---|
| 175 | {
|
|---|
| 176 | org_stdin = dup (STDIN_FILE_NO);
|
|---|
| 177 | dup2 (input_desc, STDIN_FILE_NO);
|
|---|
| 178 | close (input_desc);
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | if (output_desc != STDOUT_FILE_NO)
|
|---|
| 182 | {
|
|---|
| 183 | org_stdout = dup (STDOUT_FILE_NO);
|
|---|
| 184 | dup2 (output_desc, STDOUT_FILE_NO);
|
|---|
| 185 | close (output_desc);
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | pid = (flags & PEXECUTE_SEARCH ? _spawnvp : _spawnv)
|
|---|
| 189 | (_P_NOWAIT, program, fix_argv(argv));
|
|---|
| 190 |
|
|---|
| 191 | if (input_desc != STDIN_FILE_NO)
|
|---|
| 192 | {
|
|---|
| 193 | dup2 (org_stdin, STDIN_FILE_NO);
|
|---|
| 194 | close (org_stdin);
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | if (output_desc != STDOUT_FILE_NO)
|
|---|
| 198 | {
|
|---|
| 199 | dup2 (org_stdout, STDOUT_FILE_NO);
|
|---|
| 200 | close (org_stdout);
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | if (pid == -1)
|
|---|
| 204 | {
|
|---|
| 205 | *errmsg_fmt = install_error_msg;
|
|---|
| 206 | *errmsg_arg = program;
|
|---|
| 207 | return -1;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | return pid;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | /* MS CRTDLL doesn't return enough information in status to decide if the
|
|---|
| 214 | child exited due to a signal or not, rather it simply returns an
|
|---|
| 215 | integer with the exit code of the child; eg., if the child exited with
|
|---|
| 216 | an abort() call and didn't have a handler for SIGABRT, it simply returns
|
|---|
| 217 | with status = 3. We fix the status code to conform to the usual WIF*
|
|---|
| 218 | macros. Note that WIFSIGNALED will never be true under CRTDLL. */
|
|---|
| 219 |
|
|---|
| 220 | int
|
|---|
| 221 | pwait (pid, status, flags)
|
|---|
| 222 | int pid;
|
|---|
| 223 | int *status;
|
|---|
| 224 | int flags;
|
|---|
| 225 | {
|
|---|
| 226 | int termstat;
|
|---|
| 227 |
|
|---|
| 228 | pid = _cwait (&termstat, pid, WAIT_CHILD);
|
|---|
| 229 |
|
|---|
| 230 | /* ??? Here's an opportunity to canonicalize the values in STATUS.
|
|---|
| 231 | Needed? */
|
|---|
| 232 |
|
|---|
| 233 | /* cwait returns the child process exit code in termstat.
|
|---|
| 234 | A value of 3 indicates that the child caught a signal, but not
|
|---|
| 235 | which one. Since only SIGABRT, SIGFPE and SIGINT do anything, we
|
|---|
| 236 | report SIGABRT. */
|
|---|
| 237 | if (termstat == 3)
|
|---|
| 238 | *status = SIGABRT;
|
|---|
| 239 | else
|
|---|
| 240 | *status = (((termstat) & 0xff) << 8);
|
|---|
| 241 |
|
|---|
| 242 | return pid;
|
|---|
| 243 | }
|
|---|