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 "serialize.h"
39 #include "signal-util.h"
41 #include "stdio-util.h"
42 #include "string-table.h"
43 #include "string-util.h"
45 #include "unit-name.h"
49 #define service_spawn(...) service_spawn_internal(__func__, __VA_ARGS__)
51 static const UnitActiveState state_translation_table
[_SERVICE_STATE_MAX
] = {
52 [SERVICE_DEAD
] = UNIT_INACTIVE
,
53 [SERVICE_CONDITION
] = UNIT_ACTIVATING
,
54 [SERVICE_START_PRE
] = UNIT_ACTIVATING
,
55 [SERVICE_START
] = UNIT_ACTIVATING
,
56 [SERVICE_START_POST
] = UNIT_ACTIVATING
,
57 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
58 [SERVICE_EXITED
] = UNIT_ACTIVE
,
59 [SERVICE_RELOAD
] = UNIT_RELOADING
,
60 [SERVICE_RELOAD_SIGNAL
] = UNIT_RELOADING
,
61 [SERVICE_RELOAD_NOTIFY
] = UNIT_RELOADING
,
62 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
63 [SERVICE_STOP_WATCHDOG
] = UNIT_DEACTIVATING
,
64 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
65 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
66 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
67 [SERVICE_FINAL_WATCHDOG
] = UNIT_DEACTIVATING
,
68 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
69 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
70 [SERVICE_FAILED
] = UNIT_FAILED
,
71 [SERVICE_DEAD_BEFORE_AUTO_RESTART
] = UNIT_INACTIVE
,
72 [SERVICE_FAILED_BEFORE_AUTO_RESTART
] = UNIT_FAILED
,
73 [SERVICE_DEAD_RESOURCES_PINNED
] = UNIT_INACTIVE
,
74 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
,
75 [SERVICE_AUTO_RESTART_QUEUED
] = UNIT_ACTIVATING
,
76 [SERVICE_CLEANING
] = UNIT_MAINTENANCE
,
79 /* For Type=idle we never want to delay any other jobs, hence we
80 * consider idle jobs active as soon as we start working on them */
81 static const UnitActiveState state_translation_table_idle
[_SERVICE_STATE_MAX
] = {
82 [SERVICE_DEAD
] = UNIT_INACTIVE
,
83 [SERVICE_CONDITION
] = UNIT_ACTIVE
,
84 [SERVICE_START_PRE
] = UNIT_ACTIVE
,
85 [SERVICE_START
] = UNIT_ACTIVE
,
86 [SERVICE_START_POST
] = UNIT_ACTIVE
,
87 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
88 [SERVICE_EXITED
] = UNIT_ACTIVE
,
89 [SERVICE_RELOAD
] = UNIT_RELOADING
,
90 [SERVICE_RELOAD_SIGNAL
] = UNIT_RELOADING
,
91 [SERVICE_RELOAD_NOTIFY
] = UNIT_RELOADING
,
92 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
93 [SERVICE_STOP_WATCHDOG
] = UNIT_DEACTIVATING
,
94 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
95 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
96 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
97 [SERVICE_FINAL_WATCHDOG
] = UNIT_DEACTIVATING
,
98 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
99 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
100 [SERVICE_FAILED
] = UNIT_FAILED
,
101 [SERVICE_DEAD_BEFORE_AUTO_RESTART
] = UNIT_INACTIVE
,
102 [SERVICE_FAILED_BEFORE_AUTO_RESTART
] = UNIT_FAILED
,
103 [SERVICE_DEAD_RESOURCES_PINNED
] = UNIT_INACTIVE
,
104 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
,
105 [SERVICE_AUTO_RESTART_QUEUED
] = UNIT_ACTIVATING
,
106 [SERVICE_CLEANING
] = UNIT_MAINTENANCE
,
109 static int service_dispatch_inotify_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
);
110 static int service_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
111 static int service_dispatch_watchdog(sd_event_source
*source
, usec_t usec
, void *userdata
);
112 static int service_dispatch_exec_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
);
114 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
);
115 static void service_enter_reload_by_notify(Service
*s
);
117 static void service_init(Unit
*u
) {
118 Service
*s
= SERVICE(u
);
121 assert(u
->load_state
== UNIT_STUB
);
123 s
->timeout_start_usec
= u
->manager
->defaults
.timeout_start_usec
;
124 s
->timeout_stop_usec
= u
->manager
->defaults
.timeout_stop_usec
;
125 s
->timeout_abort_usec
= u
->manager
->defaults
.timeout_abort_usec
;
126 s
->timeout_abort_set
= u
->manager
->defaults
.timeout_abort_set
;
127 s
->restart_usec
= u
->manager
->defaults
.restart_usec
;
128 s
->restart_max_delay_usec
= USEC_INFINITY
;
129 s
->runtime_max_usec
= USEC_INFINITY
;
130 s
->type
= _SERVICE_TYPE_INVALID
;
131 s
->socket_fd
= -EBADF
;
132 s
->stdin_fd
= s
->stdout_fd
= s
->stderr_fd
= -EBADF
;
133 s
->guess_main_pid
= true;
134 s
->main_pid
= PIDREF_NULL
;
135 s
->control_pid
= PIDREF_NULL
;
136 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
138 s
->exec_context
.keyring_mode
= MANAGER_IS_SYSTEM(u
->manager
) ?
139 EXEC_KEYRING_PRIVATE
: EXEC_KEYRING_INHERIT
;
141 s
->notify_access_override
= _NOTIFY_ACCESS_INVALID
;
143 s
->watchdog_original_usec
= USEC_INFINITY
;
145 s
->oom_policy
= _OOM_POLICY_INVALID
;
146 s
->reload_begin_usec
= USEC_INFINITY
;
147 s
->reload_signal
= SIGHUP
;
149 s
->fd_store_preserve_mode
= EXEC_PRESERVE_RESTART
;
152 static void service_unwatch_control_pid(Service
*s
) {
155 if (!pidref_is_set(&s
->control_pid
))
158 unit_unwatch_pid(UNIT(s
), s
->control_pid
.pid
);
159 pidref_done(&s
->control_pid
);
162 static void service_unwatch_main_pid(Service
*s
) {
165 if (!pidref_is_set(&s
->main_pid
))
168 unit_unwatch_pid(UNIT(s
), s
->main_pid
.pid
);
169 pidref_done(&s
->main_pid
);
172 static void service_unwatch_pid_file(Service
*s
) {
173 if (!s
->pid_file_pathspec
)
176 log_unit_debug(UNIT(s
), "Stopping watch for PID file %s", s
->pid_file_pathspec
->path
);
177 path_spec_unwatch(s
->pid_file_pathspec
);
178 path_spec_done(s
->pid_file_pathspec
);
179 s
->pid_file_pathspec
= mfree(s
->pid_file_pathspec
);
182 static int service_set_main_pidref(Service
*s
, PidRef
*pidref
) {
185 /* Takes ownership of the specified pidref on success, but not on failure. */
187 if (!pidref_is_set(pidref
))
190 if (pidref
->pid
<= 1)
193 if (pidref
->pid
== getpid_cached())
196 if (s
->main_pid
.pid
== pidref
->pid
&& s
->main_pid_known
) {
201 if (s
->main_pid
.pid
!= pidref
->pid
) {
202 service_unwatch_main_pid(s
);
203 exec_status_start(&s
->main_exec_status
, pidref
->pid
);
206 s
->main_pid
= TAKE_PIDREF(*pidref
);
207 s
->main_pid_known
= true;
208 s
->main_pid_alien
= pid_is_my_child(s
->main_pid
.pid
) == 0;
210 if (s
->main_pid_alien
)
211 log_unit_warning(UNIT(s
), "Supervising process "PID_FMT
" which is not our child. We'll most likely not notice when it exits.", s
->main_pid
.pid
);
216 static int service_set_main_pid(Service
*s
, pid_t pid
) {
217 _cleanup_(pidref_done
) PidRef pidref
= PIDREF_NULL
;
222 r
= pidref_set_pid(&pidref
, pid
);
226 return service_set_main_pidref(s
, &pidref
);
229 void service_release_socket_fd(Service
*s
) {
232 if (s
->socket_fd
< 0 && !UNIT_ISSET(s
->accept_socket
) && !s
->socket_peer
)
235 log_unit_debug(UNIT(s
), "Closing connection socket.");
237 /* Undo the effect of service_set_socket_fd(). */
239 s
->socket_fd
= asynchronous_close(s
->socket_fd
);
241 if (UNIT_ISSET(s
->accept_socket
)) {
242 socket_connection_unref(SOCKET(UNIT_DEREF(s
->accept_socket
)));
243 unit_ref_unset(&s
->accept_socket
);
246 s
->socket_peer
= socket_peer_unref(s
->socket_peer
);
249 static void service_override_notify_access(Service
*s
, NotifyAccess notify_access_override
) {
252 s
->notify_access_override
= notify_access_override
;
254 log_unit_debug(UNIT(s
), "notify_access=%s", notify_access_to_string(s
->notify_access
));
255 log_unit_debug(UNIT(s
), "notify_access_override=%s", notify_access_to_string(s
->notify_access_override
));
258 static void service_stop_watchdog(Service
*s
) {
261 s
->watchdog_event_source
= sd_event_source_disable_unref(s
->watchdog_event_source
);
262 s
->watchdog_timestamp
= DUAL_TIMESTAMP_NULL
;
265 static void service_start_watchdog(Service
*s
) {
266 usec_t watchdog_usec
;
271 watchdog_usec
= service_get_watchdog_usec(s
);
272 if (!timestamp_is_set(watchdog_usec
)) {
273 service_stop_watchdog(s
);
277 if (s
->watchdog_event_source
) {
278 r
= sd_event_source_set_time(s
->watchdog_event_source
, usec_add(s
->watchdog_timestamp
.monotonic
, watchdog_usec
));
280 log_unit_warning_errno(UNIT(s
), r
, "Failed to reset watchdog timer: %m");
284 r
= sd_event_source_set_enabled(s
->watchdog_event_source
, SD_EVENT_ONESHOT
);
286 r
= sd_event_add_time(
287 UNIT(s
)->manager
->event
,
288 &s
->watchdog_event_source
,
290 usec_add(s
->watchdog_timestamp
.monotonic
, watchdog_usec
), 0,
291 service_dispatch_watchdog
, s
);
293 log_unit_warning_errno(UNIT(s
), r
, "Failed to add watchdog timer: %m");
297 (void) sd_event_source_set_description(s
->watchdog_event_source
, "service-watchdog");
299 /* Let's process everything else which might be a sign
300 * of living before we consider a service died. */
301 r
= sd_event_source_set_priority(s
->watchdog_event_source
, SD_EVENT_PRIORITY_IDLE
);
304 log_unit_warning_errno(UNIT(s
), r
, "Failed to install watchdog timer: %m");
307 usec_t
service_restart_usec_next(Service
*s
) {
308 unsigned n_restarts_next
;
313 /* When the service state is in SERVICE_*_BEFORE_AUTO_RESTART or SERVICE_AUTO_RESTART, we still need
314 * to add 1 to s->n_restarts manually, because s->n_restarts is not updated until a restart job is
315 * enqueued, i.e. state has transitioned to SERVICE_AUTO_RESTART_QUEUED. */
316 n_restarts_next
= s
->n_restarts
+ (s
->state
== SERVICE_AUTO_RESTART_QUEUED
? 0 : 1);
318 if (n_restarts_next
<= 1 ||
319 s
->restart_steps
== 0 ||
320 s
->restart_usec
== 0 ||
321 s
->restart_max_delay_usec
== USEC_INFINITY
||
322 s
->restart_usec
>= s
->restart_max_delay_usec
)
323 value
= s
->restart_usec
;
324 else if (n_restarts_next
> s
->restart_steps
)
325 value
= s
->restart_max_delay_usec
;
327 /* Enforced in service_verify() and above */
328 assert(s
->restart_max_delay_usec
> s
->restart_usec
);
330 /* r_i / r_0 = (r_n / r_0) ^ (i / n)
332 * r_0 : initial restart usec (s->restart_usec),
333 * r_i : i-th restart usec (value),
334 * r_n : maximum restart usec (s->restart_max_delay_usec),
335 * i : index of the next step (n_restarts_next - 1)
336 * n : num maximum steps (s->restart_steps) */
337 value
= (usec_t
) (s
->restart_usec
* powl((long double) s
->restart_max_delay_usec
/ s
->restart_usec
,
338 (long double) (n_restarts_next
- 1) / s
->restart_steps
));
341 log_unit_debug(UNIT(s
), "Next restart interval calculated as: %s", FORMAT_TIMESPAN(value
, 0));
345 static void service_extend_event_source_timeout(Service
*s
, sd_event_source
*source
, usec_t extended
) {
351 /* Extends the specified event source timer to at least the specified time, unless it is already later
357 r
= sd_event_source_get_time(source
, ¤t
);
360 (void) sd_event_source_get_description(s
->timer_event_source
, &desc
);
361 log_unit_warning_errno(UNIT(s
), r
, "Failed to retrieve timeout time for event source '%s', ignoring: %m", strna(desc
));
365 if (current
>= extended
) /* Current timeout is already longer, ignore this. */
368 r
= sd_event_source_set_time(source
, extended
);
371 (void) sd_event_source_get_description(s
->timer_event_source
, &desc
);
372 log_unit_warning_errno(UNIT(s
), r
, "Failed to set timeout time for event source '%s', ignoring %m", strna(desc
));
376 static void service_extend_timeout(Service
*s
, usec_t extend_timeout_usec
) {
381 if (!timestamp_is_set(extend_timeout_usec
))
384 extended
= usec_add(now(CLOCK_MONOTONIC
), extend_timeout_usec
);
386 service_extend_event_source_timeout(s
, s
->timer_event_source
, extended
);
387 service_extend_event_source_timeout(s
, s
->watchdog_event_source
, extended
);
390 static void service_reset_watchdog(Service
*s
) {
393 dual_timestamp_get(&s
->watchdog_timestamp
);
394 service_start_watchdog(s
);
397 static void service_override_watchdog_timeout(Service
*s
, usec_t watchdog_override_usec
) {
400 s
->watchdog_override_enable
= true;
401 s
->watchdog_override_usec
= watchdog_override_usec
;
402 service_reset_watchdog(s
);
404 log_unit_debug(UNIT(s
), "watchdog_usec="USEC_FMT
, s
->watchdog_usec
);
405 log_unit_debug(UNIT(s
), "watchdog_override_usec="USEC_FMT
, s
->watchdog_override_usec
);
408 static ServiceFDStore
* service_fd_store_unlink(ServiceFDStore
*fs
) {
413 assert(fs
->service
->n_fd_store
> 0);
414 LIST_REMOVE(fd_store
, fs
->service
->fd_store
, fs
);
415 fs
->service
->n_fd_store
--;
418 sd_event_source_disable_unref(fs
->event_source
);
421 asynchronous_close(fs
->fd
);
425 DEFINE_TRIVIAL_CLEANUP_FUNC(ServiceFDStore
*, service_fd_store_unlink
);
427 static void service_release_fd_store(Service
*s
) {
433 log_unit_debug(UNIT(s
), "Releasing all stored fds");
436 service_fd_store_unlink(s
->fd_store
);
438 assert(s
->n_fd_store
== 0);
441 static void service_release_stdio_fd(Service
*s
) {
444 if (s
->stdin_fd
< 0 && s
->stdout_fd
< 0 && s
->stdout_fd
< 0)
447 log_unit_debug(UNIT(s
), "Releasing stdin/stdout/stderr file descriptors.");
449 s
->stdin_fd
= asynchronous_close(s
->stdin_fd
);
450 s
->stdout_fd
= asynchronous_close(s
->stdout_fd
);
451 s
->stderr_fd
= asynchronous_close(s
->stderr_fd
);
453 static void service_done(Unit
*u
) {
454 Service
*s
= SERVICE(u
);
458 open_file_free_many(&s
->open_files
);
460 s
->pid_file
= mfree(s
->pid_file
);
461 s
->status_text
= mfree(s
->status_text
);
463 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
) {
631 if (s
->timer_event_source
) {
632 r
= (relative
? sd_event_source_set_time_relative
: sd_event_source_set_time
)(s
->timer_event_source
, usec
);
636 return sd_event_source_set_enabled(s
->timer_event_source
, SD_EVENT_ONESHOT
);
639 if (usec
== USEC_INFINITY
)
642 r
= (relative
? sd_event_add_time_relative
: sd_event_add_time
)(
643 UNIT(s
)->manager
->event
,
644 &s
->timer_event_source
,
647 service_dispatch_timer
, s
);
651 (void) sd_event_source_set_description(s
->timer_event_source
, "service-timer");
656 static int service_verify(Service
*s
) {
658 assert(UNIT(s
)->load_state
== UNIT_LOADED
);
660 for (ServiceExecCommand c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++)
661 LIST_FOREACH(command
, command
, s
->exec_command
[c
]) {
662 if (!path_is_absolute(command
->path
) && !filename_is_valid(command
->path
))
663 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
),
664 "Service %s= binary path \"%s\" is neither a valid executable name nor an absolute path. Refusing.",
666 service_exec_command_to_string(c
));
667 if (strv_isempty(command
->argv
))
668 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
),
669 "Service has an empty argv in %s=. Refusing.",
670 service_exec_command_to_string(c
));
673 if (!s
->exec_command
[SERVICE_EXEC_START
] && !s
->exec_command
[SERVICE_EXEC_STOP
] &&
674 UNIT(s
)->success_action
== EMERGENCY_ACTION_NONE
)
675 /* FailureAction= only makes sense if one of the start or stop commands is specified.
676 * SuccessAction= will be executed unconditionally if no commands are specified. Hence,
677 * either a command or SuccessAction= are required. */
679 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "Service has no ExecStart=, ExecStop=, or SuccessAction=. Refusing.");
681 if (s
->type
!= SERVICE_ONESHOT
&& !s
->exec_command
[SERVICE_EXEC_START
])
682 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "Service has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
684 if (!s
->remain_after_exit
&& !s
->exec_command
[SERVICE_EXEC_START
] && UNIT(s
)->success_action
== EMERGENCY_ACTION_NONE
)
685 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.");
687 if (s
->type
!= SERVICE_ONESHOT
&& s
->exec_command
[SERVICE_EXEC_START
]->command_next
)
688 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.");
690 if (s
->type
== SERVICE_ONESHOT
&&
691 !IN_SET(s
->restart
, SERVICE_RESTART_NO
, SERVICE_RESTART_ON_FAILURE
, SERVICE_RESTART_ON_ABNORMAL
, SERVICE_RESTART_ON_WATCHDOG
, SERVICE_RESTART_ON_ABORT
))
692 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.");
694 if (s
->type
== SERVICE_ONESHOT
&& !exit_status_set_is_empty(&s
->restart_force_status
))
695 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "Service has RestartForceExitStatus= set, which isn't allowed for Type=oneshot services. Refusing.");
697 if (s
->type
== SERVICE_ONESHOT
&& s
->exit_type
== SERVICE_EXIT_CGROUP
)
698 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "Service has ExitType=cgroup set, which isn't allowed for Type=oneshot services. Refusing.");
700 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
)
701 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.");
703 if (s
->exec_context
.pam_name
&& !IN_SET(s
->kill_context
.kill_mode
, KILL_CONTROL_GROUP
, KILL_MIXED
))
704 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.");
706 if (s
->usb_function_descriptors
&& !s
->usb_function_strings
)
707 log_unit_warning(UNIT(s
), "Service has USBFunctionDescriptors= setting, but no USBFunctionStrings=. Ignoring.");
709 if (!s
->usb_function_descriptors
&& s
->usb_function_strings
)
710 log_unit_warning(UNIT(s
), "Service has USBFunctionStrings= setting, but no USBFunctionDescriptors=. Ignoring.");
712 if (s
->runtime_max_usec
!= USEC_INFINITY
&& s
->type
== SERVICE_ONESHOT
)
713 log_unit_warning(UNIT(s
), "RuntimeMaxSec= has no effect in combination with Type=oneshot. Ignoring.");
715 if (s
->runtime_max_usec
== USEC_INFINITY
&& s
->runtime_rand_extra_usec
!= 0)
716 log_unit_warning(UNIT(s
), "Service has RuntimeRandomizedExtraSec= setting, but no RuntimeMaxSec=. Ignoring.");
718 if (s
->exit_type
== SERVICE_EXIT_CGROUP
&& cg_unified() < CGROUP_UNIFIED_SYSTEMD
)
719 log_unit_warning(UNIT(s
), "Service has ExitType=cgroup set, but we are running with legacy cgroups v1, which might not work correctly. Continuing.");
721 if (s
->restart_max_delay_usec
== USEC_INFINITY
&& s
->restart_steps
> 0)
722 log_unit_warning(UNIT(s
), "Service has RestartSteps= but no RestartMaxDelaySec= setting. Ignoring.");
724 if (s
->restart_max_delay_usec
!= USEC_INFINITY
&& s
->restart_steps
== 0)
725 log_unit_warning(UNIT(s
), "Service has RestartMaxDelaySec= but no RestartSteps= setting. Ignoring.");
727 if (s
->restart_max_delay_usec
< s
->restart_usec
) {
728 log_unit_warning(UNIT(s
), "RestartMaxDelaySec= has a value smaller than RestartSec=, resetting RestartSec= to RestartMaxDelaySec=.");
729 s
->restart_usec
= s
->restart_max_delay_usec
;
735 static int service_add_default_dependencies(Service
*s
) {
740 if (!UNIT(s
)->default_dependencies
)
743 /* Add a number of automatic dependencies useful for the
744 * majority of services. */
746 if (MANAGER_IS_SYSTEM(UNIT(s
)->manager
)) {
747 /* First, pull in the really early boot stuff, and
748 * require it, so that we fail if we can't acquire
751 r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_SYSINIT_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
756 /* In the --user instance there's no sysinit.target,
757 * in that case require basic.target instead. */
759 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
764 /* Second, if the rest of the base system is in the same
765 * transaction, order us after it, but do not pull it in or
766 * even require it. */
767 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, SPECIAL_BASIC_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
771 /* Third, add us in for normal shutdown. */
772 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
775 static void service_fix_stdio(Service
*s
) {
778 /* Note that EXEC_INPUT_NULL and EXEC_OUTPUT_INHERIT play a special role here: they are both the
779 * default value that is subject to automatic overriding triggered by other settings and an explicit
780 * choice the user can make. We don't distinguish between these cases currently. */
782 if (s
->exec_context
.std_input
== EXEC_INPUT_NULL
&&
783 s
->exec_context
.stdin_data_size
> 0)
784 s
->exec_context
.std_input
= EXEC_INPUT_DATA
;
786 if (IN_SET(s
->exec_context
.std_input
,
788 EXEC_INPUT_TTY_FORCE
,
791 EXEC_INPUT_NAMED_FD
))
794 /* We assume these listed inputs refer to bidirectional streams, and hence duplicating them from
795 * stdin to stdout/stderr makes sense and hence leaving EXEC_OUTPUT_INHERIT in place makes sense,
796 * too. Outputs such as regular files or sealed data memfds otoh don't really make sense to be
797 * duplicated for both input and output at the same time (since they then would cause a feedback
798 * loop), hence override EXEC_OUTPUT_INHERIT with the default stderr/stdout setting. */
800 if (s
->exec_context
.std_error
== EXEC_OUTPUT_INHERIT
&&
801 s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
)
802 s
->exec_context
.std_error
= UNIT(s
)->manager
->defaults
.std_error
;
804 if (s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
)
805 s
->exec_context
.std_output
= UNIT(s
)->manager
->defaults
.std_output
;
808 static int service_setup_bus_name(Service
*s
) {
813 /* If s->bus_name is not set, then the unit will be refused by service_verify() later. */
817 if (s
->type
== SERVICE_DBUS
) {
818 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_REQUIRES
, SPECIAL_DBUS_SOCKET
, true, UNIT_DEPENDENCY_FILE
);
820 return log_unit_error_errno(UNIT(s
), r
, "Failed to add dependency on " SPECIAL_DBUS_SOCKET
": %m");
822 /* We always want to be ordered against dbus.socket if both are in the transaction. */
823 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, SPECIAL_DBUS_SOCKET
, true, UNIT_DEPENDENCY_FILE
);
825 return log_unit_error_errno(UNIT(s
), r
, "Failed to add dependency on " SPECIAL_DBUS_SOCKET
": %m");
828 r
= unit_watch_bus_name(UNIT(s
), s
->bus_name
);
830 return log_unit_error_errno(UNIT(s
), r
, "Two services allocated for the same bus name %s, refusing operation.", s
->bus_name
);
832 return log_unit_error_errno(UNIT(s
), r
, "Cannot watch bus name %s: %m", s
->bus_name
);
837 static int service_add_extras(Service
*s
) {
842 if (s
->type
== _SERVICE_TYPE_INVALID
) {
843 /* Figure out a type automatically */
845 s
->type
= SERVICE_DBUS
;
846 else if (s
->exec_command
[SERVICE_EXEC_START
])
847 s
->type
= SERVICE_SIMPLE
;
849 s
->type
= SERVICE_ONESHOT
;
852 /* Oneshot services have disabled start timeout by default */
853 if (s
->type
== SERVICE_ONESHOT
&& !s
->start_timeout_defined
)
854 s
->timeout_start_usec
= USEC_INFINITY
;
856 service_fix_stdio(s
);
858 r
= unit_patch_contexts(UNIT(s
));
862 r
= unit_add_exec_dependencies(UNIT(s
), &s
->exec_context
);
866 r
= unit_set_default_slice(UNIT(s
));
870 /* If the service needs the notify socket, let's enable it automatically. */
871 if (s
->notify_access
== NOTIFY_NONE
&&
872 (IN_SET(s
->type
, SERVICE_NOTIFY
, SERVICE_NOTIFY_RELOAD
) || s
->watchdog_usec
> 0 || s
->n_fd_store_max
> 0))
873 s
->notify_access
= NOTIFY_MAIN
;
875 /* If no OOM policy was explicitly set, then default to the configure default OOM policy. Except when
876 * delegation is on, in that case it we assume the payload knows better what to do and can process
877 * things in a more focused way. */
878 if (s
->oom_policy
< 0)
879 s
->oom_policy
= s
->cgroup_context
.delegate
? OOM_CONTINUE
: UNIT(s
)->manager
->defaults
.oom_policy
;
881 /* Let the kernel do the killing if that's requested. */
882 s
->cgroup_context
.memory_oom_group
= s
->oom_policy
== OOM_KILL
;
884 r
= service_add_default_dependencies(s
);
888 r
= service_setup_bus_name(s
);
895 static int service_load(Unit
*u
) {
896 Service
*s
= SERVICE(u
);
899 r
= unit_load_fragment_and_dropin(u
, true);
903 if (u
->load_state
!= UNIT_LOADED
)
906 /* This is a new unit? Then let's add in some extras */
907 r
= service_add_extras(s
);
911 return service_verify(s
);
914 static void service_dump_fdstore(Service
*s
, FILE *f
, const char *prefix
) {
919 LIST_FOREACH(fd_store
, i
, s
->fd_store
) {
920 _cleanup_free_
char *path
= NULL
;
924 if (fstat(i
->fd
, &st
) < 0) {
925 log_debug_errno(errno
, "Failed to stat fdstore entry: %m");
929 flags
= fcntl(i
->fd
, F_GETFL
);
931 log_debug_errno(errno
, "Failed to get fdstore entry flags: %m");
935 (void) fd_get_path(i
->fd
, &path
);
938 "%s%s '%s' (type=%s; dev=" DEVNUM_FORMAT_STR
"; inode=%" PRIu64
"; rdev=" DEVNUM_FORMAT_STR
"; path=%s; access=%s)\n",
939 prefix
, i
== s
->fd_store
? "File Descriptor Store Entry:" : " ",
941 inode_type_to_string(st
.st_mode
),
942 DEVNUM_FORMAT_VAL(st
.st_dev
),
943 (uint64_t) st
.st_ino
,
944 DEVNUM_FORMAT_VAL(st
.st_rdev
),
946 accmode_to_string(flags
));
950 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
951 Service
*s
= SERVICE(u
);
956 prefix
= strempty(prefix
);
957 prefix2
= strjoina(prefix
, "\t");
960 "%sService State: %s\n"
962 "%sReload Result: %s\n"
963 "%sClean Result: %s\n"
964 "%sPermissionsStartOnly: %s\n"
965 "%sRootDirectoryStartOnly: %s\n"
966 "%sRemainAfterExit: %s\n"
967 "%sGuessMainPID: %s\n"
970 "%sNotifyAccess: %s\n"
971 "%sNotifyState: %s\n"
973 "%sReloadSignal: %s\n",
974 prefix
, service_state_to_string(s
->state
),
975 prefix
, service_result_to_string(s
->result
),
976 prefix
, service_result_to_string(s
->reload_result
),
977 prefix
, service_result_to_string(s
->clean_result
),
978 prefix
, yes_no(s
->permissions_start_only
),
979 prefix
, yes_no(s
->root_directory_start_only
),
980 prefix
, yes_no(s
->remain_after_exit
),
981 prefix
, yes_no(s
->guess_main_pid
),
982 prefix
, service_type_to_string(s
->type
),
983 prefix
, service_restart_to_string(s
->restart
),
984 prefix
, notify_access_to_string(service_get_notify_access(s
)),
985 prefix
, notify_state_to_string(s
->notify_state
),
986 prefix
, oom_policy_to_string(s
->oom_policy
),
987 prefix
, signal_to_string(s
->reload_signal
));
989 if (pidref_is_set(&s
->control_pid
))
991 "%sControl PID: "PID_FMT
"\n",
992 prefix
, s
->control_pid
.pid
);
994 if (pidref_is_set(&s
->main_pid
))
996 "%sMain PID: "PID_FMT
"\n"
997 "%sMain PID Known: %s\n"
998 "%sMain PID Alien: %s\n",
999 prefix
, s
->main_pid
.pid
,
1000 prefix
, yes_no(s
->main_pid_known
),
1001 prefix
, yes_no(s
->main_pid_alien
));
1006 prefix
, s
->pid_file
);
1011 "%sBus Name Good: %s\n",
1012 prefix
, s
->bus_name
,
1013 prefix
, yes_no(s
->bus_name_good
));
1015 if (UNIT_ISSET(s
->accept_socket
))
1017 "%sAccept Socket: %s\n",
1018 prefix
, UNIT_DEREF(s
->accept_socket
)->id
);
1021 "%sRestartSec: %s\n"
1022 "%sRestartSteps: %u\n"
1023 "%sRestartMaxDelaySec: %s\n"
1024 "%sTimeoutStartSec: %s\n"
1025 "%sTimeoutStopSec: %s\n"
1026 "%sTimeoutStartFailureMode: %s\n"
1027 "%sTimeoutStopFailureMode: %s\n",
1028 prefix
, FORMAT_TIMESPAN(s
->restart_usec
, USEC_PER_SEC
),
1029 prefix
, s
->restart_steps
,
1030 prefix
, FORMAT_TIMESPAN(s
->restart_max_delay_usec
, USEC_PER_SEC
),
1031 prefix
, FORMAT_TIMESPAN(s
->timeout_start_usec
, USEC_PER_SEC
),
1032 prefix
, FORMAT_TIMESPAN(s
->timeout_stop_usec
, USEC_PER_SEC
),
1033 prefix
, service_timeout_failure_mode_to_string(s
->timeout_start_failure_mode
),
1034 prefix
, service_timeout_failure_mode_to_string(s
->timeout_stop_failure_mode
));
1036 if (s
->timeout_abort_set
)
1038 "%sTimeoutAbortSec: %s\n",
1039 prefix
, FORMAT_TIMESPAN(s
->timeout_abort_usec
, USEC_PER_SEC
));
1042 "%sRuntimeMaxSec: %s\n"
1043 "%sRuntimeRandomizedExtraSec: %s\n"
1044 "%sWatchdogSec: %s\n",
1045 prefix
, FORMAT_TIMESPAN(s
->runtime_max_usec
, USEC_PER_SEC
),
1046 prefix
, FORMAT_TIMESPAN(s
->runtime_rand_extra_usec
, USEC_PER_SEC
),
1047 prefix
, FORMAT_TIMESPAN(s
->watchdog_usec
, USEC_PER_SEC
));
1049 kill_context_dump(&s
->kill_context
, f
, prefix
);
1050 exec_context_dump(&s
->exec_context
, f
, prefix
);
1052 for (ServiceExecCommand c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
1053 if (!s
->exec_command
[c
])
1056 fprintf(f
, "%s-> %s:\n",
1057 prefix
, service_exec_command_to_string(c
));
1059 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
1063 fprintf(f
, "%sStatus Text: %s\n",
1064 prefix
, s
->status_text
);
1066 if (s
->n_fd_store_max
> 0)
1068 "%sFile Descriptor Store Max: %u\n"
1069 "%sFile Descriptor Store Pin: %s\n"
1070 "%sFile Descriptor Store Current: %zu\n",
1071 prefix
, s
->n_fd_store_max
,
1072 prefix
, exec_preserve_mode_to_string(s
->fd_store_preserve_mode
),
1073 prefix
, s
->n_fd_store
);
1075 service_dump_fdstore(s
, f
, prefix
);
1078 LIST_FOREACH(open_files
, of
, s
->open_files
) {
1079 _cleanup_free_
char *ofs
= NULL
;
1082 r
= open_file_to_string(of
, &ofs
);
1085 "Failed to convert OpenFile= setting to string, ignoring: %m");
1089 fprintf(f
, "%sOpen File: %s\n", prefix
, ofs
);
1092 cgroup_context_dump(UNIT(s
), f
, prefix
);
1095 static int service_is_suitable_main_pid(Service
*s
, pid_t pid
, int prio
) {
1099 assert(pid_is_valid(pid
));
1101 /* Checks whether the specified PID is suitable as main PID for this service. returns negative if not, 0 if the
1102 * PID is questionnable but should be accepted if the source of configuration is trusted. > 0 if the PID is
1105 if (pid
== getpid_cached() || pid
== 1)
1106 return log_unit_full_errno(UNIT(s
), prio
, SYNTHETIC_ERRNO(EPERM
), "New main PID "PID_FMT
" is the manager, refusing.", pid
);
1108 if (pid
== s
->control_pid
.pid
)
1109 return log_unit_full_errno(UNIT(s
), prio
, SYNTHETIC_ERRNO(EPERM
), "New main PID "PID_FMT
" is the control process, refusing.", pid
);
1111 if (!pid_is_alive(pid
))
1112 return log_unit_full_errno(UNIT(s
), prio
, SYNTHETIC_ERRNO(ESRCH
), "New main PID "PID_FMT
" does not exist or is a zombie.", pid
);
1114 owner
= manager_get_unit_by_pid(UNIT(s
)->manager
, pid
);
1115 if (owner
== UNIT(s
)) {
1116 log_unit_debug(UNIT(s
), "New main PID "PID_FMT
" belongs to service, we are happy.", pid
);
1117 return 1; /* Yay, it's definitely a good PID */
1120 return 0; /* Hmm it's a suspicious PID, let's accept it if configuration source is trusted */
1123 static int service_load_pid_file(Service
*s
, bool may_warn
) {
1124 _cleanup_(pidref_done
) PidRef pidref
= PIDREF_NULL
;
1125 bool questionable_pid_file
= false;
1126 _cleanup_free_
char *k
= NULL
;
1127 _cleanup_close_
int fd
= -EBADF
;
1136 prio
= may_warn
? LOG_INFO
: LOG_DEBUG
;
1138 r
= chase(s
->pid_file
, NULL
, CHASE_SAFE
, NULL
, &fd
);
1139 if (r
== -ENOLINK
) {
1140 log_unit_debug_errno(UNIT(s
), r
,
1141 "Potentially unsafe symlink chain, will now retry with relaxed checks: %s", s
->pid_file
);
1143 questionable_pid_file
= true;
1145 r
= chase(s
->pid_file
, NULL
, 0, NULL
, &fd
);
1148 return log_unit_full_errno(UNIT(s
), prio
, r
,
1149 "Can't open PID file %s (yet?) after %s: %m", s
->pid_file
, service_state_to_string(s
->state
));
1151 /* Let's read the PID file now that we chased it down. But we need to convert the O_PATH fd
1152 * chase() returned us into a proper fd first. */
1153 r
= read_one_line_file(FORMAT_PROC_FD_PATH(fd
), &k
);
1155 return log_unit_error_errno(UNIT(s
), r
,
1156 "Can't convert PID files %s O_PATH file descriptor to proper file descriptor: %m",
1159 r
= parse_pid(k
, &pid
);
1161 return log_unit_full_errno(UNIT(s
), prio
, r
, "Failed to parse PID from file %s: %m", s
->pid_file
);
1163 if (s
->main_pid_known
&& pid
== s
->main_pid
.pid
)
1166 r
= pidref_set_pid(&pidref
, pid
);
1168 return log_unit_full_errno(UNIT(s
), prio
, r
, "Failed to pin PID " PID_FMT
": %m", pid
);
1170 r
= service_is_suitable_main_pid(s
, pidref
.pid
, prio
);
1176 if (questionable_pid_file
)
1177 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(EPERM
),
1178 "Refusing to accept PID outside of service control group, acquired through unsafe symlink chain: %s", s
->pid_file
);
1180 /* Hmm, it's not clear if the new main PID is safe. Let's allow this if the PID file is owned by root */
1182 if (fstat(fd
, &st
) < 0)
1183 return log_unit_error_errno(UNIT(s
), errno
, "Failed to fstat() PID file O_PATH fd: %m");
1186 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(EPERM
),
1187 "New main PID "PID_FMT
" does not belong to service, and PID file is not owned by root. Refusing.", pidref
.pid
);
1189 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
);
1192 if (s
->main_pid_known
) {
1193 log_unit_debug(UNIT(s
), "Main PID changing: "PID_FMT
" -> "PID_FMT
, s
->main_pid
.pid
, pidref
.pid
);
1195 service_unwatch_main_pid(s
);
1196 s
->main_pid_known
= false;
1198 log_unit_debug(UNIT(s
), "Main PID loaded: "PID_FMT
, pidref
.pid
);
1200 r
= service_set_main_pidref(s
, &pidref
);
1204 r
= unit_watch_pid(UNIT(s
), s
->main_pid
.pid
, /* exclusive= */ false);
1205 if (r
< 0) /* FIXME: we need to do something here */
1206 return log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" for service: %m", s
->main_pid
.pid
);
1211 static void service_search_main_pid(Service
*s
) {
1217 /* If we know it anyway, don't ever fall back to unreliable heuristics */
1218 if (s
->main_pid_known
)
1221 if (!s
->guess_main_pid
)
1224 assert(!pidref_is_set(&s
->main_pid
));
1226 if (unit_search_main_pid(UNIT(s
), &pid
) < 0)
1229 log_unit_debug(UNIT(s
), "Main PID guessed: "PID_FMT
, pid
);
1230 if (service_set_main_pid(s
, pid
) < 0)
1233 r
= unit_watch_pid(UNIT(s
), s
->main_pid
.pid
, /* exclusive= */ false);
1235 /* FIXME: we need to do something here */
1236 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" from: %m", s
->main_pid
.pid
);
1239 static void service_set_state(Service
*s
, ServiceState state
) {
1240 ServiceState old_state
;
1241 const UnitActiveState
*table
;
1245 if (s
->state
!= state
)
1246 bus_unit_send_pending_change_signal(UNIT(s
), false);
1248 table
= s
->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
1250 old_state
= s
->state
;
1253 service_unwatch_pid_file(s
);
1256 SERVICE_CONDITION
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
1258 SERVICE_RELOAD
, SERVICE_RELOAD_SIGNAL
, SERVICE_RELOAD_NOTIFY
,
1259 SERVICE_STOP
, SERVICE_STOP_WATCHDOG
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1260 SERVICE_FINAL_WATCHDOG
, SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
,
1261 SERVICE_AUTO_RESTART
,
1263 s
->timer_event_source
= sd_event_source_disable_unref(s
->timer_event_source
);
1266 SERVICE_START
, SERVICE_START_POST
,
1268 SERVICE_RELOAD
, SERVICE_RELOAD_SIGNAL
, SERVICE_RELOAD_NOTIFY
,
1269 SERVICE_STOP
, SERVICE_STOP_WATCHDOG
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1270 SERVICE_FINAL_WATCHDOG
, SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
1271 service_unwatch_main_pid(s
);
1272 s
->main_command
= NULL
;
1276 SERVICE_CONDITION
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
1277 SERVICE_RELOAD
, SERVICE_RELOAD_SIGNAL
, SERVICE_RELOAD_NOTIFY
,
1278 SERVICE_STOP
, SERVICE_STOP_WATCHDOG
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1279 SERVICE_FINAL_WATCHDOG
, SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
,
1280 SERVICE_CLEANING
)) {
1281 service_unwatch_control_pid(s
);
1282 s
->control_command
= NULL
;
1283 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1287 SERVICE_DEAD
, SERVICE_FAILED
,
1288 SERVICE_DEAD_BEFORE_AUTO_RESTART
, SERVICE_FAILED_BEFORE_AUTO_RESTART
, SERVICE_AUTO_RESTART
, SERVICE_AUTO_RESTART_QUEUED
,
1289 SERVICE_DEAD_RESOURCES_PINNED
)) {
1290 unit_unwatch_all_pids(UNIT(s
));
1291 unit_dequeue_rewatch_pids(UNIT(s
));
1294 if (state
!= SERVICE_START
)
1295 s
->exec_fd_event_source
= sd_event_source_disable_unref(s
->exec_fd_event_source
);
1297 if (!IN_SET(state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
, SERVICE_RELOAD_SIGNAL
, SERVICE_RELOAD_NOTIFY
))
1298 service_stop_watchdog(s
);
1300 /* For the inactive states unit_notify() will trim the cgroup,
1301 * but for exit we have to do that ourselves... */
1302 if (state
== SERVICE_EXITED
&& !MANAGER_IS_RELOADING(UNIT(s
)->manager
))
1303 unit_prune_cgroup(UNIT(s
));
1305 if (old_state
!= state
)
1306 log_unit_debug(UNIT(s
), "Changed %s -> %s", service_state_to_string(old_state
), service_state_to_string(state
));
1308 unit_notify(UNIT(s
), table
[old_state
], table
[state
], s
->reload_result
== SERVICE_SUCCESS
);
1311 static usec_t
service_coldplug_timeout(Service
*s
) {
1314 switch (s
->deserialized_state
) {
1316 case SERVICE_CONDITION
:
1317 case SERVICE_START_PRE
:
1319 case SERVICE_START_POST
:
1320 case SERVICE_RELOAD
:
1321 case SERVICE_RELOAD_SIGNAL
:
1322 case SERVICE_RELOAD_NOTIFY
:
1323 return usec_add(UNIT(s
)->state_change_timestamp
.monotonic
, s
->timeout_start_usec
);
1325 case SERVICE_RUNNING
:
1326 return service_running_timeout(s
);
1329 case SERVICE_STOP_SIGTERM
:
1330 case SERVICE_STOP_SIGKILL
:
1331 case SERVICE_STOP_POST
:
1332 case SERVICE_FINAL_SIGTERM
:
1333 case SERVICE_FINAL_SIGKILL
:
1334 return usec_add(UNIT(s
)->state_change_timestamp
.monotonic
, s
->timeout_stop_usec
);
1336 case SERVICE_STOP_WATCHDOG
:
1337 case SERVICE_FINAL_WATCHDOG
:
1338 return usec_add(UNIT(s
)->state_change_timestamp
.monotonic
, service_timeout_abort_usec(s
));
1340 case SERVICE_AUTO_RESTART
:
1341 return usec_add(UNIT(s
)->inactive_enter_timestamp
.monotonic
, service_restart_usec_next(s
));
1343 case SERVICE_CLEANING
:
1344 return usec_add(UNIT(s
)->state_change_timestamp
.monotonic
, s
->exec_context
.timeout_clean_usec
);
1347 return USEC_INFINITY
;
1351 static int service_coldplug(Unit
*u
) {
1352 Service
*s
= SERVICE(u
);
1356 assert(s
->state
== SERVICE_DEAD
);
1358 if (s
->deserialized_state
== s
->state
)
1361 r
= service_arm_timer(s
, /* relative= */ false, service_coldplug_timeout(s
));
1365 if (pidref_is_set(&s
->main_pid
) &&
1366 pid_is_unwaited(s
->main_pid
.pid
) &&
1367 (IN_SET(s
->deserialized_state
,
1368 SERVICE_START
, SERVICE_START_POST
,
1370 SERVICE_RELOAD
, SERVICE_RELOAD_SIGNAL
, SERVICE_RELOAD_NOTIFY
,
1371 SERVICE_STOP
, SERVICE_STOP_WATCHDOG
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1372 SERVICE_FINAL_WATCHDOG
, SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))) {
1373 r
= unit_watch_pid(UNIT(s
), s
->main_pid
.pid
, /* exclusive= */ false);
1378 if (pidref_is_set(&s
->control_pid
) &&
1379 pid_is_unwaited(s
->control_pid
.pid
) &&
1380 IN_SET(s
->deserialized_state
,
1381 SERVICE_CONDITION
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
1382 SERVICE_RELOAD
, SERVICE_RELOAD_SIGNAL
, SERVICE_RELOAD_NOTIFY
,
1383 SERVICE_STOP
, SERVICE_STOP_WATCHDOG
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1384 SERVICE_FINAL_WATCHDOG
, SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
,
1385 SERVICE_CLEANING
)) {
1386 r
= unit_watch_pid(UNIT(s
), s
->control_pid
.pid
, /* exclusive= */ false);
1391 if (!IN_SET(s
->deserialized_state
,
1392 SERVICE_DEAD
, SERVICE_FAILED
,
1393 SERVICE_DEAD_BEFORE_AUTO_RESTART
, SERVICE_FAILED_BEFORE_AUTO_RESTART
, SERVICE_AUTO_RESTART
, SERVICE_AUTO_RESTART_QUEUED
,
1395 SERVICE_DEAD_RESOURCES_PINNED
)) {
1396 (void) unit_enqueue_rewatch_pids(u
);
1397 (void) unit_setup_exec_runtime(u
);
1400 if (IN_SET(s
->deserialized_state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
, SERVICE_RELOAD_SIGNAL
, SERVICE_RELOAD_NOTIFY
))
1401 service_start_watchdog(s
);
1403 if (UNIT_ISSET(s
->accept_socket
)) {
1404 Socket
* socket
= SOCKET(UNIT_DEREF(s
->accept_socket
));
1406 if (socket
->max_connections_per_source
> 0) {
1409 /* Make a best-effort attempt at bumping the connection count */
1410 if (socket_acquire_peer(socket
, s
->socket_fd
, &peer
) > 0) {
1411 socket_peer_unref(s
->socket_peer
);
1412 s
->socket_peer
= peer
;
1417 service_set_state(s
, s
->deserialized_state
);
1421 static int service_collect_fds(
1425 size_t *n_socket_fds
,
1426 size_t *n_storage_fds
) {
1428 _cleanup_strv_free_
char **rfd_names
= NULL
;
1429 _cleanup_free_
int *rfds
= NULL
;
1430 size_t rn_socket_fds
= 0, rn_storage_fds
= 0;
1436 assert(n_socket_fds
);
1437 assert(n_storage_fds
);
1439 if (s
->socket_fd
>= 0) {
1441 /* Pass the per-connection socket */
1443 rfds
= newdup(int, &s
->socket_fd
, 1);
1447 rfd_names
= strv_new("connection");
1455 /* Pass all our configured sockets for singleton services */
1457 UNIT_FOREACH_DEPENDENCY(u
, UNIT(s
), UNIT_ATOM_TRIGGERED_BY
) {
1458 _cleanup_free_
int *cfds
= NULL
;
1462 if (u
->type
!= UNIT_SOCKET
)
1467 cn_fds
= socket_collect_fds(sock
, &cfds
);
1475 rfds
= TAKE_PTR(cfds
);
1476 rn_socket_fds
= cn_fds
;
1480 t
= reallocarray(rfds
, rn_socket_fds
+ cn_fds
, sizeof(int));
1484 memcpy(t
+ rn_socket_fds
, cfds
, cn_fds
* sizeof(int));
1487 rn_socket_fds
+= cn_fds
;
1490 r
= strv_extend_n(&rfd_names
, socket_fdname(sock
), cn_fds
);
1496 if (s
->n_fd_store
> 0) {
1501 t
= reallocarray(rfds
, rn_socket_fds
+ s
->n_fd_store
, sizeof(int));
1507 nl
= reallocarray(rfd_names
, rn_socket_fds
+ s
->n_fd_store
+ 1, sizeof(char *));
1512 n_fds
= rn_socket_fds
;
1514 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
1515 rfds
[n_fds
] = fs
->fd
;
1516 rfd_names
[n_fds
] = strdup(strempty(fs
->fdname
));
1517 if (!rfd_names
[n_fds
])
1524 rfd_names
[n_fds
] = NULL
;
1527 *fds
= TAKE_PTR(rfds
);
1528 *fd_names
= TAKE_PTR(rfd_names
);
1529 *n_socket_fds
= rn_socket_fds
;
1530 *n_storage_fds
= rn_storage_fds
;
1535 static int service_allocate_exec_fd_event_source(
1538 sd_event_source
**ret_event_source
) {
1540 _cleanup_(sd_event_source_unrefp
) sd_event_source
*source
= NULL
;
1545 assert(ret_event_source
);
1547 r
= sd_event_add_io(UNIT(s
)->manager
->event
, &source
, fd
, 0, service_dispatch_exec_io
, s
);
1549 return log_unit_error_errno(UNIT(s
), r
, "Failed to allocate exec_fd event source: %m");
1551 /* This is a bit lower priority than SIGCHLD, as that carries a lot more interesting failure information */
1553 r
= sd_event_source_set_priority(source
, SD_EVENT_PRIORITY_NORMAL
-3);
1555 return log_unit_error_errno(UNIT(s
), r
, "Failed to adjust priority of exec_fd event source: %m");
1557 (void) sd_event_source_set_description(source
, "service exec_fd");
1559 r
= sd_event_source_set_io_fd_own(source
, true);
1561 return log_unit_error_errno(UNIT(s
), r
, "Failed to pass ownership of fd to event source: %m");
1563 *ret_event_source
= TAKE_PTR(source
);
1567 static int service_allocate_exec_fd(
1569 sd_event_source
**ret_event_source
,
1572 _cleanup_close_pair_
int p
[] = PIPE_EBADF
;
1576 assert(ret_event_source
);
1577 assert(ret_exec_fd
);
1579 if (pipe2(p
, O_CLOEXEC
|O_NONBLOCK
) < 0)
1580 return log_unit_error_errno(UNIT(s
), errno
, "Failed to allocate exec_fd pipe: %m");
1582 r
= service_allocate_exec_fd_event_source(s
, p
[0], ret_event_source
);
1587 *ret_exec_fd
= TAKE_FD(p
[1]);
1592 static bool service_exec_needs_notify_socket(Service
*s
, ExecFlags flags
) {
1595 /* Notifications are accepted depending on the process and
1596 * the access setting of the service:
1597 * process: \ access: NONE MAIN EXEC ALL
1598 * main no yes yes yes
1599 * control no no yes yes
1600 * other (forked) no no no yes */
1602 if (flags
& EXEC_IS_CONTROL
)
1603 /* A control process */
1604 return IN_SET(service_get_notify_access(s
), NOTIFY_EXEC
, NOTIFY_ALL
);
1606 /* We only spawn main processes and control processes, so any
1607 * process that is not a control process is a main process */
1608 return service_get_notify_access(s
) != NOTIFY_NONE
;
1611 static Service
*service_get_triggering_service(Service
*s
) {
1612 Unit
*candidate
= NULL
, *other
;
1616 /* Return the service which triggered service 's', this means dependency
1617 * types which include the UNIT_ATOM_ON_{FAILURE,SUCCESS}_OF atoms.
1619 * N.B. if there are multiple services which could trigger 's' via OnFailure=
1620 * or OnSuccess= then we return NULL. This is since we don't know from which
1621 * one to propagate the exit status. */
1623 UNIT_FOREACH_DEPENDENCY(other
, UNIT(s
), UNIT_ATOM_ON_FAILURE_OF
) {
1629 UNIT_FOREACH_DEPENDENCY(other
, UNIT(s
), UNIT_ATOM_ON_SUCCESS_OF
) {
1635 return SERVICE(candidate
);
1638 log_unit_warning(UNIT(s
), "multiple trigger source candidates for exit status propagation (%s, %s), skipping.",
1639 candidate
->id
, other
->id
);
1643 static int service_spawn_internal(
1651 _cleanup_(exec_params_clear
) ExecParameters exec_params
= {
1654 .stdout_fd
= -EBADF
,
1655 .stderr_fd
= -EBADF
,
1658 _cleanup_(sd_event_source_unrefp
) sd_event_source
*exec_fd_source
= NULL
;
1659 _cleanup_strv_free_
char **final_env
= NULL
, **our_env
= NULL
;
1660 _cleanup_(pidref_done
) PidRef pidref
= PIDREF_NULL
;
1670 log_unit_debug(UNIT(s
), "Will spawn child (%s): %s", caller
, c
->path
);
1672 r
= unit_prepare_exec(UNIT(s
)); /* This realizes the cgroup, among other things */
1676 assert(!s
->exec_fd_event_source
);
1678 if (flags
& EXEC_IS_CONTROL
) {
1679 /* If this is a control process, mask the permissions/chroot application if this is requested. */
1680 if (s
->permissions_start_only
)
1681 exec_params
.flags
&= ~EXEC_APPLY_SANDBOXING
;
1682 if (s
->root_directory_start_only
)
1683 exec_params
.flags
&= ~EXEC_APPLY_CHROOT
;
1686 if ((flags
& EXEC_PASS_FDS
) ||
1687 s
->exec_context
.std_input
== EXEC_INPUT_SOCKET
||
1688 s
->exec_context
.std_output
== EXEC_OUTPUT_SOCKET
||
1689 s
->exec_context
.std_error
== EXEC_OUTPUT_SOCKET
) {
1691 r
= service_collect_fds(s
,
1693 &exec_params
.fd_names
,
1694 &exec_params
.n_socket_fds
,
1695 &exec_params
.n_storage_fds
);
1699 exec_params
.open_files
= s
->open_files
;
1701 log_unit_debug(UNIT(s
), "Passing %zu fds to service", exec_params
.n_socket_fds
+ exec_params
.n_storage_fds
);
1704 if (!FLAGS_SET(flags
, EXEC_IS_CONTROL
) && s
->type
== SERVICE_EXEC
) {
1705 r
= service_allocate_exec_fd(s
, &exec_fd_source
, &exec_params
.exec_fd
);
1710 r
= service_arm_timer(s
, /* relative= */ true, timeout
);
1714 our_env
= new0(char*, 13);
1718 if (service_exec_needs_notify_socket(s
, flags
)) {
1719 if (asprintf(our_env
+ n_env
++, "NOTIFY_SOCKET=%s", UNIT(s
)->manager
->notify_socket
) < 0)
1722 exec_params
.notify_socket
= UNIT(s
)->manager
->notify_socket
;
1724 if (s
->n_fd_store_max
> 0)
1725 if (asprintf(our_env
+ n_env
++, "FDSTORE=%u", s
->n_fd_store_max
) < 0)
1729 if (pidref_is_set(&s
->main_pid
))
1730 if (asprintf(our_env
+ n_env
++, "MAINPID="PID_FMT
, s
->main_pid
.pid
) < 0)
1733 if (MANAGER_IS_USER(UNIT(s
)->manager
))
1734 if (asprintf(our_env
+ n_env
++, "MANAGERPID="PID_FMT
, getpid_cached()) < 0)
1738 if (asprintf(our_env
+ n_env
++, "PIDFILE=%s", s
->pid_file
) < 0)
1741 if (s
->socket_fd
>= 0) {
1742 union sockaddr_union sa
;
1743 socklen_t salen
= sizeof(sa
);
1745 /* If this is a per-connection service instance, let's set $REMOTE_ADDR and $REMOTE_PORT to something
1746 * useful. Note that we do this only when we are still connected at this point in time, which we might
1747 * very well not be. Hence we ignore all errors when retrieving peer information (as that might result
1748 * in ENOTCONN), and just use whate we can use. */
1750 if (getpeername(s
->socket_fd
, &sa
.sa
, &salen
) >= 0 &&
1751 IN_SET(sa
.sa
.sa_family
, AF_INET
, AF_INET6
, AF_VSOCK
)) {
1752 _cleanup_free_
char *addr
= NULL
;
1756 r
= sockaddr_pretty(&sa
.sa
, salen
, true, false, &addr
);
1760 t
= strjoin("REMOTE_ADDR=", addr
);
1763 our_env
[n_env
++] = t
;
1765 r
= sockaddr_port(&sa
.sa
, &port
);
1769 if (asprintf(&t
, "REMOTE_PORT=%u", port
) < 0)
1771 our_env
[n_env
++] = t
;
1775 Service
*env_source
= NULL
;
1776 const char *monitor_prefix
;
1777 if (flags
& EXEC_SETENV_RESULT
) {
1779 monitor_prefix
= "";
1780 } else if (flags
& EXEC_SETENV_MONITOR_RESULT
) {
1781 env_source
= service_get_triggering_service(s
);
1782 monitor_prefix
= "MONITOR_";
1786 if (asprintf(our_env
+ n_env
++, "%sSERVICE_RESULT=%s", monitor_prefix
, service_result_to_string(env_source
->result
)) < 0)
1789 if (env_source
->main_exec_status
.pid
> 0 &&
1790 dual_timestamp_is_set(&env_source
->main_exec_status
.exit_timestamp
)) {
1791 if (asprintf(our_env
+ n_env
++, "%sEXIT_CODE=%s", monitor_prefix
, sigchld_code_to_string(env_source
->main_exec_status
.code
)) < 0)
1794 if (env_source
->main_exec_status
.code
== CLD_EXITED
)
1795 r
= asprintf(our_env
+ n_env
++, "%sEXIT_STATUS=%i", monitor_prefix
, env_source
->main_exec_status
.status
);
1797 r
= asprintf(our_env
+ n_env
++, "%sEXIT_STATUS=%s", monitor_prefix
, signal_to_string(env_source
->main_exec_status
.status
));
1803 if (env_source
!= s
) {
1804 if (!sd_id128_is_null(UNIT(env_source
)->invocation_id
)) {
1805 r
= asprintf(our_env
+ n_env
++, "%sINVOCATION_ID=" SD_ID128_FORMAT_STR
,
1806 monitor_prefix
, SD_ID128_FORMAT_VAL(UNIT(env_source
)->invocation_id
));
1811 if (asprintf(our_env
+ n_env
++, "%sUNIT=%s", monitor_prefix
, UNIT(env_source
)->id
) < 0)
1816 if (UNIT(s
)->activation_details
) {
1817 r
= activation_details_append_env(UNIT(s
)->activation_details
, &our_env
);
1820 /* The number of env vars added here can vary, rather than keeping the allocation block in
1821 * sync manually, these functions simply use the strv methods to append to it, so we need
1822 * to update n_env when we are done in case of future usage. */
1826 r
= unit_set_exec_params(UNIT(s
), &exec_params
);
1830 final_env
= strv_env_merge(exec_params
.environment
, our_env
);
1834 /* System D-Bus needs nss-systemd disabled, so that we don't deadlock */
1835 SET_FLAG(exec_params
.flags
, EXEC_NSS_DYNAMIC_BYPASS
,
1836 MANAGER_IS_SYSTEM(UNIT(s
)->manager
) && unit_has_name(UNIT(s
), SPECIAL_DBUS_SERVICE
));
1838 strv_free_and_replace(exec_params
.environment
, final_env
);
1839 exec_params
.watchdog_usec
= service_get_watchdog_usec(s
);
1840 exec_params
.selinux_context_net
= s
->socket_fd_selinux_context_net
;
1841 if (s
->type
== SERVICE_IDLE
)
1842 exec_params
.idle_pipe
= UNIT(s
)->manager
->idle_pipe
;
1843 exec_params
.stdin_fd
= s
->stdin_fd
;
1844 exec_params
.stdout_fd
= s
->stdout_fd
;
1845 exec_params
.stderr_fd
= s
->stderr_fd
;
1847 r
= exec_spawn(UNIT(s
),
1857 s
->exec_fd_event_source
= TAKE_PTR(exec_fd_source
);
1858 s
->exec_fd_hot
= false;
1860 r
= pidref_set_pid(&pidref
, pid
);
1864 r
= unit_watch_pid(UNIT(s
), pidref
.pid
, /* exclusive= */ true);
1868 *ret_pid
= TAKE_PIDREF(pidref
);
1872 static int main_pid_good(Service
*s
) {
1875 /* Returns 0 if the pid is dead, > 0 if it is good, < 0 if we don't know */
1877 /* If we know the pid file, then let's just check if it is still valid */
1878 if (s
->main_pid_known
) {
1880 /* If it's an alien child let's check if it is still alive ... */
1881 if (s
->main_pid_alien
&& pidref_is_set(&s
->main_pid
))
1882 return pid_is_alive(s
->main_pid
.pid
);
1884 /* .. otherwise assume we'll get a SIGCHLD for it, which we really should wait for to collect
1885 * exit status and code */
1886 return pidref_is_set(&s
->main_pid
);
1889 /* We don't know the pid */
1893 static int control_pid_good(Service
*s
) {
1896 /* Returns 0 if the control PID is dead, > 0 if it is good. We never actually return < 0 here, but in order to
1897 * make this function as similar as possible to main_pid_good() and cgroup_good(), we pretend that < 0 also
1898 * means: we can't figure it out. */
1900 return pidref_is_set(&s
->control_pid
);
1903 static int cgroup_good(Service
*s
) {
1908 /* Returns 0 if the cgroup is empty or doesn't exist, > 0 if it is exists and is populated, < 0 if we can't
1911 if (!UNIT(s
)->cgroup_path
)
1914 r
= cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, UNIT(s
)->cgroup_path
);
1921 static bool service_shall_restart(Service
*s
, const char **reason
) {
1924 /* Don't restart after manual stops */
1925 if (s
->forbid_restart
) {
1926 *reason
= "manual stop";
1930 /* Never restart if this is configured as special exception */
1931 if (exit_status_set_test(&s
->restart_prevent_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
)) {
1932 *reason
= "prevented by exit status";
1936 /* Restart if the exit code/status are configured as restart triggers */
1937 if (exit_status_set_test(&s
->restart_force_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
)) {
1938 *reason
= "forced by exit status";
1942 *reason
= "restart setting";
1943 switch (s
->restart
) {
1945 case SERVICE_RESTART_NO
:
1948 case SERVICE_RESTART_ALWAYS
:
1949 return s
->result
!= SERVICE_SKIP_CONDITION
;
1951 case SERVICE_RESTART_ON_SUCCESS
:
1952 return s
->result
== SERVICE_SUCCESS
;
1954 case SERVICE_RESTART_ON_FAILURE
:
1955 return !IN_SET(s
->result
, SERVICE_SUCCESS
, SERVICE_SKIP_CONDITION
);
1957 case SERVICE_RESTART_ON_ABNORMAL
:
1958 return !IN_SET(s
->result
, SERVICE_SUCCESS
, SERVICE_FAILURE_EXIT_CODE
, SERVICE_SKIP_CONDITION
);
1960 case SERVICE_RESTART_ON_WATCHDOG
:
1961 return s
->result
== SERVICE_FAILURE_WATCHDOG
;
1963 case SERVICE_RESTART_ON_ABORT
:
1964 return IN_SET(s
->result
, SERVICE_FAILURE_SIGNAL
, SERVICE_FAILURE_CORE_DUMP
);
1967 assert_not_reached();
1971 static bool service_will_restart(Unit
*u
) {
1972 Service
*s
= SERVICE(u
);
1976 if (IN_SET(s
->state
, SERVICE_DEAD_BEFORE_AUTO_RESTART
, SERVICE_FAILED_BEFORE_AUTO_RESTART
, SERVICE_AUTO_RESTART
, SERVICE_AUTO_RESTART_QUEUED
))
1979 return unit_will_restart_default(u
);
1982 static ServiceState
service_determine_dead_state(Service
*s
) {
1985 return s
->fd_store
&& s
->fd_store_preserve_mode
== EXEC_PRESERVE_YES
? SERVICE_DEAD_RESOURCES_PINNED
: SERVICE_DEAD
;
1988 static void service_enter_dead(Service
*s
, ServiceResult f
, bool allow_restart
) {
1989 ServiceState end_state
, restart_state
;
1994 /* If there's a stop job queued before we enter the DEAD state, we shouldn't act on Restart=, in order to not
1995 * undo what has already been enqueued. */
1996 if (unit_stop_pending(UNIT(s
)))
1997 allow_restart
= false;
1999 if (s
->result
== SERVICE_SUCCESS
)
2002 if (s
->result
== SERVICE_SUCCESS
) {
2003 unit_log_success(UNIT(s
));
2004 end_state
= service_determine_dead_state(s
);
2005 restart_state
= SERVICE_DEAD_BEFORE_AUTO_RESTART
;
2006 } else if (s
->result
== SERVICE_SKIP_CONDITION
) {
2007 unit_log_skip(UNIT(s
), service_result_to_string(s
->result
));
2008 end_state
= service_determine_dead_state(s
);
2009 restart_state
= SERVICE_DEAD_BEFORE_AUTO_RESTART
;
2011 unit_log_failure(UNIT(s
), service_result_to_string(s
->result
));
2012 end_state
= SERVICE_FAILED
;
2013 restart_state
= SERVICE_FAILED_BEFORE_AUTO_RESTART
;
2015 unit_warn_leftover_processes(UNIT(s
), unit_log_leftover_process_stop
);
2018 log_unit_debug(UNIT(s
), "Service restart not allowed.");
2022 allow_restart
= service_shall_restart(s
, &reason
);
2023 log_unit_debug(UNIT(s
), "Service will %srestart (%s)",
2024 allow_restart
? "" : "not ",
2028 if (allow_restart
) {
2029 /* We make two state changes here: one that maps to the high-level UNIT_INACTIVE/UNIT_FAILED
2030 * state (i.e. a state indicating deactivation), and then one that that maps to the
2031 * high-level UNIT_STARTING state (i.e. a state indicating activation). We do this so that
2032 * external software can watch the state changes and see all service failures, even if they
2033 * are only transitionary and followed by an automatic restart. We have fine-grained
2034 * low-level states for this though so that software can distinguish the permanent UNIT_INACTIVE
2035 * state from this transitionary UNIT_INACTIVE state by looking at the low-level states. */
2036 if (s
->restart_mode
!= SERVICE_RESTART_MODE_DIRECT
)
2037 service_set_state(s
, restart_state
);
2039 r
= service_arm_timer(s
, /* relative= */ true, service_restart_usec_next(s
));
2043 service_set_state(s
, SERVICE_AUTO_RESTART
);
2045 service_set_state(s
, end_state
);
2047 /* If we shan't restart, then flush out the restart counter. But don't do that immediately, so that the
2048 * user can still introspect the counter. Do so on the next start. */
2049 s
->flush_n_restarts
= true;
2052 /* The new state is in effect, let's decrease the fd store ref counter again. Let's also re-add us to the GC
2053 * queue, so that the fd store is possibly gc'ed again */
2054 unit_add_to_gc_queue(UNIT(s
));
2056 /* The next restart might not be a manual stop, hence reset the flag indicating manual stops */
2057 s
->forbid_restart
= false;
2059 /* Reset NotifyAccess override */
2060 s
->notify_access_override
= _NOTIFY_ACCESS_INVALID
;
2062 /* We want fresh tmpdirs and ephemeral snapshots in case the service is started again immediately. */
2063 s
->exec_runtime
= exec_runtime_destroy(s
->exec_runtime
);
2065 /* Also, remove the runtime directory */
2066 unit_destroy_runtime_data(UNIT(s
), &s
->exec_context
);
2068 /* Also get rid of the fd store, if that's configured. */
2069 if (s
->fd_store_preserve_mode
== EXEC_PRESERVE_NO
)
2070 service_release_fd_store(s
);
2072 /* Get rid of the IPC bits of the user */
2073 unit_unref_uid_gid(UNIT(s
), true);
2075 /* Try to delete the pid file. At this point it will be
2076 * out-of-date, and some software might be confused by it, so
2077 * let's remove it. */
2079 (void) unlink(s
->pid_file
);
2081 /* Reset TTY ownership if necessary */
2082 exec_context_revert_tty(&s
->exec_context
);
2087 log_unit_warning_errno(UNIT(s
), r
, "Failed to run install restart timer: %m");
2088 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
2091 static void service_enter_stop_post(Service
*s
, ServiceResult f
) {
2095 if (s
->result
== SERVICE_SUCCESS
)
2098 service_unwatch_control_pid(s
);
2099 (void) unit_enqueue_rewatch_pids(UNIT(s
));
2101 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
];
2102 if (s
->control_command
) {
2103 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
2104 pidref_done(&s
->control_pid
);
2106 r
= service_spawn(s
,
2108 s
->timeout_stop_usec
,
2109 EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
|EXEC_IS_CONTROL
|EXEC_SETENV_RESULT
|EXEC_CONTROL_CGROUP
,
2114 service_set_state(s
, SERVICE_STOP_POST
);
2116 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_SUCCESS
);
2121 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop-post' task: %m");
2122 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2125 static int state_to_kill_operation(Service
*s
, ServiceState state
) {
2128 case SERVICE_STOP_WATCHDOG
:
2129 case SERVICE_FINAL_WATCHDOG
:
2130 return KILL_WATCHDOG
;
2132 case SERVICE_STOP_SIGTERM
:
2133 if (unit_has_job_type(UNIT(s
), JOB_RESTART
))
2134 return KILL_RESTART
;
2137 case SERVICE_FINAL_SIGTERM
:
2138 return KILL_TERMINATE
;
2140 case SERVICE_STOP_SIGKILL
:
2141 case SERVICE_FINAL_SIGKILL
:
2145 return _KILL_OPERATION_INVALID
;
2149 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
) {
2150 int kill_operation
, r
;
2154 if (s
->result
== SERVICE_SUCCESS
)
2157 /* Before sending any signal, make sure we track all members of this cgroup */
2158 (void) unit_watch_all_pids(UNIT(s
));
2160 /* Also, enqueue a job that we recheck all our PIDs a bit later, given that it's likely some processes have
2162 (void) unit_enqueue_rewatch_pids(UNIT(s
));
2164 kill_operation
= state_to_kill_operation(s
, state
);
2165 r
= unit_kill_context(
2176 r
= service_arm_timer(s
, /* relative= */ true,
2177 kill_operation
== KILL_WATCHDOG
? service_timeout_abort_usec(s
) : s
->timeout_stop_usec
);
2181 service_set_state(s
, state
);
2182 } else if (IN_SET(state
, SERVICE_STOP_WATCHDOG
, SERVICE_STOP_SIGTERM
) && s
->kill_context
.send_sigkill
)
2183 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_SUCCESS
);
2184 else if (IN_SET(state
, SERVICE_STOP_WATCHDOG
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
2185 service_enter_stop_post(s
, SERVICE_SUCCESS
);
2186 else if (IN_SET(state
, SERVICE_FINAL_WATCHDOG
, SERVICE_FINAL_SIGTERM
) && s
->kill_context
.send_sigkill
)
2187 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_SUCCESS
);
2189 service_enter_dead(s
, SERVICE_SUCCESS
, true);
2194 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
2196 if (IN_SET(state
, SERVICE_STOP_WATCHDOG
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
2197 service_enter_stop_post(s
, SERVICE_FAILURE_RESOURCES
);
2199 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
2202 static void service_enter_stop_by_notify(Service
*s
) {
2205 (void) unit_enqueue_rewatch_pids(UNIT(s
));
2207 service_arm_timer(s
, /* relative= */ true, s
->timeout_stop_usec
);
2209 /* The service told us it's stopping, so it's as if we SIGTERM'd it. */
2210 service_set_state(s
, SERVICE_STOP_SIGTERM
);
2213 static void service_enter_stop(Service
*s
, ServiceResult f
) {
2218 if (s
->result
== SERVICE_SUCCESS
)
2221 service_unwatch_control_pid(s
);
2222 (void) unit_enqueue_rewatch_pids(UNIT(s
));
2224 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
];
2225 if (s
->control_command
) {
2226 s
->control_command_id
= SERVICE_EXEC_STOP
;
2227 pidref_done(&s
->control_pid
);
2229 r
= service_spawn(s
,
2231 s
->timeout_stop_usec
,
2232 EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_IS_CONTROL
|EXEC_SETENV_RESULT
|EXEC_CONTROL_CGROUP
,
2237 service_set_state(s
, SERVICE_STOP
);
2239 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
2244 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop' task: %m");
2245 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2248 static bool service_good(Service
*s
) {
2252 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name_good
)
2255 main_pid_ok
= main_pid_good(s
);
2256 if (main_pid_ok
> 0) /* It's alive */
2258 if (main_pid_ok
== 0 && s
->exit_type
== SERVICE_EXIT_MAIN
) /* It's dead */
2261 /* OK, we don't know anything about the main PID, maybe
2262 * because there is none. Let's check the control group
2265 return cgroup_good(s
) != 0;
2268 static void service_enter_running(Service
*s
, ServiceResult f
) {
2271 if (s
->result
== SERVICE_SUCCESS
)
2274 service_unwatch_control_pid(s
);
2276 if (s
->result
!= SERVICE_SUCCESS
)
2277 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
2278 else if (service_good(s
)) {
2280 /* If there are any queued up sd_notify() notifications, process them now */
2281 if (s
->notify_state
== NOTIFY_RELOADING
)
2282 service_enter_reload_by_notify(s
);
2283 else if (s
->notify_state
== NOTIFY_STOPPING
)
2284 service_enter_stop_by_notify(s
);
2286 service_set_state(s
, SERVICE_RUNNING
);
2287 service_arm_timer(s
, /* relative= */ false, service_running_timeout(s
));
2290 } else if (s
->remain_after_exit
)
2291 service_set_state(s
, SERVICE_EXITED
);
2293 service_enter_stop(s
, SERVICE_SUCCESS
);
2296 static void service_enter_start_post(Service
*s
) {
2300 service_unwatch_control_pid(s
);
2301 service_reset_watchdog(s
);
2303 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
];
2304 if (s
->control_command
) {
2305 s
->control_command_id
= SERVICE_EXEC_START_POST
;
2306 pidref_done(&s
->control_pid
);
2308 r
= service_spawn(s
,
2310 s
->timeout_start_usec
,
2311 EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_IS_CONTROL
|EXEC_CONTROL_CGROUP
,
2316 service_set_state(s
, SERVICE_START_POST
);
2318 service_enter_running(s
, SERVICE_SUCCESS
);
2323 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-post' task: %m");
2324 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2327 static void service_kill_control_process(Service
*s
) {
2332 if (!pidref_is_set(&s
->control_pid
))
2335 r
= pidref_kill_and_sigcont(&s
->control_pid
, SIGKILL
);
2337 _cleanup_free_
char *comm
= NULL
;
2339 (void) get_process_comm(s
->control_pid
.pid
, &comm
);
2341 log_unit_debug_errno(UNIT(s
), r
, "Failed to kill control process " PID_FMT
" (%s), ignoring: %m",
2342 s
->control_pid
.pid
, strna(comm
));
2346 static int service_adverse_to_leftover_processes(Service
*s
) {
2349 /* KillMode=mixed and control group are used to indicate that all process should be killed off.
2350 * SendSIGKILL= is used for services that require a clean shutdown. These are typically database
2351 * service where a SigKilled process would result in a lengthy recovery and who's shutdown or startup
2352 * time is quite variable (so Timeout settings aren't of use).
2354 * Here we take these two factors and refuse to start a service if there are existing processes
2355 * within a control group. Databases, while generally having some protection against multiple
2356 * instances running, lets not stress the rigor of these. Also ExecStartPre= parts of the service
2357 * aren't as rigoriously written to protect aganst against multiple use. */
2359 if (unit_warn_leftover_processes(UNIT(s
), unit_log_leftover_process_start
) > 0 &&
2360 IN_SET(s
->kill_context
.kill_mode
, KILL_MIXED
, KILL_CONTROL_GROUP
) &&
2361 !s
->kill_context
.send_sigkill
)
2362 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(EBUSY
),
2363 "Will not start SendSIGKILL=no service of type KillMode=control-group or mixed while processes exist");
2368 static void service_enter_start(Service
*s
) {
2369 _cleanup_(pidref_done
) PidRef pidref
= PIDREF_NULL
;
2376 service_unwatch_control_pid(s
);
2377 service_unwatch_main_pid(s
);
2379 r
= service_adverse_to_leftover_processes(s
);
2383 if (s
->type
== SERVICE_FORKING
) {
2384 s
->control_command_id
= SERVICE_EXEC_START
;
2385 c
= s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
2387 s
->main_command
= NULL
;
2389 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
2390 s
->control_command
= NULL
;
2392 c
= s
->main_command
= s
->exec_command
[SERVICE_EXEC_START
];
2396 if (s
->type
!= SERVICE_ONESHOT
) {
2397 /* There's no command line configured for the main command? Hmm, that is strange.
2398 * This can only happen if the configuration changes at runtime. In this case,
2399 * let's enter a failure state. */
2400 r
= log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENXIO
), "There's no 'start' task anymore we could start.");
2404 /* We force a fake state transition here. Otherwise, the unit would go directly from
2405 * SERVICE_DEAD to SERVICE_DEAD without SERVICE_ACTIVATING or SERVICE_ACTIVE
2406 * in between. This way we can later trigger actions that depend on the state
2407 * transition, including SuccessAction=. */
2408 service_set_state(s
, SERVICE_START
);
2410 service_enter_start_post(s
);
2414 if (IN_SET(s
->type
, SERVICE_SIMPLE
, SERVICE_IDLE
))
2415 /* For simple + idle this is the main process. We don't apply any timeout here, but
2416 * service_enter_running() will later apply the .runtime_max_usec timeout. */
2417 timeout
= USEC_INFINITY
;
2419 timeout
= s
->timeout_start_usec
;
2421 r
= service_spawn(s
,
2424 EXEC_PASS_FDS
|EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
|EXEC_SET_WATCHDOG
|EXEC_WRITE_CREDENTIALS
|EXEC_SETENV_MONITOR_RESULT
,
2429 if (IN_SET(s
->type
, SERVICE_SIMPLE
, SERVICE_IDLE
)) {
2430 /* For simple services we immediately start
2431 * the START_POST binaries. */
2433 (void) service_set_main_pidref(s
, &pidref
);
2434 service_enter_start_post(s
);
2436 } else if (s
->type
== SERVICE_FORKING
) {
2438 /* For forking services we wait until the start
2439 * process exited. */
2441 pidref_done(&s
->control_pid
);
2442 s
->control_pid
= TAKE_PIDREF(pidref
);
2443 service_set_state(s
, SERVICE_START
);
2445 } else if (IN_SET(s
->type
, SERVICE_ONESHOT
, SERVICE_DBUS
, SERVICE_NOTIFY
, SERVICE_NOTIFY_RELOAD
, SERVICE_EXEC
)) {
2447 /* For oneshot services we wait until the start process exited, too, but it is our main process. */
2449 /* For D-Bus services we know the main pid right away, but wait for the bus name to appear on the
2450 * bus. 'notify' and 'exec' services are similar. */
2452 (void) service_set_main_pidref(s
, &pidref
);
2453 service_set_state(s
, SERVICE_START
);
2455 assert_not_reached();
2460 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start' task: %m");
2461 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2464 static void service_enter_start_pre(Service
*s
) {
2469 service_unwatch_control_pid(s
);
2471 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
];
2472 if (s
->control_command
) {
2474 r
= service_adverse_to_leftover_processes(s
);
2478 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
2480 r
= service_spawn(s
,
2482 s
->timeout_start_usec
,
2483 EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_IS_CONTROL
|EXEC_APPLY_TTY_STDIN
|EXEC_SETENV_MONITOR_RESULT
|EXEC_WRITE_CREDENTIALS
,
2488 service_set_state(s
, SERVICE_START_PRE
);
2490 service_enter_start(s
);
2495 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-pre' task: %m");
2496 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
2499 static void service_enter_condition(Service
*s
) {
2504 service_unwatch_control_pid(s
);
2506 s
->control_command
= s
->exec_command
[SERVICE_EXEC_CONDITION
];
2507 if (s
->control_command
) {
2509 r
= service_adverse_to_leftover_processes(s
);
2513 s
->control_command_id
= SERVICE_EXEC_CONDITION
;
2514 pidref_done(&s
->control_pid
);
2516 r
= service_spawn(s
,
2518 s
->timeout_start_usec
,
2519 EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_IS_CONTROL
|EXEC_APPLY_TTY_STDIN
,
2525 service_set_state(s
, SERVICE_CONDITION
);
2527 service_enter_start_pre(s
);
2532 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'exec-condition' task: %m");
2533 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
2536 static void service_enter_restart(Service
*s
) {
2537 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2542 if (unit_has_job_type(UNIT(s
), JOB_STOP
)) {
2543 /* Don't restart things if we are going down anyway */
2544 log_unit_info(UNIT(s
), "Stop job pending for unit, skipping automatic restart.");
2548 /* Any units that are bound to this service must also be restarted. We use JOB_START for ourselves
2549 * but then set JOB_RESTART_DEPENDENCIES which will enqueue JOB_RESTART for those dependency jobs. */
2550 r
= manager_add_job(UNIT(s
)->manager
, JOB_START
, UNIT(s
), JOB_RESTART_DEPENDENCIES
, NULL
, &error
, NULL
);
2554 /* Count the jobs we enqueue for restarting. This counter is maintained as long as the unit isn't
2555 * fully stopped, i.e. as long as it remains up or remains in auto-start states. The user can reset
2556 * the counter explicitly however via the usual "systemctl reset-failure" logic. */
2558 s
->flush_n_restarts
= false;
2560 s
->notify_access_override
= _NOTIFY_ACCESS_INVALID
;
2562 log_unit_struct(UNIT(s
), LOG_INFO
,
2563 "MESSAGE_ID=" SD_MESSAGE_UNIT_RESTART_SCHEDULED_STR
,
2564 LOG_UNIT_INVOCATION_ID(UNIT(s
)),
2565 LOG_UNIT_MESSAGE(UNIT(s
),
2566 "Scheduled restart job, restart counter is at %u.", s
->n_restarts
),
2567 "N_RESTARTS=%u", s
->n_restarts
);
2569 service_set_state(s
, SERVICE_AUTO_RESTART_QUEUED
);
2571 /* Notify clients about changed restart counter */
2572 unit_add_to_dbus_queue(UNIT(s
));
2576 log_unit_warning(UNIT(s
), "Failed to schedule restart job: %s", bus_error_message(&error
, r
));
2577 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
2580 static void service_enter_reload_by_notify(Service
*s
) {
2581 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2586 service_arm_timer(s
, /* relative= */ true, s
->timeout_start_usec
);
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: %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 s
->timeout_start_usec
,
2624 EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_IS_CONTROL
|EXEC_CONTROL_CGROUP
,
2627 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'reload' task: %m");
2631 service_set_state(s
, SERVICE_RELOAD
);
2632 } else if (killed
) {
2633 service_arm_timer(s
, /* relative= */ true, s
->timeout_start_usec
);
2634 service_set_state(s
, SERVICE_RELOAD_SIGNAL
);
2636 service_enter_running(s
, SERVICE_SUCCESS
);
2640 /* Store the timestamp when we started reloading: when reloading via SIGHUP we won't leave the reload
2641 * state until we received both RELOADING=1 and READY=1 with MONOTONIC_USEC= set to a value above
2642 * this. Thus we know for sure the reload cycle was executed *after* we requested it, and is not one
2643 * that was already in progress before. */
2644 s
->reload_begin_usec
= ts
;
2648 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
2649 service_enter_running(s
, SERVICE_SUCCESS
);
2652 static void service_run_next_control(Service
*s
) {
2657 assert(s
->control_command
);
2658 assert(s
->control_command
->command_next
);
2660 assert(s
->control_command_id
!= SERVICE_EXEC_START
);
2662 s
->control_command
= s
->control_command
->command_next
;
2663 service_unwatch_control_pid(s
);
2665 if (IN_SET(s
->state
, SERVICE_CONDITION
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
2666 timeout
= s
->timeout_start_usec
;
2668 timeout
= s
->timeout_stop_usec
;
2670 pidref_done(&s
->control_pid
);
2672 r
= service_spawn(s
,
2675 EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_IS_CONTROL
|
2676 (IN_SET(s
->state
, SERVICE_CONDITION
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
) ? EXEC_WRITE_CREDENTIALS
: 0)|
2677 (IN_SET(s
->control_command_id
, SERVICE_EXEC_CONDITION
, SERVICE_EXEC_START_PRE
, SERVICE_EXEC_STOP_POST
) ? EXEC_APPLY_TTY_STDIN
: 0)|
2678 (IN_SET(s
->control_command_id
, SERVICE_EXEC_STOP
, SERVICE_EXEC_STOP_POST
) ? EXEC_SETENV_RESULT
: 0)|
2679 (IN_SET(s
->control_command_id
, SERVICE_EXEC_START_PRE
, SERVICE_EXEC_START
) ? EXEC_SETENV_MONITOR_RESULT
: 0)|
2680 (IN_SET(s
->control_command_id
, SERVICE_EXEC_START_POST
, SERVICE_EXEC_RELOAD
, SERVICE_EXEC_STOP
, SERVICE_EXEC_STOP_POST
) ? EXEC_CONTROL_CGROUP
: 0),
2688 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next control task: %m");
2690 if (IN_SET(s
->state
, SERVICE_CONDITION
, SERVICE_START_PRE
, SERVICE_START_POST
, SERVICE_STOP
))
2691 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2692 else if (s
->state
== SERVICE_STOP_POST
)
2693 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
2694 else if (s
->state
== SERVICE_RELOAD
) {
2695 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
2696 service_enter_running(s
, SERVICE_SUCCESS
);
2698 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2701 static void service_run_next_main(Service
*s
) {
2702 _cleanup_(pidref_done
) PidRef pidref
= PIDREF_NULL
;
2706 assert(s
->main_command
);
2707 assert(s
->main_command
->command_next
);
2708 assert(s
->type
== SERVICE_ONESHOT
);
2710 s
->main_command
= s
->main_command
->command_next
;
2711 service_unwatch_main_pid(s
);
2713 r
= service_spawn(s
,
2715 s
->timeout_start_usec
,
2716 EXEC_PASS_FDS
|EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
|EXEC_SET_WATCHDOG
|EXEC_SETENV_MONITOR_RESULT
|EXEC_WRITE_CREDENTIALS
,
2721 (void) service_set_main_pidref(s
, &pidref
);
2725 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next main task: %m");
2726 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2729 static int service_start(Unit
*u
) {
2730 Service
*s
= SERVICE(u
);
2735 /* We cannot fulfill this request right now, try again later
2737 if (IN_SET(s
->state
,
2738 SERVICE_STOP
, SERVICE_STOP_WATCHDOG
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
2739 SERVICE_FINAL_WATCHDOG
, SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
, SERVICE_CLEANING
))
2742 /* Already on it! */
2743 if (IN_SET(s
->state
, SERVICE_CONDITION
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
))
2746 /* A service that will be restarted must be stopped first to trigger BindsTo and/or OnFailure
2747 * dependencies. If a user does not want to wait for the holdoff time to elapse, the service should
2748 * be manually restarted, not started. We simply return EAGAIN here, so that any start jobs stay
2749 * queued, and assume that the auto restart timer will eventually trigger the restart. */
2750 if (IN_SET(s
->state
, SERVICE_AUTO_RESTART
, SERVICE_DEAD_BEFORE_AUTO_RESTART
, SERVICE_FAILED_BEFORE_AUTO_RESTART
))
2753 assert(IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_DEAD_RESOURCES_PINNED
, SERVICE_AUTO_RESTART_QUEUED
));
2755 r
= unit_acquire_invocation_id(u
);
2759 s
->result
= SERVICE_SUCCESS
;
2760 s
->reload_result
= SERVICE_SUCCESS
;
2761 s
->main_pid_known
= false;
2762 s
->main_pid_alien
= false;
2763 s
->forbid_restart
= false;
2765 s
->status_text
= mfree(s
->status_text
);
2766 s
->status_errno
= 0;
2768 s
->notify_access_override
= _NOTIFY_ACCESS_INVALID
;
2769 s
->notify_state
= NOTIFY_UNKNOWN
;
2771 s
->watchdog_original_usec
= s
->watchdog_usec
;
2772 s
->watchdog_override_enable
= false;
2773 s
->watchdog_override_usec
= USEC_INFINITY
;
2775 exec_command_reset_status_list_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
2776 exec_status_reset(&s
->main_exec_status
);
2778 /* This is not an automatic restart? Flush the restart counter then */
2779 if (s
->flush_n_restarts
) {
2781 s
->flush_n_restarts
= false;
2784 u
->reset_accounting
= true;
2786 service_enter_condition(s
);
2790 static int service_stop(Unit
*u
) {
2791 Service
*s
= SERVICE(u
);
2795 /* Don't create restart jobs from manual stops. */
2796 s
->forbid_restart
= true;
2801 case SERVICE_STOP_SIGTERM
:
2802 case SERVICE_STOP_SIGKILL
:
2803 case SERVICE_STOP_POST
:
2804 case SERVICE_FINAL_WATCHDOG
:
2805 case SERVICE_FINAL_SIGTERM
:
2806 case SERVICE_FINAL_SIGKILL
:
2810 case SERVICE_AUTO_RESTART
:
2811 case SERVICE_AUTO_RESTART_QUEUED
:
2812 /* Give up on the auto restart */
2813 service_set_state(s
, service_determine_dead_state(s
));
2816 case SERVICE_CONDITION
:
2817 case SERVICE_START_PRE
:
2819 case SERVICE_START_POST
:
2820 case SERVICE_RELOAD
:
2821 case SERVICE_RELOAD_SIGNAL
:
2822 case SERVICE_RELOAD_NOTIFY
:
2823 case SERVICE_STOP_WATCHDOG
:
2824 /* If there's already something running we go directly into kill mode. */
2825 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
2828 case SERVICE_CLEANING
:
2829 /* If we are currently cleaning, then abort it, brutally. */
2830 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_SUCCESS
);
2833 case SERVICE_RUNNING
:
2834 case SERVICE_EXITED
:
2835 service_enter_stop(s
, SERVICE_SUCCESS
);
2838 case SERVICE_DEAD_BEFORE_AUTO_RESTART
:
2839 case SERVICE_FAILED_BEFORE_AUTO_RESTART
:
2841 case SERVICE_FAILED
:
2842 case SERVICE_DEAD_RESOURCES_PINNED
:
2844 /* Unknown state, or unit_stop() should already have handled these */
2845 assert_not_reached();
2849 static int service_reload(Unit
*u
) {
2850 Service
*s
= SERVICE(u
);
2854 assert(IN_SET(s
->state
, SERVICE_RUNNING
, SERVICE_EXITED
));
2856 service_enter_reload(s
);
2860 static bool service_can_reload(Unit
*u
) {
2861 Service
*s
= SERVICE(u
);
2865 return s
->exec_command
[SERVICE_EXEC_RELOAD
] ||
2866 s
->type
== SERVICE_NOTIFY_RELOAD
;
2869 static unsigned service_exec_command_index(Unit
*u
, ServiceExecCommand id
, const ExecCommand
*current
) {
2870 Service
*s
= SERVICE(u
);
2875 assert(id
< _SERVICE_EXEC_COMMAND_MAX
);
2877 const ExecCommand
*first
= s
->exec_command
[id
];
2879 /* Figure out where we are in the list by walking back to the beginning */
2880 for (const ExecCommand
*c
= current
; c
!= first
; c
= c
->command_prev
)
2886 static int service_serialize_exec_command(Unit
*u
, FILE *f
, const ExecCommand
*command
) {
2887 _cleanup_free_
char *args
= NULL
, *p
= NULL
;
2888 Service
*s
= SERVICE(u
);
2889 const char *type
, *key
;
2890 ServiceExecCommand id
;
2900 if (command
== s
->control_command
) {
2902 id
= s
->control_command_id
;
2905 id
= SERVICE_EXEC_START
;
2908 idx
= service_exec_command_index(u
, id
, command
);
2910 STRV_FOREACH(arg
, command
->argv
) {
2911 _cleanup_free_
char *e
= NULL
;
2919 if (!GREEDY_REALLOC(args
, length
+ 2 + n
+ 2))
2923 args
[length
++] = ' ';
2925 args
[length
++] = '"';
2926 memcpy(args
+ length
, e
, n
);
2928 args
[length
++] = '"';
2931 if (!GREEDY_REALLOC(args
, length
+ 1))
2936 p
= cescape(command
->path
);
2940 key
= strjoina(type
, "-command");
2942 /* We use '+1234' instead of '1234' to mark the last command in a sequence.
2943 * This is used in service_deserialize_exec_command(). */
2944 (void) serialize_item_format(
2947 service_exec_command_to_string(id
),
2948 command
->command_next
? "" : "+",
2955 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
2956 Service
*s
= SERVICE(u
);
2963 (void) serialize_item(f
, "state", service_state_to_string(s
->state
));
2964 (void) serialize_item(f
, "result", service_result_to_string(s
->result
));
2965 (void) serialize_item(f
, "reload-result", service_result_to_string(s
->reload_result
));
2967 if (pidref_is_set(&s
->control_pid
))
2968 (void) serialize_item_format(f
, "control-pid", PID_FMT
, s
->control_pid
.pid
);
2970 if (s
->main_pid_known
&& pidref_is_set(&s
->main_pid
))
2971 (void) serialize_item_format(f
, "main-pid", PID_FMT
, s
->main_pid
.pid
);
2973 (void) serialize_bool(f
, "main-pid-known", s
->main_pid_known
);
2974 (void) serialize_bool(f
, "bus-name-good", s
->bus_name_good
);
2975 (void) serialize_bool(f
, "bus-name-owner", s
->bus_name_owner
);
2977 (void) serialize_item_format(f
, "n-restarts", "%u", s
->n_restarts
);
2978 (void) serialize_bool(f
, "flush-n-restarts", s
->flush_n_restarts
);
2980 r
= serialize_item_escaped(f
, "status-text", s
->status_text
);
2984 service_serialize_exec_command(u
, f
, s
->control_command
);
2985 service_serialize_exec_command(u
, f
, s
->main_command
);
2987 r
= serialize_fd(f
, fds
, "stdin-fd", s
->stdin_fd
);
2990 r
= serialize_fd(f
, fds
, "stdout-fd", s
->stdout_fd
);
2993 r
= serialize_fd(f
, fds
, "stderr-fd", s
->stderr_fd
);
2997 if (s
->exec_fd_event_source
) {
2998 r
= serialize_fd(f
, fds
, "exec-fd", sd_event_source_get_io_fd(s
->exec_fd_event_source
));
3002 (void) serialize_bool(f
, "exec-fd-hot", s
->exec_fd_hot
);
3005 if (UNIT_ISSET(s
->accept_socket
)) {
3006 r
= serialize_item(f
, "accept-socket", UNIT_DEREF(s
->accept_socket
)->id
);
3011 r
= serialize_fd(f
, fds
, "socket-fd", s
->socket_fd
);
3015 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
3016 _cleanup_free_
char *c
= NULL
;
3019 copy
= fdset_put_dup(fds
, fs
->fd
);
3021 return log_error_errno(copy
, "Failed to copy file descriptor for serialization: %m");
3023 c
= cescape(fs
->fdname
);
3027 (void) serialize_item_format(f
, "fd-store-fd", "%i \"%s\" %i", copy
, c
, fs
->do_poll
);
3030 if (s
->main_exec_status
.pid
> 0) {
3031 (void) serialize_item_format(f
, "main-exec-status-pid", PID_FMT
, s
->main_exec_status
.pid
);
3032 (void) serialize_dual_timestamp(f
, "main-exec-status-start", &s
->main_exec_status
.start_timestamp
);
3033 (void) serialize_dual_timestamp(f
, "main-exec-status-exit", &s
->main_exec_status
.exit_timestamp
);
3035 if (dual_timestamp_is_set(&s
->main_exec_status
.exit_timestamp
)) {
3036 (void) serialize_item_format(f
, "main-exec-status-code", "%i", s
->main_exec_status
.code
);
3037 (void) serialize_item_format(f
, "main-exec-status-status", "%i", s
->main_exec_status
.status
);
3041 if (s
->notify_access_override
>= 0)
3042 (void) serialize_item(f
, "notify-access-override", notify_access_to_string(s
->notify_access_override
));
3044 (void) serialize_dual_timestamp(f
, "watchdog-timestamp", &s
->watchdog_timestamp
);
3045 (void) serialize_bool(f
, "forbid-restart", s
->forbid_restart
);
3047 if (s
->watchdog_override_enable
)
3048 (void) serialize_item_format(f
, "watchdog-override-usec", USEC_FMT
, s
->watchdog_override_usec
);
3050 if (s
->watchdog_original_usec
!= USEC_INFINITY
)
3051 (void) serialize_item_format(f
, "watchdog-original-usec", USEC_FMT
, s
->watchdog_original_usec
);
3053 if (s
->reload_begin_usec
!= USEC_INFINITY
)
3054 (void) serialize_item_format(f
, "reload-begin-usec", USEC_FMT
, s
->reload_begin_usec
);
3059 int service_deserialize_exec_command(
3062 const char *value
) {
3064 Service
*s
= SERVICE(u
);
3066 unsigned idx
= 0, i
;
3067 bool control
, found
= false, last
= false;
3068 ServiceExecCommand id
= _SERVICE_EXEC_COMMAND_INVALID
;
3069 ExecCommand
*command
= NULL
;
3070 _cleanup_free_
char *path
= NULL
;
3071 _cleanup_strv_free_
char **argv
= NULL
;
3073 enum ExecCommandState
{
3074 STATE_EXEC_COMMAND_TYPE
,
3075 STATE_EXEC_COMMAND_INDEX
,
3076 STATE_EXEC_COMMAND_PATH
,
3077 STATE_EXEC_COMMAND_ARGS
,
3078 _STATE_EXEC_COMMAND_MAX
,
3079 _STATE_EXEC_COMMAND_INVALID
= -EINVAL
,
3086 control
= streq(key
, "control-command");
3088 state
= STATE_EXEC_COMMAND_TYPE
;
3091 _cleanup_free_
char *arg
= NULL
;
3093 r
= extract_first_word(&value
, &arg
, NULL
, EXTRACT_CUNESCAPE
| EXTRACT_UNQUOTE
);
3100 case STATE_EXEC_COMMAND_TYPE
:
3101 id
= service_exec_command_from_string(arg
);
3105 state
= STATE_EXEC_COMMAND_INDEX
;
3107 case STATE_EXEC_COMMAND_INDEX
:
3108 /* PID 1234 is serialized as either '1234' or '+1234'. The second form is used to
3109 * mark the last command in a sequence. We warn if the deserialized command doesn't
3110 * match what we have loaded from the unit, but we don't need to warn if that is the
3113 r
= safe_atou(arg
, &idx
);
3116 last
= arg
[0] == '+';
3118 state
= STATE_EXEC_COMMAND_PATH
;
3120 case STATE_EXEC_COMMAND_PATH
:
3121 path
= TAKE_PTR(arg
);
3122 state
= STATE_EXEC_COMMAND_ARGS
;
3124 case STATE_EXEC_COMMAND_ARGS
:
3125 r
= strv_extend(&argv
, arg
);
3130 assert_not_reached();
3134 if (state
!= STATE_EXEC_COMMAND_ARGS
)
3136 if (strv_isempty(argv
))
3137 return -EINVAL
; /* At least argv[0] must be always present. */
3139 /* Let's check whether exec command on given offset matches data that we just deserialized */
3140 for (command
= s
->exec_command
[id
], i
= 0; command
; command
= command
->command_next
, i
++) {
3144 found
= strv_equal(argv
, command
->argv
) && streq(command
->path
, path
);
3149 /* Command at the index we serialized is different, let's look for command that exactly
3150 * matches but is on different index. If there is no such command we will not resume execution. */
3151 for (command
= s
->exec_command
[id
]; command
; command
= command
->command_next
)
3152 if (strv_equal(command
->argv
, argv
) && streq(command
->path
, path
))
3156 if (command
&& control
) {
3157 s
->control_command
= command
;
3158 s
->control_command_id
= id
;
3160 s
->main_command
= command
;
3162 log_unit_debug(u
, "Current command vanished from the unit file.");
3164 log_unit_warning(u
, "Current command vanished from the unit file, execution of the command list won't be resumed.");
3169 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
3170 Service
*s
= SERVICE(u
);
3178 if (streq(key
, "state")) {
3181 state
= service_state_from_string(value
);
3183 log_unit_debug(u
, "Failed to parse state value: %s", value
);
3185 s
->deserialized_state
= state
;
3186 } else if (streq(key
, "result")) {
3189 f
= service_result_from_string(value
);
3191 log_unit_debug(u
, "Failed to parse result value: %s", value
);
3192 else if (f
!= SERVICE_SUCCESS
)
3195 } else if (streq(key
, "reload-result")) {
3198 f
= service_result_from_string(value
);
3200 log_unit_debug(u
, "Failed to parse reload result value: %s", value
);
3201 else if (f
!= SERVICE_SUCCESS
)
3202 s
->reload_result
= f
;
3204 } else if (streq(key
, "control-pid")) {
3205 pidref_done(&s
->control_pid
);
3206 r
= pidref_set_pidstr(&s
->control_pid
, value
);
3208 log_unit_debug_errno(u
, r
, "Failed to initialize control PID '%s' from serialization, ignoring.", value
);
3209 } else if (streq(key
, "main-pid")) {
3212 if (parse_pid(value
, &pid
) < 0)
3213 log_unit_debug(u
, "Failed to parse main-pid value: %s", value
);
3215 (void) service_set_main_pid(s
, pid
);
3216 } else if (streq(key
, "main-pid-known")) {
3219 b
= parse_boolean(value
);
3221 log_unit_debug(u
, "Failed to parse main-pid-known value: %s", value
);
3223 s
->main_pid_known
= b
;
3224 } else if (streq(key
, "bus-name-good")) {
3227 b
= parse_boolean(value
);
3229 log_unit_debug(u
, "Failed to parse bus-name-good value: %s", value
);
3231 s
->bus_name_good
= b
;
3232 } else if (streq(key
, "bus-name-owner")) {
3233 r
= free_and_strdup(&s
->bus_name_owner
, value
);
3235 log_unit_error_errno(u
, r
, "Unable to deserialize current bus owner %s: %m", value
);
3236 } else if (streq(key
, "status-text")) {
3240 l
= cunescape(value
, 0, &t
);
3242 log_unit_debug_errno(u
, l
, "Failed to unescape status text '%s': %m", value
);
3244 free_and_replace(s
->status_text
, t
);
3246 } else if (streq(key
, "accept-socket")) {
3249 if (u
->type
!= UNIT_SOCKET
) {
3250 log_unit_debug(u
, "Failed to deserialize accept-socket: unit is not a socket");
3254 r
= manager_load_unit(u
->manager
, value
, NULL
, NULL
, &socket
);
3256 log_unit_debug_errno(u
, r
, "Failed to load accept-socket unit '%s': %m", value
);
3258 unit_ref_set(&s
->accept_socket
, u
, socket
);
3259 SOCKET(socket
)->n_connections
++;
3262 } else if (streq(key
, "socket-fd")) {
3265 if ((fd
= parse_fd(value
)) < 0 || !fdset_contains(fds
, fd
))
3266 log_unit_debug(u
, "Failed to parse socket-fd value: %s", value
);
3268 asynchronous_close(s
->socket_fd
);
3269 s
->socket_fd
= fdset_remove(fds
, fd
);
3271 } else if (streq(key
, "fd-store-fd")) {
3272 _cleanup_free_
char *fdv
= NULL
, *fdn
= NULL
, *fdp
= NULL
;
3275 r
= extract_first_word(&value
, &fdv
, NULL
, 0);
3276 if (r
<= 0 || (fd
= parse_fd(fdv
)) < 0 || !fdset_contains(fds
, fd
)) {
3277 log_unit_debug(u
, "Failed to parse fd-store-fd value: %s", value
);
3281 r
= extract_first_word(&value
, &fdn
, NULL
, EXTRACT_CUNESCAPE
| EXTRACT_UNQUOTE
);
3283 log_unit_debug(u
, "Failed to parse fd-store-fd value: %s", value
);
3287 r
= extract_first_word(&value
, &fdp
, NULL
, 0);
3289 /* If the value is not present, we assume the default */
3291 } else if (r
< 0 || safe_atoi(fdp
, &do_poll
) < 0) {
3292 log_unit_debug_errno(u
, r
, "Failed to parse fd-store-fd value \"%s\": %m", value
);
3296 r
= fdset_remove(fds
, fd
);
3298 log_unit_error_errno(u
, r
, "Could not find deserialized fd %i in fdset: %m", fd
);
3303 r
= service_add_fd_store(s
, fd
, fdn
, do_poll
);
3305 log_unit_error_errno(u
, r
, "Failed to store deserialized fd %i: %m", fd
);
3308 } else if (streq(key
, "main-exec-status-pid")) {
3311 if (parse_pid(value
, &pid
) < 0)
3312 log_unit_debug(u
, "Failed to parse main-exec-status-pid value: %s", value
);
3314 s
->main_exec_status
.pid
= pid
;
3315 } else if (streq(key
, "main-exec-status-code")) {
3318 if (safe_atoi(value
, &i
) < 0)
3319 log_unit_debug(u
, "Failed to parse main-exec-status-code value: %s", value
);
3321 s
->main_exec_status
.code
= i
;
3322 } else if (streq(key
, "main-exec-status-status")) {
3325 if (safe_atoi(value
, &i
) < 0)
3326 log_unit_debug(u
, "Failed to parse main-exec-status-status value: %s", value
);
3328 s
->main_exec_status
.status
= i
;
3329 } else if (streq(key
, "main-exec-status-start"))
3330 deserialize_dual_timestamp(value
, &s
->main_exec_status
.start_timestamp
);
3331 else if (streq(key
, "main-exec-status-exit"))
3332 deserialize_dual_timestamp(value
, &s
->main_exec_status
.exit_timestamp
);
3333 else if (streq(key
, "notify-access-override")) {
3334 NotifyAccess notify_access
;
3336 notify_access
= notify_access_from_string(value
);
3337 if (notify_access
< 0)
3338 log_unit_debug(u
, "Failed to parse notify-access-override value: %s", value
);
3340 s
->notify_access_override
= notify_access
;
3341 } else if (streq(key
, "watchdog-timestamp"))
3342 deserialize_dual_timestamp(value
, &s
->watchdog_timestamp
);
3343 else if (streq(key
, "forbid-restart")) {
3346 b
= parse_boolean(value
);
3348 log_unit_debug(u
, "Failed to parse forbid-restart value: %s", value
);
3350 s
->forbid_restart
= b
;
3351 } else if (streq(key
, "stdin-fd")) {
3354 if ((fd
= parse_fd(value
)) < 0 || !fdset_contains(fds
, fd
))
3355 log_unit_debug(u
, "Failed to parse stdin-fd value: %s", value
);
3357 asynchronous_close(s
->stdin_fd
);
3358 s
->stdin_fd
= fdset_remove(fds
, fd
);
3359 s
->exec_context
.stdio_as_fds
= true;
3361 } else if (streq(key
, "stdout-fd")) {
3364 if ((fd
= parse_fd(value
)) < 0 || !fdset_contains(fds
, fd
))
3365 log_unit_debug(u
, "Failed to parse stdout-fd value: %s", value
);
3367 asynchronous_close(s
->stdout_fd
);
3368 s
->stdout_fd
= fdset_remove(fds
, fd
);
3369 s
->exec_context
.stdio_as_fds
= true;
3371 } else if (streq(key
, "stderr-fd")) {
3374 if ((fd
= parse_fd(value
)) < 0 || !fdset_contains(fds
, fd
))
3375 log_unit_debug(u
, "Failed to parse stderr-fd value: %s", value
);
3377 asynchronous_close(s
->stderr_fd
);
3378 s
->stderr_fd
= fdset_remove(fds
, fd
);
3379 s
->exec_context
.stdio_as_fds
= true;
3381 } else if (streq(key
, "exec-fd")) {
3384 if ((fd
= parse_fd(value
)) < 0 || !fdset_contains(fds
, fd
))
3385 log_unit_debug(u
, "Failed to parse exec-fd value: %s", value
);
3387 s
->exec_fd_event_source
= sd_event_source_disable_unref(s
->exec_fd_event_source
);
3389 fd
= fdset_remove(fds
, fd
);
3390 if (service_allocate_exec_fd_event_source(s
, fd
, &s
->exec_fd_event_source
) < 0)
3393 } else if (streq(key
, "watchdog-override-usec")) {
3394 if (deserialize_usec(value
, &s
->watchdog_override_usec
) < 0)
3395 log_unit_debug(u
, "Failed to parse watchdog_override_usec value: %s", value
);
3397 s
->watchdog_override_enable
= true;
3399 } else if (streq(key
, "watchdog-original-usec")) {
3400 if (deserialize_usec(value
, &s
->watchdog_original_usec
) < 0)
3401 log_unit_debug(u
, "Failed to parse watchdog_original_usec value: %s", value
);
3403 } else if (STR_IN_SET(key
, "main-command", "control-command")) {
3404 r
= service_deserialize_exec_command(u
, key
, value
);
3406 log_unit_debug_errno(u
, r
, "Failed to parse serialized command \"%s\": %m", value
);
3408 } else if (streq(key
, "n-restarts")) {
3409 r
= safe_atou(value
, &s
->n_restarts
);
3411 log_unit_debug_errno(u
, r
, "Failed to parse serialized restart counter '%s': %m", value
);
3413 } else if (streq(key
, "flush-n-restarts")) {
3414 r
= parse_boolean(value
);
3416 log_unit_debug_errno(u
, r
, "Failed to parse serialized flush restart counter setting '%s': %m", value
);
3418 s
->flush_n_restarts
= r
;
3419 } else if (streq(key
, "reload-begin-usec")) {
3420 r
= deserialize_usec(value
, &s
->reload_begin_usec
);
3422 log_unit_debug_errno(u
, r
, "Failed to parse serialized reload begin timestamp '%s', ignoring: %m", value
);
3424 log_unit_debug(u
, "Unknown serialization key: %s", key
);
3429 static UnitActiveState
service_active_state(Unit
*u
) {
3430 const UnitActiveState
*table
;
3434 table
= SERVICE(u
)->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
3436 return table
[SERVICE(u
)->state
];
3439 static const char *service_sub_state_to_string(Unit
*u
) {
3442 return service_state_to_string(SERVICE(u
)->state
);
3445 static bool service_may_gc(Unit
*u
) {
3446 Service
*s
= SERVICE(u
);
3450 /* Never clean up services that still have a process around, even if the service is formally dead. Note that
3451 * unit_may_gc() already checked our cgroup for us, we just check our two additional PIDs, too, in case they
3452 * have moved outside of the cgroup. */
3454 if (main_pid_good(s
) > 0 ||
3455 control_pid_good(s
) > 0)
3458 /* Only allow collection of actually dead services, i.e. not those that are in the transitionary
3459 * SERVICE_DEAD_BEFORE_AUTO_RESTART/SERVICE_FAILED_BEFORE_AUTO_RESTART states. */
3460 if (!IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_DEAD_RESOURCES_PINNED
))
3466 static int service_retry_pid_file(Service
*s
) {
3469 assert(s
->pid_file
);
3470 assert(IN_SET(s
->state
, SERVICE_START
, SERVICE_START_POST
));
3472 r
= service_load_pid_file(s
, false);
3476 service_unwatch_pid_file(s
);
3478 service_enter_running(s
, SERVICE_SUCCESS
);
3482 static int service_watch_pid_file(Service
*s
) {
3485 log_unit_debug(UNIT(s
), "Setting watch for PID file %s", s
->pid_file_pathspec
->path
);
3487 r
= path_spec_watch(s
->pid_file_pathspec
, service_dispatch_inotify_io
);
3491 /* the pidfile might have appeared just before we set the watch */
3492 log_unit_debug(UNIT(s
), "Trying to read PID file %s in case it changed", s
->pid_file_pathspec
->path
);
3493 service_retry_pid_file(s
);
3497 log_unit_error_errno(UNIT(s
), r
, "Failed to set a watch for PID file %s: %m", s
->pid_file_pathspec
->path
);
3498 service_unwatch_pid_file(s
);
3502 static int service_demand_pid_file(Service
*s
) {
3503 _cleanup_free_ PathSpec
*ps
= NULL
;
3505 assert(s
->pid_file
);
3506 assert(!s
->pid_file_pathspec
);
3508 ps
= new(PathSpec
, 1);
3514 .path
= strdup(s
->pid_file
),
3515 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that keep their PID file
3516 * open all the time. */
3517 .type
= PATH_MODIFIED
,
3518 .inotify_fd
= -EBADF
,
3524 path_simplify(ps
->path
);
3526 s
->pid_file_pathspec
= TAKE_PTR(ps
);
3528 return service_watch_pid_file(s
);
3531 static int service_dispatch_inotify_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
) {
3532 PathSpec
*p
= ASSERT_PTR(userdata
);
3535 s
= SERVICE(p
->unit
);
3539 assert(IN_SET(s
->state
, SERVICE_START
, SERVICE_START_POST
));
3540 assert(s
->pid_file_pathspec
);
3541 assert(path_spec_owns_inotify_fd(s
->pid_file_pathspec
, fd
));
3543 log_unit_debug(UNIT(s
), "inotify event");
3545 if (path_spec_fd_event(p
, events
) < 0)
3548 if (service_retry_pid_file(s
) == 0)
3551 if (service_watch_pid_file(s
) < 0)
3557 service_unwatch_pid_file(s
);
3558 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
3562 static int service_dispatch_exec_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
) {
3563 Service
*s
= SERVICE(userdata
);
3567 log_unit_debug(UNIT(s
), "got exec-fd event");
3569 /* If Type=exec is set, we'll consider a service started successfully the instant we invoked execve()
3570 * successfully for it. We implement this through a pipe() towards the child, which the kernel automatically
3571 * closes for us due to O_CLOEXEC on execve() in the child, which then triggers EOF on the pipe in the
3572 * parent. We need to be careful however, as there are other reasons that we might cause the child's side of
3573 * the pipe to be closed (for example, a simple exit()). To deal with that we'll ignore EOFs on the pipe unless
3574 * the child signalled us first that it is about to call the execve(). It does so by sending us a simple
3575 * non-zero byte via the pipe. We also provide the child with a way to inform us in case execve() failed: if it
3576 * sends a zero byte we'll ignore POLLHUP on the fd again. */
3582 n
= read(fd
, &x
, sizeof(x
));
3584 if (errno
== EAGAIN
) /* O_NONBLOCK in effect → everything queued has now been processed. */
3587 return log_unit_error_errno(UNIT(s
), errno
, "Failed to read from exec_fd: %m");
3589 if (n
== 0) { /* EOF → the event we are waiting for */
3591 s
->exec_fd_event_source
= sd_event_source_disable_unref(s
->exec_fd_event_source
);
3593 if (s
->exec_fd_hot
) { /* Did the child tell us to expect EOF now? */
3594 log_unit_debug(UNIT(s
), "Got EOF on exec-fd");
3596 s
->exec_fd_hot
= false;
3598 /* Nice! This is what we have been waiting for. Transition to next state. */
3599 if (s
->type
== SERVICE_EXEC
&& s
->state
== SERVICE_START
)
3600 service_enter_start_post(s
);
3602 log_unit_debug(UNIT(s
), "Got EOF on exec-fd while it was disabled, ignoring.");
3607 /* A byte was read → this turns on/off the exec fd logic */
3608 assert(n
== sizeof(x
));
3615 static void service_notify_cgroup_empty_event(Unit
*u
) {
3616 Service
*s
= SERVICE(u
);
3620 log_unit_debug(u
, "Control group is empty.");
3624 /* Waiting for SIGCHLD is usually more interesting, because it includes return
3625 * codes/signals. Which is why we ignore the cgroup events for most cases, except when we
3626 * don't know pid which to expect the SIGCHLD for. */
3629 if (IN_SET(s
->type
, SERVICE_NOTIFY
, SERVICE_NOTIFY_RELOAD
) &&
3630 main_pid_good(s
) == 0 &&
3631 control_pid_good(s
) == 0) {
3632 /* No chance of getting a ready notification anymore */
3633 service_enter_stop_post(s
, SERVICE_FAILURE_PROTOCOL
);
3637 if (s
->exit_type
== SERVICE_EXIT_CGROUP
&& main_pid_good(s
) <= 0)
3638 service_enter_start_post(s
);
3641 case SERVICE_START_POST
:
3642 if (s
->pid_file_pathspec
&&
3643 main_pid_good(s
) == 0 &&
3644 control_pid_good(s
) == 0) {
3646 /* Give up hoping for the daemon to write its PID file */
3647 log_unit_warning(u
, "Daemon never wrote its PID file. Failing.");
3649 service_unwatch_pid_file(s
);
3650 if (s
->state
== SERVICE_START
)
3651 service_enter_stop_post(s
, SERVICE_FAILURE_PROTOCOL
);
3653 service_enter_stop(s
, SERVICE_FAILURE_PROTOCOL
);
3657 case SERVICE_RUNNING
:
3658 /* service_enter_running() will figure out what to do */
3659 service_enter_running(s
, SERVICE_SUCCESS
);
3662 case SERVICE_STOP_WATCHDOG
:
3663 case SERVICE_STOP_SIGTERM
:
3664 case SERVICE_STOP_SIGKILL
:
3666 if (main_pid_good(s
) <= 0 && control_pid_good(s
) <= 0)
3667 service_enter_stop_post(s
, SERVICE_SUCCESS
);
3671 case SERVICE_STOP_POST
:
3672 case SERVICE_FINAL_WATCHDOG
:
3673 case SERVICE_FINAL_SIGTERM
:
3674 case SERVICE_FINAL_SIGKILL
:
3675 if (main_pid_good(s
) <= 0 && control_pid_good(s
) <= 0)
3676 service_enter_dead(s
, SERVICE_SUCCESS
, true);
3680 /* If the cgroup empty notification comes when the unit is not active, we must have failed to clean
3681 * up the cgroup earlier and should do it now. */
3682 case SERVICE_AUTO_RESTART
:
3683 case SERVICE_AUTO_RESTART_QUEUED
:
3684 unit_prune_cgroup(u
);
3692 static void service_notify_cgroup_oom_event(Unit
*u
, bool managed_oom
) {
3693 Service
*s
= SERVICE(u
);
3696 log_unit_debug(u
, "Process(es) of control group were killed by systemd-oomd.");
3698 log_unit_debug(u
, "Process of control group was killed by the OOM killer.");
3700 if (s
->oom_policy
== OOM_CONTINUE
)
3705 case SERVICE_CONDITION
:
3706 case SERVICE_START_PRE
:
3708 case SERVICE_START_POST
:
3710 if (s
->oom_policy
== OOM_STOP
)
3711 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_OOM_KILL
);
3712 else if (s
->oom_policy
== OOM_KILL
)
3713 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_OOM_KILL
);
3717 case SERVICE_EXITED
:
3718 case SERVICE_RUNNING
:
3719 if (s
->oom_policy
== OOM_STOP
)
3720 service_enter_stop(s
, SERVICE_FAILURE_OOM_KILL
);
3721 else if (s
->oom_policy
== OOM_KILL
)
3722 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_OOM_KILL
);
3726 case SERVICE_STOP_WATCHDOG
:
3727 case SERVICE_STOP_SIGTERM
:
3728 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_OOM_KILL
);
3731 case SERVICE_STOP_SIGKILL
:
3732 case SERVICE_FINAL_SIGKILL
:
3733 if (s
->result
== SERVICE_SUCCESS
)
3734 s
->result
= SERVICE_FAILURE_OOM_KILL
;
3737 case SERVICE_STOP_POST
:
3738 case SERVICE_FINAL_SIGTERM
:
3739 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_OOM_KILL
);
3747 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
3748 bool notify_dbus
= true;
3749 Service
*s
= SERVICE(u
);
3751 ExitClean clean_mode
;
3756 /* Oneshot services and non-SERVICE_EXEC_START commands should not be
3757 * considered daemons as they are typically not long running. */
3758 if (s
->type
== SERVICE_ONESHOT
|| (s
->control_pid
.pid
== pid
&& s
->control_command_id
!= SERVICE_EXEC_START
))
3759 clean_mode
= EXIT_CLEAN_COMMAND
;
3761 clean_mode
= EXIT_CLEAN_DAEMON
;
3763 if (is_clean_exit(code
, status
, clean_mode
, &s
->success_status
))
3764 f
= SERVICE_SUCCESS
;
3765 else if (code
== CLD_EXITED
)
3766 f
= SERVICE_FAILURE_EXIT_CODE
;
3767 else if (code
== CLD_KILLED
)
3768 f
= SERVICE_FAILURE_SIGNAL
;
3769 else if (code
== CLD_DUMPED
)
3770 f
= SERVICE_FAILURE_CORE_DUMP
;
3772 assert_not_reached();
3774 if (s
->main_pid
.pid
== pid
) {
3775 /* Clean up the exec_fd event source. We want to do this here, not later in
3776 * service_set_state(), because service_enter_stop_post() calls service_spawn().
3777 * The source owns its end of the pipe, so this will close that too. */
3778 s
->exec_fd_event_source
= sd_event_source_disable_unref(s
->exec_fd_event_source
);
3780 /* Forking services may occasionally move to a new PID.
3781 * As long as they update the PID file before exiting the old
3782 * PID, they're fine. */
3783 if (service_load_pid_file(s
, false) > 0)
3786 pidref_done(&s
->main_pid
);
3787 exec_status_exit(&s
->main_exec_status
, &s
->exec_context
, pid
, code
, status
);
3789 if (s
->main_command
) {
3790 /* If this is not a forking service than the
3791 * main process got started and hence we copy
3792 * the exit status so that it is recorded both
3793 * as main and as control process exit
3796 s
->main_command
->exec_status
= s
->main_exec_status
;
3798 if (s
->main_command
->flags
& EXEC_COMMAND_IGNORE_FAILURE
)
3799 f
= SERVICE_SUCCESS
;
3800 } else if (s
->exec_command
[SERVICE_EXEC_START
]) {
3802 /* If this is a forked process, then we should
3803 * ignore the return value if this was
3804 * configured for the starter process */
3806 if (s
->exec_command
[SERVICE_EXEC_START
]->flags
& EXEC_COMMAND_IGNORE_FAILURE
)
3807 f
= SERVICE_SUCCESS
;
3810 unit_log_process_exit(
3813 service_exec_command_to_string(SERVICE_EXEC_START
),
3814 f
== SERVICE_SUCCESS
,
3817 if (s
->result
== SERVICE_SUCCESS
)
3820 if (s
->main_command
&&
3821 s
->main_command
->command_next
&&
3822 s
->type
== SERVICE_ONESHOT
&&
3823 f
== SERVICE_SUCCESS
) {
3825 /* There is another command to execute, so let's do that. */
3827 log_unit_debug(u
, "Running next main command for state %s.", service_state_to_string(s
->state
));
3828 service_run_next_main(s
);
3831 s
->main_command
= NULL
;
3833 /* Services with ExitType=cgroup do not act on main PID exiting, unless the cgroup is
3835 if (s
->exit_type
== SERVICE_EXIT_MAIN
|| cgroup_good(s
) <= 0) {
3836 /* The service exited, so the service is officially gone. */
3839 case SERVICE_START_POST
:
3840 case SERVICE_RELOAD
:
3841 case SERVICE_RELOAD_SIGNAL
:
3842 case SERVICE_RELOAD_NOTIFY
:
3843 /* If neither main nor control processes are running then the current
3844 * state can never exit cleanly, hence immediately terminate the
3846 if (control_pid_good(s
) <= 0)
3847 service_enter_stop(s
, f
);
3849 /* Otherwise need to wait until the operation is done. */
3853 /* Need to wait until the operation is done. */
3857 if (s
->type
== SERVICE_ONESHOT
) {
3858 /* This was our main goal, so let's go on */
3859 if (f
== SERVICE_SUCCESS
)
3860 service_enter_start_post(s
);
3862 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
3864 } else if (IN_SET(s
->type
, SERVICE_NOTIFY
, SERVICE_NOTIFY_RELOAD
)) {
3865 /* Only enter running through a notification, so that the
3866 * SERVICE_START state signifies that no ready notification
3867 * has been received */
3868 if (f
!= SERVICE_SUCCESS
)
3869 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
3870 else if (!s
->remain_after_exit
|| service_get_notify_access(s
) == NOTIFY_MAIN
)
3871 /* The service has never been and will never be active */
3872 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_PROTOCOL
);
3877 case SERVICE_RUNNING
:
3878 service_enter_running(s
, f
);
3881 case SERVICE_STOP_WATCHDOG
:
3882 case SERVICE_STOP_SIGTERM
:
3883 case SERVICE_STOP_SIGKILL
:
3885 if (control_pid_good(s
) <= 0)
3886 service_enter_stop_post(s
, f
);
3888 /* If there is still a control process, wait for that first */
3891 case SERVICE_STOP_POST
:
3893 if (control_pid_good(s
) <= 0)
3894 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
3898 case SERVICE_FINAL_WATCHDOG
:
3899 case SERVICE_FINAL_SIGTERM
:
3900 case SERVICE_FINAL_SIGKILL
:
3902 if (control_pid_good(s
) <= 0)
3903 service_enter_dead(s
, f
, true);
3907 assert_not_reached();
3909 } else if (s
->exit_type
== SERVICE_EXIT_CGROUP
&& s
->state
== SERVICE_START
)
3910 /* If a main process exits very quickly, this function might be executed
3911 * before service_dispatch_exec_io(). Since this function disabled IO events
3912 * to monitor the main process above, we need to update the state here too.
3913 * Let's consider the process is successfully launched and exited. */
3914 service_enter_start_post(s
);
3917 } else if (s
->control_pid
.pid
== pid
) {
3921 pidref_done(&s
->control_pid
);
3923 if (s
->control_command
) {
3924 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
3926 if (s
->control_command
->flags
& EXEC_COMMAND_IGNORE_FAILURE
)
3927 f
= SERVICE_SUCCESS
;
3930 /* ExecCondition= calls that exit with (0, 254] should invoke skip-like behavior instead of failing */
3931 if (s
->state
== SERVICE_CONDITION
) {
3932 if (f
== SERVICE_FAILURE_EXIT_CODE
&& status
< 255) {
3933 UNIT(s
)->condition_result
= false;
3934 f
= SERVICE_SKIP_CONDITION
;
3936 } else if (f
== SERVICE_SUCCESS
) {
3937 UNIT(s
)->condition_result
= true;
3942 kind
= "Condition check process";
3944 kind
= "Control process";
3945 success
= f
== SERVICE_SUCCESS
;
3948 unit_log_process_exit(
3951 service_exec_command_to_string(s
->control_command_id
),
3955 if (s
->state
!= SERVICE_RELOAD
&& s
->result
== SERVICE_SUCCESS
)
3958 if (s
->control_command
&&
3959 s
->control_command
->command_next
&&
3960 f
== SERVICE_SUCCESS
) {
3962 /* There is another command to * execute, so let's do that. */
3964 log_unit_debug(u
, "Running next control command for state %s.", service_state_to_string(s
->state
));
3965 service_run_next_control(s
);
3968 /* No further commands for this step, so let's figure out what to do next */
3970 s
->control_command
= NULL
;
3971 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
3973 log_unit_debug(u
, "Got final SIGCHLD for state %s.", service_state_to_string(s
->state
));
3977 case SERVICE_CONDITION
:
3978 if (f
== SERVICE_SUCCESS
)
3979 service_enter_start_pre(s
);
3981 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
3984 case SERVICE_START_PRE
:
3985 if (f
== SERVICE_SUCCESS
)
3986 service_enter_start(s
);
3988 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
3992 if (s
->type
!= SERVICE_FORKING
)
3993 /* Maybe spurious event due to a reload that changed the type? */
3996 if (f
!= SERVICE_SUCCESS
) {
3997 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
4002 bool has_start_post
;
4005 /* Let's try to load the pid file here if we can.
4006 * The PID file might actually be created by a START_POST
4007 * script. In that case don't worry if the loading fails. */
4009 has_start_post
= s
->exec_command
[SERVICE_EXEC_START_POST
];
4010 r
= service_load_pid_file(s
, !has_start_post
);
4011 if (!has_start_post
&& r
< 0) {
4012 r
= service_demand_pid_file(s
);
4013 if (r
< 0 || cgroup_good(s
) == 0)
4014 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_PROTOCOL
);
4018 service_search_main_pid(s
);
4020 service_enter_start_post(s
);
4023 case SERVICE_START_POST
:
4024 if (f
!= SERVICE_SUCCESS
) {
4025 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
4032 r
= service_load_pid_file(s
, true);
4034 r
= service_demand_pid_file(s
);
4035 if (r
< 0 || cgroup_good(s
) == 0)
4036 service_enter_stop(s
, SERVICE_FAILURE_PROTOCOL
);
4040 service_search_main_pid(s
);
4042 service_enter_running(s
, SERVICE_SUCCESS
);
4045 case SERVICE_RELOAD
:
4046 case SERVICE_RELOAD_SIGNAL
:
4047 case SERVICE_RELOAD_NOTIFY
:
4048 if (f
== SERVICE_SUCCESS
)
4049 if (service_load_pid_file(s
, true) < 0)
4050 service_search_main_pid(s
);
4052 s
->reload_result
= f
;
4054 /* If the last notification we received from the service process indicates
4055 * we are still reloading, then don't leave reloading state just yet, just
4056 * transition into SERVICE_RELOAD_NOTIFY, to wait for the READY=1 coming,
4058 if (s
->notify_state
== NOTIFY_RELOADING
)
4059 service_set_state(s
, SERVICE_RELOAD_NOTIFY
);
4061 service_enter_running(s
, SERVICE_SUCCESS
);
4065 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
4068 case SERVICE_STOP_WATCHDOG
:
4069 case SERVICE_STOP_SIGTERM
:
4070 case SERVICE_STOP_SIGKILL
:
4071 if (main_pid_good(s
) <= 0)
4072 service_enter_stop_post(s
, f
);
4074 /* If there is still a service process around, wait until
4075 * that one quit, too */
4078 case SERVICE_STOP_POST
:
4079 if (main_pid_good(s
) <= 0)
4080 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
4083 case SERVICE_FINAL_WATCHDOG
:
4084 case SERVICE_FINAL_SIGTERM
:
4085 case SERVICE_FINAL_SIGKILL
:
4086 if (main_pid_good(s
) <= 0)
4087 service_enter_dead(s
, f
, true);
4090 case SERVICE_CLEANING
:
4092 if (s
->clean_result
== SERVICE_SUCCESS
)
4093 s
->clean_result
= f
;
4095 service_enter_dead(s
, SERVICE_SUCCESS
, false);
4099 assert_not_reached();
4102 } else /* Neither control nor main PID? If so, don't notify about anything */
4103 notify_dbus
= false;
4105 /* Notify clients about changed exit status */
4107 unit_add_to_dbus_queue(u
);
4109 /* We watch the main/control process otherwise we can't retrieve the unit they
4110 * belong to with cgroupv1. But if they are not our direct child, we won't get a
4111 * SIGCHLD for them. Therefore we need to look for others to watch so we can
4112 * detect when the cgroup becomes empty. Note that the control process is always
4113 * our child so it's pointless to watch all other processes. */
4114 if (!control_pid_good(s
))
4115 if (!s
->main_pid_known
|| s
->main_pid_alien
)
4116 (void) unit_enqueue_rewatch_pids(u
);
4119 static int service_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
4120 Service
*s
= SERVICE(userdata
);
4123 assert(source
== s
->timer_event_source
);
4127 case SERVICE_CONDITION
:
4128 case SERVICE_START_PRE
:
4130 case SERVICE_START_POST
:
4131 switch (s
->timeout_start_failure_mode
) {
4133 case SERVICE_TIMEOUT_TERMINATE
:
4134 log_unit_warning(UNIT(s
), "%s operation timed out. Terminating.", service_state_to_string(s
->state
));
4135 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
4138 case SERVICE_TIMEOUT_ABORT
:
4139 log_unit_warning(UNIT(s
), "%s operation timed out. Aborting.", service_state_to_string(s
->state
));
4140 service_enter_signal(s
, SERVICE_STOP_WATCHDOG
, SERVICE_FAILURE_TIMEOUT
);
4143 case SERVICE_TIMEOUT_KILL
:
4144 if (s
->kill_context
.send_sigkill
) {
4145 log_unit_warning(UNIT(s
), "%s operation timed out. Killing.", service_state_to_string(s
->state
));
4146 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
4148 log_unit_warning(UNIT(s
), "%s operation timed out. Skipping SIGKILL.", service_state_to_string(s
->state
));
4149 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
4154 assert_not_reached();
4158 case SERVICE_RUNNING
:
4159 log_unit_warning(UNIT(s
), "Service reached runtime time limit. Stopping.");
4160 service_enter_stop(s
, SERVICE_FAILURE_TIMEOUT
);
4163 case SERVICE_RELOAD
:
4164 case SERVICE_RELOAD_SIGNAL
:
4165 case SERVICE_RELOAD_NOTIFY
:
4166 log_unit_warning(UNIT(s
), "Reload operation timed out. Killing reload process.");
4167 service_kill_control_process(s
);
4168 s
->reload_result
= SERVICE_FAILURE_TIMEOUT
;
4169 service_enter_running(s
, SERVICE_SUCCESS
);
4173 switch (s
->timeout_stop_failure_mode
) {
4175 case SERVICE_TIMEOUT_TERMINATE
:
4176 log_unit_warning(UNIT(s
), "Stopping timed out. Terminating.");
4177 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
4180 case SERVICE_TIMEOUT_ABORT
:
4181 log_unit_warning(UNIT(s
), "Stopping timed out. Aborting.");
4182 service_enter_signal(s
, SERVICE_STOP_WATCHDOG
, SERVICE_FAILURE_TIMEOUT
);
4185 case SERVICE_TIMEOUT_KILL
:
4186 if (s
->kill_context
.send_sigkill
) {
4187 log_unit_warning(UNIT(s
), "Stopping timed out. Killing.");
4188 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
4190 log_unit_warning(UNIT(s
), "Stopping timed out. Skipping SIGKILL.");
4191 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
4196 assert_not_reached();
4200 case SERVICE_STOP_WATCHDOG
:
4201 if (s
->kill_context
.send_sigkill
) {
4202 log_unit_warning(UNIT(s
), "State 'stop-watchdog' timed out. Killing.");
4203 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
4205 log_unit_warning(UNIT(s
), "State 'stop-watchdog' timed out. Skipping SIGKILL.");
4206 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
4210 case SERVICE_STOP_SIGTERM
:
4211 if (s
->timeout_stop_failure_mode
== SERVICE_TIMEOUT_ABORT
) {
4212 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Aborting.");
4213 service_enter_signal(s
, SERVICE_STOP_WATCHDOG
, SERVICE_FAILURE_TIMEOUT
);
4214 } else if (s
->kill_context
.send_sigkill
) {
4215 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Killing.");
4216 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
4218 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Skipping SIGKILL.");
4219 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
4224 case SERVICE_STOP_SIGKILL
:
4225 /* Uh, we sent a SIGKILL and it is still not gone?
4226 * Must be something we cannot kill, so let's just be
4227 * weirded out and continue */
4229 log_unit_warning(UNIT(s
), "Processes still around after SIGKILL. Ignoring.");
4230 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
4233 case SERVICE_STOP_POST
:
4234 switch (s
->timeout_stop_failure_mode
) {
4236 case SERVICE_TIMEOUT_TERMINATE
:
4237 log_unit_warning(UNIT(s
), "State 'stop-post' timed out. Terminating.");
4238 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
4241 case SERVICE_TIMEOUT_ABORT
:
4242 log_unit_warning(UNIT(s
), "State 'stop-post' timed out. Aborting.");
4243 service_enter_signal(s
, SERVICE_FINAL_WATCHDOG
, SERVICE_FAILURE_TIMEOUT
);
4246 case SERVICE_TIMEOUT_KILL
:
4247 if (s
->kill_context
.send_sigkill
) {
4248 log_unit_warning(UNIT(s
), "State 'stop-post' timed out. Killing.");
4249 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
4251 log_unit_warning(UNIT(s
), "State 'stop-post' timed out. Skipping SIGKILL. Entering failed mode.");
4252 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, false);
4257 assert_not_reached();
4261 case SERVICE_FINAL_WATCHDOG
:
4262 if (s
->kill_context
.send_sigkill
) {
4263 log_unit_warning(UNIT(s
), "State 'final-watchdog' timed out. Killing.");
4264 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
4266 log_unit_warning(UNIT(s
), "State 'final-watchdog' timed out. Skipping SIGKILL. Entering failed mode.");
4267 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, false);
4271 case SERVICE_FINAL_SIGTERM
:
4272 if (s
->timeout_stop_failure_mode
== SERVICE_TIMEOUT_ABORT
) {
4273 log_unit_warning(UNIT(s
), "State 'final-sigterm' timed out. Aborting.");
4274 service_enter_signal(s
, SERVICE_FINAL_WATCHDOG
, SERVICE_FAILURE_TIMEOUT
);
4275 } else if (s
->kill_context
.send_sigkill
) {
4276 log_unit_warning(UNIT(s
), "State 'final-sigterm' timed out. Killing.");
4277 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
4279 log_unit_warning(UNIT(s
), "State 'final-sigterm' timed out. Skipping SIGKILL. Entering failed mode.");
4280 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, false);
4285 case SERVICE_FINAL_SIGKILL
:
4286 log_unit_warning(UNIT(s
), "Processes still around after final SIGKILL. Entering failed mode.");
4287 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, true);
4290 case SERVICE_AUTO_RESTART
:
4291 if (s
->restart_usec
> 0)
4292 log_unit_debug(UNIT(s
),
4293 "Service restart interval %s expired, scheduling restart.",
4294 FORMAT_TIMESPAN(service_restart_usec_next(s
), USEC_PER_SEC
));
4296 log_unit_debug(UNIT(s
),
4297 "Service has no hold-off time (RestartSec=0), scheduling restart.");
4299 service_enter_restart(s
);
4302 case SERVICE_CLEANING
:
4303 log_unit_warning(UNIT(s
), "Cleaning timed out. killing.");
4305 if (s
->clean_result
== SERVICE_SUCCESS
)
4306 s
->clean_result
= SERVICE_FAILURE_TIMEOUT
;
4308 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, 0);
4312 assert_not_reached();
4318 static int service_dispatch_watchdog(sd_event_source
*source
, usec_t usec
, void *userdata
) {
4319 Service
*s
= SERVICE(userdata
);
4320 usec_t watchdog_usec
;
4323 assert(source
== s
->watchdog_event_source
);
4325 watchdog_usec
= service_get_watchdog_usec(s
);
4327 if (UNIT(s
)->manager
->service_watchdogs
) {
4328 log_unit_error(UNIT(s
), "Watchdog timeout (limit %s)!",
4329 FORMAT_TIMESPAN(watchdog_usec
, 1));
4331 service_enter_signal(s
, SERVICE_STOP_WATCHDOG
, SERVICE_FAILURE_WATCHDOG
);
4333 log_unit_warning(UNIT(s
), "Watchdog disabled! Ignoring watchdog timeout (limit %s)!",
4334 FORMAT_TIMESPAN(watchdog_usec
, 1));
4339 static bool service_notify_message_authorized(Service
*s
, pid_t pid
, FDSet
*fds
) {
4342 NotifyAccess notify_access
= service_get_notify_access(s
);
4344 if (notify_access
== NOTIFY_NONE
) {
4345 log_unit_warning(UNIT(s
), "Got notification message from PID "PID_FMT
", but reception is disabled.", pid
);
4349 if (notify_access
== NOTIFY_MAIN
&& pid
!= s
->main_pid
.pid
) {
4350 if (pidref_is_set(&s
->main_pid
))
4351 log_unit_warning(UNIT(s
), "Got notification message from PID "PID_FMT
", but reception only permitted for main PID "PID_FMT
, pid
, s
->main_pid
.pid
);
4353 log_unit_warning(UNIT(s
), "Got notification message from PID "PID_FMT
", but reception only permitted for main PID which is currently not known", pid
);
4358 if (notify_access
== NOTIFY_EXEC
&& pid
!= s
->main_pid
.pid
&& pid
!= s
->control_pid
.pid
) {
4359 if (pidref_is_set(&s
->main_pid
) && pidref_is_set(&s
->control_pid
))
4360 log_unit_warning(UNIT(s
), "Got notification message from PID "PID_FMT
", but reception only permitted for main PID "PID_FMT
" and control PID "PID_FMT
,
4361 pid
, s
->main_pid
.pid
, s
->control_pid
.pid
);
4362 else if (pidref_is_set(&s
->main_pid
))
4363 log_unit_warning(UNIT(s
), "Got notification message from PID "PID_FMT
", but reception only permitted for main PID "PID_FMT
, pid
, s
->main_pid
.pid
);
4364 else if (pidref_is_set(&s
->control_pid
))
4365 log_unit_warning(UNIT(s
), "Got notification message from PID "PID_FMT
", but reception only permitted for control PID "PID_FMT
, pid
, s
->control_pid
.pid
);
4367 log_unit_warning(UNIT(s
), "Got notification message from PID "PID_FMT
", but reception only permitted for main PID and control PID which are currently not known", pid
);
4375 static void service_force_watchdog(Service
*s
) {
4376 if (!UNIT(s
)->manager
->service_watchdogs
)
4379 log_unit_error(UNIT(s
), "Watchdog request (last status: %s)!",
4380 s
->status_text
?: "<unset>");
4382 service_enter_signal(s
, SERVICE_STOP_WATCHDOG
, SERVICE_FAILURE_WATCHDOG
);
4385 static void service_notify_message(
4387 const struct ucred
*ucred
,
4391 Service
*s
= SERVICE(u
);
4392 bool notify_dbus
= false;
4393 usec_t monotonic_usec
= USEC_INFINITY
;
4400 if (!service_notify_message_authorized(s
, ucred
->pid
, fds
))
4403 if (DEBUG_LOGGING
) {
4404 _cleanup_free_
char *cc
= NULL
;
4406 cc
= strv_join(tags
, ", ");
4407 log_unit_debug(u
, "Got notification message from PID "PID_FMT
" (%s)", ucred
->pid
, isempty(cc
) ? "n/a" : cc
);
4410 /* Interpret MAINPID= */
4411 e
= strv_find_startswith(tags
, "MAINPID=");
4412 if (e
&& IN_SET(s
->state
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
, SERVICE_RELOAD_SIGNAL
, SERVICE_RELOAD_NOTIFY
)) {
4415 if (parse_pid(e
, &new_main_pid
) < 0)
4416 log_unit_warning(u
, "Failed to parse MAINPID= field in notification message, ignoring: %s", e
);
4417 else if (!s
->main_pid_known
|| new_main_pid
!= s
->main_pid
.pid
) {
4419 r
= service_is_suitable_main_pid(s
, new_main_pid
, LOG_WARNING
);
4421 /* The new main PID is a bit suspicious, which is OK if the sender is privileged. */
4423 if (ucred
->uid
== 0) {
4424 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
);
4427 log_unit_debug(u
, "New main PID "PID_FMT
" does not belong to service, refusing.", new_main_pid
);
4430 (void) service_set_main_pid(s
, new_main_pid
);
4432 r
= unit_watch_pid(UNIT(s
), new_main_pid
, /* exclusive= */ false);
4434 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch new main PID "PID_FMT
" for service: %m", new_main_pid
);
4441 /* Parse MONOTONIC_USEC= */
4442 e
= strv_find_startswith(tags
, "MONOTONIC_USEC=");
4444 r
= safe_atou64(e
, &monotonic_usec
);
4446 log_unit_warning_errno(u
, r
, "Failed to parse MONOTONIC_USEC= field in notification message, ignoring: %s", e
);
4449 /* Interpret READY=/STOPPING=/RELOADING=. STOPPING= wins over the others, and READY= over RELOADING= */
4450 if (strv_contains(tags
, "STOPPING=1")) {
4451 s
->notify_state
= NOTIFY_STOPPING
;
4453 if (IN_SET(s
->state
, SERVICE_RUNNING
, SERVICE_RELOAD_SIGNAL
, SERVICE_RELOAD_NOTIFY
))
4454 service_enter_stop_by_notify(s
);
4458 } else if (strv_contains(tags
, "READY=1")) {
4460 s
->notify_state
= NOTIFY_READY
;
4462 /* Type=notify services inform us about completed initialization with READY=1 */
4463 if (IN_SET(s
->type
, SERVICE_NOTIFY
, SERVICE_NOTIFY_RELOAD
) &&
4464 s
->state
== SERVICE_START
)
4465 service_enter_start_post(s
);
4467 /* Sending READY=1 while we are reloading informs us that the reloading is complete. */
4468 if (s
->state
== SERVICE_RELOAD_NOTIFY
)
4469 service_enter_running(s
, SERVICE_SUCCESS
);
4471 /* Combined RELOADING=1 and READY=1? Then this is indication that the service started and
4472 * immediately finished reloading. */
4473 if (s
->state
== SERVICE_RELOAD_SIGNAL
&&
4474 strv_contains(tags
, "RELOADING=1") &&
4475 monotonic_usec
!= USEC_INFINITY
&&
4476 monotonic_usec
>= s
->reload_begin_usec
) {
4477 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4479 /* Propagate a reload explicitly */
4480 r
= manager_propagate_reload(UNIT(s
)->manager
, UNIT(s
), JOB_FAIL
, &error
);
4482 log_unit_warning(UNIT(s
), "Failed to schedule propagation of reload, ignoring: %s", bus_error_message(&error
, r
));
4484 service_enter_running(s
, SERVICE_SUCCESS
);
4489 } else if (strv_contains(tags
, "RELOADING=1")) {
4491 s
->notify_state
= NOTIFY_RELOADING
;
4493 /* Sending RELOADING=1 after we send SIGHUP to request a reload will transition
4494 * things to "reload-notify" state, where we'll wait for READY=1 to let us know the
4495 * reload is done. Note that we insist on a timestamp being sent along here, so that
4496 * we know for sure this is a reload cycle initiated *after* we sent the signal */
4497 if (s
->state
== SERVICE_RELOAD_SIGNAL
&&
4498 monotonic_usec
!= USEC_INFINITY
&&
4499 monotonic_usec
>= s
->reload_begin_usec
)
4500 /* Note, we don't call service_enter_reload_by_notify() here, because we
4501 * don't need reload propagation nor do we want to restart the time-out. */
4502 service_set_state(s
, SERVICE_RELOAD_NOTIFY
);
4504 if (s
->state
== SERVICE_RUNNING
)
4505 service_enter_reload_by_notify(s
);
4510 /* Interpret STATUS= */
4511 e
= strv_find_startswith(tags
, "STATUS=");
4513 _cleanup_free_
char *t
= NULL
;
4516 /* Note that this size limit check is mostly paranoia: since the datagram size we are willing
4517 * to process is already limited to NOTIFY_BUFFER_MAX, this limit here should never be hit. */
4518 if (strlen(e
) > STATUS_TEXT_MAX
)
4519 log_unit_warning(u
, "Status message overly long (%zu > %u), ignoring.", strlen(e
), STATUS_TEXT_MAX
);
4520 else if (!utf8_is_valid(e
))
4521 log_unit_warning(u
, "Status message in notification message is not UTF-8 clean, ignoring.");
4529 if (!streq_ptr(s
->status_text
, t
)) {
4530 free_and_replace(s
->status_text
, t
);
4535 /* Interpret NOTIFYACCESS= */
4536 e
= strv_find_startswith(tags
, "NOTIFYACCESS=");
4538 NotifyAccess notify_access
;
4540 notify_access
= notify_access_from_string(e
);
4541 if (notify_access
< 0)
4542 log_unit_warning_errno(u
, notify_access
,
4543 "Failed to parse NOTIFYACCESS= field value '%s' in notification message, ignoring: %m", e
);
4545 /* We don't need to check whether the new access mode is more strict than what is
4546 * already in use, since only the privileged process is allowed to change it
4547 * in the first place. */
4548 if (service_get_notify_access(s
) != notify_access
) {
4549 service_override_notify_access(s
, notify_access
);
4554 /* Interpret ERRNO= */
4555 e
= strv_find_startswith(tags
, "ERRNO=");
4559 status_errno
= parse_errno(e
);
4560 if (status_errno
< 0)
4561 log_unit_warning_errno(u
, status_errno
,
4562 "Failed to parse ERRNO= field value '%s' in notification message: %m", e
);
4563 else if (s
->status_errno
!= status_errno
) {
4564 s
->status_errno
= status_errno
;
4569 /* Interpret EXTEND_TIMEOUT= */
4570 e
= strv_find_startswith(tags
, "EXTEND_TIMEOUT_USEC=");
4572 usec_t extend_timeout_usec
;
4573 if (safe_atou64(e
, &extend_timeout_usec
) < 0)
4574 log_unit_warning(u
, "Failed to parse EXTEND_TIMEOUT_USEC=%s", e
);
4576 service_extend_timeout(s
, extend_timeout_usec
);
4579 /* Interpret WATCHDOG= */
4580 e
= strv_find_startswith(tags
, "WATCHDOG=");
4583 service_reset_watchdog(s
);
4584 else if (streq(e
, "trigger"))
4585 service_force_watchdog(s
);
4587 log_unit_warning(u
, "Passed WATCHDOG= field is invalid, ignoring.");
4590 e
= strv_find_startswith(tags
, "WATCHDOG_USEC=");
4592 usec_t watchdog_override_usec
;
4593 if (safe_atou64(e
, &watchdog_override_usec
) < 0)
4594 log_unit_warning(u
, "Failed to parse WATCHDOG_USEC=%s", e
);
4596 service_override_watchdog_timeout(s
, watchdog_override_usec
);
4599 /* Process FD store messages. Either FDSTOREREMOVE=1 for removal, or FDSTORE=1 for addition. In both cases,
4600 * process FDNAME= for picking the file descriptor name to use. Note that FDNAME= is required when removing
4601 * fds, but optional when pushing in new fds, for compatibility reasons. */
4602 if (strv_contains(tags
, "FDSTOREREMOVE=1")) {
4605 name
= strv_find_startswith(tags
, "FDNAME=");
4606 if (!name
|| !fdname_is_valid(name
))
4607 log_unit_warning(u
, "FDSTOREREMOVE=1 requested, but no valid file descriptor name passed, ignoring.");
4609 service_remove_fd_store(s
, name
);
4611 } else if (strv_contains(tags
, "FDSTORE=1")) {
4614 name
= strv_find_startswith(tags
, "FDNAME=");
4615 if (name
&& !fdname_is_valid(name
)) {
4616 log_unit_warning(u
, "Passed FDNAME= name is invalid, ignoring.");
4620 (void) service_add_fd_store_set(s
, fds
, name
, !strv_contains(tags
, "FDPOLL=0"));
4623 /* Notify clients about changed status or main pid */
4625 unit_add_to_dbus_queue(u
);
4628 static int service_get_timeout(Unit
*u
, usec_t
*timeout
) {
4629 Service
*s
= SERVICE(u
);
4633 if (!s
->timer_event_source
)
4636 r
= sd_event_source_get_time(s
->timer_event_source
, &t
);
4639 if (t
== USEC_INFINITY
)
4646 static usec_t
service_get_timeout_start_usec(Unit
*u
) {
4647 Service
*s
= SERVICE(ASSERT_PTR(u
));
4648 return s
->timeout_start_usec
;
4651 static bool pick_up_pid_from_bus_name(Service
*s
) {
4654 /* If the service is running but we have no main PID yet, get it from the owner of the D-Bus name */
4656 return !pidref_is_set(&s
->main_pid
) &&
4662 SERVICE_RELOAD_SIGNAL
,
4663 SERVICE_RELOAD_NOTIFY
);
4666 static int bus_name_pid_lookup_callback(sd_bus_message
*reply
, void *userdata
, sd_bus_error
*ret_error
) {
4667 const sd_bus_error
*e
;
4668 Unit
*u
= ASSERT_PTR(userdata
);
4676 s
->bus_name_pid_lookup_slot
= sd_bus_slot_unref(s
->bus_name_pid_lookup_slot
);
4678 if (!s
->bus_name
|| !pick_up_pid_from_bus_name(s
))
4681 e
= sd_bus_message_get_error(reply
);
4683 r
= sd_bus_error_get_errno(e
);
4684 log_warning_errno(r
, "GetConnectionUnixProcessID() failed: %s", bus_error_message(e
, r
));
4688 r
= sd_bus_message_read(reply
, "u", &pid
);
4690 bus_log_parse_error(r
);
4694 if (!pid_is_valid(pid
)) {
4695 log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "GetConnectionUnixProcessID() returned invalid PID");
4699 log_unit_debug(u
, "D-Bus name %s is now owned by process " PID_FMT
, s
->bus_name
, (pid_t
) pid
);
4701 (void) service_set_main_pid(s
, pid
);
4702 (void) unit_watch_pid(UNIT(s
), pid
, /* exclusive= */ false);
4706 static void service_bus_name_owner_change(Unit
*u
, const char *new_owner
) {
4708 Service
*s
= SERVICE(u
);
4714 log_unit_debug(u
, "D-Bus name %s now owned by %s", s
->bus_name
, new_owner
);
4716 log_unit_debug(u
, "D-Bus name %s now not owned by anyone.", s
->bus_name
);
4718 s
->bus_name_good
= new_owner
;
4720 /* Track the current owner, so we can reconstruct changes after a daemon reload */
4721 r
= free_and_strdup(&s
->bus_name_owner
, new_owner
);
4723 log_unit_error_errno(u
, r
, "Unable to set new bus name owner %s: %m", new_owner
);
4727 if (s
->type
== SERVICE_DBUS
) {
4729 /* service_enter_running() will figure out what to
4731 if (s
->state
== SERVICE_RUNNING
)
4732 service_enter_running(s
, SERVICE_SUCCESS
);
4733 else if (s
->state
== SERVICE_START
&& new_owner
)
4734 service_enter_start_post(s
);
4736 } else if (new_owner
&& pick_up_pid_from_bus_name(s
)) {
4738 /* Try to acquire PID from bus service */
4740 s
->bus_name_pid_lookup_slot
= sd_bus_slot_unref(s
->bus_name_pid_lookup_slot
);
4742 r
= sd_bus_call_method_async(
4743 u
->manager
->api_bus
,
4744 &s
->bus_name_pid_lookup_slot
,
4745 "org.freedesktop.DBus",
4746 "/org/freedesktop/DBus",
4747 "org.freedesktop.DBus",
4748 "GetConnectionUnixProcessID",
4749 bus_name_pid_lookup_callback
,
4754 log_debug_errno(r
, "Failed to request owner PID of service name, ignoring: %m");
4758 int service_set_socket_fd(
4763 bool selinux_context_net
) {
4765 _cleanup_free_
char *peer_text
= NULL
;
4771 /* This is called by the socket code when instantiating a new service for a stream socket and the socket needs
4772 * to be configured. We take ownership of the passed fd on success. */
4774 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
4777 if (s
->socket_fd
>= 0)
4780 assert(!s
->socket_peer
);
4782 if (!IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_DEAD_RESOURCES_PINNED
))
4785 if (getpeername_pretty(fd
, true, &peer_text
) >= 0) {
4787 if (UNIT(s
)->description
) {
4788 _cleanup_free_
char *a
= NULL
;
4790 a
= strjoin(UNIT(s
)->description
, " (", peer_text
, ")");
4794 r
= unit_set_description(UNIT(s
), a
);
4796 r
= unit_set_description(UNIT(s
), peer_text
);
4801 r
= unit_add_two_dependencies(UNIT(sock
), UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT(s
), false, UNIT_DEPENDENCY_IMPLICIT
);
4806 s
->socket_peer
= socket_peer_ref(peer
);
4807 s
->socket_fd_selinux_context_net
= selinux_context_net
;
4809 unit_ref_set(&s
->accept_socket
, UNIT(s
), UNIT(sock
));
4813 static void service_reset_failed(Unit
*u
) {
4814 Service
*s
= SERVICE(u
);
4818 if (s
->state
== SERVICE_FAILED
)
4819 service_set_state(s
, service_determine_dead_state(s
));
4821 s
->result
= SERVICE_SUCCESS
;
4822 s
->reload_result
= SERVICE_SUCCESS
;
4823 s
->clean_result
= SERVICE_SUCCESS
;
4825 s
->flush_n_restarts
= false;
4828 static PidRef
* service_main_pid(Unit
*u
) {
4829 return &ASSERT_PTR(SERVICE(u
))->main_pid
;
4832 static PidRef
* service_control_pid(Unit
*u
) {
4833 return &ASSERT_PTR(SERVICE(u
))->control_pid
;
4836 static bool service_needs_console(Unit
*u
) {
4837 Service
*s
= SERVICE(u
);
4841 /* We provide our own implementation of this here, instead of relying of the generic implementation
4842 * unit_needs_console() provides, since we want to return false if we are in SERVICE_EXITED state. */
4844 if (!exec_context_may_touch_console(&s
->exec_context
))
4847 return IN_SET(s
->state
,
4854 SERVICE_RELOAD_SIGNAL
,
4855 SERVICE_RELOAD_NOTIFY
,
4857 SERVICE_STOP_WATCHDOG
,
4858 SERVICE_STOP_SIGTERM
,
4859 SERVICE_STOP_SIGKILL
,
4861 SERVICE_FINAL_WATCHDOG
,
4862 SERVICE_FINAL_SIGTERM
,
4863 SERVICE_FINAL_SIGKILL
);
4866 static int service_exit_status(Unit
*u
) {
4867 Service
*s
= SERVICE(u
);
4871 if (s
->main_exec_status
.pid
<= 0 ||
4872 !dual_timestamp_is_set(&s
->main_exec_status
.exit_timestamp
))
4875 if (s
->main_exec_status
.code
!= CLD_EXITED
)
4878 return s
->main_exec_status
.status
;
4881 static const char* service_status_text(Unit
*u
) {
4882 Service
*s
= SERVICE(u
);
4886 return s
->status_text
;
4889 static int service_clean(Unit
*u
, ExecCleanMask mask
) {
4890 _cleanup_strv_free_
char **l
= NULL
;
4891 bool may_clean_fdstore
= false;
4892 Service
*s
= SERVICE(u
);
4898 if (!IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_DEAD_RESOURCES_PINNED
))
4901 /* Determine if there's anything we could potentially clean */
4902 r
= exec_context_get_clean_directories(&s
->exec_context
, u
->manager
->prefix
, mask
, &l
);
4906 if (mask
& EXEC_CLEAN_FDSTORE
)
4907 may_clean_fdstore
= s
->n_fd_store
> 0 || s
->n_fd_store_max
> 0;
4909 if (strv_isempty(l
) && !may_clean_fdstore
)
4910 return -EUNATCH
; /* Nothing to potentially clean */
4912 /* Let's clean the stuff we can clean quickly */
4913 if (may_clean_fdstore
)
4914 service_release_fd_store(s
);
4916 /* If we are done, leave quickly */
4917 if (strv_isempty(l
)) {
4918 if (s
->state
== SERVICE_DEAD_RESOURCES_PINNED
&& !s
->fd_store
)
4919 service_set_state(s
, SERVICE_DEAD
);
4923 /* We need to clean disk stuff. This is slow, hence do it out of process, and change state */
4924 service_unwatch_control_pid(s
);
4925 s
->clean_result
= SERVICE_SUCCESS
;
4926 s
->control_command
= NULL
;
4927 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
4929 r
= service_arm_timer(s
, /* relative= */ true, s
->exec_context
.timeout_clean_usec
);
4933 r
= unit_fork_and_watch_rm_rf(u
, l
, &s
->control_pid
);
4937 service_set_state(s
, SERVICE_CLEANING
);
4941 log_unit_warning_errno(u
, r
, "Failed to initiate cleaning: %m");
4942 s
->clean_result
= SERVICE_FAILURE_RESOURCES
;
4943 s
->timer_event_source
= sd_event_source_disable_unref(s
->timer_event_source
);
4947 static int service_can_clean(Unit
*u
, ExecCleanMask
*ret
) {
4948 Service
*s
= SERVICE(u
);
4949 ExecCleanMask mask
= 0;
4955 r
= exec_context_get_clean_mask(&s
->exec_context
, &mask
);
4959 if (s
->n_fd_store_max
> 0)
4960 mask
|= EXEC_CLEAN_FDSTORE
;
4966 static const char *service_finished_job(Unit
*u
, JobType t
, JobResult result
) {
4967 if (t
== JOB_START
&&
4968 result
== JOB_DONE
&&
4969 SERVICE(u
)->type
== SERVICE_ONESHOT
)
4970 return "Finished %s.";
4972 /* Fall back to generic */
4976 static int service_can_start(Unit
*u
) {
4977 Service
*s
= SERVICE(u
);
4982 /* Make sure we don't enter a busy loop of some kind. */
4983 r
= unit_test_start_limit(u
);
4985 service_enter_dead(s
, SERVICE_FAILURE_START_LIMIT_HIT
, false);
4992 static void service_release_resources(Unit
*u
) {
4993 Service
*s
= SERVICE(ASSERT_PTR(u
));
4995 /* Invoked by the unit state engine, whenever it realizes that unit is dead and there's no job
4996 * anymore for it, and it hence is a good idea to release resources */
4998 /* Don't release resources if this is a transitionary failed/dead state
4999 * (i.e. SERVICE_DEAD_BEFORE_AUTO_RESTART/SERVICE_FAILED_BEFORE_AUTO_RESTART), insist on a permanent
5001 if (!IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_DEAD_RESOURCES_PINNED
))
5004 log_unit_debug(u
, "Releasing resources...");
5006 service_release_socket_fd(s
);
5007 service_release_stdio_fd(s
);
5009 if (s
->fd_store_preserve_mode
!= EXEC_PRESERVE_YES
)
5010 service_release_fd_store(s
);
5012 if (s
->state
== SERVICE_DEAD_RESOURCES_PINNED
&& !s
->fd_store
)
5013 service_set_state(s
, SERVICE_DEAD
);
5016 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
5017 [SERVICE_RESTART_NO
] = "no",
5018 [SERVICE_RESTART_ON_SUCCESS
] = "on-success",
5019 [SERVICE_RESTART_ON_FAILURE
] = "on-failure",
5020 [SERVICE_RESTART_ON_ABNORMAL
] = "on-abnormal",
5021 [SERVICE_RESTART_ON_WATCHDOG
] = "on-watchdog",
5022 [SERVICE_RESTART_ON_ABORT
] = "on-abort",
5023 [SERVICE_RESTART_ALWAYS
] = "always",
5026 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
5028 static const char* const service_restart_mode_table
[_SERVICE_RESTART_MODE_MAX
] = {
5029 [SERVICE_RESTART_MODE_NORMAL
] = "normal",
5030 [SERVICE_RESTART_MODE_DIRECT
] = "direct",
5033 DEFINE_STRING_TABLE_LOOKUP(service_restart_mode
, ServiceRestartMode
);
5035 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
5036 [SERVICE_SIMPLE
] = "simple",
5037 [SERVICE_FORKING
] = "forking",
5038 [SERVICE_ONESHOT
] = "oneshot",
5039 [SERVICE_DBUS
] = "dbus",
5040 [SERVICE_NOTIFY
] = "notify",
5041 [SERVICE_NOTIFY_RELOAD
] = "notify-reload",
5042 [SERVICE_IDLE
] = "idle",
5043 [SERVICE_EXEC
] = "exec",
5046 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
5048 static const char* const service_exit_type_table
[_SERVICE_EXIT_TYPE_MAX
] = {
5049 [SERVICE_EXIT_MAIN
] = "main",
5050 [SERVICE_EXIT_CGROUP
] = "cgroup",
5053 DEFINE_STRING_TABLE_LOOKUP(service_exit_type
, ServiceExitType
);
5055 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
5056 [SERVICE_EXEC_CONDITION
] = "ExecCondition",
5057 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
5058 [SERVICE_EXEC_START
] = "ExecStart",
5059 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
5060 [SERVICE_EXEC_RELOAD
] = "ExecReload",
5061 [SERVICE_EXEC_STOP
] = "ExecStop",
5062 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
5065 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
5067 static const char* const service_exec_ex_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
5068 [SERVICE_EXEC_CONDITION
] = "ExecConditionEx",
5069 [SERVICE_EXEC_START_PRE
] = "ExecStartPreEx",
5070 [SERVICE_EXEC_START
] = "ExecStartEx",
5071 [SERVICE_EXEC_START_POST
] = "ExecStartPostEx",
5072 [SERVICE_EXEC_RELOAD
] = "ExecReloadEx",
5073 [SERVICE_EXEC_STOP
] = "ExecStopEx",
5074 [SERVICE_EXEC_STOP_POST
] = "ExecStopPostEx",
5077 DEFINE_STRING_TABLE_LOOKUP(service_exec_ex_command
, ServiceExecCommand
);
5079 static const char* const notify_state_table
[_NOTIFY_STATE_MAX
] = {
5080 [NOTIFY_UNKNOWN
] = "unknown",
5081 [NOTIFY_READY
] = "ready",
5082 [NOTIFY_RELOADING
] = "reloading",
5083 [NOTIFY_STOPPING
] = "stopping",
5086 DEFINE_STRING_TABLE_LOOKUP(notify_state
, NotifyState
);
5088 static const char* const service_result_table
[_SERVICE_RESULT_MAX
] = {
5089 [SERVICE_SUCCESS
] = "success",
5090 [SERVICE_FAILURE_RESOURCES
] = "resources",
5091 [SERVICE_FAILURE_PROTOCOL
] = "protocol",
5092 [SERVICE_FAILURE_TIMEOUT
] = "timeout",
5093 [SERVICE_FAILURE_EXIT_CODE
] = "exit-code",
5094 [SERVICE_FAILURE_SIGNAL
] = "signal",
5095 [SERVICE_FAILURE_CORE_DUMP
] = "core-dump",
5096 [SERVICE_FAILURE_WATCHDOG
] = "watchdog",
5097 [SERVICE_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
5098 [SERVICE_FAILURE_OOM_KILL
] = "oom-kill",
5099 [SERVICE_SKIP_CONDITION
] = "exec-condition",
5102 DEFINE_STRING_TABLE_LOOKUP(service_result
, ServiceResult
);
5104 static const char* const service_timeout_failure_mode_table
[_SERVICE_TIMEOUT_FAILURE_MODE_MAX
] = {
5105 [SERVICE_TIMEOUT_TERMINATE
] = "terminate",
5106 [SERVICE_TIMEOUT_ABORT
] = "abort",
5107 [SERVICE_TIMEOUT_KILL
] = "kill",
5110 DEFINE_STRING_TABLE_LOOKUP(service_timeout_failure_mode
, ServiceTimeoutFailureMode
);
5112 const UnitVTable service_vtable
= {
5113 .object_size
= sizeof(Service
),
5114 .exec_context_offset
= offsetof(Service
, exec_context
),
5115 .cgroup_context_offset
= offsetof(Service
, cgroup_context
),
5116 .kill_context_offset
= offsetof(Service
, kill_context
),
5117 .exec_runtime_offset
= offsetof(Service
, exec_runtime
),
5123 .private_section
= "Service",
5125 .can_transient
= true,
5126 .can_delegate
= true,
5128 .can_set_managed_oom
= true,
5130 .init
= service_init
,
5131 .done
= service_done
,
5132 .load
= service_load
,
5133 .release_resources
= service_release_resources
,
5135 .coldplug
= service_coldplug
,
5137 .dump
= service_dump
,
5139 .start
= service_start
,
5140 .stop
= service_stop
,
5141 .reload
= service_reload
,
5143 .can_reload
= service_can_reload
,
5145 .clean
= service_clean
,
5146 .can_clean
= service_can_clean
,
5148 .freeze
= unit_freeze_vtable_common
,
5149 .thaw
= unit_thaw_vtable_common
,
5151 .serialize
= service_serialize
,
5152 .deserialize_item
= service_deserialize_item
,
5154 .active_state
= service_active_state
,
5155 .sub_state_to_string
= service_sub_state_to_string
,
5157 .will_restart
= service_will_restart
,
5159 .may_gc
= service_may_gc
,
5161 .sigchld_event
= service_sigchld_event
,
5163 .reset_failed
= service_reset_failed
,
5165 .notify_cgroup_empty
= service_notify_cgroup_empty_event
,
5166 .notify_cgroup_oom
= service_notify_cgroup_oom_event
,
5167 .notify_message
= service_notify_message
,
5169 .main_pid
= service_main_pid
,
5170 .control_pid
= service_control_pid
,
5172 .bus_name_owner_change
= service_bus_name_owner_change
,
5174 .bus_set_property
= bus_service_set_property
,
5175 .bus_commit_properties
= bus_service_commit_properties
,
5177 .get_timeout
= service_get_timeout
,
5178 .get_timeout_start_usec
= service_get_timeout_start_usec
,
5179 .needs_console
= service_needs_console
,
5180 .exit_status
= service_exit_status
,
5181 .status_text
= service_status_text
,
5183 .status_message_formats
= {
5184 .finished_start_job
= {
5185 [JOB_FAILED
] = "Failed to start %s.",
5187 .finished_stop_job
= {
5188 [JOB_DONE
] = "Stopped %s.",
5189 [JOB_FAILED
] = "Stopped (with error) %s.",
5191 .finished_job
= service_finished_job
,
5194 .can_start
= service_can_start
,
5196 .notify_plymouth
= true,
5198 .audit_start_message_type
= AUDIT_SERVICE_START
,
5199 .audit_stop_message_type
= AUDIT_SERVICE_STOP
,