test-process-list.c 16 KB

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