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