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