]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/service.h
core: add OpenFile setting
[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 "ratelimit.h"
12 #include "socket.h"
13 #include "unit.h"
14
15 typedef enum ServiceRestart {
16 SERVICE_RESTART_NO,
17 SERVICE_RESTART_ON_SUCCESS,
18 SERVICE_RESTART_ON_FAILURE,
19 SERVICE_RESTART_ON_ABNORMAL,
20 SERVICE_RESTART_ON_WATCHDOG,
21 SERVICE_RESTART_ON_ABORT,
22 SERVICE_RESTART_ALWAYS,
23 _SERVICE_RESTART_MAX,
24 _SERVICE_RESTART_INVALID = -EINVAL,
25 } ServiceRestart;
26
27 typedef enum ServiceType {
28 SERVICE_SIMPLE, /* we fork and go on right-away (i.e. modern socket activated daemons) */
29 SERVICE_FORKING, /* forks by itself (i.e. traditional daemons) */
30 SERVICE_ONESHOT, /* we fork and wait until the program finishes (i.e. programs like fsck which run and need to finish before we continue) */
31 SERVICE_DBUS, /* we fork and wait until a specific D-Bus name appears on the bus */
32 SERVICE_NOTIFY, /* we fork and wait until a daemon sends us a ready message with sd_notify() */
33 SERVICE_IDLE, /* much like simple, but delay exec() until all jobs are dispatched. */
34 SERVICE_EXEC, /* we fork and wait until we execute exec() (this means our own setup is waited for) */
35 _SERVICE_TYPE_MAX,
36 _SERVICE_TYPE_INVALID = -EINVAL,
37 } ServiceType;
38
39 typedef enum ServiceExitType {
40 SERVICE_EXIT_MAIN, /* we consider the main PID when deciding if the service exited */
41 SERVICE_EXIT_CGROUP, /* we wait for the last process in the cgroup to exit */
42 _SERVICE_EXIT_TYPE_MAX,
43 _SERVICE_EXIT_TYPE_INVALID = -EINVAL,
44 } ServiceExitType;
45
46 typedef enum ServiceExecCommand {
47 SERVICE_EXEC_CONDITION,
48 SERVICE_EXEC_START_PRE,
49 SERVICE_EXEC_START,
50 SERVICE_EXEC_START_POST,
51 SERVICE_EXEC_RELOAD,
52 SERVICE_EXEC_STOP,
53 SERVICE_EXEC_STOP_POST,
54 _SERVICE_EXEC_COMMAND_MAX,
55 _SERVICE_EXEC_COMMAND_INVALID = -EINVAL,
56 } ServiceExecCommand;
57
58 typedef enum NotifyState {
59 NOTIFY_UNKNOWN,
60 NOTIFY_READY,
61 NOTIFY_RELOADING,
62 NOTIFY_STOPPING,
63 _NOTIFY_STATE_MAX,
64 _NOTIFY_STATE_INVALID = -EINVAL,
65 } NotifyState;
66
67 /* The values of this enum are referenced in man/systemd.exec.xml and src/shared/bus-unit-util.c.
68 * Update those sources for each change to this enum. */
69 typedef enum ServiceResult {
70 SERVICE_SUCCESS,
71 SERVICE_FAILURE_RESOURCES, /* a bit of a misnomer, just our catch-all error for errnos we didn't expect */
72 SERVICE_FAILURE_PROTOCOL,
73 SERVICE_FAILURE_TIMEOUT,
74 SERVICE_FAILURE_EXIT_CODE,
75 SERVICE_FAILURE_SIGNAL,
76 SERVICE_FAILURE_CORE_DUMP,
77 SERVICE_FAILURE_WATCHDOG,
78 SERVICE_FAILURE_START_LIMIT_HIT,
79 SERVICE_FAILURE_OOM_KILL, /* OOM Kill by the Kernel or systemd-oomd */
80 SERVICE_SKIP_CONDITION,
81 _SERVICE_RESULT_MAX,
82 _SERVICE_RESULT_INVALID = -EINVAL,
83 } ServiceResult;
84
85 typedef enum ServiceTimeoutFailureMode {
86 SERVICE_TIMEOUT_TERMINATE,
87 SERVICE_TIMEOUT_ABORT,
88 SERVICE_TIMEOUT_KILL,
89 _SERVICE_TIMEOUT_FAILURE_MODE_MAX,
90 _SERVICE_TIMEOUT_FAILURE_MODE_INVALID = -EINVAL,
91 } ServiceTimeoutFailureMode;
92
93 struct ServiceFDStore {
94 Service *service;
95
96 int fd;
97 char *fdname;
98 sd_event_source *event_source;
99 bool do_poll;
100
101 LIST_FIELDS(ServiceFDStore, fd_store);
102 };
103
104 struct Service {
105 Unit meta;
106
107 ServiceType type;
108 ServiceExitType exit_type;
109 ServiceRestart restart;
110 ExitStatusSet restart_prevent_status;
111 ExitStatusSet restart_force_status;
112 ExitStatusSet success_status;
113
114 /* If set we'll read the main daemon PID from this file */
115 char *pid_file;
116
117 usec_t restart_usec;
118 usec_t timeout_start_usec;
119 usec_t timeout_stop_usec;
120 usec_t timeout_abort_usec;
121 bool timeout_abort_set;
122 usec_t runtime_max_usec;
123 usec_t runtime_rand_extra_usec;
124 ServiceTimeoutFailureMode timeout_start_failure_mode;
125 ServiceTimeoutFailureMode timeout_stop_failure_mode;
126
127 dual_timestamp watchdog_timestamp;
128 usec_t watchdog_usec; /* the requested watchdog timeout in the unit file */
129 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 */
130 usec_t watchdog_override_usec; /* the watchdog timeout requested by the service itself through sd_notify() */
131 bool watchdog_override_enable;
132 sd_event_source *watchdog_event_source;
133
134 ExecCommand* exec_command[_SERVICE_EXEC_COMMAND_MAX];
135
136 ExecContext exec_context;
137 KillContext kill_context;
138 CGroupContext cgroup_context;
139
140 ServiceState state, deserialized_state;
141
142 /* The exit status of the real main process */
143 ExecStatus main_exec_status;
144
145 /* The currently executed control process */
146 ExecCommand *control_command;
147
148 /* The currently executed main process, which may be NULL if
149 * the main process got started via forking mode and not by
150 * us */
151 ExecCommand *main_command;
152
153 /* The ID of the control command currently being executed */
154 ServiceExecCommand control_command_id;
155
156 /* Runtime data of the execution context */
157 ExecRuntime *exec_runtime;
158 DynamicCreds dynamic_creds;
159
160 pid_t main_pid, control_pid;
161
162 /* if we are a socket activated service instance, store information of the connection/peer/socket */
163 int socket_fd;
164 SocketPeer *socket_peer;
165 UnitRef accept_socket;
166 bool socket_fd_selinux_context_net;
167
168 bool permissions_start_only;
169 bool root_directory_start_only;
170 bool remain_after_exit;
171 bool guess_main_pid;
172
173 /* If we shut down, remember why */
174 ServiceResult result;
175 ServiceResult reload_result;
176 ServiceResult clean_result;
177
178 bool main_pid_known:1;
179 bool main_pid_alien:1;
180 bool bus_name_good:1;
181 bool forbid_restart:1;
182 /* Keep restart intention between UNIT_FAILED and UNIT_ACTIVATING */
183 bool will_auto_restart:1;
184 bool start_timeout_defined:1;
185 bool exec_fd_hot:1;
186
187 char *bus_name;
188 char *bus_name_owner; /* unique name of the current owner */
189
190 char *status_text;
191 int status_errno;
192
193 sd_event_source *timer_event_source;
194 PathSpec *pid_file_pathspec;
195
196 NotifyAccess notify_access;
197 NotifyState notify_state;
198
199 sd_bus_slot *bus_name_pid_lookup_slot;
200
201 sd_event_source *exec_fd_event_source;
202
203 ServiceFDStore *fd_store;
204 size_t n_fd_store;
205 unsigned n_fd_store_max;
206 unsigned n_keep_fd_store;
207
208 char *usb_function_descriptors;
209 char *usb_function_strings;
210
211 int stdin_fd;
212 int stdout_fd;
213 int stderr_fd;
214
215 unsigned n_restarts;
216 bool flush_n_restarts;
217
218 OOMPolicy oom_policy;
219
220 LIST_HEAD(OpenFile, open_files);
221 };
222
223 static inline usec_t service_timeout_abort_usec(Service *s) {
224 assert(s);
225 return s->timeout_abort_set ? s->timeout_abort_usec : s->timeout_stop_usec;
226 }
227
228 static inline usec_t service_get_watchdog_usec(Service *s) {
229 assert(s);
230 return s->watchdog_override_enable ? s->watchdog_override_usec : s->watchdog_original_usec;
231 }
232
233 extern const UnitVTable service_vtable;
234
235 int service_set_socket_fd(Service *s, int fd, struct Socket *socket, struct SocketPeer *peer, bool selinux_context_net);
236 void service_close_socket_fd(Service *s);
237
238 const char* service_restart_to_string(ServiceRestart i) _const_;
239 ServiceRestart service_restart_from_string(const char *s) _pure_;
240
241 const char* service_type_to_string(ServiceType i) _const_;
242 ServiceType service_type_from_string(const char *s) _pure_;
243
244 const char* service_exit_type_to_string(ServiceExitType i) _const_;
245 ServiceExitType service_exit_type_from_string(const char *s) _pure_;
246
247 const char* service_exec_command_to_string(ServiceExecCommand i) _const_;
248 ServiceExecCommand service_exec_command_from_string(const char *s) _pure_;
249
250 const char* service_exec_ex_command_to_string(ServiceExecCommand i) _const_;
251 ServiceExecCommand service_exec_ex_command_from_string(const char *s) _pure_;
252
253 const char* notify_state_to_string(NotifyState i) _const_;
254 NotifyState notify_state_from_string(const char *s) _pure_;
255
256 const char* service_result_to_string(ServiceResult i) _const_;
257 ServiceResult service_result_from_string(const char *s) _pure_;
258
259 const char* service_timeout_failure_mode_to_string(ServiceTimeoutFailureMode i) _const_;
260 ServiceTimeoutFailureMode service_timeout_failure_mode_from_string(const char *s) _pure_;
261
262 DEFINE_CAST(SERVICE, Service);
263
264 #define STATUS_TEXT_MAX (16U*1024U)
265
266 /* Only exported for unit tests */
267 int service_deserialize_exec_command(Unit *u, const char *key, const char *value);