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