BProcess.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 <string.h>
  24. #include <inttypes.h>
  25. #include <stdlib.h>
  26. #include <sys/types.h>
  27. #include <sys/wait.h>
  28. #include <unistd.h>
  29. #include <signal.h>
  30. #include <grp.h>
  31. #include <pwd.h>
  32. #include <errno.h>
  33. #include <misc/offset.h>
  34. #include <base/BLog.h>
  35. #include "BProcess.h"
  36. #include <generated/blog_channel_BProcess.h>
  37. static void call_handler (BProcess *o, int normally, uint8_t normally_exit_status)
  38. {
  39. DEBUGERROR(&o->d_err, o->handler(o->user, normally, normally_exit_status))
  40. }
  41. static BProcess * find_process (BProcessManager *o, pid_t pid)
  42. {
  43. LinkedList2Iterator it;
  44. LinkedList2Iterator_InitForward(&it, &o->processes);
  45. LinkedList2Node *node;
  46. while (node = LinkedList2Iterator_Next(&it)) {
  47. BProcess *p = UPPER_OBJECT(node, BProcess, list_node);
  48. if (p->pid == pid) {
  49. LinkedList2Iterator_Free(&it);
  50. return p;
  51. }
  52. }
  53. return NULL;
  54. }
  55. static void work_signals (BProcessManager *o)
  56. {
  57. // read exit status with waitpid()
  58. int status;
  59. pid_t pid = waitpid(-1, &status, WNOHANG);
  60. if (pid <= 0) {
  61. return;
  62. }
  63. // schedule next waitpid
  64. BPending_Set(&o->wait_job);
  65. // find process
  66. BProcess *p = find_process(o, pid);
  67. if (!p) {
  68. BLog(BLOG_DEBUG, "unknown child %p");
  69. }
  70. if (WIFEXITED(status)) {
  71. uint8_t exit_status = WEXITSTATUS(status);
  72. BLog(BLOG_INFO, "child %"PRIiMAX" exited with status %"PRIu8, (intmax_t)pid, exit_status);
  73. if (p) {
  74. call_handler(p, 1, exit_status);
  75. return;
  76. }
  77. }
  78. else if (WIFSIGNALED(status)) {
  79. int signo = WTERMSIG(status);
  80. BLog(BLOG_INFO, "child %"PRIiMAX" exited with signal %d", (intmax_t)pid, signo);
  81. if (p) {
  82. call_handler(p, 0, 0);
  83. return;
  84. }
  85. }
  86. else {
  87. BLog(BLOG_ERROR, "unknown wait status type for pid %"PRIiMAX" (%d)", (intmax_t)pid, status);
  88. }
  89. }
  90. static void wait_job_handler (BProcessManager *o)
  91. {
  92. DebugObject_Access(&o->d_obj);
  93. work_signals(o);
  94. return;
  95. }
  96. static void signal_handler (BProcessManager *o, int signo)
  97. {
  98. ASSERT(signo == SIGCHLD)
  99. DebugObject_Access(&o->d_obj);
  100. work_signals(o);
  101. return;
  102. }
  103. int BProcessManager_Init (BProcessManager *o, BReactor *reactor)
  104. {
  105. // init arguments
  106. o->reactor = reactor;
  107. // init signal handling
  108. sigset_t sset;
  109. ASSERT_FORCE(sigemptyset(&sset) == 0)
  110. ASSERT_FORCE(sigaddset(&sset, SIGCHLD) == 0)
  111. if (!BUnixSignal_Init(&o->signal, o->reactor, sset, (BUnixSignal_handler)signal_handler, o)) {
  112. BLog(BLOG_ERROR, "BUnixSignal_Init failed");
  113. goto fail0;
  114. }
  115. // init processes list
  116. LinkedList2_Init(&o->processes);
  117. // init wait job
  118. BPending_Init(&o->wait_job, BReactor_PendingGroup(o->reactor), (BPending_handler)wait_job_handler, o);
  119. DebugObject_Init(&o->d_obj);
  120. return 1;
  121. fail0:
  122. return 0;
  123. }
  124. void BProcessManager_Free (BProcessManager *o)
  125. {
  126. ASSERT(LinkedList2_IsEmpty(&o->processes))
  127. DebugObject_Free(&o->d_obj);
  128. // free wait job
  129. BPending_Free(&o->wait_job);
  130. // free signal handling
  131. BUnixSignal_Free(&o->signal, 1);
  132. }
  133. static int fds_contains (const int *fds, int fd, size_t *pos)
  134. {
  135. for (size_t i = 0; fds[i] >= 0; i++) {
  136. if (fds[i] == fd) {
  137. if (pos) {
  138. *pos = i;
  139. }
  140. return 1;
  141. }
  142. }
  143. return 0;
  144. }
  145. int BProcess_InitWithFds (BProcess *o, BProcessManager *m, BProcess_handler handler, void *user, const char *file, char *const argv[], const char *username, const int *fds, const int *fds_map)
  146. {
  147. // init arguments
  148. o->m = m;
  149. o->handler = handler;
  150. o->user = user;
  151. // count fds
  152. size_t num_fds;
  153. for (num_fds = 0; fds[num_fds] >= 0; num_fds++);
  154. // block signals
  155. // needed to prevent parent's signal handlers from being called
  156. // in the child
  157. sigset_t sset_all;
  158. sigfillset(&sset_all);
  159. sigset_t sset_old;
  160. if (sigprocmask(SIG_SETMASK, &sset_all, &sset_old) < 0) {
  161. BLog(BLOG_ERROR, "sigprocmask failed");
  162. goto fail0;
  163. }
  164. // fork
  165. pid_t pid = fork();
  166. if (pid == 0) {
  167. // this is child
  168. // restore signal dispositions
  169. for (int i = 1; i < NSIG; i++) {
  170. struct sigaction sa;
  171. memset(&sa, 0, sizeof(sa));
  172. sa.sa_handler = SIG_DFL;
  173. sa.sa_flags = 0;
  174. sigaction(i, &sa, NULL);
  175. }
  176. // unblock signals
  177. sigset_t sset_none;
  178. sigemptyset(&sset_none);
  179. if (sigprocmask(SIG_SETMASK, &sset_none, NULL) < 0) {
  180. abort();
  181. }
  182. // copy fds array
  183. int *fds2 = malloc((num_fds + 1) * sizeof(fds2[0]));
  184. if (!fds2) {
  185. abort();
  186. }
  187. memcpy(fds2, fds, (num_fds + 1) * sizeof(fds2[0]));
  188. // find maximum file descriptors
  189. int max_fd = sysconf(_SC_OPEN_MAX);
  190. if (max_fd < 0) {
  191. abort();
  192. }
  193. // close file descriptors
  194. for (int i = 0; i < max_fd; i++) {
  195. // if it's one of the given fds, don't close it
  196. if (fds_contains(fds2, i, NULL)) {
  197. continue;
  198. }
  199. close(i);
  200. }
  201. // map fds to requested fd numbers
  202. while (*fds2 >= 0) {
  203. // resolve possible conflict
  204. size_t cpos;
  205. if (fds_contains(fds2 + 1, *fds_map, &cpos)) {
  206. // dup() the fd to a new number; the old one will be closed
  207. // in the following dup2()
  208. if ((fds2[1 + cpos] = dup(fds2[1 + cpos])) < 0) {
  209. abort();
  210. }
  211. }
  212. if (*fds2 != *fds_map) {
  213. // dup fd
  214. if (dup2(*fds2, *fds_map) < 0) {
  215. abort();
  216. }
  217. // close original fd
  218. close(*fds2);
  219. }
  220. fds2++;
  221. fds_map++;
  222. }
  223. // assume identity of username, if requested
  224. if (username) {
  225. long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
  226. if (bufsize < 0) {
  227. bufsize = 16384;
  228. }
  229. char *buf = malloc(bufsize);
  230. if (!buf) {
  231. abort();
  232. }
  233. struct passwd pwd;
  234. struct passwd *res;
  235. getpwnam_r(username, &pwd, buf, bufsize, &res);
  236. if (!res) {
  237. abort();
  238. }
  239. if (initgroups(username, pwd.pw_gid) < 0) {
  240. abort();
  241. }
  242. if (setgid(pwd.pw_gid) < 0) {
  243. abort();
  244. }
  245. if (setuid(pwd.pw_uid) < 0) {
  246. abort();
  247. }
  248. }
  249. execv(file, argv);
  250. abort();
  251. }
  252. // restore original signal mask
  253. ASSERT_FORCE(sigprocmask(SIG_SETMASK, &sset_old, NULL) == 0)
  254. if (pid < 0) {
  255. BLog(BLOG_ERROR, "fork failed");
  256. goto fail0;
  257. }
  258. // remember pid
  259. o->pid = pid;
  260. // add to processes list
  261. LinkedList2_Append(&o->m->processes, &o->list_node);
  262. DebugObject_Init(&o->d_obj);
  263. DebugError_Init(&o->d_err, BReactor_PendingGroup(m->reactor));
  264. return 1;
  265. fail0:
  266. return 0;
  267. }
  268. int BProcess_Init (BProcess *o, BProcessManager *m, BProcess_handler handler, void *user, const char *file, char *const argv[], const char *username)
  269. {
  270. int fds[] = {-1};
  271. return BProcess_InitWithFds(o, m, handler, user, file, argv, username, fds, NULL);
  272. }
  273. void BProcess_Free (BProcess *o)
  274. {
  275. DebugError_Free(&o->d_err);
  276. DebugObject_Free(&o->d_obj);
  277. // remove from processes list
  278. LinkedList2_Remove(&o->m->processes, &o->list_node);
  279. }
  280. int BProcess_Terminate (BProcess *o)
  281. {
  282. DebugObject_Access(&o->d_obj);
  283. DebugError_AssertNoError(&o->d_err);
  284. ASSERT(o->pid > 0)
  285. if (kill(o->pid, SIGTERM) < 0) {
  286. BLog(BLOG_ERROR, "kill(%"PRIiMAX", SIGTERM) failed", (intmax_t)o->pid);
  287. return 0;
  288. }
  289. return 1;
  290. }
  291. int BProcess_Kill (BProcess *o)
  292. {
  293. DebugObject_Access(&o->d_obj);
  294. DebugError_AssertNoError(&o->d_err);
  295. ASSERT(o->pid > 0)
  296. if (kill(o->pid, SIGKILL) < 0) {
  297. BLog(BLOG_ERROR, "kill(%"PRIiMAX", SIGKILL) failed", (intmax_t)o->pid);
  298. return 0;
  299. }
  300. return 1;
  301. }