spawn.cpp 9.3 KB

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