]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/service.h
pcrlock: process components outside of location window properly
[thirdparty/systemd.git] / src / core / service.h
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
c2f1db8f 2#pragma once
5cb5a6ff 3
836e4e7e
DDM
4#include "cgroup.h"
5#include "core-forward.h"
71d35b6b
TA
6#include "exit-status.h"
7#include "kill.h"
c79ab77c 8#include "pidref.h"
57b7a260 9#include "unit.h"
5cb5a6ff 10
034c6ed7 11typedef enum ServiceRestart {
525ee6f4 12 SERVICE_RESTART_NO,
034c6ed7 13 SERVICE_RESTART_ON_SUCCESS,
50caaedb 14 SERVICE_RESTART_ON_FAILURE,
6cfe2fde 15 SERVICE_RESTART_ON_ABNORMAL,
dc99a976 16 SERVICE_RESTART_ON_WATCHDOG,
50caaedb 17 SERVICE_RESTART_ON_ABORT,
94f04347
LP
18 SERVICE_RESTART_ALWAYS,
19 _SERVICE_RESTART_MAX,
2d93c20e 20 _SERVICE_RESTART_INVALID = -EINVAL,
034c6ed7
LP
21} ServiceRestart;
22
23typedef enum ServiceType {
3bd28bf7
LP
24 SERVICE_SIMPLE, /* we fork and go on right-away (i.e. modern socket activated daemons) */
25 SERVICE_FORKING, /* forks by itself (i.e. traditional daemons) */
26 SERVICE_ONESHOT, /* we fork and wait until the program finishes (i.e. programs like fsck which run and need to finish before we continue) */
27 SERVICE_DBUS, /* we fork and wait until a specific D-Bus name appears on the bus */
28 SERVICE_NOTIFY, /* we fork and wait until a daemon sends us a ready message with sd_notify() */
29 SERVICE_NOTIFY_RELOAD, /* just like SERVICE_NOTIFY, but also implements a reload protocol via SIGHUP */
30 SERVICE_IDLE, /* much like simple, but delay exec() until all jobs are dispatched. */
31 SERVICE_EXEC, /* we fork and wait until we execute exec() (this means our own setup is waited for) */
94f04347 32 _SERVICE_TYPE_MAX,
2d93c20e 33 _SERVICE_TYPE_INVALID = -EINVAL,
034c6ed7 34} ServiceType;
5cb5a6ff 35
596e4470
HC
36typedef enum ServiceExitType {
37 SERVICE_EXIT_MAIN, /* we consider the main PID when deciding if the service exited */
38 SERVICE_EXIT_CGROUP, /* we wait for the last process in the cgroup to exit */
39 _SERVICE_EXIT_TYPE_MAX,
40 _SERVICE_EXIT_TYPE_INVALID = -EINVAL,
41} ServiceExitType;
42
5cb5a6ff 43typedef enum ServiceExecCommand {
31cd5f63 44 SERVICE_EXEC_CONDITION,
5cb5a6ff
LP
45 SERVICE_EXEC_START_PRE,
46 SERVICE_EXEC_START,
47 SERVICE_EXEC_START_POST,
5cb5a6ff 48 SERVICE_EXEC_RELOAD,
5cb5a6ff
LP
49 SERVICE_EXEC_STOP,
50 SERVICE_EXEC_STOP_POST,
e537352b 51 _SERVICE_EXEC_COMMAND_MAX,
2d93c20e 52 _SERVICE_EXEC_COMMAND_INVALID = -EINVAL,
5cb5a6ff
LP
53} ServiceExecCommand;
54
308d72dc
LP
55typedef enum NotifyState {
56 NOTIFY_UNKNOWN,
57 NOTIFY_READY,
58 NOTIFY_RELOADING,
59 NOTIFY_STOPPING,
60 _NOTIFY_STATE_MAX,
2d93c20e 61 _NOTIFY_STATE_INVALID = -EINVAL,
308d72dc
LP
62} NotifyState;
63
a4e26faf
JW
64/* The values of this enum are referenced in man/systemd.exec.xml and src/shared/bus-unit-util.c.
65 * Update those sources for each change to this enum. */
f42806df
LP
66typedef enum ServiceResult {
67 SERVICE_SUCCESS,
0b2de9d9 68 SERVICE_FAILURE_RESOURCES, /* a bit of a misnomer, just our catch-all error for errnos we didn't expect */
c35755fb 69 SERVICE_FAILURE_PROTOCOL,
f42806df
LP
70 SERVICE_FAILURE_TIMEOUT,
71 SERVICE_FAILURE_EXIT_CODE,
72 SERVICE_FAILURE_SIGNAL,
73 SERVICE_FAILURE_CORE_DUMP,
bb242b7b 74 SERVICE_FAILURE_WATCHDOG,
07299350 75 SERVICE_FAILURE_START_LIMIT_HIT,
38c41427 76 SERVICE_FAILURE_OOM_KILL, /* OOM Kill by the Kernel or systemd-oomd */
31cd5f63 77 SERVICE_SKIP_CONDITION,
f42806df 78 _SERVICE_RESULT_MAX,
2d93c20e 79 _SERVICE_RESULT_INVALID = -EINVAL,
f42806df
LP
80} ServiceResult;
81
bf760801
JK
82typedef enum ServiceTimeoutFailureMode {
83 SERVICE_TIMEOUT_TERMINATE,
84 SERVICE_TIMEOUT_ABORT,
85 SERVICE_TIMEOUT_KILL,
86 _SERVICE_TIMEOUT_FAILURE_MODE_MAX,
2d93c20e 87 _SERVICE_TIMEOUT_FAILURE_MODE_INVALID = -EINVAL,
bf760801
JK
88} ServiceTimeoutFailureMode;
89
e568fea9
RP
90typedef enum ServiceRestartMode {
91 SERVICE_RESTART_MODE_NORMAL,
92 SERVICE_RESTART_MODE_DIRECT,
7d8bbfbe 93 SERVICE_RESTART_MODE_DEBUG,
e568fea9
RP
94 _SERVICE_RESTART_MODE_MAX,
95 _SERVICE_RESTART_MODE_INVALID = -EINVAL,
96} ServiceRestartMode;
97
4ea4abb6 98typedef struct ServiceFDStore {
a354329f
LP
99 Service *service;
100
101 int fd;
8dd4c05b 102 char *fdname;
a354329f 103 sd_event_source *event_source;
30520492 104 bool do_poll;
a354329f 105
4ea4abb6
DDM
106 LIST_FIELDS(struct ServiceFDStore, fd_store);
107} ServiceFDStore;
a354329f 108
4ea4abb6 109typedef struct ServiceExtraFD {
3543456f
RW
110 int fd;
111 char *fdname;
4ea4abb6 112} ServiceExtraFD;
3543456f 113
836e4e7e 114typedef struct Service {
ac155bb8 115 Unit meta;
5cb5a6ff 116
034c6ed7 117 ServiceType type;
596e4470 118 ServiceExitType exit_type;
034c6ed7 119 ServiceRestart restart;
e568fea9 120 ServiceRestartMode restart_mode;
37520c1b
LP
121 ExitStatusSet restart_prevent_status;
122 ExitStatusSet restart_force_status;
96342de6 123 ExitStatusSet success_status;
034c6ed7
LP
124
125 /* If set we'll read the main daemon PID from this file */
126 char *pid_file;
127
ce31dbf4 128 unsigned n_restarts;
be1adc27 129 unsigned restart_steps;
ce31dbf4 130 usec_t restart_usec;
e9f17fa8 131 usec_t restart_max_delay_usec;
d568a335
MS
132 usec_t timeout_start_usec;
133 usec_t timeout_stop_usec;
dc653bf4 134 usec_t timeout_abort_usec;
54c1a6ab 135 bool timeout_abort_set;
36c16a7c 136 usec_t runtime_max_usec;
5918a933 137 usec_t runtime_rand_extra_usec;
bf760801
JK
138 ServiceTimeoutFailureMode timeout_start_failure_mode;
139 ServiceTimeoutFailureMode timeout_stop_failure_mode;
5cb5a6ff 140
a6927d7f 141 dual_timestamp watchdog_timestamp;
aa8c4bbf
LP
142 usec_t watchdog_usec; /* the requested watchdog timeout in the unit file */
143 usec_t watchdog_original_usec; /* the watchdog timeout that was in effect when the unit was started, i.e. the timeout the forked off processes currently see */
144 usec_t watchdog_override_usec; /* the watchdog timeout requested by the service itself through sd_notify() */
2787d83c 145 bool watchdog_override_enable;
718db961 146 sd_event_source *watchdog_event_source;
a6927d7f 147
5cb5a6ff 148 ExecContext exec_context;
4819ff03 149 KillContext kill_context;
4ad49000 150 CGroupContext cgroup_context;
5cb5a6ff 151
a16e1123 152 ServiceState state, deserialized_state;
034c6ed7 153
867b3b7d 154 /* The exit status of the real main process */
034c6ed7
LP
155 ExecStatus main_exec_status;
156
ce31dbf4 157 ExecCommand *exec_command[_SERVICE_EXEC_COMMAND_MAX];
867b3b7d 158
ce31dbf4
MY
159 /* The currently executed main process, which may be NULL if the main process got started via
160 * forking mode and not by us */
867b3b7d
LP
161 ExecCommand *main_command;
162
ce31dbf4
MY
163 /* The currently executed control process */
164 ExecCommand *control_command;
165
867b3b7d 166 /* The ID of the control command currently being executed */
a16e1123 167 ServiceExecCommand control_command_id;
867b3b7d 168
613b411c
LP
169 /* Runtime data of the execution context */
170 ExecRuntime *exec_runtime;
171
9cc54544
LP
172 CGroupRuntime *cgroup_runtime;
173
c79ab77c 174 PidRef main_pid, control_pid;
3fabebf4
LP
175
176 /* if we are a socket activated service instance, store information of the connection/peer/socket */
07459bb6 177 int socket_fd;
3fabebf4
LP
178 SocketPeer *socket_peer;
179 UnitRef accept_socket;
16115b0a 180 bool socket_fd_selinux_context_net;
8fe914ec
LP
181
182 bool permissions_start_only;
183 bool root_directory_start_only;
02ee865a 184 bool remain_after_exit;
3185a36b 185 bool guess_main_pid;
8fe914ec 186
05e343b7 187 /* If we shut down, remember why */
f42806df
LP
188 ServiceResult result;
189 ServiceResult reload_result;
5162829e 190 ServiceResult live_mount_result;
a53e92a1 191 ServiceResult clean_result;
e2f3b44c 192
5de6b302 193 bool main_pid_known:1;
6dfa5494 194 bool main_pid_alien:1;
05e343b7 195 bool bus_name_good:1;
47342320 196 bool forbid_restart:1;
d568a335 197 bool start_timeout_defined:1;
0a6991e0 198 bool exec_fd_hot:1;
2c4104f0 199
05e343b7
LP
200 char *bus_name;
201
8c47c732 202 char *status_text;
9c025022
MY
203 char *status_bus_error;
204 char *status_varlink_error;
4774e357 205 int status_errno;
8c47c732 206
718db961 207 sd_event_source *timer_event_source;
3a111838 208 PathSpec *pid_file_pathspec;
8fe914ec
LP
209
210 NotifyAccess notify_access;
19dff691 211 NotifyAccess notify_access_override;
308d72dc 212 NotifyState notify_state;
a354329f 213
e39eb045
LP
214 sd_bus_slot *bus_name_pid_lookup_slot;
215
5686391b
LP
216 sd_event_source *exec_fd_event_source;
217
6286f213 218 LIST_HEAD(ServiceFDStore, fd_store);
da6053d0 219 size_t n_fd_store;
a354329f 220 unsigned n_fd_store_max;
b9c1883a 221 ExecPreserveMode fd_store_preserve_mode;
6b7e5923 222
a34ceba6
LP
223 int stdin_fd;
224 int stdout_fd;
225 int stderr_fd;
7a0019d3 226
32af4dd8
MY
227 /* If service spawned from transient unit, extra file descriptors can be passed via dbus API */
228 ServiceExtraFD *extra_fds;
229 size_t n_extra_fds;
cd48e23f
RP
230
231 LIST_HEAD(OpenFile, open_files);
3bd28bf7
LP
232
233 int reload_signal;
234 usec_t reload_begin_usec;
5162829e 235
32af4dd8
MY
236 OOMPolicy oom_policy;
237
238 char *usb_function_descriptors;
239 char *usb_function_strings;
240
5162829e
LB
241 /* The D-Bus request, we will reply once the operation is finished, so that callers can block */
242 sd_bus_message *mount_request;
836e4e7e 243} Service;
5cb5a6ff 244
dc653bf4 245static inline usec_t service_timeout_abort_usec(Service *s) {
9c79f0e0 246 assert(s);
dc653bf4
JK
247 return s->timeout_abort_set ? s->timeout_abort_usec : s->timeout_stop_usec;
248}
249
19dff691
MY
250static inline NotifyAccess service_get_notify_access(Service *s) {
251 assert(s);
252 return s->notify_access_override < 0 ? s->notify_access : s->notify_access_override;
253}
254
4793c310
CD
255static inline usec_t service_get_watchdog_usec(Service *s) {
256 assert(s);
257 return s->watchdog_override_enable ? s->watchdog_override_usec : s->watchdog_original_usec;
258}
259
47be870b 260extern const UnitVTable service_vtable;
5cb5a6ff 261
3fabebf4 262int service_set_socket_fd(Service *s, int fd, struct Socket *socket, struct SocketPeer *peer, bool selinux_context_net);
81a1d6d6 263void service_release_socket_fd(Service *s);
4f2d528d 264
5171356e 265usec_t service_restart_usec_next(Service *s);
be1adc27 266
8017ed7e
LP
267int service_determine_exec_selinux_label(Service *s, char **ret);
268
44a6b1b6
ZJS
269const char* service_restart_to_string(ServiceRestart i) _const_;
270ServiceRestart service_restart_from_string(const char *s) _pure_;
94f04347 271
e568fea9
RP
272const char* service_restart_mode_to_string(ServiceRestartMode i) _const_;
273ServiceRestartMode service_restart_mode_from_string(const char *s) _pure_;
274
44a6b1b6
ZJS
275const char* service_type_to_string(ServiceType i) _const_;
276ServiceType service_type_from_string(const char *s) _pure_;
94f04347 277
596e4470
HC
278const char* service_exit_type_to_string(ServiceExitType i) _const_;
279ServiceExitType service_exit_type_from_string(const char *s) _pure_;
280
44a6b1b6
ZJS
281const char* service_exec_command_to_string(ServiceExecCommand i) _const_;
282ServiceExecCommand service_exec_command_from_string(const char *s) _pure_;
94f04347 283
b3d59367
AZ
284const char* service_exec_ex_command_to_string(ServiceExecCommand i) _const_;
285ServiceExecCommand service_exec_ex_command_from_string(const char *s) _pure_;
286
308d72dc
LP
287const char* notify_state_to_string(NotifyState i) _const_;
288NotifyState notify_state_from_string(const char *s) _pure_;
289
44a6b1b6
ZJS
290const char* service_result_to_string(ServiceResult i) _const_;
291ServiceResult service_result_from_string(const char *s) _pure_;
57b7a260 292
bf760801
JK
293const char* service_timeout_failure_mode_to_string(ServiceTimeoutFailureMode i) _const_;
294ServiceTimeoutFailureMode service_timeout_failure_mode_from_string(const char *s) _pure_;
295
57b7a260 296DEFINE_CAST(SERVICE, Service);
3eac1bca
LP
297
298#define STATUS_TEXT_MAX (16U*1024U)
35243b77
ZJS
299
300/* Only exported for unit tests */
301int service_deserialize_exec_command(Unit *u, const char *key, const char *value);