]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/execute.h
Merge pull request #30550 from yuwata/network-nexthop-cleanups-3
[thirdparty/systemd.git] / src / core / execute.h
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
c2f1db8f 2#pragma once
5cb5a6ff
LP
3
4typedef struct ExecStatus ExecStatus;
5typedef struct ExecCommand ExecCommand;
6typedef struct ExecContext ExecContext;
e76506b7 7typedef struct ExecSharedRuntime ExecSharedRuntime;
15220772 8typedef struct DynamicCreds DynamicCreds;
28135da3 9typedef struct ExecRuntime ExecRuntime;
9fa95f85 10typedef struct ExecParameters ExecParameters;
e8a565cb 11typedef struct Manager Manager;
5cb5a6ff 12
71d35b6b 13#include <sched.h>
5cb5a6ff
LP
14#include <stdbool.h>
15#include <stdio.h>
71d35b6b 16#include <sys/capability.h>
5cb5a6ff 17
9ce93478 18#include "cgroup-util.h"
ad21e542 19#include "coredump-util.h"
0985c7c4 20#include "cpu-set-util.h"
b3d59367 21#include "exec-util.h"
613b411c 22#include "fdset.h"
71d35b6b 23#include "list.h"
e93672ee 24#include "missing_resource.h"
417116f2 25#include "namespace.h"
add00535 26#include "nsflags.h"
1808f768 27#include "numa-util.h"
cd48e23f 28#include "open-file.h"
74e12520 29#include "path-util.h"
170d978b 30#include "runtime-scope.h"
523ea123 31#include "set.h"
ca78ad1d 32#include "time-util.h"
5cb5a6ff 33
08f3be7a
LP
34#define EXEC_STDIN_DATA_MAX (64U*1024U*1024U)
35
023a4f67
LP
36typedef enum ExecUtmpMode {
37 EXEC_UTMP_INIT,
38 EXEC_UTMP_LOGIN,
39 EXEC_UTMP_USER,
40 _EXEC_UTMP_MODE_MAX,
2d93c20e 41 _EXEC_UTMP_MODE_INVALID = -EINVAL,
023a4f67
LP
42} ExecUtmpMode;
43
80876c20
LP
44typedef enum ExecInput {
45 EXEC_INPUT_NULL,
46 EXEC_INPUT_TTY,
47 EXEC_INPUT_TTY_FORCE,
48 EXEC_INPUT_TTY_FAIL,
4f2d528d 49 EXEC_INPUT_SOCKET,
52c239d7 50 EXEC_INPUT_NAMED_FD,
08f3be7a 51 EXEC_INPUT_DATA,
2038c3f5 52 EXEC_INPUT_FILE,
80876c20 53 _EXEC_INPUT_MAX,
2d93c20e 54 _EXEC_INPUT_INVALID = -EINVAL,
80876c20
LP
55} ExecInput;
56
071830ff 57typedef enum ExecOutput {
80876c20 58 EXEC_OUTPUT_INHERIT,
94f04347 59 EXEC_OUTPUT_NULL,
80876c20 60 EXEC_OUTPUT_TTY,
9a6bca7a 61 EXEC_OUTPUT_KMSG,
28dbc1e8 62 EXEC_OUTPUT_KMSG_AND_CONSOLE,
706343f4
LP
63 EXEC_OUTPUT_JOURNAL,
64 EXEC_OUTPUT_JOURNAL_AND_CONSOLE,
4f2d528d 65 EXEC_OUTPUT_SOCKET,
52c239d7 66 EXEC_OUTPUT_NAMED_FD,
2038c3f5 67 EXEC_OUTPUT_FILE,
566b7d23 68 EXEC_OUTPUT_FILE_APPEND,
8d7dab1f 69 EXEC_OUTPUT_FILE_TRUNCATE,
94f04347 70 _EXEC_OUTPUT_MAX,
2d93c20e 71 _EXEC_OUTPUT_INVALID = -EINVAL,
071830ff
LP
72} ExecOutput;
73
53f47dfc
YW
74typedef enum ExecPreserveMode {
75 EXEC_PRESERVE_NO,
76 EXEC_PRESERVE_YES,
77 EXEC_PRESERVE_RESTART,
78 _EXEC_PRESERVE_MODE_MAX,
2d93c20e 79 _EXEC_PRESERVE_MODE_INVALID = -EINVAL,
53f47dfc
YW
80} ExecPreserveMode;
81
b1edf445
LP
82typedef enum ExecKeyringMode {
83 EXEC_KEYRING_INHERIT,
84 EXEC_KEYRING_PRIVATE,
85 EXEC_KEYRING_SHARED,
86 _EXEC_KEYRING_MODE_MAX,
2d93c20e 87 _EXEC_KEYRING_MODE_INVALID = -EINVAL,
b1edf445
LP
88} ExecKeyringMode;
89
42cb05d5 90/* Contains start and exit information about an executed command. */
5cb5a6ff 91struct ExecStatus {
63983207
LP
92 dual_timestamp start_timestamp;
93 dual_timestamp exit_timestamp;
0a6991e0 94 pid_t pid;
9152c765 95 int code; /* as in siginfo_t::si_code */
fcb7138c 96 int status; /* as in siginfo_t::si_status */
5cb5a6ff
LP
97};
98
42cb05d5 99/* Stores information about commands we execute. Covers both configuration settings as well as runtime data. */
5cb5a6ff
LP
100struct ExecCommand {
101 char *path;
102 char **argv;
b806a5d3 103 ExecStatus exec_status; /* Note that this is not serialized to sd-executor */
3ed0cd26 104 ExecCommandFlags flags;
034c6ed7 105 LIST_FIELDS(ExecCommand, command); /* useful for chaining commands */
5cb5a6ff
LP
106};
107
42cb05d5
LP
108/* Encapsulates certain aspects of the runtime environment that is to be shared between multiple otherwise separate
109 * invocations of commands. Specifically, this allows sharing of /tmp and /var/tmp data as well as network namespaces
110 * between invocations of commands. This is a reference counted object, with one reference taken by each currently
111 * active command invocation that wants to share this runtime. */
e76506b7 112struct ExecSharedRuntime {
cf4b2f99 113 unsigned n_ref;
613b411c 114
e8a565cb
YW
115 Manager *manager;
116
42cb05d5 117 char *id; /* Unit id of the owner */
e8a565cb 118
613b411c
LP
119 char *tmp_dir;
120 char *var_tmp_dir;
121
29206d46
LP
122 /* An AF_UNIX socket pair, that contains a datagram containing a file descriptor referring to the network
123 * namespace. */
613b411c 124 int netns_storage_socket[2];
a70581ff
XR
125
126 /* Like netns_storage_socket, but the file descriptor is referring to the IPC namespace. */
127 int ipcns_storage_socket[2];
613b411c
LP
128};
129
28135da3
DDM
130struct ExecRuntime {
131 ExecSharedRuntime *shared;
15220772 132 DynamicCreds *dynamic_creds;
9c0c6701
DDM
133
134 /* The path to the ephemeral snapshot of the root directory or root image if one was requested. */
135 char *ephemeral_copy;
136
137 /* An AF_UNIX socket pair that receives the locked file descriptor referring to the ephemeral copy of
138 * the root directory or root image. The lock prevents tmpfiles from removing the ephemeral snapshot
139 * until we're done using it. */
140 int ephemeral_storage_socket[2];
28135da3
DDM
141};
142
3536f49e 143typedef enum ExecDirectoryType {
d9e51371 144 EXEC_DIRECTORY_RUNTIME,
3536f49e
YW
145 EXEC_DIRECTORY_STATE,
146 EXEC_DIRECTORY_CACHE,
147 EXEC_DIRECTORY_LOGS,
148 EXEC_DIRECTORY_CONFIGURATION,
72fd1768 149 _EXEC_DIRECTORY_TYPE_MAX,
2d93c20e 150 _EXEC_DIRECTORY_TYPE_INVALID = -EINVAL,
3536f49e
YW
151} ExecDirectoryType;
152
211a3d87
LB
153typedef struct ExecDirectoryItem {
154 char *path;
155 char **symlinks;
a2ab603c 156 bool only_create;
211a3d87
LB
157} ExecDirectoryItem;
158
3536f49e 159typedef struct ExecDirectory {
3536f49e 160 mode_t mode;
211a3d87
LB
161 size_t n_items;
162 ExecDirectoryItem *items;
3536f49e
YW
163} ExecDirectory;
164
380dc8b0
LP
165typedef enum ExecCleanMask {
166 /* In case you wonder why the bitmask below doesn't use "directory" in its name: we want to keep this
167 * generic so that .timer timestamp files can nicely be covered by this too, and similar. */
168 EXEC_CLEAN_RUNTIME = 1U << EXEC_DIRECTORY_RUNTIME,
169 EXEC_CLEAN_STATE = 1U << EXEC_DIRECTORY_STATE,
170 EXEC_CLEAN_CACHE = 1U << EXEC_DIRECTORY_CACHE,
171 EXEC_CLEAN_LOGS = 1U << EXEC_DIRECTORY_LOGS,
172 EXEC_CLEAN_CONFIGURATION = 1U << EXEC_DIRECTORY_CONFIGURATION,
4fb8f1e8 173 EXEC_CLEAN_FDSTORE = 1U << _EXEC_DIRECTORY_TYPE_MAX,
380dc8b0 174 EXEC_CLEAN_NONE = 0,
4fb8f1e8 175 EXEC_CLEAN_ALL = (1U << (_EXEC_DIRECTORY_TYPE_MAX+1)) - 1,
2d93c20e 176 _EXEC_CLEAN_MASK_INVALID = -EINVAL,
380dc8b0
LP
177} ExecCleanMask;
178
42cb05d5
LP
179/* Encodes configuration parameters applied to invoked commands. Does not carry runtime data, but only configuration
180 * changes sourced from unit files and suchlike. ExecContext objects are usually embedded into Unit objects, and do not
181 * change after being loaded. */
5cb5a6ff
LP
182struct ExecContext {
183 char **environment;
8c7be95e 184 char **environment_files;
b4c14404 185 char **pass_environment;
00819cc1 186 char **unset_environment;
8c7be95e 187
517d56b1 188 struct rlimit *rlimit[_RLIMIT_MAX];
d4d55b0d
LB
189 char *working_directory, *root_directory, *root_image, *root_verity, *root_hash_path, *root_hash_sig_path;
190 void *root_hash, *root_hash_sig;
191 size_t root_hash_size, root_hash_sig_size;
18d73705 192 LIST_HEAD(MountOptions, root_image_options);
9c0c6701 193 bool root_ephemeral;
0a6991e0
LP
194 bool working_directory_missing_ok:1;
195 bool working_directory_home:1;
196
197 bool oom_score_adjust_set:1;
ad21e542 198 bool coredump_filter_set:1;
0a6991e0
LP
199 bool nice_set:1;
200 bool ioprio_set:1;
201 bool cpu_sched_set:1;
5e98086d 202 bool mount_apivfs_set:1;
0a6991e0
LP
203
204 /* This is not exposed to the user but available internally. We need it to make sure that whenever we
205 * spawn /usr/bin/mount it is run in the same process group as us so that the autofs logic detects
206 * that it belongs to us and we don't enter a trigger loop. */
207 bool same_pgrp;
208
209 bool cpu_sched_reset_on_fork;
210 bool non_blocking;
9d58f1db
LP
211
212 mode_t umask;
dd6c17b1 213 int oom_score_adjust;
5cb5a6ff 214 int nice;
9eba9da4 215 int ioprio;
94f04347
LP
216 int cpu_sched_policy;
217 int cpu_sched_priority;
ad21e542 218 uint64_t coredump_filter;
9d58f1db 219
0985c7c4 220 CPUSet cpu_set;
b070c7c0 221 NUMAPolicy numa_policy;
e2b2fb7f 222 bool cpu_affinity_from_numa;
fb33a393 223
80876c20
LP
224 ExecInput std_input;
225 ExecOutput std_output;
226 ExecOutput std_error;
561c1529
LP
227
228 /* At least one of stdin/stdout/stderr was initialized from an fd passed in. This boolean survives
229 * the fds being closed. This only makes sense for transient units. */
0a6991e0 230 bool stdio_as_fds;
561c1529 231
52c239d7 232 char *stdio_fdname[3];
2038c3f5 233 char *stdio_file[3];
80876c20 234
08f3be7a
LP
235 void *stdin_data;
236 size_t stdin_data_size;
80876c20 237
d88a251b 238 nsec_t timer_slack_nsec;
071830ff 239
9d58f1db 240 char *tty_path;
5cb5a6ff 241
6ea832a2
LP
242 bool tty_reset;
243 bool tty_vhangup;
244 bool tty_vt_disallocate;
245
51462135
DDM
246 unsigned tty_rows;
247 unsigned tty_cols;
248
353e12c2
LP
249 bool ignore_sigpipe;
250
0a6991e0
LP
251 ExecKeyringMode keyring_mode;
252
61233823 253 /* Since resolving these names might involve socket
5cb5a6ff 254 * connections and we don't want to deadlock ourselves these
94f04347
LP
255 * names are resolved on execution only and in the child
256 * process. */
5cb5a6ff
LP
257 char *user;
258 char *group;
259 char **supplementary_groups;
9d58f1db 260
854eca4a
MY
261 int set_login_environment;
262
5b6319dc
LP
263 char *pam_name;
264
169c1bda 265 char *utmp_id;
023a4f67 266 ExecUtmpMode utmp_mode;
169c1bda 267
0a6991e0 268 bool no_new_privileges;
7b52a628 269
0a6991e0 270 bool selinux_context_ignore;
eef65bf3 271 bool apparmor_profile_ignore;
2ca620c4 272 bool smack_process_label_ignore;
2ca620c4 273
0a6991e0
LP
274 char *selinux_context;
275 char *apparmor_profile;
276 char *smack_process_label;
b1edf445 277
ddc155b2 278 char **read_write_paths, **read_only_paths, **inaccessible_paths, **exec_paths, **no_exec_paths;
8c35c10d 279 char **exec_search_path;
874cdcbc 280 unsigned long mount_propagation_flag;
d2d6c096 281 BindMount *bind_mounts;
da6053d0 282 size_t n_bind_mounts;
2abd4e38 283 TemporaryFileSystem *temporary_filesystems;
da6053d0 284 size_t n_temporary_filesystems;
b3d13314
LB
285 MountImage *mount_images;
286 size_t n_mount_images;
93f59701
LB
287 MountImage *extension_images;
288 size_t n_extension_images;
a07b9926 289 char **extension_directories;
15ae422b 290
a103496c 291 uint64_t capability_bounding_set;
755d4b67 292 uint64_t capability_ambient_set;
9d58f1db
LP
293 int secure_bits;
294
7fab9d01 295 int syslog_priority;
7fab9d01 296 bool syslog_level_prefix;
0a6991e0 297 char *syslog_identifier;
d3070fbd
LP
298
299 struct iovec* log_extra_fields;
300 size_t n_log_extra_fields;
523ea123
QD
301 Set *log_filter_allowed_patterns;
302 Set *log_filter_denied_patterns;
d3070fbd 303
5ac1530e
ZJS
304 usec_t log_ratelimit_interval_usec;
305 unsigned log_ratelimit_burst;
90fc172e 306
0a6991e0
LP
307 int log_level_max;
308
91dd5f7c
LP
309 char *log_namespace;
310
4e399953
LP
311 ProtectProc protect_proc; /* hidepid= */
312 ProcSubset proc_subset; /* subset= */
313
24002121 314 int private_mounts;
85614c6e 315 int memory_ksm;
15ae422b 316 bool private_tmp;
ff01d048 317 bool private_network;
7f112f50 318 bool private_devices;
d251207d 319 bool private_users;
a70581ff 320 bool private_ipc;
59eeb84b 321 bool protect_kernel_tunables;
502d704e 322 bool protect_kernel_modules;
84703040 323 bool protect_kernel_logs;
fc64760d 324 bool protect_clock;
59eeb84b 325 bool protect_control_groups;
0a6991e0
LP
326 ProtectSystem protect_system;
327 ProtectHome protect_home;
328 bool protect_hostname;
5d997827 329 bool mount_apivfs;
9d58f1db 330
29206d46 331 bool dynamic_user;
00d9ef85 332 bool remove_ipc;
29206d46 333
0a6991e0
LP
334 bool memory_deny_write_execute;
335 bool restrict_realtime;
f69567cb 336 bool restrict_suid_sgid;
2e22afe9 337
78e864e5 338 bool lock_personality;
0a6991e0 339 unsigned long personality;
ac45f971 340
add00535
LP
341 unsigned long restrict_namespaces; /* The CLONE_NEWxyz flags permitted to the unit's processes */
342
b1994387
ILG
343 Set *restrict_filesystems;
344 bool restrict_filesystems_allow_list:1;
345
8cfa775f 346 Hashmap *syscall_filter;
57183d11 347 Set *syscall_archs;
17df7223 348 int syscall_errno;
6b000af4 349 bool syscall_allow_list:1;
8351ceae 350
9df2cdd8
TM
351 Hashmap *syscall_log;
352 bool syscall_log_allow_list:1; /* Log listed system calls */
353
6b000af4 354 bool address_families_allow_list:1;
0a6991e0 355 Set *address_families;
a8d08f39
LP
356
357 char *network_namespace_path;
a70581ff 358 char *ipc_namespace_path;
0a6991e0
LP
359
360 ExecDirectory directories[_EXEC_DIRECTORY_TYPE_MAX];
361 ExecPreserveMode runtime_directory_preserve_mode;
12213aed 362 usec_t timeout_clean_usec;
bb0c0d6f
LP
363
364 Hashmap *set_credentials; /* output id → ExecSetCredential */
43144be4 365 Hashmap *load_credentials; /* output id → ExecLoadCredential */
bbfb25f4 366 Set *import_credentials;
84be0c71
LP
367
368 ImagePolicy *root_image_policy, *mount_image_policy, *extension_image_policy;
5cb5a6ff
LP
369};
370
add00535
LP
371static inline bool exec_context_restrict_namespaces_set(const ExecContext *c) {
372 assert(c);
373
374 return (c->restrict_namespaces & NAMESPACE_FLAGS_ALL) != NAMESPACE_FLAGS_ALL;
375}
376
b1994387
ILG
377static inline bool exec_context_restrict_filesystems_set(const ExecContext *c) {
378 assert(c);
379
380 return c->restrict_filesystems_allow_list ||
381 !set_isempty(c->restrict_filesystems);
382}
383
74e12520
LP
384static inline bool exec_context_with_rootfs(const ExecContext *c) {
385 assert(c);
386
387 /* Checks if RootDirectory= or RootImage= are used */
388
389 return !empty_or_root(c->root_directory) || c->root_image;
390}
391
c39f1ce2 392typedef enum ExecFlags {
cdebedb4
PM
393 EXEC_APPLY_SANDBOXING = 1 << 0,
394 EXEC_APPLY_CHROOT = 1 << 1,
395 EXEC_APPLY_TTY_STDIN = 1 << 2,
396 EXEC_PASS_LOG_UNIT = 1 << 3, /* Whether to pass the unit name to the service's journal stream connection */
397 EXEC_CHOWN_DIRECTORIES = 1 << 4, /* chown() the runtime/state/cache/log directories to the user we run as, under all conditions */
de90700f 398 EXEC_NSS_DYNAMIC_BYPASS = 1 << 5, /* Set the SYSTEMD_NSS_DYNAMIC_BYPASS environment variable, to disable nss-systemd blocking on PID 1, for use by dbus-daemon */
cdebedb4
PM
399 EXEC_CGROUP_DELEGATE = 1 << 6,
400 EXEC_IS_CONTROL = 1 << 7,
401 EXEC_CONTROL_CGROUP = 1 << 8, /* Place the process not in the indicated cgroup but in a subcgroup '/.control', but only EXEC_CGROUP_DELEGATE and EXEC_IS_CONTROL is set, too */
402 EXEC_WRITE_CREDENTIALS = 1 << 9, /* Set up the credential store logic */
c39f1ce2 403
9c1a61ad 404 /* The following are not used by execute.c, but by consumers internally */
cdebedb4
PM
405 EXEC_PASS_FDS = 1 << 10,
406 EXEC_SETENV_RESULT = 1 << 11,
407 EXEC_SET_WATCHDOG = 1 << 12,
408 EXEC_SETENV_MONITOR_RESULT = 1 << 13, /* Pass exit status to OnFailure= and OnSuccess= dependencies. */
c39f1ce2
LP
409} ExecFlags;
410
42cb05d5
LP
411/* Parameters for a specific invocation of a command. This structure is put together right before a command is
412 * executed. */
9fa95f85 413struct ExecParameters {
170d978b
LP
414 RuntimeScope runtime_scope;
415
a34ceba6 416 char **environment;
8dd4c05b
LP
417
418 int *fds;
419 char **fd_names;
da6053d0 420 size_t n_socket_fds;
25b583d7 421 size_t n_storage_fds;
8dd4c05b 422
c39f1ce2 423 ExecFlags flags;
a34ceba6 424 bool selinux_context_net:1;
8dd4c05b 425
efdb0237 426 CGroupMask cgroup_supported;
beb4ae87 427 char *cgroup_path;
b646fc32 428 uint64_t cgroup_id;
8dd4c05b 429
3536f49e 430 char **prefix;
beb4ae87
LB
431 char *received_credentials_directory;
432 char *received_encrypted_credentials_directory;
8dd4c05b 433
beb4ae87 434 char *confirm_spawn;
154eb43f 435 bool shall_confirm_spawn;
7d5ceb64 436
9fa95f85 437 usec_t watchdog_usec;
8dd4c05b 438
9fa95f85 439 int *idle_pipe;
8dd4c05b 440
a34ceba6
LP
441 int stdin_fd;
442 int stdout_fd;
443 int stderr_fd;
5686391b
LP
444
445 /* An fd that is closed by the execve(), and thus will result in EOF when the execve() is done */
446 int exec_fd;
3bdc25a4 447
beb4ae87 448 char *notify_socket;
cd48e23f
RP
449
450 LIST_HEAD(OpenFile, open_files);
154eb43f 451
beb4ae87 452 char *fallback_smack_process_label;
154eb43f
LB
453
454 char **files_env;
455 int user_lookup_fd;
456 int bpf_outer_map_fd;
b646fc32
LB
457
458 /* Used for logging in the executor functions */
459 char *unit_id;
460 sd_id128_t invocation_id;
461 char invocation_id_string[SD_ID128_STRING_MAX];
9fa95f85
DM
462};
463
b646fc32
LB
464#define EXEC_PARAMETERS_INIT(_flags) \
465 (ExecParameters) { \
466 .flags = (_flags), \
467 .stdin_fd = -EBADF, \
468 .stdout_fd = -EBADF, \
469 .stderr_fd = -EBADF, \
470 .exec_fd = -EBADF, \
471 .bpf_outer_map_fd = -EBADF, \
472 .user_lookup_fd = -EBADF, \
473 };
474
9ce93478 475#include "unit.h"
29206d46 476#include "dynamic-user.h"
9ce93478 477
f2341e0a
LP
478int exec_spawn(Unit *unit,
479 ExecCommand *command,
9fa95f85 480 const ExecContext *context,
154eb43f 481 ExecParameters *exec_params,
28135da3 482 ExecRuntime *runtime,
6bb00842 483 const CGroupContext *cgroup_context,
81a2b7ce 484 pid_t *ret);
5cb5a6ff 485
bb5232b6 486void exec_command_done(ExecCommand *c);
da6053d0 487void exec_command_done_array(ExecCommand *c, size_t n);
f1acf85a 488ExecCommand* exec_command_free_list(ExecCommand *c);
da6053d0 489void exec_command_free_array(ExecCommand **c, size_t n);
6a1d4d9f
LP
490void exec_command_reset_status_array(ExecCommand *c, size_t n);
491void exec_command_reset_status_list_array(ExecCommand **c, size_t n);
44d8db9e 492void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix);
a6a80b4f 493void exec_command_append_list(ExecCommand **l, ExecCommand *e);
7593c3ec
LP
494int exec_command_set(ExecCommand *c, const char *path, ...) _sentinel_;
495int exec_command_append(ExecCommand *c, const char *path, ...) _sentinel_;
44d8db9e 496
034c6ed7 497void exec_context_init(ExecContext *c);
613b411c 498void exec_context_done(ExecContext *c);
34cf6c43 499void exec_context_dump(const ExecContext *c, FILE* f, const char *prefix);
5cb5a6ff 500
34cf6c43 501int exec_context_destroy_runtime_directory(const ExecContext *c, const char *runtime_root);
b9f976fb 502int exec_context_destroy_mount_ns_dir(Unit *u);
e66cf1a3 503
52c239d7 504const char* exec_context_fdname(const ExecContext *c, int fd_index);
8c7be95e 505
34cf6c43
YW
506bool exec_context_may_touch_console(const ExecContext *c);
507bool exec_context_maintains_privileges(const ExecContext *c);
6ac8fdc9 508
34cf6c43 509int exec_context_get_effective_ioprio(const ExecContext *c);
5e98086d 510bool exec_context_get_effective_mount_apivfs(const ExecContext *c);
7f452159 511
d3070fbd
LP
512void exec_context_free_log_extra_fields(ExecContext *c);
513
6f765baf
LP
514void exec_context_revert_tty(ExecContext *c);
515
4c2f5842
LP
516int exec_context_get_clean_directories(ExecContext *c, char **prefix, ExecCleanMask mask, char ***ret);
517int exec_context_get_clean_mask(ExecContext *c, ExecCleanMask *ret);
518
75689fb2 519const char *exec_context_tty_path(const ExecContext *context);
d2b9e755 520int exec_context_apply_tty_size(const ExecContext *context, int tty_fd, const char *tty_path);
75689fb2
LB
521void exec_context_tty_reset(const ExecContext *context, const ExecParameters *p);
522
ef44aa83
DDM
523uint64_t exec_context_get_rlimit(const ExecContext *c, const char *name);
524int exec_context_get_oom_score_adjust(const ExecContext *c);
525uint64_t exec_context_get_coredump_filter(const ExecContext *c);
526int exec_context_get_nice(const ExecContext *c);
527int exec_context_get_cpu_sched_policy(const ExecContext *c);
528int exec_context_get_cpu_sched_priority(const ExecContext *c);
529uint64_t exec_context_get_timer_slack_nsec(const ExecContext *c);
d1a5be82 530bool exec_context_get_set_login_environment(const ExecContext *c);
ef44aa83
DDM
531char** exec_context_get_syscall_filter(const ExecContext *c);
532char** exec_context_get_syscall_archs(const ExecContext *c);
533char** exec_context_get_syscall_log(const ExecContext *c);
534char** exec_context_get_address_families(const ExecContext *c);
535char** exec_context_get_restrict_filesystems(const ExecContext *c);
536
b58b4116 537void exec_status_start(ExecStatus *s, pid_t pid);
34cf6c43
YW
538void exec_status_exit(ExecStatus *s, const ExecContext *context, pid_t pid, int code, int status);
539void exec_status_dump(const ExecStatus *s, FILE *f, const char *prefix);
6a1d4d9f 540void exec_status_reset(ExecStatus *s);
5cb5a6ff 541
e76506b7 542int exec_shared_runtime_acquire(Manager *m, const ExecContext *c, const char *name, bool create, ExecSharedRuntime **ret);
e52a696a
DDM
543ExecSharedRuntime *exec_shared_runtime_destroy(ExecSharedRuntime *r);
544ExecSharedRuntime *exec_shared_runtime_unref(ExecSharedRuntime *r);
28135da3 545DEFINE_TRIVIAL_CLEANUP_FUNC(ExecSharedRuntime*, exec_shared_runtime_unref);
613b411c 546
e76506b7
DDM
547int exec_shared_runtime_serialize(const Manager *m, FILE *f, FDSet *fds);
548int exec_shared_runtime_deserialize_compat(Unit *u, const char *key, const char *value, FDSet *fds);
549int exec_shared_runtime_deserialize_one(Manager *m, const char *value, FDSet *fds);
bb5232b6 550void exec_shared_runtime_done(ExecSharedRuntime *rt);
e76506b7 551void exec_shared_runtime_vacuum(Manager *m);
613b411c 552
9c0c6701 553int exec_runtime_make(const Unit *unit, const ExecContext *context, ExecSharedRuntime *shared, DynamicCreds *creds, ExecRuntime **ret);
28135da3
DDM
554ExecRuntime* exec_runtime_free(ExecRuntime *rt);
555DEFINE_TRIVIAL_CLEANUP_FUNC(ExecRuntime*, exec_runtime_free);
556ExecRuntime* exec_runtime_destroy(ExecRuntime *rt);
bb5232b6 557void exec_runtime_clear(ExecRuntime *rt);
28135da3 558
75689fb2 559int exec_params_get_cgroup_path(const ExecParameters *params, const CGroupContext *c, char **ret);
fba173ff 560void exec_params_shallow_clear(ExecParameters *p);
97f53fec 561void exec_params_dump(const ExecParameters *p, FILE* f, const char *prefix);
fba173ff 562void exec_params_deep_clear(ExecParameters *p);
b9c04eaf 563
e2b2fb7f
MS
564bool exec_context_get_cpu_affinity_from_numa(const ExecContext *c);
565
211a3d87 566void exec_directory_done(ExecDirectory *d);
564e5c98 567int exec_directory_add(ExecDirectory *d, const char *path, const char *symlink);
a2ab603c 568void exec_directory_sort(ExecDirectory *d);
75689fb2 569bool exec_directory_is_private(const ExecContext *context, ExecDirectoryType type);
211a3d87 570
4fb8f1e8
LP
571ExecCleanMask exec_clean_mask_from_string(const char *s);
572
44a6b1b6
ZJS
573const char* exec_output_to_string(ExecOutput i) _const_;
574ExecOutput exec_output_from_string(const char *s) _pure_;
94f04347 575
44a6b1b6
ZJS
576const char* exec_input_to_string(ExecInput i) _const_;
577ExecInput exec_input_from_string(const char *s) _pure_;
023a4f67
LP
578
579const char* exec_utmp_mode_to_string(ExecUtmpMode i) _const_;
580ExecUtmpMode exec_utmp_mode_from_string(const char *s) _pure_;
53f47dfc
YW
581
582const char* exec_preserve_mode_to_string(ExecPreserveMode i) _const_;
583ExecPreserveMode exec_preserve_mode_from_string(const char *s) _pure_;
3536f49e 584
b1edf445
LP
585const char* exec_keyring_mode_to_string(ExecKeyringMode i) _const_;
586ExecKeyringMode exec_keyring_mode_from_string(const char *s) _pure_;
587
3536f49e
YW
588const char* exec_directory_type_to_string(ExecDirectoryType i) _const_;
589ExecDirectoryType exec_directory_type_from_string(const char *s) _pure_;
6b7b2ed9 590
211a3d87
LB
591const char* exec_directory_type_symlink_to_string(ExecDirectoryType i) _const_;
592ExecDirectoryType exec_directory_type_symlink_from_string(const char *s) _pure_;
593
ef44aa83
DDM
594const char* exec_directory_type_mode_to_string(ExecDirectoryType i) _const_;
595ExecDirectoryType exec_directory_type_mode_from_string(const char *s) _pure_;
596
6b7b2ed9
LP
597const char* exec_resource_type_to_string(ExecDirectoryType i) _const_;
598ExecDirectoryType exec_resource_type_from_string(const char *s) _pure_;
5e8deb94 599
28135da3 600bool exec_needs_mount_namespace(const ExecContext *context, const ExecParameters *params, const ExecRuntime *runtime);
fbbb9697 601bool exec_needs_network_namespace(const ExecContext *context);
75689fb2 602bool exec_needs_ipc_namespace(const ExecContext *context);
b646fc32
LB
603
604/* These logging macros do the same logging as those in unit.h, but using ExecContext and ExecParameters
605 * instead of the unit object, so that it can be used in the sd-executor context (where the unit object is
606 * not available). */
607
608#define LOG_EXEC_ID_FIELD(ep) \
609 ((ep)->runtime_scope == RUNTIME_SCOPE_USER ? "USER_UNIT=" : "UNIT=")
610#define LOG_EXEC_ID_FIELD_FORMAT(ep) \
611 ((ep)->runtime_scope == RUNTIME_SCOPE_USER ? "USER_UNIT=%s" : "UNIT=%s")
612#define LOG_EXEC_INVOCATION_ID_FIELD(ep) \
613 ((ep)->runtime_scope == RUNTIME_SCOPE_USER ? "USER_INVOCATION_ID=" : "INVOCATION_ID=")
614#define LOG_EXEC_INVOCATION_ID_FIELD_FORMAT(ep) \
615 ((ep)->runtime_scope == RUNTIME_SCOPE_USER ? "USER_INVOCATION_ID=%s" : "INVOCATION_ID=%s")
616
617#define log_exec_full_errno_zerook(ec, ep, level, error, ...) \
618 ({ \
619 const ExecContext *_c = (ec); \
620 const ExecParameters *_p = (ep); \
621 const int _l = (level); \
622 bool _do_log = !(log_get_max_level() < LOG_PRI(_l) || \
623 !(_c->log_level_max < 0 || \
624 _c->log_level_max >= LOG_PRI(_l))); \
625 LOG_CONTEXT_PUSH_IOV(_c->log_extra_fields, \
626 _c->n_log_extra_fields); \
627 !_do_log ? -ERRNO_VALUE(error) : \
628 log_object_internal(_l, error, PROJECT_FILE, \
629 __LINE__, __func__, \
630 LOG_EXEC_ID_FIELD(_p), \
631 _p->unit_id, \
632 LOG_EXEC_INVOCATION_ID_FIELD(_p), \
633 _p->invocation_id_string, ##__VA_ARGS__); \
634 })
635
636#define log_exec_full_errno(ec, ep, level, error, ...) \
637 ({ \
638 int _error = (error); \
639 ASSERT_NON_ZERO(_error); \
640 log_exec_full_errno_zerook(ec, ep, level, _error, ##__VA_ARGS__); \
641 })
642
643#define log_exec_full(ec, ep, level, ...) (void) log_exec_full_errno_zerook(ec, ep, level, 0, __VA_ARGS__)
644
645#define log_exec_debug(ec, ep, ...) log_exec_full(ec, ep, LOG_DEBUG, __VA_ARGS__)
646#define log_exec_info(ec, ep, ...) log_exec_full(ec, ep, LOG_INFO, __VA_ARGS__)
647#define log_exec_notice(ec, ep, ...) log_exec_full(ec, ep, LOG_NOTICE, __VA_ARGS__)
648#define log_exec_warning(ec, ep, ...) log_exec_full(ec, ep, LOG_WARNING, __VA_ARGS__)
649#define log_exec_error(ec, ep, ...) log_exec_full(ec, ep, LOG_ERR, __VA_ARGS__)
650
651#define log_exec_debug_errno(ec, ep, error, ...) log_exec_full_errno(ec, ep, LOG_DEBUG, error, __VA_ARGS__)
652#define log_exec_info_errno(ec, ep, error, ...) log_exec_full_errno(ec, ep, LOG_INFO, error, __VA_ARGS__)
653#define log_exec_notice_errno(ec, ep, error, ...) log_exec_full_errno(ec, ep, LOG_NOTICE, error, __VA_ARGS__)
654#define log_exec_warning_errno(ec, ep, error, ...) log_exec_full_errno(ec, ep, LOG_WARNING, error, __VA_ARGS__)
655#define log_exec_error_errno(ec, ep, error, ...) log_exec_full_errno(ec, ep, LOG_ERR, error, __VA_ARGS__)
656
657#define log_exec_struct_errno(ec, ep, level, error, ...) \
658 ({ \
659 const ExecContext *_c = (ec); \
660 const ExecParameters *_p = (ep); \
661 const int _l = (level); \
662 bool _do_log = !(_c->log_level_max < 0 || \
663 _c->log_level_max >= LOG_PRI(_l)); \
664 LOG_CONTEXT_PUSH_IOV(_c->log_extra_fields, \
665 _c->n_log_extra_fields); \
666 _do_log ? \
667 log_struct_errno(_l, error, __VA_ARGS__, LOG_EXEC_ID_FIELD_FORMAT(_p), _p->unit_id) : \
668 -ERRNO_VALUE(error); \
669 })
670
671#define log_exec_struct(ec, ep, level, ...) log_exec_struct_errno(ec, ep, level, 0, __VA_ARGS__)
672
673#define log_exec_struct_iovec_errno(ec, ep, level, error, iovec, n_iovec) \
674 ({ \
675 const ExecContext *_c = (ec); \
676 const ExecParameters *_p = (ep); \
677 const int _l = (level); \
678 bool _do_log = !(_c->log_level_max < 0 || \
679 _c->log_level_max >= LOG_PRI(_l)); \
680 LOG_CONTEXT_PUSH_IOV(_c->log_extra_fields, \
681 _c->n_log_extra_fields); \
682 _do_log ? \
683 log_struct_iovec_errno(_l, error, iovec, n_iovec) : \
684 -ERRNO_VALUE(error); \
685 })
686
687#define log_exec_struct_iovec(ec, ep, level, iovec, n_iovec) log_exec_struct_iovec_errno(ec, ep, level, 0, iovec, n_iovec)
688
689/* Like LOG_MESSAGE(), but with the unit name prefixed. */
690#define LOG_EXEC_MESSAGE(ep, fmt, ...) LOG_MESSAGE("%s: " fmt, (ep)->unit_id, ##__VA_ARGS__)
691#define LOG_EXEC_ID(ep) LOG_EXEC_ID_FIELD_FORMAT(ep), (ep)->unit_id
692#define LOG_EXEC_INVOCATION_ID(ep) LOG_EXEC_INVOCATION_ID_FIELD_FORMAT(ep), (ep)->invocation_id_string
693
694#define _LOG_CONTEXT_PUSH_EXEC(ec, ep, p, c) \
695 const ExecContext *c = (ec); \
696 const ExecParameters *p = (ep); \
697 LOG_CONTEXT_PUSH_KEY_VALUE(LOG_EXEC_ID_FIELD(p), p->unit_id); \
698 LOG_CONTEXT_PUSH_KEY_VALUE(LOG_EXEC_INVOCATION_ID_FIELD(p), p->invocation_id_string); \
699 LOG_CONTEXT_PUSH_IOV(c->log_extra_fields, c->n_log_extra_fields)
700
701#define LOG_CONTEXT_PUSH_EXEC(ec, ep) \
702 _LOG_CONTEXT_PUSH_EXEC(ec, ep, UNIQ_T(p, UNIQ), UNIQ_T(c, UNIQ))