]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/service.h
NEWS: finalize for v256~rc3
[thirdparty/systemd.git] / src / core / service.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 typedef struct Service Service;
5 typedef struct ServiceFDStore ServiceFDStore;
6
7 #include "exit-status.h"
8 #include "kill.h"
9 #include "open-file.h"
10 #include "path.h"
11 #include "pidref.h"
12 #include "ratelimit.h"
13 #include "socket.h"
14 #include "unit.h"
15
16 typedef enum ServiceRestart {
17 SERVICE_RESTART_NO,
18 SERVICE_RESTART_ON_SUCCESS,
19 SERVICE_RESTART_ON_FAILURE,
20 SERVICE_RESTART_ON_ABNORMAL,
21 SERVICE_RESTART_ON_WATCHDOG,
22 SERVICE_RESTART_ON_ABORT,
23 SERVICE_RESTART_ALWAYS,
24 _SERVICE_RESTART_MAX,
25 _SERVICE_RESTART_INVALID = -EINVAL,
26 } ServiceRestart;
27
28 typedef enum ServiceType {
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) */
37 _SERVICE_TYPE_MAX,
38 _SERVICE_TYPE_INVALID = -EINVAL,
39 } ServiceType;
40
41 typedef 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
48 typedef enum ServiceExecCommand {
49 SERVICE_EXEC_CONDITION,
50 SERVICE_EXEC_START_PRE,
51 SERVICE_EXEC_START,
52 SERVICE_EXEC_START_POST,
53 SERVICE_EXEC_RELOAD,
54 SERVICE_EXEC_STOP,
55 SERVICE_EXEC_STOP_POST,
56 _SERVICE_EXEC_COMMAND_MAX,
57 _SERVICE_EXEC_COMMAND_INVALID = -EINVAL,
58 } ServiceExecCommand;
59
60 typedef enum NotifyState {
61 NOTIFY_UNKNOWN,
62 NOTIFY_READY,
63 NOTIFY_RELOADING,
64 NOTIFY_STOPPING,
65 _NOTIFY_STATE_MAX,
66 _NOTIFY_STATE_INVALID = -EINVAL,
67 } NotifyState;
68
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. */
71 typedef enum ServiceResult {
72 SERVICE_SUCCESS,
73 SERVICE_FAILURE_RESOURCES, /* a bit of a misnomer, just our catch-all error for errnos we didn't expect */
74 SERVICE_FAILURE_PROTOCOL,
75 SERVICE_FAILURE_TIMEOUT,
76 SERVICE_FAILURE_EXIT_CODE,
77 SERVICE_FAILURE_SIGNAL,
78 SERVICE_FAILURE_CORE_DUMP,
79 SERVICE_FAILURE_WATCHDOG,
80 SERVICE_FAILURE_START_LIMIT_HIT,
81 SERVICE_FAILURE_OOM_KILL, /* OOM Kill by the Kernel or systemd-oomd */
82 SERVICE_SKIP_CONDITION,
83 _SERVICE_RESULT_MAX,
84 _SERVICE_RESULT_INVALID = -EINVAL,
85 } ServiceResult;
86
87 typedef enum ServiceTimeoutFailureMode {
88 SERVICE_TIMEOUT_TERMINATE,
89 SERVICE_TIMEOUT_ABORT,
90 SERVICE_TIMEOUT_KILL,
91 _SERVICE_TIMEOUT_FAILURE_MODE_MAX,
92 _SERVICE_TIMEOUT_FAILURE_MODE_INVALID = -EINVAL,
93 } ServiceTimeoutFailureMode;
94
95 typedef 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
102 struct ServiceFDStore {
103 Service *service;
104
105 int fd;
106 char *fdname;
107 sd_event_source *event_source;
108 bool do_poll;
109
110 LIST_FIELDS(ServiceFDStore, fd_store);
111 };
112
113 struct Service {
114 Unit meta;
115
116 ServiceType type;
117 ServiceExitType exit_type;
118 ServiceRestart restart;
119 ServiceRestartMode restart_mode;
120 ExitStatusSet restart_prevent_status;
121 ExitStatusSet restart_force_status;
122 ExitStatusSet success_status;
123
124 /* If set we'll read the main daemon PID from this file */
125 char *pid_file;
126
127 usec_t restart_usec;
128 unsigned restart_steps;
129 usec_t restart_max_delay_usec;
130 usec_t timeout_start_usec;
131 usec_t timeout_stop_usec;
132 usec_t timeout_abort_usec;
133 bool timeout_abort_set;
134 usec_t runtime_max_usec;
135 usec_t runtime_rand_extra_usec;
136 ServiceTimeoutFailureMode timeout_start_failure_mode;
137 ServiceTimeoutFailureMode timeout_stop_failure_mode;
138
139 dual_timestamp watchdog_timestamp;
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() */
143 bool watchdog_override_enable;
144 sd_event_source *watchdog_event_source;
145
146 ExecCommand* exec_command[_SERVICE_EXEC_COMMAND_MAX];
147
148 ExecContext exec_context;
149 KillContext kill_context;
150 CGroupContext cgroup_context;
151
152 ServiceState state, deserialized_state;
153
154 /* The exit status of the real main process */
155 ExecStatus main_exec_status;
156
157 /* The currently executed control process */
158 ExecCommand *control_command;
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 */
166 ServiceExecCommand control_command_id;
167
168 /* Runtime data of the execution context */
169 ExecRuntime *exec_runtime;
170
171 CGroupRuntime *cgroup_runtime;
172
173 PidRef main_pid, control_pid;
174
175 /* if we are a socket activated service instance, store information of the connection/peer/socket */
176 int socket_fd;
177 SocketPeer *socket_peer;
178 UnitRef accept_socket;
179 bool socket_fd_selinux_context_net;
180
181 bool permissions_start_only;
182 bool root_directory_start_only;
183 bool remain_after_exit;
184 bool guess_main_pid;
185
186 /* If we shut down, remember why */
187 ServiceResult result;
188 ServiceResult reload_result;
189 ServiceResult clean_result;
190
191 bool main_pid_known:1;
192 bool main_pid_alien:1;
193 bool bus_name_good:1;
194 bool forbid_restart:1;
195 bool start_timeout_defined:1;
196 bool exec_fd_hot:1;
197
198 char *bus_name;
199 char *bus_name_owner; /* unique name of the current owner */
200
201 char *status_text;
202 int status_errno;
203
204 sd_event_source *timer_event_source;
205 PathSpec *pid_file_pathspec;
206
207 NotifyAccess notify_access;
208 NotifyAccess notify_access_override;
209 NotifyState notify_state;
210
211 sd_bus_slot *bus_name_pid_lookup_slot;
212
213 sd_event_source *exec_fd_event_source;
214
215 ServiceFDStore *fd_store;
216 size_t n_fd_store;
217 unsigned n_fd_store_max;
218 ExecPreserveMode fd_store_preserve_mode;
219
220 char *usb_function_descriptors;
221 char *usb_function_strings;
222
223 int stdin_fd;
224 int stdout_fd;
225 int stderr_fd;
226
227 unsigned n_restarts;
228 bool flush_n_restarts;
229
230 OOMPolicy oom_policy;
231
232 LIST_HEAD(OpenFile, open_files);
233
234 int reload_signal;
235 usec_t reload_begin_usec;
236 };
237
238 static inline usec_t service_timeout_abort_usec(Service *s) {
239 assert(s);
240 return s->timeout_abort_set ? s->timeout_abort_usec : s->timeout_stop_usec;
241 }
242
243 static 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
248 static 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
253 extern const UnitVTable service_vtable;
254
255 int service_set_socket_fd(Service *s, int fd, struct Socket *socket, struct SocketPeer *peer, bool selinux_context_net);
256 void service_release_socket_fd(Service *s);
257
258 usec_t service_restart_usec_next(Service *s);
259
260 int service_determine_exec_selinux_label(Service *s, char **ret);
261
262 const char* service_restart_to_string(ServiceRestart i) _const_;
263 ServiceRestart service_restart_from_string(const char *s) _pure_;
264
265 const char* service_restart_mode_to_string(ServiceRestartMode i) _const_;
266 ServiceRestartMode service_restart_mode_from_string(const char *s) _pure_;
267
268 const char* service_type_to_string(ServiceType i) _const_;
269 ServiceType service_type_from_string(const char *s) _pure_;
270
271 const char* service_exit_type_to_string(ServiceExitType i) _const_;
272 ServiceExitType service_exit_type_from_string(const char *s) _pure_;
273
274 const char* service_exec_command_to_string(ServiceExecCommand i) _const_;
275 ServiceExecCommand service_exec_command_from_string(const char *s) _pure_;
276
277 const char* service_exec_ex_command_to_string(ServiceExecCommand i) _const_;
278 ServiceExecCommand service_exec_ex_command_from_string(const char *s) _pure_;
279
280 const char* notify_state_to_string(NotifyState i) _const_;
281 NotifyState notify_state_from_string(const char *s) _pure_;
282
283 const char* service_result_to_string(ServiceResult i) _const_;
284 ServiceResult service_result_from_string(const char *s) _pure_;
285
286 const char* service_timeout_failure_mode_to_string(ServiceTimeoutFailureMode i) _const_;
287 ServiceTimeoutFailureMode service_timeout_failure_mode_from_string(const char *s) _pure_;
288
289 DEFINE_CAST(SERVICE, Service);
290
291 #define STATUS_TEXT_MAX (16U*1024U)
292
293 /* Only exported for unit tests */
294 int service_deserialize_exec_command(Unit *u, const char *key, const char *value);