BProcess.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 <system/BLog.h>
  35. #include <process/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. // fork
  155. pid_t pid = fork();
  156. if (pid < 0) {
  157. BLog(BLOG_ERROR, "fork failed");
  158. goto fail0;
  159. }
  160. if (pid == 0) {
  161. // this is child
  162. // copy fds array
  163. int *fds2 = malloc((num_fds + 1) * sizeof(fds2[0]));
  164. if (!fds2) {
  165. abort();
  166. }
  167. memcpy(fds2, fds, (num_fds + 1) * sizeof(fds2[0]));
  168. // find maximum file descriptors
  169. int max_fd = sysconf(_SC_OPEN_MAX);
  170. if (max_fd < 0) {
  171. abort();
  172. }
  173. // close file descriptors
  174. for (int i = 0; i < max_fd; i++) {
  175. // if it's one of the given fds, don't close it
  176. if (fds_contains(fds2, i, NULL)) {
  177. continue;
  178. }
  179. close(i);
  180. }
  181. // map fds to requested fd numbers
  182. while (*fds2 >= 0) {
  183. // resolve possible conflict
  184. size_t cpos;
  185. if (fds_contains(fds2 + 1, *fds_map, &cpos)) {
  186. // dup() the fd to a new number; the old one will be closed
  187. // in the following dup2()
  188. if ((fds2[1 + cpos] = dup(fds2[1 + cpos])) < 0) {
  189. abort();
  190. }
  191. }
  192. if (*fds2 != *fds_map) {
  193. // dup fd
  194. if (dup2(*fds2, *fds_map) < 0) {
  195. abort();
  196. }
  197. // close original fd
  198. close(*fds2);
  199. }
  200. fds2++;
  201. fds_map++;
  202. }
  203. // assume identity of username, if requested
  204. if (username) {
  205. long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
  206. if (bufsize < 0) {
  207. bufsize = 16384;
  208. }
  209. char *buf = malloc(bufsize);
  210. if (!buf) {
  211. abort();
  212. }
  213. struct passwd pwd;
  214. struct passwd *res;
  215. getpwnam_r(username, &pwd, buf, bufsize, &res);
  216. if (!res) {
  217. abort();
  218. }
  219. if (initgroups(username, pwd.pw_gid) < 0) {
  220. abort();
  221. }
  222. if (setgid(pwd.pw_gid) < 0) {
  223. abort();
  224. }
  225. if (setuid(pwd.pw_uid) < 0) {
  226. abort();
  227. }
  228. }
  229. execv(file, argv);
  230. abort();
  231. }
  232. // remember pid
  233. o->pid = pid;
  234. // add to processes list
  235. LinkedList2_Append(&o->m->processes, &o->list_node);
  236. DebugObject_Init(&o->d_obj);
  237. DebugError_Init(&o->d_err, BReactor_PendingGroup(m->reactor));
  238. return 1;
  239. fail0:
  240. return 0;
  241. }
  242. int BProcess_Init (BProcess *o, BProcessManager *m, BProcess_handler handler, void *user, const char *file, char *const argv[], const char *username)
  243. {
  244. int fds[] = {-1};
  245. return BProcess_InitWithFds(o, m, handler, user, file, argv, username, fds, NULL);
  246. }
  247. void BProcess_Free (BProcess *o)
  248. {
  249. DebugError_Free(&o->d_err);
  250. DebugObject_Free(&o->d_obj);
  251. // remove from processes list
  252. LinkedList2_Remove(&o->m->processes, &o->list_node);
  253. }
  254. int BProcess_Terminate (BProcess *o)
  255. {
  256. DebugObject_Access(&o->d_obj);
  257. ASSERT(o->pid > 0)
  258. if (kill(o->pid, SIGTERM) < 0) {
  259. BLog(BLOG_ERROR, "kill(%"PRIiMAX", SIGTERM) failed", (intmax_t)o->pid);
  260. return 0;
  261. }
  262. return 1;
  263. }
  264. int BProcess_Kill (BProcess *o)
  265. {
  266. DebugObject_Access(&o->d_obj);
  267. ASSERT(o->pid > 0)
  268. if (kill(o->pid, SIGKILL) < 0) {
  269. BLog(BLOG_ERROR, "kill(%"PRIiMAX", SIGKILL) failed", (intmax_t)o->pid);
  270. return 0;
  271. }
  272. return 1;
  273. }