spawn.cpp 7.3 KB

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