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