testProcess.c 23 KB

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