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_pidref(UNIT(s
), &s
->control_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_pidref(UNIT(s
), &s
->main_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 (pidref_equal(&s
->main_pid
, pidref
) && s
->main_pid_known
) {
201 if (!pidref_equal(&s
->main_pid
, pidref
)) {
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
;
312 /* When the service state is in SERVICE_*_BEFORE_AUTO_RESTART or SERVICE_AUTO_RESTART, we still need
313 * to add 1 to s->n_restarts manually, because s->n_restarts is not updated until a restart job is
314 * enqueued, i.e. state has transitioned to SERVICE_AUTO_RESTART_QUEUED. */
315 n_restarts_next
= s
->n_restarts
+ (s
->state
== SERVICE_AUTO_RESTART_QUEUED
? 0 : 1);
317 if (n_restarts_next
<= 1 ||
318 s
->restart_steps
== 0 ||
319 s
->restart_usec
== 0 ||
320 s
->restart_max_delay_usec
== USEC_INFINITY
||
321 s
->restart_usec
>= s
->restart_max_delay_usec
)
322 return s
->restart_usec
;
324 if (n_restarts_next
> s
->restart_steps
)
325 return 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 return (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 static void service_extend_event_source_timeout(Service
*s
, sd_event_source
*source
, usec_t extended
) {
347 /* Extends the specified event source timer to at least the specified time, unless it is already later
353 r
= sd_event_source_get_time(source
, ¤t
);
356 (void) sd_event_source_get_description(s
->timer_event_source
, &desc
);
357 log_unit_warning_errno(UNIT(s
), r
, "Failed to retrieve timeout time for event source '%s', ignoring: %m", strna(desc
));
361 if (current
>= extended
) /* Current timeout is already longer, ignore this. */
364 r
= sd_event_source_set_time(source
, extended
);
367 (void) sd_event_source_get_description(s
->timer_event_source
, &desc
);
368 log_unit_warning_errno(UNIT(s
), r
, "Failed to set timeout time for event source '%s', ignoring %m", strna(desc
));
372 static void service_extend_timeout(Service
*s
, usec_t extend_timeout_usec
) {
377 if (!timestamp_is_set(extend_timeout_usec
))
380 extended
= usec_add(now(CLOCK_MONOTONIC
), extend_timeout_usec
);
382 service_extend_event_source_timeout(s
, s
->timer_event_source
, extended
);
383 service_extend_event_source_timeout(s
, s
->watchdog_event_source
, extended
);
386 static void service_reset_watchdog(Service
*s
) {
389 dual_timestamp_get(&s
->watchdog_timestamp
);
390 service_start_watchdog(s
);
393 static void service_override_watchdog_timeout(Service
*s
, usec_t watchdog_override_usec
) {
396 s
->watchdog_override_enable
= true;
397 s
->watchdog_override_usec
= watchdog_override_usec
;
398 service_reset_watchdog(s
);
400 log_unit_debug(UNIT(s
), "watchdog_usec="USEC_FMT
, s
->watchdog_usec
);
401 log_unit_debug(UNIT(s
), "watchdog_override_usec="USEC_FMT
, s
->watchdog_override_usec
);
404 static ServiceFDStore
* service_fd_store_unlink(ServiceFDStore
*fs
) {
409 assert(fs
->service
->n_fd_store
> 0);
410 LIST_REMOVE(fd_store
, fs
->service
->fd_store
, fs
);
411 fs
->service
->n_fd_store
--;
414 sd_event_source_disable_unref(fs
->event_source
);
417 asynchronous_close(fs
->fd
);
421 DEFINE_TRIVIAL_CLEANUP_FUNC(ServiceFDStore
*, service_fd_store_unlink
);
423 static void service_release_fd_store(Service
*s
) {
429 log_unit_debug(UNIT(s
), "Releasing all stored fds");
432 service_fd_store_unlink(s
->fd_store
);
434 assert(s
->n_fd_store
== 0);
437 static void service_release_stdio_fd(Service
*s
) {
440 if (s
->stdin_fd
< 0 && s
->stdout_fd
< 0 && s
->stdout_fd
< 0)
443 log_unit_debug(UNIT(s
), "Releasing stdin/stdout/stderr file descriptors.");
445 s
->stdin_fd
= asynchronous_close(s
->stdin_fd
);
446 s
->stdout_fd
= asynchronous_close(s
->stdout_fd
);
447 s
->stderr_fd
= asynchronous_close(s
->stderr_fd
);
449 static void service_done(Unit
*u
) {
450 Service
*s
= SERVICE(u
);
454 open_file_free_many(&s
->open_files
);
456 s
->pid_file
= mfree(s
->pid_file
);
457 s
->status_text
= mfree(s
->status_text
);
459 s
->exec_runtime
= exec_runtime_free(s
->exec_runtime
);
460 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
461 s
->control_command
= NULL
;
462 s
->main_command
= NULL
;
464 exit_status_set_free(&s
->restart_prevent_status
);
465 exit_status_set_free(&s
->restart_force_status
);
466 exit_status_set_free(&s
->success_status
);
468 /* This will leak a process, but at least no memory or any of our resources */
469 service_unwatch_main_pid(s
);
470 service_unwatch_control_pid(s
);
471 service_unwatch_pid_file(s
);
474 unit_unwatch_bus_name(u
, s
->bus_name
);
475 s
->bus_name
= mfree(s
->bus_name
);
478 s
->bus_name_owner
= mfree(s
->bus_name_owner
);
480 s
->usb_function_descriptors
= mfree(s
->usb_function_descriptors
);
481 s
->usb_function_strings
= mfree(s
->usb_function_strings
);
483 service_stop_watchdog(s
);
485 s
->timer_event_source
= sd_event_source_disable_unref(s
->timer_event_source
);
486 s
->exec_fd_event_source
= sd_event_source_disable_unref(s
->exec_fd_event_source
);
488 s
->bus_name_pid_lookup_slot
= sd_bus_slot_unref(s
->bus_name_pid_lookup_slot
);
490 service_release_socket_fd(s
);
491 service_release_stdio_fd(s
);
492 service_release_fd_store(s
);
495 static int on_fd_store_io(sd_event_source
*e
, int fd
, uint32_t revents
, void *userdata
) {
496 ServiceFDStore
*fs
= ASSERT_PTR(userdata
);
500 /* If we get either EPOLLHUP or EPOLLERR, it's time to remove this entry from the fd store */
501 log_unit_debug(UNIT(fs
->service
),
502 "Received %s on stored fd %d (%s), closing.",
503 revents
& EPOLLERR
? "EPOLLERR" : "EPOLLHUP",
504 fs
->fd
, strna(fs
->fdname
));
505 service_fd_store_unlink(fs
);
509 static int service_add_fd_store(Service
*s
, int fd_in
, const char *name
, bool do_poll
) {
510 _cleanup_(service_fd_store_unlinkp
) ServiceFDStore
*fs
= NULL
;
511 _cleanup_(asynchronous_closep
) int fd
= ASSERT_FD(fd_in
);
515 /* fd is always consumed even if the function fails. */
519 if (fstat(fd
, &st
) < 0)
522 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
);
524 if (s
->n_fd_store
>= s
->n_fd_store_max
)
525 /* Our store is full. Use this errno rather than E[NM]FILE to distinguish from the case
526 * where systemd itself hits the file limit. */
527 return log_unit_debug_errno(UNIT(s
), SYNTHETIC_ERRNO(EXFULL
), "Hit fd store limit.");
529 LIST_FOREACH(fd_store
, i
, s
->fd_store
) {
530 r
= same_fd(i
->fd
, fd
);
534 log_unit_debug(UNIT(s
), "Suppressing duplicate fd %i in fd store.", fd
);
535 return 0; /* fd already included */
539 fs
= new(ServiceFDStore
, 1);
543 *fs
= (ServiceFDStore
) {
546 .fdname
= strdup(name
?: "stored"),
553 r
= sd_event_add_io(UNIT(s
)->manager
->event
, &fs
->event_source
, fs
->fd
, 0, on_fd_store_io
, fs
);
554 if (r
< 0 && r
!= -EPERM
) /* EPERM indicates fds that aren't pollable, which is OK */
557 (void) sd_event_source_set_description(fs
->event_source
, "service-fd-store");
561 LIST_PREPEND(fd_store
, s
->fd_store
, fs
);
564 log_unit_debug(UNIT(s
), "Added fd %i (%s) to fd store.", fs
->fd
, fs
->fdname
);
567 return 1; /* fd newly stored */
570 static int service_add_fd_store_set(Service
*s
, FDSet
*fds
, const char *name
, bool do_poll
) {
578 fd
= fdset_steal_first(fds
);
582 r
= service_add_fd_store(s
, fd
, name
, do_poll
);
584 return log_unit_warning_errno(UNIT(s
), r
,
585 "Cannot store more fds than FileDescriptorStoreMax=%u, closing remaining.",
588 return log_unit_error_errno(UNIT(s
), r
, "Failed to add fd to store: %m");
594 static void service_remove_fd_store(Service
*s
, const char *name
) {
598 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
599 if (!streq(fs
->fdname
, name
))
602 log_unit_debug(UNIT(s
), "Got explicit request to remove fd %i (%s), closing.", fs
->fd
, name
);
603 service_fd_store_unlink(fs
);
607 static usec_t
service_running_timeout(Service
*s
) {
612 if (s
->runtime_rand_extra_usec
!= 0) {
613 delta
= random_u64_range(s
->runtime_rand_extra_usec
);
614 log_unit_debug(UNIT(s
), "Adding delta of %s sec to timeout", FORMAT_TIMESPAN(delta
, USEC_PER_SEC
));
617 return usec_add(usec_add(UNIT(s
)->active_enter_timestamp
.monotonic
,
618 s
->runtime_max_usec
),
622 static int service_arm_timer(Service
*s
, bool relative
, usec_t usec
) {
625 return unit_arm_timer(UNIT(s
), &s
->timer_event_source
, relative
, usec
, service_dispatch_timer
);
628 static int service_verify(Service
*s
) {
630 assert(UNIT(s
)->load_state
== UNIT_LOADED
);
632 for (ServiceExecCommand c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++)
633 LIST_FOREACH(command
, command
, s
->exec_command
[c
]) {
634 if (!path_is_absolute(command
->path
) && !filename_is_valid(command
->path
))
635 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
),
636 "Service %s= binary path \"%s\" is neither a valid executable name nor an absolute path. Refusing.",
638 service_exec_command_to_string(c
));
639 if (strv_isempty(command
->argv
))
640 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
),
641 "Service has an empty argv in %s=. Refusing.",
642 service_exec_command_to_string(c
));
645 if (!s
->exec_command
[SERVICE_EXEC_START
] && !s
->exec_command
[SERVICE_EXEC_STOP
] &&
646 UNIT(s
)->success_action
== EMERGENCY_ACTION_NONE
)
647 /* FailureAction= only makes sense if one of the start or stop commands is specified.
648 * SuccessAction= will be executed unconditionally if no commands are specified. Hence,
649 * either a command or SuccessAction= are required. */
651 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "Service has no ExecStart=, ExecStop=, or SuccessAction=. Refusing.");
653 if (s
->type
!= SERVICE_ONESHOT
&& !s
->exec_command
[SERVICE_EXEC_START
])
654 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "Service has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
656 if (!s
->remain_after_exit
&& !s
->exec_command
[SERVICE_EXEC_START
] && UNIT(s
)->success_action
== EMERGENCY_ACTION_NONE
)
657 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.");
659 if (s
->type
!= SERVICE_ONESHOT
&& s
->exec_command
[SERVICE_EXEC_START
]->command_next
)
660 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.");
662 if (s
->type
== SERVICE_ONESHOT
&&
663 !IN_SET(s
->restart
, SERVICE_RESTART_NO
, SERVICE_RESTART_ON_FAILURE
, SERVICE_RESTART_ON_ABNORMAL
, SERVICE_RESTART_ON_WATCHDOG
, SERVICE_RESTART_ON_ABORT
))
664 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.");
666 if (s
->type
== SERVICE_ONESHOT
&& !exit_status_set_is_empty(&s
->restart_force_status
))
667 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "Service has RestartForceExitStatus= set, which isn't allowed for Type=oneshot services. Refusing.");
669 if (s
->type
== SERVICE_ONESHOT
&& s
->exit_type
== SERVICE_EXIT_CGROUP
)
670 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "Service has ExitType=cgroup set, which isn't allowed for Type=oneshot services. Refusing.");
672 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
)
673 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "Service is of type D-Bus but no D-Bus service name has been specified. Refusing.");
675 if (s
->exec_context
.pam_name
&& !IN_SET(s
->kill_context
.kill_mode
, KILL_CONTROL_GROUP
, KILL_MIXED
))
676 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "Service has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing.");
678 if (s
->usb_function_descriptors
&& !s
->usb_function_strings
)
679 log_unit_warning(UNIT(s
), "Service has USBFunctionDescriptors= setting, but no USBFunctionStrings=. Ignoring.");
681 if (!s
->usb_function_descriptors
&& s
->usb_function_strings
)
682 log_unit_warning(UNIT(s
), "Service has USBFunctionStrings= setting, but no USBFunctionDescriptors=. Ignoring.");
684 if (s
->runtime_max_usec
!= USEC_INFINITY
&& s
->type
== SERVICE_ONESHOT
)
685 log_unit_warning(UNIT(s
), "RuntimeMaxSec= has no effect in combination with Type=oneshot. Ignoring.");
687 if (s
->runtime_max_usec
== USEC_INFINITY
&& s
->runtime_rand_extra_usec
!= 0)
688 log_unit_warning(UNIT(s
), "Service has RuntimeRandomizedExtraSec= setting, but no RuntimeMaxSec=. Ignoring.");
690 if (s
->exit_type
== SERVICE_EXIT_CGROUP
&& cg_unified() < CGROUP_UNIFIED_SYSTEMD
)
691 log_unit_warning(UNIT(s
), "Service has ExitType=cgroup set, but we are running with legacy cgroups v1, which might not work correctly. Continuing.");
693 if (s
->restart_max_delay_usec
== USEC_INFINITY
&& s
->restart_steps
> 0)
694 log_unit_warning(UNIT(s
), "Service has RestartSteps= but no RestartMaxDelaySec= setting. Ignoring.");
696 if (s
->restart_max_delay_usec
!= USEC_INFINITY
&& s
->restart_steps
== 0)
697 log_unit_warning(UNIT(s
), "Service has RestartMaxDelaySec= but no RestartSteps= setting. Ignoring.");
699 if (s
->restart_max_delay_usec
< s
->restart_usec
) {
700 log_unit_warning(UNIT(s
), "RestartMaxDelaySec= has a value smaller than RestartSec=, resetting RestartSec= to RestartMaxDelaySec=.");
701 s
->restart_usec
= s
->restart_max_delay_usec
;
707 static int service_add_default_dependencies(Service
*s
) {
712 if (!UNIT(s
)->default_dependencies
)
715 /* Add a number of automatic dependencies useful for the
716 * majority of services. */
718 if (MANAGER_IS_SYSTEM(UNIT(s
)->manager
)) {
719 /* First, pull in the really early boot stuff, and
720 * require it, so that we fail if we can't acquire
723 r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_SYSINIT_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
728 /* In the --user instance there's no sysinit.target,
729 * in that case require basic.target instead. */
731 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
736 /* Second, if the rest of the base system is in the same
737 * transaction, order us after it, but do not pull it in or
738 * even require it. */
739 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, SPECIAL_BASIC_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
743 /* Third, add us in for normal shutdown. */
744 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
747 static void service_fix_stdio(Service
*s
) {
750 /* Note that EXEC_INPUT_NULL and EXEC_OUTPUT_INHERIT play a special role here: they are both the
751 * default value that is subject to automatic overriding triggered by other settings and an explicit
752 * choice the user can make. We don't distinguish between these cases currently. */
754 if (s
->exec_context
.std_input
== EXEC_INPUT_NULL
&&
755 s
->exec_context
.stdin_data_size
> 0)
756 s
->exec_context
.std_input
= EXEC_INPUT_DATA
;
758 if (IN_SET(s
->exec_context
.std_input
,
760 EXEC_INPUT_TTY_FORCE
,
763 EXEC_INPUT_NAMED_FD
))
766 /* We assume these listed inputs refer to bidirectional streams, and hence duplicating them from
767 * stdin to stdout/stderr makes sense and hence leaving EXEC_OUTPUT_INHERIT in place makes sense,
768 * too. Outputs such as regular files or sealed data memfds otoh don't really make sense to be
769 * duplicated for both input and output at the same time (since they then would cause a feedback
770 * loop), hence override EXEC_OUTPUT_INHERIT with the default stderr/stdout setting. */
772 if (s
->exec_context
.std_error
== EXEC_OUTPUT_INHERIT
&&
773 s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
)
774 s
->exec_context
.std_error
= UNIT(s
)->manager
->defaults
.std_error
;
776 if (s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
)
777 s
->exec_context
.std_output
= UNIT(s
)->manager
->defaults
.std_output
;
780 static int service_setup_bus_name(Service
*s
) {
785 /* If s->bus_name is not set, then the unit will be refused by service_verify() later. */
789 if (s
->type
== SERVICE_DBUS
) {
790 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_REQUIRES
, SPECIAL_DBUS_SOCKET
, true, UNIT_DEPENDENCY_FILE
);
792 return log_unit_error_errno(UNIT(s
), r
, "Failed to add dependency on " SPECIAL_DBUS_SOCKET
": %m");
794 /* We always want to be ordered against dbus.socket if both are in the transaction. */
795 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, SPECIAL_DBUS_SOCKET
, true, UNIT_DEPENDENCY_FILE
);
797 return log_unit_error_errno(UNIT(s
), r
, "Failed to add dependency on " SPECIAL_DBUS_SOCKET
": %m");
800 r
= unit_watch_bus_name(UNIT(s
), s
->bus_name
);
802 return log_unit_error_errno(UNIT(s
), r
, "Two services allocated for the same bus name %s, refusing operation.", s
->bus_name
);
804 return log_unit_error_errno(UNIT(s
), r
, "Cannot watch bus name %s: %m", s
->bus_name
);
809 static int service_add_extras(Service
*s
) {
814 if (s
->type
== _SERVICE_TYPE_INVALID
) {
815 /* Figure out a type automatically */
817 s
->type
= SERVICE_DBUS
;
818 else if (s
->exec_command
[SERVICE_EXEC_START
])
819 s
->type
= SERVICE_SIMPLE
;
821 s
->type
= SERVICE_ONESHOT
;
824 /* Oneshot services have disabled start timeout by default */
825 if (s
->type
== SERVICE_ONESHOT
&& !s
->start_timeout_defined
)
826 s
->timeout_start_usec
= USEC_INFINITY
;
828 service_fix_stdio(s
);
830 r
= unit_patch_contexts(UNIT(s
));
834 r
= unit_add_exec_dependencies(UNIT(s
), &s
->exec_context
);
838 r
= unit_set_default_slice(UNIT(s
));
842 /* If the service needs the notify socket, let's enable it automatically. */
843 if (s
->notify_access
== NOTIFY_NONE
&&
844 (IN_SET(s
->type
, SERVICE_NOTIFY
, SERVICE_NOTIFY_RELOAD
) || s
->watchdog_usec
> 0 || s
->n_fd_store_max
> 0))
845 s
->notify_access
= NOTIFY_MAIN
;
847 /* If no OOM policy was explicitly set, then default to the configure default OOM policy. Except when
848 * delegation is on, in that case it we assume the payload knows better what to do and can process
849 * things in a more focused way. */
850 if (s
->oom_policy
< 0)
851 s
->oom_policy
= s
->cgroup_context
.delegate
? OOM_CONTINUE
: UNIT(s
)->manager
->defaults
.oom_policy
;
853 /* Let the kernel do the killing if that's requested. */
854 s
->cgroup_context
.memory_oom_group
= s
->oom_policy
== OOM_KILL
;
856 r
= service_add_default_dependencies(s
);
860 r
= service_setup_bus_name(s
);
867 static int service_load(Unit
*u
) {
868 Service
*s
= SERVICE(u
);
871 r
= unit_load_fragment_and_dropin(u
, true);
875 if (u
->load_state
!= UNIT_LOADED
)
878 /* This is a new unit? Then let's add in some extras */
879 r
= service_add_extras(s
);
883 return service_verify(s
);
886 static void service_dump_fdstore(Service
*s
, FILE *f
, const char *prefix
) {
891 LIST_FOREACH(fd_store
, i
, s
->fd_store
) {
892 _cleanup_free_
char *path
= NULL
;
896 if (fstat(i
->fd
, &st
) < 0) {
897 log_debug_errno(errno
, "Failed to stat fdstore entry: %m");
901 flags
= fcntl(i
->fd
, F_GETFL
);
903 log_debug_errno(errno
, "Failed to get fdstore entry flags: %m");
907 (void) fd_get_path(i
->fd
, &path
);
910 "%s%s '%s' (type=%s; dev=" DEVNUM_FORMAT_STR
"; inode=%" PRIu64
"; rdev=" DEVNUM_FORMAT_STR
"; path=%s; access=%s)\n",
911 prefix
, i
== s
->fd_store
? "File Descriptor Store Entry:" : " ",
913 inode_type_to_string(st
.st_mode
),
914 DEVNUM_FORMAT_VAL(st
.st_dev
),
915 (uint64_t) st
.st_ino
,
916 DEVNUM_FORMAT_VAL(st
.st_rdev
),
918 accmode_to_string(flags
));
922 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
923 Service
*s
= SERVICE(u
);
928 prefix
= strempty(prefix
);
929 prefix2
= strjoina(prefix
, "\t");
932 "%sService State: %s\n"
934 "%sReload Result: %s\n"
935 "%sClean Result: %s\n"
936 "%sPermissionsStartOnly: %s\n"
937 "%sRootDirectoryStartOnly: %s\n"
938 "%sRemainAfterExit: %s\n"
939 "%sGuessMainPID: %s\n"
942 "%sNotifyAccess: %s\n"
943 "%sNotifyState: %s\n"
945 "%sReloadSignal: %s\n",
946 prefix
, service_state_to_string(s
->state
),
947 prefix
, service_result_to_string(s
->result
),
948 prefix
, service_result_to_string(s
->reload_result
),
949 prefix
, service_result_to_string(s
->clean_result
),
950 prefix
, yes_no(s
->permissions_start_only
),
951 prefix
, yes_no(s
->root_directory_start_only
),
952 prefix
, yes_no(s
->remain_after_exit
),
953 prefix
, yes_no(s
->guess_main_pid
),
954 prefix
, service_type_to_string(s
->type
),
955 prefix
, service_restart_to_string(s
->restart
),
956 prefix
, notify_access_to_string(service_get_notify_access(s
)),
957 prefix
, notify_state_to_string(s
->notify_state
),
958 prefix
, oom_policy_to_string(s
->oom_policy
),
959 prefix
, signal_to_string(s
->reload_signal
));
961 if (pidref_is_set(&s
->control_pid
))
963 "%sControl PID: "PID_FMT
"\n",
964 prefix
, s
->control_pid
.pid
);
966 if (pidref_is_set(&s
->main_pid
))
968 "%sMain PID: "PID_FMT
"\n"
969 "%sMain PID Known: %s\n"
970 "%sMain PID Alien: %s\n",
971 prefix
, s
->main_pid
.pid
,
972 prefix
, yes_no(s
->main_pid_known
),
973 prefix
, yes_no(s
->main_pid_alien
));
978 prefix
, s
->pid_file
);
983 "%sBus Name Good: %s\n",
985 prefix
, yes_no(s
->bus_name_good
));
987 if (UNIT_ISSET(s
->accept_socket
))
989 "%sAccept Socket: %s\n",
990 prefix
, UNIT_DEREF(s
->accept_socket
)->id
);
994 "%sRestartSteps: %u\n"
995 "%sRestartMaxDelaySec: %s\n"
996 "%sTimeoutStartSec: %s\n"
997 "%sTimeoutStopSec: %s\n"
998 "%sTimeoutStartFailureMode: %s\n"
999 "%sTimeoutStopFailureMode: %s\n",
1000 prefix
, FORMAT_TIMESPAN(s
->restart_usec
, USEC_PER_SEC
),
1001 prefix
, s
->restart_steps
,
1002 prefix
, FORMAT_TIMESPAN(s
->restart_max_delay_usec
, USEC_PER_SEC
),
1003 prefix
, FORMAT_TIMESPAN(s
->timeout_start_usec
, USEC_PER_SEC
),
1004 prefix
, FORMAT_TIMESPAN(s
->timeout_stop_usec
, USEC_PER_SEC
),
1005 prefix
, service_timeout_failure_mode_to_string(s
->timeout_start_failure_mode
),
1006 prefix
, service_timeout_failure_mode_to_string(s
->timeout_stop_failure_mode
));
1008 if (s
->timeout_abort_set
)
1010 "%sTimeoutAbortSec: %s\n",
1011 prefix
, FORMAT_TIMESPAN(s
->timeout_abort_usec
, USEC_PER_SEC
));
1014 "%sRuntimeMaxSec: %s\n"
1015 "%sRuntimeRandomizedExtraSec: %s\n"
1016 "%sWatchdogSec: %s\n",
1017 prefix
, FORMAT_TIMESPAN(s
->runtime_max_usec
, USEC_PER_SEC
),
1018 prefix
, FORMAT_TIMESPAN(s
->runtime_rand_extra_usec
, USEC_PER_SEC
),
1019 prefix
, FORMAT_TIMESPAN(s
->watchdog_usec
, USEC_PER_SEC
));
1021 kill_context_dump(&s
->kill_context
, f
, prefix
);
1022 exec_context_dump(&s
->exec_context
, f
, prefix
);
1024 for (ServiceExecCommand c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
1025 if (!s
->exec_command
[c
])
1028 fprintf(f
, "%s-> %s:\n",
1029 prefix
, service_exec_command_to_string(c
));
1031 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
1035 fprintf(f
, "%sStatus Text: %s\n",
1036 prefix
, s
->status_text
);
1038 if (s
->n_fd_store_max
> 0)
1040 "%sFile Descriptor Store Max: %u\n"
1041 "%sFile Descriptor Store Pin: %s\n"
1042 "%sFile Descriptor Store Current: %zu\n",
1043 prefix
, s
->n_fd_store_max
,
1044 prefix
, exec_preserve_mode_to_string(s
->fd_store_preserve_mode
),
1045 prefix
, s
->n_fd_store
);
1047 service_dump_fdstore(s
, f
, prefix
);
1050 LIST_FOREACH(open_files
, of
, s
->open_files
) {
1051 _cleanup_free_
char *ofs
= NULL
;
1054 r
= open_file_to_string(of
, &ofs
);
1057 "Failed to convert OpenFile= setting to string, ignoring: %m");
1061 fprintf(f
, "%sOpen File: %s\n", prefix
, ofs
);
1064 cgroup_context_dump(UNIT(s
), f
, prefix
);
1067 static int service_is_suitable_main_pid(Service
*s
, PidRef
*pid
, int prio
) {
1071 assert(pidref_is_set(pid
));
1073 /* Checks whether the specified PID is suitable as main PID for this service. returns negative if not, 0 if the
1074 * PID is questionnable but should be accepted if the source of configuration is trusted. > 0 if the PID is
1077 if (pid
->pid
== getpid_cached() || pid
->pid
== 1)
1078 return log_unit_full_errno(UNIT(s
), prio
, SYNTHETIC_ERRNO(EPERM
), "New main PID "PID_FMT
" is the manager, refusing.", pid
->pid
);
1080 if (pidref_equal(pid
, &s
->control_pid
))
1081 return log_unit_full_errno(UNIT(s
), prio
, SYNTHETIC_ERRNO(EPERM
), "New main PID "PID_FMT
" is the control process, refusing.", pid
->pid
);
1083 if (!pid_is_alive(pid
->pid
))
1084 return log_unit_full_errno(UNIT(s
), prio
, SYNTHETIC_ERRNO(ESRCH
), "New main PID "PID_FMT
" does not exist or is a zombie.", pid
->pid
);
1086 owner
= manager_get_unit_by_pidref(UNIT(s
)->manager
, pid
);
1087 if (owner
== UNIT(s
)) {
1088 log_unit_debug(UNIT(s
), "New main PID "PID_FMT
" belongs to service, we are happy.", pid
->pid
);
1089 return 1; /* Yay, it's definitely a good PID */
1092 return 0; /* Hmm it's a suspicious PID, let's accept it if configuration source is trusted */
1095 static int service_load_pid_file(Service
*s
, bool may_warn
) {
1096 _cleanup_(pidref_done
) PidRef pidref
= PIDREF_NULL
;
1097 bool questionable_pid_file
= false;
1098 _cleanup_free_
char *k
= NULL
;
1099 _cleanup_close_
int fd
= -EBADF
;
1107 prio
= may_warn
? LOG_INFO
: LOG_DEBUG
;
1109 r
= chase(s
->pid_file
, NULL
, CHASE_SAFE
, NULL
, &fd
);
1110 if (r
== -ENOLINK
) {
1111 log_unit_debug_errno(UNIT(s
), r
,
1112 "Potentially unsafe symlink chain, will now retry with relaxed checks: %s", s
->pid_file
);
1114 questionable_pid_file
= true;
1116 r
= chase(s
->pid_file
, NULL
, 0, NULL
, &fd
);
1119 return log_unit_full_errno(UNIT(s
), prio
, r
,
1120 "Can't open PID file %s (yet?) after %s: %m", s
->pid_file
, service_state_to_string(s
->state
));
1122 /* Let's read the PID file now that we chased it down. But we need to convert the O_PATH fd
1123 * chase() returned us into a proper fd first. */
1124 r
= read_one_line_file(FORMAT_PROC_FD_PATH(fd
), &k
);
1126 return log_unit_error_errno(UNIT(s
), r
,
1127 "Can't convert PID files %s O_PATH file descriptor to proper file descriptor: %m",
1130 r
= pidref_set_pidstr(&pidref
, k
);
1132 return log_unit_full_errno(UNIT(s
), prio
, r
, "Failed to parse PID from file %s: %m", s
->pid_file
);
1134 if (s
->main_pid_known
&& pidref_equal(&pidref
, &s
->main_pid
))
1137 r
= service_is_suitable_main_pid(s
, &pidref
, prio
);
1143 if (questionable_pid_file
)
1144 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(EPERM
),
1145 "Refusing to accept PID outside of service control group, acquired through unsafe symlink chain: %s", s
->pid_file
);
1147 /* Hmm, it's not clear if the new main PID is safe. Let's allow this if the PID file is owned by root */
1149 if (fstat(fd
, &st
) < 0)
1150 return log_unit_error_errno(UNIT(s
), errno
, "Failed to fstat() PID file O_PATH fd: %m");
1153 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(EPERM
),
1154 "New main PID "PID_FMT
" does not belong to service, and PID file is not owned by root. Refusing.", pidref
.pid
);
1156 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
);
1159 if (s
->main_pid_known
) {
1160 log_unit_debug(UNIT(s
), "Main PID changing: "PID_FMT
" -> "PID_FMT
, s
->main_pid
.pid
, pidref
.pid
);
1162 service_unwatch_main_pid(s
);
1163 s
->main_pid_known
= false;
1165 log_unit_debug(UNIT(s
), "Main PID loaded: "PID_FMT
, pidref
.pid
);
1167 r
= service_set_main_pidref(s
, &pidref
);
1171 r
= unit_watch_pidref(UNIT(s
), &s
->main_pid
, /* exclusive= */ false);
1172 if (r
< 0) /* FIXME: we need to do something here */
1173 return log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" for service: %m", s
->main_pid
.pid
);
1178 static void service_search_main_pid(Service
*s
) {
1179 _cleanup_(pidref_done
) PidRef pid
= PIDREF_NULL
;
1184 /* If we know it anyway, don't ever fall back to unreliable heuristics */
1185 if (s
->main_pid_known
)
1188 if (!s
->guess_main_pid
)
1191 assert(!pidref_is_set(&s
->main_pid
));
1193 if (unit_search_main_pid(UNIT(s
), &pid
) < 0)
1196 log_unit_debug(UNIT(s
), "Main PID guessed: "PID_FMT
, pid
.pid
);
1197 if (service_set_main_pidref(s
, &pid
) < 0)
1200 r
= unit_watch_pidref(UNIT(s
), &s
->main_pid
, /* exclusive= */ false);
1202 /* FIXME: we need to do something here */
1203 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" from: %m", s
->main_pid
.pid
);
1206 static void service_set_state(Service
*s
, ServiceState state
) {
1207 ServiceState old_state
;
1208 const UnitActiveState
*table
;
1212 if (s
->state
!= state
)
1213 bus_unit_send_pending_change_signal(UNIT(s
), false);
1215 table
= s
->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
1217 old_state
= s
->state
;
1220 service_unwatch_pid_file(s
);
1223 SERVICE_CONDITION
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
1225 SERVICE_RELOAD
, SERVICE_RELOAD_SIGNAL
, SERVICE_RELOAD_NOTIFY
,
1226 SERVICE_STOP
, SERVICE_STOP_WATCHDOG
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1227 SERVICE_FINAL_WATCHDOG
, SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
,
1228 SERVICE_AUTO_RESTART
,
1230 s
->timer_event_source
= sd_event_source_disable_unref(s
->timer_event_source
);
1233 SERVICE_START
, SERVICE_START_POST
,
1235 SERVICE_RELOAD
, SERVICE_RELOAD_SIGNAL
, SERVICE_RELOAD_NOTIFY
,
1236 SERVICE_STOP
, SERVICE_STOP_WATCHDOG
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1237 SERVICE_FINAL_WATCHDOG
, SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
1238 service_unwatch_main_pid(s
);
1239 s
->main_command
= NULL
;
1243 SERVICE_CONDITION
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
1244 SERVICE_RELOAD
, SERVICE_RELOAD_SIGNAL
, SERVICE_RELOAD_NOTIFY
,
1245 SERVICE_STOP
, SERVICE_STOP_WATCHDOG
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1246 SERVICE_FINAL_WATCHDOG
, SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
,
1247 SERVICE_CLEANING
)) {
1248 service_unwatch_control_pid(s
);
1249 s
->control_command
= NULL
;
1250 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1254 SERVICE_DEAD
, SERVICE_FAILED
,
1255 SERVICE_DEAD_BEFORE_AUTO_RESTART
, SERVICE_FAILED_BEFORE_AUTO_RESTART
, SERVICE_AUTO_RESTART
, SERVICE_AUTO_RESTART_QUEUED
,
1256 SERVICE_DEAD_RESOURCES_PINNED
)) {
1257 unit_unwatch_all_pids(UNIT(s
));
1258 unit_dequeue_rewatch_pids(UNIT(s
));
1261 if (state
!= SERVICE_START
)
1262 s
->exec_fd_event_source
= sd_event_source_disable_unref(s
->exec_fd_event_source
);
1264 if (!IN_SET(state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
, SERVICE_RELOAD_SIGNAL
, SERVICE_RELOAD_NOTIFY
))
1265 service_stop_watchdog(s
);
1267 /* For the inactive states unit_notify() will trim the cgroup,
1268 * but for exit we have to do that ourselves... */
1269 if (state
== SERVICE_EXITED
&& !MANAGER_IS_RELOADING(UNIT(s
)->manager
))
1270 unit_prune_cgroup(UNIT(s
));
1272 if (old_state
!= state
)
1273 log_unit_debug(UNIT(s
), "Changed %s -> %s", service_state_to_string(old_state
), service_state_to_string(state
));
1275 unit_notify(UNIT(s
), table
[old_state
], table
[state
], s
->reload_result
== SERVICE_SUCCESS
);
1278 static usec_t
service_coldplug_timeout(Service
*s
) {
1281 switch (s
->deserialized_state
) {
1283 case SERVICE_CONDITION
:
1284 case SERVICE_START_PRE
:
1286 case SERVICE_START_POST
:
1287 case SERVICE_RELOAD
:
1288 case SERVICE_RELOAD_SIGNAL
:
1289 case SERVICE_RELOAD_NOTIFY
:
1290 return usec_add(UNIT(s
)->state_change_timestamp
.monotonic
, s
->timeout_start_usec
);
1292 case SERVICE_RUNNING
:
1293 return service_running_timeout(s
);
1296 case SERVICE_STOP_SIGTERM
:
1297 case SERVICE_STOP_SIGKILL
:
1298 case SERVICE_STOP_POST
:
1299 case SERVICE_FINAL_SIGTERM
:
1300 case SERVICE_FINAL_SIGKILL
:
1301 return usec_add(UNIT(s
)->state_change_timestamp
.monotonic
, s
->timeout_stop_usec
);
1303 case SERVICE_STOP_WATCHDOG
:
1304 case SERVICE_FINAL_WATCHDOG
:
1305 return usec_add(UNIT(s
)->state_change_timestamp
.monotonic
, service_timeout_abort_usec(s
));
1307 case SERVICE_AUTO_RESTART
:
1308 return usec_add(UNIT(s
)->inactive_enter_timestamp
.monotonic
, service_restart_usec_next(s
));
1310 case SERVICE_CLEANING
:
1311 return usec_add(UNIT(s
)->state_change_timestamp
.monotonic
, s
->exec_context
.timeout_clean_usec
);
1314 return USEC_INFINITY
;
1318 static int service_coldplug(Unit
*u
) {
1319 Service
*s
= SERVICE(u
);
1323 assert(s
->state
== SERVICE_DEAD
);
1325 if (s
->deserialized_state
== s
->state
)
1328 r
= service_arm_timer(s
, /* relative= */ false, service_coldplug_timeout(s
));
1332 if (pidref_is_set(&s
->main_pid
) &&
1333 pid_is_unwaited(s
->main_pid
.pid
) &&
1334 (IN_SET(s
->deserialized_state
,
1335 SERVICE_START
, SERVICE_START_POST
,
1337 SERVICE_RELOAD
, SERVICE_RELOAD_SIGNAL
, SERVICE_RELOAD_NOTIFY
,
1338 SERVICE_STOP
, SERVICE_STOP_WATCHDOG
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1339 SERVICE_FINAL_WATCHDOG
, SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))) {
1340 r
= unit_watch_pidref(UNIT(s
), &s
->main_pid
, /* exclusive= */ false);
1345 if (pidref_is_set(&s
->control_pid
) &&
1346 pid_is_unwaited(s
->control_pid
.pid
) &&
1347 IN_SET(s
->deserialized_state
,
1348 SERVICE_CONDITION
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
1349 SERVICE_RELOAD
, SERVICE_RELOAD_SIGNAL
, SERVICE_RELOAD_NOTIFY
,
1350 SERVICE_STOP
, SERVICE_STOP_WATCHDOG
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1351 SERVICE_FINAL_WATCHDOG
, SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
,
1352 SERVICE_CLEANING
)) {
1353 r
= unit_watch_pidref(UNIT(s
), &s
->control_pid
, /* exclusive= */ false);
1358 if (!IN_SET(s
->deserialized_state
,
1359 SERVICE_DEAD
, SERVICE_FAILED
,
1360 SERVICE_DEAD_BEFORE_AUTO_RESTART
, SERVICE_FAILED_BEFORE_AUTO_RESTART
, SERVICE_AUTO_RESTART
, SERVICE_AUTO_RESTART_QUEUED
,
1362 SERVICE_DEAD_RESOURCES_PINNED
)) {
1363 (void) unit_enqueue_rewatch_pids(u
);
1364 (void) unit_setup_exec_runtime(u
);
1367 if (IN_SET(s
->deserialized_state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
, SERVICE_RELOAD_SIGNAL
, SERVICE_RELOAD_NOTIFY
))
1368 service_start_watchdog(s
);
1370 if (UNIT_ISSET(s
->accept_socket
)) {
1371 Socket
* socket
= SOCKET(UNIT_DEREF(s
->accept_socket
));
1373 if (socket
->max_connections_per_source
> 0) {
1376 /* Make a best-effort attempt at bumping the connection count */
1377 if (socket_acquire_peer(socket
, s
->socket_fd
, &peer
) > 0) {
1378 socket_peer_unref(s
->socket_peer
);
1379 s
->socket_peer
= peer
;
1384 service_set_state(s
, s
->deserialized_state
);
1388 static int service_collect_fds(
1392 size_t *n_socket_fds
,
1393 size_t *n_storage_fds
) {
1395 _cleanup_strv_free_
char **rfd_names
= NULL
;
1396 _cleanup_free_
int *rfds
= NULL
;
1397 size_t rn_socket_fds
= 0, rn_storage_fds
= 0;
1403 assert(n_socket_fds
);
1404 assert(n_storage_fds
);
1406 if (s
->socket_fd
>= 0) {
1408 /* Pass the per-connection socket */
1410 rfds
= newdup(int, &s
->socket_fd
, 1);
1414 rfd_names
= strv_new("connection");
1422 /* Pass all our configured sockets for singleton services */
1424 UNIT_FOREACH_DEPENDENCY(u
, UNIT(s
), UNIT_ATOM_TRIGGERED_BY
) {
1425 _cleanup_free_
int *cfds
= NULL
;
1429 if (u
->type
!= UNIT_SOCKET
)
1434 cn_fds
= socket_collect_fds(sock
, &cfds
);
1442 rfds
= TAKE_PTR(cfds
);
1443 rn_socket_fds
= cn_fds
;
1447 t
= reallocarray(rfds
, rn_socket_fds
+ cn_fds
, sizeof(int));
1451 memcpy(t
+ rn_socket_fds
, cfds
, cn_fds
* sizeof(int));
1454 rn_socket_fds
+= cn_fds
;
1457 r
= strv_extend_n(&rfd_names
, socket_fdname(sock
), cn_fds
);
1463 if (s
->n_fd_store
> 0) {
1468 t
= reallocarray(rfds
, rn_socket_fds
+ s
->n_fd_store
, sizeof(int));
1474 nl
= reallocarray(rfd_names
, rn_socket_fds
+ s
->n_fd_store
+ 1, sizeof(char *));
1479 n_fds
= rn_socket_fds
;
1481 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
1482 rfds
[n_fds
] = fs
->fd
;
1483 rfd_names
[n_fds
] = strdup(strempty(fs
->fdname
));
1484 if (!rfd_names
[n_fds
])
1491 rfd_names
[n_fds
] = NULL
;
1494 *fds
= TAKE_PTR(rfds
);
1495 *fd_names
= TAKE_PTR(rfd_names
);
1496 *n_socket_fds
= rn_socket_fds
;
1497 *n_storage_fds
= rn_storage_fds
;
1502 static int service_allocate_exec_fd_event_source(
1505 sd_event_source
**ret_event_source
) {
1507 _cleanup_(sd_event_source_unrefp
) sd_event_source
*source
= NULL
;
1512 assert(ret_event_source
);
1514 r
= sd_event_add_io(UNIT(s
)->manager
->event
, &source
, fd
, 0, service_dispatch_exec_io
, s
);
1516 return log_unit_error_errno(UNIT(s
), r
, "Failed to allocate exec_fd event source: %m");
1518 /* This is a bit lower priority than SIGCHLD, as that carries a lot more interesting failure information */
1520 r
= sd_event_source_set_priority(source
, SD_EVENT_PRIORITY_NORMAL
-3);
1522 return log_unit_error_errno(UNIT(s
), r
, "Failed to adjust priority of exec_fd event source: %m");
1524 (void) sd_event_source_set_description(source
, "service exec_fd");
1526 r
= sd_event_source_set_io_fd_own(source
, true);
1528 return log_unit_error_errno(UNIT(s
), r
, "Failed to pass ownership of fd to event source: %m");
1530 *ret_event_source
= TAKE_PTR(source
);
1534 static int service_allocate_exec_fd(
1536 sd_event_source
**ret_event_source
,
1539 _cleanup_close_pair_
int p
[] = PIPE_EBADF
;
1543 assert(ret_event_source
);
1544 assert(ret_exec_fd
);
1546 if (pipe2(p
, O_CLOEXEC
|O_NONBLOCK
) < 0)
1547 return log_unit_error_errno(UNIT(s
), errno
, "Failed to allocate exec_fd pipe: %m");
1549 r
= service_allocate_exec_fd_event_source(s
, p
[0], ret_event_source
);
1554 *ret_exec_fd
= TAKE_FD(p
[1]);
1559 static bool service_exec_needs_notify_socket(Service
*s
, ExecFlags flags
) {
1562 /* Notifications are accepted depending on the process and
1563 * the access setting of the service:
1564 * process: \ access: NONE MAIN EXEC ALL
1565 * main no yes yes yes
1566 * control no no yes yes
1567 * other (forked) no no no yes */
1569 if (flags
& EXEC_IS_CONTROL
)
1570 /* A control process */
1571 return IN_SET(service_get_notify_access(s
), NOTIFY_EXEC
, NOTIFY_ALL
);
1573 /* We only spawn main processes and control processes, so any
1574 * process that is not a control process is a main process */
1575 return service_get_notify_access(s
) != NOTIFY_NONE
;
1578 static Service
*service_get_triggering_service(Service
*s
) {
1579 Unit
*candidate
= NULL
, *other
;
1583 /* Return the service which triggered service 's', this means dependency
1584 * types which include the UNIT_ATOM_ON_{FAILURE,SUCCESS}_OF atoms.
1586 * N.B. if there are multiple services which could trigger 's' via OnFailure=
1587 * or OnSuccess= then we return NULL. This is since we don't know from which
1588 * one to propagate the exit status. */
1590 UNIT_FOREACH_DEPENDENCY(other
, UNIT(s
), UNIT_ATOM_ON_FAILURE_OF
) {
1596 UNIT_FOREACH_DEPENDENCY(other
, UNIT(s
), UNIT_ATOM_ON_SUCCESS_OF
) {
1602 return SERVICE(candidate
);
1605 log_unit_warning(UNIT(s
), "multiple trigger source candidates for exit status propagation (%s, %s), skipping.",
1606 candidate
->id
, other
->id
);
1610 static int service_spawn_internal(
1618 _cleanup_(exec_params_clear
) ExecParameters exec_params
= {
1621 .stdout_fd
= -EBADF
,
1622 .stderr_fd
= -EBADF
,
1625 _cleanup_(sd_event_source_unrefp
) sd_event_source
*exec_fd_source
= NULL
;
1626 _cleanup_strv_free_
char **final_env
= NULL
, **our_env
= NULL
;
1627 _cleanup_(pidref_done
) PidRef pidref
= PIDREF_NULL
;
1637 log_unit_debug(UNIT(s
), "Will spawn child (%s): %s", caller
, c
->path
);
1639 r
= unit_prepare_exec(UNIT(s
)); /* This realizes the cgroup, among other things */
1643 assert(!s
->exec_fd_event_source
);
1645 if (flags
& EXEC_IS_CONTROL
) {
1646 /* If this is a control process, mask the permissions/chroot application if this is requested. */
1647 if (s
->permissions_start_only
)
1648 exec_params
.flags
&= ~EXEC_APPLY_SANDBOXING
;
1649 if (s
->root_directory_start_only
)
1650 exec_params
.flags
&= ~EXEC_APPLY_CHROOT
;
1653 if ((flags
& EXEC_PASS_FDS
) ||
1654 s
->exec_context
.std_input
== EXEC_INPUT_SOCKET
||
1655 s
->exec_context
.std_output
== EXEC_OUTPUT_SOCKET
||
1656 s
->exec_context
.std_error
== EXEC_OUTPUT_SOCKET
) {
1658 r
= service_collect_fds(s
,
1660 &exec_params
.fd_names
,
1661 &exec_params
.n_socket_fds
,
1662 &exec_params
.n_storage_fds
);
1666 exec_params
.open_files
= s
->open_files
;
1668 log_unit_debug(UNIT(s
), "Passing %zu fds to service", exec_params
.n_socket_fds
+ exec_params
.n_storage_fds
);
1671 if (!FLAGS_SET(flags
, EXEC_IS_CONTROL
) && s
->type
== SERVICE_EXEC
) {
1672 r
= service_allocate_exec_fd(s
, &exec_fd_source
, &exec_params
.exec_fd
);
1677 r
= service_arm_timer(s
, /* relative= */ true, timeout
);
1681 our_env
= new0(char*, 13);
1685 if (service_exec_needs_notify_socket(s
, flags
)) {
1686 if (asprintf(our_env
+ n_env
++, "NOTIFY_SOCKET=%s", UNIT(s
)->manager
->notify_socket
) < 0)
1689 exec_params
.notify_socket
= UNIT(s
)->manager
->notify_socket
;
1691 if (s
->n_fd_store_max
> 0)
1692 if (asprintf(our_env
+ n_env
++, "FDSTORE=%u", s
->n_fd_store_max
) < 0)
1696 if (pidref_is_set(&s
->main_pid
))
1697 if (asprintf(our_env
+ n_env
++, "MAINPID="PID_FMT
, s
->main_pid
.pid
) < 0)
1700 if (MANAGER_IS_USER(UNIT(s
)->manager
))
1701 if (asprintf(our_env
+ n_env
++, "MANAGERPID="PID_FMT
, getpid_cached()) < 0)
1705 if (asprintf(our_env
+ n_env
++, "PIDFILE=%s", s
->pid_file
) < 0)
1708 if (s
->socket_fd
>= 0) {
1709 union sockaddr_union sa
;
1710 socklen_t salen
= sizeof(sa
);
1712 /* If this is a per-connection service instance, let's set $REMOTE_ADDR and $REMOTE_PORT to something
1713 * useful. Note that we do this only when we are still connected at this point in time, which we might
1714 * very well not be. Hence we ignore all errors when retrieving peer information (as that might result
1715 * in ENOTCONN), and just use whate we can use. */
1717 if (getpeername(s
->socket_fd
, &sa
.sa
, &salen
) >= 0 &&
1718 IN_SET(sa
.sa
.sa_family
, AF_INET
, AF_INET6
, AF_VSOCK
)) {
1719 _cleanup_free_
char *addr
= NULL
;
1723 r
= sockaddr_pretty(&sa
.sa
, salen
, true, false, &addr
);
1727 t
= strjoin("REMOTE_ADDR=", addr
);
1730 our_env
[n_env
++] = t
;
1732 r
= sockaddr_port(&sa
.sa
, &port
);
1736 if (asprintf(&t
, "REMOTE_PORT=%u", port
) < 0)
1738 our_env
[n_env
++] = t
;
1742 Service
*env_source
= NULL
;
1743 const char *monitor_prefix
;
1744 if (flags
& EXEC_SETENV_RESULT
) {
1746 monitor_prefix
= "";
1747 } else if (flags
& EXEC_SETENV_MONITOR_RESULT
) {
1748 env_source
= service_get_triggering_service(s
);
1749 monitor_prefix
= "MONITOR_";
1753 if (asprintf(our_env
+ n_env
++, "%sSERVICE_RESULT=%s", monitor_prefix
, service_result_to_string(env_source
->result
)) < 0)
1756 if (env_source
->main_exec_status
.pid
> 0 &&
1757 dual_timestamp_is_set(&env_source
->main_exec_status
.exit_timestamp
)) {
1758 if (asprintf(our_env
+ n_env
++, "%sEXIT_CODE=%s", monitor_prefix
, sigchld_code_to_string(env_source
->main_exec_status
.code
)) < 0)
1761 if (env_source
->main_exec_status
.code
== CLD_EXITED
)
1762 r
= asprintf(our_env
+ n_env
++, "%sEXIT_STATUS=%i", monitor_prefix
, env_source
->main_exec_status
.status
);
1764 r
= asprintf(our_env
+ n_env
++, "%sEXIT_STATUS=%s", monitor_prefix
, signal_to_string(env_source
->main_exec_status
.status
));
1770 if (env_source
!= s
) {
1771 if (!sd_id128_is_null(UNIT(env_source
)->invocation_id
)) {
1772 r
= asprintf(our_env
+ n_env
++, "%sINVOCATION_ID=" SD_ID128_FORMAT_STR
,
1773 monitor_prefix
, SD_ID128_FORMAT_VAL(UNIT(env_source
)->invocation_id
));
1778 if (asprintf(our_env
+ n_env
++, "%sUNIT=%s", monitor_prefix
, UNIT(env_source
)->id
) < 0)
1783 if (UNIT(s
)->activation_details
) {
1784 r
= activation_details_append_env(UNIT(s
)->activation_details
, &our_env
);
1787 /* The number of env vars added here can vary, rather than keeping the allocation block in
1788 * sync manually, these functions simply use the strv methods to append to it, so we need
1789 * to update n_env when we are done in case of future usage. */
1793 r
= unit_set_exec_params(UNIT(s
), &exec_params
);
1797 final_env
= strv_env_merge(exec_params
.environment
, our_env
);
1801 /* System D-Bus needs nss-systemd disabled, so that we don't deadlock */
1802 SET_FLAG(exec_params
.flags
, EXEC_NSS_DYNAMIC_BYPASS
,
1803 MANAGER_IS_SYSTEM(UNIT(s
)->manager
) && unit_has_name(UNIT(s
), SPECIAL_DBUS_SERVICE
));
1805 strv_free_and_replace(exec_params
.environment
, final_env
);
1806 exec_params
.watchdog_usec
= service_get_watchdog_usec(s
);
1807 exec_params
.selinux_context_net
= s
->socket_fd_selinux_context_net
;
1808 if (s
->type
== SERVICE_IDLE
)
1809 exec_params
.idle_pipe
= UNIT(s
)->manager
->idle_pipe
;
1810 exec_params
.stdin_fd
= s
->stdin_fd
;
1811 exec_params
.stdout_fd
= s
->stdout_fd
;
1812 exec_params
.stderr_fd
= s
->stderr_fd
;
1814 r
= exec_spawn(UNIT(s
),
1824 s
->exec_fd_event_source
= TAKE_PTR(exec_fd_source
);
1825 s
->exec_fd_hot
= false;
1827 r
= pidref_set_pid(&pidref
, pid
);
1831 r
= unit_watch_pidref(UNIT(s
), &pidref
, /* exclusive= */ true);
1835 *ret_pid
= TAKE_PIDREF(pidref
);
1839 static int main_pid_good(Service
*s
) {
1842 /* Returns 0 if the pid is dead, > 0 if it is good, < 0 if we don't know */
1844 /* If we know the pid file, then let's just check if it is still valid */
1845 if (s
->main_pid_known
) {
1847 /* If it's an alien child let's check if it is still alive ... */
1848 if (s
->main_pid_alien
&& pidref_is_set(&s
->main_pid
))
1849 return pid_is_alive(s
->main_pid
.pid
);
1851 /* .. otherwise assume we'll get a SIGCHLD for it, which we really should wait for to collect
1852 * exit status and code */
1853 return pidref_is_set(&s
->main_pid
);
1856 /* We don't know the pid */
1860 static int control_pid_good(Service
*s
) {
1863 /* Returns 0 if the control PID is dead, > 0 if it is good. We never actually return < 0 here, but in order to
1864 * make this function as similar as possible to main_pid_good() and cgroup_good(), we pretend that < 0 also
1865 * means: we can't figure it out. */
1867 return pidref_is_set(&s
->control_pid
);
1870 static int cgroup_good(Service
*s
) {
1875 /* Returns 0 if the cgroup is empty or doesn't exist, > 0 if it is exists and is populated, < 0 if we can't
1878 if (!UNIT(s
)->cgroup_path
)
1881 r
= cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, UNIT(s
)->cgroup_path
);
1888 static bool service_shall_restart(Service
*s
, const char **reason
) {
1891 /* Don't restart after manual stops */
1892 if (s
->forbid_restart
) {
1893 *reason
= "manual stop";
1897 /* Never restart if this is configured as special exception */
1898 if (exit_status_set_test(&s
->restart_prevent_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
)) {
1899 *reason
= "prevented by exit status";
1903 /* Restart if the exit code/status are configured as restart triggers */
1904 if (exit_status_set_test(&s
->restart_force_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
)) {
1905 *reason
= "forced by exit status";
1909 *reason
= "restart setting";
1910 switch (s
->restart
) {
1912 case SERVICE_RESTART_NO
:
1915 case SERVICE_RESTART_ALWAYS
:
1916 return s
->result
!= SERVICE_SKIP_CONDITION
;
1918 case SERVICE_RESTART_ON_SUCCESS
:
1919 return s
->result
== SERVICE_SUCCESS
;
1921 case SERVICE_RESTART_ON_FAILURE
:
1922 return !IN_SET(s
->result
, SERVICE_SUCCESS
, SERVICE_SKIP_CONDITION
);
1924 case SERVICE_RESTART_ON_ABNORMAL
:
1925 return !IN_SET(s
->result
, SERVICE_SUCCESS
, SERVICE_FAILURE_EXIT_CODE
, SERVICE_SKIP_CONDITION
);
1927 case SERVICE_RESTART_ON_WATCHDOG
:
1928 return s
->result
== SERVICE_FAILURE_WATCHDOG
;
1930 case SERVICE_RESTART_ON_ABORT
:
1931 return IN_SET(s
->result
, SERVICE_FAILURE_SIGNAL
, SERVICE_FAILURE_CORE_DUMP
);
1934 assert_not_reached();
1938 static bool service_will_restart(Unit
*u
) {
1939 Service
*s
= SERVICE(u
);
1943 if (IN_SET(s
->state
, SERVICE_DEAD_BEFORE_AUTO_RESTART
, SERVICE_FAILED_BEFORE_AUTO_RESTART
, SERVICE_AUTO_RESTART
, SERVICE_AUTO_RESTART_QUEUED
))
1946 return unit_will_restart_default(u
);
1949 static ServiceState
service_determine_dead_state(Service
*s
) {
1952 return s
->fd_store
&& s
->fd_store_preserve_mode
== EXEC_PRESERVE_YES
? SERVICE_DEAD_RESOURCES_PINNED
: SERVICE_DEAD
;
1955 static void service_enter_dead(Service
*s
, ServiceResult f
, bool allow_restart
) {
1956 ServiceState end_state
, restart_state
;
1961 /* If there's a stop job queued before we enter the DEAD state, we shouldn't act on Restart=, in order to not
1962 * undo what has already been enqueued. */
1963 if (unit_stop_pending(UNIT(s
)))
1964 allow_restart
= false;
1966 if (s
->result
== SERVICE_SUCCESS
)
1969 if (s
->result
== SERVICE_SUCCESS
) {
1970 unit_log_success(UNIT(s
));
1971 end_state
= service_determine_dead_state(s
);
1972 restart_state
= SERVICE_DEAD_BEFORE_AUTO_RESTART
;
1973 } else if (s
->result
== SERVICE_SKIP_CONDITION
) {
1974 unit_log_skip(UNIT(s
), service_result_to_string(s
->result
));
1975 end_state
= service_determine_dead_state(s
);
1976 restart_state
= SERVICE_DEAD_BEFORE_AUTO_RESTART
;
1978 unit_log_failure(UNIT(s
), service_result_to_string(s
->result
));
1979 end_state
= SERVICE_FAILED
;
1980 restart_state
= SERVICE_FAILED_BEFORE_AUTO_RESTART
;
1982 unit_warn_leftover_processes(UNIT(s
), unit_log_leftover_process_stop
);
1985 log_unit_debug(UNIT(s
), "Service restart not allowed.");
1989 allow_restart
= service_shall_restart(s
, &reason
);
1990 log_unit_debug(UNIT(s
), "Service will %srestart (%s)",
1991 allow_restart
? "" : "not ",
1995 if (allow_restart
) {
1996 usec_t restart_usec_next
;
1998 /* We make two state changes here: one that maps to the high-level UNIT_INACTIVE/UNIT_FAILED
1999 * state (i.e. a state indicating deactivation), and then one that that maps to the
2000 * high-level UNIT_STARTING state (i.e. a state indicating activation). We do this so that
2001 * external software can watch the state changes and see all service failures, even if they
2002 * are only transitionary and followed by an automatic restart. We have fine-grained
2003 * low-level states for this though so that software can distinguish the permanent UNIT_INACTIVE
2004 * state from this transitionary UNIT_INACTIVE state by looking at the low-level states. */
2005 if (s
->restart_mode
!= SERVICE_RESTART_MODE_DIRECT
)
2006 service_set_state(s
, restart_state
);
2008 restart_usec_next
= service_restart_usec_next(s
);
2010 r
= service_arm_timer(s
, /* relative= */ true, restart_usec_next
);
2012 log_unit_warning_errno(UNIT(s
), r
, "Failed to install restart timer: %m");
2013 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, /* allow_restart= */ false);
2017 log_unit_debug(UNIT(s
), "Next restart interval calculated as: %s", FORMAT_TIMESPAN(restart_usec_next
, 0));
2019 service_set_state(s
, SERVICE_AUTO_RESTART
);
2021 service_set_state(s
, end_state
);
2023 /* If we shan't restart, then flush out the restart counter. But don't do that immediately, so that the
2024 * user can still introspect the counter. Do so on the next start. */
2025 s
->flush_n_restarts
= true;
2028 /* The new state is in effect, let's decrease the fd store ref counter again. Let's also re-add us to the GC
2029 * queue, so that the fd store is possibly gc'ed again */
2030 unit_add_to_gc_queue(UNIT(s
));
2032 /* The next restart might not be a manual stop, hence reset the flag indicating manual stops */
2033 s
->forbid_restart
= false;
2035 /* Reset NotifyAccess override */
2036 s
->notify_access_override
= _NOTIFY_ACCESS_INVALID
;
2038 /* We want fresh tmpdirs and ephemeral snapshots in case the service is started again immediately. */
2039 s
->exec_runtime
= exec_runtime_destroy(s
->exec_runtime
);
2041 /* Also, remove the runtime directory */
2042 unit_destroy_runtime_data(UNIT(s
), &s
->exec_context
);
2044 /* Also get rid of the fd store, if that's configured. */
2045 if (s
->fd_store_preserve_mode
== EXEC_PRESERVE_NO
)
2046 service_release_fd_store(s
);
2048 /* Get rid of the IPC bits of the user */
2049 unit_unref_uid_gid(UNIT(s
), true);
2051 /* Try to delete the pid file. At this point it will be
2052 * out-of-date, and some software might be confused by it, so
2053 * let's remove it. */
2055 (void) unlink(s
->pid_file
);
2057 /* Reset TTY ownership if necessary */
2058 exec_context_revert_tty(&s
->exec_context
);
2061 static void service_enter_stop_post(Service
*s
, ServiceResult f
) {
2065 if (s
->result
== SERVICE_SUCCESS
)
2068 service_unwatch_control_pid(s
);
2069 (void) unit_enqueue_rewatch_pids(UNIT(s
));
2071 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
];
2072 if (s
->control_command
) {
2073 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
2074 pidref_done(&s
->control_pid
);
2076 r
= service_spawn(s
,
2078 s
->timeout_stop_usec
,
2079 EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
|EXEC_IS_CONTROL
|EXEC_SETENV_RESULT
|EXEC_CONTROL_CGROUP
,
2082 log_unit_warning_errno(UNIT(s
), r
, "Failed to spawn 'stop-post' task: %m");
2083 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2087 service_set_state(s
, SERVICE_STOP_POST
);
2089 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_SUCCESS
);
2092 static int state_to_kill_operation(Service
*s
, ServiceState state
) {
2095 case SERVICE_STOP_WATCHDOG
:
2096 case SERVICE_FINAL_WATCHDOG
:
2097 return KILL_WATCHDOG
;
2099 case SERVICE_STOP_SIGTERM
:
2100 if (unit_has_job_type(UNIT(s
), JOB_RESTART
))
2101 return KILL_RESTART
;
2104 case SERVICE_FINAL_SIGTERM
:
2105 return KILL_TERMINATE
;
2107 case SERVICE_STOP_SIGKILL
:
2108 case SERVICE_FINAL_SIGKILL
:
2112 return _KILL_OPERATION_INVALID
;
2116 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
) {
2117 int kill_operation
, r
;
2121 if (s
->result
== SERVICE_SUCCESS
)
2124 /* Before sending any signal, make sure we track all members of this cgroup */
2125 (void) unit_watch_all_pids(UNIT(s
));
2127 /* Also, enqueue a job that we recheck all our PIDs a bit later, given that it's likely some processes have
2129 (void) unit_enqueue_rewatch_pids(UNIT(s
));
2131 kill_operation
= state_to_kill_operation(s
, state
);
2132 r
= unit_kill_context(
2140 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
2145 r
= service_arm_timer(s
, /* relative= */ true,
2146 kill_operation
== KILL_WATCHDOG
? service_timeout_abort_usec(s
) : s
->timeout_stop_usec
);
2148 log_unit_warning_errno(UNIT(s
), r
, "Failed to install timer: %m");
2152 service_set_state(s
, state
);
2153 } else if (IN_SET(state
, SERVICE_STOP_WATCHDOG
, SERVICE_STOP_SIGTERM
) && s
->kill_context
.send_sigkill
)
2154 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_SUCCESS
);
2155 else if (IN_SET(state
, SERVICE_STOP_WATCHDOG
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
2156 service_enter_stop_post(s
, SERVICE_SUCCESS
);
2157 else if (IN_SET(state
, SERVICE_FINAL_WATCHDOG
, SERVICE_FINAL_SIGTERM
) && s
->kill_context
.send_sigkill
)
2158 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_SUCCESS
);
2160 service_enter_dead(s
, SERVICE_SUCCESS
, /* allow_restart= */ true);
2165 if (IN_SET(state
, SERVICE_STOP_WATCHDOG
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
2166 service_enter_stop_post(s
, SERVICE_FAILURE_RESOURCES
);
2168 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, /* allow_restart= */ true);
2171 static void service_enter_stop_by_notify(Service
*s
) {
2176 (void) unit_enqueue_rewatch_pids(UNIT(s
));
2178 r
= service_arm_timer(s
, /* relative= */ true, s
->timeout_stop_usec
);
2180 log_unit_warning_errno(UNIT(s
), r
, "Failed to install timer: %m");
2181 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2185 /* The service told us it's stopping, so it's as if we SIGTERM'd it. */
2186 service_set_state(s
, SERVICE_STOP_SIGTERM
);
2189 static void service_enter_stop(Service
*s
, ServiceResult f
) {
2194 if (s
->result
== SERVICE_SUCCESS
)
2197 service_unwatch_control_pid(s
);
2198 (void) unit_enqueue_rewatch_pids(UNIT(s
));
2200 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
];
2201 if (s
->control_command
) {
2202 s
->control_command_id
= SERVICE_EXEC_STOP
;
2203 pidref_done(&s
->control_pid
);
2205 r
= service_spawn(s
,
2207 s
->timeout_stop_usec
,
2208 EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_IS_CONTROL
|EXEC_SETENV_RESULT
|EXEC_CONTROL_CGROUP
,
2211 log_unit_warning_errno(UNIT(s
), r
, "Failed to spawn 'stop' task: %m");
2212 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2216 service_set_state(s
, SERVICE_STOP
);
2218 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
2221 static bool service_good(Service
*s
) {
2225 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name_good
)
2228 main_pid_ok
= main_pid_good(s
);
2229 if (main_pid_ok
> 0) /* It's alive */
2231 if (main_pid_ok
== 0 && s
->exit_type
== SERVICE_EXIT_MAIN
) /* It's dead */
2234 /* OK, we don't know anything about the main PID, maybe
2235 * because there is none. Let's check the control group
2238 return cgroup_good(s
) != 0;
2241 static void service_enter_running(Service
*s
, ServiceResult f
) {
2246 if (s
->result
== SERVICE_SUCCESS
)
2249 service_unwatch_control_pid(s
);
2251 if (s
->result
!= SERVICE_SUCCESS
)
2252 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
2253 else if (service_good(s
)) {
2255 /* If there are any queued up sd_notify() notifications, process them now */
2256 if (s
->notify_state
== NOTIFY_RELOADING
)
2257 service_enter_reload_by_notify(s
);
2258 else if (s
->notify_state
== NOTIFY_STOPPING
)
2259 service_enter_stop_by_notify(s
);
2261 service_set_state(s
, SERVICE_RUNNING
);
2263 r
= service_arm_timer(s
, /* relative= */ false, service_running_timeout(s
));
2265 log_unit_warning_errno(UNIT(s
), r
, "Failed to install timer: %m");
2266 service_enter_running(s
, SERVICE_FAILURE_RESOURCES
);
2271 } else if (s
->remain_after_exit
)
2272 service_set_state(s
, SERVICE_EXITED
);
2274 service_enter_stop(s
, SERVICE_SUCCESS
);
2277 static void service_enter_start_post(Service
*s
) {
2281 service_unwatch_control_pid(s
);
2282 service_reset_watchdog(s
);
2284 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
];
2285 if (s
->control_command
) {
2286 s
->control_command_id
= SERVICE_EXEC_START_POST
;
2287 pidref_done(&s
->control_pid
);
2289 r
= service_spawn(s
,
2291 s
->timeout_start_usec
,
2292 EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_IS_CONTROL
|EXEC_CONTROL_CGROUP
,
2295 log_unit_warning_errno(UNIT(s
), r
, "Failed to spawn 'start-post' task: %m");
2296 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2300 service_set_state(s
, SERVICE_START_POST
);
2302 service_enter_running(s
, SERVICE_SUCCESS
);
2305 static void service_kill_control_process(Service
*s
) {
2310 if (!pidref_is_set(&s
->control_pid
))
2313 r
= pidref_kill_and_sigcont(&s
->control_pid
, SIGKILL
);
2315 _cleanup_free_
char *comm
= NULL
;
2317 (void) get_process_comm(s
->control_pid
.pid
, &comm
);
2319 log_unit_debug_errno(UNIT(s
), r
, "Failed to kill control process " PID_FMT
" (%s), ignoring: %m",
2320 s
->control_pid
.pid
, strna(comm
));
2324 static int service_adverse_to_leftover_processes(Service
*s
) {
2327 /* KillMode=mixed and control group are used to indicate that all process should be killed off.
2328 * SendSIGKILL= is used for services that require a clean shutdown. These are typically database
2329 * service where a SigKilled process would result in a lengthy recovery and who's shutdown or startup
2330 * time is quite variable (so Timeout settings aren't of use).
2332 * Here we take these two factors and refuse to start a service if there are existing processes
2333 * within a control group. Databases, while generally having some protection against multiple
2334 * instances running, lets not stress the rigor of these. Also ExecStartPre= parts of the service
2335 * aren't as rigoriously written to protect aganst against multiple use. */
2337 if (unit_warn_leftover_processes(UNIT(s
), unit_log_leftover_process_start
) > 0 &&
2338 IN_SET(s
->kill_context
.kill_mode
, KILL_MIXED
, KILL_CONTROL_GROUP
) &&
2339 !s
->kill_context
.send_sigkill
)
2340 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(EBUSY
),
2341 "Will not start SendSIGKILL=no service of type KillMode=control-group or mixed while processes exist");
2346 static void service_enter_start(Service
*s
) {
2347 _cleanup_(pidref_done
) PidRef pidref
= PIDREF_NULL
;
2354 service_unwatch_control_pid(s
);
2355 service_unwatch_main_pid(s
);
2357 r
= service_adverse_to_leftover_processes(s
);
2361 if (s
->type
== SERVICE_FORKING
) {
2362 s
->control_command_id
= SERVICE_EXEC_START
;
2363 c
= s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
2365 s
->main_command
= NULL
;
2367 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
2368 s
->control_command
= NULL
;
2370 c
= s
->main_command
= s
->exec_command
[SERVICE_EXEC_START
];
2374 if (s
->type
!= SERVICE_ONESHOT
) {
2375 /* There's no command line configured for the main command? Hmm, that is strange.
2376 * This can only happen if the configuration changes at runtime. In this case,
2377 * let's enter a failure state. */
2378 r
= log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENXIO
), "There's no 'start' task anymore we could start.");
2382 /* We force a fake state transition here. Otherwise, the unit would go directly from
2383 * SERVICE_DEAD to SERVICE_DEAD without SERVICE_ACTIVATING or SERVICE_ACTIVE
2384 * in between. This way we can later trigger actions that depend on the state
2385 * transition, including SuccessAction=. */
2386 service_set_state(s
, SERVICE_START
);
2388 service_enter_start_post(s
);
2392 if (IN_SET(s
->type
, SERVICE_SIMPLE
, SERVICE_IDLE
))
2393 /* For simple + idle this is the main process. We don't apply any timeout here, but
2394 * service_enter_running() will later apply the .runtime_max_usec timeout. */
2395 timeout
= USEC_INFINITY
;
2397 timeout
= s
->timeout_start_usec
;
2399 r
= service_spawn(s
,
2402 EXEC_PASS_FDS
|EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
|EXEC_SET_WATCHDOG
|EXEC_WRITE_CREDENTIALS
|EXEC_SETENV_MONITOR_RESULT
,
2405 log_unit_warning_errno(UNIT(s
), r
, "Failed to spawn 'start' task: %m");
2409 if (IN_SET(s
->type
, SERVICE_SIMPLE
, SERVICE_IDLE
)) {
2410 /* For simple services we immediately start
2411 * the START_POST binaries. */
2413 (void) service_set_main_pidref(s
, &pidref
);
2414 service_enter_start_post(s
);
2416 } else if (s
->type
== SERVICE_FORKING
) {
2418 /* For forking services we wait until the start
2419 * process exited. */
2421 pidref_done(&s
->control_pid
);
2422 s
->control_pid
= TAKE_PIDREF(pidref
);
2423 service_set_state(s
, SERVICE_START
);
2425 } else if (IN_SET(s
->type
, SERVICE_ONESHOT
, SERVICE_DBUS
, SERVICE_NOTIFY
, SERVICE_NOTIFY_RELOAD
, SERVICE_EXEC
)) {
2427 /* For oneshot services we wait until the start process exited, too, but it is our main process. */
2429 /* For D-Bus services we know the main pid right away, but wait for the bus name to appear on the
2430 * bus. 'notify' and 'exec' services are similar. */
2432 (void) service_set_main_pidref(s
, &pidref
);
2433 service_set_state(s
, SERVICE_START
);
2435 assert_not_reached();
2440 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2443 static void service_enter_start_pre(Service
*s
) {
2448 service_unwatch_control_pid(s
);
2450 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
];
2451 if (s
->control_command
) {
2453 r
= service_adverse_to_leftover_processes(s
);
2457 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
2459 r
= service_spawn(s
,
2461 s
->timeout_start_usec
,
2462 EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_IS_CONTROL
|EXEC_APPLY_TTY_STDIN
|EXEC_SETENV_MONITOR_RESULT
|EXEC_WRITE_CREDENTIALS
,
2465 log_unit_warning_errno(UNIT(s
), r
, "Failed to spawn 'start-pre' task: %m");
2469 service_set_state(s
, SERVICE_START_PRE
);
2471 service_enter_start(s
);
2476 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, /* allow_restart= */ true);
2479 static void service_enter_condition(Service
*s
) {
2484 service_unwatch_control_pid(s
);
2486 s
->control_command
= s
->exec_command
[SERVICE_EXEC_CONDITION
];
2487 if (s
->control_command
) {
2489 r
= service_adverse_to_leftover_processes(s
);
2493 s
->control_command_id
= SERVICE_EXEC_CONDITION
;
2494 pidref_done(&s
->control_pid
);
2496 r
= service_spawn(s
,
2498 s
->timeout_start_usec
,
2499 EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_IS_CONTROL
|EXEC_APPLY_TTY_STDIN
,
2503 log_unit_warning_errno(UNIT(s
), r
, "Failed to spawn 'exec-condition' task: %m");
2507 service_set_state(s
, SERVICE_CONDITION
);
2509 service_enter_start_pre(s
);
2514 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, /* allow_restart= */ true);
2517 static void service_enter_restart(Service
*s
) {
2518 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2523 if (unit_has_job_type(UNIT(s
), JOB_STOP
)) {
2524 /* Don't restart things if we are going down anyway */
2525 log_unit_info(UNIT(s
), "Stop job pending for unit, skipping automatic restart.");
2529 /* Any units that are bound to this service must also be restarted. We use JOB_START for ourselves
2530 * but then set JOB_RESTART_DEPENDENCIES which will enqueue JOB_RESTART for those dependency jobs. */
2531 r
= manager_add_job(UNIT(s
)->manager
, JOB_START
, UNIT(s
), JOB_RESTART_DEPENDENCIES
, NULL
, &error
, NULL
);
2533 log_unit_warning(UNIT(s
), "Failed to schedule restart job: %s", bus_error_message(&error
, r
));
2534 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, /* allow_restart= */ false);
2538 /* Count the jobs we enqueue for restarting. This counter is maintained as long as the unit isn't
2539 * fully stopped, i.e. as long as it remains up or remains in auto-start states. The user can reset
2540 * the counter explicitly however via the usual "systemctl reset-failure" logic. */
2542 s
->flush_n_restarts
= false;
2544 s
->notify_access_override
= _NOTIFY_ACCESS_INVALID
;
2546 log_unit_struct(UNIT(s
), LOG_INFO
,
2547 "MESSAGE_ID=" SD_MESSAGE_UNIT_RESTART_SCHEDULED_STR
,
2548 LOG_UNIT_INVOCATION_ID(UNIT(s
)),
2549 LOG_UNIT_MESSAGE(UNIT(s
),
2550 "Scheduled restart job, restart counter is at %u.", s
->n_restarts
),
2551 "N_RESTARTS=%u", s
->n_restarts
);
2553 service_set_state(s
, SERVICE_AUTO_RESTART_QUEUED
);
2555 /* Notify clients about changed restart counter */
2556 unit_add_to_dbus_queue(UNIT(s
));
2559 static void service_enter_reload_by_notify(Service
*s
) {
2560 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2565 r
= service_arm_timer(s
, /* relative= */ true, s
->timeout_start_usec
);
2567 log_unit_warning_errno(UNIT(s
), r
, "Failed to install timer: %m");
2568 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
2569 service_enter_running(s
, SERVICE_SUCCESS
);
2573 service_set_state(s
, SERVICE_RELOAD_NOTIFY
);
2575 /* service_enter_reload_by_notify is never called during a reload, thus no loops are possible. */
2576 r
= manager_propagate_reload(UNIT(s
)->manager
, UNIT(s
), JOB_FAIL
, &error
);
2578 log_unit_warning(UNIT(s
), "Failed to schedule propagation of reload, ignoring: %s", bus_error_message(&error
, r
));
2581 static void service_enter_reload(Service
*s
) {
2582 bool killed
= false;
2587 service_unwatch_control_pid(s
);
2588 s
->reload_result
= SERVICE_SUCCESS
;
2590 usec_t ts
= now(CLOCK_MONOTONIC
);
2592 if (s
->type
== SERVICE_NOTIFY_RELOAD
&& pidref_is_set(&s
->main_pid
)) {
2593 r
= pidref_kill_and_sigcont(&s
->main_pid
, s
->reload_signal
);
2595 log_unit_warning_errno(UNIT(s
), r
, "Failed to send reload signal: %m");
2602 s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
];
2603 if (s
->control_command
) {
2604 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
2605 pidref_done(&s
->control_pid
);
2607 r
= service_spawn(s
,
2609 s
->timeout_start_usec
,
2610 EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_IS_CONTROL
|EXEC_CONTROL_CGROUP
,
2613 log_unit_warning_errno(UNIT(s
), r
, "Failed to spawn 'reload' task: %m");
2617 service_set_state(s
, SERVICE_RELOAD
);
2618 } else if (killed
) {
2619 r
= service_arm_timer(s
, /* relative= */ true, s
->timeout_start_usec
);
2621 log_unit_warning_errno(UNIT(s
), r
, "Failed to install timer: %m");
2625 service_set_state(s
, SERVICE_RELOAD_SIGNAL
);
2627 service_enter_running(s
, SERVICE_SUCCESS
);
2631 /* Store the timestamp when we started reloading: when reloading via SIGHUP we won't leave the reload
2632 * state until we received both RELOADING=1 and READY=1 with MONOTONIC_USEC= set to a value above
2633 * this. Thus we know for sure the reload cycle was executed *after* we requested it, and is not one
2634 * that was already in progress before. */
2635 s
->reload_begin_usec
= ts
;
2639 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
2640 service_enter_running(s
, SERVICE_SUCCESS
);
2643 static void service_run_next_control(Service
*s
) {
2648 assert(s
->control_command
);
2649 assert(s
->control_command
->command_next
);
2651 assert(s
->control_command_id
!= SERVICE_EXEC_START
);
2653 s
->control_command
= s
->control_command
->command_next
;
2654 service_unwatch_control_pid(s
);
2656 if (IN_SET(s
->state
, SERVICE_CONDITION
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
2657 timeout
= s
->timeout_start_usec
;
2659 timeout
= s
->timeout_stop_usec
;
2661 pidref_done(&s
->control_pid
);
2663 r
= service_spawn(s
,
2666 EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_IS_CONTROL
|
2667 (IN_SET(s
->state
, SERVICE_CONDITION
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
) ? EXEC_WRITE_CREDENTIALS
: 0)|
2668 (IN_SET(s
->control_command_id
, SERVICE_EXEC_CONDITION
, SERVICE_EXEC_START_PRE
, SERVICE_EXEC_STOP_POST
) ? EXEC_APPLY_TTY_STDIN
: 0)|
2669 (IN_SET(s
->control_command_id
, SERVICE_EXEC_STOP
, SERVICE_EXEC_STOP_POST
) ? EXEC_SETENV_RESULT
: 0)|
2670 (IN_SET(s
->control_command_id
, SERVICE_EXEC_START_PRE
, SERVICE_EXEC_START
) ? EXEC_SETENV_MONITOR_RESULT
: 0)|
2671 (IN_SET(s
->control_command_id
, SERVICE_EXEC_START_POST
, SERVICE_EXEC_RELOAD
, SERVICE_EXEC_STOP
, SERVICE_EXEC_STOP_POST
) ? EXEC_CONTROL_CGROUP
: 0),
2674 log_unit_warning_errno(UNIT(s
), r
, "Failed to spawn next control task: %m");
2676 if (IN_SET(s
->state
, SERVICE_CONDITION
, SERVICE_START_PRE
, SERVICE_START_POST
, SERVICE_STOP
))
2677 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2678 else if (s
->state
== SERVICE_STOP_POST
)
2679 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, /* allow_restart= */ true);
2680 else if (s
->state
== SERVICE_RELOAD
) {
2681 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
2682 service_enter_running(s
, SERVICE_SUCCESS
);
2684 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2688 static void service_run_next_main(Service
*s
) {
2689 _cleanup_(pidref_done
) PidRef pidref
= PIDREF_NULL
;
2693 assert(s
->main_command
);
2694 assert(s
->main_command
->command_next
);
2695 assert(s
->type
== SERVICE_ONESHOT
);
2697 s
->main_command
= s
->main_command
->command_next
;
2698 service_unwatch_main_pid(s
);
2700 r
= service_spawn(s
,
2702 s
->timeout_start_usec
,
2703 EXEC_PASS_FDS
|EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
|EXEC_SET_WATCHDOG
|EXEC_SETENV_MONITOR_RESULT
|EXEC_WRITE_CREDENTIALS
,
2706 log_unit_warning_errno(UNIT(s
), r
, "Failed to spawn next main task: %m");
2707 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2711 (void) service_set_main_pidref(s
, &pidref
);
2714 static int service_start(Unit
*u
) {
2715 Service
*s
= SERVICE(u
);
2720 /* We cannot fulfill this request right now, try again later
2722 if (IN_SET(s
->state
,
2723 SERVICE_STOP
, SERVICE_STOP_WATCHDOG
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
2724 SERVICE_FINAL_WATCHDOG
, SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
, SERVICE_CLEANING
))
2727 /* Already on it! */
2728 if (IN_SET(s
->state
, SERVICE_CONDITION
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
))
2731 /* A service that will be restarted must be stopped first to trigger BindsTo and/or OnFailure
2732 * dependencies. If a user does not want to wait for the holdoff time to elapse, the service should
2733 * be manually restarted, not started. We simply return EAGAIN here, so that any start jobs stay
2734 * queued, and assume that the auto restart timer will eventually trigger the restart. */
2735 if (IN_SET(s
->state
, SERVICE_AUTO_RESTART
, SERVICE_DEAD_BEFORE_AUTO_RESTART
, SERVICE_FAILED_BEFORE_AUTO_RESTART
))
2738 assert(IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_DEAD_RESOURCES_PINNED
, SERVICE_AUTO_RESTART_QUEUED
));
2740 r
= unit_acquire_invocation_id(u
);
2744 s
->result
= SERVICE_SUCCESS
;
2745 s
->reload_result
= SERVICE_SUCCESS
;
2746 s
->main_pid_known
= false;
2747 s
->main_pid_alien
= false;
2748 s
->forbid_restart
= false;
2750 s
->status_text
= mfree(s
->status_text
);
2751 s
->status_errno
= 0;
2753 s
->notify_access_override
= _NOTIFY_ACCESS_INVALID
;
2754 s
->notify_state
= NOTIFY_UNKNOWN
;
2756 s
->watchdog_original_usec
= s
->watchdog_usec
;
2757 s
->watchdog_override_enable
= false;
2758 s
->watchdog_override_usec
= USEC_INFINITY
;
2760 exec_command_reset_status_list_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
2761 exec_status_reset(&s
->main_exec_status
);
2763 /* This is not an automatic restart? Flush the restart counter then */
2764 if (s
->flush_n_restarts
) {
2766 s
->flush_n_restarts
= false;
2769 u
->reset_accounting
= true;
2771 service_enter_condition(s
);
2775 static int service_stop(Unit
*u
) {
2776 Service
*s
= SERVICE(u
);
2780 /* Don't create restart jobs from manual stops. */
2781 s
->forbid_restart
= true;
2786 case SERVICE_STOP_SIGTERM
:
2787 case SERVICE_STOP_SIGKILL
:
2788 case SERVICE_STOP_POST
:
2789 case SERVICE_FINAL_WATCHDOG
:
2790 case SERVICE_FINAL_SIGTERM
:
2791 case SERVICE_FINAL_SIGKILL
:
2795 case SERVICE_AUTO_RESTART
:
2796 case SERVICE_AUTO_RESTART_QUEUED
:
2797 /* Give up on the auto restart */
2798 service_set_state(s
, service_determine_dead_state(s
));
2801 case SERVICE_CONDITION
:
2802 case SERVICE_START_PRE
:
2804 case SERVICE_START_POST
:
2805 case SERVICE_RELOAD
:
2806 case SERVICE_RELOAD_SIGNAL
:
2807 case SERVICE_RELOAD_NOTIFY
:
2808 case SERVICE_STOP_WATCHDOG
:
2809 /* If there's already something running we go directly into kill mode. */
2810 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
2813 case SERVICE_CLEANING
:
2814 /* If we are currently cleaning, then abort it, brutally. */
2815 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_SUCCESS
);
2818 case SERVICE_RUNNING
:
2819 case SERVICE_EXITED
:
2820 service_enter_stop(s
, SERVICE_SUCCESS
);
2823 case SERVICE_DEAD_BEFORE_AUTO_RESTART
:
2824 case SERVICE_FAILED_BEFORE_AUTO_RESTART
:
2826 case SERVICE_FAILED
:
2827 case SERVICE_DEAD_RESOURCES_PINNED
:
2829 /* Unknown state, or unit_stop() should already have handled these */
2830 assert_not_reached();
2834 static int service_reload(Unit
*u
) {
2835 Service
*s
= SERVICE(u
);
2839 assert(IN_SET(s
->state
, SERVICE_RUNNING
, SERVICE_EXITED
));
2841 service_enter_reload(s
);
2845 static bool service_can_reload(Unit
*u
) {
2846 Service
*s
= SERVICE(u
);
2850 return s
->exec_command
[SERVICE_EXEC_RELOAD
] ||
2851 s
->type
== SERVICE_NOTIFY_RELOAD
;
2854 static unsigned service_exec_command_index(Unit
*u
, ServiceExecCommand id
, const ExecCommand
*current
) {
2855 Service
*s
= SERVICE(u
);
2860 assert(id
< _SERVICE_EXEC_COMMAND_MAX
);
2862 const ExecCommand
*first
= s
->exec_command
[id
];
2864 /* Figure out where we are in the list by walking back to the beginning */
2865 for (const ExecCommand
*c
= current
; c
!= first
; c
= c
->command_prev
)
2871 static int service_serialize_exec_command(Unit
*u
, FILE *f
, const ExecCommand
*command
) {
2872 _cleanup_free_
char *args
= NULL
, *p
= NULL
;
2873 Service
*s
= SERVICE(u
);
2874 const char *type
, *key
;
2875 ServiceExecCommand id
;
2885 if (command
== s
->control_command
) {
2887 id
= s
->control_command_id
;
2890 id
= SERVICE_EXEC_START
;
2893 idx
= service_exec_command_index(u
, id
, command
);
2895 STRV_FOREACH(arg
, command
->argv
) {
2896 _cleanup_free_
char *e
= NULL
;
2904 if (!GREEDY_REALLOC(args
, length
+ 2 + n
+ 2))
2908 args
[length
++] = ' ';
2910 args
[length
++] = '"';
2911 memcpy(args
+ length
, e
, n
);
2913 args
[length
++] = '"';
2916 if (!GREEDY_REALLOC(args
, length
+ 1))
2921 p
= cescape(command
->path
);
2925 key
= strjoina(type
, "-command");
2927 /* We use '+1234' instead of '1234' to mark the last command in a sequence.
2928 * This is used in service_deserialize_exec_command(). */
2929 (void) serialize_item_format(
2932 service_exec_command_to_string(id
),
2933 command
->command_next
? "" : "+",
2940 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
2941 Service
*s
= SERVICE(u
);
2948 (void) serialize_item(f
, "state", service_state_to_string(s
->state
));
2949 (void) serialize_item(f
, "result", service_result_to_string(s
->result
));
2950 (void) serialize_item(f
, "reload-result", service_result_to_string(s
->reload_result
));
2952 if (pidref_is_set(&s
->control_pid
))
2953 (void) serialize_item_format(f
, "control-pid", PID_FMT
, s
->control_pid
.pid
);
2955 if (s
->main_pid_known
&& pidref_is_set(&s
->main_pid
))
2956 (void) serialize_item_format(f
, "main-pid", PID_FMT
, s
->main_pid
.pid
);
2958 (void) serialize_bool(f
, "main-pid-known", s
->main_pid_known
);
2959 (void) serialize_bool(f
, "bus-name-good", s
->bus_name_good
);
2960 (void) serialize_bool(f
, "bus-name-owner", s
->bus_name_owner
);
2962 (void) serialize_item_format(f
, "n-restarts", "%u", s
->n_restarts
);
2963 (void) serialize_bool(f
, "flush-n-restarts", s
->flush_n_restarts
);
2965 r
= serialize_item_escaped(f
, "status-text", s
->status_text
);
2969 service_serialize_exec_command(u
, f
, s
->control_command
);
2970 service_serialize_exec_command(u
, f
, s
->main_command
);
2972 r
= serialize_fd(f
, fds
, "stdin-fd", s
->stdin_fd
);
2975 r
= serialize_fd(f
, fds
, "stdout-fd", s
->stdout_fd
);
2978 r
= serialize_fd(f
, fds
, "stderr-fd", s
->stderr_fd
);
2982 if (s
->exec_fd_event_source
) {
2983 r
= serialize_fd(f
, fds
, "exec-fd", sd_event_source_get_io_fd(s
->exec_fd_event_source
));
2987 (void) serialize_bool(f
, "exec-fd-hot", s
->exec_fd_hot
);
2990 if (UNIT_ISSET(s
->accept_socket
)) {
2991 r
= serialize_item(f
, "accept-socket", UNIT_DEREF(s
->accept_socket
)->id
);
2996 r
= serialize_fd(f
, fds
, "socket-fd", s
->socket_fd
);
3000 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
3001 _cleanup_free_
char *c
= NULL
;
3004 copy
= fdset_put_dup(fds
, fs
->fd
);
3006 return log_error_errno(copy
, "Failed to copy file descriptor for serialization: %m");
3008 c
= cescape(fs
->fdname
);
3012 (void) serialize_item_format(f
, "fd-store-fd", "%i \"%s\" %i", copy
, c
, fs
->do_poll
);
3015 if (s
->main_exec_status
.pid
> 0) {
3016 (void) serialize_item_format(f
, "main-exec-status-pid", PID_FMT
, s
->main_exec_status
.pid
);
3017 (void) serialize_dual_timestamp(f
, "main-exec-status-start", &s
->main_exec_status
.start_timestamp
);
3018 (void) serialize_dual_timestamp(f
, "main-exec-status-exit", &s
->main_exec_status
.exit_timestamp
);
3020 if (dual_timestamp_is_set(&s
->main_exec_status
.exit_timestamp
)) {
3021 (void) serialize_item_format(f
, "main-exec-status-code", "%i", s
->main_exec_status
.code
);
3022 (void) serialize_item_format(f
, "main-exec-status-status", "%i", s
->main_exec_status
.status
);
3026 if (s
->notify_access_override
>= 0)
3027 (void) serialize_item(f
, "notify-access-override", notify_access_to_string(s
->notify_access_override
));
3029 (void) serialize_dual_timestamp(f
, "watchdog-timestamp", &s
->watchdog_timestamp
);
3030 (void) serialize_bool(f
, "forbid-restart", s
->forbid_restart
);
3032 if (s
->watchdog_override_enable
)
3033 (void) serialize_item_format(f
, "watchdog-override-usec", USEC_FMT
, s
->watchdog_override_usec
);
3035 if (s
->watchdog_original_usec
!= USEC_INFINITY
)
3036 (void) serialize_item_format(f
, "watchdog-original-usec", USEC_FMT
, s
->watchdog_original_usec
);
3038 if (s
->reload_begin_usec
!= USEC_INFINITY
)
3039 (void) serialize_item_format(f
, "reload-begin-usec", USEC_FMT
, s
->reload_begin_usec
);
3044 int service_deserialize_exec_command(
3047 const char *value
) {
3049 Service
*s
= SERVICE(u
);
3051 unsigned idx
= 0, i
;
3052 bool control
, found
= false, last
= false;
3053 ServiceExecCommand id
= _SERVICE_EXEC_COMMAND_INVALID
;
3054 ExecCommand
*command
= NULL
;
3055 _cleanup_free_
char *path
= NULL
;
3056 _cleanup_strv_free_
char **argv
= NULL
;
3058 enum ExecCommandState
{
3059 STATE_EXEC_COMMAND_TYPE
,
3060 STATE_EXEC_COMMAND_INDEX
,
3061 STATE_EXEC_COMMAND_PATH
,
3062 STATE_EXEC_COMMAND_ARGS
,
3063 _STATE_EXEC_COMMAND_MAX
,
3064 _STATE_EXEC_COMMAND_INVALID
= -EINVAL
,
3071 control
= streq(key
, "control-command");
3073 state
= STATE_EXEC_COMMAND_TYPE
;
3076 _cleanup_free_
char *arg
= NULL
;
3078 r
= extract_first_word(&value
, &arg
, NULL
, EXTRACT_CUNESCAPE
| EXTRACT_UNQUOTE
);
3085 case STATE_EXEC_COMMAND_TYPE
:
3086 id
= service_exec_command_from_string(arg
);
3090 state
= STATE_EXEC_COMMAND_INDEX
;
3092 case STATE_EXEC_COMMAND_INDEX
:
3093 /* PID 1234 is serialized as either '1234' or '+1234'. The second form is used to
3094 * mark the last command in a sequence. We warn if the deserialized command doesn't
3095 * match what we have loaded from the unit, but we don't need to warn if that is the
3098 r
= safe_atou(arg
, &idx
);
3101 last
= arg
[0] == '+';
3103 state
= STATE_EXEC_COMMAND_PATH
;
3105 case STATE_EXEC_COMMAND_PATH
:
3106 path
= TAKE_PTR(arg
);
3107 state
= STATE_EXEC_COMMAND_ARGS
;
3109 case STATE_EXEC_COMMAND_ARGS
:
3110 r
= strv_extend(&argv
, arg
);
3115 assert_not_reached();
3119 if (state
!= STATE_EXEC_COMMAND_ARGS
)
3121 if (strv_isempty(argv
))
3122 return -EINVAL
; /* At least argv[0] must be always present. */
3124 /* Let's check whether exec command on given offset matches data that we just deserialized */
3125 for (command
= s
->exec_command
[id
], i
= 0; command
; command
= command
->command_next
, i
++) {
3129 found
= strv_equal(argv
, command
->argv
) && streq(command
->path
, path
);
3134 /* Command at the index we serialized is different, let's look for command that exactly
3135 * matches but is on different index. If there is no such command we will not resume execution. */
3136 for (command
= s
->exec_command
[id
]; command
; command
= command
->command_next
)
3137 if (strv_equal(command
->argv
, argv
) && streq(command
->path
, path
))
3141 if (command
&& control
) {
3142 s
->control_command
= command
;
3143 s
->control_command_id
= id
;
3145 s
->main_command
= command
;
3147 log_unit_debug(u
, "Current command vanished from the unit file.");
3149 log_unit_warning(u
, "Current command vanished from the unit file, execution of the command list won't be resumed.");
3154 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
3155 Service
*s
= SERVICE(u
);
3163 if (streq(key
, "state")) {
3166 state
= service_state_from_string(value
);
3168 log_unit_debug(u
, "Failed to parse state value: %s", value
);
3170 s
->deserialized_state
= state
;
3171 } else if (streq(key
, "result")) {
3174 f
= service_result_from_string(value
);
3176 log_unit_debug(u
, "Failed to parse result value: %s", value
);
3177 else if (f
!= SERVICE_SUCCESS
)
3180 } else if (streq(key
, "reload-result")) {
3183 f
= service_result_from_string(value
);
3185 log_unit_debug(u
, "Failed to parse reload result value: %s", value
);
3186 else if (f
!= SERVICE_SUCCESS
)
3187 s
->reload_result
= f
;
3189 } else if (streq(key
, "control-pid")) {
3190 pidref_done(&s
->control_pid
);
3191 r
= pidref_set_pidstr(&s
->control_pid
, value
);
3193 log_unit_debug_errno(u
, r
, "Failed to initialize control PID '%s' from serialization, ignoring.", value
);
3194 } else if (streq(key
, "main-pid")) {
3197 if (parse_pid(value
, &pid
) < 0)
3198 log_unit_debug(u
, "Failed to parse main-pid value: %s", value
);
3200 (void) service_set_main_pid(s
, pid
);
3201 } else if (streq(key
, "main-pid-known")) {
3204 b
= parse_boolean(value
);
3206 log_unit_debug(u
, "Failed to parse main-pid-known value: %s", value
);
3208 s
->main_pid_known
= b
;
3209 } else if (streq(key
, "bus-name-good")) {
3212 b
= parse_boolean(value
);
3214 log_unit_debug(u
, "Failed to parse bus-name-good value: %s", value
);
3216 s
->bus_name_good
= b
;
3217 } else if (streq(key
, "bus-name-owner")) {
3218 r
= free_and_strdup(&s
->bus_name_owner
, value
);
3220 log_unit_error_errno(u
, r
, "Unable to deserialize current bus owner %s: %m", value
);
3221 } else if (streq(key
, "status-text")) {
3225 l
= cunescape(value
, 0, &t
);
3227 log_unit_debug_errno(u
, l
, "Failed to unescape status text '%s': %m", value
);
3229 free_and_replace(s
->status_text
, t
);
3231 } else if (streq(key
, "accept-socket")) {
3234 if (u
->type
!= UNIT_SOCKET
) {
3235 log_unit_debug(u
, "Failed to deserialize accept-socket: unit is not a socket");
3239 r
= manager_load_unit(u
->manager
, value
, NULL
, NULL
, &socket
);
3241 log_unit_debug_errno(u
, r
, "Failed to load accept-socket unit '%s': %m", value
);
3243 unit_ref_set(&s
->accept_socket
, u
, socket
);
3244 SOCKET(socket
)->n_connections
++;
3247 } else if (streq(key
, "socket-fd")) {
3250 if ((fd
= parse_fd(value
)) < 0 || !fdset_contains(fds
, fd
))
3251 log_unit_debug(u
, "Failed to parse socket-fd value: %s", value
);
3253 asynchronous_close(s
->socket_fd
);
3254 s
->socket_fd
= fdset_remove(fds
, fd
);
3256 } else if (streq(key
, "fd-store-fd")) {
3257 _cleanup_free_
char *fdv
= NULL
, *fdn
= NULL
, *fdp
= NULL
;
3260 r
= extract_first_word(&value
, &fdv
, NULL
, 0);
3261 if (r
<= 0 || (fd
= parse_fd(fdv
)) < 0 || !fdset_contains(fds
, fd
)) {
3262 log_unit_debug(u
, "Failed to parse fd-store-fd value: %s", value
);
3266 r
= extract_first_word(&value
, &fdn
, NULL
, EXTRACT_CUNESCAPE
| EXTRACT_UNQUOTE
);
3268 log_unit_debug(u
, "Failed to parse fd-store-fd value: %s", value
);
3272 r
= extract_first_word(&value
, &fdp
, NULL
, 0);
3274 /* If the value is not present, we assume the default */
3276 } else if (r
< 0 || safe_atoi(fdp
, &do_poll
) < 0) {
3277 log_unit_debug_errno(u
, r
, "Failed to parse fd-store-fd value \"%s\": %m", value
);
3281 r
= fdset_remove(fds
, fd
);
3283 log_unit_error_errno(u
, r
, "Could not find deserialized fd %i in fdset: %m", fd
);
3288 r
= service_add_fd_store(s
, fd
, fdn
, do_poll
);
3290 log_unit_error_errno(u
, r
, "Failed to store deserialized fd %i: %m", fd
);
3293 } else if (streq(key
, "main-exec-status-pid")) {
3296 if (parse_pid(value
, &pid
) < 0)
3297 log_unit_debug(u
, "Failed to parse main-exec-status-pid value: %s", value
);
3299 s
->main_exec_status
.pid
= pid
;
3300 } else if (streq(key
, "main-exec-status-code")) {
3303 if (safe_atoi(value
, &i
) < 0)
3304 log_unit_debug(u
, "Failed to parse main-exec-status-code value: %s", value
);
3306 s
->main_exec_status
.code
= i
;
3307 } else if (streq(key
, "main-exec-status-status")) {
3310 if (safe_atoi(value
, &i
) < 0)
3311 log_unit_debug(u
, "Failed to parse main-exec-status-status value: %s", value
);
3313 s
->main_exec_status
.status
= i
;
3314 } else if (streq(key
, "main-exec-status-start"))
3315 deserialize_dual_timestamp(value
, &s
->main_exec_status
.start_timestamp
);
3316 else if (streq(key
, "main-exec-status-exit"))
3317 deserialize_dual_timestamp(value
, &s
->main_exec_status
.exit_timestamp
);
3318 else if (streq(key
, "notify-access-override")) {
3319 NotifyAccess notify_access
;
3321 notify_access
= notify_access_from_string(value
);
3322 if (notify_access
< 0)
3323 log_unit_debug(u
, "Failed to parse notify-access-override value: %s", value
);
3325 s
->notify_access_override
= notify_access
;
3326 } else if (streq(key
, "watchdog-timestamp"))
3327 deserialize_dual_timestamp(value
, &s
->watchdog_timestamp
);
3328 else if (streq(key
, "forbid-restart")) {
3331 b
= parse_boolean(value
);
3333 log_unit_debug(u
, "Failed to parse forbid-restart value: %s", value
);
3335 s
->forbid_restart
= b
;
3336 } else if (streq(key
, "stdin-fd")) {
3339 if ((fd
= parse_fd(value
)) < 0 || !fdset_contains(fds
, fd
))
3340 log_unit_debug(u
, "Failed to parse stdin-fd value: %s", value
);
3342 asynchronous_close(s
->stdin_fd
);
3343 s
->stdin_fd
= fdset_remove(fds
, fd
);
3344 s
->exec_context
.stdio_as_fds
= true;
3346 } else if (streq(key
, "stdout-fd")) {
3349 if ((fd
= parse_fd(value
)) < 0 || !fdset_contains(fds
, fd
))
3350 log_unit_debug(u
, "Failed to parse stdout-fd value: %s", value
);
3352 asynchronous_close(s
->stdout_fd
);
3353 s
->stdout_fd
= fdset_remove(fds
, fd
);
3354 s
->exec_context
.stdio_as_fds
= true;
3356 } else if (streq(key
, "stderr-fd")) {
3359 if ((fd
= parse_fd(value
)) < 0 || !fdset_contains(fds
, fd
))
3360 log_unit_debug(u
, "Failed to parse stderr-fd value: %s", value
);
3362 asynchronous_close(s
->stderr_fd
);
3363 s
->stderr_fd
= fdset_remove(fds
, fd
);
3364 s
->exec_context
.stdio_as_fds
= true;
3366 } else if (streq(key
, "exec-fd")) {
3369 if ((fd
= parse_fd(value
)) < 0 || !fdset_contains(fds
, fd
))
3370 log_unit_debug(u
, "Failed to parse exec-fd value: %s", value
);
3372 s
->exec_fd_event_source
= sd_event_source_disable_unref(s
->exec_fd_event_source
);
3374 fd
= fdset_remove(fds
, fd
);
3375 if (service_allocate_exec_fd_event_source(s
, fd
, &s
->exec_fd_event_source
) < 0)
3378 } else if (streq(key
, "watchdog-override-usec")) {
3379 if (deserialize_usec(value
, &s
->watchdog_override_usec
) < 0)
3380 log_unit_debug(u
, "Failed to parse watchdog_override_usec value: %s", value
);
3382 s
->watchdog_override_enable
= true;
3384 } else if (streq(key
, "watchdog-original-usec")) {
3385 if (deserialize_usec(value
, &s
->watchdog_original_usec
) < 0)
3386 log_unit_debug(u
, "Failed to parse watchdog_original_usec value: %s", value
);
3388 } else if (STR_IN_SET(key
, "main-command", "control-command")) {
3389 r
= service_deserialize_exec_command(u
, key
, value
);
3391 log_unit_debug_errno(u
, r
, "Failed to parse serialized command \"%s\": %m", value
);
3393 } else if (streq(key
, "n-restarts")) {
3394 r
= safe_atou(value
, &s
->n_restarts
);
3396 log_unit_debug_errno(u
, r
, "Failed to parse serialized restart counter '%s': %m", value
);
3398 } else if (streq(key
, "flush-n-restarts")) {
3399 r
= parse_boolean(value
);
3401 log_unit_debug_errno(u
, r
, "Failed to parse serialized flush restart counter setting '%s': %m", value
);
3403 s
->flush_n_restarts
= r
;
3404 } else if (streq(key
, "reload-begin-usec")) {
3405 r
= deserialize_usec(value
, &s
->reload_begin_usec
);
3407 log_unit_debug_errno(u
, r
, "Failed to parse serialized reload begin timestamp '%s', ignoring: %m", value
);
3409 log_unit_debug(u
, "Unknown serialization key: %s", key
);
3414 static UnitActiveState
service_active_state(Unit
*u
) {
3415 const UnitActiveState
*table
;
3419 table
= SERVICE(u
)->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
3421 return table
[SERVICE(u
)->state
];
3424 static const char *service_sub_state_to_string(Unit
*u
) {
3427 return service_state_to_string(SERVICE(u
)->state
);
3430 static bool service_may_gc(Unit
*u
) {
3431 Service
*s
= SERVICE(u
);
3435 /* Never clean up services that still have a process around, even if the service is formally dead. Note that
3436 * unit_may_gc() already checked our cgroup for us, we just check our two additional PIDs, too, in case they
3437 * have moved outside of the cgroup. */
3439 if (main_pid_good(s
) > 0 ||
3440 control_pid_good(s
) > 0)
3443 /* Only allow collection of actually dead services, i.e. not those that are in the transitionary
3444 * SERVICE_DEAD_BEFORE_AUTO_RESTART/SERVICE_FAILED_BEFORE_AUTO_RESTART states. */
3445 if (!IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_DEAD_RESOURCES_PINNED
))
3451 static int service_retry_pid_file(Service
*s
) {
3454 assert(s
->pid_file
);
3455 assert(IN_SET(s
->state
, SERVICE_START
, SERVICE_START_POST
));
3457 r
= service_load_pid_file(s
, false);
3461 service_unwatch_pid_file(s
);
3463 service_enter_running(s
, SERVICE_SUCCESS
);
3467 static int service_watch_pid_file(Service
*s
) {
3470 log_unit_debug(UNIT(s
), "Setting watch for PID file %s", s
->pid_file_pathspec
->path
);
3472 r
= path_spec_watch(s
->pid_file_pathspec
, service_dispatch_inotify_io
);
3474 log_unit_error_errno(UNIT(s
), r
, "Failed to set a watch for PID file %s: %m", s
->pid_file_pathspec
->path
);
3475 service_unwatch_pid_file(s
);
3479 /* the pidfile might have appeared just before we set the watch */
3480 log_unit_debug(UNIT(s
), "Trying to read PID file %s in case it changed", s
->pid_file_pathspec
->path
);
3481 service_retry_pid_file(s
);
3486 static int service_demand_pid_file(Service
*s
) {
3487 _cleanup_free_ PathSpec
*ps
= NULL
;
3489 assert(s
->pid_file
);
3490 assert(!s
->pid_file_pathspec
);
3492 ps
= new(PathSpec
, 1);
3498 .path
= strdup(s
->pid_file
),
3499 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that keep their PID file
3500 * open all the time. */
3501 .type
= PATH_MODIFIED
,
3502 .inotify_fd
= -EBADF
,
3508 path_simplify(ps
->path
);
3510 s
->pid_file_pathspec
= TAKE_PTR(ps
);
3512 return service_watch_pid_file(s
);
3515 static int service_dispatch_inotify_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
) {
3516 PathSpec
*p
= ASSERT_PTR(userdata
);
3519 s
= SERVICE(p
->unit
);
3523 assert(IN_SET(s
->state
, SERVICE_START
, SERVICE_START_POST
));
3524 assert(s
->pid_file_pathspec
);
3525 assert(path_spec_owns_inotify_fd(s
->pid_file_pathspec
, fd
));
3527 log_unit_debug(UNIT(s
), "inotify event");
3529 if (path_spec_fd_event(p
, events
) < 0)
3532 if (service_retry_pid_file(s
) == 0)
3535 if (service_watch_pid_file(s
) < 0)
3541 service_unwatch_pid_file(s
);
3542 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
3546 static int service_dispatch_exec_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
) {
3547 Service
*s
= SERVICE(userdata
);
3551 log_unit_debug(UNIT(s
), "got exec-fd event");
3553 /* If Type=exec is set, we'll consider a service started successfully the instant we invoked execve()
3554 * successfully for it. We implement this through a pipe() towards the child, which the kernel automatically
3555 * closes for us due to O_CLOEXEC on execve() in the child, which then triggers EOF on the pipe in the
3556 * parent. We need to be careful however, as there are other reasons that we might cause the child's side of
3557 * the pipe to be closed (for example, a simple exit()). To deal with that we'll ignore EOFs on the pipe unless
3558 * the child signalled us first that it is about to call the execve(). It does so by sending us a simple
3559 * non-zero byte via the pipe. We also provide the child with a way to inform us in case execve() failed: if it
3560 * sends a zero byte we'll ignore POLLHUP on the fd again. */
3566 n
= read(fd
, &x
, sizeof(x
));
3568 if (errno
== EAGAIN
) /* O_NONBLOCK in effect → everything queued has now been processed. */
3571 return log_unit_error_errno(UNIT(s
), errno
, "Failed to read from exec_fd: %m");
3573 if (n
== 0) { /* EOF → the event we are waiting for */
3575 s
->exec_fd_event_source
= sd_event_source_disable_unref(s
->exec_fd_event_source
);
3577 if (s
->exec_fd_hot
) { /* Did the child tell us to expect EOF now? */
3578 log_unit_debug(UNIT(s
), "Got EOF on exec-fd");
3580 s
->exec_fd_hot
= false;
3582 /* Nice! This is what we have been waiting for. Transition to next state. */
3583 if (s
->type
== SERVICE_EXEC
&& s
->state
== SERVICE_START
)
3584 service_enter_start_post(s
);
3586 log_unit_debug(UNIT(s
), "Got EOF on exec-fd while it was disabled, ignoring.");
3591 /* A byte was read → this turns on/off the exec fd logic */
3592 assert(n
== sizeof(x
));
3599 static void service_notify_cgroup_empty_event(Unit
*u
) {
3600 Service
*s
= SERVICE(u
);
3604 log_unit_debug(u
, "Control group is empty.");
3608 /* Waiting for SIGCHLD is usually more interesting, because it includes return
3609 * codes/signals. Which is why we ignore the cgroup events for most cases, except when we
3610 * don't know pid which to expect the SIGCHLD for. */
3613 if (IN_SET(s
->type
, SERVICE_NOTIFY
, SERVICE_NOTIFY_RELOAD
) &&
3614 main_pid_good(s
) == 0 &&
3615 control_pid_good(s
) == 0) {
3616 /* No chance of getting a ready notification anymore */
3617 service_enter_stop_post(s
, SERVICE_FAILURE_PROTOCOL
);
3621 if (s
->exit_type
== SERVICE_EXIT_CGROUP
&& main_pid_good(s
) <= 0)
3622 service_enter_start_post(s
);
3625 case SERVICE_START_POST
:
3626 if (s
->pid_file_pathspec
&&
3627 main_pid_good(s
) == 0 &&
3628 control_pid_good(s
) == 0) {
3630 /* Give up hoping for the daemon to write its PID file */
3631 log_unit_warning(u
, "Daemon never wrote its PID file. Failing.");
3633 service_unwatch_pid_file(s
);
3634 if (s
->state
== SERVICE_START
)
3635 service_enter_stop_post(s
, SERVICE_FAILURE_PROTOCOL
);
3637 service_enter_stop(s
, SERVICE_FAILURE_PROTOCOL
);
3641 case SERVICE_RUNNING
:
3642 /* service_enter_running() will figure out what to do */
3643 service_enter_running(s
, SERVICE_SUCCESS
);
3646 case SERVICE_STOP_WATCHDOG
:
3647 case SERVICE_STOP_SIGTERM
:
3648 case SERVICE_STOP_SIGKILL
:
3650 if (main_pid_good(s
) <= 0 && control_pid_good(s
) <= 0)
3651 service_enter_stop_post(s
, SERVICE_SUCCESS
);
3655 case SERVICE_STOP_POST
:
3656 case SERVICE_FINAL_WATCHDOG
:
3657 case SERVICE_FINAL_SIGTERM
:
3658 case SERVICE_FINAL_SIGKILL
:
3659 if (main_pid_good(s
) <= 0 && control_pid_good(s
) <= 0)
3660 service_enter_dead(s
, SERVICE_SUCCESS
, true);
3664 /* If the cgroup empty notification comes when the unit is not active, we must have failed to clean
3665 * up the cgroup earlier and should do it now. */
3666 case SERVICE_AUTO_RESTART
:
3667 case SERVICE_AUTO_RESTART_QUEUED
:
3668 unit_prune_cgroup(u
);
3676 static void service_notify_cgroup_oom_event(Unit
*u
, bool managed_oom
) {
3677 Service
*s
= SERVICE(u
);
3680 log_unit_debug(u
, "Process(es) of control group were killed by systemd-oomd.");
3682 log_unit_debug(u
, "Process of control group was killed by the OOM killer.");
3684 if (s
->oom_policy
== OOM_CONTINUE
)
3689 case SERVICE_CONDITION
:
3690 case SERVICE_START_PRE
:
3692 case SERVICE_START_POST
:
3694 if (s
->oom_policy
== OOM_STOP
)
3695 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_OOM_KILL
);
3696 else if (s
->oom_policy
== OOM_KILL
)
3697 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_OOM_KILL
);
3701 case SERVICE_EXITED
:
3702 case SERVICE_RUNNING
:
3703 if (s
->oom_policy
== OOM_STOP
)
3704 service_enter_stop(s
, SERVICE_FAILURE_OOM_KILL
);
3705 else if (s
->oom_policy
== OOM_KILL
)
3706 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_OOM_KILL
);
3710 case SERVICE_STOP_WATCHDOG
:
3711 case SERVICE_STOP_SIGTERM
:
3712 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_OOM_KILL
);
3715 case SERVICE_STOP_SIGKILL
:
3716 case SERVICE_FINAL_SIGKILL
:
3717 if (s
->result
== SERVICE_SUCCESS
)
3718 s
->result
= SERVICE_FAILURE_OOM_KILL
;
3721 case SERVICE_STOP_POST
:
3722 case SERVICE_FINAL_SIGTERM
:
3723 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_OOM_KILL
);
3731 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
3732 bool notify_dbus
= true;
3733 Service
*s
= SERVICE(u
);
3735 ExitClean clean_mode
;
3740 /* Oneshot services and non-SERVICE_EXEC_START commands should not be
3741 * considered daemons as they are typically not long running. */
3742 if (s
->type
== SERVICE_ONESHOT
|| (s
->control_pid
.pid
== pid
&& s
->control_command_id
!= SERVICE_EXEC_START
))
3743 clean_mode
= EXIT_CLEAN_COMMAND
;
3745 clean_mode
= EXIT_CLEAN_DAEMON
;
3747 if (is_clean_exit(code
, status
, clean_mode
, &s
->success_status
))
3748 f
= SERVICE_SUCCESS
;
3749 else if (code
== CLD_EXITED
)
3750 f
= SERVICE_FAILURE_EXIT_CODE
;
3751 else if (code
== CLD_KILLED
)
3752 f
= SERVICE_FAILURE_SIGNAL
;
3753 else if (code
== CLD_DUMPED
)
3754 f
= SERVICE_FAILURE_CORE_DUMP
;
3756 assert_not_reached();
3758 if (s
->main_pid
.pid
== pid
) {
3759 /* Clean up the exec_fd event source. We want to do this here, not later in
3760 * service_set_state(), because service_enter_stop_post() calls service_spawn().
3761 * The source owns its end of the pipe, so this will close that too. */
3762 s
->exec_fd_event_source
= sd_event_source_disable_unref(s
->exec_fd_event_source
);
3764 /* Forking services may occasionally move to a new PID.
3765 * As long as they update the PID file before exiting the old
3766 * PID, they're fine. */
3767 if (service_load_pid_file(s
, false) > 0)
3770 pidref_done(&s
->main_pid
);
3771 exec_status_exit(&s
->main_exec_status
, &s
->exec_context
, pid
, code
, status
);
3773 if (s
->main_command
) {
3774 /* If this is not a forking service than the
3775 * main process got started and hence we copy
3776 * the exit status so that it is recorded both
3777 * as main and as control process exit
3780 s
->main_command
->exec_status
= s
->main_exec_status
;
3782 if (s
->main_command
->flags
& EXEC_COMMAND_IGNORE_FAILURE
)
3783 f
= SERVICE_SUCCESS
;
3784 } else if (s
->exec_command
[SERVICE_EXEC_START
]) {
3786 /* If this is a forked process, then we should
3787 * ignore the return value if this was
3788 * configured for the starter process */
3790 if (s
->exec_command
[SERVICE_EXEC_START
]->flags
& EXEC_COMMAND_IGNORE_FAILURE
)
3791 f
= SERVICE_SUCCESS
;
3794 unit_log_process_exit(
3797 service_exec_command_to_string(SERVICE_EXEC_START
),
3798 f
== SERVICE_SUCCESS
,
3801 if (s
->result
== SERVICE_SUCCESS
)
3804 if (s
->main_command
&&
3805 s
->main_command
->command_next
&&
3806 s
->type
== SERVICE_ONESHOT
&&
3807 f
== SERVICE_SUCCESS
) {
3809 /* There is another command to execute, so let's do that. */
3811 log_unit_debug(u
, "Running next main command for state %s.", service_state_to_string(s
->state
));
3812 service_run_next_main(s
);
3815 s
->main_command
= NULL
;
3817 /* Services with ExitType=cgroup do not act on main PID exiting, unless the cgroup is
3819 if (s
->exit_type
== SERVICE_EXIT_MAIN
|| cgroup_good(s
) <= 0) {
3820 /* The service exited, so the service is officially gone. */
3823 case SERVICE_START_POST
:
3824 case SERVICE_RELOAD
:
3825 case SERVICE_RELOAD_SIGNAL
:
3826 case SERVICE_RELOAD_NOTIFY
:
3827 /* If neither main nor control processes are running then the current
3828 * state can never exit cleanly, hence immediately terminate the
3830 if (control_pid_good(s
) <= 0)
3831 service_enter_stop(s
, f
);
3833 /* Otherwise need to wait until the operation is done. */
3837 /* Need to wait until the operation is done. */
3841 if (s
->type
== SERVICE_ONESHOT
) {
3842 /* This was our main goal, so let's go on */
3843 if (f
== SERVICE_SUCCESS
)
3844 service_enter_start_post(s
);
3846 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
3848 } else if (IN_SET(s
->type
, SERVICE_NOTIFY
, SERVICE_NOTIFY_RELOAD
)) {
3849 /* Only enter running through a notification, so that the
3850 * SERVICE_START state signifies that no ready notification
3851 * has been received */
3852 if (f
!= SERVICE_SUCCESS
)
3853 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
3854 else if (!s
->remain_after_exit
|| service_get_notify_access(s
) == NOTIFY_MAIN
)
3855 /* The service has never been and will never be active */
3856 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_PROTOCOL
);
3861 case SERVICE_RUNNING
:
3862 service_enter_running(s
, f
);
3865 case SERVICE_STOP_WATCHDOG
:
3866 case SERVICE_STOP_SIGTERM
:
3867 case SERVICE_STOP_SIGKILL
:
3869 if (control_pid_good(s
) <= 0)
3870 service_enter_stop_post(s
, f
);
3872 /* If there is still a control process, wait for that first */
3875 case SERVICE_STOP_POST
:
3877 if (control_pid_good(s
) <= 0)
3878 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
3882 case SERVICE_FINAL_WATCHDOG
:
3883 case SERVICE_FINAL_SIGTERM
:
3884 case SERVICE_FINAL_SIGKILL
:
3886 if (control_pid_good(s
) <= 0)
3887 service_enter_dead(s
, f
, true);
3891 assert_not_reached();
3893 } else if (s
->exit_type
== SERVICE_EXIT_CGROUP
&& s
->state
== SERVICE_START
)
3894 /* If a main process exits very quickly, this function might be executed
3895 * before service_dispatch_exec_io(). Since this function disabled IO events
3896 * to monitor the main process above, we need to update the state here too.
3897 * Let's consider the process is successfully launched and exited. */
3898 service_enter_start_post(s
);
3901 } else if (s
->control_pid
.pid
== pid
) {
3905 pidref_done(&s
->control_pid
);
3907 if (s
->control_command
) {
3908 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
3910 if (s
->control_command
->flags
& EXEC_COMMAND_IGNORE_FAILURE
)
3911 f
= SERVICE_SUCCESS
;
3914 /* ExecCondition= calls that exit with (0, 254] should invoke skip-like behavior instead of failing */
3915 if (s
->state
== SERVICE_CONDITION
) {
3916 if (f
== SERVICE_FAILURE_EXIT_CODE
&& status
< 255) {
3917 UNIT(s
)->condition_result
= false;
3918 f
= SERVICE_SKIP_CONDITION
;
3920 } else if (f
== SERVICE_SUCCESS
) {
3921 UNIT(s
)->condition_result
= true;
3926 kind
= "Condition check process";
3928 kind
= "Control process";
3929 success
= f
== SERVICE_SUCCESS
;
3932 unit_log_process_exit(
3935 service_exec_command_to_string(s
->control_command_id
),
3939 if (s
->state
!= SERVICE_RELOAD
&& s
->result
== SERVICE_SUCCESS
)
3942 if (s
->control_command
&&
3943 s
->control_command
->command_next
&&
3944 f
== SERVICE_SUCCESS
) {
3946 /* There is another command to * execute, so let's do that. */
3948 log_unit_debug(u
, "Running next control command for state %s.", service_state_to_string(s
->state
));
3949 service_run_next_control(s
);
3952 /* No further commands for this step, so let's figure out what to do next */
3954 s
->control_command
= NULL
;
3955 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
3957 log_unit_debug(u
, "Got final SIGCHLD for state %s.", service_state_to_string(s
->state
));
3961 case SERVICE_CONDITION
:
3962 if (f
== SERVICE_SUCCESS
)
3963 service_enter_start_pre(s
);
3965 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
3968 case SERVICE_START_PRE
:
3969 if (f
== SERVICE_SUCCESS
)
3970 service_enter_start(s
);
3972 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
3976 if (s
->type
!= SERVICE_FORKING
)
3977 /* Maybe spurious event due to a reload that changed the type? */
3980 if (f
!= SERVICE_SUCCESS
) {
3981 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
3986 bool has_start_post
;
3989 /* Let's try to load the pid file here if we can.
3990 * The PID file might actually be created by a START_POST
3991 * script. In that case don't worry if the loading fails. */
3993 has_start_post
= s
->exec_command
[SERVICE_EXEC_START_POST
];
3994 r
= service_load_pid_file(s
, !has_start_post
);
3995 if (!has_start_post
&& r
< 0) {
3996 r
= service_demand_pid_file(s
);
3997 if (r
< 0 || cgroup_good(s
) == 0)
3998 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_PROTOCOL
);
4002 service_search_main_pid(s
);
4004 service_enter_start_post(s
);
4007 case SERVICE_START_POST
:
4008 if (f
!= SERVICE_SUCCESS
) {
4009 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
4016 r
= service_load_pid_file(s
, true);
4018 r
= service_demand_pid_file(s
);
4019 if (r
< 0 || cgroup_good(s
) == 0)
4020 service_enter_stop(s
, SERVICE_FAILURE_PROTOCOL
);
4024 service_search_main_pid(s
);
4026 service_enter_running(s
, SERVICE_SUCCESS
);
4029 case SERVICE_RELOAD
:
4030 case SERVICE_RELOAD_SIGNAL
:
4031 case SERVICE_RELOAD_NOTIFY
:
4032 if (f
== SERVICE_SUCCESS
)
4033 if (service_load_pid_file(s
, true) < 0)
4034 service_search_main_pid(s
);
4036 s
->reload_result
= f
;
4038 /* If the last notification we received from the service process indicates
4039 * we are still reloading, then don't leave reloading state just yet, just
4040 * transition into SERVICE_RELOAD_NOTIFY, to wait for the READY=1 coming,
4042 if (s
->notify_state
== NOTIFY_RELOADING
)
4043 service_set_state(s
, SERVICE_RELOAD_NOTIFY
);
4045 service_enter_running(s
, SERVICE_SUCCESS
);
4049 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
4052 case SERVICE_STOP_WATCHDOG
:
4053 case SERVICE_STOP_SIGTERM
:
4054 case SERVICE_STOP_SIGKILL
:
4055 if (main_pid_good(s
) <= 0)
4056 service_enter_stop_post(s
, f
);
4058 /* If there is still a service process around, wait until
4059 * that one quit, too */
4062 case SERVICE_STOP_POST
:
4063 if (main_pid_good(s
) <= 0)
4064 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
4067 case SERVICE_FINAL_WATCHDOG
:
4068 case SERVICE_FINAL_SIGTERM
:
4069 case SERVICE_FINAL_SIGKILL
:
4070 if (main_pid_good(s
) <= 0)
4071 service_enter_dead(s
, f
, true);
4074 case SERVICE_CLEANING
:
4076 if (s
->clean_result
== SERVICE_SUCCESS
)
4077 s
->clean_result
= f
;
4079 service_enter_dead(s
, SERVICE_SUCCESS
, false);
4083 assert_not_reached();
4086 } else /* Neither control nor main PID? If so, don't notify about anything */
4087 notify_dbus
= false;
4089 /* Notify clients about changed exit status */
4091 unit_add_to_dbus_queue(u
);
4093 /* We watch the main/control process otherwise we can't retrieve the unit they
4094 * belong to with cgroupv1. But if they are not our direct child, we won't get a
4095 * SIGCHLD for them. Therefore we need to look for others to watch so we can
4096 * detect when the cgroup becomes empty. Note that the control process is always
4097 * our child so it's pointless to watch all other processes. */
4098 if (!control_pid_good(s
))
4099 if (!s
->main_pid_known
|| s
->main_pid_alien
)
4100 (void) unit_enqueue_rewatch_pids(u
);
4103 static int service_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
4104 Service
*s
= SERVICE(userdata
);
4107 assert(source
== s
->timer_event_source
);
4111 case SERVICE_CONDITION
:
4112 case SERVICE_START_PRE
:
4114 case SERVICE_START_POST
:
4115 switch (s
->timeout_start_failure_mode
) {
4117 case SERVICE_TIMEOUT_TERMINATE
:
4118 log_unit_warning(UNIT(s
), "%s operation timed out. Terminating.", service_state_to_string(s
->state
));
4119 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
4122 case SERVICE_TIMEOUT_ABORT
:
4123 log_unit_warning(UNIT(s
), "%s operation timed out. Aborting.", service_state_to_string(s
->state
));
4124 service_enter_signal(s
, SERVICE_STOP_WATCHDOG
, SERVICE_FAILURE_TIMEOUT
);
4127 case SERVICE_TIMEOUT_KILL
:
4128 if (s
->kill_context
.send_sigkill
) {
4129 log_unit_warning(UNIT(s
), "%s operation timed out. Killing.", service_state_to_string(s
->state
));
4130 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
4132 log_unit_warning(UNIT(s
), "%s operation timed out. Skipping SIGKILL.", service_state_to_string(s
->state
));
4133 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
4138 assert_not_reached();
4142 case SERVICE_RUNNING
:
4143 log_unit_warning(UNIT(s
), "Service reached runtime time limit. Stopping.");
4144 service_enter_stop(s
, SERVICE_FAILURE_TIMEOUT
);
4147 case SERVICE_RELOAD
:
4148 case SERVICE_RELOAD_SIGNAL
:
4149 case SERVICE_RELOAD_NOTIFY
:
4150 log_unit_warning(UNIT(s
), "Reload operation timed out. Killing reload process.");
4151 service_kill_control_process(s
);
4152 s
->reload_result
= SERVICE_FAILURE_TIMEOUT
;
4153 service_enter_running(s
, SERVICE_SUCCESS
);
4157 switch (s
->timeout_stop_failure_mode
) {
4159 case SERVICE_TIMEOUT_TERMINATE
:
4160 log_unit_warning(UNIT(s
), "Stopping timed out. Terminating.");
4161 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
4164 case SERVICE_TIMEOUT_ABORT
:
4165 log_unit_warning(UNIT(s
), "Stopping timed out. Aborting.");
4166 service_enter_signal(s
, SERVICE_STOP_WATCHDOG
, SERVICE_FAILURE_TIMEOUT
);
4169 case SERVICE_TIMEOUT_KILL
:
4170 if (s
->kill_context
.send_sigkill
) {
4171 log_unit_warning(UNIT(s
), "Stopping timed out. Killing.");
4172 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
4174 log_unit_warning(UNIT(s
), "Stopping timed out. Skipping SIGKILL.");
4175 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
4180 assert_not_reached();
4184 case SERVICE_STOP_WATCHDOG
:
4185 if (s
->kill_context
.send_sigkill
) {
4186 log_unit_warning(UNIT(s
), "State 'stop-watchdog' timed out. Killing.");
4187 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
4189 log_unit_warning(UNIT(s
), "State 'stop-watchdog' timed out. Skipping SIGKILL.");
4190 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
4194 case SERVICE_STOP_SIGTERM
:
4195 if (s
->timeout_stop_failure_mode
== SERVICE_TIMEOUT_ABORT
) {
4196 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Aborting.");
4197 service_enter_signal(s
, SERVICE_STOP_WATCHDOG
, SERVICE_FAILURE_TIMEOUT
);
4198 } else if (s
->kill_context
.send_sigkill
) {
4199 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Killing.");
4200 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
4202 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Skipping SIGKILL.");
4203 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
4208 case SERVICE_STOP_SIGKILL
:
4209 /* Uh, we sent a SIGKILL and it is still not gone?
4210 * Must be something we cannot kill, so let's just be
4211 * weirded out and continue */
4213 log_unit_warning(UNIT(s
), "Processes still around after SIGKILL. Ignoring.");
4214 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
4217 case SERVICE_STOP_POST
:
4218 switch (s
->timeout_stop_failure_mode
) {
4220 case SERVICE_TIMEOUT_TERMINATE
:
4221 log_unit_warning(UNIT(s
), "State 'stop-post' timed out. Terminating.");
4222 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
4225 case SERVICE_TIMEOUT_ABORT
:
4226 log_unit_warning(UNIT(s
), "State 'stop-post' timed out. Aborting.");
4227 service_enter_signal(s
, SERVICE_FINAL_WATCHDOG
, SERVICE_FAILURE_TIMEOUT
);
4230 case SERVICE_TIMEOUT_KILL
:
4231 if (s
->kill_context
.send_sigkill
) {
4232 log_unit_warning(UNIT(s
), "State 'stop-post' timed out. Killing.");
4233 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
4235 log_unit_warning(UNIT(s
), "State 'stop-post' timed out. Skipping SIGKILL. Entering failed mode.");
4236 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, false);
4241 assert_not_reached();
4245 case SERVICE_FINAL_WATCHDOG
:
4246 if (s
->kill_context
.send_sigkill
) {
4247 log_unit_warning(UNIT(s
), "State 'final-watchdog' timed out. Killing.");
4248 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
4250 log_unit_warning(UNIT(s
), "State 'final-watchdog' timed out. Skipping SIGKILL. Entering failed mode.");
4251 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, false);
4255 case SERVICE_FINAL_SIGTERM
:
4256 if (s
->timeout_stop_failure_mode
== SERVICE_TIMEOUT_ABORT
) {
4257 log_unit_warning(UNIT(s
), "State 'final-sigterm' timed out. Aborting.");
4258 service_enter_signal(s
, SERVICE_FINAL_WATCHDOG
, SERVICE_FAILURE_TIMEOUT
);
4259 } else if (s
->kill_context
.send_sigkill
) {
4260 log_unit_warning(UNIT(s
), "State 'final-sigterm' timed out. Killing.");
4261 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
4263 log_unit_warning(UNIT(s
), "State 'final-sigterm' timed out. Skipping SIGKILL. Entering failed mode.");
4264 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, false);
4269 case SERVICE_FINAL_SIGKILL
:
4270 log_unit_warning(UNIT(s
), "Processes still around after final SIGKILL. Entering failed mode.");
4271 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, true);
4274 case SERVICE_AUTO_RESTART
:
4275 if (s
->restart_usec
> 0)
4276 log_unit_debug(UNIT(s
),
4277 "Service restart interval %s expired, scheduling restart.",
4278 FORMAT_TIMESPAN(service_restart_usec_next(s
), USEC_PER_SEC
));
4280 log_unit_debug(UNIT(s
),
4281 "Service has no hold-off time (RestartSec=0), scheduling restart.");
4283 service_enter_restart(s
);
4286 case SERVICE_CLEANING
:
4287 log_unit_warning(UNIT(s
), "Cleaning timed out. killing.");
4289 if (s
->clean_result
== SERVICE_SUCCESS
)
4290 s
->clean_result
= SERVICE_FAILURE_TIMEOUT
;
4292 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, 0);
4296 assert_not_reached();
4302 static int service_dispatch_watchdog(sd_event_source
*source
, usec_t usec
, void *userdata
) {
4303 Service
*s
= SERVICE(userdata
);
4304 usec_t watchdog_usec
;
4307 assert(source
== s
->watchdog_event_source
);
4309 watchdog_usec
= service_get_watchdog_usec(s
);
4311 if (UNIT(s
)->manager
->service_watchdogs
) {
4312 log_unit_error(UNIT(s
), "Watchdog timeout (limit %s)!",
4313 FORMAT_TIMESPAN(watchdog_usec
, 1));
4315 service_enter_signal(s
, SERVICE_STOP_WATCHDOG
, SERVICE_FAILURE_WATCHDOG
);
4317 log_unit_warning(UNIT(s
), "Watchdog disabled! Ignoring watchdog timeout (limit %s)!",
4318 FORMAT_TIMESPAN(watchdog_usec
, 1));
4323 static bool service_notify_message_authorized(Service
*s
, pid_t pid
, FDSet
*fds
) {
4326 NotifyAccess notify_access
= service_get_notify_access(s
);
4328 if (notify_access
== NOTIFY_NONE
) {
4329 log_unit_warning(UNIT(s
), "Got notification message from PID "PID_FMT
", but reception is disabled.", pid
);
4333 if (notify_access
== NOTIFY_MAIN
&& pid
!= s
->main_pid
.pid
) {
4334 if (pidref_is_set(&s
->main_pid
))
4335 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
);
4337 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
);
4342 if (notify_access
== NOTIFY_EXEC
&& pid
!= s
->main_pid
.pid
&& pid
!= s
->control_pid
.pid
) {
4343 if (pidref_is_set(&s
->main_pid
) && pidref_is_set(&s
->control_pid
))
4344 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
,
4345 pid
, s
->main_pid
.pid
, s
->control_pid
.pid
);
4346 else if (pidref_is_set(&s
->main_pid
))
4347 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
);
4348 else if (pidref_is_set(&s
->control_pid
))
4349 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
);
4351 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
);
4359 static void service_force_watchdog(Service
*s
) {
4360 if (!UNIT(s
)->manager
->service_watchdogs
)
4363 log_unit_error(UNIT(s
), "Watchdog request (last status: %s)!",
4364 s
->status_text
?: "<unset>");
4366 service_enter_signal(s
, SERVICE_STOP_WATCHDOG
, SERVICE_FAILURE_WATCHDOG
);
4369 static void service_notify_message(
4371 const struct ucred
*ucred
,
4375 Service
*s
= SERVICE(u
);
4376 bool notify_dbus
= false;
4377 usec_t monotonic_usec
= USEC_INFINITY
;
4384 if (!service_notify_message_authorized(s
, ucred
->pid
, fds
))
4387 if (DEBUG_LOGGING
) {
4388 _cleanup_free_
char *cc
= NULL
;
4390 cc
= strv_join(tags
, ", ");
4391 log_unit_debug(u
, "Got notification message from PID "PID_FMT
" (%s)", ucred
->pid
, empty_to_na(cc
));
4394 /* Interpret MAINPID= */
4395 e
= strv_find_startswith(tags
, "MAINPID=");
4396 if (e
&& IN_SET(s
->state
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
, SERVICE_RELOAD_SIGNAL
, SERVICE_RELOAD_NOTIFY
)) {
4397 _cleanup_(pidref_done
) PidRef new_main_pid
= PIDREF_NULL
;
4399 r
= pidref_set_pidstr(&new_main_pid
, e
);
4401 log_unit_warning_errno(u
, r
, "Failed to parse MAINPID=%s field in notification message, ignoring: %m", e
);
4402 else if (!s
->main_pid_known
|| !pidref_equal(&new_main_pid
, &s
->main_pid
)) {
4404 r
= service_is_suitable_main_pid(s
, &new_main_pid
, LOG_WARNING
);
4406 /* The new main PID is a bit suspicious, which is OK if the sender is privileged. */
4408 if (ucred
->uid
== 0) {
4409 log_unit_debug(u
, "New main PID "PID_FMT
" does not belong to service, but we'll accept it as the request to change it came from a privileged process.", new_main_pid
.pid
);
4412 log_unit_debug(u
, "New main PID "PID_FMT
" does not belong to service, refusing.", new_main_pid
.pid
);
4415 (void) service_set_main_pidref(s
, &new_main_pid
);
4417 r
= unit_watch_pidref(UNIT(s
), &s
->main_pid
, /* exclusive= */ false);
4419 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch new main PID "PID_FMT
" for service: %m", s
->main_pid
.pid
);
4426 /* Parse MONOTONIC_USEC= */
4427 e
= strv_find_startswith(tags
, "MONOTONIC_USEC=");
4429 r
= safe_atou64(e
, &monotonic_usec
);
4431 log_unit_warning_errno(u
, r
, "Failed to parse MONOTONIC_USEC= field in notification message, ignoring: %s", e
);
4434 /* Interpret READY=/STOPPING=/RELOADING=. STOPPING= wins over the others, and READY= over RELOADING= */
4435 if (strv_contains(tags
, "STOPPING=1")) {
4436 s
->notify_state
= NOTIFY_STOPPING
;
4438 if (IN_SET(s
->state
, SERVICE_RUNNING
, SERVICE_RELOAD_SIGNAL
, SERVICE_RELOAD_NOTIFY
))
4439 service_enter_stop_by_notify(s
);
4443 } else if (strv_contains(tags
, "READY=1")) {
4445 s
->notify_state
= NOTIFY_READY
;
4447 /* Type=notify services inform us about completed initialization with READY=1 */
4448 if (IN_SET(s
->type
, SERVICE_NOTIFY
, SERVICE_NOTIFY_RELOAD
) &&
4449 s
->state
== SERVICE_START
)
4450 service_enter_start_post(s
);
4452 /* Sending READY=1 while we are reloading informs us that the reloading is complete. */
4453 if (s
->state
== SERVICE_RELOAD_NOTIFY
)
4454 service_enter_running(s
, SERVICE_SUCCESS
);
4456 /* Combined RELOADING=1 and READY=1? Then this is indication that the service started and
4457 * immediately finished reloading. */
4458 if (s
->state
== SERVICE_RELOAD_SIGNAL
&&
4459 strv_contains(tags
, "RELOADING=1") &&
4460 monotonic_usec
!= USEC_INFINITY
&&
4461 monotonic_usec
>= s
->reload_begin_usec
) {
4462 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4464 /* Propagate a reload explicitly */
4465 r
= manager_propagate_reload(UNIT(s
)->manager
, UNIT(s
), JOB_FAIL
, &error
);
4467 log_unit_warning(UNIT(s
), "Failed to schedule propagation of reload, ignoring: %s", bus_error_message(&error
, r
));
4469 service_enter_running(s
, SERVICE_SUCCESS
);
4474 } else if (strv_contains(tags
, "RELOADING=1")) {
4476 s
->notify_state
= NOTIFY_RELOADING
;
4478 /* Sending RELOADING=1 after we send SIGHUP to request a reload will transition
4479 * things to "reload-notify" state, where we'll wait for READY=1 to let us know the
4480 * reload is done. Note that we insist on a timestamp being sent along here, so that
4481 * we know for sure this is a reload cycle initiated *after* we sent the signal */
4482 if (s
->state
== SERVICE_RELOAD_SIGNAL
&&
4483 monotonic_usec
!= USEC_INFINITY
&&
4484 monotonic_usec
>= s
->reload_begin_usec
)
4485 /* Note, we don't call service_enter_reload_by_notify() here, because we
4486 * don't need reload propagation nor do we want to restart the time-out. */
4487 service_set_state(s
, SERVICE_RELOAD_NOTIFY
);
4489 if (s
->state
== SERVICE_RUNNING
)
4490 service_enter_reload_by_notify(s
);
4495 /* Interpret STATUS= */
4496 e
= strv_find_startswith(tags
, "STATUS=");
4498 _cleanup_free_
char *t
= NULL
;
4501 /* Note that this size limit check is mostly paranoia: since the datagram size we are willing
4502 * to process is already limited to NOTIFY_BUFFER_MAX, this limit here should never be hit. */
4503 if (strlen(e
) > STATUS_TEXT_MAX
)
4504 log_unit_warning(u
, "Status message overly long (%zu > %u), ignoring.", strlen(e
), STATUS_TEXT_MAX
);
4505 else if (!utf8_is_valid(e
))
4506 log_unit_warning(u
, "Status message in notification message is not UTF-8 clean, ignoring.");
4514 if (!streq_ptr(s
->status_text
, t
)) {
4515 free_and_replace(s
->status_text
, t
);
4520 /* Interpret NOTIFYACCESS= */
4521 e
= strv_find_startswith(tags
, "NOTIFYACCESS=");
4523 NotifyAccess notify_access
;
4525 notify_access
= notify_access_from_string(e
);
4526 if (notify_access
< 0)
4527 log_unit_warning_errno(u
, notify_access
,
4528 "Failed to parse NOTIFYACCESS= field value '%s' in notification message, ignoring: %m", e
);
4530 /* We don't need to check whether the new access mode is more strict than what is
4531 * already in use, since only the privileged process is allowed to change it
4532 * in the first place. */
4533 if (service_get_notify_access(s
) != notify_access
) {
4534 service_override_notify_access(s
, notify_access
);
4539 /* Interpret ERRNO= */
4540 e
= strv_find_startswith(tags
, "ERRNO=");
4544 status_errno
= parse_errno(e
);
4545 if (status_errno
< 0)
4546 log_unit_warning_errno(u
, status_errno
,
4547 "Failed to parse ERRNO= field value '%s' in notification message: %m", e
);
4548 else if (s
->status_errno
!= status_errno
) {
4549 s
->status_errno
= status_errno
;
4554 /* Interpret EXTEND_TIMEOUT= */
4555 e
= strv_find_startswith(tags
, "EXTEND_TIMEOUT_USEC=");
4557 usec_t extend_timeout_usec
;
4558 if (safe_atou64(e
, &extend_timeout_usec
) < 0)
4559 log_unit_warning(u
, "Failed to parse EXTEND_TIMEOUT_USEC=%s", e
);
4561 service_extend_timeout(s
, extend_timeout_usec
);
4564 /* Interpret WATCHDOG= */
4565 e
= strv_find_startswith(tags
, "WATCHDOG=");
4568 service_reset_watchdog(s
);
4569 else if (streq(e
, "trigger"))
4570 service_force_watchdog(s
);
4572 log_unit_warning(u
, "Passed WATCHDOG= field is invalid, ignoring.");
4575 e
= strv_find_startswith(tags
, "WATCHDOG_USEC=");
4577 usec_t watchdog_override_usec
;
4578 if (safe_atou64(e
, &watchdog_override_usec
) < 0)
4579 log_unit_warning(u
, "Failed to parse WATCHDOG_USEC=%s", e
);
4581 service_override_watchdog_timeout(s
, watchdog_override_usec
);
4584 /* Process FD store messages. Either FDSTOREREMOVE=1 for removal, or FDSTORE=1 for addition. In both cases,
4585 * process FDNAME= for picking the file descriptor name to use. Note that FDNAME= is required when removing
4586 * fds, but optional when pushing in new fds, for compatibility reasons. */
4587 if (strv_contains(tags
, "FDSTOREREMOVE=1")) {
4590 name
= strv_find_startswith(tags
, "FDNAME=");
4591 if (!name
|| !fdname_is_valid(name
))
4592 log_unit_warning(u
, "FDSTOREREMOVE=1 requested, but no valid file descriptor name passed, ignoring.");
4594 service_remove_fd_store(s
, name
);
4596 } else if (strv_contains(tags
, "FDSTORE=1")) {
4599 name
= strv_find_startswith(tags
, "FDNAME=");
4600 if (name
&& !fdname_is_valid(name
)) {
4601 log_unit_warning(u
, "Passed FDNAME= name is invalid, ignoring.");
4605 (void) service_add_fd_store_set(s
, fds
, name
, !strv_contains(tags
, "FDPOLL=0"));
4608 /* Notify clients about changed status or main pid */
4610 unit_add_to_dbus_queue(u
);
4613 static int service_get_timeout(Unit
*u
, usec_t
*timeout
) {
4614 Service
*s
= SERVICE(u
);
4618 if (!s
->timer_event_source
)
4621 r
= sd_event_source_get_time(s
->timer_event_source
, &t
);
4624 if (t
== USEC_INFINITY
)
4631 static usec_t
service_get_timeout_start_usec(Unit
*u
) {
4632 Service
*s
= SERVICE(ASSERT_PTR(u
));
4633 return s
->timeout_start_usec
;
4636 static bool pick_up_pid_from_bus_name(Service
*s
) {
4639 /* If the service is running but we have no main PID yet, get it from the owner of the D-Bus name */
4641 return !pidref_is_set(&s
->main_pid
) &&
4647 SERVICE_RELOAD_SIGNAL
,
4648 SERVICE_RELOAD_NOTIFY
);
4651 static int bus_name_pid_lookup_callback(sd_bus_message
*reply
, void *userdata
, sd_bus_error
*ret_error
) {
4652 _cleanup_(pidref_done
) PidRef pidref
= PIDREF_NULL
;
4653 const sd_bus_error
*e
;
4654 Unit
*u
= ASSERT_PTR(userdata
);
4662 s
->bus_name_pid_lookup_slot
= sd_bus_slot_unref(s
->bus_name_pid_lookup_slot
);
4664 if (!s
->bus_name
|| !pick_up_pid_from_bus_name(s
))
4667 e
= sd_bus_message_get_error(reply
);
4669 r
= sd_bus_error_get_errno(e
);
4670 log_warning_errno(r
, "GetConnectionUnixProcessID() failed: %s", bus_error_message(e
, r
));
4674 r
= sd_bus_message_read(reply
, "u", &pid
);
4676 bus_log_parse_error(r
);
4680 r
= pidref_set_pid(&pidref
, pid
);
4682 log_debug_errno(r
, "GetConnectionUnixProcessID() returned invalid PID: %m");
4686 log_unit_debug(u
, "D-Bus name %s is now owned by process " PID_FMT
, s
->bus_name
, pidref
.pid
);
4688 (void) service_set_main_pidref(s
, &pidref
);
4689 (void) unit_watch_pidref(UNIT(s
), &s
->main_pid
, /* exclusive= */ false);
4693 static void service_bus_name_owner_change(Unit
*u
, const char *new_owner
) {
4695 Service
*s
= SERVICE(u
);
4701 log_unit_debug(u
, "D-Bus name %s now owned by %s", s
->bus_name
, new_owner
);
4703 log_unit_debug(u
, "D-Bus name %s now not owned by anyone.", s
->bus_name
);
4705 s
->bus_name_good
= new_owner
;
4707 /* Track the current owner, so we can reconstruct changes after a daemon reload */
4708 r
= free_and_strdup(&s
->bus_name_owner
, new_owner
);
4710 log_unit_error_errno(u
, r
, "Unable to set new bus name owner %s: %m", new_owner
);
4714 if (s
->type
== SERVICE_DBUS
) {
4716 /* service_enter_running() will figure out what to
4718 if (s
->state
== SERVICE_RUNNING
)
4719 service_enter_running(s
, SERVICE_SUCCESS
);
4720 else if (s
->state
== SERVICE_START
&& new_owner
)
4721 service_enter_start_post(s
);
4723 } else if (new_owner
&& pick_up_pid_from_bus_name(s
)) {
4725 /* Try to acquire PID from bus service */
4727 s
->bus_name_pid_lookup_slot
= sd_bus_slot_unref(s
->bus_name_pid_lookup_slot
);
4729 r
= sd_bus_call_method_async(
4730 u
->manager
->api_bus
,
4731 &s
->bus_name_pid_lookup_slot
,
4732 "org.freedesktop.DBus",
4733 "/org/freedesktop/DBus",
4734 "org.freedesktop.DBus",
4735 "GetConnectionUnixProcessID",
4736 bus_name_pid_lookup_callback
,
4741 log_debug_errno(r
, "Failed to request owner PID of service name, ignoring: %m");
4745 int service_set_socket_fd(
4750 bool selinux_context_net
) {
4752 _cleanup_free_
char *peer_text
= NULL
;
4758 /* This is called by the socket code when instantiating a new service for a stream socket and the socket needs
4759 * to be configured. We take ownership of the passed fd on success. */
4761 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
4764 if (s
->socket_fd
>= 0)
4767 assert(!s
->socket_peer
);
4769 if (!IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_DEAD_RESOURCES_PINNED
))
4772 if (getpeername_pretty(fd
, true, &peer_text
) >= 0) {
4774 if (UNIT(s
)->description
) {
4775 _cleanup_free_
char *a
= NULL
;
4777 a
= strjoin(UNIT(s
)->description
, " (", peer_text
, ")");
4781 r
= unit_set_description(UNIT(s
), a
);
4783 r
= unit_set_description(UNIT(s
), peer_text
);
4788 r
= unit_add_two_dependencies(UNIT(sock
), UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT(s
), false, UNIT_DEPENDENCY_IMPLICIT
);
4793 s
->socket_peer
= socket_peer_ref(peer
);
4794 s
->socket_fd_selinux_context_net
= selinux_context_net
;
4796 unit_ref_set(&s
->accept_socket
, UNIT(s
), UNIT(sock
));
4800 static void service_reset_failed(Unit
*u
) {
4801 Service
*s
= SERVICE(u
);
4805 if (s
->state
== SERVICE_FAILED
)
4806 service_set_state(s
, service_determine_dead_state(s
));
4808 s
->result
= SERVICE_SUCCESS
;
4809 s
->reload_result
= SERVICE_SUCCESS
;
4810 s
->clean_result
= SERVICE_SUCCESS
;
4812 s
->flush_n_restarts
= false;
4815 static PidRef
* service_main_pid(Unit
*u
) {
4816 return &ASSERT_PTR(SERVICE(u
))->main_pid
;
4819 static PidRef
* service_control_pid(Unit
*u
) {
4820 return &ASSERT_PTR(SERVICE(u
))->control_pid
;
4823 static bool service_needs_console(Unit
*u
) {
4824 Service
*s
= SERVICE(u
);
4828 /* We provide our own implementation of this here, instead of relying of the generic implementation
4829 * unit_needs_console() provides, since we want to return false if we are in SERVICE_EXITED state. */
4831 if (!exec_context_may_touch_console(&s
->exec_context
))
4834 return IN_SET(s
->state
,
4841 SERVICE_RELOAD_SIGNAL
,
4842 SERVICE_RELOAD_NOTIFY
,
4844 SERVICE_STOP_WATCHDOG
,
4845 SERVICE_STOP_SIGTERM
,
4846 SERVICE_STOP_SIGKILL
,
4848 SERVICE_FINAL_WATCHDOG
,
4849 SERVICE_FINAL_SIGTERM
,
4850 SERVICE_FINAL_SIGKILL
);
4853 static int service_exit_status(Unit
*u
) {
4854 Service
*s
= SERVICE(u
);
4858 if (s
->main_exec_status
.pid
<= 0 ||
4859 !dual_timestamp_is_set(&s
->main_exec_status
.exit_timestamp
))
4862 if (s
->main_exec_status
.code
!= CLD_EXITED
)
4865 return s
->main_exec_status
.status
;
4868 static const char* service_status_text(Unit
*u
) {
4869 Service
*s
= SERVICE(u
);
4873 return s
->status_text
;
4876 static int service_clean(Unit
*u
, ExecCleanMask mask
) {
4877 _cleanup_strv_free_
char **l
= NULL
;
4878 bool may_clean_fdstore
= false;
4879 Service
*s
= SERVICE(u
);
4885 if (!IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_DEAD_RESOURCES_PINNED
))
4888 /* Determine if there's anything we could potentially clean */
4889 r
= exec_context_get_clean_directories(&s
->exec_context
, u
->manager
->prefix
, mask
, &l
);
4893 if (mask
& EXEC_CLEAN_FDSTORE
)
4894 may_clean_fdstore
= s
->n_fd_store
> 0 || s
->n_fd_store_max
> 0;
4896 if (strv_isempty(l
) && !may_clean_fdstore
)
4897 return -EUNATCH
; /* Nothing to potentially clean */
4899 /* Let's clean the stuff we can clean quickly */
4900 if (may_clean_fdstore
)
4901 service_release_fd_store(s
);
4903 /* If we are done, leave quickly */
4904 if (strv_isempty(l
)) {
4905 if (s
->state
== SERVICE_DEAD_RESOURCES_PINNED
&& !s
->fd_store
)
4906 service_set_state(s
, SERVICE_DEAD
);
4910 /* We need to clean disk stuff. This is slow, hence do it out of process, and change state */
4911 service_unwatch_control_pid(s
);
4912 s
->clean_result
= SERVICE_SUCCESS
;
4913 s
->control_command
= NULL
;
4914 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
4916 r
= service_arm_timer(s
, /* relative= */ true, s
->exec_context
.timeout_clean_usec
);
4918 log_unit_warning_errno(u
, r
, "Failed to install timer: %m");
4922 r
= unit_fork_and_watch_rm_rf(u
, l
, &s
->control_pid
);
4924 log_unit_warning_errno(u
, r
, "Failed to spawn cleaning task: %m");
4928 service_set_state(s
, SERVICE_CLEANING
);
4932 s
->clean_result
= SERVICE_FAILURE_RESOURCES
;
4933 s
->timer_event_source
= sd_event_source_disable_unref(s
->timer_event_source
);
4937 static int service_can_clean(Unit
*u
, ExecCleanMask
*ret
) {
4938 Service
*s
= SERVICE(u
);
4939 ExecCleanMask mask
= 0;
4945 r
= exec_context_get_clean_mask(&s
->exec_context
, &mask
);
4949 if (s
->n_fd_store_max
> 0)
4950 mask
|= EXEC_CLEAN_FDSTORE
;
4956 static const char *service_finished_job(Unit
*u
, JobType t
, JobResult result
) {
4957 if (t
== JOB_START
&&
4958 result
== JOB_DONE
&&
4959 SERVICE(u
)->type
== SERVICE_ONESHOT
)
4960 return "Finished %s.";
4962 /* Fall back to generic */
4966 static int service_can_start(Unit
*u
) {
4967 Service
*s
= SERVICE(u
);
4972 /* Make sure we don't enter a busy loop of some kind. */
4973 r
= unit_test_start_limit(u
);
4975 service_enter_dead(s
, SERVICE_FAILURE_START_LIMIT_HIT
, false);
4982 static void service_release_resources(Unit
*u
) {
4983 Service
*s
= SERVICE(ASSERT_PTR(u
));
4985 /* Invoked by the unit state engine, whenever it realizes that unit is dead and there's no job
4986 * anymore for it, and it hence is a good idea to release resources */
4988 /* Don't release resources if this is a transitionary failed/dead state
4989 * (i.e. SERVICE_DEAD_BEFORE_AUTO_RESTART/SERVICE_FAILED_BEFORE_AUTO_RESTART), insist on a permanent
4991 if (!IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_DEAD_RESOURCES_PINNED
))
4994 log_unit_debug(u
, "Releasing resources...");
4996 service_release_socket_fd(s
);
4997 service_release_stdio_fd(s
);
4999 if (s
->fd_store_preserve_mode
!= EXEC_PRESERVE_YES
)
5000 service_release_fd_store(s
);
5002 if (s
->state
== SERVICE_DEAD_RESOURCES_PINNED
&& !s
->fd_store
)
5003 service_set_state(s
, SERVICE_DEAD
);
5006 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
5007 [SERVICE_RESTART_NO
] = "no",
5008 [SERVICE_RESTART_ON_SUCCESS
] = "on-success",
5009 [SERVICE_RESTART_ON_FAILURE
] = "on-failure",
5010 [SERVICE_RESTART_ON_ABNORMAL
] = "on-abnormal",
5011 [SERVICE_RESTART_ON_WATCHDOG
] = "on-watchdog",
5012 [SERVICE_RESTART_ON_ABORT
] = "on-abort",
5013 [SERVICE_RESTART_ALWAYS
] = "always",
5016 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
5018 static const char* const service_restart_mode_table
[_SERVICE_RESTART_MODE_MAX
] = {
5019 [SERVICE_RESTART_MODE_NORMAL
] = "normal",
5020 [SERVICE_RESTART_MODE_DIRECT
] = "direct",
5023 DEFINE_STRING_TABLE_LOOKUP(service_restart_mode
, ServiceRestartMode
);
5025 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
5026 [SERVICE_SIMPLE
] = "simple",
5027 [SERVICE_FORKING
] = "forking",
5028 [SERVICE_ONESHOT
] = "oneshot",
5029 [SERVICE_DBUS
] = "dbus",
5030 [SERVICE_NOTIFY
] = "notify",
5031 [SERVICE_NOTIFY_RELOAD
] = "notify-reload",
5032 [SERVICE_IDLE
] = "idle",
5033 [SERVICE_EXEC
] = "exec",
5036 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
5038 static const char* const service_exit_type_table
[_SERVICE_EXIT_TYPE_MAX
] = {
5039 [SERVICE_EXIT_MAIN
] = "main",
5040 [SERVICE_EXIT_CGROUP
] = "cgroup",
5043 DEFINE_STRING_TABLE_LOOKUP(service_exit_type
, ServiceExitType
);
5045 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
5046 [SERVICE_EXEC_CONDITION
] = "ExecCondition",
5047 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
5048 [SERVICE_EXEC_START
] = "ExecStart",
5049 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
5050 [SERVICE_EXEC_RELOAD
] = "ExecReload",
5051 [SERVICE_EXEC_STOP
] = "ExecStop",
5052 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
5055 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
5057 static const char* const service_exec_ex_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
5058 [SERVICE_EXEC_CONDITION
] = "ExecConditionEx",
5059 [SERVICE_EXEC_START_PRE
] = "ExecStartPreEx",
5060 [SERVICE_EXEC_START
] = "ExecStartEx",
5061 [SERVICE_EXEC_START_POST
] = "ExecStartPostEx",
5062 [SERVICE_EXEC_RELOAD
] = "ExecReloadEx",
5063 [SERVICE_EXEC_STOP
] = "ExecStopEx",
5064 [SERVICE_EXEC_STOP_POST
] = "ExecStopPostEx",
5067 DEFINE_STRING_TABLE_LOOKUP(service_exec_ex_command
, ServiceExecCommand
);
5069 static const char* const notify_state_table
[_NOTIFY_STATE_MAX
] = {
5070 [NOTIFY_UNKNOWN
] = "unknown",
5071 [NOTIFY_READY
] = "ready",
5072 [NOTIFY_RELOADING
] = "reloading",
5073 [NOTIFY_STOPPING
] = "stopping",
5076 DEFINE_STRING_TABLE_LOOKUP(notify_state
, NotifyState
);
5078 static const char* const service_result_table
[_SERVICE_RESULT_MAX
] = {
5079 [SERVICE_SUCCESS
] = "success",
5080 [SERVICE_FAILURE_RESOURCES
] = "resources",
5081 [SERVICE_FAILURE_PROTOCOL
] = "protocol",
5082 [SERVICE_FAILURE_TIMEOUT
] = "timeout",
5083 [SERVICE_FAILURE_EXIT_CODE
] = "exit-code",
5084 [SERVICE_FAILURE_SIGNAL
] = "signal",
5085 [SERVICE_FAILURE_CORE_DUMP
] = "core-dump",
5086 [SERVICE_FAILURE_WATCHDOG
] = "watchdog",
5087 [SERVICE_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
5088 [SERVICE_FAILURE_OOM_KILL
] = "oom-kill",
5089 [SERVICE_SKIP_CONDITION
] = "exec-condition",
5092 DEFINE_STRING_TABLE_LOOKUP(service_result
, ServiceResult
);
5094 static const char* const service_timeout_failure_mode_table
[_SERVICE_TIMEOUT_FAILURE_MODE_MAX
] = {
5095 [SERVICE_TIMEOUT_TERMINATE
] = "terminate",
5096 [SERVICE_TIMEOUT_ABORT
] = "abort",
5097 [SERVICE_TIMEOUT_KILL
] = "kill",
5100 DEFINE_STRING_TABLE_LOOKUP(service_timeout_failure_mode
, ServiceTimeoutFailureMode
);
5102 const UnitVTable service_vtable
= {
5103 .object_size
= sizeof(Service
),
5104 .exec_context_offset
= offsetof(Service
, exec_context
),
5105 .cgroup_context_offset
= offsetof(Service
, cgroup_context
),
5106 .kill_context_offset
= offsetof(Service
, kill_context
),
5107 .exec_runtime_offset
= offsetof(Service
, exec_runtime
),
5113 .private_section
= "Service",
5115 .can_transient
= true,
5116 .can_delegate
= true,
5118 .can_set_managed_oom
= true,
5120 .init
= service_init
,
5121 .done
= service_done
,
5122 .load
= service_load
,
5123 .release_resources
= service_release_resources
,
5125 .coldplug
= service_coldplug
,
5127 .dump
= service_dump
,
5129 .start
= service_start
,
5130 .stop
= service_stop
,
5131 .reload
= service_reload
,
5133 .can_reload
= service_can_reload
,
5135 .clean
= service_clean
,
5136 .can_clean
= service_can_clean
,
5138 .freeze
= unit_freeze_vtable_common
,
5139 .thaw
= unit_thaw_vtable_common
,
5141 .serialize
= service_serialize
,
5142 .deserialize_item
= service_deserialize_item
,
5144 .active_state
= service_active_state
,
5145 .sub_state_to_string
= service_sub_state_to_string
,
5147 .will_restart
= service_will_restart
,
5149 .may_gc
= service_may_gc
,
5151 .sigchld_event
= service_sigchld_event
,
5153 .reset_failed
= service_reset_failed
,
5155 .notify_cgroup_empty
= service_notify_cgroup_empty_event
,
5156 .notify_cgroup_oom
= service_notify_cgroup_oom_event
,
5157 .notify_message
= service_notify_message
,
5159 .main_pid
= service_main_pid
,
5160 .control_pid
= service_control_pid
,
5162 .bus_name_owner_change
= service_bus_name_owner_change
,
5164 .bus_set_property
= bus_service_set_property
,
5165 .bus_commit_properties
= bus_service_commit_properties
,
5167 .get_timeout
= service_get_timeout
,
5168 .get_timeout_start_usec
= service_get_timeout_start_usec
,
5169 .needs_console
= service_needs_console
,
5170 .exit_status
= service_exit_status
,
5171 .status_text
= service_status_text
,
5173 .status_message_formats
= {
5174 .finished_start_job
= {
5175 [JOB_FAILED
] = "Failed to start %s.",
5177 .finished_stop_job
= {
5178 [JOB_DONE
] = "Stopped %s.",
5179 [JOB_FAILED
] = "Stopped (with error) %s.",
5181 .finished_job
= service_finished_job
,
5184 .can_start
= service_can_start
,
5186 .notify_plymouth
= true,
5188 .audit_start_message_type
= AUDIT_SERVICE_START
,
5189 .audit_stop_message_type
= AUDIT_SERVICE_STOP
,