testProcess.c 15 KB

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