4
0

process-list.c 14 KB

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