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