test-process-list.c 17 KB

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