1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
9 #include "sd-messages.h"
11 #include "alloc-util.h"
13 #include "bus-error.h"
14 #include "bus-kernel.h"
17 #include "constants.h"
18 #include "dbus-service.h"
19 #include "dbus-unit.h"
20 #include "devnum-util.h"
23 #include "exit-status.h"
26 #include "format-util.h"
27 #include "load-dropin.h"
28 #include "load-fragment.h"
31 #include "missing_audit.h"
32 #include "open-file.h"
33 #include "parse-util.h"
34 #include "path-util.h"
35 #include "process-util.h"
36 #include "random-util.h"
37 #include "selinux-util.h"
38 #include "serialize.h"
40 #include "signal-util.h"
42 #include "stdio-util.h"
43 #include "string-table.h"
44 #include "string-util.h"
46 #include "unit-name.h"
50 #define service_spawn(...) service_spawn_internal(__func__, __VA_ARGS__)
52 static const UnitActiveState state_translation_table
[_SERVICE_STATE_MAX
] = {
53 [SERVICE_DEAD
] = UNIT_INACTIVE
,
54 [SERVICE_CONDITION
] = UNIT_ACTIVATING
,
55 [SERVICE_START_PRE
] = UNIT_ACTIVATING
,
56 [SERVICE_START
] = UNIT_ACTIVATING
,
57 [SERVICE_START_POST
] = UNIT_ACTIVATING
,
58 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
59 [SERVICE_EXITED
] = UNIT_ACTIVE
,
60 [SERVICE_RELOAD
] = UNIT_RELOADING
,
61 [SERVICE_RELOAD_SIGNAL
] = UNIT_RELOADING
,
62 [SERVICE_RELOAD_NOTIFY
] = UNIT_RELOADING
,
63 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
64 [SERVICE_STOP_WATCHDOG
] = UNIT_DEACTIVATING
,
65 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
66 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
67 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
68 [SERVICE_FINAL_WATCHDOG
] = UNIT_DEACTIVATING
,
69 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
70 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
71 [SERVICE_FAILED
] = UNIT_FAILED
,
72 [SERVICE_DEAD_BEFORE_AUTO_RESTART
] = UNIT_INACTIVE
,
73 [SERVICE_FAILED_BEFORE_AUTO_RESTART
] = UNIT_FAILED
,
74 [SERVICE_DEAD_RESOURCES_PINNED
] = UNIT_INACTIVE
,
75 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
,
76 [SERVICE_AUTO_RESTART_QUEUED
] = UNIT_ACTIVATING
,
77 [SERVICE_CLEANING
] = UNIT_MAINTENANCE
,
80 /* For Type=idle we never want to delay any other jobs, hence we
81 * consider idle jobs active as soon as we start working on them */
82 static const UnitActiveState state_translation_table_idle
[_SERVICE_STATE_MAX
] = {
83 [SERVICE_DEAD
] = UNIT_INACTIVE
,
84 [SERVICE_CONDITION
] = UNIT_ACTIVE
,
85 [SERVICE_START_PRE
] = UNIT_ACTIVE
,
86 [SERVICE_START
] = UNIT_ACTIVE
,
87 [SERVICE_START_POST
] = UNIT_ACTIVE
,
88 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
89 [SERVICE_EXITED
] = UNIT_ACTIVE
,
90 [SERVICE_RELOAD
] = UNIT_RELOADING
,
91 [SERVICE_RELOAD_SIGNAL
] = UNIT_RELOADING
,
92 [SERVICE_RELOAD_NOTIFY
] = UNIT_RELOADING
,
93 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
94 [SERVICE_STOP_WATCHDOG
] = UNIT_DEACTIVATING
,
95 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
96 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
97 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
98 [SERVICE_FINAL_WATCHDOG
] = UNIT_DEACTIVATING
,
99 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
100 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
101 [SERVICE_FAILED
] = UNIT_FAILED
,
102 [SERVICE_DEAD_BEFORE_AUTO_RESTART
] = UNIT_INACTIVE
,
103 [SERVICE_FAILED_BEFORE_AUTO_RESTART
] = UNIT_FAILED
,
104 [SERVICE_DEAD_RESOURCES_PINNED
] = UNIT_INACTIVE
,
105 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
,
106 [SERVICE_AUTO_RESTART_QUEUED
] = UNIT_ACTIVATING
,
107 [SERVICE_CLEANING
] = UNIT_MAINTENANCE
,
110 static int service_dispatch_inotify_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
);
111 static int service_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
112 static int service_dispatch_watchdog(sd_event_source
*source
, usec_t usec
, void *userdata
);
113 static int service_dispatch_exec_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
);
115 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
);
116 static void service_enter_reload_by_notify(Service
*s
);
118 static bool SERVICE_STATE_WITH_MAIN_PROCESS(ServiceState state
) {
120 SERVICE_START
, SERVICE_START_POST
,
122 SERVICE_RELOAD
, SERVICE_RELOAD_SIGNAL
, SERVICE_RELOAD_NOTIFY
,
123 SERVICE_STOP
, SERVICE_STOP_WATCHDOG
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
124 SERVICE_FINAL_WATCHDOG
, SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
);
127 static bool SERVICE_STATE_WITH_CONTROL_PROCESS(ServiceState state
) {
130 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
131 SERVICE_RELOAD
, SERVICE_RELOAD_SIGNAL
, SERVICE_RELOAD_NOTIFY
,
132 SERVICE_STOP
, SERVICE_STOP_WATCHDOG
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
133 SERVICE_FINAL_WATCHDOG
, SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
,
137 static void service_init(Unit
*u
) {
138 Service
*s
= SERVICE(u
);
141 assert(u
->load_state
== UNIT_STUB
);
143 s
->timeout_start_usec
= u
->manager
->defaults
.timeout_start_usec
;
144 s
->timeout_stop_usec
= u
->manager
->defaults
.timeout_stop_usec
;
145 s
->timeout_abort_usec
= u
->manager
->defaults
.timeout_abort_usec
;
146 s
->timeout_abort_set
= u
->manager
->defaults
.timeout_abort_set
;
147 s
->restart_usec
= u
->manager
->defaults
.restart_usec
;
148 s
->restart_max_delay_usec
= USEC_INFINITY
;
149 s
->runtime_max_usec
= USEC_INFINITY
;
150 s
->type
= _SERVICE_TYPE_INVALID
;
151 s
->socket_fd
= -EBADF
;
152 s
->stdin_fd
= s
->stdout_fd
= s
->stderr_fd
= -EBADF
;
153 s
->guess_main_pid
= true;
154 s
->main_pid
= PIDREF_NULL
;
155 s
->control_pid
= PIDREF_NULL
;
156 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
158 s
->exec_context
.keyring_mode
= MANAGER_IS_SYSTEM(u
->manager
) ?
159 EXEC_KEYRING_PRIVATE
: EXEC_KEYRING_INHERIT
;
161 s
->notify_access_override
= _NOTIFY_ACCESS_INVALID
;
163 s
->watchdog_original_usec
= USEC_INFINITY
;
165 s
->oom_policy
= _OOM_POLICY_INVALID
;
166 s
->reload_begin_usec
= USEC_INFINITY
;
167 s
->reload_signal
= SIGHUP
;
169 s
->fd_store_preserve_mode
= EXEC_PRESERVE_RESTART
;
172 static void service_unwatch_control_pid(Service
*s
) {
174 unit_unwatch_pidref_done(UNIT(s
), &s
->control_pid
);
177 static void service_unwatch_main_pid(Service
*s
) {
179 unit_unwatch_pidref_done(UNIT(s
), &s
->main_pid
);
182 static void service_unwatch_pid_file(Service
*s
) {
183 if (!s
->pid_file_pathspec
)
186 log_unit_debug(UNIT(s
), "Stopping watch for PID file %s", s
->pid_file_pathspec
->path
);
187 path_spec_unwatch(s
->pid_file_pathspec
);
188 path_spec_done(s
->pid_file_pathspec
);
189 s
->pid_file_pathspec
= mfree(s
->pid_file_pathspec
);
192 static int service_set_main_pidref(Service
*s
, PidRef
*pidref
) {
197 /* Takes ownership of the specified pidref on success, but not on failure. */
199 if (!pidref_is_set(pidref
))
202 if (pidref
->pid
<= 1)
205 if (pidref_is_self(pidref
))
208 if (pidref_equal(&s
->main_pid
, pidref
) && s
->main_pid_known
) {
213 if (!pidref_equal(&s
->main_pid
, pidref
)) {
214 service_unwatch_main_pid(s
);
215 exec_status_start(&s
->main_exec_status
, pidref
->pid
);
218 s
->main_pid
= TAKE_PIDREF(*pidref
);
219 s
->main_pid_known
= true;
221 r
= pidref_is_my_child(&s
->main_pid
);
223 log_unit_warning_errno(UNIT(s
), r
, "Can't determine if process "PID_FMT
" is our child, assuming it is not: %m", s
->main_pid
.pid
);
225 log_unit_warning(UNIT(s
), "Supervising process "PID_FMT
" which is not our child. We'll most likely not notice when it exits.", s
->main_pid
.pid
);
227 s
->main_pid_alien
= r
<= 0;
231 void service_release_socket_fd(Service
*s
) {
234 if (s
->socket_fd
< 0 && !UNIT_ISSET(s
->accept_socket
) && !s
->socket_peer
)
237 log_unit_debug(UNIT(s
), "Closing connection socket.");
239 /* Undo the effect of service_set_socket_fd(). */
241 s
->socket_fd
= asynchronous_close(s
->socket_fd
);
243 if (UNIT_ISSET(s
->accept_socket
)) {
244 socket_connection_unref(SOCKET(UNIT_DEREF(s
->accept_socket
)));
245 unit_ref_unset(&s
->accept_socket
);
248 s
->socket_peer
= socket_peer_unref(s
->socket_peer
);
251 static void service_override_notify_access(Service
*s
, NotifyAccess notify_access_override
) {
254 s
->notify_access_override
= notify_access_override
;
256 log_unit_debug(UNIT(s
), "notify_access=%s", notify_access_to_string(s
->notify_access
));
257 log_unit_debug(UNIT(s
), "notify_access_override=%s", notify_access_to_string(s
->notify_access_override
));
260 static void service_stop_watchdog(Service
*s
) {
263 s
->watchdog_event_source
= sd_event_source_disable_unref(s
->watchdog_event_source
);
264 s
->watchdog_timestamp
= DUAL_TIMESTAMP_NULL
;
267 static void service_start_watchdog(Service
*s
) {
268 usec_t watchdog_usec
;
273 watchdog_usec
= service_get_watchdog_usec(s
);
274 if (!timestamp_is_set(watchdog_usec
)) {
275 service_stop_watchdog(s
);
279 if (s
->watchdog_event_source
) {
280 r
= sd_event_source_set_time(s
->watchdog_event_source
, usec_add(s
->watchdog_timestamp
.monotonic
, watchdog_usec
));
282 log_unit_warning_errno(UNIT(s
), r
, "Failed to reset watchdog timer: %m");
286 r
= sd_event_source_set_enabled(s
->watchdog_event_source
, SD_EVENT_ONESHOT
);
288 r
= sd_event_add_time(
289 UNIT(s
)->manager
->event
,
290 &s
->watchdog_event_source
,
292 usec_add(s
->watchdog_timestamp
.monotonic
, watchdog_usec
), 0,
293 service_dispatch_watchdog
, s
);
295 log_unit_warning_errno(UNIT(s
), r
, "Failed to add watchdog timer: %m");
299 (void) sd_event_source_set_description(s
->watchdog_event_source
, "service-watchdog");
301 /* Let's process everything else which might be a sign
302 * of living before we consider a service died. */
303 r
= sd_event_source_set_priority(s
->watchdog_event_source
, EVENT_PRIORITY_SERVICE_WATCHDOG
);
306 log_unit_warning_errno(UNIT(s
), r
, "Failed to install watchdog timer: %m");
309 usec_t
service_restart_usec_next(Service
*s
) {
310 unsigned n_restarts_next
;
314 /* When the service state is in SERVICE_*_BEFORE_AUTO_RESTART or SERVICE_AUTO_RESTART, we still need
315 * to add 1 to s->n_restarts manually, because s->n_restarts is not updated until a restart job is
316 * enqueued, i.e. state has transitioned to SERVICE_AUTO_RESTART_QUEUED. */
317 n_restarts_next
= s
->n_restarts
+ (s
->state
== SERVICE_AUTO_RESTART_QUEUED
? 0 : 1);
319 if (n_restarts_next
<= 1 ||
320 s
->restart_steps
== 0 ||
321 s
->restart_usec
== 0 ||
322 s
->restart_max_delay_usec
== USEC_INFINITY
||
323 s
->restart_usec
>= s
->restart_max_delay_usec
)
324 return s
->restart_usec
;
326 if (n_restarts_next
> s
->restart_steps
)
327 return s
->restart_max_delay_usec
;
329 /* Enforced in service_verify() and above */
330 assert(s
->restart_max_delay_usec
> s
->restart_usec
);
332 /* r_i / r_0 = (r_n / r_0) ^ (i / n)
334 * r_0 : initial restart usec (s->restart_usec),
335 * r_i : i-th restart usec (value),
336 * r_n : maximum restart usec (s->restart_max_delay_usec),
337 * i : index of the next step (n_restarts_next - 1)
338 * n : num maximum steps (s->restart_steps) */
339 return (usec_t
) (s
->restart_usec
* powl((long double) s
->restart_max_delay_usec
/ s
->restart_usec
,
340 (long double) (n_restarts_next
- 1) / s
->restart_steps
));
343 static void service_extend_event_source_timeout(Service
*s
, sd_event_source
*source
, usec_t extended
) {
349 /* Extends the specified event source timer to at least the specified time, unless it is already later
355 r
= sd_event_source_get_time(source
, ¤t
);
358 (void) sd_event_source_get_description(s
->timer_event_source
, &desc
);
359 log_unit_warning_errno(UNIT(s
), r
, "Failed to retrieve timeout time for event source '%s', ignoring: %m", strna(desc
));
363 if (current
>= extended
) /* Current timeout is already longer, ignore this. */
366 r
= sd_event_source_set_time(source
, extended
);
369 (void) sd_event_source_get_description(s
->timer_event_source
, &desc
);
370 log_unit_warning_errno(UNIT(s
), r
, "Failed to set timeout time for event source '%s', ignoring %m", strna(desc
));
374 static void service_extend_timeout(Service
*s
, usec_t extend_timeout_usec
) {
379 if (!timestamp_is_set(extend_timeout_usec
))
382 extended
= usec_add(now(CLOCK_MONOTONIC
), extend_timeout_usec
);
384 service_extend_event_source_timeout(s
, s
->timer_event_source
, extended
);
385 service_extend_event_source_timeout(s
, s
->watchdog_event_source
, extended
);
388 static void service_reset_watchdog(Service
*s
) {
391 dual_timestamp_now(&s
->watchdog_timestamp
);
392 service_start_watchdog(s
);
395 static void service_override_watchdog_timeout(Service
*s
, usec_t watchdog_override_usec
) {
398 s
->watchdog_override_enable
= true;
399 s
->watchdog_override_usec
= watchdog_override_usec
;
400 service_reset_watchdog(s
);
402 log_unit_debug(UNIT(s
), "watchdog_usec="USEC_FMT
, s
->watchdog_usec
);
403 log_unit_debug(UNIT(s
), "watchdog_override_usec="USEC_FMT
, s
->watchdog_override_usec
);
406 static ServiceFDStore
* service_fd_store_unlink(ServiceFDStore
*fs
) {
411 assert(fs
->service
->n_fd_store
> 0);
412 LIST_REMOVE(fd_store
, fs
->service
->fd_store
, fs
);
413 fs
->service
->n_fd_store
--;
416 sd_event_source_disable_unref(fs
->event_source
);
419 asynchronous_close(fs
->fd
);
423 DEFINE_TRIVIAL_CLEANUP_FUNC(ServiceFDStore
*, service_fd_store_unlink
);
425 static void service_release_fd_store(Service
*s
) {
431 log_unit_debug(UNIT(s
), "Releasing all stored fds");
434 service_fd_store_unlink(s
->fd_store
);
436 assert(s
->n_fd_store
== 0);
439 static void service_release_stdio_fd(Service
*s
) {
442 if (s
->stdin_fd
< 0 && s
->stdout_fd
< 0 && s
->stdout_fd
< 0)
445 log_unit_debug(UNIT(s
), "Releasing stdin/stdout/stderr file descriptors.");
447 s
->stdin_fd
= asynchronous_close(s
->stdin_fd
);
448 s
->stdout_fd
= asynchronous_close(s
->stdout_fd
);
449 s
->stderr_fd
= asynchronous_close(s
->stderr_fd
);
452 static void service_done(Unit
*u
) {
453 Service
*s
= SERVICE(u
);
457 open_file_free_many(&s
->open_files
);
459 s
->pid_file
= mfree(s
->pid_file
);
460 s
->status_text
= mfree(s
->status_text
);
462 s
->exec_runtime
= exec_runtime_free(s
->exec_runtime
);
464 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
465 s
->control_command
= NULL
;
466 s
->main_command
= NULL
;
468 exit_status_set_free(&s
->restart_prevent_status
);
469 exit_status_set_free(&s
->restart_force_status
);
470 exit_status_set_free(&s
->success_status
);
472 /* This will leak a process, but at least no memory or any of our resources */
473 service_unwatch_main_pid(s
);
474 service_unwatch_control_pid(s
);
475 service_unwatch_pid_file(s
);
478 unit_unwatch_bus_name(u
, s
->bus_name
);
479 s
->bus_name
= mfree(s
->bus_name
);
482 s
->bus_name_owner
= mfree(s
->bus_name_owner
);
484 s
->usb_function_descriptors
= mfree(s
->usb_function_descriptors
);
485 s
->usb_function_strings
= mfree(s
->usb_function_strings
);
487 service_stop_watchdog(s
);
489 s
->timer_event_source
= sd_event_source_disable_unref(s
->timer_event_source
);
490 s
->exec_fd_event_source
= sd_event_source_disable_unref(s
->exec_fd_event_source
);
492 s
->bus_name_pid_lookup_slot
= sd_bus_slot_unref(s
->bus_name_pid_lookup_slot
);
494 service_release_socket_fd(s
);
495 service_release_stdio_fd(s
);
496 service_release_fd_store(s
);
499 static int on_fd_store_io(sd_event_source
*e
, int fd
, uint32_t revents
, void *userdata
) {
500 ServiceFDStore
*fs
= ASSERT_PTR(userdata
);
504 /* If we get either EPOLLHUP or EPOLLERR, it's time to remove this entry from the fd store */
505 log_unit_debug(UNIT(fs
->service
),
506 "Received %s on stored fd %d (%s), closing.",
507 revents
& EPOLLERR
? "EPOLLERR" : "EPOLLHUP",
508 fs
->fd
, strna(fs
->fdname
));
509 service_fd_store_unlink(fs
);
513 static int service_add_fd_store(Service
*s
, int fd_in
, const char *name
, bool do_poll
) {
514 _cleanup_(service_fd_store_unlinkp
) ServiceFDStore
*fs
= NULL
;
515 _cleanup_(asynchronous_closep
) int fd
= ASSERT_FD(fd_in
);
519 /* fd is always consumed even if the function fails. */
523 if (fstat(fd
, &st
) < 0)
526 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
);
528 if (s
->n_fd_store
>= s
->n_fd_store_max
)
529 /* Our store is full. Use this errno rather than E[NM]FILE to distinguish from the case
530 * where systemd itself hits the file limit. */
531 return log_unit_debug_errno(UNIT(s
), SYNTHETIC_ERRNO(EXFULL
), "Hit fd store limit.");
533 LIST_FOREACH(fd_store
, i
, s
->fd_store
) {
534 r
= same_fd(i
->fd
, fd
);
538 log_unit_debug(UNIT(s
), "Suppressing duplicate fd %i in fd store.", fd
);
539 return 0; /* fd already included */
543 fs
= new(ServiceFDStore
, 1);
547 *fs
= (ServiceFDStore
) {
550 .fdname
= strdup(name
?: "stored"),
557 r
= sd_event_add_io(UNIT(s
)->manager
->event
, &fs
->event_source
, fs
->fd
, 0, on_fd_store_io
, fs
);
558 if (r
< 0 && r
!= -EPERM
) /* EPERM indicates fds that aren't pollable, which is OK */
561 (void) sd_event_source_set_description(fs
->event_source
, "service-fd-store");
565 LIST_PREPEND(fd_store
, s
->fd_store
, fs
);
568 log_unit_debug(UNIT(s
), "Added fd %i (%s) to fd store.", fs
->fd
, fs
->fdname
);
571 return 1; /* fd newly stored */
574 static int service_add_fd_store_set(Service
*s
, FDSet
*fds
, const char *name
, bool do_poll
) {
582 fd
= fdset_steal_first(fds
);
586 r
= service_add_fd_store(s
, fd
, name
, do_poll
);
588 return log_unit_warning_errno(UNIT(s
), r
,
589 "Cannot store more fds than FileDescriptorStoreMax=%u, closing remaining.",
592 return log_unit_error_errno(UNIT(s
), r
, "Failed to add fd to store: %m");
598 static void service_remove_fd_store(Service
*s
, const char *name
) {
602 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
603 if (!streq(fs
->fdname
, name
))
606 log_unit_debug(UNIT(s
), "Got explicit request to remove fd %i (%s), closing.", fs
->fd
, name
);
607 service_fd_store_unlink(fs
);
611 static usec_t
service_running_timeout(Service
*s
) {
616 if (s
->runtime_rand_extra_usec
!= 0) {
617 delta
= random_u64_range(s
->runtime_rand_extra_usec
);
618 log_unit_debug(UNIT(s
), "Adding delta of %s sec to timeout", FORMAT_TIMESPAN(delta
, USEC_PER_SEC
));
621 return usec_add(usec_add(UNIT(s
)->active_enter_timestamp
.monotonic
,
622 s
->runtime_max_usec
),
626 static int service_arm_timer(Service
*s
, bool relative
, usec_t usec
) {
629 return unit_arm_timer(UNIT(s
), &s
->timer_event_source
, relative
, usec
, service_dispatch_timer
);
632 static int service_verify(Service
*s
) {
634 assert(UNIT(s
)->load_state
== UNIT_LOADED
);
636 for (ServiceExecCommand c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++)
637 LIST_FOREACH(command
, command
, s
->exec_command
[c
]) {
638 if (!path_is_absolute(command
->path
) && !filename_is_valid(command
->path
))
639 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
),
640 "Service %s= binary path \"%s\" is neither a valid executable name nor an absolute path. Refusing.",
642 service_exec_command_to_string(c
));
643 if (strv_isempty(command
->argv
))
644 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
),
645 "Service has an empty argv in %s=. Refusing.",
646 service_exec_command_to_string(c
));
649 if (!s
->exec_command
[SERVICE_EXEC_START
] && !s
->exec_command
[SERVICE_EXEC_STOP
] &&
650 UNIT(s
)->success_action
== EMERGENCY_ACTION_NONE
)
651 /* FailureAction= only makes sense if one of the start or stop commands is specified.
652 * SuccessAction= will be executed unconditionally if no commands are specified. Hence,
653 * either a command or SuccessAction= are required. */
655 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "Service has no ExecStart=, ExecStop=, or SuccessAction=. Refusing.");
657 if (s
->type
!= SERVICE_ONESHOT
&& !s
->exec_command
[SERVICE_EXEC_START
])
658 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "Service has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
660 if (!s
->remain_after_exit
&& !s
->exec_command
[SERVICE_EXEC_START
] && UNIT(s
)->success_action
== EMERGENCY_ACTION_NONE
)
661 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.");
663 if (s
->type
!= SERVICE_ONESHOT
&& s
->exec_command
[SERVICE_EXEC_START
]->command_next
)
664 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.");
666 if (s
->type
== SERVICE_ONESHOT
&& IN_SET(s
->restart
, SERVICE_RESTART_ALWAYS
, SERVICE_RESTART_ON_SUCCESS
))
667 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.");
669 if (s
->type
== SERVICE_ONESHOT
&& s
->exit_type
== SERVICE_EXIT_CGROUP
)
670 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "Service has ExitType=cgroup set, which isn't allowed for Type=oneshot services. Refusing.");
672 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
)
673 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "Service is of type D-Bus but no D-Bus service name has been specified. Refusing.");
675 if (s
->exec_context
.pam_name
&& !IN_SET(s
->kill_context
.kill_mode
, KILL_CONTROL_GROUP
, KILL_MIXED
))
676 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "Service has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing.");
678 if (s
->usb_function_descriptors
&& !s
->usb_function_strings
)
679 log_unit_warning(UNIT(s
), "Service has USBFunctionDescriptors= setting, but no USBFunctionStrings=. Ignoring.");
681 if (!s
->usb_function_descriptors
&& s
->usb_function_strings
)
682 log_unit_warning(UNIT(s
), "Service has USBFunctionStrings= setting, but no USBFunctionDescriptors=. Ignoring.");
684 if (s
->runtime_max_usec
!= USEC_INFINITY
&& s
->type
== SERVICE_ONESHOT
)
685 log_unit_warning(UNIT(s
), "RuntimeMaxSec= has no effect in combination with Type=oneshot. Ignoring.");
687 if (s
->runtime_max_usec
== USEC_INFINITY
&& s
->runtime_rand_extra_usec
!= 0)
688 log_unit_warning(UNIT(s
), "Service has RuntimeRandomizedExtraSec= setting, but no RuntimeMaxSec=. Ignoring.");
690 if (s
->exit_type
== SERVICE_EXIT_CGROUP
&& cg_unified() < CGROUP_UNIFIED_SYSTEMD
)
691 log_unit_warning(UNIT(s
), "Service has ExitType=cgroup set, but we are running with legacy cgroups v1, which might not work correctly. Continuing.");
693 if (s
->restart_max_delay_usec
== USEC_INFINITY
&& s
->restart_steps
> 0)
694 log_unit_warning(UNIT(s
), "Service has RestartSteps= but no RestartMaxDelaySec= setting. Ignoring.");
696 if (s
->restart_max_delay_usec
!= USEC_INFINITY
&& s
->restart_steps
== 0)
697 log_unit_warning(UNIT(s
), "Service has RestartMaxDelaySec= but no RestartSteps= setting. Ignoring.");
699 if (s
->restart_max_delay_usec
< s
->restart_usec
) {
700 log_unit_warning(UNIT(s
), "RestartMaxDelaySec= has a value smaller than RestartSec=, resetting RestartSec= to RestartMaxDelaySec=.");
701 s
->restart_usec
= s
->restart_max_delay_usec
;
707 static int service_add_default_dependencies(Service
*s
) {
712 if (!UNIT(s
)->default_dependencies
)
715 /* Add a number of automatic dependencies useful for the
716 * majority of services. */
718 if (MANAGER_IS_SYSTEM(UNIT(s
)->manager
)) {
719 /* First, pull in the really early boot stuff, and
720 * require it, so that we fail if we can't acquire
723 r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_SYSINIT_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
728 /* In the --user instance there's no sysinit.target,
729 * in that case require basic.target instead. */
731 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
736 /* Second, if the rest of the base system is in the same
737 * transaction, order us after it, but do not pull it in or
738 * even require it. */
739 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, SPECIAL_BASIC_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
743 /* Third, add us in for normal shutdown. */
744 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
747 static void service_fix_stdio(Service
*s
) {
750 /* Note that EXEC_INPUT_NULL and EXEC_OUTPUT_INHERIT play a special role here: they are both the
751 * default value that is subject to automatic overriding triggered by other settings and an explicit
752 * choice the user can make. We don't distinguish between these cases currently. */
754 if (s
->exec_context
.std_input
== EXEC_INPUT_NULL
&&
755 s
->exec_context
.stdin_data_size
> 0)
756 s
->exec_context
.std_input
= EXEC_INPUT_DATA
;
758 if (IN_SET(s
->exec_context
.std_input
,
760 EXEC_INPUT_TTY_FORCE
,
763 EXEC_INPUT_NAMED_FD
))
766 /* We assume these listed inputs refer to bidirectional streams, and hence duplicating them from
767 * stdin to stdout/stderr makes sense and hence leaving EXEC_OUTPUT_INHERIT in place makes sense,
768 * too. Outputs such as regular files or sealed data memfds otoh don't really make sense to be
769 * duplicated for both input and output at the same time (since they then would cause a feedback
770 * loop), hence override EXEC_OUTPUT_INHERIT with the default stderr/stdout setting. */
772 if (s
->exec_context
.std_error
== EXEC_OUTPUT_INHERIT
&&
773 s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
)
774 s
->exec_context
.std_error
= UNIT(s
)->manager
->defaults
.std_error
;
776 if (s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
)
777 s
->exec_context
.std_output
= UNIT(s
)->manager
->defaults
.std_output
;
780 static int service_setup_bus_name(Service
*s
) {
785 /* If s->bus_name is not set, then the unit will be refused by service_verify() later. */
789 if (s
->type
== SERVICE_DBUS
) {
790 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_REQUIRES
, SPECIAL_DBUS_SOCKET
, true, UNIT_DEPENDENCY_FILE
);
792 return log_unit_error_errno(UNIT(s
), r
, "Failed to add dependency on " SPECIAL_DBUS_SOCKET
": %m");
794 /* We always want to be ordered against dbus.socket if both are in the transaction. */
795 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, SPECIAL_DBUS_SOCKET
, true, UNIT_DEPENDENCY_FILE
);
797 return log_unit_error_errno(UNIT(s
), r
, "Failed to add dependency on " SPECIAL_DBUS_SOCKET
": %m");
800 r
= unit_watch_bus_name(UNIT(s
), s
->bus_name
);
802 return log_unit_error_errno(UNIT(s
), r
, "Two services allocated for the same bus name %s, refusing operation.", s
->bus_name
);
804 return log_unit_error_errno(UNIT(s
), r
, "Cannot watch bus name %s: %m", s
->bus_name
);
809 static int service_add_extras(Service
*s
) {
814 if (s
->type
== _SERVICE_TYPE_INVALID
) {
815 /* Figure out a type automatically */
817 s
->type
= SERVICE_DBUS
;
818 else if (s
->exec_command
[SERVICE_EXEC_START
])
819 s
->type
= SERVICE_SIMPLE
;
821 s
->type
= SERVICE_ONESHOT
;
824 /* Oneshot services have disabled start timeout by default */
825 if (s
->type
== SERVICE_ONESHOT
&& !s
->start_timeout_defined
)
826 s
->timeout_start_usec
= USEC_INFINITY
;
828 service_fix_stdio(s
);
830 r
= unit_patch_contexts(UNIT(s
));
834 r
= unit_add_exec_dependencies(UNIT(s
), &s
->exec_context
);
838 r
= unit_set_default_slice(UNIT(s
));
842 /* If the service needs the notify socket, let's enable it automatically. */
843 if (s
->notify_access
== NOTIFY_NONE
&&
844 (IN_SET(s
->type
, SERVICE_NOTIFY
, SERVICE_NOTIFY_RELOAD
) || s
->watchdog_usec
> 0 || s
->n_fd_store_max
> 0))
845 s
->notify_access
= NOTIFY_MAIN
;
847 /* If no OOM policy was explicitly set, then default to the configure default OOM policy. Except when
848 * delegation is on, in that case it we assume the payload knows better what to do and can process
849 * things in a more focused way. */
850 if (s
->oom_policy
< 0)
851 s
->oom_policy
= s
->cgroup_context
.delegate
? OOM_CONTINUE
: UNIT(s
)->manager
->defaults
.oom_policy
;
853 /* Let the kernel do the killing if that's requested. */
854 s
->cgroup_context
.memory_oom_group
= s
->oom_policy
== OOM_KILL
;
856 r
= service_add_default_dependencies(s
);
860 r
= service_setup_bus_name(s
);
867 static int service_load(Unit
*u
) {
868 Service
*s
= SERVICE(u
);
871 r
= unit_load_fragment_and_dropin(u
, true);
875 if (u
->load_state
!= UNIT_LOADED
)
878 /* This is a new unit? Then let's add in some extras */
879 r
= service_add_extras(s
);
883 return service_verify(s
);
886 static void service_dump_fdstore(Service
*s
, FILE *f
, const char *prefix
) {
891 LIST_FOREACH(fd_store
, i
, s
->fd_store
) {
892 _cleanup_free_
char *path
= NULL
;
896 if (fstat(i
->fd
, &st
) < 0) {
897 log_debug_errno(errno
, "Failed to stat fdstore entry: %m");
901 flags
= fcntl(i
->fd
, F_GETFL
);
903 log_debug_errno(errno
, "Failed to get fdstore entry flags: %m");
907 (void) fd_get_path(i
->fd
, &path
);
910 "%s%s '%s' (type=%s; dev=" DEVNUM_FORMAT_STR
"; inode=%" PRIu64
"; rdev=" DEVNUM_FORMAT_STR
"; path=%s; access=%s)\n",
911 prefix
, i
== s
->fd_store
? "File Descriptor Store Entry:" : " ",
913 strna(inode_type_to_string(st
.st_mode
)),
914 DEVNUM_FORMAT_VAL(st
.st_dev
),
915 (uint64_t) st
.st_ino
,
916 DEVNUM_FORMAT_VAL(st
.st_rdev
),
918 strna(accmode_to_string(flags
)));
922 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
923 Service
*s
= SERVICE(u
);
928 prefix
= strempty(prefix
);
929 prefix2
= strjoina(prefix
, "\t");
932 "%sService State: %s\n"
934 "%sReload Result: %s\n"
935 "%sClean Result: %s\n"
936 "%sPermissionsStartOnly: %s\n"
937 "%sRootDirectoryStartOnly: %s\n"
938 "%sRemainAfterExit: %s\n"
939 "%sGuessMainPID: %s\n"
942 "%sNotifyAccess: %s\n"
943 "%sNotifyState: %s\n"
945 "%sReloadSignal: %s\n",
946 prefix
, service_state_to_string(s
->state
),
947 prefix
, service_result_to_string(s
->result
),
948 prefix
, service_result_to_string(s
->reload_result
),
949 prefix
, service_result_to_string(s
->clean_result
),
950 prefix
, yes_no(s
->permissions_start_only
),
951 prefix
, yes_no(s
->root_directory_start_only
),
952 prefix
, yes_no(s
->remain_after_exit
),
953 prefix
, yes_no(s
->guess_main_pid
),
954 prefix
, service_type_to_string(s
->type
),
955 prefix
, service_restart_to_string(s
->restart
),
956 prefix
, notify_access_to_string(service_get_notify_access(s
)),
957 prefix
, notify_state_to_string(s
->notify_state
),
958 prefix
, oom_policy_to_string(s
->oom_policy
),
959 prefix
, signal_to_string(s
->reload_signal
));
961 if (pidref_is_set(&s
->control_pid
))
963 "%sControl PID: "PID_FMT
"\n",
964 prefix
, s
->control_pid
.pid
);
966 if (pidref_is_set(&s
->main_pid
))
968 "%sMain PID: "PID_FMT
"\n"
969 "%sMain PID Known: %s\n"
970 "%sMain PID Alien: %s\n",
971 prefix
, s
->main_pid
.pid
,
972 prefix
, yes_no(s
->main_pid_known
),
973 prefix
, yes_no(s
->main_pid_alien
));
978 prefix
, s
->pid_file
);
983 "%sBus Name Good: %s\n",
985 prefix
, yes_no(s
->bus_name_good
));
987 if (UNIT_ISSET(s
->accept_socket
))
989 "%sAccept Socket: %s\n",
990 prefix
, UNIT_DEREF(s
->accept_socket
)->id
);
994 "%sRestartSteps: %u\n"
995 "%sRestartMaxDelaySec: %s\n"
996 "%sTimeoutStartSec: %s\n"
997 "%sTimeoutStopSec: %s\n"
998 "%sTimeoutStartFailureMode: %s\n"
999 "%sTimeoutStopFailureMode: %s\n",
1000 prefix
, FORMAT_TIMESPAN(s
->restart_usec
, USEC_PER_SEC
),
1001 prefix
, s
->restart_steps
,
1002 prefix
, FORMAT_TIMESPAN(s
->restart_max_delay_usec
, USEC_PER_SEC
),
1003 prefix
, FORMAT_TIMESPAN(s
->timeout_start_usec
, USEC_PER_SEC
),
1004 prefix
, FORMAT_TIMESPAN(s
->timeout_stop_usec
, USEC_PER_SEC
),
1005 prefix
, service_timeout_failure_mode_to_string(s
->timeout_start_failure_mode
),
1006 prefix
, service_timeout_failure_mode_to_string(s
->timeout_stop_failure_mode
));
1008 if (s
->timeout_abort_set
)
1010 "%sTimeoutAbortSec: %s\n",
1011 prefix
, FORMAT_TIMESPAN(s
->timeout_abort_usec
, USEC_PER_SEC
));
1014 "%sRuntimeMaxSec: %s\n"
1015 "%sRuntimeRandomizedExtraSec: %s\n"
1016 "%sWatchdogSec: %s\n",
1017 prefix
, FORMAT_TIMESPAN(s
->runtime_max_usec
, USEC_PER_SEC
),
1018 prefix
, FORMAT_TIMESPAN(s
->runtime_rand_extra_usec
, USEC_PER_SEC
),
1019 prefix
, FORMAT_TIMESPAN(s
->watchdog_usec
, USEC_PER_SEC
));
1021 kill_context_dump(&s
->kill_context
, f
, prefix
);
1022 exec_context_dump(&s
->exec_context
, f
, prefix
);
1024 for (ServiceExecCommand c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
1025 if (!s
->exec_command
[c
])
1028 fprintf(f
, "%s-> %s:\n",
1029 prefix
, service_exec_command_to_string(c
));
1031 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
1035 fprintf(f
, "%sStatus Text: %s\n",
1036 prefix
, s
->status_text
);
1038 if (s
->n_fd_store_max
> 0)
1040 "%sFile Descriptor Store Max: %u\n"
1041 "%sFile Descriptor Store Pin: %s\n"
1042 "%sFile Descriptor Store Current: %zu\n",
1043 prefix
, s
->n_fd_store_max
,
1044 prefix
, exec_preserve_mode_to_string(s
->fd_store_preserve_mode
),
1045 prefix
, s
->n_fd_store
);
1047 service_dump_fdstore(s
, f
, prefix
);
1050 LIST_FOREACH(open_files
, of
, s
->open_files
) {
1051 _cleanup_free_
char *ofs
= NULL
;
1054 r
= open_file_to_string(of
, &ofs
);
1057 "Failed to convert OpenFile= setting to string, ignoring: %m");
1061 fprintf(f
, "%sOpen File: %s\n", prefix
, ofs
);
1064 cgroup_context_dump(UNIT(s
), f
, prefix
);
1067 static int service_is_suitable_main_pid(Service
*s
, PidRef
*pid
, int prio
) {
1072 assert(pidref_is_set(pid
));
1074 /* Checks whether the specified PID is suitable as main PID for this service. returns negative if not, 0 if the
1075 * PID is questionnable but should be accepted if the source of configuration is trusted. > 0 if the PID is
1078 if (pidref_is_self(pid
) || pid
->pid
== 1)
1079 return log_unit_full_errno(UNIT(s
), prio
, SYNTHETIC_ERRNO(EPERM
), "New main PID "PID_FMT
" is the manager, refusing.", pid
->pid
);
1081 if (pidref_equal(pid
, &s
->control_pid
))
1082 return log_unit_full_errno(UNIT(s
), prio
, SYNTHETIC_ERRNO(EPERM
), "New main PID "PID_FMT
" is the control process, refusing.", pid
->pid
);
1084 r
= pidref_is_alive(pid
);
1086 return log_unit_full_errno(UNIT(s
), prio
, r
, "Failed to check if main PID "PID_FMT
" exists or is a zombie: %m", pid
->pid
);
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
->pid
);
1090 owner
= manager_get_unit_by_pidref(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
->pid
);
1093 return 1; /* Yay, it's definitely a good PID */
1096 return 0; /* Hmm it's a suspicious PID, let's accept it if configuration source is trusted */
1099 static int service_load_pid_file(Service
*s
, bool may_warn
) {
1100 _cleanup_(pidref_done
) PidRef pidref
= PIDREF_NULL
;
1101 bool questionable_pid_file
= false;
1102 _cleanup_free_
char *k
= NULL
;
1103 _cleanup_close_
int fd
= -EBADF
;
1111 prio
= may_warn
? LOG_INFO
: LOG_DEBUG
;
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
);
1118 questionable_pid_file
= true;
1120 r
= chase(s
->pid_file
, NULL
, 0, NULL
, &fd
);
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
));
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
);
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",
1134 r
= pidref_set_pidstr(&pidref
, k
);
1136 return log_unit_full_errno(UNIT(s
), prio
, r
, "Failed to parse PID from file %s: %m", s
->pid_file
);
1138 if (s
->main_pid_known
&& pidref_equal(&pidref
, &s
->main_pid
))
1141 r
= service_is_suitable_main_pid(s
, &pidref
, prio
);
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
);
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 */
1153 if (fstat(fd
, &st
) < 0)
1154 return log_unit_error_errno(UNIT(s
), errno
, "Failed to fstat() PID file O_PATH fd: %m");
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.", pidref
.pid
);
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.", pidref
.pid
);
1163 if (s
->main_pid_known
) {
1164 log_unit_debug(UNIT(s
), "Main PID changing: "PID_FMT
" -> "PID_FMT
, s
->main_pid
.pid
, pidref
.pid
);
1166 service_unwatch_main_pid(s
);
1167 s
->main_pid_known
= false;
1169 log_unit_debug(UNIT(s
), "Main PID loaded: "PID_FMT
, pidref
.pid
);
1171 r
= service_set_main_pidref(s
, &pidref
);
1175 r
= unit_watch_pidref(UNIT(s
), &s
->main_pid
, /* exclusive= */ 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", s
->main_pid
.pid
);
1182 static void service_search_main_pid(Service
*s
) {
1183 _cleanup_(pidref_done
) PidRef pid
= PIDREF_NULL
;
1188 /* If we know it anyway, don't ever fall back to unreliable heuristics */
1189 if (s
->main_pid_known
)
1192 if (!s
->guess_main_pid
)
1195 assert(!pidref_is_set(&s
->main_pid
));
1197 if (unit_search_main_pid(UNIT(s
), &pid
) < 0)
1200 log_unit_debug(UNIT(s
), "Main PID guessed: "PID_FMT
, pid
.pid
);
1201 if (service_set_main_pidref(s
, &pid
) < 0)
1204 r
= unit_watch_pidref(UNIT(s
), &s
->main_pid
, /* exclusive= */ false);
1206 /* FIXME: we need to do something here */
1207 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" from: %m", s
->main_pid
.pid
);
1210 static void service_set_state(Service
*s
, ServiceState state
) {
1211 ServiceState old_state
;
1212 const UnitActiveState
*table
;
1216 if (s
->state
!= state
)
1217 bus_unit_send_pending_change_signal(UNIT(s
), false);
1219 table
= s
->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
1221 old_state
= s
->state
;
1224 service_unwatch_pid_file(s
);
1227 SERVICE_CONDITION
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
1229 SERVICE_RELOAD
, SERVICE_RELOAD_SIGNAL
, SERVICE_RELOAD_NOTIFY
,
1230 SERVICE_STOP
, SERVICE_STOP_WATCHDOG
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1231 SERVICE_FINAL_WATCHDOG
, SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
,
1232 SERVICE_AUTO_RESTART
,
1234 s
->timer_event_source
= sd_event_source_disable_unref(s
->timer_event_source
);
1236 if (!SERVICE_STATE_WITH_MAIN_PROCESS(state
)) {
1237 service_unwatch_main_pid(s
);
1238 s
->main_command
= NULL
;
1241 if (!SERVICE_STATE_WITH_CONTROL_PROCESS(state
)) {
1242 service_unwatch_control_pid(s
);
1243 s
->control_command
= NULL
;
1244 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1248 SERVICE_DEAD
, SERVICE_FAILED
,
1249 SERVICE_DEAD_BEFORE_AUTO_RESTART
, SERVICE_FAILED_BEFORE_AUTO_RESTART
, SERVICE_AUTO_RESTART
, SERVICE_AUTO_RESTART_QUEUED
,
1250 SERVICE_DEAD_RESOURCES_PINNED
)) {
1251 unit_unwatch_all_pids(UNIT(s
));
1252 unit_dequeue_rewatch_pids(UNIT(s
));
1255 if (state
!= SERVICE_START
)
1256 s
->exec_fd_event_source
= sd_event_source_disable_unref(s
->exec_fd_event_source
);
1258 if (!IN_SET(state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
, SERVICE_RELOAD_SIGNAL
, SERVICE_RELOAD_NOTIFY
))
1259 service_stop_watchdog(s
);
1261 /* For the inactive states unit_notify() will trim the cgroup,
1262 * but for exit we have to do that ourselves... */
1263 if (state
== SERVICE_EXITED
&& !MANAGER_IS_RELOADING(UNIT(s
)->manager
))
1264 unit_prune_cgroup(UNIT(s
));
1266 if (old_state
!= state
)
1267 log_unit_debug(UNIT(s
), "Changed %s -> %s", service_state_to_string(old_state
), service_state_to_string(state
));
1269 unit_notify(UNIT(s
), table
[old_state
], table
[state
], s
->reload_result
== SERVICE_SUCCESS
);
1272 static usec_t
service_coldplug_timeout(Service
*s
) {
1275 switch (s
->deserialized_state
) {
1277 case SERVICE_CONDITION
:
1278 case SERVICE_START_PRE
:
1280 case SERVICE_START_POST
:
1281 case SERVICE_RELOAD
:
1282 case SERVICE_RELOAD_SIGNAL
:
1283 case SERVICE_RELOAD_NOTIFY
:
1284 return usec_add(UNIT(s
)->state_change_timestamp
.monotonic
, s
->timeout_start_usec
);
1286 case SERVICE_RUNNING
:
1287 return service_running_timeout(s
);
1290 case SERVICE_STOP_SIGTERM
:
1291 case SERVICE_STOP_SIGKILL
:
1292 case SERVICE_STOP_POST
:
1293 case SERVICE_FINAL_SIGTERM
:
1294 case SERVICE_FINAL_SIGKILL
:
1295 return usec_add(UNIT(s
)->state_change_timestamp
.monotonic
, s
->timeout_stop_usec
);
1297 case SERVICE_STOP_WATCHDOG
:
1298 case SERVICE_FINAL_WATCHDOG
:
1299 return usec_add(UNIT(s
)->state_change_timestamp
.monotonic
, service_timeout_abort_usec(s
));
1301 case SERVICE_AUTO_RESTART
:
1302 return usec_add(UNIT(s
)->inactive_enter_timestamp
.monotonic
, service_restart_usec_next(s
));
1304 case SERVICE_CLEANING
:
1305 return usec_add(UNIT(s
)->state_change_timestamp
.monotonic
, s
->exec_context
.timeout_clean_usec
);
1308 return USEC_INFINITY
;
1312 static int service_coldplug(Unit
*u
) {
1313 Service
*s
= SERVICE(u
);
1317 assert(s
->state
== SERVICE_DEAD
);
1319 if (s
->deserialized_state
== s
->state
)
1322 r
= service_arm_timer(s
, /* relative= */ false, service_coldplug_timeout(s
));
1326 if (pidref_is_set(&s
->main_pid
) &&
1327 pidref_is_unwaited(&s
->main_pid
) > 0 &&
1328 SERVICE_STATE_WITH_MAIN_PROCESS(s
->deserialized_state
)) {
1329 r
= unit_watch_pidref(UNIT(s
), &s
->main_pid
, /* exclusive= */ false);
1334 if (pidref_is_set(&s
->control_pid
) &&
1335 pidref_is_unwaited(&s
->control_pid
) > 0 &&
1336 SERVICE_STATE_WITH_CONTROL_PROCESS(s
->deserialized_state
)) {
1337 r
= unit_watch_pidref(UNIT(s
), &s
->control_pid
, /* exclusive= */ false);
1342 if (!IN_SET(s
->deserialized_state
,
1343 SERVICE_DEAD
, SERVICE_FAILED
,
1344 SERVICE_DEAD_BEFORE_AUTO_RESTART
, SERVICE_FAILED_BEFORE_AUTO_RESTART
, SERVICE_AUTO_RESTART
, SERVICE_AUTO_RESTART_QUEUED
,
1346 SERVICE_DEAD_RESOURCES_PINNED
)) {
1347 (void) unit_enqueue_rewatch_pids(u
);
1348 (void) unit_setup_exec_runtime(u
);
1349 (void) unit_setup_cgroup_runtime(u
);
1352 if (IN_SET(s
->deserialized_state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
, SERVICE_RELOAD_SIGNAL
, SERVICE_RELOAD_NOTIFY
))
1353 service_start_watchdog(s
);
1355 if (UNIT_ISSET(s
->accept_socket
)) {
1356 Socket
* socket
= SOCKET(UNIT_DEREF(s
->accept_socket
));
1358 if (socket
->max_connections_per_source
> 0) {
1361 /* Make a best-effort attempt at bumping the connection count */
1362 if (socket_acquire_peer(socket
, s
->socket_fd
, &peer
) > 0) {
1363 socket_peer_unref(s
->socket_peer
);
1364 s
->socket_peer
= peer
;
1369 service_set_state(s
, s
->deserialized_state
);
1373 static int service_collect_fds(
1377 size_t *n_socket_fds
,
1378 size_t *n_storage_fds
) {
1380 _cleanup_strv_free_
char **rfd_names
= NULL
;
1381 _cleanup_free_
int *rfds
= NULL
;
1382 size_t rn_socket_fds
= 0, rn_storage_fds
= 0;
1388 assert(n_socket_fds
);
1389 assert(n_storage_fds
);
1391 if (s
->socket_fd
>= 0) {
1393 /* Pass the per-connection socket */
1395 rfds
= newdup(int, &s
->socket_fd
, 1);
1399 rfd_names
= strv_new("connection");
1407 /* Pass all our configured sockets for singleton services */
1409 UNIT_FOREACH_DEPENDENCY(u
, UNIT(s
), UNIT_ATOM_TRIGGERED_BY
) {
1410 _cleanup_free_
int *cfds
= NULL
;
1414 if (u
->type
!= UNIT_SOCKET
)
1419 cn_fds
= socket_collect_fds(sock
, &cfds
);
1427 rfds
= TAKE_PTR(cfds
);
1428 rn_socket_fds
= cn_fds
;
1432 t
= reallocarray(rfds
, rn_socket_fds
+ cn_fds
, sizeof(int));
1436 memcpy(t
+ rn_socket_fds
, cfds
, cn_fds
* sizeof(int));
1439 rn_socket_fds
+= cn_fds
;
1442 r
= strv_extend_n(&rfd_names
, socket_fdname(sock
), cn_fds
);
1448 if (s
->n_fd_store
> 0) {
1453 t
= reallocarray(rfds
, rn_socket_fds
+ s
->n_fd_store
, sizeof(int));
1459 nl
= reallocarray(rfd_names
, rn_socket_fds
+ s
->n_fd_store
+ 1, sizeof(char *));
1464 n_fds
= rn_socket_fds
;
1466 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
1467 rfds
[n_fds
] = fs
->fd
;
1468 rfd_names
[n_fds
] = strdup(strempty(fs
->fdname
));
1469 if (!rfd_names
[n_fds
])
1476 rfd_names
[n_fds
] = NULL
;
1479 *fds
= TAKE_PTR(rfds
);
1480 *fd_names
= TAKE_PTR(rfd_names
);
1481 *n_socket_fds
= rn_socket_fds
;
1482 *n_storage_fds
= rn_storage_fds
;
1487 static int service_allocate_exec_fd_event_source(
1490 sd_event_source
**ret_event_source
) {
1492 _cleanup_(sd_event_source_unrefp
) sd_event_source
*source
= NULL
;
1497 assert(ret_event_source
);
1499 r
= sd_event_add_io(UNIT(s
)->manager
->event
, &source
, fd
, 0, service_dispatch_exec_io
, s
);
1501 return log_unit_error_errno(UNIT(s
), r
, "Failed to allocate exec_fd event source: %m");
1503 /* This is a bit higher priority than SIGCHLD, to make sure we don't confuse the case "failed to
1504 * start" from the case "succeeded to start, but failed immediately after". */
1506 r
= sd_event_source_set_priority(source
, EVENT_PRIORITY_EXEC_FD
);
1508 return log_unit_error_errno(UNIT(s
), r
, "Failed to adjust priority of exec_fd event source: %m");
1510 (void) sd_event_source_set_description(source
, "service exec_fd");
1512 r
= sd_event_source_set_io_fd_own(source
, true);
1514 return log_unit_error_errno(UNIT(s
), r
, "Failed to pass ownership of fd to event source: %m");
1516 *ret_event_source
= TAKE_PTR(source
);
1520 static int service_allocate_exec_fd(
1522 sd_event_source
**ret_event_source
,
1525 _cleanup_close_pair_
int p
[] = EBADF_PAIR
;
1529 assert(ret_event_source
);
1530 assert(ret_exec_fd
);
1532 if (pipe2(p
, O_CLOEXEC
|O_NONBLOCK
) < 0)
1533 return log_unit_error_errno(UNIT(s
), errno
, "Failed to allocate exec_fd pipe: %m");
1535 r
= service_allocate_exec_fd_event_source(s
, p
[0], ret_event_source
);
1540 *ret_exec_fd
= TAKE_FD(p
[1]);
1545 static bool service_exec_needs_notify_socket(Service
*s
, ExecFlags flags
) {
1548 /* Notifications are accepted depending on the process and
1549 * the access setting of the service:
1550 * process: \ access: NONE MAIN EXEC ALL
1551 * main no yes yes yes
1552 * control no no yes yes
1553 * other (forked) no no no yes */
1555 if (flags
& EXEC_IS_CONTROL
)
1556 /* A control process */
1557 return IN_SET(service_get_notify_access(s
), NOTIFY_EXEC
, NOTIFY_ALL
);
1559 /* We only spawn main processes and control processes, so any
1560 * process that is not a control process is a main process */
1561 return service_get_notify_access(s
) != NOTIFY_NONE
;
1564 static Service
*service_get_triggering_service(Service
*s
) {
1565 Unit
*candidate
= NULL
, *other
;
1569 /* Return the service which triggered service 's', this means dependency
1570 * types which include the UNIT_ATOM_ON_{FAILURE,SUCCESS}_OF atoms.
1572 * N.B. if there are multiple services which could trigger 's' via OnFailure=
1573 * or OnSuccess= then we return NULL. This is since we don't know from which
1574 * one to propagate the exit status. */
1576 UNIT_FOREACH_DEPENDENCY(other
, UNIT(s
), UNIT_ATOM_ON_FAILURE_OF
) {
1582 UNIT_FOREACH_DEPENDENCY(other
, UNIT(s
), UNIT_ATOM_ON_SUCCESS_OF
) {
1588 return SERVICE(candidate
);
1591 log_unit_warning(UNIT(s
), "multiple trigger source candidates for exit status propagation (%s, %s), skipping.",
1592 candidate
->id
, other
->id
);
1596 static ExecFlags
service_exec_flags(ServiceExecCommand command_id
, ExecFlags cred_flag
) {
1597 /* All service main/control processes honor sandboxing and namespacing options (except those
1598 explicitly excluded in service_spawn()) */
1599 ExecFlags flags
= EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
;
1601 assert(command_id
>= 0);
1602 assert(command_id
< _SERVICE_EXEC_COMMAND_MAX
);
1603 assert((cred_flag
& ~(EXEC_SETUP_CREDENTIALS_FRESH
|EXEC_SETUP_CREDENTIALS
)) == 0);
1604 assert((cred_flag
!= 0) == (command_id
== SERVICE_EXEC_START
));
1606 /* Control processes spawned before main process also get tty access */
1607 if (IN_SET(command_id
, SERVICE_EXEC_CONDITION
, SERVICE_EXEC_START_PRE
, SERVICE_EXEC_START
))
1608 flags
|= EXEC_APPLY_TTY_STDIN
;
1610 /* All start phases get access to credentials. ExecStartPre= gets a new credential store upon
1611 * every invocation, so that updating credential files through it works. When the first main process
1612 * starts, passed creds become stable. Also see 'cred_flag'. */
1613 if (command_id
== SERVICE_EXEC_START_PRE
)
1614 flags
|= EXEC_SETUP_CREDENTIALS_FRESH
;
1615 if (command_id
== SERVICE_EXEC_START_POST
)
1616 flags
|= EXEC_SETUP_CREDENTIALS
;
1618 if (IN_SET(command_id
, SERVICE_EXEC_START_PRE
, SERVICE_EXEC_START
))
1619 flags
|= EXEC_SETENV_MONITOR_RESULT
;
1621 if (command_id
== SERVICE_EXEC_START
)
1622 return flags
|cred_flag
|EXEC_PASS_FDS
|EXEC_SET_WATCHDOG
;
1624 flags
|= EXEC_IS_CONTROL
;
1626 /* Put control processes spawned later than main process under .control sub-cgroup if appropriate */
1627 if (!IN_SET(command_id
, SERVICE_EXEC_CONDITION
, SERVICE_EXEC_START_PRE
))
1628 flags
|= EXEC_CONTROL_CGROUP
;
1630 if (IN_SET(command_id
, SERVICE_EXEC_STOP
, SERVICE_EXEC_STOP_POST
))
1631 flags
|= EXEC_SETENV_RESULT
;
1636 static int service_spawn_internal(
1644 _cleanup_(exec_params_shallow_clear
) ExecParameters exec_params
= EXEC_PARAMETERS_INIT(flags
);
1645 _cleanup_(sd_event_source_unrefp
) sd_event_source
*exec_fd_source
= NULL
;
1646 _cleanup_strv_free_
char **final_env
= NULL
, **our_env
= NULL
;
1647 _cleanup_(pidref_done
) PidRef pidref
= PIDREF_NULL
;
1656 log_unit_debug(UNIT(s
), "Will spawn child (%s): %s", caller
, c
->path
);
1658 r
= unit_prepare_exec(UNIT(s
)); /* This realizes the cgroup, among other things */
1662 assert(!s
->exec_fd_event_source
);
1664 if (FLAGS_SET(exec_params
.flags
, EXEC_IS_CONTROL
)) {
1665 /* If this is a control process, mask the permissions/chroot application if this is requested. */
1666 if (s
->permissions_start_only
)
1667 exec_params
.flags
&= ~EXEC_APPLY_SANDBOXING
;
1668 if (s
->root_directory_start_only
)
1669 exec_params
.flags
&= ~EXEC_APPLY_CHROOT
;
1672 if (FLAGS_SET(exec_params
.flags
, EXEC_PASS_FDS
) ||
1673 s
->exec_context
.std_input
== EXEC_INPUT_SOCKET
||
1674 s
->exec_context
.std_output
== EXEC_OUTPUT_SOCKET
||
1675 s
->exec_context
.std_error
== EXEC_OUTPUT_SOCKET
) {
1677 r
= service_collect_fds(s
,
1679 &exec_params
.fd_names
,
1680 &exec_params
.n_socket_fds
,
1681 &exec_params
.n_storage_fds
);
1685 exec_params
.open_files
= s
->open_files
;
1687 log_unit_debug(UNIT(s
), "Passing %zu fds to service", exec_params
.n_socket_fds
+ exec_params
.n_storage_fds
);
1690 if (!FLAGS_SET(exec_params
.flags
, EXEC_IS_CONTROL
) && s
->type
== SERVICE_EXEC
) {
1691 r
= service_allocate_exec_fd(s
, &exec_fd_source
, &exec_params
.exec_fd
);
1696 r
= service_arm_timer(s
, /* relative= */ true, timeout
);
1700 our_env
= new0(char*, 13);
1704 if (service_exec_needs_notify_socket(s
, exec_params
.flags
)) {
1705 if (asprintf(our_env
+ n_env
++, "NOTIFY_SOCKET=%s", UNIT(s
)->manager
->notify_socket
) < 0)
1708 exec_params
.notify_socket
= UNIT(s
)->manager
->notify_socket
;
1710 if (s
->n_fd_store_max
> 0)
1711 if (asprintf(our_env
+ n_env
++, "FDSTORE=%u", s
->n_fd_store_max
) < 0)
1715 if (pidref_is_set(&s
->main_pid
))
1716 if (asprintf(our_env
+ n_env
++, "MAINPID="PID_FMT
, s
->main_pid
.pid
) < 0)
1719 if (MANAGER_IS_USER(UNIT(s
)->manager
))
1720 if (asprintf(our_env
+ n_env
++, "MANAGERPID="PID_FMT
, getpid_cached()) < 0)
1724 if (asprintf(our_env
+ n_env
++, "PIDFILE=%s", s
->pid_file
) < 0)
1727 if (s
->socket_fd
>= 0) {
1728 union sockaddr_union sa
;
1729 socklen_t salen
= sizeof(sa
);
1731 /* If this is a per-connection service instance, let's set $REMOTE_ADDR and $REMOTE_PORT to something
1732 * useful. Note that we do this only when we are still connected at this point in time, which we might
1733 * very well not be. Hence we ignore all errors when retrieving peer information (as that might result
1734 * in ENOTCONN), and just use whate we can use. */
1736 if (getpeername(s
->socket_fd
, &sa
.sa
, &salen
) >= 0 &&
1737 IN_SET(sa
.sa
.sa_family
, AF_INET
, AF_INET6
, AF_VSOCK
)) {
1738 _cleanup_free_
char *addr
= NULL
;
1742 r
= sockaddr_pretty(&sa
.sa
, salen
, true, false, &addr
);
1746 t
= strjoin("REMOTE_ADDR=", addr
);
1749 our_env
[n_env
++] = t
;
1751 r
= sockaddr_port(&sa
.sa
, &port
);
1755 if (asprintf(&t
, "REMOTE_PORT=%u", port
) < 0)
1757 our_env
[n_env
++] = t
;
1761 Service
*env_source
= NULL
;
1762 const char *monitor_prefix
;
1763 if (FLAGS_SET(exec_params
.flags
, EXEC_SETENV_RESULT
)) {
1765 monitor_prefix
= "";
1766 } else if (FLAGS_SET(exec_params
.flags
, EXEC_SETENV_MONITOR_RESULT
)) {
1767 env_source
= service_get_triggering_service(s
);
1768 monitor_prefix
= "MONITOR_";
1772 if (asprintf(our_env
+ n_env
++, "%sSERVICE_RESULT=%s", monitor_prefix
, service_result_to_string(env_source
->result
)) < 0)
1775 if (env_source
->main_exec_status
.pid
> 0 &&
1776 dual_timestamp_is_set(&env_source
->main_exec_status
.exit_timestamp
)) {
1777 if (asprintf(our_env
+ n_env
++, "%sEXIT_CODE=%s", monitor_prefix
, sigchld_code_to_string(env_source
->main_exec_status
.code
)) < 0)
1780 if (env_source
->main_exec_status
.code
== CLD_EXITED
)
1781 r
= asprintf(our_env
+ n_env
++, "%sEXIT_STATUS=%i", monitor_prefix
, env_source
->main_exec_status
.status
);
1783 r
= asprintf(our_env
+ n_env
++, "%sEXIT_STATUS=%s", monitor_prefix
, signal_to_string(env_source
->main_exec_status
.status
));
1788 if (env_source
!= s
) {
1789 if (!sd_id128_is_null(UNIT(env_source
)->invocation_id
))
1790 if (asprintf(our_env
+ n_env
++, "%sINVOCATION_ID=" SD_ID128_FORMAT_STR
,
1791 monitor_prefix
, SD_ID128_FORMAT_VAL(UNIT(env_source
)->invocation_id
)) < 0)
1794 if (asprintf(our_env
+ n_env
++, "%sUNIT=%s", monitor_prefix
, UNIT(env_source
)->id
) < 0)
1799 if (UNIT(s
)->activation_details
) {
1800 r
= activation_details_append_env(UNIT(s
)->activation_details
, &our_env
);
1803 /* The number of env vars added here can vary, rather than keeping the allocation block in
1804 * sync manually, these functions simply use the strv methods to append to it, so we need
1805 * to update n_env when we are done in case of future usage. */
1809 r
= unit_set_exec_params(UNIT(s
), &exec_params
);
1813 final_env
= strv_env_merge(exec_params
.environment
, our_env
);
1817 /* System D-Bus needs nss-systemd disabled, so that we don't deadlock */
1818 SET_FLAG(exec_params
.flags
, EXEC_NSS_DYNAMIC_BYPASS
,
1819 MANAGER_IS_SYSTEM(UNIT(s
)->manager
) && unit_has_name(UNIT(s
), SPECIAL_DBUS_SERVICE
));
1821 strv_free_and_replace(exec_params
.environment
, final_env
);
1822 exec_params
.watchdog_usec
= service_get_watchdog_usec(s
);
1823 exec_params
.selinux_context_net
= s
->socket_fd_selinux_context_net
;
1824 if (s
->type
== SERVICE_IDLE
)
1825 exec_params
.idle_pipe
= UNIT(s
)->manager
->idle_pipe
;
1826 exec_params
.stdin_fd
= s
->stdin_fd
;
1827 exec_params
.stdout_fd
= s
->stdout_fd
;
1828 exec_params
.stderr_fd
= s
->stderr_fd
;
1830 r
= exec_spawn(UNIT(s
),
1840 s
->exec_fd_event_source
= TAKE_PTR(exec_fd_source
);
1841 s
->exec_fd_hot
= false;
1843 r
= unit_watch_pidref(UNIT(s
), &pidref
, /* exclusive= */ true);
1847 *ret_pid
= TAKE_PIDREF(pidref
);
1851 static int main_pid_good(Service
*s
) {
1854 /* Returns 0 if the pid is dead, > 0 if it is good, < 0 if we don't know */
1856 /* If we know the pid file, then let's just check if it is still valid */
1857 if (s
->main_pid_known
) {
1859 /* If it's an alien child let's check if it is still alive ... */
1860 if (s
->main_pid_alien
&& pidref_is_set(&s
->main_pid
))
1861 return pidref_is_alive(&s
->main_pid
);
1863 /* .. otherwise assume we'll get a SIGCHLD for it, which we really should wait for to collect
1864 * exit status and code */
1865 return pidref_is_set(&s
->main_pid
);
1868 /* We don't know the pid */
1872 static int control_pid_good(Service
*s
) {
1875 /* Returns 0 if the control PID is dead, > 0 if it is good. We never actually return < 0 here, but in order to
1876 * make this function as similar as possible to main_pid_good() and cgroup_good(), we pretend that < 0 also
1877 * means: we can't figure it out. */
1879 return pidref_is_set(&s
->control_pid
);
1882 static int cgroup_good(Service
*s
) {
1887 /* Returns 0 if the cgroup is empty or doesn't exist, > 0 if it is exists and is populated, < 0 if we can't
1890 if (!s
->cgroup_runtime
|| !s
->cgroup_runtime
->cgroup_path
)
1893 r
= cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, s
->cgroup_runtime
->cgroup_path
);
1900 static bool service_shall_restart(Service
*s
, const char **reason
) {
1904 /* Don't restart after manual stops */
1905 if (s
->forbid_restart
) {
1906 *reason
= "manual stop";
1910 /* Never restart if this is configured as special exception */
1911 if (exit_status_set_test(&s
->restart_prevent_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
)) {
1912 *reason
= "prevented by exit status";
1916 /* Restart if the exit code/status are configured as restart triggers */
1917 if (exit_status_set_test(&s
->restart_force_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
)) {
1918 /* Don't allow Type=oneshot services to restart on success. Note that Restart=always/on-success
1919 * is already rejected in service_verify. */
1920 if (s
->type
== SERVICE_ONESHOT
&& s
->result
== SERVICE_SUCCESS
) {
1921 *reason
= "service type and exit status";
1925 *reason
= "forced by exit status";
1929 *reason
= "restart setting";
1930 switch (s
->restart
) {
1932 case SERVICE_RESTART_NO
:
1935 case SERVICE_RESTART_ALWAYS
:
1936 return s
->result
!= SERVICE_SKIP_CONDITION
;
1938 case SERVICE_RESTART_ON_SUCCESS
:
1939 return s
->result
== SERVICE_SUCCESS
;
1941 case SERVICE_RESTART_ON_FAILURE
:
1942 return !IN_SET(s
->result
, SERVICE_SUCCESS
, SERVICE_SKIP_CONDITION
);
1944 case SERVICE_RESTART_ON_ABNORMAL
:
1945 return !IN_SET(s
->result
, SERVICE_SUCCESS
, SERVICE_FAILURE_EXIT_CODE
, SERVICE_SKIP_CONDITION
);
1947 case SERVICE_RESTART_ON_WATCHDOG
:
1948 return s
->result
== SERVICE_FAILURE_WATCHDOG
;
1950 case SERVICE_RESTART_ON_ABORT
:
1951 return IN_SET(s
->result
, SERVICE_FAILURE_SIGNAL
, SERVICE_FAILURE_CORE_DUMP
);
1954 assert_not_reached();
1958 static bool service_will_restart(Unit
*u
) {
1959 Service
*s
= SERVICE(u
);
1963 if (IN_SET(s
->state
, SERVICE_DEAD_BEFORE_AUTO_RESTART
, SERVICE_FAILED_BEFORE_AUTO_RESTART
, SERVICE_AUTO_RESTART
, SERVICE_AUTO_RESTART_QUEUED
))
1966 return unit_will_restart_default(u
);
1969 static ServiceState
service_determine_dead_state(Service
*s
) {
1972 return s
->fd_store
&& s
->fd_store_preserve_mode
== EXEC_PRESERVE_YES
? SERVICE_DEAD_RESOURCES_PINNED
: SERVICE_DEAD
;
1975 static void service_enter_dead(Service
*s
, ServiceResult f
, bool allow_restart
) {
1976 ServiceState end_state
, restart_state
;
1981 /* If there's a stop job queued before we enter the DEAD state, we shouldn't act on Restart=, in order to not
1982 * undo what has already been enqueued. */
1983 if (unit_stop_pending(UNIT(s
)))
1984 allow_restart
= false;
1986 if (s
->result
== SERVICE_SUCCESS
)
1989 if (s
->result
== SERVICE_SUCCESS
) {
1990 unit_log_success(UNIT(s
));
1991 end_state
= service_determine_dead_state(s
);
1992 restart_state
= SERVICE_DEAD_BEFORE_AUTO_RESTART
;
1993 } else if (s
->result
== SERVICE_SKIP_CONDITION
) {
1994 unit_log_skip(UNIT(s
), service_result_to_string(s
->result
));
1995 end_state
= service_determine_dead_state(s
);
1996 restart_state
= SERVICE_DEAD_BEFORE_AUTO_RESTART
;
1998 unit_log_failure(UNIT(s
), service_result_to_string(s
->result
));
1999 end_state
= SERVICE_FAILED
;
2000 restart_state
= SERVICE_FAILED_BEFORE_AUTO_RESTART
;
2002 unit_warn_leftover_processes(UNIT(s
), unit_log_leftover_process_stop
);
2005 log_unit_debug(UNIT(s
), "Service restart not allowed.");
2009 allow_restart
= service_shall_restart(s
, &reason
);
2010 log_unit_debug(UNIT(s
), "Service will %srestart (%s)",
2011 allow_restart
? "" : "not ",
2015 if (allow_restart
) {
2016 usec_t restart_usec_next
;
2018 /* We make two state changes here: one that maps to the high-level UNIT_INACTIVE/UNIT_FAILED
2019 * state (i.e. a state indicating deactivation), and then one that that maps to the
2020 * high-level UNIT_STARTING state (i.e. a state indicating activation). We do this so that
2021 * external software can watch the state changes and see all service failures, even if they
2022 * are only transitionary and followed by an automatic restart. We have fine-grained
2023 * low-level states for this though so that software can distinguish the permanent UNIT_INACTIVE
2024 * state from this transitionary UNIT_INACTIVE state by looking at the low-level states. */
2025 if (s
->restart_mode
!= SERVICE_RESTART_MODE_DIRECT
)
2026 service_set_state(s
, restart_state
);
2028 restart_usec_next
= service_restart_usec_next(s
);
2030 r
= service_arm_timer(s
, /* relative= */ true, restart_usec_next
);
2032 log_unit_warning_errno(UNIT(s
), r
, "Failed to install restart timer: %m");
2033 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, /* allow_restart= */ false);
2037 log_unit_debug(UNIT(s
), "Next restart interval calculated as: %s", FORMAT_TIMESPAN(restart_usec_next
, 0));
2039 service_set_state(s
, SERVICE_AUTO_RESTART
);
2041 service_set_state(s
, end_state
);
2043 /* If we shan't restart, then flush out the restart counter. But don't do that immediately, so that the
2044 * user can still introspect the counter. Do so on the next start. */
2045 s
->flush_n_restarts
= true;
2048 /* The new state is in effect, let's decrease the fd store ref counter again. Let's also re-add us to the GC
2049 * queue, so that the fd store is possibly gc'ed again */
2050 unit_add_to_gc_queue(UNIT(s
));
2052 /* The next restart might not be a manual stop, hence reset the flag indicating manual stops */
2053 s
->forbid_restart
= false;
2055 /* Reset NotifyAccess override */
2056 s
->notify_access_override
= _NOTIFY_ACCESS_INVALID
;
2058 /* We want fresh tmpdirs and ephemeral snapshots in case the service is started again immediately. */
2059 s
->exec_runtime
= exec_runtime_destroy(s
->exec_runtime
);
2061 /* Also, remove the runtime directory */
2062 unit_destroy_runtime_data(UNIT(s
), &s
->exec_context
);
2064 /* Also get rid of the fd store, if that's configured. */
2065 if (s
->fd_store_preserve_mode
== EXEC_PRESERVE_NO
)
2066 service_release_fd_store(s
);
2068 /* Get rid of the IPC bits of the user */
2069 unit_unref_uid_gid(UNIT(s
), true);
2071 /* Try to delete the pid file. At this point it will be
2072 * out-of-date, and some software might be confused by it, so
2073 * let's remove it. */
2075 (void) unlink(s
->pid_file
);
2077 /* Reset TTY ownership if necessary */
2078 exec_context_revert_tty(&s
->exec_context
);
2081 static void service_enter_stop_post(Service
*s
, ServiceResult f
) {
2085 if (s
->result
== SERVICE_SUCCESS
)
2088 service_unwatch_control_pid(s
);
2089 (void) unit_enqueue_rewatch_pids(UNIT(s
));
2091 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
];
2092 if (s
->control_command
) {
2093 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
2094 pidref_done(&s
->control_pid
);
2096 r
= service_spawn(s
,
2098 service_exec_flags(s
->control_command_id
, /* cred_flag = */ 0),
2099 s
->timeout_stop_usec
,
2102 log_unit_warning_errno(UNIT(s
), r
, "Failed to spawn 'stop-post' task: %m");
2103 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2107 service_set_state(s
, SERVICE_STOP_POST
);
2109 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_SUCCESS
);
2112 static int state_to_kill_operation(Service
*s
, ServiceState state
) {
2115 case SERVICE_STOP_WATCHDOG
:
2116 case SERVICE_FINAL_WATCHDOG
:
2117 return KILL_WATCHDOG
;
2119 case SERVICE_STOP_SIGTERM
:
2120 if (unit_has_job_type(UNIT(s
), JOB_RESTART
))
2121 return KILL_RESTART
;
2124 case SERVICE_FINAL_SIGTERM
:
2125 return KILL_TERMINATE
;
2127 case SERVICE_STOP_SIGKILL
:
2128 case SERVICE_FINAL_SIGKILL
:
2132 return _KILL_OPERATION_INVALID
;
2136 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
) {
2137 int kill_operation
, r
;
2141 if (s
->result
== SERVICE_SUCCESS
)
2144 /* Before sending any signal, make sure we track all members of this cgroup */
2145 (void) unit_watch_all_pids(UNIT(s
));
2147 /* Also, enqueue a job that we recheck all our PIDs a bit later, given that it's likely some processes have
2149 (void) unit_enqueue_rewatch_pids(UNIT(s
));
2151 kill_operation
= state_to_kill_operation(s
, state
);
2152 r
= unit_kill_context(UNIT(s
), kill_operation
);
2154 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
2159 r
= service_arm_timer(s
, /* relative= */ true,
2160 kill_operation
== KILL_WATCHDOG
? service_timeout_abort_usec(s
) : s
->timeout_stop_usec
);
2162 log_unit_warning_errno(UNIT(s
), r
, "Failed to install timer: %m");
2166 service_set_state(s
, state
);
2167 } else if (IN_SET(state
, SERVICE_STOP_WATCHDOG
, SERVICE_STOP_SIGTERM
) && s
->kill_context
.send_sigkill
)
2168 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_SUCCESS
);
2169 else if (IN_SET(state
, SERVICE_STOP_WATCHDOG
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
2170 service_enter_stop_post(s
, SERVICE_SUCCESS
);
2171 else if (IN_SET(state
, SERVICE_FINAL_WATCHDOG
, SERVICE_FINAL_SIGTERM
) && s
->kill_context
.send_sigkill
)
2172 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_SUCCESS
);
2174 service_enter_dead(s
, SERVICE_SUCCESS
, /* allow_restart= */ true);
2179 if (IN_SET(state
, SERVICE_STOP_WATCHDOG
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
2180 service_enter_stop_post(s
, SERVICE_FAILURE_RESOURCES
);
2182 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, /* allow_restart= */ true);
2185 static void service_enter_stop_by_notify(Service
*s
) {
2190 (void) unit_enqueue_rewatch_pids(UNIT(s
));
2192 r
= service_arm_timer(s
, /* relative= */ true, s
->timeout_stop_usec
);
2194 log_unit_warning_errno(UNIT(s
), r
, "Failed to install timer: %m");
2195 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2199 /* The service told us it's stopping, so it's as if we SIGTERM'd it. */
2200 service_set_state(s
, SERVICE_STOP_SIGTERM
);
2203 static void service_enter_stop(Service
*s
, ServiceResult f
) {
2208 if (s
->result
== SERVICE_SUCCESS
)
2211 service_unwatch_control_pid(s
);
2212 (void) unit_enqueue_rewatch_pids(UNIT(s
));
2214 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
];
2215 if (s
->control_command
) {
2216 s
->control_command_id
= SERVICE_EXEC_STOP
;
2217 pidref_done(&s
->control_pid
);
2219 r
= service_spawn(s
,
2221 service_exec_flags(s
->control_command_id
, /* cred_flag = */ 0),
2222 s
->timeout_stop_usec
,
2225 log_unit_warning_errno(UNIT(s
), r
, "Failed to spawn 'stop' task: %m");
2226 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2230 service_set_state(s
, SERVICE_STOP
);
2232 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
2235 static bool service_good(Service
*s
) {
2239 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name_good
)
2242 main_pid_ok
= main_pid_good(s
);
2243 if (main_pid_ok
> 0) /* It's alive */
2245 if (main_pid_ok
== 0 && s
->exit_type
== SERVICE_EXIT_MAIN
) /* It's dead */
2248 /* OK, we don't know anything about the main PID, maybe
2249 * because there is none. Let's check the control group
2252 return cgroup_good(s
) != 0;
2255 static void service_enter_running(Service
*s
, ServiceResult f
) {
2260 if (s
->result
== SERVICE_SUCCESS
)
2263 service_unwatch_control_pid(s
);
2265 if (s
->result
!= SERVICE_SUCCESS
)
2266 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
2267 else if (service_good(s
)) {
2269 /* If there are any queued up sd_notify() notifications, process them now */
2270 if (s
->notify_state
== NOTIFY_RELOADING
)
2271 service_enter_reload_by_notify(s
);
2272 else if (s
->notify_state
== NOTIFY_STOPPING
)
2273 service_enter_stop_by_notify(s
);
2275 service_set_state(s
, SERVICE_RUNNING
);
2277 r
= service_arm_timer(s
, /* relative= */ false, service_running_timeout(s
));
2279 log_unit_warning_errno(UNIT(s
), r
, "Failed to install timer: %m");
2280 service_enter_running(s
, SERVICE_FAILURE_RESOURCES
);
2285 } else if (s
->remain_after_exit
)
2286 service_set_state(s
, SERVICE_EXITED
);
2288 service_enter_stop(s
, SERVICE_SUCCESS
);
2291 static void service_enter_start_post(Service
*s
) {
2295 service_unwatch_control_pid(s
);
2296 service_reset_watchdog(s
);
2298 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
];
2299 if (s
->control_command
) {
2300 s
->control_command_id
= SERVICE_EXEC_START_POST
;
2301 pidref_done(&s
->control_pid
);
2303 r
= service_spawn(s
,
2305 service_exec_flags(s
->control_command_id
, /* cred_flag = */ 0),
2306 s
->timeout_start_usec
,
2309 log_unit_warning_errno(UNIT(s
), r
, "Failed to spawn 'start-post' task: %m");
2310 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2314 service_set_state(s
, SERVICE_START_POST
);
2316 service_enter_running(s
, SERVICE_SUCCESS
);
2319 static void service_kill_control_process(Service
*s
) {
2324 if (!pidref_is_set(&s
->control_pid
))
2327 r
= pidref_kill_and_sigcont(&s
->control_pid
, SIGKILL
);
2329 _cleanup_free_
char *comm
= NULL
;
2331 (void) pidref_get_comm(&s
->control_pid
, &comm
);
2333 log_unit_debug_errno(UNIT(s
), r
, "Failed to kill control process " PID_FMT
" (%s), ignoring: %m",
2334 s
->control_pid
.pid
, strna(comm
));
2338 static int service_adverse_to_leftover_processes(Service
*s
) {
2341 /* KillMode=mixed and control group are used to indicate that all process should be killed off.
2342 * SendSIGKILL= is used for services that require a clean shutdown. These are typically database
2343 * service where a SigKilled process would result in a lengthy recovery and who's shutdown or startup
2344 * time is quite variable (so Timeout settings aren't of use).
2346 * Here we take these two factors and refuse to start a service if there are existing processes
2347 * within a control group. Databases, while generally having some protection against multiple
2348 * instances running, lets not stress the rigor of these. Also ExecStartPre= parts of the service
2349 * aren't as rigoriously written to protect aganst against multiple use. */
2351 if (unit_warn_leftover_processes(UNIT(s
), unit_log_leftover_process_start
) > 0 &&
2352 IN_SET(s
->kill_context
.kill_mode
, KILL_MIXED
, KILL_CONTROL_GROUP
) &&
2353 !s
->kill_context
.send_sigkill
)
2354 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(EBUSY
),
2355 "Will not start SendSIGKILL=no service of type KillMode=control-group or mixed while processes exist");
2360 static void service_enter_start(Service
*s
) {
2361 _cleanup_(pidref_done
) PidRef pidref
= PIDREF_NULL
;
2368 service_unwatch_control_pid(s
);
2369 service_unwatch_main_pid(s
);
2371 r
= service_adverse_to_leftover_processes(s
);
2375 if (s
->type
== SERVICE_FORKING
) {
2376 s
->control_command_id
= SERVICE_EXEC_START
;
2377 c
= s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
2379 s
->main_command
= NULL
;
2381 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
2382 s
->control_command
= NULL
;
2384 c
= s
->main_command
= s
->exec_command
[SERVICE_EXEC_START
];
2388 if (s
->type
!= SERVICE_ONESHOT
) {
2389 /* There's no command line configured for the main command? Hmm, that is strange.
2390 * This can only happen if the configuration changes at runtime. In this case,
2391 * let's enter a failure state. */
2392 r
= log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENXIO
), "There's no 'start' task anymore we could start.");
2396 /* We force a fake state transition here. Otherwise, the unit would go directly from
2397 * SERVICE_DEAD to SERVICE_DEAD without SERVICE_ACTIVATING or SERVICE_ACTIVE
2398 * in between. This way we can later trigger actions that depend on the state
2399 * transition, including SuccessAction=. */
2400 service_set_state(s
, SERVICE_START
);
2402 service_enter_start_post(s
);
2406 if (IN_SET(s
->type
, SERVICE_SIMPLE
, SERVICE_IDLE
))
2407 /* For simple + idle this is the main process. We don't apply any timeout here, but
2408 * service_enter_running() will later apply the .runtime_max_usec timeout. */
2409 timeout
= USEC_INFINITY
;
2411 timeout
= s
->timeout_start_usec
;
2413 r
= service_spawn(s
,
2415 service_exec_flags(SERVICE_EXEC_START
, EXEC_SETUP_CREDENTIALS_FRESH
),
2419 log_unit_warning_errno(UNIT(s
), r
, "Failed to spawn 'start' task: %m");
2423 if (IN_SET(s
->type
, SERVICE_SIMPLE
, SERVICE_IDLE
)) {
2424 /* For simple services we immediately start
2425 * the START_POST binaries. */
2427 (void) service_set_main_pidref(s
, &pidref
);
2428 service_enter_start_post(s
);
2430 } else if (s
->type
== SERVICE_FORKING
) {
2432 /* For forking services we wait until the start
2433 * process exited. */
2435 pidref_done(&s
->control_pid
);
2436 s
->control_pid
= TAKE_PIDREF(pidref
);
2437 service_set_state(s
, SERVICE_START
);
2439 } else if (IN_SET(s
->type
, SERVICE_ONESHOT
, SERVICE_DBUS
, SERVICE_NOTIFY
, SERVICE_NOTIFY_RELOAD
, SERVICE_EXEC
)) {
2441 /* For oneshot services we wait until the start process exited, too, but it is our main process. */
2443 /* For D-Bus services we know the main pid right away, but wait for the bus name to appear on the
2444 * bus. 'notify' and 'exec' services are similar. */
2446 (void) service_set_main_pidref(s
, &pidref
);
2447 service_set_state(s
, SERVICE_START
);
2449 assert_not_reached();
2454 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2457 static void service_enter_start_pre(Service
*s
) {
2462 service_unwatch_control_pid(s
);
2464 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
];
2465 if (s
->control_command
) {
2467 r
= service_adverse_to_leftover_processes(s
);
2471 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
2473 r
= service_spawn(s
,
2475 service_exec_flags(s
->control_command_id
, /* cred_flag = */ 0),
2476 s
->timeout_start_usec
,
2479 log_unit_warning_errno(UNIT(s
), r
, "Failed to spawn 'start-pre' task: %m");
2483 service_set_state(s
, SERVICE_START_PRE
);
2485 service_enter_start(s
);
2490 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, /* allow_restart= */ true);
2493 static void service_enter_condition(Service
*s
) {
2498 service_unwatch_control_pid(s
);
2500 s
->control_command
= s
->exec_command
[SERVICE_EXEC_CONDITION
];
2501 if (s
->control_command
) {
2503 r
= service_adverse_to_leftover_processes(s
);
2507 s
->control_command_id
= SERVICE_EXEC_CONDITION
;
2508 pidref_done(&s
->control_pid
);
2510 r
= service_spawn(s
,
2512 service_exec_flags(s
->control_command_id
, /* cred_flag = */ 0),
2513 s
->timeout_start_usec
,
2517 log_unit_warning_errno(UNIT(s
), r
, "Failed to spawn 'exec-condition' task: %m");
2521 service_set_state(s
, SERVICE_CONDITION
);
2523 service_enter_start_pre(s
);
2528 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, /* allow_restart= */ true);
2531 static void service_enter_restart(Service
*s
) {
2532 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2537 if (unit_has_job_type(UNIT(s
), JOB_STOP
)) {
2538 /* Don't restart things if we are going down anyway */
2539 log_unit_info(UNIT(s
), "Stop job pending for unit, skipping automatic restart.");
2543 /* Any units that are bound to this service must also be restarted. We use JOB_START for ourselves
2544 * but then set JOB_RESTART_DEPENDENCIES which will enqueue JOB_RESTART for those dependency jobs. */
2545 r
= manager_add_job(UNIT(s
)->manager
, JOB_START
, UNIT(s
), JOB_RESTART_DEPENDENCIES
, NULL
, &error
, NULL
);
2547 log_unit_warning(UNIT(s
), "Failed to schedule restart job: %s", bus_error_message(&error
, r
));
2548 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, /* allow_restart= */ false);
2552 /* Count the jobs we enqueue for restarting. This counter is maintained as long as the unit isn't
2553 * fully stopped, i.e. as long as it remains up or remains in auto-start states. The user can reset
2554 * the counter explicitly however via the usual "systemctl reset-failure" logic. */
2556 s
->flush_n_restarts
= false;
2558 s
->notify_access_override
= _NOTIFY_ACCESS_INVALID
;
2560 log_unit_struct(UNIT(s
), LOG_INFO
,
2561 "MESSAGE_ID=" SD_MESSAGE_UNIT_RESTART_SCHEDULED_STR
,
2562 LOG_UNIT_INVOCATION_ID(UNIT(s
)),
2563 LOG_UNIT_MESSAGE(UNIT(s
),
2564 "Scheduled restart job, restart counter is at %u.", s
->n_restarts
),
2565 "N_RESTARTS=%u", s
->n_restarts
);
2567 service_set_state(s
, SERVICE_AUTO_RESTART_QUEUED
);
2569 /* Notify clients about changed restart counter */
2570 unit_add_to_dbus_queue(UNIT(s
));
2573 static void service_enter_reload_by_notify(Service
*s
) {
2574 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2579 r
= service_arm_timer(s
, /* relative= */ true, s
->timeout_start_usec
);
2581 log_unit_warning_errno(UNIT(s
), r
, "Failed to install timer: %m");
2582 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
2583 service_enter_running(s
, SERVICE_SUCCESS
);
2587 service_set_state(s
, SERVICE_RELOAD_NOTIFY
);
2589 /* service_enter_reload_by_notify is never called during a reload, thus no loops are possible. */
2590 r
= manager_propagate_reload(UNIT(s
)->manager
, UNIT(s
), JOB_FAIL
, &error
);
2592 log_unit_warning(UNIT(s
), "Failed to schedule propagation of reload, ignoring: %s", bus_error_message(&error
, r
));
2595 static void service_enter_reload(Service
*s
) {
2596 bool killed
= false;
2601 service_unwatch_control_pid(s
);
2602 s
->reload_result
= SERVICE_SUCCESS
;
2604 usec_t ts
= now(CLOCK_MONOTONIC
);
2606 if (s
->type
== SERVICE_NOTIFY_RELOAD
&& pidref_is_set(&s
->main_pid
)) {
2607 r
= pidref_kill_and_sigcont(&s
->main_pid
, s
->reload_signal
);
2609 log_unit_warning_errno(UNIT(s
), r
, "Failed to send reload signal: %m");
2616 s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
];
2617 if (s
->control_command
) {
2618 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
2619 pidref_done(&s
->control_pid
);
2621 r
= service_spawn(s
,
2623 service_exec_flags(s
->control_command_id
, /* cred_flag = */ 0),
2624 s
->timeout_start_usec
,
2627 log_unit_warning_errno(UNIT(s
), r
, "Failed to spawn 'reload' task: %m");
2631 service_set_state(s
, SERVICE_RELOAD
);
2632 } else if (killed
) {
2633 r
= service_arm_timer(s
, /* relative= */ true, s
->timeout_start_usec
);
2635 log_unit_warning_errno(UNIT(s
), r
, "Failed to install timer: %m");
2639 service_set_state(s
, SERVICE_RELOAD_SIGNAL
);
2641 service_enter_running(s
, SERVICE_SUCCESS
);
2645 /* Store the timestamp when we started reloading: when reloading via SIGHUP we won't leave the reload
2646 * state until we received both RELOADING=1 and READY=1 with MONOTONIC_USEC= set to a value above
2647 * this. Thus we know for sure the reload cycle was executed *after* we requested it, and is not one
2648 * that was already in progress before. */
2649 s
->reload_begin_usec
= ts
;
2653 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
2654 service_enter_running(s
, SERVICE_SUCCESS
);
2657 static void service_run_next_control(Service
*s
) {
2662 assert(s
->control_command
);
2663 assert(s
->control_command
->command_next
);
2665 assert(s
->control_command_id
!= SERVICE_EXEC_START
);
2667 s
->control_command
= s
->control_command
->command_next
;
2668 service_unwatch_control_pid(s
);
2670 if (IN_SET(s
->state
, SERVICE_CONDITION
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
2671 timeout
= s
->timeout_start_usec
;
2673 timeout
= s
->timeout_stop_usec
;
2675 pidref_done(&s
->control_pid
);
2677 r
= service_spawn(s
,
2679 service_exec_flags(s
->control_command_id
, /* cred_flag = */ 0),
2683 log_unit_warning_errno(UNIT(s
), r
, "Failed to spawn next control task: %m");
2685 if (IN_SET(s
->state
, SERVICE_CONDITION
, SERVICE_START_PRE
, SERVICE_START_POST
, SERVICE_STOP
))
2686 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2687 else if (s
->state
== SERVICE_STOP_POST
)
2688 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, /* allow_restart= */ true);
2689 else if (s
->state
== SERVICE_RELOAD
) {
2690 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
2691 service_enter_running(s
, SERVICE_SUCCESS
);
2693 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2697 static void service_run_next_main(Service
*s
) {
2698 _cleanup_(pidref_done
) PidRef pidref
= PIDREF_NULL
;
2702 assert(s
->main_command
);
2703 assert(s
->main_command
->command_next
);
2704 assert(s
->type
== SERVICE_ONESHOT
);
2706 s
->main_command
= s
->main_command
->command_next
;
2707 service_unwatch_main_pid(s
);
2709 r
= service_spawn(s
,
2711 service_exec_flags(SERVICE_EXEC_START
, EXEC_SETUP_CREDENTIALS
),
2712 s
->timeout_start_usec
,
2715 log_unit_warning_errno(UNIT(s
), r
, "Failed to spawn next main task: %m");
2716 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2720 (void) service_set_main_pidref(s
, &pidref
);
2723 static int service_start(Unit
*u
) {
2724 Service
*s
= SERVICE(u
);
2729 /* We cannot fulfill this request right now, try again later
2731 if (IN_SET(s
->state
,
2732 SERVICE_STOP
, SERVICE_STOP_WATCHDOG
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
2733 SERVICE_FINAL_WATCHDOG
, SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
, SERVICE_CLEANING
))
2736 /* Already on it! */
2737 if (IN_SET(s
->state
, SERVICE_CONDITION
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
))
2740 /* A service that will be restarted must be stopped first to trigger BindsTo and/or OnFailure
2741 * dependencies. If a user does not want to wait for the holdoff time to elapse, the service should
2742 * be manually restarted, not started. We simply return EAGAIN here, so that any start jobs stay
2743 * queued, and assume that the auto restart timer will eventually trigger the restart. */
2744 if (IN_SET(s
->state
, SERVICE_AUTO_RESTART
, SERVICE_DEAD_BEFORE_AUTO_RESTART
, SERVICE_FAILED_BEFORE_AUTO_RESTART
))
2747 assert(IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_DEAD_RESOURCES_PINNED
, SERVICE_AUTO_RESTART_QUEUED
));
2749 r
= unit_acquire_invocation_id(u
);
2753 s
->result
= SERVICE_SUCCESS
;
2754 s
->reload_result
= SERVICE_SUCCESS
;
2755 s
->main_pid_known
= false;
2756 s
->main_pid_alien
= false;
2757 s
->forbid_restart
= false;
2759 s
->status_text
= mfree(s
->status_text
);
2760 s
->status_errno
= 0;
2762 s
->notify_access_override
= _NOTIFY_ACCESS_INVALID
;
2763 s
->notify_state
= NOTIFY_UNKNOWN
;
2765 s
->watchdog_original_usec
= s
->watchdog_usec
;
2766 s
->watchdog_override_enable
= false;
2767 s
->watchdog_override_usec
= USEC_INFINITY
;
2769 exec_command_reset_status_list_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
2770 exec_status_reset(&s
->main_exec_status
);
2772 /* This is not an automatic restart? Flush the restart counter then */
2773 if (s
->flush_n_restarts
) {
2775 s
->flush_n_restarts
= false;
2778 CGroupRuntime
*crt
= unit_get_cgroup_runtime(u
);
2780 crt
->reset_accounting
= true;
2782 service_enter_condition(s
);
2786 static int service_stop(Unit
*u
) {
2787 Service
*s
= SERVICE(u
);
2791 /* Don't create restart jobs from manual stops. */
2792 s
->forbid_restart
= true;
2797 case SERVICE_STOP_SIGTERM
:
2798 case SERVICE_STOP_SIGKILL
:
2799 case SERVICE_STOP_POST
:
2800 case SERVICE_FINAL_WATCHDOG
:
2801 case SERVICE_FINAL_SIGTERM
:
2802 case SERVICE_FINAL_SIGKILL
:
2806 case SERVICE_AUTO_RESTART
:
2807 case SERVICE_AUTO_RESTART_QUEUED
:
2808 /* Give up on the auto restart */
2809 service_set_state(s
, service_determine_dead_state(s
));
2812 case SERVICE_CONDITION
:
2813 case SERVICE_START_PRE
:
2815 case SERVICE_START_POST
:
2816 case SERVICE_RELOAD
:
2817 case SERVICE_RELOAD_SIGNAL
:
2818 case SERVICE_RELOAD_NOTIFY
:
2819 case SERVICE_STOP_WATCHDOG
:
2820 /* If there's already something running we go directly into kill mode. */
2821 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
2824 case SERVICE_CLEANING
:
2825 /* If we are currently cleaning, then abort it, brutally. */
2826 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_SUCCESS
);
2829 case SERVICE_RUNNING
:
2830 case SERVICE_EXITED
:
2831 service_enter_stop(s
, SERVICE_SUCCESS
);
2834 case SERVICE_DEAD_BEFORE_AUTO_RESTART
:
2835 case SERVICE_FAILED_BEFORE_AUTO_RESTART
:
2837 case SERVICE_FAILED
:
2838 case SERVICE_DEAD_RESOURCES_PINNED
:
2840 /* Unknown state, or unit_stop() should already have handled these */
2841 assert_not_reached();
2845 static int service_reload(Unit
*u
) {
2846 Service
*s
= SERVICE(u
);
2850 assert(IN_SET(s
->state
, SERVICE_RUNNING
, SERVICE_EXITED
));
2852 service_enter_reload(s
);
2856 static bool service_can_reload(Unit
*u
) {
2857 Service
*s
= SERVICE(u
);
2861 return s
->exec_command
[SERVICE_EXEC_RELOAD
] ||
2862 s
->type
== SERVICE_NOTIFY_RELOAD
;
2865 static unsigned service_exec_command_index(Unit
*u
, ServiceExecCommand id
, const ExecCommand
*current
) {
2866 Service
*s
= SERVICE(u
);
2871 assert(id
< _SERVICE_EXEC_COMMAND_MAX
);
2873 const ExecCommand
*first
= s
->exec_command
[id
];
2875 /* Figure out where we are in the list by walking back to the beginning */
2876 for (const ExecCommand
*c
= current
; c
!= first
; c
= c
->command_prev
)
2882 static int service_serialize_exec_command(Unit
*u
, FILE *f
, const ExecCommand
*command
) {
2883 _cleanup_free_
char *args
= NULL
, *p
= NULL
;
2884 Service
*s
= SERVICE(u
);
2885 const char *type
, *key
;
2886 ServiceExecCommand id
;
2896 if (command
== s
->control_command
) {
2898 id
= s
->control_command_id
;
2901 id
= SERVICE_EXEC_START
;
2904 idx
= service_exec_command_index(u
, id
, command
);
2906 STRV_FOREACH(arg
, command
->argv
) {
2907 _cleanup_free_
char *e
= NULL
;
2915 if (!GREEDY_REALLOC(args
, length
+ 2 + n
+ 2))
2919 args
[length
++] = ' ';
2921 args
[length
++] = '"';
2922 memcpy(args
+ length
, e
, n
);
2924 args
[length
++] = '"';
2927 if (!GREEDY_REALLOC(args
, length
+ 1))
2932 p
= cescape(command
->path
);
2936 key
= strjoina(type
, "-command");
2938 /* We use '+1234' instead of '1234' to mark the last command in a sequence.
2939 * This is used in service_deserialize_exec_command(). */
2940 (void) serialize_item_format(
2943 service_exec_command_to_string(id
),
2944 command
->command_next
? "" : "+",
2951 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
2952 Service
*s
= SERVICE(u
);
2959 (void) serialize_item(f
, "state", service_state_to_string(s
->state
));
2960 (void) serialize_item(f
, "result", service_result_to_string(s
->result
));
2961 (void) serialize_item(f
, "reload-result", service_result_to_string(s
->reload_result
));
2963 (void) serialize_pidref(f
, fds
, "control-pid", &s
->control_pid
);
2964 if (s
->main_pid_known
)
2965 (void) serialize_pidref(f
, fds
, "main-pid", &s
->main_pid
);
2967 (void) serialize_bool(f
, "main-pid-known", s
->main_pid_known
);
2968 (void) serialize_bool(f
, "bus-name-good", s
->bus_name_good
);
2969 (void) serialize_bool(f
, "bus-name-owner", s
->bus_name_owner
);
2971 (void) serialize_item_format(f
, "n-restarts", "%u", s
->n_restarts
);
2972 (void) serialize_bool(f
, "flush-n-restarts", s
->flush_n_restarts
);
2974 r
= serialize_item_escaped(f
, "status-text", s
->status_text
);
2978 service_serialize_exec_command(u
, f
, s
->control_command
);
2979 service_serialize_exec_command(u
, f
, s
->main_command
);
2981 r
= serialize_fd(f
, fds
, "stdin-fd", s
->stdin_fd
);
2984 r
= serialize_fd(f
, fds
, "stdout-fd", s
->stdout_fd
);
2987 r
= serialize_fd(f
, fds
, "stderr-fd", s
->stderr_fd
);
2991 if (s
->exec_fd_event_source
) {
2992 r
= serialize_fd(f
, fds
, "exec-fd", sd_event_source_get_io_fd(s
->exec_fd_event_source
));
2996 (void) serialize_bool(f
, "exec-fd-hot", s
->exec_fd_hot
);
2999 if (UNIT_ISSET(s
->accept_socket
)) {
3000 r
= serialize_item(f
, "accept-socket", UNIT_DEREF(s
->accept_socket
)->id
);
3005 r
= serialize_fd(f
, fds
, "socket-fd", s
->socket_fd
);
3009 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
3010 _cleanup_free_
char *c
= NULL
;
3013 copy
= fdset_put_dup(fds
, fs
->fd
);
3015 return log_error_errno(copy
, "Failed to copy file descriptor for serialization: %m");
3017 c
= cescape(fs
->fdname
);
3021 (void) serialize_item_format(f
, "fd-store-fd", "%i \"%s\" %i", copy
, c
, fs
->do_poll
);
3024 if (s
->main_exec_status
.pid
> 0) {
3025 (void) serialize_item_format(f
, "main-exec-status-pid", PID_FMT
, s
->main_exec_status
.pid
);
3026 (void) serialize_dual_timestamp(f
, "main-exec-status-start", &s
->main_exec_status
.start_timestamp
);
3027 (void) serialize_dual_timestamp(f
, "main-exec-status-exit", &s
->main_exec_status
.exit_timestamp
);
3029 if (dual_timestamp_is_set(&s
->main_exec_status
.exit_timestamp
)) {
3030 (void) serialize_item_format(f
, "main-exec-status-code", "%i", s
->main_exec_status
.code
);
3031 (void) serialize_item_format(f
, "main-exec-status-status", "%i", s
->main_exec_status
.status
);
3035 if (s
->notify_access_override
>= 0)
3036 (void) serialize_item(f
, "notify-access-override", notify_access_to_string(s
->notify_access_override
));
3038 (void) serialize_dual_timestamp(f
, "watchdog-timestamp", &s
->watchdog_timestamp
);
3039 (void) serialize_bool(f
, "forbid-restart", s
->forbid_restart
);
3041 if (s
->watchdog_override_enable
)
3042 (void) serialize_item_format(f
, "watchdog-override-usec", USEC_FMT
, s
->watchdog_override_usec
);
3044 if (s
->watchdog_original_usec
!= USEC_INFINITY
)
3045 (void) serialize_item_format(f
, "watchdog-original-usec", USEC_FMT
, s
->watchdog_original_usec
);
3047 if (s
->reload_begin_usec
!= USEC_INFINITY
)
3048 (void) serialize_item_format(f
, "reload-begin-usec", USEC_FMT
, s
->reload_begin_usec
);
3053 int service_deserialize_exec_command(
3056 const char *value
) {
3058 Service
*s
= SERVICE(u
);
3060 unsigned idx
= 0, i
;
3061 bool control
, found
= false, last
= false;
3062 ServiceExecCommand id
= _SERVICE_EXEC_COMMAND_INVALID
;
3063 ExecCommand
*command
= NULL
;
3064 _cleanup_free_
char *path
= NULL
;
3065 _cleanup_strv_free_
char **argv
= NULL
;
3067 enum ExecCommandState
{
3068 STATE_EXEC_COMMAND_TYPE
,
3069 STATE_EXEC_COMMAND_INDEX
,
3070 STATE_EXEC_COMMAND_PATH
,
3071 STATE_EXEC_COMMAND_ARGS
,
3072 _STATE_EXEC_COMMAND_MAX
,
3073 _STATE_EXEC_COMMAND_INVALID
= -EINVAL
,
3080 control
= streq(key
, "control-command");
3082 state
= STATE_EXEC_COMMAND_TYPE
;
3085 _cleanup_free_
char *arg
= NULL
;
3087 r
= extract_first_word(&value
, &arg
, NULL
, EXTRACT_CUNESCAPE
| EXTRACT_UNQUOTE
);
3094 case STATE_EXEC_COMMAND_TYPE
:
3095 id
= service_exec_command_from_string(arg
);
3099 state
= STATE_EXEC_COMMAND_INDEX
;
3101 case STATE_EXEC_COMMAND_INDEX
:
3102 /* PID 1234 is serialized as either '1234' or '+1234'. The second form is used to
3103 * mark the last command in a sequence. We warn if the deserialized command doesn't
3104 * match what we have loaded from the unit, but we don't need to warn if that is the
3107 r
= safe_atou(arg
, &idx
);
3110 last
= arg
[0] == '+';
3112 state
= STATE_EXEC_COMMAND_PATH
;
3114 case STATE_EXEC_COMMAND_PATH
:
3115 path
= TAKE_PTR(arg
);
3116 state
= STATE_EXEC_COMMAND_ARGS
;
3118 case STATE_EXEC_COMMAND_ARGS
:
3119 r
= strv_extend(&argv
, arg
);
3124 assert_not_reached();
3128 if (state
!= STATE_EXEC_COMMAND_ARGS
)
3130 if (strv_isempty(argv
))
3131 return -EINVAL
; /* At least argv[0] must be always present. */
3133 /* Let's check whether exec command on given offset matches data that we just deserialized */
3134 for (command
= s
->exec_command
[id
], i
= 0; command
; command
= command
->command_next
, i
++) {
3138 found
= strv_equal(argv
, command
->argv
) && streq(command
->path
, path
);
3143 /* Command at the index we serialized is different, let's look for command that exactly
3144 * matches but is on different index. If there is no such command we will not resume execution. */
3145 for (command
= s
->exec_command
[id
]; command
; command
= command
->command_next
)
3146 if (strv_equal(command
->argv
, argv
) && streq(command
->path
, path
))
3150 if (command
&& control
) {
3151 s
->control_command
= command
;
3152 s
->control_command_id
= id
;
3154 s
->main_command
= command
;
3156 log_unit_debug(u
, "Current command vanished from the unit file.");
3158 log_unit_warning(u
, "Current command vanished from the unit file, execution of the command list won't be resumed.");
3163 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
3164 Service
*s
= SERVICE(u
);
3172 if (streq(key
, "state")) {
3175 state
= service_state_from_string(value
);
3177 log_unit_debug(u
, "Failed to parse state value: %s", value
);
3179 s
->deserialized_state
= state
;
3180 } else if (streq(key
, "result")) {
3183 f
= service_result_from_string(value
);
3185 log_unit_debug(u
, "Failed to parse result value: %s", value
);
3186 else if (f
!= SERVICE_SUCCESS
)
3189 } else if (streq(key
, "reload-result")) {
3192 f
= service_result_from_string(value
);
3194 log_unit_debug(u
, "Failed to parse reload result value: %s", value
);
3195 else if (f
!= SERVICE_SUCCESS
)
3196 s
->reload_result
= f
;
3198 } else if (streq(key
, "control-pid")) {
3199 pidref_done(&s
->control_pid
);
3201 (void) deserialize_pidref(fds
, value
, &s
->control_pid
);
3203 } else if (streq(key
, "main-pid")) {
3204 _cleanup_(pidref_done
) PidRef pidref
= PIDREF_NULL
;
3206 if (deserialize_pidref(fds
, value
, &pidref
) >= 0)
3207 (void) service_set_main_pidref(s
, &pidref
);
3209 } else if (streq(key
, "main-pid-known")) {
3212 b
= parse_boolean(value
);
3214 log_unit_debug(u
, "Failed to parse main-pid-known value: %s", value
);
3216 s
->main_pid_known
= b
;
3217 } else if (streq(key
, "bus-name-good")) {
3220 b
= parse_boolean(value
);
3222 log_unit_debug(u
, "Failed to parse bus-name-good value: %s", value
);
3224 s
->bus_name_good
= b
;
3225 } else if (streq(key
, "bus-name-owner")) {
3226 r
= free_and_strdup(&s
->bus_name_owner
, value
);
3228 log_unit_error_errno(u
, r
, "Unable to deserialize current bus owner %s: %m", value
);
3229 } else if (streq(key
, "status-text")) {
3233 l
= cunescape(value
, 0, &t
);
3235 log_unit_debug_errno(u
, l
, "Failed to unescape status text '%s': %m", value
);
3237 free_and_replace(s
->status_text
, t
);
3239 } else if (streq(key
, "accept-socket")) {
3242 if (u
->type
!= UNIT_SOCKET
) {
3243 log_unit_debug(u
, "Failed to deserialize accept-socket: unit is not a socket");
3247 r
= manager_load_unit(u
->manager
, value
, NULL
, NULL
, &socket
);
3249 log_unit_debug_errno(u
, r
, "Failed to load accept-socket unit '%s': %m", value
);
3251 unit_ref_set(&s
->accept_socket
, u
, socket
);
3252 SOCKET(socket
)->n_connections
++;
3255 } else if (streq(key
, "socket-fd")) {
3256 asynchronous_close(s
->socket_fd
);
3257 s
->socket_fd
= deserialize_fd(fds
, value
);
3259 } else if (streq(key
, "fd-store-fd")) {
3260 _cleanup_free_
char *fdv
= NULL
, *fdn
= NULL
, *fdp
= NULL
;
3261 _cleanup_close_
int fd
= -EBADF
;
3264 r
= extract_first_word(&value
, &fdv
, NULL
, 0);
3266 log_unit_debug(u
, "Failed to parse fd-store-fd value, ignoring: %s", value
);
3270 fd
= deserialize_fd(fds
, fdv
);
3274 r
= extract_first_word(&value
, &fdn
, NULL
, EXTRACT_CUNESCAPE
| EXTRACT_UNQUOTE
);
3276 log_unit_debug(u
, "Failed to parse fd-store-fd value, ignoring: %s", value
);
3280 r
= extract_first_word(&value
, &fdp
, NULL
, 0);
3282 /* If the value is not present, we assume the default */
3284 } else if (r
< 0 || (r
= safe_atoi(fdp
, &do_poll
)) < 0) {
3285 log_unit_debug_errno(u
, r
, "Failed to parse fd-store-fd value \"%s\", ignoring: %m", value
);
3289 r
= service_add_fd_store(s
, fd
, fdn
, do_poll
);
3291 log_unit_debug_errno(u
, r
, "Failed to store deserialized fd %i, ignoring: %m", fd
);
3296 } else if (streq(key
, "main-exec-status-pid")) {
3299 if (parse_pid(value
, &pid
) < 0)
3300 log_unit_debug(u
, "Failed to parse main-exec-status-pid value: %s", value
);
3302 s
->main_exec_status
.pid
= pid
;
3303 } else if (streq(key
, "main-exec-status-code")) {
3306 if (safe_atoi(value
, &i
) < 0)
3307 log_unit_debug(u
, "Failed to parse main-exec-status-code value: %s", value
);
3309 s
->main_exec_status
.code
= i
;
3310 } else if (streq(key
, "main-exec-status-status")) {
3313 if (safe_atoi(value
, &i
) < 0)
3314 log_unit_debug(u
, "Failed to parse main-exec-status-status value: %s", value
);
3316 s
->main_exec_status
.status
= i
;
3317 } else if (streq(key
, "main-exec-status-start"))
3318 deserialize_dual_timestamp(value
, &s
->main_exec_status
.start_timestamp
);
3319 else if (streq(key
, "main-exec-status-exit"))
3320 deserialize_dual_timestamp(value
, &s
->main_exec_status
.exit_timestamp
);
3321 else if (streq(key
, "notify-access-override")) {
3322 NotifyAccess notify_access
;
3324 notify_access
= notify_access_from_string(value
);
3325 if (notify_access
< 0)
3326 log_unit_debug(u
, "Failed to parse notify-access-override value: %s", value
);
3328 s
->notify_access_override
= notify_access
;
3329 } else if (streq(key
, "watchdog-timestamp"))
3330 deserialize_dual_timestamp(value
, &s
->watchdog_timestamp
);
3331 else if (streq(key
, "forbid-restart")) {
3334 b
= parse_boolean(value
);
3336 log_unit_debug(u
, "Failed to parse forbid-restart value: %s", value
);
3338 s
->forbid_restart
= b
;
3339 } else if (streq(key
, "stdin-fd")) {
3341 asynchronous_close(s
->stdin_fd
);
3342 s
->stdin_fd
= deserialize_fd(fds
, value
);
3343 if (s
->stdin_fd
>= 0)
3344 s
->exec_context
.stdio_as_fds
= true;
3346 } else if (streq(key
, "stdout-fd")) {
3348 asynchronous_close(s
->stdout_fd
);
3349 s
->stdout_fd
= deserialize_fd(fds
, value
);
3350 if (s
->stdout_fd
>= 0)
3351 s
->exec_context
.stdio_as_fds
= true;
3353 } else if (streq(key
, "stderr-fd")) {
3355 asynchronous_close(s
->stderr_fd
);
3356 s
->stderr_fd
= deserialize_fd(fds
, value
);
3357 if (s
->stderr_fd
>= 0)
3358 s
->exec_context
.stdio_as_fds
= true;
3360 } else if (streq(key
, "exec-fd")) {
3361 _cleanup_close_
int fd
= -EBADF
;
3363 fd
= deserialize_fd(fds
, value
);
3365 s
->exec_fd_event_source
= sd_event_source_disable_unref(s
->exec_fd_event_source
);
3367 if (service_allocate_exec_fd_event_source(s
, fd
, &s
->exec_fd_event_source
) >= 0)
3371 } else if (streq(key
, "watchdog-override-usec")) {
3372 if (deserialize_usec(value
, &s
->watchdog_override_usec
) < 0)
3373 log_unit_debug(u
, "Failed to parse watchdog_override_usec value: %s", value
);
3375 s
->watchdog_override_enable
= true;
3377 } else if (streq(key
, "watchdog-original-usec")) {
3378 if (deserialize_usec(value
, &s
->watchdog_original_usec
) < 0)
3379 log_unit_debug(u
, "Failed to parse watchdog_original_usec value: %s", value
);
3381 } else if (STR_IN_SET(key
, "main-command", "control-command")) {
3382 r
= service_deserialize_exec_command(u
, key
, value
);
3384 log_unit_debug_errno(u
, r
, "Failed to parse serialized command \"%s\": %m", value
);
3386 } else if (streq(key
, "n-restarts")) {
3387 r
= safe_atou(value
, &s
->n_restarts
);
3389 log_unit_debug_errno(u
, r
, "Failed to parse serialized restart counter '%s': %m", value
);
3391 } else if (streq(key
, "flush-n-restarts")) {
3392 r
= parse_boolean(value
);
3394 log_unit_debug_errno(u
, r
, "Failed to parse serialized flush restart counter setting '%s': %m", value
);
3396 s
->flush_n_restarts
= r
;
3397 } else if (streq(key
, "reload-begin-usec")) {
3398 r
= deserialize_usec(value
, &s
->reload_begin_usec
);
3400 log_unit_debug_errno(u
, r
, "Failed to parse serialized reload begin timestamp '%s', ignoring: %m", value
);
3402 log_unit_debug(u
, "Unknown serialization key: %s", key
);
3407 static UnitActiveState
service_active_state(Unit
*u
) {
3408 const UnitActiveState
*table
;
3412 table
= SERVICE(u
)->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
3414 return table
[SERVICE(u
)->state
];
3417 static const char *service_sub_state_to_string(Unit
*u
) {
3420 return service_state_to_string(SERVICE(u
)->state
);
3423 static bool service_may_gc(Unit
*u
) {
3424 Service
*s
= SERVICE(u
);
3428 /* Never clean up services that still have a process around, even if the service is formally dead. Note that
3429 * unit_may_gc() already checked our cgroup for us, we just check our two additional PIDs, too, in case they
3430 * have moved outside of the cgroup. */
3432 if (main_pid_good(s
) > 0 ||
3433 control_pid_good(s
) > 0)
3436 /* Only allow collection of actually dead services, i.e. not those that are in the transitionary
3437 * SERVICE_DEAD_BEFORE_AUTO_RESTART/SERVICE_FAILED_BEFORE_AUTO_RESTART states. */
3438 if (!IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_DEAD_RESOURCES_PINNED
))
3444 static int service_retry_pid_file(Service
*s
) {
3447 assert(s
->pid_file
);
3448 assert(IN_SET(s
->state
, SERVICE_START
, SERVICE_START_POST
));
3450 r
= service_load_pid_file(s
, false);
3454 service_unwatch_pid_file(s
);
3456 service_enter_running(s
, SERVICE_SUCCESS
);
3460 static int service_watch_pid_file(Service
*s
) {
3463 log_unit_debug(UNIT(s
), "Setting watch for PID file %s", s
->pid_file_pathspec
->path
);
3465 r
= path_spec_watch(s
->pid_file_pathspec
, service_dispatch_inotify_io
);
3467 log_unit_error_errno(UNIT(s
), r
, "Failed to set a watch for PID file %s: %m", s
->pid_file_pathspec
->path
);
3468 service_unwatch_pid_file(s
);
3472 /* the pidfile might have appeared just before we set the watch */
3473 log_unit_debug(UNIT(s
), "Trying to read PID file %s in case it changed", s
->pid_file_pathspec
->path
);
3474 service_retry_pid_file(s
);
3479 static int service_demand_pid_file(Service
*s
) {
3480 _cleanup_free_ PathSpec
*ps
= NULL
;
3482 assert(s
->pid_file
);
3483 assert(!s
->pid_file_pathspec
);
3485 ps
= new(PathSpec
, 1);
3491 .path
= strdup(s
->pid_file
),
3492 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that keep their PID file
3493 * open all the time. */
3494 .type
= PATH_MODIFIED
,
3495 .inotify_fd
= -EBADF
,
3501 path_simplify(ps
->path
);
3503 s
->pid_file_pathspec
= TAKE_PTR(ps
);
3505 return service_watch_pid_file(s
);
3508 static int service_dispatch_inotify_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
) {
3509 PathSpec
*p
= ASSERT_PTR(userdata
);
3512 s
= SERVICE(p
->unit
);
3516 assert(IN_SET(s
->state
, SERVICE_START
, SERVICE_START_POST
));
3517 assert(s
->pid_file_pathspec
);
3518 assert(path_spec_owns_inotify_fd(s
->pid_file_pathspec
, fd
));
3520 log_unit_debug(UNIT(s
), "inotify event");
3522 if (path_spec_fd_event(p
, events
) < 0)
3525 if (service_retry_pid_file(s
) == 0)
3528 if (service_watch_pid_file(s
) < 0)
3534 service_unwatch_pid_file(s
);
3535 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
3539 static int service_dispatch_exec_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
) {
3540 Service
*s
= SERVICE(userdata
);
3544 log_unit_debug(UNIT(s
), "got exec-fd event");
3546 /* If Type=exec is set, we'll consider a service started successfully the instant we invoked execve()
3547 * successfully for it. We implement this through a pipe() towards the child, which the kernel automatically
3548 * closes for us due to O_CLOEXEC on execve() in the child, which then triggers EOF on the pipe in the
3549 * parent. We need to be careful however, as there are other reasons that we might cause the child's side of
3550 * the pipe to be closed (for example, a simple exit()). To deal with that we'll ignore EOFs on the pipe unless
3551 * the child signalled us first that it is about to call the execve(). It does so by sending us a simple
3552 * non-zero byte via the pipe. We also provide the child with a way to inform us in case execve() failed: if it
3553 * sends a zero byte we'll ignore POLLHUP on the fd again. */
3559 n
= read(fd
, &x
, sizeof(x
));
3561 if (errno
== EAGAIN
) /* O_NONBLOCK in effect → everything queued has now been processed. */
3564 return log_unit_error_errno(UNIT(s
), errno
, "Failed to read from exec_fd: %m");
3566 if (n
== 0) { /* EOF → the event we are waiting for */
3568 s
->exec_fd_event_source
= sd_event_source_disable_unref(s
->exec_fd_event_source
);
3570 if (s
->exec_fd_hot
) { /* Did the child tell us to expect EOF now? */
3571 log_unit_debug(UNIT(s
), "Got EOF on exec-fd");
3573 s
->exec_fd_hot
= false;
3575 /* Nice! This is what we have been waiting for. Transition to next state. */
3576 if (s
->type
== SERVICE_EXEC
&& s
->state
== SERVICE_START
)
3577 service_enter_start_post(s
);
3579 log_unit_debug(UNIT(s
), "Got EOF on exec-fd while it was disabled, ignoring.");
3584 /* A byte was read → this turns on/off the exec fd logic */
3585 assert(n
== sizeof(x
));
3592 static void service_notify_cgroup_empty_event(Unit
*u
) {
3593 Service
*s
= SERVICE(u
);
3597 log_unit_debug(u
, "Control group is empty.");
3601 /* Waiting for SIGCHLD is usually more interesting, because it includes return
3602 * codes/signals. Which is why we ignore the cgroup events for most cases, except when we
3603 * don't know pid which to expect the SIGCHLD for. */
3606 if (IN_SET(s
->type
, SERVICE_NOTIFY
, SERVICE_NOTIFY_RELOAD
) &&
3607 main_pid_good(s
) == 0 &&
3608 control_pid_good(s
) == 0) {
3609 /* No chance of getting a ready notification anymore */
3610 service_enter_stop_post(s
, SERVICE_FAILURE_PROTOCOL
);
3614 if (s
->exit_type
== SERVICE_EXIT_CGROUP
&& main_pid_good(s
) <= 0)
3615 service_enter_start_post(s
);
3618 case SERVICE_START_POST
:
3619 if (s
->pid_file_pathspec
&&
3620 main_pid_good(s
) == 0 &&
3621 control_pid_good(s
) == 0) {
3623 /* Give up hoping for the daemon to write its PID file */
3624 log_unit_warning(u
, "Daemon never wrote its PID file. Failing.");
3626 service_unwatch_pid_file(s
);
3627 if (s
->state
== SERVICE_START
)
3628 service_enter_stop_post(s
, SERVICE_FAILURE_PROTOCOL
);
3630 service_enter_stop(s
, SERVICE_FAILURE_PROTOCOL
);
3634 case SERVICE_RUNNING
:
3635 /* service_enter_running() will figure out what to do */
3636 service_enter_running(s
, SERVICE_SUCCESS
);
3639 case SERVICE_STOP_WATCHDOG
:
3640 case SERVICE_STOP_SIGTERM
:
3641 case SERVICE_STOP_SIGKILL
:
3643 if (main_pid_good(s
) <= 0 && control_pid_good(s
) <= 0)
3644 service_enter_stop_post(s
, SERVICE_SUCCESS
);
3648 case SERVICE_STOP_POST
:
3649 case SERVICE_FINAL_WATCHDOG
:
3650 case SERVICE_FINAL_SIGTERM
:
3651 case SERVICE_FINAL_SIGKILL
:
3652 if (main_pid_good(s
) <= 0 && control_pid_good(s
) <= 0)
3653 service_enter_dead(s
, SERVICE_SUCCESS
, true);
3657 /* If the cgroup empty notification comes when the unit is not active, we must have failed to clean
3658 * up the cgroup earlier and should do it now. */
3659 case SERVICE_AUTO_RESTART
:
3660 case SERVICE_AUTO_RESTART_QUEUED
:
3661 unit_prune_cgroup(u
);
3669 static void service_notify_cgroup_oom_event(Unit
*u
, bool managed_oom
) {
3670 Service
*s
= SERVICE(u
);
3673 log_unit_debug(u
, "Process(es) of control group were killed by systemd-oomd.");
3675 log_unit_debug(u
, "Process of control group was killed by the OOM killer.");
3677 if (s
->oom_policy
== OOM_CONTINUE
)
3682 case SERVICE_CONDITION
:
3683 case SERVICE_START_PRE
:
3685 case SERVICE_START_POST
:
3687 if (s
->oom_policy
== OOM_STOP
)
3688 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_OOM_KILL
);
3689 else if (s
->oom_policy
== OOM_KILL
)
3690 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_OOM_KILL
);
3694 case SERVICE_EXITED
:
3695 case SERVICE_RUNNING
:
3696 if (s
->oom_policy
== OOM_STOP
)
3697 service_enter_stop(s
, SERVICE_FAILURE_OOM_KILL
);
3698 else if (s
->oom_policy
== OOM_KILL
)
3699 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_OOM_KILL
);
3703 case SERVICE_STOP_WATCHDOG
:
3704 case SERVICE_STOP_SIGTERM
:
3705 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_OOM_KILL
);
3708 case SERVICE_STOP_SIGKILL
:
3709 case SERVICE_FINAL_SIGKILL
:
3710 if (s
->result
== SERVICE_SUCCESS
)
3711 s
->result
= SERVICE_FAILURE_OOM_KILL
;
3714 case SERVICE_STOP_POST
:
3715 case SERVICE_FINAL_SIGTERM
:
3716 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_OOM_KILL
);
3724 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
3725 bool notify_dbus
= true;
3726 Service
*s
= SERVICE(u
);
3728 ExitClean clean_mode
;
3734 /* Oneshot services and non-SERVICE_EXEC_START commands should not be
3735 * considered daemons as they are typically not long running. */
3736 if (s
->type
== SERVICE_ONESHOT
|| (s
->control_pid
.pid
== pid
&& s
->control_command_id
!= SERVICE_EXEC_START
))
3737 clean_mode
= EXIT_CLEAN_COMMAND
;
3739 clean_mode
= EXIT_CLEAN_DAEMON
;
3741 if (is_clean_exit(code
, status
, clean_mode
, &s
->success_status
))
3742 f
= SERVICE_SUCCESS
;
3743 else if (code
== CLD_EXITED
)
3744 f
= SERVICE_FAILURE_EXIT_CODE
;
3745 else if (code
== CLD_KILLED
)
3746 f
= SERVICE_FAILURE_SIGNAL
;
3747 else if (code
== CLD_DUMPED
)
3748 f
= SERVICE_FAILURE_CORE_DUMP
;
3750 assert_not_reached();
3752 if (s
->main_pid
.pid
== pid
) {
3753 /* Clean up the exec_fd event source. We want to do this here, not later in
3754 * service_set_state(), because service_enter_stop_post() calls service_spawn().
3755 * The source owns its end of the pipe, so this will close that too. */
3756 s
->exec_fd_event_source
= sd_event_source_disable_unref(s
->exec_fd_event_source
);
3758 /* Forking services may occasionally move to a new PID.
3759 * As long as they update the PID file before exiting the old
3760 * PID, they're fine. */
3761 if (service_load_pid_file(s
, false) > 0)
3764 pidref_done(&s
->main_pid
);
3765 exec_status_exit(&s
->main_exec_status
, &s
->exec_context
, pid
, code
, status
);
3767 if (s
->main_command
) {
3768 /* If this is not a forking service than the
3769 * main process got started and hence we copy
3770 * the exit status so that it is recorded both
3771 * as main and as control process exit
3774 s
->main_command
->exec_status
= s
->main_exec_status
;
3776 if (s
->main_command
->flags
& EXEC_COMMAND_IGNORE_FAILURE
)
3777 f
= SERVICE_SUCCESS
;
3778 } else if (s
->exec_command
[SERVICE_EXEC_START
]) {
3780 /* If this is a forked process, then we should
3781 * ignore the return value if this was
3782 * configured for the starter process */
3784 if (s
->exec_command
[SERVICE_EXEC_START
]->flags
& EXEC_COMMAND_IGNORE_FAILURE
)
3785 f
= SERVICE_SUCCESS
;
3788 unit_log_process_exit(
3791 service_exec_command_to_string(SERVICE_EXEC_START
),
3792 f
== SERVICE_SUCCESS
,
3795 if (s
->result
== SERVICE_SUCCESS
)
3798 if (s
->main_command
&&
3799 s
->main_command
->command_next
&&
3800 s
->type
== SERVICE_ONESHOT
&&
3801 f
== SERVICE_SUCCESS
) {
3803 /* There is another command to execute, so let's do that. */
3805 log_unit_debug(u
, "Running next main command for state %s.", service_state_to_string(s
->state
));
3806 service_run_next_main(s
);
3809 s
->main_command
= NULL
;
3811 /* Services with ExitType=cgroup do not act on main PID exiting, unless the cgroup is
3813 if (s
->exit_type
== SERVICE_EXIT_MAIN
|| cgroup_good(s
) <= 0) {
3814 /* The service exited, so the service is officially gone. */
3817 case SERVICE_START_POST
:
3818 case SERVICE_RELOAD
:
3819 case SERVICE_RELOAD_SIGNAL
:
3820 case SERVICE_RELOAD_NOTIFY
:
3821 /* If neither main nor control processes are running then the current
3822 * state can never exit cleanly, hence immediately terminate the
3824 if (control_pid_good(s
) <= 0)
3825 service_enter_stop(s
, f
);
3827 /* Otherwise need to wait until the operation is done. */
3831 /* Need to wait until the operation is done. */
3835 if (s
->type
== SERVICE_ONESHOT
) {
3836 /* This was our main goal, so let's go on */
3837 if (f
== SERVICE_SUCCESS
)
3838 service_enter_start_post(s
);
3840 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
3842 } else if (IN_SET(s
->type
, SERVICE_NOTIFY
, SERVICE_NOTIFY_RELOAD
)) {
3843 /* Only enter running through a notification, so that the
3844 * SERVICE_START state signifies that no ready notification
3845 * has been received */
3846 if (f
!= SERVICE_SUCCESS
)
3847 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
3848 else if (!s
->remain_after_exit
|| service_get_notify_access(s
) == NOTIFY_MAIN
)
3849 /* The service has never been and will never be active */
3850 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_PROTOCOL
);
3855 case SERVICE_RUNNING
:
3856 service_enter_running(s
, f
);
3859 case SERVICE_STOP_WATCHDOG
:
3860 case SERVICE_STOP_SIGTERM
:
3861 case SERVICE_STOP_SIGKILL
:
3863 if (control_pid_good(s
) <= 0)
3864 service_enter_stop_post(s
, f
);
3866 /* If there is still a control process, wait for that first */
3869 case SERVICE_STOP_POST
:
3871 if (control_pid_good(s
) <= 0)
3872 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
3876 case SERVICE_FINAL_WATCHDOG
:
3877 case SERVICE_FINAL_SIGTERM
:
3878 case SERVICE_FINAL_SIGKILL
:
3880 if (control_pid_good(s
) <= 0)
3881 service_enter_dead(s
, f
, true);
3885 assert_not_reached();
3887 } else if (s
->exit_type
== SERVICE_EXIT_CGROUP
&& s
->state
== SERVICE_START
)
3888 /* If a main process exits very quickly, this function might be executed
3889 * before service_dispatch_exec_io(). Since this function disabled IO events
3890 * to monitor the main process above, we need to update the state here too.
3891 * Let's consider the process is successfully launched and exited. */
3892 service_enter_start_post(s
);
3895 } else if (s
->control_pid
.pid
== pid
) {
3899 pidref_done(&s
->control_pid
);
3901 if (s
->control_command
) {
3902 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
3904 if (s
->control_command
->flags
& EXEC_COMMAND_IGNORE_FAILURE
)
3905 f
= SERVICE_SUCCESS
;
3908 /* ExecCondition= calls that exit with (0, 254] should invoke skip-like behavior instead of failing */
3909 if (s
->state
== SERVICE_CONDITION
) {
3910 if (f
== SERVICE_FAILURE_EXIT_CODE
&& status
< 255) {
3911 UNIT(s
)->condition_result
= false;
3912 f
= SERVICE_SKIP_CONDITION
;
3914 } else if (f
== SERVICE_SUCCESS
) {
3915 UNIT(s
)->condition_result
= true;
3920 kind
= "Condition check process";
3922 kind
= "Control process";
3923 success
= f
== SERVICE_SUCCESS
;
3926 unit_log_process_exit(
3929 service_exec_command_to_string(s
->control_command_id
),
3933 if (s
->state
!= SERVICE_RELOAD
&& s
->result
== SERVICE_SUCCESS
)
3936 if (s
->control_command
&&
3937 s
->control_command
->command_next
&&
3938 f
== SERVICE_SUCCESS
) {
3940 /* There is another command to execute, so let's do that. */
3942 log_unit_debug(u
, "Running next control command for state %s.", service_state_to_string(s
->state
));
3943 service_run_next_control(s
);
3946 /* No further commands for this step, so let's figure out what to do next */
3948 s
->control_command
= NULL
;
3949 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
3951 log_unit_debug(u
, "Got final SIGCHLD for state %s.", service_state_to_string(s
->state
));
3955 case SERVICE_CONDITION
:
3956 if (f
== SERVICE_SUCCESS
)
3957 service_enter_start_pre(s
);
3959 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
3962 case SERVICE_START_PRE
:
3963 if (f
== SERVICE_SUCCESS
)
3964 service_enter_start(s
);
3966 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
3970 if (s
->type
!= SERVICE_FORKING
)
3971 /* Maybe spurious event due to a reload that changed the type? */
3974 if (f
!= SERVICE_SUCCESS
) {
3975 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
3980 bool has_start_post
;
3982 /* Let's try to load the pid file here if we can.
3983 * The PID file might actually be created by a START_POST
3984 * script. In that case don't worry if the loading fails. */
3986 has_start_post
= s
->exec_command
[SERVICE_EXEC_START_POST
];
3987 r
= service_load_pid_file(s
, !has_start_post
);
3988 if (!has_start_post
&& r
< 0) {
3989 r
= service_demand_pid_file(s
);
3990 if (r
< 0 || cgroup_good(s
) == 0)
3991 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_PROTOCOL
);
3995 service_search_main_pid(s
);
3997 service_enter_start_post(s
);
4000 case SERVICE_START_POST
:
4001 if (f
!= SERVICE_SUCCESS
) {
4002 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
4007 r
= service_load_pid_file(s
, true);
4009 r
= service_demand_pid_file(s
);
4010 if (r
< 0 || cgroup_good(s
) == 0)
4011 service_enter_stop(s
, SERVICE_FAILURE_PROTOCOL
);
4015 service_search_main_pid(s
);
4017 service_enter_running(s
, SERVICE_SUCCESS
);
4020 case SERVICE_RELOAD
:
4021 case SERVICE_RELOAD_SIGNAL
:
4022 case SERVICE_RELOAD_NOTIFY
:
4023 if (f
== SERVICE_SUCCESS
)
4024 if (service_load_pid_file(s
, true) < 0)
4025 service_search_main_pid(s
);
4027 s
->reload_result
= f
;
4029 /* If the last notification we received from the service process indicates
4030 * we are still reloading, then don't leave reloading state just yet, just
4031 * transition into SERVICE_RELOAD_NOTIFY, to wait for the READY=1 coming,
4033 if (s
->notify_state
== NOTIFY_RELOADING
)
4034 service_set_state(s
, SERVICE_RELOAD_NOTIFY
);
4036 service_enter_running(s
, SERVICE_SUCCESS
);
4040 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
4043 case SERVICE_STOP_WATCHDOG
:
4044 case SERVICE_STOP_SIGTERM
:
4045 case SERVICE_STOP_SIGKILL
:
4046 if (main_pid_good(s
) <= 0)
4047 service_enter_stop_post(s
, f
);
4049 /* If there is still a service process around, wait until
4050 * that one quit, too */
4053 case SERVICE_STOP_POST
:
4054 if (main_pid_good(s
) <= 0)
4055 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
4058 case SERVICE_FINAL_WATCHDOG
:
4059 case SERVICE_FINAL_SIGTERM
:
4060 case SERVICE_FINAL_SIGKILL
:
4061 if (main_pid_good(s
) <= 0)
4062 service_enter_dead(s
, f
, true);
4065 case SERVICE_CLEANING
:
4067 if (s
->clean_result
== SERVICE_SUCCESS
)
4068 s
->clean_result
= f
;
4070 service_enter_dead(s
, SERVICE_SUCCESS
, false);
4074 assert_not_reached();
4077 } else /* Neither control nor main PID? If so, don't notify about anything */
4078 notify_dbus
= false;
4080 /* Notify clients about changed exit status */
4082 unit_add_to_dbus_queue(u
);
4084 /* We watch the main/control process otherwise we can't retrieve the unit they
4085 * belong to with cgroupv1. But if they are not our direct child, we won't get a
4086 * SIGCHLD for them. Therefore we need to look for others to watch so we can
4087 * detect when the cgroup becomes empty. Note that the control process is always
4088 * our child so it's pointless to watch all other processes. */
4089 if (!control_pid_good(s
))
4090 if (!s
->main_pid_known
|| s
->main_pid_alien
)
4091 (void) unit_enqueue_rewatch_pids(u
);
4094 static int service_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
4095 Service
*s
= SERVICE(userdata
);
4098 assert(source
== s
->timer_event_source
);
4102 case SERVICE_CONDITION
:
4103 case SERVICE_START_PRE
:
4105 case SERVICE_START_POST
:
4106 switch (s
->timeout_start_failure_mode
) {
4108 case SERVICE_TIMEOUT_TERMINATE
:
4109 log_unit_warning(UNIT(s
), "%s operation timed out. Terminating.", service_state_to_string(s
->state
));
4110 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
4113 case SERVICE_TIMEOUT_ABORT
:
4114 log_unit_warning(UNIT(s
), "%s operation timed out. Aborting.", service_state_to_string(s
->state
));
4115 service_enter_signal(s
, SERVICE_STOP_WATCHDOG
, SERVICE_FAILURE_TIMEOUT
);
4118 case SERVICE_TIMEOUT_KILL
:
4119 if (s
->kill_context
.send_sigkill
) {
4120 log_unit_warning(UNIT(s
), "%s operation timed out. Killing.", service_state_to_string(s
->state
));
4121 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
4123 log_unit_warning(UNIT(s
), "%s operation timed out. Skipping SIGKILL.", service_state_to_string(s
->state
));
4124 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
4129 assert_not_reached();
4133 case SERVICE_RUNNING
:
4134 log_unit_warning(UNIT(s
), "Service reached runtime time limit. Stopping.");
4135 service_enter_stop(s
, SERVICE_FAILURE_TIMEOUT
);
4138 case SERVICE_RELOAD
:
4139 case SERVICE_RELOAD_SIGNAL
:
4140 case SERVICE_RELOAD_NOTIFY
:
4141 log_unit_warning(UNIT(s
), "Reload operation timed out. Killing reload process.");
4142 service_kill_control_process(s
);
4143 s
->reload_result
= SERVICE_FAILURE_TIMEOUT
;
4144 service_enter_running(s
, SERVICE_SUCCESS
);
4148 switch (s
->timeout_stop_failure_mode
) {
4150 case SERVICE_TIMEOUT_TERMINATE
:
4151 log_unit_warning(UNIT(s
), "Stopping timed out. Terminating.");
4152 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
4155 case SERVICE_TIMEOUT_ABORT
:
4156 log_unit_warning(UNIT(s
), "Stopping timed out. Aborting.");
4157 service_enter_signal(s
, SERVICE_STOP_WATCHDOG
, SERVICE_FAILURE_TIMEOUT
);
4160 case SERVICE_TIMEOUT_KILL
:
4161 if (s
->kill_context
.send_sigkill
) {
4162 log_unit_warning(UNIT(s
), "Stopping timed out. Killing.");
4163 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
4165 log_unit_warning(UNIT(s
), "Stopping timed out. Skipping SIGKILL.");
4166 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
4171 assert_not_reached();
4175 case SERVICE_STOP_WATCHDOG
:
4176 if (s
->kill_context
.send_sigkill
) {
4177 log_unit_warning(UNIT(s
), "State 'stop-watchdog' timed out. Killing.");
4178 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
4180 log_unit_warning(UNIT(s
), "State 'stop-watchdog' timed out. Skipping SIGKILL.");
4181 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
4185 case SERVICE_STOP_SIGTERM
:
4186 if (s
->timeout_stop_failure_mode
== SERVICE_TIMEOUT_ABORT
) {
4187 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Aborting.");
4188 service_enter_signal(s
, SERVICE_STOP_WATCHDOG
, SERVICE_FAILURE_TIMEOUT
);
4189 } else if (s
->kill_context
.send_sigkill
) {
4190 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Killing.");
4191 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
4193 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Skipping SIGKILL.");
4194 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
4199 case SERVICE_STOP_SIGKILL
:
4200 /* Uh, we sent a SIGKILL and it is still not gone?
4201 * Must be something we cannot kill, so let's just be
4202 * weirded out and continue */
4204 log_unit_warning(UNIT(s
), "Processes still around after SIGKILL. Ignoring.");
4205 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
4208 case SERVICE_STOP_POST
:
4209 switch (s
->timeout_stop_failure_mode
) {
4211 case SERVICE_TIMEOUT_TERMINATE
:
4212 log_unit_warning(UNIT(s
), "State 'stop-post' timed out. Terminating.");
4213 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
4216 case SERVICE_TIMEOUT_ABORT
:
4217 log_unit_warning(UNIT(s
), "State 'stop-post' timed out. Aborting.");
4218 service_enter_signal(s
, SERVICE_FINAL_WATCHDOG
, SERVICE_FAILURE_TIMEOUT
);
4221 case SERVICE_TIMEOUT_KILL
:
4222 if (s
->kill_context
.send_sigkill
) {
4223 log_unit_warning(UNIT(s
), "State 'stop-post' timed out. Killing.");
4224 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
4226 log_unit_warning(UNIT(s
), "State 'stop-post' timed out. Skipping SIGKILL. Entering failed mode.");
4227 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, false);
4232 assert_not_reached();
4236 case SERVICE_FINAL_WATCHDOG
:
4237 if (s
->kill_context
.send_sigkill
) {
4238 log_unit_warning(UNIT(s
), "State 'final-watchdog' timed out. Killing.");
4239 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
4241 log_unit_warning(UNIT(s
), "State 'final-watchdog' timed out. Skipping SIGKILL. Entering failed mode.");
4242 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, false);
4246 case SERVICE_FINAL_SIGTERM
:
4247 if (s
->timeout_stop_failure_mode
== SERVICE_TIMEOUT_ABORT
) {
4248 log_unit_warning(UNIT(s
), "State 'final-sigterm' timed out. Aborting.");
4249 service_enter_signal(s
, SERVICE_FINAL_WATCHDOG
, SERVICE_FAILURE_TIMEOUT
);
4250 } else if (s
->kill_context
.send_sigkill
) {
4251 log_unit_warning(UNIT(s
), "State 'final-sigterm' timed out. Killing.");
4252 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
4254 log_unit_warning(UNIT(s
), "State 'final-sigterm' timed out. Skipping SIGKILL. Entering failed mode.");
4255 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, false);
4260 case SERVICE_FINAL_SIGKILL
:
4261 log_unit_warning(UNIT(s
), "Processes still around after final SIGKILL. Entering failed mode.");
4262 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, true);
4265 case SERVICE_AUTO_RESTART
:
4266 if (s
->restart_usec
> 0)
4267 log_unit_debug(UNIT(s
),
4268 "Service restart interval %s expired, scheduling restart.",
4269 FORMAT_TIMESPAN(service_restart_usec_next(s
), USEC_PER_SEC
));
4271 log_unit_debug(UNIT(s
),
4272 "Service has no hold-off time (RestartSec=0), scheduling restart.");
4274 service_enter_restart(s
);
4277 case SERVICE_CLEANING
:
4278 log_unit_warning(UNIT(s
), "Cleaning timed out. killing.");
4280 if (s
->clean_result
== SERVICE_SUCCESS
)
4281 s
->clean_result
= SERVICE_FAILURE_TIMEOUT
;
4283 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, 0);
4287 assert_not_reached();
4293 static int service_dispatch_watchdog(sd_event_source
*source
, usec_t usec
, void *userdata
) {
4294 Service
*s
= SERVICE(userdata
);
4295 usec_t watchdog_usec
;
4298 assert(source
== s
->watchdog_event_source
);
4300 watchdog_usec
= service_get_watchdog_usec(s
);
4302 if (UNIT(s
)->manager
->service_watchdogs
) {
4303 log_unit_error(UNIT(s
), "Watchdog timeout (limit %s)!",
4304 FORMAT_TIMESPAN(watchdog_usec
, 1));
4306 service_enter_signal(s
, SERVICE_STOP_WATCHDOG
, SERVICE_FAILURE_WATCHDOG
);
4308 log_unit_warning(UNIT(s
), "Watchdog disabled! Ignoring watchdog timeout (limit %s)!",
4309 FORMAT_TIMESPAN(watchdog_usec
, 1));
4314 static bool service_notify_message_authorized(Service
*s
, pid_t pid
, FDSet
*fds
) {
4317 NotifyAccess notify_access
= service_get_notify_access(s
);
4319 if (notify_access
== NOTIFY_NONE
) {
4320 /* Warn level only if no notifications are expected */
4321 log_unit_warning(UNIT(s
), "Got notification message from PID "PID_FMT
", but reception is disabled.", pid
);
4325 if (notify_access
== NOTIFY_MAIN
&& pid
!= s
->main_pid
.pid
) {
4326 if (pidref_is_set(&s
->main_pid
))
4327 log_unit_debug(UNIT(s
), "Got notification message from PID "PID_FMT
", but reception only permitted for main PID "PID_FMT
, pid
, s
->main_pid
.pid
);
4329 log_unit_debug(UNIT(s
), "Got notification message from PID "PID_FMT
", but reception only permitted for main PID which is currently not known", pid
);
4334 if (notify_access
== NOTIFY_EXEC
&& pid
!= s
->main_pid
.pid
&& pid
!= s
->control_pid
.pid
) {
4335 if (pidref_is_set(&s
->main_pid
) && pidref_is_set(&s
->control_pid
))
4336 log_unit_debug(UNIT(s
), "Got notification message from PID "PID_FMT
", but reception only permitted for main PID "PID_FMT
" and control PID "PID_FMT
,
4337 pid
, s
->main_pid
.pid
, s
->control_pid
.pid
);
4338 else if (pidref_is_set(&s
->main_pid
))
4339 log_unit_debug(UNIT(s
), "Got notification message from PID "PID_FMT
", but reception only permitted for main PID "PID_FMT
, pid
, s
->main_pid
.pid
);
4340 else if (pidref_is_set(&s
->control_pid
))
4341 log_unit_debug(UNIT(s
), "Got notification message from PID "PID_FMT
", but reception only permitted for control PID "PID_FMT
, pid
, s
->control_pid
.pid
);
4343 log_unit_debug(UNIT(s
), "Got notification message from PID "PID_FMT
", but reception only permitted for main PID and control PID which are currently not known", pid
);
4351 static void service_force_watchdog(Service
*s
) {
4352 if (!UNIT(s
)->manager
->service_watchdogs
)
4355 log_unit_error(UNIT(s
), "Watchdog request (last status: %s)!",
4356 s
->status_text
?: "<unset>");
4358 service_enter_signal(s
, SERVICE_STOP_WATCHDOG
, SERVICE_FAILURE_WATCHDOG
);
4361 static void service_notify_message(
4363 const struct ucred
*ucred
,
4367 Service
*s
= SERVICE(u
);
4368 bool notify_dbus
= false;
4369 usec_t monotonic_usec
= USEC_INFINITY
;
4376 if (!service_notify_message_authorized(s
, ucred
->pid
, fds
))
4379 if (DEBUG_LOGGING
) {
4380 _cleanup_free_
char *cc
= NULL
;
4382 cc
= strv_join(tags
, ", ");
4383 log_unit_debug(u
, "Got notification message from PID "PID_FMT
" (%s)", ucred
->pid
, empty_to_na(cc
));
4386 /* Interpret MAINPID= */
4387 e
= strv_find_startswith(tags
, "MAINPID=");
4388 if (e
&& IN_SET(s
->state
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
, SERVICE_RELOAD_SIGNAL
, SERVICE_RELOAD_NOTIFY
)) {
4389 _cleanup_(pidref_done
) PidRef new_main_pid
= PIDREF_NULL
;
4391 r
= pidref_set_pidstr(&new_main_pid
, e
);
4393 log_unit_warning_errno(u
, r
, "Failed to parse MAINPID=%s field in notification message, ignoring: %m", e
);
4394 else if (!s
->main_pid_known
|| !pidref_equal(&new_main_pid
, &s
->main_pid
)) {
4396 r
= service_is_suitable_main_pid(s
, &new_main_pid
, LOG_WARNING
);
4398 /* The new main PID is a bit suspicious, which is OK if the sender is privileged. */
4400 if (ucred
->uid
== 0) {
4401 log_unit_debug(u
, "New main PID "PID_FMT
" does not belong to service, but we'll accept it as the request to change it came from a privileged process.", new_main_pid
.pid
);
4404 log_unit_debug(u
, "New main PID "PID_FMT
" does not belong to service, refusing.", new_main_pid
.pid
);
4407 (void) service_set_main_pidref(s
, &new_main_pid
);
4409 r
= unit_watch_pidref(UNIT(s
), &s
->main_pid
, /* exclusive= */ false);
4411 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch new main PID "PID_FMT
" for service: %m", s
->main_pid
.pid
);
4418 /* Parse MONOTONIC_USEC= */
4419 e
= strv_find_startswith(tags
, "MONOTONIC_USEC=");
4421 r
= safe_atou64(e
, &monotonic_usec
);
4423 log_unit_warning_errno(u
, r
, "Failed to parse MONOTONIC_USEC= field in notification message, ignoring: %s", e
);
4426 /* Interpret READY=/STOPPING=/RELOADING=. STOPPING= wins over the others, and READY= over RELOADING= */
4427 if (strv_contains(tags
, "STOPPING=1")) {
4428 s
->notify_state
= NOTIFY_STOPPING
;
4430 if (IN_SET(s
->state
, SERVICE_RUNNING
, SERVICE_RELOAD_SIGNAL
, SERVICE_RELOAD_NOTIFY
))
4431 service_enter_stop_by_notify(s
);
4435 } else if (strv_contains(tags
, "READY=1")) {
4437 s
->notify_state
= NOTIFY_READY
;
4439 /* Type=notify services inform us about completed initialization with READY=1 */
4440 if (IN_SET(s
->type
, SERVICE_NOTIFY
, SERVICE_NOTIFY_RELOAD
) &&
4441 s
->state
== SERVICE_START
)
4442 service_enter_start_post(s
);
4444 /* Sending READY=1 while we are reloading informs us that the reloading is complete. */
4445 if (s
->state
== SERVICE_RELOAD_NOTIFY
)
4446 service_enter_running(s
, SERVICE_SUCCESS
);
4448 /* Combined RELOADING=1 and READY=1? Then this is indication that the service started and
4449 * immediately finished reloading. */
4450 if (s
->state
== SERVICE_RELOAD_SIGNAL
&&
4451 strv_contains(tags
, "RELOADING=1") &&
4452 monotonic_usec
!= USEC_INFINITY
&&
4453 monotonic_usec
>= s
->reload_begin_usec
) {
4454 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4456 /* Propagate a reload explicitly */
4457 r
= manager_propagate_reload(UNIT(s
)->manager
, UNIT(s
), JOB_FAIL
, &error
);
4459 log_unit_warning(UNIT(s
), "Failed to schedule propagation of reload, ignoring: %s", bus_error_message(&error
, r
));
4461 service_enter_running(s
, SERVICE_SUCCESS
);
4466 } else if (strv_contains(tags
, "RELOADING=1")) {
4468 s
->notify_state
= NOTIFY_RELOADING
;
4470 /* Sending RELOADING=1 after we send SIGHUP to request a reload will transition
4471 * things to "reload-notify" state, where we'll wait for READY=1 to let us know the
4472 * reload is done. Note that we insist on a timestamp being sent along here, so that
4473 * we know for sure this is a reload cycle initiated *after* we sent the signal */
4474 if (s
->state
== SERVICE_RELOAD_SIGNAL
&&
4475 monotonic_usec
!= USEC_INFINITY
&&
4476 monotonic_usec
>= s
->reload_begin_usec
)
4477 /* Note, we don't call service_enter_reload_by_notify() here, because we
4478 * don't need reload propagation nor do we want to restart the time-out. */
4479 service_set_state(s
, SERVICE_RELOAD_NOTIFY
);
4481 if (s
->state
== SERVICE_RUNNING
)
4482 service_enter_reload_by_notify(s
);
4487 /* Interpret STATUS= */
4488 e
= strv_find_startswith(tags
, "STATUS=");
4490 _cleanup_free_
char *t
= NULL
;
4493 /* Note that this size limit check is mostly paranoia: since the datagram size we are willing
4494 * to process is already limited to NOTIFY_BUFFER_MAX, this limit here should never be hit. */
4495 if (strlen(e
) > STATUS_TEXT_MAX
)
4496 log_unit_warning(u
, "Status message overly long (%zu > %u), ignoring.", strlen(e
), STATUS_TEXT_MAX
);
4497 else if (!utf8_is_valid(e
))
4498 log_unit_warning(u
, "Status message in notification message is not UTF-8 clean, ignoring.");
4506 if (!streq_ptr(s
->status_text
, t
)) {
4507 free_and_replace(s
->status_text
, t
);
4512 /* Interpret NOTIFYACCESS= */
4513 e
= strv_find_startswith(tags
, "NOTIFYACCESS=");
4515 NotifyAccess notify_access
;
4517 notify_access
= notify_access_from_string(e
);
4518 if (notify_access
< 0)
4519 log_unit_warning_errno(u
, notify_access
,
4520 "Failed to parse NOTIFYACCESS= field value '%s' in notification message, ignoring: %m", e
);
4522 /* We don't need to check whether the new access mode is more strict than what is
4523 * already in use, since only the privileged process is allowed to change it
4524 * in the first place. */
4525 if (service_get_notify_access(s
) != notify_access
) {
4526 service_override_notify_access(s
, notify_access
);
4531 /* Interpret ERRNO= */
4532 e
= strv_find_startswith(tags
, "ERRNO=");
4536 status_errno
= parse_errno(e
);
4537 if (status_errno
< 0)
4538 log_unit_warning_errno(u
, status_errno
,
4539 "Failed to parse ERRNO= field value '%s' in notification message: %m", e
);
4540 else if (s
->status_errno
!= status_errno
) {
4541 s
->status_errno
= status_errno
;
4546 /* Interpret EXTEND_TIMEOUT= */
4547 e
= strv_find_startswith(tags
, "EXTEND_TIMEOUT_USEC=");
4549 usec_t extend_timeout_usec
;
4550 if (safe_atou64(e
, &extend_timeout_usec
) < 0)
4551 log_unit_warning(u
, "Failed to parse EXTEND_TIMEOUT_USEC=%s", e
);
4553 service_extend_timeout(s
, extend_timeout_usec
);
4556 /* Interpret WATCHDOG= */
4557 e
= strv_find_startswith(tags
, "WATCHDOG=");
4560 service_reset_watchdog(s
);
4561 else if (streq(e
, "trigger"))
4562 service_force_watchdog(s
);
4564 log_unit_warning(u
, "Passed WATCHDOG= field is invalid, ignoring.");
4567 e
= strv_find_startswith(tags
, "WATCHDOG_USEC=");
4569 usec_t watchdog_override_usec
;
4570 if (safe_atou64(e
, &watchdog_override_usec
) < 0)
4571 log_unit_warning(u
, "Failed to parse WATCHDOG_USEC=%s", e
);
4573 service_override_watchdog_timeout(s
, watchdog_override_usec
);
4576 /* Process FD store messages. Either FDSTOREREMOVE=1 for removal, or FDSTORE=1 for addition. In both cases,
4577 * process FDNAME= for picking the file descriptor name to use. Note that FDNAME= is required when removing
4578 * fds, but optional when pushing in new fds, for compatibility reasons. */
4579 if (strv_contains(tags
, "FDSTOREREMOVE=1")) {
4582 name
= strv_find_startswith(tags
, "FDNAME=");
4583 if (!name
|| !fdname_is_valid(name
))
4584 log_unit_warning(u
, "FDSTOREREMOVE=1 requested, but no valid file descriptor name passed, ignoring.");
4586 service_remove_fd_store(s
, name
);
4588 } else if (strv_contains(tags
, "FDSTORE=1")) {
4591 name
= strv_find_startswith(tags
, "FDNAME=");
4592 if (name
&& !fdname_is_valid(name
)) {
4593 log_unit_warning(u
, "Passed FDNAME= name is invalid, ignoring.");
4597 (void) service_add_fd_store_set(s
, fds
, name
, !strv_contains(tags
, "FDPOLL=0"));
4600 /* Notify clients about changed status or main pid */
4602 unit_add_to_dbus_queue(u
);
4605 static int service_get_timeout(Unit
*u
, usec_t
*timeout
) {
4606 Service
*s
= SERVICE(u
);
4610 if (!s
->timer_event_source
)
4613 r
= sd_event_source_get_time(s
->timer_event_source
, &t
);
4616 if (t
== USEC_INFINITY
)
4623 static usec_t
service_get_timeout_start_usec(Unit
*u
) {
4624 Service
*s
= SERVICE(ASSERT_PTR(u
));
4625 return s
->timeout_start_usec
;
4628 static bool pick_up_pid_from_bus_name(Service
*s
) {
4631 /* If the service is running but we have no main PID yet, get it from the owner of the D-Bus name */
4633 return !pidref_is_set(&s
->main_pid
) &&
4639 SERVICE_RELOAD_SIGNAL
,
4640 SERVICE_RELOAD_NOTIFY
);
4643 static int bus_name_pid_lookup_callback(sd_bus_message
*reply
, void *userdata
, sd_bus_error
*ret_error
) {
4644 _cleanup_(pidref_done
) PidRef pidref
= PIDREF_NULL
;
4645 const sd_bus_error
*e
;
4646 Unit
*u
= ASSERT_PTR(userdata
);
4654 s
->bus_name_pid_lookup_slot
= sd_bus_slot_unref(s
->bus_name_pid_lookup_slot
);
4656 if (!s
->bus_name
|| !pick_up_pid_from_bus_name(s
))
4659 e
= sd_bus_message_get_error(reply
);
4661 r
= sd_bus_error_get_errno(e
);
4662 log_warning_errno(r
, "GetConnectionUnixProcessID() failed: %s", bus_error_message(e
, r
));
4666 r
= sd_bus_message_read(reply
, "u", &pid
);
4668 bus_log_parse_error(r
);
4672 r
= pidref_set_pid(&pidref
, pid
);
4674 log_debug_errno(r
, "GetConnectionUnixProcessID() returned invalid PID: %m");
4678 log_unit_debug(u
, "D-Bus name %s is now owned by process " PID_FMT
, s
->bus_name
, pidref
.pid
);
4680 (void) service_set_main_pidref(s
, &pidref
);
4681 (void) unit_watch_pidref(UNIT(s
), &s
->main_pid
, /* exclusive= */ false);
4685 static void service_bus_name_owner_change(Unit
*u
, const char *new_owner
) {
4687 Service
*s
= SERVICE(u
);
4693 log_unit_debug(u
, "D-Bus name %s now owned by %s", s
->bus_name
, new_owner
);
4695 log_unit_debug(u
, "D-Bus name %s now not owned by anyone.", s
->bus_name
);
4697 s
->bus_name_good
= new_owner
;
4699 /* Track the current owner, so we can reconstruct changes after a daemon reload */
4700 r
= free_and_strdup(&s
->bus_name_owner
, new_owner
);
4702 log_unit_error_errno(u
, r
, "Unable to set new bus name owner %s: %m", new_owner
);
4706 if (s
->type
== SERVICE_DBUS
) {
4708 /* service_enter_running() will figure out what to
4710 if (s
->state
== SERVICE_RUNNING
)
4711 service_enter_running(s
, SERVICE_SUCCESS
);
4712 else if (s
->state
== SERVICE_START
&& new_owner
)
4713 service_enter_start_post(s
);
4715 } else if (new_owner
&& pick_up_pid_from_bus_name(s
)) {
4717 /* Try to acquire PID from bus service */
4719 s
->bus_name_pid_lookup_slot
= sd_bus_slot_unref(s
->bus_name_pid_lookup_slot
);
4721 r
= sd_bus_call_method_async(
4722 u
->manager
->api_bus
,
4723 &s
->bus_name_pid_lookup_slot
,
4724 "org.freedesktop.DBus",
4725 "/org/freedesktop/DBus",
4726 "org.freedesktop.DBus",
4727 "GetConnectionUnixProcessID",
4728 bus_name_pid_lookup_callback
,
4733 log_debug_errno(r
, "Failed to request owner PID of service name, ignoring: %m");
4737 int service_set_socket_fd(
4742 bool selinux_context_net
) {
4744 _cleanup_free_
char *peer_text
= NULL
;
4750 /* This is called by the socket code when instantiating a new service for a stream socket and the socket needs
4751 * to be configured. We take ownership of the passed fd on success. */
4753 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
4756 if (s
->socket_fd
>= 0)
4759 assert(!s
->socket_peer
);
4761 if (!IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_DEAD_RESOURCES_PINNED
))
4764 if (getpeername_pretty(fd
, true, &peer_text
) >= 0) {
4766 if (UNIT(s
)->description
) {
4767 _cleanup_free_
char *a
= NULL
;
4769 a
= strjoin(UNIT(s
)->description
, " (", peer_text
, ")");
4773 r
= unit_set_description(UNIT(s
), a
);
4775 r
= unit_set_description(UNIT(s
), peer_text
);
4780 r
= unit_add_two_dependencies(UNIT(sock
), UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT(s
), false, UNIT_DEPENDENCY_IMPLICIT
);
4785 s
->socket_peer
= socket_peer_ref(peer
);
4786 s
->socket_fd_selinux_context_net
= selinux_context_net
;
4788 unit_ref_set(&s
->accept_socket
, UNIT(s
), UNIT(sock
));
4792 static void service_reset_failed(Unit
*u
) {
4793 Service
*s
= SERVICE(u
);
4797 if (s
->state
== SERVICE_FAILED
)
4798 service_set_state(s
, service_determine_dead_state(s
));
4800 s
->result
= SERVICE_SUCCESS
;
4801 s
->reload_result
= SERVICE_SUCCESS
;
4802 s
->clean_result
= SERVICE_SUCCESS
;
4804 s
->flush_n_restarts
= false;
4807 static PidRef
* service_main_pid(Unit
*u
, bool *ret_is_alien
) {
4808 Service
*s
= ASSERT_PTR(SERVICE(u
));
4811 *ret_is_alien
= s
->main_pid_alien
;
4813 return &s
->main_pid
;
4816 static PidRef
* service_control_pid(Unit
*u
) {
4817 return &ASSERT_PTR(SERVICE(u
))->control_pid
;
4820 static bool service_needs_console(Unit
*u
) {
4821 Service
*s
= SERVICE(u
);
4825 /* We provide our own implementation of this here, instead of relying of the generic implementation
4826 * unit_needs_console() provides, since we want to return false if we are in SERVICE_EXITED state. */
4828 if (!exec_context_may_touch_console(&s
->exec_context
))
4831 return IN_SET(s
->state
,
4838 SERVICE_RELOAD_SIGNAL
,
4839 SERVICE_RELOAD_NOTIFY
,
4841 SERVICE_STOP_WATCHDOG
,
4842 SERVICE_STOP_SIGTERM
,
4843 SERVICE_STOP_SIGKILL
,
4845 SERVICE_FINAL_WATCHDOG
,
4846 SERVICE_FINAL_SIGTERM
,
4847 SERVICE_FINAL_SIGKILL
);
4850 static int service_exit_status(Unit
*u
) {
4851 Service
*s
= SERVICE(u
);
4855 if (s
->main_exec_status
.pid
<= 0 ||
4856 !dual_timestamp_is_set(&s
->main_exec_status
.exit_timestamp
))
4859 if (s
->main_exec_status
.code
!= CLD_EXITED
)
4862 return s
->main_exec_status
.status
;
4865 static const char* service_status_text(Unit
*u
) {
4866 Service
*s
= SERVICE(u
);
4870 return s
->status_text
;
4873 static int service_clean(Unit
*u
, ExecCleanMask mask
) {
4874 _cleanup_strv_free_
char **l
= NULL
;
4875 bool may_clean_fdstore
= false;
4876 Service
*s
= SERVICE(u
);
4882 if (!IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_DEAD_RESOURCES_PINNED
))
4885 /* Determine if there's anything we could potentially clean */
4886 r
= exec_context_get_clean_directories(&s
->exec_context
, u
->manager
->prefix
, mask
, &l
);
4890 if (mask
& EXEC_CLEAN_FDSTORE
)
4891 may_clean_fdstore
= s
->n_fd_store
> 0 || s
->n_fd_store_max
> 0;
4893 if (strv_isempty(l
) && !may_clean_fdstore
)
4894 return -EUNATCH
; /* Nothing to potentially clean */
4896 /* Let's clean the stuff we can clean quickly */
4897 if (may_clean_fdstore
)
4898 service_release_fd_store(s
);
4900 /* If we are done, leave quickly */
4901 if (strv_isempty(l
)) {
4902 if (s
->state
== SERVICE_DEAD_RESOURCES_PINNED
&& !s
->fd_store
)
4903 service_set_state(s
, SERVICE_DEAD
);
4907 /* We need to clean disk stuff. This is slow, hence do it out of process, and change state */
4908 service_unwatch_control_pid(s
);
4909 s
->clean_result
= SERVICE_SUCCESS
;
4910 s
->control_command
= NULL
;
4911 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
4913 r
= service_arm_timer(s
, /* relative= */ true, s
->exec_context
.timeout_clean_usec
);
4915 log_unit_warning_errno(u
, r
, "Failed to install timer: %m");
4919 r
= unit_fork_and_watch_rm_rf(u
, l
, &s
->control_pid
);
4921 log_unit_warning_errno(u
, r
, "Failed to spawn cleaning task: %m");
4925 service_set_state(s
, SERVICE_CLEANING
);
4929 s
->clean_result
= SERVICE_FAILURE_RESOURCES
;
4930 s
->timer_event_source
= sd_event_source_disable_unref(s
->timer_event_source
);
4934 static int service_can_clean(Unit
*u
, ExecCleanMask
*ret
) {
4935 Service
*s
= SERVICE(u
);
4936 ExecCleanMask mask
= 0;
4942 r
= exec_context_get_clean_mask(&s
->exec_context
, &mask
);
4946 if (s
->n_fd_store_max
> 0)
4947 mask
|= EXEC_CLEAN_FDSTORE
;
4953 static const char *service_finished_job(Unit
*u
, JobType t
, JobResult result
) {
4954 if (t
== JOB_START
&&
4955 result
== JOB_DONE
&&
4956 SERVICE(u
)->type
== SERVICE_ONESHOT
)
4957 return "Finished %s.";
4959 /* Fall back to generic */
4963 static int service_can_start(Unit
*u
) {
4964 Service
*s
= SERVICE(u
);
4969 /* Make sure we don't enter a busy loop of some kind. */
4970 r
= unit_test_start_limit(u
);
4972 service_enter_dead(s
, SERVICE_FAILURE_START_LIMIT_HIT
, false);
4979 static void service_release_resources(Unit
*u
) {
4980 Service
*s
= SERVICE(ASSERT_PTR(u
));
4982 /* Invoked by the unit state engine, whenever it realizes that unit is dead and there's no job
4983 * anymore for it, and it hence is a good idea to release resources */
4985 /* Don't release resources if this is a transitionary failed/dead state
4986 * (i.e. SERVICE_DEAD_BEFORE_AUTO_RESTART/SERVICE_FAILED_BEFORE_AUTO_RESTART), insist on a permanent
4988 if (!IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_DEAD_RESOURCES_PINNED
))
4991 log_unit_debug(u
, "Releasing resources...");
4993 service_release_socket_fd(s
);
4994 service_release_stdio_fd(s
);
4996 if (s
->fd_store_preserve_mode
!= EXEC_PRESERVE_YES
)
4997 service_release_fd_store(s
);
4999 if (s
->state
== SERVICE_DEAD_RESOURCES_PINNED
&& !s
->fd_store
)
5000 service_set_state(s
, SERVICE_DEAD
);
5003 int service_determine_exec_selinux_label(Service
*s
, char **ret
) {
5009 if (!mac_selinux_use())
5012 /* Returns the SELinux label used for execution of the main service binary */
5014 if (s
->exec_context
.selinux_context
) { /* Prefer the explicitly configured label if there is one */
5015 char *con
= strdup(s
->exec_context
.selinux_context
);
5023 if (s
->exec_context
.root_image
||
5024 s
->exec_context
.n_extension_images
> 0 ||
5025 !strv_isempty(s
->exec_context
.extension_directories
)) /* We cannot chase paths through images */
5026 return log_unit_debug_errno(UNIT(s
), SYNTHETIC_ERRNO(ENODATA
), "Service with RootImage=, ExtensionImages= or ExtensionDirectories= set, cannot determine socket SELinux label before activation, ignoring.");
5028 ExecCommand
*c
= s
->exec_command
[SERVICE_EXEC_START
];
5032 _cleanup_free_
char *path
= NULL
;
5033 r
= chase(c
->path
, s
->exec_context
.root_directory
, CHASE_PREFIX_ROOT
, &path
, NULL
);
5035 log_unit_debug_errno(UNIT(s
), r
, "Failed to resolve service binary '%s', ignoring.", c
->path
);
5039 r
= mac_selinux_get_create_label_from_exe(path
, ret
);
5040 if (ERRNO_IS_NEG_NOT_SUPPORTED(r
)) {
5041 log_unit_debug_errno(UNIT(s
), r
, "Reading SELinux label off binary '%s' is not supported, ignoring.", path
);
5044 if (ERRNO_IS_NEG_PRIVILEGE(r
)) {
5045 log_unit_debug_errno(UNIT(s
), r
, "Can't read SELinux label off binary '%s', due to privileges, ignoring.", path
);
5049 return log_unit_debug_errno(UNIT(s
), r
, "Failed to read SELinux label off binary '%s': %m", path
);
5054 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
5055 [SERVICE_RESTART_NO
] = "no",
5056 [SERVICE_RESTART_ON_SUCCESS
] = "on-success",
5057 [SERVICE_RESTART_ON_FAILURE
] = "on-failure",
5058 [SERVICE_RESTART_ON_ABNORMAL
] = "on-abnormal",
5059 [SERVICE_RESTART_ON_WATCHDOG
] = "on-watchdog",
5060 [SERVICE_RESTART_ON_ABORT
] = "on-abort",
5061 [SERVICE_RESTART_ALWAYS
] = "always",
5064 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
5066 static const char* const service_restart_mode_table
[_SERVICE_RESTART_MODE_MAX
] = {
5067 [SERVICE_RESTART_MODE_NORMAL
] = "normal",
5068 [SERVICE_RESTART_MODE_DIRECT
] = "direct",
5071 DEFINE_STRING_TABLE_LOOKUP(service_restart_mode
, ServiceRestartMode
);
5073 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
5074 [SERVICE_SIMPLE
] = "simple",
5075 [SERVICE_FORKING
] = "forking",
5076 [SERVICE_ONESHOT
] = "oneshot",
5077 [SERVICE_DBUS
] = "dbus",
5078 [SERVICE_NOTIFY
] = "notify",
5079 [SERVICE_NOTIFY_RELOAD
] = "notify-reload",
5080 [SERVICE_IDLE
] = "idle",
5081 [SERVICE_EXEC
] = "exec",
5084 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
5086 static const char* const service_exit_type_table
[_SERVICE_EXIT_TYPE_MAX
] = {
5087 [SERVICE_EXIT_MAIN
] = "main",
5088 [SERVICE_EXIT_CGROUP
] = "cgroup",
5091 DEFINE_STRING_TABLE_LOOKUP(service_exit_type
, ServiceExitType
);
5093 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
5094 [SERVICE_EXEC_CONDITION
] = "ExecCondition",
5095 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
5096 [SERVICE_EXEC_START
] = "ExecStart",
5097 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
5098 [SERVICE_EXEC_RELOAD
] = "ExecReload",
5099 [SERVICE_EXEC_STOP
] = "ExecStop",
5100 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
5103 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
5105 static const char* const service_exec_ex_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
5106 [SERVICE_EXEC_CONDITION
] = "ExecConditionEx",
5107 [SERVICE_EXEC_START_PRE
] = "ExecStartPreEx",
5108 [SERVICE_EXEC_START
] = "ExecStartEx",
5109 [SERVICE_EXEC_START_POST
] = "ExecStartPostEx",
5110 [SERVICE_EXEC_RELOAD
] = "ExecReloadEx",
5111 [SERVICE_EXEC_STOP
] = "ExecStopEx",
5112 [SERVICE_EXEC_STOP_POST
] = "ExecStopPostEx",
5115 DEFINE_STRING_TABLE_LOOKUP(service_exec_ex_command
, ServiceExecCommand
);
5117 static const char* const notify_state_table
[_NOTIFY_STATE_MAX
] = {
5118 [NOTIFY_UNKNOWN
] = "unknown",
5119 [NOTIFY_READY
] = "ready",
5120 [NOTIFY_RELOADING
] = "reloading",
5121 [NOTIFY_STOPPING
] = "stopping",
5124 DEFINE_STRING_TABLE_LOOKUP(notify_state
, NotifyState
);
5126 static const char* const service_result_table
[_SERVICE_RESULT_MAX
] = {
5127 [SERVICE_SUCCESS
] = "success",
5128 [SERVICE_FAILURE_RESOURCES
] = "resources",
5129 [SERVICE_FAILURE_PROTOCOL
] = "protocol",
5130 [SERVICE_FAILURE_TIMEOUT
] = "timeout",
5131 [SERVICE_FAILURE_EXIT_CODE
] = "exit-code",
5132 [SERVICE_FAILURE_SIGNAL
] = "signal",
5133 [SERVICE_FAILURE_CORE_DUMP
] = "core-dump",
5134 [SERVICE_FAILURE_WATCHDOG
] = "watchdog",
5135 [SERVICE_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
5136 [SERVICE_FAILURE_OOM_KILL
] = "oom-kill",
5137 [SERVICE_SKIP_CONDITION
] = "exec-condition",
5140 DEFINE_STRING_TABLE_LOOKUP(service_result
, ServiceResult
);
5142 static const char* const service_timeout_failure_mode_table
[_SERVICE_TIMEOUT_FAILURE_MODE_MAX
] = {
5143 [SERVICE_TIMEOUT_TERMINATE
] = "terminate",
5144 [SERVICE_TIMEOUT_ABORT
] = "abort",
5145 [SERVICE_TIMEOUT_KILL
] = "kill",
5148 DEFINE_STRING_TABLE_LOOKUP(service_timeout_failure_mode
, ServiceTimeoutFailureMode
);
5150 const UnitVTable service_vtable
= {
5151 .object_size
= sizeof(Service
),
5152 .exec_context_offset
= offsetof(Service
, exec_context
),
5153 .cgroup_context_offset
= offsetof(Service
, cgroup_context
),
5154 .kill_context_offset
= offsetof(Service
, kill_context
),
5155 .exec_runtime_offset
= offsetof(Service
, exec_runtime
),
5156 .cgroup_runtime_offset
= offsetof(Service
, cgroup_runtime
),
5162 .private_section
= "Service",
5164 .can_transient
= true,
5165 .can_delegate
= true,
5167 .can_set_managed_oom
= true,
5169 .init
= service_init
,
5170 .done
= service_done
,
5171 .load
= service_load
,
5172 .release_resources
= service_release_resources
,
5174 .coldplug
= service_coldplug
,
5176 .dump
= service_dump
,
5178 .start
= service_start
,
5179 .stop
= service_stop
,
5180 .reload
= service_reload
,
5182 .can_reload
= service_can_reload
,
5184 .clean
= service_clean
,
5185 .can_clean
= service_can_clean
,
5187 .freezer_action
= unit_cgroup_freezer_action
,
5189 .serialize
= service_serialize
,
5190 .deserialize_item
= service_deserialize_item
,
5192 .active_state
= service_active_state
,
5193 .sub_state_to_string
= service_sub_state_to_string
,
5195 .will_restart
= service_will_restart
,
5197 .may_gc
= service_may_gc
,
5199 .sigchld_event
= service_sigchld_event
,
5201 .reset_failed
= service_reset_failed
,
5203 .notify_cgroup_empty
= service_notify_cgroup_empty_event
,
5204 .notify_cgroup_oom
= service_notify_cgroup_oom_event
,
5205 .notify_message
= service_notify_message
,
5207 .main_pid
= service_main_pid
,
5208 .control_pid
= service_control_pid
,
5210 .bus_name_owner_change
= service_bus_name_owner_change
,
5212 .bus_set_property
= bus_service_set_property
,
5213 .bus_commit_properties
= bus_service_commit_properties
,
5215 .get_timeout
= service_get_timeout
,
5216 .get_timeout_start_usec
= service_get_timeout_start_usec
,
5217 .needs_console
= service_needs_console
,
5218 .exit_status
= service_exit_status
,
5219 .status_text
= service_status_text
,
5221 .status_message_formats
= {
5222 .finished_start_job
= {
5223 [JOB_FAILED
] = "Failed to start %s.",
5225 .finished_stop_job
= {
5226 [JOB_DONE
] = "Stopped %s.",
5227 [JOB_FAILED
] = "Stopped (with error) %s.",
5229 .finished_job
= service_finished_job
,
5232 .can_start
= service_can_start
,
5234 .notify_plymouth
= true,
5236 .audit_start_message_type
= AUDIT_SERVICE_START
,
5237 .audit_stop_message_type
= AUDIT_SERVICE_STOP
,