spawn.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright (c) 2020, Nico Weber <thakis@chromium.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. /* posix_spawn and friends
  7. *
  8. * values from POSIX standard unix specification
  9. *
  10. * https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/spawn.h.html
  11. */
  12. #include <spawn.h>
  13. #include <AK/Function.h>
  14. #include <AK/Vector.h>
  15. #include <fcntl.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <sys/stat.h>
  19. #include <sys/types.h>
  20. #include <unistd.h>
  21. struct posix_spawn_file_actions_state {
  22. Vector<Function<int()>, 4> actions;
  23. };
  24. extern "C" {
  25. [[noreturn]] static void posix_spawn_child(const char* path, const posix_spawn_file_actions_t* file_actions, const posix_spawnattr_t* attr, char* const argv[], char* const envp[], int (*exec)(const char*, char* const[], char* const[]))
  26. {
  27. if (attr) {
  28. short flags = attr->flags;
  29. if (flags & POSIX_SPAWN_RESETIDS) {
  30. if (seteuid(getuid()) < 0) {
  31. perror("posix_spawn seteuid");
  32. _exit(127);
  33. }
  34. if (setegid(getgid()) < 0) {
  35. perror("posix_spawn setegid");
  36. _exit(127);
  37. }
  38. }
  39. if (flags & POSIX_SPAWN_SETPGROUP) {
  40. if (setpgid(0, attr->pgroup) < 0) {
  41. perror("posix_spawn setpgid");
  42. _exit(127);
  43. }
  44. }
  45. if (flags & POSIX_SPAWN_SETSCHEDPARAM) {
  46. if (sched_setparam(0, &attr->schedparam) < 0) {
  47. perror("posix_spawn sched_setparam");
  48. _exit(127);
  49. }
  50. }
  51. if (flags & POSIX_SPAWN_SETSIGDEF) {
  52. struct sigaction default_action;
  53. default_action.sa_flags = 0;
  54. sigemptyset(&default_action.sa_mask);
  55. default_action.sa_handler = SIG_DFL;
  56. sigset_t sigdefault = attr->sigdefault;
  57. for (int i = 0; i < NSIG; ++i) {
  58. if (sigismember(&sigdefault, i) && sigaction(i, &default_action, nullptr) < 0) {
  59. perror("posix_spawn sigaction");
  60. _exit(127);
  61. }
  62. }
  63. }
  64. if (flags & POSIX_SPAWN_SETSIGMASK) {
  65. if (sigprocmask(SIG_SETMASK, &attr->sigmask, nullptr) < 0) {
  66. perror("posix_spawn sigprocmask");
  67. _exit(127);
  68. }
  69. }
  70. if (flags & POSIX_SPAWN_SETSID) {
  71. if (setsid() < 0) {
  72. perror("posix_spawn setsid");
  73. _exit(127);
  74. }
  75. }
  76. // FIXME: POSIX_SPAWN_SETSCHEDULER
  77. }
  78. if (file_actions) {
  79. for (const auto& action : file_actions->state->actions) {
  80. if (action() < 0) {
  81. perror("posix_spawn file action");
  82. _exit(127);
  83. }
  84. }
  85. }
  86. exec(path, argv, envp);
  87. perror("posix_spawn exec");
  88. _exit(127);
  89. }
  90. int posix_spawn(pid_t* out_pid, const char* path, const posix_spawn_file_actions_t* file_actions, const posix_spawnattr_t* attr, char* const argv[], char* const envp[])
  91. {
  92. pid_t child_pid = fork();
  93. if (child_pid < 0)
  94. return errno;
  95. if (child_pid != 0) {
  96. *out_pid = child_pid;
  97. return 0;
  98. }
  99. posix_spawn_child(path, file_actions, attr, argv, envp, execve);
  100. }
  101. int posix_spawnp(pid_t* out_pid, const char* path, const posix_spawn_file_actions_t* file_actions, const posix_spawnattr_t* attr, char* const argv[], char* const envp[])
  102. {
  103. pid_t child_pid = fork();
  104. if (child_pid < 0)
  105. return errno;
  106. if (child_pid != 0) {
  107. *out_pid = child_pid;
  108. return 0;
  109. }
  110. posix_spawn_child(path, file_actions, attr, argv, envp, execvpe);
  111. }
  112. int posix_spawn_file_actions_addchdir(posix_spawn_file_actions_t* actions, const char* path)
  113. {
  114. actions->state->actions.append([path]() { return chdir(path); });
  115. return 0;
  116. }
  117. int posix_spawn_file_actions_addfchdir(posix_spawn_file_actions_t* actions, int fd)
  118. {
  119. actions->state->actions.append([fd]() { return fchdir(fd); });
  120. return 0;
  121. }
  122. int posix_spawn_file_actions_addclose(posix_spawn_file_actions_t* actions, int fd)
  123. {
  124. actions->state->actions.append([fd]() { return close(fd); });
  125. return 0;
  126. }
  127. int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t* actions, int old_fd, int new_fd)
  128. {
  129. actions->state->actions.append([old_fd, new_fd]() { return dup2(old_fd, new_fd); });
  130. return 0;
  131. }
  132. int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t* actions, int want_fd, const char* path, int flags, mode_t mode)
  133. {
  134. actions->state->actions.append([want_fd, path, flags, mode]() {
  135. int opened_fd = open(path, flags, mode);
  136. if (opened_fd < 0 || opened_fd == want_fd)
  137. return opened_fd;
  138. if (int rc = dup2(opened_fd, want_fd); rc < 0)
  139. return rc;
  140. return close(opened_fd);
  141. });
  142. return 0;
  143. }
  144. int posix_spawn_file_actions_destroy(posix_spawn_file_actions_t* actions)
  145. {
  146. delete actions->state;
  147. return 0;
  148. }
  149. int posix_spawn_file_actions_init(posix_spawn_file_actions_t* actions)
  150. {
  151. actions->state = new posix_spawn_file_actions_state;
  152. return 0;
  153. }
  154. int posix_spawnattr_destroy(posix_spawnattr_t*)
  155. {
  156. return 0;
  157. }
  158. int posix_spawnattr_getflags(const posix_spawnattr_t* attr, short* out_flags)
  159. {
  160. *out_flags = attr->flags;
  161. return 0;
  162. }
  163. int posix_spawnattr_getpgroup(const posix_spawnattr_t* attr, pid_t* out_pgroup)
  164. {
  165. *out_pgroup = attr->pgroup;
  166. return 0;
  167. }
  168. int posix_spawnattr_getschedparam(const posix_spawnattr_t* attr, struct sched_param* out_schedparam)
  169. {
  170. *out_schedparam = attr->schedparam;
  171. return 0;
  172. }
  173. int posix_spawnattr_getschedpolicy(const posix_spawnattr_t* attr, int* out_schedpolicty)
  174. {
  175. *out_schedpolicty = attr->schedpolicy;
  176. return 0;
  177. }
  178. int posix_spawnattr_getsigdefault(const posix_spawnattr_t* attr, sigset_t* out_sigdefault)
  179. {
  180. *out_sigdefault = attr->sigdefault;
  181. return 0;
  182. }
  183. int posix_spawnattr_getsigmask(const posix_spawnattr_t* attr, sigset_t* out_sigmask)
  184. {
  185. *out_sigmask = attr->sigmask;
  186. return 0;
  187. }
  188. int posix_spawnattr_init(posix_spawnattr_t* attr)
  189. {
  190. attr->flags = 0;
  191. attr->pgroup = 0;
  192. // attr->schedparam intentionally not written; its default value is unspecified.
  193. // attr->schedpolicy intentionally not written; its default value is unspecified.
  194. sigemptyset(&attr->sigdefault);
  195. // attr->sigmask intentionally not written; its default value is unspecified.
  196. return 0;
  197. }
  198. int posix_spawnattr_setflags(posix_spawnattr_t* attr, short flags)
  199. {
  200. if (flags & ~(POSIX_SPAWN_RESETIDS | POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER | POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSID))
  201. return EINVAL;
  202. attr->flags = flags;
  203. return 0;
  204. }
  205. int posix_spawnattr_setpgroup(posix_spawnattr_t* attr, pid_t pgroup)
  206. {
  207. attr->pgroup = pgroup;
  208. return 0;
  209. }
  210. int posix_spawnattr_setschedparam(posix_spawnattr_t* attr, const struct sched_param* schedparam)
  211. {
  212. attr->schedparam = *schedparam;
  213. return 0;
  214. }
  215. int posix_spawnattr_setschedpolicy(posix_spawnattr_t* attr, int schedpolicy)
  216. {
  217. attr->schedpolicy = schedpolicy;
  218. return 0;
  219. }
  220. int posix_spawnattr_setsigdefault(posix_spawnattr_t* attr, const sigset_t* sigdefault)
  221. {
  222. attr->sigdefault = *sigdefault;
  223. return 0;
  224. }
  225. int posix_spawnattr_setsigmask(posix_spawnattr_t* attr, const sigset_t* sigmask)
  226. {
  227. attr->sigmask = *sigmask;
  228. return 0;
  229. }
  230. }