]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
c2f1db8f | 2 | #pragma once |
5cb5a6ff LP |
3 | |
4 | typedef struct ExecStatus ExecStatus; | |
5 | typedef struct ExecCommand ExecCommand; | |
6 | typedef struct ExecContext ExecContext; | |
613b411c | 7 | typedef struct ExecRuntime ExecRuntime; |
9fa95f85 | 8 | typedef struct ExecParameters ExecParameters; |
e8a565cb | 9 | typedef struct Manager Manager; |
5cb5a6ff | 10 | |
71d35b6b | 11 | #include <sched.h> |
5cb5a6ff LP |
12 | #include <stdbool.h> |
13 | #include <stdio.h> | |
71d35b6b | 14 | #include <sys/capability.h> |
5cb5a6ff | 15 | |
9ce93478 | 16 | #include "cgroup-util.h" |
613b411c | 17 | #include "fdset.h" |
71d35b6b | 18 | #include "list.h" |
517d56b1 | 19 | #include "missing.h" |
417116f2 | 20 | #include "namespace.h" |
add00535 | 21 | #include "nsflags.h" |
5cb5a6ff | 22 | |
08f3be7a LP |
23 | #define EXEC_STDIN_DATA_MAX (64U*1024U*1024U) |
24 | ||
023a4f67 LP |
25 | typedef enum ExecUtmpMode { |
26 | EXEC_UTMP_INIT, | |
27 | EXEC_UTMP_LOGIN, | |
28 | EXEC_UTMP_USER, | |
29 | _EXEC_UTMP_MODE_MAX, | |
2307f37e | 30 | _EXEC_UTMP_MODE_INVALID = -1 |
023a4f67 LP |
31 | } ExecUtmpMode; |
32 | ||
80876c20 LP |
33 | typedef enum ExecInput { |
34 | EXEC_INPUT_NULL, | |
35 | EXEC_INPUT_TTY, | |
36 | EXEC_INPUT_TTY_FORCE, | |
37 | EXEC_INPUT_TTY_FAIL, | |
4f2d528d | 38 | EXEC_INPUT_SOCKET, |
52c239d7 | 39 | EXEC_INPUT_NAMED_FD, |
08f3be7a | 40 | EXEC_INPUT_DATA, |
2038c3f5 | 41 | EXEC_INPUT_FILE, |
80876c20 LP |
42 | _EXEC_INPUT_MAX, |
43 | _EXEC_INPUT_INVALID = -1 | |
44 | } ExecInput; | |
45 | ||
071830ff | 46 | typedef enum ExecOutput { |
80876c20 | 47 | EXEC_OUTPUT_INHERIT, |
94f04347 | 48 | EXEC_OUTPUT_NULL, |
80876c20 | 49 | EXEC_OUTPUT_TTY, |
94f04347 | 50 | EXEC_OUTPUT_SYSLOG, |
28dbc1e8 | 51 | EXEC_OUTPUT_SYSLOG_AND_CONSOLE, |
9a6bca7a | 52 | EXEC_OUTPUT_KMSG, |
28dbc1e8 | 53 | EXEC_OUTPUT_KMSG_AND_CONSOLE, |
706343f4 LP |
54 | EXEC_OUTPUT_JOURNAL, |
55 | EXEC_OUTPUT_JOURNAL_AND_CONSOLE, | |
4f2d528d | 56 | EXEC_OUTPUT_SOCKET, |
52c239d7 | 57 | EXEC_OUTPUT_NAMED_FD, |
2038c3f5 | 58 | EXEC_OUTPUT_FILE, |
94f04347 LP |
59 | _EXEC_OUTPUT_MAX, |
60 | _EXEC_OUTPUT_INVALID = -1 | |
071830ff LP |
61 | } ExecOutput; |
62 | ||
53f47dfc YW |
63 | typedef enum ExecPreserveMode { |
64 | EXEC_PRESERVE_NO, | |
65 | EXEC_PRESERVE_YES, | |
66 | EXEC_PRESERVE_RESTART, | |
67 | _EXEC_PRESERVE_MODE_MAX, | |
68 | _EXEC_PRESERVE_MODE_INVALID = -1 | |
69 | } ExecPreserveMode; | |
70 | ||
b1edf445 LP |
71 | typedef enum ExecKeyringMode { |
72 | EXEC_KEYRING_INHERIT, | |
73 | EXEC_KEYRING_PRIVATE, | |
74 | EXEC_KEYRING_SHARED, | |
75 | _EXEC_KEYRING_MODE_MAX, | |
76 | _EXEC_KEYRING_MODE_INVALID = -1, | |
77 | } ExecKeyringMode; | |
78 | ||
5cb5a6ff | 79 | struct ExecStatus { |
63983207 LP |
80 | dual_timestamp start_timestamp; |
81 | dual_timestamp exit_timestamp; | |
9d58f1db | 82 | pid_t pid; |
9152c765 LP |
83 | int code; /* as in siginfo_t::si_code */ |
84 | int status; /* as in sigingo_t::si_status */ | |
5cb5a6ff LP |
85 | }; |
86 | ||
3ed0cd26 LP |
87 | typedef enum ExecCommandFlags { |
88 | EXEC_COMMAND_IGNORE_FAILURE = 1, | |
89 | EXEC_COMMAND_FULLY_PRIVILEGED = 2, | |
165a31c0 LP |
90 | EXEC_COMMAND_NO_SETUID = 4, |
91 | EXEC_COMMAND_AMBIENT_MAGIC = 8, | |
3ed0cd26 LP |
92 | } ExecCommandFlags; |
93 | ||
5cb5a6ff LP |
94 | struct ExecCommand { |
95 | char *path; | |
96 | char **argv; | |
034c6ed7 | 97 | ExecStatus exec_status; |
3ed0cd26 | 98 | ExecCommandFlags flags; |
034c6ed7 | 99 | LIST_FIELDS(ExecCommand, command); /* useful for chaining commands */ |
5cb5a6ff LP |
100 | }; |
101 | ||
613b411c LP |
102 | struct ExecRuntime { |
103 | int n_ref; | |
104 | ||
e8a565cb YW |
105 | Manager *manager; |
106 | ||
107 | /* unit id of the owner */ | |
108 | char *id; | |
109 | ||
613b411c LP |
110 | char *tmp_dir; |
111 | char *var_tmp_dir; | |
112 | ||
29206d46 LP |
113 | /* An AF_UNIX socket pair, that contains a datagram containing a file descriptor referring to the network |
114 | * namespace. */ | |
613b411c LP |
115 | int netns_storage_socket[2]; |
116 | }; | |
117 | ||
3536f49e YW |
118 | typedef enum ExecDirectoryType { |
119 | EXEC_DIRECTORY_RUNTIME = 0, | |
120 | EXEC_DIRECTORY_STATE, | |
121 | EXEC_DIRECTORY_CACHE, | |
122 | EXEC_DIRECTORY_LOGS, | |
123 | EXEC_DIRECTORY_CONFIGURATION, | |
72fd1768 LP |
124 | _EXEC_DIRECTORY_TYPE_MAX, |
125 | _EXEC_DIRECTORY_TYPE_INVALID = -1, | |
3536f49e YW |
126 | } ExecDirectoryType; |
127 | ||
128 | typedef struct ExecDirectory { | |
129 | char **paths; | |
130 | mode_t mode; | |
131 | } ExecDirectory; | |
132 | ||
5cb5a6ff LP |
133 | struct ExecContext { |
134 | char **environment; | |
8c7be95e | 135 | char **environment_files; |
b4c14404 | 136 | char **pass_environment; |
00819cc1 | 137 | char **unset_environment; |
8c7be95e | 138 | |
517d56b1 | 139 | struct rlimit *rlimit[_RLIMIT_MAX]; |
915e6d16 | 140 | char *working_directory, *root_directory, *root_image; |
4c08c824 | 141 | bool working_directory_missing_ok; |
5f5d8eab | 142 | bool working_directory_home; |
9d58f1db LP |
143 | |
144 | mode_t umask; | |
dd6c17b1 | 145 | int oom_score_adjust; |
5cb5a6ff | 146 | int nice; |
9eba9da4 | 147 | int ioprio; |
94f04347 LP |
148 | int cpu_sched_policy; |
149 | int cpu_sched_priority; | |
9d58f1db | 150 | |
82c121a4 LP |
151 | cpu_set_t *cpuset; |
152 | unsigned cpuset_ncpus; | |
fb33a393 | 153 | |
80876c20 LP |
154 | ExecInput std_input; |
155 | ExecOutput std_output; | |
156 | ExecOutput std_error; | |
52c239d7 | 157 | char *stdio_fdname[3]; |
2038c3f5 | 158 | char *stdio_file[3]; |
80876c20 | 159 | |
08f3be7a LP |
160 | void *stdin_data; |
161 | size_t stdin_data_size; | |
80876c20 | 162 | |
d88a251b | 163 | nsec_t timer_slack_nsec; |
071830ff | 164 | |
1e22b5cd LP |
165 | bool stdio_as_fds; |
166 | ||
9d58f1db | 167 | char *tty_path; |
5cb5a6ff | 168 | |
6ea832a2 LP |
169 | bool tty_reset; |
170 | bool tty_vhangup; | |
171 | bool tty_vt_disallocate; | |
172 | ||
353e12c2 LP |
173 | bool ignore_sigpipe; |
174 | ||
61233823 | 175 | /* Since resolving these names might involve socket |
5cb5a6ff | 176 | * connections and we don't want to deadlock ourselves these |
94f04347 LP |
177 | * names are resolved on execution only and in the child |
178 | * process. */ | |
5cb5a6ff LP |
179 | char *user; |
180 | char *group; | |
181 | char **supplementary_groups; | |
9d58f1db | 182 | |
5b6319dc LP |
183 | char *pam_name; |
184 | ||
169c1bda | 185 | char *utmp_id; |
023a4f67 | 186 | ExecUtmpMode utmp_mode; |
169c1bda | 187 | |
5f8640fb | 188 | bool selinux_context_ignore; |
7b52a628 MS |
189 | char *selinux_context; |
190 | ||
eef65bf3 MS |
191 | bool apparmor_profile_ignore; |
192 | char *apparmor_profile; | |
193 | ||
2ca620c4 WC |
194 | bool smack_process_label_ignore; |
195 | char *smack_process_label; | |
196 | ||
b1edf445 LP |
197 | ExecKeyringMode keyring_mode; |
198 | ||
2a624c36 | 199 | char **read_write_paths, **read_only_paths, **inaccessible_paths; |
15ae422b | 200 | unsigned long mount_flags; |
d2d6c096 | 201 | BindMount *bind_mounts; |
da6053d0 | 202 | size_t n_bind_mounts; |
2abd4e38 | 203 | TemporaryFileSystem *temporary_filesystems; |
da6053d0 | 204 | size_t n_temporary_filesystems; |
15ae422b | 205 | |
a103496c | 206 | uint64_t capability_bounding_set; |
755d4b67 | 207 | uint64_t capability_ambient_set; |
9d58f1db LP |
208 | int secure_bits; |
209 | ||
7fab9d01 LP |
210 | int syslog_priority; |
211 | char *syslog_identifier; | |
212 | bool syslog_level_prefix; | |
213 | ||
d3070fbd LP |
214 | int log_level_max; |
215 | ||
216 | struct iovec* log_extra_fields; | |
217 | size_t n_log_extra_fields; | |
218 | ||
9d58f1db LP |
219 | bool cpu_sched_reset_on_fork; |
220 | bool non_blocking; | |
15ae422b | 221 | bool private_tmp; |
ff01d048 | 222 | bool private_network; |
7f112f50 | 223 | bool private_devices; |
d251207d | 224 | bool private_users; |
228af36f | 225 | bool private_mounts; |
1b8689f9 LP |
226 | ProtectSystem protect_system; |
227 | ProtectHome protect_home; | |
59eeb84b | 228 | bool protect_kernel_tunables; |
502d704e | 229 | bool protect_kernel_modules; |
59eeb84b | 230 | bool protect_control_groups; |
5d997827 | 231 | bool mount_apivfs; |
9d58f1db | 232 | |
8351ceae LP |
233 | bool no_new_privileges; |
234 | ||
29206d46 | 235 | bool dynamic_user; |
00d9ef85 | 236 | bool remove_ipc; |
29206d46 | 237 | |
9d58f1db LP |
238 | /* This is not exposed to the user but available |
239 | * internally. We need it to make sure that whenever we spawn | |
f00929ad | 240 | * /usr/bin/mount it is run in the same process group as us so |
9d58f1db LP |
241 | * that the autofs logic detects that it belongs to us and we |
242 | * don't enter a trigger loop. */ | |
74922904 | 243 | bool same_pgrp; |
2e22afe9 | 244 | |
ac45f971 | 245 | unsigned long personality; |
78e864e5 | 246 | bool lock_personality; |
ac45f971 | 247 | |
add00535 LP |
248 | unsigned long restrict_namespaces; /* The CLONE_NEWxyz flags permitted to the unit's processes */ |
249 | ||
8cfa775f | 250 | Hashmap *syscall_filter; |
57183d11 | 251 | Set *syscall_archs; |
17df7223 LP |
252 | int syscall_errno; |
253 | bool syscall_whitelist:1; | |
8351ceae | 254 | |
4298d0b5 LP |
255 | Set *address_families; |
256 | bool address_families_whitelist:1; | |
257 | ||
53f47dfc | 258 | ExecPreserveMode runtime_directory_preserve_mode; |
72fd1768 | 259 | ExecDirectory directories[_EXEC_DIRECTORY_TYPE_MAX]; |
e66cf1a3 | 260 | |
f4170c67 LP |
261 | bool memory_deny_write_execute; |
262 | bool restrict_realtime; | |
263 | ||
dd6c17b1 | 264 | bool oom_score_adjust_set:1; |
7fab9d01 LP |
265 | bool nice_set:1; |
266 | bool ioprio_set:1; | |
267 | bool cpu_sched_set:1; | |
5cb5a6ff LP |
268 | }; |
269 | ||
add00535 LP |
270 | static inline bool exec_context_restrict_namespaces_set(const ExecContext *c) { |
271 | assert(c); | |
272 | ||
273 | return (c->restrict_namespaces & NAMESPACE_FLAGS_ALL) != NAMESPACE_FLAGS_ALL; | |
274 | } | |
275 | ||
c39f1ce2 | 276 | typedef enum ExecFlags { |
ef31828d LP |
277 | EXEC_APPLY_SANDBOXING = 1 << 0, |
278 | EXEC_APPLY_CHROOT = 1 << 1, | |
279 | EXEC_APPLY_TTY_STDIN = 1 << 2, | |
280 | EXEC_NEW_KEYRING = 1 << 3, | |
281 | EXEC_PASS_LOG_UNIT = 1 << 4, /* Whether to pass the unit name to the service's journal stream connection */ | |
282 | EXEC_CHOWN_DIRECTORIES = 1 << 5, /* chown() the runtime/state/cache/log directories to the user we run as, under all conditions */ | |
283 | EXEC_NSS_BYPASS_BUS = 1 << 6, /* Set the SYSTEMD_NSS_BYPASS_BUS environment variable, to disable nss-systemd for dbus */ | |
284 | EXEC_CGROUP_DELEGATE = 1 << 7, | |
c39f1ce2 | 285 | |
9c1a61ad | 286 | /* The following are not used by execute.c, but by consumers internally */ |
ef31828d LP |
287 | EXEC_PASS_FDS = 1 << 8, |
288 | EXEC_IS_CONTROL = 1 << 9, | |
289 | EXEC_SETENV_RESULT = 1 << 10, | |
290 | EXEC_SET_WATCHDOG = 1 << 11, | |
c39f1ce2 LP |
291 | } ExecFlags; |
292 | ||
9fa95f85 DM |
293 | struct ExecParameters { |
294 | char **argv; | |
a34ceba6 | 295 | char **environment; |
8dd4c05b LP |
296 | |
297 | int *fds; | |
298 | char **fd_names; | |
da6053d0 LP |
299 | size_t n_storage_fds; |
300 | size_t n_socket_fds; | |
8dd4c05b | 301 | |
c39f1ce2 | 302 | ExecFlags flags; |
a34ceba6 | 303 | bool selinux_context_net:1; |
8dd4c05b | 304 | |
efdb0237 | 305 | CGroupMask cgroup_supported; |
9fa95f85 | 306 | const char *cgroup_path; |
8dd4c05b | 307 | |
3536f49e | 308 | char **prefix; |
8dd4c05b | 309 | |
7d5ceb64 FB |
310 | const char *confirm_spawn; |
311 | ||
9fa95f85 | 312 | usec_t watchdog_usec; |
8dd4c05b | 313 | |
9fa95f85 | 314 | int *idle_pipe; |
8dd4c05b | 315 | |
a34ceba6 LP |
316 | int stdin_fd; |
317 | int stdout_fd; | |
318 | int stderr_fd; | |
9fa95f85 DM |
319 | }; |
320 | ||
9ce93478 | 321 | #include "unit.h" |
29206d46 | 322 | #include "dynamic-user.h" |
9ce93478 | 323 | |
f2341e0a LP |
324 | int exec_spawn(Unit *unit, |
325 | ExecCommand *command, | |
9fa95f85 DM |
326 | const ExecContext *context, |
327 | const ExecParameters *exec_params, | |
613b411c | 328 | ExecRuntime *runtime, |
29206d46 | 329 | DynamicCreds *dynamic_creds, |
81a2b7ce | 330 | pid_t *ret); |
5cb5a6ff | 331 | |
da6053d0 | 332 | void exec_command_done_array(ExecCommand *c, size_t n); |
43d0fcbd | 333 | |
f1acf85a | 334 | ExecCommand* exec_command_free_list(ExecCommand *c); |
da6053d0 | 335 | void exec_command_free_array(ExecCommand **c, size_t n); |
5cb5a6ff | 336 | |
44d8db9e | 337 | void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix); |
a6a80b4f | 338 | void exec_command_append_list(ExecCommand **l, ExecCommand *e); |
26fd040d | 339 | int exec_command_set(ExecCommand *c, const char *path, ...); |
86b23b07 | 340 | int exec_command_append(ExecCommand *c, const char *path, ...); |
44d8db9e | 341 | |
034c6ed7 | 342 | void exec_context_init(ExecContext *c); |
613b411c | 343 | void exec_context_done(ExecContext *c); |
34cf6c43 | 344 | void exec_context_dump(const ExecContext *c, FILE* f, const char *prefix); |
5cb5a6ff | 345 | |
34cf6c43 | 346 | int exec_context_destroy_runtime_directory(const ExecContext *c, const char *runtime_root); |
e66cf1a3 | 347 | |
52c239d7 | 348 | const char* exec_context_fdname(const ExecContext *c, int fd_index); |
8c7be95e | 349 | |
34cf6c43 YW |
350 | bool exec_context_may_touch_console(const ExecContext *c); |
351 | bool exec_context_maintains_privileges(const ExecContext *c); | |
6ac8fdc9 | 352 | |
34cf6c43 | 353 | int exec_context_get_effective_ioprio(const ExecContext *c); |
7f452159 | 354 | |
d3070fbd LP |
355 | void exec_context_free_log_extra_fields(ExecContext *c); |
356 | ||
b58b4116 | 357 | void exec_status_start(ExecStatus *s, pid_t pid); |
34cf6c43 YW |
358 | void exec_status_exit(ExecStatus *s, const ExecContext *context, pid_t pid, int code, int status); |
359 | void exec_status_dump(const ExecStatus *s, FILE *f, const char *prefix); | |
5cb5a6ff | 360 | |
e8a565cb YW |
361 | int exec_runtime_acquire(Manager *m, const ExecContext *c, const char *name, bool create, ExecRuntime **ret); |
362 | ExecRuntime *exec_runtime_unref(ExecRuntime *r, bool destroy); | |
613b411c | 363 | |
e8a565cb YW |
364 | int exec_runtime_serialize(const Manager *m, FILE *f, FDSet *fds); |
365 | int exec_runtime_deserialize_compat(Unit *u, const char *key, const char *value, FDSet *fds); | |
366 | void exec_runtime_deserialize_one(Manager *m, const char *value, FDSet *fds); | |
367 | void exec_runtime_vacuum(Manager *m); | |
613b411c | 368 | |
44a6b1b6 ZJS |
369 | const char* exec_output_to_string(ExecOutput i) _const_; |
370 | ExecOutput exec_output_from_string(const char *s) _pure_; | |
94f04347 | 371 | |
44a6b1b6 ZJS |
372 | const char* exec_input_to_string(ExecInput i) _const_; |
373 | ExecInput exec_input_from_string(const char *s) _pure_; | |
023a4f67 LP |
374 | |
375 | const char* exec_utmp_mode_to_string(ExecUtmpMode i) _const_; | |
376 | ExecUtmpMode exec_utmp_mode_from_string(const char *s) _pure_; | |
53f47dfc YW |
377 | |
378 | const char* exec_preserve_mode_to_string(ExecPreserveMode i) _const_; | |
379 | ExecPreserveMode exec_preserve_mode_from_string(const char *s) _pure_; | |
3536f49e | 380 | |
b1edf445 LP |
381 | const char* exec_keyring_mode_to_string(ExecKeyringMode i) _const_; |
382 | ExecKeyringMode exec_keyring_mode_from_string(const char *s) _pure_; | |
383 | ||
3536f49e YW |
384 | const char* exec_directory_type_to_string(ExecDirectoryType i) _const_; |
385 | ExecDirectoryType exec_directory_type_from_string(const char *s) _pure_; |