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