]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/execute.h
Merge pull request #33079 from poettering/watchdog-no-disarm
[thirdparty/systemd.git] / src / core / execute.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 typedef struct ExecStatus ExecStatus;
5 typedef struct ExecCommand ExecCommand;
6 typedef struct ExecContext ExecContext;
7 typedef struct ExecSharedRuntime ExecSharedRuntime;
8 typedef struct DynamicCreds DynamicCreds;
9 typedef struct ExecRuntime ExecRuntime;
10 typedef struct ExecParameters ExecParameters;
11 typedef struct Manager Manager;
12
13 #include <sched.h>
14 #include <stdbool.h>
15 #include <stdio.h>
16 #include <sys/capability.h>
17
18 #include "cgroup-util.h"
19 #include "coredump-util.h"
20 #include "cpu-set-util.h"
21 #include "exec-util.h"
22 #include "fdset.h"
23 #include "list.h"
24 #include "missing_resource.h"
25 #include "namespace.h"
26 #include "nsflags.h"
27 #include "numa-util.h"
28 #include "open-file.h"
29 #include "path-util.h"
30 #include "runtime-scope.h"
31 #include "set.h"
32 #include "time-util.h"
33
34 #define EXEC_STDIN_DATA_MAX (64U*1024U*1024U)
35
36 typedef enum ExecUtmpMode {
37 EXEC_UTMP_INIT,
38 EXEC_UTMP_LOGIN,
39 EXEC_UTMP_USER,
40 _EXEC_UTMP_MODE_MAX,
41 _EXEC_UTMP_MODE_INVALID = -EINVAL,
42 } ExecUtmpMode;
43
44 typedef enum ExecInput {
45 EXEC_INPUT_NULL,
46 EXEC_INPUT_TTY,
47 EXEC_INPUT_TTY_FORCE,
48 EXEC_INPUT_TTY_FAIL,
49 EXEC_INPUT_SOCKET,
50 EXEC_INPUT_NAMED_FD,
51 EXEC_INPUT_DATA,
52 EXEC_INPUT_FILE,
53 _EXEC_INPUT_MAX,
54 _EXEC_INPUT_INVALID = -EINVAL,
55 } ExecInput;
56
57 typedef enum ExecOutput {
58 EXEC_OUTPUT_INHERIT,
59 EXEC_OUTPUT_NULL,
60 EXEC_OUTPUT_TTY,
61 EXEC_OUTPUT_KMSG,
62 EXEC_OUTPUT_KMSG_AND_CONSOLE,
63 EXEC_OUTPUT_JOURNAL,
64 EXEC_OUTPUT_JOURNAL_AND_CONSOLE,
65 EXEC_OUTPUT_SOCKET,
66 EXEC_OUTPUT_NAMED_FD,
67 EXEC_OUTPUT_FILE,
68 EXEC_OUTPUT_FILE_APPEND,
69 EXEC_OUTPUT_FILE_TRUNCATE,
70 _EXEC_OUTPUT_MAX,
71 _EXEC_OUTPUT_INVALID = -EINVAL,
72 } ExecOutput;
73
74 typedef enum ExecPreserveMode {
75 EXEC_PRESERVE_NO,
76 EXEC_PRESERVE_YES,
77 EXEC_PRESERVE_RESTART,
78 _EXEC_PRESERVE_MODE_MAX,
79 _EXEC_PRESERVE_MODE_INVALID = -EINVAL,
80 } ExecPreserveMode;
81
82 typedef enum ExecKeyringMode {
83 EXEC_KEYRING_INHERIT,
84 EXEC_KEYRING_PRIVATE,
85 EXEC_KEYRING_SHARED,
86 _EXEC_KEYRING_MODE_MAX,
87 _EXEC_KEYRING_MODE_INVALID = -EINVAL,
88 } ExecKeyringMode;
89
90 /* Contains start and exit information about an executed command. */
91 struct ExecStatus {
92 dual_timestamp start_timestamp;
93 dual_timestamp exit_timestamp;
94 dual_timestamp handoff_timestamp;
95 pid_t pid;
96 int code; /* as in siginfo_t::si_code */
97 int status; /* as in siginfo_t::si_status */
98 };
99
100 /* Stores information about commands we execute. Covers both configuration settings as well as runtime data. */
101 struct ExecCommand {
102 char *path;
103 char **argv;
104 ExecStatus exec_status; /* Note that this is not serialized to sd-executor */
105 ExecCommandFlags flags;
106 LIST_FIELDS(ExecCommand, command); /* useful for chaining commands */
107 };
108
109 /* Encapsulates certain aspects of the runtime environment that is to be shared between multiple otherwise separate
110 * invocations of commands. Specifically, this allows sharing of /tmp and /var/tmp data as well as network namespaces
111 * between invocations of commands. This is a reference counted object, with one reference taken by each currently
112 * active command invocation that wants to share this runtime. */
113 struct ExecSharedRuntime {
114 unsigned n_ref;
115
116 Manager *manager;
117
118 char *id; /* Unit id of the owner */
119
120 char *tmp_dir;
121 char *var_tmp_dir;
122
123 /* An AF_UNIX socket pair, that contains a datagram containing a file descriptor referring to the network
124 * namespace. */
125 int netns_storage_socket[2];
126
127 /* Like netns_storage_socket, but the file descriptor is referring to the IPC namespace. */
128 int ipcns_storage_socket[2];
129 };
130
131 struct ExecRuntime {
132 ExecSharedRuntime *shared;
133 DynamicCreds *dynamic_creds;
134
135 /* The path to the ephemeral snapshot of the root directory or root image if one was requested. */
136 char *ephemeral_copy;
137
138 /* An AF_UNIX socket pair that receives the locked file descriptor referring to the ephemeral copy of
139 * the root directory or root image. The lock prevents tmpfiles from removing the ephemeral snapshot
140 * until we're done using it. */
141 int ephemeral_storage_socket[2];
142 };
143
144 typedef enum ExecDirectoryType {
145 EXEC_DIRECTORY_RUNTIME,
146 EXEC_DIRECTORY_STATE,
147 EXEC_DIRECTORY_CACHE,
148 EXEC_DIRECTORY_LOGS,
149 EXEC_DIRECTORY_CONFIGURATION,
150 _EXEC_DIRECTORY_TYPE_MAX,
151 _EXEC_DIRECTORY_TYPE_INVALID = -EINVAL,
152 } ExecDirectoryType;
153
154 typedef struct ExecDirectoryItem {
155 char *path;
156 char **symlinks;
157 bool only_create;
158 } ExecDirectoryItem;
159
160 typedef struct ExecDirectory {
161 mode_t mode;
162 size_t n_items;
163 ExecDirectoryItem *items;
164 } ExecDirectory;
165
166 typedef enum ExecCleanMask {
167 /* In case you wonder why the bitmask below doesn't use "directory" in its name: we want to keep this
168 * generic so that .timer timestamp files can nicely be covered by this too, and similar. */
169 EXEC_CLEAN_RUNTIME = 1U << EXEC_DIRECTORY_RUNTIME,
170 EXEC_CLEAN_STATE = 1U << EXEC_DIRECTORY_STATE,
171 EXEC_CLEAN_CACHE = 1U << EXEC_DIRECTORY_CACHE,
172 EXEC_CLEAN_LOGS = 1U << EXEC_DIRECTORY_LOGS,
173 EXEC_CLEAN_CONFIGURATION = 1U << EXEC_DIRECTORY_CONFIGURATION,
174 EXEC_CLEAN_FDSTORE = 1U << _EXEC_DIRECTORY_TYPE_MAX,
175 EXEC_CLEAN_NONE = 0,
176 EXEC_CLEAN_ALL = (1U << (_EXEC_DIRECTORY_TYPE_MAX+1)) - 1,
177 _EXEC_CLEAN_MASK_INVALID = -EINVAL,
178 } ExecCleanMask;
179
180 /* Encodes configuration parameters applied to invoked commands. Does not carry runtime data, but only configuration
181 * changes sourced from unit files and suchlike. ExecContext objects are usually embedded into Unit objects, and do not
182 * change after being loaded. */
183 struct ExecContext {
184 char **environment;
185 char **environment_files;
186 char **pass_environment;
187 char **unset_environment;
188
189 struct rlimit *rlimit[_RLIMIT_MAX];
190 char *working_directory, *root_directory, *root_image, *root_verity, *root_hash_path, *root_hash_sig_path;
191 void *root_hash, *root_hash_sig;
192 size_t root_hash_size, root_hash_sig_size;
193 LIST_HEAD(MountOptions, root_image_options);
194 bool root_ephemeral;
195 bool working_directory_missing_ok:1;
196 bool working_directory_home:1;
197
198 bool oom_score_adjust_set:1;
199 bool coredump_filter_set:1;
200 bool nice_set:1;
201 bool ioprio_set:1;
202 bool cpu_sched_set:1;
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;
211
212 mode_t umask;
213 int oom_score_adjust;
214 int nice;
215 int ioprio;
216 int cpu_sched_policy;
217 int cpu_sched_priority;
218 uint64_t coredump_filter;
219
220 CPUSet cpu_set;
221 NUMAPolicy numa_policy;
222 bool cpu_affinity_from_numa;
223
224 ExecInput std_input;
225 ExecOutput std_output;
226 ExecOutput std_error;
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. */
230 bool stdio_as_fds;
231
232 char *stdio_fdname[3];
233 char *stdio_file[3];
234
235 void *stdin_data;
236 size_t stdin_data_size;
237
238 nsec_t timer_slack_nsec;
239
240 char *tty_path;
241
242 bool tty_reset;
243 bool tty_vhangup;
244 bool tty_vt_disallocate;
245
246 unsigned tty_rows;
247 unsigned tty_cols;
248
249 bool ignore_sigpipe;
250
251 ExecKeyringMode keyring_mode;
252
253 /* Since resolving these names might involve socket
254 * connections and we don't want to deadlock ourselves these
255 * names are resolved on execution only and in the child
256 * process. */
257 char *user;
258 char *group;
259 char **supplementary_groups;
260
261 int set_login_environment;
262
263 char *pam_name;
264
265 char *utmp_id;
266 ExecUtmpMode utmp_mode;
267
268 bool no_new_privileges;
269
270 bool selinux_context_ignore;
271 bool apparmor_profile_ignore;
272 bool smack_process_label_ignore;
273
274 char *selinux_context;
275 char *apparmor_profile;
276 char *smack_process_label;
277
278 char **read_write_paths, **read_only_paths, **inaccessible_paths, **exec_paths, **no_exec_paths;
279 char **exec_search_path;
280 unsigned long mount_propagation_flag;
281 BindMount *bind_mounts;
282 size_t n_bind_mounts;
283 TemporaryFileSystem *temporary_filesystems;
284 size_t n_temporary_filesystems;
285 MountImage *mount_images;
286 size_t n_mount_images;
287 MountImage *extension_images;
288 size_t n_extension_images;
289 char **extension_directories;
290
291 uint64_t capability_bounding_set;
292 uint64_t capability_ambient_set;
293 int secure_bits;
294
295 int syslog_priority;
296 bool syslog_level_prefix;
297 char *syslog_identifier;
298
299 struct iovec* log_extra_fields;
300 size_t n_log_extra_fields;
301 Set *log_filter_allowed_patterns;
302 Set *log_filter_denied_patterns;
303
304 usec_t log_ratelimit_interval_usec;
305 unsigned log_ratelimit_burst;
306
307 int log_level_max;
308
309 char *log_namespace;
310
311 ProtectProc protect_proc; /* hidepid= */
312 ProcSubset proc_subset; /* subset= */
313
314 int private_mounts;
315 int mount_apivfs;
316 int memory_ksm;
317 bool private_tmp;
318 bool private_network;
319 bool private_devices;
320 bool private_users;
321 bool private_ipc;
322 bool protect_kernel_tunables;
323 bool protect_kernel_modules;
324 bool protect_kernel_logs;
325 bool protect_clock;
326 bool protect_control_groups;
327 ProtectSystem protect_system;
328 ProtectHome protect_home;
329 bool protect_hostname;
330
331 bool dynamic_user;
332 bool remove_ipc;
333
334 bool memory_deny_write_execute;
335 bool restrict_realtime;
336 bool restrict_suid_sgid;
337
338 bool lock_personality;
339 unsigned long personality;
340
341 unsigned long restrict_namespaces; /* The CLONE_NEWxyz flags permitted to the unit's processes */
342
343 Set *restrict_filesystems;
344 bool restrict_filesystems_allow_list:1;
345
346 Hashmap *syscall_filter;
347 Set *syscall_archs;
348 int syscall_errno;
349 bool syscall_allow_list:1;
350
351 Hashmap *syscall_log;
352 bool syscall_log_allow_list:1; /* Log listed system calls */
353
354 bool address_families_allow_list:1;
355 Set *address_families;
356
357 char *network_namespace_path;
358 char *ipc_namespace_path;
359
360 ExecDirectory directories[_EXEC_DIRECTORY_TYPE_MAX];
361 ExecPreserveMode runtime_directory_preserve_mode;
362 usec_t timeout_clean_usec;
363
364 Hashmap *set_credentials; /* output id → ExecSetCredential */
365 Hashmap *load_credentials; /* output id → ExecLoadCredential */
366 Set *import_credentials;
367
368 ImagePolicy *root_image_policy, *mount_image_policy, *extension_image_policy;
369 };
370
371 static 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
377 static 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
384 static 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
392 typedef enum ExecFlags {
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 */
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 */
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_SETUP_CREDENTIALS = 1 << 9, /* Set up the credential store logic */
403 EXEC_SETUP_CREDENTIALS_FRESH = 1 << 10, /* Set up a new credential store (disable reuse) */
404
405 /* The following are not used by execute.c, but by consumers internally */
406 EXEC_PASS_FDS = 1 << 11,
407 EXEC_SETENV_RESULT = 1 << 12,
408 EXEC_SET_WATCHDOG = 1 << 13,
409 EXEC_SETENV_MONITOR_RESULT = 1 << 14, /* Pass exit status to OnFailure= and OnSuccess= dependencies. */
410 } ExecFlags;
411
412 /* Parameters for a specific invocation of a command. This structure is put together right before a command is
413 * executed. */
414 struct ExecParameters {
415 RuntimeScope runtime_scope;
416
417 char **environment;
418
419 int *fds;
420 char **fd_names;
421 size_t n_socket_fds;
422 size_t n_storage_fds;
423
424 ExecFlags flags;
425 bool selinux_context_net:1;
426
427 CGroupMask cgroup_supported;
428 char *cgroup_path;
429 uint64_t cgroup_id;
430
431 char **prefix;
432 char *received_credentials_directory;
433 char *received_encrypted_credentials_directory;
434
435 char *confirm_spawn;
436 bool shall_confirm_spawn;
437
438 usec_t watchdog_usec;
439
440 int *idle_pipe;
441
442 int stdin_fd;
443 int stdout_fd;
444 int stderr_fd;
445
446 /* An fd that is closed by the execve(), and thus will result in EOF when the execve() is done. */
447 int exec_fd;
448
449 char *notify_socket;
450
451 LIST_HEAD(OpenFile, open_files);
452
453 char *fallback_smack_process_label;
454
455 char **files_env;
456 int user_lookup_fd;
457 int handoff_timestamp_fd;
458
459 int bpf_restrict_fs_map_fd;
460
461 /* Used for logging in the executor functions */
462 char *unit_id;
463 sd_id128_t invocation_id;
464 char invocation_id_string[SD_ID128_STRING_MAX];
465 };
466
467 #define EXEC_PARAMETERS_INIT(_flags) \
468 (ExecParameters) { \
469 .flags = (_flags), \
470 .stdin_fd = -EBADF, \
471 .stdout_fd = -EBADF, \
472 .stderr_fd = -EBADF, \
473 .exec_fd = -EBADF, \
474 .bpf_restrict_fs_map_fd = -EBADF, \
475 .user_lookup_fd = -EBADF, \
476 .handoff_timestamp_fd = -EBADF, \
477 }
478
479 #include "unit.h"
480 #include "dynamic-user.h"
481
482 int exec_spawn(
483 Unit *unit,
484 ExecCommand *command,
485 const ExecContext *context,
486 ExecParameters *exec_params,
487 ExecRuntime *runtime,
488 const CGroupContext *cgroup_context,
489 PidRef *ret);
490
491 void exec_command_done(ExecCommand *c);
492 void exec_command_done_array(ExecCommand *c, size_t n);
493 ExecCommand* exec_command_free(ExecCommand *c);
494 DEFINE_TRIVIAL_CLEANUP_FUNC(ExecCommand*, exec_command_free);
495 ExecCommand* exec_command_free_list(ExecCommand *c);
496 void exec_command_free_array(ExecCommand **c, size_t n);
497 void exec_command_reset_status_array(ExecCommand *c, size_t n);
498 void exec_command_reset_status_list_array(ExecCommand **c, size_t n);
499
500 void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix);
501 void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix);
502 void exec_command_append_list(ExecCommand **l, ExecCommand *e);
503 int exec_command_set(ExecCommand *c, const char *path, ...) _sentinel_;
504 int exec_command_append(ExecCommand *c, const char *path, ...) _sentinel_;
505
506 void exec_context_init(ExecContext *c);
507 void exec_context_done(ExecContext *c);
508 void exec_context_dump(const ExecContext *c, FILE* f, const char *prefix);
509
510 int exec_context_destroy_runtime_directory(const ExecContext *c, const char *runtime_root);
511 int exec_context_destroy_mount_ns_dir(Unit *u);
512
513 const char* exec_context_fdname(const ExecContext *c, int fd_index);
514
515 bool exec_context_may_touch_console(const ExecContext *c);
516 bool exec_context_maintains_privileges(const ExecContext *c);
517
518 int exec_context_get_effective_ioprio(const ExecContext *c);
519 bool exec_context_get_effective_mount_apivfs(const ExecContext *c);
520
521 void exec_context_free_log_extra_fields(ExecContext *c);
522
523 void exec_context_revert_tty(ExecContext *c);
524
525 int exec_context_get_clean_directories(ExecContext *c, char **prefix, ExecCleanMask mask, char ***ret);
526 int exec_context_get_clean_mask(ExecContext *c, ExecCleanMask *ret);
527
528 const char *exec_context_tty_path(const ExecContext *context);
529 int exec_context_apply_tty_size(const ExecContext *context, int tty_fd, const char *tty_path);
530 void exec_context_tty_reset(const ExecContext *context, const ExecParameters *p);
531
532 uint64_t exec_context_get_rlimit(const ExecContext *c, const char *name);
533 int exec_context_get_oom_score_adjust(const ExecContext *c);
534 uint64_t exec_context_get_coredump_filter(const ExecContext *c);
535 int exec_context_get_nice(const ExecContext *c);
536 int exec_context_get_cpu_sched_policy(const ExecContext *c);
537 int exec_context_get_cpu_sched_priority(const ExecContext *c);
538 uint64_t exec_context_get_timer_slack_nsec(const ExecContext *c);
539 bool exec_context_get_set_login_environment(const ExecContext *c);
540 char** exec_context_get_syscall_filter(const ExecContext *c);
541 char** exec_context_get_syscall_archs(const ExecContext *c);
542 char** exec_context_get_syscall_log(const ExecContext *c);
543 char** exec_context_get_address_families(const ExecContext *c);
544 char** exec_context_get_restrict_filesystems(const ExecContext *c);
545
546 void exec_status_start(ExecStatus *s, pid_t pid, const dual_timestamp *ts);
547 void exec_status_exit(ExecStatus *s, const ExecContext *context, pid_t pid, int code, int status);
548 void exec_status_handoff(ExecStatus *s, const struct ucred *ucred, const dual_timestamp *ts);
549 void exec_status_dump(const ExecStatus *s, FILE *f, const char *prefix);
550 void exec_status_reset(ExecStatus *s);
551
552 int exec_shared_runtime_acquire(Manager *m, const ExecContext *c, const char *name, bool create, ExecSharedRuntime **ret);
553 ExecSharedRuntime *exec_shared_runtime_destroy(ExecSharedRuntime *r);
554 ExecSharedRuntime *exec_shared_runtime_unref(ExecSharedRuntime *r);
555 DEFINE_TRIVIAL_CLEANUP_FUNC(ExecSharedRuntime*, exec_shared_runtime_unref);
556
557 int exec_shared_runtime_serialize(const Manager *m, FILE *f, FDSet *fds);
558 int exec_shared_runtime_deserialize_compat(Unit *u, const char *key, const char *value, FDSet *fds);
559 int exec_shared_runtime_deserialize_one(Manager *m, const char *value, FDSet *fds);
560 void exec_shared_runtime_done(ExecSharedRuntime *rt);
561 void exec_shared_runtime_vacuum(Manager *m);
562
563 int exec_runtime_make(const Unit *unit, const ExecContext *context, ExecSharedRuntime *shared, DynamicCreds *creds, ExecRuntime **ret);
564 ExecRuntime* exec_runtime_free(ExecRuntime *rt);
565 DEFINE_TRIVIAL_CLEANUP_FUNC(ExecRuntime*, exec_runtime_free);
566 ExecRuntime* exec_runtime_destroy(ExecRuntime *rt);
567 void exec_runtime_clear(ExecRuntime *rt);
568
569 int exec_params_get_cgroup_path(const ExecParameters *params, const CGroupContext *c, char **ret);
570 void exec_params_shallow_clear(ExecParameters *p);
571 void exec_params_dump(const ExecParameters *p, FILE* f, const char *prefix);
572 void exec_params_deep_clear(ExecParameters *p);
573
574 bool exec_context_get_cpu_affinity_from_numa(const ExecContext *c);
575
576 void exec_directory_done(ExecDirectory *d);
577 int exec_directory_add(ExecDirectory *d, const char *path, const char *symlink);
578 void exec_directory_sort(ExecDirectory *d);
579 bool exec_directory_is_private(const ExecContext *context, ExecDirectoryType type);
580
581 ExecCleanMask exec_clean_mask_from_string(const char *s);
582
583 const char* exec_output_to_string(ExecOutput i) _const_;
584 ExecOutput exec_output_from_string(const char *s) _pure_;
585
586 const char* exec_input_to_string(ExecInput i) _const_;
587 ExecInput exec_input_from_string(const char *s) _pure_;
588
589 const char* exec_utmp_mode_to_string(ExecUtmpMode i) _const_;
590 ExecUtmpMode exec_utmp_mode_from_string(const char *s) _pure_;
591
592 const char* exec_preserve_mode_to_string(ExecPreserveMode i) _const_;
593 ExecPreserveMode exec_preserve_mode_from_string(const char *s) _pure_;
594
595 const char* exec_keyring_mode_to_string(ExecKeyringMode i) _const_;
596 ExecKeyringMode exec_keyring_mode_from_string(const char *s) _pure_;
597
598 const char* exec_directory_type_to_string(ExecDirectoryType i) _const_;
599 ExecDirectoryType exec_directory_type_from_string(const char *s) _pure_;
600
601 const char* exec_directory_type_symlink_to_string(ExecDirectoryType i) _const_;
602 ExecDirectoryType exec_directory_type_symlink_from_string(const char *s) _pure_;
603
604 const char* exec_directory_type_mode_to_string(ExecDirectoryType i) _const_;
605 ExecDirectoryType exec_directory_type_mode_from_string(const char *s) _pure_;
606
607 const char* exec_resource_type_to_string(ExecDirectoryType i) _const_;
608 ExecDirectoryType exec_resource_type_from_string(const char *s) _pure_;
609
610 bool exec_needs_mount_namespace(const ExecContext *context, const ExecParameters *params, const ExecRuntime *runtime);
611 bool exec_needs_network_namespace(const ExecContext *context);
612 bool exec_needs_ipc_namespace(const ExecContext *context);
613
614 /* These logging macros do the same logging as those in unit.h, but using ExecContext and ExecParameters
615 * instead of the unit object, so that it can be used in the sd-executor context (where the unit object is
616 * not available). */
617
618 #define LOG_EXEC_ID_FIELD(ep) \
619 ((ep)->runtime_scope == RUNTIME_SCOPE_USER ? "USER_UNIT=" : "UNIT=")
620 #define LOG_EXEC_ID_FIELD_FORMAT(ep) \
621 ((ep)->runtime_scope == RUNTIME_SCOPE_USER ? "USER_UNIT=%s" : "UNIT=%s")
622 #define LOG_EXEC_INVOCATION_ID_FIELD(ep) \
623 ((ep)->runtime_scope == RUNTIME_SCOPE_USER ? "USER_INVOCATION_ID=" : "INVOCATION_ID=")
624 #define LOG_EXEC_INVOCATION_ID_FIELD_FORMAT(ep) \
625 ((ep)->runtime_scope == RUNTIME_SCOPE_USER ? "USER_INVOCATION_ID=%s" : "INVOCATION_ID=%s")
626
627 #define log_exec_full_errno_zerook(ec, ep, level, error, ...) \
628 ({ \
629 const ExecContext *_c = (ec); \
630 const ExecParameters *_p = (ep); \
631 const int _l = (level); \
632 bool _do_log = _c->log_level_max < 0 || \
633 _c->log_level_max >= LOG_PRI(_l); \
634 LOG_CONTEXT_PUSH_IOV(_c->log_extra_fields, \
635 _c->n_log_extra_fields); \
636 !_do_log ? -ERRNO_VALUE(error) : \
637 log_object_internal(_l, error, \
638 PROJECT_FILE, __LINE__, __func__, \
639 LOG_EXEC_ID_FIELD(_p), \
640 _p->unit_id, \
641 LOG_EXEC_INVOCATION_ID_FIELD(_p), \
642 _p->invocation_id_string, \
643 ##__VA_ARGS__); \
644 })
645
646 #define log_exec_full_errno(ec, ep, level, error, ...) \
647 ({ \
648 int _error = (error); \
649 ASSERT_NON_ZERO(_error); \
650 log_exec_full_errno_zerook(ec, ep, level, _error, ##__VA_ARGS__); \
651 })
652
653 #define log_exec_full(ec, ep, level, ...) (void) log_exec_full_errno_zerook(ec, ep, level, 0, __VA_ARGS__)
654
655 #define log_exec_debug(ec, ep, ...) log_exec_full(ec, ep, LOG_DEBUG, __VA_ARGS__)
656 #define log_exec_info(ec, ep, ...) log_exec_full(ec, ep, LOG_INFO, __VA_ARGS__)
657 #define log_exec_notice(ec, ep, ...) log_exec_full(ec, ep, LOG_NOTICE, __VA_ARGS__)
658 #define log_exec_warning(ec, ep, ...) log_exec_full(ec, ep, LOG_WARNING, __VA_ARGS__)
659 #define log_exec_error(ec, ep, ...) log_exec_full(ec, ep, LOG_ERR, __VA_ARGS__)
660
661 #define log_exec_debug_errno(ec, ep, error, ...) log_exec_full_errno(ec, ep, LOG_DEBUG, error, __VA_ARGS__)
662 #define log_exec_info_errno(ec, ep, error, ...) log_exec_full_errno(ec, ep, LOG_INFO, error, __VA_ARGS__)
663 #define log_exec_notice_errno(ec, ep, error, ...) log_exec_full_errno(ec, ep, LOG_NOTICE, error, __VA_ARGS__)
664 #define log_exec_warning_errno(ec, ep, error, ...) log_exec_full_errno(ec, ep, LOG_WARNING, error, __VA_ARGS__)
665 #define log_exec_error_errno(ec, ep, error, ...) log_exec_full_errno(ec, ep, LOG_ERR, error, __VA_ARGS__)
666
667 /* Like LOG_MESSAGE(), but with the unit name prefixed. */
668 #define LOG_EXEC_MESSAGE(ep, fmt, ...) LOG_MESSAGE("%s: " fmt, (ep)->unit_id, ##__VA_ARGS__)
669 #define LOG_EXEC_ID(ep) LOG_EXEC_ID_FIELD_FORMAT(ep), (ep)->unit_id
670 #define LOG_EXEC_INVOCATION_ID(ep) LOG_EXEC_INVOCATION_ID_FIELD_FORMAT(ep), (ep)->invocation_id_string
671
672 #define log_exec_struct_errno(ec, ep, level, error, ...) \
673 ({ \
674 const ExecContext *_c = (ec); \
675 const ExecParameters *_p = (ep); \
676 const int _l = (level); \
677 bool _do_log = _c->log_level_max < 0 || \
678 _c->log_level_max >= LOG_PRI(_l); \
679 LOG_CONTEXT_PUSH_IOV(_c->log_extra_fields, \
680 _c->n_log_extra_fields); \
681 !_do_log ? -ERRNO_VALUE(error) : \
682 log_struct_errno(_l, error, \
683 LOG_EXEC_ID(_p), \
684 LOG_EXEC_INVOCATION_ID(_p), \
685 __VA_ARGS__); \
686 })
687
688 #define log_exec_struct(ec, ep, level, ...) log_exec_struct_errno(ec, ep, level, 0, __VA_ARGS__)
689
690 #define _LOG_CONTEXT_PUSH_EXEC(ec, ep, p, c) \
691 const ExecContext *c = (ec); \
692 const ExecParameters *p = (ep); \
693 LOG_CONTEXT_PUSH_KEY_VALUE(LOG_EXEC_ID_FIELD(p), p->unit_id); \
694 LOG_CONTEXT_PUSH_KEY_VALUE(LOG_EXEC_INVOCATION_ID_FIELD(p), p->invocation_id_string); \
695 LOG_CONTEXT_PUSH_IOV(c->log_extra_fields, c->n_log_extra_fields)
696
697 #define LOG_CONTEXT_PUSH_EXEC(ec, ep) \
698 _LOG_CONTEXT_PUSH_EXEC(ec, ep, UNIQ_T(p, UNIQ), UNIQ_T(c, UNIQ))