1
0

testProcess.c 23 KB

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