]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/execute.h
Merge pull request #9274 from poettering/comment-header-cleanup
[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_MAX,
60 _EXEC_OUTPUT_INVALID = -1
61 } ExecOutput;
62
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
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
79 struct ExecStatus {
80 dual_timestamp start_timestamp;
81 dual_timestamp exit_timestamp;
82 pid_t pid;
83 int code; /* as in siginfo_t::si_code */
84 int status; /* as in sigingo_t::si_status */
85 };
86
87 typedef enum ExecCommandFlags {
88 EXEC_COMMAND_IGNORE_FAILURE = 1,
89 EXEC_COMMAND_FULLY_PRIVILEGED = 2,
90 EXEC_COMMAND_NO_SETUID = 4,
91 EXEC_COMMAND_AMBIENT_MAGIC = 8,
92 } ExecCommandFlags;
93
94 struct ExecCommand {
95 char *path;
96 char **argv;
97 ExecStatus exec_status;
98 ExecCommandFlags flags;
99 LIST_FIELDS(ExecCommand, command); /* useful for chaining commands */
100 };
101
102 struct ExecRuntime {
103 int n_ref;
104
105 Manager *manager;
106
107 /* unit id of the owner */
108 char *id;
109
110 char *tmp_dir;
111 char *var_tmp_dir;
112
113 /* An AF_UNIX socket pair, that contains a datagram containing a file descriptor referring to the network
114 * namespace. */
115 int netns_storage_socket[2];
116 };
117
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,
124 _EXEC_DIRECTORY_TYPE_MAX,
125 _EXEC_DIRECTORY_TYPE_INVALID = -1,
126 } ExecDirectoryType;
127
128 typedef struct ExecDirectory {
129 char **paths;
130 mode_t mode;
131 } ExecDirectory;
132
133 struct ExecContext {
134 char **environment;
135 char **environment_files;
136 char **pass_environment;
137 char **unset_environment;
138
139 struct rlimit *rlimit[_RLIMIT_MAX];
140 char *working_directory, *root_directory, *root_image;
141 bool working_directory_missing_ok;
142 bool working_directory_home;
143
144 mode_t umask;
145 int oom_score_adjust;
146 int nice;
147 int ioprio;
148 int cpu_sched_policy;
149 int cpu_sched_priority;
150
151 cpu_set_t *cpuset;
152 unsigned cpuset_ncpus;
153
154 ExecInput std_input;
155 ExecOutput std_output;
156 ExecOutput std_error;
157 char *stdio_fdname[3];
158 char *stdio_file[3];
159
160 void *stdin_data;
161 size_t stdin_data_size;
162
163 nsec_t timer_slack_nsec;
164
165 bool stdio_as_fds;
166
167 char *tty_path;
168
169 bool tty_reset;
170 bool tty_vhangup;
171 bool tty_vt_disallocate;
172
173 bool ignore_sigpipe;
174
175 /* Since resolving these names might involve socket
176 * connections and we don't want to deadlock ourselves these
177 * names are resolved on execution only and in the child
178 * process. */
179 char *user;
180 char *group;
181 char **supplementary_groups;
182
183 char *pam_name;
184
185 char *utmp_id;
186 ExecUtmpMode utmp_mode;
187
188 bool selinux_context_ignore;
189 char *selinux_context;
190
191 bool apparmor_profile_ignore;
192 char *apparmor_profile;
193
194 bool smack_process_label_ignore;
195 char *smack_process_label;
196
197 ExecKeyringMode keyring_mode;
198
199 char **read_write_paths, **read_only_paths, **inaccessible_paths;
200 unsigned long mount_flags;
201 BindMount *bind_mounts;
202 size_t n_bind_mounts;
203 TemporaryFileSystem *temporary_filesystems;
204 size_t n_temporary_filesystems;
205
206 uint64_t capability_bounding_set;
207 uint64_t capability_ambient_set;
208 int secure_bits;
209
210 int syslog_priority;
211 char *syslog_identifier;
212 bool syslog_level_prefix;
213
214 int log_level_max;
215
216 struct iovec* log_extra_fields;
217 size_t n_log_extra_fields;
218
219 bool cpu_sched_reset_on_fork;
220 bool non_blocking;
221 bool private_tmp;
222 bool private_network;
223 bool private_devices;
224 bool private_users;
225 bool private_mounts;
226 ProtectSystem protect_system;
227 ProtectHome protect_home;
228 bool protect_kernel_tunables;
229 bool protect_kernel_modules;
230 bool protect_control_groups;
231 bool mount_apivfs;
232
233 bool no_new_privileges;
234
235 bool dynamic_user;
236 bool remove_ipc;
237
238 /* This is not exposed to the user but available
239 * internally. We need it to make sure that whenever we spawn
240 * /usr/bin/mount it is run in the same process group as us so
241 * that the autofs logic detects that it belongs to us and we
242 * don't enter a trigger loop. */
243 bool same_pgrp;
244
245 unsigned long personality;
246 bool lock_personality;
247
248 unsigned long restrict_namespaces; /* The CLONE_NEWxyz flags permitted to the unit's processes */
249
250 Hashmap *syscall_filter;
251 Set *syscall_archs;
252 int syscall_errno;
253 bool syscall_whitelist:1;
254
255 Set *address_families;
256 bool address_families_whitelist:1;
257
258 ExecPreserveMode runtime_directory_preserve_mode;
259 ExecDirectory directories[_EXEC_DIRECTORY_TYPE_MAX];
260
261 bool memory_deny_write_execute;
262 bool restrict_realtime;
263
264 bool oom_score_adjust_set:1;
265 bool nice_set:1;
266 bool ioprio_set:1;
267 bool cpu_sched_set:1;
268 };
269
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
276 typedef enum ExecFlags {
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,
285
286 /* The following are not used by execute.c, but by consumers internally */
287 EXEC_PASS_FDS = 1 << 8,
288 EXEC_IS_CONTROL = 1 << 9,
289 EXEC_SETENV_RESULT = 1 << 10,
290 EXEC_SET_WATCHDOG = 1 << 11,
291 } ExecFlags;
292
293 struct ExecParameters {
294 char **argv;
295 char **environment;
296
297 int *fds;
298 char **fd_names;
299 size_t n_storage_fds;
300 size_t n_socket_fds;
301
302 ExecFlags flags;
303 bool selinux_context_net:1;
304
305 CGroupMask cgroup_supported;
306 const char *cgroup_path;
307
308 char **prefix;
309
310 const char *confirm_spawn;
311
312 usec_t watchdog_usec;
313
314 int *idle_pipe;
315
316 int stdin_fd;
317 int stdout_fd;
318 int stderr_fd;
319 };
320
321 #include "unit.h"
322 #include "dynamic-user.h"
323
324 int exec_spawn(Unit *unit,
325 ExecCommand *command,
326 const ExecContext *context,
327 const ExecParameters *exec_params,
328 ExecRuntime *runtime,
329 DynamicCreds *dynamic_creds,
330 pid_t *ret);
331
332 void exec_command_done_array(ExecCommand *c, size_t n);
333
334 ExecCommand* exec_command_free_list(ExecCommand *c);
335 void exec_command_free_array(ExecCommand **c, size_t n);
336
337 void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix);
338 void exec_command_append_list(ExecCommand **l, ExecCommand *e);
339 int exec_command_set(ExecCommand *c, const char *path, ...);
340 int exec_command_append(ExecCommand *c, const char *path, ...);
341
342 void exec_context_init(ExecContext *c);
343 void exec_context_done(ExecContext *c);
344 void exec_context_dump(const ExecContext *c, FILE* f, const char *prefix);
345
346 int exec_context_destroy_runtime_directory(const ExecContext *c, const char *runtime_root);
347
348 const char* exec_context_fdname(const ExecContext *c, int fd_index);
349
350 bool exec_context_may_touch_console(const ExecContext *c);
351 bool exec_context_maintains_privileges(const ExecContext *c);
352
353 int exec_context_get_effective_ioprio(const ExecContext *c);
354
355 void exec_context_free_log_extra_fields(ExecContext *c);
356
357 void exec_status_start(ExecStatus *s, pid_t pid);
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);
360
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);
363
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);
368
369 const char* exec_output_to_string(ExecOutput i) _const_;
370 ExecOutput exec_output_from_string(const char *s) _pure_;
371
372 const char* exec_input_to_string(ExecInput i) _const_;
373 ExecInput exec_input_from_string(const char *s) _pure_;
374
375 const char* exec_utmp_mode_to_string(ExecUtmpMode i) _const_;
376 ExecUtmpMode exec_utmp_mode_from_string(const char *s) _pure_;
377
378 const char* exec_preserve_mode_to_string(ExecPreserveMode i) _const_;
379 ExecPreserveMode exec_preserve_mode_from_string(const char *s) _pure_;
380
381 const char* exec_keyring_mode_to_string(ExecKeyringMode i) _const_;
382 ExecKeyringMode exec_keyring_mode_from_string(const char *s) _pure_;
383
384 const char* exec_directory_type_to_string(ExecDirectoryType i) _const_;
385 ExecDirectoryType exec_directory_type_from_string(const char *s) _pure_;