test-process-list.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * Copyright (c) 2015-2018 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36. #include <stdio.h>
  37. #include <assert.h>
  38. #include <string.h>
  39. #include <poll.h>
  40. #include <signal.h>
  41. #include <stdlib.h>
  42. #include <limits.h>
  43. #include <unistd.h>
  44. #include "process-list.h"
  45. /*
  46. * 1 min timeout
  47. */
  48. #define WAIT_FOR_NO_RUNNING_REPEATS 6000
  49. #define WAIT_FOR_NO_RUNNING_TIMEOUT 60000
  50. /*
  51. * 1 sec timeout
  52. */
  53. /*
  54. #define WAIT_FOR_NO_RUNNING_REPEATS 100
  55. #define WAIT_FOR_NO_RUNNING_TIMEOUT 1000
  56. */
  57. static int no_executed;
  58. static int no_finished;
  59. static void
  60. signal_handlers_register(void)
  61. {
  62. struct sigaction act;
  63. act.sa_handler = SIG_DFL;
  64. sigemptyset(&act.sa_mask);
  65. act.sa_flags = SA_RESTART;
  66. sigaction(SIGCHLD, &act, NULL);
  67. act.sa_handler = SIG_IGN;
  68. sigemptyset(&act.sa_mask);
  69. act.sa_flags = SA_RESTART;
  70. sigaction(SIGPIPE, &act, NULL);
  71. }
  72. static void
  73. plist_notify(enum process_list_notify_reason reason, const struct process_list_entry *entry,
  74. void *user_data)
  75. {
  76. assert(user_data == (void *)0x42);
  77. switch (reason) {
  78. case PROCESS_LIST_NOTIFY_REASON_EXECUTED:
  79. no_executed++;
  80. break;
  81. case PROCESS_LIST_NOTIFY_REASON_FINISHED:
  82. no_finished++;
  83. break;
  84. }
  85. }
  86. static char *
  87. find_exec_path(const char *exec)
  88. {
  89. struct stat stat_buf;
  90. char *res_path;
  91. int res;
  92. assert((res_path = malloc(PATH_MAX)) != NULL);
  93. memset(res_path, 0, PATH_MAX);
  94. res = snprintf(res_path, PATH_MAX, "/bin/%s", exec);
  95. assert(res > 0 && res < PATH_MAX);
  96. if (stat(res_path, &stat_buf) == 0 && (stat_buf.st_mode & S_IXUSR)) {
  97. return (res_path);
  98. }
  99. res = snprintf(res_path, PATH_MAX, "/usr/bin/%s", exec);
  100. assert(res > 0 && res < PATH_MAX);
  101. if (stat(res_path, &stat_buf) == 0 && (stat_buf.st_mode & S_IXUSR)) {
  102. return (res_path);
  103. }
  104. return (NULL);
  105. }
  106. static int
  107. wait_for_no_running(struct process_list *plist, int no_running, int no_in_kill_list)
  108. {
  109. int timeout;
  110. int no_repeats;
  111. int i;
  112. no_repeats = WAIT_FOR_NO_RUNNING_REPEATS;
  113. timeout = WAIT_FOR_NO_RUNNING_TIMEOUT / no_repeats;
  114. for (i = 0; i < no_repeats; i++) {
  115. assert(process_list_waitpid(plist) == 0);
  116. if (process_list_get_no_running(plist) == no_running &&
  117. process_list_get_kill_list_items(plist) == no_in_kill_list) {
  118. return (0);
  119. }
  120. poll(NULL, 0, timeout);
  121. }
  122. return (-1);
  123. }
  124. int
  125. main(void)
  126. {
  127. struct process_list plist;
  128. struct process_list_entry *plist_entry;
  129. char *true_path, *false_path;
  130. assert((true_path = find_exec_path("true")) != NULL);
  131. assert((false_path = find_exec_path("false")) != NULL);
  132. signal_handlers_register();
  133. process_list_init(&plist, 10, 1, plist_notify, (void *)0x42);
  134. plist_entry = process_list_add(&plist, "test name", "command");
  135. assert(plist_entry != NULL);
  136. assert(strcmp(plist_entry->name, "test name") == 0);
  137. assert(plist_entry->state == PROCESS_LIST_ENTRY_STATE_INITIALIZED);
  138. assert(plist_entry->exec_argc == 1);
  139. assert(plist_entry->exec_argv[0] != NULL && strcmp(plist_entry->exec_argv[0], "command") == 0);
  140. assert(plist_entry->exec_argv[1] == NULL);
  141. plist_entry = process_list_add(&plist, "test name", "/bin/ping -c \"host wit\\\"h space\" notaspace");
  142. assert(plist_entry != NULL);
  143. assert(strcmp(plist_entry->name, "test name") == 0);
  144. assert(plist_entry->state == PROCESS_LIST_ENTRY_STATE_INITIALIZED);
  145. assert(plist_entry->exec_argc == 4);
  146. assert(plist_entry->exec_argv[0] != NULL && strcmp(plist_entry->exec_argv[0], "/bin/ping") == 0);
  147. assert(plist_entry->exec_argv[1] != NULL && strcmp(plist_entry->exec_argv[1], "-c") == 0);
  148. assert(plist_entry->exec_argv[2] != NULL && strcmp(plist_entry->exec_argv[2], "host wit\"h space") == 0);
  149. assert(plist_entry->exec_argv[3] != NULL && strcmp(plist_entry->exec_argv[3], "notaspace") == 0);
  150. assert(plist_entry->exec_argv[4] == NULL);
  151. process_list_free(&plist);
  152. /*
  153. * Test no process
  154. */
  155. no_executed = 0;
  156. no_finished = 0;
  157. assert(process_list_exec_initialized(&plist) == 0);
  158. assert(no_executed == 0);
  159. assert(process_list_get_no_running(&plist) == 0);
  160. assert(wait_for_no_running(&plist, 0, 0) == 0);
  161. assert(no_finished == 0);
  162. assert(process_list_get_summary_result(&plist) == 0);
  163. assert(process_list_get_summary_result_short(&plist) == 0);
  164. process_list_free(&plist);
  165. /*
  166. * Test two processes. /bin/true and /bin/false. Accumulated result should be fail
  167. */
  168. plist_entry = process_list_add(&plist, "true", true_path);
  169. assert(plist_entry != NULL);
  170. plist_entry = process_list_add(&plist, "false", false_path);
  171. assert(plist_entry != NULL);
  172. no_executed = 0;
  173. no_finished = 0;
  174. assert(process_list_exec_initialized(&plist) == 0);
  175. assert(no_executed == 2);
  176. assert(process_list_get_no_running(&plist) == 2);
  177. /*
  178. * Wait to exit
  179. */
  180. assert(wait_for_no_running(&plist, 0, 0) == 0);
  181. assert(process_list_waitpid(&plist) == 0);
  182. assert(process_list_get_no_running(&plist) == 0);
  183. assert(no_finished == 2);
  184. assert(process_list_get_summary_result(&plist) == 1);
  185. assert(process_list_get_summary_result_short(&plist) == 1);
  186. process_list_free(&plist);
  187. /*
  188. * Test two processes. /bin/true and one non-existing. Accumulated result should be fail
  189. */
  190. plist_entry = process_list_add(&plist, "true", true_path);
  191. assert(plist_entry != NULL);
  192. plist_entry = process_list_add(&plist, "false", "/nonexistingdir/nonexistingfile");
  193. assert(plist_entry != NULL);
  194. no_executed = 0;
  195. no_finished = 0;
  196. assert(process_list_exec_initialized(&plist) == 0);
  197. assert(no_executed == 2);
  198. assert(process_list_get_no_running(&plist) == 2);
  199. /*
  200. * Wait to exit
  201. */
  202. assert(wait_for_no_running(&plist, 0, 0) == 0);
  203. assert(no_finished == 2);
  204. assert(process_list_get_summary_result(&plist) == 1);
  205. assert(process_list_get_summary_result_short(&plist) == 1);
  206. process_list_free(&plist);
  207. /*
  208. * Test three processes /bin/true. Accumulated result should be success.
  209. */
  210. plist_entry = process_list_add(&plist, "true", true_path);
  211. assert(plist_entry != NULL);
  212. plist_entry = process_list_add(&plist, "true2", true_path);
  213. assert(plist_entry != NULL);
  214. plist_entry = process_list_add(&plist, "true3", true_path);
  215. assert(plist_entry != NULL);
  216. no_executed = 0;
  217. no_finished = 0;
  218. assert(process_list_exec_initialized(&plist) == 0);
  219. assert(no_executed == 3);
  220. assert(process_list_get_no_running(&plist) == 3);
  221. /*
  222. * Wait to exit
  223. */
  224. assert(wait_for_no_running(&plist, 0, 0) == 0);
  225. assert(no_finished == 3);
  226. assert(process_list_get_summary_result(&plist) == 0);
  227. assert(process_list_get_summary_result_short(&plist) == 0);
  228. process_list_free(&plist);
  229. /*
  230. * Test two processes. /bin/true and cat. Cat blocks so test kill list
  231. */
  232. plist_entry = process_list_add(&plist, "true", true_path);
  233. assert(plist_entry != NULL);
  234. plist_entry = process_list_add(&plist, "cat", "/bin/cat /dev/zero");
  235. assert(plist_entry != NULL);
  236. no_executed = 0;
  237. no_finished = 0;
  238. assert(process_list_exec_initialized(&plist) == 0);
  239. assert(no_executed == 2);
  240. assert(process_list_get_no_running(&plist) == 2);
  241. assert(wait_for_no_running(&plist, 1, 0) == 0);
  242. assert(no_finished == 1);
  243. assert(process_list_get_summary_result(&plist) == -1);
  244. assert(process_list_get_summary_result_short(&plist) == -1);
  245. process_list_move_active_entries_to_kill_list(&plist);
  246. assert(process_list_process_kill_list(&plist) == 0);
  247. /*
  248. * There should be 0 running and 0 in kill list
  249. */
  250. assert(wait_for_no_running(&plist, 0, 0) == 0);
  251. assert(process_list_get_kill_list_items(&plist) == 0);
  252. assert(process_list_process_kill_list(&plist) == 0);
  253. process_list_free(&plist);
  254. /*
  255. * Test two bash proceses. One ignores INT and second ignores INT and TERM.
  256. */
  257. plist_entry = process_list_add(&plist, "ignoresig1", "bash -c \"trap 'echo trap' SIGINT;while true;do sleep 1;done\"");
  258. assert(plist_entry != NULL);
  259. plist_entry = process_list_add(&plist, "ignoresig2", "bash -c \"trap 'echo trap' SIGINT SIGTERM;while true;do sleep 1;done\"");
  260. assert(plist_entry != NULL);
  261. no_executed = 0;
  262. no_finished = 0;
  263. assert(process_list_exec_initialized(&plist) == 0);
  264. assert(no_executed == 2);
  265. assert(process_list_get_no_running(&plist) == 2);
  266. /*
  267. * Wait some time. 2 processes should be running
  268. */
  269. poll(NULL, 0, 500);
  270. assert(process_list_waitpid(&plist) == 0);
  271. assert(process_list_get_no_running(&plist) == 2);
  272. assert(no_finished == 0);
  273. assert(process_list_get_summary_result(&plist) == -1);
  274. assert(process_list_get_summary_result_short(&plist) == -1);
  275. process_list_move_active_entries_to_kill_list(&plist);
  276. assert(wait_for_no_running(&plist, 0, 2) == 0);
  277. assert(process_list_process_kill_list(&plist) == 0);
  278. assert(wait_for_no_running(&plist, 0, 1) == 0);
  279. assert(process_list_process_kill_list(&plist) == 0);
  280. assert(wait_for_no_running(&plist, 0, 0) == 0);
  281. process_list_free(&plist);
  282. /*
  283. * Test 3 processes. Test if entries are properly deallocated
  284. */
  285. process_list_init(&plist, 3, 1, plist_notify, (void *)0x42);
  286. plist_entry = process_list_add(&plist, "true", true_path);
  287. assert(plist_entry != NULL);
  288. plist_entry = process_list_add(&plist, "true2", true_path);
  289. assert(plist_entry != NULL);
  290. plist_entry = process_list_add(&plist, "true3", true_path);
  291. assert(plist_entry != NULL);
  292. /*
  293. * Insert fails
  294. */
  295. plist_entry = process_list_add(&plist, "true4", true_path);
  296. assert(plist_entry == NULL);
  297. no_executed = 0;
  298. no_finished = 0;
  299. assert(process_list_exec_initialized(&plist) == 0);
  300. assert(no_executed == 3);
  301. assert(process_list_get_no_running(&plist) == 3);
  302. /*
  303. * Wait to exit
  304. */
  305. assert(wait_for_no_running(&plist, 0, 0) == 0);
  306. assert(no_finished == 3);
  307. assert(process_list_get_summary_result(&plist) == 0);
  308. assert(process_list_get_summary_result_short(&plist) == 0);
  309. process_list_move_active_entries_to_kill_list(&plist);
  310. plist_entry = process_list_add(&plist, "true", true_path);
  311. assert(plist_entry != NULL);
  312. plist_entry = process_list_add(&plist, "ignoresig1", "bash -c \"trap 'echo trap' SIGINT;while true;do sleep 1;done\"");
  313. assert(plist_entry != NULL);
  314. plist_entry = process_list_add(&plist, "ignoresig2", "bash -c \"trap 'echo trap' SIGINT SIGTERM;while true;do sleep 1;done\"");
  315. assert(plist_entry != NULL);
  316. plist_entry = process_list_add(&plist, "true4", true_path);
  317. assert(plist_entry == NULL);
  318. no_executed = 0;
  319. no_finished = 0;
  320. assert(process_list_exec_initialized(&plist) == 0);
  321. assert(no_executed == 3);
  322. assert(process_list_get_no_running(&plist) == 3);
  323. assert(wait_for_no_running(&plist, 2, 0) == 0);
  324. assert(process_list_get_no_running(&plist) == 2);
  325. assert(no_finished == 1);
  326. assert(process_list_get_summary_result(&plist) == -1);
  327. assert(process_list_get_summary_result_short(&plist) == -1);
  328. plist_entry = process_list_add(&plist, "true4", true_path);
  329. assert(plist_entry == NULL);
  330. process_list_move_active_entries_to_kill_list(&plist);
  331. plist_entry = process_list_add(&plist, "true4", true_path);
  332. assert(plist_entry != NULL);
  333. plist_entry = process_list_add(&plist, "true5", true_path);
  334. assert(plist_entry == NULL);
  335. assert(process_list_process_kill_list(&plist) == 0);
  336. assert(wait_for_no_running(&plist, 0, 1) == 0);
  337. assert(process_list_process_kill_list(&plist) == 0);
  338. assert(wait_for_no_running(&plist, 0, 0) == 0);
  339. process_list_move_active_entries_to_kill_list(&plist);
  340. assert(process_list_get_summary_result(&plist) == 0);
  341. assert(process_list_get_summary_result_short(&plist) == 0);
  342. plist_entry = process_list_add(&plist, "true", true_path);
  343. assert(plist_entry != NULL);
  344. plist_entry = process_list_add(&plist, "true2", true_path);
  345. assert(plist_entry != NULL);
  346. plist_entry = process_list_add(&plist, "true3", true_path);
  347. assert(plist_entry != NULL);
  348. plist_entry = process_list_add(&plist, "true4", true_path);
  349. assert(plist_entry == NULL);
  350. process_list_free(&plist);
  351. /*
  352. * Test 3 processes and difference between summary and short-circuit summary
  353. */
  354. process_list_init(&plist, 3, 1, plist_notify, (void *)0x42);
  355. plist_entry = process_list_add(&plist, "true", true_path);
  356. assert(plist_entry != NULL);
  357. plist_entry = process_list_add(&plist, "false", false_path);
  358. assert(plist_entry != NULL);
  359. plist_entry = process_list_add(&plist, "loop", "bash -c \"while true;do sleep 1;done\"");
  360. assert(plist_entry != NULL);
  361. plist_entry = process_list_add(&plist, "true4", true_path);
  362. assert(plist_entry == NULL);
  363. no_executed = 0;
  364. no_finished = 0;
  365. assert(process_list_exec_initialized(&plist) == 0);
  366. assert(no_executed == 3);
  367. assert(process_list_get_no_running(&plist) == 3);
  368. /*
  369. * Wait to exit
  370. */
  371. assert(wait_for_no_running(&plist, 1, 0) == 0);
  372. assert(no_finished == 2);
  373. assert(process_list_get_summary_result(&plist) == -1);
  374. assert(process_list_get_summary_result_short(&plist) == 1);
  375. process_list_move_active_entries_to_kill_list(&plist);
  376. assert(process_list_process_kill_list(&plist) == 0);
  377. assert(wait_for_no_running(&plist, 0, 0) == 0);
  378. process_list_free(&plist);
  379. /*
  380. * Test process_list_killall by running two bash proceses.
  381. * One ignores INT and second ignores INT and TERM. Waiting for maximum of 2 sec
  382. */
  383. plist_entry = process_list_add(&plist, "ignoresig1", "bash -c \"trap 'echo trap' SIGINT;while true;do sleep 1;done\"");
  384. assert(plist_entry != NULL);
  385. plist_entry = process_list_add(&plist, "ignoresig2", "bash -c \"trap 'echo trap' SIGINT SIGTERM;while true;do sleep 1;done\"");
  386. assert(plist_entry != NULL);
  387. no_executed = 0;
  388. no_finished = 0;
  389. assert(process_list_exec_initialized(&plist) == 0);
  390. assert(no_executed == 2);
  391. assert(process_list_get_no_running(&plist) == 2);
  392. /*
  393. * Ensure processes are running after pause
  394. */
  395. poll(NULL, 0, 500);
  396. assert(process_list_waitpid(&plist) == 0);
  397. assert(process_list_get_no_running(&plist) == 2);
  398. assert(no_finished == 0);
  399. assert(process_list_get_summary_result(&plist) == -1);
  400. assert(process_list_get_summary_result_short(&plist) == -1);
  401. assert(process_list_killall(&plist, 2000) == 0);
  402. assert(process_list_get_kill_list_items(&plist) == 0);
  403. process_list_free(&plist);
  404. /*
  405. * Empty killall exits with sucess result
  406. */
  407. assert(process_list_killall(&plist, 2000) == 0);
  408. process_list_free(&plist);
  409. return (0);
  410. }