]> git.ipfire.org Git - people/ms/systemd.git/blame - execute.c
reset signal mask when forking
[people/ms/systemd.git] / execute.c
CommitLineData
5cb5a6ff
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3#include <assert.h>
034c6ed7
LP
4#include <dirent.h>
5#include <errno.h>
6#include <fcntl.h>
7#include <unistd.h>
44d8db9e 8#include <string.h>
309bff19 9#include <signal.h>
5cb5a6ff
LP
10
11#include "execute.h"
12#include "strv.h"
13#include "macro.h"
14#include "util.h"
acbb0225 15#include "log.h"
5cb5a6ff 16
034c6ed7
LP
17static int close_fds(int except[], unsigned n_except) {
18 DIR *d;
19 struct dirent *de;
20 int r = 0;
21
22 /* Modifies the fds array! (sorts it) */
23
24 if (!(d = opendir("/proc/self/fd")))
25 return -errno;
26
27 while ((de = readdir(d))) {
28 int fd;
29
30 if (de->d_name[0] == '.')
31 continue;
32
33 if ((r = safe_atoi(de->d_name, &fd)) < 0)
34 goto finish;
35
36 if (fd < 3)
37 continue;
38
39 if (fd == dirfd(d))
40 continue;
41
42 if (except) {
43 bool found;
44 unsigned i;
45
46 found = false;
47 for (i = 0; i < n_except; i++)
48 if (except[i] == fd) {
49 found = true;
50 break;
51 }
52
53 if (found)
54 continue;
55 }
56
57 if ((r = close_nointr(fd)) < 0)
58 goto finish;
59 }
60
61finish:
62 closedir(d);
63 return r;
64}
65
66static int shift_fds(int fds[], unsigned n_fds) {
67 int start, restart_from;
68
69 if (n_fds <= 0)
70 return 0;
71
72 assert(fds);
73
74 start = 0;
75 for (;;) {
76 int i;
77
78 restart_from = -1;
79
80 for (i = start; i < (int) n_fds; i++) {
81 int nfd;
82
83 /* Already at right index? */
84 if (fds[i] == i+3)
85 continue;
86
87 if ((nfd = fcntl(fds[i], F_DUPFD, i+3)) < 0)
88 return -errno;
89
2da3263a 90 assert_se(close_nointr(fds[i]) == 0);
034c6ed7
LP
91 fds[i] = nfd;
92
93 /* Hmm, the fd we wanted isn't free? Then
94 * let's remember that and try again from here*/
95 if (nfd != i+3 && restart_from < 0)
96 restart_from = i;
97 }
98
99 if (restart_from < 0)
100 break;
101
102 start = restart_from;
103 }
104
105 return 0;
106}
107
108int exec_spawn(const ExecCommand *command, const ExecContext *context, int *fds, unsigned n_fds, pid_t *ret) {
109 pid_t pid;
110
5cb5a6ff
LP
111 assert(command);
112 assert(context);
113 assert(ret);
034c6ed7
LP
114 assert(fds || n_fds <= 0);
115
2da3263a 116 log_debug("about to execute %s", command->path);
acbb0225 117
034c6ed7
LP
118 if ((pid = fork()) < 0)
119 return -errno;
120
121 if (pid == 0) {
122 char **e, **f = NULL;
123 int i, r;
124 char t[16];
309bff19
LP
125 sigset_t ss;
126
034c6ed7 127 /* child */
5cb5a6ff 128
309bff19
LP
129 if (sigemptyset(&ss) < 0 ||
130 sigprocmask(SIG_SETMASK, &ss, NULL) < 0) {
131 r = EXIT_SIGNAL_MASK;
132 goto fail;
133 }
134
034c6ed7
LP
135 umask(context->umask);
136
137 if (chdir(context->directory ? context->directory : "/") < 0) {
138 r = EXIT_CHDIR;
139 goto fail;
140 }
141
142 snprintf(t, sizeof(t), "%i", context->oom_adjust);
143 char_array_0(t);
144
145 if (write_one_line_file("/proc/self/oom_adj", t) < 0) {
146 r = EXIT_OOM_ADJUST;
147 goto fail;
148 }
149
150 if (setpriority(PRIO_PROCESS, 0, context->nice) < 0) {
151 r = EXIT_NICE;
152 goto fail;
153 }
154
155 if (close_fds(fds, n_fds) < 0 ||
156 shift_fds(fds, n_fds) < 0) {
157 r = EXIT_FDS;
158 goto fail;
159 }
160
161 for (i = 0; i < RLIMIT_NLIMITS; i++) {
162 if (!context->rlimit[i])
163 continue;
164
165 if (setrlimit(i, context->rlimit[i]) < 0) {
166 r = EXIT_LIMITS;
167 goto fail;
168 }
169 }
170
171 if (n_fds > 0) {
172 char a[64], b[64];
173 char *listen_env[3] = {
174 a,
175 b,
176 NULL
177 };
178
179 snprintf(a, sizeof(a), "LISTEN_PID=%llu", (unsigned long long) getpid());
180 snprintf(b, sizeof(b), "LISTEN_FDS=%u", n_fds);
181
182 a[sizeof(a)-1] = 0;
183 b[sizeof(b)-1] = 0;
184
185 if (context->environment) {
186 if (!(f = strv_merge(listen_env, context->environment))) {
187 r = EXIT_MEMORY;
188 goto fail;
189 }
190 e = f;
191 } else
192 e = listen_env;
193
194 } else
195 e = context->environment;
196
197 execve(command->path, command->argv, e);
198 r = EXIT_EXEC;
199
200 fail:
201 strv_free(f);
202 _exit(r);
203 }
204
2da3263a
LP
205
206 log_debug("executed %s as %llu", command->path, (unsigned long long) pid);
207
034c6ed7 208 *ret = pid;
5cb5a6ff
LP
209 return 0;
210}
211
034c6ed7
LP
212void exec_context_init(ExecContext *c) {
213 assert(c);
214
215 c->umask = 0002;
216 cap_clear(c->capabilities);
217 c->oom_adjust = 0;
218 c->nice = 0;
219}
220
221void exec_context_done(ExecContext *c) {
5cb5a6ff
LP
222 unsigned l;
223
224 assert(c);
225
226 strv_free(c->environment);
034c6ed7 227 c->environment = NULL;
5cb5a6ff 228
034c6ed7 229 for (l = 0; l < ELEMENTSOF(c->rlimit); l++) {
5cb5a6ff 230 free(c->rlimit[l]);
034c6ed7
LP
231 c->rlimit[l] = NULL;
232 }
233
234 free(c->directory);
235 c->directory = NULL;
5cb5a6ff 236
5cb5a6ff 237 free(c->user);
034c6ed7
LP
238 c->user = NULL;
239
5cb5a6ff 240 free(c->group);
034c6ed7
LP
241 c->group = NULL;
242
243 strv_free(c->supplementary_groups);
244 c->supplementary_groups = NULL;
5cb5a6ff
LP
245}
246
247void exec_command_free_list(ExecCommand *c) {
248 ExecCommand *i;
249
250 while ((i = c)) {
034c6ed7 251 LIST_REMOVE(ExecCommand, command, c, i);
5cb5a6ff
LP
252
253 free(i->path);
44d8db9e 254 strv_free(i->argv);
5cb5a6ff
LP
255 free(i);
256 }
257}
258
034c6ed7
LP
259void exec_command_free_array(ExecCommand **c, unsigned n) {
260 unsigned i;
261
262 for (i = 0; i < n; i++) {
263 exec_command_free_list(c[i]);
264 c[i] = NULL;
265 }
266}
267
5cb5a6ff
LP
268void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
269 assert(c);
270 assert(f);
271
272 if (!prefix)
273 prefix = "";
274
275 fprintf(f,
276 "%sUmask: %04o\n"
034c6ed7
LP
277 "%sDirectory: %s\n"
278 "%sNice: %i\n"
279 "%sOOMAdjust: %i\n",
5cb5a6ff 280 prefix, c->umask,
034c6ed7
LP
281 prefix, c->directory ? c->directory : "/",
282 prefix, c->nice,
283 prefix, c->oom_adjust);
5cb5a6ff
LP
284}
285
034c6ed7
LP
286void exec_status_fill(ExecStatus *s, pid_t pid, int code, int status) {
287 assert(s);
5cb5a6ff 288
034c6ed7
LP
289 s->pid = pid;
290 s->code = code;
291 s->status = status;
292 s->timestamp = now(CLOCK_REALTIME);
5cb5a6ff 293}
44d8db9e
LP
294
295char *exec_command_line(ExecCommand *c) {
296 size_t k;
297 char *n, *p, **a;
298 bool first = true;
299
300 assert(c);
301 assert(c->argv);
302
9164977d 303 k = 1;
44d8db9e
LP
304 STRV_FOREACH(a, c->argv)
305 k += strlen(*a)+3;
306
307 if (!(n = new(char, k)))
308 return NULL;
309
310 p = n;
311 STRV_FOREACH(a, c->argv) {
312
313 if (!first)
314 *(p++) = ' ';
315 else
316 first = false;
317
318 if (strpbrk(*a, WHITESPACE)) {
319 *(p++) = '\'';
320 p = stpcpy(p, *a);
321 *(p++) = '\'';
322 } else
323 p = stpcpy(p, *a);
324
325 }
326
9164977d
LP
327 *p = 0;
328
44d8db9e
LP
329 /* FIXME: this doesn't really handle arguments that have
330 * spaces and ticks in them */
331
332 return n;
333}
334
335void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) {
336 char *cmd;
337
338 assert(c);
339 assert(f);
340
341 if (!prefix)
342 prefix = "";
343
344 cmd = exec_command_line(c);
345
346 fprintf(f,
347 "%sCommand Line: %s\n",
348 prefix, cmd ? cmd : strerror(ENOMEM));
349
350 free(cmd);
351}
352
353void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix) {
354 assert(f);
355
356 if (!prefix)
357 prefix = "";
358
359 LIST_FOREACH(command, c, c)
360 exec_command_dump(c, f, prefix);
361}