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