test-process-list.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. * 10 min timeout
  47. */
  48. #define WAIT_FOR_NO_RUNNING_REPEATS 6000
  49. #define WAIT_FOR_NO_RUNNING_TIMEOUT 600000
  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 volatile sig_atomic_t sigusr1_received;
  60. static volatile sig_atomic_t sigusr2_received;
  61. static void
  62. signal_usr1_handler(int sig)
  63. {
  64. sigusr1_received = 1;
  65. }
  66. static void
  67. signal_usr2_handler(int sig)
  68. {
  69. sigusr2_received = 1;
  70. }
  71. static void
  72. signal_handlers_register(void)
  73. {
  74. struct sigaction act;
  75. act.sa_handler = SIG_DFL;
  76. sigemptyset(&act.sa_mask);
  77. act.sa_flags = SA_RESTART;
  78. sigaction(SIGCHLD, &act, NULL);
  79. act.sa_handler = SIG_IGN;
  80. sigemptyset(&act.sa_mask);
  81. act.sa_flags = SA_RESTART;
  82. sigaction(SIGPIPE, &act, NULL);
  83. act.sa_handler = signal_usr1_handler;
  84. sigemptyset(&act.sa_mask);
  85. act.sa_flags = SA_RESTART;
  86. sigaction(SIGUSR1, &act, NULL);
  87. act.sa_handler = signal_usr2_handler;
  88. sigemptyset(&act.sa_mask);
  89. act.sa_flags = SA_RESTART;
  90. sigaction(SIGUSR2, &act, NULL);
  91. }
  92. static void
  93. plist_notify(enum process_list_notify_reason reason, const struct process_list_entry *entry,
  94. void *user_data)
  95. {
  96. assert(user_data == (void *)0x42);
  97. switch (reason) {
  98. case PROCESS_LIST_NOTIFY_REASON_EXECUTED:
  99. no_executed++;
  100. break;
  101. case PROCESS_LIST_NOTIFY_REASON_FINISHED:
  102. no_finished++;
  103. break;
  104. }
  105. }
  106. static char *
  107. find_exec_path(const char *exec)
  108. {
  109. struct stat stat_buf;
  110. char *res_path;
  111. int res;
  112. assert((res_path = malloc(PATH_MAX)) != NULL);
  113. memset(res_path, 0, PATH_MAX);
  114. res = snprintf(res_path, PATH_MAX, "/bin/%s", exec);
  115. assert(res > 0 && res < PATH_MAX);
  116. if (stat(res_path, &stat_buf) == 0 && (stat_buf.st_mode & S_IXUSR)) {
  117. return (res_path);
  118. }
  119. res = snprintf(res_path, PATH_MAX, "/usr/bin/%s", exec);
  120. assert(res > 0 && res < PATH_MAX);
  121. if (stat(res_path, &stat_buf) == 0 && (stat_buf.st_mode & S_IXUSR)) {
  122. return (res_path);
  123. }
  124. return (NULL);
  125. }
  126. static int
  127. wait_for_no_running(struct process_list *plist, int no_running, int no_in_kill_list)
  128. {
  129. int timeout;
  130. int no_repeats;
  131. int i;
  132. no_repeats = WAIT_FOR_NO_RUNNING_REPEATS;
  133. timeout = WAIT_FOR_NO_RUNNING_TIMEOUT / no_repeats;
  134. for (i = 0; i < no_repeats; i++) {
  135. assert(process_list_waitpid(plist) == 0);
  136. if (process_list_get_no_running(plist) == no_running &&
  137. process_list_get_kill_list_items(plist) == no_in_kill_list) {
  138. return (0);
  139. }
  140. poll(NULL, 0, timeout);
  141. }
  142. return (-1);
  143. }
  144. static int
  145. wait_for_sigusrs_received(void)
  146. {
  147. int timeout;
  148. int no_repeats;
  149. int i;
  150. no_repeats = WAIT_FOR_NO_RUNNING_REPEATS;
  151. timeout = WAIT_FOR_NO_RUNNING_TIMEOUT / no_repeats;
  152. for (i = 0; i < no_repeats; i++) {
  153. if (sigusr1_received && sigusr2_received) {
  154. return (0);
  155. }
  156. poll(NULL, 0, timeout);
  157. }
  158. return (-1);
  159. }
  160. int
  161. main(void)
  162. {
  163. struct process_list plist;
  164. struct process_list_entry *plist_entry;
  165. char *true_path, *false_path;
  166. char ignore_sigint_cmd[PATH_MAX];
  167. char ignore_sigintterm_cmd[PATH_MAX];
  168. assert(snprintf(ignore_sigint_cmd, PATH_MAX,
  169. "bash -c \"trap 'echo trap' SIGINT;kill -USR1 %ld;while true;do sleep 1;done\"",
  170. (long int)getpid()) < PATH_MAX);
  171. assert(snprintf(ignore_sigintterm_cmd, PATH_MAX,
  172. "bash -c \"trap 'echo trap' SIGINT SIGTERM;kill -USR2 %ld;while true;do sleep 1;done\"",
  173. (long int)getpid()) < PATH_MAX);
  174. assert((true_path = find_exec_path("true")) != NULL);
  175. assert((false_path = find_exec_path("false")) != NULL);
  176. signal_handlers_register();
  177. process_list_init(&plist, 10, 1, plist_notify, (void *)0x42);
  178. plist_entry = process_list_add(&plist, "test name", "command");
  179. assert(plist_entry != NULL);
  180. assert(strcmp(plist_entry->name, "test name") == 0);
  181. assert(plist_entry->state == PROCESS_LIST_ENTRY_STATE_INITIALIZED);
  182. assert(plist_entry->exec_argc == 1);
  183. assert(plist_entry->exec_argv[0] != NULL && strcmp(plist_entry->exec_argv[0], "command") == 0);
  184. assert(plist_entry->exec_argv[1] == NULL);
  185. plist_entry = process_list_add(&plist, "test name", "/bin/ping -c \"host wit\\\"h space\" notaspace");
  186. assert(plist_entry != NULL);
  187. assert(strcmp(plist_entry->name, "test name") == 0);
  188. assert(plist_entry->state == PROCESS_LIST_ENTRY_STATE_INITIALIZED);
  189. assert(plist_entry->exec_argc == 4);
  190. assert(plist_entry->exec_argv[0] != NULL && strcmp(plist_entry->exec_argv[0], "/bin/ping") == 0);
  191. assert(plist_entry->exec_argv[1] != NULL && strcmp(plist_entry->exec_argv[1], "-c") == 0);
  192. assert(plist_entry->exec_argv[2] != NULL && strcmp(plist_entry->exec_argv[2], "host wit\"h space") == 0);
  193. assert(plist_entry->exec_argv[3] != NULL && strcmp(plist_entry->exec_argv[3], "notaspace") == 0);
  194. assert(plist_entry->exec_argv[4] == NULL);
  195. process_list_free(&plist);
  196. /*
  197. * Test no process
  198. */
  199. no_executed = 0;
  200. no_finished = 0;
  201. assert(process_list_exec_initialized(&plist) == 0);
  202. assert(no_executed == 0);
  203. assert(process_list_get_no_running(&plist) == 0);
  204. assert(wait_for_no_running(&plist, 0, 0) == 0);
  205. assert(no_finished == 0);
  206. assert(process_list_get_summary_result(&plist) == 0);
  207. assert(process_list_get_summary_result_short(&plist) == 0);
  208. process_list_free(&plist);
  209. /*
  210. * Test two processes. /bin/true and /bin/false. Accumulated result should be fail
  211. */
  212. plist_entry = process_list_add(&plist, "true", true_path);
  213. assert(plist_entry != NULL);
  214. plist_entry = process_list_add(&plist, "false", false_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 == 2);
  220. assert(process_list_get_no_running(&plist) == 2);
  221. /*
  222. * Wait to exit
  223. */
  224. assert(wait_for_no_running(&plist, 0, 0) == 0);
  225. assert(process_list_waitpid(&plist) == 0);
  226. assert(process_list_get_no_running(&plist) == 0);
  227. assert(no_finished == 2);
  228. assert(process_list_get_summary_result(&plist) == 1);
  229. assert(process_list_get_summary_result_short(&plist) == 1);
  230. process_list_free(&plist);
  231. /*
  232. * Test two processes. /bin/true and one non-existing. Accumulated result should be fail
  233. */
  234. plist_entry = process_list_add(&plist, "true", true_path);
  235. assert(plist_entry != NULL);
  236. plist_entry = process_list_add(&plist, "false", "/nonexistingdir/nonexistingfile");
  237. assert(plist_entry != NULL);
  238. no_executed = 0;
  239. no_finished = 0;
  240. assert(process_list_exec_initialized(&plist) == 0);
  241. assert(no_executed == 2);
  242. assert(process_list_get_no_running(&plist) == 2);
  243. /*
  244. * Wait to exit
  245. */
  246. assert(wait_for_no_running(&plist, 0, 0) == 0);
  247. assert(no_finished == 2);
  248. assert(process_list_get_summary_result(&plist) == 1);
  249. assert(process_list_get_summary_result_short(&plist) == 1);
  250. process_list_free(&plist);
  251. /*
  252. * Test three processes /bin/true. Accumulated result should be success.
  253. */
  254. plist_entry = process_list_add(&plist, "true", true_path);
  255. assert(plist_entry != NULL);
  256. plist_entry = process_list_add(&plist, "true2", true_path);
  257. assert(plist_entry != NULL);
  258. plist_entry = process_list_add(&plist, "true3", true_path);
  259. assert(plist_entry != NULL);
  260. no_executed = 0;
  261. no_finished = 0;
  262. assert(process_list_exec_initialized(&plist) == 0);
  263. assert(no_executed == 3);
  264. assert(process_list_get_no_running(&plist) == 3);
  265. /*
  266. * Wait to exit
  267. */
  268. assert(wait_for_no_running(&plist, 0, 0) == 0);
  269. assert(no_finished == 3);
  270. assert(process_list_get_summary_result(&plist) == 0);
  271. assert(process_list_get_summary_result_short(&plist) == 0);
  272. process_list_free(&plist);
  273. /*
  274. * Test two processes. /bin/true and cat. Cat blocks so test kill list
  275. */
  276. plist_entry = process_list_add(&plist, "true", true_path);
  277. assert(plist_entry != NULL);
  278. plist_entry = process_list_add(&plist, "cat", "/bin/cat /dev/zero");
  279. assert(plist_entry != NULL);
  280. no_executed = 0;
  281. no_finished = 0;
  282. assert(process_list_exec_initialized(&plist) == 0);
  283. assert(no_executed == 2);
  284. assert(process_list_get_no_running(&plist) == 2);
  285. assert(wait_for_no_running(&plist, 1, 0) == 0);
  286. assert(no_finished == 1);
  287. assert(process_list_get_summary_result(&plist) == -1);
  288. assert(process_list_get_summary_result_short(&plist) == -1);
  289. process_list_move_active_entries_to_kill_list(&plist);
  290. assert(process_list_process_kill_list(&plist) == 0);
  291. /*
  292. * There should be 0 running and 0 in kill list
  293. */
  294. assert(wait_for_no_running(&plist, 0, 0) == 0);
  295. assert(process_list_get_kill_list_items(&plist) == 0);
  296. assert(process_list_process_kill_list(&plist) == 0);
  297. process_list_free(&plist);
  298. /*
  299. * Test two bash proceses. One ignores INT and second ignores INT and TERM.
  300. */
  301. sigusr1_received = 0;
  302. plist_entry = process_list_add(&plist, "ignoresig1", ignore_sigint_cmd);
  303. assert(plist_entry != NULL);
  304. sigusr2_received = 0;
  305. plist_entry = process_list_add(&plist, "ignoresig2", ignore_sigintterm_cmd);
  306. assert(plist_entry != NULL);
  307. no_executed = 0;
  308. no_finished = 0;
  309. assert(process_list_exec_initialized(&plist) == 0);
  310. assert(no_executed == 2);
  311. assert(process_list_get_no_running(&plist) == 2);
  312. assert(wait_for_sigusrs_received() == 0);
  313. /*
  314. * Wait some time. 2 processes should be running
  315. */
  316. poll(NULL, 0, 500);
  317. assert(process_list_waitpid(&plist) == 0);
  318. assert(process_list_get_no_running(&plist) == 2);
  319. assert(no_finished == 0);
  320. assert(process_list_get_summary_result(&plist) == -1);
  321. assert(process_list_get_summary_result_short(&plist) == -1);
  322. process_list_move_active_entries_to_kill_list(&plist);
  323. assert(wait_for_no_running(&plist, 0, 2) == 0);
  324. assert(process_list_process_kill_list(&plist) == 0);
  325. assert(wait_for_no_running(&plist, 0, 1) == 0);
  326. assert(process_list_process_kill_list(&plist) == 0);
  327. assert(wait_for_no_running(&plist, 0, 0) == 0);
  328. process_list_free(&plist);
  329. /*
  330. * Test 3 processes. Test if entries are properly deallocated
  331. */
  332. process_list_init(&plist, 3, 1, plist_notify, (void *)0x42);
  333. plist_entry = process_list_add(&plist, "true", true_path);
  334. assert(plist_entry != NULL);
  335. plist_entry = process_list_add(&plist, "true2", true_path);
  336. assert(plist_entry != NULL);
  337. plist_entry = process_list_add(&plist, "true3", true_path);
  338. assert(plist_entry != NULL);
  339. /*
  340. * Insert fails
  341. */
  342. plist_entry = process_list_add(&plist, "true4", true_path);
  343. assert(plist_entry == NULL);
  344. no_executed = 0;
  345. no_finished = 0;
  346. assert(process_list_exec_initialized(&plist) == 0);
  347. assert(no_executed == 3);
  348. assert(process_list_get_no_running(&plist) == 3);
  349. /*
  350. * Wait to exit
  351. */
  352. assert(wait_for_no_running(&plist, 0, 0) == 0);
  353. assert(no_finished == 3);
  354. assert(process_list_get_summary_result(&plist) == 0);
  355. assert(process_list_get_summary_result_short(&plist) == 0);
  356. process_list_move_active_entries_to_kill_list(&plist);
  357. plist_entry = process_list_add(&plist, "true", true_path);
  358. assert(plist_entry != NULL);
  359. sigusr1_received = 0;
  360. plist_entry = process_list_add(&plist, "ignoresig1", ignore_sigint_cmd);
  361. assert(plist_entry != NULL);
  362. sigusr2_received = 0;
  363. plist_entry = process_list_add(&plist, "ignoresig2", ignore_sigintterm_cmd);
  364. assert(plist_entry != NULL);
  365. plist_entry = process_list_add(&plist, "true4", true_path);
  366. assert(plist_entry == NULL);
  367. no_executed = 0;
  368. no_finished = 0;
  369. assert(process_list_exec_initialized(&plist) == 0);
  370. assert(no_executed == 3);
  371. assert(process_list_get_no_running(&plist) == 3);
  372. assert(wait_for_sigusrs_received() == 0);
  373. assert(wait_for_no_running(&plist, 2, 0) == 0);
  374. assert(process_list_get_no_running(&plist) == 2);
  375. assert(no_finished == 1);
  376. assert(process_list_get_summary_result(&plist) == -1);
  377. assert(process_list_get_summary_result_short(&plist) == -1);
  378. plist_entry = process_list_add(&plist, "true4", true_path);
  379. assert(plist_entry == NULL);
  380. process_list_move_active_entries_to_kill_list(&plist);
  381. plist_entry = process_list_add(&plist, "true4", true_path);
  382. assert(plist_entry != NULL);
  383. plist_entry = process_list_add(&plist, "true5", true_path);
  384. assert(plist_entry == NULL);
  385. assert(process_list_process_kill_list(&plist) == 0);
  386. assert(wait_for_no_running(&plist, 0, 1) == 0);
  387. assert(process_list_process_kill_list(&plist) == 0);
  388. assert(wait_for_no_running(&plist, 0, 0) == 0);
  389. process_list_move_active_entries_to_kill_list(&plist);
  390. assert(process_list_get_summary_result(&plist) == 0);
  391. assert(process_list_get_summary_result_short(&plist) == 0);
  392. plist_entry = process_list_add(&plist, "true", true_path);
  393. assert(plist_entry != NULL);
  394. plist_entry = process_list_add(&plist, "true2", true_path);
  395. assert(plist_entry != NULL);
  396. plist_entry = process_list_add(&plist, "true3", true_path);
  397. assert(plist_entry != NULL);
  398. plist_entry = process_list_add(&plist, "true4", true_path);
  399. assert(plist_entry == NULL);
  400. process_list_free(&plist);
  401. /*
  402. * Test 3 processes and difference between summary and short-circuit summary
  403. */
  404. process_list_init(&plist, 3, 1, plist_notify, (void *)0x42);
  405. plist_entry = process_list_add(&plist, "true", true_path);
  406. assert(plist_entry != NULL);
  407. plist_entry = process_list_add(&plist, "false", false_path);
  408. assert(plist_entry != NULL);
  409. plist_entry = process_list_add(&plist, "loop", "bash -c \"while true;do sleep 1;done\"");
  410. assert(plist_entry != NULL);
  411. plist_entry = process_list_add(&plist, "true4", true_path);
  412. assert(plist_entry == NULL);
  413. no_executed = 0;
  414. no_finished = 0;
  415. assert(process_list_exec_initialized(&plist) == 0);
  416. assert(no_executed == 3);
  417. assert(process_list_get_no_running(&plist) == 3);
  418. /*
  419. * Wait to exit
  420. */
  421. assert(wait_for_no_running(&plist, 1, 0) == 0);
  422. assert(no_finished == 2);
  423. assert(process_list_get_summary_result(&plist) == -1);
  424. assert(process_list_get_summary_result_short(&plist) == 1);
  425. process_list_move_active_entries_to_kill_list(&plist);
  426. assert(process_list_process_kill_list(&plist) == 0);
  427. assert(wait_for_no_running(&plist, 0, 0) == 0);
  428. process_list_free(&plist);
  429. /*
  430. * Test process_list_killall by running two bash proceses.
  431. * One ignores INT and second ignores INT and TERM. Waiting for maximum of 2 sec
  432. */
  433. sigusr1_received = 0;
  434. plist_entry = process_list_add(&plist, "ignoresig1", ignore_sigint_cmd);
  435. assert(plist_entry != NULL);
  436. sigusr2_received = 0;
  437. plist_entry = process_list_add(&plist, "ignoresig2", ignore_sigintterm_cmd);
  438. assert(plist_entry != NULL);
  439. no_executed = 0;
  440. no_finished = 0;
  441. assert(process_list_exec_initialized(&plist) == 0);
  442. assert(no_executed == 2);
  443. assert(process_list_get_no_running(&plist) == 2);
  444. assert(wait_for_sigusrs_received() == 0);
  445. /*
  446. * Ensure processes are running after pause
  447. */
  448. poll(NULL, 0, 500);
  449. assert(process_list_waitpid(&plist) == 0);
  450. assert(process_list_get_no_running(&plist) == 2);
  451. assert(no_finished == 0);
  452. assert(process_list_get_summary_result(&plist) == -1);
  453. assert(process_list_get_summary_result_short(&plist) == -1);
  454. assert(process_list_killall(&plist, 2000) == 0);
  455. assert(process_list_get_kill_list_items(&plist) == 0);
  456. process_list_free(&plist);
  457. /*
  458. * Empty killall exits with sucess result
  459. */
  460. assert(process_list_killall(&plist, 2000) == 0);
  461. process_list_free(&plist);
  462. return (0);
  463. }