]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/execute.h
Merge pull request #27020 from 1awesomeJ/nit
[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 ExecRuntime ExecRuntime;
8 typedef struct ExecParameters ExecParameters;
9 typedef struct Manager Manager;
10
11 #include <sched.h>
12 #include <stdbool.h>
13 #include <stdio.h>
14 #include <sys/capability.h>
15
16 #include "cgroup-util.h"
17 #include "coredump-util.h"
18 #include "cpu-set-util.h"
19 #include "exec-util.h"
20 #include "fdset.h"
21 #include "list.h"
22 #include "missing_resource.h"
23 #include "namespace.h"
24 #include "nsflags.h"
25 #include "numa-util.h"
26 #include "open-file.h"
27 #include "path-util.h"
28 #include "set.h"
29 #include "time-util.h"
30
31 #define EXEC_STDIN_DATA_MAX (64U*1024U*1024U)
32
33 typedef enum ExecUtmpMode {
34 EXEC_UTMP_INIT,
35 EXEC_UTMP_LOGIN,
36 EXEC_UTMP_USER,
37 _EXEC_UTMP_MODE_MAX,
38 _EXEC_UTMP_MODE_INVALID = -EINVAL,
39 } ExecUtmpMode;
40
41 typedef enum ExecInput {
42 EXEC_INPUT_NULL,
43 EXEC_INPUT_TTY,
44 EXEC_INPUT_TTY_FORCE,
45 EXEC_INPUT_TTY_FAIL,
46 EXEC_INPUT_SOCKET,
47 EXEC_INPUT_NAMED_FD,
48 EXEC_INPUT_DATA,
49 EXEC_INPUT_FILE,
50 _EXEC_INPUT_MAX,
51 _EXEC_INPUT_INVALID = -EINVAL,
52 } ExecInput;
53
54 typedef enum ExecOutput {
55 EXEC_OUTPUT_INHERIT,
56 EXEC_OUTPUT_NULL,
57 EXEC_OUTPUT_TTY,
58 EXEC_OUTPUT_KMSG,
59 EXEC_OUTPUT_KMSG_AND_CONSOLE,
60 EXEC_OUTPUT_JOURNAL,
61 EXEC_OUTPUT_JOURNAL_AND_CONSOLE,
62 EXEC_OUTPUT_SOCKET,
63 EXEC_OUTPUT_NAMED_FD,
64 EXEC_OUTPUT_FILE,
65 EXEC_OUTPUT_FILE_APPEND,
66 EXEC_OUTPUT_FILE_TRUNCATE,
67 _EXEC_OUTPUT_MAX,
68 _EXEC_OUTPUT_INVALID = -EINVAL,
69 } ExecOutput;
70
71 typedef enum ExecPreserveMode {
72 EXEC_PRESERVE_NO,
73 EXEC_PRESERVE_YES,
74 EXEC_PRESERVE_RESTART,
75 _EXEC_PRESERVE_MODE_MAX,
76 _EXEC_PRESERVE_MODE_INVALID = -EINVAL,
77 } ExecPreserveMode;
78
79 typedef enum ExecKeyringMode {
80 EXEC_KEYRING_INHERIT,
81 EXEC_KEYRING_PRIVATE,
82 EXEC_KEYRING_SHARED,
83 _EXEC_KEYRING_MODE_MAX,
84 _EXEC_KEYRING_MODE_INVALID = -EINVAL,
85 } ExecKeyringMode;
86
87 /* Contains start and exit information about an executed command. */
88 struct ExecStatus {
89 dual_timestamp start_timestamp;
90 dual_timestamp exit_timestamp;
91 pid_t pid;
92 int code; /* as in siginfo_t::si_code */
93 int status; /* as in siginfo_t::si_status */
94 };
95
96 /* Stores information about commands we execute. Covers both configuration settings as well as runtime data. */
97 struct ExecCommand {
98 char *path;
99 char **argv;
100 ExecStatus exec_status;
101 ExecCommandFlags flags;
102 LIST_FIELDS(ExecCommand, command); /* useful for chaining commands */
103 };
104
105 /* Encapsulates certain aspects of the runtime environment that is to be shared between multiple otherwise separate
106 * invocations of commands. Specifically, this allows sharing of /tmp and /var/tmp data as well as network namespaces
107 * between invocations of commands. This is a reference counted object, with one reference taken by each currently
108 * active command invocation that wants to share this runtime. */
109 struct ExecRuntime {
110 unsigned n_ref;
111
112 Manager *manager;
113
114 char *id; /* Unit id of the owner */
115
116 char *tmp_dir;
117 char *var_tmp_dir;
118
119 /* An AF_UNIX socket pair, that contains a datagram containing a file descriptor referring to the network
120 * namespace. */
121 int netns_storage_socket[2];
122
123 /* Like netns_storage_socket, but the file descriptor is referring to the IPC namespace. */
124 int ipcns_storage_socket[2];
125 };
126
127 typedef enum ExecDirectoryType {
128 EXEC_DIRECTORY_RUNTIME = 0,
129 EXEC_DIRECTORY_STATE,
130 EXEC_DIRECTORY_CACHE,
131 EXEC_DIRECTORY_LOGS,
132 EXEC_DIRECTORY_CONFIGURATION,
133 _EXEC_DIRECTORY_TYPE_MAX,
134 _EXEC_DIRECTORY_TYPE_INVALID = -EINVAL,
135 } ExecDirectoryType;
136
137 typedef struct ExecDirectoryItem {
138 char *path;
139 char **symlinks;
140 bool only_create;
141 } ExecDirectoryItem;
142
143 typedef struct ExecDirectory {
144 mode_t mode;
145 size_t n_items;
146 ExecDirectoryItem *items;
147 } ExecDirectory;
148
149 typedef enum ExecCleanMask {
150 /* In case you wonder why the bitmask below doesn't use "directory" in its name: we want to keep this
151 * generic so that .timer timestamp files can nicely be covered by this too, and similar. */
152 EXEC_CLEAN_RUNTIME = 1U << EXEC_DIRECTORY_RUNTIME,
153 EXEC_CLEAN_STATE = 1U << EXEC_DIRECTORY_STATE,
154 EXEC_CLEAN_CACHE = 1U << EXEC_DIRECTORY_CACHE,
155 EXEC_CLEAN_LOGS = 1U << EXEC_DIRECTORY_LOGS,
156 EXEC_CLEAN_CONFIGURATION = 1U << EXEC_DIRECTORY_CONFIGURATION,
157 EXEC_CLEAN_NONE = 0,
158 EXEC_CLEAN_ALL = (1U << _EXEC_DIRECTORY_TYPE_MAX) - 1,
159 _EXEC_CLEAN_MASK_INVALID = -EINVAL,
160 } ExecCleanMask;
161
162 /* A credential configured with LoadCredential= */
163 typedef struct ExecLoadCredential {
164 char *id, *path;
165 bool encrypted;
166 } ExecLoadCredential;
167
168 /* A credential configured with SetCredential= */
169 typedef struct ExecSetCredential {
170 char *id;
171 bool encrypted;
172 void *data;
173 size_t size;
174 } ExecSetCredential;
175
176 /* Encodes configuration parameters applied to invoked commands. Does not carry runtime data, but only configuration
177 * changes sourced from unit files and suchlike. ExecContext objects are usually embedded into Unit objects, and do not
178 * change after being loaded. */
179 struct ExecContext {
180 char **environment;
181 char **environment_files;
182 char **pass_environment;
183 char **unset_environment;
184
185 struct rlimit *rlimit[_RLIMIT_MAX];
186 char *working_directory, *root_directory, *root_image, *root_verity, *root_hash_path, *root_hash_sig_path;
187 void *root_hash, *root_hash_sig;
188 size_t root_hash_size, root_hash_sig_size;
189 LIST_HEAD(MountOptions, root_image_options);
190 bool working_directory_missing_ok:1;
191 bool working_directory_home:1;
192
193 bool oom_score_adjust_set:1;
194 bool coredump_filter_set:1;
195 bool nice_set:1;
196 bool ioprio_set:1;
197 bool cpu_sched_set:1;
198 bool mount_apivfs_set:1;
199
200 /* This is not exposed to the user but available internally. We need it to make sure that whenever we
201 * spawn /usr/bin/mount it is run in the same process group as us so that the autofs logic detects
202 * that it belongs to us and we don't enter a trigger loop. */
203 bool same_pgrp;
204
205 bool cpu_sched_reset_on_fork;
206 bool non_blocking;
207
208 mode_t umask;
209 int oom_score_adjust;
210 int nice;
211 int ioprio;
212 int cpu_sched_policy;
213 int cpu_sched_priority;
214 uint64_t coredump_filter;
215
216 CPUSet cpu_set;
217 NUMAPolicy numa_policy;
218 bool cpu_affinity_from_numa;
219
220 ExecInput std_input;
221 ExecOutput std_output;
222 ExecOutput std_error;
223 bool stdio_as_fds;
224 char *stdio_fdname[3];
225 char *stdio_file[3];
226
227 void *stdin_data;
228 size_t stdin_data_size;
229
230 nsec_t timer_slack_nsec;
231
232 char *tty_path;
233
234 bool tty_reset;
235 bool tty_vhangup;
236 bool tty_vt_disallocate;
237
238 unsigned tty_rows;
239 unsigned tty_cols;
240
241 bool ignore_sigpipe;
242
243 ExecKeyringMode keyring_mode;
244
245 /* Since resolving these names might involve socket
246 * connections and we don't want to deadlock ourselves these
247 * names are resolved on execution only and in the child
248 * process. */
249 char *user;
250 char *group;
251 char **supplementary_groups;
252
253 char *pam_name;
254
255 char *utmp_id;
256 ExecUtmpMode utmp_mode;
257
258 bool no_new_privileges;
259
260 bool selinux_context_ignore;
261 bool apparmor_profile_ignore;
262 bool smack_process_label_ignore;
263
264 char *selinux_context;
265 char *apparmor_profile;
266 char *smack_process_label;
267
268 char **read_write_paths, **read_only_paths, **inaccessible_paths, **exec_paths, **no_exec_paths;
269 char **exec_search_path;
270 unsigned long mount_propagation_flag;
271 BindMount *bind_mounts;
272 size_t n_bind_mounts;
273 TemporaryFileSystem *temporary_filesystems;
274 size_t n_temporary_filesystems;
275 MountImage *mount_images;
276 size_t n_mount_images;
277 MountImage *extension_images;
278 size_t n_extension_images;
279 char **extension_directories;
280
281 uint64_t capability_bounding_set;
282 uint64_t capability_ambient_set;
283 int secure_bits;
284
285 int syslog_priority;
286 bool syslog_level_prefix;
287 char *syslog_identifier;
288
289 struct iovec* log_extra_fields;
290 size_t n_log_extra_fields;
291 Set *log_filter_allowed_patterns;
292 Set *log_filter_denied_patterns;
293
294 usec_t log_ratelimit_interval_usec;
295 unsigned log_ratelimit_burst;
296
297 int log_level_max;
298
299 char *log_namespace;
300
301 ProtectProc protect_proc; /* hidepid= */
302 ProcSubset proc_subset; /* subset= */
303
304 int private_mounts;
305 bool private_tmp;
306 bool private_network;
307 bool private_devices;
308 bool private_users;
309 bool private_ipc;
310 bool protect_kernel_tunables;
311 bool protect_kernel_modules;
312 bool protect_kernel_logs;
313 bool protect_clock;
314 bool protect_control_groups;
315 ProtectSystem protect_system;
316 ProtectHome protect_home;
317 bool protect_hostname;
318 bool mount_apivfs;
319
320 bool dynamic_user;
321 bool remove_ipc;
322
323 bool memory_deny_write_execute;
324 bool restrict_realtime;
325 bool restrict_suid_sgid;
326
327 bool lock_personality;
328 unsigned long personality;
329
330 unsigned long restrict_namespaces; /* The CLONE_NEWxyz flags permitted to the unit's processes */
331
332 Set *restrict_filesystems;
333 bool restrict_filesystems_allow_list:1;
334
335 Hashmap *syscall_filter;
336 Set *syscall_archs;
337 int syscall_errno;
338 bool syscall_allow_list:1;
339
340 Hashmap *syscall_log;
341 bool syscall_log_allow_list:1; /* Log listed system calls */
342
343 bool address_families_allow_list:1;
344 Set *address_families;
345
346 char *network_namespace_path;
347 char *ipc_namespace_path;
348
349 ExecDirectory directories[_EXEC_DIRECTORY_TYPE_MAX];
350 ExecPreserveMode runtime_directory_preserve_mode;
351 usec_t timeout_clean_usec;
352
353 Hashmap *set_credentials; /* output id → ExecSetCredential */
354 Hashmap *load_credentials; /* output id → ExecLoadCredential */
355 };
356
357 static inline bool exec_context_restrict_namespaces_set(const ExecContext *c) {
358 assert(c);
359
360 return (c->restrict_namespaces & NAMESPACE_FLAGS_ALL) != NAMESPACE_FLAGS_ALL;
361 }
362
363 static inline bool exec_context_restrict_filesystems_set(const ExecContext *c) {
364 assert(c);
365
366 return c->restrict_filesystems_allow_list ||
367 !set_isempty(c->restrict_filesystems);
368 }
369
370 static inline bool exec_context_with_rootfs(const ExecContext *c) {
371 assert(c);
372
373 /* Checks if RootDirectory= or RootImage= are used */
374
375 return !empty_or_root(c->root_directory) || c->root_image;
376 }
377
378 typedef enum ExecFlags {
379 EXEC_APPLY_SANDBOXING = 1 << 0,
380 EXEC_APPLY_CHROOT = 1 << 1,
381 EXEC_APPLY_TTY_STDIN = 1 << 2,
382 EXEC_PASS_LOG_UNIT = 1 << 3, /* Whether to pass the unit name to the service's journal stream connection */
383 EXEC_CHOWN_DIRECTORIES = 1 << 4, /* chown() the runtime/state/cache/log directories to the user we run as, under all conditions */
384 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 */
385 EXEC_CGROUP_DELEGATE = 1 << 6,
386 EXEC_IS_CONTROL = 1 << 7,
387 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 */
388 EXEC_WRITE_CREDENTIALS = 1 << 9, /* Set up the credential store logic */
389
390 /* The following are not used by execute.c, but by consumers internally */
391 EXEC_PASS_FDS = 1 << 10,
392 EXEC_SETENV_RESULT = 1 << 11,
393 EXEC_SET_WATCHDOG = 1 << 12,
394 EXEC_SETENV_MONITOR_RESULT = 1 << 13, /* Pass exit status to OnFailure= and OnSuccess= dependencies. */
395 } ExecFlags;
396
397 /* Parameters for a specific invocation of a command. This structure is put together right before a command is
398 * executed. */
399 struct ExecParameters {
400 char **environment;
401
402 int *fds;
403 char **fd_names;
404 size_t n_socket_fds;
405 size_t n_storage_fds;
406
407 ExecFlags flags;
408 bool selinux_context_net:1;
409
410 CGroupMask cgroup_supported;
411 const char *cgroup_path;
412
413 char **prefix;
414 const char *received_credentials_directory;
415 const char *received_encrypted_credentials_directory;
416
417 const char *confirm_spawn;
418
419 usec_t watchdog_usec;
420
421 int *idle_pipe;
422
423 int stdin_fd;
424 int stdout_fd;
425 int stderr_fd;
426
427 /* An fd that is closed by the execve(), and thus will result in EOF when the execve() is done */
428 int exec_fd;
429
430 const char *notify_socket;
431
432 LIST_HEAD(OpenFile, open_files);
433 };
434
435 #include "unit.h"
436 #include "dynamic-user.h"
437
438 int exec_spawn(Unit *unit,
439 ExecCommand *command,
440 const ExecContext *context,
441 const ExecParameters *exec_params,
442 ExecRuntime *runtime,
443 DynamicCreds *dynamic_creds,
444 const CGroupContext *cgroup_context,
445 pid_t *ret);
446
447 void exec_command_done_array(ExecCommand *c, size_t n);
448 ExecCommand* exec_command_free_list(ExecCommand *c);
449 void exec_command_free_array(ExecCommand **c, size_t n);
450 void exec_command_reset_status_array(ExecCommand *c, size_t n);
451 void exec_command_reset_status_list_array(ExecCommand **c, size_t n);
452 void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix);
453 void exec_command_append_list(ExecCommand **l, ExecCommand *e);
454 int exec_command_set(ExecCommand *c, const char *path, ...) _sentinel_;
455 int exec_command_append(ExecCommand *c, const char *path, ...) _sentinel_;
456
457 void exec_context_init(ExecContext *c);
458 void exec_context_done(ExecContext *c);
459 void exec_context_dump(const ExecContext *c, FILE* f, const char *prefix);
460
461 int exec_context_destroy_runtime_directory(const ExecContext *c, const char *runtime_root);
462 int exec_context_destroy_credentials(const ExecContext *c, const char *runtime_root, const char *unit);
463 int exec_context_destroy_mount_ns_dir(Unit *u);
464
465 const char* exec_context_fdname(const ExecContext *c, int fd_index);
466
467 bool exec_context_may_touch_console(const ExecContext *c);
468 bool exec_context_maintains_privileges(const ExecContext *c);
469 bool exec_context_has_encrypted_credentials(ExecContext *c);
470
471 int exec_context_get_effective_ioprio(const ExecContext *c);
472 bool exec_context_get_effective_mount_apivfs(const ExecContext *c);
473
474 void exec_context_free_log_extra_fields(ExecContext *c);
475
476 void exec_context_revert_tty(ExecContext *c);
477
478 int exec_context_get_clean_directories(ExecContext *c, char **prefix, ExecCleanMask mask, char ***ret);
479 int exec_context_get_clean_mask(ExecContext *c, ExecCleanMask *ret);
480
481 void exec_status_start(ExecStatus *s, pid_t pid);
482 void exec_status_exit(ExecStatus *s, const ExecContext *context, pid_t pid, int code, int status);
483 void exec_status_dump(const ExecStatus *s, FILE *f, const char *prefix);
484 void exec_status_reset(ExecStatus *s);
485
486 int exec_runtime_acquire(Manager *m, const ExecContext *c, const char *name, bool create, ExecRuntime **ret);
487 ExecRuntime *exec_runtime_unref(ExecRuntime *r, bool destroy);
488
489 int exec_runtime_serialize(const Manager *m, FILE *f, FDSet *fds);
490 int exec_runtime_deserialize_compat(Unit *u, const char *key, const char *value, FDSet *fds);
491 int exec_runtime_deserialize_one(Manager *m, const char *value, FDSet *fds);
492 void exec_runtime_vacuum(Manager *m);
493
494 void exec_params_clear(ExecParameters *p);
495
496 bool exec_context_get_cpu_affinity_from_numa(const ExecContext *c);
497
498 ExecSetCredential *exec_set_credential_free(ExecSetCredential *sc);
499 DEFINE_TRIVIAL_CLEANUP_FUNC(ExecSetCredential*, exec_set_credential_free);
500
501 ExecLoadCredential *exec_load_credential_free(ExecLoadCredential *lc);
502 DEFINE_TRIVIAL_CLEANUP_FUNC(ExecLoadCredential*, exec_load_credential_free);
503
504 void exec_directory_done(ExecDirectory *d);
505 int exec_directory_add(ExecDirectory *d, const char *path, const char *symlink);
506 void exec_directory_sort(ExecDirectory *d);
507
508 extern const struct hash_ops exec_set_credential_hash_ops;
509 extern const struct hash_ops exec_load_credential_hash_ops;
510
511 const char* exec_output_to_string(ExecOutput i) _const_;
512 ExecOutput exec_output_from_string(const char *s) _pure_;
513
514 const char* exec_input_to_string(ExecInput i) _const_;
515 ExecInput exec_input_from_string(const char *s) _pure_;
516
517 const char* exec_utmp_mode_to_string(ExecUtmpMode i) _const_;
518 ExecUtmpMode exec_utmp_mode_from_string(const char *s) _pure_;
519
520 const char* exec_preserve_mode_to_string(ExecPreserveMode i) _const_;
521 ExecPreserveMode exec_preserve_mode_from_string(const char *s) _pure_;
522
523 const char* exec_keyring_mode_to_string(ExecKeyringMode i) _const_;
524 ExecKeyringMode exec_keyring_mode_from_string(const char *s) _pure_;
525
526 const char* exec_directory_type_to_string(ExecDirectoryType i) _const_;
527 ExecDirectoryType exec_directory_type_from_string(const char *s) _pure_;
528
529 const char* exec_directory_type_symlink_to_string(ExecDirectoryType i) _const_;
530 ExecDirectoryType exec_directory_type_symlink_from_string(const char *s) _pure_;
531
532 const char* exec_resource_type_to_string(ExecDirectoryType i) _const_;
533 ExecDirectoryType exec_resource_type_from_string(const char *s) _pure_;
534
535 bool exec_needs_mount_namespace(const ExecContext *context, const ExecParameters *params, const ExecRuntime *runtime);
536 bool exec_needs_network_namespace(const ExecContext *context);