]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/execute.h
execute: add new helper exec_context_apply_tty_size()
[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 pid_t pid;
95 int code; /* as in siginfo_t::si_code */
96 int status; /* as in siginfo_t::si_status */
97 };
98
99 /* Stores information about commands we execute. Covers both configuration settings as well as runtime data. */
100 struct ExecCommand {
101 char *path;
102 char **argv;
103 ExecStatus exec_status; /* Note that this is not serialized to sd-executor */
104 ExecCommandFlags flags;
105 LIST_FIELDS(ExecCommand, command); /* useful for chaining commands */
106 };
107
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. */
112 struct ExecSharedRuntime {
113 unsigned n_ref;
114
115 Manager *manager;
116
117 char *id; /* Unit id of the owner */
118
119 char *tmp_dir;
120 char *var_tmp_dir;
121
122 /* An AF_UNIX socket pair, that contains a datagram containing a file descriptor referring to the network
123 * namespace. */
124 int netns_storage_socket[2];
125
126 /* Like netns_storage_socket, but the file descriptor is referring to the IPC namespace. */
127 int ipcns_storage_socket[2];
128 };
129
130 struct ExecRuntime {
131 ExecSharedRuntime *shared;
132 DynamicCreds *dynamic_creds;
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];
141 };
142
143 typedef enum ExecDirectoryType {
144 EXEC_DIRECTORY_RUNTIME,
145 EXEC_DIRECTORY_STATE,
146 EXEC_DIRECTORY_CACHE,
147 EXEC_DIRECTORY_LOGS,
148 EXEC_DIRECTORY_CONFIGURATION,
149 _EXEC_DIRECTORY_TYPE_MAX,
150 _EXEC_DIRECTORY_TYPE_INVALID = -EINVAL,
151 } ExecDirectoryType;
152
153 typedef struct ExecDirectoryItem {
154 char *path;
155 char **symlinks;
156 bool only_create;
157 } ExecDirectoryItem;
158
159 typedef struct ExecDirectory {
160 mode_t mode;
161 size_t n_items;
162 ExecDirectoryItem *items;
163 } ExecDirectory;
164
165 typedef 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,
173 EXEC_CLEAN_FDSTORE = 1U << _EXEC_DIRECTORY_TYPE_MAX,
174 EXEC_CLEAN_NONE = 0,
175 EXEC_CLEAN_ALL = (1U << (_EXEC_DIRECTORY_TYPE_MAX+1)) - 1,
176 _EXEC_CLEAN_MASK_INVALID = -EINVAL,
177 } ExecCleanMask;
178
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. */
182 struct ExecContext {
183 char **environment;
184 char **environment_files;
185 char **pass_environment;
186 char **unset_environment;
187
188 struct rlimit *rlimit[_RLIMIT_MAX];
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;
192 LIST_HEAD(MountOptions, root_image_options);
193 bool root_ephemeral;
194 bool working_directory_missing_ok:1;
195 bool working_directory_home:1;
196
197 bool oom_score_adjust_set:1;
198 bool coredump_filter_set:1;
199 bool nice_set:1;
200 bool ioprio_set:1;
201 bool cpu_sched_set:1;
202 bool mount_apivfs_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 bool stdio_as_fds;
228 char *stdio_fdname[3];
229 char *stdio_file[3];
230
231 void *stdin_data;
232 size_t stdin_data_size;
233
234 nsec_t timer_slack_nsec;
235
236 char *tty_path;
237
238 bool tty_reset;
239 bool tty_vhangup;
240 bool tty_vt_disallocate;
241
242 unsigned tty_rows;
243 unsigned tty_cols;
244
245 bool ignore_sigpipe;
246
247 ExecKeyringMode keyring_mode;
248
249 /* Since resolving these names might involve socket
250 * connections and we don't want to deadlock ourselves these
251 * names are resolved on execution only and in the child
252 * process. */
253 char *user;
254 char *group;
255 char **supplementary_groups;
256
257 int set_login_environment;
258
259 char *pam_name;
260
261 char *utmp_id;
262 ExecUtmpMode utmp_mode;
263
264 bool no_new_privileges;
265
266 bool selinux_context_ignore;
267 bool apparmor_profile_ignore;
268 bool smack_process_label_ignore;
269
270 char *selinux_context;
271 char *apparmor_profile;
272 char *smack_process_label;
273
274 char **read_write_paths, **read_only_paths, **inaccessible_paths, **exec_paths, **no_exec_paths;
275 char **exec_search_path;
276 unsigned long mount_propagation_flag;
277 BindMount *bind_mounts;
278 size_t n_bind_mounts;
279 TemporaryFileSystem *temporary_filesystems;
280 size_t n_temporary_filesystems;
281 MountImage *mount_images;
282 size_t n_mount_images;
283 MountImage *extension_images;
284 size_t n_extension_images;
285 char **extension_directories;
286
287 uint64_t capability_bounding_set;
288 uint64_t capability_ambient_set;
289 int secure_bits;
290
291 int syslog_priority;
292 bool syslog_level_prefix;
293 char *syslog_identifier;
294
295 struct iovec* log_extra_fields;
296 size_t n_log_extra_fields;
297 Set *log_filter_allowed_patterns;
298 Set *log_filter_denied_patterns;
299
300 usec_t log_ratelimit_interval_usec;
301 unsigned log_ratelimit_burst;
302
303 int log_level_max;
304
305 char *log_namespace;
306
307 ProtectProc protect_proc; /* hidepid= */
308 ProcSubset proc_subset; /* subset= */
309
310 int private_mounts;
311 int memory_ksm;
312 bool private_tmp;
313 bool private_network;
314 bool private_devices;
315 bool private_users;
316 bool private_ipc;
317 bool protect_kernel_tunables;
318 bool protect_kernel_modules;
319 bool protect_kernel_logs;
320 bool protect_clock;
321 bool protect_control_groups;
322 ProtectSystem protect_system;
323 ProtectHome protect_home;
324 bool protect_hostname;
325 bool mount_apivfs;
326
327 bool dynamic_user;
328 bool remove_ipc;
329
330 bool memory_deny_write_execute;
331 bool restrict_realtime;
332 bool restrict_suid_sgid;
333
334 bool lock_personality;
335 unsigned long personality;
336
337 unsigned long restrict_namespaces; /* The CLONE_NEWxyz flags permitted to the unit's processes */
338
339 Set *restrict_filesystems;
340 bool restrict_filesystems_allow_list:1;
341
342 Hashmap *syscall_filter;
343 Set *syscall_archs;
344 int syscall_errno;
345 bool syscall_allow_list:1;
346
347 Hashmap *syscall_log;
348 bool syscall_log_allow_list:1; /* Log listed system calls */
349
350 bool address_families_allow_list:1;
351 Set *address_families;
352
353 char *network_namespace_path;
354 char *ipc_namespace_path;
355
356 ExecDirectory directories[_EXEC_DIRECTORY_TYPE_MAX];
357 ExecPreserveMode runtime_directory_preserve_mode;
358 usec_t timeout_clean_usec;
359
360 Hashmap *set_credentials; /* output id → ExecSetCredential */
361 Hashmap *load_credentials; /* output id → ExecLoadCredential */
362 Set *import_credentials;
363
364 ImagePolicy *root_image_policy, *mount_image_policy, *extension_image_policy;
365 };
366
367 static inline bool exec_context_restrict_namespaces_set(const ExecContext *c) {
368 assert(c);
369
370 return (c->restrict_namespaces & NAMESPACE_FLAGS_ALL) != NAMESPACE_FLAGS_ALL;
371 }
372
373 static inline bool exec_context_restrict_filesystems_set(const ExecContext *c) {
374 assert(c);
375
376 return c->restrict_filesystems_allow_list ||
377 !set_isempty(c->restrict_filesystems);
378 }
379
380 static inline bool exec_context_with_rootfs(const ExecContext *c) {
381 assert(c);
382
383 /* Checks if RootDirectory= or RootImage= are used */
384
385 return !empty_or_root(c->root_directory) || c->root_image;
386 }
387
388 typedef enum ExecFlags {
389 EXEC_APPLY_SANDBOXING = 1 << 0,
390 EXEC_APPLY_CHROOT = 1 << 1,
391 EXEC_APPLY_TTY_STDIN = 1 << 2,
392 EXEC_PASS_LOG_UNIT = 1 << 3, /* Whether to pass the unit name to the service's journal stream connection */
393 EXEC_CHOWN_DIRECTORIES = 1 << 4, /* chown() the runtime/state/cache/log directories to the user we run as, under all conditions */
394 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 */
395 EXEC_CGROUP_DELEGATE = 1 << 6,
396 EXEC_IS_CONTROL = 1 << 7,
397 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 */
398 EXEC_WRITE_CREDENTIALS = 1 << 9, /* Set up the credential store logic */
399
400 /* The following are not used by execute.c, but by consumers internally */
401 EXEC_PASS_FDS = 1 << 10,
402 EXEC_SETENV_RESULT = 1 << 11,
403 EXEC_SET_WATCHDOG = 1 << 12,
404 EXEC_SETENV_MONITOR_RESULT = 1 << 13, /* Pass exit status to OnFailure= and OnSuccess= dependencies. */
405 } ExecFlags;
406
407 /* Parameters for a specific invocation of a command. This structure is put together right before a command is
408 * executed. */
409 struct ExecParameters {
410 RuntimeScope runtime_scope;
411
412 char **environment;
413
414 int *fds;
415 char **fd_names;
416 size_t n_socket_fds;
417 size_t n_storage_fds;
418
419 ExecFlags flags;
420 bool selinux_context_net:1;
421
422 CGroupMask cgroup_supported;
423 char *cgroup_path;
424 uint64_t cgroup_id;
425
426 char **prefix;
427 char *received_credentials_directory;
428 char *received_encrypted_credentials_directory;
429
430 char *confirm_spawn;
431 bool shall_confirm_spawn;
432
433 usec_t watchdog_usec;
434
435 int *idle_pipe;
436
437 int stdin_fd;
438 int stdout_fd;
439 int stderr_fd;
440
441 /* An fd that is closed by the execve(), and thus will result in EOF when the execve() is done */
442 int exec_fd;
443
444 char *notify_socket;
445
446 LIST_HEAD(OpenFile, open_files);
447
448 char *fallback_smack_process_label;
449
450 char **files_env;
451 int user_lookup_fd;
452 int bpf_outer_map_fd;
453
454 /* Used for logging in the executor functions */
455 char *unit_id;
456 sd_id128_t invocation_id;
457 char invocation_id_string[SD_ID128_STRING_MAX];
458 };
459
460 #define EXEC_PARAMETERS_INIT(_flags) \
461 (ExecParameters) { \
462 .flags = (_flags), \
463 .stdin_fd = -EBADF, \
464 .stdout_fd = -EBADF, \
465 .stderr_fd = -EBADF, \
466 .exec_fd = -EBADF, \
467 .bpf_outer_map_fd = -EBADF, \
468 .user_lookup_fd = -EBADF, \
469 };
470
471 #include "unit.h"
472 #include "dynamic-user.h"
473
474 int exec_spawn(Unit *unit,
475 ExecCommand *command,
476 const ExecContext *context,
477 ExecParameters *exec_params,
478 ExecRuntime *runtime,
479 const CGroupContext *cgroup_context,
480 pid_t *ret);
481
482 void exec_command_done(ExecCommand *c);
483 void exec_command_done_array(ExecCommand *c, size_t n);
484 ExecCommand* exec_command_free_list(ExecCommand *c);
485 void exec_command_free_array(ExecCommand **c, size_t n);
486 void exec_command_reset_status_array(ExecCommand *c, size_t n);
487 void exec_command_reset_status_list_array(ExecCommand **c, size_t n);
488 void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix);
489 void exec_command_append_list(ExecCommand **l, ExecCommand *e);
490 int exec_command_set(ExecCommand *c, const char *path, ...) _sentinel_;
491 int exec_command_append(ExecCommand *c, const char *path, ...) _sentinel_;
492
493 void exec_context_init(ExecContext *c);
494 void exec_context_done(ExecContext *c);
495 void exec_context_dump(const ExecContext *c, FILE* f, const char *prefix);
496
497 int exec_context_destroy_runtime_directory(const ExecContext *c, const char *runtime_root);
498 int exec_context_destroy_mount_ns_dir(Unit *u);
499
500 const char* exec_context_fdname(const ExecContext *c, int fd_index);
501
502 bool exec_context_may_touch_console(const ExecContext *c);
503 bool exec_context_maintains_privileges(const ExecContext *c);
504
505 int exec_context_get_effective_ioprio(const ExecContext *c);
506 bool exec_context_get_effective_mount_apivfs(const ExecContext *c);
507
508 void exec_context_free_log_extra_fields(ExecContext *c);
509
510 void exec_context_revert_tty(ExecContext *c);
511
512 int exec_context_get_clean_directories(ExecContext *c, char **prefix, ExecCleanMask mask, char ***ret);
513 int exec_context_get_clean_mask(ExecContext *c, ExecCleanMask *ret);
514
515 const char *exec_context_tty_path(const ExecContext *context);
516 int exec_context_apply_tty_size(const ExecContext *context, int tty_fd, const char *tty_path);
517 void exec_context_tty_reset(const ExecContext *context, const ExecParameters *p);
518
519 uint64_t exec_context_get_rlimit(const ExecContext *c, const char *name);
520 int exec_context_get_oom_score_adjust(const ExecContext *c);
521 uint64_t exec_context_get_coredump_filter(const ExecContext *c);
522 int exec_context_get_nice(const ExecContext *c);
523 int exec_context_get_cpu_sched_policy(const ExecContext *c);
524 int exec_context_get_cpu_sched_priority(const ExecContext *c);
525 uint64_t exec_context_get_timer_slack_nsec(const ExecContext *c);
526 char** exec_context_get_syscall_filter(const ExecContext *c);
527 char** exec_context_get_syscall_archs(const ExecContext *c);
528 char** exec_context_get_syscall_log(const ExecContext *c);
529 char** exec_context_get_address_families(const ExecContext *c);
530 char** exec_context_get_restrict_filesystems(const ExecContext *c);
531
532 void exec_status_start(ExecStatus *s, pid_t pid);
533 void exec_status_exit(ExecStatus *s, const ExecContext *context, pid_t pid, int code, int status);
534 void exec_status_dump(const ExecStatus *s, FILE *f, const char *prefix);
535 void exec_status_reset(ExecStatus *s);
536
537 int exec_shared_runtime_acquire(Manager *m, const ExecContext *c, const char *name, bool create, ExecSharedRuntime **ret);
538 ExecSharedRuntime *exec_shared_runtime_destroy(ExecSharedRuntime *r);
539 ExecSharedRuntime *exec_shared_runtime_unref(ExecSharedRuntime *r);
540 DEFINE_TRIVIAL_CLEANUP_FUNC(ExecSharedRuntime*, exec_shared_runtime_unref);
541
542 int exec_shared_runtime_serialize(const Manager *m, FILE *f, FDSet *fds);
543 int exec_shared_runtime_deserialize_compat(Unit *u, const char *key, const char *value, FDSet *fds);
544 int exec_shared_runtime_deserialize_one(Manager *m, const char *value, FDSet *fds);
545 void exec_shared_runtime_done(ExecSharedRuntime *rt);
546 void exec_shared_runtime_vacuum(Manager *m);
547
548 int exec_runtime_make(const Unit *unit, const ExecContext *context, ExecSharedRuntime *shared, DynamicCreds *creds, ExecRuntime **ret);
549 ExecRuntime* exec_runtime_free(ExecRuntime *rt);
550 DEFINE_TRIVIAL_CLEANUP_FUNC(ExecRuntime*, exec_runtime_free);
551 ExecRuntime* exec_runtime_destroy(ExecRuntime *rt);
552 void exec_runtime_clear(ExecRuntime *rt);
553
554 int exec_params_get_cgroup_path(const ExecParameters *params, const CGroupContext *c, char **ret);
555 void exec_params_shallow_clear(ExecParameters *p);
556 void exec_params_dump(const ExecParameters *p, FILE* f, const char *prefix);
557 void exec_params_deep_clear(ExecParameters *p);
558
559 bool exec_context_get_cpu_affinity_from_numa(const ExecContext *c);
560
561 void exec_directory_done(ExecDirectory *d);
562 int exec_directory_add(ExecDirectory *d, const char *path, const char *symlink);
563 void exec_directory_sort(ExecDirectory *d);
564 bool exec_directory_is_private(const ExecContext *context, ExecDirectoryType type);
565
566 ExecCleanMask exec_clean_mask_from_string(const char *s);
567
568 const char* exec_output_to_string(ExecOutput i) _const_;
569 ExecOutput exec_output_from_string(const char *s) _pure_;
570
571 const char* exec_input_to_string(ExecInput i) _const_;
572 ExecInput exec_input_from_string(const char *s) _pure_;
573
574 const char* exec_utmp_mode_to_string(ExecUtmpMode i) _const_;
575 ExecUtmpMode exec_utmp_mode_from_string(const char *s) _pure_;
576
577 const char* exec_preserve_mode_to_string(ExecPreserveMode i) _const_;
578 ExecPreserveMode exec_preserve_mode_from_string(const char *s) _pure_;
579
580 const char* exec_keyring_mode_to_string(ExecKeyringMode i) _const_;
581 ExecKeyringMode exec_keyring_mode_from_string(const char *s) _pure_;
582
583 const char* exec_directory_type_to_string(ExecDirectoryType i) _const_;
584 ExecDirectoryType exec_directory_type_from_string(const char *s) _pure_;
585
586 const char* exec_directory_type_symlink_to_string(ExecDirectoryType i) _const_;
587 ExecDirectoryType exec_directory_type_symlink_from_string(const char *s) _pure_;
588
589 const char* exec_directory_type_mode_to_string(ExecDirectoryType i) _const_;
590 ExecDirectoryType exec_directory_type_mode_from_string(const char *s) _pure_;
591
592 const char* exec_resource_type_to_string(ExecDirectoryType i) _const_;
593 ExecDirectoryType exec_resource_type_from_string(const char *s) _pure_;
594
595 bool exec_needs_mount_namespace(const ExecContext *context, const ExecParameters *params, const ExecRuntime *runtime);
596 bool exec_needs_network_namespace(const ExecContext *context);
597 bool exec_needs_ipc_namespace(const ExecContext *context);
598
599 /* These logging macros do the same logging as those in unit.h, but using ExecContext and ExecParameters
600 * instead of the unit object, so that it can be used in the sd-executor context (where the unit object is
601 * not available). */
602
603 #define LOG_EXEC_ID_FIELD(ep) \
604 ((ep)->runtime_scope == RUNTIME_SCOPE_USER ? "USER_UNIT=" : "UNIT=")
605 #define LOG_EXEC_ID_FIELD_FORMAT(ep) \
606 ((ep)->runtime_scope == RUNTIME_SCOPE_USER ? "USER_UNIT=%s" : "UNIT=%s")
607 #define LOG_EXEC_INVOCATION_ID_FIELD(ep) \
608 ((ep)->runtime_scope == RUNTIME_SCOPE_USER ? "USER_INVOCATION_ID=" : "INVOCATION_ID=")
609 #define LOG_EXEC_INVOCATION_ID_FIELD_FORMAT(ep) \
610 ((ep)->runtime_scope == RUNTIME_SCOPE_USER ? "USER_INVOCATION_ID=%s" : "INVOCATION_ID=%s")
611
612 #define log_exec_full_errno_zerook(ec, ep, level, error, ...) \
613 ({ \
614 const ExecContext *_c = (ec); \
615 const ExecParameters *_p = (ep); \
616 const int _l = (level); \
617 bool _do_log = !(log_get_max_level() < LOG_PRI(_l) || \
618 !(_c->log_level_max < 0 || \
619 _c->log_level_max >= LOG_PRI(_l))); \
620 LOG_CONTEXT_PUSH_IOV(_c->log_extra_fields, \
621 _c->n_log_extra_fields); \
622 !_do_log ? -ERRNO_VALUE(error) : \
623 log_object_internal(_l, error, PROJECT_FILE, \
624 __LINE__, __func__, \
625 LOG_EXEC_ID_FIELD(_p), \
626 _p->unit_id, \
627 LOG_EXEC_INVOCATION_ID_FIELD(_p), \
628 _p->invocation_id_string, ##__VA_ARGS__); \
629 })
630
631 #define log_exec_full_errno(ec, ep, level, error, ...) \
632 ({ \
633 int _error = (error); \
634 ASSERT_NON_ZERO(_error); \
635 log_exec_full_errno_zerook(ec, ep, level, _error, ##__VA_ARGS__); \
636 })
637
638 #define log_exec_full(ec, ep, level, ...) (void) log_exec_full_errno_zerook(ec, ep, level, 0, __VA_ARGS__)
639
640 #define log_exec_debug(ec, ep, ...) log_exec_full(ec, ep, LOG_DEBUG, __VA_ARGS__)
641 #define log_exec_info(ec, ep, ...) log_exec_full(ec, ep, LOG_INFO, __VA_ARGS__)
642 #define log_exec_notice(ec, ep, ...) log_exec_full(ec, ep, LOG_NOTICE, __VA_ARGS__)
643 #define log_exec_warning(ec, ep, ...) log_exec_full(ec, ep, LOG_WARNING, __VA_ARGS__)
644 #define log_exec_error(ec, ep, ...) log_exec_full(ec, ep, LOG_ERR, __VA_ARGS__)
645
646 #define log_exec_debug_errno(ec, ep, error, ...) log_exec_full_errno(ec, ep, LOG_DEBUG, error, __VA_ARGS__)
647 #define log_exec_info_errno(ec, ep, error, ...) log_exec_full_errno(ec, ep, LOG_INFO, error, __VA_ARGS__)
648 #define log_exec_notice_errno(ec, ep, error, ...) log_exec_full_errno(ec, ep, LOG_NOTICE, error, __VA_ARGS__)
649 #define log_exec_warning_errno(ec, ep, error, ...) log_exec_full_errno(ec, ep, LOG_WARNING, error, __VA_ARGS__)
650 #define log_exec_error_errno(ec, ep, error, ...) log_exec_full_errno(ec, ep, LOG_ERR, error, __VA_ARGS__)
651
652 #define log_exec_struct_errno(ec, ep, level, error, ...) \
653 ({ \
654 const ExecContext *_c = (ec); \
655 const ExecParameters *_p = (ep); \
656 const int _l = (level); \
657 bool _do_log = !(_c->log_level_max < 0 || \
658 _c->log_level_max >= LOG_PRI(_l)); \
659 LOG_CONTEXT_PUSH_IOV(_c->log_extra_fields, \
660 _c->n_log_extra_fields); \
661 _do_log ? \
662 log_struct_errno(_l, error, __VA_ARGS__, LOG_EXEC_ID_FIELD_FORMAT(_p), _p->unit_id) : \
663 -ERRNO_VALUE(error); \
664 })
665
666 #define log_exec_struct(ec, ep, level, ...) log_exec_struct_errno(ec, ep, level, 0, __VA_ARGS__)
667
668 #define log_exec_struct_iovec_errno(ec, ep, level, error, iovec, n_iovec) \
669 ({ \
670 const ExecContext *_c = (ec); \
671 const ExecParameters *_p = (ep); \
672 const int _l = (level); \
673 bool _do_log = !(_c->log_level_max < 0 || \
674 _c->log_level_max >= LOG_PRI(_l)); \
675 LOG_CONTEXT_PUSH_IOV(_c->log_extra_fields, \
676 _c->n_log_extra_fields); \
677 _do_log ? \
678 log_struct_iovec_errno(_l, error, iovec, n_iovec) : \
679 -ERRNO_VALUE(error); \
680 })
681
682 #define log_exec_struct_iovec(ec, ep, level, iovec, n_iovec) log_exec_struct_iovec_errno(ec, ep, level, 0, iovec, n_iovec)
683
684 /* Like LOG_MESSAGE(), but with the unit name prefixed. */
685 #define LOG_EXEC_MESSAGE(ep, fmt, ...) LOG_MESSAGE("%s: " fmt, (ep)->unit_id, ##__VA_ARGS__)
686 #define LOG_EXEC_ID(ep) LOG_EXEC_ID_FIELD_FORMAT(ep), (ep)->unit_id
687 #define LOG_EXEC_INVOCATION_ID(ep) LOG_EXEC_INVOCATION_ID_FIELD_FORMAT(ep), (ep)->invocation_id_string
688
689 #define _LOG_CONTEXT_PUSH_EXEC(ec, ep, p, c) \
690 const ExecContext *c = (ec); \
691 const ExecParameters *p = (ep); \
692 LOG_CONTEXT_PUSH_KEY_VALUE(LOG_EXEC_ID_FIELD(p), p->unit_id); \
693 LOG_CONTEXT_PUSH_KEY_VALUE(LOG_EXEC_INVOCATION_ID_FIELD(p), p->invocation_id_string); \
694 LOG_CONTEXT_PUSH_IOV(c->log_extra_fields, c->n_log_extra_fields)
695
696 #define LOG_CONTEXT_PUSH_EXEC(ec, ep) \
697 _LOG_CONTEXT_PUSH_EXEC(ec, ep, UNIQ_T(p, UNIQ), UNIQ_T(c, UNIQ))