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/>.
22 #pragma GCC diagnostic ignored "-Wattributes"
31 #include <sys/socket.h>
33 #include <sys/prctl.h>
34 #include <linux/sched.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) {
143 /* Drops 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)
151 /* Since we are at it, let's make sure that nobody
152 * forgot setting O_NONBLOCK for all our fds */
154 if (fcntl(fds[i], F_SETFL, flags &~O_NONBLOCK) < 0)
157 if ((flags = fcntl(fds[i], F_GETFD, 0)) < 0)
160 /* Also make sure nobody forgot O_CLOEXEC for all our
162 if (fcntl(fds[i], F_SETFD, flags &~FD_CLOEXEC) < 0)
169 static int replace_null_fd(int fd, int flags) {
175 if ((nfd = open("/dev/null", flags|O_NOCTTY)) < 0)
179 close_nointr_nofail(nfd);
186 static int setup_output(const ExecContext *context, const char *ident) {
191 switch (context->output) {
193 case EXEC_OUTPUT_CONSOLE:
196 case EXEC_OUTPUT_NULL:
198 if ((r = replace_null_fd(STDOUT_FILENO, O_WRONLY)) < 0 ||
199 (r = replace_null_fd(STDERR_FILENO, O_WRONLY)) < 0)
204 case EXEC_OUTPUT_KERNEL:
205 case EXEC_OUTPUT_SYSLOG: {
210 struct sockaddr_un un;
213 close_nointr(STDOUT_FILENO);
214 close_nointr(STDERR_FILENO);
216 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
219 if (fd != STDOUT_FILENO) {
220 close_nointr_nofail(fd);
225 sa.sa.sa_family = AF_UNIX;
226 strncpy(sa.un.sun_path+1, LOGGER_SOCKET, sizeof(sa.un.sun_path)-1);
228 if (connect(fd, &sa.sa, sizeof(sa)) < 0) {
229 close_nointr_nofail(fd);
233 if (shutdown(fd, SHUT_RD) < 0) {
234 close_nointr_nofail(fd);
238 if ((fd = dup(fd)) < 0) {
239 close_nointr_nofail(fd);
243 if (fd != STDERR_FILENO) {
244 close_nointr_nofail(fd);
248 /* We speak a very simple protocol between log server
249 * and client: one line for the log destination (kmsg
250 * or syslog), followed by the priority field,
251 * followed by the process name. Since we replaced
252 * stdin/stderr we simple use stdio to write to
253 * it. Note that we use stderr, to minimize buffer
254 * flushing issues. */
260 context->output == EXEC_OUTPUT_KERNEL ? "kmsg" : "syslog",
261 context->syslog_priority,
262 context->syslog_identifier ? context->syslog_identifier : ident);
268 assert_not_reached("Unknown output type");
272 static int setup_input(const ExecContext *context) {
277 switch (context->input) {
279 case EXEC_INPUT_CONSOLE:
282 case EXEC_INPUT_NULL:
283 if ((r = replace_null_fd(STDIN_FILENO, O_RDONLY)) < 0)
289 assert_not_reached("Unknown input type");
293 int exec_spawn(const ExecCommand *command, const ExecContext *context, int *fds, unsigned n_fds, pid_t *ret) {
299 assert(fds || n_fds <= 0);
301 log_debug("about to execute %s", command->path);
303 if ((pid = fork()) < 0)
307 char **e, **f = NULL;
313 if (sigemptyset(&ss) < 0 ||
314 sigprocmask(SIG_SETMASK, &ss, NULL) < 0) {
315 r = EXIT_SIGNAL_MASK;
319 if (setpgid(0, 0) < 0) {
324 umask(context->umask);
326 if (setup_input(context) < 0) {
331 if (setup_output(context, file_name_from_path(command->path)) < 0) {
336 if (context->oom_adjust_set) {
339 snprintf(t, sizeof(t), "%i", context->oom_adjust);
342 if (write_one_line_file("/proc/self/oom_adj", t) < 0) {
348 if (context->root_directory)
349 if (chroot(context->root_directory) < 0) {
354 if (chdir(context->working_directory ? context->working_directory : "/") < 0) {
359 if (context->nice_set)
360 if (setpriority(PRIO_PROCESS, 0, context->nice) < 0) {
365 if (context->cpu_sched_set) {
366 struct sched_param param;
369 param.sched_priority = context->cpu_sched_priority;
371 if (sched_setscheduler(0, context->cpu_sched_policy |
372 (context->cpu_sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0), ¶m) < 0) {
373 r = EXIT_SETSCHEDULER;
378 if (context->cpu_affinity_set)
379 if (sched_setaffinity(0, sizeof(context->cpu_affinity), &context->cpu_affinity) < 0) {
380 r = EXIT_CPUAFFINITY;
384 if (context->ioprio_set)
385 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, context->ioprio) < 0) {
390 if (context->timer_slack_ns_set)
391 if (prctl(PR_SET_TIMERSLACK, context->timer_slack_ns_set) < 0) {
396 if (close_fds(fds, n_fds) < 0 ||
397 shift_fds(fds, n_fds) < 0 ||
398 flags_fds(fds, n_fds) < 0) {
403 for (i = 0; i < RLIMIT_NLIMITS; i++) {
404 if (!context->rlimit[i])
407 if (setrlimit(i, context->rlimit[i]) < 0) {
413 if (context->secure_bits) {
414 if (prctl(PR_SET_SECUREBITS, context->secure_bits) < 0) {
422 char *listen_env[3] = {
428 snprintf(a, sizeof(a), "LISTEN_PID=%llu", (unsigned long long) getpid());
429 snprintf(b, sizeof(b), "LISTEN_FDS=%u", n_fds);
434 if (context->environment) {
435 if (!(f = strv_merge(listen_env, context->environment))) {
444 e = context->environment;
446 execve(command->path, command->argv, e);
455 log_debug("executed %s as %llu", command->path, (unsigned long long) pid);
461 void exec_context_init(ExecContext *c) {
466 c->oom_adjust_set = false;
469 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0);
470 c->ioprio_set = false;
471 c->cpu_sched_policy = SCHED_OTHER;
472 c->cpu_sched_priority = 0;
473 c->cpu_sched_set = false;
474 CPU_ZERO(&c->cpu_affinity);
475 c->cpu_affinity_set = false;
479 c->syslog_priority = LOG_DAEMON|LOG_INFO;
482 c->capability_bounding_set_drop = 0;
485 void exec_context_done(ExecContext *c) {
490 strv_free(c->environment);
491 c->environment = NULL;
493 for (l = 0; l < ELEMENTSOF(c->rlimit); l++) {
498 free(c->working_directory);
499 c->working_directory = NULL;
500 free(c->root_directory);
501 c->root_directory = NULL;
503 free(c->syslog_identifier);
504 c->syslog_identifier = NULL;
512 strv_free(c->supplementary_groups);
513 c->supplementary_groups = NULL;
515 if (c->capabilities) {
516 cap_free(c->capabilities);
517 c->capabilities = NULL;
521 void exec_command_free_list(ExecCommand *c) {
525 LIST_REMOVE(ExecCommand, command, c, i);
533 void exec_command_free_array(ExecCommand **c, unsigned n) {
536 for (i = 0; i < n; i++) {
537 exec_command_free_list(c[i]);
542 void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
554 "%sWorkingDirectory: %s\n"
555 "%sRootDirectory: %s\n",
557 prefix, c->working_directory ? c->working_directory : "/",
558 prefix, c->root_directory ? c->root_directory : "/");
561 for (e = c->environment; *e; e++)
562 fprintf(f, "%sEnvironment: %s\n", prefix, *e);
569 if (c->oom_adjust_set)
572 prefix, c->oom_adjust);
574 for (i = 0; i < RLIM_NLIMITS; i++)
576 fprintf(f, "%s%s: %llu\n", prefix, rlimit_to_string(i), (unsigned long long) c->rlimit[i]->rlim_max);
580 "%sIOSchedulingClass: %s\n"
581 "%sIOPriority: %i\n",
582 prefix, ioprio_class_to_string(IOPRIO_PRIO_CLASS(c->ioprio)),
583 prefix, (int) IOPRIO_PRIO_DATA(c->ioprio));
585 if (c->cpu_sched_set)
587 "%sCPUSchedulingPolicy: %s\n"
588 "%sCPUSchedulingPriority: %i\n"
589 "%sCPUSchedulingResetOnFork: %s\n",
590 prefix, sched_policy_to_string(c->cpu_sched_policy),
591 prefix, c->cpu_sched_priority,
592 prefix, yes_no(c->cpu_sched_reset_on_fork));
594 if (c->cpu_affinity_set) {
595 fprintf(f, "%sCPUAffinity:", prefix);
596 for (i = 0; i < CPU_SETSIZE; i++)
597 if (CPU_ISSET(i, &c->cpu_affinity))
598 fprintf(f, " %i", i);
602 if (c->timer_slack_ns_set)
603 fprintf(f, "%sTimerSlackNS: %lu\n", prefix, c->timer_slack_ns);
608 prefix, exec_input_to_string(c->input),
609 prefix, exec_output_to_string(c->output));
611 if (c->output == EXEC_OUTPUT_SYSLOG || c->output == EXEC_OUTPUT_KERNEL)
613 "%sSyslogFacility: %s\n"
614 "%sSyslogLevel: %s\n",
615 prefix, log_facility_to_string(LOG_FAC(c->syslog_priority)),
616 prefix, log_level_to_string(LOG_PRI(c->syslog_priority)));
618 if (c->capabilities) {
620 if ((t = cap_to_text(c->capabilities, NULL))) {
621 fprintf(f, "%sCapabilities: %s\n",
628 fprintf(f, "%sSecure Bits:%s%s%s%s%s%s\n",
630 (c->secure_bits & SECURE_KEEP_CAPS) ? " keep-caps" : "",
631 (c->secure_bits & SECURE_KEEP_CAPS_LOCKED) ? " keep-caps-locked" : "",
632 (c->secure_bits & SECURE_NO_SETUID_FIXUP) ? " no-setuid-fixup" : "",
633 (c->secure_bits & SECURE_NO_SETUID_FIXUP_LOCKED) ? " no-setuid-fixup-locked" : "",
634 (c->secure_bits & SECURE_NOROOT) ? " noroot" : "",
635 (c->secure_bits & SECURE_NOROOT_LOCKED) ? "noroot-locked" : "");
637 if (c->capability_bounding_set_drop) {
638 fprintf(f, "%sCapabilityBoundingSetDrop:", prefix);
640 for (i = 0; i <= CAP_LAST_CAP; i++)
641 if (c->capability_bounding_set_drop & (1 << i)) {
644 if ((t = cap_to_name(i))) {
645 fprintf(f, " %s", t);
654 fprintf(f, "%sUser: %s", prefix, c->user);
656 fprintf(f, "%sGroup: %s", prefix, c->group);
658 if (c->supplementary_groups) {
661 fprintf(f, "%sSupplementaryGroups:", prefix);
663 STRV_FOREACH(g, c->supplementary_groups)
664 fprintf(f, " %s", *g);
670 void exec_status_fill(ExecStatus *s, pid_t pid, int code, int status) {
676 s->timestamp = now(CLOCK_REALTIME);
679 char *exec_command_line(ExecCommand *c) {
688 STRV_FOREACH(a, c->argv)
691 if (!(n = new(char, k)))
695 STRV_FOREACH(a, c->argv) {
702 if (strpbrk(*a, WHITESPACE)) {
713 /* FIXME: this doesn't really handle arguments that have
714 * spaces and ticks in them */
719 void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) {
728 cmd = exec_command_line(c);
731 "%sCommand Line: %s\n",
732 prefix, cmd ? cmd : strerror(ENOMEM));
737 void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix) {
743 LIST_FOREACH(command, c, c)
744 exec_command_dump(c, f, prefix);
747 static const char* const exec_output_table[_EXEC_OUTPUT_MAX] = {
748 [EXEC_OUTPUT_CONSOLE] = "console",
749 [EXEC_OUTPUT_NULL] = "null",
750 [EXEC_OUTPUT_SYSLOG] = "syslog",
751 [EXEC_OUTPUT_KERNEL] = "kernel"
754 DEFINE_STRING_TABLE_LOOKUP(exec_output, ExecOutput);
756 static const char* const exec_input_table[_EXEC_INPUT_MAX] = {
757 [EXEC_INPUT_NULL] = "null",
758 [EXEC_INPUT_CONSOLE] = "console"
761 DEFINE_STRING_TABLE_LOOKUP(exec_input, ExecInput);