]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/execute.h
Merge pull request #9832 from yuwata/fix-9831
[thirdparty/systemd.git] / src / core / execute.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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 "fdset.h"
18 #include "list.h"
19 #include "missing.h"
20 #include "namespace.h"
21 #include "nsflags.h"
22
23 #define EXEC_STDIN_DATA_MAX (64U*1024U*1024U)
24
25 typedef enum ExecUtmpMode {
26 EXEC_UTMP_INIT,
27 EXEC_UTMP_LOGIN,
28 EXEC_UTMP_USER,
29 _EXEC_UTMP_MODE_MAX,
30 _EXEC_UTMP_MODE_INVALID = -1
31 } ExecUtmpMode;
32
33 typedef enum ExecInput {
34 EXEC_INPUT_NULL,
35 EXEC_INPUT_TTY,
36 EXEC_INPUT_TTY_FORCE,
37 EXEC_INPUT_TTY_FAIL,
38 EXEC_INPUT_SOCKET,
39 EXEC_INPUT_NAMED_FD,
40 EXEC_INPUT_DATA,
41 EXEC_INPUT_FILE,
42 _EXEC_INPUT_MAX,
43 _EXEC_INPUT_INVALID = -1
44 } ExecInput;
45
46 typedef enum ExecOutput {
47 EXEC_OUTPUT_INHERIT,
48 EXEC_OUTPUT_NULL,
49 EXEC_OUTPUT_TTY,
50 EXEC_OUTPUT_SYSLOG,
51 EXEC_OUTPUT_SYSLOG_AND_CONSOLE,
52 EXEC_OUTPUT_KMSG,
53 EXEC_OUTPUT_KMSG_AND_CONSOLE,
54 EXEC_OUTPUT_JOURNAL,
55 EXEC_OUTPUT_JOURNAL_AND_CONSOLE,
56 EXEC_OUTPUT_SOCKET,
57 EXEC_OUTPUT_NAMED_FD,
58 EXEC_OUTPUT_FILE,
59 EXEC_OUTPUT_FILE_APPEND,
60 _EXEC_OUTPUT_MAX,
61 _EXEC_OUTPUT_INVALID = -1
62 } ExecOutput;
63
64 typedef enum ExecPreserveMode {
65 EXEC_PRESERVE_NO,
66 EXEC_PRESERVE_YES,
67 EXEC_PRESERVE_RESTART,
68 _EXEC_PRESERVE_MODE_MAX,
69 _EXEC_PRESERVE_MODE_INVALID = -1
70 } ExecPreserveMode;
71
72 typedef enum ExecKeyringMode {
73 EXEC_KEYRING_INHERIT,
74 EXEC_KEYRING_PRIVATE,
75 EXEC_KEYRING_SHARED,
76 _EXEC_KEYRING_MODE_MAX,
77 _EXEC_KEYRING_MODE_INVALID = -1,
78 } ExecKeyringMode;
79
80 /* Contains start and exit information about an executed command. */
81 struct ExecStatus {
82 pid_t pid;
83 dual_timestamp start_timestamp;
84 dual_timestamp exit_timestamp;
85 int code; /* as in siginfo_t::si_code */
86 int status; /* as in sigingo_t::si_status */
87 };
88
89 typedef enum ExecCommandFlags {
90 EXEC_COMMAND_IGNORE_FAILURE = 1 << 0,
91 EXEC_COMMAND_FULLY_PRIVILEGED = 1 << 1,
92 EXEC_COMMAND_NO_SETUID = 1 << 2,
93 EXEC_COMMAND_AMBIENT_MAGIC = 1 << 3,
94 } ExecCommandFlags;
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
124 typedef enum ExecDirectoryType {
125 EXEC_DIRECTORY_RUNTIME = 0,
126 EXEC_DIRECTORY_STATE,
127 EXEC_DIRECTORY_CACHE,
128 EXEC_DIRECTORY_LOGS,
129 EXEC_DIRECTORY_CONFIGURATION,
130 _EXEC_DIRECTORY_TYPE_MAX,
131 _EXEC_DIRECTORY_TYPE_INVALID = -1,
132 } ExecDirectoryType;
133
134 typedef struct ExecDirectory {
135 char **paths;
136 mode_t mode;
137 } ExecDirectory;
138
139 /* Encodes configuration parameters applied to invoked commands. Does not carry runtime data, but only configuration
140 * changes sourced from unit files and suchlike. ExecContext objects are usually embedded into Unit objects, and do not
141 * change after being loaded. */
142 struct ExecContext {
143 char **environment;
144 char **environment_files;
145 char **pass_environment;
146 char **unset_environment;
147
148 struct rlimit *rlimit[_RLIMIT_MAX];
149 char *working_directory, *root_directory, *root_image;
150 bool working_directory_missing_ok;
151 bool working_directory_home;
152
153 mode_t umask;
154 int oom_score_adjust;
155 int nice;
156 int ioprio;
157 int cpu_sched_policy;
158 int cpu_sched_priority;
159
160 cpu_set_t *cpuset;
161 unsigned cpuset_ncpus;
162
163 ExecInput std_input;
164 ExecOutput std_output;
165 ExecOutput std_error;
166 char *stdio_fdname[3];
167 char *stdio_file[3];
168
169 void *stdin_data;
170 size_t stdin_data_size;
171
172 nsec_t timer_slack_nsec;
173
174 bool stdio_as_fds;
175
176 char *tty_path;
177
178 bool tty_reset;
179 bool tty_vhangup;
180 bool tty_vt_disallocate;
181
182 bool ignore_sigpipe;
183
184 /* Since resolving these names might involve socket
185 * connections and we don't want to deadlock ourselves these
186 * names are resolved on execution only and in the child
187 * process. */
188 char *user;
189 char *group;
190 char **supplementary_groups;
191
192 char *pam_name;
193
194 char *utmp_id;
195 ExecUtmpMode utmp_mode;
196
197 bool selinux_context_ignore;
198 char *selinux_context;
199
200 bool apparmor_profile_ignore;
201 char *apparmor_profile;
202
203 bool smack_process_label_ignore;
204 char *smack_process_label;
205
206 ExecKeyringMode keyring_mode;
207
208 char **read_write_paths, **read_only_paths, **inaccessible_paths;
209 unsigned long mount_flags;
210 BindMount *bind_mounts;
211 size_t n_bind_mounts;
212 TemporaryFileSystem *temporary_filesystems;
213 size_t n_temporary_filesystems;
214
215 uint64_t capability_bounding_set;
216 uint64_t capability_ambient_set;
217 int secure_bits;
218
219 int syslog_priority;
220 char *syslog_identifier;
221 bool syslog_level_prefix;
222
223 int log_level_max;
224
225 struct iovec* log_extra_fields;
226 size_t n_log_extra_fields;
227
228 bool cpu_sched_reset_on_fork;
229 bool non_blocking;
230 bool private_tmp;
231 bool private_network;
232 bool private_devices;
233 bool private_users;
234 bool private_mounts;
235 ProtectSystem protect_system;
236 ProtectHome protect_home;
237 bool protect_kernel_tunables;
238 bool protect_kernel_modules;
239 bool protect_control_groups;
240 bool mount_apivfs;
241
242 bool no_new_privileges;
243
244 bool dynamic_user;
245 bool remove_ipc;
246
247 /* This is not exposed to the user but available
248 * internally. We need it to make sure that whenever we spawn
249 * /usr/bin/mount it is run in the same process group as us so
250 * that the autofs logic detects that it belongs to us and we
251 * don't enter a trigger loop. */
252 bool same_pgrp;
253
254 unsigned long personality;
255 bool lock_personality;
256
257 unsigned long restrict_namespaces; /* The CLONE_NEWxyz flags permitted to the unit's processes */
258
259 Hashmap *syscall_filter;
260 Set *syscall_archs;
261 int syscall_errno;
262 bool syscall_whitelist:1;
263
264 Set *address_families;
265 bool address_families_whitelist:1;
266
267 ExecPreserveMode runtime_directory_preserve_mode;
268 ExecDirectory directories[_EXEC_DIRECTORY_TYPE_MAX];
269
270 bool memory_deny_write_execute;
271 bool restrict_realtime;
272
273 bool oom_score_adjust_set:1;
274 bool nice_set:1;
275 bool ioprio_set:1;
276 bool cpu_sched_set:1;
277 };
278
279 static inline bool exec_context_restrict_namespaces_set(const ExecContext *c) {
280 assert(c);
281
282 return (c->restrict_namespaces & NAMESPACE_FLAGS_ALL) != NAMESPACE_FLAGS_ALL;
283 }
284
285 typedef enum ExecFlags {
286 EXEC_APPLY_SANDBOXING = 1 << 0,
287 EXEC_APPLY_CHROOT = 1 << 1,
288 EXEC_APPLY_TTY_STDIN = 1 << 2,
289 EXEC_NEW_KEYRING = 1 << 3,
290 EXEC_PASS_LOG_UNIT = 1 << 4, /* Whether to pass the unit name to the service's journal stream connection */
291 EXEC_CHOWN_DIRECTORIES = 1 << 5, /* chown() the runtime/state/cache/log directories to the user we run as, under all conditions */
292 EXEC_NSS_BYPASS_BUS = 1 << 6, /* Set the SYSTEMD_NSS_BYPASS_BUS environment variable, to disable nss-systemd for dbus */
293 EXEC_CGROUP_DELEGATE = 1 << 7,
294
295 /* The following are not used by execute.c, but by consumers internally */
296 EXEC_PASS_FDS = 1 << 8,
297 EXEC_IS_CONTROL = 1 << 9,
298 EXEC_SETENV_RESULT = 1 << 10,
299 EXEC_SET_WATCHDOG = 1 << 11,
300 } ExecFlags;
301
302 /* Parameters for a specific invocation of a command. This structure is put together right before a command is
303 * executed. */
304 struct ExecParameters {
305 char **environment;
306
307 int *fds;
308 char **fd_names;
309 size_t n_socket_fds;
310 size_t n_storage_fds;
311
312 ExecFlags flags;
313 bool selinux_context_net:1;
314
315 CGroupMask cgroup_supported;
316 const char *cgroup_path;
317
318 char **prefix;
319
320 const char *confirm_spawn;
321
322 usec_t watchdog_usec;
323
324 int *idle_pipe;
325
326 int stdin_fd;
327 int stdout_fd;
328 int stderr_fd;
329
330 /* An fd that is closed by the execve(), and thus will result in EOF when the execve() is done */
331 int exec_fd;
332 };
333
334 #include "unit.h"
335 #include "dynamic-user.h"
336
337 int exec_spawn(Unit *unit,
338 ExecCommand *command,
339 const ExecContext *context,
340 const ExecParameters *exec_params,
341 ExecRuntime *runtime,
342 DynamicCreds *dynamic_creds,
343 pid_t *ret);
344
345 void exec_command_done_array(ExecCommand *c, size_t n);
346 ExecCommand* exec_command_free_list(ExecCommand *c);
347 void exec_command_free_array(ExecCommand **c, size_t n);
348 void exec_command_reset_status_array(ExecCommand *c, size_t n);
349 void exec_command_reset_status_list_array(ExecCommand **c, size_t n);
350 void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix);
351 void exec_command_append_list(ExecCommand **l, ExecCommand *e);
352 int exec_command_set(ExecCommand *c, const char *path, ...);
353 int exec_command_append(ExecCommand *c, const char *path, ...);
354
355 void exec_context_init(ExecContext *c);
356 void exec_context_done(ExecContext *c);
357 void exec_context_dump(const ExecContext *c, FILE* f, const char *prefix);
358
359 int exec_context_destroy_runtime_directory(const ExecContext *c, const char *runtime_root);
360
361 const char* exec_context_fdname(const ExecContext *c, int fd_index);
362
363 bool exec_context_may_touch_console(const ExecContext *c);
364 bool exec_context_maintains_privileges(const ExecContext *c);
365
366 int exec_context_get_effective_ioprio(const ExecContext *c);
367
368 void exec_context_free_log_extra_fields(ExecContext *c);
369
370 void exec_status_start(ExecStatus *s, pid_t pid);
371 void exec_status_exit(ExecStatus *s, const ExecContext *context, pid_t pid, int code, int status);
372 void exec_status_dump(const ExecStatus *s, FILE *f, const char *prefix);
373 void exec_status_reset(ExecStatus *s);
374
375 int exec_runtime_acquire(Manager *m, const ExecContext *c, const char *name, bool create, ExecRuntime **ret);
376 ExecRuntime *exec_runtime_unref(ExecRuntime *r, bool destroy);
377
378 int exec_runtime_serialize(const Manager *m, FILE *f, FDSet *fds);
379 int exec_runtime_deserialize_compat(Unit *u, const char *key, const char *value, FDSet *fds);
380 void exec_runtime_deserialize_one(Manager *m, const char *value, FDSet *fds);
381 void exec_runtime_vacuum(Manager *m);
382
383 const char* exec_output_to_string(ExecOutput i) _const_;
384 ExecOutput exec_output_from_string(const char *s) _pure_;
385
386 const char* exec_input_to_string(ExecInput i) _const_;
387 ExecInput exec_input_from_string(const char *s) _pure_;
388
389 const char* exec_utmp_mode_to_string(ExecUtmpMode i) _const_;
390 ExecUtmpMode exec_utmp_mode_from_string(const char *s) _pure_;
391
392 const char* exec_preserve_mode_to_string(ExecPreserveMode i) _const_;
393 ExecPreserveMode exec_preserve_mode_from_string(const char *s) _pure_;
394
395 const char* exec_keyring_mode_to_string(ExecKeyringMode i) _const_;
396 ExecKeyringMode exec_keyring_mode_from_string(const char *s) _pure_;
397
398 const char* exec_directory_type_to_string(ExecDirectoryType i) _const_;
399 ExecDirectoryType exec_directory_type_from_string(const char *s) _pure_;