testProcess.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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 <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #if defined(_WIN32)
  23. # include <windows.h>
  24. #else
  25. # include <unistd.h>
  26. #endif
  27. #if defined(__BORLANDC__)
  28. # pragma warn -8060 /* possibly incorrect assignment */
  29. #endif
  30. #if defined(__BEOS__) && !defined(__ZETA__)
  31. /* BeOS 5 doesn't have usleep(), but it has snooze(), which is identical. */
  32. # include <be/kernel/OS.h>
  33. static inline void testProcess_usleep(unsigned int msec)
  34. {
  35. snooze(msec);
  36. }
  37. #else
  38. # define testProcess_usleep usleep
  39. #endif
  40. int runChild(const char* cmd[], int state, int exception, int value,
  41. int share, int output, int delay, double timeout, int poll,
  42. int repeat, int disown);
  43. static int test1(int argc, const char* argv[])
  44. {
  45. (void)argc; (void)argv;
  46. fprintf(stdout, "Output on stdout from test returning 0.\n");
  47. fprintf(stderr, "Output on stderr from test returning 0.\n");
  48. return 0;
  49. }
  50. static int test2(int argc, const char* argv[])
  51. {
  52. (void)argc; (void)argv;
  53. fprintf(stdout, "Output on stdout from test returning 123.\n");
  54. fprintf(stderr, "Output on stderr from test returning 123.\n");
  55. return 123;
  56. }
  57. static int test3(int argc, const char* argv[])
  58. {
  59. (void)argc; (void)argv;
  60. fprintf(stdout, "Output before sleep on stdout from timeout test.\n");
  61. fprintf(stderr, "Output before sleep on stderr from timeout test.\n");
  62. fflush(stdout);
  63. fflush(stderr);
  64. #if defined(_WIN32)
  65. Sleep(15000);
  66. #else
  67. sleep(15);
  68. #endif
  69. fprintf(stdout, "Output after sleep on stdout from timeout test.\n");
  70. fprintf(stderr, "Output after sleep on stderr from timeout test.\n");
  71. return 0;
  72. }
  73. static int test4(int argc, const char* argv[])
  74. {
  75. /* Prepare a pointer to an invalid address. Don't use null, because
  76. dereferencing null is undefined behaviour and compilers are free to
  77. do whatever they want. ex: Clang will warn at compile time, or even
  78. optimize away the write. We hope to 'outsmart' them by using
  79. 'volatile' and a slightly larger address, based on a runtime value. */
  80. volatile int* invalidAddress = 0;
  81. invalidAddress += argc?1:2;
  82. #if defined(_WIN32)
  83. /* Avoid error diagnostic popups since we are crashing on purpose. */
  84. SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
  85. #elif defined(__BEOS__) || defined(__HAIKU__)
  86. /* Avoid error diagnostic popups since we are crashing on purpose. */
  87. disable_debugger(1);
  88. #endif
  89. (void)argc; (void)argv;
  90. fprintf(stdout, "Output before crash on stdout from crash test.\n");
  91. fprintf(stderr, "Output before crash on stderr from crash test.\n");
  92. fflush(stdout);
  93. fflush(stderr);
  94. /* Provoke deliberate crash by writing to the invalid address. */
  95. *invalidAddress = 0;
  96. fprintf(stdout, "Output after crash on stdout from crash test.\n");
  97. fprintf(stderr, "Output after crash on stderr from crash test.\n");
  98. return 0;
  99. }
  100. static int test5(int argc, const char* argv[])
  101. {
  102. int r;
  103. const char* cmd[4];
  104. (void)argc;
  105. cmd[0] = argv[0];
  106. cmd[1] = "run";
  107. cmd[2] = "4";
  108. cmd[3] = 0;
  109. fprintf(stdout, "Output on stdout before recursive test.\n");
  110. fprintf(stderr, "Output on stderr before recursive test.\n");
  111. fflush(stdout);
  112. fflush(stderr);
  113. r = runChild(cmd, kwsysProcess_State_Exception,
  114. kwsysProcess_Exception_Fault, 1, 1, 1, 0, 15, 0, 1, 0);
  115. fprintf(stdout, "Output on stdout after recursive test.\n");
  116. fprintf(stderr, "Output on stderr after recursive test.\n");
  117. fflush(stdout);
  118. fflush(stderr);
  119. return r;
  120. }
  121. #define TEST6_SIZE (4096*2)
  122. static void test6(int argc, const char* argv[])
  123. {
  124. int i;
  125. char runaway[TEST6_SIZE+1];
  126. (void)argc; (void)argv;
  127. for(i=0;i < TEST6_SIZE;++i)
  128. {
  129. runaway[i] = '.';
  130. }
  131. runaway[TEST6_SIZE] = '\n';
  132. /* Generate huge amounts of output to test killing. */
  133. for(;;)
  134. {
  135. fwrite(runaway, 1, TEST6_SIZE+1, stdout);
  136. fflush(stdout);
  137. }
  138. }
  139. /* Define MINPOLL to be one more than the number of times output is
  140. written. Define MAXPOLL to be the largest number of times a loop
  141. delaying 1/10th of a second should ever have to poll. */
  142. #define MINPOLL 5
  143. #define MAXPOLL 20
  144. static int test7(int argc, const char* argv[])
  145. {
  146. (void)argc; (void)argv;
  147. fprintf(stdout, "Output on stdout before sleep.\n");
  148. fprintf(stderr, "Output on stderr before sleep.\n");
  149. fflush(stdout);
  150. fflush(stderr);
  151. /* Sleep for 1 second. */
  152. #if defined(_WIN32)
  153. Sleep(1000);
  154. #else
  155. sleep(1);
  156. #endif
  157. fprintf(stdout, "Output on stdout after sleep.\n");
  158. fprintf(stderr, "Output on stderr after sleep.\n");
  159. fflush(stdout);
  160. fflush(stderr);
  161. return 0;
  162. }
  163. static int test8(int argc, const char* argv[])
  164. {
  165. /* Create a disowned grandchild to test handling of processes
  166. that exit before their children. */
  167. int r;
  168. const char* cmd[4];
  169. (void)argc;
  170. cmd[0] = argv[0];
  171. cmd[1] = "run";
  172. cmd[2] = "108";
  173. cmd[3] = 0;
  174. fprintf(stdout, "Output on stdout before grandchild test.\n");
  175. fprintf(stderr, "Output on stderr before grandchild test.\n");
  176. fflush(stdout);
  177. fflush(stderr);
  178. r = runChild(cmd, kwsysProcess_State_Disowned, kwsysProcess_Exception_None,
  179. 1, 1, 1, 0, 10, 0, 1, 1);
  180. fprintf(stdout, "Output on stdout after grandchild test.\n");
  181. fprintf(stderr, "Output on stderr after grandchild test.\n");
  182. fflush(stdout);
  183. fflush(stderr);
  184. return r;
  185. }
  186. static int test8_grandchild(int argc, const char* argv[])
  187. {
  188. (void)argc; (void)argv;
  189. fprintf(stdout, "Output on stdout from grandchild before sleep.\n");
  190. fprintf(stderr, "Output on stderr from grandchild before sleep.\n");
  191. fflush(stdout);
  192. fflush(stderr);
  193. /* TODO: Instead of closing pipes here leave them open to make sure
  194. the grandparent can stop listening when the parent exits. This
  195. part of the test cannot be enabled until the feature is
  196. implemented. */
  197. fclose(stdout);
  198. fclose(stderr);
  199. #if defined(_WIN32)
  200. Sleep(15000);
  201. #else
  202. sleep(15);
  203. #endif
  204. return 0;
  205. }
  206. static int runChild2(kwsysProcess* kp,
  207. const char* cmd[], int state, int exception, int value,
  208. int share, int output, int delay, double timeout,
  209. int poll, int disown)
  210. {
  211. int result = 0;
  212. char* data = 0;
  213. int length = 0;
  214. double userTimeout = 0;
  215. double* pUserTimeout = 0;
  216. kwsysProcess_SetCommand(kp, cmd);
  217. if(timeout >= 0)
  218. {
  219. kwsysProcess_SetTimeout(kp, timeout);
  220. }
  221. if(share)
  222. {
  223. kwsysProcess_SetPipeShared(kp, kwsysProcess_Pipe_STDOUT, 1);
  224. kwsysProcess_SetPipeShared(kp, kwsysProcess_Pipe_STDERR, 1);
  225. }
  226. if(disown)
  227. {
  228. kwsysProcess_SetOption(kp, kwsysProcess_Option_Detach, 1);
  229. }
  230. kwsysProcess_Execute(kp);
  231. if(poll)
  232. {
  233. pUserTimeout = &userTimeout;
  234. }
  235. if(!share && !disown)
  236. {
  237. int p;
  238. while((p = kwsysProcess_WaitForData(kp, &data, &length, pUserTimeout)))
  239. {
  240. if(output)
  241. {
  242. if(poll && p == kwsysProcess_Pipe_Timeout)
  243. {
  244. fprintf(stdout, "WaitForData timeout reached.\n");
  245. fflush(stdout);
  246. /* Count the number of times we polled without getting data.
  247. If it is excessive then kill the child and fail. */
  248. if(++poll >= MAXPOLL)
  249. {
  250. fprintf(stdout, "Poll count reached limit %d.\n",
  251. MAXPOLL);
  252. kwsysProcess_Kill(kp);
  253. }
  254. }
  255. else
  256. {
  257. fwrite(data, 1, (size_t) length, stdout);
  258. fflush(stdout);
  259. }
  260. }
  261. if(poll)
  262. {
  263. /* Delay to avoid busy loop during polling. */
  264. #if defined(_WIN32)
  265. Sleep(100);
  266. #else
  267. testProcess_usleep(100000);
  268. #endif
  269. }
  270. if(delay)
  271. {
  272. /* Purposely sleeping only on Win32 to let pipe fill up. */
  273. #if defined(_WIN32)
  274. Sleep(100);
  275. #endif
  276. }
  277. }
  278. }
  279. if(disown)
  280. {
  281. kwsysProcess_Disown(kp);
  282. }
  283. else
  284. {
  285. kwsysProcess_WaitForExit(kp, 0);
  286. }
  287. switch (kwsysProcess_GetState(kp))
  288. {
  289. case kwsysProcess_State_Starting:
  290. printf("No process has been executed.\n"); break;
  291. case kwsysProcess_State_Executing:
  292. printf("The process is still executing.\n"); break;
  293. case kwsysProcess_State_Expired:
  294. printf("Child was killed when timeout expired.\n"); break;
  295. case kwsysProcess_State_Exited:
  296. printf("Child exited with value = %d\n",
  297. kwsysProcess_GetExitValue(kp));
  298. result = ((exception != kwsysProcess_GetExitException(kp)) ||
  299. (value != kwsysProcess_GetExitValue(kp))); break;
  300. case kwsysProcess_State_Killed:
  301. printf("Child was killed by parent.\n"); break;
  302. case kwsysProcess_State_Exception:
  303. printf("Child terminated abnormally: %s\n",
  304. kwsysProcess_GetExceptionString(kp));
  305. result = ((exception != kwsysProcess_GetExitException(kp)) ||
  306. (value != kwsysProcess_GetExitValue(kp))); break;
  307. case kwsysProcess_State_Disowned:
  308. printf("Child was disowned.\n"); break;
  309. case kwsysProcess_State_Error:
  310. printf("Error in administrating child process: [%s]\n",
  311. kwsysProcess_GetErrorString(kp)); break;
  312. };
  313. if(result)
  314. {
  315. if(exception != kwsysProcess_GetExitException(kp))
  316. {
  317. fprintf(stderr, "Mismatch in exit exception. "
  318. "Should have been %d, was %d.\n",
  319. exception, kwsysProcess_GetExitException(kp));
  320. }
  321. if(value != kwsysProcess_GetExitValue(kp))
  322. {
  323. fprintf(stderr, "Mismatch in exit value. "
  324. "Should have been %d, was %d.\n",
  325. value, kwsysProcess_GetExitValue(kp));
  326. }
  327. }
  328. if(kwsysProcess_GetState(kp) != state)
  329. {
  330. fprintf(stderr, "Mismatch in state. "
  331. "Should have been %d, was %d.\n",
  332. state, kwsysProcess_GetState(kp));
  333. result = 1;
  334. }
  335. /* We should have polled more times than there were data if polling
  336. was enabled. */
  337. if(poll && poll < MINPOLL)
  338. {
  339. fprintf(stderr, "Poll count is %d, which is less than %d.\n",
  340. poll, MINPOLL);
  341. result = 1;
  342. }
  343. return result;
  344. }
  345. int runChild(const char* cmd[], int state, int exception, int value,
  346. int share, int output, int delay, double timeout,
  347. int poll, int repeat, int disown)
  348. {
  349. int result = 1;
  350. kwsysProcess* kp = kwsysProcess_New();
  351. if(!kp)
  352. {
  353. fprintf(stderr, "kwsysProcess_New returned NULL!\n");
  354. return 1;
  355. }
  356. while(repeat-- > 0)
  357. {
  358. result = runChild2(kp, cmd, state, exception, value, share,
  359. output, delay, timeout, poll, disown);
  360. }
  361. kwsysProcess_Delete(kp);
  362. return result;
  363. }
  364. int main(int argc, const char* argv[])
  365. {
  366. int n = 0;
  367. #ifdef _WIN32
  368. int i;
  369. char new_args[10][_MAX_PATH];
  370. LPWSTR* w_av = CommandLineToArgvW(GetCommandLineW(), &argc);
  371. for(i=0; i<argc; i++)
  372. {
  373. kwsysEncoding_wcstombs(new_args[i], w_av[i], _MAX_PATH);
  374. argv[i] = new_args[i];
  375. }
  376. LocalFree(w_av);
  377. #endif
  378. #if 0
  379. {
  380. HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
  381. DuplicateHandle(GetCurrentProcess(), out,
  382. GetCurrentProcess(), &out, 0, FALSE,
  383. DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
  384. SetStdHandle(STD_OUTPUT_HANDLE, out);
  385. }
  386. {
  387. HANDLE out = GetStdHandle(STD_ERROR_HANDLE);
  388. DuplicateHandle(GetCurrentProcess(), out,
  389. GetCurrentProcess(), &out, 0, FALSE,
  390. DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
  391. SetStdHandle(STD_ERROR_HANDLE, out);
  392. }
  393. #endif
  394. if(argc == 2)
  395. {
  396. n = atoi(argv[1]);
  397. }
  398. else if(argc == 3 && strcmp(argv[1], "run") == 0)
  399. {
  400. n = atoi(argv[2]);
  401. }
  402. /* Check arguments. */
  403. if(((n >= 1 && n <= 8) || n == 108) && argc == 3)
  404. {
  405. /* This is the child process for a requested test number. */
  406. switch (n)
  407. {
  408. case 1: return test1(argc, argv);
  409. case 2: return test2(argc, argv);
  410. case 3: return test3(argc, argv);
  411. case 4: return test4(argc, argv);
  412. case 5: return test5(argc, argv);
  413. case 6: test6(argc, argv); return 0;
  414. case 7: return test7(argc, argv);
  415. case 8: return test8(argc, argv);
  416. case 108: return test8_grandchild(argc, argv);
  417. }
  418. fprintf(stderr, "Invalid test number %d.\n", n);
  419. return 1;
  420. }
  421. else if(n >= 1 && n <= 8)
  422. {
  423. /* This is the parent process for a requested test number. */
  424. int states[8] =
  425. {
  426. kwsysProcess_State_Exited,
  427. kwsysProcess_State_Exited,
  428. kwsysProcess_State_Expired,
  429. kwsysProcess_State_Exception,
  430. kwsysProcess_State_Exited,
  431. kwsysProcess_State_Expired,
  432. kwsysProcess_State_Exited,
  433. kwsysProcess_State_Exited
  434. };
  435. int exceptions[8] =
  436. {
  437. kwsysProcess_Exception_None,
  438. kwsysProcess_Exception_None,
  439. kwsysProcess_Exception_None,
  440. kwsysProcess_Exception_Fault,
  441. kwsysProcess_Exception_None,
  442. kwsysProcess_Exception_None,
  443. kwsysProcess_Exception_None,
  444. kwsysProcess_Exception_None
  445. };
  446. int values[8] = {0, 123, 1, 1, 0, 0, 0, 0};
  447. int outputs[8] = {1, 1, 1, 1, 1, 0, 1, 1};
  448. int delays[8] = {0, 0, 0, 0, 0, 1, 0, 0};
  449. double timeouts[8] = {10, 10, 10, 30, 30, 10, -1, 10};
  450. int polls[8] = {0, 0, 0, 0, 0, 0, 1, 0};
  451. int repeat[8] = {2, 1, 1, 1, 1, 1, 1, 1};
  452. int r;
  453. const char* cmd[4];
  454. #ifdef _WIN32
  455. char* argv0 = 0;
  456. if(n == 0 && (argv0 = strdup(argv[0])))
  457. {
  458. /* Try converting to forward slashes to see if it works. */
  459. char* c;
  460. for(c=argv0; *c; ++c)
  461. {
  462. if(*c == '\\')
  463. {
  464. *c = '/';
  465. }
  466. }
  467. cmd[0] = argv0;
  468. }
  469. else
  470. {
  471. cmd[0] = argv[0];
  472. }
  473. #else
  474. cmd[0] = argv[0];
  475. #endif
  476. cmd[1] = "run";
  477. cmd[2] = argv[1];
  478. cmd[3] = 0;
  479. fprintf(stdout, "Output on stdout before test %d.\n", n);
  480. fprintf(stderr, "Output on stderr before test %d.\n", n);
  481. fflush(stdout);
  482. fflush(stderr);
  483. r = runChild(cmd, states[n-1], exceptions[n-1], values[n-1], 0,
  484. outputs[n-1], delays[n-1], timeouts[n-1],
  485. polls[n-1], repeat[n-1], 0);
  486. fprintf(stdout, "Output on stdout after test %d.\n", n);
  487. fprintf(stderr, "Output on stderr after test %d.\n", n);
  488. fflush(stdout);
  489. fflush(stderr);
  490. #if defined(_WIN32)
  491. if(argv0) { free(argv0); }
  492. #endif
  493. return r;
  494. }
  495. else if(argc > 2 && strcmp(argv[1], "0") == 0)
  496. {
  497. /* This is the special debugging test to run a given command
  498. line. */
  499. const char** cmd = argv+2;
  500. int state = kwsysProcess_State_Exited;
  501. int exception = kwsysProcess_Exception_None;
  502. int value = 0;
  503. double timeout = 0;
  504. int r = runChild(cmd, state, exception, value, 0, 1, 0, timeout, 0, 1, 0);
  505. return r;
  506. }
  507. else
  508. {
  509. /* Improper usage. */
  510. fprintf(stdout, "Usage: %s <test number>\n", argv[0]);
  511. return 1;
  512. }
  513. }