]>
Commit | Line | Data |
---|---|---|
5cb5a6ff LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
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> | |
5cb5a6ff LP |
37 | |
38 | #include "execute.h" | |
39 | #include "strv.h" | |
40 | #include "macro.h" | |
41 | #include "util.h" | |
acbb0225 | 42 | #include "log.h" |
9eba9da4 | 43 | #include "ioprio.h" |
94f04347 | 44 | #include "securebits.h" |
8e274523 | 45 | #include "cgroup.h" |
5cb5a6ff | 46 | |
02a51aba LP |
47 | /* This assumes there is a 'tty' group */ |
48 | #define TTY_MODE 0620 | |
49 | ||
034c6ed7 LP |
50 | static int shift_fds(int fds[], unsigned n_fds) { |
51 | int start, restart_from; | |
52 | ||
53 | if (n_fds <= 0) | |
54 | return 0; | |
55 | ||
a0d40ac5 LP |
56 | /* Modifies the fds array! (sorts it) */ |
57 | ||
034c6ed7 LP |
58 | assert(fds); |
59 | ||
60 | start = 0; | |
61 | for (;;) { | |
62 | int i; | |
63 | ||
64 | restart_from = -1; | |
65 | ||
66 | for (i = start; i < (int) n_fds; i++) { | |
67 | int nfd; | |
68 | ||
69 | /* Already at right index? */ | |
70 | if (fds[i] == i+3) | |
71 | continue; | |
72 | ||
73 | if ((nfd = fcntl(fds[i], F_DUPFD, i+3)) < 0) | |
74 | return -errno; | |
75 | ||
e1f5e051 | 76 | close_nointr_nofail(fds[i]); |
034c6ed7 LP |
77 | fds[i] = nfd; |
78 | ||
79 | /* Hmm, the fd we wanted isn't free? Then | |
80 | * let's remember that and try again from here*/ | |
81 | if (nfd != i+3 && restart_from < 0) | |
82 | restart_from = i; | |
83 | } | |
84 | ||
85 | if (restart_from < 0) | |
86 | break; | |
87 | ||
88 | start = restart_from; | |
89 | } | |
90 | ||
91 | return 0; | |
92 | } | |
93 | ||
c2748801 | 94 | static int flags_fds(const int fds[], unsigned n_fds, bool nonblock) { |
47a71eed | 95 | unsigned i; |
e2c76839 | 96 | int r; |
47a71eed LP |
97 | |
98 | if (n_fds <= 0) | |
99 | return 0; | |
100 | ||
101 | assert(fds); | |
102 | ||
451a074f | 103 | /* Drops/Sets O_NONBLOCK and FD_CLOEXEC from the file flags */ |
47a71eed LP |
104 | |
105 | for (i = 0; i < n_fds; i++) { | |
47a71eed | 106 | |
e2c76839 LP |
107 | if ((r = fd_nonblock(fds[i], nonblock)) < 0) |
108 | return r; | |
47a71eed | 109 | |
451a074f LP |
110 | /* We unconditionally drop FD_CLOEXEC from the fds, |
111 | * since after all we want to pass these fds to our | |
112 | * children */ | |
47a71eed | 113 | |
e2c76839 LP |
114 | if ((r = fd_cloexec(fds[i], false)) < 0) |
115 | return r; | |
47a71eed LP |
116 | } |
117 | ||
118 | return 0; | |
119 | } | |
120 | ||
80876c20 LP |
121 | static const char *tty_path(const ExecContext *context) { |
122 | assert(context); | |
123 | ||
124 | if (context->tty_path) | |
125 | return context->tty_path; | |
126 | ||
127 | return "/dev/console"; | |
128 | } | |
129 | ||
130 | static int open_null_as(int flags, int nfd) { | |
131 | int fd, r; | |
071830ff | 132 | |
80876c20 | 133 | assert(nfd >= 0); |
071830ff | 134 | |
80876c20 | 135 | if ((fd = open("/dev/null", flags|O_NOCTTY)) < 0) |
071830ff LP |
136 | return -errno; |
137 | ||
80876c20 LP |
138 | if (fd != nfd) { |
139 | r = dup2(fd, nfd) < 0 ? -errno : nfd; | |
e1f5e051 | 140 | close_nointr_nofail(fd); |
80876c20 LP |
141 | } else |
142 | r = nfd; | |
071830ff | 143 | |
80876c20 | 144 | return r; |
071830ff LP |
145 | } |
146 | ||
80876c20 LP |
147 | static int connect_logger_as(const ExecContext *context, ExecOutput output, const char *ident, int nfd) { |
148 | int fd, r; | |
149 | union { | |
150 | struct sockaddr sa; | |
151 | struct sockaddr_un un; | |
152 | } sa; | |
071830ff LP |
153 | |
154 | assert(context); | |
80876c20 LP |
155 | assert(output < _EXEC_OUTPUT_MAX); |
156 | assert(ident); | |
157 | assert(nfd >= 0); | |
071830ff | 158 | |
80876c20 LP |
159 | if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) |
160 | return -errno; | |
071830ff | 161 | |
80876c20 LP |
162 | zero(sa); |
163 | sa.sa.sa_family = AF_UNIX; | |
164 | strncpy(sa.un.sun_path+1, LOGGER_SOCKET, sizeof(sa.un.sun_path)-1); | |
071830ff | 165 | |
80876c20 LP |
166 | if (connect(fd, &sa.sa, sizeof(sa)) < 0) { |
167 | close_nointr_nofail(fd); | |
168 | return -errno; | |
169 | } | |
071830ff | 170 | |
80876c20 LP |
171 | if (shutdown(fd, SHUT_RD) < 0) { |
172 | close_nointr_nofail(fd); | |
173 | return -errno; | |
174 | } | |
071830ff | 175 | |
80876c20 LP |
176 | /* We speak a very simple protocol between log server |
177 | * and client: one line for the log destination (kmsg | |
178 | * or syslog), followed by the priority field, | |
179 | * followed by the process name. Since we replaced | |
180 | * stdin/stderr we simple use stdio to write to | |
181 | * it. Note that we use stderr, to minimize buffer | |
182 | * flushing issues. */ | |
183 | ||
184 | dprintf(fd, | |
185 | "%s\n" | |
186 | "%i\n" | |
187 | "%s\n", | |
188 | output == EXEC_OUTPUT_KERNEL ? "kmsg" : "syslog", | |
189 | context->syslog_priority, | |
190 | context->syslog_identifier ? context->syslog_identifier : ident); | |
191 | ||
192 | if (fd != nfd) { | |
193 | r = dup2(fd, nfd) < 0 ? -errno : nfd; | |
e1f5e051 | 194 | close_nointr_nofail(fd); |
80876c20 LP |
195 | } else |
196 | r = nfd; | |
071830ff | 197 | |
80876c20 LP |
198 | return r; |
199 | } | |
200 | static int open_terminal_as(const char *path, mode_t mode, int nfd) { | |
201 | int fd, r; | |
071830ff | 202 | |
80876c20 LP |
203 | assert(path); |
204 | assert(nfd >= 0); | |
071830ff | 205 | |
80876c20 LP |
206 | if ((fd = open_terminal(path, mode | O_NOCTTY)) < 0) |
207 | return fd; | |
071830ff | 208 | |
80876c20 LP |
209 | if (fd != nfd) { |
210 | r = dup2(fd, nfd) < 0 ? -errno : nfd; | |
211 | close_nointr_nofail(fd); | |
212 | } else | |
213 | r = nfd; | |
071830ff | 214 | |
80876c20 LP |
215 | return r; |
216 | } | |
071830ff | 217 | |
80876c20 LP |
218 | static bool is_terminal_input(ExecInput i) { |
219 | return | |
220 | i == EXEC_INPUT_TTY || | |
221 | i == EXEC_INPUT_TTY_FORCE || | |
222 | i == EXEC_INPUT_TTY_FAIL; | |
223 | } | |
071830ff | 224 | |
4f2d528d | 225 | static int fixup_input(const ExecContext *context, int socket_fd) { |
80876c20 | 226 | assert(context); |
071830ff | 227 | |
4f2d528d LP |
228 | if (socket_fd < 0 && context->std_input == EXEC_INPUT_SOCKET) |
229 | return EXEC_INPUT_NULL; | |
230 | ||
231 | return context->std_input; | |
232 | } | |
233 | ||
234 | static int fixup_output(const ExecContext *context, int socket_fd) { | |
235 | assert(context); | |
236 | ||
237 | if (socket_fd < 0 && context->std_output == EXEC_OUTPUT_SOCKET) | |
238 | return EXEC_OUTPUT_INHERIT; | |
239 | ||
240 | return context->std_output; | |
241 | } | |
242 | ||
243 | static int fixup_error(const ExecContext *context, int socket_fd) { | |
244 | assert(context); | |
245 | ||
246 | if (socket_fd < 0 && context->std_error == EXEC_OUTPUT_SOCKET) | |
247 | return EXEC_OUTPUT_INHERIT; | |
248 | ||
249 | return context->std_error; | |
250 | } | |
251 | ||
252 | static int setup_input(const ExecContext *context, int socket_fd) { | |
253 | ExecInput i; | |
254 | ||
255 | assert(context); | |
256 | ||
257 | i = fixup_input(context, socket_fd); | |
258 | ||
259 | switch (i) { | |
071830ff | 260 | |
80876c20 LP |
261 | case EXEC_INPUT_NULL: |
262 | return open_null_as(O_RDONLY, STDIN_FILENO); | |
263 | ||
264 | case EXEC_INPUT_TTY: | |
265 | case EXEC_INPUT_TTY_FORCE: | |
266 | case EXEC_INPUT_TTY_FAIL: { | |
267 | int fd, r; | |
071830ff | 268 | |
80876c20 LP |
269 | if ((fd = acquire_terminal( |
270 | tty_path(context), | |
4f2d528d LP |
271 | i == EXEC_INPUT_TTY_FAIL, |
272 | i == EXEC_INPUT_TTY_FORCE)) < 0) | |
80876c20 LP |
273 | return fd; |
274 | ||
275 | if (fd != STDIN_FILENO) { | |
276 | r = dup2(fd, STDIN_FILENO) < 0 ? -errno : STDIN_FILENO; | |
071830ff | 277 | close_nointr_nofail(fd); |
80876c20 LP |
278 | } else |
279 | r = STDIN_FILENO; | |
280 | ||
281 | return r; | |
282 | } | |
283 | ||
4f2d528d LP |
284 | case EXEC_INPUT_SOCKET: |
285 | return dup2(socket_fd, STDIN_FILENO) < 0 ? -errno : STDIN_FILENO; | |
286 | ||
80876c20 LP |
287 | default: |
288 | assert_not_reached("Unknown input type"); | |
289 | } | |
290 | } | |
291 | ||
4f2d528d LP |
292 | static int setup_output(const ExecContext *context, int socket_fd, const char *ident) { |
293 | ExecOutput o; | |
294 | ExecInput i; | |
295 | ||
80876c20 LP |
296 | assert(context); |
297 | assert(ident); | |
298 | ||
4f2d528d LP |
299 | i = fixup_input(context, socket_fd); |
300 | o = fixup_output(context, socket_fd); | |
301 | ||
80876c20 LP |
302 | /* This expects the input is already set up */ |
303 | ||
4f2d528d | 304 | switch (o) { |
071830ff | 305 | |
80876c20 LP |
306 | case EXEC_OUTPUT_INHERIT: |
307 | ||
308 | /* If the input is connected to a terminal, inherit that... */ | |
4f2d528d | 309 | if (is_terminal_input(i) || i == EXEC_INPUT_SOCKET) |
80876c20 | 310 | return dup2(STDIN_FILENO, STDOUT_FILENO) < 0 ? -errno : STDOUT_FILENO; |
071830ff | 311 | |
4f2d528d | 312 | return STDIN_FILENO; |
80876c20 LP |
313 | |
314 | case EXEC_OUTPUT_NULL: | |
315 | return open_null_as(O_WRONLY, STDOUT_FILENO); | |
316 | ||
4f2d528d LP |
317 | case EXEC_OUTPUT_TTY: |
318 | if (is_terminal_input(i)) | |
80876c20 LP |
319 | return dup2(STDIN_FILENO, STDOUT_FILENO) < 0 ? -errno : STDOUT_FILENO; |
320 | ||
321 | /* We don't reset the terminal if this is just about output */ | |
322 | return open_terminal_as(tty_path(context), O_WRONLY, STDOUT_FILENO); | |
94f04347 | 323 | |
80876c20 LP |
324 | case EXEC_OUTPUT_SYSLOG: |
325 | case EXEC_OUTPUT_KERNEL: | |
4f2d528d LP |
326 | return connect_logger_as(context, o, ident, STDOUT_FILENO); |
327 | ||
328 | case EXEC_OUTPUT_SOCKET: | |
329 | assert(socket_fd >= 0); | |
330 | return dup2(socket_fd, STDOUT_FILENO) < 0 ? -errno : STDOUT_FILENO; | |
80876c20 | 331 | |
94f04347 LP |
332 | default: |
333 | assert_not_reached("Unknown output type"); | |
071830ff | 334 | } |
94f04347 LP |
335 | } |
336 | ||
4f2d528d LP |
337 | static int setup_error(const ExecContext *context, int socket_fd, const char *ident) { |
338 | ExecOutput o, e; | |
339 | ExecInput i; | |
340 | ||
94f04347 | 341 | assert(context); |
02a51aba | 342 | assert(ident); |
071830ff | 343 | |
4f2d528d LP |
344 | i = fixup_input(context, socket_fd); |
345 | o = fixup_output(context, socket_fd); | |
346 | e = fixup_error(context, socket_fd); | |
347 | ||
80876c20 | 348 | /* This expects the input and output are already set up */ |
94f04347 | 349 | |
80876c20 LP |
350 | /* Don't change the stderr file descriptor if we inherit all |
351 | * the way and are not on a tty */ | |
4f2d528d LP |
352 | if (e == EXEC_OUTPUT_INHERIT && |
353 | o == EXEC_OUTPUT_INHERIT && | |
354 | !is_terminal_input(i)) | |
80876c20 | 355 | return STDERR_FILENO; |
94f04347 | 356 | |
80876c20 | 357 | /* Duplicate form stdout if possible */ |
4f2d528d | 358 | if (e == o || e == EXEC_OUTPUT_INHERIT) |
80876c20 | 359 | return dup2(STDOUT_FILENO, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO; |
94f04347 | 360 | |
4f2d528d | 361 | switch (e) { |
80876c20 LP |
362 | |
363 | case EXEC_OUTPUT_NULL: | |
364 | return open_null_as(O_WRONLY, STDERR_FILENO); | |
365 | ||
366 | case EXEC_OUTPUT_TTY: | |
4f2d528d | 367 | if (is_terminal_input(i)) |
80876c20 LP |
368 | return dup2(STDIN_FILENO, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO; |
369 | ||
370 | /* We don't reset the terminal if this is just about output */ | |
371 | return open_terminal_as(tty_path(context), O_WRONLY, STDERR_FILENO); | |
372 | ||
373 | case EXEC_OUTPUT_SYSLOG: | |
374 | case EXEC_OUTPUT_KERNEL: | |
4f2d528d LP |
375 | return connect_logger_as(context, e, ident, STDERR_FILENO); |
376 | ||
377 | case EXEC_OUTPUT_SOCKET: | |
378 | assert(socket_fd >= 0); | |
379 | return dup2(socket_fd, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO; | |
94f04347 LP |
380 | |
381 | default: | |
80876c20 | 382 | assert_not_reached("Unknown error type"); |
94f04347 | 383 | } |
071830ff LP |
384 | } |
385 | ||
02a51aba LP |
386 | static int chown_terminal(int fd, uid_t uid) { |
387 | struct stat st; | |
388 | ||
389 | assert(fd >= 0); | |
02a51aba LP |
390 | |
391 | /* This might fail. What matters are the results. */ | |
392 | fchown(fd, uid, -1); | |
393 | fchmod(fd, TTY_MODE); | |
394 | ||
395 | if (fstat(fd, &st) < 0) | |
396 | return -errno; | |
397 | ||
d8b4e2e9 | 398 | if (st.st_uid != uid || (st.st_mode & 0777) != TTY_MODE) |
02a51aba LP |
399 | return -EPERM; |
400 | ||
401 | return 0; | |
402 | } | |
403 | ||
80876c20 LP |
404 | static int setup_confirm_stdio(const ExecContext *context, |
405 | int *_saved_stdin, | |
406 | int *_saved_stdout) { | |
407 | int fd = -1, saved_stdin, saved_stdout = -1, r; | |
408 | ||
409 | assert(context); | |
410 | assert(_saved_stdin); | |
411 | assert(_saved_stdout); | |
412 | ||
413 | /* This returns positive EXIT_xxx return values instead of | |
414 | * negative errno style values! */ | |
415 | ||
416 | if ((saved_stdin = fcntl(STDIN_FILENO, F_DUPFD, 3)) < 0) | |
417 | return EXIT_STDIN; | |
418 | ||
419 | if ((saved_stdout = fcntl(STDOUT_FILENO, F_DUPFD, 3)) < 0) { | |
420 | r = EXIT_STDOUT; | |
421 | goto fail; | |
422 | } | |
423 | ||
424 | if ((fd = acquire_terminal( | |
425 | tty_path(context), | |
426 | context->std_input == EXEC_INPUT_TTY_FAIL, | |
427 | context->std_input == EXEC_INPUT_TTY_FORCE)) < 0) { | |
428 | r = EXIT_STDIN; | |
429 | goto fail; | |
430 | } | |
431 | ||
02a51aba LP |
432 | if (chown_terminal(fd, getuid()) < 0) { |
433 | r = EXIT_STDIN; | |
434 | goto fail; | |
435 | } | |
436 | ||
80876c20 LP |
437 | if (dup2(fd, STDIN_FILENO) < 0) { |
438 | r = EXIT_STDIN; | |
439 | goto fail; | |
440 | } | |
441 | ||
442 | if (dup2(fd, STDOUT_FILENO) < 0) { | |
443 | r = EXIT_STDOUT; | |
444 | goto fail; | |
445 | } | |
446 | ||
447 | if (fd >= 2) | |
448 | close_nointr_nofail(fd); | |
449 | ||
450 | *_saved_stdin = saved_stdin; | |
451 | *_saved_stdout = saved_stdout; | |
452 | ||
453 | return 0; | |
454 | ||
455 | fail: | |
456 | if (saved_stdout >= 0) | |
457 | close_nointr_nofail(saved_stdout); | |
458 | ||
459 | if (saved_stdin >= 0) | |
460 | close_nointr_nofail(saved_stdin); | |
461 | ||
462 | if (fd >= 0) | |
463 | close_nointr_nofail(fd); | |
464 | ||
465 | return r; | |
466 | } | |
467 | ||
468 | static int restore_conform_stdio(const ExecContext *context, | |
469 | int *saved_stdin, | |
470 | int *saved_stdout, | |
471 | bool *keep_stdin, | |
472 | bool *keep_stdout) { | |
473 | ||
474 | assert(context); | |
475 | assert(saved_stdin); | |
476 | assert(*saved_stdin >= 0); | |
477 | assert(saved_stdout); | |
478 | assert(*saved_stdout >= 0); | |
479 | ||
480 | /* This returns positive EXIT_xxx return values instead of | |
481 | * negative errno style values! */ | |
482 | ||
483 | if (is_terminal_input(context->std_input)) { | |
484 | ||
485 | /* The service wants terminal input. */ | |
486 | ||
487 | *keep_stdin = true; | |
488 | *keep_stdout = | |
489 | context->std_output == EXEC_OUTPUT_INHERIT || | |
490 | context->std_output == EXEC_OUTPUT_TTY; | |
491 | ||
492 | } else { | |
493 | /* If the service doesn't want a controlling terminal, | |
494 | * then we need to get rid entirely of what we have | |
495 | * already. */ | |
496 | ||
497 | if (release_terminal() < 0) | |
498 | return EXIT_STDIN; | |
499 | ||
500 | if (dup2(*saved_stdin, STDIN_FILENO) < 0) | |
501 | return EXIT_STDIN; | |
502 | ||
503 | if (dup2(*saved_stdout, STDOUT_FILENO) < 0) | |
504 | return EXIT_STDOUT; | |
505 | ||
506 | *keep_stdout = *keep_stdin = false; | |
507 | } | |
508 | ||
509 | return 0; | |
510 | } | |
511 | ||
81a2b7ce LP |
512 | static int get_group_creds(const char *groupname, gid_t *gid) { |
513 | struct group *g; | |
514 | unsigned long lu; | |
515 | ||
516 | assert(groupname); | |
517 | assert(gid); | |
518 | ||
519 | /* We enforce some special rules for gid=0: in order to avoid | |
520 | * NSS lookups for root we hardcode its data. */ | |
521 | ||
522 | if (streq(groupname, "root") || streq(groupname, "0")) { | |
523 | *gid = 0; | |
524 | return 0; | |
525 | } | |
526 | ||
527 | if (safe_atolu(groupname, &lu) >= 0) { | |
528 | errno = 0; | |
529 | g = getgrgid((gid_t) lu); | |
530 | } else { | |
531 | errno = 0; | |
532 | g = getgrnam(groupname); | |
533 | } | |
534 | ||
535 | if (!g) | |
536 | return errno != 0 ? -errno : -ESRCH; | |
537 | ||
538 | *gid = g->gr_gid; | |
539 | return 0; | |
540 | } | |
541 | ||
542 | static int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home) { | |
543 | struct passwd *p; | |
544 | unsigned long lu; | |
545 | ||
546 | assert(username); | |
547 | assert(*username); | |
548 | assert(uid); | |
549 | assert(gid); | |
550 | assert(home); | |
551 | ||
552 | /* We enforce some special rules for uid=0: in order to avoid | |
553 | * NSS lookups for root we hardcode its data. */ | |
554 | ||
555 | if (streq(*username, "root") || streq(*username, "0")) { | |
556 | *username = "root"; | |
557 | *uid = 0; | |
558 | *gid = 0; | |
559 | *home = "/root"; | |
560 | return 0; | |
561 | } | |
562 | ||
563 | if (safe_atolu(*username, &lu) >= 0) { | |
564 | errno = 0; | |
565 | p = getpwuid((uid_t) lu); | |
566 | ||
567 | /* If there are multiple users with the same id, make | |
568 | * sure to leave $USER to the configured value instead | |
569 | * of the first occurence in the database. However if | |
570 | * the uid was configured by a numeric uid, then let's | |
571 | * pick the real username from /etc/passwd. */ | |
572 | if (*username && p) | |
573 | *username = p->pw_name; | |
574 | } else { | |
575 | errno = 0; | |
576 | p = getpwnam(*username); | |
577 | } | |
578 | ||
579 | if (!p) | |
580 | return errno != 0 ? -errno : -ESRCH; | |
581 | ||
582 | *uid = p->pw_uid; | |
583 | *gid = p->pw_gid; | |
584 | *home = p->pw_dir; | |
585 | return 0; | |
586 | } | |
587 | ||
588 | static int enforce_groups(const ExecContext *context, const char *username, gid_t gid) { | |
589 | bool keep_groups = false; | |
590 | int r; | |
591 | ||
592 | assert(context); | |
593 | ||
594 | /* Lookup and ser GID and supplementary group list. Here too | |
595 | * we avoid NSS lookups for gid=0. */ | |
596 | ||
597 | if (context->group || username) { | |
598 | ||
599 | if (context->group) | |
600 | if ((r = get_group_creds(context->group, &gid)) < 0) | |
601 | return r; | |
602 | ||
603 | /* First step, initialize groups from /etc/groups */ | |
604 | if (username && gid != 0) { | |
605 | if (initgroups(username, gid) < 0) | |
606 | return -errno; | |
607 | ||
608 | keep_groups = true; | |
609 | } | |
610 | ||
611 | /* Second step, set our gids */ | |
612 | if (setresgid(gid, gid, gid) < 0) | |
613 | return -errno; | |
614 | } | |
615 | ||
616 | if (context->supplementary_groups) { | |
617 | int ngroups_max, k; | |
618 | gid_t *gids; | |
619 | char **i; | |
620 | ||
621 | /* Final step, initialize any manually set supplementary groups */ | |
622 | ngroups_max = (int) sysconf(_SC_NGROUPS_MAX); | |
623 | ||
624 | if (!(gids = new(gid_t, ngroups_max))) | |
625 | return -ENOMEM; | |
626 | ||
627 | if (keep_groups) { | |
628 | if ((k = getgroups(ngroups_max, gids)) < 0) { | |
629 | free(gids); | |
630 | return -errno; | |
631 | } | |
632 | } else | |
633 | k = 0; | |
634 | ||
635 | STRV_FOREACH(i, context->supplementary_groups) { | |
636 | ||
637 | if (k >= ngroups_max) { | |
638 | free(gids); | |
639 | return -E2BIG; | |
640 | } | |
641 | ||
642 | if ((r = get_group_creds(*i, gids+k)) < 0) { | |
643 | free(gids); | |
644 | return r; | |
645 | } | |
646 | ||
647 | k++; | |
648 | } | |
649 | ||
650 | if (setgroups(k, gids) < 0) { | |
651 | free(gids); | |
652 | return -errno; | |
653 | } | |
654 | ||
655 | free(gids); | |
656 | } | |
657 | ||
658 | return 0; | |
659 | } | |
660 | ||
661 | static int enforce_user(const ExecContext *context, uid_t uid) { | |
662 | int r; | |
663 | assert(context); | |
664 | ||
665 | /* Sets (but doesn't lookup) the uid and make sure we keep the | |
666 | * capabilities while doing so. */ | |
667 | ||
668 | if (context->capabilities) { | |
669 | cap_t d; | |
670 | static const cap_value_t bits[] = { | |
671 | CAP_SETUID, /* Necessary so that we can run setresuid() below */ | |
672 | CAP_SETPCAP /* Necessary so that we can set PR_SET_SECUREBITS later on */ | |
673 | }; | |
674 | ||
675 | /* First step: If we need to keep capabilities but | |
676 | * drop privileges we need to make sure we keep our | |
677 | * caps, whiel we drop priviliges. */ | |
693ced48 LP |
678 | if (uid != 0) { |
679 | int sb = context->secure_bits|SECURE_KEEP_CAPS; | |
680 | ||
681 | if (prctl(PR_GET_SECUREBITS) != sb) | |
682 | if (prctl(PR_SET_SECUREBITS, sb) < 0) | |
683 | return -errno; | |
684 | } | |
81a2b7ce LP |
685 | |
686 | /* Second step: set the capabilites. This will reduce | |
687 | * the capabilities to the minimum we need. */ | |
688 | ||
689 | if (!(d = cap_dup(context->capabilities))) | |
690 | return -errno; | |
691 | ||
692 | if (cap_set_flag(d, CAP_EFFECTIVE, ELEMENTSOF(bits), bits, CAP_SET) < 0 || | |
693 | cap_set_flag(d, CAP_PERMITTED, ELEMENTSOF(bits), bits, CAP_SET) < 0) { | |
694 | r = -errno; | |
695 | cap_free(d); | |
696 | return r; | |
697 | } | |
698 | ||
699 | if (cap_set_proc(d) < 0) { | |
700 | r = -errno; | |
701 | cap_free(d); | |
702 | return r; | |
703 | } | |
704 | ||
705 | cap_free(d); | |
706 | } | |
707 | ||
708 | /* Third step: actually set the uids */ | |
709 | if (setresuid(uid, uid, uid) < 0) | |
710 | return -errno; | |
711 | ||
712 | /* At this point we should have all necessary capabilities but | |
713 | are otherwise a normal user. However, the caps might got | |
714 | corrupted due to the setresuid() so we need clean them up | |
715 | later. This is done outside of this call. */ | |
716 | ||
717 | return 0; | |
718 | } | |
719 | ||
9fb86720 | 720 | int exec_spawn(ExecCommand *command, |
9e2f7c11 | 721 | char **argv, |
81a2b7ce | 722 | const ExecContext *context, |
c2748801 | 723 | int fds[], unsigned n_fds, |
81a2b7ce LP |
724 | bool apply_permissions, |
725 | bool apply_chroot, | |
80876c20 | 726 | bool confirm_spawn, |
8e274523 | 727 | CGroupBonding *cgroup_bondings, |
81a2b7ce LP |
728 | pid_t *ret) { |
729 | ||
034c6ed7 | 730 | pid_t pid; |
8e274523 | 731 | int r; |
1a63a750 | 732 | char *line; |
4f2d528d | 733 | int socket_fd; |
034c6ed7 | 734 | |
5cb5a6ff LP |
735 | assert(command); |
736 | assert(context); | |
737 | assert(ret); | |
034c6ed7 LP |
738 | assert(fds || n_fds <= 0); |
739 | ||
4f2d528d LP |
740 | if (context->std_input == EXEC_INPUT_SOCKET || |
741 | context->std_output == EXEC_OUTPUT_SOCKET || | |
742 | context->std_error == EXEC_OUTPUT_SOCKET) { | |
743 | ||
744 | if (n_fds != 1) | |
745 | return -EINVAL; | |
746 | ||
747 | socket_fd = fds[0]; | |
748 | ||
749 | fds = NULL; | |
750 | n_fds = 0; | |
751 | } else | |
752 | socket_fd = -1; | |
753 | ||
9e2f7c11 LP |
754 | if (!argv) |
755 | argv = command->argv; | |
756 | ||
757 | if (!(line = exec_command_line(argv))) | |
1a63a750 LP |
758 | return -ENOMEM; |
759 | ||
760 | log_debug("About to execute: %s", line); | |
761 | free(line); | |
acbb0225 | 762 | |
8e274523 LP |
763 | if (cgroup_bondings) |
764 | if ((r = cgroup_bonding_realize_list(cgroup_bondings))) | |
765 | return r; | |
766 | ||
034c6ed7 LP |
767 | if ((pid = fork()) < 0) |
768 | return -errno; | |
769 | ||
770 | if (pid == 0) { | |
8e274523 | 771 | int i; |
309bff19 | 772 | sigset_t ss; |
81a2b7ce LP |
773 | const char *username = NULL, *home = NULL; |
774 | uid_t uid = (uid_t) -1; | |
775 | gid_t gid = (gid_t) -1; | |
776 | char **our_env = NULL, **final_env = NULL; | |
777 | unsigned n_env = 0; | |
80876c20 LP |
778 | int saved_stdout = -1, saved_stdin = -1; |
779 | bool keep_stdout = false, keep_stdin = false; | |
309bff19 | 780 | |
034c6ed7 | 781 | /* child */ |
5cb5a6ff | 782 | |
7b683879 LP |
783 | reset_all_signal_handlers(); |
784 | ||
309bff19 LP |
785 | if (sigemptyset(&ss) < 0 || |
786 | sigprocmask(SIG_SETMASK, &ss, NULL) < 0) { | |
787 | r = EXIT_SIGNAL_MASK; | |
788 | goto fail; | |
789 | } | |
790 | ||
8d567588 LP |
791 | if (!context->no_setsid) |
792 | if (setsid() < 0) { | |
793 | r = EXIT_SETSID; | |
794 | goto fail; | |
795 | } | |
80876c20 LP |
796 | |
797 | umask(context->umask); | |
798 | ||
799 | if (confirm_spawn) { | |
800 | char response; | |
801 | ||
802 | /* Set up terminal for the question */ | |
803 | if ((r = setup_confirm_stdio(context, | |
804 | &saved_stdin, &saved_stdout))) | |
805 | goto fail; | |
806 | ||
807 | /* Now ask the question. */ | |
9e2f7c11 | 808 | if (!(line = exec_command_line(argv))) { |
80876c20 | 809 | r = EXIT_MEMORY; |
ee2b4894 LP |
810 | goto fail; |
811 | } | |
80876c20 LP |
812 | |
813 | r = ask(&response, "yns", "Execute %s? [Yes, No, Skip] ", line); | |
814 | free(line); | |
815 | ||
816 | if (r < 0 || response == 'n') { | |
817 | r = EXIT_CONFIRM; | |
818 | goto fail; | |
819 | } else if (response == 's') { | |
820 | r = 0; | |
ee2b4894 LP |
821 | goto fail; |
822 | } | |
80876c20 LP |
823 | |
824 | /* Release terminal for the question */ | |
825 | if ((r = restore_conform_stdio(context, | |
826 | &saved_stdin, &saved_stdout, | |
827 | &keep_stdin, &keep_stdout))) | |
828 | goto fail; | |
034c6ed7 LP |
829 | } |
830 | ||
80876c20 | 831 | if (!keep_stdin) |
4f2d528d | 832 | if (setup_input(context, socket_fd) < 0) { |
80876c20 LP |
833 | r = EXIT_STDIN; |
834 | goto fail; | |
835 | } | |
9eba9da4 | 836 | |
80876c20 | 837 | if (!keep_stdout) |
4f2d528d | 838 | if (setup_output(context, socket_fd, file_name_from_path(command->path)) < 0) { |
80876c20 LP |
839 | r = EXIT_STDOUT; |
840 | goto fail; | |
841 | } | |
94f04347 | 842 | |
4f2d528d | 843 | if (setup_error(context, socket_fd, file_name_from_path(command->path)) < 0) { |
80876c20 | 844 | r = EXIT_STDERR; |
071830ff LP |
845 | goto fail; |
846 | } | |
847 | ||
8e274523 LP |
848 | if (cgroup_bondings) |
849 | if ((r = cgroup_bonding_install_list(cgroup_bondings, 0)) < 0) { | |
850 | r = EXIT_CGROUP; | |
851 | goto fail; | |
852 | } | |
853 | ||
fb33a393 LP |
854 | if (context->oom_adjust_set) { |
855 | char t[16]; | |
034c6ed7 | 856 | |
fb33a393 LP |
857 | snprintf(t, sizeof(t), "%i", context->oom_adjust); |
858 | char_array_0(t); | |
034c6ed7 | 859 | |
fb33a393 LP |
860 | if (write_one_line_file("/proc/self/oom_adj", t) < 0) { |
861 | r = EXIT_OOM_ADJUST; | |
862 | goto fail; | |
863 | } | |
034c6ed7 LP |
864 | } |
865 | ||
fb33a393 LP |
866 | if (context->nice_set) |
867 | if (setpriority(PRIO_PROCESS, 0, context->nice) < 0) { | |
868 | r = EXIT_NICE; | |
869 | goto fail; | |
870 | } | |
871 | ||
94f04347 LP |
872 | if (context->cpu_sched_set) { |
873 | struct sched_param param; | |
874 | ||
875 | zero(param); | |
876 | param.sched_priority = context->cpu_sched_priority; | |
877 | ||
38b48754 LP |
878 | if (sched_setscheduler(0, context->cpu_sched_policy | |
879 | (context->cpu_sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0), ¶m) < 0) { | |
94f04347 LP |
880 | r = EXIT_SETSCHEDULER; |
881 | goto fail; | |
882 | } | |
883 | } | |
884 | ||
885 | if (context->cpu_affinity_set) | |
886 | if (sched_setaffinity(0, sizeof(context->cpu_affinity), &context->cpu_affinity) < 0) { | |
887 | r = EXIT_CPUAFFINITY; | |
888 | goto fail; | |
889 | } | |
890 | ||
9eba9da4 LP |
891 | if (context->ioprio_set) |
892 | if (ioprio_set(IOPRIO_WHO_PROCESS, 0, context->ioprio) < 0) { | |
893 | r = EXIT_IOPRIO; | |
894 | goto fail; | |
895 | } | |
896 | ||
94f04347 LP |
897 | if (context->timer_slack_ns_set) |
898 | if (prctl(PR_SET_TIMERSLACK, context->timer_slack_ns_set) < 0) { | |
899 | r = EXIT_TIMERSLACK; | |
900 | goto fail; | |
901 | } | |
902 | ||
81a2b7ce LP |
903 | if (context->user) { |
904 | username = context->user; | |
905 | if (get_user_creds(&username, &uid, &gid, &home) < 0) { | |
906 | r = EXIT_USER; | |
907 | goto fail; | |
908 | } | |
d8b4e2e9 LP |
909 | |
910 | if (is_terminal_input(context->std_input)) | |
911 | if (chown_terminal(STDIN_FILENO, uid) < 0) { | |
912 | r = EXIT_STDIN; | |
913 | goto fail; | |
914 | } | |
81a2b7ce LP |
915 | } |
916 | ||
917 | if (apply_permissions) | |
918 | if (enforce_groups(context, username, uid) < 0) { | |
919 | r = EXIT_GROUP; | |
920 | goto fail; | |
921 | } | |
922 | ||
923 | if (apply_chroot) { | |
924 | if (context->root_directory) | |
925 | if (chroot(context->root_directory) < 0) { | |
926 | r = EXIT_CHROOT; | |
927 | goto fail; | |
928 | } | |
929 | ||
930 | if (chdir(context->working_directory ? context->working_directory : "/") < 0) { | |
931 | r = EXIT_CHDIR; | |
932 | goto fail; | |
933 | } | |
934 | } else { | |
935 | ||
936 | char *d; | |
937 | ||
938 | if (asprintf(&d, "%s/%s", | |
939 | context->root_directory ? context->root_directory : "", | |
940 | context->working_directory ? context->working_directory : "") < 0) { | |
941 | r = EXIT_MEMORY; | |
942 | goto fail; | |
943 | } | |
944 | ||
945 | if (chdir(d) < 0) { | |
946 | free(d); | |
947 | r = EXIT_CHDIR; | |
948 | goto fail; | |
949 | } | |
950 | ||
951 | free(d); | |
952 | } | |
953 | ||
a0d40ac5 | 954 | if (close_all_fds(fds, n_fds) < 0 || |
47a71eed | 955 | shift_fds(fds, n_fds) < 0 || |
451a074f | 956 | flags_fds(fds, n_fds, context->non_blocking) < 0) { |
034c6ed7 LP |
957 | r = EXIT_FDS; |
958 | goto fail; | |
959 | } | |
960 | ||
81a2b7ce | 961 | if (apply_permissions) { |
034c6ed7 | 962 | |
81a2b7ce LP |
963 | for (i = 0; i < RLIMIT_NLIMITS; i++) { |
964 | if (!context->rlimit[i]) | |
965 | continue; | |
966 | ||
967 | if (setrlimit(i, context->rlimit[i]) < 0) { | |
968 | r = EXIT_LIMITS; | |
969 | goto fail; | |
970 | } | |
034c6ed7 | 971 | } |
034c6ed7 | 972 | |
81a2b7ce LP |
973 | if (context->user) |
974 | if (enforce_user(context, uid) < 0) { | |
975 | r = EXIT_USER; | |
976 | goto fail; | |
977 | } | |
978 | ||
693ced48 LP |
979 | /* PR_GET_SECUREBITS is not priviliged, while |
980 | * PR_SET_SECUREBITS is. So to suppress | |
981 | * potential EPERMs we'll try not to call | |
982 | * PR_SET_SECUREBITS unless necessary. */ | |
983 | if (prctl(PR_GET_SECUREBITS) != context->secure_bits) | |
984 | if (prctl(PR_SET_SECUREBITS, context->secure_bits) < 0) { | |
985 | r = EXIT_SECUREBITS; | |
986 | goto fail; | |
987 | } | |
81a2b7ce LP |
988 | |
989 | if (context->capabilities) | |
990 | if (cap_set_proc(context->capabilities) < 0) { | |
991 | r = EXIT_CAPABILITIES; | |
992 | goto fail; | |
993 | } | |
94f04347 LP |
994 | } |
995 | ||
81a2b7ce LP |
996 | if (!(our_env = new0(char*, 6))) { |
997 | r = EXIT_MEMORY; | |
998 | goto fail; | |
999 | } | |
034c6ed7 | 1000 | |
81a2b7ce LP |
1001 | if (n_fds > 0) |
1002 | if (asprintf(our_env + n_env++, "LISTEN_PID=%llu", (unsigned long long) getpid()) < 0 || | |
1003 | asprintf(our_env + n_env++, "LISTEN_FDS=%u", n_fds) < 0) { | |
1004 | r = EXIT_MEMORY; | |
1005 | goto fail; | |
1006 | } | |
034c6ed7 | 1007 | |
81a2b7ce LP |
1008 | if (home) |
1009 | if (asprintf(our_env + n_env++, "HOME=%s", home) < 0) { | |
1010 | r = EXIT_MEMORY; | |
1011 | goto fail; | |
1012 | } | |
034c6ed7 | 1013 | |
81a2b7ce LP |
1014 | if (username) |
1015 | if (asprintf(our_env + n_env++, "LOGNAME=%s", username) < 0 || | |
1016 | asprintf(our_env + n_env++, "USER=%s", username) < 0) { | |
1017 | r = EXIT_MEMORY; | |
1018 | goto fail; | |
1019 | } | |
034c6ed7 | 1020 | |
81a2b7ce LP |
1021 | if (!(final_env = strv_env_merge(environ, our_env, context->environment, NULL))) { |
1022 | r = EXIT_MEMORY; | |
1023 | goto fail; | |
1024 | } | |
034c6ed7 | 1025 | |
9e2f7c11 | 1026 | execve(command->path, argv, final_env); |
034c6ed7 LP |
1027 | r = EXIT_EXEC; |
1028 | ||
1029 | fail: | |
81a2b7ce LP |
1030 | strv_free(our_env); |
1031 | strv_free(final_env); | |
1032 | ||
80876c20 LP |
1033 | if (saved_stdin >= 0) |
1034 | close_nointr_nofail(saved_stdin); | |
1035 | ||
1036 | if (saved_stdout >= 0) | |
1037 | close_nointr_nofail(saved_stdout); | |
1038 | ||
034c6ed7 LP |
1039 | _exit(r); |
1040 | } | |
1041 | ||
80876c20 LP |
1042 | /* We add the new process to the cgroup both in the child (so |
1043 | * that we can be sure that no user code is ever executed | |
1044 | * outside of the cgroup) and in the parent (so that we can be | |
1045 | * sure that when we kill the cgroup the process will be | |
1046 | * killed too). */ | |
1047 | if (cgroup_bondings) | |
1048 | if ((r = cgroup_bonding_install_list(cgroup_bondings, pid)) < 0) { | |
1049 | r = EXIT_CGROUP; | |
1050 | goto fail; | |
1051 | } | |
2da3263a | 1052 | |
81a2b7ce | 1053 | log_debug("Forked %s as %llu", command->path, (unsigned long long) pid); |
2da3263a | 1054 | |
9fb86720 LP |
1055 | command->exec_status.pid = pid; |
1056 | command->exec_status.start_timestamp = now(CLOCK_REALTIME); | |
1057 | ||
034c6ed7 | 1058 | *ret = pid; |
5cb5a6ff LP |
1059 | return 0; |
1060 | } | |
1061 | ||
034c6ed7 LP |
1062 | void exec_context_init(ExecContext *c) { |
1063 | assert(c); | |
1064 | ||
1065 | c->umask = 0002; | |
9eba9da4 | 1066 | c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0); |
94f04347 | 1067 | c->cpu_sched_policy = SCHED_OTHER; |
071830ff | 1068 | c->syslog_priority = LOG_DAEMON|LOG_INFO; |
034c6ed7 LP |
1069 | } |
1070 | ||
1071 | void exec_context_done(ExecContext *c) { | |
5cb5a6ff LP |
1072 | unsigned l; |
1073 | ||
1074 | assert(c); | |
1075 | ||
1076 | strv_free(c->environment); | |
034c6ed7 | 1077 | c->environment = NULL; |
5cb5a6ff | 1078 | |
034c6ed7 | 1079 | for (l = 0; l < ELEMENTSOF(c->rlimit); l++) { |
5cb5a6ff | 1080 | free(c->rlimit[l]); |
034c6ed7 LP |
1081 | c->rlimit[l] = NULL; |
1082 | } | |
1083 | ||
9eba9da4 LP |
1084 | free(c->working_directory); |
1085 | c->working_directory = NULL; | |
1086 | free(c->root_directory); | |
1087 | c->root_directory = NULL; | |
5cb5a6ff | 1088 | |
80876c20 LP |
1089 | free(c->tty_path); |
1090 | c->tty_path = NULL; | |
1091 | ||
071830ff LP |
1092 | free(c->syslog_identifier); |
1093 | c->syslog_identifier = NULL; | |
1094 | ||
5cb5a6ff | 1095 | free(c->user); |
034c6ed7 LP |
1096 | c->user = NULL; |
1097 | ||
5cb5a6ff | 1098 | free(c->group); |
034c6ed7 LP |
1099 | c->group = NULL; |
1100 | ||
1101 | strv_free(c->supplementary_groups); | |
1102 | c->supplementary_groups = NULL; | |
94f04347 LP |
1103 | |
1104 | if (c->capabilities) { | |
1105 | cap_free(c->capabilities); | |
1106 | c->capabilities = NULL; | |
1107 | } | |
5cb5a6ff LP |
1108 | } |
1109 | ||
43d0fcbd LP |
1110 | void exec_command_done(ExecCommand *c) { |
1111 | assert(c); | |
1112 | ||
1113 | free(c->path); | |
1114 | c->path = NULL; | |
1115 | ||
1116 | strv_free(c->argv); | |
1117 | c->argv = NULL; | |
1118 | } | |
1119 | ||
1120 | void exec_command_done_array(ExecCommand *c, unsigned n) { | |
1121 | unsigned i; | |
1122 | ||
1123 | for (i = 0; i < n; i++) | |
1124 | exec_command_done(c+i); | |
1125 | } | |
1126 | ||
5cb5a6ff LP |
1127 | void exec_command_free_list(ExecCommand *c) { |
1128 | ExecCommand *i; | |
1129 | ||
1130 | while ((i = c)) { | |
034c6ed7 | 1131 | LIST_REMOVE(ExecCommand, command, c, i); |
43d0fcbd | 1132 | exec_command_done(i); |
5cb5a6ff LP |
1133 | free(i); |
1134 | } | |
1135 | } | |
1136 | ||
034c6ed7 LP |
1137 | void exec_command_free_array(ExecCommand **c, unsigned n) { |
1138 | unsigned i; | |
1139 | ||
1140 | for (i = 0; i < n; i++) { | |
1141 | exec_command_free_list(c[i]); | |
1142 | c[i] = NULL; | |
1143 | } | |
1144 | } | |
1145 | ||
5cb5a6ff | 1146 | void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) { |
94f04347 LP |
1147 | char ** e; |
1148 | unsigned i; | |
9eba9da4 | 1149 | |
5cb5a6ff LP |
1150 | assert(c); |
1151 | assert(f); | |
1152 | ||
1153 | if (!prefix) | |
1154 | prefix = ""; | |
1155 | ||
1156 | fprintf(f, | |
94f04347 LP |
1157 | "%sUMask: %04o\n" |
1158 | "%sWorkingDirectory: %s\n" | |
451a074f | 1159 | "%sRootDirectory: %s\n" |
80876c20 | 1160 | "%sNonBlocking: %s\n", |
5cb5a6ff | 1161 | prefix, c->umask, |
9eba9da4 | 1162 | prefix, c->working_directory ? c->working_directory : "/", |
451a074f | 1163 | prefix, c->root_directory ? c->root_directory : "/", |
80876c20 | 1164 | prefix, yes_no(c->non_blocking)); |
fb33a393 | 1165 | |
94f04347 LP |
1166 | if (c->environment) |
1167 | for (e = c->environment; *e; e++) | |
1168 | fprintf(f, "%sEnvironment: %s\n", prefix, *e); | |
1169 | ||
fb33a393 LP |
1170 | if (c->nice_set) |
1171 | fprintf(f, | |
1172 | "%sNice: %i\n", | |
1173 | prefix, c->nice); | |
1174 | ||
1175 | if (c->oom_adjust_set) | |
1176 | fprintf(f, | |
1177 | "%sOOMAdjust: %i\n", | |
1178 | prefix, c->oom_adjust); | |
9eba9da4 | 1179 | |
94f04347 LP |
1180 | for (i = 0; i < RLIM_NLIMITS; i++) |
1181 | if (c->rlimit[i]) | |
ea430986 | 1182 | fprintf(f, "%s%s: %llu\n", prefix, rlimit_to_string(i), (unsigned long long) c->rlimit[i]->rlim_max); |
94f04347 | 1183 | |
9eba9da4 LP |
1184 | if (c->ioprio_set) |
1185 | fprintf(f, | |
1186 | "%sIOSchedulingClass: %s\n" | |
1187 | "%sIOPriority: %i\n", | |
94f04347 | 1188 | prefix, ioprio_class_to_string(IOPRIO_PRIO_CLASS(c->ioprio)), |
9eba9da4 | 1189 | prefix, (int) IOPRIO_PRIO_DATA(c->ioprio)); |
94f04347 LP |
1190 | |
1191 | if (c->cpu_sched_set) | |
1192 | fprintf(f, | |
1193 | "%sCPUSchedulingPolicy: %s\n" | |
38b48754 LP |
1194 | "%sCPUSchedulingPriority: %i\n" |
1195 | "%sCPUSchedulingResetOnFork: %s\n", | |
94f04347 | 1196 | prefix, sched_policy_to_string(c->cpu_sched_policy), |
38b48754 LP |
1197 | prefix, c->cpu_sched_priority, |
1198 | prefix, yes_no(c->cpu_sched_reset_on_fork)); | |
94f04347 LP |
1199 | |
1200 | if (c->cpu_affinity_set) { | |
1201 | fprintf(f, "%sCPUAffinity:", prefix); | |
1202 | for (i = 0; i < CPU_SETSIZE; i++) | |
1203 | if (CPU_ISSET(i, &c->cpu_affinity)) | |
1204 | fprintf(f, " %i", i); | |
1205 | fputs("\n", f); | |
1206 | } | |
1207 | ||
1208 | if (c->timer_slack_ns_set) | |
1209 | fprintf(f, "%sTimerSlackNS: %lu\n", prefix, c->timer_slack_ns); | |
1210 | ||
1211 | fprintf(f, | |
80876c20 LP |
1212 | "%sStandardInput: %s\n" |
1213 | "%sStandardOutput: %s\n" | |
1214 | "%sStandardError: %s\n", | |
1215 | prefix, exec_input_to_string(c->std_input), | |
1216 | prefix, exec_output_to_string(c->std_output), | |
1217 | prefix, exec_output_to_string(c->std_error)); | |
1218 | ||
1219 | if (c->tty_path) | |
1220 | fprintf(f, | |
1221 | "%sTTYPath: %s\n", | |
1222 | prefix, c->tty_path); | |
94f04347 | 1223 | |
80876c20 LP |
1224 | if (c->std_output == EXEC_OUTPUT_SYSLOG || c->std_output == EXEC_OUTPUT_KERNEL || |
1225 | c->std_error == EXEC_OUTPUT_SYSLOG || c->std_error == EXEC_OUTPUT_KERNEL) | |
94f04347 LP |
1226 | fprintf(f, |
1227 | "%sSyslogFacility: %s\n" | |
1228 | "%sSyslogLevel: %s\n", | |
1229 | prefix, log_facility_to_string(LOG_FAC(c->syslog_priority)), | |
1230 | prefix, log_level_to_string(LOG_PRI(c->syslog_priority))); | |
1231 | ||
1232 | if (c->capabilities) { | |
1233 | char *t; | |
1234 | if ((t = cap_to_text(c->capabilities, NULL))) { | |
1235 | fprintf(f, "%sCapabilities: %s\n", | |
1236 | prefix, t); | |
1237 | cap_free(t); | |
1238 | } | |
1239 | } | |
1240 | ||
1241 | if (c->secure_bits) | |
1242 | fprintf(f, "%sSecure Bits:%s%s%s%s%s%s\n", | |
1243 | prefix, | |
1244 | (c->secure_bits & SECURE_KEEP_CAPS) ? " keep-caps" : "", | |
1245 | (c->secure_bits & SECURE_KEEP_CAPS_LOCKED) ? " keep-caps-locked" : "", | |
1246 | (c->secure_bits & SECURE_NO_SETUID_FIXUP) ? " no-setuid-fixup" : "", | |
1247 | (c->secure_bits & SECURE_NO_SETUID_FIXUP_LOCKED) ? " no-setuid-fixup-locked" : "", | |
1248 | (c->secure_bits & SECURE_NOROOT) ? " noroot" : "", | |
1249 | (c->secure_bits & SECURE_NOROOT_LOCKED) ? "noroot-locked" : ""); | |
1250 | ||
1251 | if (c->capability_bounding_set_drop) { | |
1252 | fprintf(f, "%sCapabilityBoundingSetDrop:", prefix); | |
1253 | ||
1254 | for (i = 0; i <= CAP_LAST_CAP; i++) | |
1255 | if (c->capability_bounding_set_drop & (1 << i)) { | |
1256 | char *t; | |
1257 | ||
1258 | if ((t = cap_to_name(i))) { | |
1259 | fprintf(f, " %s", t); | |
1260 | free(t); | |
1261 | } | |
1262 | } | |
1263 | ||
1264 | fputs("\n", f); | |
1265 | } | |
1266 | ||
1267 | if (c->user) | |
1268 | fprintf(f, "%sUser: %s", prefix, c->user); | |
1269 | if (c->group) | |
1270 | fprintf(f, "%sGroup: %s", prefix, c->group); | |
1271 | ||
1272 | if (c->supplementary_groups) { | |
1273 | char **g; | |
1274 | ||
1275 | fprintf(f, "%sSupplementaryGroups:", prefix); | |
1276 | ||
1277 | STRV_FOREACH(g, c->supplementary_groups) | |
1278 | fprintf(f, " %s", *g); | |
1279 | ||
1280 | fputs("\n", f); | |
1281 | } | |
5cb5a6ff LP |
1282 | } |
1283 | ||
034c6ed7 LP |
1284 | void exec_status_fill(ExecStatus *s, pid_t pid, int code, int status) { |
1285 | assert(s); | |
5cb5a6ff | 1286 | |
034c6ed7 | 1287 | s->pid = pid; |
9fb86720 LP |
1288 | s->exit_timestamp = now(CLOCK_REALTIME); |
1289 | ||
034c6ed7 LP |
1290 | s->code = code; |
1291 | s->status = status; | |
9fb86720 LP |
1292 | } |
1293 | ||
1294 | void exec_status_dump(ExecStatus *s, FILE *f, const char *prefix) { | |
1295 | char buf[FORMAT_TIMESTAMP_MAX]; | |
1296 | ||
1297 | assert(s); | |
1298 | assert(f); | |
1299 | ||
1300 | if (!prefix) | |
1301 | prefix = ""; | |
1302 | ||
1303 | if (s->pid <= 0) | |
1304 | return; | |
1305 | ||
1306 | fprintf(f, | |
1307 | "%sPID: %llu\n", | |
1308 | prefix, (unsigned long long) s->pid); | |
1309 | ||
1310 | if (s->start_timestamp > 0) | |
1311 | fprintf(f, | |
1312 | "%sStart Timestamp: %s\n", | |
1313 | prefix, format_timestamp(buf, sizeof(buf), s->start_timestamp)); | |
1314 | ||
1315 | if (s->exit_timestamp > 0) | |
1316 | fprintf(f, | |
1317 | "%sExit Timestamp: %s\n" | |
1318 | "%sExit Code: %s\n" | |
1319 | "%sExit Status: %i\n", | |
1320 | prefix, format_timestamp(buf, sizeof(buf), s->exit_timestamp), | |
1321 | prefix, sigchld_code_to_string(s->code), | |
1322 | prefix, s->status); | |
5cb5a6ff | 1323 | } |
44d8db9e | 1324 | |
9e2f7c11 | 1325 | char *exec_command_line(char **argv) { |
44d8db9e LP |
1326 | size_t k; |
1327 | char *n, *p, **a; | |
1328 | bool first = true; | |
1329 | ||
9e2f7c11 | 1330 | assert(argv); |
44d8db9e | 1331 | |
9164977d | 1332 | k = 1; |
9e2f7c11 | 1333 | STRV_FOREACH(a, argv) |
44d8db9e LP |
1334 | k += strlen(*a)+3; |
1335 | ||
1336 | if (!(n = new(char, k))) | |
1337 | return NULL; | |
1338 | ||
1339 | p = n; | |
9e2f7c11 | 1340 | STRV_FOREACH(a, argv) { |
44d8db9e LP |
1341 | |
1342 | if (!first) | |
1343 | *(p++) = ' '; | |
1344 | else | |
1345 | first = false; | |
1346 | ||
1347 | if (strpbrk(*a, WHITESPACE)) { | |
1348 | *(p++) = '\''; | |
1349 | p = stpcpy(p, *a); | |
1350 | *(p++) = '\''; | |
1351 | } else | |
1352 | p = stpcpy(p, *a); | |
1353 | ||
1354 | } | |
1355 | ||
9164977d LP |
1356 | *p = 0; |
1357 | ||
44d8db9e LP |
1358 | /* FIXME: this doesn't really handle arguments that have |
1359 | * spaces and ticks in them */ | |
1360 | ||
1361 | return n; | |
1362 | } | |
1363 | ||
1364 | void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) { | |
9fb86720 LP |
1365 | char *p2; |
1366 | const char *prefix2; | |
1367 | ||
44d8db9e LP |
1368 | char *cmd; |
1369 | ||
1370 | assert(c); | |
1371 | assert(f); | |
1372 | ||
1373 | if (!prefix) | |
1374 | prefix = ""; | |
9fb86720 LP |
1375 | p2 = strappend(prefix, "\t"); |
1376 | prefix2 = p2 ? p2 : prefix; | |
44d8db9e | 1377 | |
9e2f7c11 | 1378 | cmd = exec_command_line(c->argv); |
44d8db9e LP |
1379 | |
1380 | fprintf(f, | |
1381 | "%sCommand Line: %s\n", | |
1382 | prefix, cmd ? cmd : strerror(ENOMEM)); | |
1383 | ||
1384 | free(cmd); | |
9fb86720 LP |
1385 | |
1386 | exec_status_dump(&c->exec_status, f, prefix2); | |
1387 | ||
1388 | free(p2); | |
44d8db9e LP |
1389 | } |
1390 | ||
1391 | void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix) { | |
1392 | assert(f); | |
1393 | ||
1394 | if (!prefix) | |
1395 | prefix = ""; | |
1396 | ||
1397 | LIST_FOREACH(command, c, c) | |
1398 | exec_command_dump(c, f, prefix); | |
1399 | } | |
94f04347 | 1400 | |
a6a80b4f LP |
1401 | void exec_command_append_list(ExecCommand **l, ExecCommand *e) { |
1402 | ExecCommand *end; | |
1403 | ||
1404 | assert(l); | |
1405 | assert(e); | |
1406 | ||
1407 | if (*l) { | |
1408 | /* It's kinda important that we keep the order here */ | |
1409 | LIST_FIND_TAIL(ExecCommand, command, *l, end); | |
1410 | LIST_INSERT_AFTER(ExecCommand, command, *l, end, e); | |
1411 | } else | |
1412 | *l = e; | |
1413 | } | |
1414 | ||
26fd040d LP |
1415 | int exec_command_set(ExecCommand *c, const char *path, ...) { |
1416 | va_list ap; | |
1417 | char **l, *p; | |
1418 | ||
1419 | assert(c); | |
1420 | assert(path); | |
1421 | ||
1422 | va_start(ap, path); | |
1423 | l = strv_new_ap(path, ap); | |
1424 | va_end(ap); | |
1425 | ||
1426 | if (!l) | |
1427 | return -ENOMEM; | |
1428 | ||
1429 | if (!(p = strdup(path))) { | |
1430 | strv_free(l); | |
1431 | return -ENOMEM; | |
1432 | } | |
1433 | ||
1434 | free(c->path); | |
1435 | c->path = p; | |
1436 | ||
1437 | strv_free(c->argv); | |
1438 | c->argv = l; | |
1439 | ||
1440 | return 0; | |
1441 | } | |
1442 | ||
80876c20 | 1443 | const char* exit_status_to_string(ExitStatus status) { |
b8d3418f LP |
1444 | |
1445 | /* We cast to int here, so that -Wenum doesn't complain that | |
1446 | * EXIT_SUCCESS/EXIT_FAILURE aren't in the enum */ | |
1447 | ||
1448 | switch ((int) status) { | |
80876c20 LP |
1449 | |
1450 | case EXIT_SUCCESS: | |
1451 | return "SUCCESS"; | |
1452 | ||
1453 | case EXIT_FAILURE: | |
1454 | return "FAILURE"; | |
1455 | ||
1456 | case EXIT_INVALIDARGUMENT: | |
1457 | return "INVALIDARGUMENT"; | |
1458 | ||
1459 | case EXIT_NOTIMPLEMENTED: | |
1460 | return "NOTIMPLEMENTED"; | |
1461 | ||
1462 | case EXIT_NOPERMISSION: | |
1463 | return "NOPERMISSION"; | |
1464 | ||
1465 | case EXIT_NOTINSTALLED: | |
1466 | return "NOTINSSTALLED"; | |
1467 | ||
1468 | case EXIT_NOTCONFIGURED: | |
1469 | return "NOTCONFIGURED"; | |
1470 | ||
1471 | case EXIT_NOTRUNNING: | |
1472 | return "NOTRUNNING"; | |
1473 | ||
1474 | case EXIT_CHDIR: | |
1475 | return "CHDIR"; | |
1476 | ||
1477 | case EXIT_NICE: | |
1478 | return "NICE"; | |
1479 | ||
1480 | case EXIT_FDS: | |
1481 | return "FDS"; | |
1482 | ||
1483 | case EXIT_EXEC: | |
1484 | return "EXEC"; | |
1485 | ||
1486 | case EXIT_MEMORY: | |
1487 | return "MEMORY"; | |
1488 | ||
1489 | case EXIT_LIMITS: | |
1490 | return "LIMITS"; | |
1491 | ||
1492 | case EXIT_OOM_ADJUST: | |
1493 | return "OOM_ADJUST"; | |
1494 | ||
1495 | case EXIT_SIGNAL_MASK: | |
1496 | return "SIGNAL_MASK"; | |
1497 | ||
1498 | case EXIT_STDIN: | |
1499 | return "STDIN"; | |
1500 | ||
1501 | case EXIT_STDOUT: | |
1502 | return "STDOUT"; | |
1503 | ||
1504 | case EXIT_CHROOT: | |
1505 | return "CHROOT"; | |
1506 | ||
1507 | case EXIT_IOPRIO: | |
1508 | return "IOPRIO"; | |
1509 | ||
1510 | case EXIT_TIMERSLACK: | |
1511 | return "TIMERSLACK"; | |
1512 | ||
1513 | case EXIT_SECUREBITS: | |
1514 | return "SECUREBITS"; | |
1515 | ||
1516 | case EXIT_SETSCHEDULER: | |
1517 | return "SETSCHEDULER"; | |
1518 | ||
1519 | case EXIT_CPUAFFINITY: | |
1520 | return "CPUAFFINITY"; | |
1521 | ||
1522 | case EXIT_GROUP: | |
1523 | return "GROUP"; | |
1524 | ||
1525 | case EXIT_USER: | |
1526 | return "USER"; | |
1527 | ||
1528 | case EXIT_CAPABILITIES: | |
1529 | return "CAPABILITIES"; | |
1530 | ||
1531 | case EXIT_CGROUP: | |
1532 | return "CGROUP"; | |
1533 | ||
1534 | case EXIT_SETSID: | |
1535 | return "SETSID"; | |
1536 | ||
1537 | case EXIT_CONFIRM: | |
1538 | return "CONFIRM"; | |
1539 | ||
1540 | case EXIT_STDERR: | |
1541 | return "STDERR"; | |
1542 | ||
1543 | default: | |
1544 | return NULL; | |
1545 | } | |
1546 | } | |
1547 | ||
1548 | static const char* const exec_input_table[_EXEC_INPUT_MAX] = { | |
1549 | [EXEC_INPUT_NULL] = "null", | |
1550 | [EXEC_INPUT_TTY] = "tty", | |
1551 | [EXEC_INPUT_TTY_FORCE] = "tty-force", | |
4f2d528d LP |
1552 | [EXEC_INPUT_TTY_FAIL] = "tty-fail", |
1553 | [EXEC_INPUT_SOCKET] = "socket" | |
80876c20 LP |
1554 | }; |
1555 | ||
94f04347 | 1556 | static const char* const exec_output_table[_EXEC_OUTPUT_MAX] = { |
80876c20 | 1557 | [EXEC_OUTPUT_INHERIT] = "inherit", |
94f04347 | 1558 | [EXEC_OUTPUT_NULL] = "null", |
80876c20 | 1559 | [EXEC_OUTPUT_TTY] = "tty", |
94f04347 | 1560 | [EXEC_OUTPUT_SYSLOG] = "syslog", |
4f2d528d LP |
1561 | [EXEC_OUTPUT_KERNEL] = "kernel", |
1562 | [EXEC_OUTPUT_SOCKET] = "socket" | |
94f04347 LP |
1563 | }; |
1564 | ||
1565 | DEFINE_STRING_TABLE_LOOKUP(exec_output, ExecOutput); | |
1566 | ||
94f04347 | 1567 | DEFINE_STRING_TABLE_LOOKUP(exec_input, ExecInput); |