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