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