1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 typedef struct Service Service
;
5 typedef struct ServiceFDStore ServiceFDStore
;
7 #include "exit-status.h"
11 #include "ratelimit.h"
15 typedef enum ServiceRestart
{
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
,
24 _SERVICE_RESTART_INVALID
= -EINVAL
,
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) */
36 _SERVICE_TYPE_INVALID
= -EINVAL
,
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
,
46 typedef enum ServiceExecCommand
{
47 SERVICE_EXEC_CONDITION
,
48 SERVICE_EXEC_START_PRE
,
50 SERVICE_EXEC_START_POST
,
53 SERVICE_EXEC_STOP_POST
,
54 _SERVICE_EXEC_COMMAND_MAX
,
55 _SERVICE_EXEC_COMMAND_INVALID
= -EINVAL
,
58 typedef enum NotifyState
{
64 _NOTIFY_STATE_INVALID
= -EINVAL
,
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
{
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
,
82 _SERVICE_RESULT_INVALID
= -EINVAL
,
85 typedef enum ServiceTimeoutFailureMode
{
86 SERVICE_TIMEOUT_TERMINATE
,
87 SERVICE_TIMEOUT_ABORT
,
89 _SERVICE_TIMEOUT_FAILURE_MODE_MAX
,
90 _SERVICE_TIMEOUT_FAILURE_MODE_INVALID
= -EINVAL
,
91 } ServiceTimeoutFailureMode
;
93 struct ServiceFDStore
{
98 sd_event_source
*event_source
;
101 LIST_FIELDS(ServiceFDStore
, fd_store
);
108 ServiceExitType exit_type
;
109 ServiceRestart restart
;
110 ExitStatusSet restart_prevent_status
;
111 ExitStatusSet restart_force_status
;
112 ExitStatusSet success_status
;
114 /* If set we'll read the main daemon PID from this file */
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
;
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
;
134 ExecCommand
* exec_command
[_SERVICE_EXEC_COMMAND_MAX
];
136 ExecContext exec_context
;
137 KillContext kill_context
;
138 CGroupContext cgroup_context
;
140 ServiceState state
, deserialized_state
;
142 /* The exit status of the real main process */
143 ExecStatus main_exec_status
;
145 /* The currently executed control process */
146 ExecCommand
*control_command
;
148 /* The currently executed main process, which may be NULL if
149 * the main process got started via forking mode and not by
151 ExecCommand
*main_command
;
153 /* The ID of the control command currently being executed */
154 ServiceExecCommand control_command_id
;
156 /* Runtime data of the execution context */
157 ExecRuntime
*exec_runtime
;
158 DynamicCreds dynamic_creds
;
160 pid_t main_pid
, control_pid
;
162 /* if we are a socket activated service instance, store information of the connection/peer/socket */
164 SocketPeer
*socket_peer
;
165 UnitRef accept_socket
;
166 bool socket_fd_selinux_context_net
;
168 bool permissions_start_only
;
169 bool root_directory_start_only
;
170 bool remain_after_exit
;
173 /* If we shut down, remember why */
174 ServiceResult result
;
175 ServiceResult reload_result
;
176 ServiceResult clean_result
;
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;
188 char *bus_name_owner
; /* unique name of the current owner */
193 sd_event_source
*timer_event_source
;
194 PathSpec
*pid_file_pathspec
;
196 NotifyAccess notify_access
;
197 NotifyState notify_state
;
199 sd_bus_slot
*bus_name_pid_lookup_slot
;
201 sd_event_source
*exec_fd_event_source
;
203 ServiceFDStore
*fd_store
;
205 unsigned n_fd_store_max
;
206 unsigned n_keep_fd_store
;
208 char *usb_function_descriptors
;
209 char *usb_function_strings
;
216 bool flush_n_restarts
;
218 OOMPolicy oom_policy
;
220 LIST_HEAD(OpenFile
, open_files
);
223 static inline usec_t
service_timeout_abort_usec(Service
*s
) {
225 return s
->timeout_abort_set
? s
->timeout_abort_usec
: s
->timeout_stop_usec
;
228 static inline usec_t
service_get_watchdog_usec(Service
*s
) {
230 return s
->watchdog_override_enable
? s
->watchdog_override_usec
: s
->watchdog_original_usec
;
233 extern const UnitVTable service_vtable
;
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
);
238 const char* service_restart_to_string(ServiceRestart i
) _const_
;
239 ServiceRestart
service_restart_from_string(const char *s
) _pure_
;
241 const char* service_type_to_string(ServiceType i
) _const_
;
242 ServiceType
service_type_from_string(const char *s
) _pure_
;
244 const char* service_exit_type_to_string(ServiceExitType i
) _const_
;
245 ServiceExitType
service_exit_type_from_string(const char *s
) _pure_
;
247 const char* service_exec_command_to_string(ServiceExecCommand i
) _const_
;
248 ServiceExecCommand
service_exec_command_from_string(const char *s
) _pure_
;
250 const char* service_exec_ex_command_to_string(ServiceExecCommand i
) _const_
;
251 ServiceExecCommand
service_exec_ex_command_from_string(const char *s
) _pure_
;
253 const char* notify_state_to_string(NotifyState i
) _const_
;
254 NotifyState
notify_state_from_string(const char *s
) _pure_
;
256 const char* service_result_to_string(ServiceResult i
) _const_
;
257 ServiceResult
service_result_from_string(const char *s
) _pure_
;
259 const char* service_timeout_failure_mode_to_string(ServiceTimeoutFailureMode i
) _const_
;
260 ServiceTimeoutFailureMode
service_timeout_failure_mode_from_string(const char *s
) _pure_
;
262 DEFINE_CAST(SERVICE
, Service
);
264 #define STATUS_TEXT_MAX (16U*1024U)
266 /* Only exported for unit tests */
267 int service_deserialize_exec_command(Unit
*u
, const char *key
, const char *value
);