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