BProcess.c 6.5 KB

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