Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */
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 = -1
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 = -1
36 : } ServiceType;
37 :
38 : typedef enum ServiceExecCommand {
39 : SERVICE_EXEC_CONDITION,
40 : SERVICE_EXEC_START_PRE,
41 : SERVICE_EXEC_START,
42 : SERVICE_EXEC_START_POST,
43 : SERVICE_EXEC_RELOAD,
44 : SERVICE_EXEC_STOP,
45 : SERVICE_EXEC_STOP_POST,
46 : _SERVICE_EXEC_COMMAND_MAX,
47 : _SERVICE_EXEC_COMMAND_INVALID = -1
48 : } ServiceExecCommand;
49 :
50 : typedef enum NotifyState {
51 : NOTIFY_UNKNOWN,
52 : NOTIFY_READY,
53 : NOTIFY_RELOADING,
54 : NOTIFY_STOPPING,
55 : _NOTIFY_STATE_MAX,
56 : _NOTIFY_STATE_INVALID = -1
57 : } NotifyState;
58 :
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 {
62 : SERVICE_SUCCESS,
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,
73 : _SERVICE_RESULT_MAX,
74 : _SERVICE_RESULT_INVALID = -1
75 : } ServiceResult;
76 :
77 : struct ServiceFDStore {
78 : Service *service;
79 :
80 : int fd;
81 : char *fdname;
82 : sd_event_source *event_source;
83 :
84 : LIST_FIELDS(ServiceFDStore, fd_store);
85 : };
86 :
87 : struct Service {
88 : Unit meta;
89 :
90 : ServiceType type;
91 : ServiceRestart restart;
92 : ExitStatusSet restart_prevent_status;
93 : ExitStatusSet restart_force_status;
94 : ExitStatusSet success_status;
95 :
96 : /* If set we'll read the main daemon PID from this file */
97 : char *pid_file;
98 :
99 : usec_t restart_usec;
100 : usec_t timeout_start_usec;
101 : usec_t timeout_stop_usec;
102 : usec_t timeout_abort_usec;
103 : bool timeout_abort_set;
104 : usec_t timeout_clean_usec;
105 : usec_t runtime_max_usec;
106 :
107 : dual_timestamp watchdog_timestamp;
108 : usec_t watchdog_usec; /* the requested watchdog timeout in the unit file */
109 : 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 */
110 : usec_t watchdog_override_usec; /* the watchdog timeout requested by the service itself through sd_notify() */
111 : bool watchdog_override_enable;
112 : sd_event_source *watchdog_event_source;
113 :
114 : ExecCommand* exec_command[_SERVICE_EXEC_COMMAND_MAX];
115 :
116 : ExecContext exec_context;
117 : KillContext kill_context;
118 : CGroupContext cgroup_context;
119 :
120 : ServiceState state, deserialized_state;
121 :
122 : /* The exit status of the real main process */
123 : ExecStatus main_exec_status;
124 :
125 : /* The currently executed control process */
126 : ExecCommand *control_command;
127 :
128 : /* The currently executed main process, which may be NULL if
129 : * the main process got started via forking mode and not by
130 : * us */
131 : ExecCommand *main_command;
132 :
133 : /* The ID of the control command currently being executed */
134 : ServiceExecCommand control_command_id;
135 :
136 : /* Runtime data of the execution context */
137 : ExecRuntime *exec_runtime;
138 : DynamicCreds dynamic_creds;
139 :
140 : pid_t main_pid, control_pid;
141 : int socket_fd;
142 : SocketPeer *peer;
143 : bool socket_fd_selinux_context_net;
144 :
145 : bool permissions_start_only;
146 : bool root_directory_start_only;
147 : bool remain_after_exit;
148 : bool guess_main_pid;
149 :
150 : /* If we shut down, remember why */
151 : ServiceResult result;
152 : ServiceResult reload_result;
153 : ServiceResult clean_result;
154 :
155 : bool main_pid_known:1;
156 : bool main_pid_alien:1;
157 : bool bus_name_good:1;
158 : bool forbid_restart:1;
159 : /* Keep restart intention between UNIT_FAILED and UNIT_ACTIVATING */
160 : bool will_auto_restart:1;
161 : bool start_timeout_defined:1;
162 : bool exec_fd_hot:1;
163 :
164 : char *bus_name;
165 : char *bus_name_owner; /* unique name of the current owner */
166 :
167 : char *status_text;
168 : int status_errno;
169 :
170 : UnitRef accept_socket;
171 :
172 : sd_event_source *timer_event_source;
173 : PathSpec *pid_file_pathspec;
174 :
175 : NotifyAccess notify_access;
176 : NotifyState notify_state;
177 :
178 : sd_event_source *exec_fd_event_source;
179 :
180 : ServiceFDStore *fd_store;
181 : size_t n_fd_store;
182 : unsigned n_fd_store_max;
183 : unsigned n_keep_fd_store;
184 :
185 : char *usb_function_descriptors;
186 : char *usb_function_strings;
187 :
188 : int stdin_fd;
189 : int stdout_fd;
190 : int stderr_fd;
191 :
192 : unsigned n_restarts;
193 : bool flush_n_restarts;
194 :
195 : OOMPolicy oom_policy;
196 : };
197 :
198 0 : static inline usec_t service_timeout_abort_usec(Service *s) {
199 0 : assert(s);
200 0 : return s->timeout_abort_set ? s->timeout_abort_usec : s->timeout_stop_usec;
201 : }
202 :
203 : extern const UnitVTable service_vtable;
204 :
205 : int service_set_socket_fd(Service *s, int fd, struct Socket *socket, bool selinux_context_net);
206 : void service_close_socket_fd(Service *s);
207 :
208 : const char* service_restart_to_string(ServiceRestart i) _const_;
209 : ServiceRestart service_restart_from_string(const char *s) _pure_;
210 :
211 : const char* service_type_to_string(ServiceType i) _const_;
212 : ServiceType service_type_from_string(const char *s) _pure_;
213 :
214 : const char* service_exec_command_to_string(ServiceExecCommand i) _const_;
215 : ServiceExecCommand service_exec_command_from_string(const char *s) _pure_;
216 :
217 : const char* service_exec_ex_command_to_string(ServiceExecCommand i) _const_;
218 : ServiceExecCommand service_exec_ex_command_from_string(const char *s) _pure_;
219 :
220 : const char* notify_state_to_string(NotifyState i) _const_;
221 : NotifyState notify_state_from_string(const char *s) _pure_;
222 :
223 : const char* service_result_to_string(ServiceResult i) _const_;
224 : ServiceResult service_result_from_string(const char *s) _pure_;
225 :
226 1186 : DEFINE_CAST(SERVICE, Service);
227 :
228 : #define STATUS_TEXT_MAX (16U*1024U)
|