test-process-list.c 17 KB

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