1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
|
/**********************************************************************
*
* Project: CPL - Common Portability Library
* Purpose: Implement CPLSystem().
* Author: Even Rouault, <even dot rouault at spatialys.com>
*
**********************************************************************
* Copyright (c) 2012-2013, Even Rouault <even dot rouault at spatialys.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "cpl_port.h"
#include "cpl_spawn.h"
#include <cstring>
#include "cpl_config.h"
#include "cpl_conv.h"
#include "cpl_error.h"
#include "cpl_multiproc.h"
#include "cpl_string.h"
#if defined(WIN32)
#include <windows.h>
#else
#include <cassert>
#include <cerrno>
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#ifdef HAVE_POSIX_SPAWNP
#include <spawn.h>
#ifdef __APPLE__
#include <TargetConditionals.h>
#endif
#if defined(__APPLE__) && (!defined(TARGET_OS_IPHONE) || TARGET_OS_IPHONE == 0)
#include <crt_externs.h>
#define environ (*_NSGetEnviron())
#else
#if defined(__FreeBSD__)
extern __attribute__((__weak__)) char **environ;
#else
extern char **environ;
#endif
#endif
#endif
#endif
constexpr int PIPE_BUFFER_SIZE = 4096;
constexpr int IN_FOR_PARENT = 0;
constexpr int OUT_FOR_PARENT = 1;
static void FillFileFromPipe(CPL_FILE_HANDLE pipe_fd, VSILFILE *fout);
/************************************************************************/
/* FillPipeFromFile() */
/************************************************************************/
static void FillPipeFromFile(VSILFILE *fin, CPL_FILE_HANDLE pipe_fd)
{
char buf[PIPE_BUFFER_SIZE] = {};
while (true)
{
const int nRead =
static_cast<int>(VSIFReadL(buf, 1, PIPE_BUFFER_SIZE, fin));
if (nRead <= 0)
break;
if (!CPLPipeWrite(pipe_fd, buf, nRead))
break;
}
}
/************************************************************************/
/* CPLSpawn() */
/************************************************************************/
/**
* Runs an executable in another process.
*
* This function runs an executable, wait for it to finish and returns
* its exit code.
*
* It is implemented as CreateProcess() on Windows platforms, and fork()/exec()
* on other platforms.
*
* @param papszArgv argument list of the executable to run. papszArgv[0] is the
* name of the executable
* @param fin File handle for input data to feed to the standard input of the
* sub-process. May be NULL.
* @param fout File handle for output data to extract from the standard output
* of the sub-process. May be NULL.
* @param bDisplayErr Set to TRUE to emit the content of the standard error
* stream of the sub-process with CPLError().
*
* @return the exit code of the spawned process, or -1 in case of error.
*
* @since GDAL 1.10.0
*/
int CPLSpawn(const char *const papszArgv[], VSILFILE *fin, VSILFILE *fout,
int bDisplayErr)
{
CPLSpawnedProcess *sp =
CPLSpawnAsync(nullptr, papszArgv, TRUE, TRUE, TRUE, nullptr);
if (sp == nullptr)
return -1;
CPL_FILE_HANDLE in_child = CPLSpawnAsyncGetOutputFileHandle(sp);
if (fin != nullptr)
FillPipeFromFile(fin, in_child);
CPLSpawnAsyncCloseOutputFileHandle(sp);
CPL_FILE_HANDLE out_child = CPLSpawnAsyncGetInputFileHandle(sp);
if (fout != nullptr)
FillFileFromPipe(out_child, fout);
CPLSpawnAsyncCloseInputFileHandle(sp);
CPL_FILE_HANDLE err_child = CPLSpawnAsyncGetErrorFileHandle(sp);
CPLString osName;
osName.Printf("/vsimem/child_stderr_" CPL_FRMT_GIB, CPLGetPID());
VSILFILE *ferr = VSIFOpenL(osName.c_str(), "w");
FillFileFromPipe(err_child, ferr);
CPLSpawnAsyncCloseErrorFileHandle(sp);
CPL_IGNORE_RET_VAL(VSIFCloseL(ferr));
vsi_l_offset nDataLength = 0;
GByte *pData = VSIGetMemFileBuffer(osName.c_str(), &nDataLength, TRUE);
if (nDataLength > 0)
pData[nDataLength - 1] = '\0';
if (pData &&
strstr(const_cast<const char *>(reinterpret_cast<char *>(pData)),
"An error occurred while forking process") != nullptr)
bDisplayErr = TRUE;
if (pData && bDisplayErr)
CPLError(CE_Failure, CPLE_AppDefined, "[%s error] %s", papszArgv[0],
pData);
CPLFree(pData);
return CPLSpawnAsyncFinish(sp, TRUE, FALSE);
}
#if defined(WIN32)
/************************************************************************/
/* CPLPipeRead() */
/************************************************************************/
int CPLPipeRead(CPL_FILE_HANDLE fin, void *data, int length)
{
GByte *pabyData = static_cast<GByte *>(data);
int nRemain = length;
while (nRemain > 0)
{
DWORD nRead = 0;
if (!ReadFile(fin, pabyData, nRemain, &nRead, nullptr))
return FALSE;
pabyData += nRead;
nRemain -= nRead;
}
return TRUE;
}
/************************************************************************/
/* CPLPipeWrite() */
/************************************************************************/
int CPLPipeWrite(CPL_FILE_HANDLE fout, const void *data, int length)
{
const GByte *pabyData = static_cast<const GByte *>(data);
int nRemain = length;
while (nRemain > 0)
{
DWORD nWritten = 0;
if (!WriteFile(fout, pabyData, nRemain, &nWritten, nullptr))
return FALSE;
pabyData += nWritten;
nRemain -= nWritten;
}
return TRUE;
}
/************************************************************************/
/* FillFileFromPipe() */
/************************************************************************/
static void FillFileFromPipe(CPL_FILE_HANDLE pipe_fd, VSILFILE *fout)
{
char buf[PIPE_BUFFER_SIZE] = {};
while (true)
{
DWORD nRead = 0;
if (!ReadFile(pipe_fd, buf, PIPE_BUFFER_SIZE, &nRead, nullptr))
break;
if (nRead <= 0)
break;
const int nWritten = static_cast<int>(VSIFWriteL(buf, 1, nRead, fout));
if (nWritten < static_cast<int>(nRead))
break;
}
}
struct _CPLSpawnedProcess
{
HANDLE hProcess;
DWORD nProcessId;
HANDLE hThread;
CPL_FILE_HANDLE fin;
CPL_FILE_HANDLE fout;
CPL_FILE_HANDLE ferr;
};
/************************************************************************/
/* CPLSpawnAsync() */
/************************************************************************/
CPLSpawnedProcess *
CPLSpawnAsync(CPL_UNUSED int (*pfnMain)(CPL_FILE_HANDLE, CPL_FILE_HANDLE),
const char *const papszArgv[], int bCreateInputPipe,
int bCreateOutputPipe, int bCreateErrorPipe,
char ** /* papszOptions */)
{
if (papszArgv == nullptr)
{
CPLError(CE_Failure, CPLE_AppDefined,
"On Windows, papszArgv argument must not be NULL");
return nullptr;
}
// TODO(schwehr): Consider initializing saAttr.
SECURITY_ATTRIBUTES saAttr;
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = nullptr;
// TODO(schwehr): Move these to where they are used after gotos are removed.
HANDLE pipe_out[2] = {nullptr, nullptr};
HANDLE pipe_err[2] = {nullptr, nullptr};
CPLString osCommandLine;
HANDLE pipe_in[2] = {nullptr, nullptr};
if (bCreateInputPipe)
{
if (!CreatePipe(&pipe_in[IN_FOR_PARENT], &pipe_in[OUT_FOR_PARENT],
&saAttr, 0))
goto err_pipe;
// The child must not inherit from the write side of the pipe_in.
if (!SetHandleInformation(pipe_in[OUT_FOR_PARENT], HANDLE_FLAG_INHERIT,
0))
goto err_pipe;
}
if (bCreateOutputPipe)
{
if (!CreatePipe(&pipe_out[IN_FOR_PARENT], &pipe_out[OUT_FOR_PARENT],
&saAttr, 0))
goto err_pipe;
// The child must not inherit from the read side of the pipe_out.
if (!SetHandleInformation(pipe_out[IN_FOR_PARENT], HANDLE_FLAG_INHERIT,
0))
goto err_pipe;
}
if (bCreateErrorPipe)
{
if (!CreatePipe(&pipe_err[IN_FOR_PARENT], &pipe_err[OUT_FOR_PARENT],
&saAttr, 0))
goto err_pipe;
// The child must not inherit from the read side of the pipe_err.
if (!SetHandleInformation(pipe_err[IN_FOR_PARENT], HANDLE_FLAG_INHERIT,
0))
goto err_pipe;
}
// TODO(schwehr): Consider initializing piProcInfo.
PROCESS_INFORMATION piProcInfo;
memset(&piProcInfo, 0, sizeof(PROCESS_INFORMATION));
STARTUPINFO siStartInfo;
memset(&siStartInfo, 0, sizeof(STARTUPINFO));
siStartInfo.cb = sizeof(STARTUPINFO);
siStartInfo.hStdInput = bCreateInputPipe ? pipe_in[IN_FOR_PARENT]
: GetStdHandle(STD_INPUT_HANDLE);
siStartInfo.hStdOutput = bCreateOutputPipe
? pipe_out[OUT_FOR_PARENT]
: GetStdHandle(STD_OUTPUT_HANDLE);
siStartInfo.hStdError = bCreateErrorPipe ? pipe_err[OUT_FOR_PARENT]
: GetStdHandle(STD_ERROR_HANDLE);
siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
for (int i = 0; papszArgv[i] != nullptr; i++)
{
if (i > 0)
osCommandLine += " ";
// We need to quote arguments with spaces in them (if not already done).
if (strchr(papszArgv[i], ' ') != nullptr && papszArgv[i][0] != '"')
{
osCommandLine += "\"";
osCommandLine += papszArgv[i];
osCommandLine += "\"";
}
else
{
osCommandLine += papszArgv[i];
}
}
if (!CreateProcess(nullptr, const_cast<CHAR *>(osCommandLine.c_str()),
nullptr, // Process security attributes
nullptr, // Primary thread security attributes
TRUE, // Handles are inherited
CREATE_NO_WINDOW |
NORMAL_PRIORITY_CLASS, // Creation flags
nullptr, // Use parent's environment
nullptr, // Use parent's current directory
&siStartInfo, &piProcInfo))
{
CPLError(CE_Failure, CPLE_AppDefined, "Could not create process %s",
osCommandLine.c_str());
goto err;
}
// Close unused end of pipe.
if (bCreateInputPipe)
CloseHandle(pipe_in[IN_FOR_PARENT]);
if (bCreateOutputPipe)
CloseHandle(pipe_out[OUT_FOR_PARENT]);
if (bCreateErrorPipe)
CloseHandle(pipe_err[OUT_FOR_PARENT]);
{
CPLSpawnedProcess *p = static_cast<CPLSpawnedProcess *>(
CPLMalloc(sizeof(CPLSpawnedProcess)));
p->hProcess = piProcInfo.hProcess;
p->nProcessId = piProcInfo.dwProcessId;
p->hThread = piProcInfo.hThread;
p->fin = pipe_out[IN_FOR_PARENT];
p->fout = pipe_in[OUT_FOR_PARENT];
p->ferr = pipe_err[IN_FOR_PARENT];
return p;
}
err_pipe:
CPLError(CE_Failure, CPLE_AppDefined, "Could not create pipe");
err:
for (int i = 0; i < 2; i++)
{
if (pipe_in[i] != nullptr)
CloseHandle(pipe_in[i]);
if (pipe_out[i] != nullptr)
CloseHandle(pipe_out[i]);
if (pipe_err[i] != nullptr)
CloseHandle(pipe_err[i]);
}
return nullptr;
}
/************************************************************************/
/* CPLSpawnAsyncGetChildProcessId() */
/************************************************************************/
CPL_PID CPLSpawnAsyncGetChildProcessId(CPLSpawnedProcess *p)
{
return p->nProcessId;
}
/************************************************************************/
/* CPLSpawnAsyncFinish() */
/************************************************************************/
int CPLSpawnAsyncFinish(CPLSpawnedProcess *p, int bWait, int /* bKill */)
{
// Get the exit code.
DWORD exitCode = -1;
if (bWait)
{
WaitForSingleObject(p->hProcess, INFINITE);
GetExitCodeProcess(p->hProcess, &exitCode);
}
else
{
exitCode = 0;
}
CloseHandle(p->hProcess);
CloseHandle(p->hThread);
CPLSpawnAsyncCloseInputFileHandle(p);
CPLSpawnAsyncCloseOutputFileHandle(p);
CPLSpawnAsyncCloseErrorFileHandle(p);
CPLFree(p);
return static_cast<int>(exitCode);
}
/************************************************************************/
/* CPLSpawnAsyncCloseInputFileHandle() */
/************************************************************************/
void CPLSpawnAsyncCloseInputFileHandle(CPLSpawnedProcess *p)
{
if (p->fin != nullptr)
CloseHandle(p->fin);
p->fin = nullptr;
}
/************************************************************************/
/* CPLSpawnAsyncCloseOutputFileHandle() */
/************************************************************************/
void CPLSpawnAsyncCloseOutputFileHandle(CPLSpawnedProcess *p)
{
if (p->fout != nullptr)
CloseHandle(p->fout);
p->fout = nullptr;
}
/************************************************************************/
/* CPLSpawnAsyncCloseErrorFileHandle() */
/************************************************************************/
void CPLSpawnAsyncCloseErrorFileHandle(CPLSpawnedProcess *p)
{
if (p->ferr != nullptr)
CloseHandle(p->ferr);
p->ferr = nullptr;
}
#else // Not WIN32
/************************************************************************/
/* CPLPipeRead() */
/************************************************************************/
/**
* Read data from the standard output of a forked process.
*
* @param fin handle returned by CPLSpawnAsyncGetInputFileHandle().
* @param data buffer in which to write.
* @param length number of bytes to read.
*
* @return TRUE in case of success.
*
* @since GDAL 1.10.0
*/
int CPLPipeRead(CPL_FILE_HANDLE fin, void *data, int length)
{
GByte *pabyData = static_cast<GByte *>(data);
int nRemain = length;
while (nRemain > 0)
{
while (true)
{
const int n = static_cast<int>(read(fin, pabyData, nRemain));
if (n < 0)
{
if (errno == EINTR)
continue;
else
return FALSE;
}
else if (n == 0)
return FALSE;
pabyData += n;
nRemain -= n;
break;
}
}
return TRUE;
}
/************************************************************************/
/* CPLPipeWrite() */
/************************************************************************/
/**
* Write data to the standard input of a forked process.
*
* @param fout handle returned by CPLSpawnAsyncGetOutputFileHandle().
* @param data buffer from which to read.
* @param length number of bytes to write.
*
* @return TRUE in case of success.
*
* @since GDAL 1.10.0
*/
int CPLPipeWrite(CPL_FILE_HANDLE fout, const void *data, int length)
{
const GByte *pabyData = static_cast<const GByte *>(data);
int nRemain = length;
while (nRemain > 0)
{
while (true)
{
const int n = static_cast<int>(write(fout, pabyData, nRemain));
if (n < 0)
{
if (errno == EINTR)
continue;
else
return FALSE;
}
pabyData += n;
nRemain -= n;
break;
}
}
return TRUE;
}
/************************************************************************/
/* FillFileFromPipe() */
/************************************************************************/
static void FillFileFromPipe(CPL_FILE_HANDLE pipe_fd, VSILFILE *fout)
{
char buf[PIPE_BUFFER_SIZE] = {};
while (true)
{
const int nRead =
static_cast<int>(read(pipe_fd, buf, PIPE_BUFFER_SIZE));
if (nRead <= 0)
break;
const int nWritten = static_cast<int>(VSIFWriteL(buf, 1, nRead, fout));
if (nWritten < nRead)
break;
}
}
/************************************************************************/
/* CPLSpawnAsync() */
/************************************************************************/
struct _CPLSpawnedProcess
{
pid_t pid;
CPL_FILE_HANDLE fin;
CPL_FILE_HANDLE fout;
CPL_FILE_HANDLE ferr;
#ifdef HAVE_POSIX_SPAWNP
bool bFreeActions;
posix_spawn_file_actions_t actions;
#endif
};
/**
* Runs an executable in another process (or fork the current process)
* and return immediately.
*
* This function launches an executable and returns immediately, while letting
* the sub-process to run asynchronously.
*
* It is implemented as CreateProcess() on Windows platforms, and fork()/exec()
* on other platforms.
*
* On Unix, a pointer of function can be provided to run in the child process,
* without exec()'ing a new executable.
*
* @param pfnMain the function to run in the child process (Unix only).
* @param papszArgv argument list of the executable to run. papszArgv[0] is the
* name of the executable.
* @param bCreateInputPipe set to TRUE to create a pipe for the child
* input stream.
* @param bCreateOutputPipe set to TRUE to create a pipe for the child
* output stream.
* @param bCreateErrorPipe set to TRUE to create a pipe for the child
* error stream.
* @param papszOptions unused. should be set to NULL.
*
* @return a handle, that must be freed with CPLSpawnAsyncFinish()
*
* @since GDAL 1.10.0
*/
CPLSpawnedProcess *
CPLSpawnAsync(int (*pfnMain)(CPL_FILE_HANDLE, CPL_FILE_HANDLE),
const char *const papszArgv[], int bCreateInputPipe,
int bCreateOutputPipe, int bCreateErrorPipe,
CPL_UNUSED char **papszOptions)
{
int pipe_in[2] = {-1, -1};
int pipe_out[2] = {-1, -1};
int pipe_err[2] = {-1, -1};
if ((bCreateInputPipe && pipe(pipe_in)) ||
(bCreateOutputPipe && pipe(pipe_out)) ||
(bCreateErrorPipe && pipe(pipe_err)))
{
CPLError(CE_Failure, CPLE_AppDefined, "Could not create pipe");
return nullptr;
}
bool bDup2In = CPL_TO_BOOL(bCreateInputPipe);
bool bDup2Out = CPL_TO_BOOL(bCreateOutputPipe);
bool bDup2Err = CPL_TO_BOOL(bCreateErrorPipe);
char **papszArgvDup = CSLDuplicate(const_cast<char **>(papszArgv));
// If we don't do any file actions, posix_spawnp() might be implemented
// efficiently as a vfork()/exec() pair (or if it is not available, we
// can use vfork()/exec()), so if the child is cooperative
// we pass the pipe handles as commandline arguments.
if (papszArgv != nullptr)
{
for (int i = 0; papszArgvDup[i] != nullptr; i++)
{
if (bCreateInputPipe && strcmp(papszArgvDup[i], "{pipe_in}") == 0)
{
CPLFree(papszArgvDup[i]);
papszArgvDup[i] = CPLStrdup(CPLSPrintf(
"%d,%d", pipe_in[IN_FOR_PARENT], pipe_in[OUT_FOR_PARENT]));
bDup2In = false;
}
else if (bCreateOutputPipe &&
strcmp(papszArgvDup[i], "{pipe_out}") == 0)
{
CPLFree(papszArgvDup[i]);
papszArgvDup[i] =
CPLStrdup(CPLSPrintf("%d,%d", pipe_out[OUT_FOR_PARENT],
pipe_out[IN_FOR_PARENT]));
bDup2Out = false;
}
else if (bCreateErrorPipe &&
strcmp(papszArgvDup[i], "{pipe_err}") == 0)
{
CPLFree(papszArgvDup[i]);
papszArgvDup[i] =
CPLStrdup(CPLSPrintf("%d,%d", pipe_err[OUT_FOR_PARENT],
pipe_err[IN_FOR_PARENT]));
bDup2Err = false;
}
}
}
#ifdef HAVE_POSIX_SPAWNP
if (papszArgv != nullptr)
{
bool bHasActions = false;
posix_spawn_file_actions_t actions;
if (bDup2In)
{
/*if( !bHasActions )*/ posix_spawn_file_actions_init(&actions);
posix_spawn_file_actions_adddup2(&actions, pipe_in[IN_FOR_PARENT],
fileno(stdin));
posix_spawn_file_actions_addclose(&actions,
pipe_in[OUT_FOR_PARENT]);
bHasActions = true;
}
if (bDup2Out)
{
if (!bHasActions)
posix_spawn_file_actions_init(&actions);
posix_spawn_file_actions_adddup2(&actions, pipe_out[OUT_FOR_PARENT],
fileno(stdout));
posix_spawn_file_actions_addclose(&actions,
pipe_out[IN_FOR_PARENT]);
bHasActions = true;
}
if (bDup2Err)
{
if (!bHasActions)
posix_spawn_file_actions_init(&actions);
posix_spawn_file_actions_adddup2(&actions, pipe_err[OUT_FOR_PARENT],
fileno(stderr));
posix_spawn_file_actions_addclose(&actions,
pipe_err[IN_FOR_PARENT]);
bHasActions = true;
}
pid_t pid = 0;
assert(papszArgvDup[0] != nullptr);
if (posix_spawnp(&pid, papszArgvDup[0],
bHasActions ? &actions : nullptr, nullptr,
papszArgvDup, environ) != 0)
{
if (bHasActions)
posix_spawn_file_actions_destroy(&actions);
CPLError(CE_Failure, CPLE_AppDefined, "posix_spawnp() failed");
CSLDestroy(papszArgvDup);
for (int i = 0; i < 2; i++)
{
if (pipe_in[i] >= 0)
close(pipe_in[i]);
if (pipe_out[i] >= 0)
close(pipe_out[i]);
if (pipe_err[i] >= 0)
close(pipe_err[i]);
}
return nullptr;
}
CSLDestroy(papszArgvDup);
// Close unused end of pipe.
if (bCreateInputPipe)
close(pipe_in[IN_FOR_PARENT]);
if (bCreateOutputPipe)
close(pipe_out[OUT_FOR_PARENT]);
if (bCreateErrorPipe)
close(pipe_err[OUT_FOR_PARENT]);
// Ignore SIGPIPE.
#ifdef SIGPIPE
std::signal(SIGPIPE, SIG_IGN);
#endif
CPLSpawnedProcess *p = static_cast<CPLSpawnedProcess *>(
CPLMalloc(sizeof(CPLSpawnedProcess)));
if (bHasActions)
memcpy(&p->actions, &actions, sizeof(actions));
p->bFreeActions = bHasActions;
p->pid = pid;
p->fin = pipe_out[IN_FOR_PARENT];
p->fout = pipe_in[OUT_FOR_PARENT];
p->ferr = pipe_err[IN_FOR_PARENT];
return p;
}
#endif // #ifdef HAVE_POSIX_SPAWNP
pid_t pid = 0;
#if defined(HAVE_VFORK) && !defined(HAVE_POSIX_SPAWNP)
if (papszArgv != nullptr && !bDup2In && !bDup2Out && !bDup2Err)
{
// Workaround clang static analyzer warning about unsafe use of vfork.
pid_t (*p_vfork)(void) = vfork;
pid = p_vfork();
}
else
#endif
{
pid = fork();
}
if (pid == 0)
{
// Close unused end of pipe.
if (bDup2In)
close(pipe_in[OUT_FOR_PARENT]);
if (bDup2Out)
close(pipe_out[IN_FOR_PARENT]);
if (bDup2Err)
close(pipe_err[IN_FOR_PARENT]);
#ifndef HAVE_POSIX_SPAWNP
if (papszArgv != nullptr)
{
if (bDup2In)
dup2(pipe_in[IN_FOR_PARENT], fileno(stdin));
if (bDup2Out)
dup2(pipe_out[OUT_FOR_PARENT], fileno(stdout));
if (bDup2Err)
dup2(pipe_err[OUT_FOR_PARENT], fileno(stderr));
execvp(papszArgvDup[0], papszArgvDup);
_exit(1);
}
else
#endif // HAVE_POSIX_SPAWNP
{
if (bCreateErrorPipe)
close(pipe_err[OUT_FOR_PARENT]);
int nRet = 0;
if (pfnMain != nullptr)
nRet = pfnMain(bCreateInputPipe ? pipe_in[IN_FOR_PARENT]
: fileno(stdin),
bCreateOutputPipe ? pipe_out[OUT_FOR_PARENT]
: fileno(stdout));
_exit(nRet);
}
}
else if (pid > 0)
{
CSLDestroy(papszArgvDup);
// Close unused end of pipe.
if (bCreateInputPipe)
close(pipe_in[IN_FOR_PARENT]);
if (bCreateOutputPipe)
close(pipe_out[OUT_FOR_PARENT]);
if (bCreateErrorPipe)
close(pipe_err[OUT_FOR_PARENT]);
// Ignore SIGPIPE.
#ifdef SIGPIPE
std::signal(SIGPIPE, SIG_IGN);
#endif
CPLSpawnedProcess *p = static_cast<CPLSpawnedProcess *>(
CPLMalloc(sizeof(CPLSpawnedProcess)));
#ifdef HAVE_POSIX_SPAWNP
p->bFreeActions = false;
#endif
p->pid = pid;
p->fin = pipe_out[IN_FOR_PARENT];
p->fout = pipe_in[OUT_FOR_PARENT];
p->ferr = pipe_err[IN_FOR_PARENT];
return p;
}
CPLError(CE_Failure, CPLE_AppDefined, "Fork failed");
CSLDestroy(papszArgvDup);
for (int i = 0; i < 2; i++)
{
if (pipe_in[i] >= 0)
close(pipe_in[i]);
if (pipe_out[i] >= 0)
close(pipe_out[i]);
if (pipe_err[i] >= 0)
close(pipe_err[i]);
}
return nullptr;
}
/************************************************************************/
/* CPLSpawnAsyncGetChildProcessId() */
/************************************************************************/
CPL_PID CPLSpawnAsyncGetChildProcessId(CPLSpawnedProcess *p)
{
return p->pid;
}
/************************************************************************/
/* CPLSpawnAsyncFinish() */
/************************************************************************/
/**
* \fn CPLSpawnAsyncFinish(CPLSpawnedProcess*,int,int)
* Wait for the forked process to finish.
*
* @param p handle returned by CPLSpawnAsync()
* @param bWait set to TRUE to wait for the child to terminate. Otherwise the
* associated handles are just cleaned.
* @param bKill set to TRUE to force child termination (unimplemented right
* now).
*
* @return the return code of the forked process if bWait == TRUE, 0 otherwise
*
* @since GDAL 1.10.0
*/
int CPLSpawnAsyncFinish(CPLSpawnedProcess *p, int bWait, CPL_UNUSED int bKill)
{
int status = 0;
if (bWait)
{
while (true)
{
status = -1;
const int ret = waitpid(p->pid, &status, 0);
if (ret < 0)
{
if (errno != EINTR)
{
break;
}
}
else
{
break;
}
}
}
CPLSpawnAsyncCloseInputFileHandle(p);
CPLSpawnAsyncCloseOutputFileHandle(p);
CPLSpawnAsyncCloseErrorFileHandle(p);
#ifdef HAVE_POSIX_SPAWNP
if (p->bFreeActions)
posix_spawn_file_actions_destroy(&p->actions);
#endif
CPLFree(p);
return status;
}
/************************************************************************/
/* CPLSpawnAsyncCloseInputFileHandle() */
/************************************************************************/
void CPLSpawnAsyncCloseInputFileHandle(CPLSpawnedProcess *p)
{
if (p->fin >= 0)
close(p->fin);
p->fin = -1;
}
/************************************************************************/
/* CPLSpawnAsyncCloseOutputFileHandle() */
/************************************************************************/
void CPLSpawnAsyncCloseOutputFileHandle(CPLSpawnedProcess *p)
{
if (p->fout >= 0)
close(p->fout);
p->fout = -1;
}
/************************************************************************/
/* CPLSpawnAsyncCloseErrorFileHandle() */
/************************************************************************/
void CPLSpawnAsyncCloseErrorFileHandle(CPLSpawnedProcess *p)
{
if (p->ferr >= 0)
close(p->ferr);
p->ferr = -1;
}
#endif
/************************************************************************/
/* CPLSpawnAsyncGetInputFileHandle() */
/************************************************************************/
/**
* Return the file handle of the standard output of the forked process
* from which to read.
*
* @param p handle returned by CPLSpawnAsync().
*
* @return the file handle.
*
* @since GDAL 1.10.0
*/
CPL_FILE_HANDLE CPLSpawnAsyncGetInputFileHandle(CPLSpawnedProcess *p)
{
return p->fin;
}
/************************************************************************/
/* CPLSpawnAsyncGetOutputFileHandle() */
/************************************************************************/
/**
* Return the file handle of the standard input of the forked process
* into which to write
*
* @param p handle returned by CPLSpawnAsync().
*
* @return the file handle.
*
* @since GDAL 1.10.0
*/
CPL_FILE_HANDLE CPLSpawnAsyncGetOutputFileHandle(CPLSpawnedProcess *p)
{
return p->fout;
}
/************************************************************************/
/* CPLSpawnAsyncGetErrorFileHandle() */
/************************************************************************/
/**
* Return the file handle of the standard error of the forked process
* from which to read.
*
* @param p handle returned by CPLSpawnAsync().
*
* @return the file handle
*
* @since GDAL 1.10.0
*/
CPL_FILE_HANDLE CPLSpawnAsyncGetErrorFileHandle(CPLSpawnedProcess *p)
{
return p->ferr;
}
|