1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
29 #include <sys/socket.h>
31 #include <sys/prctl.h>
32 #include <linux/sched.h>
33 #include <sys/types.h>
42 #include "securebits.h"
44 static int close_fds(int except[], unsigned n_except) {
49 /* Modifies the fds array! (sorts it) */
51 if (!(d = opendir("/proc/self/fd")))
54 while ((de = readdir(d))) {
57 if (de->d_name[0] == '.')
60 if ((r = safe_atoi(de->d_name, &fd)) < 0)
74 for (i = 0; i < n_except; i++)
75 if (except[i] == fd) {
84 if ((r = close_nointr(fd)) < 0)
93 static int shift_fds(int fds[], unsigned n_fds) {
94 int start, restart_from;
107 for (i = start; i < (int) n_fds; i++) {
110 /* Already at right index? */
114 if ((nfd = fcntl(fds[i], F_DUPFD, i+3)) < 0)
117 assert_se(close_nointr(fds[i]) == 0);
120 /* Hmm, the fd we wanted isn't free? Then
121 * let's remember that and try again from here*/
122 if (nfd != i+3 && restart_from < 0)
126 if (restart_from < 0)
129 start = restart_from;
135 static int flags_fds(int fds[], unsigned n_fds, bool nonblock) {
143 /* Drops/Sets O_NONBLOCK and FD_CLOEXEC from the file flags */
145 for (i = 0; i < n_fds; i++) {
148 if ((flags = fcntl(fds[i], F_GETFL, 0)) < 0)
154 flags &= ~O_NONBLOCK;
156 if (fcntl(fds[i], F_SETFL, flags) < 0)
159 /* We unconditionally drop FD_CLOEXEC from the fds,
160 * since after all we want to pass these fds to our
162 if ((flags = fcntl(fds[i], F_GETFD, 0)) < 0)
165 if (fcntl(fds[i], F_SETFD, flags &~FD_CLOEXEC) < 0)
172 static int replace_null_fd(int fd, int flags) {
178 if ((nfd = open("/dev/null", flags|O_NOCTTY)) < 0)
182 close_nointr_nofail(nfd);
189 static int setup_output(const ExecContext *context, const char *ident) {
194 switch (context->output) {
196 case EXEC_OUTPUT_CONSOLE:
199 case EXEC_OUTPUT_NULL:
201 if ((r = replace_null_fd(STDOUT_FILENO, O_WRONLY)) < 0 ||
202 (r = replace_null_fd(STDERR_FILENO, O_WRONLY)) < 0)
207 case EXEC_OUTPUT_KERNEL:
208 case EXEC_OUTPUT_SYSLOG: {
213 struct sockaddr_un un;
216 close_nointr(STDOUT_FILENO);
217 close_nointr(STDERR_FILENO);
219 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
222 if (fd != STDOUT_FILENO) {
223 close_nointr_nofail(fd);
228 sa.sa.sa_family = AF_UNIX;
229 strncpy(sa.un.sun_path+1, LOGGER_SOCKET, sizeof(sa.un.sun_path)-1);
231 if (connect(fd, &sa.sa, sizeof(sa)) < 0) {
232 close_nointr_nofail(fd);
236 if (shutdown(fd, SHUT_RD) < 0) {
237 close_nointr_nofail(fd);
241 if ((fd = dup(fd)) < 0) {
242 close_nointr_nofail(fd);
246 if (fd != STDERR_FILENO) {
247 close_nointr_nofail(fd);
251 /* We speak a very simple protocol between log server
252 * and client: one line for the log destination (kmsg
253 * or syslog), followed by the priority field,
254 * followed by the process name. Since we replaced
255 * stdin/stderr we simple use stdio to write to
256 * it. Note that we use stderr, to minimize buffer
257 * flushing issues. */
263 context->output == EXEC_OUTPUT_KERNEL ? "kmsg" : "syslog",
264 context->syslog_priority,
265 context->syslog_identifier ? context->syslog_identifier : ident);
271 assert_not_reached("Unknown output type");
275 static int setup_input(const ExecContext *context) {
280 switch (context->input) {
282 case EXEC_INPUT_CONSOLE:
285 case EXEC_INPUT_NULL:
286 if ((r = replace_null_fd(STDIN_FILENO, O_RDONLY)) < 0)
292 assert_not_reached("Unknown input type");
296 int exec_spawn(const ExecCommand *command, const ExecContext *context, int *fds, unsigned n_fds, pid_t *ret) {
302 assert(fds || n_fds <= 0);
304 log_debug("about to execute %s", command->path);
306 if ((pid = fork()) < 0)
310 char **e, **f = NULL;
316 if (sigemptyset(&ss) < 0 ||
317 sigprocmask(SIG_SETMASK, &ss, NULL) < 0) {
318 r = EXIT_SIGNAL_MASK;
322 if (setpgid(0, 0) < 0) {
327 umask(context->umask);
329 if (setup_input(context) < 0) {
334 if (setup_output(context, file_name_from_path(command->path)) < 0) {
339 if (context->oom_adjust_set) {
342 snprintf(t, sizeof(t), "%i", context->oom_adjust);
345 if (write_one_line_file("/proc/self/oom_adj", t) < 0) {
351 if (context->root_directory)
352 if (chroot(context->root_directory) < 0) {
357 if (chdir(context->working_directory ? context->working_directory : "/") < 0) {
362 if (context->nice_set)
363 if (setpriority(PRIO_PROCESS, 0, context->nice) < 0) {
368 if (context->cpu_sched_set) {
369 struct sched_param param;
372 param.sched_priority = context->cpu_sched_priority;
374 if (sched_setscheduler(0, context->cpu_sched_policy |
375 (context->cpu_sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0), ¶m) < 0) {
376 r = EXIT_SETSCHEDULER;
381 if (context->cpu_affinity_set)
382 if (sched_setaffinity(0, sizeof(context->cpu_affinity), &context->cpu_affinity) < 0) {
383 r = EXIT_CPUAFFINITY;
387 if (context->ioprio_set)
388 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, context->ioprio) < 0) {
393 if (context->timer_slack_ns_set)
394 if (prctl(PR_SET_TIMERSLACK, context->timer_slack_ns_set) < 0) {
399 if (close_fds(fds, n_fds) < 0 ||
400 shift_fds(fds, n_fds) < 0 ||
401 flags_fds(fds, n_fds, context->non_blocking) < 0) {
406 for (i = 0; i < RLIMIT_NLIMITS; i++) {
407 if (!context->rlimit[i])
410 if (setrlimit(i, context->rlimit[i]) < 0) {
416 if (context->secure_bits) {
417 if (prctl(PR_SET_SECUREBITS, context->secure_bits) < 0) {
425 char *listen_env[3] = {
431 snprintf(a, sizeof(a), "LISTEN_PID=%llu", (unsigned long long) getpid());
432 snprintf(b, sizeof(b), "LISTEN_FDS=%u", n_fds);
437 if (context->environment) {
438 if (!(f = strv_merge(listen_env, context->environment))) {
447 e = context->environment;
449 execve(command->path, command->argv, e);
458 log_debug("executed %s as %llu", command->path, (unsigned long long) pid);
464 void exec_context_init(ExecContext *c) {
469 c->oom_adjust_set = false;
472 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0);
473 c->ioprio_set = false;
474 c->cpu_sched_policy = SCHED_OTHER;
475 c->cpu_sched_priority = 0;
476 c->cpu_sched_set = false;
477 CPU_ZERO(&c->cpu_affinity);
478 c->cpu_affinity_set = false;
482 c->syslog_priority = LOG_DAEMON|LOG_INFO;
485 c->capability_bounding_set_drop = 0;
488 void exec_context_done(ExecContext *c) {
493 strv_free(c->environment);
494 c->environment = NULL;
496 for (l = 0; l < ELEMENTSOF(c->rlimit); l++) {
501 free(c->working_directory);
502 c->working_directory = NULL;
503 free(c->root_directory);
504 c->root_directory = NULL;
506 free(c->syslog_identifier);
507 c->syslog_identifier = NULL;
515 strv_free(c->supplementary_groups);
516 c->supplementary_groups = NULL;
518 if (c->capabilities) {
519 cap_free(c->capabilities);
520 c->capabilities = NULL;
524 void exec_command_free_list(ExecCommand *c) {
528 LIST_REMOVE(ExecCommand, command, c, i);
536 void exec_command_free_array(ExecCommand **c, unsigned n) {
539 for (i = 0; i < n; i++) {
540 exec_command_free_list(c[i]);
545 void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
557 "%sWorkingDirectory: %s\n"
558 "%sRootDirectory: %s\n"
559 "%sNonBlocking: %s\n",
561 prefix, c->working_directory ? c->working_directory : "/",
562 prefix, c->root_directory ? c->root_directory : "/",
563 prefix, yes_no(c->non_blocking));
566 for (e = c->environment; *e; e++)
567 fprintf(f, "%sEnvironment: %s\n", prefix, *e);
574 if (c->oom_adjust_set)
577 prefix, c->oom_adjust);
579 for (i = 0; i < RLIM_NLIMITS; i++)
581 fprintf(f, "%s%s: %llu\n", prefix, rlimit_to_string(i), (unsigned long long) c->rlimit[i]->rlim_max);
585 "%sIOSchedulingClass: %s\n"
586 "%sIOPriority: %i\n",
587 prefix, ioprio_class_to_string(IOPRIO_PRIO_CLASS(c->ioprio)),
588 prefix, (int) IOPRIO_PRIO_DATA(c->ioprio));
590 if (c->cpu_sched_set)
592 "%sCPUSchedulingPolicy: %s\n"
593 "%sCPUSchedulingPriority: %i\n"
594 "%sCPUSchedulingResetOnFork: %s\n",
595 prefix, sched_policy_to_string(c->cpu_sched_policy),
596 prefix, c->cpu_sched_priority,
597 prefix, yes_no(c->cpu_sched_reset_on_fork));
599 if (c->cpu_affinity_set) {
600 fprintf(f, "%sCPUAffinity:", prefix);
601 for (i = 0; i < CPU_SETSIZE; i++)
602 if (CPU_ISSET(i, &c->cpu_affinity))
603 fprintf(f, " %i", i);
607 if (c->timer_slack_ns_set)
608 fprintf(f, "%sTimerSlackNS: %lu\n", prefix, c->timer_slack_ns);
613 prefix, exec_input_to_string(c->input),
614 prefix, exec_output_to_string(c->output));
616 if (c->output == EXEC_OUTPUT_SYSLOG || c->output == EXEC_OUTPUT_KERNEL)
618 "%sSyslogFacility: %s\n"
619 "%sSyslogLevel: %s\n",
620 prefix, log_facility_to_string(LOG_FAC(c->syslog_priority)),
621 prefix, log_level_to_string(LOG_PRI(c->syslog_priority)));
623 if (c->capabilities) {
625 if ((t = cap_to_text(c->capabilities, NULL))) {
626 fprintf(f, "%sCapabilities: %s\n",
633 fprintf(f, "%sSecure Bits:%s%s%s%s%s%s\n",
635 (c->secure_bits & SECURE_KEEP_CAPS) ? " keep-caps" : "",
636 (c->secure_bits & SECURE_KEEP_CAPS_LOCKED) ? " keep-caps-locked" : "",
637 (c->secure_bits & SECURE_NO_SETUID_FIXUP) ? " no-setuid-fixup" : "",
638 (c->secure_bits & SECURE_NO_SETUID_FIXUP_LOCKED) ? " no-setuid-fixup-locked" : "",
639 (c->secure_bits & SECURE_NOROOT) ? " noroot" : "",
640 (c->secure_bits & SECURE_NOROOT_LOCKED) ? "noroot-locked" : "");
642 if (c->capability_bounding_set_drop) {
643 fprintf(f, "%sCapabilityBoundingSetDrop:", prefix);
645 for (i = 0; i <= CAP_LAST_CAP; i++)
646 if (c->capability_bounding_set_drop & (1 << i)) {
649 if ((t = cap_to_name(i))) {
650 fprintf(f, " %s", t);
659 fprintf(f, "%sUser: %s", prefix, c->user);
661 fprintf(f, "%sGroup: %s", prefix, c->group);
663 if (c->supplementary_groups) {
666 fprintf(f, "%sSupplementaryGroups:", prefix);
668 STRV_FOREACH(g, c->supplementary_groups)
669 fprintf(f, " %s", *g);
675 void exec_status_fill(ExecStatus *s, pid_t pid, int code, int status) {
681 s->timestamp = now(CLOCK_REALTIME);
684 char *exec_command_line(ExecCommand *c) {
693 STRV_FOREACH(a, c->argv)
696 if (!(n = new(char, k)))
700 STRV_FOREACH(a, c->argv) {
707 if (strpbrk(*a, WHITESPACE)) {
718 /* FIXME: this doesn't really handle arguments that have
719 * spaces and ticks in them */
724 void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) {
733 cmd = exec_command_line(c);
736 "%sCommand Line: %s\n",
737 prefix, cmd ? cmd : strerror(ENOMEM));
742 void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix) {
748 LIST_FOREACH(command, c, c)
749 exec_command_dump(c, f, prefix);
752 static const char* const exec_output_table[_EXEC_OUTPUT_MAX] = {
753 [EXEC_OUTPUT_CONSOLE] = "console",
754 [EXEC_OUTPUT_NULL] = "null",
755 [EXEC_OUTPUT_SYSLOG] = "syslog",
756 [EXEC_OUTPUT_KERNEL] = "kernel"
759 DEFINE_STRING_TABLE_LOOKUP(exec_output, ExecOutput);
761 static const char* const exec_input_table[_EXEC_INPUT_MAX] = {
762 [EXEC_INPUT_NULL] = "null",
763 [EXEC_INPUT_CONSOLE] = "console"
766 DEFINE_STRING_TABLE_LOOKUP(exec_input, ExecInput);