]> git.ipfire.org Git - people/ms/systemd.git/blame - execute.c
unit: automatically connect to syslog when it becomes available
[people/ms/systemd.git] / execute.c
CommitLineData
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
034c6ed7
LP
47static int close_fds(int except[], unsigned n_except) {
48 DIR *d;
49 struct dirent *de;
50 int r = 0;
51
52 /* Modifies the fds array! (sorts it) */
53
54 if (!(d = opendir("/proc/self/fd")))
55 return -errno;
56
57 while ((de = readdir(d))) {
58 int fd;
59
60 if (de->d_name[0] == '.')
61 continue;
62
63 if ((r = safe_atoi(de->d_name, &fd)) < 0)
64 goto finish;
65
66 if (fd < 3)
67 continue;
68
69 if (fd == dirfd(d))
70 continue;
71
72 if (except) {
73 bool found;
74 unsigned i;
75
76 found = false;
77 for (i = 0; i < n_except; i++)
78 if (except[i] == fd) {
79 found = true;
80 break;
81 }
82
83 if (found)
84 continue;
85 }
86
87 if ((r = close_nointr(fd)) < 0)
88 goto finish;
89 }
90
91finish:
92 closedir(d);
93 return r;
94}
95
96static int shift_fds(int fds[], unsigned n_fds) {
97 int start, restart_from;
98
99 if (n_fds <= 0)
100 return 0;
101
102 assert(fds);
103
104 start = 0;
105 for (;;) {
106 int i;
107
108 restart_from = -1;
109
110 for (i = start; i < (int) n_fds; i++) {
111 int nfd;
112
113 /* Already at right index? */
114 if (fds[i] == i+3)
115 continue;
116
117 if ((nfd = fcntl(fds[i], F_DUPFD, i+3)) < 0)
118 return -errno;
119
2da3263a 120 assert_se(close_nointr(fds[i]) == 0);
034c6ed7
LP
121 fds[i] = nfd;
122
123 /* Hmm, the fd we wanted isn't free? Then
124 * let's remember that and try again from here*/
125 if (nfd != i+3 && restart_from < 0)
126 restart_from = i;
127 }
128
129 if (restart_from < 0)
130 break;
131
132 start = restart_from;
133 }
134
135 return 0;
136}
137
451a074f 138static int flags_fds(int fds[], unsigned n_fds, bool nonblock) {
47a71eed 139 unsigned i;
e2c76839 140 int r;
47a71eed
LP
141
142 if (n_fds <= 0)
143 return 0;
144
145 assert(fds);
146
451a074f 147 /* Drops/Sets O_NONBLOCK and FD_CLOEXEC from the file flags */
47a71eed
LP
148
149 for (i = 0; i < n_fds; i++) {
47a71eed 150
e2c76839
LP
151 if ((r = fd_nonblock(fds[i], nonblock)) < 0)
152 return r;
47a71eed 153
451a074f
LP
154 /* We unconditionally drop FD_CLOEXEC from the fds,
155 * since after all we want to pass these fds to our
156 * children */
47a71eed 157
e2c76839
LP
158 if ((r = fd_cloexec(fds[i], false)) < 0)
159 return r;
47a71eed
LP
160 }
161
162 return 0;
163}
164
071830ff
LP
165static int replace_null_fd(int fd, int flags) {
166 int nfd;
167 assert(fd >= 0);
168
169 close_nointr(fd);
170
171 if ((nfd = open("/dev/null", flags|O_NOCTTY)) < 0)
172 return -errno;
173
174 if (nfd != fd) {
175 close_nointr_nofail(nfd);
176 return -EIO;
177 }
178
179 return 0;
180}
181
182static int setup_output(const ExecContext *context, const char *ident) {
183 int r;
184
185 assert(context);
186
187 switch (context->output) {
188
94f04347 189 case EXEC_OUTPUT_CONSOLE:
071830ff
LP
190 return 0;
191
94f04347 192 case EXEC_OUTPUT_NULL:
071830ff 193
94f04347 194 if ((r = replace_null_fd(STDOUT_FILENO, O_WRONLY)) < 0 ||
071830ff
LP
195 (r = replace_null_fd(STDERR_FILENO, O_WRONLY)) < 0)
196 return r;
197
198 return 0;
199
94f04347
LP
200 case EXEC_OUTPUT_KERNEL:
201 case EXEC_OUTPUT_SYSLOG: {
071830ff
LP
202
203 int fd;
204 union {
205 struct sockaddr sa;
206 struct sockaddr_un un;
207 } sa;
208
071830ff
LP
209 close_nointr(STDOUT_FILENO);
210 close_nointr(STDERR_FILENO);
211
212 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
213 return -errno;
214
215 if (fd != STDOUT_FILENO) {
216 close_nointr_nofail(fd);
217 return -EIO;
218 }
219
220 zero(sa);
221 sa.sa.sa_family = AF_UNIX;
222 strncpy(sa.un.sun_path+1, LOGGER_SOCKET, sizeof(sa.un.sun_path)-1);
223
224 if (connect(fd, &sa.sa, sizeof(sa)) < 0) {
225 close_nointr_nofail(fd);
226 return -errno;
227 }
228
229 if (shutdown(fd, SHUT_RD) < 0) {
230 close_nointr_nofail(fd);
231 return -errno;
232 }
233
234 if ((fd = dup(fd)) < 0) {
235 close_nointr_nofail(fd);
236 return -errno;
237 }
238
239 if (fd != STDERR_FILENO) {
240 close_nointr_nofail(fd);
241 return -EIO;
242 }
243
244 /* We speak a very simple protocol between log server
245 * and client: one line for the log destination (kmsg
246 * or syslog), followed by the priority field,
247 * followed by the process name. Since we replaced
248 * stdin/stderr we simple use stdio to write to
249 * it. Note that we use stderr, to minimize buffer
250 * flushing issues. */
251
252 fprintf(stderr,
253 "%s\n"
254 "%i\n"
255 "%s\n",
94f04347 256 context->output == EXEC_OUTPUT_KERNEL ? "kmsg" : "syslog",
071830ff
LP
257 context->syslog_priority,
258 context->syslog_identifier ? context->syslog_identifier : ident);
259
260 return 0;
261 }
94f04347
LP
262
263 default:
264 assert_not_reached("Unknown output type");
071830ff 265 }
94f04347
LP
266}
267
47be870b 268static int setup_input(const ExecContext *context) {
94f04347
LP
269 int r;
270
271 assert(context);
071830ff 272
94f04347
LP
273 switch (context->input) {
274
275 case EXEC_INPUT_CONSOLE:
276 return 0;
277
278 case EXEC_INPUT_NULL:
279 if ((r = replace_null_fd(STDIN_FILENO, O_RDONLY)) < 0)
280 return r;
281
282 return 0;
283
284 default:
285 assert_not_reached("Unknown input type");
286 }
071830ff
LP
287}
288
81a2b7ce
LP
289static int get_group_creds(const char *groupname, gid_t *gid) {
290 struct group *g;
291 unsigned long lu;
292
293 assert(groupname);
294 assert(gid);
295
296 /* We enforce some special rules for gid=0: in order to avoid
297 * NSS lookups for root we hardcode its data. */
298
299 if (streq(groupname, "root") || streq(groupname, "0")) {
300 *gid = 0;
301 return 0;
302 }
303
304 if (safe_atolu(groupname, &lu) >= 0) {
305 errno = 0;
306 g = getgrgid((gid_t) lu);
307 } else {
308 errno = 0;
309 g = getgrnam(groupname);
310 }
311
312 if (!g)
313 return errno != 0 ? -errno : -ESRCH;
314
315 *gid = g->gr_gid;
316 return 0;
317}
318
319static int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home) {
320 struct passwd *p;
321 unsigned long lu;
322
323 assert(username);
324 assert(*username);
325 assert(uid);
326 assert(gid);
327 assert(home);
328
329 /* We enforce some special rules for uid=0: in order to avoid
330 * NSS lookups for root we hardcode its data. */
331
332 if (streq(*username, "root") || streq(*username, "0")) {
333 *username = "root";
334 *uid = 0;
335 *gid = 0;
336 *home = "/root";
337 return 0;
338 }
339
340 if (safe_atolu(*username, &lu) >= 0) {
341 errno = 0;
342 p = getpwuid((uid_t) lu);
343
344 /* If there are multiple users with the same id, make
345 * sure to leave $USER to the configured value instead
346 * of the first occurence in the database. However if
347 * the uid was configured by a numeric uid, then let's
348 * pick the real username from /etc/passwd. */
349 if (*username && p)
350 *username = p->pw_name;
351 } else {
352 errno = 0;
353 p = getpwnam(*username);
354 }
355
356 if (!p)
357 return errno != 0 ? -errno : -ESRCH;
358
359 *uid = p->pw_uid;
360 *gid = p->pw_gid;
361 *home = p->pw_dir;
362 return 0;
363}
364
365static int enforce_groups(const ExecContext *context, const char *username, gid_t gid) {
366 bool keep_groups = false;
367 int r;
368
369 assert(context);
370
371 /* Lookup and ser GID and supplementary group list. Here too
372 * we avoid NSS lookups for gid=0. */
373
374 if (context->group || username) {
375
376 if (context->group)
377 if ((r = get_group_creds(context->group, &gid)) < 0)
378 return r;
379
380 /* First step, initialize groups from /etc/groups */
381 if (username && gid != 0) {
382 if (initgroups(username, gid) < 0)
383 return -errno;
384
385 keep_groups = true;
386 }
387
388 /* Second step, set our gids */
389 if (setresgid(gid, gid, gid) < 0)
390 return -errno;
391 }
392
393 if (context->supplementary_groups) {
394 int ngroups_max, k;
395 gid_t *gids;
396 char **i;
397
398 /* Final step, initialize any manually set supplementary groups */
399 ngroups_max = (int) sysconf(_SC_NGROUPS_MAX);
400
401 if (!(gids = new(gid_t, ngroups_max)))
402 return -ENOMEM;
403
404 if (keep_groups) {
405 if ((k = getgroups(ngroups_max, gids)) < 0) {
406 free(gids);
407 return -errno;
408 }
409 } else
410 k = 0;
411
412 STRV_FOREACH(i, context->supplementary_groups) {
413
414 if (k >= ngroups_max) {
415 free(gids);
416 return -E2BIG;
417 }
418
419 if ((r = get_group_creds(*i, gids+k)) < 0) {
420 free(gids);
421 return r;
422 }
423
424 k++;
425 }
426
427 if (setgroups(k, gids) < 0) {
428 free(gids);
429 return -errno;
430 }
431
432 free(gids);
433 }
434
435 return 0;
436}
437
438static int enforce_user(const ExecContext *context, uid_t uid) {
439 int r;
440 assert(context);
441
442 /* Sets (but doesn't lookup) the uid and make sure we keep the
443 * capabilities while doing so. */
444
445 if (context->capabilities) {
446 cap_t d;
447 static const cap_value_t bits[] = {
448 CAP_SETUID, /* Necessary so that we can run setresuid() below */
449 CAP_SETPCAP /* Necessary so that we can set PR_SET_SECUREBITS later on */
450 };
451
452 /* First step: If we need to keep capabilities but
453 * drop privileges we need to make sure we keep our
454 * caps, whiel we drop priviliges. */
693ced48
LP
455 if (uid != 0) {
456 int sb = context->secure_bits|SECURE_KEEP_CAPS;
457
458 if (prctl(PR_GET_SECUREBITS) != sb)
459 if (prctl(PR_SET_SECUREBITS, sb) < 0)
460 return -errno;
461 }
81a2b7ce
LP
462
463 /* Second step: set the capabilites. This will reduce
464 * the capabilities to the minimum we need. */
465
466 if (!(d = cap_dup(context->capabilities)))
467 return -errno;
468
469 if (cap_set_flag(d, CAP_EFFECTIVE, ELEMENTSOF(bits), bits, CAP_SET) < 0 ||
470 cap_set_flag(d, CAP_PERMITTED, ELEMENTSOF(bits), bits, CAP_SET) < 0) {
471 r = -errno;
472 cap_free(d);
473 return r;
474 }
475
476 if (cap_set_proc(d) < 0) {
477 r = -errno;
478 cap_free(d);
479 return r;
480 }
481
482 cap_free(d);
483 }
484
485 /* Third step: actually set the uids */
486 if (setresuid(uid, uid, uid) < 0)
487 return -errno;
488
489 /* At this point we should have all necessary capabilities but
490 are otherwise a normal user. However, the caps might got
491 corrupted due to the setresuid() so we need clean them up
492 later. This is done outside of this call. */
493
494 return 0;
495}
496
497int exec_spawn(const ExecCommand *command,
498 const ExecContext *context,
499 int *fds, unsigned n_fds,
500 bool apply_permissions,
501 bool apply_chroot,
8e274523 502 CGroupBonding *cgroup_bondings,
81a2b7ce
LP
503 pid_t *ret) {
504
034c6ed7 505 pid_t pid;
8e274523 506 int r;
034c6ed7 507
5cb5a6ff
LP
508 assert(command);
509 assert(context);
510 assert(ret);
034c6ed7
LP
511 assert(fds || n_fds <= 0);
512
81a2b7ce 513 log_debug("About to execute %s", command->path);
acbb0225 514
8e274523
LP
515 if (cgroup_bondings)
516 if ((r = cgroup_bonding_realize_list(cgroup_bondings)))
517 return r;
518
034c6ed7
LP
519 if ((pid = fork()) < 0)
520 return -errno;
521
522 if (pid == 0) {
8e274523 523 int i;
309bff19 524 sigset_t ss;
81a2b7ce
LP
525 const char *username = NULL, *home = NULL;
526 uid_t uid = (uid_t) -1;
527 gid_t gid = (gid_t) -1;
528 char **our_env = NULL, **final_env = NULL;
529 unsigned n_env = 0;
309bff19 530
034c6ed7 531 /* child */
5cb5a6ff 532
309bff19
LP
533 if (sigemptyset(&ss) < 0 ||
534 sigprocmask(SIG_SETMASK, &ss, NULL) < 0) {
535 r = EXIT_SIGNAL_MASK;
536 goto fail;
537 }
538
9eba9da4
LP
539 if (setpgid(0, 0) < 0) {
540 r = EXIT_PGID;
034c6ed7
LP
541 goto fail;
542 }
543
9eba9da4
LP
544 umask(context->umask);
545
94f04347
LP
546 if (setup_input(context) < 0) {
547 r = EXIT_INPUT;
548 goto fail;
549 }
550
071830ff
LP
551 if (setup_output(context, file_name_from_path(command->path)) < 0) {
552 r = EXIT_OUTPUT;
553 goto fail;
554 }
555
8e274523
LP
556 if (cgroup_bondings)
557 if ((r = cgroup_bonding_install_list(cgroup_bondings, 0)) < 0) {
558 r = EXIT_CGROUP;
559 goto fail;
560 }
561
fb33a393
LP
562 if (context->oom_adjust_set) {
563 char t[16];
034c6ed7 564
fb33a393
LP
565 snprintf(t, sizeof(t), "%i", context->oom_adjust);
566 char_array_0(t);
034c6ed7 567
fb33a393
LP
568 if (write_one_line_file("/proc/self/oom_adj", t) < 0) {
569 r = EXIT_OOM_ADJUST;
570 goto fail;
571 }
034c6ed7
LP
572 }
573
fb33a393
LP
574 if (context->nice_set)
575 if (setpriority(PRIO_PROCESS, 0, context->nice) < 0) {
576 r = EXIT_NICE;
577 goto fail;
578 }
579
94f04347
LP
580 if (context->cpu_sched_set) {
581 struct sched_param param;
582
583 zero(param);
584 param.sched_priority = context->cpu_sched_priority;
585
38b48754
LP
586 if (sched_setscheduler(0, context->cpu_sched_policy |
587 (context->cpu_sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0), &param) < 0) {
94f04347
LP
588 r = EXIT_SETSCHEDULER;
589 goto fail;
590 }
591 }
592
593 if (context->cpu_affinity_set)
594 if (sched_setaffinity(0, sizeof(context->cpu_affinity), &context->cpu_affinity) < 0) {
595 r = EXIT_CPUAFFINITY;
596 goto fail;
597 }
598
9eba9da4
LP
599 if (context->ioprio_set)
600 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, context->ioprio) < 0) {
601 r = EXIT_IOPRIO;
602 goto fail;
603 }
604
94f04347
LP
605 if (context->timer_slack_ns_set)
606 if (prctl(PR_SET_TIMERSLACK, context->timer_slack_ns_set) < 0) {
607 r = EXIT_TIMERSLACK;
608 goto fail;
609 }
610
81a2b7ce
LP
611 if (context->user) {
612 username = context->user;
613 if (get_user_creds(&username, &uid, &gid, &home) < 0) {
614 r = EXIT_USER;
615 goto fail;
616 }
617 }
618
619 if (apply_permissions)
620 if (enforce_groups(context, username, uid) < 0) {
621 r = EXIT_GROUP;
622 goto fail;
623 }
624
625 if (apply_chroot) {
626 if (context->root_directory)
627 if (chroot(context->root_directory) < 0) {
628 r = EXIT_CHROOT;
629 goto fail;
630 }
631
632 if (chdir(context->working_directory ? context->working_directory : "/") < 0) {
633 r = EXIT_CHDIR;
634 goto fail;
635 }
636 } else {
637
638 char *d;
639
640 if (asprintf(&d, "%s/%s",
641 context->root_directory ? context->root_directory : "",
642 context->working_directory ? context->working_directory : "") < 0) {
643 r = EXIT_MEMORY;
644 goto fail;
645 }
646
647 if (chdir(d) < 0) {
648 free(d);
649 r = EXIT_CHDIR;
650 goto fail;
651 }
652
653 free(d);
654 }
655
034c6ed7 656 if (close_fds(fds, n_fds) < 0 ||
47a71eed 657 shift_fds(fds, n_fds) < 0 ||
451a074f 658 flags_fds(fds, n_fds, context->non_blocking) < 0) {
034c6ed7
LP
659 r = EXIT_FDS;
660 goto fail;
661 }
662
81a2b7ce 663 if (apply_permissions) {
034c6ed7 664
81a2b7ce
LP
665 for (i = 0; i < RLIMIT_NLIMITS; i++) {
666 if (!context->rlimit[i])
667 continue;
668
669 if (setrlimit(i, context->rlimit[i]) < 0) {
670 r = EXIT_LIMITS;
671 goto fail;
672 }
034c6ed7 673 }
034c6ed7 674
81a2b7ce
LP
675 if (context->user)
676 if (enforce_user(context, uid) < 0) {
677 r = EXIT_USER;
678 goto fail;
679 }
680
693ced48
LP
681 /* PR_GET_SECUREBITS is not priviliged, while
682 * PR_SET_SECUREBITS is. So to suppress
683 * potential EPERMs we'll try not to call
684 * PR_SET_SECUREBITS unless necessary. */
685 if (prctl(PR_GET_SECUREBITS) != context->secure_bits)
686 if (prctl(PR_SET_SECUREBITS, context->secure_bits) < 0) {
687 r = EXIT_SECUREBITS;
688 goto fail;
689 }
81a2b7ce
LP
690
691 if (context->capabilities)
692 if (cap_set_proc(context->capabilities) < 0) {
693 r = EXIT_CAPABILITIES;
694 goto fail;
695 }
94f04347
LP
696 }
697
81a2b7ce
LP
698 if (!(our_env = new0(char*, 6))) {
699 r = EXIT_MEMORY;
700 goto fail;
701 }
034c6ed7 702
81a2b7ce
LP
703 if (n_fds > 0)
704 if (asprintf(our_env + n_env++, "LISTEN_PID=%llu", (unsigned long long) getpid()) < 0 ||
705 asprintf(our_env + n_env++, "LISTEN_FDS=%u", n_fds) < 0) {
706 r = EXIT_MEMORY;
707 goto fail;
708 }
034c6ed7 709
81a2b7ce
LP
710 if (home)
711 if (asprintf(our_env + n_env++, "HOME=%s", home) < 0) {
712 r = EXIT_MEMORY;
713 goto fail;
714 }
034c6ed7 715
81a2b7ce
LP
716 if (username)
717 if (asprintf(our_env + n_env++, "LOGNAME=%s", username) < 0 ||
718 asprintf(our_env + n_env++, "USER=%s", username) < 0) {
719 r = EXIT_MEMORY;
720 goto fail;
721 }
034c6ed7 722
81a2b7ce
LP
723 if (!(final_env = strv_env_merge(environ, our_env, context->environment, NULL))) {
724 r = EXIT_MEMORY;
725 goto fail;
726 }
034c6ed7 727
81a2b7ce 728 execve(command->path, command->argv, final_env);
034c6ed7
LP
729 r = EXIT_EXEC;
730
731 fail:
81a2b7ce
LP
732 strv_free(our_env);
733 strv_free(final_env);
734
034c6ed7
LP
735 _exit(r);
736 }
737
2da3263a 738
81a2b7ce 739 log_debug("Forked %s as %llu", command->path, (unsigned long long) pid);
2da3263a 740
034c6ed7 741 *ret = pid;
5cb5a6ff
LP
742 return 0;
743}
744
034c6ed7
LP
745void exec_context_init(ExecContext *c) {
746 assert(c);
747
748 c->umask = 0002;
034c6ed7 749 c->oom_adjust = 0;
9eba9da4 750 c->oom_adjust_set = false;
034c6ed7 751 c->nice = 0;
9eba9da4
LP
752 c->nice_set = false;
753 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0);
754 c->ioprio_set = false;
94f04347
LP
755 c->cpu_sched_policy = SCHED_OTHER;
756 c->cpu_sched_priority = 0;
757 c->cpu_sched_set = false;
758 CPU_ZERO(&c->cpu_affinity);
759 c->cpu_affinity_set = false;
071830ff 760
94f04347 761 c->input = 0;
071830ff
LP
762 c->output = 0;
763 c->syslog_priority = LOG_DAEMON|LOG_INFO;
94f04347
LP
764
765 c->secure_bits = 0;
766 c->capability_bounding_set_drop = 0;
034c6ed7
LP
767}
768
769void exec_context_done(ExecContext *c) {
5cb5a6ff
LP
770 unsigned l;
771
772 assert(c);
773
774 strv_free(c->environment);
034c6ed7 775 c->environment = NULL;
5cb5a6ff 776
034c6ed7 777 for (l = 0; l < ELEMENTSOF(c->rlimit); l++) {
5cb5a6ff 778 free(c->rlimit[l]);
034c6ed7
LP
779 c->rlimit[l] = NULL;
780 }
781
9eba9da4
LP
782 free(c->working_directory);
783 c->working_directory = NULL;
784 free(c->root_directory);
785 c->root_directory = NULL;
5cb5a6ff 786
071830ff
LP
787 free(c->syslog_identifier);
788 c->syslog_identifier = NULL;
789
5cb5a6ff 790 free(c->user);
034c6ed7
LP
791 c->user = NULL;
792
5cb5a6ff 793 free(c->group);
034c6ed7
LP
794 c->group = NULL;
795
796 strv_free(c->supplementary_groups);
797 c->supplementary_groups = NULL;
94f04347
LP
798
799 if (c->capabilities) {
800 cap_free(c->capabilities);
801 c->capabilities = NULL;
802 }
5cb5a6ff
LP
803}
804
805void exec_command_free_list(ExecCommand *c) {
806 ExecCommand *i;
807
808 while ((i = c)) {
034c6ed7 809 LIST_REMOVE(ExecCommand, command, c, i);
5cb5a6ff
LP
810
811 free(i->path);
44d8db9e 812 strv_free(i->argv);
5cb5a6ff
LP
813 free(i);
814 }
815}
816
034c6ed7
LP
817void exec_command_free_array(ExecCommand **c, unsigned n) {
818 unsigned i;
819
820 for (i = 0; i < n; i++) {
821 exec_command_free_list(c[i]);
822 c[i] = NULL;
823 }
824}
825
5cb5a6ff 826void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
94f04347
LP
827 char ** e;
828 unsigned i;
9eba9da4 829
5cb5a6ff
LP
830 assert(c);
831 assert(f);
832
833 if (!prefix)
834 prefix = "";
835
836 fprintf(f,
94f04347
LP
837 "%sUMask: %04o\n"
838 "%sWorkingDirectory: %s\n"
451a074f
LP
839 "%sRootDirectory: %s\n"
840 "%sNonBlocking: %s\n",
5cb5a6ff 841 prefix, c->umask,
9eba9da4 842 prefix, c->working_directory ? c->working_directory : "/",
451a074f
LP
843 prefix, c->root_directory ? c->root_directory : "/",
844 prefix, yes_no(c->non_blocking));
fb33a393 845
94f04347
LP
846 if (c->environment)
847 for (e = c->environment; *e; e++)
848 fprintf(f, "%sEnvironment: %s\n", prefix, *e);
849
fb33a393
LP
850 if (c->nice_set)
851 fprintf(f,
852 "%sNice: %i\n",
853 prefix, c->nice);
854
855 if (c->oom_adjust_set)
856 fprintf(f,
857 "%sOOMAdjust: %i\n",
858 prefix, c->oom_adjust);
9eba9da4 859
94f04347
LP
860 for (i = 0; i < RLIM_NLIMITS; i++)
861 if (c->rlimit[i])
ea430986 862 fprintf(f, "%s%s: %llu\n", prefix, rlimit_to_string(i), (unsigned long long) c->rlimit[i]->rlim_max);
94f04347 863
9eba9da4
LP
864 if (c->ioprio_set)
865 fprintf(f,
866 "%sIOSchedulingClass: %s\n"
867 "%sIOPriority: %i\n",
94f04347 868 prefix, ioprio_class_to_string(IOPRIO_PRIO_CLASS(c->ioprio)),
9eba9da4 869 prefix, (int) IOPRIO_PRIO_DATA(c->ioprio));
94f04347
LP
870
871 if (c->cpu_sched_set)
872 fprintf(f,
873 "%sCPUSchedulingPolicy: %s\n"
38b48754
LP
874 "%sCPUSchedulingPriority: %i\n"
875 "%sCPUSchedulingResetOnFork: %s\n",
94f04347 876 prefix, sched_policy_to_string(c->cpu_sched_policy),
38b48754
LP
877 prefix, c->cpu_sched_priority,
878 prefix, yes_no(c->cpu_sched_reset_on_fork));
94f04347
LP
879
880 if (c->cpu_affinity_set) {
881 fprintf(f, "%sCPUAffinity:", prefix);
882 for (i = 0; i < CPU_SETSIZE; i++)
883 if (CPU_ISSET(i, &c->cpu_affinity))
884 fprintf(f, " %i", i);
885 fputs("\n", f);
886 }
887
888 if (c->timer_slack_ns_set)
889 fprintf(f, "%sTimerSlackNS: %lu\n", prefix, c->timer_slack_ns);
890
891 fprintf(f,
892 "%sInput: %s\n"
893 "%sOutput: %s\n",
894 prefix, exec_input_to_string(c->input),
895 prefix, exec_output_to_string(c->output));
896
897 if (c->output == EXEC_OUTPUT_SYSLOG || c->output == EXEC_OUTPUT_KERNEL)
898 fprintf(f,
899 "%sSyslogFacility: %s\n"
900 "%sSyslogLevel: %s\n",
901 prefix, log_facility_to_string(LOG_FAC(c->syslog_priority)),
902 prefix, log_level_to_string(LOG_PRI(c->syslog_priority)));
903
904 if (c->capabilities) {
905 char *t;
906 if ((t = cap_to_text(c->capabilities, NULL))) {
907 fprintf(f, "%sCapabilities: %s\n",
908 prefix, t);
909 cap_free(t);
910 }
911 }
912
913 if (c->secure_bits)
914 fprintf(f, "%sSecure Bits:%s%s%s%s%s%s\n",
915 prefix,
916 (c->secure_bits & SECURE_KEEP_CAPS) ? " keep-caps" : "",
917 (c->secure_bits & SECURE_KEEP_CAPS_LOCKED) ? " keep-caps-locked" : "",
918 (c->secure_bits & SECURE_NO_SETUID_FIXUP) ? " no-setuid-fixup" : "",
919 (c->secure_bits & SECURE_NO_SETUID_FIXUP_LOCKED) ? " no-setuid-fixup-locked" : "",
920 (c->secure_bits & SECURE_NOROOT) ? " noroot" : "",
921 (c->secure_bits & SECURE_NOROOT_LOCKED) ? "noroot-locked" : "");
922
923 if (c->capability_bounding_set_drop) {
924 fprintf(f, "%sCapabilityBoundingSetDrop:", prefix);
925
926 for (i = 0; i <= CAP_LAST_CAP; i++)
927 if (c->capability_bounding_set_drop & (1 << i)) {
928 char *t;
929
930 if ((t = cap_to_name(i))) {
931 fprintf(f, " %s", t);
932 free(t);
933 }
934 }
935
936 fputs("\n", f);
937 }
938
939 if (c->user)
940 fprintf(f, "%sUser: %s", prefix, c->user);
941 if (c->group)
942 fprintf(f, "%sGroup: %s", prefix, c->group);
943
944 if (c->supplementary_groups) {
945 char **g;
946
947 fprintf(f, "%sSupplementaryGroups:", prefix);
948
949 STRV_FOREACH(g, c->supplementary_groups)
950 fprintf(f, " %s", *g);
951
952 fputs("\n", f);
953 }
5cb5a6ff
LP
954}
955
034c6ed7
LP
956void exec_status_fill(ExecStatus *s, pid_t pid, int code, int status) {
957 assert(s);
5cb5a6ff 958
034c6ed7
LP
959 s->pid = pid;
960 s->code = code;
961 s->status = status;
962 s->timestamp = now(CLOCK_REALTIME);
5cb5a6ff 963}
44d8db9e
LP
964
965char *exec_command_line(ExecCommand *c) {
966 size_t k;
967 char *n, *p, **a;
968 bool first = true;
969
970 assert(c);
971 assert(c->argv);
972
9164977d 973 k = 1;
44d8db9e
LP
974 STRV_FOREACH(a, c->argv)
975 k += strlen(*a)+3;
976
977 if (!(n = new(char, k)))
978 return NULL;
979
980 p = n;
981 STRV_FOREACH(a, c->argv) {
982
983 if (!first)
984 *(p++) = ' ';
985 else
986 first = false;
987
988 if (strpbrk(*a, WHITESPACE)) {
989 *(p++) = '\'';
990 p = stpcpy(p, *a);
991 *(p++) = '\'';
992 } else
993 p = stpcpy(p, *a);
994
995 }
996
9164977d
LP
997 *p = 0;
998
44d8db9e
LP
999 /* FIXME: this doesn't really handle arguments that have
1000 * spaces and ticks in them */
1001
1002 return n;
1003}
1004
1005void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) {
1006 char *cmd;
1007
1008 assert(c);
1009 assert(f);
1010
1011 if (!prefix)
1012 prefix = "";
1013
1014 cmd = exec_command_line(c);
1015
1016 fprintf(f,
1017 "%sCommand Line: %s\n",
1018 prefix, cmd ? cmd : strerror(ENOMEM));
1019
1020 free(cmd);
1021}
1022
1023void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix) {
1024 assert(f);
1025
1026 if (!prefix)
1027 prefix = "";
1028
1029 LIST_FOREACH(command, c, c)
1030 exec_command_dump(c, f, prefix);
1031}
94f04347 1032
a6a80b4f
LP
1033void exec_command_append_list(ExecCommand **l, ExecCommand *e) {
1034 ExecCommand *end;
1035
1036 assert(l);
1037 assert(e);
1038
1039 if (*l) {
1040 /* It's kinda important that we keep the order here */
1041 LIST_FIND_TAIL(ExecCommand, command, *l, end);
1042 LIST_INSERT_AFTER(ExecCommand, command, *l, end, e);
1043 } else
1044 *l = e;
1045}
1046
94f04347
LP
1047static const char* const exec_output_table[_EXEC_OUTPUT_MAX] = {
1048 [EXEC_OUTPUT_CONSOLE] = "console",
1049 [EXEC_OUTPUT_NULL] = "null",
1050 [EXEC_OUTPUT_SYSLOG] = "syslog",
1051 [EXEC_OUTPUT_KERNEL] = "kernel"
1052};
1053
1054DEFINE_STRING_TABLE_LOOKUP(exec_output, ExecOutput);
1055
1056static const char* const exec_input_table[_EXEC_INPUT_MAX] = {
1057 [EXEC_INPUT_NULL] = "null",
1058 [EXEC_INPUT_CONSOLE] = "console"
1059};
1060
1061DEFINE_STRING_TABLE_LOOKUP(exec_input, ExecInput);