BProcess.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 <sys/stat.h>
  34. #include <fcntl.h>
  35. #include <misc/offset.h>
  36. #include <misc/open_standard_streams.h>
  37. #include <base/BLog.h>
  38. #include "BProcess.h"
  39. #include <generated/blog_channel_BProcess.h>
  40. static void call_handler (BProcess *o, int normally, uint8_t normally_exit_status)
  41. {
  42. DEBUGERROR(&o->d_err, o->handler(o->user, normally, normally_exit_status))
  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. static int fds_contains (const int *fds, int fd, size_t *pos)
  137. {
  138. for (size_t i = 0; fds[i] >= 0; i++) {
  139. if (fds[i] == fd) {
  140. if (pos) {
  141. *pos = i;
  142. }
  143. return 1;
  144. }
  145. }
  146. return 0;
  147. }
  148. 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)
  149. {
  150. // init arguments
  151. o->m = m;
  152. o->handler = handler;
  153. o->user = user;
  154. // count fds
  155. size_t num_fds;
  156. for (num_fds = 0; fds[num_fds] >= 0; num_fds++);
  157. // block signals
  158. // needed to prevent parent's signal handlers from being called
  159. // in the child
  160. sigset_t sset_all;
  161. sigfillset(&sset_all);
  162. sigset_t sset_old;
  163. if (sigprocmask(SIG_SETMASK, &sset_all, &sset_old) < 0) {
  164. BLog(BLOG_ERROR, "sigprocmask failed");
  165. goto fail0;
  166. }
  167. // fork
  168. pid_t pid = fork();
  169. if (pid == 0) {
  170. // this is child
  171. // restore signal dispositions
  172. for (int i = 1; i < NSIG; i++) {
  173. struct sigaction sa;
  174. memset(&sa, 0, sizeof(sa));
  175. sa.sa_handler = SIG_DFL;
  176. sa.sa_flags = 0;
  177. sigaction(i, &sa, NULL);
  178. }
  179. // unblock signals
  180. sigset_t sset_none;
  181. sigemptyset(&sset_none);
  182. if (sigprocmask(SIG_SETMASK, &sset_none, NULL) < 0) {
  183. abort();
  184. }
  185. // copy fds array
  186. int *fds2 = malloc((num_fds + 1) * sizeof(fds2[0]));
  187. if (!fds2) {
  188. abort();
  189. }
  190. memcpy(fds2, fds, (num_fds + 1) * sizeof(fds2[0]));
  191. // find maximum file descriptors
  192. int max_fd = sysconf(_SC_OPEN_MAX);
  193. if (max_fd < 0) {
  194. abort();
  195. }
  196. // close file descriptors
  197. for (int i = 0; i < max_fd; i++) {
  198. // if it's one of the given fds, don't close it
  199. if (fds_contains(fds2, i, NULL)) {
  200. continue;
  201. }
  202. close(i);
  203. }
  204. const int *orig_fds_map = fds_map;
  205. // map fds to requested fd numbers
  206. while (*fds2 >= 0) {
  207. // resolve possible conflict
  208. size_t cpos;
  209. if (fds_contains(fds2 + 1, *fds_map, &cpos)) {
  210. // dup() the fd to a new number; the old one will be closed
  211. // in the following dup2()
  212. if ((fds2[1 + cpos] = dup(fds2[1 + cpos])) < 0) {
  213. abort();
  214. }
  215. }
  216. if (*fds2 != *fds_map) {
  217. // dup fd
  218. if (dup2(*fds2, *fds_map) < 0) {
  219. abort();
  220. }
  221. // close original fd
  222. close(*fds2);
  223. }
  224. fds2++;
  225. fds_map++;
  226. }
  227. // make sure standard streams are open
  228. open_standard_streams();
  229. // assume identity of username, if requested
  230. if (username) {
  231. long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
  232. if (bufsize < 0) {
  233. bufsize = 16384;
  234. }
  235. char *buf = malloc(bufsize);
  236. if (!buf) {
  237. abort();
  238. }
  239. struct passwd pwd;
  240. struct passwd *res;
  241. getpwnam_r(username, &pwd, buf, bufsize, &res);
  242. if (!res) {
  243. abort();
  244. }
  245. if (initgroups(username, pwd.pw_gid) < 0) {
  246. abort();
  247. }
  248. if (setgid(pwd.pw_gid) < 0) {
  249. abort();
  250. }
  251. if (setuid(pwd.pw_uid) < 0) {
  252. abort();
  253. }
  254. }
  255. execv(file, argv);
  256. abort();
  257. }
  258. // restore original signal mask
  259. ASSERT_FORCE(sigprocmask(SIG_SETMASK, &sset_old, NULL) == 0)
  260. if (pid < 0) {
  261. BLog(BLOG_ERROR, "fork failed");
  262. goto fail0;
  263. }
  264. // remember pid
  265. o->pid = pid;
  266. // add to processes list
  267. LinkedList2_Append(&o->m->processes, &o->list_node);
  268. DebugObject_Init(&o->d_obj);
  269. DebugError_Init(&o->d_err, BReactor_PendingGroup(m->reactor));
  270. return 1;
  271. fail0:
  272. return 0;
  273. }
  274. int BProcess_Init (BProcess *o, BProcessManager *m, BProcess_handler handler, void *user, const char *file, char *const argv[], const char *username)
  275. {
  276. int fds[] = {-1};
  277. return BProcess_InitWithFds(o, m, handler, user, file, argv, username, fds, NULL);
  278. }
  279. void BProcess_Free (BProcess *o)
  280. {
  281. DebugError_Free(&o->d_err);
  282. DebugObject_Free(&o->d_obj);
  283. // remove from processes list
  284. LinkedList2_Remove(&o->m->processes, &o->list_node);
  285. }
  286. int BProcess_Terminate (BProcess *o)
  287. {
  288. DebugObject_Access(&o->d_obj);
  289. DebugError_AssertNoError(&o->d_err);
  290. ASSERT(o->pid > 0)
  291. if (kill(o->pid, SIGTERM) < 0) {
  292. BLog(BLOG_ERROR, "kill(%"PRIiMAX", SIGTERM) failed", (intmax_t)o->pid);
  293. return 0;
  294. }
  295. return 1;
  296. }
  297. int BProcess_Kill (BProcess *o)
  298. {
  299. DebugObject_Access(&o->d_obj);
  300. DebugError_AssertNoError(&o->d_err);
  301. ASSERT(o->pid > 0)
  302. if (kill(o->pid, SIGKILL) < 0) {
  303. BLog(BLOG_ERROR, "kill(%"PRIiMAX", SIGKILL) failed", (intmax_t)o->pid);
  304. return 0;
  305. }
  306. return 1;
  307. }