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