testProcess.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*=========================================================================
  2. Program: KWSys - Kitware System Library
  3. Module: $RCSfile$
  4. Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
  5. See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  8. PURPOSE. See the above copyright notices 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. int runChild(const char* cmd[], int state, int exception, int value,
  26. int share, int output, int delay, double timeout, int poll,
  27. int repeat);
  28. int test1(int argc, const char* argv[])
  29. {
  30. (void)argc; (void)argv;
  31. fprintf(stdout, "Output on stdout from test returning 0.\n");
  32. fprintf(stderr, "Output on stderr from test returning 0.\n");
  33. return 0;
  34. }
  35. int test2(int argc, const char* argv[])
  36. {
  37. (void)argc; (void)argv;
  38. fprintf(stdout, "Output on stdout from test returning 123.\n");
  39. fprintf(stderr, "Output on stderr from test returning 123.\n");
  40. return 123;
  41. }
  42. int test3(int argc, const char* argv[])
  43. {
  44. (void)argc; (void)argv;
  45. fprintf(stdout, "Output before sleep on stdout from timeout test.\n");
  46. fprintf(stderr, "Output before sleep on stderr from timeout test.\n");
  47. fflush(stdout);
  48. fflush(stderr);
  49. #if defined(_WIN32)
  50. Sleep(15000);
  51. #else
  52. sleep(15);
  53. #endif
  54. fprintf(stdout, "Output after sleep on stdout from timeout test.\n");
  55. fprintf(stderr, "Output after sleep on stderr from timeout test.\n");
  56. return 0;
  57. }
  58. int test4(int argc, const char* argv[])
  59. {
  60. #if defined(_WIN32)
  61. /* Avoid error diagnostic popups since we are crashing on purpose. */
  62. SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
  63. #endif
  64. (void)argc; (void)argv;
  65. fprintf(stdout, "Output before crash on stdout from crash test.\n");
  66. fprintf(stderr, "Output before crash on stderr from crash test.\n");
  67. fflush(stdout);
  68. fflush(stderr);
  69. *(int*)0 = 0;
  70. fprintf(stdout, "Output after crash on stdout from crash test.\n");
  71. fprintf(stderr, "Output after crash on stderr from crash test.\n");
  72. return 0;
  73. }
  74. int test5(int argc, const char* argv[])
  75. {
  76. int r;
  77. const char* cmd[4];
  78. (void)argc;
  79. cmd[0] = argv[0];
  80. cmd[1] = "run";
  81. cmd[2] = "4";
  82. cmd[3] = 0;
  83. fprintf(stdout, "Output on stdout before recursive test.\n");
  84. fprintf(stderr, "Output on stderr before recursive test.\n");
  85. fflush(stdout);
  86. fflush(stderr);
  87. r = runChild(cmd, kwsysProcess_State_Exception,
  88. kwsysProcess_Exception_Fault, 1, 1, 1, 0, 15, 0, 1);
  89. fprintf(stdout, "Output on stdout after recursive test.\n");
  90. fprintf(stderr, "Output on stderr after recursive test.\n");
  91. fflush(stdout);
  92. fflush(stderr);
  93. return r;
  94. }
  95. #define TEST6_SIZE (4096*2)
  96. void test6(int argc, const char* argv[])
  97. {
  98. int i;
  99. char runaway[TEST6_SIZE+1];
  100. (void)argc; (void)argv;
  101. for(i=0;i < TEST6_SIZE;++i)
  102. {
  103. runaway[i] = '.';
  104. }
  105. runaway[TEST6_SIZE] = '\n';
  106. /* Generate huge amounts of output to test killing. */
  107. for(;;)
  108. {
  109. fwrite(runaway, 1, TEST6_SIZE+1, stdout);
  110. fflush(stdout);
  111. }
  112. }
  113. /* Define MINPOLL to be one more than the number of times output is
  114. written. Define MAXPOLL to be the largest number of times a loop
  115. delaying 1/10th of a second should ever have to poll. */
  116. #define MINPOLL 5
  117. #define MAXPOLL 20
  118. int test7(int argc, const char* argv[])
  119. {
  120. (void)argc; (void)argv;
  121. fprintf(stdout, "Output on stdout before sleep.\n");
  122. fprintf(stderr, "Output on stderr before sleep.\n");
  123. fflush(stdout);
  124. fflush(stderr);
  125. /* Sleep for 1 second. */
  126. #if defined(_WIN32)
  127. Sleep(1000);
  128. #else
  129. sleep(1);
  130. #endif
  131. fprintf(stdout, "Output on stdout after sleep.\n");
  132. fprintf(stderr, "Output on stderr after sleep.\n");
  133. fflush(stdout);
  134. fflush(stderr);
  135. return 0;
  136. }
  137. int runChild2(kwsysProcess* kp,
  138. const char* cmd[], int state, int exception, int value,
  139. int share, int output, int delay, double timeout,
  140. int poll)
  141. {
  142. int result = 0;
  143. char* data = 0;
  144. int length = 0;
  145. double userTimeout = 0;
  146. double* pUserTimeout = 0;
  147. kwsysProcess_SetCommand(kp, cmd);
  148. if(timeout >= 0)
  149. {
  150. kwsysProcess_SetTimeout(kp, timeout);
  151. }
  152. if(share)
  153. {
  154. kwsysProcess_SetPipeShared(kp, kwsysProcess_Pipe_STDOUT, 1);
  155. kwsysProcess_SetPipeShared(kp, kwsysProcess_Pipe_STDERR, 1);
  156. }
  157. kwsysProcess_Execute(kp);
  158. if(poll)
  159. {
  160. pUserTimeout = &userTimeout;
  161. }
  162. if(!share)
  163. {
  164. int p;
  165. while((p = kwsysProcess_WaitForData(kp, &data, &length, pUserTimeout)))
  166. {
  167. if(output)
  168. {
  169. if(poll && p == kwsysProcess_Pipe_Timeout)
  170. {
  171. fprintf(stdout, "WaitForData timeout reached.\n");
  172. fflush(stdout);
  173. /* Count the number of times we polled without getting data.
  174. If it is excessive then kill the child and fail. */
  175. if(++poll >= MAXPOLL)
  176. {
  177. fprintf(stdout, "Poll count reached limit %d.\n",
  178. MAXPOLL);
  179. kwsysProcess_Kill(kp);
  180. }
  181. }
  182. else
  183. {
  184. fwrite(data, 1, length, stdout);
  185. fflush(stdout);
  186. }
  187. }
  188. if(poll)
  189. {
  190. /* Delay to avoid busy loop during polling. */
  191. #if defined(_WIN32)
  192. Sleep(100);
  193. #else
  194. usleep(100000);
  195. #endif
  196. }
  197. if(delay)
  198. {
  199. /* Purposely sleeping only on Win32 to let pipe fill up. */
  200. #if defined(_WIN32)
  201. Sleep(100);
  202. #endif
  203. }
  204. }
  205. }
  206. kwsysProcess_WaitForExit(kp, 0);
  207. switch (kwsysProcess_GetState(kp))
  208. {
  209. case kwsysProcess_State_Starting:
  210. printf("No process has been executed.\n"); break;
  211. case kwsysProcess_State_Executing:
  212. printf("The process is still executing.\n"); break;
  213. case kwsysProcess_State_Expired:
  214. printf("Child was killed when timeout expired.\n"); break;
  215. case kwsysProcess_State_Exited:
  216. printf("Child exited with value = %d\n",
  217. kwsysProcess_GetExitValue(kp));
  218. result = ((exception != kwsysProcess_GetExitException(kp)) ||
  219. (value != kwsysProcess_GetExitValue(kp))); break;
  220. case kwsysProcess_State_Killed:
  221. printf("Child was killed by parent.\n"); break;
  222. case kwsysProcess_State_Exception:
  223. printf("Child terminated abnormally: %s\n",
  224. kwsysProcess_GetExceptionString(kp));
  225. result = ((exception != kwsysProcess_GetExitException(kp)) ||
  226. (value != kwsysProcess_GetExitValue(kp))); break;
  227. case kwsysProcess_State_Error:
  228. printf("Error in administrating child process: [%s]\n",
  229. kwsysProcess_GetErrorString(kp)); break;
  230. };
  231. if(result)
  232. {
  233. if(exception != kwsysProcess_GetExitException(kp))
  234. {
  235. fprintf(stderr, "Mismatch in exit exception. "
  236. "Should have been %d, was %d.\n",
  237. exception, kwsysProcess_GetExitException(kp));
  238. }
  239. if(value != kwsysProcess_GetExitValue(kp))
  240. {
  241. fprintf(stderr, "Mismatch in exit value. "
  242. "Should have been %d, was %d.\n",
  243. value, kwsysProcess_GetExitValue(kp));
  244. }
  245. }
  246. if(kwsysProcess_GetState(kp) != state)
  247. {
  248. fprintf(stderr, "Mismatch in state. "
  249. "Should have been %d, was %d.\n",
  250. state, kwsysProcess_GetState(kp));
  251. result = 1;
  252. }
  253. /* We should have polled more times than there were data if polling
  254. was enabled. */
  255. if(poll && poll < MINPOLL)
  256. {
  257. fprintf(stderr, "Poll count is %d, which is less than %d.\n",
  258. poll, MINPOLL);
  259. result = 1;
  260. }
  261. return result;
  262. }
  263. int runChild(const char* cmd[], int state, int exception, int value,
  264. int share, int output, int delay, double timeout,
  265. int poll, int repeat)
  266. {
  267. int result = 1;
  268. kwsysProcess* kp = kwsysProcess_New();
  269. if(!kp)
  270. {
  271. fprintf(stderr, "kwsysProcess_New returned NULL!\n");
  272. return 1;
  273. }
  274. while(repeat-- > 0)
  275. {
  276. result = runChild2(kp, cmd, state, exception, value, share,
  277. output, delay, timeout, poll);
  278. }
  279. kwsysProcess_Delete(kp);
  280. return result;
  281. }
  282. int main(int argc, const char* argv[])
  283. {
  284. int n = 0;
  285. #if 0
  286. {
  287. HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
  288. DuplicateHandle(GetCurrentProcess(), out,
  289. GetCurrentProcess(), &out, 0, FALSE,
  290. DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
  291. SetStdHandle(STD_OUTPUT_HANDLE, out);
  292. }
  293. {
  294. HANDLE out = GetStdHandle(STD_ERROR_HANDLE);
  295. DuplicateHandle(GetCurrentProcess(), out,
  296. GetCurrentProcess(), &out, 0, FALSE,
  297. DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
  298. SetStdHandle(STD_ERROR_HANDLE, out);
  299. }
  300. #endif
  301. if(argc == 2)
  302. {
  303. n = atoi(argv[1]);
  304. }
  305. else if(argc == 3)
  306. {
  307. n = atoi(argv[2]);
  308. }
  309. /* Check arguments. */
  310. if(n < 1 || n > 7 || (argc == 3 && strcmp(argv[1], "run") != 0))
  311. {
  312. fprintf(stdout, "Usage: %s <test number>\n", argv[0]);
  313. return 1;
  314. }
  315. if(argc == 3)
  316. {
  317. switch (n)
  318. {
  319. case 1: return test1(argc, argv);
  320. case 2: return test2(argc, argv);
  321. case 3: return test3(argc, argv);
  322. case 4: return test4(argc, argv);
  323. case 5: return test5(argc, argv);
  324. case 6: test6(argc, argv); return 0;
  325. case 7: return test7(argc, argv);
  326. }
  327. fprintf(stderr, "Invalid test number %d.\n", n);
  328. return 1;
  329. }
  330. if(n >= 0 && n <= 7)
  331. {
  332. int states[7] =
  333. {
  334. kwsysProcess_State_Exited,
  335. kwsysProcess_State_Exited,
  336. kwsysProcess_State_Expired,
  337. kwsysProcess_State_Exception,
  338. kwsysProcess_State_Exited,
  339. kwsysProcess_State_Expired,
  340. kwsysProcess_State_Exited
  341. };
  342. int exceptions[7] =
  343. {
  344. kwsysProcess_Exception_None,
  345. kwsysProcess_Exception_None,
  346. kwsysProcess_Exception_None,
  347. kwsysProcess_Exception_Fault,
  348. kwsysProcess_Exception_None,
  349. kwsysProcess_Exception_None,
  350. kwsysProcess_Exception_None
  351. };
  352. int values[7] = {0, 123, 1, 1, 0, 0, 0};
  353. int outputs[7] = {1, 1, 1, 1, 1, 0, 1};
  354. int delays[7] = {0, 0, 0, 0, 0, 1, 0};
  355. double timeouts[7] = {10, 10, 10, 10, 30, 10, -1};
  356. int polls[7] = {0, 0, 0, 0, 0, 0, 1};
  357. int repeat[7] = {2, 1, 1, 1, 1, 1, 1};
  358. int r;
  359. const char* cmd[4];
  360. #ifdef _WIN32
  361. char* argv0 = 0;
  362. if(n == 0 && (argv0 = strdup(argv[0])))
  363. {
  364. /* Try converting to forward slashes to see if it works. */
  365. char* c;
  366. for(c=argv0; *c; ++c)
  367. {
  368. if(*c == '\\')
  369. {
  370. *c = '/';
  371. }
  372. }
  373. cmd[0] = argv0;
  374. }
  375. else
  376. {
  377. cmd[0] = argv[0];
  378. }
  379. #else
  380. cmd[0] = argv[0];
  381. #endif
  382. cmd[1] = "run";
  383. cmd[2] = argv[1];
  384. cmd[3] = 0;
  385. fprintf(stdout, "Output on stdout before test %d.\n", n);
  386. fprintf(stderr, "Output on stderr before test %d.\n", n);
  387. fflush(stdout);
  388. fflush(stderr);
  389. r = runChild(cmd, states[n-1], exceptions[n-1], values[n-1], 0,
  390. outputs[n-1], delays[n-1], timeouts[n-1],
  391. polls[n-1], repeat[n-1]);
  392. fprintf(stdout, "Output on stdout after test %d.\n", n);
  393. fprintf(stderr, "Output on stderr after test %d.\n", n);
  394. fflush(stdout);
  395. fflush(stderr);
  396. #if _WIN32
  397. if(argv0) { free(argv0); }
  398. #endif
  399. return r;
  400. }
  401. else
  402. {
  403. fprintf(stderr, "Test number out of range\n");
  404. return 1;
  405. }
  406. }