1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 typedef struct Service Service
;
5 typedef struct ServiceFDStore ServiceFDStore
;
7 #include "exit-status.h"
12 #include "ratelimit.h"
16 typedef enum ServiceRestart
{
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
,
25 _SERVICE_RESTART_INVALID
= -EINVAL
,
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) */
38 _SERVICE_TYPE_INVALID
= -EINVAL
,
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
,
48 typedef enum ServiceExecCommand
{
49 SERVICE_EXEC_CONDITION
,
50 SERVICE_EXEC_START_PRE
,
52 SERVICE_EXEC_START_POST
,
55 SERVICE_EXEC_STOP_POST
,
56 _SERVICE_EXEC_COMMAND_MAX
,
57 _SERVICE_EXEC_COMMAND_INVALID
= -EINVAL
,
60 typedef enum NotifyState
{
66 _NOTIFY_STATE_INVALID
= -EINVAL
,
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
{
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
,
84 _SERVICE_RESULT_INVALID
= -EINVAL
,
87 typedef enum ServiceTimeoutFailureMode
{
88 SERVICE_TIMEOUT_TERMINATE
,
89 SERVICE_TIMEOUT_ABORT
,
91 _SERVICE_TIMEOUT_FAILURE_MODE_MAX
,
92 _SERVICE_TIMEOUT_FAILURE_MODE_INVALID
= -EINVAL
,
93 } ServiceTimeoutFailureMode
;
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
;
102 struct ServiceFDStore
{
107 sd_event_source
*event_source
;
110 LIST_FIELDS(ServiceFDStore
, fd_store
);
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
;
124 /* If set we'll read the main daemon PID from this file */
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
;
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
;
146 ExecCommand
* exec_command
[_SERVICE_EXEC_COMMAND_MAX
];
148 ExecContext exec_context
;
149 KillContext kill_context
;
150 CGroupContext cgroup_context
;
152 ServiceState state
, deserialized_state
;
154 /* The exit status of the real main process */
155 ExecStatus main_exec_status
;
157 /* The currently executed control process */
158 ExecCommand
*control_command
;
160 /* The currently executed main process, which may be NULL if
161 * the main process got started via forking mode and not by
163 ExecCommand
*main_command
;
165 /* The ID of the control command currently being executed */
166 ServiceExecCommand control_command_id
;
168 /* Runtime data of the execution context */
169 ExecRuntime
*exec_runtime
;
171 CGroupRuntime
*cgroup_runtime
;
173 PidRef main_pid
, control_pid
;
175 /* if we are a socket activated service instance, store information of the connection/peer/socket */
177 SocketPeer
*socket_peer
;
178 UnitRef accept_socket
;
179 bool socket_fd_selinux_context_net
;
181 bool permissions_start_only
;
182 bool root_directory_start_only
;
183 bool remain_after_exit
;
186 /* If we shut down, remember why */
187 ServiceResult result
;
188 ServiceResult reload_result
;
189 ServiceResult clean_result
;
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;
199 char *bus_name_owner
; /* unique name of the current owner */
204 sd_event_source
*timer_event_source
;
205 PathSpec
*pid_file_pathspec
;
207 NotifyAccess notify_access
;
208 NotifyAccess notify_access_override
;
209 NotifyState notify_state
;
211 sd_bus_slot
*bus_name_pid_lookup_slot
;
213 sd_event_source
*exec_fd_event_source
;
215 ServiceFDStore
*fd_store
;
217 unsigned n_fd_store_max
;
218 ExecPreserveMode fd_store_preserve_mode
;
220 char *usb_function_descriptors
;
221 char *usb_function_strings
;
228 bool flush_n_restarts
;
230 OOMPolicy oom_policy
;
232 LIST_HEAD(OpenFile
, open_files
);
235 usec_t reload_begin_usec
;
238 static inline usec_t
service_timeout_abort_usec(Service
*s
) {
240 return s
->timeout_abort_set
? s
->timeout_abort_usec
: s
->timeout_stop_usec
;
243 static inline NotifyAccess
service_get_notify_access(Service
*s
) {
245 return s
->notify_access_override
< 0 ? s
->notify_access
: s
->notify_access_override
;
248 static inline usec_t
service_get_watchdog_usec(Service
*s
) {
250 return s
->watchdog_override_enable
? s
->watchdog_override_usec
: s
->watchdog_original_usec
;
253 extern const UnitVTable service_vtable
;
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
);
258 usec_t
service_restart_usec_next(Service
*s
);
260 int service_determine_exec_selinux_label(Service
*s
, char **ret
);
262 const char* service_restart_to_string(ServiceRestart i
) _const_
;
263 ServiceRestart
service_restart_from_string(const char *s
) _pure_
;
265 const char* service_restart_mode_to_string(ServiceRestartMode i
) _const_
;
266 ServiceRestartMode
service_restart_mode_from_string(const char *s
) _pure_
;
268 const char* service_type_to_string(ServiceType i
) _const_
;
269 ServiceType
service_type_from_string(const char *s
) _pure_
;
271 const char* service_exit_type_to_string(ServiceExitType i
) _const_
;
272 ServiceExitType
service_exit_type_from_string(const char *s
) _pure_
;
274 const char* service_exec_command_to_string(ServiceExecCommand i
) _const_
;
275 ServiceExecCommand
service_exec_command_from_string(const char *s
) _pure_
;
277 const char* service_exec_ex_command_to_string(ServiceExecCommand i
) _const_
;
278 ServiceExecCommand
service_exec_ex_command_from_string(const char *s
) _pure_
;
280 const char* notify_state_to_string(NotifyState i
) _const_
;
281 NotifyState
notify_state_from_string(const char *s
) _pure_
;
283 const char* service_result_to_string(ServiceResult i
) _const_
;
284 ServiceResult
service_result_from_string(const char *s
) _pure_
;
286 const char* service_timeout_failure_mode_to_string(ServiceTimeoutFailureMode i
) _const_
;
287 ServiceTimeoutFailureMode
service_timeout_failure_mode_from_string(const char *s
) _pure_
;
289 DEFINE_CAST(SERVICE
, Service
);
291 #define STATUS_TEXT_MAX (16U*1024U)
293 /* Only exported for unit tests */
294 int service_deserialize_exec_command(Unit
*u
, const char *key
, const char *value
);