spawn.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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(char const* path, posix_spawn_file_actions_t const* file_actions, posix_spawnattr_t const* attr, char* const argv[], char* const envp[], int (*exec)(char const*, 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 (auto const& 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. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn.html
  92. int posix_spawn(pid_t* out_pid, char const* path, posix_spawn_file_actions_t const* file_actions, posix_spawnattr_t const* attr, char* const argv[], char* const envp[])
  93. {
  94. pid_t child_pid = fork();
  95. if (child_pid < 0)
  96. return errno;
  97. if (child_pid != 0) {
  98. *out_pid = child_pid;
  99. return 0;
  100. }
  101. posix_spawn_child(path, file_actions, attr, argv, envp, execve);
  102. }
  103. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnp.html
  104. int posix_spawnp(pid_t* out_pid, char const* path, posix_spawn_file_actions_t const* file_actions, posix_spawnattr_t const* attr, char* const argv[], char* const envp[])
  105. {
  106. pid_t child_pid = fork();
  107. if (child_pid < 0)
  108. return errno;
  109. if (child_pid != 0) {
  110. *out_pid = child_pid;
  111. return 0;
  112. }
  113. posix_spawn_child(path, file_actions, attr, argv, envp, execvpe);
  114. }
  115. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_addchdir.html
  116. int posix_spawn_file_actions_addchdir(posix_spawn_file_actions_t* actions, char const* path)
  117. {
  118. actions->state->actions.append([path]() { return chdir(path); });
  119. return 0;
  120. }
  121. int posix_spawn_file_actions_addfchdir(posix_spawn_file_actions_t* actions, int fd)
  122. {
  123. actions->state->actions.append([fd]() { return fchdir(fd); });
  124. return 0;
  125. }
  126. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_addclose.html
  127. int posix_spawn_file_actions_addclose(posix_spawn_file_actions_t* actions, int fd)
  128. {
  129. actions->state->actions.append([fd]() { return close(fd); });
  130. return 0;
  131. }
  132. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_adddup2.html
  133. int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t* actions, int old_fd, int new_fd)
  134. {
  135. actions->state->actions.append([old_fd, new_fd]() { return dup2(old_fd, new_fd); });
  136. return 0;
  137. }
  138. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_addopen.html
  139. int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t* actions, int want_fd, char const* path, int flags, mode_t mode)
  140. {
  141. actions->state->actions.append([want_fd, path, flags, mode]() {
  142. int opened_fd = open(path, flags, mode);
  143. if (opened_fd < 0 || opened_fd == want_fd)
  144. return opened_fd;
  145. if (int rc = dup2(opened_fd, want_fd); rc < 0)
  146. return rc;
  147. return close(opened_fd);
  148. });
  149. return 0;
  150. }
  151. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_destroy.html
  152. int posix_spawn_file_actions_destroy(posix_spawn_file_actions_t* actions)
  153. {
  154. delete actions->state;
  155. return 0;
  156. }
  157. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_init.html
  158. int posix_spawn_file_actions_init(posix_spawn_file_actions_t* actions)
  159. {
  160. actions->state = new posix_spawn_file_actions_state;
  161. return 0;
  162. }
  163. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_destroy.html
  164. int posix_spawnattr_destroy(posix_spawnattr_t*)
  165. {
  166. return 0;
  167. }
  168. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getflags.html
  169. int posix_spawnattr_getflags(posix_spawnattr_t const* attr, short* out_flags)
  170. {
  171. *out_flags = attr->flags;
  172. return 0;
  173. }
  174. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getpgroup.html
  175. int posix_spawnattr_getpgroup(posix_spawnattr_t const* attr, pid_t* out_pgroup)
  176. {
  177. *out_pgroup = attr->pgroup;
  178. return 0;
  179. }
  180. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getschedparam.html
  181. int posix_spawnattr_getschedparam(posix_spawnattr_t const* attr, struct sched_param* out_schedparam)
  182. {
  183. *out_schedparam = attr->schedparam;
  184. return 0;
  185. }
  186. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getschedpolicy.html
  187. int posix_spawnattr_getschedpolicy(posix_spawnattr_t const* attr, int* out_schedpolicy)
  188. {
  189. *out_schedpolicy = attr->schedpolicy;
  190. return 0;
  191. }
  192. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getsigdefault.html
  193. int posix_spawnattr_getsigdefault(posix_spawnattr_t const* attr, sigset_t* out_sigdefault)
  194. {
  195. *out_sigdefault = attr->sigdefault;
  196. return 0;
  197. }
  198. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getsigmask.html
  199. int posix_spawnattr_getsigmask(posix_spawnattr_t const* attr, sigset_t* out_sigmask)
  200. {
  201. *out_sigmask = attr->sigmask;
  202. return 0;
  203. }
  204. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_init.html
  205. int posix_spawnattr_init(posix_spawnattr_t* attr)
  206. {
  207. attr->flags = 0;
  208. attr->pgroup = 0;
  209. // attr->schedparam intentionally not written; its default value is unspecified.
  210. // attr->schedpolicy intentionally not written; its default value is unspecified.
  211. sigemptyset(&attr->sigdefault);
  212. // attr->sigmask intentionally not written; its default value is unspecified.
  213. return 0;
  214. }
  215. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_setflags.html
  216. int posix_spawnattr_setflags(posix_spawnattr_t* attr, short flags)
  217. {
  218. if (flags & ~(POSIX_SPAWN_RESETIDS | POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER | POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSID))
  219. return EINVAL;
  220. attr->flags = flags;
  221. return 0;
  222. }
  223. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_setpgroup.html
  224. int posix_spawnattr_setpgroup(posix_spawnattr_t* attr, pid_t pgroup)
  225. {
  226. attr->pgroup = pgroup;
  227. return 0;
  228. }
  229. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_setschedparam.html
  230. int posix_spawnattr_setschedparam(posix_spawnattr_t* attr, const struct sched_param* schedparam)
  231. {
  232. attr->schedparam = *schedparam;
  233. return 0;
  234. }
  235. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_setschedpolicy.html
  236. int posix_spawnattr_setschedpolicy(posix_spawnattr_t* attr, int schedpolicy)
  237. {
  238. attr->schedpolicy = schedpolicy;
  239. return 0;
  240. }
  241. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_setsigdefault.html
  242. int posix_spawnattr_setsigdefault(posix_spawnattr_t* attr, sigset_t const* sigdefault)
  243. {
  244. attr->sigdefault = *sigdefault;
  245. return 0;
  246. }
  247. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_setsigmask.html
  248. int posix_spawnattr_setsigmask(posix_spawnattr_t* attr, sigset_t const* sigmask)
  249. {
  250. attr->sigmask = *sigmask;
  251. return 0;
  252. }
  253. }