| 1 | /* Process handling for Windows.
|
|---|
| 2 | Copyright (C) 1996-2016 Free Software Foundation, Inc.
|
|---|
| 3 | This file is part of GNU Make.
|
|---|
| 4 |
|
|---|
| 5 | GNU Make is free software; you can redistribute it and/or modify it under the
|
|---|
| 6 | terms of the GNU General Public License as published by the Free Software
|
|---|
| 7 | Foundation; either version 3 of the License, or (at your option) any later
|
|---|
| 8 | version.
|
|---|
| 9 |
|
|---|
| 10 | GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
|
|---|
| 11 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|---|
| 12 | A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|---|
| 13 |
|
|---|
| 14 | You should have received a copy of the GNU General Public License along with
|
|---|
| 15 | this program. If not, see <http://www.gnu.org/licenses/>. */
|
|---|
| 16 |
|
|---|
| 17 | #include <config.h>
|
|---|
| 18 | #include <stdlib.h>
|
|---|
| 19 | #include <stdio.h>
|
|---|
| 20 | #include <io.h> /* for _get_osfhandle */
|
|---|
| 21 | #ifdef _MSC_VER
|
|---|
| 22 | # include <stddef.h> /* for intptr_t */
|
|---|
| 23 | #else
|
|---|
| 24 | # include <stdint.h>
|
|---|
| 25 | #endif
|
|---|
| 26 | #include <string.h>
|
|---|
| 27 | #include <process.h> /* for msvc _beginthreadex, _endthreadex */
|
|---|
| 28 | #include <signal.h>
|
|---|
| 29 | #include <windows.h>
|
|---|
| 30 |
|
|---|
| 31 | #include "makeint.h"
|
|---|
| 32 | #include "filedef.h"
|
|---|
| 33 | #include "variable.h"
|
|---|
| 34 | #include "sub_proc.h"
|
|---|
| 35 | #include "proc.h"
|
|---|
| 36 | #include "w32err.h"
|
|---|
| 37 | #include "debug.h"
|
|---|
| 38 |
|
|---|
| 39 | #ifdef KMK
|
|---|
| 40 | # include <assert.h>
|
|---|
| 41 | # include "kmkbuiltin.h"
|
|---|
| 42 | extern void kmk_cache_exec_image_a(const char *); /* imagecache.c */
|
|---|
| 43 | #endif
|
|---|
| 44 |
|
|---|
| 45 | static char *make_command_line(char *shell_name, char *exec_path, char **argv);
|
|---|
| 46 |
|
|---|
| 47 | typedef struct sub_process_t {
|
|---|
| 48 | #ifdef KMK
|
|---|
| 49 | enum { kRegular = 0, kSubmit, kSubProcFreed } enmType;
|
|---|
| 50 | intptr_t clue;
|
|---|
| 51 | #endif
|
|---|
| 52 | intptr_t sv_stdin[2];
|
|---|
| 53 | intptr_t sv_stdout[2];
|
|---|
| 54 | intptr_t sv_stderr[2];
|
|---|
| 55 | int using_pipes;
|
|---|
| 56 | char *inp;
|
|---|
| 57 | DWORD incnt;
|
|---|
| 58 | char * volatile outp;
|
|---|
| 59 | volatile DWORD outcnt;
|
|---|
| 60 | char * volatile errp;
|
|---|
| 61 | volatile DWORD errcnt;
|
|---|
| 62 | pid_t pid;
|
|---|
| 63 | int exit_code;
|
|---|
| 64 | int signal;
|
|---|
| 65 | long last_err;
|
|---|
| 66 | long lerrno;
|
|---|
| 67 | } sub_process;
|
|---|
| 68 |
|
|---|
| 69 | static long process_file_io_private(sub_process *pproc, BOOL fNeedToWait); /* bird */
|
|---|
| 70 |
|
|---|
| 71 | /* keep track of children so we can implement a waitpid-like routine */
|
|---|
| 72 | static sub_process *proc_array[MAXIMUM_WAIT_OBJECTS];
|
|---|
| 73 | static int proc_index = 0;
|
|---|
| 74 | static int fake_exits_pending = 0;
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 | /*
|
|---|
| 78 | * Fill a HANDLE list with handles to wait for.
|
|---|
| 79 | */
|
|---|
| 80 | DWORD
|
|---|
| 81 | process_set_handles(HANDLE *handles)
|
|---|
| 82 | {
|
|---|
| 83 | DWORD count = 0;
|
|---|
| 84 | int i;
|
|---|
| 85 |
|
|---|
| 86 | /* Build array of handles to wait for */
|
|---|
| 87 | for (i = 0; i < proc_index; i++) {
|
|---|
| 88 | /* Don't wait on child processes that have already finished */
|
|---|
| 89 | if (fake_exits_pending && proc_array[i]->exit_code)
|
|---|
| 90 | continue;
|
|---|
| 91 |
|
|---|
| 92 | handles[count++] = (HANDLE) proc_array[i]->pid;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | return count;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | #ifndef KMK /* Inefficient! */
|
|---|
| 99 | /*
|
|---|
| 100 | * When a process has been waited for, adjust the wait state
|
|---|
| 101 | * array so that we don't wait for it again
|
|---|
| 102 | */
|
|---|
| 103 | static void
|
|---|
| 104 | process_adjust_wait_state(sub_process* pproc)
|
|---|
| 105 | {
|
|---|
| 106 | int i;
|
|---|
| 107 |
|
|---|
| 108 | if (!proc_index)
|
|---|
| 109 | return;
|
|---|
| 110 |
|
|---|
| 111 | for (i = 0; i < proc_index; i++)
|
|---|
| 112 | if (proc_array[i]->pid == pproc->pid)
|
|---|
| 113 | break;
|
|---|
| 114 |
|
|---|
| 115 | if (i < proc_index) {
|
|---|
| 116 | proc_index--;
|
|---|
| 117 | if (i != proc_index)
|
|---|
| 118 | memmove(&proc_array[i], &proc_array[i+1],
|
|---|
| 119 | (proc_index-i) * sizeof(sub_process*));
|
|---|
| 120 | proc_array[proc_index] = NULL;
|
|---|
| 121 | }
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | #endif /* !KMK */
|
|---|
| 125 |
|
|---|
| 126 | /*
|
|---|
| 127 | * Waits for any of the registered child processes to finish.
|
|---|
| 128 | */
|
|---|
| 129 | static sub_process *
|
|---|
| 130 | process_wait_for_any_private(int block, DWORD* pdwWaitStatus)
|
|---|
| 131 | {
|
|---|
| 132 | HANDLE handles[MAXIMUM_WAIT_OBJECTS];
|
|---|
| 133 | DWORD retval, which;
|
|---|
| 134 | int i;
|
|---|
| 135 |
|
|---|
| 136 | if (!proc_index)
|
|---|
| 137 | return NULL;
|
|---|
| 138 |
|
|---|
| 139 | /* build array of handles to wait for */
|
|---|
| 140 | for (i = 0; i < proc_index; i++) {
|
|---|
| 141 | handles[i] = (HANDLE) proc_array[i]->pid;
|
|---|
| 142 |
|
|---|
| 143 | if (fake_exits_pending && proc_array[i]->exit_code)
|
|---|
| 144 | break;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | /* wait for someone to exit */
|
|---|
| 148 | if (!fake_exits_pending) {
|
|---|
| 149 | #ifdef KMK
|
|---|
| 150 | l_wait_again:
|
|---|
| 151 | #endif
|
|---|
| 152 | retval = WaitForMultipleObjects(proc_index, handles, FALSE, (block ? INFINITE : 0));
|
|---|
| 153 | which = retval - WAIT_OBJECT_0;
|
|---|
| 154 | } else {
|
|---|
| 155 | fake_exits_pending--;
|
|---|
| 156 | retval = !WAIT_FAILED;
|
|---|
| 157 | which = i;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | /* If the pointer is not NULL, set the wait status result variable. */
|
|---|
| 161 | if (pdwWaitStatus)
|
|---|
| 162 | *pdwWaitStatus = retval;
|
|---|
| 163 |
|
|---|
| 164 | /* return pointer to process */
|
|---|
| 165 | if ((retval == WAIT_TIMEOUT) || (retval == WAIT_FAILED)) {
|
|---|
| 166 | return NULL;
|
|---|
| 167 | }
|
|---|
| 168 | else {
|
|---|
| 169 | sub_process* pproc = proc_array[which];
|
|---|
| 170 | #ifdef KMK
|
|---|
| 171 | if (pproc->enmType == kSubmit) {
|
|---|
| 172 | /* Try get the result from kSubmit.c. This may not succeed if the whole
|
|---|
| 173 | result hasn't arrived yet, in which we just restart the wait. */
|
|---|
| 174 | if (kSubmitSubProcGetResult(pproc->clue, &pproc->exit_code, &pproc->signal) != 0) {
|
|---|
| 175 | goto l_wait_again;
|
|---|
| 176 | }
|
|---|
| 177 | }
|
|---|
| 178 | #endif
|
|---|
| 179 | #ifndef KMK /* Inefficient! */
|
|---|
| 180 | process_adjust_wait_state(pproc);
|
|---|
| 181 | #else
|
|---|
| 182 | proc_index--;
|
|---|
| 183 | if ((int)which < proc_index)
|
|---|
| 184 | proc_array[which] = proc_array[proc_index];
|
|---|
| 185 | proc_array[proc_index] = NULL;
|
|---|
| 186 | #endif
|
|---|
| 187 | return pproc;
|
|---|
| 188 | }
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | /*
|
|---|
| 192 | * Terminate a process.
|
|---|
| 193 | */
|
|---|
| 194 | BOOL
|
|---|
| 195 | process_kill(HANDLE proc, int signal)
|
|---|
| 196 | {
|
|---|
| 197 | sub_process* pproc = (sub_process*) proc;
|
|---|
| 198 | #ifdef KMK
|
|---|
| 199 | if (pproc->enmType == kRegular) {
|
|---|
| 200 | #endif
|
|---|
| 201 | pproc->signal = signal;
|
|---|
| 202 | return (TerminateProcess((HANDLE) pproc->pid, signal));
|
|---|
| 203 | #ifdef KMK
|
|---|
| 204 | } else if (pproc->enmType == kSubmit)
|
|---|
| 205 | return kSubmitSubProcKill(pproc->clue, signal) == 0;
|
|---|
| 206 | assert(0);
|
|---|
| 207 | return FALSE;
|
|---|
| 208 | #endif
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | /*
|
|---|
| 212 | * Use this function to register processes you wish to wait for by
|
|---|
| 213 | * calling process_file_io(NULL) or process_wait_any(). This must be done
|
|---|
| 214 | * because it is possible for callers of this library to reuse the same
|
|---|
| 215 | * handle for multiple processes launches :-(
|
|---|
| 216 | */
|
|---|
| 217 | void
|
|---|
| 218 | process_register(HANDLE proc)
|
|---|
| 219 | {
|
|---|
| 220 | #ifdef KMK
|
|---|
| 221 | assert(((sub_process *)proc)->enmType == kRegular);
|
|---|
| 222 | #endif
|
|---|
| 223 | if (proc_index < MAXIMUM_WAIT_OBJECTS)
|
|---|
| 224 | proc_array[proc_index++] = (sub_process *) proc;
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | #ifdef KMK
|
|---|
| 228 |
|
|---|
| 229 | /**
|
|---|
| 230 | * Interface used by kmkbuiltin/kSubmit.c to register stuff going down in a
|
|---|
| 231 | * worker process.
|
|---|
| 232 | *
|
|---|
| 233 | * @returns 0 on success, -1 if there are too many sub-processes already.
|
|---|
| 234 | * @param hEvent The event semaphore to wait on.
|
|---|
| 235 | * @param clue The clue to base.
|
|---|
| 236 | * @param pPid Where to return the pid that job.c expects.
|
|---|
| 237 | */
|
|---|
| 238 | int
|
|---|
| 239 | process_kmk_register_submit(HANDLE hEvent, intptr_t clue, pid_t *pPid)
|
|---|
| 240 | {
|
|---|
| 241 | if (proc_index < MAXIMUM_WAIT_OBJECTS) {
|
|---|
| 242 | sub_process *pSubProc = (sub_process *)xcalloc(sizeof(*pSubProc));
|
|---|
| 243 | pSubProc->enmType = kSubmit;
|
|---|
| 244 | pSubProc->clue = clue;
|
|---|
| 245 | pSubProc->pid = (intptr_t)hEvent;
|
|---|
| 246 |
|
|---|
| 247 | proc_array[proc_index++] = pSubProc;
|
|---|
| 248 | *pPid = (intptr_t)pSubProc;
|
|---|
| 249 | return 0;
|
|---|
| 250 | }
|
|---|
| 251 | return -1;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | /**
|
|---|
| 255 | * Interface used by kmkbuiltin/kRedirect.c to register a spawned process.
|
|---|
| 256 | *
|
|---|
| 257 | * @returns 0 on success, -1 if there are too many sub-processes already.
|
|---|
| 258 | * @param hProcess The process handle.
|
|---|
| 259 | * @param pPid Where to return the pid that job.c expects.
|
|---|
| 260 | */
|
|---|
| 261 | int
|
|---|
| 262 | process_kmk_register_redirect(HANDLE hProcess, pid_t *pPid)
|
|---|
| 263 | {
|
|---|
| 264 | if (proc_index < MAXIMUM_WAIT_OBJECTS) {
|
|---|
| 265 | sub_process *pSubProc = (sub_process *)xcalloc(sizeof(*pSubProc));
|
|---|
| 266 | pSubProc->enmType = kRegular;
|
|---|
| 267 | pSubProc->pid = (intptr_t)hProcess;
|
|---|
| 268 |
|
|---|
| 269 | proc_array[proc_index++] = pSubProc;
|
|---|
| 270 | *pPid = (intptr_t)pSubProc;
|
|---|
| 271 | return 0;
|
|---|
| 272 | }
|
|---|
| 273 | return -1;
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | #endif /* KMK */
|
|---|
| 277 |
|
|---|
| 278 | /*
|
|---|
| 279 | * Return the number of processes that we are still waiting for.
|
|---|
| 280 | */
|
|---|
| 281 | int
|
|---|
| 282 | process_used_slots(void)
|
|---|
| 283 | {
|
|---|
| 284 | return proc_index;
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | /*
|
|---|
| 288 | * Public function which works kind of like waitpid(). Wait for any
|
|---|
| 289 | * of the children to die and return results. To call this function,
|
|---|
| 290 | * you must do 1 of things:
|
|---|
| 291 | *
|
|---|
| 292 | * x = process_easy(...);
|
|---|
| 293 | *
|
|---|
| 294 | * or
|
|---|
| 295 | *
|
|---|
| 296 | * x = process_init_fd();
|
|---|
| 297 | * process_register(x);
|
|---|
| 298 | *
|
|---|
| 299 | * or
|
|---|
| 300 | *
|
|---|
| 301 | * x = process_init();
|
|---|
| 302 | * process_register(x);
|
|---|
| 303 | *
|
|---|
| 304 | * You must NOT then call process_pipe_io() because this function is
|
|---|
| 305 | * not capable of handling automatic notification of any child
|
|---|
| 306 | * death.
|
|---|
| 307 | */
|
|---|
| 308 |
|
|---|
| 309 | HANDLE
|
|---|
| 310 | process_wait_for_any(int block, DWORD* pdwWaitStatus)
|
|---|
| 311 | {
|
|---|
| 312 | sub_process* pproc = process_wait_for_any_private(block, pdwWaitStatus);
|
|---|
| 313 |
|
|---|
| 314 | if (!pproc)
|
|---|
| 315 | return NULL;
|
|---|
| 316 | else {
|
|---|
| 317 | /*
|
|---|
| 318 | * Ouch! can't tell caller if this fails directly. Caller
|
|---|
| 319 | * will have to use process_last_err()
|
|---|
| 320 | */
|
|---|
| 321 | #ifdef KMK
|
|---|
| 322 | /* Invalidate negative directory cache entries now that a
|
|---|
| 323 | job has completed and possibly created new files that
|
|---|
| 324 | was missing earlier. */
|
|---|
| 325 | dir_cache_invalid_after_job ();
|
|---|
| 326 |
|
|---|
| 327 | if (pproc->enmType == kRegular) {
|
|---|
| 328 | (void)process_file_io_private(pproc, FALSE);
|
|---|
| 329 | }
|
|---|
| 330 | #else
|
|---|
| 331 | (void) process_file_io(pproc);
|
|---|
| 332 | #endif
|
|---|
| 333 | return ((HANDLE) pproc);
|
|---|
| 334 | }
|
|---|
| 335 | }
|
|---|
| 336 |
|
|---|
| 337 | long
|
|---|
| 338 | process_signal(HANDLE proc)
|
|---|
| 339 | {
|
|---|
| 340 | if (proc == INVALID_HANDLE_VALUE) return 0;
|
|---|
| 341 | return (((sub_process *)proc)->signal);
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| 344 | long
|
|---|
| 345 | process_last_err(HANDLE proc)
|
|---|
| 346 | {
|
|---|
| 347 | if (proc == INVALID_HANDLE_VALUE) return ERROR_INVALID_HANDLE;
|
|---|
| 348 | return (((sub_process *)proc)->last_err);
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | long
|
|---|
| 352 | process_exit_code(HANDLE proc)
|
|---|
| 353 | {
|
|---|
| 354 | if (proc == INVALID_HANDLE_VALUE) return EXIT_FAILURE;
|
|---|
| 355 | return (((sub_process *)proc)->exit_code);
|
|---|
| 356 | }
|
|---|
| 357 |
|
|---|
| 358 | void
|
|---|
| 359 | process_noinherit(int fd)
|
|---|
| 360 | {
|
|---|
| 361 | HANDLE fh = (HANDLE)_get_osfhandle(fd);
|
|---|
| 362 |
|
|---|
| 363 | if (fh && fh != INVALID_HANDLE_VALUE)
|
|---|
| 364 | SetHandleInformation(fh, HANDLE_FLAG_INHERIT, 0);
|
|---|
| 365 | }
|
|---|
| 366 |
|
|---|
| 367 | /*
|
|---|
| 368 | 2006-02:
|
|---|
| 369 | All the following functions are currently unused.
|
|---|
| 370 | All of them would crash gmake if called with argument INVALID_HANDLE_VALUE.
|
|---|
| 371 | Hence whoever wants to use one of this functions must invent and implement
|
|---|
| 372 | a reasonable error handling for this function.
|
|---|
| 373 |
|
|---|
| 374 | char *
|
|---|
| 375 | process_outbuf(HANDLE proc)
|
|---|
| 376 | {
|
|---|
| 377 | return (((sub_process *)proc)->outp);
|
|---|
| 378 | }
|
|---|
| 379 |
|
|---|
| 380 | char *
|
|---|
| 381 | process_errbuf(HANDLE proc)
|
|---|
| 382 | {
|
|---|
| 383 | return (((sub_process *)proc)->errp);
|
|---|
| 384 | }
|
|---|
| 385 |
|
|---|
| 386 | int
|
|---|
| 387 | process_outcnt(HANDLE proc)
|
|---|
| 388 | {
|
|---|
| 389 | return (((sub_process *)proc)->outcnt);
|
|---|
| 390 | }
|
|---|
| 391 |
|
|---|
| 392 | int
|
|---|
| 393 | process_errcnt(HANDLE proc)
|
|---|
| 394 | {
|
|---|
| 395 | return (((sub_process *)proc)->errcnt);
|
|---|
| 396 | }
|
|---|
| 397 |
|
|---|
| 398 | void
|
|---|
| 399 | process_pipes(HANDLE proc, int pipes[3])
|
|---|
| 400 | {
|
|---|
| 401 | pipes[0] = ((sub_process *)proc)->sv_stdin[0];
|
|---|
| 402 | pipes[1] = ((sub_process *)proc)->sv_stdout[0];
|
|---|
| 403 | pipes[2] = ((sub_process *)proc)->sv_stderr[0];
|
|---|
| 404 | return;
|
|---|
| 405 | }
|
|---|
| 406 | */
|
|---|
| 407 |
|
|---|
| 408 | HANDLE
|
|---|
| 409 | process_init()
|
|---|
| 410 | {
|
|---|
| 411 | sub_process *pproc;
|
|---|
| 412 | /*
|
|---|
| 413 | * open file descriptors for attaching stdin/stdout/sterr
|
|---|
| 414 | */
|
|---|
| 415 | HANDLE stdin_pipes[2];
|
|---|
| 416 | HANDLE stdout_pipes[2];
|
|---|
| 417 | HANDLE stderr_pipes[2];
|
|---|
| 418 | SECURITY_ATTRIBUTES inherit;
|
|---|
| 419 | BYTE sd[SECURITY_DESCRIPTOR_MIN_LENGTH];
|
|---|
| 420 |
|
|---|
| 421 | pproc = malloc(sizeof(*pproc));
|
|---|
| 422 | memset(pproc, 0, sizeof(*pproc));
|
|---|
| 423 |
|
|---|
| 424 | /* We can't use NULL for lpSecurityDescriptor because that
|
|---|
| 425 | uses the default security descriptor of the calling process.
|
|---|
| 426 | Instead we use a security descriptor with no DACL. This
|
|---|
| 427 | allows nonrestricted access to the associated objects. */
|
|---|
| 428 |
|
|---|
| 429 | if (!InitializeSecurityDescriptor((PSECURITY_DESCRIPTOR)(&sd),
|
|---|
| 430 | SECURITY_DESCRIPTOR_REVISION)) {
|
|---|
| 431 | pproc->last_err = GetLastError();
|
|---|
| 432 | pproc->lerrno = E_SCALL;
|
|---|
| 433 | return((HANDLE)pproc);
|
|---|
| 434 | }
|
|---|
| 435 |
|
|---|
| 436 | inherit.nLength = sizeof(inherit);
|
|---|
| 437 | inherit.lpSecurityDescriptor = (PSECURITY_DESCRIPTOR)(&sd);
|
|---|
| 438 | inherit.bInheritHandle = TRUE;
|
|---|
| 439 |
|
|---|
| 440 | // By convention, parent gets pipe[0], and child gets pipe[1]
|
|---|
| 441 | // This means the READ side of stdin pipe goes into pipe[1]
|
|---|
| 442 | // and the WRITE side of the stdout and stderr pipes go into pipe[1]
|
|---|
| 443 | if (CreatePipe( &stdin_pipes[1], &stdin_pipes[0], &inherit, 0) == FALSE ||
|
|---|
| 444 | CreatePipe( &stdout_pipes[0], &stdout_pipes[1], &inherit, 0) == FALSE ||
|
|---|
| 445 | CreatePipe( &stderr_pipes[0], &stderr_pipes[1], &inherit, 0) == FALSE) {
|
|---|
| 446 |
|
|---|
| 447 | pproc->last_err = GetLastError();
|
|---|
| 448 | pproc->lerrno = E_SCALL;
|
|---|
| 449 | return((HANDLE)pproc);
|
|---|
| 450 | }
|
|---|
| 451 |
|
|---|
| 452 | //
|
|---|
| 453 | // Mark the parent sides of the pipes as non-inheritable
|
|---|
| 454 | //
|
|---|
| 455 | if (SetHandleInformation(stdin_pipes[0],
|
|---|
| 456 | HANDLE_FLAG_INHERIT, 0) == FALSE ||
|
|---|
| 457 | SetHandleInformation(stdout_pipes[0],
|
|---|
| 458 | HANDLE_FLAG_INHERIT, 0) == FALSE ||
|
|---|
| 459 | SetHandleInformation(stderr_pipes[0],
|
|---|
| 460 | HANDLE_FLAG_INHERIT, 0) == FALSE) {
|
|---|
| 461 |
|
|---|
| 462 | pproc->last_err = GetLastError();
|
|---|
| 463 | pproc->lerrno = E_SCALL;
|
|---|
| 464 | return((HANDLE)pproc);
|
|---|
| 465 | }
|
|---|
| 466 | pproc->sv_stdin[0] = (intptr_t) stdin_pipes[0];
|
|---|
| 467 | pproc->sv_stdin[1] = (intptr_t) stdin_pipes[1];
|
|---|
| 468 | pproc->sv_stdout[0] = (intptr_t) stdout_pipes[0];
|
|---|
| 469 | pproc->sv_stdout[1] = (intptr_t) stdout_pipes[1];
|
|---|
| 470 | pproc->sv_stderr[0] = (intptr_t) stderr_pipes[0];
|
|---|
| 471 | pproc->sv_stderr[1] = (intptr_t) stderr_pipes[1];
|
|---|
| 472 |
|
|---|
| 473 | pproc->using_pipes = 1;
|
|---|
| 474 |
|
|---|
| 475 | pproc->lerrno = 0;
|
|---|
| 476 |
|
|---|
| 477 | return((HANDLE)pproc);
|
|---|
| 478 | }
|
|---|
| 479 |
|
|---|
| 480 |
|
|---|
| 481 | HANDLE
|
|---|
| 482 | process_init_fd(HANDLE stdinh, HANDLE stdouth, HANDLE stderrh)
|
|---|
| 483 | {
|
|---|
| 484 | sub_process *pproc;
|
|---|
| 485 |
|
|---|
| 486 | pproc = malloc(sizeof(*pproc));
|
|---|
| 487 | if (pproc) {
|
|---|
| 488 | memset(pproc, 0, sizeof(*pproc));
|
|---|
| 489 |
|
|---|
| 490 | /*
|
|---|
| 491 | * Just pass the provided file handles to the 'child
|
|---|
| 492 | * side' of the pipe, bypassing pipes altogether.
|
|---|
| 493 | */
|
|---|
| 494 | pproc->sv_stdin[1] = (intptr_t) stdinh;
|
|---|
| 495 | pproc->sv_stdout[1] = (intptr_t) stdouth;
|
|---|
| 496 | pproc->sv_stderr[1] = (intptr_t) stderrh;
|
|---|
| 497 |
|
|---|
| 498 | pproc->last_err = pproc->lerrno = 0;
|
|---|
| 499 | }
|
|---|
| 500 |
|
|---|
| 501 | return((HANDLE)pproc);
|
|---|
| 502 | }
|
|---|
| 503 |
|
|---|
| 504 |
|
|---|
| 505 | static HANDLE
|
|---|
| 506 | find_file(const char *exec_path, const char *path_var,
|
|---|
| 507 | char *full_fname, DWORD full_len)
|
|---|
| 508 | {
|
|---|
| 509 | HANDLE exec_handle;
|
|---|
| 510 | char *fname;
|
|---|
| 511 | char *ext;
|
|---|
| 512 | DWORD req_len;
|
|---|
| 513 | int i;
|
|---|
| 514 | static const char *extensions[] =
|
|---|
| 515 | /* Should .com come before no-extension case? */
|
|---|
| 516 | { ".exe", ".cmd", ".bat", "", ".com", NULL };
|
|---|
| 517 |
|
|---|
| 518 | fname = xmalloc(strlen(exec_path) + 5);
|
|---|
| 519 | strcpy(fname, exec_path);
|
|---|
| 520 | ext = fname + strlen(fname);
|
|---|
| 521 |
|
|---|
| 522 | for (i = 0; extensions[i]; i++) {
|
|---|
| 523 | strcpy(ext, extensions[i]);
|
|---|
| 524 | if (((req_len = SearchPath (path_var, fname, NULL, full_len,
|
|---|
| 525 | full_fname, NULL)) > 0
|
|---|
| 526 | /* For compatibility with previous code, which
|
|---|
| 527 | used OpenFile, and with Windows operation in
|
|---|
| 528 | general, also look in various default
|
|---|
| 529 | locations, such as Windows directory and
|
|---|
| 530 | Windows System directory. Warning: this also
|
|---|
| 531 | searches PATH in the Make's environment, which
|
|---|
| 532 | might not be what the Makefile wants, but it
|
|---|
| 533 | seems to be OK as a fallback, after the
|
|---|
| 534 | previous SearchPath failed to find on child's
|
|---|
| 535 | PATH. */
|
|---|
| 536 | || (req_len = SearchPath (NULL, fname, NULL, full_len,
|
|---|
| 537 | full_fname, NULL)) > 0)
|
|---|
| 538 | && req_len <= full_len
|
|---|
| 539 | && (exec_handle =
|
|---|
| 540 | CreateFile(full_fname,
|
|---|
| 541 | GENERIC_READ,
|
|---|
| 542 | FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|---|
| 543 | NULL,
|
|---|
| 544 | OPEN_EXISTING,
|
|---|
| 545 | FILE_ATTRIBUTE_NORMAL,
|
|---|
| 546 | NULL)) != INVALID_HANDLE_VALUE) {
|
|---|
| 547 | free(fname);
|
|---|
| 548 | return(exec_handle);
|
|---|
| 549 | }
|
|---|
| 550 | }
|
|---|
| 551 |
|
|---|
| 552 | free(fname);
|
|---|
| 553 | return INVALID_HANDLE_VALUE;
|
|---|
| 554 | }
|
|---|
| 555 |
|
|---|
| 556 | /*
|
|---|
| 557 | * Return non-zero of FNAME specifies a batch file and its name
|
|---|
| 558 | * includes embedded whitespace.
|
|---|
| 559 | */
|
|---|
| 560 |
|
|---|
| 561 | static int
|
|---|
| 562 | batch_file_with_spaces(const char *fname)
|
|---|
| 563 | {
|
|---|
| 564 | size_t fnlen = strlen(fname);
|
|---|
| 565 |
|
|---|
| 566 | return (fnlen > 4
|
|---|
| 567 | && (_strnicmp(fname + fnlen - 4, ".bat", 4) == 0
|
|---|
| 568 | || _strnicmp(fname + fnlen - 4, ".cmd", 4) == 0)
|
|---|
| 569 | /* The set of characters in the 2nd arg to strpbrk
|
|---|
| 570 | should be the same one used by make_command_line
|
|---|
| 571 | below to decide whether an argv[] element needs
|
|---|
| 572 | quoting. */
|
|---|
| 573 | && strpbrk(fname, " \t") != NULL);
|
|---|
| 574 | }
|
|---|
| 575 |
|
|---|
| 576 |
|
|---|
| 577 | /*
|
|---|
| 578 | * Description: Create the child process to be helped
|
|---|
| 579 | *
|
|---|
| 580 | * Returns: success <=> 0
|
|---|
| 581 | *
|
|---|
| 582 | * Notes/Dependencies:
|
|---|
| 583 | */
|
|---|
| 584 | long
|
|---|
| 585 | process_begin(
|
|---|
| 586 | HANDLE proc,
|
|---|
| 587 | char **argv,
|
|---|
| 588 | char **envp,
|
|---|
| 589 | char *exec_path,
|
|---|
| 590 | char *as_user)
|
|---|
| 591 | {
|
|---|
| 592 | sub_process *pproc = (sub_process *)proc;
|
|---|
| 593 | char *shell_name = 0;
|
|---|
| 594 | int file_not_found=0;
|
|---|
| 595 | HANDLE exec_handle;
|
|---|
| 596 | char exec_fname[MAX_PATH];
|
|---|
| 597 | const char *path_var = NULL;
|
|---|
| 598 | char **ep;
|
|---|
| 599 | char buf[MAX_PATH];
|
|---|
| 600 | DWORD bytes_returned;
|
|---|
| 601 | DWORD flags;
|
|---|
| 602 | char *command_line;
|
|---|
| 603 | STARTUPINFO startInfo;
|
|---|
| 604 | PROCESS_INFORMATION procInfo;
|
|---|
| 605 | char *envblk=NULL;
|
|---|
| 606 | int envsize_needed = 0;
|
|---|
| 607 | int pass_null_exec_path = 0;
|
|---|
| 608 | #ifdef KMK
|
|---|
| 609 | size_t exec_path_len;
|
|---|
| 610 | extern int process_priority;
|
|---|
| 611 |
|
|---|
| 612 | assert (pproc->enmType == kRegular);
|
|---|
| 613 | #endif
|
|---|
| 614 |
|
|---|
| 615 | /*
|
|---|
| 616 | * Shell script detection... if the exec_path starts with #! then
|
|---|
| 617 | * we want to exec shell-script-name exec-path, not just exec-path
|
|---|
| 618 | * NT doesn't recognize #!/bin/sh or #!/etc/Tivoli/bin/perl. We do not
|
|---|
| 619 | * hard-code the path to the shell or perl or whatever: Instead, we
|
|---|
| 620 | * assume it's in the path somewhere (generally, the NT tools
|
|---|
| 621 | * bin directory)
|
|---|
| 622 | */
|
|---|
| 623 |
|
|---|
| 624 | #ifdef KMK
|
|---|
| 625 | /* kmk performance: Don't bother looking for shell scripts in .exe files. */
|
|---|
| 626 | exec_path_len = strlen(exec_path);
|
|---|
| 627 | if (exec_path_len > 4
|
|---|
| 628 | && exec_path[exec_path_len - 4] == '.'
|
|---|
| 629 | && !stricmp(exec_path + exec_path_len - 3, "exe")) {
|
|---|
| 630 | exec_handle = INVALID_HANDLE_VALUE;
|
|---|
| 631 | exec_fname[0] = '\0';
|
|---|
| 632 | }
|
|---|
| 633 | else {
|
|---|
| 634 | #endif /* KMK */
|
|---|
| 635 |
|
|---|
| 636 | /* Use the Makefile's value of PATH to look for the program to
|
|---|
| 637 | execute, because it could be different from Make's PATH
|
|---|
| 638 | (e.g., if the target sets its own value. */
|
|---|
| 639 | if (envp)
|
|---|
| 640 | for (ep = envp; *ep; ep++) {
|
|---|
| 641 | if (strncmp (*ep, "PATH=", 5) == 0
|
|---|
| 642 | || strncmp (*ep, "Path=", 5) == 0) {
|
|---|
| 643 | path_var = *ep + 5;
|
|---|
| 644 | break;
|
|---|
| 645 | }
|
|---|
| 646 | }
|
|---|
| 647 | exec_handle = find_file(exec_path, path_var,
|
|---|
| 648 | exec_fname, sizeof(exec_fname));
|
|---|
| 649 | #ifdef KMK
|
|---|
| 650 | }
|
|---|
| 651 | #endif
|
|---|
| 652 |
|
|---|
| 653 | /*
|
|---|
| 654 | * If we couldn't open the file, just assume that Windows will be
|
|---|
| 655 | * somehow able to find and execute it. If the first character
|
|---|
| 656 | * of the command is '/', assume they set SHELL to a Unixy shell
|
|---|
| 657 | * that have some magic mounts known only to it, and run the whole
|
|---|
| 658 | * command via $SHELL -c "COMMAND" instead.
|
|---|
| 659 | */
|
|---|
| 660 | if (exec_handle == INVALID_HANDLE_VALUE) {
|
|---|
| 661 | if (exec_path[0] == '/') {
|
|---|
| 662 | char *new_argv0;
|
|---|
| 663 | char **argvi = argv;
|
|---|
| 664 | int arglen = 0;
|
|---|
| 665 |
|
|---|
| 666 | strcpy(buf, variable_expand ("$(SHELL)"));
|
|---|
| 667 | shell_name = &buf[0];
|
|---|
| 668 | strcpy(exec_fname, "-c");
|
|---|
| 669 | /* Construct a single command string in argv[0]. */
|
|---|
| 670 | while (*argvi) {
|
|---|
| 671 | arglen += strlen(*argvi) + 1;
|
|---|
| 672 | argvi++;
|
|---|
| 673 | }
|
|---|
| 674 | new_argv0 = xmalloc(arglen + 1);
|
|---|
| 675 | new_argv0[0] = '\0';
|
|---|
| 676 | for (argvi = argv; *argvi; argvi++) {
|
|---|
| 677 | strcat(new_argv0, *argvi);
|
|---|
| 678 | strcat(new_argv0, " ");
|
|---|
| 679 | }
|
|---|
| 680 | /* Remove the extra blank at the end. */
|
|---|
| 681 | new_argv0[arglen-1] = '\0';
|
|---|
| 682 | free(argv[0]);
|
|---|
| 683 | argv[0] = new_argv0;
|
|---|
| 684 | argv[1] = NULL;
|
|---|
| 685 | }
|
|---|
| 686 | else
|
|---|
| 687 | file_not_found++;
|
|---|
| 688 | }
|
|---|
| 689 | else {
|
|---|
| 690 | /* Attempt to read the first line of the file */
|
|---|
| 691 | if (ReadFile( exec_handle,
|
|---|
| 692 | buf, sizeof(buf) - 1, /* leave room for trailing NULL */
|
|---|
| 693 | &bytes_returned, 0) == FALSE || bytes_returned < 2) {
|
|---|
| 694 |
|
|---|
| 695 | pproc->last_err = GetLastError();
|
|---|
| 696 | pproc->lerrno = E_IO;
|
|---|
| 697 | CloseHandle(exec_handle);
|
|---|
| 698 | return(-1);
|
|---|
| 699 | }
|
|---|
| 700 | if (buf[0] == '#' && buf[1] == '!') {
|
|---|
| 701 | /*
|
|---|
| 702 | * This is a shell script... Change the command line from
|
|---|
| 703 | * exec_path args to shell_name exec_path args
|
|---|
| 704 | */
|
|---|
| 705 | char *p;
|
|---|
| 706 |
|
|---|
| 707 | /* Make sure buf is NULL terminated */
|
|---|
| 708 | buf[bytes_returned] = 0;
|
|---|
| 709 | /*
|
|---|
| 710 | * Depending on the file system type, etc. the first line
|
|---|
| 711 | * of the shell script may end with newline or newline-carriage-return
|
|---|
| 712 | * Whatever it ends with, cut it off.
|
|---|
| 713 | */
|
|---|
| 714 | p= strchr(buf, '\n');
|
|---|
| 715 | if (p)
|
|---|
| 716 | *p = 0;
|
|---|
| 717 | p = strchr(buf, '\r');
|
|---|
| 718 | if (p)
|
|---|
| 719 | *p = 0;
|
|---|
| 720 |
|
|---|
| 721 | /*
|
|---|
| 722 | * Find base name of shell
|
|---|
| 723 | */
|
|---|
| 724 | shell_name = strrchr( buf, '/');
|
|---|
| 725 | if (shell_name) {
|
|---|
| 726 | shell_name++;
|
|---|
| 727 | } else {
|
|---|
| 728 | shell_name = &buf[2];/* skipping "#!" */
|
|---|
| 729 | }
|
|---|
| 730 |
|
|---|
| 731 | }
|
|---|
| 732 | CloseHandle(exec_handle);
|
|---|
| 733 | }
|
|---|
| 734 |
|
|---|
| 735 | flags = 0;
|
|---|
| 736 |
|
|---|
| 737 | if (file_not_found)
|
|---|
| 738 | command_line = make_command_line( shell_name, exec_path, argv);
|
|---|
| 739 | else {
|
|---|
| 740 | /* If exec_fname includes whitespace, CreateProcess
|
|---|
| 741 | behaves erratically and unreliably, and often fails
|
|---|
| 742 | if argv[0] also includes whitespace (and thus will
|
|---|
| 743 | be quoted by make_command_line below). So in that
|
|---|
| 744 | case, we don't pass exec_fname as the 1st arg to
|
|---|
| 745 | CreateProcess, but instead replace argv[0] with
|
|---|
| 746 | exec_fname (to keep its leading directories and
|
|---|
| 747 | extension as found by find_file), and pass NULL to
|
|---|
| 748 | CreateProcess as its 1st arg. This works around
|
|---|
| 749 | the bugs in CreateProcess, which are probably
|
|---|
| 750 | caused by its passing the command to cmd.exe with
|
|---|
| 751 | some incorrect quoting. */
|
|---|
| 752 | if (!shell_name
|
|---|
| 753 | && batch_file_with_spaces(exec_fname)
|
|---|
| 754 | && _stricmp(exec_path, argv[0]) == 0) {
|
|---|
| 755 | char *new_argv, *p;
|
|---|
| 756 | char **argvi;
|
|---|
| 757 | int arglen, i;
|
|---|
| 758 | pass_null_exec_path = 1;
|
|---|
| 759 | /* Rewrite argv[] replacing argv[0] with exec_fname. */
|
|---|
| 760 | for (argvi = argv + 1, arglen = strlen(exec_fname) + 1;
|
|---|
| 761 | *argvi;
|
|---|
| 762 | argvi++) {
|
|---|
| 763 | arglen += strlen(*argvi) + 1;
|
|---|
| 764 | }
|
|---|
| 765 | new_argv = xmalloc(arglen);
|
|---|
| 766 | p = strcpy(new_argv, exec_fname) + strlen(exec_fname) + 1;
|
|---|
| 767 | for (argvi = argv + 1, i = 1; *argvi; argvi++, i++) {
|
|---|
| 768 | strcpy(p, *argvi);
|
|---|
| 769 | argv[i] = p;
|
|---|
| 770 | p += strlen(*argvi) + 1;
|
|---|
| 771 | }
|
|---|
| 772 | argv[i] = NULL;
|
|---|
| 773 | free (argv[0]);
|
|---|
| 774 | argv[0] = new_argv;
|
|---|
| 775 | }
|
|---|
| 776 | command_line = make_command_line( shell_name, exec_fname, argv);
|
|---|
| 777 | }
|
|---|
| 778 |
|
|---|
| 779 | if ( command_line == NULL ) {
|
|---|
| 780 | pproc->last_err = 0;
|
|---|
| 781 | pproc->lerrno = E_NO_MEM;
|
|---|
| 782 | return(-1);
|
|---|
| 783 | }
|
|---|
| 784 |
|
|---|
| 785 | if (envp) {
|
|---|
| 786 | if (arr2envblk(envp, &envblk, &envsize_needed) == FALSE) {
|
|---|
| 787 | pproc->lerrno = E_NO_MEM;
|
|---|
| 788 | free( command_line );
|
|---|
| 789 | if ((pproc->last_err == ERROR_INVALID_PARAMETER
|
|---|
| 790 | || pproc->last_err == ERROR_MORE_DATA)
|
|---|
| 791 | && envsize_needed > 32*1024) {
|
|---|
| 792 | fprintf (stderr, "CreateProcess failed, probably because environment is too large (%d bytes).\n",
|
|---|
| 793 | envsize_needed);
|
|---|
| 794 | }
|
|---|
| 795 | pproc->last_err = 0;
|
|---|
| 796 | return(-1);
|
|---|
| 797 | }
|
|---|
| 798 | }
|
|---|
| 799 |
|
|---|
| 800 | if (shell_name || file_not_found || pass_null_exec_path) {
|
|---|
| 801 | exec_path = 0; /* Search for the program in %Path% */
|
|---|
| 802 | } else {
|
|---|
| 803 | exec_path = exec_fname;
|
|---|
| 804 | }
|
|---|
| 805 |
|
|---|
| 806 | /*
|
|---|
| 807 | * Set up inherited stdin, stdout, stderr for child
|
|---|
| 808 | */
|
|---|
| 809 | memset(&startInfo, '\0', sizeof(startInfo));
|
|---|
| 810 | GetStartupInfo(&startInfo);
|
|---|
| 811 | #ifndef KMK
|
|---|
| 812 | startInfo.dwFlags = STARTF_USESTDHANDLES;
|
|---|
| 813 | #endif
|
|---|
| 814 | startInfo.lpReserved = 0;
|
|---|
| 815 | startInfo.cbReserved2 = 0;
|
|---|
| 816 | startInfo.lpReserved2 = 0;
|
|---|
| 817 | #ifndef KMK
|
|---|
| 818 | startInfo.hStdInput = (HANDLE)pproc->sv_stdin[1];
|
|---|
| 819 | startInfo.hStdOutput = (HANDLE)pproc->sv_stdout[1];
|
|---|
| 820 | startInfo.hStdError = (HANDLE)pproc->sv_stderr[1];
|
|---|
| 821 | #else
|
|---|
| 822 | if ( ((HANDLE)pproc->sv_stdin[1] != INVALID_HANDLE_VALUE && pproc->sv_stdin[1])
|
|---|
| 823 | || ((HANDLE)pproc->sv_stdout[1] != INVALID_HANDLE_VALUE && pproc->sv_stdout[1])
|
|---|
| 824 | || ((HANDLE)pproc->sv_stderr[1] != INVALID_HANDLE_VALUE && pproc->sv_stderr[1]) ) {
|
|---|
| 825 | startInfo.dwFlags = STARTF_USESTDHANDLES;
|
|---|
| 826 | startInfo.hStdInput = (HANDLE)pproc->sv_stdin[1];
|
|---|
| 827 | startInfo.hStdOutput = (HANDLE)pproc->sv_stdout[1];
|
|---|
| 828 | startInfo.hStdError = (HANDLE)pproc->sv_stderr[1];
|
|---|
| 829 | } else {
|
|---|
| 830 | startInfo.dwFlags = 0;
|
|---|
| 831 | startInfo.hStdInput = 0;
|
|---|
| 832 | startInfo.hStdOutput = 0;
|
|---|
| 833 | startInfo.hStdError = 0;
|
|---|
| 834 | }
|
|---|
| 835 | #endif
|
|---|
| 836 |
|
|---|
| 837 | if (as_user) {
|
|---|
| 838 | free(envblk);
|
|---|
| 839 | return -1;
|
|---|
| 840 | } else {
|
|---|
| 841 | DB (DB_JOBS, ("CreateProcess(%s,%s,...)\n",
|
|---|
| 842 | exec_path ? exec_path : "NULL",
|
|---|
| 843 | command_line ? command_line : "NULL"));
|
|---|
| 844 | #ifdef KMK
|
|---|
| 845 | if (exec_fname[0])
|
|---|
| 846 | kmk_cache_exec_image_a(exec_fname);
|
|---|
| 847 | else if (exec_path)
|
|---|
| 848 | kmk_cache_exec_image_a(exec_path);
|
|---|
| 849 | else if (argv[0])
|
|---|
| 850 | kmk_cache_exec_image_a(argv[0]);
|
|---|
| 851 |
|
|---|
| 852 | switch (process_priority) {
|
|---|
| 853 | case 1: flags |= CREATE_SUSPENDED | IDLE_PRIORITY_CLASS; break;
|
|---|
| 854 | case 2: flags |= CREATE_SUSPENDED | BELOW_NORMAL_PRIORITY_CLASS; break;
|
|---|
| 855 | case 3: flags |= CREATE_SUSPENDED | NORMAL_PRIORITY_CLASS; break;
|
|---|
| 856 | case 4: flags |= CREATE_SUSPENDED | HIGH_PRIORITY_CLASS; break;
|
|---|
| 857 | case 5: flags |= CREATE_SUSPENDED | REALTIME_PRIORITY_CLASS; break;
|
|---|
| 858 | }
|
|---|
| 859 | #endif
|
|---|
| 860 | if (CreateProcess(
|
|---|
| 861 | exec_path,
|
|---|
| 862 | command_line,
|
|---|
| 863 | NULL,
|
|---|
| 864 | 0, /* default security attributes for thread */
|
|---|
| 865 | TRUE, /* inherit handles (e.g. helper pipes, oserv socket) */
|
|---|
| 866 | flags,
|
|---|
| 867 | envblk,
|
|---|
| 868 | 0, /* default starting directory */
|
|---|
| 869 | &startInfo,
|
|---|
| 870 | &procInfo) == FALSE) {
|
|---|
| 871 |
|
|---|
| 872 | pproc->last_err = GetLastError();
|
|---|
| 873 | pproc->lerrno = E_FORK;
|
|---|
| 874 | fprintf(stderr, "process_begin: CreateProcess(%s, %s, ...) failed.\n",
|
|---|
| 875 | exec_path ? exec_path : "NULL", command_line);
|
|---|
| 876 | free(envblk);
|
|---|
| 877 | free( command_line );
|
|---|
| 878 | return(-1);
|
|---|
| 879 | }
|
|---|
| 880 | #ifdef KMK
|
|---|
| 881 | switch (process_priority) {
|
|---|
| 882 | case 1: SetThreadPriority(procInfo.hThread, THREAD_PRIORITY_IDLE); break;
|
|---|
| 883 | case 2: SetThreadPriority(procInfo.hThread, THREAD_PRIORITY_BELOW_NORMAL); break;
|
|---|
| 884 | case 3: SetThreadPriority(procInfo.hThread, THREAD_PRIORITY_NORMAL); break;
|
|---|
| 885 | case 4: SetThreadPriority(procInfo.hThread, THREAD_PRIORITY_HIGHEST); break;
|
|---|
| 886 | case 5: SetThreadPriority(procInfo.hThread, THREAD_PRIORITY_TIME_CRITICAL); break;
|
|---|
| 887 | }
|
|---|
| 888 | ResumeThread(procInfo.hThread);
|
|---|
| 889 | #endif
|
|---|
| 890 | }
|
|---|
| 891 |
|
|---|
| 892 | pproc->pid = (pid_t)procInfo.hProcess;
|
|---|
| 893 | /* Close the thread handle -- we'll just watch the process */
|
|---|
| 894 | CloseHandle(procInfo.hThread);
|
|---|
| 895 |
|
|---|
| 896 | /* Close the halves of the pipes we don't need */
|
|---|
| 897 | if ((HANDLE)pproc->sv_stdin[1] != INVALID_HANDLE_VALUE && pproc->sv_stdin[1])
|
|---|
| 898 | CloseHandle((HANDLE)pproc->sv_stdin[1]);
|
|---|
| 899 | if ((HANDLE)pproc->sv_stdout[1] != INVALID_HANDLE_VALUE && pproc->sv_stdout[1])
|
|---|
| 900 | CloseHandle((HANDLE)pproc->sv_stdout[1]);
|
|---|
| 901 | if ((HANDLE)pproc->sv_stderr[1] != INVALID_HANDLE_VALUE && pproc->sv_stderr[1])
|
|---|
| 902 | CloseHandle((HANDLE)pproc->sv_stderr[1]);
|
|---|
| 903 | pproc->sv_stdin[1] = 0;
|
|---|
| 904 | pproc->sv_stdout[1] = 0;
|
|---|
| 905 | pproc->sv_stderr[1] = 0;
|
|---|
| 906 |
|
|---|
| 907 | free( command_line );
|
|---|
| 908 | free(envblk);
|
|---|
| 909 | pproc->lerrno=0;
|
|---|
| 910 | return 0;
|
|---|
| 911 | }
|
|---|
| 912 |
|
|---|
| 913 |
|
|---|
| 914 |
|
|---|
| 915 | #if 0 /* unused */
|
|---|
| 916 | static DWORD
|
|---|
| 917 | proc_stdin_thread(sub_process *pproc)
|
|---|
| 918 | {
|
|---|
| 919 | DWORD in_done;
|
|---|
| 920 | for (;;) {
|
|---|
| 921 | if (WriteFile( (HANDLE) pproc->sv_stdin[0], pproc->inp, pproc->incnt,
|
|---|
| 922 | &in_done, NULL) == FALSE)
|
|---|
| 923 | _endthreadex(0);
|
|---|
| 924 | // This if should never be true for anonymous pipes, but gives
|
|---|
| 925 | // us a chance to change I/O mechanisms later
|
|---|
| 926 | if (in_done < pproc->incnt) {
|
|---|
| 927 | pproc->incnt -= in_done;
|
|---|
| 928 | pproc->inp += in_done;
|
|---|
| 929 | } else {
|
|---|
| 930 | _endthreadex(0);
|
|---|
| 931 | }
|
|---|
| 932 | }
|
|---|
| 933 | return 0; // for compiler warnings only.. not reached
|
|---|
| 934 | }
|
|---|
| 935 |
|
|---|
| 936 | static DWORD
|
|---|
| 937 | proc_stdout_thread(sub_process *pproc)
|
|---|
| 938 | {
|
|---|
| 939 | DWORD bufsize = 1024;
|
|---|
| 940 | char c;
|
|---|
| 941 | DWORD nread;
|
|---|
| 942 | pproc->outp = malloc(bufsize);
|
|---|
| 943 | if (pproc->outp == NULL)
|
|---|
| 944 | _endthreadex(0);
|
|---|
| 945 | pproc->outcnt = 0;
|
|---|
| 946 |
|
|---|
| 947 | for (;;) {
|
|---|
| 948 | if (ReadFile( (HANDLE)pproc->sv_stdout[0], &c, 1, &nread, NULL)
|
|---|
| 949 | == FALSE) {
|
|---|
| 950 | /* map_windows32_error_to_string(GetLastError());*/
|
|---|
| 951 | _endthreadex(0);
|
|---|
| 952 | }
|
|---|
| 953 | if (nread == 0)
|
|---|
| 954 | _endthreadex(0);
|
|---|
| 955 | if (pproc->outcnt + nread > bufsize) {
|
|---|
| 956 | bufsize += nread + 512;
|
|---|
| 957 | pproc->outp = realloc(pproc->outp, bufsize);
|
|---|
| 958 | if (pproc->outp == NULL) {
|
|---|
| 959 | pproc->outcnt = 0;
|
|---|
| 960 | _endthreadex(0);
|
|---|
| 961 | }
|
|---|
| 962 | }
|
|---|
| 963 | pproc->outp[pproc->outcnt++] = c;
|
|---|
| 964 | }
|
|---|
| 965 | return 0;
|
|---|
| 966 | }
|
|---|
| 967 |
|
|---|
| 968 | static DWORD
|
|---|
| 969 | proc_stderr_thread(sub_process *pproc)
|
|---|
| 970 | {
|
|---|
| 971 | DWORD bufsize = 1024;
|
|---|
| 972 | char c;
|
|---|
| 973 | DWORD nread;
|
|---|
| 974 | pproc->errp = malloc(bufsize);
|
|---|
| 975 | if (pproc->errp == NULL)
|
|---|
| 976 | _endthreadex(0);
|
|---|
| 977 | pproc->errcnt = 0;
|
|---|
| 978 |
|
|---|
| 979 | for (;;) {
|
|---|
| 980 | if (ReadFile( (HANDLE)pproc->sv_stderr[0], &c, 1, &nread, NULL) == FALSE) {
|
|---|
| 981 | map_windows32_error_to_string(GetLastError());
|
|---|
| 982 | _endthreadex(0);
|
|---|
| 983 | }
|
|---|
| 984 | if (nread == 0)
|
|---|
| 985 | _endthreadex(0);
|
|---|
| 986 | if (pproc->errcnt + nread > bufsize) {
|
|---|
| 987 | bufsize += nread + 512;
|
|---|
| 988 | pproc->errp = realloc(pproc->errp, bufsize);
|
|---|
| 989 | if (pproc->errp == NULL) {
|
|---|
| 990 | pproc->errcnt = 0;
|
|---|
| 991 | _endthreadex(0);
|
|---|
| 992 | }
|
|---|
| 993 | }
|
|---|
| 994 | pproc->errp[pproc->errcnt++] = c;
|
|---|
| 995 | }
|
|---|
| 996 | return 0;
|
|---|
| 997 | }
|
|---|
| 998 |
|
|---|
| 999 |
|
|---|
| 1000 | /*
|
|---|
| 1001 | * Purpose: collects output from child process and returns results
|
|---|
| 1002 | *
|
|---|
| 1003 | * Description:
|
|---|
| 1004 | *
|
|---|
| 1005 | * Returns:
|
|---|
| 1006 | *
|
|---|
| 1007 | * Notes/Dependencies:
|
|---|
| 1008 | */
|
|---|
| 1009 | long
|
|---|
| 1010 | process_pipe_io(
|
|---|
| 1011 | HANDLE proc,
|
|---|
| 1012 | char *stdin_data,
|
|---|
| 1013 | int stdin_data_len)
|
|---|
| 1014 | {
|
|---|
| 1015 | sub_process *pproc = (sub_process *)proc;
|
|---|
| 1016 | bool_t stdin_eof = FALSE, stdout_eof = FALSE, stderr_eof = FALSE;
|
|---|
| 1017 | HANDLE childhand = (HANDLE) pproc->pid;
|
|---|
| 1018 | HANDLE tStdin = NULL, tStdout = NULL, tStderr = NULL;
|
|---|
| 1019 | unsigned int dwStdin, dwStdout, dwStderr;
|
|---|
| 1020 | HANDLE wait_list[4];
|
|---|
| 1021 | DWORD wait_count;
|
|---|
| 1022 | DWORD wait_return;
|
|---|
| 1023 | HANDLE ready_hand;
|
|---|
| 1024 | bool_t child_dead = FALSE;
|
|---|
| 1025 | BOOL GetExitCodeResult;
|
|---|
| 1026 | #ifdef KMK
|
|---|
| 1027 | assert (pproc->enmType == kRegular);
|
|---|
| 1028 | #endif
|
|---|
| 1029 |
|
|---|
| 1030 | /*
|
|---|
| 1031 | * Create stdin thread, if needed
|
|---|
| 1032 | */
|
|---|
| 1033 | pproc->inp = stdin_data;
|
|---|
| 1034 | pproc->incnt = stdin_data_len;
|
|---|
| 1035 | if (!pproc->inp) {
|
|---|
| 1036 | stdin_eof = TRUE;
|
|---|
| 1037 | CloseHandle((HANDLE)pproc->sv_stdin[0]);
|
|---|
| 1038 | pproc->sv_stdin[0] = 0;
|
|---|
| 1039 | } else {
|
|---|
| 1040 | tStdin = (HANDLE) _beginthreadex( 0, 1024,
|
|---|
| 1041 | (unsigned (__stdcall *) (void *))proc_stdin_thread,
|
|---|
| 1042 | pproc, 0, &dwStdin);
|
|---|
| 1043 | if (tStdin == 0) {
|
|---|
| 1044 | pproc->last_err = GetLastError();
|
|---|
| 1045 | pproc->lerrno = E_SCALL;
|
|---|
| 1046 | goto done;
|
|---|
| 1047 | }
|
|---|
| 1048 | }
|
|---|
| 1049 |
|
|---|
| 1050 | /*
|
|---|
| 1051 | * Assume child will produce stdout and stderr
|
|---|
| 1052 | */
|
|---|
| 1053 | tStdout = (HANDLE) _beginthreadex( 0, 1024,
|
|---|
| 1054 | (unsigned (__stdcall *) (void *))proc_stdout_thread, pproc, 0,
|
|---|
| 1055 | &dwStdout);
|
|---|
| 1056 | tStderr = (HANDLE) _beginthreadex( 0, 1024,
|
|---|
| 1057 | (unsigned (__stdcall *) (void *))proc_stderr_thread, pproc, 0,
|
|---|
| 1058 | &dwStderr);
|
|---|
| 1059 |
|
|---|
| 1060 | if (tStdout == 0 || tStderr == 0) {
|
|---|
| 1061 |
|
|---|
| 1062 | pproc->last_err = GetLastError();
|
|---|
| 1063 | pproc->lerrno = E_SCALL;
|
|---|
| 1064 | goto done;
|
|---|
| 1065 | }
|
|---|
| 1066 |
|
|---|
| 1067 |
|
|---|
| 1068 | /*
|
|---|
| 1069 | * Wait for all I/O to finish and for the child process to exit
|
|---|
| 1070 | */
|
|---|
| 1071 |
|
|---|
| 1072 | while (!stdin_eof || !stdout_eof || !stderr_eof || !child_dead) {
|
|---|
| 1073 | wait_count = 0;
|
|---|
| 1074 | if (!stdin_eof) {
|
|---|
| 1075 | wait_list[wait_count++] = tStdin;
|
|---|
| 1076 | }
|
|---|
| 1077 | if (!stdout_eof) {
|
|---|
| 1078 | wait_list[wait_count++] = tStdout;
|
|---|
| 1079 | }
|
|---|
| 1080 | if (!stderr_eof) {
|
|---|
| 1081 | wait_list[wait_count++] = tStderr;
|
|---|
| 1082 | }
|
|---|
| 1083 | if (!child_dead) {
|
|---|
| 1084 | wait_list[wait_count++] = childhand;
|
|---|
| 1085 | }
|
|---|
| 1086 |
|
|---|
| 1087 | wait_return = WaitForMultipleObjects(wait_count, wait_list,
|
|---|
| 1088 | FALSE, /* don't wait for all: one ready will do */
|
|---|
| 1089 | child_dead? 1000 :INFINITE); /* after the child dies, subthreads have
|
|---|
| 1090 | one second to collect all remaining output */
|
|---|
| 1091 |
|
|---|
| 1092 | if (wait_return == WAIT_FAILED) {
|
|---|
| 1093 | /* map_windows32_error_to_string(GetLastError());*/
|
|---|
| 1094 | pproc->last_err = GetLastError();
|
|---|
| 1095 | pproc->lerrno = E_SCALL;
|
|---|
| 1096 | goto done;
|
|---|
| 1097 | }
|
|---|
| 1098 |
|
|---|
| 1099 | ready_hand = wait_list[wait_return - WAIT_OBJECT_0];
|
|---|
| 1100 |
|
|---|
| 1101 | if (ready_hand == tStdin) {
|
|---|
| 1102 | CloseHandle((HANDLE)pproc->sv_stdin[0]);
|
|---|
| 1103 | pproc->sv_stdin[0] = 0;
|
|---|
| 1104 | CloseHandle(tStdin);
|
|---|
| 1105 | tStdin = 0;
|
|---|
| 1106 | stdin_eof = TRUE;
|
|---|
| 1107 |
|
|---|
| 1108 | } else if (ready_hand == tStdout) {
|
|---|
| 1109 |
|
|---|
| 1110 | CloseHandle((HANDLE)pproc->sv_stdout[0]);
|
|---|
| 1111 | pproc->sv_stdout[0] = 0;
|
|---|
| 1112 | CloseHandle(tStdout);
|
|---|
| 1113 | tStdout = 0;
|
|---|
| 1114 | stdout_eof = TRUE;
|
|---|
| 1115 |
|
|---|
| 1116 | } else if (ready_hand == tStderr) {
|
|---|
| 1117 |
|
|---|
| 1118 | CloseHandle((HANDLE)pproc->sv_stderr[0]);
|
|---|
| 1119 | pproc->sv_stderr[0] = 0;
|
|---|
| 1120 | CloseHandle(tStderr);
|
|---|
| 1121 | tStderr = 0;
|
|---|
| 1122 | stderr_eof = TRUE;
|
|---|
| 1123 |
|
|---|
| 1124 | } else if (ready_hand == childhand) {
|
|---|
| 1125 |
|
|---|
| 1126 | DWORD ierr;
|
|---|
| 1127 | GetExitCodeResult = GetExitCodeProcess(childhand, &ierr);
|
|---|
| 1128 | if (ierr == CONTROL_C_EXIT) {
|
|---|
| 1129 | pproc->signal = SIGINT;
|
|---|
| 1130 | } else {
|
|---|
| 1131 | pproc->exit_code = ierr;
|
|---|
| 1132 | }
|
|---|
| 1133 | if (GetExitCodeResult == FALSE) {
|
|---|
| 1134 | pproc->last_err = GetLastError();
|
|---|
| 1135 | pproc->lerrno = E_SCALL;
|
|---|
| 1136 | goto done;
|
|---|
| 1137 | }
|
|---|
| 1138 | child_dead = TRUE;
|
|---|
| 1139 |
|
|---|
| 1140 | } else {
|
|---|
| 1141 |
|
|---|
| 1142 | /* ?? Got back a handle we didn't query ?? */
|
|---|
| 1143 | pproc->last_err = 0;
|
|---|
| 1144 | pproc->lerrno = E_FAIL;
|
|---|
| 1145 | goto done;
|
|---|
| 1146 | }
|
|---|
| 1147 | }
|
|---|
| 1148 |
|
|---|
| 1149 | done:
|
|---|
| 1150 | if (tStdin != 0)
|
|---|
| 1151 | CloseHandle(tStdin);
|
|---|
| 1152 | if (tStdout != 0)
|
|---|
| 1153 | CloseHandle(tStdout);
|
|---|
| 1154 | if (tStderr != 0)
|
|---|
| 1155 | CloseHandle(tStderr);
|
|---|
| 1156 |
|
|---|
| 1157 | if (pproc->lerrno)
|
|---|
| 1158 | return(-1);
|
|---|
| 1159 | else
|
|---|
| 1160 | return(0);
|
|---|
| 1161 |
|
|---|
| 1162 | }
|
|---|
| 1163 | #endif /* unused */
|
|---|
| 1164 |
|
|---|
| 1165 | #ifndef KMK /* unused */
|
|---|
| 1166 | /*
|
|---|
| 1167 | * Purpose: collects output from child process and returns results
|
|---|
| 1168 | *
|
|---|
| 1169 | * Description:
|
|---|
| 1170 | *
|
|---|
| 1171 | * Returns:
|
|---|
| 1172 | *
|
|---|
| 1173 | * Notes/Dependencies:
|
|---|
| 1174 | */
|
|---|
| 1175 | long
|
|---|
| 1176 | process_file_io(
|
|---|
| 1177 | HANDLE proc)
|
|---|
| 1178 | {
|
|---|
| 1179 | sub_process *pproc;
|
|---|
| 1180 |
|
|---|
| 1181 | if (proc == NULL)
|
|---|
| 1182 | pproc = process_wait_for_any_private(1, 0);
|
|---|
| 1183 | else
|
|---|
| 1184 | pproc = (sub_process *)proc;
|
|---|
| 1185 |
|
|---|
| 1186 | /* some sort of internal error */
|
|---|
| 1187 | if (!pproc)
|
|---|
| 1188 | return -1;
|
|---|
| 1189 |
|
|---|
| 1190 | return process_file_io_private(proc, TRUE);
|
|---|
| 1191 | }
|
|---|
| 1192 | #endif /* !KMK - unused */
|
|---|
| 1193 |
|
|---|
| 1194 | /* private function, avoid some kernel calls. (bird) */
|
|---|
| 1195 | static long
|
|---|
| 1196 | process_file_io_private(
|
|---|
| 1197 | sub_process *pproc,
|
|---|
| 1198 | BOOL fNeedToWait)
|
|---|
| 1199 | {
|
|---|
| 1200 | HANDLE childhand;
|
|---|
| 1201 | DWORD wait_return;
|
|---|
| 1202 | BOOL GetExitCodeResult;
|
|---|
| 1203 | DWORD ierr;
|
|---|
| 1204 |
|
|---|
| 1205 | childhand = (HANDLE) pproc->pid;
|
|---|
| 1206 |
|
|---|
| 1207 | /*
|
|---|
| 1208 | * This function is poorly named, and could also be used just to wait
|
|---|
| 1209 | * for child death if you're doing your own pipe I/O. If that is
|
|---|
| 1210 | * the case, close the pipe handles here.
|
|---|
| 1211 | */
|
|---|
| 1212 | if (pproc->sv_stdin[0]) {
|
|---|
| 1213 | CloseHandle((HANDLE)pproc->sv_stdin[0]);
|
|---|
| 1214 | pproc->sv_stdin[0] = 0;
|
|---|
| 1215 | }
|
|---|
| 1216 | if (pproc->sv_stdout[0]) {
|
|---|
| 1217 | CloseHandle((HANDLE)pproc->sv_stdout[0]);
|
|---|
| 1218 | pproc->sv_stdout[0] = 0;
|
|---|
| 1219 | }
|
|---|
| 1220 | if (pproc->sv_stderr[0]) {
|
|---|
| 1221 | CloseHandle((HANDLE)pproc->sv_stderr[0]);
|
|---|
| 1222 | pproc->sv_stderr[0] = 0;
|
|---|
| 1223 | }
|
|---|
| 1224 |
|
|---|
| 1225 | #ifdef KMK
|
|---|
| 1226 | if (childhand == NULL || childhand == INVALID_HANDLE_VALUE) {
|
|---|
| 1227 | goto done2;
|
|---|
| 1228 | }
|
|---|
| 1229 | #endif
|
|---|
| 1230 |
|
|---|
| 1231 | /*
|
|---|
| 1232 | * Wait for the child process to exit
|
|---|
| 1233 | */
|
|---|
| 1234 |
|
|---|
| 1235 | if (fNeedToWait) { /* bird */
|
|---|
| 1236 | wait_return = WaitForSingleObject(childhand, INFINITE);
|
|---|
| 1237 |
|
|---|
| 1238 | if (wait_return != WAIT_OBJECT_0) {
|
|---|
| 1239 | /* map_windows32_error_to_string(GetLastError());*/
|
|---|
| 1240 | pproc->last_err = GetLastError();
|
|---|
| 1241 | pproc->lerrno = E_SCALL;
|
|---|
| 1242 | goto done2;
|
|---|
| 1243 | }
|
|---|
| 1244 | } /* bird */
|
|---|
| 1245 |
|
|---|
| 1246 | GetExitCodeResult = GetExitCodeProcess(childhand, &ierr);
|
|---|
| 1247 | if (ierr == CONTROL_C_EXIT) {
|
|---|
| 1248 | pproc->signal = SIGINT;
|
|---|
| 1249 | } else {
|
|---|
| 1250 | pproc->exit_code = ierr;
|
|---|
| 1251 | }
|
|---|
| 1252 | if (GetExitCodeResult == FALSE) {
|
|---|
| 1253 | pproc->last_err = GetLastError();
|
|---|
| 1254 | pproc->lerrno = E_SCALL;
|
|---|
| 1255 | }
|
|---|
| 1256 |
|
|---|
| 1257 | done2:
|
|---|
| 1258 | if (pproc->lerrno)
|
|---|
| 1259 | return(-1);
|
|---|
| 1260 | else
|
|---|
| 1261 | return(0);
|
|---|
| 1262 |
|
|---|
| 1263 | }
|
|---|
| 1264 |
|
|---|
| 1265 | /*
|
|---|
| 1266 | * Description: Clean up any leftover handles, etc. It is up to the
|
|---|
| 1267 | * caller to manage and free the input, output, and stderr buffers.
|
|---|
| 1268 | */
|
|---|
| 1269 | void
|
|---|
| 1270 | process_cleanup(
|
|---|
| 1271 | HANDLE proc)
|
|---|
| 1272 | {
|
|---|
| 1273 | sub_process *pproc = (sub_process *)proc;
|
|---|
| 1274 | int i;
|
|---|
| 1275 |
|
|---|
| 1276 | #ifdef KMK
|
|---|
| 1277 | if (pproc->enmType == kRegular) {
|
|---|
| 1278 | #endif
|
|---|
| 1279 | if (pproc->using_pipes) {
|
|---|
| 1280 | for (i= 0; i <= 1; i++) {
|
|---|
| 1281 | if ((HANDLE)pproc->sv_stdin[i]
|
|---|
| 1282 | && (HANDLE)pproc->sv_stdin[i] != INVALID_HANDLE_VALUE)
|
|---|
| 1283 | CloseHandle((HANDLE)pproc->sv_stdin[i]);
|
|---|
| 1284 | if ((HANDLE)pproc->sv_stdout[i]
|
|---|
| 1285 | && (HANDLE)pproc->sv_stdout[i] != INVALID_HANDLE_VALUE)
|
|---|
| 1286 | CloseHandle((HANDLE)pproc->sv_stdout[i]);
|
|---|
| 1287 | if ((HANDLE)pproc->sv_stderr[i]
|
|---|
| 1288 | && (HANDLE)pproc->sv_stderr[i] != INVALID_HANDLE_VALUE)
|
|---|
| 1289 | CloseHandle((HANDLE)pproc->sv_stderr[i]);
|
|---|
| 1290 | }
|
|---|
| 1291 | }
|
|---|
| 1292 | if ((HANDLE)pproc->pid)
|
|---|
| 1293 | CloseHandle((HANDLE)pproc->pid);
|
|---|
| 1294 | #ifdef KMK
|
|---|
| 1295 | } else if (pproc->enmType == kSubmit) {
|
|---|
| 1296 | kSubmitSubProcCleanup(pproc->clue);
|
|---|
| 1297 | } else {
|
|---|
| 1298 | assert(0);
|
|---|
| 1299 | return;
|
|---|
| 1300 | }
|
|---|
| 1301 | pproc->enmType = kSubProcFreed;
|
|---|
| 1302 | #endif
|
|---|
| 1303 |
|
|---|
| 1304 | free(pproc);
|
|---|
| 1305 | }
|
|---|
| 1306 |
|
|---|
| 1307 |
|
|---|
| 1308 | /*
|
|---|
| 1309 | * Description:
|
|---|
| 1310 | * Create a command line buffer to pass to CreateProcess
|
|---|
| 1311 | *
|
|---|
| 1312 | * Returns: the buffer or NULL for failure
|
|---|
| 1313 | * Shell case: sh_name a:/full/path/to/script argv[1] argv[2] ...
|
|---|
| 1314 | * Otherwise: argv[0] argv[1] argv[2] ...
|
|---|
| 1315 | *
|
|---|
| 1316 | * Notes/Dependencies:
|
|---|
| 1317 | * CreateProcess does not take an argv, so this command creates a
|
|---|
| 1318 | * command line for the executable.
|
|---|
| 1319 | */
|
|---|
| 1320 |
|
|---|
| 1321 | static char *
|
|---|
| 1322 | make_command_line( char *shell_name, char *full_exec_path, char **argv)
|
|---|
| 1323 | {
|
|---|
| 1324 | int argc = 0;
|
|---|
| 1325 | char** argvi;
|
|---|
| 1326 | int* enclose_in_quotes = NULL;
|
|---|
| 1327 | int* enclose_in_quotes_i;
|
|---|
| 1328 | unsigned int bytes_required = 0;
|
|---|
| 1329 | char* command_line;
|
|---|
| 1330 | char* command_line_i;
|
|---|
| 1331 | int cygwin_mode = 0; /* HAVE_CYGWIN_SHELL */
|
|---|
| 1332 | int have_sh = 0; /* HAVE_CYGWIN_SHELL */
|
|---|
| 1333 | #undef HAVE_CYGWIN_SHELL /* bird: paranoia */
|
|---|
| 1334 | #ifdef HAVE_CYGWIN_SHELL
|
|---|
| 1335 | have_sh = (shell_name != NULL || strstr(full_exec_path, "sh.exe"));
|
|---|
| 1336 | cygwin_mode = 1;
|
|---|
| 1337 | #endif
|
|---|
| 1338 |
|
|---|
| 1339 | if (shell_name && full_exec_path) {
|
|---|
| 1340 | bytes_required
|
|---|
| 1341 | = strlen(shell_name) + 1 + strlen(full_exec_path);
|
|---|
| 1342 | /*
|
|---|
| 1343 | * Skip argv[0] if any, when shell_name is given.
|
|---|
| 1344 | * The special case of "-c" in full_exec_path means
|
|---|
| 1345 | * argv[0] is not the shell name, but the command string
|
|---|
| 1346 | * to pass to the shell.
|
|---|
| 1347 | */
|
|---|
| 1348 | if (*argv && strcmp(full_exec_path, "-c")) argv++;
|
|---|
| 1349 | /*
|
|---|
| 1350 | * Add one for the intervening space.
|
|---|
| 1351 | */
|
|---|
| 1352 | if (*argv) bytes_required++;
|
|---|
| 1353 | }
|
|---|
| 1354 |
|
|---|
| 1355 | argvi = argv;
|
|---|
| 1356 | while (*(argvi++)) argc++;
|
|---|
| 1357 |
|
|---|
| 1358 | if (argc) {
|
|---|
| 1359 | enclose_in_quotes = (int*) calloc(1, argc * sizeof(int));
|
|---|
| 1360 |
|
|---|
| 1361 | if (!enclose_in_quotes) {
|
|---|
| 1362 | return NULL;
|
|---|
| 1363 | }
|
|---|
| 1364 | }
|
|---|
| 1365 |
|
|---|
| 1366 | /* We have to make one pass through each argv[i] to see if we need
|
|---|
| 1367 | * to enclose it in ", so we might as well figure out how much
|
|---|
| 1368 | * memory we'll need on the same pass.
|
|---|
| 1369 | */
|
|---|
| 1370 |
|
|---|
| 1371 | argvi = argv;
|
|---|
| 1372 | enclose_in_quotes_i = enclose_in_quotes;
|
|---|
| 1373 | while(*argvi) {
|
|---|
| 1374 | char* p = *argvi;
|
|---|
| 1375 | unsigned int backslash_count = 0;
|
|---|
| 1376 |
|
|---|
| 1377 | /*
|
|---|
| 1378 | * We have to enclose empty arguments in ".
|
|---|
| 1379 | */
|
|---|
| 1380 | if (!(*p)) *enclose_in_quotes_i = 1;
|
|---|
| 1381 |
|
|---|
| 1382 | while(*p) {
|
|---|
| 1383 | switch (*p) {
|
|---|
| 1384 | case '\"':
|
|---|
| 1385 | /*
|
|---|
| 1386 | * We have to insert a backslash for each "
|
|---|
| 1387 | * and each \ that precedes the ".
|
|---|
| 1388 | */
|
|---|
| 1389 | bytes_required += (backslash_count + 1);
|
|---|
| 1390 | backslash_count = 0;
|
|---|
| 1391 | break;
|
|---|
| 1392 |
|
|---|
| 1393 | #if !defined(HAVE_MKS_SHELL) && !defined(HAVE_CYGWIN_SHELL)
|
|---|
| 1394 | case '\\':
|
|---|
| 1395 | backslash_count++;
|
|---|
| 1396 | break;
|
|---|
| 1397 | #endif
|
|---|
| 1398 | /*
|
|---|
| 1399 | * At one time we set *enclose_in_quotes_i for '*' or '?' to suppress
|
|---|
| 1400 | * wildcard expansion in programs linked with MSVC's SETARGV.OBJ so
|
|---|
| 1401 | * that argv in always equals argv out. This was removed. Say you have
|
|---|
| 1402 | * such a program named glob.exe. You enter
|
|---|
| 1403 | * glob '*'
|
|---|
| 1404 | * at the sh command prompt. Obviously the intent is to make glob do the
|
|---|
| 1405 | * wildcarding instead of sh. If we set *enclose_in_quotes_i for '*' or '?',
|
|---|
| 1406 | * then the command line that glob would see would be
|
|---|
| 1407 | * glob "*"
|
|---|
| 1408 | * and the _setargv in SETARGV.OBJ would _not_ expand the *.
|
|---|
| 1409 | */
|
|---|
| 1410 | case ' ':
|
|---|
| 1411 | case '\t':
|
|---|
| 1412 | *enclose_in_quotes_i = 1;
|
|---|
| 1413 | /* fall through */
|
|---|
| 1414 |
|
|---|
| 1415 | default:
|
|---|
| 1416 | backslash_count = 0;
|
|---|
| 1417 | break;
|
|---|
| 1418 | }
|
|---|
| 1419 |
|
|---|
| 1420 | /*
|
|---|
| 1421 | * Add one for each character in argv[i].
|
|---|
| 1422 | */
|
|---|
| 1423 | bytes_required++;
|
|---|
| 1424 |
|
|---|
| 1425 | p++;
|
|---|
| 1426 | }
|
|---|
| 1427 |
|
|---|
| 1428 | if (*enclose_in_quotes_i) {
|
|---|
| 1429 | /*
|
|---|
| 1430 | * Add one for each enclosing ",
|
|---|
| 1431 | * and one for each \ that precedes the
|
|---|
| 1432 | * closing ".
|
|---|
| 1433 | */
|
|---|
| 1434 | bytes_required += (backslash_count + 2);
|
|---|
| 1435 | }
|
|---|
| 1436 |
|
|---|
| 1437 | /*
|
|---|
| 1438 | * Add one for the intervening space.
|
|---|
| 1439 | */
|
|---|
| 1440 | if (*(++argvi)) bytes_required++;
|
|---|
| 1441 | enclose_in_quotes_i++;
|
|---|
| 1442 | }
|
|---|
| 1443 |
|
|---|
| 1444 | /*
|
|---|
| 1445 | * Add one for the terminating NULL.
|
|---|
| 1446 | */
|
|---|
| 1447 | bytes_required++;
|
|---|
| 1448 | #ifdef KMK /* for the space before the final " in case we need it. */
|
|---|
| 1449 | bytes_required++;
|
|---|
| 1450 | #endif
|
|---|
| 1451 |
|
|---|
| 1452 | command_line = (char*) malloc(bytes_required);
|
|---|
| 1453 |
|
|---|
| 1454 | if (!command_line) {
|
|---|
| 1455 | free(enclose_in_quotes);
|
|---|
| 1456 | return NULL;
|
|---|
| 1457 | }
|
|---|
| 1458 |
|
|---|
| 1459 | command_line_i = command_line;
|
|---|
| 1460 |
|
|---|
| 1461 | if (shell_name && full_exec_path) {
|
|---|
| 1462 | while(*shell_name) {
|
|---|
| 1463 | *(command_line_i++) = *(shell_name++);
|
|---|
| 1464 | }
|
|---|
| 1465 |
|
|---|
| 1466 | *(command_line_i++) = ' ';
|
|---|
| 1467 |
|
|---|
| 1468 | while(*full_exec_path) {
|
|---|
| 1469 | *(command_line_i++) = *(full_exec_path++);
|
|---|
| 1470 | }
|
|---|
| 1471 |
|
|---|
| 1472 | if (*argv) {
|
|---|
| 1473 | *(command_line_i++) = ' ';
|
|---|
| 1474 | }
|
|---|
| 1475 | }
|
|---|
| 1476 |
|
|---|
| 1477 | argvi = argv;
|
|---|
| 1478 | enclose_in_quotes_i = enclose_in_quotes;
|
|---|
| 1479 |
|
|---|
| 1480 | while(*argvi) {
|
|---|
| 1481 | char* p = *argvi;
|
|---|
| 1482 | unsigned int backslash_count = 0;
|
|---|
| 1483 |
|
|---|
| 1484 | if (*enclose_in_quotes_i) {
|
|---|
| 1485 | *(command_line_i++) = '\"';
|
|---|
| 1486 | }
|
|---|
| 1487 |
|
|---|
| 1488 | while(*p) {
|
|---|
| 1489 | if (*p == '\"') {
|
|---|
| 1490 | if (cygwin_mode && have_sh) { /* HAVE_CYGWIN_SHELL */
|
|---|
| 1491 | /* instead of a \", cygwin likes "" */
|
|---|
| 1492 | *(command_line_i++) = '\"';
|
|---|
| 1493 | } else {
|
|---|
| 1494 |
|
|---|
| 1495 | /*
|
|---|
| 1496 | * We have to insert a backslash for the "
|
|---|
| 1497 | * and each \ that precedes the ".
|
|---|
| 1498 | */
|
|---|
| 1499 | backslash_count++;
|
|---|
| 1500 |
|
|---|
| 1501 | while(backslash_count) {
|
|---|
| 1502 | *(command_line_i++) = '\\';
|
|---|
| 1503 | backslash_count--;
|
|---|
| 1504 | };
|
|---|
| 1505 | }
|
|---|
| 1506 | #if !defined(HAVE_MKS_SHELL) && !defined(HAVE_CYGWIN_SHELL)
|
|---|
| 1507 | } else if (*p == '\\') {
|
|---|
| 1508 | backslash_count++;
|
|---|
| 1509 | } else {
|
|---|
| 1510 | backslash_count = 0;
|
|---|
| 1511 | #endif
|
|---|
| 1512 | }
|
|---|
| 1513 |
|
|---|
| 1514 | /*
|
|---|
| 1515 | * Copy the character.
|
|---|
| 1516 | */
|
|---|
| 1517 | *(command_line_i++) = *(p++);
|
|---|
| 1518 | }
|
|---|
| 1519 |
|
|---|
| 1520 | if (*enclose_in_quotes_i) {
|
|---|
| 1521 | #if !defined(HAVE_MKS_SHELL) && !defined(HAVE_CYGWIN_SHELL)
|
|---|
| 1522 | /*
|
|---|
| 1523 | * Add one \ for each \ that precedes the
|
|---|
| 1524 | * closing ".
|
|---|
| 1525 | */
|
|---|
| 1526 | while(backslash_count--) {
|
|---|
| 1527 | *(command_line_i++) = '\\';
|
|---|
| 1528 | };
|
|---|
| 1529 | #endif
|
|---|
| 1530 | #ifdef KMK
|
|---|
| 1531 | /*
|
|---|
| 1532 | * ash it put off by echo "hello world" ending up as:
|
|---|
| 1533 | * G:/.../kmk_ash.exe -c "echo ""hello world"""
|
|---|
| 1534 | * It wants a space before the last '"'.
|
|---|
| 1535 | * (The 'test_shell' goals in Makefile.kmk tests this problem.)
|
|---|
| 1536 | */
|
|---|
| 1537 | if (command_line_i[-1] == '\"' /* && cygwin_mode && have_sh*/ && !argvi[1]) {
|
|---|
| 1538 | *(command_line_i++) = ' ';
|
|---|
| 1539 | }
|
|---|
| 1540 | #endif
|
|---|
| 1541 |
|
|---|
| 1542 | *(command_line_i++) = '\"';
|
|---|
| 1543 | }
|
|---|
| 1544 |
|
|---|
| 1545 | /*
|
|---|
| 1546 | * Append an intervening space.
|
|---|
| 1547 | */
|
|---|
| 1548 | if (*(++argvi)) {
|
|---|
| 1549 | *(command_line_i++) = ' ';
|
|---|
| 1550 | }
|
|---|
| 1551 |
|
|---|
| 1552 | enclose_in_quotes_i++;
|
|---|
| 1553 | }
|
|---|
| 1554 |
|
|---|
| 1555 | /*
|
|---|
| 1556 | * Append the terminating NULL.
|
|---|
| 1557 | */
|
|---|
| 1558 | *command_line_i = '\0';
|
|---|
| 1559 |
|
|---|
| 1560 | free(enclose_in_quotes);
|
|---|
| 1561 | return command_line;
|
|---|
| 1562 | }
|
|---|
| 1563 |
|
|---|
| 1564 | /*
|
|---|
| 1565 | * Description: Given an argv and optional envp, launch the process
|
|---|
| 1566 | * using the default stdin, stdout, and stderr handles.
|
|---|
| 1567 | * Also, register process so that process_wait_for_any_private()
|
|---|
| 1568 | * can be used via process_file_io(NULL) or
|
|---|
| 1569 | * process_wait_for_any().
|
|---|
| 1570 | *
|
|---|
| 1571 | * Returns:
|
|---|
| 1572 | *
|
|---|
| 1573 | * Notes/Dependencies:
|
|---|
| 1574 | */
|
|---|
| 1575 | HANDLE
|
|---|
| 1576 | process_easy(
|
|---|
| 1577 | char **argv,
|
|---|
| 1578 | char **envp,
|
|---|
| 1579 | int outfd,
|
|---|
| 1580 | int errfd)
|
|---|
| 1581 | {
|
|---|
| 1582 | HANDLE hIn = INVALID_HANDLE_VALUE;
|
|---|
| 1583 | HANDLE hOut = INVALID_HANDLE_VALUE;
|
|---|
| 1584 | HANDLE hErr = INVALID_HANDLE_VALUE;
|
|---|
| 1585 | HANDLE hProcess, tmpIn, tmpOut, tmpErr;
|
|---|
| 1586 | DWORD e;
|
|---|
| 1587 |
|
|---|
| 1588 | if (proc_index >= MAXIMUM_WAIT_OBJECTS) {
|
|---|
| 1589 | DB (DB_JOBS, ("process_easy: All process slots used up\n"));
|
|---|
| 1590 | return INVALID_HANDLE_VALUE;
|
|---|
| 1591 | }
|
|---|
| 1592 | #ifdef KMK /* We can effort here by lettering CreateProcess/kernel do the standard handle duplication. */
|
|---|
| 1593 | if (outfd != -1 || errfd != -1) {
|
|---|
| 1594 | #endif
|
|---|
| 1595 | /* Standard handles returned by GetStdHandle can be NULL or
|
|---|
| 1596 | INVALID_HANDLE_VALUE if the parent process closed them. If that
|
|---|
| 1597 | happens, we open the null device and pass its handle to
|
|---|
| 1598 | CreateProcess as the corresponding handle to inherit. */
|
|---|
| 1599 | tmpIn = GetStdHandle(STD_INPUT_HANDLE);
|
|---|
| 1600 | if (DuplicateHandle(GetCurrentProcess(),
|
|---|
| 1601 | tmpIn,
|
|---|
| 1602 | GetCurrentProcess(),
|
|---|
| 1603 | &hIn,
|
|---|
| 1604 | 0,
|
|---|
| 1605 | TRUE,
|
|---|
| 1606 | DUPLICATE_SAME_ACCESS) == FALSE) {
|
|---|
| 1607 | if ((e = GetLastError()) == ERROR_INVALID_HANDLE) {
|
|---|
| 1608 | tmpIn = CreateFile("NUL", GENERIC_READ,
|
|---|
| 1609 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
|
|---|
| 1610 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
|---|
| 1611 | if (tmpIn != INVALID_HANDLE_VALUE
|
|---|
| 1612 | && DuplicateHandle(GetCurrentProcess(),
|
|---|
| 1613 | tmpIn,
|
|---|
| 1614 | GetCurrentProcess(),
|
|---|
| 1615 | &hIn,
|
|---|
| 1616 | 0,
|
|---|
| 1617 | TRUE,
|
|---|
| 1618 | DUPLICATE_SAME_ACCESS) == FALSE)
|
|---|
| 1619 | CloseHandle(tmpIn);
|
|---|
| 1620 | }
|
|---|
| 1621 | if (hIn == INVALID_HANDLE_VALUE) {
|
|---|
| 1622 | fprintf(stderr, "process_easy: DuplicateHandle(In) failed (e=%ld)\n", e);
|
|---|
| 1623 | return INVALID_HANDLE_VALUE;
|
|---|
| 1624 | }
|
|---|
| 1625 | }
|
|---|
| 1626 | if (outfd >= 0)
|
|---|
| 1627 | tmpOut = (HANDLE)_get_osfhandle (outfd);
|
|---|
| 1628 | else
|
|---|
| 1629 | tmpOut = GetStdHandle (STD_OUTPUT_HANDLE);
|
|---|
| 1630 | if (DuplicateHandle(GetCurrentProcess(),
|
|---|
| 1631 | tmpOut,
|
|---|
| 1632 | GetCurrentProcess(),
|
|---|
| 1633 | &hOut,
|
|---|
| 1634 | 0,
|
|---|
| 1635 | TRUE,
|
|---|
| 1636 | DUPLICATE_SAME_ACCESS) == FALSE) {
|
|---|
| 1637 | if ((e = GetLastError()) == ERROR_INVALID_HANDLE) {
|
|---|
| 1638 | tmpOut = CreateFile("NUL", GENERIC_WRITE,
|
|---|
| 1639 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
|
|---|
| 1640 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
|---|
| 1641 | if (tmpOut != INVALID_HANDLE_VALUE
|
|---|
| 1642 | && DuplicateHandle(GetCurrentProcess(),
|
|---|
| 1643 | tmpOut,
|
|---|
| 1644 | GetCurrentProcess(),
|
|---|
| 1645 | &hOut,
|
|---|
| 1646 | 0,
|
|---|
| 1647 | TRUE,
|
|---|
| 1648 | DUPLICATE_SAME_ACCESS) == FALSE)
|
|---|
| 1649 | CloseHandle(tmpOut);
|
|---|
| 1650 | }
|
|---|
| 1651 | if (hOut == INVALID_HANDLE_VALUE) {
|
|---|
| 1652 | fprintf(stderr, "process_easy: DuplicateHandle(Out) failed (e=%ld)\n", e);
|
|---|
| 1653 | return INVALID_HANDLE_VALUE;
|
|---|
| 1654 | }
|
|---|
| 1655 | }
|
|---|
| 1656 | if (errfd >= 0)
|
|---|
| 1657 | tmpErr = (HANDLE)_get_osfhandle (errfd);
|
|---|
| 1658 | else
|
|---|
| 1659 | tmpErr = GetStdHandle(STD_ERROR_HANDLE);
|
|---|
| 1660 | if (DuplicateHandle(GetCurrentProcess(),
|
|---|
| 1661 | tmpErr,
|
|---|
| 1662 | GetCurrentProcess(),
|
|---|
| 1663 | &hErr,
|
|---|
| 1664 | 0,
|
|---|
| 1665 | TRUE,
|
|---|
| 1666 | DUPLICATE_SAME_ACCESS) == FALSE) {
|
|---|
| 1667 | if ((e = GetLastError()) == ERROR_INVALID_HANDLE) {
|
|---|
| 1668 | tmpErr = CreateFile("NUL", GENERIC_WRITE,
|
|---|
| 1669 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
|
|---|
| 1670 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
|---|
| 1671 | if (tmpErr != INVALID_HANDLE_VALUE
|
|---|
| 1672 | && DuplicateHandle(GetCurrentProcess(),
|
|---|
| 1673 | tmpErr,
|
|---|
| 1674 | GetCurrentProcess(),
|
|---|
| 1675 | &hErr,
|
|---|
| 1676 | 0,
|
|---|
| 1677 | TRUE,
|
|---|
| 1678 | DUPLICATE_SAME_ACCESS) == FALSE)
|
|---|
| 1679 | CloseHandle(tmpErr);
|
|---|
| 1680 | }
|
|---|
| 1681 | if (hErr == INVALID_HANDLE_VALUE) {
|
|---|
| 1682 | fprintf(stderr, "process_easy: DuplicateHandle(Err) failed (e=%ld)\n", e);
|
|---|
| 1683 | return INVALID_HANDLE_VALUE;
|
|---|
| 1684 | }
|
|---|
| 1685 | }
|
|---|
| 1686 | #ifdef KMK /* saving effort */
|
|---|
| 1687 | }
|
|---|
| 1688 | #endif
|
|---|
| 1689 |
|
|---|
| 1690 | hProcess = process_init_fd(hIn, hOut, hErr);
|
|---|
| 1691 |
|
|---|
| 1692 | if (process_begin(hProcess, argv, envp, argv[0], NULL)) {
|
|---|
| 1693 | fake_exits_pending++;
|
|---|
| 1694 | /* process_begin() failed: make a note of that. */
|
|---|
| 1695 | if (!((sub_process*) hProcess)->last_err)
|
|---|
| 1696 | ((sub_process*) hProcess)->last_err = -1;
|
|---|
| 1697 | #ifdef KMK
|
|---|
| 1698 | if (!((sub_process*) hProcess)->exit_code)
|
|---|
| 1699 | #endif
|
|---|
| 1700 | ((sub_process*) hProcess)->exit_code = process_last_err(hProcess);
|
|---|
| 1701 |
|
|---|
| 1702 | /* close up unused handles */
|
|---|
| 1703 | if (hIn != INVALID_HANDLE_VALUE)
|
|---|
| 1704 | CloseHandle(hIn);
|
|---|
| 1705 | if (hOut != INVALID_HANDLE_VALUE)
|
|---|
| 1706 | CloseHandle(hOut);
|
|---|
| 1707 | if (hErr != INVALID_HANDLE_VALUE)
|
|---|
| 1708 | CloseHandle(hErr);
|
|---|
| 1709 | }
|
|---|
| 1710 |
|
|---|
| 1711 | process_register(hProcess);
|
|---|
| 1712 |
|
|---|
| 1713 | return hProcess;
|
|---|
| 1714 | }
|
|---|