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