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