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>
40 #include "securebits.h"
42 static int close_fds(int except
[], unsigned n_except
) {
47 /* Modifies the fds array! (sorts it) */
49 if (!(d
= opendir("/proc/self/fd")))
52 while ((de
= readdir(d
))) {
55 if (de
->d_name
[0] == '.')
58 if ((r
= safe_atoi(de
->d_name
, &fd
)) < 0)
72 for (i
= 0; i
< n_except
; i
++)
73 if (except
[i
] == fd
) {
82 if ((r
= close_nointr(fd
)) < 0)
91 static int shift_fds(int fds
[], unsigned n_fds
) {
92 int start
, restart_from
;
105 for (i
= start
; i
< (int) n_fds
; i
++) {
108 /* Already at right index? */
112 if ((nfd
= fcntl(fds
[i
], F_DUPFD
, i
+3)) < 0)
115 assert_se(close_nointr(fds
[i
]) == 0);
118 /* Hmm, the fd we wanted isn't free? Then
119 * let's remember that and try again from here*/
120 if (nfd
!= i
+3 && restart_from
< 0)
124 if (restart_from
< 0)
127 start
= restart_from
;
133 static int flags_fds(int fds
[], unsigned n_fds
) {
141 /* Drops O_NONBLOCK and FD_CLOEXEC from the file flags */
143 for (i
= 0; i
< n_fds
; i
++) {
146 if ((flags
= fcntl(fds
[i
], F_GETFL
, 0)) < 0)
149 /* Since we are at it, let's make sure that nobody
150 * forgot setting O_NONBLOCK for all our fds */
152 if (fcntl(fds
[i
], F_SETFL
, flags
&~O_NONBLOCK
) < 0)
155 if ((flags
= fcntl(fds
[i
], F_GETFD
, 0)) < 0)
158 /* Also make sure nobody forgot O_CLOEXEC for all our
160 if (fcntl(fds
[i
], F_SETFD
, flags
&~FD_CLOEXEC
) < 0)
167 static int replace_null_fd(int fd
, int flags
) {
173 if ((nfd
= open("/dev/null", flags
|O_NOCTTY
)) < 0)
177 close_nointr_nofail(nfd
);
184 static int setup_output(const ExecContext
*context
, const char *ident
) {
189 switch (context
->output
) {
191 case EXEC_OUTPUT_CONSOLE
:
194 case EXEC_OUTPUT_NULL
:
196 if ((r
= replace_null_fd(STDOUT_FILENO
, O_WRONLY
)) < 0 ||
197 (r
= replace_null_fd(STDERR_FILENO
, O_WRONLY
)) < 0)
202 case EXEC_OUTPUT_KERNEL
:
203 case EXEC_OUTPUT_SYSLOG
: {
208 struct sockaddr_un un
;
211 close_nointr(STDOUT_FILENO
);
212 close_nointr(STDERR_FILENO
);
214 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
217 if (fd
!= STDOUT_FILENO
) {
218 close_nointr_nofail(fd
);
223 sa
.sa
.sa_family
= AF_UNIX
;
224 strncpy(sa
.un
.sun_path
+1, LOGGER_SOCKET
, sizeof(sa
.un
.sun_path
)-1);
226 if (connect(fd
, &sa
.sa
, sizeof(sa
)) < 0) {
227 close_nointr_nofail(fd
);
231 if (shutdown(fd
, SHUT_RD
) < 0) {
232 close_nointr_nofail(fd
);
236 if ((fd
= dup(fd
)) < 0) {
237 close_nointr_nofail(fd
);
241 if (fd
!= STDERR_FILENO
) {
242 close_nointr_nofail(fd
);
246 /* We speak a very simple protocol between log server
247 * and client: one line for the log destination (kmsg
248 * or syslog), followed by the priority field,
249 * followed by the process name. Since we replaced
250 * stdin/stderr we simple use stdio to write to
251 * it. Note that we use stderr, to minimize buffer
252 * flushing issues. */
258 context
->output
== EXEC_OUTPUT_KERNEL
? "kmsg" : "syslog",
259 context
->syslog_priority
,
260 context
->syslog_identifier
? context
->syslog_identifier
: ident
);
266 assert_not_reached("Unknown output type");
270 static int setup_input(const ExecContext
*context
) {
275 switch (context
->input
) {
277 case EXEC_INPUT_CONSOLE
:
280 case EXEC_INPUT_NULL
:
281 if ((r
= replace_null_fd(STDIN_FILENO
, O_RDONLY
)) < 0)
287 assert_not_reached("Unknown input type");
291 int exec_spawn(const ExecCommand
*command
, const ExecContext
*context
, int *fds
, unsigned n_fds
, pid_t
*ret
) {
297 assert(fds
|| n_fds
<= 0);
299 log_debug("about to execute %s", command
->path
);
301 if ((pid
= fork()) < 0)
305 char **e
, **f
= NULL
;
311 if (sigemptyset(&ss
) < 0 ||
312 sigprocmask(SIG_SETMASK
, &ss
, NULL
) < 0) {
313 r
= EXIT_SIGNAL_MASK
;
317 if (setpgid(0, 0) < 0) {
322 umask(context
->umask
);
324 if (setup_input(context
) < 0) {
329 if (setup_output(context
, file_name_from_path(command
->path
)) < 0) {
334 if (context
->oom_adjust_set
) {
337 snprintf(t
, sizeof(t
), "%i", context
->oom_adjust
);
340 if (write_one_line_file("/proc/self/oom_adj", t
) < 0) {
346 if (context
->root_directory
)
347 if (chroot(context
->root_directory
) < 0) {
352 if (chdir(context
->working_directory
? context
->working_directory
: "/") < 0) {
357 if (context
->nice_set
)
358 if (setpriority(PRIO_PROCESS
, 0, context
->nice
) < 0) {
363 if (context
->cpu_sched_set
) {
364 struct sched_param param
;
367 param
.sched_priority
= context
->cpu_sched_priority
;
369 if (sched_setscheduler(0, context
->cpu_sched_policy
|
370 (context
->cpu_sched_reset_on_fork
? SCHED_RESET_ON_FORK
: 0), ¶m
) < 0) {
371 r
= EXIT_SETSCHEDULER
;
376 if (context
->cpu_affinity_set
)
377 if (sched_setaffinity(0, sizeof(context
->cpu_affinity
), &context
->cpu_affinity
) < 0) {
378 r
= EXIT_CPUAFFINITY
;
382 if (context
->ioprio_set
)
383 if (ioprio_set(IOPRIO_WHO_PROCESS
, 0, context
->ioprio
) < 0) {
388 if (context
->timer_slack_ns_set
)
389 if (prctl(PR_SET_TIMERSLACK
, context
->timer_slack_ns_set
) < 0) {
394 if (close_fds(fds
, n_fds
) < 0 ||
395 shift_fds(fds
, n_fds
) < 0 ||
396 flags_fds(fds
, n_fds
) < 0) {
401 for (i
= 0; i
< RLIMIT_NLIMITS
; i
++) {
402 if (!context
->rlimit
[i
])
405 if (setrlimit(i
, context
->rlimit
[i
]) < 0) {
411 if (context
->secure_bits
) {
412 if (prctl(PR_SET_SECUREBITS
, context
->secure_bits
) < 0) {
420 char *listen_env
[3] = {
426 snprintf(a
, sizeof(a
), "LISTEN_PID=%llu", (unsigned long long) getpid());
427 snprintf(b
, sizeof(b
), "LISTEN_FDS=%u", n_fds
);
432 if (context
->environment
) {
433 if (!(f
= strv_merge(listen_env
, context
->environment
))) {
442 e
= context
->environment
;
444 execve(command
->path
, command
->argv
, e
);
453 log_debug("executed %s as %llu", command
->path
, (unsigned long long) pid
);
459 void exec_context_init(ExecContext
*c
) {
464 c
->oom_adjust_set
= false;
467 c
->ioprio
= IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE
, 0);
468 c
->ioprio_set
= false;
469 c
->cpu_sched_policy
= SCHED_OTHER
;
470 c
->cpu_sched_priority
= 0;
471 c
->cpu_sched_set
= false;
472 CPU_ZERO(&c
->cpu_affinity
);
473 c
->cpu_affinity_set
= false;
477 c
->syslog_priority
= LOG_DAEMON
|LOG_INFO
;
480 c
->capability_bounding_set_drop
= 0;
483 void exec_context_done(ExecContext
*c
) {
488 strv_free(c
->environment
);
489 c
->environment
= NULL
;
491 for (l
= 0; l
< ELEMENTSOF(c
->rlimit
); l
++) {
496 free(c
->working_directory
);
497 c
->working_directory
= NULL
;
498 free(c
->root_directory
);
499 c
->root_directory
= NULL
;
501 free(c
->syslog_identifier
);
502 c
->syslog_identifier
= NULL
;
510 strv_free(c
->supplementary_groups
);
511 c
->supplementary_groups
= NULL
;
513 if (c
->capabilities
) {
514 cap_free(c
->capabilities
);
515 c
->capabilities
= NULL
;
519 void exec_command_free_list(ExecCommand
*c
) {
523 LIST_REMOVE(ExecCommand
, command
, c
, i
);
531 void exec_command_free_array(ExecCommand
**c
, unsigned n
) {
534 for (i
= 0; i
< n
; i
++) {
535 exec_command_free_list(c
[i
]);
540 void exec_context_dump(ExecContext
*c
, FILE* f
, const char *prefix
) {
552 "%sWorkingDirectory: %s\n"
553 "%sRootDirectory: %s\n",
555 prefix
, c
->working_directory
? c
->working_directory
: "/",
556 prefix
, c
->root_directory
? c
->root_directory
: "/");
559 for (e
= c
->environment
; *e
; e
++)
560 fprintf(f
, "%sEnvironment: %s\n", prefix
, *e
);
567 if (c
->oom_adjust_set
)
570 prefix
, c
->oom_adjust
);
572 for (i
= 0; i
< RLIM_NLIMITS
; i
++)
574 fprintf(f
, "%s%s: %llu\n", prefix
, rlimit_to_string(i
), (unsigned long long) c
->rlimit
[i
]->rlim_max
);
578 "%sIOSchedulingClass: %s\n"
579 "%sIOPriority: %i\n",
580 prefix
, ioprio_class_to_string(IOPRIO_PRIO_CLASS(c
->ioprio
)),
581 prefix
, (int) IOPRIO_PRIO_DATA(c
->ioprio
));
583 if (c
->cpu_sched_set
)
585 "%sCPUSchedulingPolicy: %s\n"
586 "%sCPUSchedulingPriority: %i\n"
587 "%sCPUSchedulingResetOnFork: %s\n",
588 prefix
, sched_policy_to_string(c
->cpu_sched_policy
),
589 prefix
, c
->cpu_sched_priority
,
590 prefix
, yes_no(c
->cpu_sched_reset_on_fork
));
592 if (c
->cpu_affinity_set
) {
593 fprintf(f
, "%sCPUAffinity:", prefix
);
594 for (i
= 0; i
< CPU_SETSIZE
; i
++)
595 if (CPU_ISSET(i
, &c
->cpu_affinity
))
596 fprintf(f
, " %i", i
);
600 if (c
->timer_slack_ns_set
)
601 fprintf(f
, "%sTimerSlackNS: %lu\n", prefix
, c
->timer_slack_ns
);
606 prefix
, exec_input_to_string(c
->input
),
607 prefix
, exec_output_to_string(c
->output
));
609 if (c
->output
== EXEC_OUTPUT_SYSLOG
|| c
->output
== EXEC_OUTPUT_KERNEL
)
611 "%sSyslogFacility: %s\n"
612 "%sSyslogLevel: %s\n",
613 prefix
, log_facility_to_string(LOG_FAC(c
->syslog_priority
)),
614 prefix
, log_level_to_string(LOG_PRI(c
->syslog_priority
)));
616 if (c
->capabilities
) {
618 if ((t
= cap_to_text(c
->capabilities
, NULL
))) {
619 fprintf(f
, "%sCapabilities: %s\n",
626 fprintf(f
, "%sSecure Bits:%s%s%s%s%s%s\n",
628 (c
->secure_bits
& SECURE_KEEP_CAPS
) ? " keep-caps" : "",
629 (c
->secure_bits
& SECURE_KEEP_CAPS_LOCKED
) ? " keep-caps-locked" : "",
630 (c
->secure_bits
& SECURE_NO_SETUID_FIXUP
) ? " no-setuid-fixup" : "",
631 (c
->secure_bits
& SECURE_NO_SETUID_FIXUP_LOCKED
) ? " no-setuid-fixup-locked" : "",
632 (c
->secure_bits
& SECURE_NOROOT
) ? " noroot" : "",
633 (c
->secure_bits
& SECURE_NOROOT_LOCKED
) ? "noroot-locked" : "");
635 if (c
->capability_bounding_set_drop
) {
636 fprintf(f
, "%sCapabilityBoundingSetDrop:", prefix
);
638 for (i
= 0; i
<= CAP_LAST_CAP
; i
++)
639 if (c
->capability_bounding_set_drop
& (1 << i
)) {
642 if ((t
= cap_to_name(i
))) {
643 fprintf(f
, " %s", t
);
652 fprintf(f
, "%sUser: %s", prefix
, c
->user
);
654 fprintf(f
, "%sGroup: %s", prefix
, c
->group
);
656 if (c
->supplementary_groups
) {
659 fprintf(f
, "%sSupplementaryGroups:", prefix
);
661 STRV_FOREACH(g
, c
->supplementary_groups
)
662 fprintf(f
, " %s", *g
);
668 void exec_status_fill(ExecStatus
*s
, pid_t pid
, int code
, int status
) {
674 s
->timestamp
= now(CLOCK_REALTIME
);
677 char *exec_command_line(ExecCommand
*c
) {
686 STRV_FOREACH(a
, c
->argv
)
689 if (!(n
= new(char, k
)))
693 STRV_FOREACH(a
, c
->argv
) {
700 if (strpbrk(*a
, WHITESPACE
)) {
711 /* FIXME: this doesn't really handle arguments that have
712 * spaces and ticks in them */
717 void exec_command_dump(ExecCommand
*c
, FILE *f
, const char *prefix
) {
726 cmd
= exec_command_line(c
);
729 "%sCommand Line: %s\n",
730 prefix
, cmd
? cmd
: strerror(ENOMEM
));
735 void exec_command_dump_list(ExecCommand
*c
, FILE *f
, const char *prefix
) {
741 LIST_FOREACH(command
, c
, c
)
742 exec_command_dump(c
, f
, prefix
);
745 static const char* const exec_output_table
[_EXEC_OUTPUT_MAX
] = {
746 [EXEC_OUTPUT_CONSOLE
] = "console",
747 [EXEC_OUTPUT_NULL
] = "null",
748 [EXEC_OUTPUT_SYSLOG
] = "syslog",
749 [EXEC_OUTPUT_KERNEL
] = "kernel"
752 DEFINE_STRING_TABLE_LOOKUP(exec_output
, ExecOutput
);
754 static const char* const exec_input_table
[_EXEC_INPUT_MAX
] = {
755 [EXEC_INPUT_NULL
] = "null",
756 [EXEC_INPUT_CONSOLE
] = "console"
759 DEFINE_STRING_TABLE_LOOKUP(exec_input
, ExecInput
);