1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 typedef struct Service Service
;
5 typedef struct ServiceFDStore ServiceFDStore
;
7 #include "exit-status.h"
10 #include "ratelimit.h"
14 typedef enum ServiceRestart
{
16 SERVICE_RESTART_ON_SUCCESS
,
17 SERVICE_RESTART_ON_FAILURE
,
18 SERVICE_RESTART_ON_ABNORMAL
,
19 SERVICE_RESTART_ON_WATCHDOG
,
20 SERVICE_RESTART_ON_ABORT
,
21 SERVICE_RESTART_ALWAYS
,
23 _SERVICE_RESTART_INVALID
= -EINVAL
,
26 typedef enum ServiceType
{
27 SERVICE_SIMPLE
, /* we fork and go on right-away (i.e. modern socket activated daemons) */
28 SERVICE_FORKING
, /* forks by itself (i.e. traditional daemons) */
29 SERVICE_ONESHOT
, /* we fork and wait until the program finishes (i.e. programs like fsck which run and need to finish before we continue) */
30 SERVICE_DBUS
, /* we fork and wait until a specific D-Bus name appears on the bus */
31 SERVICE_NOTIFY
, /* we fork and wait until a daemon sends us a ready message with sd_notify() */
32 SERVICE_IDLE
, /* much like simple, but delay exec() until all jobs are dispatched. */
33 SERVICE_EXEC
, /* we fork and wait until we execute exec() (this means our own setup is waited for) */
35 _SERVICE_TYPE_INVALID
= -EINVAL
,
38 typedef enum ServiceExecCommand
{
39 SERVICE_EXEC_CONDITION
,
40 SERVICE_EXEC_START_PRE
,
42 SERVICE_EXEC_START_POST
,
45 SERVICE_EXEC_STOP_POST
,
46 _SERVICE_EXEC_COMMAND_MAX
,
47 _SERVICE_EXEC_COMMAND_INVALID
= -EINVAL
,
50 typedef enum NotifyState
{
56 _NOTIFY_STATE_INVALID
= -EINVAL
,
59 /* The values of this enum are referenced in man/systemd.exec.xml and src/shared/bus-unit-util.c.
60 * Update those sources for each change to this enum. */
61 typedef enum ServiceResult
{
63 SERVICE_FAILURE_RESOURCES
, /* a bit of a misnomer, just our catch-all error for errnos we didn't expect */
64 SERVICE_FAILURE_PROTOCOL
,
65 SERVICE_FAILURE_TIMEOUT
,
66 SERVICE_FAILURE_EXIT_CODE
,
67 SERVICE_FAILURE_SIGNAL
,
68 SERVICE_FAILURE_CORE_DUMP
,
69 SERVICE_FAILURE_WATCHDOG
,
70 SERVICE_FAILURE_START_LIMIT_HIT
,
71 SERVICE_FAILURE_OOM_KILL
,
72 SERVICE_SKIP_CONDITION
,
74 _SERVICE_RESULT_INVALID
= -EINVAL
,
77 typedef enum ServiceTimeoutFailureMode
{
78 SERVICE_TIMEOUT_TERMINATE
,
79 SERVICE_TIMEOUT_ABORT
,
81 _SERVICE_TIMEOUT_FAILURE_MODE_MAX
,
82 _SERVICE_TIMEOUT_FAILURE_MODE_INVALID
= -EINVAL
,
83 } ServiceTimeoutFailureMode
;
85 struct ServiceFDStore
{
90 sd_event_source
*event_source
;
93 LIST_FIELDS(ServiceFDStore
, fd_store
);
100 ServiceRestart restart
;
101 ExitStatusSet restart_prevent_status
;
102 ExitStatusSet restart_force_status
;
103 ExitStatusSet success_status
;
105 /* If set we'll read the main daemon PID from this file */
109 usec_t timeout_start_usec
;
110 usec_t timeout_stop_usec
;
111 usec_t timeout_abort_usec
;
112 bool timeout_abort_set
;
113 usec_t runtime_max_usec
;
114 ServiceTimeoutFailureMode timeout_start_failure_mode
;
115 ServiceTimeoutFailureMode timeout_stop_failure_mode
;
117 dual_timestamp watchdog_timestamp
;
118 usec_t watchdog_usec
; /* the requested watchdog timeout in the unit file */
119 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 */
120 usec_t watchdog_override_usec
; /* the watchdog timeout requested by the service itself through sd_notify() */
121 bool watchdog_override_enable
;
122 sd_event_source
*watchdog_event_source
;
124 ExecCommand
* exec_command
[_SERVICE_EXEC_COMMAND_MAX
];
126 ExecContext exec_context
;
127 KillContext kill_context
;
128 CGroupContext cgroup_context
;
130 ServiceState state
, deserialized_state
;
132 /* The exit status of the real main process */
133 ExecStatus main_exec_status
;
135 /* The currently executed control process */
136 ExecCommand
*control_command
;
138 /* The currently executed main process, which may be NULL if
139 * the main process got started via forking mode and not by
141 ExecCommand
*main_command
;
143 /* The ID of the control command currently being executed */
144 ServiceExecCommand control_command_id
;
146 /* Runtime data of the execution context */
147 ExecRuntime
*exec_runtime
;
148 DynamicCreds dynamic_creds
;
150 pid_t main_pid
, control_pid
;
153 bool socket_fd_selinux_context_net
;
155 bool permissions_start_only
;
156 bool root_directory_start_only
;
157 bool remain_after_exit
;
160 /* If we shut down, remember why */
161 ServiceResult result
;
162 ServiceResult reload_result
;
163 ServiceResult clean_result
;
165 bool main_pid_known
:1;
166 bool main_pid_alien
:1;
167 bool bus_name_good
:1;
168 bool forbid_restart
:1;
169 /* Keep restart intention between UNIT_FAILED and UNIT_ACTIVATING */
170 bool will_auto_restart
:1;
171 bool start_timeout_defined
:1;
175 char *bus_name_owner
; /* unique name of the current owner */
180 UnitRef accept_socket
;
182 sd_event_source
*timer_event_source
;
183 PathSpec
*pid_file_pathspec
;
185 NotifyAccess notify_access
;
186 NotifyState notify_state
;
188 sd_event_source
*exec_fd_event_source
;
190 ServiceFDStore
*fd_store
;
192 unsigned n_fd_store_max
;
193 unsigned n_keep_fd_store
;
195 char *usb_function_descriptors
;
196 char *usb_function_strings
;
203 bool flush_n_restarts
;
205 OOMPolicy oom_policy
;
208 static inline usec_t
service_timeout_abort_usec(Service
*s
) {
210 return s
->timeout_abort_set
? s
->timeout_abort_usec
: s
->timeout_stop_usec
;
213 static inline usec_t
service_get_watchdog_usec(Service
*s
) {
215 return s
->watchdog_override_enable
? s
->watchdog_override_usec
: s
->watchdog_original_usec
;
218 extern const UnitVTable service_vtable
;
220 int service_set_socket_fd(Service
*s
, int fd
, struct Socket
*socket
, bool selinux_context_net
);
221 void service_close_socket_fd(Service
*s
);
223 const char* service_restart_to_string(ServiceRestart i
) _const_
;
224 ServiceRestart
service_restart_from_string(const char *s
) _pure_
;
226 const char* service_type_to_string(ServiceType i
) _const_
;
227 ServiceType
service_type_from_string(const char *s
) _pure_
;
229 const char* service_exec_command_to_string(ServiceExecCommand i
) _const_
;
230 ServiceExecCommand
service_exec_command_from_string(const char *s
) _pure_
;
232 const char* service_exec_ex_command_to_string(ServiceExecCommand i
) _const_
;
233 ServiceExecCommand
service_exec_ex_command_from_string(const char *s
) _pure_
;
235 const char* notify_state_to_string(NotifyState i
) _const_
;
236 NotifyState
notify_state_from_string(const char *s
) _pure_
;
238 const char* service_result_to_string(ServiceResult i
) _const_
;
239 ServiceResult
service_result_from_string(const char *s
) _pure_
;
241 const char* service_timeout_failure_mode_to_string(ServiceTimeoutFailureMode i
) _const_
;
242 ServiceTimeoutFailureMode
service_timeout_failure_mode_from_string(const char *s
) _pure_
;
244 DEFINE_CAST(SERVICE
, Service
);
246 #define STATUS_TEXT_MAX (16U*1024U)