BProcess.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 void signal_handler (BProcessManager *o, struct BUnixSignal_siginfo siginfo)
  45. {
  46. ASSERT(siginfo.signo == SIGCHLD)
  47. DebugObject_Access(&o->d_obj);
  48. // find process
  49. BProcess *p = NULL;
  50. LinkedList2Iterator it;
  51. LinkedList2Iterator_InitForward(&it, &o->processes);
  52. LinkedList2Node *node;
  53. while (node = LinkedList2Iterator_Next(&it)) {
  54. BProcess *this_p = UPPER_OBJECT(node, BProcess, list_node);
  55. if (this_p->pid == siginfo.pid) {
  56. p = this_p;
  57. LinkedList2Iterator_Free(&it);
  58. break;
  59. }
  60. }
  61. if (!p) {
  62. BLog(BLOG_ERROR, "got SIGCHLD for unknown pid");
  63. return;
  64. }
  65. // read exit status with waitpid()
  66. int status;
  67. pid_t res = waitpid(p->pid, &status, WNOHANG);
  68. if (res < 0) {
  69. BLog(BLOG_ERROR, "waitpid(%"PRIiMAX") failed", (intmax_t)p->pid);
  70. return;
  71. }
  72. if (res == 0) {
  73. BLog(BLOG_ERROR, "waitpid(%"PRIiMAX") returned 0", (intmax_t)p->pid);
  74. return;
  75. }
  76. if (WIFEXITED(status)) {
  77. uint8_t exit_status = WEXITSTATUS(status);
  78. BLog(BLOG_INFO, "child %"PRIiMAX" exited with status %"PRIu8, (intmax_t)p->pid, exit_status);
  79. call_handler(p, 1, exit_status);
  80. return;
  81. }
  82. if (WIFSIGNALED(status)) {
  83. int signo = WTERMSIG(status);
  84. BLog(BLOG_INFO, "child %"PRIiMAX" exited with signal %d", (intmax_t)p->pid, signo);
  85. call_handler(p, 0, 0);
  86. return;
  87. }
  88. BLog(BLOG_ERROR, "unknown wait status type for pid %"PRIiMAX" (%d)", (intmax_t)p->pid, status);
  89. }
  90. int BProcessManager_Init (BProcessManager *o, BReactor *reactor)
  91. {
  92. // init arguments
  93. o->reactor = reactor;
  94. // init signal handling
  95. sigset_t sset;
  96. ASSERT_FORCE(sigemptyset(&sset) == 0)
  97. ASSERT_FORCE(sigaddset(&sset, SIGCHLD) == 0)
  98. if (!BUnixSignal_Init(&o->signal, o->reactor, sset, (BUnixSignal_handler)signal_handler, o)) {
  99. BLog(BLOG_ERROR, "BUnixSignal_Init failed");
  100. goto fail0;
  101. }
  102. // init processes list
  103. LinkedList2_Init(&o->processes);
  104. DebugObject_Init(&o->d_obj);
  105. return 1;
  106. fail0:
  107. return 0;
  108. }
  109. void BProcessManager_Free (BProcessManager *o)
  110. {
  111. ASSERT(LinkedList2_IsEmpty(&o->processes))
  112. DebugObject_Free(&o->d_obj);
  113. // free signal handling
  114. BUnixSignal_Free(&o->signal, 1);
  115. }
  116. int BProcess_Init (BProcess *o, BProcessManager *m, BProcess_handler handler, void *user, const char *file, char *const argv[])
  117. {
  118. // init arguments
  119. o->m = m;
  120. o->handler = handler;
  121. o->user = user;
  122. // fork
  123. pid_t pid = fork();
  124. if (pid < 0) {
  125. BLog(BLOG_ERROR, "fork failed");
  126. goto fail0;
  127. }
  128. if (pid == 0) {
  129. // this is child
  130. int max_fd = sysconf(_SC_OPEN_MAX);
  131. if (max_fd < 0) {
  132. abort();
  133. }
  134. for (int i = 0; i < max_fd; i++) {
  135. close(i);
  136. }
  137. execv(file, argv);
  138. abort();
  139. }
  140. // remember pid
  141. o->pid = pid;
  142. // add to processes list
  143. LinkedList2_Append(&o->m->processes, &o->list_node);
  144. DebugObject_Init(&o->d_obj);
  145. #ifndef NDEBUG
  146. DEAD_INIT(o->d_dead);
  147. #endif
  148. return 1;
  149. fail0:
  150. return 0;
  151. }
  152. void BProcess_Free (BProcess *o)
  153. {
  154. DebugObject_Free(&o->d_obj);
  155. #ifndef NDEBUG
  156. DEAD_KILL(o->d_dead);
  157. #endif
  158. // remove from processes list
  159. LinkedList2_Remove(&o->m->processes, &o->list_node);
  160. }
  161. int BProcess_Terminate (BProcess *o)
  162. {
  163. DebugObject_Access(&o->d_obj);
  164. ASSERT(o->pid > 0)
  165. if (kill(o->pid, SIGTERM) < 0) {
  166. BLog(BLOG_ERROR, "kill(%"PRIiMAX", SIGTERM) failed", (intmax_t)o->pid);
  167. return 0;
  168. }
  169. return 1;
  170. }
  171. int BProcess_Kill (BProcess *o)
  172. {
  173. DebugObject_Access(&o->d_obj);
  174. ASSERT(o->pid > 0)
  175. if (kill(o->pid, SIGKILL) < 0) {
  176. BLog(BLOG_ERROR, "kill(%"PRIiMAX", SIGKILL) failed", (intmax_t)o->pid);
  177. return 0;
  178. }
  179. return 1;
  180. }