]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
a7334b09 | 2 | |
3fc2a440 | 3 | #include <linux/audit.h> |
be1adc27 | 4 | #include <math.h> |
ca78ad1d | 5 | #include <sys/stat.h> |
2c4104f0 | 6 | #include <unistd.h> |
5cb5a6ff | 7 | |
836e4e7e | 8 | #include "sd-bus.h" |
7a0019d3 LP |
9 | #include "sd-messages.h" |
10 | ||
b5efdb8a | 11 | #include "alloc-util.h" |
574634bc | 12 | #include "async.h" |
5162829e | 13 | #include "bus-common-errors.h" |
4f5dd394 | 14 | #include "bus-error.h" |
4f5dd394 | 15 | #include "bus-util.h" |
f461a28d | 16 | #include "chase.h" |
4139c1b2 | 17 | #include "dbus-service.h" |
6fcbec6f | 18 | #include "dbus-unit.h" |
f2eb0c50 | 19 | #include "devnum-util.h" |
4d1a6904 | 20 | #include "env-util.h" |
836e4e7e | 21 | #include "errno-util.h" |
4f5dd394 | 22 | #include "escape.h" |
dfdeb0b1 | 23 | #include "execute.h" |
757e469d | 24 | #include "exec-credential.h" |
4f5dd394 | 25 | #include "exit-status.h" |
836e4e7e | 26 | #include "extract-word.h" |
3ffd4af2 | 27 | #include "fd-util.h" |
836e4e7e | 28 | #include "fdset.h" |
a5c32cff | 29 | #include "fileio.h" |
f97b34a6 | 30 | #include "format-util.h" |
836e4e7e DDM |
31 | #include "glyph-util.h" |
32 | #include "image-policy.h" | |
4f5dd394 LP |
33 | #include "log.h" |
34 | #include "manager.h" | |
5162829e | 35 | #include "mount-util.h" |
dfdeb0b1 | 36 | #include "namespace.h" |
cd48e23f | 37 | #include "open-file.h" |
6bedfcbb | 38 | #include "parse-util.h" |
4f5dd394 | 39 | #include "path-util.h" |
836e4e7e | 40 | #include "path.h" |
7933e971 | 41 | #include "pidfd-util.h" |
0b452006 | 42 | #include "process-util.h" |
5918a933 | 43 | #include "random-util.h" |
8017ed7e | 44 | #include "selinux-util.h" |
d68c645b | 45 | #include "serialize.h" |
3ffd4af2 | 46 | #include "service.h" |
24882e06 | 47 | #include "signal-util.h" |
836e4e7e | 48 | #include "socket.h" |
4f5dd394 | 49 | #include "special.h" |
836e4e7e | 50 | #include "stat-util.h" |
8b43440b | 51 | #include "string-table.h" |
07630cea | 52 | #include "string-util.h" |
4f5dd394 | 53 | #include "strv.h" |
569269d0 | 54 | #include "transaction.h" |
4f5dd394 | 55 | #include "unit.h" |
1cf40697 | 56 | #include "unit-name.h" |
4f5dd394 | 57 | #include "utf8.h" |
034c6ed7 | 58 | |
edbf8984 ZJS |
59 | #define service_spawn(...) service_spawn_internal(__func__, __VA_ARGS__) |
60 | ||
acbb0225 | 61 | static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = { |
17f6b640 YW |
62 | [SERVICE_DEAD] = UNIT_INACTIVE, |
63 | [SERVICE_CONDITION] = UNIT_ACTIVATING, | |
64 | [SERVICE_START_PRE] = UNIT_ACTIVATING, | |
65 | [SERVICE_START] = UNIT_ACTIVATING, | |
66 | [SERVICE_START_POST] = UNIT_ACTIVATING, | |
67 | [SERVICE_RUNNING] = UNIT_ACTIVE, | |
68 | [SERVICE_EXITED] = UNIT_ACTIVE, | |
69 | [SERVICE_RELOAD] = UNIT_RELOADING, | |
70 | [SERVICE_RELOAD_SIGNAL] = UNIT_RELOADING, | |
71 | [SERVICE_RELOAD_NOTIFY] = UNIT_RELOADING, | |
dfdeb0b1 | 72 | [SERVICE_REFRESH_EXTENSIONS] = UNIT_REFRESHING, |
a53e92a1 | 73 | [SERVICE_MOUNTING] = UNIT_REFRESHING, |
17f6b640 YW |
74 | [SERVICE_STOP] = UNIT_DEACTIVATING, |
75 | [SERVICE_STOP_WATCHDOG] = UNIT_DEACTIVATING, | |
76 | [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING, | |
77 | [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING, | |
78 | [SERVICE_STOP_POST] = UNIT_DEACTIVATING, | |
79 | [SERVICE_FINAL_WATCHDOG] = UNIT_DEACTIVATING, | |
80 | [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING, | |
81 | [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING, | |
82 | [SERVICE_FAILED] = UNIT_FAILED, | |
83 | [SERVICE_DEAD_BEFORE_AUTO_RESTART] = UNIT_INACTIVE, | |
a1d31573 | 84 | [SERVICE_FAILED_BEFORE_AUTO_RESTART] = UNIT_FAILED, |
17f6b640 YW |
85 | [SERVICE_DEAD_RESOURCES_PINNED] = UNIT_INACTIVE, |
86 | [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING, | |
87 | [SERVICE_AUTO_RESTART_QUEUED] = UNIT_ACTIVATING, | |
88 | [SERVICE_CLEANING] = UNIT_MAINTENANCE, | |
034c6ed7 | 89 | }; |
5cb5a6ff | 90 | |
e056b01d LP |
91 | /* For Type=idle we never want to delay any other jobs, hence we |
92 | * consider idle jobs active as soon as we start working on them */ | |
93 | static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = { | |
17f6b640 YW |
94 | [SERVICE_DEAD] = UNIT_INACTIVE, |
95 | [SERVICE_CONDITION] = UNIT_ACTIVE, | |
96 | [SERVICE_START_PRE] = UNIT_ACTIVE, | |
97 | [SERVICE_START] = UNIT_ACTIVE, | |
98 | [SERVICE_START_POST] = UNIT_ACTIVE, | |
99 | [SERVICE_RUNNING] = UNIT_ACTIVE, | |
100 | [SERVICE_EXITED] = UNIT_ACTIVE, | |
101 | [SERVICE_RELOAD] = UNIT_RELOADING, | |
102 | [SERVICE_RELOAD_SIGNAL] = UNIT_RELOADING, | |
103 | [SERVICE_RELOAD_NOTIFY] = UNIT_RELOADING, | |
dfdeb0b1 | 104 | [SERVICE_REFRESH_EXTENSIONS] = UNIT_REFRESHING, |
a53e92a1 | 105 | [SERVICE_MOUNTING] = UNIT_REFRESHING, |
17f6b640 YW |
106 | [SERVICE_STOP] = UNIT_DEACTIVATING, |
107 | [SERVICE_STOP_WATCHDOG] = UNIT_DEACTIVATING, | |
108 | [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING, | |
109 | [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING, | |
110 | [SERVICE_STOP_POST] = UNIT_DEACTIVATING, | |
111 | [SERVICE_FINAL_WATCHDOG] = UNIT_DEACTIVATING, | |
112 | [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING, | |
113 | [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING, | |
114 | [SERVICE_FAILED] = UNIT_FAILED, | |
115 | [SERVICE_DEAD_BEFORE_AUTO_RESTART] = UNIT_INACTIVE, | |
a1d31573 | 116 | [SERVICE_FAILED_BEFORE_AUTO_RESTART] = UNIT_FAILED, |
17f6b640 YW |
117 | [SERVICE_DEAD_RESOURCES_PINNED] = UNIT_INACTIVE, |
118 | [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING, | |
119 | [SERVICE_AUTO_RESTART_QUEUED] = UNIT_ACTIVATING, | |
120 | [SERVICE_CLEANING] = UNIT_MAINTENANCE, | |
e056b01d LP |
121 | }; |
122 | ||
5686391b | 123 | static int service_dispatch_inotify_io(sd_event_source *source, int fd, uint32_t events, void *userdata); |
718db961 LP |
124 | static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata); |
125 | static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata); | |
5686391b | 126 | static int service_dispatch_exec_io(sd_event_source *source, int fd, uint32_t events, void *userdata); |
718db961 | 127 | |
842129f5 | 128 | static void service_enter_signal(Service *s, ServiceState state, ServiceResult f); |
308d72dc | 129 | static void service_enter_reload_by_notify(Service *s); |
842129f5 | 130 | |
94114711 YW |
131 | static bool SERVICE_STATE_WITH_MAIN_PROCESS(ServiceState state) { |
132 | return IN_SET(state, | |
133 | SERVICE_START, SERVICE_START_POST, | |
134 | SERVICE_RUNNING, | |
dfdeb0b1 | 135 | SERVICE_RELOAD, SERVICE_RELOAD_SIGNAL, SERVICE_RELOAD_NOTIFY, SERVICE_REFRESH_EXTENSIONS, |
5162829e | 136 | SERVICE_MOUNTING, |
94114711 YW |
137 | SERVICE_STOP, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
138 | SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL); | |
139 | } | |
140 | ||
141 | static bool SERVICE_STATE_WITH_CONTROL_PROCESS(ServiceState state) { | |
142 | return IN_SET(state, | |
143 | SERVICE_CONDITION, | |
144 | SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, | |
dfdeb0b1 | 145 | SERVICE_RELOAD, SERVICE_RELOAD_SIGNAL, SERVICE_RELOAD_NOTIFY, SERVICE_REFRESH_EXTENSIONS, |
a53e92a1 | 146 | SERVICE_MOUNTING, |
94114711 YW |
147 | SERVICE_STOP, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
148 | SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL, | |
a53e92a1 | 149 | SERVICE_CLEANING); |
94114711 YW |
150 | } |
151 | ||
a16e1123 LP |
152 | static void service_init(Unit *u) { |
153 | Service *s = SERVICE(u); | |
154 | ||
155 | assert(u); | |
ac155bb8 | 156 | assert(u->load_state == UNIT_STUB); |
a16e1123 | 157 | |
c9e120e0 LP |
158 | s->timeout_start_usec = u->manager->defaults.timeout_start_usec; |
159 | s->timeout_stop_usec = u->manager->defaults.timeout_stop_usec; | |
160 | s->timeout_abort_usec = u->manager->defaults.timeout_abort_usec; | |
161 | s->timeout_abort_set = u->manager->defaults.timeout_abort_set; | |
162 | s->restart_usec = u->manager->defaults.restart_usec; | |
e9f17fa8 | 163 | s->restart_max_delay_usec = USEC_INFINITY; |
36c16a7c | 164 | s->runtime_max_usec = USEC_INFINITY; |
0b86feac | 165 | s->type = _SERVICE_TYPE_INVALID; |
254d1313 ZJS |
166 | s->socket_fd = -EBADF; |
167 | s->stdin_fd = s->stdout_fd = s->stderr_fd = -EBADF; | |
3185a36b | 168 | s->guess_main_pid = true; |
c79ab77c LP |
169 | s->main_pid = PIDREF_NULL; |
170 | s->control_pid = PIDREF_NULL; | |
a16e1123 | 171 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; |
00f5ad93 LP |
172 | |
173 | s->exec_context.keyring_mode = MANAGER_IS_SYSTEM(u->manager) ? | |
174 | EXEC_KEYRING_PRIVATE : EXEC_KEYRING_INHERIT; | |
aa8c4bbf | 175 | |
19dff691 MY |
176 | s->notify_access_override = _NOTIFY_ACCESS_INVALID; |
177 | ||
aa8c4bbf | 178 | s->watchdog_original_usec = USEC_INFINITY; |
afcfaa69 LP |
179 | |
180 | s->oom_policy = _OOM_POLICY_INVALID; | |
3bd28bf7 LP |
181 | s->reload_begin_usec = USEC_INFINITY; |
182 | s->reload_signal = SIGHUP; | |
b9c1883a LP |
183 | |
184 | s->fd_store_preserve_mode = EXEC_PRESERVE_RESTART; | |
a16e1123 LP |
185 | } |
186 | ||
5e94833f LP |
187 | static void service_unwatch_control_pid(Service *s) { |
188 | assert(s); | |
ea1e0bf1 | 189 | unit_unwatch_pidref_done(UNIT(s), &s->control_pid); |
5e94833f LP |
190 | } |
191 | ||
192 | static void service_unwatch_main_pid(Service *s) { | |
193 | assert(s); | |
ea1e0bf1 | 194 | unit_unwatch_pidref_done(UNIT(s), &s->main_pid); |
5e94833f LP |
195 | } |
196 | ||
3e52541e | 197 | static void service_unwatch_pid_file(Service *s) { |
e9fa1bf7 MY |
198 | assert(s); |
199 | ||
3e52541e MS |
200 | if (!s->pid_file_pathspec) |
201 | return; | |
202 | ||
f2341e0a | 203 | log_unit_debug(UNIT(s), "Stopping watch for PID file %s", s->pid_file_pathspec->path); |
718db961 | 204 | path_spec_unwatch(s->pid_file_pathspec); |
3e52541e | 205 | path_spec_done(s->pid_file_pathspec); |
a1e58e8e | 206 | s->pid_file_pathspec = mfree(s->pid_file_pathspec); |
3e52541e MS |
207 | } |
208 | ||
7e0e6b50 | 209 | static int service_set_main_pidref(Service *s, PidRef pidref_consume, const dual_timestamp *start_timestamp) { |
c603f523 | 210 | _cleanup_(pidref_done) PidRef pidref = pidref_consume; |
6774be42 LP |
211 | int r; |
212 | ||
5925dd3c LP |
213 | assert(s); |
214 | ||
c603f523 | 215 | /* Takes ownership of the specified pidref on both success and failure. */ |
b1f6901d | 216 | |
c603f523 | 217 | if (!pidref_is_set(&pidref)) |
b1f6901d LP |
218 | return -ESRCH; |
219 | ||
c603f523 | 220 | if (pidref.pid <= 1) |
5925dd3c LP |
221 | return -EINVAL; |
222 | ||
c603f523 | 223 | if (pidref_is_self(&pidref)) |
5925dd3c LP |
224 | return -EINVAL; |
225 | ||
c603f523 | 226 | if (s->main_pid_known && pidref_equal(&s->main_pid, &pidref)) |
7400b9d2 LP |
227 | return 0; |
228 | ||
c603f523 | 229 | if (!pidref_equal(&s->main_pid, &pidref)) { |
7400b9d2 | 230 | service_unwatch_main_pid(s); |
f781292d MY |
231 | |
232 | dual_timestamp pid_start_time; | |
233 | ||
234 | if (!start_timestamp) { | |
235 | usec_t t; | |
236 | ||
237 | if (pidref_get_start_time(&pidref, &t) >= 0) | |
238 | start_timestamp = dual_timestamp_from_boottime(&pid_start_time, t); | |
239 | } | |
240 | ||
7e0e6b50 | 241 | exec_status_start(&s->main_exec_status, pidref.pid, start_timestamp); |
7400b9d2 | 242 | } |
41efeaec | 243 | |
c603f523 | 244 | s->main_pid = TAKE_PIDREF(pidref); |
6dfa5494 LP |
245 | s->main_pid_known = true; |
246 | ||
6774be42 LP |
247 | r = pidref_is_my_child(&s->main_pid); |
248 | if (r < 0) | |
249 | log_unit_warning_errno(UNIT(s), r, "Can't determine if process "PID_FMT" is our child, assuming it is not: %m", s->main_pid.pid); | |
120be68b | 250 | else if (r == 0) // FIXME: Supervise through pidfd here |
b1f6901d | 251 | log_unit_warning(UNIT(s), "Supervising process "PID_FMT" which is not our child. We'll most likely not notice when it exits.", s->main_pid.pid); |
6774be42 | 252 | s->main_pid_alien = r <= 0; |
120be68b | 253 | |
5925dd3c LP |
254 | return 0; |
255 | } | |
256 | ||
81a1d6d6 | 257 | void service_release_socket_fd(Service *s) { |
4f2d528d LP |
258 | assert(s); |
259 | ||
c25fac9a LP |
260 | if (s->socket_fd < 0 && !UNIT_ISSET(s->accept_socket) && !s->socket_peer) |
261 | return; | |
262 | ||
263 | log_unit_debug(UNIT(s), "Closing connection socket."); | |
264 | ||
5cc3985e | 265 | /* Undo the effect of service_set_socket_fd(). */ |
4f2d528d | 266 | |
5cc3985e | 267 | s->socket_fd = asynchronous_close(s->socket_fd); |
6cf6bbc2 | 268 | |
5cc3985e LP |
269 | if (UNIT_ISSET(s->accept_socket)) { |
270 | socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket))); | |
271 | unit_ref_unset(&s->accept_socket); | |
272 | } | |
3fabebf4 LP |
273 | |
274 | s->socket_peer = socket_peer_unref(s->socket_peer); | |
6cf6bbc2 LP |
275 | } |
276 | ||
19dff691 MY |
277 | static void service_override_notify_access(Service *s, NotifyAccess notify_access_override) { |
278 | assert(s); | |
279 | ||
280 | s->notify_access_override = notify_access_override; | |
281 | ||
282 | log_unit_debug(UNIT(s), "notify_access=%s", notify_access_to_string(s->notify_access)); | |
283 | log_unit_debug(UNIT(s), "notify_access_override=%s", notify_access_to_string(s->notify_access_override)); | |
284 | } | |
285 | ||
a6927d7f MO |
286 | static void service_stop_watchdog(Service *s) { |
287 | assert(s); | |
288 | ||
5dcadb4c | 289 | s->watchdog_event_source = sd_event_source_disable_unref(s->watchdog_event_source); |
842129f5 | 290 | s->watchdog_timestamp = DUAL_TIMESTAMP_NULL; |
a6927d7f MO |
291 | } |
292 | ||
842129f5 | 293 | static void service_start_watchdog(Service *s) { |
2787d83c | 294 | usec_t watchdog_usec; |
9fb1cdb4 | 295 | int r; |
bb242b7b MO |
296 | |
297 | assert(s); | |
298 | ||
2787d83c | 299 | watchdog_usec = service_get_watchdog_usec(s); |
0da36375 | 300 | if (!timestamp_is_set(watchdog_usec)) { |
9fb1cdb4 | 301 | service_stop_watchdog(s); |
bb242b7b | 302 | return; |
9fb1cdb4 | 303 | } |
bb242b7b | 304 | |
718db961 | 305 | if (s->watchdog_event_source) { |
2787d83c | 306 | r = sd_event_source_set_time(s->watchdog_event_source, usec_add(s->watchdog_timestamp.monotonic, watchdog_usec)); |
718db961 | 307 | if (r < 0) { |
f2341e0a | 308 | log_unit_warning_errno(UNIT(s), r, "Failed to reset watchdog timer: %m"); |
718db961 LP |
309 | return; |
310 | } | |
311 | ||
842129f5 | 312 | r = sd_event_source_set_enabled(s->watchdog_event_source, SD_EVENT_ONESHOT); |
c4ef3317 | 313 | } else { |
6a0f1f6d LP |
314 | r = sd_event_add_time( |
315 | UNIT(s)->manager->event, | |
316 | &s->watchdog_event_source, | |
317 | CLOCK_MONOTONIC, | |
2787d83c | 318 | usec_add(s->watchdog_timestamp.monotonic, watchdog_usec), 0, |
6a0f1f6d | 319 | service_dispatch_watchdog, s); |
c4ef3317 | 320 | if (r < 0) { |
f2341e0a | 321 | log_unit_warning_errno(UNIT(s), r, "Failed to add watchdog timer: %m"); |
c4ef3317 LP |
322 | return; |
323 | } | |
324 | ||
7dfbe2e3 TG |
325 | (void) sd_event_source_set_description(s->watchdog_event_source, "service-watchdog"); |
326 | ||
c4ef3317 LP |
327 | /* Let's process everything else which might be a sign |
328 | * of living before we consider a service died. */ | |
d42b61d2 | 329 | r = sd_event_source_set_priority(s->watchdog_event_source, EVENT_PRIORITY_SERVICE_WATCHDOG); |
c4ef3317 | 330 | } |
bb242b7b | 331 | if (r < 0) |
f2341e0a | 332 | log_unit_warning_errno(UNIT(s), r, "Failed to install watchdog timer: %m"); |
bb242b7b MO |
333 | } |
334 | ||
5171356e MY |
335 | usec_t service_restart_usec_next(Service *s) { |
336 | unsigned n_restarts_next; | |
be1adc27 MY |
337 | |
338 | assert(s); | |
339 | ||
0c59d2e4 | 340 | /* When the service state is in SERVICE_*_BEFORE_AUTO_RESTART or SERVICE_AUTO_RESTART, we still need |
ba5e342c MY |
341 | * to add 1 to s->n_restarts manually, because s->n_restarts is not updated until a restart job is |
342 | * enqueued, i.e. state has transitioned to SERVICE_AUTO_RESTART_QUEUED. */ | |
343 | n_restarts_next = s->n_restarts + (s->state == SERVICE_AUTO_RESTART_QUEUED ? 0 : 1); | |
be1adc27 | 344 | |
5171356e | 345 | if (n_restarts_next <= 1 || |
be1adc27 | 346 | s->restart_steps == 0 || |
ea792cac | 347 | s->restart_usec == 0 || |
e9f17fa8 MY |
348 | s->restart_max_delay_usec == USEC_INFINITY || |
349 | s->restart_usec >= s->restart_max_delay_usec) | |
fe3d33c1 MY |
350 | return s->restart_usec; |
351 | ||
352 | if (n_restarts_next > s->restart_steps) | |
353 | return s->restart_max_delay_usec; | |
354 | ||
355 | /* Enforced in service_verify() and above */ | |
356 | assert(s->restart_max_delay_usec > s->restart_usec); | |
357 | ||
358 | /* r_i / r_0 = (r_n / r_0) ^ (i / n) | |
359 | * where, | |
360 | * r_0 : initial restart usec (s->restart_usec), | |
361 | * r_i : i-th restart usec (value), | |
362 | * r_n : maximum restart usec (s->restart_max_delay_usec), | |
363 | * i : index of the next step (n_restarts_next - 1) | |
364 | * n : num maximum steps (s->restart_steps) */ | |
365 | return (usec_t) (s->restart_usec * powl((long double) s->restart_max_delay_usec / s->restart_usec, | |
366 | (long double) (n_restarts_next - 1) / s->restart_steps)); | |
be1adc27 MY |
367 | } |
368 | ||
ec35a7f6 LP |
369 | static void service_extend_event_source_timeout(Service *s, sd_event_source *source, usec_t extended) { |
370 | usec_t current; | |
371 | int r; | |
372 | ||
a327431b DB |
373 | assert(s); |
374 | ||
ec35a7f6 LP |
375 | /* Extends the specified event source timer to at least the specified time, unless it is already later |
376 | * anyway. */ | |
a327431b | 377 | |
ec35a7f6 LP |
378 | if (!source) |
379 | return; | |
a327431b | 380 | |
ec35a7f6 LP |
381 | r = sd_event_source_get_time(source, ¤t); |
382 | if (r < 0) { | |
383 | const char *desc; | |
384 | (void) sd_event_source_get_description(s->timer_event_source, &desc); | |
385 | log_unit_warning_errno(UNIT(s), r, "Failed to retrieve timeout time for event source '%s', ignoring: %m", strna(desc)); | |
386 | return; | |
387 | } | |
a327431b | 388 | |
ec35a7f6 LP |
389 | if (current >= extended) /* Current timeout is already longer, ignore this. */ |
390 | return; | |
a327431b | 391 | |
ec35a7f6 LP |
392 | r = sd_event_source_set_time(source, extended); |
393 | if (r < 0) { | |
394 | const char *desc; | |
395 | (void) sd_event_source_get_description(s->timer_event_source, &desc); | |
5c34d85c | 396 | log_unit_warning_errno(UNIT(s), r, "Failed to set timeout time for event source '%s', ignoring: %m", strna(desc)); |
a327431b DB |
397 | } |
398 | } | |
399 | ||
ec35a7f6 LP |
400 | static void service_extend_timeout(Service *s, usec_t extend_timeout_usec) { |
401 | usec_t extended; | |
402 | ||
403 | assert(s); | |
404 | ||
0da36375 | 405 | if (!timestamp_is_set(extend_timeout_usec)) |
ec35a7f6 LP |
406 | return; |
407 | ||
408 | extended = usec_add(now(CLOCK_MONOTONIC), extend_timeout_usec); | |
409 | ||
410 | service_extend_event_source_timeout(s, s->timer_event_source, extended); | |
411 | service_extend_event_source_timeout(s, s->watchdog_event_source, extended); | |
412 | } | |
413 | ||
a6927d7f MO |
414 | static void service_reset_watchdog(Service *s) { |
415 | assert(s); | |
416 | ||
0cf84c9a YW |
417 | if (freezer_state_finish(UNIT(s)->freezer_state) != FREEZER_RUNNING) { |
418 | log_unit_debug(UNIT(s), "Service is currently %s, skipping resetting watchdog.", | |
419 | freezer_state_to_string(UNIT(s)->freezer_state)); | |
420 | return; | |
421 | } | |
422 | ||
fa5a0251 | 423 | dual_timestamp_now(&s->watchdog_timestamp); |
842129f5 | 424 | service_start_watchdog(s); |
a6927d7f MO |
425 | } |
426 | ||
95d0d8ed | 427 | static void service_override_watchdog_timeout(Service *s, usec_t watchdog_override_usec) { |
2787d83c M |
428 | assert(s); |
429 | ||
430 | s->watchdog_override_enable = true; | |
431 | s->watchdog_override_usec = watchdog_override_usec; | |
432 | service_reset_watchdog(s); | |
433 | ||
434 | log_unit_debug(UNIT(s), "watchdog_usec="USEC_FMT, s->watchdog_usec); | |
435 | log_unit_debug(UNIT(s), "watchdog_override_usec="USEC_FMT, s->watchdog_override_usec); | |
436 | } | |
437 | ||
a02287ea | 438 | static ServiceFDStore* service_fd_store_unlink(ServiceFDStore *fs) { |
a354329f | 439 | if (!fs) |
a02287ea | 440 | return NULL; |
a354329f LP |
441 | |
442 | if (fs->service) { | |
443 | assert(fs->service->n_fd_store > 0); | |
444 | LIST_REMOVE(fd_store, fs->service->fd_store, fs); | |
445 | fs->service->n_fd_store--; | |
446 | } | |
447 | ||
1d3fe304 | 448 | sd_event_source_disable_unref(fs->event_source); |
a354329f | 449 | |
8dd4c05b | 450 | free(fs->fdname); |
99620f45 | 451 | asynchronous_close(fs->fd); |
a02287ea | 452 | return mfree(fs); |
a354329f LP |
453 | } |
454 | ||
a02287ea YW |
455 | DEFINE_TRIVIAL_CLEANUP_FUNC(ServiceFDStore*, service_fd_store_unlink); |
456 | ||
f0bfbfac ZJS |
457 | static void service_release_fd_store(Service *s) { |
458 | assert(s); | |
459 | ||
c25fac9a LP |
460 | if (!s->fd_store) |
461 | return; | |
462 | ||
32af4dd8 | 463 | log_unit_debug(UNIT(s), "Releasing all stored fds."); |
c25fac9a | 464 | |
f0bfbfac ZJS |
465 | while (s->fd_store) |
466 | service_fd_store_unlink(s->fd_store); | |
467 | ||
468 | assert(s->n_fd_store == 0); | |
469 | } | |
470 | ||
3543456f RW |
471 | static void service_release_extra_fds(Service *s) { |
472 | assert(s); | |
473 | ||
32af4dd8 MY |
474 | if (!s->extra_fds) |
475 | return; | |
476 | ||
477 | log_unit_debug(UNIT(s), "Releasing extra file descriptors."); | |
478 | ||
479 | FOREACH_ARRAY(i, s->extra_fds, s->n_extra_fds) { | |
480 | asynchronous_close(i->fd); | |
481 | free(i->fdname); | |
482 | } | |
483 | ||
484 | s->extra_fds = mfree(s->extra_fds); | |
485 | s->n_extra_fds = 0; | |
3543456f RW |
486 | } |
487 | ||
c25fac9a | 488 | static void service_release_stdio_fd(Service *s) { |
a354329f LP |
489 | assert(s); |
490 | ||
5bcf0881 | 491 | if (s->stdin_fd < 0 && s->stdout_fd < 0 && s->stderr_fd < 0) |
a354329f LP |
492 | return; |
493 | ||
c25fac9a | 494 | log_unit_debug(UNIT(s), "Releasing stdin/stdout/stderr file descriptors."); |
a354329f | 495 | |
99620f45 LP |
496 | s->stdin_fd = asynchronous_close(s->stdin_fd); |
497 | s->stdout_fd = asynchronous_close(s->stdout_fd); | |
498 | s->stderr_fd = asynchronous_close(s->stderr_fd); | |
a354329f | 499 | } |
81006ebb | 500 | |
87f0e418 | 501 | static void service_done(Unit *u) { |
e9fa1bf7 | 502 | Service *s = ASSERT_PTR(SERVICE(u)); |
44d8db9e | 503 | |
cd48e23f RP |
504 | open_file_free_many(&s->open_files); |
505 | ||
a1e58e8e LP |
506 | s->pid_file = mfree(s->pid_file); |
507 | s->status_text = mfree(s->status_text); | |
9c025022 MY |
508 | s->status_bus_error = mfree(s->status_bus_error); |
509 | s->status_varlink_error = mfree(s->status_varlink_error); | |
efe6e7d3 | 510 | |
28135da3 | 511 | s->exec_runtime = exec_runtime_free(s->exec_runtime); |
9cc54544 | 512 | |
e537352b | 513 | exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX); |
44d8db9e | 514 | s->control_command = NULL; |
867b3b7d | 515 | s->main_command = NULL; |
44d8db9e | 516 | |
37520c1b LP |
517 | exit_status_set_free(&s->restart_prevent_status); |
518 | exit_status_set_free(&s->restart_force_status); | |
519 | exit_status_set_free(&s->success_status); | |
96342de6 | 520 | |
c79ab77c | 521 | /* This will leak a process, but at least no memory or any of our resources */ |
5e94833f LP |
522 | service_unwatch_main_pid(s); |
523 | service_unwatch_control_pid(s); | |
3e52541e | 524 | service_unwatch_pid_file(s); |
44d8db9e | 525 | |
05e343b7 | 526 | if (s->bus_name) { |
ac155bb8 | 527 | unit_unwatch_bus_name(u, s->bus_name); |
a1e58e8e | 528 | s->bus_name = mfree(s->bus_name); |
05e343b7 LP |
529 | } |
530 | ||
064c5938 ZJS |
531 | s->usb_function_descriptors = mfree(s->usb_function_descriptors); |
532 | s->usb_function_strings = mfree(s->usb_function_strings); | |
533 | ||
bb242b7b MO |
534 | service_stop_watchdog(s); |
535 | ||
5dcadb4c ZJS |
536 | s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); |
537 | s->exec_fd_event_source = sd_event_source_disable_unref(s->exec_fd_event_source); | |
a354329f | 538 | |
e39eb045 LP |
539 | s->bus_name_pid_lookup_slot = sd_bus_slot_unref(s->bus_name_pid_lookup_slot); |
540 | ||
81a1d6d6 | 541 | service_release_socket_fd(s); |
c25fac9a | 542 | service_release_stdio_fd(s); |
81a1d6d6 | 543 | service_release_fd_store(s); |
3543456f | 544 | service_release_extra_fds(s); |
5162829e LB |
545 | |
546 | s->mount_request = sd_bus_message_unref(s->mount_request); | |
718db961 LP |
547 | } |
548 | ||
2339fc93 | 549 | static int on_fd_store_io(sd_event_source *e, int fd, uint32_t revents, void *userdata) { |
99534007 | 550 | ServiceFDStore *fs = ASSERT_PTR(userdata); |
2339fc93 LP |
551 | |
552 | assert(e); | |
2339fc93 LP |
553 | |
554 | /* If we get either EPOLLHUP or EPOLLERR, it's time to remove this entry from the fd store */ | |
16f70d63 ZJS |
555 | log_unit_debug(UNIT(fs->service), |
556 | "Received %s on stored fd %d (%s), closing.", | |
557 | revents & EPOLLERR ? "EPOLLERR" : "EPOLLHUP", | |
558 | fs->fd, strna(fs->fdname)); | |
2339fc93 LP |
559 | service_fd_store_unlink(fs); |
560 | return 0; | |
561 | } | |
562 | ||
a02287ea YW |
563 | static int service_add_fd_store(Service *s, int fd_in, const char *name, bool do_poll) { |
564 | _cleanup_(service_fd_store_unlinkp) ServiceFDStore *fs = NULL; | |
565 | _cleanup_(asynchronous_closep) int fd = ASSERT_FD(fd_in); | |
e8783d76 | 566 | struct stat st; |
2339fc93 LP |
567 | int r; |
568 | ||
a02287ea | 569 | /* fd is always consumed even if the function fails. */ |
9021ff17 | 570 | |
2339fc93 | 571 | assert(s); |
2339fc93 | 572 | |
e8783d76 LP |
573 | if (fstat(fd, &st) < 0) |
574 | return -errno; | |
575 | ||
0578371f MY |
576 | log_unit_debug(UNIT(s), "Trying to stash fd for dev=" DEVNUM_FORMAT_STR "/inode=%" PRIu64, |
577 | DEVNUM_FORMAT_VAL(st.st_dev), (uint64_t) st.st_ino); | |
e8783d76 | 578 | |
2339fc93 | 579 | if (s->n_fd_store >= s->n_fd_store_max) |
e8783d76 LP |
580 | /* Our store is full. Use this errno rather than E[NM]FILE to distinguish from the case |
581 | * where systemd itself hits the file limit. */ | |
582 | return log_unit_debug_errno(UNIT(s), SYNTHETIC_ERRNO(EXFULL), "Hit fd store limit."); | |
2339fc93 | 583 | |
03677889 YW |
584 | LIST_FOREACH(fd_store, i, s->fd_store) { |
585 | r = same_fd(i->fd, fd); | |
2339fc93 LP |
586 | if (r < 0) |
587 | return r; | |
588 | if (r > 0) { | |
a02287ea | 589 | log_unit_debug(UNIT(s), "Suppressing duplicate fd %i in fd store.", fd); |
9021ff17 | 590 | return 0; /* fd already included */ |
2339fc93 LP |
591 | } |
592 | } | |
593 | ||
b0cea477 | 594 | fs = new(ServiceFDStore, 1); |
2339fc93 LP |
595 | if (!fs) |
596 | return -ENOMEM; | |
597 | ||
b0cea477 | 598 | *fs = (ServiceFDStore) { |
a02287ea | 599 | .fd = TAKE_FD(fd), |
b0cea477 LP |
600 | .do_poll = do_poll, |
601 | .fdname = strdup(name ?: "stored"), | |
602 | }; | |
603 | ||
a02287ea | 604 | if (!fs->fdname) |
8dd4c05b | 605 | return -ENOMEM; |
2339fc93 | 606 | |
cb5a46b8 | 607 | if (do_poll) { |
a02287ea YW |
608 | r = sd_event_add_io(UNIT(s)->manager->event, &fs->event_source, fs->fd, 0, on_fd_store_io, fs); |
609 | if (r < 0 && r != -EPERM) /* EPERM indicates fds that aren't pollable, which is OK */ | |
cb5a46b8 | 610 | return r; |
0578371f | 611 | if (r >= 0) |
cb5a46b8 KL |
612 | (void) sd_event_source_set_description(fs->event_source, "service-fd-store"); |
613 | } | |
7dfbe2e3 | 614 | |
0578371f MY |
615 | log_unit_debug(UNIT(s), "Added fd %i (%s) to fd store.", fs->fd, fs->fdname); |
616 | ||
a02287ea | 617 | fs->service = s; |
0578371f | 618 | LIST_PREPEND(fd_store, s->fd_store, TAKE_PTR(fs)); |
2339fc93 LP |
619 | s->n_fd_store++; |
620 | ||
9021ff17 | 621 | return 1; /* fd newly stored */ |
2339fc93 LP |
622 | } |
623 | ||
cb5a46b8 | 624 | static int service_add_fd_store_set(Service *s, FDSet *fds, const char *name, bool do_poll) { |
2339fc93 LP |
625 | int r; |
626 | ||
627 | assert(s); | |
628 | ||
a02287ea YW |
629 | for (;;) { |
630 | int fd; | |
2339fc93 LP |
631 | |
632 | fd = fdset_steal_first(fds); | |
633 | if (fd < 0) | |
634 | break; | |
635 | ||
cb5a46b8 | 636 | r = service_add_fd_store(s, fd, name, do_poll); |
b0924635 ZJS |
637 | if (r == -EXFULL) |
638 | return log_unit_warning_errno(UNIT(s), r, | |
639 | "Cannot store more fds than FileDescriptorStoreMax=%u, closing remaining.", | |
640 | s->n_fd_store_max); | |
2339fc93 | 641 | if (r < 0) |
b0924635 | 642 | return log_unit_error_errno(UNIT(s), r, "Failed to add fd to store: %m"); |
2339fc93 LP |
643 | } |
644 | ||
2339fc93 LP |
645 | return 0; |
646 | } | |
647 | ||
e78ee06d | 648 | static void service_remove_fd_store(Service *s, const char *name) { |
e78ee06d LP |
649 | assert(s); |
650 | assert(name); | |
651 | ||
80a226b2 | 652 | LIST_FOREACH(fd_store, fs, s->fd_store) { |
e78ee06d LP |
653 | if (!streq(fs->fdname, name)) |
654 | continue; | |
655 | ||
656 | log_unit_debug(UNIT(s), "Got explicit request to remove fd %i (%s), closing.", fs->fd, name); | |
657 | service_fd_store_unlink(fs); | |
658 | } | |
659 | } | |
660 | ||
ecea250d | 661 | static usec_t service_running_timeout(Service *s) { |
5918a933 AB |
662 | usec_t delta = 0; |
663 | ||
664 | assert(s); | |
665 | ||
666 | if (s->runtime_rand_extra_usec != 0) { | |
667 | delta = random_u64_range(s->runtime_rand_extra_usec); | |
668 | log_unit_debug(UNIT(s), "Adding delta of %s sec to timeout", FORMAT_TIMESPAN(delta, USEC_PER_SEC)); | |
669 | } | |
670 | ||
671 | return usec_add(usec_add(UNIT(s)->active_enter_timestamp.monotonic, | |
672 | s->runtime_max_usec), | |
673 | delta); | |
674 | } | |
675 | ||
e5d6dcce | 676 | static int service_arm_timer(Service *s, bool relative, usec_t usec) { |
718db961 LP |
677 | assert(s); |
678 | ||
e9276800 | 679 | return unit_arm_timer(UNIT(s), &s->timer_event_source, relative, usec, service_dispatch_timer); |
44d8db9e LP |
680 | } |
681 | ||
243b1432 LP |
682 | static int service_verify(Service *s) { |
683 | assert(s); | |
e0cfed4c | 684 | assert(UNIT(s)->load_state == UNIT_LOADED); |
243b1432 | 685 | |
d85ff944 YW |
686 | if (!s->exec_command[SERVICE_EXEC_START] && !s->exec_command[SERVICE_EXEC_STOP] && |
687 | UNIT(s)->success_action == EMERGENCY_ACTION_NONE) | |
3f00d379 ZJS |
688 | /* FailureAction= only makes sense if one of the start or stop commands is specified. |
689 | * SuccessAction= will be executed unconditionally if no commands are specified. Hence, | |
690 | * either a command or SuccessAction= are required. */ | |
d85ff944 | 691 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has no ExecStart=, ExecStop=, or SuccessAction=. Refusing."); |
243b1432 | 692 | |
d85ff944 YW |
693 | if (s->type != SERVICE_ONESHOT && !s->exec_command[SERVICE_EXEC_START]) |
694 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing."); | |
96fb8242 | 695 | |
d85ff944 YW |
696 | if (!s->remain_after_exit && !s->exec_command[SERVICE_EXEC_START] && UNIT(s)->success_action == EMERGENCY_ACTION_NONE) |
697 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has no ExecStart= and no SuccessAction= settings and does not have RemainAfterExit=yes set. Refusing."); | |
96fb8242 | 698 | |
d85ff944 YW |
699 | if (s->type != SERVICE_ONESHOT && s->exec_command[SERVICE_EXEC_START]->command_next) |
700 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing."); | |
6cf6bbc2 | 701 | |
d67c51e3 | 702 | if (s->type == SERVICE_ONESHOT && IN_SET(s->restart, SERVICE_RESTART_ALWAYS, SERVICE_RESTART_ON_SUCCESS)) |
d85ff944 | 703 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has Restart= set to either always or on-success, which isn't allowed for Type=oneshot services. Refusing."); |
37520c1b | 704 | |
596e4470 HC |
705 | if (s->type == SERVICE_ONESHOT && s->exit_type == SERVICE_EXIT_CGROUP) |
706 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has ExitType=cgroup set, which isn't allowed for Type=oneshot services. Refusing."); | |
707 | ||
d85ff944 YW |
708 | if (s->type == SERVICE_DBUS && !s->bus_name) |
709 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service is of type D-Bus but no D-Bus service name has been specified. Refusing."); | |
4d0e5dbd | 710 | |
2d8191f4 | 711 | if (s->type == SERVICE_FORKING && exec_needs_pid_namespace(&s->exec_context, /* params= */ NULL)) |
406f1775 DDM |
712 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service of Type=forking does not support PrivatePIDs=yes. Refusing."); |
713 | ||
6b7e5923 PS |
714 | if (s->usb_function_descriptors && !s->usb_function_strings) |
715 | log_unit_warning(UNIT(s), "Service has USBFunctionDescriptors= setting, but no USBFunctionStrings=. Ignoring."); | |
716 | ||
717 | if (!s->usb_function_descriptors && s->usb_function_strings) | |
718 | log_unit_warning(UNIT(s), "Service has USBFunctionStrings= setting, but no USBFunctionDescriptors=. Ignoring."); | |
719 | ||
36c16a7c | 720 | if (s->runtime_max_usec != USEC_INFINITY && s->type == SERVICE_ONESHOT) |
226a08f2 | 721 | log_unit_warning(UNIT(s), "RuntimeMaxSec= has no effect in combination with Type=oneshot. Ignoring."); |
36c16a7c | 722 | |
5918a933 AB |
723 | if (s->runtime_max_usec == USEC_INFINITY && s->runtime_rand_extra_usec != 0) |
724 | log_unit_warning(UNIT(s), "Service has RuntimeRandomizedExtraSec= setting, but no RuntimeMaxSec=. Ignoring."); | |
725 | ||
757e469d MY |
726 | if (s->type == SERVICE_SIMPLE && s->exec_command[SERVICE_EXEC_START_POST] && exec_context_has_credentials(&s->exec_context)) |
727 | log_unit_warning(UNIT(s), "Service uses a combination of Type=simple, ExecStartPost=, and credentials. This could lead to race conditions. Continuing."); | |
728 | ||
e9f17fa8 MY |
729 | if (s->restart_max_delay_usec == USEC_INFINITY && s->restart_steps > 0) |
730 | log_unit_warning(UNIT(s), "Service has RestartSteps= but no RestartMaxDelaySec= setting. Ignoring."); | |
be1adc27 | 731 | |
e9f17fa8 MY |
732 | if (s->restart_max_delay_usec != USEC_INFINITY && s->restart_steps == 0) |
733 | log_unit_warning(UNIT(s), "Service has RestartMaxDelaySec= but no RestartSteps= setting. Ignoring."); | |
be1adc27 | 734 | |
e9f17fa8 MY |
735 | if (s->restart_max_delay_usec < s->restart_usec) { |
736 | log_unit_warning(UNIT(s), "RestartMaxDelaySec= has a value smaller than RestartSec=, resetting RestartSec= to RestartMaxDelaySec=."); | |
737 | s->restart_usec = s->restart_max_delay_usec; | |
be1adc27 MY |
738 | } |
739 | ||
243b1432 LP |
740 | return 0; |
741 | } | |
742 | ||
a40eb732 LP |
743 | static int service_add_default_dependencies(Service *s) { |
744 | int r; | |
745 | ||
746 | assert(s); | |
747 | ||
45f06b34 LP |
748 | if (!UNIT(s)->default_dependencies) |
749 | return 0; | |
750 | ||
a40eb732 LP |
751 | /* Add a number of automatic dependencies useful for the |
752 | * majority of services. */ | |
753 | ||
463d0d15 | 754 | if (MANAGER_IS_SYSTEM(UNIT(s)->manager)) { |
3835b9aa LB |
755 | /* First, pull in the really early boot stuff, and |
756 | * require it, so that we fail if we can't acquire | |
757 | * it. */ | |
758 | ||
759 | r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, true, UNIT_DEPENDENCY_DEFAULT); | |
cb4c247d LP |
760 | if (r < 0) |
761 | return r; | |
762 | } else { | |
763 | ||
764 | /* In the --user instance there's no sysinit.target, | |
765 | * in that case require basic.target instead. */ | |
766 | ||
35d8c19a | 767 | r = unit_add_dependency_by_name(UNIT(s), UNIT_REQUIRES, SPECIAL_BASIC_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
cb4c247d LP |
768 | if (r < 0) |
769 | return r; | |
770 | } | |
771 | ||
772 | /* Second, if the rest of the base system is in the same | |
773 | * transaction, order us after it, but do not pull it in or | |
774 | * even require it. */ | |
35d8c19a | 775 | r = unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_BASIC_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
fccd44ec KS |
776 | if (r < 0) |
777 | return r; | |
a40eb732 | 778 | |
cb4c247d | 779 | /* Third, add us in for normal shutdown. */ |
3835b9aa | 780 | return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
a40eb732 LP |
781 | } |
782 | ||
3b7f79dc | 783 | static void service_fix_stdio(Service *s) { |
4dfc092a LP |
784 | assert(s); |
785 | ||
3b7f79dc LP |
786 | /* Note that EXEC_INPUT_NULL and EXEC_OUTPUT_INHERIT play a special role here: they are both the |
787 | * default value that is subject to automatic overriding triggered by other settings and an explicit | |
162392b7 | 788 | * choice the user can make. We don't distinguish between these cases currently. */ |
3b7f79dc LP |
789 | |
790 | if (s->exec_context.std_input == EXEC_INPUT_NULL && | |
791 | s->exec_context.stdin_data_size > 0) | |
792 | s->exec_context.std_input = EXEC_INPUT_DATA; | |
793 | ||
794 | if (IN_SET(s->exec_context.std_input, | |
795 | EXEC_INPUT_TTY, | |
796 | EXEC_INPUT_TTY_FORCE, | |
797 | EXEC_INPUT_TTY_FAIL, | |
798 | EXEC_INPUT_SOCKET, | |
799 | EXEC_INPUT_NAMED_FD)) | |
800 | return; | |
801 | ||
802 | /* We assume these listed inputs refer to bidirectional streams, and hence duplicating them from | |
803 | * stdin to stdout/stderr makes sense and hence leaving EXEC_OUTPUT_INHERIT in place makes sense, | |
804 | * too. Outputs such as regular files or sealed data memfds otoh don't really make sense to be | |
805 | * duplicated for both input and output at the same time (since they then would cause a feedback | |
806 | * loop), hence override EXEC_OUTPUT_INHERIT with the default stderr/stdout setting. */ | |
4dfc092a LP |
807 | |
808 | if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT && | |
3b7f79dc | 809 | s->exec_context.std_output == EXEC_OUTPUT_INHERIT) |
c9e120e0 | 810 | s->exec_context.std_error = UNIT(s)->manager->defaults.std_error; |
4dfc092a | 811 | |
3b7f79dc | 812 | if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT) |
c9e120e0 | 813 | s->exec_context.std_output = UNIT(s)->manager->defaults.std_output; |
4dfc092a LP |
814 | } |
815 | ||
45f06b34 LP |
816 | static int service_setup_bus_name(Service *s) { |
817 | int r; | |
818 | ||
819 | assert(s); | |
820 | ||
0f97b7c3 | 821 | /* If s->bus_name is not set, then the unit will be refused by service_verify() later. */ |
1e8b312e | 822 | if (!s->bus_name) |
45f06b34 LP |
823 | return 0; |
824 | ||
1e8b312e LP |
825 | if (s->type == SERVICE_DBUS) { |
826 | r = unit_add_dependency_by_name(UNIT(s), UNIT_REQUIRES, SPECIAL_DBUS_SOCKET, true, UNIT_DEPENDENCY_FILE); | |
827 | if (r < 0) | |
2b2ca7ff | 828 | return log_unit_error_errno(UNIT(s), r, "Failed to add dependency on %s: %m", SPECIAL_DBUS_SOCKET); |
45f06b34 | 829 | |
1e8b312e LP |
830 | /* We always want to be ordered against dbus.socket if both are in the transaction. */ |
831 | r = unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_DBUS_SOCKET, true, UNIT_DEPENDENCY_FILE); | |
832 | if (r < 0) | |
2b2ca7ff | 833 | return log_unit_error_errno(UNIT(s), r, "Failed to add dependency on %s: %m", SPECIAL_DBUS_SOCKET); |
1e8b312e | 834 | } |
45f06b34 LP |
835 | |
836 | r = unit_watch_bus_name(UNIT(s), s->bus_name); | |
837 | if (r == -EEXIST) | |
838 | return log_unit_error_errno(UNIT(s), r, "Two services allocated for the same bus name %s, refusing operation.", s->bus_name); | |
839 | if (r < 0) | |
840 | return log_unit_error_errno(UNIT(s), r, "Cannot watch bus name %s: %m", s->bus_name); | |
841 | ||
842 | return 0; | |
843 | } | |
844 | ||
8545f7ce LP |
845 | static int service_add_extras(Service *s) { |
846 | int r; | |
847 | ||
848 | assert(s); | |
849 | ||
850 | if (s->type == _SERVICE_TYPE_INVALID) { | |
851 | /* Figure out a type automatically */ | |
852 | if (s->bus_name) | |
853 | s->type = SERVICE_DBUS; | |
757e469d MY |
854 | else if (exec_context_has_credentials(&s->exec_context)) |
855 | s->type = SERVICE_EXEC; | |
8545f7ce LP |
856 | else if (s->exec_command[SERVICE_EXEC_START]) |
857 | s->type = SERVICE_SIMPLE; | |
858 | else | |
859 | s->type = SERVICE_ONESHOT; | |
860 | } | |
861 | ||
862 | /* Oneshot services have disabled start timeout by default */ | |
863 | if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined) | |
36c16a7c | 864 | s->timeout_start_usec = USEC_INFINITY; |
8545f7ce | 865 | |
3b7f79dc | 866 | service_fix_stdio(s); |
8545f7ce LP |
867 | |
868 | r = unit_patch_contexts(UNIT(s)); | |
869 | if (r < 0) | |
870 | return r; | |
871 | ||
872 | r = unit_add_exec_dependencies(UNIT(s), &s->exec_context); | |
873 | if (r < 0) | |
874 | return r; | |
875 | ||
d79200e2 | 876 | r = unit_set_default_slice(UNIT(s)); |
8545f7ce LP |
877 | if (r < 0) |
878 | return r; | |
879 | ||
4330dc03 AJ |
880 | /* If the service needs the notify socket, let's enable it automatically. */ |
881 | if (s->notify_access == NOTIFY_NONE && | |
3bd28bf7 | 882 | (IN_SET(s->type, SERVICE_NOTIFY, SERVICE_NOTIFY_RELOAD) || s->watchdog_usec > 0 || s->n_fd_store_max > 0)) |
8545f7ce LP |
883 | s->notify_access = NOTIFY_MAIN; |
884 | ||
afcfaa69 LP |
885 | /* If no OOM policy was explicitly set, then default to the configure default OOM policy. Except when |
886 | * delegation is on, in that case it we assume the payload knows better what to do and can process | |
5238e957 | 887 | * things in a more focused way. */ |
afcfaa69 | 888 | if (s->oom_policy < 0) |
c9e120e0 | 889 | s->oom_policy = s->cgroup_context.delegate ? OOM_CONTINUE : UNIT(s)->manager->defaults.oom_policy; |
afcfaa69 LP |
890 | |
891 | /* Let the kernel do the killing if that's requested. */ | |
892 | s->cgroup_context.memory_oom_group = s->oom_policy == OOM_KILL; | |
893 | ||
45f06b34 LP |
894 | r = service_add_default_dependencies(s); |
895 | if (r < 0) | |
896 | return r; | |
8545f7ce | 897 | |
45f06b34 LP |
898 | r = service_setup_bus_name(s); |
899 | if (r < 0) | |
900 | return r; | |
8545f7ce LP |
901 | |
902 | return 0; | |
903 | } | |
904 | ||
e537352b | 905 | static int service_load(Unit *u) { |
e9fa1bf7 | 906 | Service *s = ASSERT_PTR(SERVICE(u)); |
8bb2d17d | 907 | int r; |
e537352b | 908 | |
e0cfed4c | 909 | r = unit_load_fragment_and_dropin(u, true); |
c2756a68 | 910 | if (r < 0) |
5cb5a6ff LP |
911 | return r; |
912 | ||
e0cfed4c ZJS |
913 | if (u->load_state != UNIT_LOADED) |
914 | return 0; | |
034c6ed7 | 915 | |
23a177ef | 916 | /* This is a new unit? Then let's add in some extras */ |
e0cfed4c ZJS |
917 | r = service_add_extras(s); |
918 | if (r < 0) | |
919 | return r; | |
8e274523 | 920 | |
243b1432 | 921 | return service_verify(s); |
034c6ed7 LP |
922 | } |
923 | ||
3543456f RW |
924 | static int service_dump_fd(int fd, const char *fdname, const char *header, FILE *f, const char *prefix) { |
925 | _cleanup_free_ char *path = NULL; | |
926 | struct stat st; | |
927 | int flags; | |
928 | ||
32af4dd8 MY |
929 | assert(fd >= 0); |
930 | assert(fdname); | |
931 | assert(header); | |
932 | assert(f); | |
933 | assert(prefix); | |
934 | ||
3543456f | 935 | if (fstat(fd, &st) < 0) |
32af4dd8 | 936 | return log_debug_errno(errno, "Failed to stat service fd: %m"); |
3543456f RW |
937 | |
938 | flags = fcntl(fd, F_GETFL); | |
939 | if (flags < 0) | |
32af4dd8 | 940 | return log_debug_errno(errno, "Failed to get service fd flags: %m"); |
3543456f RW |
941 | |
942 | (void) fd_get_path(fd, &path); | |
943 | ||
944 | fprintf(f, | |
945 | "%s%s '%s' (type=%s; dev=" DEVNUM_FORMAT_STR "; inode=%" PRIu64 "; rdev=" DEVNUM_FORMAT_STR "; path=%s; access=%s)\n", | |
946 | prefix, | |
947 | header, | |
948 | fdname, | |
949 | strna(inode_type_to_string(st.st_mode)), | |
950 | DEVNUM_FORMAT_VAL(st.st_dev), | |
951 | (uint64_t) st.st_ino, | |
952 | DEVNUM_FORMAT_VAL(st.st_rdev), | |
953 | strna(path), | |
954 | strna(accmode_to_string(flags))); | |
955 | ||
956 | return 0; | |
957 | } | |
958 | ||
87f0e418 | 959 | static void service_dump(Unit *u, FILE *f, const char *prefix) { |
e9fa1bf7 | 960 | Service *s = ASSERT_PTR(SERVICE(u)); |
47be870b | 961 | const char *prefix2; |
5cb5a6ff | 962 | |
4c940960 | 963 | prefix = strempty(prefix); |
63c372cb | 964 | prefix2 = strjoina(prefix, "\t"); |
44d8db9e | 965 | |
5cb5a6ff | 966 | fprintf(f, |
81a2b7ce | 967 | "%sService State: %s\n" |
f42806df LP |
968 | "%sResult: %s\n" |
969 | "%sReload Result: %s\n" | |
4c2f5842 | 970 | "%sClean Result: %s\n" |
dc851222 | 971 | "%sLiveMount Result: %s\n" |
81a2b7ce | 972 | "%sPermissionsStartOnly: %s\n" |
8e274523 | 973 | "%sRootDirectoryStartOnly: %s\n" |
02ee865a | 974 | "%sRemainAfterExit: %s\n" |
3185a36b | 975 | "%sGuessMainPID: %s\n" |
c952c6ec | 976 | "%sType: %s\n" |
2cf3143a | 977 | "%sRestart: %s\n" |
308d72dc | 978 | "%sNotifyAccess: %s\n" |
afcfaa69 | 979 | "%sNotifyState: %s\n" |
3bd28bf7 LP |
980 | "%sOOMPolicy: %s\n" |
981 | "%sReloadSignal: %s\n", | |
81a2b7ce | 982 | prefix, service_state_to_string(s->state), |
f42806df LP |
983 | prefix, service_result_to_string(s->result), |
984 | prefix, service_result_to_string(s->reload_result), | |
5162829e | 985 | prefix, service_result_to_string(s->live_mount_result), |
a53e92a1 | 986 | prefix, service_result_to_string(s->clean_result), |
81a2b7ce | 987 | prefix, yes_no(s->permissions_start_only), |
8e274523 | 988 | prefix, yes_no(s->root_directory_start_only), |
02ee865a | 989 | prefix, yes_no(s->remain_after_exit), |
3185a36b | 990 | prefix, yes_no(s->guess_main_pid), |
c952c6ec | 991 | prefix, service_type_to_string(s->type), |
2cf3143a | 992 | prefix, service_restart_to_string(s->restart), |
19dff691 | 993 | prefix, notify_access_to_string(service_get_notify_access(s)), |
afcfaa69 | 994 | prefix, notify_state_to_string(s->notify_state), |
3bd28bf7 LP |
995 | prefix, oom_policy_to_string(s->oom_policy), |
996 | prefix, signal_to_string(s->reload_signal)); | |
5cb5a6ff | 997 | |
c79ab77c | 998 | if (pidref_is_set(&s->control_pid)) |
70123e68 | 999 | fprintf(f, |
ccd06097 | 1000 | "%sControl PID: "PID_FMT"\n", |
c79ab77c | 1001 | prefix, s->control_pid.pid); |
70123e68 | 1002 | |
c79ab77c | 1003 | if (pidref_is_set(&s->main_pid)) |
70123e68 | 1004 | fprintf(f, |
ccd06097 | 1005 | "%sMain PID: "PID_FMT"\n" |
6dfa5494 LP |
1006 | "%sMain PID Known: %s\n" |
1007 | "%sMain PID Alien: %s\n", | |
c79ab77c | 1008 | prefix, s->main_pid.pid, |
6dfa5494 LP |
1009 | prefix, yes_no(s->main_pid_known), |
1010 | prefix, yes_no(s->main_pid_alien)); | |
70123e68 | 1011 | |
034c6ed7 LP |
1012 | if (s->pid_file) |
1013 | fprintf(f, | |
1014 | "%sPIDFile: %s\n", | |
1015 | prefix, s->pid_file); | |
1016 | ||
05e343b7 LP |
1017 | if (s->bus_name) |
1018 | fprintf(f, | |
1019 | "%sBusName: %s\n" | |
1020 | "%sBus Name Good: %s\n", | |
1021 | prefix, s->bus_name, | |
1022 | prefix, yes_no(s->bus_name_good)); | |
1023 | ||
9dfb64f8 ZJS |
1024 | if (UNIT_ISSET(s->accept_socket)) |
1025 | fprintf(f, | |
1026 | "%sAccept Socket: %s\n", | |
1027 | prefix, UNIT_DEREF(s->accept_socket)->id); | |
1028 | ||
c9d41699 YW |
1029 | fprintf(f, |
1030 | "%sRestartSec: %s\n" | |
be1adc27 | 1031 | "%sRestartSteps: %u\n" |
e9f17fa8 | 1032 | "%sRestartMaxDelaySec: %s\n" |
c9d41699 | 1033 | "%sTimeoutStartSec: %s\n" |
bf760801 JK |
1034 | "%sTimeoutStopSec: %s\n" |
1035 | "%sTimeoutStartFailureMode: %s\n" | |
1036 | "%sTimeoutStopFailureMode: %s\n", | |
5291f26d | 1037 | prefix, FORMAT_TIMESPAN(s->restart_usec, USEC_PER_SEC), |
be1adc27 | 1038 | prefix, s->restart_steps, |
e9f17fa8 | 1039 | prefix, FORMAT_TIMESPAN(s->restart_max_delay_usec, USEC_PER_SEC), |
5291f26d ZJS |
1040 | prefix, FORMAT_TIMESPAN(s->timeout_start_usec, USEC_PER_SEC), |
1041 | prefix, FORMAT_TIMESPAN(s->timeout_stop_usec, USEC_PER_SEC), | |
bf760801 JK |
1042 | prefix, service_timeout_failure_mode_to_string(s->timeout_start_failure_mode), |
1043 | prefix, service_timeout_failure_mode_to_string(s->timeout_stop_failure_mode)); | |
dcab85be YW |
1044 | |
1045 | if (s->timeout_abort_set) | |
1046 | fprintf(f, | |
1047 | "%sTimeoutAbortSec: %s\n", | |
5291f26d | 1048 | prefix, FORMAT_TIMESPAN(s->timeout_abort_usec, USEC_PER_SEC)); |
dcab85be YW |
1049 | |
1050 | fprintf(f, | |
1051 | "%sRuntimeMaxSec: %s\n" | |
5918a933 | 1052 | "%sRuntimeRandomizedExtraSec: %s\n" |
dcab85be | 1053 | "%sWatchdogSec: %s\n", |
5291f26d | 1054 | prefix, FORMAT_TIMESPAN(s->runtime_max_usec, USEC_PER_SEC), |
5918a933 | 1055 | prefix, FORMAT_TIMESPAN(s->runtime_rand_extra_usec, USEC_PER_SEC), |
5291f26d | 1056 | prefix, FORMAT_TIMESPAN(s->watchdog_usec, USEC_PER_SEC)); |
c9d41699 | 1057 | |
4819ff03 | 1058 | kill_context_dump(&s->kill_context, f, prefix); |
5cb5a6ff LP |
1059 | exec_context_dump(&s->exec_context, f, prefix); |
1060 | ||
68e58ca9 | 1061 | for (ServiceExecCommand c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) { |
44d8db9e LP |
1062 | if (!s->exec_command[c]) |
1063 | continue; | |
1064 | ||
3a7bb5c9 | 1065 | fprintf(f, "%s%s %s:\n", |
1ae9b0cf | 1066 | prefix, glyph(GLYPH_ARROW_RIGHT), service_exec_command_to_string(c)); |
44d8db9e LP |
1067 | |
1068 | exec_command_dump_list(s->exec_command[c], f, prefix2); | |
5cb5a6ff | 1069 | } |
44d8db9e | 1070 | |
8c47c732 LP |
1071 | if (s->status_text) |
1072 | fprintf(f, "%sStatus Text: %s\n", | |
1073 | prefix, s->status_text); | |
a354329f | 1074 | |
a74b2840 MY |
1075 | if (s->status_errno > 0) |
1076 | fprintf(f, "%sStatus Errno: %s\n", | |
1077 | prefix, STRERROR(s->status_errno)); | |
1078 | ||
9c025022 MY |
1079 | if (s->status_bus_error) |
1080 | fprintf(f, "%sStatus Bus Error: %s\n", | |
1081 | prefix, s->status_bus_error); | |
1082 | ||
1083 | if (s->status_varlink_error) | |
1084 | fprintf(f, "%sStatus Varlink Error: %s\n", | |
1085 | prefix, s->status_varlink_error); | |
1086 | ||
32af4dd8 | 1087 | if (s->n_fd_store_max > 0) { |
a354329f LP |
1088 | fprintf(f, |
1089 | "%sFile Descriptor Store Max: %u\n" | |
b9c1883a | 1090 | "%sFile Descriptor Store Pin: %s\n" |
da6053d0 | 1091 | "%sFile Descriptor Store Current: %zu\n", |
a354329f | 1092 | prefix, s->n_fd_store_max, |
b9c1883a | 1093 | prefix, exec_preserve_mode_to_string(s->fd_store_preserve_mode), |
a354329f | 1094 | prefix, s->n_fd_store); |
18f573aa | 1095 | |
32af4dd8 MY |
1096 | LIST_FOREACH(fd_store, i, s->fd_store) |
1097 | (void) service_dump_fd(i->fd, | |
1098 | i->fdname, | |
1099 | i == s->fd_store ? "File Descriptor Store Entry:" : " ", | |
1100 | f, | |
1101 | prefix); | |
1102 | } | |
1103 | ||
1104 | FOREACH_ARRAY(i, s->extra_fds, s->n_extra_fds) | |
1105 | (void) service_dump_fd(i->fd, | |
1106 | i->fdname, | |
1107 | i == s->extra_fds ? "Extra File Descriptor Entry:" : " ", | |
1108 | f, | |
1109 | prefix); | |
f2eb0c50 | 1110 | |
cd48e23f RP |
1111 | if (s->open_files) |
1112 | LIST_FOREACH(open_files, of, s->open_files) { | |
1113 | _cleanup_free_ char *ofs = NULL; | |
1114 | int r; | |
1115 | ||
1116 | r = open_file_to_string(of, &ofs); | |
1117 | if (r < 0) { | |
1118 | log_debug_errno(r, | |
1119 | "Failed to convert OpenFile= setting to string, ignoring: %m"); | |
1120 | continue; | |
1121 | } | |
1122 | ||
1123 | fprintf(f, "%sOpen File: %s\n", prefix, ofs); | |
1124 | } | |
1125 | ||
bc0623df | 1126 | cgroup_context_dump(UNIT(s), f, prefix); |
5cb5a6ff LP |
1127 | } |
1128 | ||
495e75ed | 1129 | static int service_is_suitable_main_pid(Service *s, PidRef *pid, int prio) { |
db256aab | 1130 | Unit *owner; |
becdfcb9 | 1131 | int r; |
db256aab LP |
1132 | |
1133 | assert(s); | |
495e75ed | 1134 | assert(pidref_is_set(pid)); |
db256aab LP |
1135 | |
1136 | /* Checks whether the specified PID is suitable as main PID for this service. returns negative if not, 0 if the | |
1137 | * PID is questionnable but should be accepted if the source of configuration is trusted. > 0 if the PID is | |
1138 | * good */ | |
1139 | ||
a7a87769 | 1140 | if (pidref_is_self(pid) || pid->pid == 1) |
495e75ed | 1141 | return log_unit_full_errno(UNIT(s), prio, SYNTHETIC_ERRNO(EPERM), "New main PID "PID_FMT" is the manager, refusing.", pid->pid); |
db256aab | 1142 | |
495e75ed LP |
1143 | if (pidref_equal(pid, &s->control_pid)) |
1144 | return log_unit_full_errno(UNIT(s), prio, SYNTHETIC_ERRNO(EPERM), "New main PID "PID_FMT" is the control process, refusing.", pid->pid); | |
db256aab | 1145 | |
becdfcb9 | 1146 | r = pidref_is_alive(pid); |
bca08053 MY |
1147 | if (r < 0) |
1148 | return log_unit_full_errno(UNIT(s), prio, r, "Failed to check if main PID "PID_FMT" exists or is a zombie: %m", pid->pid); | |
becdfcb9 | 1149 | if (r == 0) |
495e75ed | 1150 | return log_unit_full_errno(UNIT(s), prio, SYNTHETIC_ERRNO(ESRCH), "New main PID "PID_FMT" does not exist or is a zombie.", pid->pid); |
db256aab | 1151 | |
495e75ed | 1152 | owner = manager_get_unit_by_pidref(UNIT(s)->manager, pid); |
db256aab | 1153 | if (owner == UNIT(s)) { |
495e75ed | 1154 | log_unit_debug(UNIT(s), "New main PID "PID_FMT" belongs to service, we are happy.", pid->pid); |
db256aab LP |
1155 | return 1; /* Yay, it's definitely a good PID */ |
1156 | } | |
1157 | ||
1158 | return 0; /* Hmm it's a suspicious PID, let's accept it if configuration source is trusted */ | |
1159 | } | |
1160 | ||
c5419d42 | 1161 | static int service_load_pid_file(Service *s, bool may_warn) { |
b1f6901d | 1162 | _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
7e6ef434 | 1163 | _cleanup_fclose_ FILE *f = NULL; |
7fd1b19b | 1164 | _cleanup_free_ char *k = NULL; |
7e6ef434 MY |
1165 | bool questionable_pid_file = false; |
1166 | int r, prio = may_warn ? LOG_INFO : LOG_DEBUG; | |
034c6ed7 LP |
1167 | |
1168 | assert(s); | |
1169 | ||
034c6ed7 | 1170 | if (!s->pid_file) |
13230d5d | 1171 | return -ENOENT; |
034c6ed7 | 1172 | |
7e6ef434 | 1173 | r = chase_and_fopen_unlocked(s->pid_file, NULL, CHASE_SAFE, "re", NULL, &f); |
a5648b80 | 1174 | if (r == -ENOLINK) { |
8ed6f81b YW |
1175 | log_unit_debug_errno(UNIT(s), r, |
1176 | "Potentially unsafe symlink chain, will now retry with relaxed checks: %s", s->pid_file); | |
73969ab6 LP |
1177 | |
1178 | questionable_pid_file = true; | |
1179 | ||
7e6ef434 | 1180 | r = chase_and_fopen_unlocked(s->pid_file, NULL, 0, "re", NULL, &f); |
73969ab6 | 1181 | } |
a5648b80 | 1182 | if (r < 0) |
f86a388d | 1183 | return log_unit_full_errno(UNIT(s), prio, r, |
7e6ef434 | 1184 | "Can't open PID file '%s' (yet?) after %s: %m", s->pid_file, service_state_to_string(s->state)); |
db256aab | 1185 | |
7e6ef434 MY |
1186 | /* Let's read the PID file now that we chased it down. */ |
1187 | r = read_line(f, LINE_MAX, &k); | |
db256aab | 1188 | if (r < 0) |
7e6ef434 | 1189 | return log_unit_error_errno(UNIT(s), r, "Failed to read PID file '%s': %m", s->pid_file); |
034c6ed7 | 1190 | |
495e75ed | 1191 | r = pidref_set_pidstr(&pidref, k); |
db256aab | 1192 | if (r < 0) |
7e6ef434 | 1193 | return log_unit_full_errno(UNIT(s), prio, r, "Failed to create reference to PID %s from file '%s': %m", k, s->pid_file); |
db256aab | 1194 | |
495e75ed | 1195 | if (s->main_pid_known && pidref_equal(&pidref, &s->main_pid)) |
db256aab LP |
1196 | return 0; |
1197 | ||
495e75ed | 1198 | r = service_is_suitable_main_pid(s, &pidref, prio); |
db256aab | 1199 | if (r < 0) |
5925dd3c | 1200 | return r; |
db256aab LP |
1201 | if (r == 0) { |
1202 | struct stat st; | |
406eaf93 | 1203 | |
d85ff944 YW |
1204 | if (questionable_pid_file) |
1205 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(EPERM), | |
1206 | "Refusing to accept PID outside of service control group, acquired through unsafe symlink chain: %s", s->pid_file); | |
73969ab6 | 1207 | |
db256aab LP |
1208 | /* Hmm, it's not clear if the new main PID is safe. Let's allow this if the PID file is owned by root */ |
1209 | ||
7e6ef434 MY |
1210 | if (fstat(fileno(f), &st) < 0) |
1211 | return log_unit_error_errno(UNIT(s), errno, "Failed to fstat() PID file '%s': %m", s->pid_file); | |
db256aab | 1212 | |
7e269126 | 1213 | if (st.st_uid != getuid()) |
d85ff944 | 1214 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(EPERM), |
7e269126 YW |
1215 | "New main PID "PID_FMT" from PID file does not belong to service, and PID file is owned by "UID_FMT" (must be owned by "UID_FMT"). Refusing.", |
1216 | pidref.pid, st.st_uid, getuid()); | |
db256aab | 1217 | |
7e269126 YW |
1218 | log_unit_debug(UNIT(s), "New main PID "PID_FMT" does not belong to service, accepting anyway since PID file is owned by "UID_FMT".", |
1219 | pidref.pid, st.st_uid); | |
e10c9985 YS |
1220 | } |
1221 | ||
db01f8b3 | 1222 | if (s->main_pid_known) { |
b1f6901d | 1223 | log_unit_debug(UNIT(s), "Main PID changing: "PID_FMT" -> "PID_FMT, s->main_pid.pid, pidref.pid); |
8bb2d17d | 1224 | |
db01f8b3 MS |
1225 | service_unwatch_main_pid(s); |
1226 | s->main_pid_known = false; | |
3a111838 | 1227 | } else |
b1f6901d | 1228 | log_unit_debug(UNIT(s), "Main PID loaded: "PID_FMT, pidref.pid); |
db01f8b3 | 1229 | |
7e0e6b50 | 1230 | r = service_set_main_pidref(s, TAKE_PIDREF(pidref), /* start_timestamp = */ NULL); |
117dcc57 | 1231 | if (r < 0) |
16f6025e LP |
1232 | return r; |
1233 | ||
495e75ed | 1234 | r = unit_watch_pidref(UNIT(s), &s->main_pid, /* exclusive= */ false); |
e8b509d3 | 1235 | if (r < 0) /* FIXME: we need to do something here */ |
b1f6901d | 1236 | return log_unit_warning_errno(UNIT(s), r, "Failed to watch PID "PID_FMT" for service: %m", s->main_pid.pid); |
034c6ed7 | 1237 | |
db256aab | 1238 | return 1; |
034c6ed7 LP |
1239 | } |
1240 | ||
783e05d6 | 1241 | static void service_search_main_pid(Service *s) { |
495e75ed | 1242 | _cleanup_(pidref_done) PidRef pid = PIDREF_NULL; |
4fbf50b3 LP |
1243 | int r; |
1244 | ||
1245 | assert(s); | |
1246 | ||
c79ab77c | 1247 | /* If we know it anyway, don't ever fall back to unreliable heuristics */ |
4fbf50b3 | 1248 | if (s->main_pid_known) |
783e05d6 | 1249 | return; |
4fbf50b3 | 1250 | |
3185a36b | 1251 | if (!s->guess_main_pid) |
783e05d6 | 1252 | return; |
3185a36b | 1253 | |
c79ab77c | 1254 | assert(!pidref_is_set(&s->main_pid)); |
4fbf50b3 | 1255 | |
783e05d6 ZJS |
1256 | if (unit_search_main_pid(UNIT(s), &pid) < 0) |
1257 | return; | |
4fbf50b3 | 1258 | |
495e75ed | 1259 | log_unit_debug(UNIT(s), "Main PID guessed: "PID_FMT, pid.pid); |
7e0e6b50 | 1260 | if (service_set_main_pidref(s, TAKE_PIDREF(pid), /* start_timestamp = */ NULL) < 0) |
783e05d6 | 1261 | return; |
4fbf50b3 | 1262 | |
495e75ed | 1263 | r = unit_watch_pidref(UNIT(s), &s->main_pid, /* exclusive= */ false); |
783e05d6 | 1264 | if (r < 0) |
4fbf50b3 | 1265 | /* FIXME: we need to do something here */ |
f5b0e4f9 | 1266 | log_unit_warning_errno(UNIT(s), r, "Failed to watch main PID "PID_FMT": %m", s->main_pid.pid); |
4fbf50b3 LP |
1267 | } |
1268 | ||
034c6ed7 | 1269 | static void service_set_state(Service *s, ServiceState state) { |
c26948c6 | 1270 | Unit *u = UNIT(ASSERT_PTR(s)); |
034c6ed7 | 1271 | ServiceState old_state; |
e056b01d | 1272 | const UnitActiveState *table; |
842129f5 | 1273 | |
6fcbec6f | 1274 | if (s->state != state) |
c26948c6 | 1275 | bus_unit_send_pending_change_signal(u, false); |
6fcbec6f | 1276 | |
e056b01d LP |
1277 | table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table; |
1278 | ||
034c6ed7 | 1279 | old_state = s->state; |
5cb5a6ff | 1280 | s->state = state; |
034c6ed7 | 1281 | |
3a111838 MS |
1282 | service_unwatch_pid_file(s); |
1283 | ||
842129f5 | 1284 | if (!IN_SET(state, |
31cd5f63 | 1285 | SERVICE_CONDITION, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, |
36c16a7c | 1286 | SERVICE_RUNNING, |
dfdeb0b1 | 1287 | SERVICE_RELOAD, SERVICE_RELOAD_SIGNAL, SERVICE_RELOAD_NOTIFY, SERVICE_REFRESH_EXTENSIONS, |
a53e92a1 | 1288 | SERVICE_MOUNTING, |
c87700a1 | 1289 | SERVICE_STOP, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
bf760801 | 1290 | SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL, |
4c2f5842 LP |
1291 | SERVICE_AUTO_RESTART, |
1292 | SERVICE_CLEANING)) | |
5dcadb4c | 1293 | s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); |
034c6ed7 | 1294 | |
94114711 | 1295 | if (!SERVICE_STATE_WITH_MAIN_PROCESS(state)) { |
5e94833f | 1296 | service_unwatch_main_pid(s); |
867b3b7d LP |
1297 | s->main_command = NULL; |
1298 | } | |
034c6ed7 | 1299 | |
94114711 | 1300 | if (!SERVICE_STATE_WITH_CONTROL_PROCESS(state)) { |
5e94833f | 1301 | service_unwatch_control_pid(s); |
034c6ed7 | 1302 | s->control_command = NULL; |
a16e1123 | 1303 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; |
e537352b | 1304 | } |
034c6ed7 | 1305 | |
a1d31573 LP |
1306 | if (IN_SET(state, |
1307 | SERVICE_DEAD, SERVICE_FAILED, | |
09d04ad3 | 1308 | SERVICE_DEAD_BEFORE_AUTO_RESTART, SERVICE_FAILED_BEFORE_AUTO_RESTART, SERVICE_AUTO_RESTART, SERVICE_AUTO_RESTART_QUEUED, |
55e4df21 | 1309 | SERVICE_DEAD_RESOURCES_PINNED)) |
c26948c6 | 1310 | unit_unwatch_all_pids(u); |
a911bb9a | 1311 | |
5686391b | 1312 | if (state != SERVICE_START) |
5dcadb4c | 1313 | s->exec_fd_event_source = sd_event_source_disable_unref(s->exec_fd_event_source); |
5686391b | 1314 | |
dfdeb0b1 | 1315 | if (!IN_SET(state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD, SERVICE_RELOAD_SIGNAL, SERVICE_RELOAD_NOTIFY, SERVICE_REFRESH_EXTENSIONS, SERVICE_MOUNTING)) |
a6927d7f MO |
1316 | service_stop_watchdog(s); |
1317 | ||
a53e92a1 MY |
1318 | if (state != SERVICE_MOUNTING) /* Just in case */ |
1319 | s->mount_request = sd_bus_message_unref(s->mount_request); | |
1320 | ||
c26948c6 MY |
1321 | if (state == SERVICE_EXITED && !MANAGER_IS_RELOADING(u->manager)) { |
1322 | /* For the inactive states unit_notify() will trim the cgroup. But for exit we have to | |
1323 | * do that ourselves... */ | |
1324 | unit_prune_cgroup(u); | |
1325 | ||
1326 | /* If none of ExecReload= and ExecStop*= is used, we can safely destroy runtime data | |
1327 | * as soon as the service enters SERVICE_EXITED. This saves us from keeping the credential mount | |
1328 | * for the whole duration of the oneshot service while no processes are actually running, | |
1329 | * among other things. */ | |
1330 | ||
1331 | bool start_only = true; | |
1332 | for (ServiceExecCommand c = SERVICE_EXEC_RELOAD; c < _SERVICE_EXEC_COMMAND_MAX; c++) | |
1333 | if (s->exec_command[c]) { | |
1334 | start_only = false; | |
1335 | break; | |
1336 | } | |
1337 | ||
1338 | if (start_only) | |
703b1b7f | 1339 | unit_destroy_runtime_data(u, &s->exec_context, /* destroy_runtime_dir = */ false); |
c26948c6 | 1340 | } |
f6023656 | 1341 | |
e537352b | 1342 | if (old_state != state) |
c26948c6 | 1343 | log_unit_debug(u, "Changed %s -> %s", service_state_to_string(old_state), service_state_to_string(state)); |
acbb0225 | 1344 | |
c26948c6 | 1345 | unit_notify(u, table[old_state], table[state], s->reload_result == SERVICE_SUCCESS); |
034c6ed7 LP |
1346 | } |
1347 | ||
36c16a7c LP |
1348 | static usec_t service_coldplug_timeout(Service *s) { |
1349 | assert(s); | |
1350 | ||
1351 | switch (s->deserialized_state) { | |
1352 | ||
31cd5f63 | 1353 | case SERVICE_CONDITION: |
36c16a7c LP |
1354 | case SERVICE_START_PRE: |
1355 | case SERVICE_START: | |
1356 | case SERVICE_START_POST: | |
1357 | case SERVICE_RELOAD: | |
3bd28bf7 LP |
1358 | case SERVICE_RELOAD_SIGNAL: |
1359 | case SERVICE_RELOAD_NOTIFY: | |
dfdeb0b1 | 1360 | case SERVICE_REFRESH_EXTENSIONS: |
a53e92a1 | 1361 | case SERVICE_MOUNTING: |
36c16a7c LP |
1362 | return usec_add(UNIT(s)->state_change_timestamp.monotonic, s->timeout_start_usec); |
1363 | ||
1364 | case SERVICE_RUNNING: | |
5918a933 | 1365 | return service_running_timeout(s); |
36c16a7c LP |
1366 | |
1367 | case SERVICE_STOP: | |
36c16a7c LP |
1368 | case SERVICE_STOP_SIGTERM: |
1369 | case SERVICE_STOP_SIGKILL: | |
1370 | case SERVICE_STOP_POST: | |
1371 | case SERVICE_FINAL_SIGTERM: | |
1372 | case SERVICE_FINAL_SIGKILL: | |
1373 | return usec_add(UNIT(s)->state_change_timestamp.monotonic, s->timeout_stop_usec); | |
1374 | ||
dc653bf4 | 1375 | case SERVICE_STOP_WATCHDOG: |
bf760801 | 1376 | case SERVICE_FINAL_WATCHDOG: |
dc653bf4 JK |
1377 | return usec_add(UNIT(s)->state_change_timestamp.monotonic, service_timeout_abort_usec(s)); |
1378 | ||
36c16a7c | 1379 | case SERVICE_AUTO_RESTART: |
5171356e | 1380 | return usec_add(UNIT(s)->inactive_enter_timestamp.monotonic, service_restart_usec_next(s)); |
36c16a7c | 1381 | |
4c2f5842 | 1382 | case SERVICE_CLEANING: |
12213aed | 1383 | return usec_add(UNIT(s)->state_change_timestamp.monotonic, s->exec_context.timeout_clean_usec); |
4c2f5842 | 1384 | |
36c16a7c LP |
1385 | default: |
1386 | return USEC_INFINITY; | |
1387 | } | |
1388 | } | |
1389 | ||
be847e82 | 1390 | static int service_coldplug(Unit *u) { |
a16e1123 LP |
1391 | Service *s = SERVICE(u); |
1392 | int r; | |
1393 | ||
1394 | assert(s); | |
1395 | assert(s->state == SERVICE_DEAD); | |
1396 | ||
930d2838 LP |
1397 | if (s->deserialized_state == s->state) |
1398 | return 0; | |
6c12b52e | 1399 | |
e5d6dcce | 1400 | r = service_arm_timer(s, /* relative= */ false, service_coldplug_timeout(s)); |
36c16a7c LP |
1401 | if (r < 0) |
1402 | return r; | |
a911bb9a | 1403 | |
c79ab77c | 1404 | if (pidref_is_set(&s->main_pid) && |
4d9f092b | 1405 | pidref_is_unwaited(&s->main_pid) > 0 && |
94114711 | 1406 | SERVICE_STATE_WITH_MAIN_PROCESS(s->deserialized_state)) { |
495e75ed | 1407 | r = unit_watch_pidref(UNIT(s), &s->main_pid, /* exclusive= */ false); |
930d2838 LP |
1408 | if (r < 0) |
1409 | return r; | |
1410 | } | |
bb242b7b | 1411 | |
c79ab77c | 1412 | if (pidref_is_set(&s->control_pid) && |
4d9f092b | 1413 | pidref_is_unwaited(&s->control_pid) > 0 && |
94114711 | 1414 | SERVICE_STATE_WITH_CONTROL_PROCESS(s->deserialized_state)) { |
495e75ed | 1415 | r = unit_watch_pidref(UNIT(s), &s->control_pid, /* exclusive= */ false); |
930d2838 LP |
1416 | if (r < 0) |
1417 | return r; | |
a16e1123 | 1418 | } |
92c1622e | 1419 | |
a1d31573 LP |
1420 | if (!IN_SET(s->deserialized_state, |
1421 | SERVICE_DEAD, SERVICE_FAILED, | |
09d04ad3 | 1422 | SERVICE_DEAD_BEFORE_AUTO_RESTART, SERVICE_FAILED_BEFORE_AUTO_RESTART, SERVICE_AUTO_RESTART, SERVICE_AUTO_RESTART_QUEUED, |
b9c1883a | 1423 | SERVICE_CLEANING, |
55e4df21 | 1424 | SERVICE_DEAD_RESOURCES_PINNED)) |
e8a565cb | 1425 | (void) unit_setup_exec_runtime(u); |
29206d46 | 1426 | |
c70816fd YW |
1427 | if (IN_SET(s->deserialized_state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD, SERVICE_RELOAD_SIGNAL, SERVICE_RELOAD_NOTIFY, SERVICE_REFRESH_EXTENSIONS, SERVICE_MOUNTING) && |
1428 | freezer_state_finish(u->freezer_state) == FREEZER_RUNNING) | |
50be4f4a LP |
1429 | service_start_watchdog(s); |
1430 | ||
3ebcd323 | 1431 | if (UNIT_ISSET(s->accept_socket)) { |
9f5d8c3d | 1432 | Socket *socket = SOCKET(UNIT_DEREF(s->accept_socket)); |
3ebcd323 ZJS |
1433 | |
1434 | if (socket->max_connections_per_source > 0) { | |
1435 | SocketPeer *peer; | |
1436 | ||
1437 | /* Make a best-effort attempt at bumping the connection count */ | |
1438 | if (socket_acquire_peer(socket, s->socket_fd, &peer) > 0) { | |
3fabebf4 LP |
1439 | socket_peer_unref(s->socket_peer); |
1440 | s->socket_peer = peer; | |
3ebcd323 ZJS |
1441 | } |
1442 | } | |
1443 | } | |
1444 | ||
930d2838 | 1445 | service_set_state(s, s->deserialized_state); |
a16e1123 LP |
1446 | return 0; |
1447 | } | |
1448 | ||
25b583d7 LP |
1449 | static int service_collect_fds( |
1450 | Service *s, | |
1451 | int **fds, | |
1452 | char ***fd_names, | |
1453 | size_t *n_socket_fds, | |
3543456f RW |
1454 | size_t *n_storage_fds, |
1455 | size_t *n_extra_fds) { | |
4c47affc | 1456 | |
8dd4c05b | 1457 | _cleanup_strv_free_ char **rfd_names = NULL; |
a354329f | 1458 | _cleanup_free_ int *rfds = NULL; |
32af4dd8 | 1459 | size_t rn_socket_fds = 0; |
4c47affc | 1460 | int r; |
44d8db9e LP |
1461 | |
1462 | assert(s); | |
1463 | assert(fds); | |
8dd4c05b | 1464 | assert(fd_names); |
9b141911 | 1465 | assert(n_socket_fds); |
25b583d7 | 1466 | assert(n_storage_fds); |
3543456f | 1467 | assert(n_extra_fds); |
44d8db9e | 1468 | |
8dd4c05b | 1469 | if (s->socket_fd >= 0) { |
daa78907 | 1470 | Socket *sock = ASSERT_PTR(SOCKET(UNIT_DEREF(s->accept_socket))); |
6cf6bbc2 | 1471 | |
8dd4c05b | 1472 | /* Pass the per-connection socket */ |
57020a3a | 1473 | |
aa7c4dd6 | 1474 | rfds = newdup(int, &s->socket_fd, 1); |
8dd4c05b LP |
1475 | if (!rfds) |
1476 | return -ENOMEM; | |
57020a3a | 1477 | |
daa78907 | 1478 | rfd_names = strv_new(socket_fdname(sock)); |
8dd4c05b LP |
1479 | if (!rfd_names) |
1480 | return -ENOMEM; | |
44d8db9e | 1481 | |
4c47affc | 1482 | rn_socket_fds = 1; |
8dd4c05b | 1483 | } else { |
8dd4c05b | 1484 | /* Pass all our configured sockets for singleton services */ |
44d8db9e | 1485 | |
0ec3d45b | 1486 | Unit *u; |
15ed3c3a | 1487 | UNIT_FOREACH_DEPENDENCY(u, UNIT(s), UNIT_ATOM_TRIGGERED_BY) { |
8dd4c05b | 1488 | _cleanup_free_ int *cfds = NULL; |
8dd4c05b | 1489 | int cn_fds; |
8d83e0c2 | 1490 | Socket *sock; |
44d8db9e | 1491 | |
8dd4c05b | 1492 | sock = SOCKET(u); |
469ff6b4 MY |
1493 | if (!sock) |
1494 | continue; | |
a354329f | 1495 | |
8dd4c05b LP |
1496 | cn_fds = socket_collect_fds(sock, &cfds); |
1497 | if (cn_fds < 0) | |
1498 | return cn_fds; | |
0ec3d45b | 1499 | if (cn_fds == 0) |
8dd4c05b | 1500 | continue; |
79c7626d | 1501 | |
8dd4c05b | 1502 | if (!rfds) { |
1cc6c93a | 1503 | rfds = TAKE_PTR(cfds); |
4c47affc | 1504 | rn_socket_fds = cn_fds; |
8d83e0c2 MY |
1505 | } else if (!GREEDY_REALLOC_APPEND(rfds, rn_socket_fds, cfds, cn_fds)) |
1506 | return -ENOMEM; | |
8dd4c05b LP |
1507 | |
1508 | r = strv_extend_n(&rfd_names, socket_fdname(sock), cn_fds); | |
1509 | if (r < 0) | |
1510 | return r; | |
44d8db9e LP |
1511 | } |
1512 | } | |
1513 | ||
32af4dd8 MY |
1514 | if (s->n_fd_store + s->n_extra_fds > 0) { |
1515 | int *t = reallocarray(rfds, rn_socket_fds + s->n_fd_store + s->n_extra_fds, sizeof(int)); | |
a354329f LP |
1516 | if (!t) |
1517 | return -ENOMEM; | |
a354329f | 1518 | rfds = t; |
8dd4c05b | 1519 | |
32af4dd8 | 1520 | char **nl = reallocarray(rfd_names, rn_socket_fds + s->n_fd_store + s->n_extra_fds + 1, sizeof(char *)); |
8dd4c05b LP |
1521 | if (!nl) |
1522 | return -ENOMEM; | |
8dd4c05b | 1523 | rfd_names = nl; |
32af4dd8 MY |
1524 | |
1525 | size_t n_fds = rn_socket_fds; | |
8dd4c05b LP |
1526 | |
1527 | LIST_FOREACH(fd_store, fs, s->fd_store) { | |
4c47affc | 1528 | rfds[n_fds] = fs->fd; |
32af4dd8 | 1529 | rfd_names[n_fds] = strdup(fs->fdname); |
4c47affc | 1530 | if (!rfd_names[n_fds]) |
8dd4c05b LP |
1531 | return -ENOMEM; |
1532 | ||
4c47affc | 1533 | n_fds++; |
8dd4c05b LP |
1534 | } |
1535 | ||
32af4dd8 MY |
1536 | FOREACH_ARRAY(i, s->extra_fds, s->n_extra_fds) { |
1537 | rfds[n_fds] = i->fd; | |
1538 | rfd_names[n_fds] = strdup(i->fdname); | |
3543456f RW |
1539 | if (!rfd_names[n_fds]) |
1540 | return -ENOMEM; | |
1541 | ||
1542 | n_fds++; | |
1543 | } | |
1544 | ||
1545 | rfd_names[n_fds] = NULL; | |
1546 | } | |
1547 | ||
1cc6c93a YW |
1548 | *fds = TAKE_PTR(rfds); |
1549 | *fd_names = TAKE_PTR(rfd_names); | |
9b141911 | 1550 | *n_socket_fds = rn_socket_fds; |
32af4dd8 MY |
1551 | *n_storage_fds = s->n_fd_store; |
1552 | *n_extra_fds = s->n_extra_fds; | |
3e33402a | 1553 | |
4c47affc | 1554 | return 0; |
44d8db9e LP |
1555 | } |
1556 | ||
5686391b LP |
1557 | static int service_allocate_exec_fd_event_source( |
1558 | Service *s, | |
1559 | int fd, | |
1560 | sd_event_source **ret_event_source) { | |
1561 | ||
1562 | _cleanup_(sd_event_source_unrefp) sd_event_source *source = NULL; | |
1563 | int r; | |
1564 | ||
1565 | assert(s); | |
1566 | assert(fd >= 0); | |
1567 | assert(ret_event_source); | |
1568 | ||
1569 | r = sd_event_add_io(UNIT(s)->manager->event, &source, fd, 0, service_dispatch_exec_io, s); | |
1570 | if (r < 0) | |
1571 | return log_unit_error_errno(UNIT(s), r, "Failed to allocate exec_fd event source: %m"); | |
1572 | ||
f3a269a9 LP |
1573 | /* This is a bit higher priority than SIGCHLD, to make sure we don't confuse the case "failed to |
1574 | * start" from the case "succeeded to start, but failed immediately after". */ | |
5686391b | 1575 | |
d42b61d2 | 1576 | r = sd_event_source_set_priority(source, EVENT_PRIORITY_EXEC_FD); |
5686391b LP |
1577 | if (r < 0) |
1578 | return log_unit_error_errno(UNIT(s), r, "Failed to adjust priority of exec_fd event source: %m"); | |
1579 | ||
bc989831 | 1580 | (void) sd_event_source_set_description(source, "service exec_fd"); |
5686391b LP |
1581 | |
1582 | r = sd_event_source_set_io_fd_own(source, true); | |
1583 | if (r < 0) | |
1584 | return log_unit_error_errno(UNIT(s), r, "Failed to pass ownership of fd to event source: %m"); | |
1585 | ||
1586 | *ret_event_source = TAKE_PTR(source); | |
1587 | return 0; | |
1588 | } | |
1589 | ||
1590 | static int service_allocate_exec_fd( | |
1591 | Service *s, | |
1592 | sd_event_source **ret_event_source, | |
e78695d4 | 1593 | int *ret_exec_fd) { |
5686391b | 1594 | |
71136404 | 1595 | _cleanup_close_pair_ int p[] = EBADF_PAIR; |
5686391b LP |
1596 | int r; |
1597 | ||
1598 | assert(s); | |
1599 | assert(ret_event_source); | |
1600 | assert(ret_exec_fd); | |
1601 | ||
1602 | if (pipe2(p, O_CLOEXEC|O_NONBLOCK) < 0) | |
1603 | return log_unit_error_errno(UNIT(s), errno, "Failed to allocate exec_fd pipe: %m"); | |
1604 | ||
1605 | r = service_allocate_exec_fd_event_source(s, p[0], ret_event_source); | |
1606 | if (r < 0) | |
1607 | return r; | |
1608 | ||
e78695d4 | 1609 | TAKE_FD(p[0]); |
5686391b LP |
1610 | *ret_exec_fd = TAKE_FD(p[1]); |
1611 | ||
1612 | return 0; | |
1613 | } | |
1614 | ||
6375bd20 JW |
1615 | static bool service_exec_needs_notify_socket(Service *s, ExecFlags flags) { |
1616 | assert(s); | |
1617 | ||
1618 | /* Notifications are accepted depending on the process and | |
1619 | * the access setting of the service: | |
1620 | * process: \ access: NONE MAIN EXEC ALL | |
1621 | * main no yes yes yes | |
1622 | * control no no yes yes | |
1623 | * other (forked) no no no yes */ | |
1624 | ||
1625 | if (flags & EXEC_IS_CONTROL) | |
1626 | /* A control process */ | |
19dff691 | 1627 | return IN_SET(service_get_notify_access(s), NOTIFY_EXEC, NOTIFY_ALL); |
6375bd20 JW |
1628 | |
1629 | /* We only spawn main processes and control processes, so any | |
1630 | * process that is not a control process is a main process */ | |
19dff691 | 1631 | return service_get_notify_access(s) != NOTIFY_NONE; |
6375bd20 JW |
1632 | } |
1633 | ||
95c81c55 LB |
1634 | static Service *service_get_triggering_service(Service *s) { |
1635 | Unit *candidate = NULL, *other; | |
cdebedb4 | 1636 | |
95c81c55 | 1637 | assert(s); |
cdebedb4 | 1638 | |
95c81c55 LB |
1639 | /* Return the service which triggered service 's', this means dependency |
1640 | * types which include the UNIT_ATOM_ON_{FAILURE,SUCCESS}_OF atoms. | |
cdebedb4 | 1641 | * |
95c81c55 LB |
1642 | * N.B. if there are multiple services which could trigger 's' via OnFailure= |
1643 | * or OnSuccess= then we return NULL. This is since we don't know from which | |
1644 | * one to propagate the exit status. */ | |
1645 | ||
e76d83d1 | 1646 | UNIT_FOREACH_DEPENDENCY(other, UNIT(s), UNIT_ATOM_ON_SUCCESS_OF|UNIT_ATOM_ON_FAILURE_OF) { |
7a5049c7 ZJS |
1647 | if (candidate) |
1648 | goto have_other; | |
95c81c55 | 1649 | candidate = other; |
cdebedb4 PM |
1650 | } |
1651 | ||
95c81c55 | 1652 | return SERVICE(candidate); |
7a5049c7 ZJS |
1653 | |
1654 | have_other: | |
1655 | log_unit_warning(UNIT(s), "multiple trigger source candidates for exit status propagation (%s, %s), skipping.", | |
1656 | candidate->id, other->id); | |
1657 | return NULL; | |
cdebedb4 PM |
1658 | } |
1659 | ||
cfbf7538 | 1660 | static ExecFlags service_exec_flags(ServiceExecCommand command_id, ExecFlags cred_flag) { |
81006ebb MY |
1661 | /* All service main/control processes honor sandboxing and namespacing options (except those |
1662 | explicitly excluded in service_spawn()) */ | |
1663 | ExecFlags flags = EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT; | |
1664 | ||
1665 | assert(command_id >= 0); | |
1666 | assert(command_id < _SERVICE_EXEC_COMMAND_MAX); | |
cfbf7538 MY |
1667 | assert((cred_flag & ~(EXEC_SETUP_CREDENTIALS_FRESH|EXEC_SETUP_CREDENTIALS)) == 0); |
1668 | assert((cred_flag != 0) == (command_id == SERVICE_EXEC_START)); | |
81006ebb MY |
1669 | |
1670 | /* Control processes spawned before main process also get tty access */ | |
fe760177 | 1671 | if (IN_SET(command_id, SERVICE_EXEC_CONDITION, SERVICE_EXEC_START_PRE, SERVICE_EXEC_START)) |
81006ebb MY |
1672 | flags |= EXEC_APPLY_TTY_STDIN; |
1673 | ||
cfbf7538 MY |
1674 | /* All start phases get access to credentials. ExecStartPre= gets a new credential store upon |
1675 | * every invocation, so that updating credential files through it works. When the first main process | |
1676 | * starts, passed creds become stable. Also see 'cred_flag'. */ | |
1677 | if (command_id == SERVICE_EXEC_START_PRE) | |
1678 | flags |= EXEC_SETUP_CREDENTIALS_FRESH; | |
1679 | if (command_id == SERVICE_EXEC_START_POST) | |
1221ba0f | 1680 | flags |= EXEC_SETUP_CREDENTIALS; |
81006ebb MY |
1681 | |
1682 | if (IN_SET(command_id, SERVICE_EXEC_START_PRE, SERVICE_EXEC_START)) | |
1683 | flags |= EXEC_SETENV_MONITOR_RESULT; | |
1684 | ||
1685 | if (command_id == SERVICE_EXEC_START) | |
cfbf7538 | 1686 | return flags|cred_flag|EXEC_PASS_FDS|EXEC_SET_WATCHDOG; |
81006ebb MY |
1687 | |
1688 | flags |= EXEC_IS_CONTROL; | |
1689 | ||
1690 | /* Put control processes spawned later than main process under .control sub-cgroup if appropriate */ | |
1691 | if (!IN_SET(command_id, SERVICE_EXEC_CONDITION, SERVICE_EXEC_START_PRE)) | |
1692 | flags |= EXEC_CONTROL_CGROUP; | |
1693 | ||
1694 | if (IN_SET(command_id, SERVICE_EXEC_STOP, SERVICE_EXEC_STOP_POST)) | |
1695 | flags |= EXEC_SETENV_RESULT; | |
1696 | ||
1697 | return flags; | |
1698 | } | |
1699 | ||
edbf8984 ZJS |
1700 | static int service_spawn_internal( |
1701 | const char *caller, | |
81a2b7ce LP |
1702 | Service *s, |
1703 | ExecCommand *c, | |
cfbf7538 | 1704 | ExecFlags flags, |
21b2ce39 | 1705 | usec_t timeout, |
c79ab77c | 1706 | PidRef *ret_pid) { |
81a2b7ce | 1707 | |
cfbf7538 | 1708 | _cleanup_(exec_params_shallow_clear) ExecParameters exec_params = EXEC_PARAMETERS_INIT(flags); |
5686391b | 1709 | _cleanup_(sd_event_source_unrefp) sd_event_source *exec_fd_source = NULL; |
c3f8a065 | 1710 | _cleanup_strv_free_ char **final_env = NULL, **our_env = NULL; |
c79ab77c | 1711 | _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
c3f8a065 | 1712 | size_t n_env = 0; |
8dd4c05b LP |
1713 | int r; |
1714 | ||
edbf8984 | 1715 | assert(caller); |
034c6ed7 LP |
1716 | assert(s); |
1717 | assert(c); | |
e78695d4 | 1718 | assert(ret_pid); |
034c6ed7 | 1719 | |
edbf8984 ZJS |
1720 | log_unit_debug(UNIT(s), "Will spawn child (%s): %s", caller, c->path); |
1721 | ||
78f93209 | 1722 | r = unit_prepare_exec(UNIT(s)); /* This realizes the cgroup, among other things */ |
3c7416b6 LP |
1723 | if (r < 0) |
1724 | return r; | |
1725 | ||
bc989831 ZJS |
1726 | assert(!s->exec_fd_event_source); |
1727 | ||
81006ebb | 1728 | if (FLAGS_SET(exec_params.flags, EXEC_IS_CONTROL)) { |
9c1a61ad LP |
1729 | /* If this is a control process, mask the permissions/chroot application if this is requested. */ |
1730 | if (s->permissions_start_only) | |
1703fa41 | 1731 | exec_params.flags &= ~EXEC_APPLY_SANDBOXING; |
9c1a61ad LP |
1732 | if (s->root_directory_start_only) |
1733 | exec_params.flags &= ~EXEC_APPLY_CHROOT; | |
1734 | } | |
1735 | ||
81006ebb | 1736 | if (FLAGS_SET(exec_params.flags, EXEC_PASS_FDS) || |
6cf6bbc2 LP |
1737 | s->exec_context.std_input == EXEC_INPUT_SOCKET || |
1738 | s->exec_context.std_output == EXEC_OUTPUT_SOCKET || | |
1739 | s->exec_context.std_error == EXEC_OUTPUT_SOCKET) { | |
1740 | ||
c3f8a065 LP |
1741 | r = service_collect_fds(s, |
1742 | &exec_params.fds, | |
1743 | &exec_params.fd_names, | |
1744 | &exec_params.n_socket_fds, | |
3543456f RW |
1745 | &exec_params.n_storage_fds, |
1746 | &exec_params.n_extra_fds); | |
8dd4c05b | 1747 | if (r < 0) |
36c16a7c | 1748 | return r; |
6cf6bbc2 | 1749 | |
cd48e23f RP |
1750 | exec_params.open_files = s->open_files; |
1751 | ||
07ff03d6 MY |
1752 | exec_params.flags |= EXEC_PASS_FDS; |
1753 | ||
3543456f | 1754 | log_unit_debug(UNIT(s), "Passing %zu fds to service", exec_params.n_socket_fds + exec_params.n_storage_fds + exec_params.n_extra_fds); |
4f2d528d | 1755 | } |
44d8db9e | 1756 | |
12001b1b | 1757 | if (!FLAGS_SET(exec_params.flags, EXEC_IS_CONTROL) && s->type == SERVICE_EXEC) { |
c3f8a065 | 1758 | r = service_allocate_exec_fd(s, &exec_fd_source, &exec_params.exec_fd); |
5686391b LP |
1759 | if (r < 0) |
1760 | return r; | |
1761 | } | |
1762 | ||
e5d6dcce | 1763 | r = service_arm_timer(s, /* relative= */ true, timeout); |
36c16a7c LP |
1764 | if (r < 0) |
1765 | return r; | |
034c6ed7 | 1766 | |
bfb1f9e2 | 1767 | our_env = new0(char*, 16); |
36c16a7c LP |
1768 | if (!our_env) |
1769 | return -ENOMEM; | |
c952c6ec | 1770 | |
81006ebb | 1771 | if (service_exec_needs_notify_socket(s, exec_params.flags)) { |
3bdc25a4 | 1772 | exec_params.notify_socket = UNIT(s)->manager->notify_socket; |
75b29fda LP |
1773 | |
1774 | if (s->n_fd_store_max > 0) | |
1775 | if (asprintf(our_env + n_env++, "FDSTORE=%u", s->n_fd_store_max) < 0) | |
1776 | return -ENOMEM; | |
3bdc25a4 LP |
1777 | } |
1778 | ||
7933e971 | 1779 | if (pidref_is_set(&s->main_pid)) { |
c79ab77c | 1780 | if (asprintf(our_env + n_env++, "MAINPID="PID_FMT, s->main_pid.pid) < 0) |
36c16a7c | 1781 | return -ENOMEM; |
2105e76a | 1782 | |
7933e971 LP |
1783 | if (pidref_acquire_pidfd_id(&s->main_pid) >= 0) |
1784 | if (asprintf(our_env + n_env++, "MAINPIDFDID=%" PRIu64, s->main_pid.fd_id) < 0) | |
1785 | return -ENOMEM; | |
1786 | } | |
1787 | ||
1788 | if (MANAGER_IS_USER(UNIT(s)->manager)) { | |
df0ff127 | 1789 | if (asprintf(our_env + n_env++, "MANAGERPID="PID_FMT, getpid_cached()) < 0) |
36c16a7c | 1790 | return -ENOMEM; |
97ae63e2 | 1791 | |
7933e971 LP |
1792 | uint64_t pidfdid; |
1793 | if (pidfd_get_inode_id_self_cached(&pidfdid) >= 0) | |
1794 | if (asprintf(our_env + n_env++, "MANAGERPIDFDID=%" PRIu64, pidfdid) < 0) | |
1795 | return -ENOMEM; | |
1796 | } | |
1797 | ||
dcf3c3c3 LP |
1798 | if (s->pid_file) |
1799 | if (asprintf(our_env + n_env++, "PIDFILE=%s", s->pid_file) < 0) | |
1800 | return -ENOMEM; | |
1801 | ||
8dd4c05b | 1802 | if (s->socket_fd >= 0) { |
3b1c5241 SL |
1803 | union sockaddr_union sa; |
1804 | socklen_t salen = sizeof(sa); | |
1805 | ||
f56e7bfe LP |
1806 | /* If this is a per-connection service instance, let's set $REMOTE_ADDR and $REMOTE_PORT to something |
1807 | * useful. Note that we do this only when we are still connected at this point in time, which we might | |
1808 | * very well not be. Hence we ignore all errors when retrieving peer information (as that might result | |
1809 | * in ENOTCONN), and just use whate we can use. */ | |
f2dbd059 | 1810 | |
f56e7bfe | 1811 | if (getpeername(s->socket_fd, &sa.sa, &salen) >= 0 && |
608bfe76 | 1812 | IN_SET(sa.sa.sa_family, AF_INET, AF_INET6, AF_VSOCK, AF_UNIX)) { |
3b1c5241 SL |
1813 | _cleanup_free_ char *addr = NULL; |
1814 | char *t; | |
3b1c5241 | 1815 | |
608bfe76 | 1816 | r = sockaddr_pretty(&sa.sa, salen, /* translate_ipv6= */ true, /* include_port= */ false, &addr); |
3b1c5241 | 1817 | if (r < 0) |
36c16a7c | 1818 | return r; |
3b1c5241 | 1819 | |
608bfe76 KS |
1820 | if (sa.sa.sa_family != AF_UNIX || IN_SET(addr[0], '/', '@')) { |
1821 | t = strjoin("REMOTE_ADDR=", addr); | |
1822 | if (!t) | |
1823 | return -ENOMEM; | |
1824 | our_env[n_env++] = t; | |
1825 | } | |
3b1c5241 | 1826 | |
608bfe76 KS |
1827 | if (IN_SET(sa.sa.sa_family, AF_INET, AF_INET6, AF_VSOCK)) { |
1828 | unsigned port; | |
3b1c5241 | 1829 | |
608bfe76 KS |
1830 | r = sockaddr_port(&sa.sa, &port); |
1831 | if (r < 0) | |
1832 | return r; | |
1833 | ||
1834 | if (asprintf(&t, "REMOTE_PORT=%u", port) < 0) | |
1835 | return -ENOMEM; | |
1836 | our_env[n_env++] = t; | |
1837 | } | |
3b1c5241 | 1838 | } |
bfb1f9e2 LP |
1839 | |
1840 | uint64_t cookie; | |
1841 | if (socket_get_cookie(s->socket_fd, &cookie) >= 0) { | |
1842 | char *t; | |
1843 | if (asprintf(&t, "SO_COOKIE=%" PRIu64, cookie) < 0) | |
1844 | return -ENOMEM; | |
1845 | our_env[n_env++] = t; | |
1846 | } | |
3b1c5241 SL |
1847 | } |
1848 | ||
95c81c55 LB |
1849 | Service *env_source = NULL; |
1850 | const char *monitor_prefix; | |
81006ebb | 1851 | if (FLAGS_SET(exec_params.flags, EXEC_SETENV_RESULT)) { |
95c81c55 LB |
1852 | env_source = s; |
1853 | monitor_prefix = ""; | |
81006ebb | 1854 | } else if (FLAGS_SET(exec_params.flags, EXEC_SETENV_MONITOR_RESULT)) { |
95c81c55 LB |
1855 | env_source = service_get_triggering_service(s); |
1856 | monitor_prefix = "MONITOR_"; | |
1857 | } | |
1858 | ||
1859 | if (env_source) { | |
1860 | if (asprintf(our_env + n_env++, "%sSERVICE_RESULT=%s", monitor_prefix, service_result_to_string(env_source->result)) < 0) | |
136dc4c4 LP |
1861 | return -ENOMEM; |
1862 | ||
95c81c55 LB |
1863 | if (env_source->main_exec_status.pid > 0 && |
1864 | dual_timestamp_is_set(&env_source->main_exec_status.exit_timestamp)) { | |
1865 | if (asprintf(our_env + n_env++, "%sEXIT_CODE=%s", monitor_prefix, sigchld_code_to_string(env_source->main_exec_status.code)) < 0) | |
136dc4c4 LP |
1866 | return -ENOMEM; |
1867 | ||
95c81c55 LB |
1868 | if (env_source->main_exec_status.code == CLD_EXITED) |
1869 | r = asprintf(our_env + n_env++, "%sEXIT_STATUS=%i", monitor_prefix, env_source->main_exec_status.status); | |
136dc4c4 | 1870 | else |
95c81c55 | 1871 | r = asprintf(our_env + n_env++, "%sEXIT_STATUS=%s", monitor_prefix, signal_to_string(env_source->main_exec_status.status)); |
136dc4c4 LP |
1872 | if (r < 0) |
1873 | return -ENOMEM; | |
1874 | } | |
cdebedb4 | 1875 | |
95c81c55 | 1876 | if (env_source != s) { |
81006ebb MY |
1877 | if (!sd_id128_is_null(UNIT(env_source)->invocation_id)) |
1878 | if (asprintf(our_env + n_env++, "%sINVOCATION_ID=" SD_ID128_FORMAT_STR, | |
1879 | monitor_prefix, SD_ID128_FORMAT_VAL(UNIT(env_source)->invocation_id)) < 0) | |
95c81c55 | 1880 | return -ENOMEM; |
95c81c55 LB |
1881 | |
1882 | if (asprintf(our_env + n_env++, "%sUNIT=%s", monitor_prefix, UNIT(env_source)->id) < 0) | |
1883 | return -ENOMEM; | |
cdebedb4 | 1884 | } |
136dc4c4 LP |
1885 | } |
1886 | ||
fbb159b7 | 1887 | if (UNIT(s)->debug_invocation) { |
7d8bbfbe LB |
1888 | char *t = strdup("DEBUG_INVOCATION=1"); |
1889 | if (!t) | |
1890 | return -ENOMEM; | |
1891 | our_env[n_env++] = t; | |
1892 | } | |
1893 | ||
48b92b37 LB |
1894 | if (UNIT(s)->activation_details) { |
1895 | r = activation_details_append_env(UNIT(s)->activation_details, &our_env); | |
1896 | if (r < 0) | |
1897 | return r; | |
1898 | /* The number of env vars added here can vary, rather than keeping the allocation block in | |
1899 | * sync manually, these functions simply use the strv methods to append to it, so we need | |
1900 | * to update n_env when we are done in case of future usage. */ | |
1901 | n_env += r; | |
1902 | } | |
1903 | ||
1ad6e8b3 LP |
1904 | r = unit_set_exec_params(UNIT(s), &exec_params); |
1905 | if (r < 0) | |
1906 | return r; | |
3536f49e | 1907 | |
4ab3d29f | 1908 | final_env = strv_env_merge(exec_params.environment, our_env); |
36c16a7c LP |
1909 | if (!final_env) |
1910 | return -ENOMEM; | |
c952c6ec | 1911 | |
ac647978 | 1912 | /* System D-Bus needs nss-systemd disabled, so that we don't deadlock */ |
de90700f | 1913 | SET_FLAG(exec_params.flags, EXEC_NSS_DYNAMIC_BYPASS, |
ac647978 | 1914 | MANAGER_IS_SYSTEM(UNIT(s)->manager) && unit_has_name(UNIT(s), SPECIAL_DBUS_SERVICE)); |
4ad49000 | 1915 | |
b9c04eaf | 1916 | strv_free_and_replace(exec_params.environment, final_env); |
34b3f625 | 1917 | exec_params.watchdog_usec = service_get_watchdog_usec(s); |
a34ceba6 | 1918 | exec_params.selinux_context_net = s->socket_fd_selinux_context_net; |
9fa95f85 DM |
1919 | if (s->type == SERVICE_IDLE) |
1920 | exec_params.idle_pipe = UNIT(s)->manager->idle_pipe; | |
a34ceba6 LP |
1921 | exec_params.stdin_fd = s->stdin_fd; |
1922 | exec_params.stdout_fd = s->stdout_fd; | |
1923 | exec_params.stderr_fd = s->stderr_fd; | |
9fa95f85 | 1924 | |
f2341e0a LP |
1925 | r = exec_spawn(UNIT(s), |
1926 | c, | |
9e2f7c11 | 1927 | &s->exec_context, |
9fa95f85 | 1928 | &exec_params, |
613b411c | 1929 | s->exec_runtime, |
6bb00842 | 1930 | &s->cgroup_context, |
556d2bc4 | 1931 | &pidref); |
9e2f7c11 | 1932 | if (r < 0) |
36c16a7c | 1933 | return r; |
034c6ed7 | 1934 | |
5686391b LP |
1935 | s->exec_fd_event_source = TAKE_PTR(exec_fd_source); |
1936 | s->exec_fd_hot = false; | |
1937 | ||
495e75ed | 1938 | r = unit_watch_pidref(UNIT(s), &pidref, /* exclusive= */ true); |
c79ab77c LP |
1939 | if (r < 0) |
1940 | return r; | |
034c6ed7 | 1941 | |
c79ab77c | 1942 | *ret_pid = TAKE_PIDREF(pidref); |
5cb5a6ff | 1943 | return 0; |
034c6ed7 LP |
1944 | } |
1945 | ||
80876c20 LP |
1946 | static int main_pid_good(Service *s) { |
1947 | assert(s); | |
1948 | ||
51894d70 | 1949 | /* Returns 0 if the pid is dead, > 0 if it is good, < 0 if we don't know */ |
80876c20 | 1950 | |
c79ab77c | 1951 | /* If we know the pid file, then let's just check if it is still valid */ |
6dfa5494 LP |
1952 | if (s->main_pid_known) { |
1953 | ||
c79ab77c LP |
1954 | /* If it's an alien child let's check if it is still alive ... */ |
1955 | if (s->main_pid_alien && pidref_is_set(&s->main_pid)) | |
becdfcb9 | 1956 | return pidref_is_alive(&s->main_pid); |
6dfa5494 | 1957 | |
c79ab77c LP |
1958 | /* .. otherwise assume we'll get a SIGCHLD for it, which we really should wait for to collect |
1959 | * exit status and code */ | |
1960 | return pidref_is_set(&s->main_pid); | |
6dfa5494 | 1961 | } |
80876c20 LP |
1962 | |
1963 | /* We don't know the pid */ | |
1964 | return -EAGAIN; | |
1965 | } | |
1966 | ||
019be286 | 1967 | static int control_pid_good(Service *s) { |
80876c20 LP |
1968 | assert(s); |
1969 | ||
07697d7e LP |
1970 | /* Returns 0 if the control PID is dead, > 0 if it is good. We never actually return < 0 here, but in order to |
1971 | * make this function as similar as possible to main_pid_good() and cgroup_good(), we pretend that < 0 also | |
1972 | * means: we can't figure it out. */ | |
1973 | ||
c79ab77c | 1974 | return pidref_is_set(&s->control_pid); |
80876c20 LP |
1975 | } |
1976 | ||
cb0e818f HC |
1977 | static int cgroup_good(Service *s) { |
1978 | int r; | |
1979 | ||
abaf5edd ZJS |
1980 | assert(s); |
1981 | ||
cb0e818f HC |
1982 | /* Returns 0 if the cgroup is empty or doesn't exist, > 0 if it is exists and is populated, < 0 if we can't |
1983 | * figure it out */ | |
1984 | ||
9cc54544 | 1985 | if (!s->cgroup_runtime || !s->cgroup_runtime->cgroup_path) |
abaf5edd ZJS |
1986 | return 0; |
1987 | ||
c3f90077 | 1988 | r = cg_is_empty(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_runtime->cgroup_path); |
117dcc57 | 1989 | if (r < 0) |
80876c20 LP |
1990 | return r; |
1991 | ||
b13ddbbc | 1992 | return r == 0; |
80876c20 LP |
1993 | } |
1994 | ||
ebc57b89 | 1995 | static bool service_shall_restart(Service *s, const char **reason) { |
a509f0e6 | 1996 | assert(s); |
189a08e8 | 1997 | assert(reason); |
a509f0e6 LP |
1998 | |
1999 | /* Don't restart after manual stops */ | |
ebc57b89 ZJS |
2000 | if (s->forbid_restart) { |
2001 | *reason = "manual stop"; | |
a509f0e6 | 2002 | return false; |
ebc57b89 | 2003 | } |
a509f0e6 LP |
2004 | |
2005 | /* Never restart if this is configured as special exception */ | |
ebc57b89 ZJS |
2006 | if (exit_status_set_test(&s->restart_prevent_status, s->main_exec_status.code, s->main_exec_status.status)) { |
2007 | *reason = "prevented by exit status"; | |
a509f0e6 | 2008 | return false; |
ebc57b89 | 2009 | } |
a509f0e6 LP |
2010 | |
2011 | /* Restart if the exit code/status are configured as restart triggers */ | |
ebc57b89 | 2012 | if (exit_status_set_test(&s->restart_force_status, s->main_exec_status.code, s->main_exec_status.status)) { |
189a08e8 MY |
2013 | /* Don't allow Type=oneshot services to restart on success. Note that Restart=always/on-success |
2014 | * is already rejected in service_verify. */ | |
2015 | if (s->type == SERVICE_ONESHOT && s->result == SERVICE_SUCCESS) { | |
2016 | *reason = "service type and exit status"; | |
2017 | return false; | |
2018 | } | |
2019 | ||
ebc57b89 | 2020 | *reason = "forced by exit status"; |
a509f0e6 | 2021 | return true; |
ebc57b89 | 2022 | } |
a509f0e6 | 2023 | |
ebc57b89 | 2024 | *reason = "restart setting"; |
a509f0e6 LP |
2025 | switch (s->restart) { |
2026 | ||
2027 | case SERVICE_RESTART_NO: | |
2028 | return false; | |
2029 | ||
2030 | case SERVICE_RESTART_ALWAYS: | |
abb99360 | 2031 | return s->result != SERVICE_SKIP_CONDITION; |
a509f0e6 LP |
2032 | |
2033 | case SERVICE_RESTART_ON_SUCCESS: | |
2034 | return s->result == SERVICE_SUCCESS; | |
2035 | ||
2036 | case SERVICE_RESTART_ON_FAILURE: | |
bb924478 | 2037 | return !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_SKIP_CONDITION); |
a509f0e6 LP |
2038 | |
2039 | case SERVICE_RESTART_ON_ABNORMAL: | |
bb924478 | 2040 | return !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_FAILURE_EXIT_CODE, SERVICE_SKIP_CONDITION); |
a509f0e6 LP |
2041 | |
2042 | case SERVICE_RESTART_ON_WATCHDOG: | |
2043 | return s->result == SERVICE_FAILURE_WATCHDOG; | |
2044 | ||
2045 | case SERVICE_RESTART_ON_ABORT: | |
2046 | return IN_SET(s->result, SERVICE_FAILURE_SIGNAL, SERVICE_FAILURE_CORE_DUMP); | |
2047 | ||
2048 | default: | |
04499a70 | 2049 | assert_not_reached(); |
a509f0e6 LP |
2050 | } |
2051 | } | |
2052 | ||
deb4e708 MK |
2053 | static bool service_will_restart(Unit *u) { |
2054 | Service *s = SERVICE(u); | |
2055 | ||
53f47dfc YW |
2056 | assert(s); |
2057 | ||
09d04ad3 | 2058 | if (IN_SET(s->state, SERVICE_DEAD_BEFORE_AUTO_RESTART, SERVICE_FAILED_BEFORE_AUTO_RESTART, SERVICE_AUTO_RESTART, SERVICE_AUTO_RESTART_QUEUED)) |
53f47dfc | 2059 | return true; |
2ad2e41a | 2060 | |
52a12341 | 2061 | return unit_will_restart_default(u); |
53f47dfc YW |
2062 | } |
2063 | ||
b9c1883a LP |
2064 | static ServiceState service_determine_dead_state(Service *s) { |
2065 | assert(s); | |
2066 | ||
2067 | return s->fd_store && s->fd_store_preserve_mode == EXEC_PRESERVE_YES ? SERVICE_DEAD_RESOURCES_PINNED : SERVICE_DEAD; | |
2068 | } | |
2069 | ||
f42806df | 2070 | static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) { |
a1d31573 | 2071 | ServiceState end_state, restart_state; |
034c6ed7 | 2072 | int r; |
0f52f8e5 | 2073 | |
034c6ed7 LP |
2074 | assert(s); |
2075 | ||
0f52f8e5 LP |
2076 | /* If there's a stop job queued before we enter the DEAD state, we shouldn't act on Restart=, in order to not |
2077 | * undo what has already been enqueued. */ | |
2078 | if (unit_stop_pending(UNIT(s))) | |
2079 | allow_restart = false; | |
2080 | ||
a0fef983 | 2081 | if (s->result == SERVICE_SUCCESS) |
f42806df | 2082 | s->result = f; |
034c6ed7 | 2083 | |
31cd5f63 AZ |
2084 | if (s->result == SERVICE_SUCCESS) { |
2085 | unit_log_success(UNIT(s)); | |
b9c1883a | 2086 | end_state = service_determine_dead_state(s); |
a1d31573 | 2087 | restart_state = SERVICE_DEAD_BEFORE_AUTO_RESTART; |
31cd5f63 AZ |
2088 | } else if (s->result == SERVICE_SKIP_CONDITION) { |
2089 | unit_log_skip(UNIT(s), service_result_to_string(s->result)); | |
b9c1883a | 2090 | end_state = service_determine_dead_state(s); |
8959e17d | 2091 | restart_state = _SERVICE_STATE_INVALID; /* Never restart if skipped due to condition failure */ |
31cd5f63 AZ |
2092 | } else { |
2093 | unit_log_failure(UNIT(s), service_result_to_string(s->result)); | |
2094 | end_state = SERVICE_FAILED; | |
a1d31573 | 2095 | restart_state = SERVICE_FAILED_BEFORE_AUTO_RESTART; |
31cd5f63 | 2096 | } |
95e631da | 2097 | unit_warn_leftover_processes(UNIT(s), /* start = */ false); |
ed77d407 | 2098 | |
ebc57b89 ZJS |
2099 | if (!allow_restart) |
2100 | log_unit_debug(UNIT(s), "Service restart not allowed."); | |
2101 | else { | |
2102 | const char *reason; | |
ebc57b89 | 2103 | |
49b34f75 | 2104 | allow_restart = service_shall_restart(s, &reason); |
ebc57b89 | 2105 | log_unit_debug(UNIT(s), "Service will %srestart (%s)", |
49b34f75 | 2106 | allow_restart ? "" : "not ", |
ebc57b89 | 2107 | reason); |
ebc57b89 | 2108 | } |
deb4e708 | 2109 | |
49b34f75 | 2110 | if (allow_restart) { |
fe3d33c1 MY |
2111 | usec_t restart_usec_next; |
2112 | ||
8959e17d MY |
2113 | assert(restart_state >= 0 && restart_state < _SERVICE_STATE_MAX); |
2114 | ||
a1d31573 | 2115 | /* We make two state changes here: one that maps to the high-level UNIT_INACTIVE/UNIT_FAILED |
c309b9e9 | 2116 | * state (i.e. a state indicating deactivation), and then one that maps to the |
a1d31573 LP |
2117 | * high-level UNIT_STARTING state (i.e. a state indicating activation). We do this so that |
2118 | * external software can watch the state changes and see all service failures, even if they | |
2119 | * are only transitionary and followed by an automatic restart. We have fine-grained | |
2120 | * low-level states for this though so that software can distinguish the permanent UNIT_INACTIVE | |
2121 | * state from this transitionary UNIT_INACTIVE state by looking at the low-level states. */ | |
e568fea9 RP |
2122 | if (s->restart_mode != SERVICE_RESTART_MODE_DIRECT) |
2123 | service_set_state(s, restart_state); | |
a1d31573 | 2124 | |
fe3d33c1 MY |
2125 | restart_usec_next = service_restart_usec_next(s); |
2126 | ||
2127 | r = service_arm_timer(s, /* relative= */ true, restart_usec_next); | |
10691b9e LP |
2128 | if (r < 0) { |
2129 | log_unit_warning_errno(UNIT(s), r, "Failed to install restart timer: %m"); | |
d9b8d86b | 2130 | return service_enter_dead(s, SERVICE_FAILURE_RESOURCES, /* allow_restart= */ false); |
10691b9e | 2131 | } |
034c6ed7 | 2132 | |
7d8bbfbe LB |
2133 | /* If the relevant option is set, and the unit doesn't already have logging level set to |
2134 | * debug, enable it now. Make sure to overwrite the state in /run/systemd/units/ too, to | |
2135 | * ensure journald doesn't prune the messages. The previous state is saved and restored | |
2136 | * once the auto-restart flow ends. */ | |
7f2a7ccf MY |
2137 | if (s->restart_mode == SERVICE_RESTART_MODE_DEBUG) { |
2138 | r = unit_set_debug_invocation(UNIT(s), true); | |
2139 | if (r < 0) | |
2140 | log_unit_warning_errno(UNIT(s), r, "Failed to enable debug invocation, ignoring: %m"); | |
2141 | if (r > 0) | |
2142 | log_unit_notice(UNIT(s), "Service dead, subsequent restarts will be executed with debug level logging."); | |
2143 | } | |
2144 | ||
2145 | log_unit_debug(UNIT(s), "Next restart interval calculated as: %s", FORMAT_TIMESPAN(restart_usec_next, 0)); | |
7d8bbfbe | 2146 | |
034c6ed7 | 2147 | service_set_state(s, SERVICE_AUTO_RESTART); |
7d8bbfbe | 2148 | } else { |
ce31dbf4 MY |
2149 | /* If we shan't restart, the restart counter would be flushed out. But rather than doing that |
2150 | * immediately here, this is delegated to service_start(), i.e. next start, so that the user | |
2151 | * can still introspect the counter. */ | |
a1d31573 LP |
2152 | service_set_state(s, end_state); |
2153 | ||
7f2a7ccf | 2154 | (void) unit_set_debug_invocation(UNIT(s), false); |
7d8bbfbe LB |
2155 | } |
2156 | ||
f2341e0a | 2157 | /* The next restart might not be a manual stop, hence reset the flag indicating manual stops */ |
47342320 LP |
2158 | s->forbid_restart = false; |
2159 | ||
19dff691 MY |
2160 | /* Reset NotifyAccess override */ |
2161 | s->notify_access_override = _NOTIFY_ACCESS_INVALID; | |
2162 | ||
9c0c6701 | 2163 | /* We want fresh tmpdirs and ephemeral snapshots in case the service is started again immediately. */ |
28135da3 | 2164 | s->exec_runtime = exec_runtime_destroy(s->exec_runtime); |
c17ec25e | 2165 | |
95939aed | 2166 | /* Also, remove the runtime directory */ |
703b1b7f | 2167 | unit_destroy_runtime_data(UNIT(s), &s->exec_context, /* destroy_runtime_dir = */ true); |
e66cf1a3 | 2168 | |
b9c1883a LP |
2169 | /* Also get rid of the fd store, if that's configured. */ |
2170 | if (s->fd_store_preserve_mode == EXEC_PRESERVE_NO) | |
2171 | service_release_fd_store(s); | |
2172 | ||
00d9ef85 LP |
2173 | /* Get rid of the IPC bits of the user */ |
2174 | unit_unref_uid_gid(UNIT(s), true); | |
2175 | ||
9285c9ff LN |
2176 | /* Try to delete the pid file. At this point it will be |
2177 | * out-of-date, and some software might be confused by it, so | |
2178 | * let's remove it. */ | |
2179 | if (s->pid_file) | |
fabab190 | 2180 | (void) unlink(s->pid_file); |
9285c9ff | 2181 | |
6f765baf | 2182 | /* Reset TTY ownership if necessary */ |
bbdad5c0 | 2183 | exec_context_revert_tty(&s->exec_context, UNIT(s)->invocation_id); |
034c6ed7 LP |
2184 | } |
2185 | ||
f42806df | 2186 | static void service_enter_stop_post(Service *s, ServiceResult f) { |
034c6ed7 LP |
2187 | int r; |
2188 | assert(s); | |
2189 | ||
a0fef983 | 2190 | if (s->result == SERVICE_SUCCESS) |
f42806df | 2191 | s->result = f; |
034c6ed7 | 2192 | |
5e94833f LP |
2193 | service_unwatch_control_pid(s); |
2194 | ||
117dcc57 ZJS |
2195 | s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST]; |
2196 | if (s->control_command) { | |
867b3b7d | 2197 | s->control_command_id = SERVICE_EXEC_STOP_POST; |
c79ab77c | 2198 | pidref_done(&s->control_pid); |
867b3b7d | 2199 | |
ecedd90f LP |
2200 | r = service_spawn(s, |
2201 | s->control_command, | |
cfbf7538 | 2202 | service_exec_flags(s->control_command_id, /* cred_flag = */ 0), |
21b2ce39 | 2203 | s->timeout_stop_usec, |
ecedd90f | 2204 | &s->control_pid); |
10691b9e LP |
2205 | if (r < 0) { |
2206 | log_unit_warning_errno(UNIT(s), r, "Failed to spawn 'stop-post' task: %m"); | |
2207 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES); | |
2208 | return; | |
2209 | } | |
034c6ed7 | 2210 | |
80876c20 LP |
2211 | service_set_state(s, SERVICE_STOP_POST); |
2212 | } else | |
ac84d1fb | 2213 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS); |
034c6ed7 LP |
2214 | } |
2215 | ||
a232ebcc | 2216 | static int state_to_kill_operation(Service *s, ServiceState state) { |
4940c0b0 LP |
2217 | switch (state) { |
2218 | ||
c87700a1 | 2219 | case SERVICE_STOP_WATCHDOG: |
bf760801 | 2220 | case SERVICE_FINAL_WATCHDOG: |
c87700a1 | 2221 | return KILL_WATCHDOG; |
4940c0b0 LP |
2222 | |
2223 | case SERVICE_STOP_SIGTERM: | |
a232ebcc ZJS |
2224 | if (unit_has_job_type(UNIT(s), JOB_RESTART)) |
2225 | return KILL_RESTART; | |
2226 | _fallthrough_; | |
2227 | ||
4940c0b0 LP |
2228 | case SERVICE_FINAL_SIGTERM: |
2229 | return KILL_TERMINATE; | |
2230 | ||
2231 | case SERVICE_STOP_SIGKILL: | |
2232 | case SERVICE_FINAL_SIGKILL: | |
2233 | return KILL_KILL; | |
2234 | ||
2235 | default: | |
2236 | return _KILL_OPERATION_INVALID; | |
2237 | } | |
2238 | } | |
2239 | ||
f42806df | 2240 | static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) { |
bf760801 | 2241 | int kill_operation, r; |
034c6ed7 LP |
2242 | |
2243 | assert(s); | |
2244 | ||
a0fef983 | 2245 | if (s->result == SERVICE_SUCCESS) |
f42806df | 2246 | s->result = f; |
034c6ed7 | 2247 | |
bf760801 | 2248 | kill_operation = state_to_kill_operation(s, state); |
b826e317 | 2249 | r = unit_kill_context(UNIT(s), kill_operation); |
10691b9e LP |
2250 | if (r < 0) { |
2251 | log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m"); | |
cd2086fe | 2252 | goto fail; |
10691b9e | 2253 | } |
034c6ed7 | 2254 | |
cd2086fe | 2255 | if (r > 0) { |
e5d6dcce LP |
2256 | r = service_arm_timer(s, /* relative= */ true, |
2257 | kill_operation == KILL_WATCHDOG ? service_timeout_abort_usec(s) : s->timeout_stop_usec); | |
10691b9e LP |
2258 | if (r < 0) { |
2259 | log_unit_warning_errno(UNIT(s), r, "Failed to install timer: %m"); | |
36c16a7c | 2260 | goto fail; |
10691b9e | 2261 | } |
d6ea93e3 | 2262 | |
80876c20 | 2263 | service_set_state(s, state); |
c87700a1 | 2264 | } else if (IN_SET(state, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM) && s->kill_context.send_sigkill) |
ac84d1fb | 2265 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_SUCCESS); |
c87700a1 | 2266 | else if (IN_SET(state, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL)) |
f42806df | 2267 | service_enter_stop_post(s, SERVICE_SUCCESS); |
bf760801 | 2268 | else if (IN_SET(state, SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM) && s->kill_context.send_sigkill) |
ac84d1fb | 2269 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_SUCCESS); |
80876c20 | 2270 | else |
10691b9e | 2271 | service_enter_dead(s, SERVICE_SUCCESS, /* allow_restart= */ true); |
034c6ed7 LP |
2272 | |
2273 | return; | |
2274 | ||
2275 | fail: | |
c87700a1 | 2276 | if (IN_SET(state, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL)) |
f42806df | 2277 | service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES); |
034c6ed7 | 2278 | else |
10691b9e | 2279 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, /* allow_restart= */ true); |
034c6ed7 LP |
2280 | } |
2281 | ||
308d72dc | 2282 | static void service_enter_stop_by_notify(Service *s) { |
3dde96d8 LP |
2283 | int r; |
2284 | ||
308d72dc LP |
2285 | assert(s); |
2286 | ||
3dde96d8 LP |
2287 | r = service_arm_timer(s, /* relative= */ true, s->timeout_stop_usec); |
2288 | if (r < 0) { | |
2289 | log_unit_warning_errno(UNIT(s), r, "Failed to install timer: %m"); | |
2290 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); | |
2291 | return; | |
2292 | } | |
308d72dc | 2293 | |
6041a7ee MS |
2294 | /* The service told us it's stopping, so it's as if we SIGTERM'd it. */ |
2295 | service_set_state(s, SERVICE_STOP_SIGTERM); | |
308d72dc LP |
2296 | } |
2297 | ||
f42806df | 2298 | static void service_enter_stop(Service *s, ServiceResult f) { |
034c6ed7 | 2299 | int r; |
5925dd3c | 2300 | |
034c6ed7 LP |
2301 | assert(s); |
2302 | ||
a0fef983 | 2303 | if (s->result == SERVICE_SUCCESS) |
f42806df | 2304 | s->result = f; |
034c6ed7 | 2305 | |
5e94833f LP |
2306 | service_unwatch_control_pid(s); |
2307 | ||
117dcc57 ZJS |
2308 | s->control_command = s->exec_command[SERVICE_EXEC_STOP]; |
2309 | if (s->control_command) { | |
867b3b7d | 2310 | s->control_command_id = SERVICE_EXEC_STOP; |
c79ab77c | 2311 | pidref_done(&s->control_pid); |
867b3b7d | 2312 | |
ecedd90f LP |
2313 | r = service_spawn(s, |
2314 | s->control_command, | |
cfbf7538 | 2315 | service_exec_flags(s->control_command_id, /* cred_flag = */ 0), |
21b2ce39 | 2316 | s->timeout_stop_usec, |
ecedd90f | 2317 | &s->control_pid); |
10691b9e LP |
2318 | if (r < 0) { |
2319 | log_unit_warning_errno(UNIT(s), r, "Failed to spawn 'stop' task: %m"); | |
2320 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); | |
2321 | return; | |
2322 | } | |
034c6ed7 | 2323 | |
80876c20 LP |
2324 | service_set_state(s, SERVICE_STOP); |
2325 | } else | |
f42806df | 2326 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS); |
034c6ed7 LP |
2327 | } |
2328 | ||
957c3cf9 LP |
2329 | static bool service_good(Service *s) { |
2330 | int main_pid_ok; | |
e9fa1bf7 | 2331 | |
957c3cf9 LP |
2332 | assert(s); |
2333 | ||
2334 | if (s->type == SERVICE_DBUS && !s->bus_name_good) | |
2335 | return false; | |
2336 | ||
2337 | main_pid_ok = main_pid_good(s); | |
2338 | if (main_pid_ok > 0) /* It's alive */ | |
2339 | return true; | |
ef430065 | 2340 | if (main_pid_ok == 0 && s->exit_type == SERVICE_EXIT_MAIN) /* It's dead */ |
957c3cf9 LP |
2341 | return false; |
2342 | ||
2343 | /* OK, we don't know anything about the main PID, maybe | |
2344 | * because there is none. Let's check the control group | |
2345 | * instead. */ | |
2346 | ||
2347 | return cgroup_good(s) != 0; | |
2348 | } | |
2349 | ||
f42806df | 2350 | static void service_enter_running(Service *s, ServiceResult f) { |
3dde96d8 LP |
2351 | int r; |
2352 | ||
80876c20 LP |
2353 | assert(s); |
2354 | ||
a0fef983 | 2355 | if (s->result == SERVICE_SUCCESS) |
f42806df | 2356 | s->result = f; |
80876c20 | 2357 | |
089b64d5 LP |
2358 | service_unwatch_control_pid(s); |
2359 | ||
ec5b1452 LP |
2360 | if (s->result != SERVICE_SUCCESS) |
2361 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); | |
2362 | else if (service_good(s)) { | |
308d72dc | 2363 | |
ec5b1452 | 2364 | /* If there are any queued up sd_notify() notifications, process them now */ |
308d72dc LP |
2365 | if (s->notify_state == NOTIFY_RELOADING) |
2366 | service_enter_reload_by_notify(s); | |
2367 | else if (s->notify_state == NOTIFY_STOPPING) | |
2368 | service_enter_stop_by_notify(s); | |
36c16a7c | 2369 | else { |
308d72dc | 2370 | service_set_state(s, SERVICE_RUNNING); |
3dde96d8 LP |
2371 | |
2372 | r = service_arm_timer(s, /* relative= */ false, service_running_timeout(s)); | |
2373 | if (r < 0) { | |
2374 | log_unit_warning_errno(UNIT(s), r, "Failed to install timer: %m"); | |
2375 | service_enter_running(s, SERVICE_FAILURE_RESOURCES); | |
2376 | return; | |
2377 | } | |
36c16a7c | 2378 | } |
308d72dc | 2379 | |
ec5b1452 | 2380 | } else if (s->remain_after_exit) |
80876c20 LP |
2381 | service_set_state(s, SERVICE_EXITED); |
2382 | else | |
f42806df | 2383 | service_enter_stop(s, SERVICE_SUCCESS); |
80876c20 LP |
2384 | } |
2385 | ||
034c6ed7 LP |
2386 | static void service_enter_start_post(Service *s) { |
2387 | int r; | |
e9fa1bf7 | 2388 | |
034c6ed7 LP |
2389 | assert(s); |
2390 | ||
5e94833f | 2391 | service_unwatch_control_pid(s); |
842129f5 | 2392 | service_reset_watchdog(s); |
bb242b7b | 2393 | |
117dcc57 ZJS |
2394 | s->control_command = s->exec_command[SERVICE_EXEC_START_POST]; |
2395 | if (s->control_command) { | |
867b3b7d | 2396 | s->control_command_id = SERVICE_EXEC_START_POST; |
c79ab77c | 2397 | pidref_done(&s->control_pid); |
867b3b7d | 2398 | |
ecedd90f LP |
2399 | r = service_spawn(s, |
2400 | s->control_command, | |
cfbf7538 | 2401 | service_exec_flags(s->control_command_id, /* cred_flag = */ 0), |
21b2ce39 | 2402 | s->timeout_start_usec, |
ecedd90f | 2403 | &s->control_pid); |
10691b9e LP |
2404 | if (r < 0) { |
2405 | log_unit_warning_errno(UNIT(s), r, "Failed to spawn 'start-post' task: %m"); | |
2406 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); | |
2407 | return; | |
2408 | } | |
034c6ed7 | 2409 | |
80876c20 LP |
2410 | service_set_state(s, SERVICE_START_POST); |
2411 | } else | |
f42806df | 2412 | service_enter_running(s, SERVICE_SUCCESS); |
034c6ed7 LP |
2413 | } |
2414 | ||
e9a4f676 | 2415 | static void service_kill_control_process(Service *s) { |
a6951a50 | 2416 | int r; |
4ad49000 | 2417 | |
a6951a50 LP |
2418 | assert(s); |
2419 | ||
c79ab77c | 2420 | if (!pidref_is_set(&s->control_pid)) |
e9a4f676 | 2421 | return; |
4ad49000 | 2422 | |
c79ab77c | 2423 | r = pidref_kill_and_sigcont(&s->control_pid, SIGKILL); |
e9a4f676 LP |
2424 | if (r < 0) { |
2425 | _cleanup_free_ char *comm = NULL; | |
a6951a50 | 2426 | |
d7d74854 | 2427 | (void) pidref_get_comm(&s->control_pid, &comm); |
a6951a50 | 2428 | |
e9a4f676 | 2429 | log_unit_debug_errno(UNIT(s), r, "Failed to kill control process " PID_FMT " (%s), ignoring: %m", |
c79ab77c | 2430 | s->control_pid.pid, strna(comm)); |
a6951a50 | 2431 | } |
4ad49000 LP |
2432 | } |
2433 | ||
c53d2d54 DB |
2434 | static int service_adverse_to_leftover_processes(Service *s) { |
2435 | assert(s); | |
2436 | ||
2437 | /* KillMode=mixed and control group are used to indicate that all process should be killed off. | |
4c425434 LP |
2438 | * SendSIGKILL= is used for services that require a clean shutdown. These are typically database |
2439 | * service where a SigKilled process would result in a lengthy recovery and who's shutdown or startup | |
2440 | * time is quite variable (so Timeout settings aren't of use). | |
c53d2d54 DB |
2441 | * |
2442 | * Here we take these two factors and refuse to start a service if there are existing processes | |
2443 | * within a control group. Databases, while generally having some protection against multiple | |
4c425434 | 2444 | * instances running, lets not stress the rigor of these. Also ExecStartPre= parts of the service |
38747451 | 2445 | * aren't as rigoriously written to protect against multiple use. */ |
4c425434 | 2446 | |
95e631da | 2447 | if (unit_warn_leftover_processes(UNIT(s), /* start = */ true) > 0 && |
c53d2d54 | 2448 | IN_SET(s->kill_context.kill_mode, KILL_MIXED, KILL_CONTROL_GROUP) && |
569554d9 | 2449 | !s->kill_context.send_sigkill) |
95e631da MY |
2450 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(EBUSY), |
2451 | "Will not start SendSIGKILL=no service of type KillMode=control-group or mixed while processes exist"); | |
569554d9 | 2452 | |
c53d2d54 DB |
2453 | return 0; |
2454 | } | |
2455 | ||
034c6ed7 | 2456 | static void service_enter_start(Service *s) { |
c79ab77c | 2457 | _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
4ad49000 | 2458 | ExecCommand *c; |
36c16a7c | 2459 | usec_t timeout; |
034c6ed7 LP |
2460 | int r; |
2461 | ||
2462 | assert(s); | |
2463 | ||
41efeaec LP |
2464 | service_unwatch_control_pid(s); |
2465 | service_unwatch_main_pid(s); | |
80876c20 | 2466 | |
c53d2d54 DB |
2467 | r = service_adverse_to_leftover_processes(s); |
2468 | if (r < 0) | |
2469 | goto fail; | |
a4634b21 | 2470 | |
867b3b7d LP |
2471 | if (s->type == SERVICE_FORKING) { |
2472 | s->control_command_id = SERVICE_EXEC_START; | |
2473 | c = s->control_command = s->exec_command[SERVICE_EXEC_START]; | |
2474 | ||
2475 | s->main_command = NULL; | |
2476 | } else { | |
2477 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
2478 | s->control_command = NULL; | |
2479 | ||
2480 | c = s->main_command = s->exec_command[SERVICE_EXEC_START]; | |
2481 | } | |
34e9ba66 | 2482 | |
96fb8242 | 2483 | if (!c) { |
47fffb35 | 2484 | if (s->type != SERVICE_ONESHOT) { |
cbc056c8 ZJS |
2485 | /* There's no command line configured for the main command? Hmm, that is strange. |
2486 | * This can only happen if the configuration changes at runtime. In this case, | |
2487 | * let's enter a failure state. */ | |
d85ff944 | 2488 | r = log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENXIO), "There's no 'start' task anymore we could start."); |
47fffb35 LP |
2489 | goto fail; |
2490 | } | |
2491 | ||
ef5ae8e7 ZJS |
2492 | /* We force a fake state transition here. Otherwise, the unit would go directly from |
2493 | * SERVICE_DEAD to SERVICE_DEAD without SERVICE_ACTIVATING or SERVICE_ACTIVE | |
5238e957 | 2494 | * in between. This way we can later trigger actions that depend on the state |
ef5ae8e7 ZJS |
2495 | * transition, including SuccessAction=. */ |
2496 | service_set_state(s, SERVICE_START); | |
2497 | ||
96fb8242 LP |
2498 | service_enter_start_post(s); |
2499 | return; | |
2500 | } | |
2501 | ||
36c16a7c LP |
2502 | if (IN_SET(s->type, SERVICE_SIMPLE, SERVICE_IDLE)) |
2503 | /* For simple + idle this is the main process. We don't apply any timeout here, but | |
2504 | * service_enter_running() will later apply the .runtime_max_usec timeout. */ | |
2505 | timeout = USEC_INFINITY; | |
2506 | else | |
2507 | timeout = s->timeout_start_usec; | |
2508 | ||
ecedd90f LP |
2509 | r = service_spawn(s, |
2510 | c, | |
cfbf7538 | 2511 | service_exec_flags(SERVICE_EXEC_START, EXEC_SETUP_CREDENTIALS_FRESH), |
36c16a7c | 2512 | timeout, |
c79ab77c | 2513 | &pidref); |
10691b9e LP |
2514 | if (r < 0) { |
2515 | log_unit_warning_errno(UNIT(s), r, "Failed to spawn 'start' task: %m"); | |
034c6ed7 | 2516 | goto fail; |
10691b9e | 2517 | } |
034c6ed7 | 2518 | |
026335e9 | 2519 | assert(pidref.pid == c->exec_status.pid); |
034c6ed7 | 2520 | |
026335e9 | 2521 | switch (s->type) { |
034c6ed7 | 2522 | |
026335e9 MY |
2523 | case SERVICE_SIMPLE: |
2524 | case SERVICE_IDLE: | |
2525 | /* For simple services we immediately start the START_POST binaries. */ | |
2526 | (void) service_set_main_pidref(s, TAKE_PIDREF(pidref), &c->exec_status.start_timestamp); | |
2527 | return service_enter_start_post(s); | |
034c6ed7 | 2528 | |
026335e9 MY |
2529 | case SERVICE_FORKING: |
2530 | /* For forking services we wait until the start process exited. */ | |
c79ab77c LP |
2531 | pidref_done(&s->control_pid); |
2532 | s->control_pid = TAKE_PIDREF(pidref); | |
026335e9 MY |
2533 | return service_set_state(s, SERVICE_START); |
2534 | ||
2535 | case SERVICE_ONESHOT: /* For oneshot services we wait until the start process exited, too, but it is our main process. */ | |
2536 | case SERVICE_EXEC: | |
2537 | case SERVICE_DBUS: | |
2538 | case SERVICE_NOTIFY: | |
2539 | case SERVICE_NOTIFY_RELOAD: | |
2540 | /* For D-Bus services we know the main pid right away, but wait for the bus name to appear | |
2541 | * on the bus. 'notify' and 'exec' services wait for readiness notification and EOF | |
2542 | * on exec_fd, respectively. */ | |
7e0e6b50 | 2543 | (void) service_set_main_pidref(s, TAKE_PIDREF(pidref), &c->exec_status.start_timestamp); |
026335e9 | 2544 | return service_set_state(s, SERVICE_START); |
034c6ed7 | 2545 | |
026335e9 | 2546 | default: |
ae1cb17c | 2547 | assert_not_reached(); |
026335e9 MY |
2548 | } |
2549 | ||
034c6ed7 | 2550 | fail: |
c3fda31d | 2551 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
034c6ed7 LP |
2552 | } |
2553 | ||
2554 | static void service_enter_start_pre(Service *s) { | |
2555 | int r; | |
2556 | ||
2557 | assert(s); | |
2558 | ||
5e94833f LP |
2559 | service_unwatch_control_pid(s); |
2560 | ||
117dcc57 ZJS |
2561 | s->control_command = s->exec_command[SERVICE_EXEC_START_PRE]; |
2562 | if (s->control_command) { | |
8f53a7b8 | 2563 | |
c53d2d54 DB |
2564 | r = service_adverse_to_leftover_processes(s); |
2565 | if (r < 0) | |
2566 | goto fail; | |
a4634b21 | 2567 | |
867b3b7d LP |
2568 | s->control_command_id = SERVICE_EXEC_START_PRE; |
2569 | ||
ecedd90f LP |
2570 | r = service_spawn(s, |
2571 | s->control_command, | |
cfbf7538 | 2572 | service_exec_flags(s->control_command_id, /* cred_flag = */ 0), |
21b2ce39 | 2573 | s->timeout_start_usec, |
ecedd90f | 2574 | &s->control_pid); |
10691b9e LP |
2575 | if (r < 0) { |
2576 | log_unit_warning_errno(UNIT(s), r, "Failed to spawn 'start-pre' task: %m"); | |
034c6ed7 | 2577 | goto fail; |
10691b9e | 2578 | } |
034c6ed7 | 2579 | |
80876c20 LP |
2580 | service_set_state(s, SERVICE_START_PRE); |
2581 | } else | |
034c6ed7 LP |
2582 | service_enter_start(s); |
2583 | ||
2584 | return; | |
2585 | ||
2586 | fail: | |
10691b9e | 2587 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, /* allow_restart= */ true); |
034c6ed7 LP |
2588 | } |
2589 | ||
31cd5f63 AZ |
2590 | static void service_enter_condition(Service *s) { |
2591 | int r; | |
2592 | ||
2593 | assert(s); | |
2594 | ||
2595 | service_unwatch_control_pid(s); | |
2596 | ||
2597 | s->control_command = s->exec_command[SERVICE_EXEC_CONDITION]; | |
2598 | if (s->control_command) { | |
2599 | ||
2600 | r = service_adverse_to_leftover_processes(s); | |
2601 | if (r < 0) | |
2602 | goto fail; | |
2603 | ||
2604 | s->control_command_id = SERVICE_EXEC_CONDITION; | |
c79ab77c | 2605 | pidref_done(&s->control_pid); |
31cd5f63 AZ |
2606 | |
2607 | r = service_spawn(s, | |
2608 | s->control_command, | |
cfbf7538 | 2609 | service_exec_flags(s->control_command_id, /* cred_flag = */ 0), |
31cd5f63 | 2610 | s->timeout_start_usec, |
31cd5f63 | 2611 | &s->control_pid); |
10691b9e LP |
2612 | if (r < 0) { |
2613 | log_unit_warning_errno(UNIT(s), r, "Failed to spawn 'exec-condition' task: %m"); | |
31cd5f63 | 2614 | goto fail; |
10691b9e | 2615 | } |
31cd5f63 AZ |
2616 | |
2617 | service_set_state(s, SERVICE_CONDITION); | |
2618 | } else | |
2619 | service_enter_start_pre(s); | |
2620 | ||
2621 | return; | |
2622 | ||
2623 | fail: | |
10691b9e | 2624 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, /* allow_restart= */ true); |
31cd5f63 AZ |
2625 | } |
2626 | ||
1391f149 | 2627 | static void service_enter_restart(Service *s, bool shortcut) { |
4afd3348 | 2628 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
034c6ed7 | 2629 | int r; |
398ef8ba | 2630 | |
1391f149 MY |
2631 | /* shortcut: a manual start request is received, restart immediately */ |
2632 | ||
034c6ed7 | 2633 | assert(s); |
1391f149 | 2634 | assert(s->state == SERVICE_AUTO_RESTART); |
034c6ed7 | 2635 | |
1391f149 | 2636 | if (!shortcut && unit_has_job_type(UNIT(s), JOB_STOP)) { |
a8bb2e65 | 2637 | /* Don't restart things if we are going down anyway */ |
c9728806 | 2638 | log_unit_info(UNIT(s), "Stop job pending for unit, skipping automatic restart."); |
feae8adb | 2639 | return; |
2edfa366 LP |
2640 | } |
2641 | ||
7a139370 MY |
2642 | /* Any units that are bound to this service must also be restarted, unless RestartMode=direct. |
2643 | * We use JOB_START for ourselves but then set JOB_RESTART_DEPENDENCIES which will enqueue JOB_RESTART | |
2644 | * for those dependency jobs in the former case, plain JOB_REPLACE when RestartMode=direct. | |
569269d0 | 2645 | * |
7a139370 | 2646 | * Also, when RestartMode=direct is used, the service being restarted don't enter the inactive/failed state, |
569269d0 MY |
2647 | * i.e. unit_process_job -> job_finish_and_invalidate is never called, and the previous job might still |
2648 | * be running (especially for Type=oneshot services). | |
2649 | * We need to refuse late merge and re-enqueue the anchor job. */ | |
2650 | r = manager_add_job_full(UNIT(s)->manager, | |
2651 | JOB_START, UNIT(s), | |
7a139370 | 2652 | s->restart_mode == SERVICE_RESTART_MODE_DIRECT ? JOB_REPLACE : JOB_RESTART_DEPENDENCIES, |
569269d0 MY |
2653 | TRANSACTION_REENQUEUE_ANCHOR, |
2654 | /* affected_jobs = */ NULL, | |
2655 | &error, /* ret = */ NULL); | |
10691b9e LP |
2656 | if (r < 0) { |
2657 | log_unit_warning(UNIT(s), "Failed to schedule restart job: %s", bus_error_message(&error, r)); | |
d993ad6c | 2658 | return service_enter_dead(s, SERVICE_FAILURE_RESOURCES, /* allow_restart= */ false); |
10691b9e | 2659 | } |
034c6ed7 | 2660 | |
0c59d2e4 LP |
2661 | /* Count the jobs we enqueue for restarting. This counter is maintained as long as the unit isn't |
2662 | * fully stopped, i.e. as long as it remains up or remains in auto-start states. The user can reset | |
2663 | * the counter explicitly however via the usual "systemctl reset-failure" logic. */ | |
b3a9d980 | 2664 | s->n_restarts++; |
7a0019d3 | 2665 | |
c2503e35 | 2666 | log_unit_struct(UNIT(s), LOG_INFO, |
3cf6a3a3 | 2667 | LOG_MESSAGE_ID(SD_MESSAGE_UNIT_RESTART_SCHEDULED_STR), |
c2503e35 | 2668 | LOG_UNIT_INVOCATION_ID(UNIT(s)), |
92663a5e | 2669 | LOG_UNIT_MESSAGE(UNIT(s), |
1391f149 MY |
2670 | "Scheduled restart job%s, restart counter is at %u.", |
2671 | shortcut ? " immediately on client request" : "", s->n_restarts), | |
3cf6a3a3 | 2672 | LOG_ITEM("N_RESTARTS=%u", s->n_restarts)); |
7a0019d3 | 2673 | |
09d04ad3 LP |
2674 | service_set_state(s, SERVICE_AUTO_RESTART_QUEUED); |
2675 | ||
7a0019d3 LP |
2676 | /* Notify clients about changed restart counter */ |
2677 | unit_add_to_dbus_queue(UNIT(s)); | |
034c6ed7 LP |
2678 | } |
2679 | ||
308d72dc | 2680 | static void service_enter_reload_by_notify(Service *s) { |
15d167f8 JW |
2681 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2682 | int r; | |
2683 | ||
308d72dc LP |
2684 | assert(s); |
2685 | ||
3dde96d8 LP |
2686 | r = service_arm_timer(s, /* relative= */ true, s->timeout_start_usec); |
2687 | if (r < 0) { | |
2688 | log_unit_warning_errno(UNIT(s), r, "Failed to install timer: %m"); | |
2689 | s->reload_result = SERVICE_FAILURE_RESOURCES; | |
2690 | service_enter_running(s, SERVICE_SUCCESS); | |
2691 | return; | |
2692 | } | |
2693 | ||
3bd28bf7 | 2694 | service_set_state(s, SERVICE_RELOAD_NOTIFY); |
15d167f8 JW |
2695 | |
2696 | /* service_enter_reload_by_notify is never called during a reload, thus no loops are possible. */ | |
2697 | r = manager_propagate_reload(UNIT(s)->manager, UNIT(s), JOB_FAIL, &error); | |
2698 | if (r < 0) | |
3dde96d8 | 2699 | log_unit_warning(UNIT(s), "Failed to schedule propagation of reload, ignoring: %s", bus_error_message(&error, r)); |
308d72dc LP |
2700 | } |
2701 | ||
dfdeb0b1 | 2702 | static void service_enter_reload_signal_exec(Service *s) { |
3bd28bf7 | 2703 | bool killed = false; |
034c6ed7 LP |
2704 | int r; |
2705 | ||
2706 | assert(s); | |
2707 | ||
5e94833f | 2708 | service_unwatch_control_pid(s); |
95c906ae | 2709 | s->reload_result = SERVICE_SUCCESS; |
5e94833f | 2710 | |
3bd28bf7 LP |
2711 | usec_t ts = now(CLOCK_MONOTONIC); |
2712 | ||
c79ab77c LP |
2713 | if (s->type == SERVICE_NOTIFY_RELOAD && pidref_is_set(&s->main_pid)) { |
2714 | r = pidref_kill_and_sigcont(&s->main_pid, s->reload_signal); | |
3bd28bf7 LP |
2715 | if (r < 0) { |
2716 | log_unit_warning_errno(UNIT(s), r, "Failed to send reload signal: %m"); | |
2717 | goto fail; | |
2718 | } | |
2719 | ||
2720 | killed = true; | |
2721 | } | |
2722 | ||
117dcc57 ZJS |
2723 | s->control_command = s->exec_command[SERVICE_EXEC_RELOAD]; |
2724 | if (s->control_command) { | |
867b3b7d | 2725 | s->control_command_id = SERVICE_EXEC_RELOAD; |
c79ab77c | 2726 | pidref_done(&s->control_pid); |
867b3b7d | 2727 | |
ecedd90f LP |
2728 | r = service_spawn(s, |
2729 | s->control_command, | |
cfbf7538 | 2730 | service_exec_flags(s->control_command_id, /* cred_flag = */ 0), |
21b2ce39 | 2731 | s->timeout_start_usec, |
ecedd90f | 2732 | &s->control_pid); |
3bd28bf7 | 2733 | if (r < 0) { |
10691b9e | 2734 | log_unit_warning_errno(UNIT(s), r, "Failed to spawn 'reload' task: %m"); |
034c6ed7 | 2735 | goto fail; |
3bd28bf7 | 2736 | } |
034c6ed7 | 2737 | |
80876c20 | 2738 | service_set_state(s, SERVICE_RELOAD); |
3bd28bf7 | 2739 | } else if (killed) { |
3dde96d8 LP |
2740 | r = service_arm_timer(s, /* relative= */ true, s->timeout_start_usec); |
2741 | if (r < 0) { | |
2742 | log_unit_warning_errno(UNIT(s), r, "Failed to install timer: %m"); | |
2743 | goto fail; | |
2744 | } | |
2745 | ||
3bd28bf7 LP |
2746 | service_set_state(s, SERVICE_RELOAD_SIGNAL); |
2747 | } else { | |
f42806df | 2748 | service_enter_running(s, SERVICE_SUCCESS); |
3bd28bf7 LP |
2749 | return; |
2750 | } | |
034c6ed7 | 2751 | |
3bd28bf7 LP |
2752 | /* Store the timestamp when we started reloading: when reloading via SIGHUP we won't leave the reload |
2753 | * state until we received both RELOADING=1 and READY=1 with MONOTONIC_USEC= set to a value above | |
2754 | * this. Thus we know for sure the reload cycle was executed *after* we requested it, and is not one | |
2755 | * that was already in progress before. */ | |
2756 | s->reload_begin_usec = ts; | |
034c6ed7 LP |
2757 | return; |
2758 | ||
2759 | fail: | |
f42806df LP |
2760 | s->reload_result = SERVICE_FAILURE_RESOURCES; |
2761 | service_enter_running(s, SERVICE_SUCCESS); | |
034c6ed7 LP |
2762 | } |
2763 | ||
dfdeb0b1 | 2764 | static bool service_should_reload_extensions(Service *s) { |
2765 | int r; | |
2766 | ||
2767 | assert(s); | |
2768 | ||
2769 | if (!pidref_is_set(&s->main_pid)) { | |
2770 | log_unit_debug(UNIT(s), "Not reloading extensions for service without main PID."); | |
2771 | return false; | |
2772 | } | |
2773 | ||
2774 | r = exec_context_has_vpicked_extensions(&s->exec_context); | |
2775 | if (r < 0) | |
2776 | log_unit_warning_errno(UNIT(s), r, "Failed to determine if service should reload extensions, assuming false: %m"); | |
2777 | if (r == 0) | |
2778 | log_unit_debug(UNIT(s), "Service has no extensions to reload."); | |
2779 | if (r <= 0) | |
2780 | return false; | |
2781 | ||
2782 | // TODO: Add support for user services, which can use ExtensionDirectories= + notify-reload. | |
2783 | // For now, skip for user services. | |
2784 | if (!MANAGER_IS_SYSTEM(UNIT(s)->manager)) { | |
2785 | log_once(LOG_WARNING, "Not reloading extensions for user services."); | |
2786 | return false; | |
2787 | } | |
2788 | ||
2789 | return true; | |
2790 | } | |
2791 | ||
2792 | static void service_enter_refresh_extensions(Service *s) { | |
2793 | _cleanup_(pidref_done) PidRef worker = PIDREF_NULL; | |
2794 | int r; | |
2795 | ||
2796 | assert(s); | |
2797 | ||
2798 | /* If we don't have extensions to reload, immediately go to the signal step */ | |
2799 | if (!service_should_reload_extensions(s)) | |
2800 | return (void) service_enter_reload_signal_exec(s); | |
2801 | ||
2802 | service_unwatch_control_pid(s); | |
2803 | s->reload_result = SERVICE_SUCCESS; | |
2804 | s->control_command = NULL; | |
2805 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
2806 | ||
2807 | /* Given we are running from PID1, avoid doing potentially heavy I/O operations like opening images | |
2808 | * directly, and instead fork a worker process. */ | |
2809 | r = unit_fork_helper_process(UNIT(s), "(sd-refresh-extensions)", /* into_cgroup= */ false, &worker); | |
2810 | if (r < 0) { | |
2811 | log_unit_error_errno(UNIT(s), r, "Failed to fork process to refresh extensions in unit's namespace: %m"); | |
2812 | goto fail; | |
2813 | } | |
2814 | if (r == 0) { | |
2815 | PidRef *unit_pid = &s->main_pid; | |
2816 | assert(pidref_is_set(unit_pid)); | |
2817 | ||
2818 | _cleanup_free_ char *propagate_dir = path_join("/run/systemd/propagate/", UNIT(s)->id); | |
2819 | if (!propagate_dir) { | |
2820 | log_unit_error_errno(UNIT(s), -ENOMEM, "Failed to allocate memory for propagate directory: %m"); | |
2821 | _exit(EXIT_FAILURE); | |
2822 | } | |
2823 | ||
2824 | NamespaceParameters p = { | |
2825 | .private_namespace_dir = "/run/systemd", | |
2826 | .incoming_dir = "/run/systemd/incoming", | |
2827 | .propagate_dir = propagate_dir, | |
2828 | .runtime_scope = UNIT(s)->manager->runtime_scope, | |
2829 | .extension_images = s->exec_context.extension_images, | |
2830 | .n_extension_images = s->exec_context.n_extension_images, | |
2831 | .extension_directories = s->exec_context.extension_directories, | |
2832 | .extension_image_policy = s->exec_context.extension_image_policy, | |
2833 | }; | |
2834 | ||
2835 | /* Only reload confext, and not sysext as they also typically contain the executable(s) used | |
2836 | * by the service and a simply reload cannot meaningfully handle that. */ | |
2837 | r = refresh_extensions_in_namespace( | |
2838 | unit_pid, | |
2839 | "SYSTEMD_CONFEXT_HIERARCHIES", | |
2840 | &p); | |
2841 | if (r < 0) | |
2842 | log_unit_error_errno(UNIT(s), r, "Failed to refresh extensions in unit's namespace: %m"); | |
2843 | else | |
2844 | log_unit_debug(UNIT(s), "Refreshed extensions in unit's namespace"); | |
2845 | ||
2846 | _exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS); | |
2847 | } | |
2848 | ||
2849 | r = unit_watch_pidref(UNIT(s), &worker, /* exclusive= */ true); | |
2850 | if (r < 0) { | |
2851 | log_unit_warning_errno(UNIT(s), r, "Failed to watch extensions refresh helper process: %m"); | |
2852 | goto fail; | |
2853 | } | |
2854 | ||
2855 | s->control_pid = TAKE_PIDREF(worker); | |
2856 | service_set_state(s, SERVICE_REFRESH_EXTENSIONS); | |
2857 | return; | |
2858 | ||
2859 | fail: | |
2860 | s->reload_result = SERVICE_FAILURE_RESOURCES; | |
2861 | service_enter_running(s, SERVICE_SUCCESS); | |
2862 | } | |
2863 | ||
2864 | static void service_enter_reload_mounting(Service *s) { | |
2865 | int r; | |
2866 | ||
2867 | assert(s); | |
2868 | ||
2869 | usec_t ts = now(CLOCK_MONOTONIC); | |
2870 | ||
2871 | r = service_arm_timer(s, /* relative= */ true, s->timeout_start_usec); | |
2872 | if (r < 0) { | |
2873 | log_unit_warning_errno(UNIT(s), r, "Failed to install timer: %m"); | |
2874 | s->reload_result = SERVICE_FAILURE_RESOURCES; | |
2875 | service_enter_running(s, SERVICE_SUCCESS); | |
2876 | return; | |
2877 | } | |
2878 | ||
2879 | s->reload_begin_usec = ts; | |
2880 | ||
2881 | service_enter_refresh_extensions(s); | |
2882 | } | |
2883 | ||
f42806df | 2884 | static void service_run_next_control(Service *s) { |
36c16a7c | 2885 | usec_t timeout; |
034c6ed7 LP |
2886 | int r; |
2887 | ||
2888 | assert(s); | |
2889 | assert(s->control_command); | |
2890 | assert(s->control_command->command_next); | |
2891 | ||
34e9ba66 | 2892 | assert(s->control_command_id != SERVICE_EXEC_START); |
034c6ed7 | 2893 | |
34e9ba66 | 2894 | s->control_command = s->control_command->command_next; |
5e94833f LP |
2895 | service_unwatch_control_pid(s); |
2896 | ||
31cd5f63 | 2897 | if (IN_SET(s->state, SERVICE_CONDITION, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) |
36c16a7c LP |
2898 | timeout = s->timeout_start_usec; |
2899 | else | |
2900 | timeout = s->timeout_stop_usec; | |
2901 | ||
c79ab77c LP |
2902 | pidref_done(&s->control_pid); |
2903 | ||
ecedd90f LP |
2904 | r = service_spawn(s, |
2905 | s->control_command, | |
cfbf7538 | 2906 | service_exec_flags(s->control_command_id, /* cred_flag = */ 0), |
36c16a7c | 2907 | timeout, |
ecedd90f | 2908 | &s->control_pid); |
10691b9e LP |
2909 | if (r < 0) { |
2910 | log_unit_warning_errno(UNIT(s), r, "Failed to spawn next control task: %m"); | |
2911 | ||
2912 | if (IN_SET(s->state, SERVICE_CONDITION, SERVICE_START_PRE, SERVICE_START_POST, SERVICE_STOP)) | |
2913 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); | |
2914 | else if (s->state == SERVICE_STOP_POST) | |
2915 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, /* allow_restart= */ true); | |
2916 | else if (s->state == SERVICE_RELOAD) { | |
2917 | s->reload_result = SERVICE_FAILURE_RESOURCES; | |
2918 | service_enter_running(s, SERVICE_SUCCESS); | |
2919 | } else | |
2920 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); | |
2921 | } | |
5cb5a6ff LP |
2922 | } |
2923 | ||
f42806df | 2924 | static void service_run_next_main(Service *s) { |
c79ab77c | 2925 | _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
34e9ba66 LP |
2926 | int r; |
2927 | ||
2928 | assert(s); | |
867b3b7d LP |
2929 | assert(s->main_command); |
2930 | assert(s->main_command->command_next); | |
2931 | assert(s->type == SERVICE_ONESHOT); | |
34e9ba66 | 2932 | |
867b3b7d | 2933 | s->main_command = s->main_command->command_next; |
34e9ba66 LP |
2934 | service_unwatch_main_pid(s); |
2935 | ||
ecedd90f LP |
2936 | r = service_spawn(s, |
2937 | s->main_command, | |
cfbf7538 | 2938 | service_exec_flags(SERVICE_EXEC_START, EXEC_SETUP_CREDENTIALS), |
21b2ce39 | 2939 | s->timeout_start_usec, |
c79ab77c | 2940 | &pidref); |
10691b9e LP |
2941 | if (r < 0) { |
2942 | log_unit_warning_errno(UNIT(s), r, "Failed to spawn next main task: %m"); | |
2943 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); | |
2944 | return; | |
2945 | } | |
34e9ba66 | 2946 | |
7e0e6b50 | 2947 | (void) service_set_main_pidref(s, TAKE_PIDREF(pidref), &s->main_command->exec_status.start_timestamp); |
34e9ba66 LP |
2948 | } |
2949 | ||
87f0e418 | 2950 | static int service_start(Unit *u) { |
1391f149 | 2951 | Service *s = ASSERT_PTR(SERVICE(u)); |
07299350 | 2952 | int r; |
5cb5a6ff | 2953 | |
034c6ed7 LP |
2954 | /* We cannot fulfill this request right now, try again later |
2955 | * please! */ | |
a00973af | 2956 | if (IN_SET(s->state, |
c87700a1 | 2957 | SERVICE_STOP, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
bf760801 | 2958 | SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL, SERVICE_CLEANING)) |
5cb5a6ff LP |
2959 | return -EAGAIN; |
2960 | ||
034c6ed7 | 2961 | /* Already on it! */ |
31cd5f63 | 2962 | if (IN_SET(s->state, SERVICE_CONDITION, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST)) |
034c6ed7 LP |
2963 | return 0; |
2964 | ||
1391f149 MY |
2965 | if (s->state == SERVICE_AUTO_RESTART) { |
2966 | /* As mentioned in unit_start(), we allow manual starts to act as "hurry up" signals | |
2967 | * for auto restart. We need to re-enqueue the job though, as the job type has changed | |
2968 | * (JOB_RESTART_DEPENDENCIES). */ | |
2969 | ||
2970 | service_enter_restart(s, /* shortcut = */ true); | |
a8bb2e65 | 2971 | return -EAGAIN; |
1391f149 | 2972 | } |
2e9d6c12 | 2973 | |
1391f149 MY |
2974 | /* SERVICE_*_BEFORE_AUTO_RESTART are not to be expected here, as those are intermediate states |
2975 | * that should never be seen outside of service_enter_dead(). */ | |
09d04ad3 | 2976 | assert(IN_SET(s->state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_DEAD_RESOURCES_PINNED, SERVICE_AUTO_RESTART_QUEUED)); |
5cb5a6ff | 2977 | |
4b58153d LP |
2978 | r = unit_acquire_invocation_id(u); |
2979 | if (r < 0) | |
2980 | return r; | |
2981 | ||
f42806df LP |
2982 | s->result = SERVICE_SUCCESS; |
2983 | s->reload_result = SERVICE_SUCCESS; | |
034c6ed7 | 2984 | s->main_pid_known = false; |
6dfa5494 | 2985 | s->main_pid_alien = false; |
47342320 | 2986 | s->forbid_restart = false; |
3c7416b6 | 2987 | |
ce31dbf4 MY |
2988 | /* This is not an automatic restart? Flush the restart counter then. */ |
2989 | if (s->state != SERVICE_AUTO_RESTART_QUEUED) | |
2990 | s->n_restarts = 0; | |
2991 | ||
a1e58e8e | 2992 | s->status_text = mfree(s->status_text); |
8cfdb077 | 2993 | s->status_errno = 0; |
9c025022 MY |
2994 | s->status_bus_error = mfree(s->status_bus_error); |
2995 | s->status_varlink_error = mfree(s->status_varlink_error); | |
8cfdb077 | 2996 | |
19dff691 | 2997 | s->notify_access_override = _NOTIFY_ACCESS_INVALID; |
308d72dc LP |
2998 | s->notify_state = NOTIFY_UNKNOWN; |
2999 | ||
aa8c4bbf | 3000 | s->watchdog_original_usec = s->watchdog_usec; |
2787d83c | 3001 | s->watchdog_override_enable = false; |
aa8c4bbf | 3002 | s->watchdog_override_usec = USEC_INFINITY; |
2787d83c | 3003 | |
6a1d4d9f LP |
3004 | exec_command_reset_status_list_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX); |
3005 | exec_status_reset(&s->main_exec_status); | |
3006 | ||
9cc54544 LP |
3007 | CGroupRuntime *crt = unit_get_cgroup_runtime(u); |
3008 | if (crt) | |
3009 | crt->reset_accounting = true; | |
6a1d4d9f | 3010 | |
31cd5f63 | 3011 | service_enter_condition(s); |
82a2b6bb | 3012 | return 1; |
5cb5a6ff LP |
3013 | } |
3014 | ||
a6eeca9a | 3015 | static void service_live_mount_finish(Service *s, ServiceResult f, const char *error) { |
5162829e LB |
3016 | assert(s); |
3017 | assert(error); | |
3018 | ||
a6eeca9a MY |
3019 | s->live_mount_result = f; |
3020 | ||
5162829e LB |
3021 | if (!s->mount_request) |
3022 | return; | |
3023 | ||
a6eeca9a | 3024 | if (f == SERVICE_SUCCESS) { |
5162829e LB |
3025 | (void) sd_bus_reply_method_return(s->mount_request, NULL); |
3026 | log_unit_debug(UNIT(s), | |
3027 | "'%s' method succeeded", | |
3028 | strna(sd_bus_message_get_member(s->mount_request))); | |
3029 | } else { | |
3030 | (void) sd_bus_reply_method_errorf(s->mount_request, error, | |
3031 | "method '%s' for unit '%s' failed", | |
3032 | strna(sd_bus_message_get_member(s->mount_request)), | |
3033 | UNIT(s)->id); | |
3034 | log_unit_debug(UNIT(s), | |
3035 | "'%s' method failed: %s", | |
3036 | strna(sd_bus_message_get_member(s->mount_request)), | |
3037 | error); | |
3038 | } | |
3039 | ||
3040 | s->mount_request = sd_bus_message_unref(s->mount_request); | |
3041 | } | |
3042 | ||
87f0e418 | 3043 | static int service_stop(Unit *u) { |
e9fa1bf7 | 3044 | Service *s = ASSERT_PTR(SERVICE(u)); |
5cb5a6ff | 3045 | |
a509f0e6 | 3046 | /* Don't create restart jobs from manual stops. */ |
47342320 | 3047 | s->forbid_restart = true; |
034c6ed7 | 3048 | |
a1d31573 LP |
3049 | switch (s->state) { |
3050 | ||
3051 | case SERVICE_STOP: | |
3052 | case SERVICE_STOP_SIGTERM: | |
3053 | case SERVICE_STOP_SIGKILL: | |
3054 | case SERVICE_STOP_POST: | |
3055 | case SERVICE_FINAL_WATCHDOG: | |
3056 | case SERVICE_FINAL_SIGTERM: | |
3057 | case SERVICE_FINAL_SIGKILL: | |
3058 | /* Already on it */ | |
e537352b LP |
3059 | return 0; |
3060 | ||
a1d31573 | 3061 | case SERVICE_AUTO_RESTART: |
09d04ad3 LP |
3062 | case SERVICE_AUTO_RESTART_QUEUED: |
3063 | /* Give up on the auto restart */ | |
b9c1883a | 3064 | service_set_state(s, service_determine_dead_state(s)); |
034c6ed7 | 3065 | return 0; |
034c6ed7 | 3066 | |
5162829e | 3067 | case SERVICE_MOUNTING: |
a6eeca9a | 3068 | service_live_mount_finish(s, SERVICE_FAILURE_PROTOCOL, BUS_ERROR_UNIT_INACTIVE); |
5162829e | 3069 | _fallthrough_; |
dfdeb0b1 | 3070 | case SERVICE_REFRESH_EXTENSIONS: |
3071 | service_kill_control_process(s); | |
3072 | _fallthrough_; | |
a1d31573 LP |
3073 | case SERVICE_CONDITION: |
3074 | case SERVICE_START_PRE: | |
3075 | case SERVICE_START: | |
3076 | case SERVICE_START_POST: | |
3077 | case SERVICE_RELOAD: | |
3078 | case SERVICE_RELOAD_SIGNAL: | |
3079 | case SERVICE_RELOAD_NOTIFY: | |
3080 | case SERVICE_STOP_WATCHDOG: | |
3081 | /* If there's already something running we go directly into kill mode. */ | |
f42806df | 3082 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS); |
3f6c78dc | 3083 | return 0; |
5cb5a6ff | 3084 | |
a1d31573 LP |
3085 | case SERVICE_CLEANING: |
3086 | /* If we are currently cleaning, then abort it, brutally. */ | |
4c2f5842 LP |
3087 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_SUCCESS); |
3088 | return 0; | |
a1d31573 LP |
3089 | |
3090 | case SERVICE_RUNNING: | |
3091 | case SERVICE_EXITED: | |
3092 | service_enter_stop(s, SERVICE_SUCCESS); | |
3093 | return 1; | |
3094 | ||
3095 | case SERVICE_DEAD_BEFORE_AUTO_RESTART: | |
3096 | case SERVICE_FAILED_BEFORE_AUTO_RESTART: | |
3097 | case SERVICE_DEAD: | |
3098 | case SERVICE_FAILED: | |
b9c1883a | 3099 | case SERVICE_DEAD_RESOURCES_PINNED: |
a1d31573 LP |
3100 | default: |
3101 | /* Unknown state, or unit_stop() should already have handled these */ | |
3102 | assert_not_reached(); | |
4c2f5842 | 3103 | } |
5cb5a6ff LP |
3104 | } |
3105 | ||
87f0e418 | 3106 | static int service_reload(Unit *u) { |
e9fa1bf7 | 3107 | Service *s = ASSERT_PTR(SERVICE(u)); |
034c6ed7 | 3108 | |
3742095b | 3109 | assert(IN_SET(s->state, SERVICE_RUNNING, SERVICE_EXITED)); |
034c6ed7 | 3110 | |
dfdeb0b1 | 3111 | service_enter_reload_mounting(s); |
3112 | ||
2d018ae2 | 3113 | return 1; |
5cb5a6ff LP |
3114 | } |
3115 | ||
d1e8e8b5 | 3116 | static bool service_can_reload(Unit *u) { |
e9fa1bf7 | 3117 | Service *s = ASSERT_PTR(SERVICE(u)); |
034c6ed7 | 3118 | |
3bd28bf7 LP |
3119 | return s->exec_command[SERVICE_EXEC_RELOAD] || |
3120 | s->type == SERVICE_NOTIFY_RELOAD; | |
034c6ed7 LP |
3121 | } |
3122 | ||
502096b5 | 3123 | static unsigned service_exec_command_index(Unit *u, ServiceExecCommand id, const ExecCommand *current) { |
e266c068 MS |
3124 | Service *s = SERVICE(u); |
3125 | unsigned idx = 0; | |
e266c068 MS |
3126 | |
3127 | assert(s); | |
5b99bd5f LP |
3128 | assert(id >= 0); |
3129 | assert(id < _SERVICE_EXEC_COMMAND_MAX); | |
e266c068 | 3130 | |
502096b5 | 3131 | const ExecCommand *first = s->exec_command[id]; |
e266c068 MS |
3132 | |
3133 | /* Figure out where we are in the list by walking back to the beginning */ | |
502096b5 | 3134 | for (const ExecCommand *c = current; c != first; c = c->command_prev) |
e266c068 MS |
3135 | idx++; |
3136 | ||
3137 | return idx; | |
3138 | } | |
3139 | ||
502096b5 | 3140 | static int service_serialize_exec_command(Unit *u, FILE *f, const ExecCommand *command) { |
e9fa1bf7 | 3141 | Service *s = ASSERT_PTR(SERVICE(u)); |
d68c645b | 3142 | _cleanup_free_ char *args = NULL, *p = NULL; |
d68c645b | 3143 | const char *type, *key; |
e266c068 | 3144 | ServiceExecCommand id; |
319a4f4b | 3145 | size_t length = 0; |
e266c068 | 3146 | unsigned idx; |
e266c068 | 3147 | |
e266c068 MS |
3148 | assert(f); |
3149 | ||
3150 | if (!command) | |
3151 | return 0; | |
3152 | ||
3153 | if (command == s->control_command) { | |
3154 | type = "control"; | |
3155 | id = s->control_command_id; | |
3156 | } else { | |
3157 | type = "main"; | |
3158 | id = SERVICE_EXEC_START; | |
3159 | } | |
3160 | ||
3161 | idx = service_exec_command_index(u, id, command); | |
3162 | ||
3163 | STRV_FOREACH(arg, command->argv) { | |
e266c068 | 3164 | _cleanup_free_ char *e = NULL; |
d68c645b | 3165 | size_t n; |
e266c068 | 3166 | |
d68c645b | 3167 | e = cescape(*arg); |
e266c068 | 3168 | if (!e) |
d68c645b | 3169 | return log_oom(); |
e266c068 MS |
3170 | |
3171 | n = strlen(e); | |
319a4f4b | 3172 | if (!GREEDY_REALLOC(args, length + 2 + n + 2)) |
d68c645b | 3173 | return log_oom(); |
e266c068 MS |
3174 | |
3175 | if (length > 0) | |
3176 | args[length++] = ' '; | |
3177 | ||
334c0979 | 3178 | args[length++] = '"'; |
e266c068 MS |
3179 | memcpy(args + length, e, n); |
3180 | length += n; | |
334c0979 | 3181 | args[length++] = '"'; |
e266c068 MS |
3182 | } |
3183 | ||
319a4f4b | 3184 | if (!GREEDY_REALLOC(args, length + 1)) |
d68c645b LP |
3185 | return log_oom(); |
3186 | ||
e266c068 MS |
3187 | args[length++] = 0; |
3188 | ||
d68c645b | 3189 | p = cescape(command->path); |
e266c068 | 3190 | if (!p) |
5b99bd5f | 3191 | return log_oom(); |
e266c068 | 3192 | |
d68c645b | 3193 | key = strjoina(type, "-command"); |
a99bd455 ZJS |
3194 | |
3195 | /* We use '+1234' instead of '1234' to mark the last command in a sequence. | |
3196 | * This is used in service_deserialize_exec_command(). */ | |
3197 | (void) serialize_item_format( | |
3198 | f, key, | |
3199 | "%s %s%u %s %s", | |
3200 | service_exec_command_to_string(id), | |
3201 | command->command_next ? "" : "+", | |
3202 | idx, | |
3203 | p, args); | |
5b99bd5f LP |
3204 | |
3205 | return 0; | |
e266c068 MS |
3206 | } |
3207 | ||
a16e1123 | 3208 | static int service_serialize(Unit *u, FILE *f, FDSet *fds) { |
e9fa1bf7 | 3209 | Service *s = ASSERT_PTR(SERVICE(u)); |
a34ceba6 | 3210 | int r; |
a16e1123 | 3211 | |
a16e1123 LP |
3212 | assert(f); |
3213 | assert(fds); | |
3214 | ||
d68c645b LP |
3215 | (void) serialize_item(f, "state", service_state_to_string(s->state)); |
3216 | (void) serialize_item(f, "result", service_result_to_string(s->result)); | |
3217 | (void) serialize_item(f, "reload-result", service_result_to_string(s->reload_result)); | |
78270121 | 3218 | (void) serialize_item(f, "live-mount-result", service_result_to_string(s->live_mount_result)); |
a16e1123 | 3219 | |
2a7451dc LP |
3220 | (void) serialize_pidref(f, fds, "control-pid", &s->control_pid); |
3221 | if (s->main_pid_known) | |
3222 | (void) serialize_pidref(f, fds, "main-pid", &s->main_pid); | |
a16e1123 | 3223 | |
d68c645b LP |
3224 | (void) serialize_bool(f, "main-pid-known", s->main_pid_known); |
3225 | (void) serialize_bool(f, "bus-name-good", s->bus_name_good); | |
a16e1123 | 3226 | |
d68c645b | 3227 | (void) serialize_item_format(f, "n-restarts", "%u", s->n_restarts); |
301dc073 | 3228 | (void) serialize_bool(f, "forbid-restart", s->forbid_restart); |
3a2776bc | 3229 | |
e266c068 MS |
3230 | service_serialize_exec_command(u, f, s->control_command); |
3231 | service_serialize_exec_command(u, f, s->main_command); | |
a16e1123 | 3232 | |
d68c645b | 3233 | r = serialize_fd(f, fds, "stdin-fd", s->stdin_fd); |
a34ceba6 LP |
3234 | if (r < 0) |
3235 | return r; | |
d68c645b | 3236 | r = serialize_fd(f, fds, "stdout-fd", s->stdout_fd); |
a34ceba6 LP |
3237 | if (r < 0) |
3238 | return r; | |
d68c645b | 3239 | r = serialize_fd(f, fds, "stderr-fd", s->stderr_fd); |
a34ceba6 LP |
3240 | if (r < 0) |
3241 | return r; | |
e44da745 | 3242 | |
5686391b | 3243 | if (s->exec_fd_event_source) { |
d68c645b | 3244 | r = serialize_fd(f, fds, "exec-fd", sd_event_source_get_io_fd(s->exec_fd_event_source)); |
5686391b LP |
3245 | if (r < 0) |
3246 | return r; | |
d68c645b LP |
3247 | |
3248 | (void) serialize_bool(f, "exec-fd-hot", s->exec_fd_hot); | |
5686391b LP |
3249 | } |
3250 | ||
9dfb64f8 | 3251 | if (UNIT_ISSET(s->accept_socket)) { |
d68c645b | 3252 | r = serialize_item(f, "accept-socket", UNIT_DEREF(s->accept_socket)->id); |
9dfb64f8 ZJS |
3253 | if (r < 0) |
3254 | return r; | |
3255 | } | |
3256 | ||
d68c645b | 3257 | r = serialize_fd(f, fds, "socket-fd", s->socket_fd); |
a34ceba6 LP |
3258 | if (r < 0) |
3259 | return r; | |
e44da745 | 3260 | |
2339fc93 | 3261 | LIST_FOREACH(fd_store, fs, s->fd_store) { |
8dd4c05b | 3262 | _cleanup_free_ char *c = NULL; |
2339fc93 LP |
3263 | int copy; |
3264 | ||
3265 | copy = fdset_put_dup(fds, fs->fd); | |
3266 | if (copy < 0) | |
d68c645b | 3267 | return log_error_errno(copy, "Failed to copy file descriptor for serialization: %m"); |
2339fc93 | 3268 | |
8dd4c05b | 3269 | c = cescape(fs->fdname); |
d68c645b LP |
3270 | if (!c) |
3271 | return log_oom(); | |
8dd4c05b | 3272 | |
9b85bf02 | 3273 | (void) serialize_item_format(f, "fd-store-fd", "%i \"%s\" %s", copy, c, one_zero(fs->do_poll)); |
2339fc93 LP |
3274 | } |
3275 | ||
8e66f42b MY |
3276 | FOREACH_ARRAY(i, s->extra_fds, s->n_extra_fds) { |
3277 | _cleanup_free_ char *c = NULL; | |
3278 | int copy; | |
3279 | ||
3280 | copy = fdset_put_dup(fds, i->fd); | |
3281 | if (copy < 0) | |
3282 | return log_error_errno(copy, "Failed to copy file descriptor for serialization: %m"); | |
3283 | ||
3284 | c = cescape(i->fdname); | |
3285 | if (!c) | |
3286 | return log_oom(); | |
3287 | ||
3288 | (void) serialize_item_format(f, "extra-fd", "%i \"%s\"", copy, c); | |
3289 | } | |
3290 | ||
ecdbca40 | 3291 | if (s->main_exec_status.pid > 0) { |
d68c645b LP |
3292 | (void) serialize_item_format(f, "main-exec-status-pid", PID_FMT, s->main_exec_status.pid); |
3293 | (void) serialize_dual_timestamp(f, "main-exec-status-start", &s->main_exec_status.start_timestamp); | |
3294 | (void) serialize_dual_timestamp(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp); | |
3c1d1ca1 | 3295 | (void) serialize_dual_timestamp(f, "main-exec-status-handoff", &s->main_exec_status.handoff_timestamp); |
ecdbca40 | 3296 | |
799fd0fd | 3297 | if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) { |
d68c645b LP |
3298 | (void) serialize_item_format(f, "main-exec-status-code", "%i", s->main_exec_status.code); |
3299 | (void) serialize_item_format(f, "main-exec-status-status", "%i", s->main_exec_status.status); | |
ecdbca40 LP |
3300 | } |
3301 | } | |
f06db334 | 3302 | |
19dff691 MY |
3303 | if (s->notify_access_override >= 0) |
3304 | (void) serialize_item(f, "notify-access-override", notify_access_to_string(s->notify_access_override)); | |
3305 | ||
301dc073 MY |
3306 | r = serialize_item_escaped(f, "status-text", s->status_text); |
3307 | if (r < 0) | |
3308 | return r; | |
3309 | ||
a74b2840 | 3310 | (void) serialize_item_format(f, "status-errno", "%d", s->status_errno); |
9c025022 MY |
3311 | (void) serialize_item(f, "status-bus-error", s->status_bus_error); |
3312 | (void) serialize_item(f, "status-varlink-error", s->status_varlink_error); | |
a74b2840 | 3313 | |
d68c645b | 3314 | (void) serialize_dual_timestamp(f, "watchdog-timestamp", &s->watchdog_timestamp); |
6aca9a58 | 3315 | |
301dc073 | 3316 | (void) serialize_usec(f, "watchdog-original-usec", s->watchdog_original_usec); |
2787d83c | 3317 | if (s->watchdog_override_enable) |
301dc073 | 3318 | (void) serialize_usec(f, "watchdog-override-usec", s->watchdog_override_usec); |
aa8c4bbf | 3319 | |
301dc073 | 3320 | (void) serialize_usec(f, "reload-begin-usec", s->reload_begin_usec); |
3bd28bf7 | 3321 | |
a16e1123 LP |
3322 | return 0; |
3323 | } | |
3324 | ||
35243b77 | 3325 | int service_deserialize_exec_command( |
5b99bd5f LP |
3326 | Unit *u, |
3327 | const char *key, | |
3328 | const char *value) { | |
3329 | ||
e9fa1bf7 | 3330 | Service *s = ASSERT_PTR(SERVICE(u)); |
e266c068 | 3331 | ExecCommand *command = NULL; |
e9fa1bf7 | 3332 | ServiceExecCommand id = _SERVICE_EXEC_COMMAND_INVALID; |
6eeec374 | 3333 | _cleanup_free_ char *path = NULL; |
e266c068 | 3334 | _cleanup_strv_free_ char **argv = NULL; |
e9fa1bf7 MY |
3335 | unsigned idx = 0, i; |
3336 | bool control, found = false, last = false; | |
3337 | int r; | |
e266c068 | 3338 | |
e1fa0e1a | 3339 | enum { |
e266c068 MS |
3340 | STATE_EXEC_COMMAND_TYPE, |
3341 | STATE_EXEC_COMMAND_INDEX, | |
3342 | STATE_EXEC_COMMAND_PATH, | |
3343 | STATE_EXEC_COMMAND_ARGS, | |
3344 | _STATE_EXEC_COMMAND_MAX, | |
2d93c20e | 3345 | _STATE_EXEC_COMMAND_INVALID = -EINVAL, |
e266c068 MS |
3346 | } state; |
3347 | ||
e266c068 MS |
3348 | assert(key); |
3349 | assert(value); | |
3350 | ||
3351 | control = streq(key, "control-command"); | |
3352 | ||
3353 | state = STATE_EXEC_COMMAND_TYPE; | |
3354 | ||
3355 | for (;;) { | |
3356 | _cleanup_free_ char *arg = NULL; | |
3357 | ||
334c0979 | 3358 | r = extract_first_word(&value, &arg, NULL, EXTRACT_CUNESCAPE | EXTRACT_UNQUOTE); |
efa3f34e LP |
3359 | if (r < 0) |
3360 | return r; | |
e266c068 MS |
3361 | if (r == 0) |
3362 | break; | |
e266c068 MS |
3363 | |
3364 | switch (state) { | |
e1fa0e1a | 3365 | |
e266c068 MS |
3366 | case STATE_EXEC_COMMAND_TYPE: |
3367 | id = service_exec_command_from_string(arg); | |
3368 | if (id < 0) | |
7211c853 | 3369 | return id; |
e266c068 MS |
3370 | |
3371 | state = STATE_EXEC_COMMAND_INDEX; | |
3372 | break; | |
e1fa0e1a | 3373 | |
e266c068 | 3374 | case STATE_EXEC_COMMAND_INDEX: |
e1fa0e1a MY |
3375 | /* ExecCommand index 1234 is serialized as either '1234' or '+1234'. The second form |
3376 | * is used to mark the last command in a sequence. We warn if the deserialized command | |
3377 | * doesn't match what we have loaded from the unit, but we don't need to warn if | |
3378 | * that is the last command. */ | |
a99bd455 | 3379 | |
e266c068 MS |
3380 | r = safe_atou(arg, &idx); |
3381 | if (r < 0) | |
7211c853 | 3382 | return r; |
a99bd455 | 3383 | last = arg[0] == '+'; |
e266c068 MS |
3384 | |
3385 | state = STATE_EXEC_COMMAND_PATH; | |
3386 | break; | |
e1fa0e1a | 3387 | |
e266c068 | 3388 | case STATE_EXEC_COMMAND_PATH: |
ae2a15bc | 3389 | path = TAKE_PTR(arg); |
e266c068 | 3390 | state = STATE_EXEC_COMMAND_ARGS; |
e266c068 | 3391 | break; |
e1fa0e1a | 3392 | |
e266c068 MS |
3393 | case STATE_EXEC_COMMAND_ARGS: |
3394 | r = strv_extend(&argv, arg); | |
3395 | if (r < 0) | |
010cd1dc | 3396 | return r; |
e266c068 | 3397 | break; |
e1fa0e1a | 3398 | |
e266c068 | 3399 | default: |
04499a70 | 3400 | assert_not_reached(); |
e266c068 MS |
3401 | } |
3402 | } | |
3403 | ||
3404 | if (state != STATE_EXEC_COMMAND_ARGS) | |
3405 | return -EINVAL; | |
90204792 ZJS |
3406 | if (strv_isempty(argv)) |
3407 | return -EINVAL; /* At least argv[0] must be always present. */ | |
e266c068 MS |
3408 | |
3409 | /* Let's check whether exec command on given offset matches data that we just deserialized */ | |
3410 | for (command = s->exec_command[id], i = 0; command; command = command->command_next, i++) { | |
3411 | if (i != idx) | |
3412 | continue; | |
3413 | ||
3414 | found = strv_equal(argv, command->argv) && streq(command->path, path); | |
3415 | break; | |
3416 | } | |
3417 | ||
3418 | if (!found) { | |
3419 | /* Command at the index we serialized is different, let's look for command that exactly | |
3420 | * matches but is on different index. If there is no such command we will not resume execution. */ | |
3421 | for (command = s->exec_command[id]; command; command = command->command_next) | |
3422 | if (strv_equal(command->argv, argv) && streq(command->path, path)) | |
3423 | break; | |
3424 | } | |
3425 | ||
e9da62b1 | 3426 | if (command && control) { |
e266c068 | 3427 | s->control_command = command; |
e9da62b1 LP |
3428 | s->control_command_id = id; |
3429 | } else if (command) | |
e266c068 | 3430 | s->main_command = command; |
a99bd455 ZJS |
3431 | else if (last) |
3432 | log_unit_debug(u, "Current command vanished from the unit file."); | |
e266c068 MS |
3433 | else |
3434 | log_unit_warning(u, "Current command vanished from the unit file, execution of the command list won't be resumed."); | |
3435 | ||
3436 | return 0; | |
3437 | } | |
3438 | ||
a16e1123 | 3439 | static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { |
e9fa1bf7 | 3440 | Service *s = ASSERT_PTR(SERVICE(u)); |
2339fc93 | 3441 | int r; |
a16e1123 | 3442 | |
a16e1123 LP |
3443 | assert(key); |
3444 | assert(value); | |
3445 | assert(fds); | |
3446 | ||
3447 | if (streq(key, "state")) { | |
3448 | ServiceState state; | |
3449 | ||
117dcc57 ZJS |
3450 | state = service_state_from_string(value); |
3451 | if (state < 0) | |
78270121 | 3452 | log_unit_debug_errno(u, state, "Failed to parse state value: %s", value); |
a16e1123 LP |
3453 | else |
3454 | s->deserialized_state = state; | |
f42806df LP |
3455 | } else if (streq(key, "result")) { |
3456 | ServiceResult f; | |
3457 | ||
3458 | f = service_result_from_string(value); | |
3459 | if (f < 0) | |
78270121 | 3460 | log_unit_debug_errno(u, f, "Failed to parse result value: %s", value); |
f42806df LP |
3461 | else if (f != SERVICE_SUCCESS) |
3462 | s->result = f; | |
3463 | ||
3464 | } else if (streq(key, "reload-result")) { | |
3465 | ServiceResult f; | |
3466 | ||
3467 | f = service_result_from_string(value); | |
3468 | if (f < 0) | |
78270121 | 3469 | log_unit_debug_errno(u, f, "Failed to parse reload result value: %s", value); |
f42806df LP |
3470 | else if (f != SERVICE_SUCCESS) |
3471 | s->reload_result = f; | |
a16e1123 | 3472 | |
78270121 MY |
3473 | } else if (streq(key, "live-mount-result")) { |
3474 | ServiceResult f; | |
3475 | ||
3476 | f = service_result_from_string(value); | |
3477 | if (f < 0) | |
3478 | log_unit_debug_errno(u, f, "Failed to parse live mount result value: %s", value); | |
3479 | else if (f != SERVICE_SUCCESS) | |
3480 | s->live_mount_result = f; | |
3481 | ||
a16e1123 | 3482 | } else if (streq(key, "control-pid")) { |
2a7451dc | 3483 | |
aaa872a7 DDM |
3484 | if (!pidref_is_set(&s->control_pid)) |
3485 | (void) deserialize_pidref(fds, value, &s->control_pid); | |
2a7451dc | 3486 | |
a16e1123 | 3487 | } else if (streq(key, "main-pid")) { |
c603f523 | 3488 | PidRef pidref; |
2a7451dc | 3489 | |
70727771 | 3490 | if (!pidref_is_set(&s->main_pid) && deserialize_pidref(fds, value, &pidref) >= 0) |
7e0e6b50 | 3491 | (void) service_set_main_pidref(s, pidref, /* start_timestamp = */ NULL); |
a16e1123 | 3492 | |
a16e1123 | 3493 | } else if (streq(key, "main-pid-known")) { |
156d23ab MY |
3494 | r = parse_boolean(value); |
3495 | if (r < 0) | |
3496 | log_unit_debug_errno(u, r, "Failed to parse main-pid-known value: %s", value); | |
a16e1123 | 3497 | else |
156d23ab | 3498 | s->main_pid_known = r; |
de1d4f9b | 3499 | } else if (streq(key, "bus-name-good")) { |
156d23ab MY |
3500 | r = parse_boolean(value); |
3501 | if (r < 0) | |
3502 | log_unit_debug_errno(u, r, "Failed to parse bus-name-good value: %s", value); | |
de1d4f9b | 3503 | else |
156d23ab | 3504 | s->bus_name_good = r; |
9dfb64f8 ZJS |
3505 | } else if (streq(key, "accept-socket")) { |
3506 | Unit *socket; | |
3507 | ||
9f5d8c3d MY |
3508 | if (unit_name_to_type(value) != UNIT_SOCKET) { |
3509 | log_unit_debug(u, "Deserialized accept-socket is not a socket unit, ignoring: %s", value); | |
45b10174 FS |
3510 | return 0; |
3511 | } | |
3512 | ||
9dfb64f8 ZJS |
3513 | r = manager_load_unit(u->manager, value, NULL, NULL, &socket); |
3514 | if (r < 0) | |
5e1ee764 | 3515 | log_unit_debug_errno(u, r, "Failed to load accept-socket unit '%s': %m", value); |
9dfb64f8 | 3516 | else { |
7f7d01ed | 3517 | unit_ref_set(&s->accept_socket, u, socket); |
9f5d8c3d | 3518 | ASSERT_PTR(SOCKET(socket))->n_connections++; |
9dfb64f8 ZJS |
3519 | } |
3520 | ||
a16e1123 | 3521 | } else if (streq(key, "socket-fd")) { |
dff9808a LP |
3522 | asynchronous_close(s->socket_fd); |
3523 | s->socket_fd = deserialize_fd(fds, value); | |
a16e1123 | 3524 | |
2339fc93 | 3525 | } else if (streq(key, "fd-store-fd")) { |
30520492 | 3526 | _cleanup_free_ char *fdv = NULL, *fdn = NULL, *fdp = NULL; |
dff9808a LP |
3527 | _cleanup_close_ int fd = -EBADF; |
3528 | int do_poll; | |
2339fc93 | 3529 | |
9b85bf02 MY |
3530 | r = extract_many_words(&value, " ", EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE, &fdv, &fdn, &fdp); |
3531 | if (r < 2 || r > 3) { | |
3532 | log_unit_debug(u, "Failed to deserialize fd-store-fd, ignoring: %s", value); | |
30520492 KL |
3533 | return 0; |
3534 | } | |
8dd4c05b | 3535 | |
dff9808a LP |
3536 | fd = deserialize_fd(fds, fdv); |
3537 | if (fd < 0) | |
3538 | return 0; | |
3539 | ||
9b85bf02 MY |
3540 | do_poll = r == 3 ? parse_boolean(fdp) : true; |
3541 | if (do_poll < 0) { | |
3542 | log_unit_debug_errno(u, do_poll, | |
3543 | "Failed to deserialize fd-store-fd do_poll, ignoring: %s", fdp); | |
30520492 | 3544 | return 0; |
2339fc93 LP |
3545 | } |
3546 | ||
d9919881 | 3547 | r = service_add_fd_store(s, TAKE_FD(fd), fdn, do_poll); |
a02287ea | 3548 | if (r < 0) { |
9b85bf02 MY |
3549 | log_unit_debug_errno(u, r, |
3550 | "Failed to store deserialized fd '%s', ignoring: %m", fdn); | |
a02287ea YW |
3551 | return 0; |
3552 | } | |
8e66f42b MY |
3553 | } else if (streq(key, "extra-fd")) { |
3554 | _cleanup_free_ char *fdv = NULL, *fdn = NULL; | |
3555 | _cleanup_close_ int fd = -EBADF; | |
3556 | ||
3557 | r = extract_many_words(&value, " ", EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE, &fdv, &fdn); | |
3558 | if (r != 2) { | |
3559 | log_unit_debug(u, "Failed to deserialize extra-fd, ignoring: %s", value); | |
3560 | return 0; | |
3561 | } | |
3562 | ||
3563 | fd = deserialize_fd(fds, fdv); | |
3564 | if (fd < 0) | |
3565 | return 0; | |
3566 | ||
3567 | if (!GREEDY_REALLOC(s->extra_fds, s->n_extra_fds + 1)) { | |
3568 | log_oom_debug(); | |
3569 | return 0; | |
3570 | } | |
3571 | ||
3572 | s->extra_fds[s->n_extra_fds++] = (ServiceExtraFD) { | |
3573 | .fd = TAKE_FD(fd), | |
3574 | .fdname = TAKE_PTR(fdn), | |
3575 | }; | |
ecdbca40 LP |
3576 | } else if (streq(key, "main-exec-status-pid")) { |
3577 | pid_t pid; | |
3578 | ||
e364ad06 | 3579 | if (parse_pid(value, &pid) < 0) |
f2341e0a | 3580 | log_unit_debug(u, "Failed to parse main-exec-status-pid value: %s", value); |
ecdbca40 LP |
3581 | else |
3582 | s->main_exec_status.pid = pid; | |
3583 | } else if (streq(key, "main-exec-status-code")) { | |
3584 | int i; | |
3585 | ||
e364ad06 | 3586 | if (safe_atoi(value, &i) < 0) |
f2341e0a | 3587 | log_unit_debug(u, "Failed to parse main-exec-status-code value: %s", value); |
ecdbca40 LP |
3588 | else |
3589 | s->main_exec_status.code = i; | |
3590 | } else if (streq(key, "main-exec-status-status")) { | |
3591 | int i; | |
3592 | ||
e364ad06 | 3593 | if (safe_atoi(value, &i) < 0) |
f2341e0a | 3594 | log_unit_debug(u, "Failed to parse main-exec-status-status value: %s", value); |
ecdbca40 LP |
3595 | else |
3596 | s->main_exec_status.status = i; | |
799fd0fd | 3597 | } else if (streq(key, "main-exec-status-start")) |
42fdb142 | 3598 | (void) deserialize_dual_timestamp(value, &s->main_exec_status.start_timestamp); |
799fd0fd | 3599 | else if (streq(key, "main-exec-status-exit")) |
42fdb142 | 3600 | (void) deserialize_dual_timestamp(value, &s->main_exec_status.exit_timestamp); |
3c1d1ca1 | 3601 | else if (streq(key, "main-exec-status-handoff")) |
42fdb142 | 3602 | (void) deserialize_dual_timestamp(value, &s->main_exec_status.handoff_timestamp); |
301dc073 MY |
3603 | else if (STR_IN_SET(key, "main-command", "control-command")) { |
3604 | r = service_deserialize_exec_command(u, key, value); | |
3605 | if (r < 0) | |
3606 | log_unit_debug_errno(u, r, "Failed to parse serialized command \"%s\": %m", value); | |
3607 | } else if (streq(key, "notify-access-override")) { | |
19dff691 MY |
3608 | NotifyAccess notify_access; |
3609 | ||
3610 | notify_access = notify_access_from_string(value); | |
3611 | if (notify_access < 0) | |
3612 | log_unit_debug(u, "Failed to parse notify-access-override value: %s", value); | |
3613 | else | |
3614 | s->notify_access_override = notify_access; | |
301dc073 MY |
3615 | } else if (streq(key, "n-restarts")) { |
3616 | r = safe_atou(value, &s->n_restarts); | |
3617 | if (r < 0) | |
3618 | log_unit_debug_errno(u, r, "Failed to parse serialized restart counter '%s': %m", value); | |
6aca9a58 | 3619 | |
301dc073 MY |
3620 | } else if (streq(key, "forbid-restart")) { |
3621 | r = parse_boolean(value); | |
3622 | if (r < 0) | |
3623 | log_unit_debug_errno(u, r, "Failed to parse forbid-restart value: %s", value); | |
6aca9a58 | 3624 | else |
301dc073 | 3625 | s->forbid_restart = r; |
a34ceba6 | 3626 | } else if (streq(key, "stdin-fd")) { |
a34ceba6 | 3627 | |
dff9808a LP |
3628 | asynchronous_close(s->stdin_fd); |
3629 | s->stdin_fd = deserialize_fd(fds, value); | |
3630 | if (s->stdin_fd >= 0) | |
1e22b5cd | 3631 | s->exec_context.stdio_as_fds = true; |
dff9808a | 3632 | |
a34ceba6 | 3633 | } else if (streq(key, "stdout-fd")) { |
a34ceba6 | 3634 | |
dff9808a LP |
3635 | asynchronous_close(s->stdout_fd); |
3636 | s->stdout_fd = deserialize_fd(fds, value); | |
3637 | if (s->stdout_fd >= 0) | |
1e22b5cd | 3638 | s->exec_context.stdio_as_fds = true; |
dff9808a | 3639 | |
a34ceba6 | 3640 | } else if (streq(key, "stderr-fd")) { |
a34ceba6 | 3641 | |
dff9808a LP |
3642 | asynchronous_close(s->stderr_fd); |
3643 | s->stderr_fd = deserialize_fd(fds, value); | |
3644 | if (s->stderr_fd >= 0) | |
1e22b5cd | 3645 | s->exec_context.stdio_as_fds = true; |
dff9808a | 3646 | |
5686391b | 3647 | } else if (streq(key, "exec-fd")) { |
dff9808a | 3648 | _cleanup_close_ int fd = -EBADF; |
5686391b | 3649 | |
dff9808a LP |
3650 | fd = deserialize_fd(fds, value); |
3651 | if (fd >= 0) { | |
5dcadb4c | 3652 | s->exec_fd_event_source = sd_event_source_disable_unref(s->exec_fd_event_source); |
5686391b | 3653 | |
dff9808a LP |
3654 | if (service_allocate_exec_fd_event_source(s, fd, &s->exec_fd_event_source) >= 0) |
3655 | TAKE_FD(fd); | |
5686391b | 3656 | } |
dff9808a | 3657 | |
301dc073 MY |
3658 | } else if (streq(key, "status-text")) { |
3659 | char *t; | |
3660 | ssize_t l; | |
aa8c4bbf | 3661 | |
301dc073 MY |
3662 | l = cunescape(value, 0, &t); |
3663 | if (l < 0) | |
3664 | log_unit_debug_errno(u, l, "Failed to unescape status text '%s': %m", value); | |
3665 | else | |
3666 | free_and_replace(s->status_text, t); | |
7a0019d3 | 3667 | |
a74b2840 MY |
3668 | } else if (streq(key, "status-errno")) { |
3669 | int i; | |
3670 | ||
3671 | if (safe_atoi(value, &i) < 0) | |
3672 | log_unit_debug(u, "Failed to parse status-errno value: %s", value); | |
3673 | else | |
3674 | s->status_errno = i; | |
3675 | ||
9c025022 MY |
3676 | } else if (streq(key, "status-bus-error")) { |
3677 | if (free_and_strdup(&s->status_bus_error, value) < 0) | |
3678 | log_oom_debug(); | |
3679 | ||
3680 | } else if (streq(key, "status-varlink-error")) { | |
3681 | if (free_and_strdup(&s->status_varlink_error, value) < 0) | |
3682 | log_oom_debug(); | |
3683 | ||
301dc073 | 3684 | } else if (streq(key, "watchdog-timestamp")) |
42fdb142 | 3685 | (void) deserialize_dual_timestamp(value, &s->watchdog_timestamp); |
301dc073 | 3686 | else if (streq(key, "watchdog-original-usec")) |
42fdb142 | 3687 | (void) deserialize_usec(value, &s->watchdog_original_usec); |
301dc073 MY |
3688 | else if (streq(key, "watchdog-override-usec")) { |
3689 | if (deserialize_usec(value, &s->watchdog_override_usec) >= 0) | |
3690 | s->watchdog_override_enable = true; | |
7a0019d3 | 3691 | |
301dc073 | 3692 | } else if (streq(key, "reload-begin-usec")) |
42fdb142 | 3693 | (void) deserialize_usec(value, &s->reload_begin_usec); |
301dc073 | 3694 | else |
f2341e0a | 3695 | log_unit_debug(u, "Unknown serialization key: %s", key); |
a16e1123 LP |
3696 | |
3697 | return 0; | |
3698 | } | |
3699 | ||
d1e8e8b5 | 3700 | static UnitActiveState service_active_state(Unit *u) { |
e9fa1bf7 | 3701 | Service *s = ASSERT_PTR(SERVICE(u)); |
e056b01d LP |
3702 | const UnitActiveState *table; |
3703 | ||
e9fa1bf7 | 3704 | table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table; |
e056b01d | 3705 | |
e9fa1bf7 | 3706 | return table[s->state]; |
034c6ed7 LP |
3707 | } |
3708 | ||
10a94420 LP |
3709 | static const char *service_sub_state_to_string(Unit *u) { |
3710 | assert(u); | |
3711 | ||
3712 | return service_state_to_string(SERVICE(u)->state); | |
3713 | } | |
3714 | ||
f2f725e5 | 3715 | static bool service_may_gc(Unit *u) { |
e9fa1bf7 | 3716 | Service *s = ASSERT_PTR(SERVICE(u)); |
701cc384 | 3717 | |
e98b2fbb | 3718 | /* Never clean up services that still have a process around, even if the service is formally dead. Note that |
f2f725e5 | 3719 | * unit_may_gc() already checked our cgroup for us, we just check our two additional PIDs, too, in case they |
e98b2fbb LP |
3720 | * have moved outside of the cgroup. */ |
3721 | ||
3722 | if (main_pid_good(s) > 0 || | |
6d55002a | 3723 | control_pid_good(s) > 0) |
f2f725e5 | 3724 | return false; |
6d55002a | 3725 | |
a1d31573 LP |
3726 | /* Only allow collection of actually dead services, i.e. not those that are in the transitionary |
3727 | * SERVICE_DEAD_BEFORE_AUTO_RESTART/SERVICE_FAILED_BEFORE_AUTO_RESTART states. */ | |
b9c1883a | 3728 | if (!IN_SET(s->state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_DEAD_RESOURCES_PINNED)) |
a1d31573 LP |
3729 | return false; |
3730 | ||
f2f725e5 | 3731 | return true; |
6d55002a LP |
3732 | } |
3733 | ||
3a111838 MS |
3734 | static int service_retry_pid_file(Service *s) { |
3735 | int r; | |
3736 | ||
e9fa1bf7 | 3737 | assert(s); |
3a111838 | 3738 | assert(s->pid_file); |
3742095b | 3739 | assert(IN_SET(s->state, SERVICE_START, SERVICE_START_POST)); |
3a111838 MS |
3740 | |
3741 | r = service_load_pid_file(s, false); | |
3742 | if (r < 0) | |
3743 | return r; | |
3744 | ||
3745 | service_unwatch_pid_file(s); | |
3746 | ||
f42806df | 3747 | service_enter_running(s, SERVICE_SUCCESS); |
3a111838 MS |
3748 | return 0; |
3749 | } | |
3750 | ||
3751 | static int service_watch_pid_file(Service *s) { | |
3752 | int r; | |
3753 | ||
e9fa1bf7 MY |
3754 | assert(s); |
3755 | ||
f2341e0a | 3756 | log_unit_debug(UNIT(s), "Setting watch for PID file %s", s->pid_file_pathspec->path); |
8bb2d17d | 3757 | |
5686391b | 3758 | r = path_spec_watch(s->pid_file_pathspec, service_dispatch_inotify_io); |
10691b9e LP |
3759 | if (r < 0) { |
3760 | log_unit_error_errno(UNIT(s), r, "Failed to set a watch for PID file %s: %m", s->pid_file_pathspec->path); | |
3761 | service_unwatch_pid_file(s); | |
3762 | return r; | |
3763 | } | |
3a111838 MS |
3764 | |
3765 | /* the pidfile might have appeared just before we set the watch */ | |
f2341e0a | 3766 | log_unit_debug(UNIT(s), "Trying to read PID file %s in case it changed", s->pid_file_pathspec->path); |
3a111838 MS |
3767 | service_retry_pid_file(s); |
3768 | ||
3769 | return 0; | |
3a111838 MS |
3770 | } |
3771 | ||
3772 | static int service_demand_pid_file(Service *s) { | |
51339a9a | 3773 | _cleanup_free_ PathSpec *ps = NULL; |
3a111838 | 3774 | |
e9fa1bf7 | 3775 | assert(s); |
3a111838 MS |
3776 | assert(s->pid_file); |
3777 | assert(!s->pid_file_pathspec); | |
3778 | ||
51339a9a | 3779 | ps = new(PathSpec, 1); |
3a111838 MS |
3780 | if (!ps) |
3781 | return -ENOMEM; | |
3782 | ||
51339a9a LP |
3783 | *ps = (PathSpec) { |
3784 | .unit = UNIT(s), | |
3785 | .path = strdup(s->pid_file), | |
3786 | /* PATH_CHANGED would not be enough. There are daemons (sendmail) that keep their PID file | |
3787 | * open all the time. */ | |
3788 | .type = PATH_MODIFIED, | |
3789 | .inotify_fd = -EBADF, | |
3790 | }; | |
3791 | ||
3792 | if (!ps->path) | |
3a111838 | 3793 | return -ENOMEM; |
3a111838 | 3794 | |
4ff361cc | 3795 | path_simplify(ps->path); |
3a111838 | 3796 | |
51339a9a | 3797 | s->pid_file_pathspec = TAKE_PTR(ps); |
3a111838 MS |
3798 | |
3799 | return service_watch_pid_file(s); | |
3800 | } | |
3801 | ||
5686391b | 3802 | static int service_dispatch_inotify_io(sd_event_source *source, int fd, uint32_t events, void *userdata) { |
99534007 | 3803 | PathSpec *p = ASSERT_PTR(userdata); |
e9fa1bf7 | 3804 | Service *s = ASSERT_PTR(SERVICE(p->unit)); |
3a111838 | 3805 | |
3a111838 | 3806 | assert(fd >= 0); |
3742095b | 3807 | assert(IN_SET(s->state, SERVICE_START, SERVICE_START_POST)); |
3a111838 | 3808 | assert(s->pid_file_pathspec); |
57020a3a | 3809 | assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd)); |
3a111838 | 3810 | |
f2341e0a | 3811 | log_unit_debug(UNIT(s), "inotify event"); |
3a111838 | 3812 | |
e14c2802 | 3813 | if (path_spec_fd_event(p, events) < 0) |
3a111838 MS |
3814 | goto fail; |
3815 | ||
3816 | if (service_retry_pid_file(s) == 0) | |
718db961 | 3817 | return 0; |
3a111838 MS |
3818 | |
3819 | if (service_watch_pid_file(s) < 0) | |
3820 | goto fail; | |
3821 | ||
718db961 LP |
3822 | return 0; |
3823 | ||
3a111838 MS |
3824 | fail: |
3825 | service_unwatch_pid_file(s); | |
f42806df | 3826 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
718db961 | 3827 | return 0; |
3a111838 MS |
3828 | } |
3829 | ||
5686391b | 3830 | static int service_dispatch_exec_io(sd_event_source *source, int fd, uint32_t events, void *userdata) { |
e9fa1bf7 | 3831 | Service *s = ASSERT_PTR(SERVICE(userdata)); |
5686391b LP |
3832 | |
3833 | log_unit_debug(UNIT(s), "got exec-fd event"); | |
3834 | ||
3835 | /* If Type=exec is set, we'll consider a service started successfully the instant we invoked execve() | |
12001b1b LP |
3836 | * successfully for it. We implement this through a pipe() towards the child, which the kernel |
3837 | * automatically closes for us due to O_CLOEXEC on execve() in the child, which then triggers EOF on | |
3838 | * the pipe in the parent. We need to be careful however, as there are other reasons that we might | |
3839 | * cause the child's side of the pipe to be closed (for example, a simple exit()). To deal with that | |
3840 | * we'll ignore EOFs on the pipe unless the child signalled us first that it is about to call the | |
3841 | * execve(). It does so by sending us a simple non-zero byte via the pipe. We also provide the child | |
3842 | * with a way to inform us in case execve() failed: if it sends a zero byte we'll ignore POLLHUP on | |
3843 | * the fd again. */ | |
5686391b LP |
3844 | |
3845 | for (;;) { | |
12001b1b | 3846 | uint8_t x; |
5686391b LP |
3847 | ssize_t n; |
3848 | ||
12001b1b LP |
3849 | n = read(fd, &x, sizeof(x)); |
3850 | if (n < 0) { | |
3851 | if (errno == EAGAIN) /* O_NONBLOCK in effect → everything queued has now been processed. */ | |
3852 | return 0; | |
3853 | ||
3854 | return log_unit_error_errno(UNIT(s), errno, "Failed to read from exec_fd: %m"); | |
3855 | } | |
3856 | if (n == 0) { /* EOF → the event we are waiting for in case of Type=exec */ | |
5dcadb4c | 3857 | s->exec_fd_event_source = sd_event_source_disable_unref(s->exec_fd_event_source); |
5686391b LP |
3858 | |
3859 | if (s->exec_fd_hot) { /* Did the child tell us to expect EOF now? */ | |
3860 | log_unit_debug(UNIT(s), "Got EOF on exec-fd"); | |
3861 | ||
3862 | s->exec_fd_hot = false; | |
3863 | ||
3864 | /* Nice! This is what we have been waiting for. Transition to next state. */ | |
3865 | if (s->type == SERVICE_EXEC && s->state == SERVICE_START) | |
3866 | service_enter_start_post(s); | |
3867 | } else | |
3868 | log_unit_debug(UNIT(s), "Got EOF on exec-fd while it was disabled, ignoring."); | |
3869 | ||
3870 | return 0; | |
3871 | } | |
5686391b | 3872 | |
12001b1b LP |
3873 | /* A byte was read → this turns on/off the exec fd logic */ |
3874 | assert(n == sizeof(x)); | |
93cb78ae | 3875 | |
12001b1b | 3876 | s->exec_fd_hot = x; |
93cb78ae | 3877 | } |
5686391b LP |
3878 | } |
3879 | ||
a911bb9a | 3880 | static void service_notify_cgroup_empty_event(Unit *u) { |
e9fa1bf7 | 3881 | Service *s = ASSERT_PTR(SERVICE(u)); |
a911bb9a | 3882 | |
a5b5aece | 3883 | log_unit_debug(u, "Control group is empty."); |
a911bb9a LP |
3884 | |
3885 | switch (s->state) { | |
3886 | ||
a1d31573 LP |
3887 | /* Waiting for SIGCHLD is usually more interesting, because it includes return |
3888 | * codes/signals. Which is why we ignore the cgroup events for most cases, except when we | |
3889 | * don't know pid which to expect the SIGCHLD for. */ | |
a911bb9a LP |
3890 | |
3891 | case SERVICE_START: | |
3bd28bf7 | 3892 | if (IN_SET(s->type, SERVICE_NOTIFY, SERVICE_NOTIFY_RELOAD) && |
3c751b1b LP |
3893 | main_pid_good(s) == 0 && |
3894 | control_pid_good(s) == 0) { | |
3d474ef7 | 3895 | /* No chance of getting a ready notification anymore */ |
c3fda31d | 3896 | service_enter_stop_post(s, SERVICE_FAILURE_PROTOCOL); |
71e529fc JW |
3897 | break; |
3898 | } | |
3899 | ||
f52e9ed6 MY |
3900 | if (s->exit_type == SERVICE_EXIT_CGROUP && main_pid_good(s) <= 0) { |
3901 | service_enter_stop_post(s, SERVICE_SUCCESS); | |
3902 | break; | |
3903 | } | |
596e4470 | 3904 | |
4831981d | 3905 | _fallthrough_; |
71e529fc | 3906 | case SERVICE_START_POST: |
3c751b1b LP |
3907 | if (s->pid_file_pathspec && |
3908 | main_pid_good(s) == 0 && | |
3909 | control_pid_good(s) == 0) { | |
3910 | ||
3d474ef7 | 3911 | /* Give up hoping for the daemon to write its PID file */ |
f2341e0a | 3912 | log_unit_warning(u, "Daemon never wrote its PID file. Failing."); |
8bb2d17d | 3913 | |
a911bb9a LP |
3914 | service_unwatch_pid_file(s); |
3915 | if (s->state == SERVICE_START) | |
c3fda31d | 3916 | service_enter_stop_post(s, SERVICE_FAILURE_PROTOCOL); |
a911bb9a | 3917 | else |
c35755fb | 3918 | service_enter_stop(s, SERVICE_FAILURE_PROTOCOL); |
a911bb9a LP |
3919 | } |
3920 | break; | |
3921 | ||
3922 | case SERVICE_RUNNING: | |
3923 | /* service_enter_running() will figure out what to do */ | |
3924 | service_enter_running(s, SERVICE_SUCCESS); | |
3925 | break; | |
3926 | ||
c87700a1 | 3927 | case SERVICE_STOP_WATCHDOG: |
a911bb9a LP |
3928 | case SERVICE_STOP_SIGTERM: |
3929 | case SERVICE_STOP_SIGKILL: | |
3930 | ||
b13ddbbc | 3931 | if (main_pid_good(s) <= 0 && control_pid_good(s) <= 0) |
a911bb9a LP |
3932 | service_enter_stop_post(s, SERVICE_SUCCESS); |
3933 | ||
3934 | break; | |
3935 | ||
3936 | case SERVICE_STOP_POST: | |
bf760801 | 3937 | case SERVICE_FINAL_WATCHDOG: |
a911bb9a LP |
3938 | case SERVICE_FINAL_SIGTERM: |
3939 | case SERVICE_FINAL_SIGKILL: | |
b13ddbbc | 3940 | if (main_pid_good(s) <= 0 && control_pid_good(s) <= 0) |
a911bb9a LP |
3941 | service_enter_dead(s, SERVICE_SUCCESS, true); |
3942 | ||
3943 | break; | |
3944 | ||
e08dabfe AZ |
3945 | /* If the cgroup empty notification comes when the unit is not active, we must have failed to clean |
3946 | * up the cgroup earlier and should do it now. */ | |
a1d31573 | 3947 | case SERVICE_AUTO_RESTART: |
09d04ad3 | 3948 | case SERVICE_AUTO_RESTART_QUEUED: |
e08dabfe AZ |
3949 | unit_prune_cgroup(u); |
3950 | break; | |
3951 | ||
a911bb9a LP |
3952 | default: |
3953 | ; | |
3954 | } | |
3955 | } | |
3956 | ||
38c41427 | 3957 | static void service_notify_cgroup_oom_event(Unit *u, bool managed_oom) { |
e9fa1bf7 | 3958 | Service *s = ASSERT_PTR(SERVICE(u)); |
afcfaa69 | 3959 | |
38c41427 NK |
3960 | if (managed_oom) |
3961 | log_unit_debug(u, "Process(es) of control group were killed by systemd-oomd."); | |
3962 | else | |
3963 | log_unit_debug(u, "Process of control group was killed by the OOM killer."); | |
afcfaa69 LP |
3964 | |
3965 | if (s->oom_policy == OOM_CONTINUE) | |
3966 | return; | |
3967 | ||
3968 | switch (s->state) { | |
3969 | ||
31cd5f63 | 3970 | case SERVICE_CONDITION: |
afcfaa69 LP |
3971 | case SERVICE_START_PRE: |
3972 | case SERVICE_START: | |
3973 | case SERVICE_START_POST: | |
3974 | case SERVICE_STOP: | |
3975 | if (s->oom_policy == OOM_STOP) | |
3976 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_OOM_KILL); | |
3977 | else if (s->oom_policy == OOM_KILL) | |
3978 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_OOM_KILL); | |
3979 | ||
3980 | break; | |
3981 | ||
3982 | case SERVICE_EXITED: | |
3983 | case SERVICE_RUNNING: | |
3984 | if (s->oom_policy == OOM_STOP) | |
3985 | service_enter_stop(s, SERVICE_FAILURE_OOM_KILL); | |
3986 | else if (s->oom_policy == OOM_KILL) | |
3987 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_OOM_KILL); | |
3988 | ||
3989 | break; | |
3990 | ||
3991 | case SERVICE_STOP_WATCHDOG: | |
3992 | case SERVICE_STOP_SIGTERM: | |
3993 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_OOM_KILL); | |
3994 | break; | |
3995 | ||
3996 | case SERVICE_STOP_SIGKILL: | |
3997 | case SERVICE_FINAL_SIGKILL: | |
3998 | if (s->result == SERVICE_SUCCESS) | |
3999 | s->result = SERVICE_FAILURE_OOM_KILL; | |
4000 | break; | |
4001 | ||
4002 | case SERVICE_STOP_POST: | |
4003 | case SERVICE_FINAL_SIGTERM: | |
4004 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_OOM_KILL); | |
4005 | break; | |
4006 | ||
4007 | default: | |
4008 | ; | |
4009 | } | |
4010 | } | |
4011 | ||
87f0e418 | 4012 | static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) { |
e9fa1bf7 | 4013 | Service *s = ASSERT_PTR(SERVICE(u)); |
5cdabc8d | 4014 | bool notify_dbus = true; |
f42806df | 4015 | ServiceResult f; |
e5123725 | 4016 | ExitClean clean_mode; |
c917a807 | 4017 | int r; |
5cb5a6ff | 4018 | |
034c6ed7 LP |
4019 | assert(pid >= 0); |
4020 | ||
e5123725 AZ |
4021 | /* Oneshot services and non-SERVICE_EXEC_START commands should not be |
4022 | * considered daemons as they are typically not long running. */ | |
c79ab77c | 4023 | if (s->type == SERVICE_ONESHOT || (s->control_pid.pid == pid && s->control_command_id != SERVICE_EXEC_START)) |
e5123725 AZ |
4024 | clean_mode = EXIT_CLEAN_COMMAND; |
4025 | else | |
4026 | clean_mode = EXIT_CLEAN_DAEMON; | |
4027 | ||
4028 | if (is_clean_exit(code, status, clean_mode, &s->success_status)) | |
f42806df LP |
4029 | f = SERVICE_SUCCESS; |
4030 | else if (code == CLD_EXITED) | |
4031 | f = SERVICE_FAILURE_EXIT_CODE; | |
4032 | else if (code == CLD_KILLED) | |
4033 | f = SERVICE_FAILURE_SIGNAL; | |
4034 | else if (code == CLD_DUMPED) | |
4035 | f = SERVICE_FAILURE_CORE_DUMP; | |
d06dacd0 | 4036 | else |
04499a70 | 4037 | assert_not_reached(); |
034c6ed7 | 4038 | |
c79ab77c | 4039 | if (s->main_pid.pid == pid) { |
13bb1ffb ZJS |
4040 | /* Clean up the exec_fd event source. We want to do this here, not later in |
4041 | * service_set_state(), because service_enter_stop_post() calls service_spawn(). | |
4042 | * The source owns its end of the pipe, so this will close that too. */ | |
4043 | s->exec_fd_event_source = sd_event_source_disable_unref(s->exec_fd_event_source); | |
4044 | ||
db01f8b3 MS |
4045 | /* Forking services may occasionally move to a new PID. |
4046 | * As long as they update the PID file before exiting the old | |
4047 | * PID, they're fine. */ | |
db256aab | 4048 | if (service_load_pid_file(s, false) > 0) |
db01f8b3 | 4049 | return; |
034c6ed7 | 4050 | |
c79ab77c | 4051 | pidref_done(&s->main_pid); |
6ea832a2 | 4052 | exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status); |
034c6ed7 | 4053 | |
867b3b7d | 4054 | if (s->main_command) { |
fbeefb45 LP |
4055 | /* If this is not a forking service than the |
4056 | * main process got started and hence we copy | |
4057 | * the exit status so that it is recorded both | |
4058 | * as main and as control process exit | |
4059 | * status */ | |
4060 | ||
867b3b7d | 4061 | s->main_command->exec_status = s->main_exec_status; |
b708e7ce | 4062 | |
3ed0cd26 | 4063 | if (s->main_command->flags & EXEC_COMMAND_IGNORE_FAILURE) |
f42806df | 4064 | f = SERVICE_SUCCESS; |
fbeefb45 LP |
4065 | } else if (s->exec_command[SERVICE_EXEC_START]) { |
4066 | ||
4067 | /* If this is a forked process, then we should | |
4068 | * ignore the return value if this was | |
4069 | * configured for the starter process */ | |
4070 | ||
3ed0cd26 | 4071 | if (s->exec_command[SERVICE_EXEC_START]->flags & EXEC_COMMAND_IGNORE_FAILURE) |
fbeefb45 | 4072 | f = SERVICE_SUCCESS; |
034c6ed7 LP |
4073 | } |
4074 | ||
91bbd9b7 | 4075 | unit_log_process_exit( |
5cc2cd1c | 4076 | u, |
abaf5edd | 4077 | "Main process", |
91bbd9b7 | 4078 | service_exec_command_to_string(SERVICE_EXEC_START), |
5cc2cd1c | 4079 | f == SERVICE_SUCCESS, |
91bbd9b7 | 4080 | code, status); |
f42806df | 4081 | |
a0fef983 | 4082 | if (s->result == SERVICE_SUCCESS) |
f42806df | 4083 | s->result = f; |
034c6ed7 | 4084 | |
867b3b7d LP |
4085 | if (s->main_command && |
4086 | s->main_command->command_next && | |
b58aeb70 | 4087 | s->type == SERVICE_ONESHOT && |
f42806df | 4088 | f == SERVICE_SUCCESS) { |
034c6ed7 | 4089 | |
e78695d4 | 4090 | /* There is another command to execute, so let's do that. */ |
034c6ed7 | 4091 | |
f2341e0a | 4092 | log_unit_debug(u, "Running next main command for state %s.", service_state_to_string(s->state)); |
f42806df | 4093 | service_run_next_main(s); |
034c6ed7 | 4094 | |
34e9ba66 | 4095 | } else { |
867b3b7d | 4096 | s->main_command = NULL; |
34e9ba66 | 4097 | |
3bd28bf7 LP |
4098 | /* Services with ExitType=cgroup do not act on main PID exiting, unless the cgroup is |
4099 | * already empty */ | |
596e4470 HC |
4100 | if (s->exit_type == SERVICE_EXIT_MAIN || cgroup_good(s) <= 0) { |
4101 | /* The service exited, so the service is officially gone. */ | |
4102 | switch (s->state) { | |
4103 | ||
4104 | case SERVICE_START_POST: | |
4105 | case SERVICE_RELOAD: | |
3bd28bf7 LP |
4106 | case SERVICE_RELOAD_SIGNAL: |
4107 | case SERVICE_RELOAD_NOTIFY: | |
dfdeb0b1 | 4108 | case SERVICE_REFRESH_EXTENSIONS: |
5162829e | 4109 | case SERVICE_MOUNTING: |
3bd28bf7 LP |
4110 | /* If neither main nor control processes are running then the current |
4111 | * state can never exit cleanly, hence immediately terminate the | |
4112 | * service. */ | |
596e4470 HC |
4113 | if (control_pid_good(s) <= 0) |
4114 | service_enter_stop(s, f); | |
4115 | ||
4116 | /* Otherwise need to wait until the operation is done. */ | |
4117 | break; | |
bbe19f68 | 4118 | |
596e4470 HC |
4119 | case SERVICE_STOP: |
4120 | /* Need to wait until the operation is done. */ | |
4121 | break; | |
bbe19f68 | 4122 | |
596e4470 HC |
4123 | case SERVICE_START: |
4124 | if (s->type == SERVICE_ONESHOT) { | |
4125 | /* This was our main goal, so let's go on */ | |
4126 | if (f == SERVICE_SUCCESS) | |
4127 | service_enter_start_post(s); | |
4128 | else | |
4129 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); | |
4130 | break; | |
3bd28bf7 | 4131 | } else if (IN_SET(s->type, SERVICE_NOTIFY, SERVICE_NOTIFY_RELOAD)) { |
596e4470 HC |
4132 | /* Only enter running through a notification, so that the |
4133 | * SERVICE_START state signifies that no ready notification | |
4134 | * has been received */ | |
4135 | if (f != SERVICE_SUCCESS) | |
4136 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); | |
19dff691 | 4137 | else if (!s->remain_after_exit || service_get_notify_access(s) == NOTIFY_MAIN) |
596e4470 HC |
4138 | /* The service has never been and will never be active */ |
4139 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_PROTOCOL); | |
4140 | break; | |
4141 | } | |
7d55e835 | 4142 | |
596e4470 HC |
4143 | _fallthrough_; |
4144 | case SERVICE_RUNNING: | |
4145 | service_enter_running(s, f); | |
3d474ef7 | 4146 | break; |
034c6ed7 | 4147 | |
596e4470 HC |
4148 | case SERVICE_STOP_WATCHDOG: |
4149 | case SERVICE_STOP_SIGTERM: | |
4150 | case SERVICE_STOP_SIGKILL: | |
5cb5a6ff | 4151 | |
596e4470 HC |
4152 | if (control_pid_good(s) <= 0) |
4153 | service_enter_stop_post(s, f); | |
5cb5a6ff | 4154 | |
596e4470 HC |
4155 | /* If there is still a control process, wait for that first */ |
4156 | break; | |
34e9ba66 | 4157 | |
596e4470 | 4158 | case SERVICE_STOP_POST: |
c1566ef0 | 4159 | |
596e4470 HC |
4160 | if (control_pid_good(s) <= 0) |
4161 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, f); | |
c1566ef0 | 4162 | |
596e4470 | 4163 | break; |
c1566ef0 | 4164 | |
596e4470 HC |
4165 | case SERVICE_FINAL_WATCHDOG: |
4166 | case SERVICE_FINAL_SIGTERM: | |
4167 | case SERVICE_FINAL_SIGKILL: | |
bf108e55 | 4168 | |
596e4470 HC |
4169 | if (control_pid_good(s) <= 0) |
4170 | service_enter_dead(s, f, true); | |
4171 | break; | |
bf108e55 | 4172 | |
596e4470 HC |
4173 | default: |
4174 | assert_not_reached(); | |
4175 | } | |
1651ce09 MY |
4176 | } else if (s->exit_type == SERVICE_EXIT_CGROUP && s->state == SERVICE_START && |
4177 | !IN_SET(s->type, SERVICE_NOTIFY, SERVICE_NOTIFY_RELOAD, SERVICE_DBUS)) | |
ef430065 FT |
4178 | /* If a main process exits very quickly, this function might be executed |
4179 | * before service_dispatch_exec_io(). Since this function disabled IO events | |
4180 | * to monitor the main process above, we need to update the state here too. | |
1651ce09 MY |
4181 | * Let's consider the process is successfully launched and exited, but |
4182 | * only when we're not expecting a readiness notification or dbus name. */ | |
ef430065 | 4183 | service_enter_start_post(s); |
034c6ed7 | 4184 | } |
5cb5a6ff | 4185 | |
c79ab77c | 4186 | } else if (s->control_pid.pid == pid) { |
58441bc1 ZJS |
4187 | const char *kind; |
4188 | bool success; | |
4189 | ||
c79ab77c | 4190 | pidref_done(&s->control_pid); |
34e9ba66 | 4191 | |
b708e7ce | 4192 | if (s->control_command) { |
8bb2d17d | 4193 | exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status); |
a16e1123 | 4194 | |
3ed0cd26 | 4195 | if (s->control_command->flags & EXEC_COMMAND_IGNORE_FAILURE) |
f42806df | 4196 | f = SERVICE_SUCCESS; |
b708e7ce LP |
4197 | } |
4198 | ||
42e6f549 AZ |
4199 | /* ExecCondition= calls that exit with (0, 254] should invoke skip-like behavior instead of failing */ |
4200 | if (s->state == SERVICE_CONDITION) { | |
4201 | if (f == SERVICE_FAILURE_EXIT_CODE && status < 255) { | |
4202 | UNIT(s)->condition_result = false; | |
4203 | f = SERVICE_SKIP_CONDITION; | |
58441bc1 ZJS |
4204 | success = true; |
4205 | } else if (f == SERVICE_SUCCESS) { | |
42e6f549 | 4206 | UNIT(s)->condition_result = true; |
58441bc1 ZJS |
4207 | success = true; |
4208 | } else | |
4209 | success = false; | |
4210 | ||
4211 | kind = "Condition check process"; | |
4212 | } else { | |
4213 | kind = "Control process"; | |
4214 | success = f == SERVICE_SUCCESS; | |
42e6f549 AZ |
4215 | } |
4216 | ||
91bbd9b7 | 4217 | unit_log_process_exit( |
5cc2cd1c | 4218 | u, |
58441bc1 | 4219 | kind, |
91bbd9b7 | 4220 | service_exec_command_to_string(s->control_command_id), |
58441bc1 | 4221 | success, |
91bbd9b7 | 4222 | code, status); |
f42806df | 4223 | |
5162829e | 4224 | if (!IN_SET(s->state, SERVICE_RELOAD, SERVICE_MOUNTING) && s->result == SERVICE_SUCCESS) |
f42806df | 4225 | s->result = f; |
034c6ed7 | 4226 | |
34e9ba66 LP |
4227 | if (s->control_command && |
4228 | s->control_command->command_next && | |
f42806df | 4229 | f == SERVICE_SUCCESS) { |
034c6ed7 | 4230 | |
81006ebb | 4231 | /* There is another command to execute, so let's do that. */ |
034c6ed7 | 4232 | |
f2341e0a | 4233 | log_unit_debug(u, "Running next control command for state %s.", service_state_to_string(s->state)); |
f42806df | 4234 | service_run_next_control(s); |
034c6ed7 | 4235 | |
80876c20 | 4236 | } else { |
3bd28bf7 | 4237 | /* No further commands for this step, so let's figure out what to do next */ |
034c6ed7 | 4238 | |
a16e1123 LP |
4239 | s->control_command = NULL; |
4240 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
4241 | ||
f2341e0a | 4242 | log_unit_debug(u, "Got final SIGCHLD for state %s.", service_state_to_string(s->state)); |
bd982a8b | 4243 | |
034c6ed7 LP |
4244 | switch (s->state) { |
4245 | ||
31cd5f63 AZ |
4246 | case SERVICE_CONDITION: |
4247 | if (f == SERVICE_SUCCESS) | |
4248 | service_enter_start_pre(s); | |
4249 | else | |
4250 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); | |
4251 | break; | |
4252 | ||
034c6ed7 | 4253 | case SERVICE_START_PRE: |
f42806df | 4254 | if (f == SERVICE_SUCCESS) |
034c6ed7 LP |
4255 | service_enter_start(s); |
4256 | else | |
c3fda31d | 4257 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); |
034c6ed7 LP |
4258 | break; |
4259 | ||
4260 | case SERVICE_START: | |
bfba3256 LP |
4261 | if (s->type != SERVICE_FORKING) |
4262 | /* Maybe spurious event due to a reload that changed the type? */ | |
4263 | break; | |
034c6ed7 | 4264 | |
f42806df | 4265 | if (f != SERVICE_SUCCESS) { |
c3fda31d | 4266 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); |
3a111838 MS |
4267 | break; |
4268 | } | |
034c6ed7 | 4269 | |
3a111838 | 4270 | if (s->pid_file) { |
f42806df | 4271 | bool has_start_post; |
f42806df | 4272 | |
3a111838 MS |
4273 | /* Let's try to load the pid file here if we can. |
4274 | * The PID file might actually be created by a START_POST | |
4275 | * script. In that case don't worry if the loading fails. */ | |
f42806df | 4276 | |
5d904a6a | 4277 | has_start_post = s->exec_command[SERVICE_EXEC_START_POST]; |
f42806df | 4278 | r = service_load_pid_file(s, !has_start_post); |
3a111838 MS |
4279 | if (!has_start_post && r < 0) { |
4280 | r = service_demand_pid_file(s); | |
b13ddbbc | 4281 | if (r < 0 || cgroup_good(s) == 0) |
c3fda31d | 4282 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_PROTOCOL); |
3a111838 MS |
4283 | break; |
4284 | } | |
034c6ed7 | 4285 | } else |
783e05d6 | 4286 | service_search_main_pid(s); |
034c6ed7 | 4287 | |
3a111838 | 4288 | service_enter_start_post(s); |
034c6ed7 LP |
4289 | break; |
4290 | ||
4291 | case SERVICE_START_POST: | |
f42806df | 4292 | if (f != SERVICE_SUCCESS) { |
ce359e98 | 4293 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); |
2096e009 | 4294 | break; |
034c6ed7 LP |
4295 | } |
4296 | ||
2096e009 | 4297 | if (s->pid_file) { |
f42806df | 4298 | r = service_load_pid_file(s, true); |
2096e009 MS |
4299 | if (r < 0) { |
4300 | r = service_demand_pid_file(s); | |
b13ddbbc | 4301 | if (r < 0 || cgroup_good(s) == 0) |
c35755fb | 4302 | service_enter_stop(s, SERVICE_FAILURE_PROTOCOL); |
2096e009 MS |
4303 | break; |
4304 | } | |
4305 | } else | |
783e05d6 | 4306 | service_search_main_pid(s); |
2096e009 | 4307 | |
f42806df | 4308 | service_enter_running(s, SERVICE_SUCCESS); |
3185a36b | 4309 | break; |
034c6ed7 LP |
4310 | |
4311 | case SERVICE_RELOAD: | |
3bd28bf7 LP |
4312 | case SERVICE_RELOAD_SIGNAL: |
4313 | case SERVICE_RELOAD_NOTIFY: | |
7236ce6e ZJS |
4314 | if (f == SERVICE_SUCCESS) |
4315 | if (service_load_pid_file(s, true) < 0) | |
4316 | service_search_main_pid(s); | |
3185a36b | 4317 | |
f42806df | 4318 | s->reload_result = f; |
3bd28bf7 | 4319 | |
d09df6b9 | 4320 | /* If the last notification we received from the service process indicates |
3bd28bf7 LP |
4321 | * we are still reloading, then don't leave reloading state just yet, just |
4322 | * transition into SERVICE_RELOAD_NOTIFY, to wait for the READY=1 coming, | |
4323 | * too. */ | |
4324 | if (s->notify_state == NOTIFY_RELOADING) | |
4325 | service_set_state(s, SERVICE_RELOAD_NOTIFY); | |
4326 | else | |
4327 | service_enter_running(s, SERVICE_SUCCESS); | |
034c6ed7 LP |
4328 | break; |
4329 | ||
dfdeb0b1 | 4330 | case SERVICE_REFRESH_EXTENSIONS: |
4331 | /* Remounting extensions asynchronously done, proceed to signal */ | |
4332 | service_enter_reload_signal_exec(s); | |
4333 | break; | |
4334 | ||
a53e92a1 | 4335 | case SERVICE_MOUNTING: |
a6eeca9a | 4336 | service_live_mount_finish(s, f, SD_BUS_ERROR_FAILED); |
a53e92a1 MY |
4337 | |
4338 | service_enter_running(s, SERVICE_SUCCESS); | |
4339 | break; | |
4340 | ||
034c6ed7 | 4341 | case SERVICE_STOP: |
f42806df | 4342 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); |
034c6ed7 LP |
4343 | break; |
4344 | ||
c87700a1 | 4345 | case SERVICE_STOP_WATCHDOG: |
034c6ed7 LP |
4346 | case SERVICE_STOP_SIGTERM: |
4347 | case SERVICE_STOP_SIGKILL: | |
4348 | if (main_pid_good(s) <= 0) | |
f42806df | 4349 | service_enter_stop_post(s, f); |
034c6ed7 | 4350 | |
846a07b5 | 4351 | /* If there is still a service process around, wait until |
034c6ed7 LP |
4352 | * that one quit, too */ |
4353 | break; | |
4354 | ||
4355 | case SERVICE_STOP_POST: | |
c1566ef0 AZ |
4356 | if (main_pid_good(s) <= 0) |
4357 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, f); | |
4358 | break; | |
4359 | ||
bf760801 | 4360 | case SERVICE_FINAL_WATCHDOG: |
034c6ed7 LP |
4361 | case SERVICE_FINAL_SIGTERM: |
4362 | case SERVICE_FINAL_SIGKILL: | |
bf108e55 LP |
4363 | if (main_pid_good(s) <= 0) |
4364 | service_enter_dead(s, f, true); | |
034c6ed7 LP |
4365 | break; |
4366 | ||
4c2f5842 LP |
4367 | case SERVICE_CLEANING: |
4368 | ||
4369 | if (s->clean_result == SERVICE_SUCCESS) | |
4370 | s->clean_result = f; | |
4371 | ||
4372 | service_enter_dead(s, SERVICE_SUCCESS, false); | |
4373 | break; | |
4374 | ||
034c6ed7 | 4375 | default: |
04499a70 | 4376 | assert_not_reached(); |
034c6ed7 LP |
4377 | } |
4378 | } | |
5cdabc8d LP |
4379 | } else /* Neither control nor main PID? If so, don't notify about anything */ |
4380 | notify_dbus = false; | |
c4e2ceae LP |
4381 | |
4382 | /* Notify clients about changed exit status */ | |
5cdabc8d LP |
4383 | if (notify_dbus) |
4384 | unit_add_to_dbus_queue(u); | |
034c6ed7 LP |
4385 | } |
4386 | ||
718db961 | 4387 | static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) { |
e9fa1bf7 | 4388 | Service *s = ASSERT_PTR(SERVICE(userdata)); |
034c6ed7 | 4389 | |
718db961 | 4390 | assert(source == s->timer_event_source); |
034c6ed7 LP |
4391 | |
4392 | switch (s->state) { | |
4393 | ||
31cd5f63 | 4394 | case SERVICE_CONDITION: |
034c6ed7 LP |
4395 | case SERVICE_START_PRE: |
4396 | case SERVICE_START: | |
4397 | case SERVICE_START_POST: | |
bf760801 JK |
4398 | switch (s->timeout_start_failure_mode) { |
4399 | ||
4400 | case SERVICE_TIMEOUT_TERMINATE: | |
4401 | log_unit_warning(UNIT(s), "%s operation timed out. Terminating.", service_state_to_string(s->state)); | |
4402 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT); | |
4403 | break; | |
4404 | ||
4405 | case SERVICE_TIMEOUT_ABORT: | |
4406 | log_unit_warning(UNIT(s), "%s operation timed out. Aborting.", service_state_to_string(s->state)); | |
4407 | service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_TIMEOUT); | |
4408 | break; | |
4409 | ||
4410 | case SERVICE_TIMEOUT_KILL: | |
4411 | if (s->kill_context.send_sigkill) { | |
4412 | log_unit_warning(UNIT(s), "%s operation timed out. Killing.", service_state_to_string(s->state)); | |
4413 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT); | |
4414 | } else { | |
4415 | log_unit_warning(UNIT(s), "%s operation timed out. Skipping SIGKILL.", service_state_to_string(s->state)); | |
4416 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); | |
4417 | } | |
4418 | break; | |
4419 | ||
4420 | default: | |
04499a70 | 4421 | assert_not_reached(); |
bf760801 | 4422 | } |
034c6ed7 LP |
4423 | break; |
4424 | ||
36c16a7c LP |
4425 | case SERVICE_RUNNING: |
4426 | log_unit_warning(UNIT(s), "Service reached runtime time limit. Stopping."); | |
4427 | service_enter_stop(s, SERVICE_FAILURE_TIMEOUT); | |
4428 | break; | |
4429 | ||
e2f3b44c | 4430 | case SERVICE_RELOAD: |
3bd28bf7 LP |
4431 | case SERVICE_RELOAD_SIGNAL: |
4432 | case SERVICE_RELOAD_NOTIFY: | |
dfdeb0b1 | 4433 | case SERVICE_REFRESH_EXTENSIONS: |
089b64d5 | 4434 | log_unit_warning(UNIT(s), "Reload operation timed out. Killing reload process."); |
e9a4f676 | 4435 | service_kill_control_process(s); |
f42806df LP |
4436 | s->reload_result = SERVICE_FAILURE_TIMEOUT; |
4437 | service_enter_running(s, SERVICE_SUCCESS); | |
e2f3b44c LP |
4438 | break; |
4439 | ||
5162829e LB |
4440 | case SERVICE_MOUNTING: |
4441 | log_unit_warning(UNIT(s), "Mount operation timed out. Killing mount process."); | |
4442 | service_kill_control_process(s); | |
a6eeca9a | 4443 | service_live_mount_finish(s, SERVICE_FAILURE_TIMEOUT, SD_BUS_ERROR_TIMEOUT); |
5162829e LB |
4444 | service_enter_running(s, SERVICE_SUCCESS); |
4445 | break; | |
4446 | ||
034c6ed7 | 4447 | case SERVICE_STOP: |
bf760801 JK |
4448 | switch (s->timeout_stop_failure_mode) { |
4449 | ||
4450 | case SERVICE_TIMEOUT_TERMINATE: | |
4451 | log_unit_warning(UNIT(s), "Stopping timed out. Terminating."); | |
4452 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT); | |
4453 | break; | |
4454 | ||
4455 | case SERVICE_TIMEOUT_ABORT: | |
4456 | log_unit_warning(UNIT(s), "Stopping timed out. Aborting."); | |
4457 | service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_TIMEOUT); | |
4458 | break; | |
4459 | ||
4460 | case SERVICE_TIMEOUT_KILL: | |
4461 | if (s->kill_context.send_sigkill) { | |
4462 | log_unit_warning(UNIT(s), "Stopping timed out. Killing."); | |
4463 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT); | |
4464 | } else { | |
4465 | log_unit_warning(UNIT(s), "Stopping timed out. Skipping SIGKILL."); | |
4466 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); | |
4467 | } | |
4468 | break; | |
4469 | ||
4470 | default: | |
04499a70 | 4471 | assert_not_reached(); |
bf760801 | 4472 | } |
034c6ed7 LP |
4473 | break; |
4474 | ||
c87700a1 | 4475 | case SERVICE_STOP_WATCHDOG: |
bf760801 JK |
4476 | if (s->kill_context.send_sigkill) { |
4477 | log_unit_warning(UNIT(s), "State 'stop-watchdog' timed out. Killing."); | |
4478 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT); | |
4479 | } else { | |
4480 | log_unit_warning(UNIT(s), "State 'stop-watchdog' timed out. Skipping SIGKILL."); | |
4481 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); | |
4482 | } | |
db2cb23b UTL |
4483 | break; |
4484 | ||
034c6ed7 | 4485 | case SERVICE_STOP_SIGTERM: |
bf760801 JK |
4486 | if (s->timeout_stop_failure_mode == SERVICE_TIMEOUT_ABORT) { |
4487 | log_unit_warning(UNIT(s), "State 'stop-sigterm' timed out. Aborting."); | |
4488 | service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_TIMEOUT); | |
4489 | } else if (s->kill_context.send_sigkill) { | |
f2341e0a | 4490 | log_unit_warning(UNIT(s), "State 'stop-sigterm' timed out. Killing."); |
f42806df | 4491 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT); |
ba035df2 | 4492 | } else { |
f2341e0a | 4493 | log_unit_warning(UNIT(s), "State 'stop-sigterm' timed out. Skipping SIGKILL."); |
f42806df | 4494 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); |
ba035df2 LP |
4495 | } |
4496 | ||
034c6ed7 LP |
4497 | break; |
4498 | ||
4499 | case SERVICE_STOP_SIGKILL: | |
35b8ca3a | 4500 | /* Uh, we sent a SIGKILL and it is still not gone? |
034c6ed7 LP |
4501 | * Must be something we cannot kill, so let's just be |
4502 | * weirded out and continue */ | |
4503 | ||
f2341e0a | 4504 | log_unit_warning(UNIT(s), "Processes still around after SIGKILL. Ignoring."); |
f42806df | 4505 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); |
034c6ed7 LP |
4506 | break; |
4507 | ||
4508 | case SERVICE_STOP_POST: | |
bf760801 JK |
4509 | switch (s->timeout_stop_failure_mode) { |
4510 | ||
4511 | case SERVICE_TIMEOUT_TERMINATE: | |
4512 | log_unit_warning(UNIT(s), "State 'stop-post' timed out. Terminating."); | |
4513 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT); | |
4514 | break; | |
4515 | ||
4516 | case SERVICE_TIMEOUT_ABORT: | |
4517 | log_unit_warning(UNIT(s), "State 'stop-post' timed out. Aborting."); | |
4518 | service_enter_signal(s, SERVICE_FINAL_WATCHDOG, SERVICE_FAILURE_TIMEOUT); | |
4519 | break; | |
4520 | ||
4521 | case SERVICE_TIMEOUT_KILL: | |
4522 | if (s->kill_context.send_sigkill) { | |
4523 | log_unit_warning(UNIT(s), "State 'stop-post' timed out. Killing."); | |
4524 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT); | |
4525 | } else { | |
4526 | log_unit_warning(UNIT(s), "State 'stop-post' timed out. Skipping SIGKILL. Entering failed mode."); | |
4527 | service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false); | |
4528 | } | |
4529 | break; | |
4530 | ||
4531 | default: | |
04499a70 | 4532 | assert_not_reached(); |
bf760801 | 4533 | } |
034c6ed7 LP |
4534 | break; |
4535 | ||
bf760801 | 4536 | case SERVICE_FINAL_WATCHDOG: |
4819ff03 | 4537 | if (s->kill_context.send_sigkill) { |
bf760801 | 4538 | log_unit_warning(UNIT(s), "State 'final-watchdog' timed out. Killing."); |
f42806df | 4539 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT); |
ba035df2 | 4540 | } else { |
bf760801 JK |
4541 | log_unit_warning(UNIT(s), "State 'final-watchdog' timed out. Skipping SIGKILL. Entering failed mode."); |
4542 | service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false); | |
4543 | } | |
4544 | break; | |
4545 | ||
4546 | case SERVICE_FINAL_SIGTERM: | |
4547 | if (s->timeout_stop_failure_mode == SERVICE_TIMEOUT_ABORT) { | |
4548 | log_unit_warning(UNIT(s), "State 'final-sigterm' timed out. Aborting."); | |
4549 | service_enter_signal(s, SERVICE_FINAL_WATCHDOG, SERVICE_FAILURE_TIMEOUT); | |
4550 | } else if (s->kill_context.send_sigkill) { | |
4551 | log_unit_warning(UNIT(s), "State 'final-sigterm' timed out. Killing."); | |
4552 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT); | |
4553 | } else { | |
4554 | log_unit_warning(UNIT(s), "State 'final-sigterm' timed out. Skipping SIGKILL. Entering failed mode."); | |
f42806df | 4555 | service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false); |
ba035df2 LP |
4556 | } |
4557 | ||
034c6ed7 LP |
4558 | break; |
4559 | ||
4560 | case SERVICE_FINAL_SIGKILL: | |
f2341e0a | 4561 | log_unit_warning(UNIT(s), "Processes still around after final SIGKILL. Entering failed mode."); |
f42806df | 4562 | service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true); |
034c6ed7 LP |
4563 | break; |
4564 | ||
4565 | case SERVICE_AUTO_RESTART: | |
5291f26d | 4566 | if (s->restart_usec > 0) |
868f7d36 | 4567 | log_unit_debug(UNIT(s), |
be1adc27 | 4568 | "Service restart interval %s expired, scheduling restart.", |
5171356e | 4569 | FORMAT_TIMESPAN(service_restart_usec_next(s), USEC_PER_SEC)); |
5291f26d | 4570 | else |
868f7d36 ZJS |
4571 | log_unit_debug(UNIT(s), |
4572 | "Service has no hold-off time (RestartSec=0), scheduling restart."); | |
5ce6e7f5 | 4573 | |
1391f149 | 4574 | service_enter_restart(s, /* shortcut = */ false); |
034c6ed7 LP |
4575 | break; |
4576 | ||
4c2f5842 LP |
4577 | case SERVICE_CLEANING: |
4578 | log_unit_warning(UNIT(s), "Cleaning timed out. killing."); | |
4579 | ||
4580 | if (s->clean_result == SERVICE_SUCCESS) | |
4581 | s->clean_result = SERVICE_FAILURE_TIMEOUT; | |
4582 | ||
4583 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, 0); | |
4584 | break; | |
4585 | ||
034c6ed7 | 4586 | default: |
04499a70 | 4587 | assert_not_reached(); |
034c6ed7 | 4588 | } |
718db961 LP |
4589 | |
4590 | return 0; | |
4591 | } | |
4592 | ||
4593 | static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata) { | |
e9fa1bf7 | 4594 | Service *s = ASSERT_PTR(SERVICE(userdata)); |
2787d83c | 4595 | usec_t watchdog_usec; |
718db961 | 4596 | |
718db961 LP |
4597 | assert(source == s->watchdog_event_source); |
4598 | ||
2787d83c M |
4599 | watchdog_usec = service_get_watchdog_usec(s); |
4600 | ||
2a12e32e JK |
4601 | if (UNIT(s)->manager->service_watchdogs) { |
4602 | log_unit_error(UNIT(s), "Watchdog timeout (limit %s)!", | |
5291f26d | 4603 | FORMAT_TIMESPAN(watchdog_usec, 1)); |
8bb2d17d | 4604 | |
c87700a1 | 4605 | service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_WATCHDOG); |
2a12e32e JK |
4606 | } else |
4607 | log_unit_warning(UNIT(s), "Watchdog disabled! Ignoring watchdog timeout (limit %s)!", | |
5291f26d | 4608 | FORMAT_TIMESPAN(watchdog_usec, 1)); |
842129f5 | 4609 | |
718db961 | 4610 | return 0; |
5cb5a6ff LP |
4611 | } |
4612 | ||
3a45fdfc | 4613 | static void service_force_watchdog(Service *s) { |
e9fa1bf7 MY |
4614 | assert(s); |
4615 | ||
3a45fdfc MY |
4616 | if (!UNIT(s)->manager->service_watchdogs) |
4617 | return; | |
4618 | ||
4619 | log_unit_error(UNIT(s), "Watchdog request (last status: %s)!", | |
4620 | s->status_text ?: "<unset>"); | |
4621 | ||
4622 | service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_WATCHDOG); | |
4623 | } | |
4624 | ||
19eccb76 | 4625 | static bool service_notify_message_authorized(Service *s, PidRef *pid) { |
e3285237 | 4626 | assert(s); |
19eccb76 | 4627 | assert(pidref_is_set(pid)); |
8c47c732 | 4628 | |
d3d0e361 | 4629 | switch (service_get_notify_access(s)) { |
19dff691 | 4630 | |
d3d0e361 | 4631 | case NOTIFY_NONE: |
5734d5d0 | 4632 | /* Warn level only if no notifications are expected */ |
19eccb76 | 4633 | log_unit_warning(UNIT(s), "Got notification message from PID "PID_FMT", but reception is disabled", pid->pid); |
e3285237 | 4634 | return false; |
e3285237 | 4635 | |
d3d0e361 MY |
4636 | case NOTIFY_ALL: |
4637 | return true; | |
4638 | ||
4639 | case NOTIFY_MAIN: | |
19eccb76 | 4640 | if (pidref_equal(pid, &s->main_pid)) |
d3d0e361 MY |
4641 | return true; |
4642 | ||
c79ab77c | 4643 | if (pidref_is_set(&s->main_pid)) |
19eccb76 | 4644 | log_unit_debug(UNIT(s), "Got notification message from PID "PID_FMT", but reception only permitted for main PID "PID_FMT, pid->pid, s->main_pid.pid); |
336c6e46 | 4645 | else |
19eccb76 | 4646 | log_unit_debug(UNIT(s), "Got notification message from PID "PID_FMT", but reception only permitted for main PID which is currently not known", pid->pid); |
e3285237 LP |
4647 | |
4648 | return false; | |
e3285237 | 4649 | |
d3d0e361 | 4650 | case NOTIFY_EXEC: |
19eccb76 | 4651 | if (pidref_equal(pid, &s->main_pid) || pidref_equal(pid, &s->control_pid)) |
d3d0e361 MY |
4652 | return true; |
4653 | ||
c79ab77c | 4654 | if (pidref_is_set(&s->main_pid) && pidref_is_set(&s->control_pid)) |
5734d5d0 | 4655 | log_unit_debug(UNIT(s), "Got notification message from PID "PID_FMT", but reception only permitted for main PID "PID_FMT" and control PID "PID_FMT, |
19eccb76 | 4656 | pid->pid, s->main_pid.pid, s->control_pid.pid); |
c79ab77c | 4657 | else if (pidref_is_set(&s->main_pid)) |
19eccb76 | 4658 | log_unit_debug(UNIT(s), "Got notification message from PID "PID_FMT", but reception only permitted for main PID "PID_FMT, pid->pid, s->main_pid.pid); |
c79ab77c | 4659 | else if (pidref_is_set(&s->control_pid)) |
19eccb76 | 4660 | log_unit_debug(UNIT(s), "Got notification message from PID "PID_FMT", but reception only permitted for control PID "PID_FMT, pid->pid, s->control_pid.pid); |
6375bd20 | 4661 | else |
19eccb76 | 4662 | log_unit_debug(UNIT(s), "Got notification message from PID "PID_FMT", but reception only permitted for main PID and control PID which are currently not known", pid->pid); |
e3285237 LP |
4663 | |
4664 | return false; | |
9711848f | 4665 | |
d3d0e361 MY |
4666 | default: |
4667 | assert_not_reached(); | |
4668 | } | |
e3285237 LP |
4669 | } |
4670 | ||
695323d9 MY |
4671 | static int service_notify_message_parse_new_pid( |
4672 | Unit *u, | |
4673 | char * const *tags, | |
4674 | FDSet *fds, | |
4675 | PidRef *ret) { | |
4676 | ||
4677 | _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; | |
4678 | const char *e; | |
4679 | int r; | |
4680 | ||
4681 | assert(u); | |
4682 | assert(ret); | |
4683 | ||
4684 | /* MAINPIDFD=1 always takes precedence */ | |
4685 | if (strv_contains(tags, "MAINPIDFD=1")) { | |
4686 | unsigned n_fds = fdset_size(fds); | |
4687 | if (n_fds != 1) | |
4688 | return log_unit_warning_errno(u, SYNTHETIC_ERRNO(EINVAL), | |
4689 | "Got MAINPIDFD=1 with %s fd, ignoring.", n_fds == 0 ? "no" : "more than one"); | |
4690 | ||
4691 | r = pidref_set_pidfd_consume(&pidref, ASSERT_FD(fdset_steal_first(fds))); | |
4692 | if (r < 0) | |
4693 | return log_unit_warning_errno(u, r, "Failed to create reference to received new main pidfd: %m"); | |
4694 | ||
4695 | goto finish; | |
4696 | } | |
4697 | ||
4698 | e = strv_find_startswith(tags, "MAINPID="); | |
4699 | if (!e) { | |
4700 | *ret = PIDREF_NULL; | |
4701 | return 0; | |
4702 | } | |
4703 | ||
4704 | r = pidref_set_pidstr(&pidref, e); | |
4705 | if (r < 0) | |
4706 | return log_unit_warning_errno(u, r, "Failed to parse MAINPID=%s field in notification message, ignoring: %m", e); | |
4707 | ||
4708 | e = strv_find_startswith(tags, "MAINPIDFDID="); | |
4709 | if (!e) | |
4710 | goto finish; | |
4711 | ||
4712 | uint64_t pidfd_id; | |
4713 | ||
4714 | r = safe_atou64(e, &pidfd_id); | |
4715 | if (r < 0) | |
4716 | return log_unit_warning_errno(u, r, "Failed to parse MAINPIDFDID= in notification message, refusing: %s", e); | |
4717 | ||
4718 | r = pidref_acquire_pidfd_id(&pidref); | |
4719 | if (r < 0) { | |
4720 | if (!ERRNO_IS_NEG_NOT_SUPPORTED(r)) | |
4721 | log_unit_warning_errno(u, r, | |
4722 | "Failed to acquire pidfd id of process " PID_FMT ", not validating MAINPIDFDID=%" PRIu64 ": %m", | |
4723 | pidref.pid, pidfd_id); | |
4724 | goto finish; | |
4725 | } | |
4726 | ||
4727 | if (pidref.fd_id != pidfd_id) | |
4728 | return log_unit_warning_errno(u, SYNTHETIC_ERRNO(ESRCH), | |
4729 | "PIDFD ID of process " PID_FMT " (%" PRIu64 ") mismatches with received MAINPIDFDID=%" PRIu64 ", not changing main PID.", | |
4730 | pidref.pid, pidref.fd_id, pidfd_id); | |
4731 | ||
4732 | finish: | |
4733 | *ret = TAKE_PIDREF(pidref); | |
4734 | return 1; | |
4735 | } | |
4736 | ||
db256aab LP |
4737 | static void service_notify_message( |
4738 | Unit *u, | |
19eccb76 | 4739 | PidRef *pidref, |
db256aab | 4740 | const struct ucred *ucred, |
fcee2755 | 4741 | char * const *tags, |
db256aab LP |
4742 | FDSet *fds) { |
4743 | ||
b7a22068 | 4744 | Service *s = ASSERT_PTR(SERVICE(u)); |
db256aab | 4745 | int r; |
e3285237 | 4746 | |
19eccb76 | 4747 | assert(pidref_is_set(pidref)); |
db256aab | 4748 | assert(ucred); |
e3285237 | 4749 | |
19eccb76 | 4750 | if (!service_notify_message_authorized(s, pidref)) |
e3285237 LP |
4751 | return; |
4752 | ||
f1d34068 | 4753 | if (DEBUG_LOGGING) { |
b7a22068 | 4754 | _cleanup_free_ char *cc = strv_join(tags, ", "); |
19eccb76 | 4755 | log_unit_debug(u, "Got notification message from PID "PID_FMT": %s", pidref->pid, empty_to_na(cc)); |
9711848f | 4756 | } |
c952c6ec | 4757 | |
b7a22068 MY |
4758 | usec_t monotonic_usec = USEC_INFINITY; |
4759 | bool notify_dbus = false; | |
4760 | const char *e; | |
4761 | ||
695323d9 MY |
4762 | /* Interpret MAINPID= (+ MAINPIDFDID=) / MAINPIDFD=1 */ |
4763 | _cleanup_(pidref_done) PidRef new_main_pid = PIDREF_NULL; | |
31197c68 | 4764 | |
695323d9 MY |
4765 | r = service_notify_message_parse_new_pid(u, tags, fds, &new_main_pid); |
4766 | if (r > 0 && | |
4767 | IN_SET(s->state, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, | |
dfdeb0b1 | 4768 | SERVICE_RELOAD, SERVICE_RELOAD_SIGNAL, SERVICE_RELOAD_NOTIFY, SERVICE_REFRESH_EXTENSIONS, |
695323d9 MY |
4769 | SERVICE_STOP, SERVICE_STOP_SIGTERM) && |
4770 | (!s->main_pid_known || !pidref_equal(&new_main_pid, &s->main_pid))) { | |
db256aab | 4771 | |
695323d9 MY |
4772 | r = service_is_suitable_main_pid(s, &new_main_pid, LOG_WARNING); |
4773 | if (r == 0) { | |
4774 | /* The new main PID is a bit suspicious, which is OK if the sender is privileged. */ | |
db256aab | 4775 | |
695323d9 MY |
4776 | if (ucred->uid == 0) { |
4777 | log_unit_debug(u, "New main PID "PID_FMT" does not belong to service, but we'll accept it as the request to change it came from a privileged process.", new_main_pid.pid); | |
4778 | r = 1; | |
4779 | } else | |
4780 | log_unit_warning(u, "New main PID "PID_FMT" does not belong to service, refusing.", new_main_pid.pid); | |
4781 | } | |
4782 | if (r > 0) { | |
4783 | (void) service_set_main_pidref(s, TAKE_PIDREF(new_main_pid), /* start_timestamp = */ NULL); | |
cdc2af3e | 4784 | |
695323d9 MY |
4785 | r = unit_watch_pidref(UNIT(s), &s->main_pid, /* exclusive= */ false); |
4786 | if (r < 0) | |
4787 | log_unit_warning_errno(UNIT(s), r, "Failed to watch new main PID "PID_FMT" for service: %m", s->main_pid.pid); | |
cdc2af3e | 4788 | |
695323d9 | 4789 | notify_dbus = true; |
8c47c732 LP |
4790 | } |
4791 | } | |
4792 | ||
3bd28bf7 LP |
4793 | /* Parse MONOTONIC_USEC= */ |
4794 | e = strv_find_startswith(tags, "MONOTONIC_USEC="); | |
4795 | if (e) { | |
4796 | r = safe_atou64(e, &monotonic_usec); | |
4797 | if (r < 0) | |
4798 | log_unit_warning_errno(u, r, "Failed to parse MONOTONIC_USEC= field in notification message, ignoring: %s", e); | |
4799 | } | |
308d72dc | 4800 | |
3bd28bf7 LP |
4801 | /* Interpret READY=/STOPPING=/RELOADING=. STOPPING= wins over the others, and READY= over RELOADING= */ |
4802 | if (strv_contains(tags, "STOPPING=1")) { | |
4803 | s->notify_state = NOTIFY_STOPPING; | |
308d72dc | 4804 | |
dfdeb0b1 | 4805 | if (IN_SET(s->state, SERVICE_RUNNING, SERVICE_RELOAD_SIGNAL, SERVICE_RELOAD_NOTIFY, SERVICE_REFRESH_EXTENSIONS)) |
3bd28bf7 | 4806 | service_enter_stop_by_notify(s); |
308d72dc | 4807 | |
3bd28bf7 | 4808 | notify_dbus = true; |
308d72dc | 4809 | |
3bd28bf7 | 4810 | } else if (strv_contains(tags, "READY=1")) { |
308d72dc | 4811 | |
3bd28bf7 | 4812 | s->notify_state = NOTIFY_READY; |
308d72dc | 4813 | |
c337a130 MY |
4814 | /* Combined RELOADING=1 and READY=1? Then this is indication that the service started and |
4815 | * immediately finished reloading. */ | |
4816 | if (strv_contains(tags, "RELOADING=1")) { | |
4817 | if (s->state == SERVICE_RELOAD_SIGNAL && | |
4818 | monotonic_usec != USEC_INFINITY && | |
4819 | monotonic_usec >= s->reload_begin_usec) | |
4820 | /* Valid Type=notify-reload protocol? Then we're all good. */ | |
4821 | service_enter_running(s, SERVICE_SUCCESS); | |
4822 | ||
4823 | else if (s->state == SERVICE_RUNNING) { | |
4824 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
4825 | ||
4826 | /* Propagate a reload explicitly for plain RELOADING=1 (semantically equivalent to | |
dfdeb0b1 | 4827 | * service_enter_reload_mounting() call in below) */ |
c337a130 MY |
4828 | r = manager_propagate_reload(UNIT(s)->manager, UNIT(s), JOB_FAIL, &error); |
4829 | if (r < 0) | |
4830 | log_unit_warning(UNIT(s), "Failed to schedule propagation of reload, ignoring: %s", | |
4831 | bus_error_message(&error, r)); | |
4832 | } | |
4833 | } | |
4834 | ||
4835 | /* Type=notify(-reload) services inform us about completed initialization with READY=1 */ | |
3bd28bf7 LP |
4836 | if (IN_SET(s->type, SERVICE_NOTIFY, SERVICE_NOTIFY_RELOAD) && |
4837 | s->state == SERVICE_START) | |
4838 | service_enter_start_post(s); | |
308d72dc | 4839 | |
3bd28bf7 LP |
4840 | /* Sending READY=1 while we are reloading informs us that the reloading is complete. */ |
4841 | if (s->state == SERVICE_RELOAD_NOTIFY) | |
4842 | service_enter_running(s, SERVICE_SUCCESS); | |
308d72dc | 4843 | |
3bd28bf7 LP |
4844 | notify_dbus = true; |
4845 | ||
4846 | } else if (strv_contains(tags, "RELOADING=1")) { | |
4847 | ||
4848 | s->notify_state = NOTIFY_RELOADING; | |
4849 | ||
4850 | /* Sending RELOADING=1 after we send SIGHUP to request a reload will transition | |
4851 | * things to "reload-notify" state, where we'll wait for READY=1 to let us know the | |
4852 | * reload is done. Note that we insist on a timestamp being sent along here, so that | |
4853 | * we know for sure this is a reload cycle initiated *after* we sent the signal */ | |
4854 | if (s->state == SERVICE_RELOAD_SIGNAL && | |
4855 | monotonic_usec != USEC_INFINITY && | |
4856 | monotonic_usec >= s->reload_begin_usec) | |
4857 | /* Note, we don't call service_enter_reload_by_notify() here, because we | |
2257be13 | 4858 | * don't need reload propagation nor do we want to restart the timeout. */ |
3bd28bf7 LP |
4859 | service_set_state(s, SERVICE_RELOAD_NOTIFY); |
4860 | ||
4861 | if (s->state == SERVICE_RUNNING) | |
4862 | service_enter_reload_by_notify(s); | |
4863 | ||
4864 | notify_dbus = true; | |
8c47c732 LP |
4865 | } |
4866 | ||
4867 | /* Interpret STATUS= */ | |
28849dba | 4868 | e = strv_find_startswith(tags, "STATUS="); |
7f110ff9 | 4869 | if (e) { |
28849dba | 4870 | _cleanup_free_ char *t = NULL; |
8c47c732 | 4871 | |
28849dba | 4872 | if (!isempty(e)) { |
3eac1bca LP |
4873 | /* Note that this size limit check is mostly paranoia: since the datagram size we are willing |
4874 | * to process is already limited to NOTIFY_BUFFER_MAX, this limit here should never be hit. */ | |
4875 | if (strlen(e) > STATUS_TEXT_MAX) | |
4876 | log_unit_warning(u, "Status message overly long (%zu > %u), ignoring.", strlen(e), STATUS_TEXT_MAX); | |
4877 | else if (!utf8_is_valid(e)) | |
4878 | log_unit_warning(u, "Status message in notification message is not UTF-8 clean, ignoring."); | |
28849dba | 4879 | else { |
28849dba LP |
4880 | t = strdup(e); |
4881 | if (!t) | |
9c025022 | 4882 | log_oom_warning(); |
3a2776bc | 4883 | } |
28849dba | 4884 | } |
8c47c732 | 4885 | |
30b5275a | 4886 | if (!streq_ptr(s->status_text, t)) { |
3b319885 | 4887 | free_and_replace(s->status_text, t); |
30b5275a | 4888 | notify_dbus = true; |
28849dba | 4889 | } |
8c47c732 | 4890 | } |
842129f5 | 4891 | |
19dff691 MY |
4892 | /* Interpret NOTIFYACCESS= */ |
4893 | e = strv_find_startswith(tags, "NOTIFYACCESS="); | |
4894 | if (e) { | |
4895 | NotifyAccess notify_access; | |
4896 | ||
4897 | notify_access = notify_access_from_string(e); | |
4898 | if (notify_access < 0) | |
4899 | log_unit_warning_errno(u, notify_access, | |
4900 | "Failed to parse NOTIFYACCESS= field value '%s' in notification message, ignoring: %m", e); | |
4901 | ||
4902 | /* We don't need to check whether the new access mode is more strict than what is | |
4903 | * already in use, since only the privileged process is allowed to change it | |
4904 | * in the first place. */ | |
4905 | if (service_get_notify_access(s) != notify_access) { | |
4906 | service_override_notify_access(s, notify_access); | |
4907 | notify_dbus = true; | |
4908 | } | |
4909 | } | |
4910 | ||
4774e357 | 4911 | /* Interpret ERRNO= */ |
28849dba | 4912 | e = strv_find_startswith(tags, "ERRNO="); |
4774e357 MAA |
4913 | if (e) { |
4914 | int status_errno; | |
4915 | ||
2fa40742 LP |
4916 | status_errno = parse_errno(e); |
4917 | if (status_errno < 0) | |
4918 | log_unit_warning_errno(u, status_errno, | |
5e1ee764 | 4919 | "Failed to parse ERRNO= field value '%s' in notification message: %m", e); |
2fa40742 LP |
4920 | else if (s->status_errno != status_errno) { |
4921 | s->status_errno = status_errno; | |
4922 | notify_dbus = true; | |
4774e357 MAA |
4923 | } |
4924 | } | |
4925 | ||
9c025022 MY |
4926 | static const struct { |
4927 | const char *tag; | |
4928 | size_t status_offset; | |
4929 | } status_errors[] = { | |
4930 | { "BUSERROR=", offsetof(Service, status_bus_error) }, | |
4931 | { "VARLINKERROR=", offsetof(Service, status_varlink_error) }, | |
4932 | }; | |
4933 | ||
4934 | FOREACH_ELEMENT(i, status_errors) { | |
4935 | e = strv_find_startswith(tags, i->tag); | |
4936 | if (!e) | |
4937 | continue; | |
4938 | ||
4939 | char **status_error = (char**) ((uint8_t*) s + i->status_offset); | |
4940 | ||
4941 | e = empty_to_null(e); | |
4942 | ||
4943 | if (e && !string_is_safe_ascii(e)) { | |
4944 | _cleanup_free_ char *escaped = cescape(e); | |
4945 | log_unit_warning(u, "Got invalid %s string, ignoring: %s", i->tag, strna(escaped)); | |
4946 | } else if (free_and_strdup_warn(status_error, e) > 0) | |
4947 | notify_dbus = true; | |
4948 | } | |
4949 | ||
a327431b DB |
4950 | /* Interpret EXTEND_TIMEOUT= */ |
4951 | e = strv_find_startswith(tags, "EXTEND_TIMEOUT_USEC="); | |
4952 | if (e) { | |
4953 | usec_t extend_timeout_usec; | |
9c025022 | 4954 | |
a327431b DB |
4955 | if (safe_atou64(e, &extend_timeout_usec) < 0) |
4956 | log_unit_warning(u, "Failed to parse EXTEND_TIMEOUT_USEC=%s", e); | |
4957 | else | |
4958 | service_extend_timeout(s, extend_timeout_usec); | |
4959 | } | |
4960 | ||
6f285378 | 4961 | /* Interpret WATCHDOG= */ |
99b43caf JK |
4962 | e = strv_find_startswith(tags, "WATCHDOG="); |
4963 | if (e) { | |
4964 | if (streq(e, "1")) | |
4965 | service_reset_watchdog(s); | |
4966 | else if (streq(e, "trigger")) | |
4967 | service_force_watchdog(s); | |
4968 | else | |
4969 | log_unit_warning(u, "Passed WATCHDOG= field is invalid, ignoring."); | |
4970 | } | |
c4e2ceae | 4971 | |
c45d11cb LP |
4972 | e = strv_find_startswith(tags, "WATCHDOG_USEC="); |
4973 | if (e) { | |
4974 | usec_t watchdog_override_usec; | |
4975 | if (safe_atou64(e, &watchdog_override_usec) < 0) | |
4976 | log_unit_warning(u, "Failed to parse WATCHDOG_USEC=%s", e); | |
4977 | else | |
95d0d8ed | 4978 | service_override_watchdog_timeout(s, watchdog_override_usec); |
c45d11cb LP |
4979 | } |
4980 | ||
406aeb5d MY |
4981 | /* Interpret RESTART_RESET=1 */ |
4982 | if (strv_contains(tags, "RESTART_RESET=1") && IN_SET(s->state, SERVICE_RUNNING, SERVICE_STOP)) { | |
4983 | log_unit_struct(u, LOG_NOTICE, | |
4984 | LOG_UNIT_MESSAGE(u, "Got RESTART_RESET=1, resetting restart counter from %u.", s->n_restarts), | |
3cf6a3a3 | 4985 | LOG_ITEM("N_RESTARTS=0"), |
406aeb5d MY |
4986 | LOG_UNIT_INVOCATION_ID(u)); |
4987 | ||
4988 | s->n_restarts = 0; | |
4989 | notify_dbus = true; | |
4990 | } | |
4991 | ||
e78ee06d LP |
4992 | /* Process FD store messages. Either FDSTOREREMOVE=1 for removal, or FDSTORE=1 for addition. In both cases, |
4993 | * process FDNAME= for picking the file descriptor name to use. Note that FDNAME= is required when removing | |
4994 | * fds, but optional when pushing in new fds, for compatibility reasons. */ | |
d29cc4d6 | 4995 | if (strv_contains(tags, "FDSTOREREMOVE=1")) { |
e78ee06d LP |
4996 | const char *name; |
4997 | ||
4998 | name = strv_find_startswith(tags, "FDNAME="); | |
4999 | if (!name || !fdname_is_valid(name)) | |
5000 | log_unit_warning(u, "FDSTOREREMOVE=1 requested, but no valid file descriptor name passed, ignoring."); | |
5001 | else | |
5002 | service_remove_fd_store(s, name); | |
5003 | ||
d29cc4d6 | 5004 | } else if (strv_contains(tags, "FDSTORE=1")) { |
8dd4c05b LP |
5005 | const char *name; |
5006 | ||
5007 | name = strv_find_startswith(tags, "FDNAME="); | |
5008 | if (name && !fdname_is_valid(name)) { | |
5009 | log_unit_warning(u, "Passed FDNAME= name is invalid, ignoring."); | |
5010 | name = NULL; | |
5011 | } | |
5012 | ||
cb5a46b8 | 5013 | (void) service_add_fd_store_set(s, fds, name, !strv_contains(tags, "FDPOLL=0")); |
8dd4c05b | 5014 | } |
a354329f | 5015 | |
c4e2ceae | 5016 | /* Notify clients about changed status or main pid */ |
30b5275a LP |
5017 | if (notify_dbus) |
5018 | unit_add_to_dbus_queue(u); | |
8c47c732 LP |
5019 | } |
5020 | ||
3c1d1ca1 LP |
5021 | static void service_handoff_timestamp( |
5022 | Unit *u, | |
5023 | const struct ucred *ucred, | |
5024 | const dual_timestamp *ts) { | |
5025 | ||
5026 | Service *s = ASSERT_PTR(SERVICE(u)); | |
5027 | ||
5028 | assert(ucred); | |
5029 | assert(ts); | |
5030 | ||
5031 | if (s->main_pid.pid == ucred->pid) { | |
5032 | if (s->main_command) | |
5033 | exec_status_handoff(&s->main_command->exec_status, ucred, ts); | |
5034 | ||
5035 | exec_status_handoff(&s->main_exec_status, ucred, ts); | |
5036 | } else if (s->control_pid.pid == ucred->pid && s->control_command) | |
5037 | exec_status_handoff(&s->control_command->exec_status, ucred, ts); | |
5038 | else | |
5039 | return; | |
5040 | ||
5041 | unit_add_to_dbus_queue(u); | |
5042 | } | |
5043 | ||
406f1775 DDM |
5044 | static void service_notify_pidref(Unit *u, PidRef *parent_pidref, PidRef *child_pidref) { |
5045 | Service *s = ASSERT_PTR(SERVICE(u)); | |
5046 | int r; | |
5047 | ||
5048 | assert(pidref_is_set(parent_pidref)); | |
5049 | assert(pidref_is_set(child_pidref)); | |
5050 | ||
5051 | if (pidref_equal(&s->main_pid, parent_pidref)) { | |
5052 | r = service_set_main_pidref(s, TAKE_PIDREF(*child_pidref), /* start_timestamp = */ NULL); | |
5053 | if (r < 0) | |
5054 | return (void) log_unit_warning_errno(u, r, "Failed to set new main pid: %m"); | |
5055 | ||
5056 | /* Since the child process is PID 1 in a new PID namespace, it must be exclusive to this unit. */ | |
5057 | r = unit_watch_pidref(u, &s->main_pid, /* exclusive= */ true); | |
5058 | if (r < 0) | |
5059 | log_unit_warning_errno(u, r, "Failed to watch new main PID " PID_FMT ": %m", s->main_pid.pid); | |
5060 | } else if (pidref_equal(&s->control_pid, parent_pidref)) { | |
5061 | service_unwatch_control_pid(s); | |
5062 | s->control_pid = TAKE_PIDREF(*child_pidref); | |
5063 | ||
5064 | r = unit_watch_pidref(u, &s->control_pid, /* exclusive= */ true); | |
5065 | if (r < 0) | |
5066 | log_unit_warning_errno(u, r, "Failed to watch new control PID " PID_FMT ": %m", s->control_pid.pid); | |
5067 | } else | |
5068 | return (void) log_unit_debug(u, "Parent process " PID_FMT " does not match main or control processes, ignoring.", parent_pidref->pid); | |
5069 | ||
5070 | unit_add_to_dbus_queue(u); | |
5071 | } | |
5072 | ||
7a7821c8 | 5073 | static int service_get_timeout(Unit *u, usec_t *timeout) { |
e9fa1bf7 | 5074 | Service *s = ASSERT_PTR(SERVICE(u)); |
7a7821c8 | 5075 | uint64_t t; |
68db7a3b ZJS |
5076 | int r; |
5077 | ||
e9fa1bf7 MY |
5078 | assert(timeout); |
5079 | ||
68db7a3b ZJS |
5080 | if (!s->timer_event_source) |
5081 | return 0; | |
5082 | ||
7a7821c8 | 5083 | r = sd_event_source_get_time(s->timer_event_source, &t); |
68db7a3b ZJS |
5084 | if (r < 0) |
5085 | return r; | |
7a7821c8 LP |
5086 | if (t == USEC_INFINITY) |
5087 | return 0; | |
68db7a3b | 5088 | |
7a7821c8 | 5089 | *timeout = t; |
68db7a3b ZJS |
5090 | return 1; |
5091 | } | |
5092 | ||
f5a9d2ee | 5093 | static usec_t service_get_timeout_start_usec(Unit *u) { |
e9fa1bf7 | 5094 | Service *s = ASSERT_PTR(SERVICE(u)); |
f5a9d2ee | 5095 | return s->timeout_start_usec; |
5096 | } | |
5097 | ||
e39eb045 LP |
5098 | static bool pick_up_pid_from_bus_name(Service *s) { |
5099 | assert(s); | |
5100 | ||
5101 | /* If the service is running but we have no main PID yet, get it from the owner of the D-Bus name */ | |
5102 | ||
c79ab77c | 5103 | return !pidref_is_set(&s->main_pid) && |
e39eb045 LP |
5104 | IN_SET(s->state, |
5105 | SERVICE_START, | |
5106 | SERVICE_START_POST, | |
5107 | SERVICE_RUNNING, | |
3bd28bf7 LP |
5108 | SERVICE_RELOAD, |
5109 | SERVICE_RELOAD_SIGNAL, | |
5162829e | 5110 | SERVICE_RELOAD_NOTIFY, |
dfdeb0b1 | 5111 | SERVICE_REFRESH_EXTENSIONS, |
5162829e | 5112 | SERVICE_MOUNTING); |
e39eb045 LP |
5113 | } |
5114 | ||
5115 | static int bus_name_pid_lookup_callback(sd_bus_message *reply, void *userdata, sd_bus_error *ret_error) { | |
e9fa1bf7 | 5116 | Service *s = ASSERT_PTR(SERVICE(userdata)); |
495e75ed | 5117 | _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
e39eb045 | 5118 | const sd_bus_error *e; |
e39eb045 | 5119 | uint32_t pid; |
e39eb045 LP |
5120 | int r; |
5121 | ||
5122 | assert(reply); | |
e39eb045 | 5123 | |
e39eb045 LP |
5124 | s->bus_name_pid_lookup_slot = sd_bus_slot_unref(s->bus_name_pid_lookup_slot); |
5125 | ||
5126 | if (!s->bus_name || !pick_up_pid_from_bus_name(s)) | |
5127 | return 1; | |
5128 | ||
5129 | e = sd_bus_message_get_error(reply); | |
5130 | if (e) { | |
5131 | r = sd_bus_error_get_errno(e); | |
1e8f0bee | 5132 | log_unit_warning_errno(UNIT(s), r, "GetConnectionUnixProcessID() failed: %s", bus_error_message(e, r)); |
e39eb045 LP |
5133 | return 1; |
5134 | } | |
5135 | ||
5136 | r = sd_bus_message_read(reply, "u", &pid); | |
5137 | if (r < 0) { | |
5138 | bus_log_parse_error(r); | |
5139 | return 1; | |
5140 | } | |
5141 | ||
495e75ed LP |
5142 | r = pidref_set_pid(&pidref, pid); |
5143 | if (r < 0) { | |
1e8f0bee | 5144 | log_unit_debug_errno(UNIT(s), r, "GetConnectionUnixProcessID() returned invalid PID: %m"); |
e39eb045 LP |
5145 | return 1; |
5146 | } | |
5147 | ||
e9fa1bf7 | 5148 | log_unit_debug(UNIT(s), "D-Bus name %s is now owned by process " PID_FMT, s->bus_name, pidref.pid); |
e39eb045 | 5149 | |
7e0e6b50 | 5150 | (void) service_set_main_pidref(s, TAKE_PIDREF(pidref), /* start_timestamp = */ NULL); |
495e75ed | 5151 | (void) unit_watch_pidref(UNIT(s), &s->main_pid, /* exclusive= */ false); |
e39eb045 LP |
5152 | return 1; |
5153 | } | |
5154 | ||
fc67a943 | 5155 | static void service_bus_name_owner_change(Unit *u, const char *new_owner) { |
e9fa1bf7 | 5156 | Service *s = ASSERT_PTR(SERVICE(u)); |
718db961 | 5157 | int r; |
05e343b7 | 5158 | |
fc67a943 LP |
5159 | if (new_owner) |
5160 | log_unit_debug(u, "D-Bus name %s now owned by %s", s->bus_name, new_owner); | |
05e343b7 | 5161 | else |
fc67a943 | 5162 | log_unit_debug(u, "D-Bus name %s now not owned by anyone.", s->bus_name); |
05e343b7 | 5163 | |
d7a0f1f4 | 5164 | s->bus_name_good = new_owner; |
05e343b7 LP |
5165 | |
5166 | if (s->type == SERVICE_DBUS) { | |
029df9ed | 5167 | /* service_enter_running() will figure out what to do */ |
05e343b7 | 5168 | if (s->state == SERVICE_RUNNING) |
f42806df | 5169 | service_enter_running(s, SERVICE_SUCCESS); |
05e343b7 LP |
5170 | else if (s->state == SERVICE_START && new_owner) |
5171 | service_enter_start_post(s); | |
5172 | ||
e39eb045 | 5173 | } else if (new_owner && pick_up_pid_from_bus_name(s)) { |
05e343b7 | 5174 | |
718db961 | 5175 | /* Try to acquire PID from bus service */ |
05e343b7 | 5176 | |
e39eb045 LP |
5177 | s->bus_name_pid_lookup_slot = sd_bus_slot_unref(s->bus_name_pid_lookup_slot); |
5178 | ||
5179 | r = sd_bus_call_method_async( | |
5180 | u->manager->api_bus, | |
5181 | &s->bus_name_pid_lookup_slot, | |
5182 | "org.freedesktop.DBus", | |
5183 | "/org/freedesktop/DBus", | |
5184 | "org.freedesktop.DBus", | |
5185 | "GetConnectionUnixProcessID", | |
5186 | bus_name_pid_lookup_callback, | |
5187 | s, | |
5188 | "s", | |
5189 | s->bus_name); | |
5190 | if (r < 0) | |
1e8f0bee | 5191 | log_unit_debug_errno(u, r, "Failed to request owner PID of service name, ignoring: %m"); |
7400b9d2 | 5192 | } |
05e343b7 LP |
5193 | } |
5194 | ||
3fabebf4 LP |
5195 | int service_set_socket_fd( |
5196 | Service *s, | |
5197 | int fd, | |
5198 | Socket *sock, | |
82bdb3ed | 5199 | SocketPeer *peer, /* reference to object is donated to us on success */ |
3fabebf4 LP |
5200 | bool selinux_context_net) { |
5201 | ||
5202 | _cleanup_free_ char *peer_text = NULL; | |
79a98c60 | 5203 | int r; |
57020a3a | 5204 | |
4f2d528d LP |
5205 | assert(s); |
5206 | assert(fd >= 0); | |
23908d84 | 5207 | assert(sock); |
4f2d528d | 5208 | |
7f2fbbff LP |
5209 | /* This is called by the socket code when instantiating a new service for a stream socket and the socket needs |
5210 | * to be configured. We take ownership of the passed fd on success. */ | |
4f2d528d | 5211 | |
1124fe6f | 5212 | if (UNIT(s)->load_state != UNIT_LOADED) |
4f2d528d LP |
5213 | return -EINVAL; |
5214 | ||
5215 | if (s->socket_fd >= 0) | |
5216 | return -EBUSY; | |
5217 | ||
3fabebf4 LP |
5218 | assert(!s->socket_peer); |
5219 | ||
b9c1883a | 5220 | if (!IN_SET(s->state, SERVICE_DEAD, SERVICE_DEAD_RESOURCES_PINNED)) |
4f2d528d LP |
5221 | return -EAGAIN; |
5222 | ||
3fabebf4 | 5223 | if (getpeername_pretty(fd, true, &peer_text) >= 0) { |
79a98c60 LP |
5224 | |
5225 | if (UNIT(s)->description) { | |
c2b2df60 | 5226 | _cleanup_free_ char *a = NULL; |
79a98c60 | 5227 | |
3fabebf4 | 5228 | a = strjoin(UNIT(s)->description, " (", peer_text, ")"); |
79a98c60 LP |
5229 | if (!a) |
5230 | return -ENOMEM; | |
5231 | ||
5232 | r = unit_set_description(UNIT(s), a); | |
5233 | } else | |
3fabebf4 | 5234 | r = unit_set_description(UNIT(s), peer_text); |
79a98c60 LP |
5235 | if (r < 0) |
5236 | return r; | |
5237 | } | |
5238 | ||
23908d84 | 5239 | r = unit_add_two_dependencies(UNIT(s), UNIT_AFTER, UNIT_TRIGGERED_BY, UNIT(sock), false, UNIT_DEPENDENCY_IMPLICIT); |
7f2fbbff | 5240 | if (r < 0) |
23908d84 MY |
5241 | return log_unit_debug_errno(UNIT(s), r, |
5242 | "Failed to add After=/TriggeredBy= dependencies on socket unit: %m"); | |
7f2fbbff | 5243 | |
4f2d528d | 5244 | s->socket_fd = fd; |
d6a911e9 | 5245 | s->socket_peer = peer; |
16115b0a | 5246 | s->socket_fd_selinux_context_net = selinux_context_net; |
6cf6bbc2 | 5247 | |
7f7d01ed | 5248 | unit_ref_set(&s->accept_socket, UNIT(s), UNIT(sock)); |
7f2fbbff | 5249 | return 0; |
4f2d528d LP |
5250 | } |
5251 | ||
fdf20a31 | 5252 | static void service_reset_failed(Unit *u) { |
e9fa1bf7 | 5253 | Service *s = ASSERT_PTR(SERVICE(u)); |
5632e374 | 5254 | |
fdf20a31 | 5255 | if (s->state == SERVICE_FAILED) |
b9c1883a | 5256 | service_set_state(s, service_determine_dead_state(s)); |
5632e374 | 5257 | |
f42806df LP |
5258 | s->result = SERVICE_SUCCESS; |
5259 | s->reload_result = SERVICE_SUCCESS; | |
5162829e | 5260 | s->live_mount_result = SERVICE_SUCCESS; |
a53e92a1 | 5261 | s->clean_result = SERVICE_SUCCESS; |
7a0019d3 | 5262 | s->n_restarts = 0; |
5632e374 LP |
5263 | } |
5264 | ||
3e22239d YW |
5265 | static PidRef* service_main_pid(Unit *u, bool *ret_is_alien) { |
5266 | Service *s = ASSERT_PTR(SERVICE(u)); | |
5267 | ||
5268 | if (ret_is_alien) | |
5269 | *ret_is_alien = s->main_pid_alien; | |
5270 | ||
5271 | return &s->main_pid; | |
291d565a LP |
5272 | } |
5273 | ||
37eb258e LP |
5274 | static PidRef* service_control_pid(Unit *u) { |
5275 | return &ASSERT_PTR(SERVICE(u))->control_pid; | |
291d565a LP |
5276 | } |
5277 | ||
bb2c7685 | 5278 | static bool service_needs_console(Unit *u) { |
e9fa1bf7 | 5279 | Service *s = ASSERT_PTR(SERVICE(u)); |
bb2c7685 LP |
5280 | |
5281 | /* We provide our own implementation of this here, instead of relying of the generic implementation | |
5282 | * unit_needs_console() provides, since we want to return false if we are in SERVICE_EXITED state. */ | |
5283 | ||
5284 | if (!exec_context_may_touch_console(&s->exec_context)) | |
5285 | return false; | |
5286 | ||
5287 | return IN_SET(s->state, | |
31cd5f63 | 5288 | SERVICE_CONDITION, |
bb2c7685 LP |
5289 | SERVICE_START_PRE, |
5290 | SERVICE_START, | |
5291 | SERVICE_START_POST, | |
5292 | SERVICE_RUNNING, | |
5293 | SERVICE_RELOAD, | |
3bd28bf7 LP |
5294 | SERVICE_RELOAD_SIGNAL, |
5295 | SERVICE_RELOAD_NOTIFY, | |
dfdeb0b1 | 5296 | SERVICE_REFRESH_EXTENSIONS, |
5162829e | 5297 | SERVICE_MOUNTING, |
bb2c7685 | 5298 | SERVICE_STOP, |
c87700a1 | 5299 | SERVICE_STOP_WATCHDOG, |
bb2c7685 LP |
5300 | SERVICE_STOP_SIGTERM, |
5301 | SERVICE_STOP_SIGKILL, | |
5302 | SERVICE_STOP_POST, | |
bf760801 | 5303 | SERVICE_FINAL_WATCHDOG, |
bb2c7685 LP |
5304 | SERVICE_FINAL_SIGTERM, |
5305 | SERVICE_FINAL_SIGKILL); | |
5306 | } | |
5307 | ||
7af67e9a | 5308 | static int service_exit_status(Unit *u) { |
e9fa1bf7 | 5309 | Service *s = ASSERT_PTR(SERVICE(u)); |
7af67e9a LP |
5310 | |
5311 | if (s->main_exec_status.pid <= 0 || | |
5312 | !dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) | |
5313 | return -ENODATA; | |
5314 | ||
5315 | if (s->main_exec_status.code != CLD_EXITED) | |
5316 | return -EBADE; | |
5317 | ||
5318 | return s->main_exec_status.status; | |
5319 | } | |
5320 | ||
5e1669ff | 5321 | static const char* service_status_text(Unit *u) { |
e9fa1bf7 | 5322 | Service *s = ASSERT_PTR(SERVICE(u)); |
5e1669ff ZJS |
5323 | |
5324 | return s->status_text; | |
5325 | } | |
5326 | ||
4c2f5842 | 5327 | static int service_clean(Unit *u, ExecCleanMask mask) { |
e9fa1bf7 | 5328 | Service *s = ASSERT_PTR(SERVICE(u)); |
4c2f5842 | 5329 | _cleanup_strv_free_ char **l = NULL; |
4fb8f1e8 | 5330 | bool may_clean_fdstore = false; |
4c2f5842 LP |
5331 | int r; |
5332 | ||
4c2f5842 LP |
5333 | assert(mask != 0); |
5334 | ||
4fb8f1e8 | 5335 | if (!IN_SET(s->state, SERVICE_DEAD, SERVICE_DEAD_RESOURCES_PINNED)) |
4c2f5842 LP |
5336 | return -EBUSY; |
5337 | ||
4fb8f1e8 | 5338 | /* Determine if there's anything we could potentially clean */ |
4c2f5842 LP |
5339 | r = exec_context_get_clean_directories(&s->exec_context, u->manager->prefix, mask, &l); |
5340 | if (r < 0) | |
5341 | return r; | |
5342 | ||
4fb8f1e8 LP |
5343 | if (mask & EXEC_CLEAN_FDSTORE) |
5344 | may_clean_fdstore = s->n_fd_store > 0 || s->n_fd_store_max > 0; | |
5345 | ||
5346 | if (strv_isempty(l) && !may_clean_fdstore) | |
5347 | return -EUNATCH; /* Nothing to potentially clean */ | |
5348 | ||
5349 | /* Let's clean the stuff we can clean quickly */ | |
5350 | if (may_clean_fdstore) | |
5351 | service_release_fd_store(s); | |
5352 | ||
5353 | /* If we are done, leave quickly */ | |
5354 | if (strv_isempty(l)) { | |
5355 | if (s->state == SERVICE_DEAD_RESOURCES_PINNED && !s->fd_store) | |
5356 | service_set_state(s, SERVICE_DEAD); | |
5357 | return 0; | |
5358 | } | |
4c2f5842 | 5359 | |
4fb8f1e8 | 5360 | /* We need to clean disk stuff. This is slow, hence do it out of process, and change state */ |
4c2f5842 LP |
5361 | service_unwatch_control_pid(s); |
5362 | s->clean_result = SERVICE_SUCCESS; | |
5363 | s->control_command = NULL; | |
5364 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
5365 | ||
e5d6dcce | 5366 | r = service_arm_timer(s, /* relative= */ true, s->exec_context.timeout_clean_usec); |
10691b9e LP |
5367 | if (r < 0) { |
5368 | log_unit_warning_errno(u, r, "Failed to install timer: %m"); | |
4c2f5842 | 5369 | goto fail; |
10691b9e | 5370 | } |
4c2f5842 | 5371 | |
4775b55d | 5372 | r = unit_fork_and_watch_rm_rf(u, l, &s->control_pid); |
10691b9e LP |
5373 | if (r < 0) { |
5374 | log_unit_warning_errno(u, r, "Failed to spawn cleaning task: %m"); | |
c79ab77c | 5375 | goto fail; |
10691b9e | 5376 | } |
4c2f5842 | 5377 | |
c79ab77c | 5378 | service_set_state(s, SERVICE_CLEANING); |
4c2f5842 LP |
5379 | return 0; |
5380 | ||
5381 | fail: | |
4c2f5842 | 5382 | s->clean_result = SERVICE_FAILURE_RESOURCES; |
5dcadb4c | 5383 | s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); |
4c2f5842 LP |
5384 | return r; |
5385 | } | |
5386 | ||
5387 | static int service_can_clean(Unit *u, ExecCleanMask *ret) { | |
e9fa1bf7 | 5388 | Service *s = ASSERT_PTR(SERVICE(u)); |
4fb8f1e8 LP |
5389 | ExecCleanMask mask = 0; |
5390 | int r; | |
4c2f5842 | 5391 | |
4fb8f1e8 LP |
5392 | assert(ret); |
5393 | ||
5394 | r = exec_context_get_clean_mask(&s->exec_context, &mask); | |
5395 | if (r < 0) | |
5396 | return r; | |
4c2f5842 | 5397 | |
4fb8f1e8 LP |
5398 | if (s->n_fd_store_max > 0) |
5399 | mask |= EXEC_CLEAN_FDSTORE; | |
5400 | ||
5401 | *ret = mask; | |
5402 | return 0; | |
4c2f5842 LP |
5403 | } |
5404 | ||
f19afb21 MY |
5405 | static int service_live_mount( |
5406 | Unit *u, | |
5162829e LB |
5407 | const char *src, |
5408 | const char *dst, | |
5409 | sd_bus_message *message, | |
5410 | MountInNamespaceFlags flags, | |
5411 | const MountOptions *options, | |
5412 | sd_bus_error *error) { | |
5413 | ||
5162829e | 5414 | Service *s = ASSERT_PTR(SERVICE(u)); |
f19afb21 | 5415 | _cleanup_(pidref_done) PidRef worker = PIDREF_NULL; |
5162829e LB |
5416 | int r; |
5417 | ||
5418 | assert(u); | |
5419 | assert(u->manager); | |
5420 | assert(src); | |
5421 | assert(dst); | |
5422 | assert(message); | |
5423 | assert(!s->mount_request); | |
5424 | ||
5425 | if (s->state != SERVICE_RUNNING || !pidref_is_set(&s->main_pid)) { | |
f19afb21 | 5426 | log_unit_warning(u, "Service is not running, cannot live mount."); |
5162829e LB |
5427 | return sd_bus_error_setf( |
5428 | error, | |
5429 | BUS_ERROR_UNIT_INACTIVE, | |
5430 | "Live mounting '%s' on '%s' for unit '%s' cannot be scheduled: service not running", | |
5431 | src, | |
5432 | dst, | |
5433 | u->id); | |
5434 | } | |
5435 | ||
5436 | if (mount_point_is_credentials(u->manager->prefix[EXEC_DIRECTORY_RUNTIME], dst)) { | |
f19afb21 | 5437 | log_unit_warning(u, "Refusing to live mount over credential mount '%s'.", dst); |
5162829e LB |
5438 | return sd_bus_error_setf( |
5439 | error, | |
5440 | SD_BUS_ERROR_INVALID_ARGS, | |
5441 | "Live mounting '%s' on '%s' for unit '%s' cannot be scheduled: cannot mount over credential mount", | |
5442 | src, | |
5443 | dst, | |
5444 | u->id); | |
5445 | } | |
5446 | ||
5447 | if (path_startswith_strv(dst, s->exec_context.inaccessible_paths)) { | |
f19afb21 | 5448 | log_unit_warning(u, "%s is not accessible to this unit, cannot live mount.", dst); |
5162829e LB |
5449 | return sd_bus_error_setf( |
5450 | error, | |
5451 | SD_BUS_ERROR_INVALID_ARGS, | |
5452 | "Live mounting '%s' on '%s' for unit '%s' cannot be scheduled: destination is not accessible to this unit", | |
5453 | src, | |
5454 | dst, | |
5455 | u->id); | |
5456 | } | |
5457 | ||
5458 | service_unwatch_control_pid(s); | |
5459 | s->live_mount_result = SERVICE_SUCCESS; | |
5460 | s->control_command = NULL; | |
5461 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
5462 | ||
5463 | r = service_arm_timer(s, /* relative= */ true, s->timeout_start_usec); | |
5464 | if (r < 0) { | |
f19afb21 MY |
5465 | log_unit_error_errno(u, r, "Failed to install timer: %m"); |
5466 | sd_bus_error_set_errnof(error, r, | |
5467 | "Live mounting '%s' on '%s' for unit '%s': failed to install timer: %m", | |
5468 | src, dst, u->id); | |
5162829e LB |
5469 | goto fail; |
5470 | } | |
5471 | ||
f19afb21 | 5472 | const char *propagate_directory = strjoina("/run/systemd/propagate/", u->id); |
5162829e LB |
5473 | |
5474 | /* Given we are running from PID1, avoid doing potentially heavy I/O operations like opening images | |
5475 | * directly, and instead fork a worker process. We record the D-Bus message, so that we can reply | |
5476 | * after the operation has finished. This way callers can wait on the message and know that the new | |
5477 | * resource is available (or the operation failed) once they receive the response. */ | |
5478 | r = unit_fork_helper_process(u, "(sd-mount-in-ns)", /* into_cgroup= */ false, &worker); | |
5479 | if (r < 0) { | |
f19afb21 MY |
5480 | log_unit_error_errno(u, r, |
5481 | "Failed to fork process to mount '%s' on '%s' in unit's namespace: %m", | |
5482 | src, dst); | |
5483 | sd_bus_error_set_errnof(error, r, | |
5484 | "Live mounting '%s' on '%s' for unit '%s': failed to fork off helper process into namespace: %m", | |
5485 | src, dst, u->id); | |
5162829e LB |
5486 | goto fail; |
5487 | } | |
5488 | if (r == 0) { | |
5489 | if (flags & MOUNT_IN_NAMESPACE_IS_IMAGE) | |
5490 | r = mount_image_in_namespace( | |
5491 | &s->main_pid, | |
5492 | propagate_directory, | |
5493 | "/run/systemd/incoming/", | |
5494 | src, dst, | |
5495 | flags, | |
5496 | options, | |
5497 | s->exec_context.mount_image_policy ?: &image_policy_service); | |
5498 | else | |
5499 | r = bind_mount_in_namespace( | |
5500 | &s->main_pid, | |
5501 | propagate_directory, | |
5502 | "/run/systemd/incoming/", | |
5503 | src, dst, | |
5504 | flags); | |
5505 | if (r < 0) | |
f19afb21 MY |
5506 | log_unit_error_errno(u, r, |
5507 | "Failed to mount '%s' on '%s' in unit's namespace: %m", | |
5508 | src, dst); | |
5162829e LB |
5509 | else |
5510 | log_unit_debug(u, "Mounted '%s' on '%s' in unit's namespace", src, dst); | |
f19afb21 | 5511 | |
5162829e LB |
5512 | _exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS); |
5513 | } | |
5514 | ||
5515 | r = unit_watch_pidref(u, &worker, /* exclusive= */ true); | |
5516 | if (r < 0) { | |
f19afb21 MY |
5517 | log_unit_warning_errno(u, r, "Failed to watch live mount helper process: %m"); |
5518 | sd_bus_error_set_errnof(error, r, | |
5519 | "Live mounting '%s' on '%s' for unit '%s': failed to watch live mount helper process: %m", | |
5520 | src, dst, u->id); | |
5162829e LB |
5521 | goto fail; |
5522 | } | |
5523 | ||
5524 | s->mount_request = sd_bus_message_ref(message); | |
5525 | s->control_pid = TAKE_PIDREF(worker); | |
5526 | service_set_state(s, SERVICE_MOUNTING); | |
5527 | return 0; | |
5528 | ||
5529 | fail: | |
5530 | s->live_mount_result = SERVICE_FAILURE_RESOURCES; | |
20366875 | 5531 | service_enter_running(s, SERVICE_SUCCESS); |
5162829e LB |
5532 | return r; |
5533 | } | |
5534 | ||
f19afb21 MY |
5535 | static int service_can_live_mount(Unit *u, sd_bus_error *error) { |
5536 | Service *s = ASSERT_PTR(SERVICE(u)); | |
5162829e LB |
5537 | |
5538 | /* Ensure that the unit runs in a private mount namespace */ | |
f19afb21 | 5539 | if (!exec_needs_mount_namespace(&s->exec_context, /* params= */ NULL, s->exec_runtime)) |
5162829e LB |
5540 | return sd_bus_error_setf( |
5541 | error, | |
5542 | SD_BUS_ERROR_INVALID_ARGS, | |
d24d546d | 5543 | "Unit '%s' not running in private mount namespace, cannot live mount.", |
5162829e | 5544 | u->id); |
5162829e LB |
5545 | |
5546 | return 0; | |
5547 | } | |
5548 | ||
e9fa1bf7 MY |
5549 | static const char* service_finished_job(Unit *u, JobType t, JobResult result) { |
5550 | Service *s = ASSERT_PTR(SERVICE(u)); | |
5551 | ||
04d232d8 ZJS |
5552 | if (t == JOB_START && |
5553 | result == JOB_DONE && | |
e9fa1bf7 | 5554 | s->type == SERVICE_ONESHOT) |
04d232d8 | 5555 | return "Finished %s."; |
eda0cbf0 ZJS |
5556 | |
5557 | /* Fall back to generic */ | |
5558 | return NULL; | |
5559 | } | |
5560 | ||
705578c3 | 5561 | static int service_can_start(Unit *u) { |
e9fa1bf7 | 5562 | Service *s = ASSERT_PTR(SERVICE(u)); |
9727f242 DDM |
5563 | int r; |
5564 | ||
9727f242 DDM |
5565 | /* Make sure we don't enter a busy loop of some kind. */ |
5566 | r = unit_test_start_limit(u); | |
5567 | if (r < 0) { | |
5568 | service_enter_dead(s, SERVICE_FAILURE_START_LIMIT_HIT, false); | |
5569 | return r; | |
5570 | } | |
5571 | ||
705578c3 | 5572 | return 1; |
9727f242 DDM |
5573 | } |
5574 | ||
c25fac9a | 5575 | static void service_release_resources(Unit *u) { |
e9fa1bf7 | 5576 | Service *s = ASSERT_PTR(SERVICE(u)); |
c25fac9a LP |
5577 | |
5578 | /* Invoked by the unit state engine, whenever it realizes that unit is dead and there's no job | |
5579 | * anymore for it, and it hence is a good idea to release resources */ | |
5580 | ||
5581 | /* Don't release resources if this is a transitionary failed/dead state | |
5582 | * (i.e. SERVICE_DEAD_BEFORE_AUTO_RESTART/SERVICE_FAILED_BEFORE_AUTO_RESTART), insist on a permanent | |
5583 | * failure state. */ | |
b9c1883a | 5584 | if (!IN_SET(s->state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_DEAD_RESOURCES_PINNED)) |
c25fac9a LP |
5585 | return; |
5586 | ||
5587 | log_unit_debug(u, "Releasing resources..."); | |
5588 | ||
81a1d6d6 | 5589 | service_release_socket_fd(s); |
c25fac9a | 5590 | service_release_stdio_fd(s); |
3543456f | 5591 | service_release_extra_fds(s); |
b9c1883a LP |
5592 | |
5593 | if (s->fd_store_preserve_mode != EXEC_PRESERVE_YES) | |
5594 | service_release_fd_store(s); | |
5595 | ||
5596 | if (s->state == SERVICE_DEAD_RESOURCES_PINNED && !s->fd_store) | |
5597 | service_set_state(s, SERVICE_DEAD); | |
c25fac9a LP |
5598 | } |
5599 | ||
8017ed7e LP |
5600 | int service_determine_exec_selinux_label(Service *s, char **ret) { |
5601 | int r; | |
5602 | ||
5603 | assert(s); | |
5604 | assert(ret); | |
5605 | ||
5606 | if (!mac_selinux_use()) | |
5607 | return -ENODATA; | |
5608 | ||
5609 | /* Returns the SELinux label used for execution of the main service binary */ | |
5610 | ||
045d9424 ZJS |
5611 | if (s->exec_context.selinux_context) |
5612 | /* Prefer the explicitly configured label if there is one */ | |
5613 | return strdup_to(ret, s->exec_context.selinux_context); | |
8017ed7e LP |
5614 | |
5615 | if (s->exec_context.root_image || | |
5616 | s->exec_context.n_extension_images > 0 || | |
5617 | !strv_isempty(s->exec_context.extension_directories)) /* We cannot chase paths through images */ | |
5618 | return log_unit_debug_errno(UNIT(s), SYNTHETIC_ERRNO(ENODATA), "Service with RootImage=, ExtensionImages= or ExtensionDirectories= set, cannot determine socket SELinux label before activation, ignoring."); | |
5619 | ||
5620 | ExecCommand *c = s->exec_command[SERVICE_EXEC_START]; | |
5621 | if (!c) | |
5622 | return -ENODATA; | |
5623 | ||
5624 | _cleanup_free_ char *path = NULL; | |
42793366 | 5625 | r = chase(c->path, s->exec_context.root_directory, CHASE_PREFIX_ROOT|CHASE_TRIGGER_AUTOFS, &path, NULL); |
8017ed7e LP |
5626 | if (r < 0) { |
5627 | log_unit_debug_errno(UNIT(s), r, "Failed to resolve service binary '%s', ignoring.", c->path); | |
5628 | return -ENODATA; | |
5629 | } | |
5630 | ||
5631 | r = mac_selinux_get_create_label_from_exe(path, ret); | |
5632 | if (ERRNO_IS_NEG_NOT_SUPPORTED(r)) { | |
5633 | log_unit_debug_errno(UNIT(s), r, "Reading SELinux label off binary '%s' is not supported, ignoring.", path); | |
5634 | return -ENODATA; | |
5635 | } | |
5636 | if (ERRNO_IS_NEG_PRIVILEGE(r)) { | |
5637 | log_unit_debug_errno(UNIT(s), r, "Can't read SELinux label off binary '%s', due to privileges, ignoring.", path); | |
5638 | return -ENODATA; | |
5639 | } | |
5640 | if (r < 0) | |
5641 | return log_unit_debug_errno(UNIT(s), r, "Failed to read SELinux label off binary '%s': %m", path); | |
5642 | ||
5643 | return 0; | |
5644 | } | |
5645 | ||
25178aad LB |
5646 | static int service_cgroup_freezer_action(Unit *u, FreezerAction action) { |
5647 | Service *s = ASSERT_PTR(SERVICE(u)); | |
5648 | int r; | |
5649 | ||
5650 | r = unit_cgroup_freezer_action(u, action); | |
5651 | if (r <= 0) | |
5652 | return r; | |
5653 | ||
5654 | if (action == FREEZER_FREEZE) | |
5655 | service_stop_watchdog(s); | |
5656 | else if (action == FREEZER_THAW) | |
5657 | service_reset_watchdog(s); | |
5658 | ||
5659 | return r; | |
5660 | } | |
5661 | ||
94f04347 | 5662 | static const char* const service_restart_table[_SERVICE_RESTART_MAX] = { |
48d83e33 ZJS |
5663 | [SERVICE_RESTART_NO] = "no", |
5664 | [SERVICE_RESTART_ON_SUCCESS] = "on-success", | |
5665 | [SERVICE_RESTART_ON_FAILURE] = "on-failure", | |
6cfe2fde | 5666 | [SERVICE_RESTART_ON_ABNORMAL] = "on-abnormal", |
dc99a976 | 5667 | [SERVICE_RESTART_ON_WATCHDOG] = "on-watchdog", |
48d83e33 ZJS |
5668 | [SERVICE_RESTART_ON_ABORT] = "on-abort", |
5669 | [SERVICE_RESTART_ALWAYS] = "always", | |
94f04347 LP |
5670 | }; |
5671 | ||
5672 | DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart); | |
5673 | ||
e568fea9 RP |
5674 | static const char* const service_restart_mode_table[_SERVICE_RESTART_MODE_MAX] = { |
5675 | [SERVICE_RESTART_MODE_NORMAL] = "normal", | |
17f6b640 | 5676 | [SERVICE_RESTART_MODE_DIRECT] = "direct", |
7d8bbfbe | 5677 | [SERVICE_RESTART_MODE_DEBUG] = "debug", |
e568fea9 RP |
5678 | }; |
5679 | ||
5680 | DEFINE_STRING_TABLE_LOOKUP(service_restart_mode, ServiceRestartMode); | |
5681 | ||
94f04347 | 5682 | static const char* const service_type_table[_SERVICE_TYPE_MAX] = { |
3bd28bf7 LP |
5683 | [SERVICE_SIMPLE] = "simple", |
5684 | [SERVICE_FORKING] = "forking", | |
5685 | [SERVICE_ONESHOT] = "oneshot", | |
5686 | [SERVICE_DBUS] = "dbus", | |
5687 | [SERVICE_NOTIFY] = "notify", | |
5688 | [SERVICE_NOTIFY_RELOAD] = "notify-reload", | |
5689 | [SERVICE_IDLE] = "idle", | |
5690 | [SERVICE_EXEC] = "exec", | |
94f04347 LP |
5691 | }; |
5692 | ||
5693 | DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType); | |
5694 | ||
596e4470 | 5695 | static const char* const service_exit_type_table[_SERVICE_EXIT_TYPE_MAX] = { |
fb138166 | 5696 | [SERVICE_EXIT_MAIN] = "main", |
596e4470 HC |
5697 | [SERVICE_EXIT_CGROUP] = "cgroup", |
5698 | }; | |
5699 | ||
5700 | DEFINE_STRING_TABLE_LOOKUP(service_exit_type, ServiceExitType); | |
5701 | ||
e537352b | 5702 | static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = { |
48d83e33 ZJS |
5703 | [SERVICE_EXEC_CONDITION] = "ExecCondition", |
5704 | [SERVICE_EXEC_START_PRE] = "ExecStartPre", | |
5705 | [SERVICE_EXEC_START] = "ExecStart", | |
94f04347 | 5706 | [SERVICE_EXEC_START_POST] = "ExecStartPost", |
48d83e33 ZJS |
5707 | [SERVICE_EXEC_RELOAD] = "ExecReload", |
5708 | [SERVICE_EXEC_STOP] = "ExecStop", | |
5709 | [SERVICE_EXEC_STOP_POST] = "ExecStopPost", | |
94f04347 LP |
5710 | }; |
5711 | ||
5712 | DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand); | |
5713 | ||
b3d59367 | 5714 | static const char* const service_exec_ex_command_table[_SERVICE_EXEC_COMMAND_MAX] = { |
48d83e33 ZJS |
5715 | [SERVICE_EXEC_CONDITION] = "ExecConditionEx", |
5716 | [SERVICE_EXEC_START_PRE] = "ExecStartPreEx", | |
5717 | [SERVICE_EXEC_START] = "ExecStartEx", | |
b3d59367 | 5718 | [SERVICE_EXEC_START_POST] = "ExecStartPostEx", |
48d83e33 ZJS |
5719 | [SERVICE_EXEC_RELOAD] = "ExecReloadEx", |
5720 | [SERVICE_EXEC_STOP] = "ExecStopEx", | |
5721 | [SERVICE_EXEC_STOP_POST] = "ExecStopPostEx", | |
b3d59367 AZ |
5722 | }; |
5723 | ||
5724 | DEFINE_STRING_TABLE_LOOKUP(service_exec_ex_command, ServiceExecCommand); | |
5725 | ||
308d72dc | 5726 | static const char* const notify_state_table[_NOTIFY_STATE_MAX] = { |
48d83e33 ZJS |
5727 | [NOTIFY_UNKNOWN] = "unknown", |
5728 | [NOTIFY_READY] = "ready", | |
308d72dc | 5729 | [NOTIFY_RELOADING] = "reloading", |
48d83e33 | 5730 | [NOTIFY_STOPPING] = "stopping", |
308d72dc LP |
5731 | }; |
5732 | ||
5733 | DEFINE_STRING_TABLE_LOOKUP(notify_state, NotifyState); | |
5734 | ||
f42806df | 5735 | static const char* const service_result_table[_SERVICE_RESULT_MAX] = { |
48d83e33 ZJS |
5736 | [SERVICE_SUCCESS] = "success", |
5737 | [SERVICE_FAILURE_RESOURCES] = "resources", | |
5738 | [SERVICE_FAILURE_PROTOCOL] = "protocol", | |
5739 | [SERVICE_FAILURE_TIMEOUT] = "timeout", | |
5740 | [SERVICE_FAILURE_EXIT_CODE] = "exit-code", | |
5741 | [SERVICE_FAILURE_SIGNAL] = "signal", | |
5742 | [SERVICE_FAILURE_CORE_DUMP] = "core-dump", | |
5743 | [SERVICE_FAILURE_WATCHDOG] = "watchdog", | |
07299350 | 5744 | [SERVICE_FAILURE_START_LIMIT_HIT] = "start-limit-hit", |
48d83e33 ZJS |
5745 | [SERVICE_FAILURE_OOM_KILL] = "oom-kill", |
5746 | [SERVICE_SKIP_CONDITION] = "exec-condition", | |
f42806df LP |
5747 | }; |
5748 | ||
5749 | DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult); | |
5750 | ||
bf760801 JK |
5751 | static const char* const service_timeout_failure_mode_table[_SERVICE_TIMEOUT_FAILURE_MODE_MAX] = { |
5752 | [SERVICE_TIMEOUT_TERMINATE] = "terminate", | |
48d83e33 ZJS |
5753 | [SERVICE_TIMEOUT_ABORT] = "abort", |
5754 | [SERVICE_TIMEOUT_KILL] = "kill", | |
bf760801 JK |
5755 | }; |
5756 | ||
5757 | DEFINE_STRING_TABLE_LOOKUP(service_timeout_failure_mode, ServiceTimeoutFailureMode); | |
5758 | ||
87f0e418 | 5759 | const UnitVTable service_vtable = { |
7d17cfbc | 5760 | .object_size = sizeof(Service), |
718db961 LP |
5761 | .exec_context_offset = offsetof(Service, exec_context), |
5762 | .cgroup_context_offset = offsetof(Service, cgroup_context), | |
5763 | .kill_context_offset = offsetof(Service, kill_context), | |
613b411c | 5764 | .exec_runtime_offset = offsetof(Service, exec_runtime), |
9cc54544 | 5765 | .cgroup_runtime_offset = offsetof(Service, cgroup_runtime), |
3ef63c31 | 5766 | |
f975e971 LP |
5767 | .sections = |
5768 | "Unit\0" | |
5769 | "Service\0" | |
5770 | "Install\0", | |
4ad49000 | 5771 | .private_section = "Service", |
71645aca | 5772 | |
1d9cc876 LP |
5773 | .can_transient = true, |
5774 | .can_delegate = true, | |
c80a9a33 | 5775 | .can_fail = true, |
4d824a4e | 5776 | .can_set_managed_oom = true, |
1d9cc876 | 5777 | |
034c6ed7 LP |
5778 | .init = service_init, |
5779 | .done = service_done, | |
a16e1123 | 5780 | .load = service_load, |
a354329f | 5781 | .release_resources = service_release_resources, |
a16e1123 LP |
5782 | |
5783 | .coldplug = service_coldplug, | |
034c6ed7 | 5784 | |
5cb5a6ff LP |
5785 | .dump = service_dump, |
5786 | ||
5787 | .start = service_start, | |
5788 | .stop = service_stop, | |
5789 | .reload = service_reload, | |
5790 | ||
034c6ed7 LP |
5791 | .can_reload = service_can_reload, |
5792 | ||
4c2f5842 LP |
5793 | .clean = service_clean, |
5794 | .can_clean = service_can_clean, | |
8a0867d6 | 5795 | |
5162829e LB |
5796 | .live_mount = service_live_mount, |
5797 | .can_live_mount = service_can_live_mount, | |
5798 | ||
25178aad | 5799 | .freezer_action = service_cgroup_freezer_action, |
d9e45bc3 | 5800 | |
a16e1123 LP |
5801 | .serialize = service_serialize, |
5802 | .deserialize_item = service_deserialize_item, | |
5803 | ||
5cb5a6ff | 5804 | .active_state = service_active_state, |
10a94420 | 5805 | .sub_state_to_string = service_sub_state_to_string, |
5cb5a6ff | 5806 | |
deb4e708 MK |
5807 | .will_restart = service_will_restart, |
5808 | ||
f2f725e5 | 5809 | .may_gc = service_may_gc, |
701cc384 | 5810 | |
034c6ed7 | 5811 | .sigchld_event = service_sigchld_event, |
2c4104f0 | 5812 | |
fdf20a31 | 5813 | .reset_failed = service_reset_failed, |
5632e374 | 5814 | |
4ad49000 | 5815 | .notify_cgroup_empty = service_notify_cgroup_empty_event, |
afcfaa69 | 5816 | .notify_cgroup_oom = service_notify_cgroup_oom_event, |
8c47c732 | 5817 | .notify_message = service_notify_message, |
3c1d1ca1 | 5818 | .notify_handoff_timestamp = service_handoff_timestamp, |
406f1775 | 5819 | .notify_pidref = service_notify_pidref, |
8e274523 | 5820 | |
291d565a LP |
5821 | .main_pid = service_main_pid, |
5822 | .control_pid = service_control_pid, | |
5823 | ||
05e343b7 | 5824 | .bus_name_owner_change = service_bus_name_owner_change, |
05e343b7 | 5825 | |
74c964d3 LP |
5826 | .bus_set_property = bus_service_set_property, |
5827 | .bus_commit_properties = bus_service_commit_properties, | |
4139c1b2 | 5828 | |
68db7a3b | 5829 | .get_timeout = service_get_timeout, |
f5a9d2ee | 5830 | .get_timeout_start_usec = service_get_timeout_start_usec, |
bb2c7685 | 5831 | .needs_console = service_needs_console, |
7af67e9a | 5832 | .exit_status = service_exit_status, |
5e1669ff | 5833 | .status_text = service_status_text, |
718db961 | 5834 | |
c6918296 | 5835 | .status_message_formats = { |
eda0cbf0 | 5836 | .finished_job = service_finished_job, |
c6918296 | 5837 | }, |
9727f242 | 5838 | |
705578c3 | 5839 | .can_start = service_can_start, |
d52b8493 | 5840 | |
b2bfd121 LP |
5841 | .notify_plymouth = true, |
5842 | ||
d52b8493 LP |
5843 | .audit_start_message_type = AUDIT_SERVICE_START, |
5844 | .audit_stop_message_type = AUDIT_SERVICE_STOP, | |
5cb5a6ff | 5845 | }; |