process-list.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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 <fcntl.h>
  37. #include <sys/wait.h>
  38. #include <string.h>
  39. #include <stdlib.h>
  40. #include <assert.h>
  41. #include <err.h>
  42. #include <errno.h>
  43. #include <poll.h>
  44. #include <unistd.h>
  45. #include "dynar.h"
  46. #include "dynar-str.h"
  47. #include "dynar-simple-lex.h"
  48. #include "process-list.h"
  49. static void process_list_free_argv(size_t no_params, char **argv);
  50. static void process_list_entry_free(struct process_list_entry *entry);
  51. static char **process_list_parse_command(const char *command, size_t *no_params);
  52. static int process_list_entry_exec(const struct process_list *plist,
  53. struct process_list_entry *entry);
  54. void
  55. process_list_init(struct process_list *plist, size_t max_list_entries, int use_execvp,
  56. process_list_notify_fn_t notify_fn, void *notify_fn_user_data)
  57. {
  58. memset(plist, 0, sizeof(*plist));
  59. plist->max_list_entries = max_list_entries;
  60. plist->allocated_list_entries = 0;
  61. plist->use_execvp = use_execvp;
  62. plist->notify_fn = notify_fn;
  63. plist->notify_fn_user_data = notify_fn_user_data;
  64. TAILQ_INIT(&plist->active_list);
  65. TAILQ_INIT(&plist->to_kill_list);
  66. }
  67. static void
  68. process_list_free_argv(size_t no_params, char **argv)
  69. {
  70. size_t zi;
  71. for (zi = 0; zi < no_params; zi++) {
  72. free(argv[zi]);
  73. }
  74. free(argv);
  75. }
  76. static void
  77. process_list_entry_free(struct process_list_entry *entry)
  78. {
  79. process_list_free_argv(entry->exec_argc, entry->exec_argv);
  80. free(entry->name);
  81. free(entry);
  82. }
  83. static char **
  84. process_list_parse_command(const char *command, size_t *no_params)
  85. {
  86. struct dynar command_dstr;
  87. struct dynar_simple_lex lex;
  88. struct dynar *token;
  89. int finished;
  90. char **res_argv;
  91. size_t zi;
  92. res_argv = NULL;
  93. dynar_init(&command_dstr, strlen(command) + 1);
  94. if (dynar_str_cpy(&command_dstr, command) != 0) {
  95. return (NULL);
  96. }
  97. dynar_simple_lex_init(&lex, &command_dstr, DYNAR_SIMPLE_LEX_TYPE_QUOTE);
  98. *no_params = 0;
  99. finished = 0;
  100. while (!finished) {
  101. token = dynar_simple_lex_token_next(&lex);
  102. if (token == NULL) {
  103. goto exit_res;
  104. }
  105. if (strcmp(dynar_data(token), "") == 0) {
  106. finished = 1;
  107. } else {
  108. (*no_params)++;
  109. }
  110. }
  111. if (*no_params < 1) {
  112. goto exit_res;
  113. }
  114. dynar_simple_lex_destroy(&lex);
  115. res_argv = malloc(sizeof(char *) * (*no_params + 1));
  116. if (res_argv == NULL) {
  117. goto exit_res;
  118. }
  119. memset(res_argv, 0, sizeof(char *) * (*no_params + 1));
  120. dynar_simple_lex_init(&lex, &command_dstr, DYNAR_SIMPLE_LEX_TYPE_QUOTE);
  121. finished = 0;
  122. zi = 0;
  123. while (!finished) {
  124. token = dynar_simple_lex_token_next(&lex);
  125. if (token == NULL) {
  126. process_list_free_argv(*no_params, res_argv);
  127. res_argv = NULL;
  128. goto exit_res;
  129. }
  130. if (strcmp(dynar_data(token), "") == 0) {
  131. finished = 1;
  132. } else {
  133. res_argv[zi] = strdup(dynar_data(token));
  134. if (res_argv[zi] == NULL) {
  135. process_list_free_argv(*no_params, res_argv);
  136. res_argv = NULL;
  137. goto exit_res;
  138. }
  139. zi++;
  140. }
  141. }
  142. if (zi != *no_params) {
  143. /*
  144. * If this happens it means something is seriously broken (memory corrupted)
  145. */
  146. process_list_free_argv(*no_params, res_argv);
  147. res_argv = NULL;
  148. goto exit_res;
  149. }
  150. exit_res:
  151. dynar_simple_lex_destroy(&lex);
  152. dynar_destroy(&command_dstr);
  153. return (res_argv);
  154. }
  155. struct process_list_entry *
  156. process_list_add(struct process_list *plist, const char *name, const char *command)
  157. {
  158. struct process_list_entry *entry;
  159. if (plist->allocated_list_entries + 1 > plist->max_list_entries) {
  160. return (NULL);
  161. }
  162. /*
  163. * Alloc new entry
  164. */
  165. entry = malloc(sizeof(*entry));
  166. if (entry == NULL) {
  167. return (NULL);
  168. }
  169. memset(entry, 0, sizeof(*entry));
  170. entry->name = strdup(name);
  171. if (entry->name == NULL) {
  172. process_list_entry_free(entry);
  173. return (NULL);
  174. }
  175. entry->state = PROCESS_LIST_ENTRY_STATE_INITIALIZED;
  176. entry->exec_argv = process_list_parse_command(command, &entry->exec_argc);
  177. if (entry->exec_argv == NULL) {
  178. process_list_entry_free(entry);
  179. return (NULL);
  180. }
  181. plist->allocated_list_entries++;
  182. TAILQ_INSERT_TAIL(&plist->active_list, entry, entries);
  183. return (entry);
  184. }
  185. void
  186. process_list_free(struct process_list *plist)
  187. {
  188. struct process_list_entry *entry;
  189. struct process_list_entry *entry_next;
  190. entry = TAILQ_FIRST(&plist->active_list);
  191. while (entry != NULL) {
  192. entry_next = TAILQ_NEXT(entry, entries);
  193. process_list_entry_free(entry);
  194. entry = entry_next;
  195. }
  196. entry = TAILQ_FIRST(&plist->to_kill_list);
  197. while (entry != NULL) {
  198. entry_next = TAILQ_NEXT(entry, entries);
  199. process_list_entry_free(entry);
  200. entry = entry_next;
  201. }
  202. plist->allocated_list_entries = 0;
  203. TAILQ_INIT(&plist->active_list);
  204. TAILQ_INIT(&plist->to_kill_list);
  205. }
  206. static void
  207. process_list_entry_exec_helper_set_stdfd(void)
  208. {
  209. int devnull;
  210. devnull = open("/dev/null", O_RDWR);
  211. if (devnull == -1) {
  212. err(EXIT_FAILURE, "Can't open /dev/null");
  213. }
  214. if (dup2(devnull, 0) < 0 || dup2(devnull, 1) < 0 || dup2(devnull, 2) < 0) {
  215. close(devnull);
  216. err(EXIT_FAILURE, "Can't dup2 stdin/out/err to /dev/null");
  217. }
  218. close(devnull);
  219. }
  220. static int
  221. process_list_entry_exec(const struct process_list *plist, struct process_list_entry *entry)
  222. {
  223. pid_t pid;
  224. if (entry->state != PROCESS_LIST_ENTRY_STATE_INITIALIZED) {
  225. return (-1);
  226. }
  227. pid = fork();
  228. if (pid == -1) {
  229. return (-1);
  230. } else if (pid == 0) {
  231. process_list_entry_exec_helper_set_stdfd();
  232. if (!plist->use_execvp) {
  233. execv(entry->exec_argv[0], entry->exec_argv);
  234. } else {
  235. execvp(entry->exec_argv[0], entry->exec_argv);
  236. }
  237. /*
  238. * Exec returned -> exec failed
  239. */
  240. err(EXIT_FAILURE, "Can't execute command %s (%s)", entry->name, entry->exec_argv[0]);
  241. } else {
  242. entry->pid = pid;
  243. entry->state = PROCESS_LIST_ENTRY_STATE_RUNNING;
  244. if (plist->notify_fn != NULL) {
  245. plist->notify_fn(PROCESS_LIST_NOTIFY_REASON_EXECUTED, entry,
  246. plist->notify_fn_user_data);
  247. }
  248. }
  249. return (0);
  250. }
  251. int
  252. process_list_exec_initialized(struct process_list *plist)
  253. {
  254. struct process_list_entry *entry;
  255. TAILQ_FOREACH(entry, &plist->active_list, entries) {
  256. if (entry->state == PROCESS_LIST_ENTRY_STATE_INITIALIZED) {
  257. if (process_list_entry_exec(plist, entry) != 0) {
  258. return (-1);
  259. }
  260. }
  261. }
  262. return (0);
  263. }
  264. static int
  265. process_list_entry_waitpid(const struct process_list *plist, struct process_list_entry *entry)
  266. {
  267. pid_t wpid_res;
  268. int status;
  269. if (entry->state == PROCESS_LIST_ENTRY_STATE_INITIALIZED ||
  270. entry->state == PROCESS_LIST_ENTRY_STATE_FINISHED) {
  271. return (0);
  272. }
  273. wpid_res = waitpid(entry->pid, &status, WNOHANG);
  274. if (wpid_res == -1) {
  275. return (-1);
  276. }
  277. if (wpid_res == 0) {
  278. /*
  279. * No change
  280. */
  281. return (0);
  282. }
  283. entry->exit_status = status;
  284. if (entry->state == PROCESS_LIST_ENTRY_STATE_RUNNING) {
  285. if (plist->notify_fn != NULL) {
  286. plist->notify_fn(PROCESS_LIST_NOTIFY_REASON_FINISHED, entry,
  287. plist->notify_fn_user_data);
  288. }
  289. }
  290. entry->state = PROCESS_LIST_ENTRY_STATE_FINISHED;
  291. return (0);
  292. }
  293. int
  294. process_list_waitpid(struct process_list *plist)
  295. {
  296. struct process_list_entry *entry;
  297. struct process_list_entry *entry_next;
  298. TAILQ_FOREACH(entry, &plist->active_list, entries) {
  299. if (process_list_entry_waitpid(plist, entry) != 0) {
  300. return (-1);
  301. }
  302. }
  303. entry = TAILQ_FIRST(&plist->to_kill_list);
  304. while (entry != NULL) {
  305. entry_next = TAILQ_NEXT(entry, entries);
  306. if (process_list_entry_waitpid(plist, entry) != 0) {
  307. return (-1);
  308. }
  309. if (entry->state == PROCESS_LIST_ENTRY_STATE_FINISHED) {
  310. /*
  311. * Process finished -> remove it from list
  312. */
  313. TAILQ_REMOVE(&plist->to_kill_list, entry, entries);
  314. process_list_entry_free(entry);
  315. plist->allocated_list_entries--;
  316. }
  317. entry = entry_next;
  318. }
  319. return (0);
  320. }
  321. size_t
  322. process_list_get_no_running(struct process_list *plist)
  323. {
  324. struct process_list_entry *entry;
  325. size_t res;
  326. res = 0;
  327. TAILQ_FOREACH(entry, &plist->active_list, entries) {
  328. if (entry->state == PROCESS_LIST_ENTRY_STATE_RUNNING) {
  329. res++;
  330. }
  331. }
  332. return (res);
  333. }
  334. /*
  335. * -1 = Not all processes finished
  336. * 0 = All processes finished successfully
  337. * 1 - All processes finished but some of them not successfully
  338. */
  339. int
  340. process_list_get_summary_result(struct process_list *plist)
  341. {
  342. struct process_list_entry *entry;
  343. int res;
  344. res = 0;
  345. TAILQ_FOREACH(entry, &plist->active_list, entries) {
  346. if (entry->state != PROCESS_LIST_ENTRY_STATE_FINISHED) {
  347. return (-1);
  348. }
  349. if (!WIFEXITED(entry->exit_status) || WEXITSTATUS(entry->exit_status) != 0) {
  350. res = 1;
  351. }
  352. }
  353. return (res);
  354. }
  355. /*
  356. * 0 = All processes finished successfully
  357. * 1 = Some process finished and failed
  358. * -1 = Not all processed finished and none of finished failed
  359. */
  360. int
  361. process_list_get_summary_result_short(struct process_list *plist)
  362. {
  363. struct process_list_entry *entry;
  364. int res;
  365. res = 0;
  366. TAILQ_FOREACH(entry, &plist->active_list, entries) {
  367. if (entry->state == PROCESS_LIST_ENTRY_STATE_FINISHED) {
  368. if (!WIFEXITED(entry->exit_status) || WEXITSTATUS(entry->exit_status) != 0) {
  369. return (1);
  370. }
  371. } else {
  372. res = -1;
  373. }
  374. }
  375. return (res);
  376. }
  377. static void
  378. process_list_move_entry_to_kill_list(struct process_list *plist, struct process_list_entry *entry)
  379. {
  380. TAILQ_REMOVE(&plist->active_list, entry, entries);
  381. TAILQ_INSERT_TAIL(&plist->to_kill_list, entry, entries);
  382. }
  383. void
  384. process_list_move_active_entries_to_kill_list(struct process_list *plist)
  385. {
  386. struct process_list_entry *entry;
  387. struct process_list_entry *entry_next;
  388. entry = TAILQ_FIRST(&plist->active_list);
  389. while (entry != NULL) {
  390. entry_next = TAILQ_NEXT(entry, entries);
  391. if (entry->state == PROCESS_LIST_ENTRY_STATE_INITIALIZED ||
  392. entry->state == PROCESS_LIST_ENTRY_STATE_FINISHED) {
  393. TAILQ_REMOVE(&plist->active_list, entry, entries);
  394. process_list_entry_free(entry);
  395. plist->allocated_list_entries--;
  396. } else {
  397. process_list_move_entry_to_kill_list(plist, entry);
  398. }
  399. entry = entry_next;
  400. }
  401. }
  402. static int
  403. process_list_process_kill_list_entry(struct process_list *plist, struct process_list_entry *entry)
  404. {
  405. int sig_to_send;
  406. enum process_list_entry_state new_state;
  407. int res;
  408. sig_to_send = 0;
  409. new_state = PROCESS_LIST_ENTRY_STATE_INITIALIZED;
  410. switch (entry->state) {
  411. case PROCESS_LIST_ENTRY_STATE_INITIALIZED:
  412. /*
  413. * This shouldn't happen. If it does, process_list_move_active_entries_to_kill_list
  414. * doesn't work as expected or there is some kind of memory corruption.
  415. */
  416. assert(entry->state != PROCESS_LIST_ENTRY_STATE_INITIALIZED);
  417. break;
  418. case PROCESS_LIST_ENTRY_STATE_FINISHED:
  419. /*
  420. * This shouldn't happen. If it does, process_list_waitpid
  421. * doesn't work as expected or there is some kind of memory corruption.
  422. */
  423. assert(entry->state != PROCESS_LIST_ENTRY_STATE_FINISHED);
  424. break;
  425. case PROCESS_LIST_ENTRY_STATE_RUNNING:
  426. sig_to_send = SIGTERM;
  427. new_state = PROCESS_LIST_ENTRY_STATE_SIGTERM_SENT;
  428. break;
  429. case PROCESS_LIST_ENTRY_STATE_SIGTERM_SENT:
  430. sig_to_send = SIGKILL;
  431. new_state = PROCESS_LIST_ENTRY_STATE_SIGKILL_SENT;
  432. break;
  433. case PROCESS_LIST_ENTRY_STATE_SIGKILL_SENT:
  434. sig_to_send = SIGKILL;
  435. new_state = PROCESS_LIST_ENTRY_STATE_SIGKILL_SENT;
  436. break;
  437. }
  438. res = 0;
  439. if (kill(entry->pid, sig_to_send) == -1) {
  440. if (errno == EPERM || errno == EINVAL) {
  441. res = -1;
  442. }
  443. }
  444. entry->state = new_state;
  445. return (res);
  446. }
  447. int
  448. process_list_process_kill_list(struct process_list *plist)
  449. {
  450. struct process_list_entry *entry;
  451. if (process_list_waitpid(plist) != 0) {
  452. return (-1);
  453. }
  454. TAILQ_FOREACH(entry, &plist->to_kill_list, entries) {
  455. if (process_list_process_kill_list_entry(plist, entry) != 0) {
  456. return (-1);
  457. }
  458. }
  459. return (0);
  460. }
  461. size_t
  462. process_list_get_kill_list_items(struct process_list *plist)
  463. {
  464. struct process_list_entry *entry;
  465. size_t res;
  466. res = 0;
  467. TAILQ_FOREACH(entry, &plist->to_kill_list, entries) {
  468. res++;
  469. }
  470. return (res);
  471. }
  472. int
  473. process_list_killall(struct process_list *plist, uint32_t timeout)
  474. {
  475. uint32_t action_timeout;
  476. int i;
  477. process_list_move_active_entries_to_kill_list(plist);
  478. action_timeout = timeout / 10;
  479. if (action_timeout < 1) {
  480. action_timeout = 1;
  481. }
  482. for (i = 0; i < 10; i++) {
  483. /*
  484. * Make sure all process got signal (quick phase)
  485. */
  486. if (process_list_process_kill_list(plist) != 0) {
  487. return (-1);
  488. }
  489. }
  490. for (i = 0; i < 10 && process_list_get_kill_list_items(plist) > 0; i++) {
  491. if (process_list_process_kill_list(plist) != 0) {
  492. return (-1);
  493. }
  494. poll(NULL, 0, action_timeout);
  495. }
  496. if (process_list_get_kill_list_items(plist) > 0) {
  497. return (-1);
  498. }
  499. return (0);
  500. }