Debian Codegraph @

src/core/execute.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <sys/uio.h>
5
6 #include "sd-id128.h"
7
8 #include "bus-unit-util.h"
9 #include "core-forward.h"
10 #include "cpu-set-util.h"
11 #include "exec-util.h"
12 #include "list.h"
13 #include "log-context.h"
14 #include "namespace.h"
15 #include "numa-util.h"
16 #include "ratelimit.h"
17 #include "rlimit-util.h"
18 #include "time-util.h"
19
20 #define EXEC_STDIN_DATA_MAX (64U*1024U*1024U)
21
22 typedef enum ExecUtmpMode {
23 EXEC_UTMP_INIT,
24 EXEC_UTMP_LOGIN,
25 EXEC_UTMP_USER,
26 _EXEC_UTMP_MODE_MAX,
27 _EXEC_UTMP_MODE_INVALID = -EINVAL,
28 } ExecUtmpMode;
29
30 typedef enum ExecInput {
31 EXEC_INPUT_NULL,
32 EXEC_INPUT_TTY,
33 EXEC_INPUT_TTY_FORCE,
34 EXEC_INPUT_TTY_FAIL,
35 EXEC_INPUT_SOCKET,
36 EXEC_INPUT_NAMED_FD,
37 EXEC_INPUT_DATA,
38 EXEC_INPUT_FILE,
39 _EXEC_INPUT_MAX,
40 _EXEC_INPUT_INVALID = -EINVAL,
41 } ExecInput;
42
43 typedef enum ExecOutput {
44 EXEC_OUTPUT_INHERIT,
45 EXEC_OUTPUT_NULL,
46 EXEC_OUTPUT_TTY,
47 EXEC_OUTPUT_KMSG,
48 EXEC_OUTPUT_KMSG_AND_CONSOLE,
49 EXEC_OUTPUT_JOURNAL,
50 EXEC_OUTPUT_JOURNAL_AND_CONSOLE,
51 EXEC_OUTPUT_SOCKET,
52 EXEC_OUTPUT_NAMED_FD,
53 EXEC_OUTPUT_FILE,
54 EXEC_OUTPUT_FILE_APPEND,
55 EXEC_OUTPUT_FILE_TRUNCATE,
56 _EXEC_OUTPUT_MAX,
57 _EXEC_OUTPUT_INVALID = -EINVAL,
58 } ExecOutput;
59
60 typedef enum ExecPreserveMode {
61 EXEC_PRESERVE_NO,
62 EXEC_PRESERVE_YES,
63 EXEC_PRESERVE_RESTART,
64 EXEC_PRESERVE_ON_SUCCESS,
65 _EXEC_PRESERVE_MODE_MAX,
66 _EXEC_PRESERVE_MODE_INVALID = -EINVAL,
67 } ExecPreserveMode;
68
69 typedef enum ExecKeyringMode {
70 EXEC_KEYRING_INHERIT,
71 EXEC_KEYRING_PRIVATE,
72 EXEC_KEYRING_SHARED,
73 _EXEC_KEYRING_MODE_MAX,
74 _EXEC_KEYRING_MODE_INVALID = -EINVAL,
75 } ExecKeyringMode;
76
77 typedef enum ExecMemoryTHP {
78 /*
79 * Inherit default from process that starts systemd, i.e. do not make
80 * any PR_SET_THP_DISABLE call.
81 */
82 EXEC_MEMORY_THP_INHERIT,
83 EXEC_MEMORY_THP_DISABLE, /* Disable THPs completely for the process */
84 EXEC_MEMORY_THP_MADVISE, /* Disable THPs for the process except when madvised */
85 /*
86 * Use system default THP setting. this can be used when the process that
87 * starts systemd has already disabled THPs via PR_SET_THP_DISABLE, and we
88 * want to restore the system default THP setting at process invocation time.
89 */
90 EXEC_MEMORY_THP_SYSTEM,
91 _EXEC_MEMORY_THP_MAX,
92 _EXEC_MEMORY_THP_INVALID = -EINVAL,
93 } ExecMemoryTHP;
94
95 /* Contains start and exit information about an executed command. */
96 typedef struct ExecStatus {
97 dual_timestamp start_timestamp;
98 dual_timestamp exit_timestamp;
99 dual_timestamp handoff_timestamp;
100 pid_t pid;
101 int code; /* as in siginfo_t::si_code */
102 int status; /* as in siginfo_t::si_status */
103 } ExecStatus;
104
105 /* Stores information about commands we execute. Covers both configuration settings as well as runtime data. */
106 typedef struct ExecCommand {
107 char *path;
108 char **argv;
109 ExecStatus exec_status; /* Note that this is not serialized to sd-executor */
110 ExecCommandFlags flags;
111 LIST_FIELDS(ExecCommand, command); /* useful for chaining commands */
112 } ExecCommand;
113
114 /* Encapsulates certain aspects of the runtime environment that is to be shared between multiple otherwise separate
115 * invocations of commands. Specifically, this allows sharing of /tmp and /var/tmp data as well as network namespaces
116 * between invocations of commands. This is a reference counted object, with one reference taken by each currently
117 * active command invocation that wants to share this runtime. */
118 typedef struct ExecSharedRuntime {
119 unsigned n_ref;
120
121 Manager *manager;
122
123 char *id; /* Unit id of the owner */
124
125 char *tmp_dir;
126 char *var_tmp_dir;
127
128 /* An AF_UNIX socket pair, that contains a datagram containing a file descriptor referring to the network
129 * namespace. */
130 int netns_storage_socket[2];
131
132 /* Like netns_storage_socket, but the file descriptor is referring to the IPC namespace. */
133 int ipcns_storage_socket[2];
134
135 /* Like netns_storage_socket, but the file descriptor is referring to the user namespace. */
136 int userns_storage_socket[2];
137 } ExecSharedRuntime;
138
139 typedef struct ExecRuntime {
140 ExecSharedRuntime *shared;
141 DynamicCreds *dynamic_creds;
142
143 /* The path to the ephemeral snapshot of the root directory or root image if one was requested. */
144 char *ephemeral_copy;
145
146 /* An AF_UNIX socket pair that receives the locked file descriptor referring to the ephemeral copy of
147 * the root directory or root image. The lock prevents tmpfiles from removing the ephemeral snapshot
148 * until we're done using it. */
149 int ephemeral_storage_socket[2];
150 } ExecRuntime;
151
152 static inline bool EXEC_DIRECTORY_TYPE_SHALL_CHOWN(ExecDirectoryType t) {
153 /* Returns true for the ExecDirectoryTypes that we shall chown()ing for the user to. We do this for
154 * all of them, except for configuration */
155 return t >= 0 && t < _EXEC_DIRECTORY_TYPE_MAX && t != EXEC_DIRECTORY_CONFIGURATION;
156 }
157
158 typedef struct ExecQuotaLimit {
159 uint64_t quota_absolute; /* absolute quota in bytes; if UINT64_MAX relative quota configured, see below */
160 uint32_t quota_scale; /* relative quota to backend size, scaled to 0…UINT32_MAX */
161 bool quota_enforce;
162 bool quota_accounting;
163 } ExecQuotaLimit;
164
165 typedef struct ExecDirectoryItem {
166 char *path;
167 char **symlinks;
168 ExecDirectoryFlags flags;
169 bool idmapped;
170 } ExecDirectoryItem;
171
172 typedef struct ExecDirectory {
173 mode_t mode;
174 size_t n_items;
175 ExecDirectoryItem *items;
176 ExecQuotaLimit exec_quota;
177 } ExecDirectory;
178
179 typedef enum ExecCleanMask {
180 /* In case you wonder why the bitmask below doesn't use "directory" in its name: we want to keep this
181 * generic so that .timer timestamp files can nicely be covered by this too, and similar. */
182 EXEC_CLEAN_RUNTIME = 1U << EXEC_DIRECTORY_RUNTIME,
183 EXEC_CLEAN_STATE = 1U << EXEC_DIRECTORY_STATE,
184 EXEC_CLEAN_CACHE = 1U << EXEC_DIRECTORY_CACHE,
185 EXEC_CLEAN_LOGS = 1U << EXEC_DIRECTORY_LOGS,
186 EXEC_CLEAN_CONFIGURATION = 1U << EXEC_DIRECTORY_CONFIGURATION,
187 EXEC_CLEAN_FDSTORE = 1U << _EXEC_DIRECTORY_TYPE_MAX,
188 EXEC_CLEAN_NONE = 0,
189 EXEC_CLEAN_ALL = (1U << (_EXEC_DIRECTORY_TYPE_MAX+1)) - 1,
190 _EXEC_CLEAN_MASK_INVALID = -EINVAL,
191 } ExecCleanMask;
192
193 /* Encodes configuration parameters applied to invoked commands. Does not carry runtime data, but only configuration
194 * changes sourced from unit files and suchlike. ExecContext objects are usually embedded into Unit objects, and do not
195 * change after being loaded. */
196 typedef struct ExecContext {
197 char **environment;
198 char **environment_files;
199 char **pass_environment;
200 char **unset_environment;
201
202 struct rlimit *rlimit[_RLIMIT_MAX];
203 char *working_directory;
204 char *root_directory;
205 char *root_image, *root_verity, *root_hash_path, *root_hash_sig_path;
206 struct iovec root_hash, root_hash_sig;
207 MountOptions *root_image_options;
208 char *root_mstack;
209 bool root_ephemeral;
210 bool working_directory_missing_ok:1;
211 bool working_directory_home:1;
212
213 bool oom_score_adjust_set:1;
214 bool coredump_filter_set:1;
215 bool nice_set:1;
216 bool ioprio_is_set:1;
217 bool cpu_sched_set:1;
218
219 /* This is not exposed to the user but available internally. We need it to make sure that whenever we
220 * spawn /usr/bin/mount it is run in the same process group as us so that the autofs logic detects
221 * that it belongs to us and we don't enter a trigger loop. */
222 bool same_pgrp;
223
224 bool cpu_sched_reset_on_fork;
225 bool non_blocking;
226
227 mode_t umask;
228 int oom_score_adjust;
229 int nice;
230 int ioprio;
231 int cpu_sched_policy;
232 int cpu_sched_priority;
233 uint64_t coredump_filter;
234
235 CPUSet cpu_set;
236 NUMAPolicy numa_policy;
237 bool cpu_affinity_from_numa;
238
239 ExecInput std_input;
240 ExecOutput std_output;
241 ExecOutput std_error;
242
243 /* At least one of stdin/stdout/stderr was initialized from an fd passed in. This boolean survives
244 * the fds being closed. This only makes sense for transient units. */
245 bool stdio_as_fds;
246 bool root_directory_as_fd;
247
248 char *stdio_fdname[3];
249 char *stdio_file[3];
250
251 void *stdin_data;
252 size_t stdin_data_size;
253
254 nsec_t timer_slack_nsec;
255
256 char *tty_path;
257
258 bool tty_reset;
259 bool tty_vhangup;
260 bool tty_vt_disallocate;
261
262 unsigned tty_rows;
263 unsigned tty_cols;
264
265 bool ignore_sigpipe;
266
267 ExecKeyringMode keyring_mode;
268
269 /* Since resolving these names might involve socket
270 * connections and we don't want to deadlock ourselves these
271 * names are resolved on execution only and in the child
272 * process. */
273 char *user;
274 char *group;
275 char **supplementary_groups;
276
277 int set_login_environment;
278
279 char *pam_name;
280
281 char *utmp_id;
282 ExecUtmpMode utmp_mode;
283
284 bool no_new_privileges;
285
286 bool selinux_context_ignore;
287 bool apparmor_profile_ignore;
288 bool smack_process_label_ignore;
289
290 char *selinux_context;
291 char *apparmor_profile;
292 char *smack_process_label;
293
294 char **read_write_paths, **read_only_paths, **inaccessible_paths, **exec_paths, **no_exec_paths;
295 char **exec_search_path;
296 unsigned long mount_propagation_flag;
297 BindMount *bind_mounts;
298 size_t n_bind_mounts;
299 TemporaryFileSystem *temporary_filesystems;
300 size_t n_temporary_filesystems;
301 MountImage *mount_images;
302 size_t n_mount_images;
303 MountImage *extension_images;
304 size_t n_extension_images;
305 char **extension_directories;
306
307 uint64_t capability_bounding_set;
308 uint64_t capability_ambient_set;
309 int secure_bits;
310
311 int syslog_priority;
312 bool syslog_level_prefix;
313 char *syslog_identifier;
314
315 struct iovec* log_extra_fields;
316 size_t n_log_extra_fields;
317 Set *log_filter_allowed_patterns;
318 Set *log_filter_denied_patterns;
319
320 RateLimit log_ratelimit;
321
322 int log_level_max;
323
324 char *log_namespace;
325
326 ProtectProc protect_proc; /* hidepid= */
327 ProcSubset proc_subset; /* subset= */
328
329 PrivateBPF private_bpf;
330 uint64_t bpf_delegate_commands, bpf_delegate_maps, bpf_delegate_programs, bpf_delegate_attachments;
331
332 int private_mounts;
333 int mount_apivfs;
334 int bind_log_sockets;
335 int memory_ksm;
336 ExecMemoryTHP memory_thp;
337 PrivateTmp private_tmp; /* Those are not independent parameters, but are calculated from */
338 PrivateTmp private_var_tmp; /* other parameters in unit_patch_contexts(). */
339
340 bool private_network;
341 bool private_devices;
342 PrivateUsers private_users;
343 bool private_ipc;
344 bool protect_kernel_tunables;
345 bool protect_kernel_modules;
346 bool protect_kernel_logs;
347 bool protect_clock;
348 ProtectControlGroups protect_control_groups;
349 ProtectSystem protect_system;
350 ProtectHome protect_home;
351 PrivatePIDs private_pids;
352 ProtectHostname protect_hostname;
353 char *private_hostname;
354
355 bool dynamic_user;
356 bool remove_ipc;
357
358 bool memory_deny_write_execute;
359 bool restrict_realtime;
360 bool restrict_suid_sgid;
361
362 bool lock_personality;
363 unsigned long personality;
364
365 unsigned long restrict_namespaces; /* The CLONE_NEWxyz flags permitted to the unit's processes */
366 unsigned long delegate_namespaces; /* The CLONE_NEWxyz flags delegated to the unit's processes */
367
368 Set *restrict_filesystems;
369 bool restrict_filesystems_allow_list:1;
370
371 Hashmap *syscall_filter;
372 Set *syscall_archs;
373 int syscall_errno;
374 bool syscall_allow_list:1;
375
376 Hashmap *syscall_log;
377 bool syscall_log_allow_list:1; /* Log listed system calls */
378
379 bool address_families_allow_list:1;
380 Set *address_families;
381
382 char *user_namespace_path;
383 char *network_namespace_path;
384 char *ipc_namespace_path;
385
386 ExecDirectory directories[_EXEC_DIRECTORY_TYPE_MAX];
387 ExecPreserveMode runtime_directory_preserve_mode;
388 usec_t timeout_clean_usec;
389
390 Hashmap *set_credentials; /* output id → ExecSetCredential */
391 Hashmap *load_credentials; /* output id → ExecLoadCredential */
392 OrderedSet *import_credentials; /* ExecImportCredential */
393
394 ImagePolicy *root_image_policy, *mount_image_policy, *extension_image_policy;
395 } ExecContext;
396
397 typedef enum ExecFlags {
398 EXEC_APPLY_SANDBOXING = 1 << 0,
399 EXEC_APPLY_CHROOT = 1 << 1,
400 EXEC_APPLY_TTY_STDIN = 1 << 2,
401 EXEC_PASS_LOG_UNIT = 1 << 3, /* Whether to pass the unit name to the service's journal stream connection */
402 EXEC_CHOWN_DIRECTORIES = 1 << 4, /* chown() the runtime/state/cache/log directories to the user we run as, under all conditions */
403 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 */
404 EXEC_CGROUP_DELEGATE = 1 << 6,
405 EXEC_IS_CONTROL = 1 << 7,
406 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 */
407 EXEC_SETUP_CREDENTIALS = 1 << 9, /* Set up the credential store logic */
408 EXEC_SETUP_CREDENTIALS_FRESH = 1 << 10, /* Set up a new credential store (disable reuse) */
409
410 /* The following are not used by execute.c, but by consumers internally */
411 EXEC_PASS_FDS = 1 << 11,
412 EXEC_SETENV_RESULT = 1 << 12,
413 EXEC_SET_WATCHDOG = 1 << 13,
414 EXEC_SETENV_MONITOR_RESULT = 1 << 14, /* Pass exit status to OnFailure= and OnSuccess= dependencies. */
415 } ExecFlags;
416
417 /* Parameters for a specific invocation of a command. This structure is put together right before a command is
418 * executed. */
419 typedef struct ExecParameters {
420 RuntimeScope runtime_scope;
421
422 ExecFlags flags;
423
424 char **environment;
425 char **files_env;
426
427 int *fds;
428 char **fd_names;
429 size_t n_socket_fds;
430 size_t n_stashed_fds;
431
432 char *cgroup_path;
433 uint64_t cgroup_id;
434
435 char **prefix;
436 char *received_credentials_directory;
437 char *received_encrypted_credentials_directory;
438
439 char *confirm_spawn;
440 bool shall_confirm_spawn;
441
442 usec_t watchdog_usec;
443
444 int *idle_pipe;
445
446 int stdin_fd;
447 int stdout_fd;
448 int stderr_fd;
449 int root_directory_fd;
450
451 /* An fd that is closed by the execve(), and thus will result in EOF when the execve() is done. */
452 int exec_fd;
453
454 char *notify_socket;
455
456 LIST_HEAD(OpenFile, open_files);
457
458 char *fallback_smack_process_label;
459
460 int user_lookup_fd;
461 int handoff_timestamp_fd;
462 int pidref_transport_fd;
463
464 int bpf_restrict_fs_map_fd;
465
466 /* Used for logging in the executor functions */
467 char *unit_id;
468 sd_id128_t invocation_id;
469 char invocation_id_string[SD_ID128_STRING_MAX];
470
471 bool debug_invocation;
472 bool selinux_context_net;
473 } ExecParameters;
474
475 #define EXEC_PARAMETERS_INIT(_flags) \
476 (ExecParameters) { \
477 .flags = (_flags), \
478 .stdin_fd = -EBADF, \
479 .stdout_fd = -EBADF, \
480 .stderr_fd = -EBADF, \
481 .root_directory_fd = -EBADF, \
482 .exec_fd = -EBADF, \
483 .bpf_restrict_fs_map_fd = -EBADF, \
484 .user_lookup_fd = -EBADF, \
485 .handoff_timestamp_fd = -EBADF, \
486 .pidref_transport_fd = -EBADF, \
487 }
488
489 static inline bool exec_input_is_terminal(ExecInput i) {
490 return IN_SET(i,
491 EXEC_INPUT_TTY,
492 EXEC_INPUT_TTY_FORCE,
493 EXEC_INPUT_TTY_FAIL);
494 }
495
496 static inline bool exec_context_has_tty(const ExecContext *context) {
497 assert(context);
498
499 return
500 context->tty_path ||
501 exec_input_is_terminal(context->std_input) ||
502 context->std_output == EXEC_OUTPUT_TTY ||
503 context->std_error == EXEC_OUTPUT_TTY;
504 }
505
506 static inline bool exec_input_is_inheritable(ExecInput i) {
507 /* We assume these listed inputs refer to bidirectional streams, and hence duplicating them from
508 * stdin to stdout/stderr makes sense and hence allowing EXEC_OUTPUT_INHERIT makes sense, too.
509 * Outputs such as regular files or sealed data memfds otoh don't really make sense to be
510 * duplicated for both input and output at the same time (since they then would cause a feedback
511 * loop). */
512
513 return exec_input_is_terminal(i) || IN_SET(i, EXEC_INPUT_SOCKET, EXEC_INPUT_NAMED_FD);
514 }
515
516 int exec_spawn(
517 Unit *unit,
518 ExecCommand *command,
519 const ExecContext *context,
520 ExecParameters *exec_params,
521 ExecRuntime *runtime,
522 const CGroupContext *cgroup_context,
523 PidRef *ret);
524
525 void exec_command_done(ExecCommand *c);
526 void exec_command_done_array(ExecCommand *c, size_t n);
527 ExecCommand* exec_command_free(ExecCommand *c);
528 DEFINE_TRIVIAL_CLEANUP_FUNC(ExecCommand*, exec_command_free);
529 ExecCommand* exec_command_free_list(ExecCommand *c);
530 void exec_command_free_array(ExecCommand **c, size_t n);
531 void exec_command_reset_status_array(ExecCommand *c, size_t n);
532 void exec_command_reset_status_list_array(ExecCommand **c, size_t n);
533
534 void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix);
535 void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix);
536 void exec_command_append_list(ExecCommand **l, ExecCommand *e);
537 int exec_command_set(ExecCommand *c, const char *path, ...) _sentinel_;
538 int exec_command_append(ExecCommand *c, const char *path, ...) _sentinel_;
539
540 void exec_context_init(ExecContext *c);
541 void exec_context_done(ExecContext *c);
542 void exec_context_dump(const ExecContext *c, FILE* f, const char *prefix);
543
544 int exec_context_apply_environment(Unit *u, ExecContext *c, char **env, UnitWriteFlags flags);
545 int exec_context_apply_set_credential(Unit *u, ExecContext *c, const char *id, const void *data, size_t size, bool encrypted, UnitWriteFlags flags, const char **reterr_message);
546
547 int exec_context_destroy_runtime_directory(const ExecContext *c, const char *runtime_prefix);
548 int exec_context_destroy_mount_ns_dir(Unit *u);
549
550 const char* exec_context_fdname(const ExecContext *c, int fd_index) _pure_;
551
552 bool exec_context_may_touch_console(const ExecContext *c);
553 bool exec_context_maintains_privileges(const ExecContext *c);
554 bool exec_context_shall_ansi_seq_reset(const ExecContext *c);
555
556 int exec_context_get_effective_ioprio(const ExecContext *c);
557 bool exec_context_get_effective_mount_apivfs(const ExecContext *c);
558 bool exec_context_get_effective_bind_log_sockets(const ExecContext *c);
559
560 void exec_context_free_log_extra_fields(ExecContext *c);
561
562 void exec_context_revert_tty(ExecContext *c, sd_id128_t invocation_id);
563
564 int exec_context_get_clean_directories(ExecContext *c, char **prefix, ExecCleanMask mask, char ***ret);
565 int exec_context_get_clean_mask(ExecContext *c, ExecCleanMask *ret);
566
567 const char* exec_context_tty_path(const ExecContext *context);
568 int exec_context_apply_tty_size(const ExecContext *context, int input_fd, int output_fd, const char *tty_path);
569 void exec_context_tty_reset(const ExecContext *context, const ExecParameters *parameters, sd_id128_t invocation_id);
570
571 uint64_t exec_context_get_rlimit(const ExecContext *c, const char *name);
572 int exec_context_get_oom_score_adjust(const ExecContext *c);
573 uint64_t exec_context_get_coredump_filter(const ExecContext *c);
574 int exec_context_get_nice(const ExecContext *c);
575 int exec_context_get_cpu_sched_policy(const ExecContext *c);
576 int exec_context_get_cpu_sched_priority(const ExecContext *c);
577 uint64_t exec_context_get_timer_slack_nsec(const ExecContext *c);
578 bool exec_context_get_set_login_environment(const ExecContext *c);
579 char** exec_context_get_syscall_filter(const ExecContext *c);
580 char** exec_context_get_syscall_archs(const ExecContext *c);
581 char** exec_context_get_syscall_log(const ExecContext *c);
582 char** exec_context_get_address_families(const ExecContext *c);
583 char** exec_context_get_restrict_filesystems(const ExecContext *c);
584 bool exec_context_restrict_namespaces_set(const ExecContext *c);
585 bool exec_context_restrict_filesystems_set(const ExecContext *c);
586 bool exec_context_with_rootfs(const ExecContext *c);
587 bool exec_context_with_rootfs_strict(const ExecContext *c);
588
589 int exec_context_has_vpicked_extensions(const ExecContext *context);
590
591 void exec_status_start(ExecStatus *s, pid_t pid, const dual_timestamp *ts);
592 void exec_status_exit(ExecStatus *s, const ExecContext *context, pid_t pid, int code, int status);
593 void exec_status_handoff(ExecStatus *s, const struct ucred *ucred, const dual_timestamp *ts);
594 void exec_status_dump(const ExecStatus *s, FILE *f, const char *prefix);
595 void exec_status_reset(ExecStatus *s);
596
597 int exec_shared_runtime_acquire(Manager *m, const ExecContext *c, const char *id, bool create, ExecSharedRuntime **ret);
598 ExecSharedRuntime *exec_shared_runtime_destroy(ExecSharedRuntime *r);
599 DECLARE_TRIVIAL_UNREF_FUNC(ExecSharedRuntime, exec_shared_runtime);
600 DEFINE_TRIVIAL_CLEANUP_FUNC(ExecSharedRuntime*, exec_shared_runtime_unref);
601
602 int exec_shared_runtime_serialize(const Manager *m, FILE *f, FDSet *fds);
603 int exec_shared_runtime_deserialize_compat(Unit *u, const char *key, const char *value, FDSet *fds);
604 int exec_shared_runtime_deserialize_one(Manager *m, const char *value, FDSet *fds);
605 void exec_shared_runtime_done(ExecSharedRuntime *rt);
606 void exec_shared_runtime_vacuum(Manager *m);
607
608 int exec_runtime_make(const Unit *unit, const ExecContext *context, ExecSharedRuntime *shared, DynamicCreds *creds, ExecRuntime **ret);
609 ExecRuntime* exec_runtime_free(ExecRuntime *rt);
610 DEFINE_TRIVIAL_CLEANUP_FUNC(ExecRuntime*, exec_runtime_free);
611 ExecRuntime* exec_runtime_destroy(ExecRuntime *rt);
612 void exec_runtime_clear(ExecRuntime *rt);
613
614 int exec_params_needs_control_subcgroup(const ExecParameters *params);
615 int exec_params_get_cgroup_path(const ExecParameters *params, const CGroupContext *c, const char *prefix, char **ret);
616 void exec_params_shallow_clear(ExecParameters *p);
617 void exec_params_deep_clear(ExecParameters *p);
618 void exec_params_dump(const ExecParameters *p, FILE* f, const char *prefix);
619
620 bool exec_context_get_cpu_affinity_from_numa(const ExecContext *c);
621
622 void exec_directory_done(ExecDirectory *d);
623 int exec_directory_add(ExecDirectory *d, const char *path, const char *symlink, ExecDirectoryFlags flags);
624 void exec_directory_sort(ExecDirectory *d);
625 bool exec_directory_is_private(const ExecContext *context, ExecDirectoryType type);
626
627 DECLARE_STRING_TABLE_LOOKUP_FROM_STRING(exec_clean_mask, ExecCleanMask);
628
629 DECLARE_STRING_TABLE_LOOKUP(exec_input, ExecInput);
630 DECLARE_STRING_TABLE_LOOKUP(exec_output, ExecOutput);
631
632 DECLARE_STRING_TABLE_LOOKUP(exec_utmp_mode, ExecUtmpMode);
633
634 DECLARE_STRING_TABLE_LOOKUP(exec_preserve_mode, ExecPreserveMode);
635
636 DECLARE_STRING_TABLE_LOOKUP(exec_keyring_mode, ExecKeyringMode);
637
638 DECLARE_STRING_TABLE_LOOKUP(exec_directory_type_symlink, ExecDirectoryType);
639 DECLARE_STRING_TABLE_LOOKUP(exec_directory_type_mode, ExecDirectoryType);
640
641 DECLARE_STRING_TABLE_LOOKUP(exec_resource_type, ExecDirectoryType);
642
643 DECLARE_STRING_TABLE_LOOKUP(exec_memory_thp, ExecMemoryTHP);
644
645 bool exec_needs_mount_namespace(const ExecContext *context, const ExecParameters *params);
646 bool exec_needs_network_namespace(const ExecContext *context);
647 bool exec_needs_ipc_namespace(const ExecContext *context);
648 bool exec_needs_pid_namespace(const ExecContext *context, const ExecParameters *params);
649
650 ProtectControlGroups exec_get_protect_control_groups(const ExecContext *context);
651 bool exec_needs_cgroup_namespace(const ExecContext *context);
652 bool exec_needs_cgroup_mount(const ExecContext *context);
653 bool exec_is_cgroup_mount_read_only(const ExecContext *context);
654
655 const char* exec_get_private_notify_socket_path(const ExecContext *context, const ExecParameters *params, bool needs_sandboxing);
656
657 int exec_log_level_max_with_exec_params(const ExecContext *context, const ExecParameters *params);
658 int exec_log_level_max(const ExecContext *context);
659
660 /* These logging macros do the same logging as those in unit.h, but using ExecContext and ExecParameters
661 * instead of the unit object, so that it can be used in the sd-executor context (where the unit object is
662 * not available). */
663
664 #define LOG_EXEC_ID_FIELD(ep) \
665 ((ep)->runtime_scope == RUNTIME_SCOPE_USER ? "USER_UNIT=" : "UNIT=")
666 #define LOG_EXEC_INVOCATION_ID_FIELD(ep) \
667 ((ep)->runtime_scope == RUNTIME_SCOPE_USER ? "USER_INVOCATION_ID=" : "INVOCATION_ID=")
668
669 /* Like LOG_MESSAGE(), but with the unit name prefixed. */
670 #define LOG_EXEC_MESSAGE(ep, fmt, ...) LOG_MESSAGE("%s: " fmt, (ep)->unit_id, ##__VA_ARGS__)
671 #define LOG_EXEC_ID(ep) LOG_ITEM("%s%s", LOG_EXEC_ID_FIELD(ep), (ep)->unit_id)
672 #define LOG_EXEC_INVOCATION_ID(ep) LOG_ITEM("%s%s", LOG_EXEC_INVOCATION_ID_FIELD(ep), (ep)->invocation_id_string)
673
674 #define _LOG_CONTEXT_PUSH_EXEC(ec, ep, p, c) \
675 const ExecContext *c = (ec); \
676 const ExecParameters *p = (ep); \
677 LOG_CONTEXT_PUSH_KEY_VALUE(LOG_EXEC_ID_FIELD(p), p->unit_id); \
678 LOG_CONTEXT_PUSH_KEY_VALUE(LOG_EXEC_INVOCATION_ID_FIELD(p), p->invocation_id_string); \
679 LOG_CONTEXT_PUSH_IOV(c->log_extra_fields, c->n_log_extra_fields) \
680 LOG_CONTEXT_SET_LOG_LEVEL(exec_log_level_max_with_exec_params(c, p)) \
681 LOG_SET_PREFIX(p->unit_id);
682
683 #define LOG_CONTEXT_PUSH_EXEC(ec, ep) \
684 _LOG_CONTEXT_PUSH_EXEC(ec, ep, UNIQ_T(p, UNIQ), UNIQ_T(c, UNIQ))