BProcess.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 <grp.h>
  30. #include <pwd.h>
  31. #include <errno.h>
  32. #include <misc/offset.h>
  33. #include <system/BLog.h>
  34. #include <system/BProcess.h>
  35. #include <generated/blog_channel_BProcess.h>
  36. static void call_handler (BProcess *o, int normally, uint8_t normally_exit_status)
  37. {
  38. #ifndef NDEBUG
  39. DEAD_ENTER(o->d_dead)
  40. #endif
  41. o->handler(o->user, normally, normally_exit_status);
  42. #ifndef NDEBUG
  43. ASSERT(DEAD_KILLED)
  44. DEAD_LEAVE(o->d_dead);
  45. #endif
  46. }
  47. static BProcess * find_process (BProcessManager *o, pid_t pid)
  48. {
  49. LinkedList2Iterator it;
  50. LinkedList2Iterator_InitForward(&it, &o->processes);
  51. LinkedList2Node *node;
  52. while (node = LinkedList2Iterator_Next(&it)) {
  53. BProcess *p = UPPER_OBJECT(node, BProcess, list_node);
  54. if (p->pid == pid) {
  55. LinkedList2Iterator_Free(&it);
  56. return p;
  57. }
  58. }
  59. return NULL;
  60. }
  61. static void work_signals (BProcessManager *o)
  62. {
  63. // read exit status with waitpid()
  64. int status;
  65. pid_t pid = waitpid(-1, &status, WNOHANG);
  66. if (pid <= 0) {
  67. return;
  68. }
  69. // schedule next waitpid
  70. BPending_Set(&o->wait_job);
  71. // find process
  72. BProcess *p = find_process(o, pid);
  73. if (!p) {
  74. BLog(BLOG_DEBUG, "unknown child %p");
  75. }
  76. if (WIFEXITED(status)) {
  77. uint8_t exit_status = WEXITSTATUS(status);
  78. BLog(BLOG_INFO, "child %"PRIiMAX" exited with status %"PRIu8, (intmax_t)pid, exit_status);
  79. if (p) {
  80. call_handler(p, 1, exit_status);
  81. return;
  82. }
  83. }
  84. else if (WIFSIGNALED(status)) {
  85. int signo = WTERMSIG(status);
  86. BLog(BLOG_INFO, "child %"PRIiMAX" exited with signal %d", (intmax_t)pid, signo);
  87. if (p) {
  88. call_handler(p, 0, 0);
  89. return;
  90. }
  91. }
  92. else {
  93. BLog(BLOG_ERROR, "unknown wait status type for pid %"PRIiMAX" (%d)", (intmax_t)pid, status);
  94. }
  95. }
  96. static void wait_job_handler (BProcessManager *o)
  97. {
  98. DebugObject_Access(&o->d_obj);
  99. work_signals(o);
  100. return;
  101. }
  102. static void signal_handler (BProcessManager *o, int signo)
  103. {
  104. ASSERT(signo == SIGCHLD)
  105. DebugObject_Access(&o->d_obj);
  106. work_signals(o);
  107. return;
  108. }
  109. int BProcessManager_Init (BProcessManager *o, BReactor *reactor)
  110. {
  111. // init arguments
  112. o->reactor = reactor;
  113. // init signal handling
  114. sigset_t sset;
  115. ASSERT_FORCE(sigemptyset(&sset) == 0)
  116. ASSERT_FORCE(sigaddset(&sset, SIGCHLD) == 0)
  117. if (!BUnixSignal_Init(&o->signal, o->reactor, sset, (BUnixSignal_handler)signal_handler, o)) {
  118. BLog(BLOG_ERROR, "BUnixSignal_Init failed");
  119. goto fail0;
  120. }
  121. // init processes list
  122. LinkedList2_Init(&o->processes);
  123. // init wait job
  124. BPending_Init(&o->wait_job, BReactor_PendingGroup(o->reactor), (BPending_handler)wait_job_handler, o);
  125. DebugObject_Init(&o->d_obj);
  126. return 1;
  127. fail0:
  128. return 0;
  129. }
  130. void BProcessManager_Free (BProcessManager *o)
  131. {
  132. ASSERT(LinkedList2_IsEmpty(&o->processes))
  133. DebugObject_Free(&o->d_obj);
  134. // free wait job
  135. BPending_Free(&o->wait_job);
  136. // free signal handling
  137. BUnixSignal_Free(&o->signal, 1);
  138. }
  139. int BProcess_Init (BProcess *o, BProcessManager *m, BProcess_handler handler, void *user, const char *file, char *const argv[], const char *username)
  140. {
  141. // init arguments
  142. o->m = m;
  143. o->handler = handler;
  144. o->user = user;
  145. // fork
  146. pid_t pid = fork();
  147. if (pid < 0) {
  148. BLog(BLOG_ERROR, "fork failed");
  149. goto fail0;
  150. }
  151. if (pid == 0) {
  152. // this is child
  153. // find maximum file descriptors
  154. int max_fd = sysconf(_SC_OPEN_MAX);
  155. if (max_fd < 0) {
  156. abort();
  157. }
  158. // close all file descriptors
  159. for (int i = 0; i < max_fd; i++) {
  160. close(i);
  161. }
  162. // assume identity of username, if requested
  163. if (username) {
  164. size_t bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
  165. if (bufsize < 0) {
  166. bufsize = 16384;
  167. }
  168. char *buf = malloc(bufsize);
  169. if (!buf) {
  170. abort();
  171. }
  172. struct passwd pwd;
  173. struct passwd *res;
  174. getpwnam_r(username, &pwd, buf, bufsize, &res);
  175. if (!res) {
  176. abort();
  177. }
  178. if (initgroups(username, pwd.pw_gid) < 0) {
  179. abort();
  180. }
  181. if (setgid(pwd.pw_gid) < 0) {
  182. abort();
  183. }
  184. if (setuid(pwd.pw_uid) < 0) {
  185. abort();
  186. }
  187. }
  188. execv(file, argv);
  189. abort();
  190. }
  191. // remember pid
  192. o->pid = pid;
  193. // add to processes list
  194. LinkedList2_Append(&o->m->processes, &o->list_node);
  195. DebugObject_Init(&o->d_obj);
  196. #ifndef NDEBUG
  197. DEAD_INIT(o->d_dead);
  198. #endif
  199. return 1;
  200. fail0:
  201. return 0;
  202. }
  203. void BProcess_Free (BProcess *o)
  204. {
  205. DebugObject_Free(&o->d_obj);
  206. #ifndef NDEBUG
  207. DEAD_KILL(o->d_dead);
  208. #endif
  209. // remove from processes list
  210. LinkedList2_Remove(&o->m->processes, &o->list_node);
  211. }
  212. int BProcess_Terminate (BProcess *o)
  213. {
  214. DebugObject_Access(&o->d_obj);
  215. ASSERT(o->pid > 0)
  216. if (kill(o->pid, SIGTERM) < 0) {
  217. BLog(BLOG_ERROR, "kill(%"PRIiMAX", SIGTERM) failed", (intmax_t)o->pid);
  218. return 0;
  219. }
  220. return 1;
  221. }
  222. int BProcess_Kill (BProcess *o)
  223. {
  224. DebugObject_Access(&o->d_obj);
  225. ASSERT(o->pid > 0)
  226. if (kill(o->pid, SIGKILL) < 0) {
  227. BLog(BLOG_ERROR, "kill(%"PRIiMAX", SIGKILL) failed", (intmax_t)o->pid);
  228. return 0;
  229. }
  230. return 1;
  231. }