| 1 |
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
| 2 |
file Copyright.txt or https://cmake.org/licensing#kwsys for details. */ |
| 3 |
#if !defined(_WIN32) && !defined(__APPLE__) && !defined(__OpenBSD__) |
| 4 |
/* NOLINTNEXTLINE(bugprone-reserved-identifier) */ |
| 5 |
# define _XOPEN_SOURCE 600 |
| 6 |
#endif |
| 7 |
#include "kwsysPrivate.h" |
| 8 |
#include KWSYS_HEADER(Process.h) |
| 9 |
#include KWSYS_HEADER(Encoding.h) |
| 10 |
|
| 11 |
/* Work-around CMake dependency scanning limitation. This must |
| 12 |
duplicate the above list of headers. */ |
| 13 |
#if 0 |
| 14 |
# include "Encoding.h.in" |
| 15 |
# include "Process.h.in" |
| 16 |
#endif |
| 17 |
|
| 18 |
#include <assert.h> |
| 19 |
#include <limits.h> |
| 20 |
#include <stdio.h> |
| 21 |
#include <stdlib.h> |
| 22 |
#include <string.h> |
| 23 |
|
| 24 |
#if defined(_WIN32) |
| 25 |
# include <windows.h> |
| 26 |
#else |
| 27 |
# include <signal.h> |
| 28 |
# include <unistd.h> |
| 29 |
#endif |
| 30 |
|
| 31 |
/* Platform-specific sleep functions. */ |
| 32 |
|
| 33 |
#if defined(__BEOS__) && !defined(__ZETA__) |
| 34 |
/* BeOS 5 doesn't have usleep(), but it has snooze(), which is identical. */ |
| 35 |
# include <be/kernel/OS.h> |
| 36 |
static inline void testProcess_usleep(unsigned int usec) |
| 37 |
{ |
| 38 |
snooze(usec); |
| 39 |
} |
| 40 |
#elif defined(_WIN32) |
| 41 |
/* Windows can only sleep in millisecond intervals. */ |
| 42 |
static void testProcess_usleep(unsigned int usec) |
| 43 |
{ |
| 44 |
Sleep(usec / 1000); |
| 45 |
} |
| 46 |
#else |
| 47 |
# define testProcess_usleep usleep |
| 48 |
#endif |
| 49 |
|
| 50 |
#if defined(_WIN32) |
| 51 |
static void testProcess_sleep(unsigned int sec) |
| 52 |
{ |
| 53 |
Sleep(sec * 1000); |
| 54 |
} |
| 55 |
#else |
| 56 |
static void testProcess_sleep(unsigned int sec) |
| 57 |
{ |
| 58 |
sleep(sec); |
| 59 |
} |
| 60 |
#endif |
| 61 |
|
| 62 |
int runChild(const char* cmd[], int state, int exception, int value, int share, |
| 63 |
int output, int delay, double timeout, int poll, int repeat, |
| 64 |
int disown, int createNewGroup, unsigned int interruptDelay); |
| 65 |
|
| 66 |
static int test1(int argc, const char* argv[]) |
| 67 |
{ |
| 68 |
/* This is a very basic functional test of kwsysProcess. It is repeated |
| 69 |
numerous times to verify that there are no resource leaks in kwsysProcess |
| 70 |
that eventually lead to an error. Many versions of OS X will fail after |
| 71 |
256 leaked file handles, so 257 iterations seems to be a good test. On |
| 72 |
the other hand, too many iterations will cause the test to time out - |
| 73 |
especially if the test is instrumented with e.g. valgrind. |
| 74 |
|
| 75 |
If you have problems with this test timing out on your system, or want to |
| 76 |
run more than 257 iterations, you can change the number of iterations by |
| 77 |
setting the KWSYS_TEST_PROCESS_1_COUNT environment variable. */ |
| 78 |
(void)argc; |
| 79 |
(void)argv; |
| 80 |
fprintf(stdout, "Output on stdout from test returning 0.\n"); |
| 81 |
fprintf(stderr, "Output on stderr from test returning 0.\n"); |
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
static int test2(int argc, const char* argv[]) |
| 86 |
{ |
| 87 |
(void)argc; |
| 88 |
(void)argv; |
| 89 |
fprintf(stdout, "Output on stdout from test returning 123.\n"); |
| 90 |
fprintf(stderr, "Output on stderr from test returning 123.\n"); |
| 91 |
return 123; |
| 92 |
} |
| 93 |
|
| 94 |
static int test3(int argc, const char* argv[]) |
| 95 |
{ |
| 96 |
(void)argc; |
| 97 |
(void)argv; |
| 98 |
fprintf(stdout, "Output before sleep on stdout from timeout test.\n"); |
| 99 |
fprintf(stderr, "Output before sleep on stderr from timeout test.\n"); |
| 100 |
fflush(stdout); |
| 101 |
fflush(stderr); |
| 102 |
testProcess_sleep(15); |
| 103 |
fprintf(stdout, "Output after sleep on stdout from timeout test.\n"); |
| 104 |
fprintf(stderr, "Output after sleep on stderr from timeout test.\n"); |
| 105 |
return 0; |
| 106 |
} |
| 107 |
|
| 108 |
static int test4(int argc, const char* argv[]) |
| 109 |
{ |
| 110 |
#ifndef CRASH_USING_ABORT |
| 111 |
/* Prepare a pointer to an invalid address. Don't use null, because |
| 112 |
dereferencing null is undefined behaviour and compilers are free to |
| 113 |
do whatever they want. ex: Clang will warn at compile time, or even |
| 114 |
optimize away the write. We hope to 'outsmart' them by using |
| 115 |
'volatile' and a slightly larger address, based on a runtime value. */ |
| 116 |
volatile int* invalidAddress = 0; |
| 117 |
invalidAddress += argc ? 1 : 2; |
| 118 |
#endif |
| 119 |
|
| 120 |
#if defined(_WIN32) |
| 121 |
/* Avoid error diagnostic popups since we are crashing on purpose. */ |
| 122 |
SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX); |
| 123 |
#elif defined(__BEOS__) || defined(__HAIKU__) |
| 124 |
/* Avoid error diagnostic popups since we are crashing on purpose. */ |
| 125 |
disable_debugger(1); |
| 126 |
#endif |
| 127 |
(void)argc; |
| 128 |
(void)argv; |
| 129 |
fprintf(stdout, "Output before crash on stdout from crash test.\n"); |
| 130 |
fprintf(stderr, "Output before crash on stderr from crash test.\n"); |
| 131 |
fflush(stdout); |
| 132 |
fflush(stderr); |
| 133 |
#ifdef CRASH_USING_ABORT |
| 134 |
abort(); |
| 135 |
#else |
| 136 |
assert(invalidAddress); /* Quiet Clang scan-build. */ |
| 137 |
/* Provoke deliberate crash by writing to the invalid address. */ |
| 138 |
*invalidAddress = 0; |
| 139 |
#endif |
| 140 |
fprintf(stdout, "Output after crash on stdout from crash test.\n"); |
| 141 |
fprintf(stderr, "Output after crash on stderr from crash test.\n"); |
| 142 |
return 0; |
| 143 |
} |
| 144 |
|
| 145 |
static int test5(int argc, const char* argv[]) |
| 146 |
{ |
| 147 |
int r; |
| 148 |
const char* cmd[4]; |
| 149 |
(void)argc; |
| 150 |
cmd[0] = argv[0]; |
| 151 |
cmd[1] = "run"; |
| 152 |
cmd[2] = "4"; |
| 153 |
cmd[3] = 0; |
| 154 |
fprintf(stdout, "Output on stdout before recursive test.\n"); |
| 155 |
fprintf(stderr, "Output on stderr before recursive test.\n"); |
| 156 |
fflush(stdout); |
| 157 |
fflush(stderr); |
| 158 |
r = runChild(cmd, kwsysProcess_State_Exception, |
| 159 |
#ifdef CRASH_USING_ABORT |
| 160 |
kwsysProcess_Exception_Other, |
| 161 |
#else |
| 162 |
kwsysProcess_Exception_Fault, |
| 163 |
#endif |
| 164 |
1, 1, 1, 0, 15, 0, 1, 0, 0, 0); |
| 165 |
fprintf(stdout, "Output on stdout after recursive test.\n"); |
| 166 |
fprintf(stderr, "Output on stderr after recursive test.\n"); |
| 167 |
fflush(stdout); |
| 168 |
fflush(stderr); |
| 169 |
return r; |
| 170 |
} |
| 171 |
|
| 172 |
#define TEST6_SIZE (4096 * 2) |
| 173 |
static void test6(int argc, const char* argv[]) |
| 174 |
{ |
| 175 |
int i; |
| 176 |
char runaway[TEST6_SIZE + 1]; |
| 177 |
(void)argc; |
| 178 |
(void)argv; |
| 179 |
for (i = 0; i < TEST6_SIZE; ++i) { |
| 180 |
runaway[i] = '.'; |
| 181 |
} |
| 182 |
runaway[TEST6_SIZE] = '\n'; |
| 183 |
|
| 184 |
/* Generate huge amounts of output to test killing. */ |
| 185 |
for (;;) { |
| 186 |
fwrite(runaway, 1, TEST6_SIZE + 1, stdout); |
| 187 |
fflush(stdout); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
/* Define MINPOLL to be one more than the number of times output is |
| 192 |
written. Define MAXPOLL to be the largest number of times a loop |
| 193 |
delaying 1/10th of a second should ever have to poll. */ |
| 194 |
#define MINPOLL 5 |
| 195 |
#define MAXPOLL 20 |
| 196 |
static int test7(int argc, const char* argv[]) |
| 197 |
{ |
| 198 |
(void)argc; |
| 199 |
(void)argv; |
| 200 |
fprintf(stdout, "Output on stdout before sleep.\n"); |
| 201 |
fprintf(stderr, "Output on stderr before sleep.\n"); |
| 202 |
fflush(stdout); |
| 203 |
fflush(stderr); |
| 204 |
/* Sleep for 1 second. */ |
| 205 |
testProcess_sleep(1); |
| 206 |
fprintf(stdout, "Output on stdout after sleep.\n"); |
| 207 |
fprintf(stderr, "Output on stderr after sleep.\n"); |
| 208 |
fflush(stdout); |
| 209 |
fflush(stderr); |
| 210 |
return 0; |
| 211 |
} |
| 212 |
|
| 213 |
static int test8(int argc, const char* argv[]) |
| 214 |
{ |
| 215 |
/* Create a disowned grandchild to test handling of processes |
| 216 |
that exit before their children. */ |
| 217 |
int r; |
| 218 |
const char* cmd[4]; |
| 219 |
(void)argc; |
| 220 |
cmd[0] = argv[0]; |
| 221 |
cmd[1] = "run"; |
| 222 |
cmd[2] = "108"; |
| 223 |
cmd[3] = 0; |
| 224 |
fprintf(stdout, "Output on stdout before grandchild test.\n"); |
| 225 |
fprintf(stderr, "Output on stderr before grandchild test.\n"); |
| 226 |
fflush(stdout); |
| 227 |
fflush(stderr); |
| 228 |
r = runChild(cmd, kwsysProcess_State_Disowned, kwsysProcess_Exception_None, |
| 229 |
1, 1, 1, 0, 10, 0, 1, 1, 0, 0); |
| 230 |
fprintf(stdout, "Output on stdout after grandchild test.\n"); |
| 231 |
fprintf(stderr, "Output on stderr after grandchild test.\n"); |
| 232 |
fflush(stdout); |
| 233 |
fflush(stderr); |
| 234 |
return r; |
| 235 |
} |
| 236 |
|
| 237 |
static int test8_grandchild(int argc, const char* argv[]) |
| 238 |
{ |
| 239 |
(void)argc; |
| 240 |
(void)argv; |
| 241 |
fprintf(stdout, "Output on stdout from grandchild before sleep.\n"); |
| 242 |
fprintf(stderr, "Output on stderr from grandchild before sleep.\n"); |
| 243 |
fflush(stdout); |
| 244 |
fflush(stderr); |
| 245 |
/* TODO: Instead of closing pipes here leave them open to make sure |
| 246 |
the grandparent can stop listening when the parent exits. This |
| 247 |
part of the test cannot be enabled until the feature is |
| 248 |
implemented. */ |
| 249 |
fclose(stdout); |
| 250 |
fclose(stderr); |
| 251 |
testProcess_sleep(15); |
| 252 |
return 0; |
| 253 |
} |
| 254 |
|
| 255 |
static int test9(int argc, const char* argv[]) |
| 256 |
{ |
| 257 |
/* Test Ctrl+C behavior: the root test program will send a Ctrl+C to this |
| 258 |
process. Here, we start a child process that sleeps for a long time |
| 259 |
while ignoring signals. The test is successful if this process waits |
| 260 |
for the child to return before exiting from the Ctrl+C handler. |
| 261 |
|
| 262 |
WARNING: This test will falsely pass if the share parameter of runChild |
| 263 |
was set to 0 when invoking the test9 process. */ |
| 264 |
int r; |
| 265 |
const char* cmd[4]; |
| 266 |
(void)argc; |
| 267 |
cmd[0] = argv[0]; |
| 268 |
cmd[1] = "run"; |
| 269 |
cmd[2] = "109"; |
| 270 |
cmd[3] = 0; |
| 271 |
fprintf(stdout, "Output on stdout before grandchild test.\n"); |
| 272 |
fprintf(stderr, "Output on stderr before grandchild test.\n"); |
| 273 |
fflush(stdout); |
| 274 |
fflush(stderr); |
| 275 |
r = runChild(cmd, kwsysProcess_State_Exited, kwsysProcess_Exception_None, 0, |
| 276 |
1, 1, 0, 30, 0, 1, 0, 0, 0); |
| 277 |
/* This sleep will avoid a race condition between this function exiting |
| 278 |
normally and our Ctrl+C handler exiting abnormally after the process |
| 279 |
exits. */ |
| 280 |
testProcess_sleep(1); |
| 281 |
fprintf(stdout, "Output on stdout after grandchild test.\n"); |
| 282 |
fprintf(stderr, "Output on stderr after grandchild test.\n"); |
| 283 |
fflush(stdout); |
| 284 |
fflush(stderr); |
| 285 |
return r; |
| 286 |
} |
| 287 |
|
| 288 |
#if defined(_WIN32) |
| 289 |
static BOOL WINAPI test9_grandchild_handler(DWORD dwCtrlType) |
| 290 |
{ |
| 291 |
/* Ignore all Ctrl+C/Break signals. We must use an actual handler function |
| 292 |
instead of using SetConsoleCtrlHandler(NULL, TRUE) so that we can also |
| 293 |
ignore Ctrl+Break in addition to Ctrl+C. */ |
| 294 |
(void)dwCtrlType; |
| 295 |
return TRUE; |
| 296 |
} |
| 297 |
#endif |
| 298 |
|
| 299 |
static int test9_grandchild(int argc, const char* argv[]) |
| 300 |
{ |
| 301 |
/* The grandchild just sleeps for a few seconds while ignoring signals. */ |
| 302 |
(void)argc; |
| 303 |
(void)argv; |
| 304 |
#if defined(_WIN32) |
| 305 |
if (!SetConsoleCtrlHandler(test9_grandchild_handler, TRUE)) { |
| 306 |
return 1; |
| 307 |
} |
| 308 |
#else |
| 309 |
struct sigaction sa; |
| 310 |
memset(&sa, 0, sizeof(sa)); |
| 311 |
sa.sa_handler = SIG_IGN; |
| 312 |
sigemptyset(&sa.sa_mask); |
| 313 |
if (sigaction(SIGINT, &sa, 0) < 0) { |
| 314 |
return 1; |
| 315 |
} |
| 316 |
#endif |
| 317 |
fprintf(stdout, "Output on stdout from grandchild before sleep.\n"); |
| 318 |
fprintf(stderr, "Output on stderr from grandchild before sleep.\n"); |
| 319 |
fflush(stdout); |
| 320 |
fflush(stderr); |
| 321 |
/* Sleep for 9 seconds. */ |
| 322 |
testProcess_sleep(9); |
| 323 |
fprintf(stdout, "Output on stdout from grandchild after sleep.\n"); |
| 324 |
fprintf(stderr, "Output on stderr from grandchild after sleep.\n"); |
| 325 |
fflush(stdout); |
| 326 |
fflush(stderr); |
| 327 |
return 0; |
| 328 |
} |
| 329 |
|
| 330 |
static int test10(int argc, const char* argv[]) |
| 331 |
{ |
| 332 |
/* Test Ctrl+C behavior: the root test program will send a Ctrl+C to this |
| 333 |
process. Here, we start a child process that sleeps for a long time and |
| 334 |
processes signals normally. However, this grandchild is created in a new |
| 335 |
process group - ensuring that Ctrl+C we receive is sent to our process |
| 336 |
groups. We make sure it exits anyway. */ |
| 337 |
int r; |
| 338 |
const char* cmd[4]; |
| 339 |
(void)argc; |
| 340 |
cmd[0] = argv[0]; |
| 341 |
cmd[1] = "run"; |
| 342 |
cmd[2] = "110"; |
| 343 |
cmd[3] = 0; |
| 344 |
fprintf(stdout, "Output on stdout before grandchild test.\n"); |
| 345 |
fprintf(stderr, "Output on stderr before grandchild test.\n"); |
| 346 |
fflush(stdout); |
| 347 |
fflush(stderr); |
| 348 |
r = |
| 349 |
runChild(cmd, kwsysProcess_State_Exception, |
| 350 |
kwsysProcess_Exception_Interrupt, 0, 1, 1, 0, 30, 0, 1, 0, 1, 0); |
| 351 |
fprintf(stdout, "Output on stdout after grandchild test.\n"); |
| 352 |
fprintf(stderr, "Output on stderr after grandchild test.\n"); |
| 353 |
fflush(stdout); |
| 354 |
fflush(stderr); |
| 355 |
return r; |
| 356 |
} |
| 357 |
|
| 358 |
static int test10_grandchild(int argc, const char* argv[]) |
| 359 |
{ |
| 360 |
/* The grandchild just sleeps for a few seconds and handles signals. */ |
| 361 |
(void)argc; |
| 362 |
(void)argv; |
| 363 |
fprintf(stdout, "Output on stdout from grandchild before sleep.\n"); |
| 364 |
fprintf(stderr, "Output on stderr from grandchild before sleep.\n"); |
| 365 |
fflush(stdout); |
| 366 |
fflush(stderr); |
| 367 |
/* Sleep for 6 seconds. */ |
| 368 |
testProcess_sleep(6); |
| 369 |
fprintf(stdout, "Output on stdout from grandchild after sleep.\n"); |
| 370 |
fprintf(stderr, "Output on stderr from grandchild after sleep.\n"); |
| 371 |
fflush(stdout); |
| 372 |
fflush(stderr); |
| 373 |
return 0; |
| 374 |
} |
| 375 |
|
| 376 |
static int runChild2(kwsysProcess* kp, const char* cmd[], int state, |
| 377 |
int exception, int value, int share, int output, |
| 378 |
int delay, double timeout, int poll, int disown, |
| 379 |
int createNewGroup, unsigned int interruptDelay) |
| 380 |
{ |
| 381 |
int result = 0; |
| 382 |
char* data = 0; |
| 383 |
int length = 0; |
| 384 |
double userTimeout = 0; |
| 385 |
double* pUserTimeout = 0; |
| 386 |
kwsysProcess_SetCommand(kp, cmd); |
| 387 |
if (timeout >= 0) { |
| 388 |
kwsysProcess_SetTimeout(kp, timeout); |
| 389 |
} |
| 390 |
if (share) { |
| 391 |
kwsysProcess_SetPipeShared(kp, kwsysProcess_Pipe_STDOUT, 1); |
| 392 |
kwsysProcess_SetPipeShared(kp, kwsysProcess_Pipe_STDERR, 1); |
| 393 |
} |
| 394 |
if (disown) { |
| 395 |
kwsysProcess_SetOption(kp, kwsysProcess_Option_Detach, 1); |
| 396 |
} |
| 397 |
if (createNewGroup) { |
| 398 |
kwsysProcess_SetOption(kp, kwsysProcess_Option_CreateProcessGroup, 1); |
| 399 |
} |
| 400 |
kwsysProcess_Execute(kp); |
| 401 |
|
| 402 |
if (poll) { |
| 403 |
pUserTimeout = &userTimeout; |
| 404 |
} |
| 405 |
|
| 406 |
if (interruptDelay) { |
| 407 |
testProcess_sleep(interruptDelay); |
| 408 |
kwsysProcess_Interrupt(kp); |
| 409 |
} |
| 410 |
|
| 411 |
if (!share && !disown) { |
| 412 |
int p; |
| 413 |
while ((p = kwsysProcess_WaitForData(kp, &data, &length, pUserTimeout))) { |
| 414 |
if (output) { |
| 415 |
if (poll && p == kwsysProcess_Pipe_Timeout) { |
| 416 |
fprintf(stdout, "WaitForData timeout reached.\n"); |
| 417 |
fflush(stdout); |
| 418 |
|
| 419 |
/* Count the number of times we polled without getting data. |
| 420 |
If it is excessive then kill the child and fail. */ |
| 421 |
if (++poll >= MAXPOLL) { |
| 422 |
fprintf(stdout, "Poll count reached limit %d.\n", MAXPOLL); |
| 423 |
kwsysProcess_Kill(kp); |
| 424 |
} |
| 425 |
} else { |
| 426 |
fwrite(data, 1, (size_t)length, stdout); |
| 427 |
fflush(stdout); |
| 428 |
} |
| 429 |
} |
| 430 |
if (poll) { |
| 431 |
/* Delay to avoid busy loop during polling. */ |
| 432 |
testProcess_usleep(100000); |
| 433 |
} |
| 434 |
if (delay) { |
| 435 |
/* Purposely sleeping only on Win32 to let pipe fill up. */ |
| 436 |
#if defined(_WIN32) |
| 437 |
testProcess_usleep(100000); |
| 438 |
#endif |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
if (disown) { |
| 444 |
kwsysProcess_Disown(kp); |
| 445 |
} else { |
| 446 |
kwsysProcess_WaitForExit(kp, 0); |
| 447 |
} |
| 448 |
|
| 449 |
switch (kwsysProcess_GetState(kp)) { |
| 450 |
case kwsysProcess_State_Starting: |
| 451 |
printf("No process has been executed.\n"); |
| 452 |
break; |
| 453 |
case kwsysProcess_State_Executing: |
| 454 |
printf("The process is still executing.\n"); |
| 455 |
break; |
| 456 |
case kwsysProcess_State_Expired: |
| 457 |
printf("Subprocess was killed when timeout expired.\n"); |
| 458 |
break; |
| 459 |
case kwsysProcess_State_Exited: |
| 460 |
printf("Subprocess exited with value = %d\n", |
| 461 |
kwsysProcess_GetExitValue(kp)); |
| 462 |
result = ((exception != kwsysProcess_GetExitException(kp)) || |
| 463 |
(value != kwsysProcess_GetExitValue(kp))); |
| 464 |
break; |
| 465 |
case kwsysProcess_State_Killed: |
| 466 |
printf("Subprocess was killed by parent.\n"); |
| 467 |
break; |
| 468 |
case kwsysProcess_State_Exception: |
| 469 |
printf("Subprocess terminated abnormally: %s\n", |
| 470 |
kwsysProcess_GetExceptionString(kp)); |
| 471 |
result = ((exception != kwsysProcess_GetExitException(kp)) || |
| 472 |
(value != kwsysProcess_GetExitValue(kp))); |
| 473 |
break; |
| 474 |
case kwsysProcess_State_Disowned: |
| 475 |
printf("Subprocess was disowned.\n"); |
| 476 |
break; |
| 477 |
case kwsysProcess_State_Error: |
| 478 |
printf("Error in administrating child process: [%s]\n", |
| 479 |
kwsysProcess_GetErrorString(kp)); |
| 480 |
break; |
| 481 |
} |
| 482 |
|
| 483 |
if (result) { |
| 484 |
if (exception != kwsysProcess_GetExitException(kp)) { |
| 485 |
fprintf(stderr, |
| 486 |
"Mismatch in exit exception. " |
| 487 |
"Should have been %d, was %d.\n", |
| 488 |
exception, kwsysProcess_GetExitException(kp)); |
| 489 |
} |
| 490 |
if (value != kwsysProcess_GetExitValue(kp)) { |
| 491 |
fprintf(stderr, |
| 492 |
"Mismatch in exit value. " |
| 493 |
"Should have been %d, was %d.\n", |
| 494 |
value, kwsysProcess_GetExitValue(kp)); |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
if (kwsysProcess_GetState(kp) != state) { |
| 499 |
fprintf(stderr, |
| 500 |
"Mismatch in state. " |
| 501 |
"Should have been %d, was %d.\n", |
| 502 |
state, kwsysProcess_GetState(kp)); |
| 503 |
result = 1; |
| 504 |
} |
| 505 |
|
| 506 |
/* We should have polled more times than there were data if polling |
| 507 |
was enabled. */ |
| 508 |
if (poll && poll < MINPOLL) { |
| 509 |
fprintf(stderr, "Poll count is %d, which is less than %d.\n", poll, |
| 510 |
MINPOLL); |
| 511 |
result = 1; |
| 512 |
} |
| 513 |
|
| 514 |
return result; |
| 515 |
} |
| 516 |
|
| 517 |
/** |
| 518 |
* Runs a child process and blocks until it returns. Arguments as follows: |
| 519 |
* |
| 520 |
* cmd = Command line to run. |
| 521 |
* state = Expected return value of kwsysProcess_GetState after exit. |
| 522 |
* exception = Expected return value of kwsysProcess_GetExitException. |
| 523 |
* value = Expected return value of kwsysProcess_GetExitValue. |
| 524 |
* share = Whether to share stdout/stderr child pipes with our pipes |
| 525 |
* by way of kwsysProcess_SetPipeShared. If false, new pipes |
| 526 |
* are created. |
| 527 |
* output = If !share && !disown, whether to write the child's stdout |
| 528 |
* and stderr output to our stdout. |
| 529 |
* delay = If !share && !disown, adds an additional short delay to |
| 530 |
* the pipe loop to allow the pipes to fill up; Windows only. |
| 531 |
* timeout = Non-zero to sets a timeout in seconds via |
| 532 |
* kwsysProcess_SetTimeout. |
| 533 |
* poll = If !share && !disown, we count the number of 0.1 second |
| 534 |
* intervals where the child pipes had no new data. We fail |
| 535 |
* if not in the bounds of MINPOLL/MAXPOLL. |
| 536 |
* repeat = Number of times to run the process. |
| 537 |
* disown = If set, the process is disowned. |
| 538 |
* createNewGroup = If set, the process is created in a new process group. |
| 539 |
* interruptDelay = If non-zero, number of seconds to delay before |
| 540 |
* interrupting the process. Note that this delay will occur |
| 541 |
* BEFORE any reading/polling of pipes occurs and before any |
| 542 |
* detachment occurs. |
| 543 |
*/ |
| 544 |
int runChild(const char* cmd[], int state, int exception, int value, int share, |
| 545 |
int output, int delay, double timeout, int poll, int repeat, |
| 546 |
int disown, int createNewGroup, unsigned int interruptDelay) |
| 547 |
{ |
| 548 |
int result = 1; |
| 549 |
kwsysProcess* kp = kwsysProcess_New(); |
| 550 |
if (!kp) { |
| 551 |
fprintf(stderr, "kwsysProcess_New returned NULL!\n"); |
| 552 |
return 1; |
| 553 |
} |
| 554 |
while (repeat-- > 0) { |
| 555 |
result = runChild2(kp, cmd, state, exception, value, share, output, delay, |
| 556 |
timeout, poll, disown, createNewGroup, interruptDelay); |
| 557 |
if (result) { |
| 558 |
break; |
| 559 |
} |
| 560 |
} |
| 561 |
kwsysProcess_Delete(kp); |
| 562 |
return result; |
| 563 |
} |
| 564 |
|
| 565 |
int main(int argc, const char* argv[]) |
| 566 |
{ |
| 567 |
int n = 0; |
| 568 |
|
| 569 |
#ifdef _WIN32 |
| 570 |
int i; |
| 571 |
char new_args[10][_MAX_PATH]; |
| 572 |
LPWSTR* w_av = CommandLineToArgvW(GetCommandLineW(), &argc); |
| 573 |
for (i = 0; i < argc; i++) { |
| 574 |
kwsysEncoding_wcstombs(new_args[i], w_av[i], _MAX_PATH); |
| 575 |
argv[i] = new_args[i]; |
| 576 |
} |
| 577 |
LocalFree(w_av); |
| 578 |
#endif |
| 579 |
|
| 580 |
#if 0 |
| 581 |
{ |
| 582 |
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); |
| 583 |
DuplicateHandle(GetCurrentProcess(), out, |
| 584 |
GetCurrentProcess(), &out, 0, FALSE, |
| 585 |
DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE); |
| 586 |
SetStdHandle(STD_OUTPUT_HANDLE, out); |
| 587 |
} |
| 588 |
{ |
| 589 |
HANDLE out = GetStdHandle(STD_ERROR_HANDLE); |
| 590 |
DuplicateHandle(GetCurrentProcess(), out, |
| 591 |
GetCurrentProcess(), &out, 0, FALSE, |
| 592 |
DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE); |
| 593 |
SetStdHandle(STD_ERROR_HANDLE, out); |
| 594 |
} |
| 595 |
#endif |
| 596 |
if (argc == 2) { |
| 597 |
n = atoi(argv[1]); |
| 598 |
} else if (argc == 3 && strcmp(argv[1], "run") == 0) { |
| 599 |
n = atoi(argv[2]); |
| 600 |
} |
| 601 |
/* Check arguments. */ |
| 602 |
if (((n >= 1 && n <= 10) || n == 108 || n == 109 || n == 110) && argc == 3) { |
| 603 |
/* This is the child process for a requested test number. */ |
| 604 |
switch (n) { |
| 605 |
case 1: |
| 606 |
return test1(argc, argv); |
| 607 |
case 2: |
| 608 |
return test2(argc, argv); |
| 609 |
case 3: |
| 610 |
return test3(argc, argv); |
| 611 |
case 4: |
| 612 |
return test4(argc, argv); |
| 613 |
case 5: |
| 614 |
return test5(argc, argv); |
| 615 |
case 6: |
| 616 |
test6(argc, argv); |
| 617 |
return 0; |
| 618 |
case 7: |
| 619 |
return test7(argc, argv); |
| 620 |
case 8: |
| 621 |
return test8(argc, argv); |
| 622 |
case 9: |
| 623 |
return test9(argc, argv); |
| 624 |
case 10: |
| 625 |
return test10(argc, argv); |
| 626 |
case 108: |
| 627 |
return test8_grandchild(argc, argv); |
| 628 |
case 109: |
| 629 |
return test9_grandchild(argc, argv); |
| 630 |
case 110: |
| 631 |
return test10_grandchild(argc, argv); |
| 632 |
} |
| 633 |
fprintf(stderr, "Invalid test number %d.\n", n); |
| 634 |
return 1; |
| 635 |
} |
| 636 |
if (n >= 1 && n <= 10) { |
| 637 |
/* This is the parent process for a requested test number. */ |
| 638 |
int states[10] = { |
| 639 |
kwsysProcess_State_Exited, kwsysProcess_State_Exited, |
| 640 |
kwsysProcess_State_Expired, kwsysProcess_State_Exception, |
| 641 |
kwsysProcess_State_Exited, kwsysProcess_State_Expired, |
| 642 |
kwsysProcess_State_Exited, kwsysProcess_State_Exited, |
| 643 |
kwsysProcess_State_Expired, /* Ctrl+C handler test */ |
| 644 |
kwsysProcess_State_Exception /* Process group test */ |
| 645 |
}; |
| 646 |
int exceptions[10] = { |
| 647 |
kwsysProcess_Exception_None, kwsysProcess_Exception_None, |
| 648 |
kwsysProcess_Exception_None, |
| 649 |
#ifdef CRASH_USING_ABORT |
| 650 |
kwsysProcess_Exception_Other, |
| 651 |
#else |
| 652 |
kwsysProcess_Exception_Fault, |
| 653 |
#endif |
| 654 |
kwsysProcess_Exception_None, kwsysProcess_Exception_None, |
| 655 |
kwsysProcess_Exception_None, kwsysProcess_Exception_None, |
| 656 |
kwsysProcess_Exception_None, kwsysProcess_Exception_Interrupt |
| 657 |
}; |
| 658 |
int values[10] = { 0, 123, 1, 1, 0, 0, 0, 0, 1, 1 }; |
| 659 |
int shares[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 }; |
| 660 |
int outputs[10] = { 1, 1, 1, 1, 1, 0, 1, 1, 1, 1 }; |
| 661 |
int delays[10] = { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 }; |
| 662 |
double timeouts[10] = { 10, 10, 10, 30, 30, 10, -1, 10, 6, 4 }; |
| 663 |
int polls[10] = { 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 }; |
| 664 |
int repeat[10] = { 257, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; |
| 665 |
int createNewGroups[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 }; |
| 666 |
unsigned int interruptDelays[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 3, 2 }; |
| 667 |
int r; |
| 668 |
const char* cmd[4]; |
| 669 |
#ifdef _WIN32 |
| 670 |
char* argv0 = 0; |
| 671 |
#endif |
| 672 |
char* test1IterationsStr = getenv("KWSYS_TEST_PROCESS_1_COUNT"); |
| 673 |
if (test1IterationsStr) { |
| 674 |
long int test1Iterations = strtol(test1IterationsStr, 0, 10); |
| 675 |
if (test1Iterations > 10 && test1Iterations != LONG_MAX) { |
| 676 |
repeat[0] = (int)test1Iterations; |
| 677 |
} |
| 678 |
} |
| 679 |
#ifdef _WIN32 |
| 680 |
if (n == 0 && (argv0 = strdup(argv[0]))) { |
| 681 |
/* Try converting to forward slashes to see if it works. */ |
| 682 |
char* c; |
| 683 |
for (c = argv0; *c; ++c) { |
| 684 |
if (*c == '\\') { |
| 685 |
*c = '/'; |
| 686 |
} |
| 687 |
} |
| 688 |
cmd[0] = argv0; |
| 689 |
} else { |
| 690 |
cmd[0] = argv[0]; |
| 691 |
} |
| 692 |
#else |
| 693 |
cmd[0] = argv[0]; |
| 694 |
#endif |
| 695 |
cmd[1] = "run"; |
| 696 |
cmd[2] = argv[1]; |
| 697 |
cmd[3] = 0; |
| 698 |
fprintf(stdout, "Output on stdout before test %d.\n", n); |
| 699 |
fprintf(stderr, "Output on stderr before test %d.\n", n); |
| 700 |
fflush(stdout); |
| 701 |
fflush(stderr); |
| 702 |
r = runChild(cmd, states[n - 1], exceptions[n - 1], values[n - 1], |
| 703 |
shares[n - 1], outputs[n - 1], delays[n - 1], timeouts[n - 1], |
| 704 |
polls[n - 1], repeat[n - 1], 0, createNewGroups[n - 1], |
| 705 |
interruptDelays[n - 1]); |
| 706 |
fprintf(stdout, "Output on stdout after test %d.\n", n); |
| 707 |
fprintf(stderr, "Output on stderr after test %d.\n", n); |
| 708 |
fflush(stdout); |
| 709 |
fflush(stderr); |
| 710 |
#if defined(_WIN32) |
| 711 |
free(argv0); |
| 712 |
#endif |
| 713 |
return r; |
| 714 |
} |
| 715 |
if (argc > 2 && strcmp(argv[1], "0") == 0) { |
| 716 |
/* This is the special debugging test to run a given command |
| 717 |
line. */ |
| 718 |
const char** cmd = argv + 2; |
| 719 |
int state = kwsysProcess_State_Exited; |
| 720 |
int exception = kwsysProcess_Exception_None; |
| 721 |
int value = 0; |
| 722 |
double timeout = 0; |
| 723 |
int r = |
| 724 |
runChild(cmd, state, exception, value, 0, 1, 0, timeout, 0, 1, 0, 0, 0); |
| 725 |
return r; |
| 726 |
} |
| 727 |
/* Improper usage. */ |
| 728 |
fprintf(stdout, "Usage: %s <test number>\n", argv[0]); |
| 729 |
return 1; |
| 730 |
} |