testProcess.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 <kwsys/Process.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #if defined(_WIN32)
  14. # include <windows.h>
  15. #else
  16. # include <unistd.h>
  17. #endif
  18. int runChild(const char* cmd[], int state, int exception, int value,
  19. int share, int delay, double timeout);
  20. int test1(int argc, const char* argv[])
  21. {
  22. (void)argc; (void)argv;
  23. fprintf(stdout, "Output on stdout from test returning 0.\n");
  24. fprintf(stderr, "Output on stderr from test returning 0.\n");
  25. return 0;
  26. }
  27. int test2(int argc, const char* argv[])
  28. {
  29. (void)argc; (void)argv;
  30. fprintf(stdout, "Output on stdout from test returning 123.\n");
  31. fprintf(stderr, "Output on stderr from test returning 123.\n");
  32. return 123;
  33. }
  34. int test3(int argc, const char* argv[])
  35. {
  36. (void)argc; (void)argv;
  37. fprintf(stdout, "Output before sleep on stdout from timeout test.\n");
  38. fprintf(stderr, "Output before sleep on stderr from timeout test.\n");
  39. fflush(stdout);
  40. fflush(stderr);
  41. #if defined(_WIN32)
  42. Sleep(5000);
  43. #else
  44. sleep(5);
  45. #endif
  46. fprintf(stdout, "Output after sleep on stdout from timeout test.\n");
  47. fprintf(stderr, "Output after sleep on stderr from timeout test.\n");
  48. return 0;
  49. }
  50. int test4(int argc, const char* argv[])
  51. {
  52. (void)argc; (void)argv;
  53. fprintf(stdout, "Output before crash on stdout from crash test.\n");
  54. fprintf(stderr, "Output before crash on stderr from crash test.\n");
  55. fflush(stdout);
  56. fflush(stderr);
  57. *(int*)0 = 0;
  58. fprintf(stdout, "Output after crash on stdout from crash test.\n");
  59. fprintf(stderr, "Output after crash on stderr from crash test.\n");
  60. return 0;
  61. }
  62. int test5(int argc, const char* argv[])
  63. {
  64. int r;
  65. const char* cmd[4];
  66. (void)argc;
  67. cmd[0] = argv[0];
  68. cmd[1] = "run";
  69. cmd[2] = "4";
  70. cmd[3] = 0;
  71. fprintf(stdout, "Output on stdout before recursive test.\n");
  72. fprintf(stderr, "Output on stderr before recursive test.\n");
  73. fflush(stdout);
  74. fflush(stderr);
  75. r = runChild(cmd, kwsysProcess_State_Exception,
  76. kwsysProcess_Exception_Fault, 1, 1, 0, 2);
  77. fprintf(stdout, "Output on stdout after recursive test.\n");
  78. fprintf(stderr, "Output on stderr after recursive test.\n");
  79. fflush(stdout);
  80. fflush(stderr);
  81. return r;
  82. }
  83. #define TEST6_SIZE (4096*2)
  84. int test6(int argc, const char* argv[])
  85. {
  86. int i;
  87. char runaway[TEST6_SIZE+2];
  88. (void)argc; (void)argv;
  89. for(i=0;i < TEST6_SIZE;++i)
  90. {
  91. runaway[i] = '.';
  92. }
  93. runaway[TEST6_SIZE] = '\n';
  94. runaway[TEST6_SIZE] = 0;
  95. /* Generate huge amounts of output to test killing. */
  96. for(;;)
  97. {
  98. fwrite(runaway, 1, TEST6_SIZE+2, stdout);
  99. fflush(stdout);
  100. }
  101. return 0;
  102. }
  103. int runChild(const char* cmd[], int state, int exception, int value,
  104. int share, int delay, double timeout)
  105. {
  106. int result = 0;
  107. char* data = 0;
  108. int length = 0;
  109. kwsysProcess* kp = kwsysProcess_New();
  110. if(!kp)
  111. {
  112. fprintf(stderr, "kwsysProcess_New returned NULL!\n");
  113. return 1;
  114. }
  115. kwsysProcess_SetCommand(kp, cmd);
  116. kwsysProcess_SetTimeout(kp, timeout);
  117. if(share)
  118. {
  119. kwsysProcess_SetPipeShared(kp, kwsysProcess_Pipe_STDOUT, 1);
  120. kwsysProcess_SetPipeShared(kp, kwsysProcess_Pipe_STDERR, 1);
  121. }
  122. kwsysProcess_Execute(kp);
  123. if(!share)
  124. {
  125. while(kwsysProcess_WaitForData(kp, &data, &length, 0))
  126. {
  127. fwrite(data, 1, length, stdout);
  128. fflush(stdout);
  129. if(delay)
  130. {
  131. #if defined(_WIN32)
  132. Sleep(100);
  133. #endif
  134. }
  135. }
  136. }
  137. kwsysProcess_WaitForExit(kp, 0);
  138. switch (kwsysProcess_GetState(kp))
  139. {
  140. case kwsysProcess_State_Starting:
  141. printf("No process has been executed.\n"); break;
  142. case kwsysProcess_State_Executing:
  143. printf("The process is still executing.\n"); break;
  144. case kwsysProcess_State_Expired:
  145. printf("Child was killed when timeout expired.\n"); break;
  146. case kwsysProcess_State_Exited:
  147. printf("Child exited with value = %d\n",
  148. kwsysProcess_GetExitValue(kp));
  149. result = ((exception != kwsysProcess_GetExitException(kp)) ||
  150. (value != kwsysProcess_GetExitValue(kp))); break;
  151. case kwsysProcess_State_Killed:
  152. printf("Child was killed by parent.\n"); break;
  153. case kwsysProcess_State_Exception:
  154. printf("Child terminated abnormally: %s\n",
  155. kwsysProcess_GetExceptionString(kp));
  156. result = ((exception != kwsysProcess_GetExitException(kp)) ||
  157. (value != kwsysProcess_GetExitValue(kp))); break;
  158. case kwsysProcess_State_Error:
  159. printf("Error in administrating child process: [%s]\n",
  160. kwsysProcess_GetErrorString(kp)); break;
  161. };
  162. if(result)
  163. {
  164. if(exception != kwsysProcess_GetExitException(kp))
  165. {
  166. fprintf(stderr, "Mismatch in exit exception. Should have been %d.\n",
  167. exception);
  168. }
  169. if(value != kwsysProcess_GetExitValue(kp))
  170. {
  171. fprintf(stderr, "Mismatch in exit value. Should have been %d.\n",
  172. value);
  173. }
  174. }
  175. if(kwsysProcess_GetState(kp) != state)
  176. {
  177. fprintf(stderr, "Mismatch in state. Should have been %d.\n", state);
  178. result = 1;
  179. }
  180. kwsysProcess_Delete(kp);
  181. return result;
  182. }
  183. int main(int argc, const char* argv[])
  184. {
  185. int n = 0;
  186. #if 0
  187. {
  188. HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
  189. DuplicateHandle(GetCurrentProcess(), out,
  190. GetCurrentProcess(), &out, 0, FALSE,
  191. DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
  192. SetStdHandle(STD_OUTPUT_HANDLE, out);
  193. }
  194. {
  195. HANDLE out = GetStdHandle(STD_ERROR_HANDLE);
  196. DuplicateHandle(GetCurrentProcess(), out,
  197. GetCurrentProcess(), &out, 0, FALSE,
  198. DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
  199. SetStdHandle(STD_ERROR_HANDLE, out);
  200. }
  201. #endif
  202. if(argc == 2)
  203. {
  204. n = atoi(argv[1]);
  205. }
  206. else if(argc == 3)
  207. {
  208. n = atoi(argv[2]);
  209. }
  210. /* Check arguments. */
  211. if(n < 1 || n > 6 || (argc == 3 && strcmp(argv[1], "run") != 0))
  212. {
  213. fprintf(stdout, "Usage: %s <test number>\n", argv[0]);
  214. return 1;
  215. }
  216. if(argc == 3)
  217. {
  218. switch (n)
  219. {
  220. case 1: return test1(argc, argv);
  221. case 2: return test2(argc, argv);
  222. case 3: return test3(argc, argv);
  223. case 4: return test4(argc, argv);
  224. case 5: return test5(argc, argv);
  225. case 6: return test6(argc, argv);
  226. }
  227. fprintf(stderr, "Invalid test number %d.\n", n);
  228. return 1;
  229. }
  230. if(n >= 0 && n <= 6)
  231. {
  232. int states[6] =
  233. {
  234. kwsysProcess_State_Exited,
  235. kwsysProcess_State_Exited,
  236. kwsysProcess_State_Expired,
  237. kwsysProcess_State_Exception,
  238. kwsysProcess_State_Exited,
  239. kwsysProcess_State_Expired
  240. };
  241. int exceptions[6] =
  242. {
  243. kwsysProcess_Exception_None,
  244. kwsysProcess_Exception_None,
  245. kwsysProcess_Exception_None,
  246. kwsysProcess_Exception_Fault,
  247. kwsysProcess_Exception_None,
  248. kwsysProcess_Exception_None
  249. };
  250. int values[6] = {0, 123, 1, 1, 0, 0};
  251. int delays[6] = {0, 0, 0, 0, 0, 1};
  252. double timeouts[6] = {3, 3, 3, 3, 3, 0.1};
  253. int r;
  254. const char* cmd[4];
  255. cmd[0] = argv[0];
  256. cmd[1] = "run";
  257. cmd[2] = argv[1];
  258. cmd[3] = 0;
  259. fprintf(stdout, "Output on stdout before test %d.\n", n);
  260. fprintf(stderr, "Output on stderr before test %d.\n", n);
  261. fflush(stdout);
  262. fflush(stderr);
  263. r = runChild(cmd, states[n-1], exceptions[n-1], values[n-1], 0,
  264. delays[n-1], timeouts[n-1]);
  265. fprintf(stdout, "Output on stdout after test %d.\n", n);
  266. fprintf(stderr, "Output on stderr after test %d.\n", n);
  267. fflush(stdout);
  268. fflush(stderr);
  269. return r;
  270. }
  271. else
  272. {
  273. fprintf(stderr, "Test number out of range\n");
  274. return 1;
  275. }
  276. }