BProcess.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /**
  2. * @file BProcess.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <stddef.h>
  23. #include <inttypes.h>
  24. #include <stdlib.h>
  25. #include <sys/types.h>
  26. #include <sys/wait.h>
  27. #include <unistd.h>
  28. #include <signal.h>
  29. #include <misc/offset.h>
  30. #include <system/BLog.h>
  31. #include <system/BProcess.h>
  32. #include <generated/blog_channel_BProcess.h>
  33. static void call_handler (BProcess *o, int normally, uint8_t normally_exit_status)
  34. {
  35. #ifndef NDEBUG
  36. DEAD_ENTER(o->d_dead)
  37. #endif
  38. o->handler(o->user, normally, normally_exit_status);
  39. #ifndef NDEBUG
  40. ASSERT(DEAD_KILLED)
  41. DEAD_LEAVE(o->d_dead);
  42. #endif
  43. }
  44. static BProcess * find_process (BProcessManager *o, pid_t pid)
  45. {
  46. LinkedList2Iterator it;
  47. LinkedList2Iterator_InitForward(&it, &o->processes);
  48. LinkedList2Node *node;
  49. while (node = LinkedList2Iterator_Next(&it)) {
  50. BProcess *p = UPPER_OBJECT(node, BProcess, list_node);
  51. if (p->pid == pid) {
  52. LinkedList2Iterator_Free(&it);
  53. return p;
  54. }
  55. }
  56. return NULL;
  57. }
  58. static void work_signals (BProcessManager *o)
  59. {
  60. // read exit status with waitpid()
  61. int status;
  62. pid_t pid = waitpid(-1, &status, WNOHANG);
  63. if (pid <= 0) {
  64. return;
  65. }
  66. // schedule next waitpid
  67. BPending_Set(&o->wait_job);
  68. // find process
  69. BProcess *p = find_process(o, pid);
  70. if (!p) {
  71. BLog(BLOG_DEBUG, "unknown child %p");
  72. }
  73. if (WIFEXITED(status)) {
  74. uint8_t exit_status = WEXITSTATUS(status);
  75. BLog(BLOG_INFO, "child %"PRIiMAX" exited with status %"PRIu8, (intmax_t)pid, exit_status);
  76. if (p) {
  77. call_handler(p, 1, exit_status);
  78. return;
  79. }
  80. }
  81. else if (WIFSIGNALED(status)) {
  82. int signo = WTERMSIG(status);
  83. BLog(BLOG_INFO, "child %"PRIiMAX" exited with signal %d", (intmax_t)pid, signo);
  84. if (p) {
  85. call_handler(p, 0, 0);
  86. return;
  87. }
  88. }
  89. else {
  90. BLog(BLOG_ERROR, "unknown wait status type for pid %"PRIiMAX" (%d)", (intmax_t)pid, status);
  91. }
  92. }
  93. static void wait_job_handler (BProcessManager *o)
  94. {
  95. DebugObject_Access(&o->d_obj);
  96. work_signals(o);
  97. return;
  98. }
  99. static void signal_handler (BProcessManager *o, int signo)
  100. {
  101. ASSERT(signo == SIGCHLD)
  102. DebugObject_Access(&o->d_obj);
  103. work_signals(o);
  104. return;
  105. }
  106. int BProcessManager_Init (BProcessManager *o, BReactor *reactor)
  107. {
  108. // init arguments
  109. o->reactor = reactor;
  110. // init signal handling
  111. sigset_t sset;
  112. ASSERT_FORCE(sigemptyset(&sset) == 0)
  113. ASSERT_FORCE(sigaddset(&sset, SIGCHLD) == 0)
  114. if (!BUnixSignal_Init(&o->signal, o->reactor, sset, (BUnixSignal_handler)signal_handler, o)) {
  115. BLog(BLOG_ERROR, "BUnixSignal_Init failed");
  116. goto fail0;
  117. }
  118. // init processes list
  119. LinkedList2_Init(&o->processes);
  120. // init wait job
  121. BPending_Init(&o->wait_job, BReactor_PendingGroup(o->reactor), (BPending_handler)wait_job_handler, o);
  122. DebugObject_Init(&o->d_obj);
  123. return 1;
  124. fail0:
  125. return 0;
  126. }
  127. void BProcessManager_Free (BProcessManager *o)
  128. {
  129. ASSERT(LinkedList2_IsEmpty(&o->processes))
  130. DebugObject_Free(&o->d_obj);
  131. // free wait job
  132. BPending_Free(&o->wait_job);
  133. // free signal handling
  134. BUnixSignal_Free(&o->signal, 1);
  135. }
  136. int BProcess_Init (BProcess *o, BProcessManager *m, BProcess_handler handler, void *user, const char *file, char *const argv[])
  137. {
  138. // init arguments
  139. o->m = m;
  140. o->handler = handler;
  141. o->user = user;
  142. // fork
  143. pid_t pid = fork();
  144. if (pid < 0) {
  145. BLog(BLOG_ERROR, "fork failed");
  146. goto fail0;
  147. }
  148. if (pid == 0) {
  149. // this is child
  150. int max_fd = sysconf(_SC_OPEN_MAX);
  151. if (max_fd < 0) {
  152. abort();
  153. }
  154. for (int i = 0; i < max_fd; i++) {
  155. close(i);
  156. }
  157. execv(file, argv);
  158. abort();
  159. }
  160. // remember pid
  161. o->pid = pid;
  162. // add to processes list
  163. LinkedList2_Append(&o->m->processes, &o->list_node);
  164. DebugObject_Init(&o->d_obj);
  165. #ifndef NDEBUG
  166. DEAD_INIT(o->d_dead);
  167. #endif
  168. return 1;
  169. fail0:
  170. return 0;
  171. }
  172. void BProcess_Free (BProcess *o)
  173. {
  174. DebugObject_Free(&o->d_obj);
  175. #ifndef NDEBUG
  176. DEAD_KILL(o->d_dead);
  177. #endif
  178. // remove from processes list
  179. LinkedList2_Remove(&o->m->processes, &o->list_node);
  180. }
  181. int BProcess_Terminate (BProcess *o)
  182. {
  183. DebugObject_Access(&o->d_obj);
  184. ASSERT(o->pid > 0)
  185. if (kill(o->pid, SIGTERM) < 0) {
  186. BLog(BLOG_ERROR, "kill(%"PRIiMAX", SIGTERM) failed", (intmax_t)o->pid);
  187. return 0;
  188. }
  189. return 1;
  190. }
  191. int BProcess_Kill (BProcess *o)
  192. {
  193. DebugObject_Access(&o->d_obj);
  194. ASSERT(o->pid > 0)
  195. if (kill(o->pid, SIGKILL) < 0) {
  196. BLog(BLOG_ERROR, "kill(%"PRIiMAX", SIGKILL) failed", (intmax_t)o->pid);
  197. return 0;
  198. }
  199. return 1;
  200. }