]>
Commit | Line | Data |
---|---|---|
d6c9574f | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
5cb5a6ff | 2 | |
a7334b09 LP |
3 | /*** |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
9 | under the terms of the GNU Lesser General Public License as published by |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
a7334b09 LP |
11 | (at your option) any later version. |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 16 | Lesser General Public License for more details. |
a7334b09 | 17 | |
5430f7f2 | 18 | You should have received a copy of the GNU Lesser General Public License |
a7334b09 LP |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
20 | ***/ | |
21 | ||
5cb5a6ff | 22 | #include <assert.h> |
034c6ed7 LP |
23 | #include <dirent.h> |
24 | #include <errno.h> | |
25 | #include <fcntl.h> | |
26 | #include <unistd.h> | |
44d8db9e | 27 | #include <string.h> |
309bff19 | 28 | #include <signal.h> |
071830ff LP |
29 | #include <sys/socket.h> |
30 | #include <sys/un.h> | |
94f04347 | 31 | #include <sys/prctl.h> |
66ee3769 | 32 | #include <linux/sched.h> |
451a074f LP |
33 | #include <sys/types.h> |
34 | #include <sys/stat.h> | |
81a2b7ce LP |
35 | #include <grp.h> |
36 | #include <pwd.h> | |
15ae422b | 37 | #include <sys/mount.h> |
25e870b5 | 38 | #include <linux/fs.h> |
dd6c17b1 | 39 | #include <linux/oom.h> |
f2b68789 | 40 | #include <sys/poll.h> |
8351ceae | 41 | #include <linux/seccomp-bpf.h> |
2bef10ab | 42 | #include <glob.h> |
5cb5a6ff | 43 | |
5b6319dc LP |
44 | #ifdef HAVE_PAM |
45 | #include <security/pam_appl.h> | |
46 | #endif | |
47 | ||
5cb5a6ff LP |
48 | #include "execute.h" |
49 | #include "strv.h" | |
50 | #include "macro.h" | |
d7832d2c | 51 | #include "capability.h" |
5cb5a6ff | 52 | #include "util.h" |
acbb0225 | 53 | #include "log.h" |
20ad4cfd | 54 | #include "sd-messages.h" |
9eba9da4 | 55 | #include "ioprio.h" |
94f04347 | 56 | #include "securebits.h" |
8e274523 | 57 | #include "cgroup.h" |
15ae422b | 58 | #include "namespace.h" |
df1f0afe | 59 | #include "tcpwrap.h" |
d06dacd0 | 60 | #include "exit-status.h" |
dd6c17b1 | 61 | #include "missing.h" |
169c1bda | 62 | #include "utmp-wtmp.h" |
f6a6225e | 63 | #include "def.h" |
ff01d048 | 64 | #include "loopback-setup.h" |
9eb977db | 65 | #include "path-util.h" |
8351ceae | 66 | #include "syscall-list.h" |
4d1a6904 | 67 | #include "env-util.h" |
a5c32cff | 68 | #include "fileio.h" |
5cb5a6ff | 69 | |
e056b01d | 70 | #define IDLE_TIMEOUT_USEC (5*USEC_PER_SEC) |
e6a26745 | 71 | |
02a51aba LP |
72 | /* This assumes there is a 'tty' group */ |
73 | #define TTY_MODE 0620 | |
74 | ||
034c6ed7 LP |
75 | static int shift_fds(int fds[], unsigned n_fds) { |
76 | int start, restart_from; | |
77 | ||
78 | if (n_fds <= 0) | |
79 | return 0; | |
80 | ||
a0d40ac5 LP |
81 | /* Modifies the fds array! (sorts it) */ |
82 | ||
034c6ed7 LP |
83 | assert(fds); |
84 | ||
85 | start = 0; | |
86 | for (;;) { | |
87 | int i; | |
88 | ||
89 | restart_from = -1; | |
90 | ||
91 | for (i = start; i < (int) n_fds; i++) { | |
92 | int nfd; | |
93 | ||
94 | /* Already at right index? */ | |
95 | if (fds[i] == i+3) | |
96 | continue; | |
97 | ||
98 | if ((nfd = fcntl(fds[i], F_DUPFD, i+3)) < 0) | |
99 | return -errno; | |
100 | ||
e1f5e051 | 101 | close_nointr_nofail(fds[i]); |
034c6ed7 LP |
102 | fds[i] = nfd; |
103 | ||
104 | /* Hmm, the fd we wanted isn't free? Then | |
105 | * let's remember that and try again from here*/ | |
106 | if (nfd != i+3 && restart_from < 0) | |
107 | restart_from = i; | |
108 | } | |
109 | ||
110 | if (restart_from < 0) | |
111 | break; | |
112 | ||
113 | start = restart_from; | |
114 | } | |
115 | ||
116 | return 0; | |
117 | } | |
118 | ||
c2748801 | 119 | static int flags_fds(const int fds[], unsigned n_fds, bool nonblock) { |
47a71eed | 120 | unsigned i; |
e2c76839 | 121 | int r; |
47a71eed LP |
122 | |
123 | if (n_fds <= 0) | |
124 | return 0; | |
125 | ||
126 | assert(fds); | |
127 | ||
451a074f | 128 | /* Drops/Sets O_NONBLOCK and FD_CLOEXEC from the file flags */ |
47a71eed LP |
129 | |
130 | for (i = 0; i < n_fds; i++) { | |
47a71eed | 131 | |
e2c76839 LP |
132 | if ((r = fd_nonblock(fds[i], nonblock)) < 0) |
133 | return r; | |
47a71eed | 134 | |
451a074f LP |
135 | /* We unconditionally drop FD_CLOEXEC from the fds, |
136 | * since after all we want to pass these fds to our | |
137 | * children */ | |
47a71eed | 138 | |
e2c76839 LP |
139 | if ((r = fd_cloexec(fds[i], false)) < 0) |
140 | return r; | |
47a71eed LP |
141 | } |
142 | ||
143 | return 0; | |
144 | } | |
145 | ||
80876c20 LP |
146 | static const char *tty_path(const ExecContext *context) { |
147 | assert(context); | |
148 | ||
149 | if (context->tty_path) | |
150 | return context->tty_path; | |
151 | ||
152 | return "/dev/console"; | |
153 | } | |
154 | ||
6ea832a2 LP |
155 | void exec_context_tty_reset(const ExecContext *context) { |
156 | assert(context); | |
157 | ||
158 | if (context->tty_vhangup) | |
159 | terminal_vhangup(tty_path(context)); | |
160 | ||
161 | if (context->tty_reset) | |
162 | reset_terminal(tty_path(context)); | |
163 | ||
164 | if (context->tty_vt_disallocate && context->tty_path) | |
165 | vt_disallocate(context->tty_path); | |
166 | } | |
167 | ||
80876c20 LP |
168 | static int open_null_as(int flags, int nfd) { |
169 | int fd, r; | |
071830ff | 170 | |
80876c20 | 171 | assert(nfd >= 0); |
071830ff | 172 | |
80876c20 | 173 | if ((fd = open("/dev/null", flags|O_NOCTTY)) < 0) |
071830ff LP |
174 | return -errno; |
175 | ||
80876c20 LP |
176 | if (fd != nfd) { |
177 | r = dup2(fd, nfd) < 0 ? -errno : nfd; | |
e1f5e051 | 178 | close_nointr_nofail(fd); |
80876c20 LP |
179 | } else |
180 | r = nfd; | |
071830ff | 181 | |
80876c20 | 182 | return r; |
071830ff LP |
183 | } |
184 | ||
62bca2c6 | 185 | static int connect_logger_as(const ExecContext *context, ExecOutput output, const char *ident, const char *unit_id, int nfd) { |
80876c20 | 186 | int fd, r; |
54fe0cdb | 187 | union sockaddr_union sa; |
071830ff LP |
188 | |
189 | assert(context); | |
80876c20 LP |
190 | assert(output < _EXEC_OUTPUT_MAX); |
191 | assert(ident); | |
192 | assert(nfd >= 0); | |
071830ff | 193 | |
54fe0cdb LP |
194 | fd = socket(AF_UNIX, SOCK_STREAM, 0); |
195 | if (fd < 0) | |
80876c20 | 196 | return -errno; |
071830ff | 197 | |
80876c20 | 198 | zero(sa); |
54fe0cdb LP |
199 | sa.un.sun_family = AF_UNIX; |
200 | strncpy(sa.un.sun_path, "/run/systemd/journal/stdout", sizeof(sa.un.sun_path)); | |
071830ff | 201 | |
54fe0cdb LP |
202 | r = connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)); |
203 | if (r < 0) { | |
80876c20 LP |
204 | close_nointr_nofail(fd); |
205 | return -errno; | |
206 | } | |
071830ff | 207 | |
80876c20 LP |
208 | if (shutdown(fd, SHUT_RD) < 0) { |
209 | close_nointr_nofail(fd); | |
210 | return -errno; | |
211 | } | |
071830ff | 212 | |
80876c20 | 213 | dprintf(fd, |
62bca2c6 | 214 | "%s\n" |
80876c20 LP |
215 | "%s\n" |
216 | "%i\n" | |
54fe0cdb LP |
217 | "%i\n" |
218 | "%i\n" | |
219 | "%i\n" | |
4f4a1dbf | 220 | "%i\n", |
4f4a1dbf | 221 | context->syslog_identifier ? context->syslog_identifier : ident, |
62bca2c6 | 222 | unit_id, |
54fe0cdb LP |
223 | context->syslog_priority, |
224 | !!context->syslog_level_prefix, | |
225 | output == EXEC_OUTPUT_SYSLOG || output == EXEC_OUTPUT_SYSLOG_AND_CONSOLE, | |
226 | output == EXEC_OUTPUT_KMSG || output == EXEC_OUTPUT_KMSG_AND_CONSOLE, | |
706343f4 | 227 | output == EXEC_OUTPUT_SYSLOG_AND_CONSOLE || output == EXEC_OUTPUT_KMSG_AND_CONSOLE || output == EXEC_OUTPUT_JOURNAL_AND_CONSOLE); |
80876c20 LP |
228 | |
229 | if (fd != nfd) { | |
230 | r = dup2(fd, nfd) < 0 ? -errno : nfd; | |
e1f5e051 | 231 | close_nointr_nofail(fd); |
80876c20 LP |
232 | } else |
233 | r = nfd; | |
071830ff | 234 | |
80876c20 LP |
235 | return r; |
236 | } | |
237 | static int open_terminal_as(const char *path, mode_t mode, int nfd) { | |
238 | int fd, r; | |
071830ff | 239 | |
80876c20 LP |
240 | assert(path); |
241 | assert(nfd >= 0); | |
071830ff | 242 | |
80876c20 LP |
243 | if ((fd = open_terminal(path, mode | O_NOCTTY)) < 0) |
244 | return fd; | |
071830ff | 245 | |
80876c20 LP |
246 | if (fd != nfd) { |
247 | r = dup2(fd, nfd) < 0 ? -errno : nfd; | |
248 | close_nointr_nofail(fd); | |
249 | } else | |
250 | r = nfd; | |
071830ff | 251 | |
80876c20 LP |
252 | return r; |
253 | } | |
071830ff | 254 | |
80876c20 LP |
255 | static bool is_terminal_input(ExecInput i) { |
256 | return | |
257 | i == EXEC_INPUT_TTY || | |
258 | i == EXEC_INPUT_TTY_FORCE || | |
259 | i == EXEC_INPUT_TTY_FAIL; | |
260 | } | |
071830ff | 261 | |
1e3ad081 LP |
262 | static int fixup_input(ExecInput std_input, int socket_fd, bool apply_tty_stdin) { |
263 | ||
264 | if (is_terminal_input(std_input) && !apply_tty_stdin) | |
265 | return EXEC_INPUT_NULL; | |
071830ff | 266 | |
03fd9c49 | 267 | if (std_input == EXEC_INPUT_SOCKET && socket_fd < 0) |
4f2d528d LP |
268 | return EXEC_INPUT_NULL; |
269 | ||
03fd9c49 | 270 | return std_input; |
4f2d528d LP |
271 | } |
272 | ||
03fd9c49 | 273 | static int fixup_output(ExecOutput std_output, int socket_fd) { |
4f2d528d | 274 | |
03fd9c49 | 275 | if (std_output == EXEC_OUTPUT_SOCKET && socket_fd < 0) |
4f2d528d LP |
276 | return EXEC_OUTPUT_INHERIT; |
277 | ||
03fd9c49 | 278 | return std_output; |
4f2d528d LP |
279 | } |
280 | ||
1e3ad081 | 281 | static int setup_input(const ExecContext *context, int socket_fd, bool apply_tty_stdin) { |
4f2d528d LP |
282 | ExecInput i; |
283 | ||
284 | assert(context); | |
285 | ||
1e3ad081 | 286 | i = fixup_input(context->std_input, socket_fd, apply_tty_stdin); |
4f2d528d LP |
287 | |
288 | switch (i) { | |
071830ff | 289 | |
80876c20 LP |
290 | case EXEC_INPUT_NULL: |
291 | return open_null_as(O_RDONLY, STDIN_FILENO); | |
292 | ||
293 | case EXEC_INPUT_TTY: | |
294 | case EXEC_INPUT_TTY_FORCE: | |
295 | case EXEC_INPUT_TTY_FAIL: { | |
296 | int fd, r; | |
071830ff | 297 | |
80876c20 LP |
298 | if ((fd = acquire_terminal( |
299 | tty_path(context), | |
4f2d528d | 300 | i == EXEC_INPUT_TTY_FAIL, |
21de3988 | 301 | i == EXEC_INPUT_TTY_FORCE, |
af6da548 LP |
302 | false, |
303 | (usec_t) -1)) < 0) | |
80876c20 LP |
304 | return fd; |
305 | ||
306 | if (fd != STDIN_FILENO) { | |
307 | r = dup2(fd, STDIN_FILENO) < 0 ? -errno : STDIN_FILENO; | |
071830ff | 308 | close_nointr_nofail(fd); |
80876c20 LP |
309 | } else |
310 | r = STDIN_FILENO; | |
311 | ||
312 | return r; | |
313 | } | |
314 | ||
4f2d528d LP |
315 | case EXEC_INPUT_SOCKET: |
316 | return dup2(socket_fd, STDIN_FILENO) < 0 ? -errno : STDIN_FILENO; | |
317 | ||
80876c20 LP |
318 | default: |
319 | assert_not_reached("Unknown input type"); | |
320 | } | |
321 | } | |
322 | ||
eb17e935 | 323 | static int setup_output(const ExecContext *context, int fileno, int socket_fd, const char *ident, const char *unit_id, bool apply_tty_stdin) { |
4f2d528d LP |
324 | ExecOutput o; |
325 | ExecInput i; | |
47c1d80d | 326 | int r; |
4f2d528d | 327 | |
80876c20 LP |
328 | assert(context); |
329 | assert(ident); | |
330 | ||
1e3ad081 | 331 | i = fixup_input(context->std_input, socket_fd, apply_tty_stdin); |
03fd9c49 | 332 | o = fixup_output(context->std_output, socket_fd); |
4f2d528d | 333 | |
eb17e935 MS |
334 | if (fileno == STDERR_FILENO) { |
335 | ExecOutput e; | |
336 | e = fixup_output(context->std_error, socket_fd); | |
80876c20 | 337 | |
eb17e935 MS |
338 | /* This expects the input and output are already set up */ |
339 | ||
340 | /* Don't change the stderr file descriptor if we inherit all | |
341 | * the way and are not on a tty */ | |
342 | if (e == EXEC_OUTPUT_INHERIT && | |
343 | o == EXEC_OUTPUT_INHERIT && | |
344 | i == EXEC_INPUT_NULL && | |
345 | !is_terminal_input(context->std_input) && | |
346 | getppid () != 1) | |
347 | return fileno; | |
348 | ||
349 | /* Duplicate from stdout if possible */ | |
350 | if (e == o || e == EXEC_OUTPUT_INHERIT) | |
351 | return dup2(STDOUT_FILENO, fileno) < 0 ? -errno : fileno; | |
071830ff | 352 | |
eb17e935 | 353 | o = e; |
80876c20 | 354 | |
eb17e935 | 355 | } else if (o == EXEC_OUTPUT_INHERIT) { |
21d21ea4 LP |
356 | /* If input got downgraded, inherit the original value */ |
357 | if (i == EXEC_INPUT_NULL && is_terminal_input(context->std_input)) | |
eb17e935 | 358 | return open_terminal_as(tty_path(context), O_WRONLY, fileno); |
21d21ea4 | 359 | |
acb591e4 | 360 | /* If the input is connected to anything that's not a /dev/null, inherit that... */ |
ff876e28 | 361 | if (i != EXEC_INPUT_NULL) |
eb17e935 | 362 | return dup2(STDIN_FILENO, fileno) < 0 ? -errno : fileno; |
071830ff | 363 | |
acb591e4 LP |
364 | /* If we are not started from PID 1 we just inherit STDOUT from our parent process. */ |
365 | if (getppid() != 1) | |
eb17e935 | 366 | return fileno; |
94f04347 | 367 | |
eb17e935 MS |
368 | /* We need to open /dev/null here anew, to get the right access mode. */ |
369 | return open_null_as(O_WRONLY, fileno); | |
071830ff | 370 | } |
94f04347 | 371 | |
eb17e935 | 372 | switch (o) { |
80876c20 LP |
373 | |
374 | case EXEC_OUTPUT_NULL: | |
eb17e935 | 375 | return open_null_as(O_WRONLY, fileno); |
80876c20 LP |
376 | |
377 | case EXEC_OUTPUT_TTY: | |
4f2d528d | 378 | if (is_terminal_input(i)) |
eb17e935 | 379 | return dup2(STDIN_FILENO, fileno) < 0 ? -errno : fileno; |
80876c20 LP |
380 | |
381 | /* We don't reset the terminal if this is just about output */ | |
eb17e935 | 382 | return open_terminal_as(tty_path(context), O_WRONLY, fileno); |
80876c20 LP |
383 | |
384 | case EXEC_OUTPUT_SYSLOG: | |
28dbc1e8 | 385 | case EXEC_OUTPUT_SYSLOG_AND_CONSOLE: |
9a6bca7a | 386 | case EXEC_OUTPUT_KMSG: |
28dbc1e8 | 387 | case EXEC_OUTPUT_KMSG_AND_CONSOLE: |
706343f4 LP |
388 | case EXEC_OUTPUT_JOURNAL: |
389 | case EXEC_OUTPUT_JOURNAL_AND_CONSOLE: | |
eb17e935 | 390 | r = connect_logger_as(context, o, ident, unit_id, fileno); |
47c1d80d | 391 | if (r < 0) { |
eb17e935 MS |
392 | log_error("Failed to connect std%s of %s to the journal socket: %s", |
393 | fileno == STDOUT_FILENO ? "out" : "err", | |
394 | unit_id, strerror(-r)); | |
395 | r = open_null_as(O_WRONLY, fileno); | |
47c1d80d MS |
396 | } |
397 | return r; | |
4f2d528d LP |
398 | |
399 | case EXEC_OUTPUT_SOCKET: | |
400 | assert(socket_fd >= 0); | |
eb17e935 | 401 | return dup2(socket_fd, fileno) < 0 ? -errno : fileno; |
94f04347 LP |
402 | |
403 | default: | |
80876c20 | 404 | assert_not_reached("Unknown error type"); |
94f04347 | 405 | } |
071830ff LP |
406 | } |
407 | ||
02a51aba LP |
408 | static int chown_terminal(int fd, uid_t uid) { |
409 | struct stat st; | |
410 | ||
411 | assert(fd >= 0); | |
02a51aba LP |
412 | |
413 | /* This might fail. What matters are the results. */ | |
bab45044 LP |
414 | (void) fchown(fd, uid, -1); |
415 | (void) fchmod(fd, TTY_MODE); | |
02a51aba LP |
416 | |
417 | if (fstat(fd, &st) < 0) | |
418 | return -errno; | |
419 | ||
d8b4e2e9 | 420 | if (st.st_uid != uid || (st.st_mode & 0777) != TTY_MODE) |
02a51aba LP |
421 | return -EPERM; |
422 | ||
423 | return 0; | |
424 | } | |
425 | ||
af6da548 | 426 | static int setup_confirm_stdio(int *_saved_stdin, |
80876c20 LP |
427 | int *_saved_stdout) { |
428 | int fd = -1, saved_stdin, saved_stdout = -1, r; | |
429 | ||
80876c20 LP |
430 | assert(_saved_stdin); |
431 | assert(_saved_stdout); | |
432 | ||
af6da548 LP |
433 | saved_stdin = fcntl(STDIN_FILENO, F_DUPFD, 3); |
434 | if (saved_stdin < 0) | |
435 | return -errno; | |
80876c20 | 436 | |
af6da548 LP |
437 | saved_stdout = fcntl(STDOUT_FILENO, F_DUPFD, 3); |
438 | if (saved_stdout < 0) { | |
439 | r = errno; | |
80876c20 LP |
440 | goto fail; |
441 | } | |
442 | ||
af6da548 LP |
443 | fd = acquire_terminal( |
444 | "/dev/console", | |
445 | false, | |
446 | false, | |
447 | false, | |
448 | DEFAULT_CONFIRM_USEC); | |
449 | if (fd < 0) { | |
450 | r = fd; | |
80876c20 LP |
451 | goto fail; |
452 | } | |
453 | ||
af6da548 LP |
454 | r = chown_terminal(fd, getuid()); |
455 | if (r < 0) | |
02a51aba | 456 | goto fail; |
02a51aba | 457 | |
80876c20 | 458 | if (dup2(fd, STDIN_FILENO) < 0) { |
af6da548 | 459 | r = -errno; |
80876c20 LP |
460 | goto fail; |
461 | } | |
462 | ||
463 | if (dup2(fd, STDOUT_FILENO) < 0) { | |
af6da548 | 464 | r = -errno; |
80876c20 LP |
465 | goto fail; |
466 | } | |
467 | ||
468 | if (fd >= 2) | |
469 | close_nointr_nofail(fd); | |
470 | ||
471 | *_saved_stdin = saved_stdin; | |
472 | *_saved_stdout = saved_stdout; | |
473 | ||
474 | return 0; | |
475 | ||
476 | fail: | |
477 | if (saved_stdout >= 0) | |
478 | close_nointr_nofail(saved_stdout); | |
479 | ||
480 | if (saved_stdin >= 0) | |
481 | close_nointr_nofail(saved_stdin); | |
482 | ||
483 | if (fd >= 0) | |
484 | close_nointr_nofail(fd); | |
485 | ||
486 | return r; | |
487 | } | |
488 | ||
af6da548 LP |
489 | static int write_confirm_message(const char *format, ...) { |
490 | int fd; | |
491 | va_list ap; | |
80876c20 | 492 | |
af6da548 | 493 | assert(format); |
80876c20 | 494 | |
af6da548 LP |
495 | fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC); |
496 | if (fd < 0) | |
497 | return fd; | |
80876c20 | 498 | |
af6da548 LP |
499 | va_start(ap, format); |
500 | vdprintf(fd, format, ap); | |
501 | va_end(ap); | |
80876c20 | 502 | |
af6da548 | 503 | close_nointr_nofail(fd); |
80876c20 | 504 | |
af6da548 LP |
505 | return 0; |
506 | } | |
80876c20 | 507 | |
af6da548 LP |
508 | static int restore_confirm_stdio(int *saved_stdin, |
509 | int *saved_stdout) { | |
80876c20 | 510 | |
af6da548 | 511 | int r = 0; |
80876c20 | 512 | |
af6da548 LP |
513 | assert(saved_stdin); |
514 | assert(saved_stdout); | |
515 | ||
516 | release_terminal(); | |
517 | ||
518 | if (*saved_stdin >= 0) | |
80876c20 | 519 | if (dup2(*saved_stdin, STDIN_FILENO) < 0) |
af6da548 | 520 | r = -errno; |
80876c20 | 521 | |
af6da548 | 522 | if (*saved_stdout >= 0) |
80876c20 | 523 | if (dup2(*saved_stdout, STDOUT_FILENO) < 0) |
af6da548 | 524 | r = -errno; |
80876c20 | 525 | |
af6da548 LP |
526 | if (*saved_stdin >= 0) |
527 | close_nointr_nofail(*saved_stdin); | |
80876c20 | 528 | |
af6da548 LP |
529 | if (*saved_stdout >= 0) |
530 | close_nointr_nofail(*saved_stdout); | |
531 | ||
532 | return r; | |
533 | } | |
534 | ||
535 | static int ask_for_confirmation(char *response, char **argv) { | |
536 | int saved_stdout = -1, saved_stdin = -1, r; | |
537 | char *line; | |
538 | ||
539 | r = setup_confirm_stdio(&saved_stdin, &saved_stdout); | |
540 | if (r < 0) | |
541 | return r; | |
542 | ||
543 | line = exec_command_line(argv); | |
544 | if (!line) | |
545 | return -ENOMEM; | |
546 | ||
547 | r = ask(response, "yns", "Execute %s? [Yes, No, Skip] ", line); | |
548 | free(line); | |
549 | ||
550 | restore_confirm_stdio(&saved_stdin, &saved_stdout); | |
551 | ||
552 | return r; | |
80876c20 LP |
553 | } |
554 | ||
81a2b7ce LP |
555 | static int enforce_groups(const ExecContext *context, const char *username, gid_t gid) { |
556 | bool keep_groups = false; | |
557 | int r; | |
558 | ||
559 | assert(context); | |
560 | ||
35b8ca3a | 561 | /* Lookup and set GID and supplementary group list. Here too |
81a2b7ce LP |
562 | * we avoid NSS lookups for gid=0. */ |
563 | ||
564 | if (context->group || username) { | |
565 | ||
4b67834e LP |
566 | if (context->group) { |
567 | const char *g = context->group; | |
568 | ||
569 | if ((r = get_group_creds(&g, &gid)) < 0) | |
81a2b7ce | 570 | return r; |
4b67834e | 571 | } |
81a2b7ce LP |
572 | |
573 | /* First step, initialize groups from /etc/groups */ | |
574 | if (username && gid != 0) { | |
575 | if (initgroups(username, gid) < 0) | |
576 | return -errno; | |
577 | ||
578 | keep_groups = true; | |
579 | } | |
580 | ||
581 | /* Second step, set our gids */ | |
582 | if (setresgid(gid, gid, gid) < 0) | |
583 | return -errno; | |
584 | } | |
585 | ||
586 | if (context->supplementary_groups) { | |
587 | int ngroups_max, k; | |
588 | gid_t *gids; | |
589 | char **i; | |
590 | ||
591 | /* Final step, initialize any manually set supplementary groups */ | |
da19d5c1 | 592 | assert_se((ngroups_max = (int) sysconf(_SC_NGROUPS_MAX)) > 0); |
81a2b7ce LP |
593 | |
594 | if (!(gids = new(gid_t, ngroups_max))) | |
595 | return -ENOMEM; | |
596 | ||
597 | if (keep_groups) { | |
598 | if ((k = getgroups(ngroups_max, gids)) < 0) { | |
599 | free(gids); | |
600 | return -errno; | |
601 | } | |
602 | } else | |
603 | k = 0; | |
604 | ||
605 | STRV_FOREACH(i, context->supplementary_groups) { | |
4b67834e | 606 | const char *g; |
81a2b7ce LP |
607 | |
608 | if (k >= ngroups_max) { | |
609 | free(gids); | |
610 | return -E2BIG; | |
611 | } | |
612 | ||
4b67834e LP |
613 | g = *i; |
614 | r = get_group_creds(&g, gids+k); | |
615 | if (r < 0) { | |
81a2b7ce LP |
616 | free(gids); |
617 | return r; | |
618 | } | |
619 | ||
620 | k++; | |
621 | } | |
622 | ||
623 | if (setgroups(k, gids) < 0) { | |
624 | free(gids); | |
625 | return -errno; | |
626 | } | |
627 | ||
628 | free(gids); | |
629 | } | |
630 | ||
631 | return 0; | |
632 | } | |
633 | ||
634 | static int enforce_user(const ExecContext *context, uid_t uid) { | |
635 | int r; | |
636 | assert(context); | |
637 | ||
638 | /* Sets (but doesn't lookup) the uid and make sure we keep the | |
639 | * capabilities while doing so. */ | |
640 | ||
641 | if (context->capabilities) { | |
642 | cap_t d; | |
643 | static const cap_value_t bits[] = { | |
644 | CAP_SETUID, /* Necessary so that we can run setresuid() below */ | |
645 | CAP_SETPCAP /* Necessary so that we can set PR_SET_SECUREBITS later on */ | |
646 | }; | |
647 | ||
648 | /* First step: If we need to keep capabilities but | |
649 | * drop privileges we need to make sure we keep our | |
35b8ca3a | 650 | * caps, whiel we drop privileges. */ |
693ced48 LP |
651 | if (uid != 0) { |
652 | int sb = context->secure_bits|SECURE_KEEP_CAPS; | |
653 | ||
654 | if (prctl(PR_GET_SECUREBITS) != sb) | |
655 | if (prctl(PR_SET_SECUREBITS, sb) < 0) | |
656 | return -errno; | |
657 | } | |
81a2b7ce | 658 | |
35b8ca3a | 659 | /* Second step: set the capabilities. This will reduce |
81a2b7ce LP |
660 | * the capabilities to the minimum we need. */ |
661 | ||
662 | if (!(d = cap_dup(context->capabilities))) | |
663 | return -errno; | |
664 | ||
665 | if (cap_set_flag(d, CAP_EFFECTIVE, ELEMENTSOF(bits), bits, CAP_SET) < 0 || | |
666 | cap_set_flag(d, CAP_PERMITTED, ELEMENTSOF(bits), bits, CAP_SET) < 0) { | |
667 | r = -errno; | |
668 | cap_free(d); | |
669 | return r; | |
670 | } | |
671 | ||
672 | if (cap_set_proc(d) < 0) { | |
673 | r = -errno; | |
674 | cap_free(d); | |
675 | return r; | |
676 | } | |
677 | ||
678 | cap_free(d); | |
679 | } | |
680 | ||
681 | /* Third step: actually set the uids */ | |
682 | if (setresuid(uid, uid, uid) < 0) | |
683 | return -errno; | |
684 | ||
685 | /* At this point we should have all necessary capabilities but | |
686 | are otherwise a normal user. However, the caps might got | |
687 | corrupted due to the setresuid() so we need clean them up | |
688 | later. This is done outside of this call. */ | |
689 | ||
690 | return 0; | |
691 | } | |
692 | ||
5b6319dc LP |
693 | #ifdef HAVE_PAM |
694 | ||
695 | static int null_conv( | |
696 | int num_msg, | |
697 | const struct pam_message **msg, | |
698 | struct pam_response **resp, | |
699 | void *appdata_ptr) { | |
700 | ||
701 | /* We don't support conversations */ | |
702 | ||
703 | return PAM_CONV_ERR; | |
704 | } | |
705 | ||
706 | static int setup_pam( | |
707 | const char *name, | |
708 | const char *user, | |
940c5210 | 709 | uid_t uid, |
5b6319dc LP |
710 | const char *tty, |
711 | char ***pam_env, | |
712 | int fds[], unsigned n_fds) { | |
713 | ||
714 | static const struct pam_conv conv = { | |
715 | .conv = null_conv, | |
716 | .appdata_ptr = NULL | |
717 | }; | |
718 | ||
719 | pam_handle_t *handle = NULL; | |
720 | sigset_t ss, old_ss; | |
721 | int pam_code = PAM_SUCCESS; | |
9ba35398 | 722 | int err; |
5b6319dc LP |
723 | char **e = NULL; |
724 | bool close_session = false; | |
725 | pid_t pam_pid = 0, parent_pid; | |
726 | ||
727 | assert(name); | |
728 | assert(user); | |
729 | assert(pam_env); | |
730 | ||
731 | /* We set up PAM in the parent process, then fork. The child | |
35b8ca3a | 732 | * will then stay around until killed via PR_GET_PDEATHSIG or |
5b6319dc LP |
733 | * systemd via the cgroup logic. It will then remove the PAM |
734 | * session again. The parent process will exec() the actual | |
735 | * daemon. We do things this way to ensure that the main PID | |
736 | * of the daemon is the one we initially fork()ed. */ | |
737 | ||
738 | if ((pam_code = pam_start(name, user, &conv, &handle)) != PAM_SUCCESS) { | |
739 | handle = NULL; | |
740 | goto fail; | |
741 | } | |
742 | ||
743 | if (tty) | |
744 | if ((pam_code = pam_set_item(handle, PAM_TTY, tty)) != PAM_SUCCESS) | |
745 | goto fail; | |
746 | ||
747 | if ((pam_code = pam_acct_mgmt(handle, PAM_SILENT)) != PAM_SUCCESS) | |
748 | goto fail; | |
749 | ||
750 | if ((pam_code = pam_open_session(handle, PAM_SILENT)) != PAM_SUCCESS) | |
751 | goto fail; | |
752 | ||
753 | close_session = true; | |
754 | ||
5b6319dc LP |
755 | if ((!(e = pam_getenvlist(handle)))) { |
756 | pam_code = PAM_BUF_ERR; | |
757 | goto fail; | |
758 | } | |
759 | ||
760 | /* Block SIGTERM, so that we know that it won't get lost in | |
761 | * the child */ | |
762 | if (sigemptyset(&ss) < 0 || | |
763 | sigaddset(&ss, SIGTERM) < 0 || | |
764 | sigprocmask(SIG_BLOCK, &ss, &old_ss) < 0) | |
765 | goto fail; | |
766 | ||
767 | parent_pid = getpid(); | |
768 | ||
769 | if ((pam_pid = fork()) < 0) | |
770 | goto fail; | |
771 | ||
772 | if (pam_pid == 0) { | |
773 | int sig; | |
774 | int r = EXIT_PAM; | |
775 | ||
776 | /* The child's job is to reset the PAM session on | |
777 | * termination */ | |
778 | ||
779 | /* This string must fit in 10 chars (i.e. the length | |
5d6b1584 LP |
780 | * of "/sbin/init"), to look pretty in /bin/ps */ |
781 | rename_process("(sd-pam)"); | |
5b6319dc LP |
782 | |
783 | /* Make sure we don't keep open the passed fds in this | |
784 | child. We assume that otherwise only those fds are | |
785 | open here that have been opened by PAM. */ | |
786 | close_many(fds, n_fds); | |
787 | ||
940c5210 AK |
788 | /* Drop privileges - we don't need any to pam_close_session |
789 | * and this will make PR_SET_PDEATHSIG work in most cases. | |
790 | * If this fails, ignore the error - but expect sd-pam threads | |
791 | * to fail to exit normally */ | |
792 | if (setresuid(uid, uid, uid) < 0) | |
793 | log_error("Error: Failed to setresuid() in sd-pam: %s", strerror(-r)); | |
794 | ||
795 | /* Wait until our parent died. This will only work if | |
796 | * the above setresuid() succeeds, otherwise the kernel | |
797 | * will not allow unprivileged parents kill their privileged | |
798 | * children this way. We rely on the control groups kill logic | |
5b6319dc LP |
799 | * to do the rest for us. */ |
800 | if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0) | |
801 | goto child_finish; | |
802 | ||
803 | /* Check if our parent process might already have | |
804 | * died? */ | |
805 | if (getppid() == parent_pid) { | |
3dead8d9 LP |
806 | for (;;) { |
807 | if (sigwait(&ss, &sig) < 0) { | |
808 | if (errno == EINTR) | |
809 | continue; | |
810 | ||
811 | goto child_finish; | |
812 | } | |
5b6319dc | 813 | |
3dead8d9 LP |
814 | assert(sig == SIGTERM); |
815 | break; | |
816 | } | |
5b6319dc LP |
817 | } |
818 | ||
3dead8d9 | 819 | /* If our parent died we'll end the session */ |
5b6319dc LP |
820 | if (getppid() != parent_pid) |
821 | if ((pam_code = pam_close_session(handle, PAM_DATA_SILENT)) != PAM_SUCCESS) | |
822 | goto child_finish; | |
823 | ||
824 | r = 0; | |
825 | ||
826 | child_finish: | |
827 | pam_end(handle, pam_code | PAM_DATA_SILENT); | |
828 | _exit(r); | |
829 | } | |
830 | ||
831 | /* If the child was forked off successfully it will do all the | |
832 | * cleanups, so forget about the handle here. */ | |
833 | handle = NULL; | |
834 | ||
3b8bddde | 835 | /* Unblock SIGTERM again in the parent */ |
5b6319dc LP |
836 | if (sigprocmask(SIG_SETMASK, &old_ss, NULL) < 0) |
837 | goto fail; | |
838 | ||
839 | /* We close the log explicitly here, since the PAM modules | |
840 | * might have opened it, but we don't want this fd around. */ | |
841 | closelog(); | |
842 | ||
aa87e624 LP |
843 | *pam_env = e; |
844 | e = NULL; | |
845 | ||
5b6319dc LP |
846 | return 0; |
847 | ||
848 | fail: | |
9ba35398 MS |
849 | if (pam_code != PAM_SUCCESS) |
850 | err = -EPERM; /* PAM errors do not map to errno */ | |
851 | else | |
852 | err = -errno; | |
853 | ||
5b6319dc LP |
854 | if (handle) { |
855 | if (close_session) | |
856 | pam_code = pam_close_session(handle, PAM_DATA_SILENT); | |
857 | ||
858 | pam_end(handle, pam_code | PAM_DATA_SILENT); | |
859 | } | |
860 | ||
861 | strv_free(e); | |
862 | ||
863 | closelog(); | |
864 | ||
430c18ed | 865 | if (pam_pid > 1) { |
5b6319dc | 866 | kill(pam_pid, SIGTERM); |
430c18ed LP |
867 | kill(pam_pid, SIGCONT); |
868 | } | |
5b6319dc | 869 | |
9ba35398 | 870 | return err; |
5b6319dc LP |
871 | } |
872 | #endif | |
873 | ||
5d6b1584 LP |
874 | static void rename_process_from_path(const char *path) { |
875 | char process_name[11]; | |
876 | const char *p; | |
877 | size_t l; | |
878 | ||
879 | /* This resulting string must fit in 10 chars (i.e. the length | |
880 | * of "/sbin/init") to look pretty in /bin/ps */ | |
881 | ||
9eb977db | 882 | p = path_get_file_name(path); |
5d6b1584 LP |
883 | if (isempty(p)) { |
884 | rename_process("(...)"); | |
885 | return; | |
886 | } | |
887 | ||
888 | l = strlen(p); | |
889 | if (l > 8) { | |
890 | /* The end of the process name is usually more | |
891 | * interesting, since the first bit might just be | |
892 | * "systemd-" */ | |
893 | p = p + l - 8; | |
894 | l = 8; | |
895 | } | |
896 | ||
897 | process_name[0] = '('; | |
898 | memcpy(process_name+1, p, l); | |
899 | process_name[1+l] = ')'; | |
900 | process_name[1+l+1] = 0; | |
901 | ||
902 | rename_process(process_name); | |
903 | } | |
904 | ||
8351ceae LP |
905 | static int apply_seccomp(uint32_t *syscall_filter) { |
906 | static const struct sock_filter header[] = { | |
907 | VALIDATE_ARCHITECTURE, | |
908 | EXAMINE_SYSCALL | |
909 | }; | |
910 | static const struct sock_filter footer[] = { | |
911 | _KILL_PROCESS | |
912 | }; | |
913 | ||
914 | int i; | |
915 | unsigned n; | |
916 | struct sock_filter *f; | |
917 | struct sock_fprog prog; | |
918 | ||
919 | assert(syscall_filter); | |
920 | ||
921 | /* First: count the syscalls to check for */ | |
922 | for (i = 0, n = 0; i < syscall_max(); i++) | |
923 | if (syscall_filter[i >> 4] & (1 << (i & 31))) | |
924 | n++; | |
925 | ||
926 | /* Second: build the filter program from a header the syscall | |
927 | * matches and the footer */ | |
928 | f = alloca(sizeof(struct sock_filter) * (ELEMENTSOF(header) + 2*n + ELEMENTSOF(footer))); | |
929 | memcpy(f, header, sizeof(header)); | |
930 | ||
931 | for (i = 0, n = 0; i < syscall_max(); i++) | |
932 | if (syscall_filter[i >> 4] & (1 << (i & 31))) { | |
933 | struct sock_filter item[] = { | |
843fc7f7 | 934 | BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, INDEX_TO_SYSCALL(i), 0, 1), |
8351ceae LP |
935 | BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW) |
936 | }; | |
937 | ||
938 | assert_cc(ELEMENTSOF(item) == 2); | |
939 | ||
940 | f[ELEMENTSOF(header) + 2*n] = item[0]; | |
941 | f[ELEMENTSOF(header) + 2*n+1] = item[1]; | |
942 | ||
943 | n++; | |
944 | } | |
945 | ||
946 | memcpy(f + (ELEMENTSOF(header) + 2*n), footer, sizeof(footer)); | |
947 | ||
948 | /* Third: install the filter */ | |
949 | zero(prog); | |
950 | prog.len = ELEMENTSOF(header) + ELEMENTSOF(footer) + 2*n; | |
951 | prog.filter = f; | |
952 | if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) < 0) | |
953 | return -errno; | |
954 | ||
955 | return 0; | |
956 | } | |
957 | ||
9fb86720 | 958 | int exec_spawn(ExecCommand *command, |
9e2f7c11 | 959 | char **argv, |
81a2b7ce | 960 | const ExecContext *context, |
c2748801 | 961 | int fds[], unsigned n_fds, |
1137a57c | 962 | char **environment, |
81a2b7ce LP |
963 | bool apply_permissions, |
964 | bool apply_chroot, | |
1e3ad081 | 965 | bool apply_tty_stdin, |
80876c20 | 966 | bool confirm_spawn, |
8e274523 | 967 | CGroupBonding *cgroup_bondings, |
ab1f0633 | 968 | CGroupAttribute *cgroup_attributes, |
ecedd90f | 969 | const char *cgroup_suffix, |
62bca2c6 | 970 | const char *unit_id, |
f2b68789 | 971 | int idle_pipe[2], |
81a2b7ce LP |
972 | pid_t *ret) { |
973 | ||
034c6ed7 | 974 | pid_t pid; |
8e274523 | 975 | int r; |
1a63a750 | 976 | char *line; |
4f2d528d | 977 | int socket_fd; |
b66871da | 978 | char _cleanup_strv_free_ **files_env = NULL; |
034c6ed7 | 979 | |
5cb5a6ff LP |
980 | assert(command); |
981 | assert(context); | |
982 | assert(ret); | |
034c6ed7 LP |
983 | assert(fds || n_fds <= 0); |
984 | ||
4f2d528d LP |
985 | if (context->std_input == EXEC_INPUT_SOCKET || |
986 | context->std_output == EXEC_OUTPUT_SOCKET || | |
987 | context->std_error == EXEC_OUTPUT_SOCKET) { | |
988 | ||
989 | if (n_fds != 1) | |
990 | return -EINVAL; | |
991 | ||
992 | socket_fd = fds[0]; | |
993 | ||
994 | fds = NULL; | |
995 | n_fds = 0; | |
996 | } else | |
997 | socket_fd = -1; | |
998 | ||
b66871da ZJS |
999 | r = exec_context_load_environment(context, &files_env); |
1000 | if (r < 0) { | |
bbc9006e MT |
1001 | log_struct_unit(LOG_ERR, |
1002 | unit_id, | |
23635a85 ZJS |
1003 | "MESSAGE=Failed to load environment files: %s", strerror(-r), |
1004 | "ERRNO=%d", -r, | |
1005 | NULL); | |
8c7be95e LP |
1006 | return r; |
1007 | } | |
1008 | ||
9e2f7c11 LP |
1009 | if (!argv) |
1010 | argv = command->argv; | |
1011 | ||
af6da548 | 1012 | line = exec_command_line(argv); |
b66871da ZJS |
1013 | if (!line) |
1014 | return log_oom(); | |
1a63a750 | 1015 | |
bbc9006e MT |
1016 | log_struct_unit(LOG_DEBUG, |
1017 | unit_id, | |
23635a85 ZJS |
1018 | "MESSAGE=About to execute %s", line, |
1019 | NULL); | |
1a63a750 | 1020 | free(line); |
acbb0225 | 1021 | |
ab1f0633 LP |
1022 | r = cgroup_bonding_realize_list(cgroup_bondings); |
1023 | if (r < 0) | |
b66871da | 1024 | return r; |
ab1f0633 LP |
1025 | |
1026 | cgroup_attribute_apply_list(cgroup_attributes, cgroup_bondings); | |
8e274523 | 1027 | |
b66871da ZJS |
1028 | pid = fork(); |
1029 | if (pid < 0) | |
1030 | return -errno; | |
034c6ed7 LP |
1031 | |
1032 | if (pid == 0) { | |
4c2630eb | 1033 | int i, err; |
309bff19 | 1034 | sigset_t ss; |
81a2b7ce LP |
1035 | const char *username = NULL, *home = NULL; |
1036 | uid_t uid = (uid_t) -1; | |
1037 | gid_t gid = (gid_t) -1; | |
b66871da ZJS |
1038 | char _cleanup_strv_free_ **our_env = NULL, **pam_env = NULL, |
1039 | **final_env = NULL, **final_argv = NULL; | |
81a2b7ce | 1040 | unsigned n_env = 0; |
af6da548 | 1041 | bool set_access = false; |
309bff19 | 1042 | |
034c6ed7 | 1043 | /* child */ |
5cb5a6ff | 1044 | |
5d6b1584 | 1045 | rename_process_from_path(command->path); |
5b6319dc | 1046 | |
1b91d3e8 LP |
1047 | /* We reset exactly these signals, since they are the |
1048 | * only ones we set to SIG_IGN in the main daemon. All | |
1049 | * others we leave untouched because we set them to | |
1050 | * SIG_DFL or a valid handler initially, both of which | |
1051 | * will be demoted to SIG_DFL. */ | |
1052 | default_signals(SIGNALS_CRASH_HANDLER, | |
9a34ec5f | 1053 | SIGNALS_IGNORE, -1); |
7b683879 | 1054 | |
353e12c2 LP |
1055 | if (context->ignore_sigpipe) |
1056 | ignore_signals(SIGPIPE, -1); | |
1057 | ||
1058 | assert_se(sigemptyset(&ss) == 0); | |
1059 | if (sigprocmask(SIG_SETMASK, &ss, NULL) < 0) { | |
4c2630eb | 1060 | err = -errno; |
309bff19 | 1061 | r = EXIT_SIGNAL_MASK; |
8c7be95e | 1062 | goto fail_child; |
309bff19 LP |
1063 | } |
1064 | ||
f2b68789 LP |
1065 | if (idle_pipe) { |
1066 | if (idle_pipe[1] >= 0) | |
1067 | close_nointr_nofail(idle_pipe[1]); | |
1068 | if (idle_pipe[0] >= 0) { | |
e6a26745 | 1069 | fd_wait_for_event(idle_pipe[0], POLLHUP, IDLE_TIMEOUT_USEC); |
f2b68789 LP |
1070 | close_nointr_nofail(idle_pipe[0]); |
1071 | } | |
1072 | } | |
1073 | ||
85d73053 LP |
1074 | /* Close sockets very early to make sure we don't |
1075 | * block init reexecution because it cannot bind its | |
1076 | * sockets */ | |
4d8a7798 | 1077 | log_forget_fds(); |
4c2630eb MS |
1078 | err = close_all_fds(socket_fd >= 0 ? &socket_fd : fds, |
1079 | socket_fd >= 0 ? 1 : n_fds); | |
1080 | if (err < 0) { | |
fc9b2a84 | 1081 | r = EXIT_FDS; |
8c7be95e | 1082 | goto fail_child; |
fc9b2a84 LP |
1083 | } |
1084 | ||
74922904 | 1085 | if (!context->same_pgrp) |
8d567588 | 1086 | if (setsid() < 0) { |
4c2630eb | 1087 | err = -errno; |
8d567588 | 1088 | r = EXIT_SETSID; |
8c7be95e | 1089 | goto fail_child; |
8d567588 | 1090 | } |
80876c20 | 1091 | |
c5da34ef LP |
1092 | if (context->tcpwrap_name) { |
1093 | if (socket_fd >= 0) | |
1094 | if (!socket_tcpwrap(socket_fd, context->tcpwrap_name)) { | |
4c2630eb | 1095 | err = -EACCES; |
c5da34ef | 1096 | r = EXIT_TCPWRAP; |
8c7be95e | 1097 | goto fail_child; |
c5da34ef LP |
1098 | } |
1099 | ||
1100 | for (i = 0; i < (int) n_fds; i++) { | |
1101 | if (!socket_tcpwrap(fds[i], context->tcpwrap_name)) { | |
4c2630eb | 1102 | err = -EACCES; |
c5da34ef | 1103 | r = EXIT_TCPWRAP; |
8c7be95e | 1104 | goto fail_child; |
c5da34ef | 1105 | } |
df1f0afe | 1106 | } |
c5da34ef | 1107 | } |
df1f0afe | 1108 | |
6ea832a2 LP |
1109 | exec_context_tty_reset(context); |
1110 | ||
af6da548 | 1111 | if (confirm_spawn) { |
80876c20 LP |
1112 | char response; |
1113 | ||
af6da548 LP |
1114 | err = ask_for_confirmation(&response, argv); |
1115 | if (err == -ETIMEDOUT) | |
1116 | write_confirm_message("Confirmation question timed out, assuming positive response.\n"); | |
1117 | else if (err < 0) | |
1118 | write_confirm_message("Couldn't ask confirmation question, assuming positive response: %s\n", strerror(-err)); | |
1119 | else if (response == 's') { | |
1120 | write_confirm_message("Skipping execution.\n"); | |
4c2630eb | 1121 | err = -ECANCELED; |
80876c20 | 1122 | r = EXIT_CONFIRM; |
8c7be95e | 1123 | goto fail_child; |
af6da548 LP |
1124 | } else if (response == 'n') { |
1125 | write_confirm_message("Failing execution.\n"); | |
4c2630eb | 1126 | err = r = 0; |
8c7be95e | 1127 | goto fail_child; |
ee2b4894 | 1128 | } |
034c6ed7 LP |
1129 | } |
1130 | ||
da726a4d LP |
1131 | /* If a socket is connected to STDIN/STDOUT/STDERR, we |
1132 | * must sure to drop O_NONBLOCK */ | |
1133 | if (socket_fd >= 0) | |
1134 | fd_nonblock(socket_fd, false); | |
1135 | ||
af6da548 LP |
1136 | err = setup_input(context, socket_fd, apply_tty_stdin); |
1137 | if (err < 0) { | |
1138 | r = EXIT_STDIN; | |
1139 | goto fail_child; | |
4c2630eb | 1140 | } |
9eba9da4 | 1141 | |
eb17e935 | 1142 | err = setup_output(context, STDOUT_FILENO, socket_fd, path_get_file_name(command->path), unit_id, apply_tty_stdin); |
af6da548 LP |
1143 | if (err < 0) { |
1144 | r = EXIT_STDOUT; | |
1145 | goto fail_child; | |
4c2630eb | 1146 | } |
94f04347 | 1147 | |
eb17e935 | 1148 | err = setup_output(context, STDERR_FILENO, socket_fd, path_get_file_name(command->path), unit_id, apply_tty_stdin); |
4c2630eb | 1149 | if (err < 0) { |
80876c20 | 1150 | r = EXIT_STDERR; |
8c7be95e | 1151 | goto fail_child; |
071830ff LP |
1152 | } |
1153 | ||
4c2630eb | 1154 | if (cgroup_bondings) { |
ecedd90f | 1155 | err = cgroup_bonding_install_list(cgroup_bondings, 0, cgroup_suffix); |
4c2630eb | 1156 | if (err < 0) { |
8e274523 | 1157 | r = EXIT_CGROUP; |
8c7be95e | 1158 | goto fail_child; |
8e274523 | 1159 | } |
4c2630eb | 1160 | } |
8e274523 | 1161 | |
dd6c17b1 | 1162 | if (context->oom_score_adjust_set) { |
fb33a393 | 1163 | char t[16]; |
034c6ed7 | 1164 | |
dd6c17b1 | 1165 | snprintf(t, sizeof(t), "%i", context->oom_score_adjust); |
fb33a393 | 1166 | char_array_0(t); |
034c6ed7 | 1167 | |
dd6c17b1 | 1168 | if (write_one_line_file("/proc/self/oom_score_adj", t) < 0) { |
8600c525 KS |
1169 | err = -errno; |
1170 | r = EXIT_OOM_ADJUST; | |
1171 | goto fail_child; | |
fb33a393 | 1172 | } |
034c6ed7 LP |
1173 | } |
1174 | ||
fb33a393 LP |
1175 | if (context->nice_set) |
1176 | if (setpriority(PRIO_PROCESS, 0, context->nice) < 0) { | |
4c2630eb | 1177 | err = -errno; |
fb33a393 | 1178 | r = EXIT_NICE; |
8c7be95e | 1179 | goto fail_child; |
fb33a393 LP |
1180 | } |
1181 | ||
94f04347 LP |
1182 | if (context->cpu_sched_set) { |
1183 | struct sched_param param; | |
1184 | ||
1185 | zero(param); | |
1186 | param.sched_priority = context->cpu_sched_priority; | |
1187 | ||
38b48754 LP |
1188 | if (sched_setscheduler(0, context->cpu_sched_policy | |
1189 | (context->cpu_sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0), ¶m) < 0) { | |
4c2630eb | 1190 | err = -errno; |
94f04347 | 1191 | r = EXIT_SETSCHEDULER; |
8c7be95e | 1192 | goto fail_child; |
94f04347 LP |
1193 | } |
1194 | } | |
1195 | ||
82c121a4 LP |
1196 | if (context->cpuset) |
1197 | if (sched_setaffinity(0, CPU_ALLOC_SIZE(context->cpuset_ncpus), context->cpuset) < 0) { | |
4c2630eb | 1198 | err = -errno; |
94f04347 | 1199 | r = EXIT_CPUAFFINITY; |
8c7be95e | 1200 | goto fail_child; |
94f04347 LP |
1201 | } |
1202 | ||
9eba9da4 LP |
1203 | if (context->ioprio_set) |
1204 | if (ioprio_set(IOPRIO_WHO_PROCESS, 0, context->ioprio) < 0) { | |
4c2630eb | 1205 | err = -errno; |
9eba9da4 | 1206 | r = EXIT_IOPRIO; |
8c7be95e | 1207 | goto fail_child; |
9eba9da4 LP |
1208 | } |
1209 | ||
d88a251b | 1210 | if (context->timer_slack_nsec != (nsec_t) -1) |
03fae018 | 1211 | if (prctl(PR_SET_TIMERSLACK, context->timer_slack_nsec) < 0) { |
4c2630eb | 1212 | err = -errno; |
94f04347 | 1213 | r = EXIT_TIMERSLACK; |
8c7be95e | 1214 | goto fail_child; |
94f04347 LP |
1215 | } |
1216 | ||
169c1bda | 1217 | if (context->utmp_id) |
0ad26e09 | 1218 | utmp_put_init_process(context->utmp_id, getpid(), getsid(0), context->tty_path); |
169c1bda | 1219 | |
81a2b7ce LP |
1220 | if (context->user) { |
1221 | username = context->user; | |
d05c5031 | 1222 | err = get_user_creds(&username, &uid, &gid, &home, NULL); |
4c2630eb | 1223 | if (err < 0) { |
81a2b7ce | 1224 | r = EXIT_USER; |
8c7be95e | 1225 | goto fail_child; |
81a2b7ce | 1226 | } |
d8b4e2e9 | 1227 | |
4c2630eb MS |
1228 | if (is_terminal_input(context->std_input)) { |
1229 | err = chown_terminal(STDIN_FILENO, uid); | |
1230 | if (err < 0) { | |
d8b4e2e9 | 1231 | r = EXIT_STDIN; |
8c7be95e | 1232 | goto fail_child; |
d8b4e2e9 | 1233 | } |
4c2630eb | 1234 | } |
64747e2d | 1235 | |
4c2630eb MS |
1236 | if (cgroup_bondings && context->control_group_modify) { |
1237 | err = cgroup_bonding_set_group_access_list(cgroup_bondings, 0755, uid, gid); | |
1238 | if (err >= 0) | |
891703e1 | 1239 | err = cgroup_bonding_set_task_access_list(cgroup_bondings, 0644, uid, gid, context->control_group_persistent); |
4c2630eb | 1240 | if (err < 0) { |
64747e2d LP |
1241 | r = EXIT_CGROUP; |
1242 | goto fail_child; | |
1243 | } | |
8d53b453 LP |
1244 | |
1245 | set_access = true; | |
1246 | } | |
1247 | } | |
1248 | ||
891703e1 KS |
1249 | if (cgroup_bondings && !set_access && context->control_group_persistent >= 0) { |
1250 | err = cgroup_bonding_set_task_access_list(cgroup_bondings, (mode_t) -1, (uid_t) -1, (uid_t) -1, context->control_group_persistent); | |
8d53b453 LP |
1251 | if (err < 0) { |
1252 | r = EXIT_CGROUP; | |
1253 | goto fail_child; | |
4c2630eb | 1254 | } |
81a2b7ce LP |
1255 | } |
1256 | ||
4c2630eb MS |
1257 | if (apply_permissions) { |
1258 | err = enforce_groups(context, username, gid); | |
1259 | if (err < 0) { | |
3b8bddde LP |
1260 | r = EXIT_GROUP; |
1261 | goto fail_child; | |
1262 | } | |
4c2630eb | 1263 | } |
3b8bddde LP |
1264 | |
1265 | umask(context->umask); | |
1266 | ||
5b6319dc | 1267 | #ifdef HAVE_PAM |
b7848021 | 1268 | if (apply_permissions && context->pam_name && username) { |
940c5210 | 1269 | err = setup_pam(context->pam_name, username, uid, context->tty_path, &pam_env, fds, n_fds); |
4c2630eb | 1270 | if (err < 0) { |
5b6319dc | 1271 | r = EXIT_PAM; |
8c7be95e | 1272 | goto fail_child; |
5b6319dc LP |
1273 | } |
1274 | } | |
1275 | #endif | |
ff01d048 LP |
1276 | if (context->private_network) { |
1277 | if (unshare(CLONE_NEWNET) < 0) { | |
4c2630eb | 1278 | err = -errno; |
ff01d048 LP |
1279 | r = EXIT_NETWORK; |
1280 | goto fail_child; | |
1281 | } | |
1282 | ||
1283 | loopback_setup(); | |
1284 | } | |
5b6319dc | 1285 | |
04aa0cb9 LP |
1286 | if (strv_length(context->read_write_dirs) > 0 || |
1287 | strv_length(context->read_only_dirs) > 0 || | |
1288 | strv_length(context->inaccessible_dirs) > 0 || | |
ac0930c8 | 1289 | context->mount_flags != 0 || |
4c2630eb MS |
1290 | context->private_tmp) { |
1291 | err = setup_namespace(context->read_write_dirs, | |
1292 | context->read_only_dirs, | |
1293 | context->inaccessible_dirs, | |
1294 | context->private_tmp, | |
1295 | context->mount_flags); | |
1296 | if (err < 0) { | |
1297 | r = EXIT_NAMESPACE; | |
8c7be95e | 1298 | goto fail_child; |
4c2630eb MS |
1299 | } |
1300 | } | |
04aa0cb9 | 1301 | |
81a2b7ce LP |
1302 | if (apply_chroot) { |
1303 | if (context->root_directory) | |
1304 | if (chroot(context->root_directory) < 0) { | |
4c2630eb | 1305 | err = -errno; |
81a2b7ce | 1306 | r = EXIT_CHROOT; |
8c7be95e | 1307 | goto fail_child; |
81a2b7ce LP |
1308 | } |
1309 | ||
1310 | if (chdir(context->working_directory ? context->working_directory : "/") < 0) { | |
4c2630eb | 1311 | err = -errno; |
81a2b7ce | 1312 | r = EXIT_CHDIR; |
8c7be95e | 1313 | goto fail_child; |
81a2b7ce LP |
1314 | } |
1315 | } else { | |
b66871da | 1316 | char _cleanup_free_ *d = NULL; |
81a2b7ce LP |
1317 | |
1318 | if (asprintf(&d, "%s/%s", | |
1319 | context->root_directory ? context->root_directory : "", | |
1320 | context->working_directory ? context->working_directory : "") < 0) { | |
4c2630eb | 1321 | err = -ENOMEM; |
81a2b7ce | 1322 | r = EXIT_MEMORY; |
8c7be95e | 1323 | goto fail_child; |
81a2b7ce LP |
1324 | } |
1325 | ||
1326 | if (chdir(d) < 0) { | |
4c2630eb | 1327 | err = -errno; |
81a2b7ce | 1328 | r = EXIT_CHDIR; |
8c7be95e | 1329 | goto fail_child; |
81a2b7ce | 1330 | } |
81a2b7ce LP |
1331 | } |
1332 | ||
fc9b2a84 LP |
1333 | /* We repeat the fd closing here, to make sure that |
1334 | * nothing is leaked from the PAM modules */ | |
4c2630eb MS |
1335 | err = close_all_fds(fds, n_fds); |
1336 | if (err >= 0) | |
1337 | err = shift_fds(fds, n_fds); | |
1338 | if (err >= 0) | |
1339 | err = flags_fds(fds, n_fds, context->non_blocking); | |
1340 | if (err < 0) { | |
034c6ed7 | 1341 | r = EXIT_FDS; |
8c7be95e | 1342 | goto fail_child; |
034c6ed7 LP |
1343 | } |
1344 | ||
81a2b7ce | 1345 | if (apply_permissions) { |
034c6ed7 | 1346 | |
81a2b7ce LP |
1347 | for (i = 0; i < RLIMIT_NLIMITS; i++) { |
1348 | if (!context->rlimit[i]) | |
1349 | continue; | |
1350 | ||
68faf98c | 1351 | if (setrlimit_closest(i, context->rlimit[i]) < 0) { |
4c2630eb | 1352 | err = -errno; |
81a2b7ce | 1353 | r = EXIT_LIMITS; |
8c7be95e | 1354 | goto fail_child; |
81a2b7ce | 1355 | } |
034c6ed7 | 1356 | } |
034c6ed7 | 1357 | |
4c2630eb | 1358 | if (context->capability_bounding_set_drop) { |
ec8927ca | 1359 | err = capability_bounding_set_drop(context->capability_bounding_set_drop, false); |
4c2630eb | 1360 | if (err < 0) { |
73090dc8 LP |
1361 | r = EXIT_CAPABILITIES; |
1362 | goto fail_child; | |
1363 | } | |
4c2630eb | 1364 | } |
260abb78 | 1365 | |
4c2630eb MS |
1366 | if (context->user) { |
1367 | err = enforce_user(context, uid); | |
1368 | if (err < 0) { | |
81a2b7ce | 1369 | r = EXIT_USER; |
8c7be95e | 1370 | goto fail_child; |
81a2b7ce | 1371 | } |
4c2630eb | 1372 | } |
81a2b7ce | 1373 | |
35b8ca3a | 1374 | /* PR_GET_SECUREBITS is not privileged, while |
693ced48 LP |
1375 | * PR_SET_SECUREBITS is. So to suppress |
1376 | * potential EPERMs we'll try not to call | |
1377 | * PR_SET_SECUREBITS unless necessary. */ | |
1378 | if (prctl(PR_GET_SECUREBITS) != context->secure_bits) | |
1379 | if (prctl(PR_SET_SECUREBITS, context->secure_bits) < 0) { | |
4c2630eb | 1380 | err = -errno; |
693ced48 | 1381 | r = EXIT_SECUREBITS; |
8c7be95e | 1382 | goto fail_child; |
693ced48 | 1383 | } |
81a2b7ce LP |
1384 | |
1385 | if (context->capabilities) | |
1386 | if (cap_set_proc(context->capabilities) < 0) { | |
4c2630eb | 1387 | err = -errno; |
81a2b7ce | 1388 | r = EXIT_CAPABILITIES; |
8c7be95e | 1389 | goto fail_child; |
81a2b7ce | 1390 | } |
8351ceae LP |
1391 | |
1392 | if (context->no_new_privileges) | |
1393 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) { | |
1394 | err = -errno; | |
1395 | r = EXIT_NO_NEW_PRIVILEGES; | |
1396 | goto fail_child; | |
1397 | } | |
1398 | ||
1399 | if (context->syscall_filter) { | |
1400 | err = apply_seccomp(context->syscall_filter); | |
1401 | if (err < 0) { | |
1402 | r = EXIT_SECCOMP; | |
1403 | goto fail_child; | |
1404 | } | |
1405 | } | |
94f04347 LP |
1406 | } |
1407 | ||
e3aa71c3 | 1408 | if (!(our_env = new0(char*, 7))) { |
4c2630eb | 1409 | err = -ENOMEM; |
81a2b7ce | 1410 | r = EXIT_MEMORY; |
8c7be95e | 1411 | goto fail_child; |
81a2b7ce | 1412 | } |
034c6ed7 | 1413 | |
81a2b7ce | 1414 | if (n_fds > 0) |
bb00e604 | 1415 | if (asprintf(our_env + n_env++, "LISTEN_PID=%lu", (unsigned long) getpid()) < 0 || |
81a2b7ce | 1416 | asprintf(our_env + n_env++, "LISTEN_FDS=%u", n_fds) < 0) { |
4c2630eb | 1417 | err = -ENOMEM; |
81a2b7ce | 1418 | r = EXIT_MEMORY; |
8c7be95e | 1419 | goto fail_child; |
81a2b7ce | 1420 | } |
034c6ed7 | 1421 | |
81a2b7ce LP |
1422 | if (home) |
1423 | if (asprintf(our_env + n_env++, "HOME=%s", home) < 0) { | |
4c2630eb | 1424 | err = -ENOMEM; |
81a2b7ce | 1425 | r = EXIT_MEMORY; |
8c7be95e | 1426 | goto fail_child; |
81a2b7ce | 1427 | } |
034c6ed7 | 1428 | |
81a2b7ce LP |
1429 | if (username) |
1430 | if (asprintf(our_env + n_env++, "LOGNAME=%s", username) < 0 || | |
1431 | asprintf(our_env + n_env++, "USER=%s", username) < 0) { | |
4c2630eb | 1432 | err = -ENOMEM; |
81a2b7ce | 1433 | r = EXIT_MEMORY; |
8c7be95e | 1434 | goto fail_child; |
81a2b7ce | 1435 | } |
034c6ed7 | 1436 | |
e3aa71c3 LP |
1437 | if (is_terminal_input(context->std_input) || |
1438 | context->std_output == EXEC_OUTPUT_TTY || | |
1439 | context->std_error == EXEC_OUTPUT_TTY) | |
1440 | if (!(our_env[n_env++] = strdup(default_term_for_tty(tty_path(context))))) { | |
4c2630eb | 1441 | err = -ENOMEM; |
e3aa71c3 | 1442 | r = EXIT_MEMORY; |
8c7be95e | 1443 | goto fail_child; |
e3aa71c3 LP |
1444 | } |
1445 | ||
1446 | assert(n_env <= 7); | |
4e85aff4 | 1447 | |
5b6319dc | 1448 | if (!(final_env = strv_env_merge( |
8c7be95e | 1449 | 5, |
5b6319dc LP |
1450 | environment, |
1451 | our_env, | |
1452 | context->environment, | |
8c7be95e | 1453 | files_env, |
5b6319dc LP |
1454 | pam_env, |
1455 | NULL))) { | |
4c2630eb | 1456 | err = -ENOMEM; |
81a2b7ce | 1457 | r = EXIT_MEMORY; |
8c7be95e | 1458 | goto fail_child; |
81a2b7ce | 1459 | } |
034c6ed7 | 1460 | |
fab56fc5 | 1461 | if (!(final_argv = replace_env_argv(argv, final_env))) { |
4c2630eb | 1462 | err = -ENOMEM; |
fab56fc5 | 1463 | r = EXIT_MEMORY; |
8c7be95e | 1464 | goto fail_child; |
fab56fc5 LP |
1465 | } |
1466 | ||
a6ff950e LP |
1467 | final_env = strv_env_clean(final_env); |
1468 | ||
fab56fc5 | 1469 | execve(command->path, final_argv, final_env); |
4c2630eb | 1470 | err = -errno; |
034c6ed7 LP |
1471 | r = EXIT_EXEC; |
1472 | ||
8c7be95e | 1473 | fail_child: |
4c2630eb MS |
1474 | if (r != 0) { |
1475 | log_open(); | |
23635a85 ZJS |
1476 | log_struct(LOG_ERR, MESSAGE_ID(SD_MESSAGE_SPAWN_FAILED), |
1477 | "EXECUTABLE=%s", command->path, | |
1478 | "MESSAGE=Failed at step %s spawning %s: %s", | |
1479 | exit_status_to_string(r, EXIT_STATUS_SYSTEMD), | |
1480 | command->path, strerror(-err), | |
1481 | "ERRNO=%d", -err, | |
1482 | NULL); | |
1483 | log_close(); | |
4c2630eb MS |
1484 | } |
1485 | ||
034c6ed7 LP |
1486 | _exit(r); |
1487 | } | |
1488 | ||
bbc9006e MT |
1489 | log_struct_unit(LOG_DEBUG, |
1490 | unit_id, | |
23635a85 ZJS |
1491 | "MESSAGE=Forked %s as %lu", |
1492 | command->path, (unsigned long) pid, | |
1493 | NULL); | |
1494 | ||
80876c20 LP |
1495 | /* We add the new process to the cgroup both in the child (so |
1496 | * that we can be sure that no user code is ever executed | |
1497 | * outside of the cgroup) and in the parent (so that we can be | |
1498 | * sure that when we kill the cgroup the process will be | |
1499 | * killed too). */ | |
1500 | if (cgroup_bondings) | |
ecedd90f | 1501 | cgroup_bonding_install_list(cgroup_bondings, pid, cgroup_suffix); |
2da3263a | 1502 | |
b58b4116 | 1503 | exec_status_start(&command->exec_status, pid); |
9fb86720 | 1504 | |
034c6ed7 | 1505 | *ret = pid; |
5cb5a6ff LP |
1506 | return 0; |
1507 | } | |
1508 | ||
034c6ed7 LP |
1509 | void exec_context_init(ExecContext *c) { |
1510 | assert(c); | |
1511 | ||
4c12626c | 1512 | c->umask = 0022; |
9eba9da4 | 1513 | c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0); |
94f04347 | 1514 | c->cpu_sched_policy = SCHED_OTHER; |
071830ff | 1515 | c->syslog_priority = LOG_DAEMON|LOG_INFO; |
74922904 | 1516 | c->syslog_level_prefix = true; |
891703e1 | 1517 | c->control_group_persistent = -1; |
353e12c2 | 1518 | c->ignore_sigpipe = true; |
d88a251b | 1519 | c->timer_slack_nsec = (nsec_t) -1; |
034c6ed7 LP |
1520 | } |
1521 | ||
1522 | void exec_context_done(ExecContext *c) { | |
5cb5a6ff LP |
1523 | unsigned l; |
1524 | ||
1525 | assert(c); | |
1526 | ||
1527 | strv_free(c->environment); | |
034c6ed7 | 1528 | c->environment = NULL; |
5cb5a6ff | 1529 | |
8c7be95e LP |
1530 | strv_free(c->environment_files); |
1531 | c->environment_files = NULL; | |
1532 | ||
034c6ed7 | 1533 | for (l = 0; l < ELEMENTSOF(c->rlimit); l++) { |
5cb5a6ff | 1534 | free(c->rlimit[l]); |
034c6ed7 LP |
1535 | c->rlimit[l] = NULL; |
1536 | } | |
1537 | ||
9eba9da4 LP |
1538 | free(c->working_directory); |
1539 | c->working_directory = NULL; | |
1540 | free(c->root_directory); | |
1541 | c->root_directory = NULL; | |
5cb5a6ff | 1542 | |
80876c20 LP |
1543 | free(c->tty_path); |
1544 | c->tty_path = NULL; | |
1545 | ||
df1f0afe LP |
1546 | free(c->tcpwrap_name); |
1547 | c->tcpwrap_name = NULL; | |
1548 | ||
071830ff LP |
1549 | free(c->syslog_identifier); |
1550 | c->syslog_identifier = NULL; | |
1551 | ||
5cb5a6ff | 1552 | free(c->user); |
034c6ed7 LP |
1553 | c->user = NULL; |
1554 | ||
5cb5a6ff | 1555 | free(c->group); |
034c6ed7 LP |
1556 | c->group = NULL; |
1557 | ||
1558 | strv_free(c->supplementary_groups); | |
1559 | c->supplementary_groups = NULL; | |
94f04347 | 1560 | |
5b6319dc LP |
1561 | free(c->pam_name); |
1562 | c->pam_name = NULL; | |
1563 | ||
94f04347 LP |
1564 | if (c->capabilities) { |
1565 | cap_free(c->capabilities); | |
1566 | c->capabilities = NULL; | |
1567 | } | |
15ae422b LP |
1568 | |
1569 | strv_free(c->read_only_dirs); | |
1570 | c->read_only_dirs = NULL; | |
1571 | ||
1572 | strv_free(c->read_write_dirs); | |
1573 | c->read_write_dirs = NULL; | |
1574 | ||
1575 | strv_free(c->inaccessible_dirs); | |
1576 | c->inaccessible_dirs = NULL; | |
82c121a4 LP |
1577 | |
1578 | if (c->cpuset) | |
1579 | CPU_FREE(c->cpuset); | |
86a3475b LP |
1580 | |
1581 | free(c->utmp_id); | |
1582 | c->utmp_id = NULL; | |
b9a0e010 LP |
1583 | |
1584 | free(c->syscall_filter); | |
1585 | c->syscall_filter = NULL; | |
5cb5a6ff LP |
1586 | } |
1587 | ||
43d0fcbd LP |
1588 | void exec_command_done(ExecCommand *c) { |
1589 | assert(c); | |
1590 | ||
1591 | free(c->path); | |
1592 | c->path = NULL; | |
1593 | ||
1594 | strv_free(c->argv); | |
1595 | c->argv = NULL; | |
1596 | } | |
1597 | ||
1598 | void exec_command_done_array(ExecCommand *c, unsigned n) { | |
1599 | unsigned i; | |
1600 | ||
1601 | for (i = 0; i < n; i++) | |
1602 | exec_command_done(c+i); | |
1603 | } | |
1604 | ||
5cb5a6ff LP |
1605 | void exec_command_free_list(ExecCommand *c) { |
1606 | ExecCommand *i; | |
1607 | ||
1608 | while ((i = c)) { | |
034c6ed7 | 1609 | LIST_REMOVE(ExecCommand, command, c, i); |
43d0fcbd | 1610 | exec_command_done(i); |
5cb5a6ff LP |
1611 | free(i); |
1612 | } | |
1613 | } | |
1614 | ||
034c6ed7 LP |
1615 | void exec_command_free_array(ExecCommand **c, unsigned n) { |
1616 | unsigned i; | |
1617 | ||
1618 | for (i = 0; i < n; i++) { | |
1619 | exec_command_free_list(c[i]); | |
1620 | c[i] = NULL; | |
1621 | } | |
1622 | } | |
1623 | ||
8c7be95e LP |
1624 | int exec_context_load_environment(const ExecContext *c, char ***l) { |
1625 | char **i, **r = NULL; | |
1626 | ||
1627 | assert(c); | |
1628 | assert(l); | |
1629 | ||
1630 | STRV_FOREACH(i, c->environment_files) { | |
1631 | char *fn; | |
1632 | int k; | |
1633 | bool ignore = false; | |
1634 | char **p; | |
2bef10ab PL |
1635 | glob_t pglob; |
1636 | int count, n; | |
8c7be95e LP |
1637 | |
1638 | fn = *i; | |
1639 | ||
1640 | if (fn[0] == '-') { | |
1641 | ignore = true; | |
1642 | fn ++; | |
1643 | } | |
1644 | ||
1645 | if (!path_is_absolute(fn)) { | |
1646 | ||
1647 | if (ignore) | |
1648 | continue; | |
1649 | ||
1650 | strv_free(r); | |
1651 | return -EINVAL; | |
1652 | } | |
1653 | ||
2bef10ab PL |
1654 | /* Filename supports globbing, take all matching files */ |
1655 | zero(pglob); | |
1656 | errno = 0; | |
1657 | if (glob(fn, 0, NULL, &pglob) != 0) { | |
1658 | globfree(&pglob); | |
1659 | if (ignore) | |
1660 | continue; | |
8c7be95e | 1661 | |
2bef10ab PL |
1662 | strv_free(r); |
1663 | return errno ? -errno : -EINVAL; | |
1664 | } | |
1665 | count = pglob.gl_pathc; | |
1666 | if (count == 0) { | |
1667 | globfree(&pglob); | |
8c7be95e LP |
1668 | if (ignore) |
1669 | continue; | |
1670 | ||
1671 | strv_free(r); | |
2bef10ab | 1672 | return -EINVAL; |
8c7be95e | 1673 | } |
2bef10ab PL |
1674 | for (n = 0; n < count; n++) { |
1675 | k = load_env_file(pglob.gl_pathv[n], &p); | |
1676 | if (k < 0) { | |
1677 | if (ignore) | |
1678 | continue; | |
8c7be95e | 1679 | |
2bef10ab PL |
1680 | strv_free(r); |
1681 | globfree(&pglob); | |
1682 | return k; | |
1683 | } | |
8c7be95e | 1684 | |
2bef10ab PL |
1685 | if (r == NULL) |
1686 | r = p; | |
1687 | else { | |
1688 | char **m; | |
8c7be95e | 1689 | |
2bef10ab PL |
1690 | m = strv_env_merge(2, r, p); |
1691 | strv_free(r); | |
1692 | strv_free(p); | |
8c7be95e | 1693 | |
2bef10ab PL |
1694 | if (!m) { |
1695 | globfree(&pglob); | |
1696 | return -ENOMEM; | |
1697 | } | |
1698 | ||
1699 | r = m; | |
1700 | } | |
8c7be95e | 1701 | } |
2bef10ab | 1702 | globfree(&pglob); |
8c7be95e LP |
1703 | } |
1704 | ||
1705 | *l = r; | |
1706 | ||
1707 | return 0; | |
1708 | } | |
1709 | ||
15ae422b LP |
1710 | static void strv_fprintf(FILE *f, char **l) { |
1711 | char **g; | |
1712 | ||
1713 | assert(f); | |
1714 | ||
1715 | STRV_FOREACH(g, l) | |
1716 | fprintf(f, " %s", *g); | |
1717 | } | |
1718 | ||
5cb5a6ff | 1719 | void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) { |
94f04347 LP |
1720 | char ** e; |
1721 | unsigned i; | |
9eba9da4 | 1722 | |
5cb5a6ff LP |
1723 | assert(c); |
1724 | assert(f); | |
1725 | ||
1726 | if (!prefix) | |
1727 | prefix = ""; | |
1728 | ||
1729 | fprintf(f, | |
94f04347 LP |
1730 | "%sUMask: %04o\n" |
1731 | "%sWorkingDirectory: %s\n" | |
451a074f | 1732 | "%sRootDirectory: %s\n" |
15ae422b | 1733 | "%sNonBlocking: %s\n" |
64747e2d | 1734 | "%sPrivateTmp: %s\n" |
ff01d048 | 1735 | "%sControlGroupModify: %s\n" |
891703e1 | 1736 | "%sControlGroupPersistent: %s\n" |
4819ff03 LP |
1737 | "%sPrivateNetwork: %s\n" |
1738 | "%sIgnoreSIGPIPE: %s\n", | |
5cb5a6ff | 1739 | prefix, c->umask, |
9eba9da4 | 1740 | prefix, c->working_directory ? c->working_directory : "/", |
451a074f | 1741 | prefix, c->root_directory ? c->root_directory : "/", |
15ae422b | 1742 | prefix, yes_no(c->non_blocking), |
64747e2d | 1743 | prefix, yes_no(c->private_tmp), |
ff01d048 | 1744 | prefix, yes_no(c->control_group_modify), |
891703e1 | 1745 | prefix, yes_no(c->control_group_persistent), |
4819ff03 LP |
1746 | prefix, yes_no(c->private_network), |
1747 | prefix, yes_no(c->ignore_sigpipe)); | |
fb33a393 | 1748 | |
8c7be95e LP |
1749 | STRV_FOREACH(e, c->environment) |
1750 | fprintf(f, "%sEnvironment: %s\n", prefix, *e); | |
1751 | ||
1752 | STRV_FOREACH(e, c->environment_files) | |
1753 | fprintf(f, "%sEnvironmentFile: %s\n", prefix, *e); | |
94f04347 | 1754 | |
df1f0afe LP |
1755 | if (c->tcpwrap_name) |
1756 | fprintf(f, | |
1757 | "%sTCPWrapName: %s\n", | |
1758 | prefix, c->tcpwrap_name); | |
1759 | ||
fb33a393 LP |
1760 | if (c->nice_set) |
1761 | fprintf(f, | |
1762 | "%sNice: %i\n", | |
1763 | prefix, c->nice); | |
1764 | ||
dd6c17b1 | 1765 | if (c->oom_score_adjust_set) |
fb33a393 | 1766 | fprintf(f, |
dd6c17b1 LP |
1767 | "%sOOMScoreAdjust: %i\n", |
1768 | prefix, c->oom_score_adjust); | |
9eba9da4 | 1769 | |
94f04347 LP |
1770 | for (i = 0; i < RLIM_NLIMITS; i++) |
1771 | if (c->rlimit[i]) | |
ea430986 | 1772 | fprintf(f, "%s%s: %llu\n", prefix, rlimit_to_string(i), (unsigned long long) c->rlimit[i]->rlim_max); |
94f04347 | 1773 | |
f8b69d1d MS |
1774 | if (c->ioprio_set) { |
1775 | char *class_str; | |
1776 | int r; | |
1777 | ||
1778 | r = ioprio_class_to_string_alloc(IOPRIO_PRIO_CLASS(c->ioprio), &class_str); | |
1779 | if (r < 0) | |
1780 | class_str = NULL; | |
9eba9da4 LP |
1781 | fprintf(f, |
1782 | "%sIOSchedulingClass: %s\n" | |
1783 | "%sIOPriority: %i\n", | |
f8b69d1d | 1784 | prefix, strna(class_str), |
9eba9da4 | 1785 | prefix, (int) IOPRIO_PRIO_DATA(c->ioprio)); |
f8b69d1d MS |
1786 | free(class_str); |
1787 | } | |
94f04347 | 1788 | |
f8b69d1d MS |
1789 | if (c->cpu_sched_set) { |
1790 | char *policy_str; | |
1791 | int r; | |
1792 | ||
1793 | r = sched_policy_to_string_alloc(c->cpu_sched_policy, &policy_str); | |
1794 | if (r < 0) | |
1795 | policy_str = NULL; | |
94f04347 LP |
1796 | fprintf(f, |
1797 | "%sCPUSchedulingPolicy: %s\n" | |
38b48754 LP |
1798 | "%sCPUSchedulingPriority: %i\n" |
1799 | "%sCPUSchedulingResetOnFork: %s\n", | |
f8b69d1d | 1800 | prefix, strna(policy_str), |
38b48754 LP |
1801 | prefix, c->cpu_sched_priority, |
1802 | prefix, yes_no(c->cpu_sched_reset_on_fork)); | |
f8b69d1d | 1803 | free(policy_str); |
b929bf04 | 1804 | } |
94f04347 | 1805 | |
82c121a4 | 1806 | if (c->cpuset) { |
94f04347 | 1807 | fprintf(f, "%sCPUAffinity:", prefix); |
82c121a4 LP |
1808 | for (i = 0; i < c->cpuset_ncpus; i++) |
1809 | if (CPU_ISSET_S(i, CPU_ALLOC_SIZE(c->cpuset_ncpus), c->cpuset)) | |
94f04347 LP |
1810 | fprintf(f, " %i", i); |
1811 | fputs("\n", f); | |
1812 | } | |
1813 | ||
d88a251b | 1814 | if (c->timer_slack_nsec != (nsec_t) -1) |
f96096db | 1815 | fprintf(f, "%sTimerSlackNSec: %lu\n", prefix, (unsigned long)c->timer_slack_nsec); |
94f04347 LP |
1816 | |
1817 | fprintf(f, | |
80876c20 LP |
1818 | "%sStandardInput: %s\n" |
1819 | "%sStandardOutput: %s\n" | |
1820 | "%sStandardError: %s\n", | |
1821 | prefix, exec_input_to_string(c->std_input), | |
1822 | prefix, exec_output_to_string(c->std_output), | |
1823 | prefix, exec_output_to_string(c->std_error)); | |
1824 | ||
1825 | if (c->tty_path) | |
1826 | fprintf(f, | |
6ea832a2 LP |
1827 | "%sTTYPath: %s\n" |
1828 | "%sTTYReset: %s\n" | |
1829 | "%sTTYVHangup: %s\n" | |
1830 | "%sTTYVTDisallocate: %s\n", | |
1831 | prefix, c->tty_path, | |
1832 | prefix, yes_no(c->tty_reset), | |
1833 | prefix, yes_no(c->tty_vhangup), | |
1834 | prefix, yes_no(c->tty_vt_disallocate)); | |
94f04347 | 1835 | |
706343f4 LP |
1836 | if (c->std_output == EXEC_OUTPUT_SYSLOG || c->std_output == EXEC_OUTPUT_KMSG || c->std_output == EXEC_OUTPUT_JOURNAL || |
1837 | c->std_output == EXEC_OUTPUT_SYSLOG_AND_CONSOLE || c->std_output == EXEC_OUTPUT_KMSG_AND_CONSOLE || c->std_output == EXEC_OUTPUT_JOURNAL_AND_CONSOLE || | |
1838 | c->std_error == EXEC_OUTPUT_SYSLOG || c->std_error == EXEC_OUTPUT_KMSG || c->std_error == EXEC_OUTPUT_JOURNAL || | |
f8b69d1d MS |
1839 | c->std_error == EXEC_OUTPUT_SYSLOG_AND_CONSOLE || c->std_error == EXEC_OUTPUT_KMSG_AND_CONSOLE || c->std_error == EXEC_OUTPUT_JOURNAL_AND_CONSOLE) { |
1840 | char *fac_str, *lvl_str; | |
1841 | int r; | |
1842 | ||
1843 | r = log_facility_unshifted_to_string_alloc(c->syslog_priority >> 3, &fac_str); | |
1844 | if (r < 0) | |
1845 | fac_str = NULL; | |
1846 | ||
1847 | r = log_level_to_string_alloc(LOG_PRI(c->syslog_priority), &lvl_str); | |
1848 | if (r < 0) | |
1849 | lvl_str = NULL; | |
1850 | ||
94f04347 LP |
1851 | fprintf(f, |
1852 | "%sSyslogFacility: %s\n" | |
1853 | "%sSyslogLevel: %s\n", | |
f8b69d1d MS |
1854 | prefix, strna(fac_str), |
1855 | prefix, strna(lvl_str)); | |
1856 | free(lvl_str); | |
1857 | free(fac_str); | |
1858 | } | |
94f04347 LP |
1859 | |
1860 | if (c->capabilities) { | |
1861 | char *t; | |
1862 | if ((t = cap_to_text(c->capabilities, NULL))) { | |
1863 | fprintf(f, "%sCapabilities: %s\n", | |
1864 | prefix, t); | |
1865 | cap_free(t); | |
1866 | } | |
1867 | } | |
1868 | ||
1869 | if (c->secure_bits) | |
1870 | fprintf(f, "%sSecure Bits:%s%s%s%s%s%s\n", | |
1871 | prefix, | |
1872 | (c->secure_bits & SECURE_KEEP_CAPS) ? " keep-caps" : "", | |
1873 | (c->secure_bits & SECURE_KEEP_CAPS_LOCKED) ? " keep-caps-locked" : "", | |
1874 | (c->secure_bits & SECURE_NO_SETUID_FIXUP) ? " no-setuid-fixup" : "", | |
1875 | (c->secure_bits & SECURE_NO_SETUID_FIXUP_LOCKED) ? " no-setuid-fixup-locked" : "", | |
1876 | (c->secure_bits & SECURE_NOROOT) ? " noroot" : "", | |
1877 | (c->secure_bits & SECURE_NOROOT_LOCKED) ? "noroot-locked" : ""); | |
1878 | ||
1879 | if (c->capability_bounding_set_drop) { | |
ae556c21 | 1880 | unsigned long l; |
260abb78 | 1881 | fprintf(f, "%sCapabilityBoundingSet:", prefix); |
94f04347 | 1882 | |
64685e0c | 1883 | for (l = 0; l <= cap_last_cap(); l++) |
ae556c21 | 1884 | if (!(c->capability_bounding_set_drop & ((uint64_t) 1ULL << (uint64_t) l))) { |
94f04347 LP |
1885 | char *t; |
1886 | ||
ae556c21 | 1887 | if ((t = cap_to_name(l))) { |
94f04347 | 1888 | fprintf(f, " %s", t); |
260abb78 | 1889 | cap_free(t); |
94f04347 LP |
1890 | } |
1891 | } | |
1892 | ||
1893 | fputs("\n", f); | |
1894 | } | |
1895 | ||
1896 | if (c->user) | |
f2d3769a | 1897 | fprintf(f, "%sUser: %s\n", prefix, c->user); |
94f04347 | 1898 | if (c->group) |
f2d3769a | 1899 | fprintf(f, "%sGroup: %s\n", prefix, c->group); |
94f04347 | 1900 | |
15ae422b | 1901 | if (strv_length(c->supplementary_groups) > 0) { |
94f04347 | 1902 | fprintf(f, "%sSupplementaryGroups:", prefix); |
15ae422b LP |
1903 | strv_fprintf(f, c->supplementary_groups); |
1904 | fputs("\n", f); | |
1905 | } | |
94f04347 | 1906 | |
5b6319dc | 1907 | if (c->pam_name) |
f2d3769a | 1908 | fprintf(f, "%sPAMName: %s\n", prefix, c->pam_name); |
5b6319dc | 1909 | |
15ae422b LP |
1910 | if (strv_length(c->read_write_dirs) > 0) { |
1911 | fprintf(f, "%sReadWriteDirs:", prefix); | |
1912 | strv_fprintf(f, c->read_write_dirs); | |
1913 | fputs("\n", f); | |
1914 | } | |
1915 | ||
1916 | if (strv_length(c->read_only_dirs) > 0) { | |
1917 | fprintf(f, "%sReadOnlyDirs:", prefix); | |
1918 | strv_fprintf(f, c->read_only_dirs); | |
1919 | fputs("\n", f); | |
1920 | } | |
94f04347 | 1921 | |
15ae422b LP |
1922 | if (strv_length(c->inaccessible_dirs) > 0) { |
1923 | fprintf(f, "%sInaccessibleDirs:", prefix); | |
1924 | strv_fprintf(f, c->inaccessible_dirs); | |
94f04347 LP |
1925 | fputs("\n", f); |
1926 | } | |
2e22afe9 | 1927 | |
169c1bda LP |
1928 | if (c->utmp_id) |
1929 | fprintf(f, | |
1930 | "%sUtmpIdentifier: %s\n", | |
1931 | prefix, c->utmp_id); | |
5cb5a6ff LP |
1932 | } |
1933 | ||
b58b4116 | 1934 | void exec_status_start(ExecStatus *s, pid_t pid) { |
034c6ed7 | 1935 | assert(s); |
5cb5a6ff | 1936 | |
b58b4116 LP |
1937 | zero(*s); |
1938 | s->pid = pid; | |
1939 | dual_timestamp_get(&s->start_timestamp); | |
1940 | } | |
1941 | ||
6ea832a2 | 1942 | void exec_status_exit(ExecStatus *s, ExecContext *context, pid_t pid, int code, int status) { |
b58b4116 LP |
1943 | assert(s); |
1944 | ||
0b1f4ae6 | 1945 | if (s->pid && s->pid != pid) |
b58b4116 LP |
1946 | zero(*s); |
1947 | ||
034c6ed7 | 1948 | s->pid = pid; |
63983207 | 1949 | dual_timestamp_get(&s->exit_timestamp); |
9fb86720 | 1950 | |
034c6ed7 LP |
1951 | s->code = code; |
1952 | s->status = status; | |
169c1bda | 1953 | |
6ea832a2 LP |
1954 | if (context) { |
1955 | if (context->utmp_id) | |
1956 | utmp_put_dead_process(context->utmp_id, pid, code, status); | |
1957 | ||
1958 | exec_context_tty_reset(context); | |
1959 | } | |
9fb86720 LP |
1960 | } |
1961 | ||
1962 | void exec_status_dump(ExecStatus *s, FILE *f, const char *prefix) { | |
1963 | char buf[FORMAT_TIMESTAMP_MAX]; | |
1964 | ||
1965 | assert(s); | |
1966 | assert(f); | |
1967 | ||
1968 | if (!prefix) | |
1969 | prefix = ""; | |
1970 | ||
1971 | if (s->pid <= 0) | |
1972 | return; | |
1973 | ||
1974 | fprintf(f, | |
bb00e604 LP |
1975 | "%sPID: %lu\n", |
1976 | prefix, (unsigned long) s->pid); | |
9fb86720 | 1977 | |
63983207 | 1978 | if (s->start_timestamp.realtime > 0) |
9fb86720 LP |
1979 | fprintf(f, |
1980 | "%sStart Timestamp: %s\n", | |
63983207 | 1981 | prefix, format_timestamp(buf, sizeof(buf), s->start_timestamp.realtime)); |
9fb86720 | 1982 | |
63983207 | 1983 | if (s->exit_timestamp.realtime > 0) |
9fb86720 LP |
1984 | fprintf(f, |
1985 | "%sExit Timestamp: %s\n" | |
1986 | "%sExit Code: %s\n" | |
1987 | "%sExit Status: %i\n", | |
63983207 | 1988 | prefix, format_timestamp(buf, sizeof(buf), s->exit_timestamp.realtime), |
9fb86720 LP |
1989 | prefix, sigchld_code_to_string(s->code), |
1990 | prefix, s->status); | |
5cb5a6ff | 1991 | } |
44d8db9e | 1992 | |
9e2f7c11 | 1993 | char *exec_command_line(char **argv) { |
44d8db9e LP |
1994 | size_t k; |
1995 | char *n, *p, **a; | |
1996 | bool first = true; | |
1997 | ||
9e2f7c11 | 1998 | assert(argv); |
44d8db9e | 1999 | |
9164977d | 2000 | k = 1; |
9e2f7c11 | 2001 | STRV_FOREACH(a, argv) |
44d8db9e LP |
2002 | k += strlen(*a)+3; |
2003 | ||
2004 | if (!(n = new(char, k))) | |
2005 | return NULL; | |
2006 | ||
2007 | p = n; | |
9e2f7c11 | 2008 | STRV_FOREACH(a, argv) { |
44d8db9e LP |
2009 | |
2010 | if (!first) | |
2011 | *(p++) = ' '; | |
2012 | else | |
2013 | first = false; | |
2014 | ||
2015 | if (strpbrk(*a, WHITESPACE)) { | |
2016 | *(p++) = '\''; | |
2017 | p = stpcpy(p, *a); | |
2018 | *(p++) = '\''; | |
2019 | } else | |
2020 | p = stpcpy(p, *a); | |
2021 | ||
2022 | } | |
2023 | ||
9164977d LP |
2024 | *p = 0; |
2025 | ||
44d8db9e LP |
2026 | /* FIXME: this doesn't really handle arguments that have |
2027 | * spaces and ticks in them */ | |
2028 | ||
2029 | return n; | |
2030 | } | |
2031 | ||
2032 | void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) { | |
9fb86720 LP |
2033 | char *p2; |
2034 | const char *prefix2; | |
2035 | ||
44d8db9e LP |
2036 | char *cmd; |
2037 | ||
2038 | assert(c); | |
2039 | assert(f); | |
2040 | ||
2041 | if (!prefix) | |
2042 | prefix = ""; | |
9fb86720 LP |
2043 | p2 = strappend(prefix, "\t"); |
2044 | prefix2 = p2 ? p2 : prefix; | |
44d8db9e | 2045 | |
9e2f7c11 | 2046 | cmd = exec_command_line(c->argv); |
44d8db9e LP |
2047 | |
2048 | fprintf(f, | |
2049 | "%sCommand Line: %s\n", | |
2050 | prefix, cmd ? cmd : strerror(ENOMEM)); | |
2051 | ||
2052 | free(cmd); | |
9fb86720 LP |
2053 | |
2054 | exec_status_dump(&c->exec_status, f, prefix2); | |
2055 | ||
2056 | free(p2); | |
44d8db9e LP |
2057 | } |
2058 | ||
2059 | void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix) { | |
2060 | assert(f); | |
2061 | ||
2062 | if (!prefix) | |
2063 | prefix = ""; | |
2064 | ||
2065 | LIST_FOREACH(command, c, c) | |
2066 | exec_command_dump(c, f, prefix); | |
2067 | } | |
94f04347 | 2068 | |
a6a80b4f LP |
2069 | void exec_command_append_list(ExecCommand **l, ExecCommand *e) { |
2070 | ExecCommand *end; | |
2071 | ||
2072 | assert(l); | |
2073 | assert(e); | |
2074 | ||
2075 | if (*l) { | |
35b8ca3a | 2076 | /* It's kind of important, that we keep the order here */ |
a6a80b4f LP |
2077 | LIST_FIND_TAIL(ExecCommand, command, *l, end); |
2078 | LIST_INSERT_AFTER(ExecCommand, command, *l, end, e); | |
2079 | } else | |
2080 | *l = e; | |
2081 | } | |
2082 | ||
26fd040d LP |
2083 | int exec_command_set(ExecCommand *c, const char *path, ...) { |
2084 | va_list ap; | |
2085 | char **l, *p; | |
2086 | ||
2087 | assert(c); | |
2088 | assert(path); | |
2089 | ||
2090 | va_start(ap, path); | |
2091 | l = strv_new_ap(path, ap); | |
2092 | va_end(ap); | |
2093 | ||
2094 | if (!l) | |
2095 | return -ENOMEM; | |
2096 | ||
2097 | if (!(p = strdup(path))) { | |
2098 | strv_free(l); | |
2099 | return -ENOMEM; | |
2100 | } | |
2101 | ||
2102 | free(c->path); | |
2103 | c->path = p; | |
2104 | ||
2105 | strv_free(c->argv); | |
2106 | c->argv = l; | |
2107 | ||
2108 | return 0; | |
2109 | } | |
2110 | ||
80876c20 LP |
2111 | static const char* const exec_input_table[_EXEC_INPUT_MAX] = { |
2112 | [EXEC_INPUT_NULL] = "null", | |
2113 | [EXEC_INPUT_TTY] = "tty", | |
2114 | [EXEC_INPUT_TTY_FORCE] = "tty-force", | |
4f2d528d LP |
2115 | [EXEC_INPUT_TTY_FAIL] = "tty-fail", |
2116 | [EXEC_INPUT_SOCKET] = "socket" | |
80876c20 LP |
2117 | }; |
2118 | ||
8a0867d6 LP |
2119 | DEFINE_STRING_TABLE_LOOKUP(exec_input, ExecInput); |
2120 | ||
94f04347 | 2121 | static const char* const exec_output_table[_EXEC_OUTPUT_MAX] = { |
80876c20 | 2122 | [EXEC_OUTPUT_INHERIT] = "inherit", |
94f04347 | 2123 | [EXEC_OUTPUT_NULL] = "null", |
80876c20 | 2124 | [EXEC_OUTPUT_TTY] = "tty", |
94f04347 | 2125 | [EXEC_OUTPUT_SYSLOG] = "syslog", |
28dbc1e8 | 2126 | [EXEC_OUTPUT_SYSLOG_AND_CONSOLE] = "syslog+console", |
9a6bca7a | 2127 | [EXEC_OUTPUT_KMSG] = "kmsg", |
28dbc1e8 | 2128 | [EXEC_OUTPUT_KMSG_AND_CONSOLE] = "kmsg+console", |
706343f4 LP |
2129 | [EXEC_OUTPUT_JOURNAL] = "journal", |
2130 | [EXEC_OUTPUT_JOURNAL_AND_CONSOLE] = "journal+console", | |
4f2d528d | 2131 | [EXEC_OUTPUT_SOCKET] = "socket" |
94f04347 LP |
2132 | }; |
2133 | ||
2134 | DEFINE_STRING_TABLE_LOOKUP(exec_output, ExecOutput); |