2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include "sd-messages.h"
26 #include "alloc-util.h"
28 #include "bus-error.h"
29 #include "bus-kernel.h"
31 #include "dbus-service.h"
35 #include "exit-status.h"
38 #include "format-util.h"
40 #include "load-dropin.h"
41 #include "load-fragment.h"
44 #include "parse-util.h"
45 #include "path-util.h"
46 #include "process-util.h"
48 #include "signal-util.h"
50 #include "stdio-util.h"
51 #include "string-table.h"
52 #include "string-util.h"
54 #include "unit-name.h"
59 static const UnitActiveState state_translation_table
[_SERVICE_STATE_MAX
] = {
60 [SERVICE_DEAD
] = UNIT_INACTIVE
,
61 [SERVICE_START_PRE
] = UNIT_ACTIVATING
,
62 [SERVICE_START
] = UNIT_ACTIVATING
,
63 [SERVICE_START_POST
] = UNIT_ACTIVATING
,
64 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
65 [SERVICE_EXITED
] = UNIT_ACTIVE
,
66 [SERVICE_RELOAD
] = UNIT_RELOADING
,
67 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
68 [SERVICE_STOP_SIGABRT
] = UNIT_DEACTIVATING
,
69 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
70 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
71 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
72 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
73 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
74 [SERVICE_FAILED
] = UNIT_FAILED
,
75 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
78 /* For Type=idle we never want to delay any other jobs, hence we
79 * consider idle jobs active as soon as we start working on them */
80 static const UnitActiveState state_translation_table_idle
[_SERVICE_STATE_MAX
] = {
81 [SERVICE_DEAD
] = UNIT_INACTIVE
,
82 [SERVICE_START_PRE
] = UNIT_ACTIVE
,
83 [SERVICE_START
] = UNIT_ACTIVE
,
84 [SERVICE_START_POST
] = UNIT_ACTIVE
,
85 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
86 [SERVICE_EXITED
] = UNIT_ACTIVE
,
87 [SERVICE_RELOAD
] = UNIT_RELOADING
,
88 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
89 [SERVICE_STOP_SIGABRT
] = UNIT_DEACTIVATING
,
90 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
91 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
92 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
93 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
94 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
95 [SERVICE_FAILED
] = UNIT_FAILED
,
96 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
99 static int service_dispatch_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
);
100 static int service_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
101 static int service_dispatch_watchdog(sd_event_source
*source
, usec_t usec
, void *userdata
);
103 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
);
104 static void service_enter_reload_by_notify(Service
*s
);
106 static void service_init(Unit
*u
) {
107 Service
*s
= SERVICE(u
);
110 assert(u
->load_state
== UNIT_STUB
);
112 s
->timeout_start_usec
= u
->manager
->default_timeout_start_usec
;
113 s
->timeout_stop_usec
= u
->manager
->default_timeout_stop_usec
;
114 s
->restart_usec
= u
->manager
->default_restart_usec
;
115 s
->runtime_max_usec
= USEC_INFINITY
;
116 s
->type
= _SERVICE_TYPE_INVALID
;
118 s
->stdin_fd
= s
->stdout_fd
= s
->stderr_fd
= -1;
119 s
->guess_main_pid
= true;
121 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
124 static void service_unwatch_control_pid(Service
*s
) {
127 if (s
->control_pid
<= 0)
130 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
134 static void service_unwatch_main_pid(Service
*s
) {
137 if (s
->main_pid
<= 0)
140 unit_unwatch_pid(UNIT(s
), s
->main_pid
);
144 static void service_unwatch_pid_file(Service
*s
) {
145 if (!s
->pid_file_pathspec
)
148 log_unit_debug(UNIT(s
), "Stopping watch for PID file %s", s
->pid_file_pathspec
->path
);
149 path_spec_unwatch(s
->pid_file_pathspec
);
150 path_spec_done(s
->pid_file_pathspec
);
151 s
->pid_file_pathspec
= mfree(s
->pid_file_pathspec
);
154 static int service_set_main_pid(Service
*s
, pid_t pid
) {
162 if (pid
== getpid_cached())
165 if (s
->main_pid
== pid
&& s
->main_pid_known
)
168 if (s
->main_pid
!= pid
) {
169 service_unwatch_main_pid(s
);
170 exec_status_start(&s
->main_exec_status
, pid
);
174 s
->main_pid_known
= true;
176 if (get_process_ppid(pid
, &ppid
) >= 0 && ppid
!= getpid_cached()) {
177 log_unit_warning(UNIT(s
), "Supervising process "PID_FMT
" which is not our child. We'll most likely not notice when it exits.", pid
);
178 s
->main_pid_alien
= true;
180 s
->main_pid_alien
= false;
185 void service_close_socket_fd(Service
*s
) {
188 /* Undo the effect of service_set_socket_fd(). */
190 s
->socket_fd
= asynchronous_close(s
->socket_fd
);
192 if (UNIT_ISSET(s
->accept_socket
)) {
193 socket_connection_unref(SOCKET(UNIT_DEREF(s
->accept_socket
)));
194 unit_ref_unset(&s
->accept_socket
);
198 static void service_stop_watchdog(Service
*s
) {
201 s
->watchdog_event_source
= sd_event_source_unref(s
->watchdog_event_source
);
202 s
->watchdog_timestamp
= DUAL_TIMESTAMP_NULL
;
205 static usec_t
service_get_watchdog_usec(Service
*s
) {
208 if (s
->watchdog_override_enable
)
209 return s
->watchdog_override_usec
;
211 return s
->watchdog_usec
;
214 static void service_start_watchdog(Service
*s
) {
216 usec_t watchdog_usec
;
220 watchdog_usec
= service_get_watchdog_usec(s
);
221 if (watchdog_usec
== 0 || watchdog_usec
== USEC_INFINITY
)
224 if (s
->watchdog_event_source
) {
225 r
= sd_event_source_set_time(s
->watchdog_event_source
, usec_add(s
->watchdog_timestamp
.monotonic
, watchdog_usec
));
227 log_unit_warning_errno(UNIT(s
), r
, "Failed to reset watchdog timer: %m");
231 r
= sd_event_source_set_enabled(s
->watchdog_event_source
, SD_EVENT_ONESHOT
);
233 r
= sd_event_add_time(
234 UNIT(s
)->manager
->event
,
235 &s
->watchdog_event_source
,
237 usec_add(s
->watchdog_timestamp
.monotonic
, watchdog_usec
), 0,
238 service_dispatch_watchdog
, s
);
240 log_unit_warning_errno(UNIT(s
), r
, "Failed to add watchdog timer: %m");
244 (void) sd_event_source_set_description(s
->watchdog_event_source
, "service-watchdog");
246 /* Let's process everything else which might be a sign
247 * of living before we consider a service died. */
248 r
= sd_event_source_set_priority(s
->watchdog_event_source
, SD_EVENT_PRIORITY_IDLE
);
252 log_unit_warning_errno(UNIT(s
), r
, "Failed to install watchdog timer: %m");
255 static void service_reset_watchdog(Service
*s
) {
258 dual_timestamp_get(&s
->watchdog_timestamp
);
259 service_start_watchdog(s
);
262 static void service_reset_watchdog_timeout(Service
*s
, usec_t watchdog_override_usec
) {
265 s
->watchdog_override_enable
= true;
266 s
->watchdog_override_usec
= watchdog_override_usec
;
267 service_reset_watchdog(s
);
269 log_unit_debug(UNIT(s
), "watchdog_usec="USEC_FMT
, s
->watchdog_usec
);
270 log_unit_debug(UNIT(s
), "watchdog_override_usec="USEC_FMT
, s
->watchdog_override_usec
);
273 static void service_fd_store_unlink(ServiceFDStore
*fs
) {
279 assert(fs
->service
->n_fd_store
> 0);
280 LIST_REMOVE(fd_store
, fs
->service
->fd_store
, fs
);
281 fs
->service
->n_fd_store
--;
284 if (fs
->event_source
) {
285 sd_event_source_set_enabled(fs
->event_source
, SD_EVENT_OFF
);
286 sd_event_source_unref(fs
->event_source
);
294 static void service_release_fd_store(Service
*s
) {
297 log_unit_debug(UNIT(s
), "Releasing all stored fds");
299 service_fd_store_unlink(s
->fd_store
);
301 assert(s
->n_fd_store
== 0);
304 static void service_release_resources(Unit
*u
, bool inactive
) {
305 Service
*s
= SERVICE(u
);
309 if (!s
->fd_store
&& s
->stdin_fd
< 0 && s
->stdout_fd
< 0 && s
->stderr_fd
< 0)
312 log_unit_debug(u
, "Releasing resources.");
314 s
->stdin_fd
= safe_close(s
->stdin_fd
);
315 s
->stdout_fd
= safe_close(s
->stdout_fd
);
316 s
->stderr_fd
= safe_close(s
->stderr_fd
);
319 service_release_fd_store(s
);
322 static void service_done(Unit
*u
) {
323 Service
*s
= SERVICE(u
);
327 s
->pid_file
= mfree(s
->pid_file
);
328 s
->status_text
= mfree(s
->status_text
);
330 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
331 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
332 s
->control_command
= NULL
;
333 s
->main_command
= NULL
;
335 dynamic_creds_unref(&s
->dynamic_creds
);
337 exit_status_set_free(&s
->restart_prevent_status
);
338 exit_status_set_free(&s
->restart_force_status
);
339 exit_status_set_free(&s
->success_status
);
341 /* This will leak a process, but at least no memory or any of
343 service_unwatch_main_pid(s
);
344 service_unwatch_control_pid(s
);
345 service_unwatch_pid_file(s
);
348 unit_unwatch_bus_name(u
, s
->bus_name
);
349 s
->bus_name
= mfree(s
->bus_name
);
352 s
->bus_name_owner
= mfree(s
->bus_name_owner
);
354 service_close_socket_fd(s
);
355 s
->peer
= socket_peer_unref(s
->peer
);
357 unit_ref_unset(&s
->accept_socket
);
359 service_stop_watchdog(s
);
361 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
363 service_release_resources(u
, true);
366 static int on_fd_store_io(sd_event_source
*e
, int fd
, uint32_t revents
, void *userdata
) {
367 ServiceFDStore
*fs
= userdata
;
372 /* If we get either EPOLLHUP or EPOLLERR, it's time to remove this entry from the fd store */
373 log_unit_debug(UNIT(fs
->service
),
374 "Received %s on stored fd %d (%s), closing.",
375 revents
& EPOLLERR
? "EPOLLERR" : "EPOLLHUP",
376 fs
->fd
, strna(fs
->fdname
));
377 service_fd_store_unlink(fs
);
381 static int service_add_fd_store(Service
*s
, int fd
, const char *name
) {
385 /* fd is always consumed if we return >= 0 */
390 if (s
->n_fd_store
>= s
->n_fd_store_max
)
391 return -EXFULL
; /* Our store is full.
392 * Use this errno rather than E[NM]FILE to distinguish from
393 * the case where systemd itself hits the file limit. */
395 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
396 r
= same_fd(fs
->fd
, fd
);
401 return 0; /* fd already included */
405 fs
= new0(ServiceFDStore
, 1);
411 fs
->fdname
= strdup(name
?: "stored");
417 r
= sd_event_add_io(UNIT(s
)->manager
->event
, &fs
->event_source
, fd
, 0, on_fd_store_io
, fs
);
418 if (r
< 0 && r
!= -EPERM
) { /* EPERM indicates fds that aren't pollable, which is OK */
423 (void) sd_event_source_set_description(fs
->event_source
, "service-fd-store");
425 LIST_PREPEND(fd_store
, s
->fd_store
, fs
);
428 return 1; /* fd newly stored */
431 static int service_add_fd_store_set(Service
*s
, FDSet
*fds
, const char *name
) {
436 while (fdset_size(fds
) > 0) {
437 _cleanup_close_
int fd
= -1;
439 fd
= fdset_steal_first(fds
);
443 r
= service_add_fd_store(s
, fd
, name
);
445 return log_unit_warning_errno(UNIT(s
), r
,
446 "Cannot store more fds than FileDescriptorStoreMax=%u, closing remaining.",
449 return log_unit_error_errno(UNIT(s
), r
, "Failed to add fd to store: %m");
451 log_unit_debug(UNIT(s
), "Added fd %u (%s) to fd store.", fd
, strna(name
));
458 static int service_arm_timer(Service
*s
, usec_t usec
) {
463 if (s
->timer_event_source
) {
464 r
= sd_event_source_set_time(s
->timer_event_source
, usec
);
468 return sd_event_source_set_enabled(s
->timer_event_source
, SD_EVENT_ONESHOT
);
471 if (usec
== USEC_INFINITY
)
474 r
= sd_event_add_time(
475 UNIT(s
)->manager
->event
,
476 &s
->timer_event_source
,
479 service_dispatch_timer
, s
);
483 (void) sd_event_source_set_description(s
->timer_event_source
, "service-timer");
488 static int service_verify(Service
*s
) {
491 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
494 if (!s
->exec_command
[SERVICE_EXEC_START
] && !s
->exec_command
[SERVICE_EXEC_STOP
]) {
495 log_unit_error(UNIT(s
), "Service lacks both ExecStart= and ExecStop= setting. Refusing.");
499 if (s
->type
!= SERVICE_ONESHOT
&& !s
->exec_command
[SERVICE_EXEC_START
]) {
500 log_unit_error(UNIT(s
), "Service has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
504 if (!s
->remain_after_exit
&& !s
->exec_command
[SERVICE_EXEC_START
]) {
505 log_unit_error(UNIT(s
), "Service has no ExecStart= setting, which is only allowed for RemainAfterExit=yes services. Refusing.");
509 if (s
->type
!= SERVICE_ONESHOT
&& s
->exec_command
[SERVICE_EXEC_START
]->command_next
) {
510 log_unit_error(UNIT(s
), "Service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
514 if (s
->type
== SERVICE_ONESHOT
&& s
->restart
!= SERVICE_RESTART_NO
) {
515 log_unit_error(UNIT(s
), "Service has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing.");
519 if (s
->type
== SERVICE_ONESHOT
&& !exit_status_set_is_empty(&s
->restart_force_status
)) {
520 log_unit_error(UNIT(s
), "Service has RestartForceStatus= set, which isn't allowed for Type=oneshot services. Refusing.");
524 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
) {
525 log_unit_error(UNIT(s
), "Service is of type D-Bus but no D-Bus service name has been specified. Refusing.");
529 if (s
->bus_name
&& s
->type
!= SERVICE_DBUS
)
530 log_unit_warning(UNIT(s
), "Service has a D-Bus service name specified, but is not of type dbus. Ignoring.");
532 if (s
->exec_context
.pam_name
&& !(s
->kill_context
.kill_mode
== KILL_CONTROL_GROUP
|| s
->kill_context
.kill_mode
== KILL_MIXED
)) {
533 log_unit_error(UNIT(s
), "Service has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing.");
537 if (s
->usb_function_descriptors
&& !s
->usb_function_strings
)
538 log_unit_warning(UNIT(s
), "Service has USBFunctionDescriptors= setting, but no USBFunctionStrings=. Ignoring.");
540 if (!s
->usb_function_descriptors
&& s
->usb_function_strings
)
541 log_unit_warning(UNIT(s
), "Service has USBFunctionStrings= setting, but no USBFunctionDescriptors=. Ignoring.");
543 if (s
->runtime_max_usec
!= USEC_INFINITY
&& s
->type
== SERVICE_ONESHOT
)
544 log_unit_warning(UNIT(s
), "MaxRuntimeSec= has no effect in combination with Type=oneshot. Ignoring.");
549 static int service_add_default_dependencies(Service
*s
) {
554 if (!UNIT(s
)->default_dependencies
)
557 /* Add a number of automatic dependencies useful for the
558 * majority of services. */
560 if (MANAGER_IS_SYSTEM(UNIT(s
)->manager
)) {
561 /* First, pull in the really early boot stuff, and
562 * require it, so that we fail if we can't acquire
565 r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_SYSINIT_TARGET
, NULL
, true);
570 /* In the --user instance there's no sysinit.target,
571 * in that case require basic.target instead. */
573 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, NULL
, true);
578 /* Second, if the rest of the base system is in the same
579 * transaction, order us after it, but do not pull it in or
580 * even require it. */
581 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, SPECIAL_BASIC_TARGET
, NULL
, true);
585 /* Third, add us in for normal shutdown. */
586 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true);
589 static void service_fix_output(Service
*s
) {
592 /* If nothing has been explicitly configured, patch default
593 * output in. If input is socket/tty we avoid this however,
594 * since in that case we want output to default to the same
595 * place as we read input from. */
597 if (s
->exec_context
.std_error
== EXEC_OUTPUT_INHERIT
&&
598 s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
599 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
600 s
->exec_context
.std_error
= UNIT(s
)->manager
->default_std_error
;
602 if (s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
603 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
604 s
->exec_context
.std_output
= UNIT(s
)->manager
->default_std_output
;
607 static int service_setup_bus_name(Service
*s
) {
615 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_REQUIRES
, SPECIAL_DBUS_SOCKET
, NULL
, true);
617 return log_unit_error_errno(UNIT(s
), r
, "Failed to add dependency on " SPECIAL_DBUS_SOCKET
": %m");
619 /* We always want to be ordered against dbus.socket if both are in the transaction. */
620 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, SPECIAL_DBUS_SOCKET
, NULL
, true);
622 return log_unit_error_errno(UNIT(s
), r
, "Failed to add dependency on " SPECIAL_DBUS_SOCKET
": %m");
624 r
= unit_watch_bus_name(UNIT(s
), s
->bus_name
);
626 return log_unit_error_errno(UNIT(s
), r
, "Two services allocated for the same bus name %s, refusing operation.", s
->bus_name
);
628 return log_unit_error_errno(UNIT(s
), r
, "Cannot watch bus name %s: %m", s
->bus_name
);
633 static int service_add_extras(Service
*s
) {
638 if (s
->type
== _SERVICE_TYPE_INVALID
) {
639 /* Figure out a type automatically */
641 s
->type
= SERVICE_DBUS
;
642 else if (s
->exec_command
[SERVICE_EXEC_START
])
643 s
->type
= SERVICE_SIMPLE
;
645 s
->type
= SERVICE_ONESHOT
;
648 /* Oneshot services have disabled start timeout by default */
649 if (s
->type
== SERVICE_ONESHOT
&& !s
->start_timeout_defined
)
650 s
->timeout_start_usec
= USEC_INFINITY
;
652 service_fix_output(s
);
654 r
= unit_patch_contexts(UNIT(s
));
658 r
= unit_add_exec_dependencies(UNIT(s
), &s
->exec_context
);
662 r
= unit_set_default_slice(UNIT(s
));
666 if (s
->type
== SERVICE_NOTIFY
&& s
->notify_access
== NOTIFY_NONE
)
667 s
->notify_access
= NOTIFY_MAIN
;
669 if (s
->watchdog_usec
> 0 && s
->notify_access
== NOTIFY_NONE
)
670 s
->notify_access
= NOTIFY_MAIN
;
672 r
= service_add_default_dependencies(s
);
676 r
= service_setup_bus_name(s
);
683 static int service_load(Unit
*u
) {
684 Service
*s
= SERVICE(u
);
689 /* Load a .service file */
690 r
= unit_load_fragment(u
);
694 /* Still nothing found? Then let's give up */
695 if (u
->load_state
== UNIT_STUB
)
698 /* This is a new unit? Then let's add in some extras */
699 if (u
->load_state
== UNIT_LOADED
) {
701 /* We were able to load something, then let's add in
702 * the dropin directories. */
703 r
= unit_load_dropin(u
);
707 /* This is a new unit? Then let's add in some
709 r
= service_add_extras(s
);
714 return service_verify(s
);
717 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
718 ServiceExecCommand c
;
719 Service
*s
= SERVICE(u
);
724 prefix
= strempty(prefix
);
725 prefix2
= strjoina(prefix
, "\t");
728 "%sService State: %s\n"
730 "%sReload Result: %s\n"
731 "%sPermissionsStartOnly: %s\n"
732 "%sRootDirectoryStartOnly: %s\n"
733 "%sRemainAfterExit: %s\n"
734 "%sGuessMainPID: %s\n"
737 "%sNotifyAccess: %s\n"
738 "%sNotifyState: %s\n",
739 prefix
, service_state_to_string(s
->state
),
740 prefix
, service_result_to_string(s
->result
),
741 prefix
, service_result_to_string(s
->reload_result
),
742 prefix
, yes_no(s
->permissions_start_only
),
743 prefix
, yes_no(s
->root_directory_start_only
),
744 prefix
, yes_no(s
->remain_after_exit
),
745 prefix
, yes_no(s
->guess_main_pid
),
746 prefix
, service_type_to_string(s
->type
),
747 prefix
, service_restart_to_string(s
->restart
),
748 prefix
, notify_access_to_string(s
->notify_access
),
749 prefix
, notify_state_to_string(s
->notify_state
));
751 if (s
->control_pid
> 0)
753 "%sControl PID: "PID_FMT
"\n",
754 prefix
, s
->control_pid
);
758 "%sMain PID: "PID_FMT
"\n"
759 "%sMain PID Known: %s\n"
760 "%sMain PID Alien: %s\n",
762 prefix
, yes_no(s
->main_pid_known
),
763 prefix
, yes_no(s
->main_pid_alien
));
768 prefix
, s
->pid_file
);
773 "%sBus Name Good: %s\n",
775 prefix
, yes_no(s
->bus_name_good
));
777 if (UNIT_ISSET(s
->accept_socket
))
779 "%sAccept Socket: %s\n",
780 prefix
, UNIT_DEREF(s
->accept_socket
)->id
);
782 kill_context_dump(&s
->kill_context
, f
, prefix
);
783 exec_context_dump(&s
->exec_context
, f
, prefix
);
785 for (c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
787 if (!s
->exec_command
[c
])
790 fprintf(f
, "%s-> %s:\n",
791 prefix
, service_exec_command_to_string(c
));
793 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
797 fprintf(f
, "%sStatus Text: %s\n",
798 prefix
, s
->status_text
);
800 if (s
->n_fd_store_max
> 0)
802 "%sFile Descriptor Store Max: %u\n"
803 "%sFile Descriptor Store Current: %u\n",
804 prefix
, s
->n_fd_store_max
,
805 prefix
, s
->n_fd_store
);
808 static int service_load_pid_file(Service
*s
, bool may_warn
) {
809 _cleanup_free_
char *k
= NULL
;
818 r
= read_one_line_file(s
->pid_file
, &k
);
821 log_unit_info_errno(UNIT(s
), r
, "PID file %s not readable (yet?) after %s: %m", s
->pid_file
, service_state_to_string(s
->state
));
825 r
= parse_pid(k
, &pid
);
828 log_unit_info_errno(UNIT(s
), r
, "Failed to read PID from file %s: %m", s
->pid_file
);
832 if (!pid_is_alive(pid
)) {
834 log_unit_info(UNIT(s
), "PID "PID_FMT
" read from file %s does not exist or is a zombie.", pid
, s
->pid_file
);
838 if (s
->main_pid_known
) {
839 if (pid
== s
->main_pid
)
842 log_unit_debug(UNIT(s
), "Main PID changing: "PID_FMT
" -> "PID_FMT
, s
->main_pid
, pid
);
844 service_unwatch_main_pid(s
);
845 s
->main_pid_known
= false;
847 log_unit_debug(UNIT(s
), "Main PID loaded: "PID_FMT
, pid
);
849 r
= service_set_main_pid(s
, pid
);
853 r
= unit_watch_pid(UNIT(s
), pid
);
854 if (r
< 0) /* FIXME: we need to do something here */
855 return log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" for service: %m", pid
);
860 static void service_search_main_pid(Service
*s
) {
866 /* If we know it anyway, don't ever fallback to unreliable
868 if (s
->main_pid_known
)
871 if (!s
->guess_main_pid
)
874 assert(s
->main_pid
<= 0);
876 if (unit_search_main_pid(UNIT(s
), &pid
) < 0)
879 log_unit_debug(UNIT(s
), "Main PID guessed: "PID_FMT
, pid
);
880 if (service_set_main_pid(s
, pid
) < 0)
883 r
= unit_watch_pid(UNIT(s
), pid
);
885 /* FIXME: we need to do something here */
886 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" from: %m", pid
);
889 static void service_set_state(Service
*s
, ServiceState state
) {
890 ServiceState old_state
;
891 const UnitActiveState
*table
;
895 table
= s
->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
897 old_state
= s
->state
;
900 service_unwatch_pid_file(s
);
903 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
906 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
907 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
,
908 SERVICE_AUTO_RESTART
))
909 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
912 SERVICE_START
, SERVICE_START_POST
,
913 SERVICE_RUNNING
, SERVICE_RELOAD
,
914 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
915 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
916 service_unwatch_main_pid(s
);
917 s
->main_command
= NULL
;
921 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
923 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
924 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
925 service_unwatch_control_pid(s
);
926 s
->control_command
= NULL
;
927 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
930 if (IN_SET(state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
931 unit_unwatch_all_pids(UNIT(s
));
934 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
935 SERVICE_RUNNING
, SERVICE_RELOAD
,
936 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
937 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
) &&
938 !(state
== SERVICE_DEAD
&& UNIT(s
)->job
))
939 service_close_socket_fd(s
);
941 if (!IN_SET(state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
942 service_stop_watchdog(s
);
944 /* For the inactive states unit_notify() will trim the cgroup,
945 * but for exit we have to do that ourselves... */
946 if (state
== SERVICE_EXITED
&& !MANAGER_IS_RELOADING(UNIT(s
)->manager
))
947 unit_prune_cgroup(UNIT(s
));
949 /* For remain_after_exit services, let's see if we can "release" the
950 * hold on the console, since unit_notify() only does that in case of
952 if (state
== SERVICE_EXITED
&&
953 s
->remain_after_exit
&&
954 UNIT(s
)->manager
->n_on_console
> 0) {
958 ec
= unit_get_exec_context(UNIT(s
));
959 if (ec
&& exec_context_may_touch_console(ec
)) {
960 Manager
*m
= UNIT(s
)->manager
;
963 if (m
->n_on_console
== 0)
964 /* unset no_console_output flag, since the console is free */
965 m
->no_console_output
= false;
969 if (old_state
!= state
)
970 log_unit_debug(UNIT(s
), "Changed %s -> %s", service_state_to_string(old_state
), service_state_to_string(state
));
972 unit_notify(UNIT(s
), table
[old_state
], table
[state
], s
->reload_result
== SERVICE_SUCCESS
);
975 static usec_t
service_coldplug_timeout(Service
*s
) {
978 switch (s
->deserialized_state
) {
980 case SERVICE_START_PRE
:
982 case SERVICE_START_POST
:
984 return usec_add(UNIT(s
)->state_change_timestamp
.monotonic
, s
->timeout_start_usec
);
986 case SERVICE_RUNNING
:
987 return usec_add(UNIT(s
)->active_enter_timestamp
.monotonic
, s
->runtime_max_usec
);
990 case SERVICE_STOP_SIGABRT
:
991 case SERVICE_STOP_SIGTERM
:
992 case SERVICE_STOP_SIGKILL
:
993 case SERVICE_STOP_POST
:
994 case SERVICE_FINAL_SIGTERM
:
995 case SERVICE_FINAL_SIGKILL
:
996 return usec_add(UNIT(s
)->state_change_timestamp
.monotonic
, s
->timeout_stop_usec
);
998 case SERVICE_AUTO_RESTART
:
999 return usec_add(UNIT(s
)->inactive_enter_timestamp
.monotonic
, s
->restart_usec
);
1002 return USEC_INFINITY
;
1006 static int service_coldplug(Unit
*u
) {
1007 Service
*s
= SERVICE(u
);
1011 assert(s
->state
== SERVICE_DEAD
);
1013 if (s
->deserialized_state
== s
->state
)
1016 r
= service_arm_timer(s
, service_coldplug_timeout(s
));
1020 if (s
->main_pid
> 0 &&
1021 pid_is_unwaited(s
->main_pid
) &&
1022 ((s
->deserialized_state
== SERVICE_START
&& IN_SET(s
->type
, SERVICE_FORKING
, SERVICE_DBUS
, SERVICE_ONESHOT
, SERVICE_NOTIFY
)) ||
1023 IN_SET(s
->deserialized_state
,
1024 SERVICE_START
, SERVICE_START_POST
,
1025 SERVICE_RUNNING
, SERVICE_RELOAD
,
1026 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1027 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))) {
1028 r
= unit_watch_pid(UNIT(s
), s
->main_pid
);
1033 if (s
->control_pid
> 0 &&
1034 pid_is_unwaited(s
->control_pid
) &&
1035 IN_SET(s
->deserialized_state
,
1036 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
1038 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1039 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
1040 r
= unit_watch_pid(UNIT(s
), s
->control_pid
);
1045 if (!IN_SET(s
->deserialized_state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
1046 unit_watch_all_pids(UNIT(s
));
1048 if (IN_SET(s
->deserialized_state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
1049 service_start_watchdog(s
);
1051 if (!IN_SET(s
->deserialized_state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
1052 (void) unit_setup_dynamic_creds(u
);
1054 if (UNIT_ISSET(s
->accept_socket
)) {
1055 Socket
* socket
= SOCKET(UNIT_DEREF(s
->accept_socket
));
1057 if (socket
->max_connections_per_source
> 0) {
1060 /* Make a best-effort attempt at bumping the connection count */
1061 if (socket_acquire_peer(socket
, s
->socket_fd
, &peer
) > 0) {
1062 socket_peer_unref(s
->peer
);
1068 service_set_state(s
, s
->deserialized_state
);
1072 static int service_collect_fds(Service
*s
,
1075 unsigned *n_storage_fds
,
1076 unsigned *n_socket_fds
) {
1078 _cleanup_strv_free_
char **rfd_names
= NULL
;
1079 _cleanup_free_
int *rfds
= NULL
;
1080 unsigned rn_socket_fds
= 0, rn_storage_fds
= 0;
1086 assert(n_socket_fds
);
1088 if (s
->socket_fd
>= 0) {
1090 /* Pass the per-connection socket */
1095 rfds
[0] = s
->socket_fd
;
1097 rfd_names
= strv_new("connection", NULL
);
1106 /* Pass all our configured sockets for singleton services */
1108 SET_FOREACH(u
, UNIT(s
)->dependencies
[UNIT_TRIGGERED_BY
], i
) {
1109 _cleanup_free_
int *cfds
= NULL
;
1113 if (u
->type
!= UNIT_SOCKET
)
1118 cn_fds
= socket_collect_fds(sock
, &cfds
);
1127 rn_socket_fds
= cn_fds
;
1133 t
= realloc(rfds
, (rn_socket_fds
+ cn_fds
) * sizeof(int));
1137 memcpy(t
+ rn_socket_fds
, cfds
, cn_fds
* sizeof(int));
1140 rn_socket_fds
+= cn_fds
;
1143 r
= strv_extend_n(&rfd_names
, socket_fdname(sock
), cn_fds
);
1149 if (s
->n_fd_store
> 0) {
1155 t
= realloc(rfds
, (rn_socket_fds
+ s
->n_fd_store
) * sizeof(int));
1161 nl
= realloc(rfd_names
, (rn_socket_fds
+ s
->n_fd_store
+ 1) * sizeof(char*));
1166 n_fds
= rn_socket_fds
;
1168 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
1169 rfds
[n_fds
] = fs
->fd
;
1170 rfd_names
[n_fds
] = strdup(strempty(fs
->fdname
));
1171 if (!rfd_names
[n_fds
])
1178 rfd_names
[n_fds
] = NULL
;
1182 *fd_names
= rfd_names
;
1183 *n_socket_fds
= rn_socket_fds
;
1184 *n_storage_fds
= rn_storage_fds
;
1192 static bool service_exec_needs_notify_socket(Service
*s
, ExecFlags flags
) {
1195 /* Notifications are accepted depending on the process and
1196 * the access setting of the service:
1197 * process: \ access: NONE MAIN EXEC ALL
1198 * main no yes yes yes
1199 * control no no yes yes
1200 * other (forked) no no no yes */
1202 if (flags
& EXEC_IS_CONTROL
)
1203 /* A control process */
1204 return IN_SET(s
->notify_access
, NOTIFY_EXEC
, NOTIFY_ALL
);
1206 /* We only spawn main processes and control processes, so any
1207 * process that is not a control process is a main process */
1208 return s
->notify_access
!= NOTIFY_NONE
;
1211 static int service_spawn(
1218 _cleanup_strv_free_
char **final_env
= NULL
, **our_env
= NULL
, **fd_names
= NULL
;
1219 _cleanup_free_
int *fds
= NULL
;
1220 unsigned n_storage_fds
= 0, n_socket_fds
= 0, n_env
= 0;
1224 ExecParameters exec_params
= {
1237 if (flags
& EXEC_IS_CONTROL
) {
1238 /* If this is a control process, mask the permissions/chroot application if this is requested. */
1239 if (s
->permissions_start_only
)
1240 exec_params
.flags
&= ~EXEC_APPLY_PERMISSIONS
;
1241 if (s
->root_directory_start_only
)
1242 exec_params
.flags
&= ~EXEC_APPLY_CHROOT
;
1245 (void) unit_realize_cgroup(UNIT(s
));
1246 if (s
->reset_cpu_usage
) {
1247 (void) unit_reset_cpu_usage(UNIT(s
));
1248 s
->reset_cpu_usage
= false;
1251 r
= unit_setup_exec_runtime(UNIT(s
));
1255 r
= unit_setup_dynamic_creds(UNIT(s
));
1259 if ((flags
& EXEC_PASS_FDS
) ||
1260 s
->exec_context
.std_input
== EXEC_INPUT_SOCKET
||
1261 s
->exec_context
.std_output
== EXEC_OUTPUT_SOCKET
||
1262 s
->exec_context
.std_error
== EXEC_OUTPUT_SOCKET
) {
1264 r
= service_collect_fds(s
, &fds
, &fd_names
, &n_storage_fds
, &n_socket_fds
);
1268 log_unit_debug(UNIT(s
), "Passing %i fds to service", n_storage_fds
+ n_socket_fds
);
1271 r
= service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), timeout
));
1275 our_env
= new0(char*, 9);
1279 if (service_exec_needs_notify_socket(s
, flags
))
1280 if (asprintf(our_env
+ n_env
++, "NOTIFY_SOCKET=%s", UNIT(s
)->manager
->notify_socket
) < 0)
1283 if (s
->main_pid
> 0)
1284 if (asprintf(our_env
+ n_env
++, "MAINPID="PID_FMT
, s
->main_pid
) < 0)
1287 if (MANAGER_IS_USER(UNIT(s
)->manager
))
1288 if (asprintf(our_env
+ n_env
++, "MANAGERPID="PID_FMT
, getpid_cached()) < 0)
1291 if (s
->socket_fd
>= 0) {
1292 union sockaddr_union sa
;
1293 socklen_t salen
= sizeof(sa
);
1295 r
= getpeername(s
->socket_fd
, &sa
.sa
, &salen
);
1299 /* ENOTCONN is legitimate if the endpoint disappeared on shutdown.
1300 * This connection is over, but the socket unit lives on. */
1301 if (r
!= -ENOTCONN
|| !IN_SET(s
->control_command_id
, SERVICE_EXEC_STOP
, SERVICE_EXEC_STOP_POST
))
1305 if (r
== 0 && IN_SET(sa
.sa
.sa_family
, AF_INET
, AF_INET6
, AF_VSOCK
)) {
1306 _cleanup_free_
char *addr
= NULL
;
1310 r
= sockaddr_pretty(&sa
.sa
, salen
, true, false, &addr
);
1314 t
= strappend("REMOTE_ADDR=", addr
);
1317 our_env
[n_env
++] = t
;
1319 r
= sockaddr_port(&sa
.sa
, &port
);
1323 if (asprintf(&t
, "REMOTE_PORT=%u", port
) < 0)
1325 our_env
[n_env
++] = t
;
1329 if (flags
& EXEC_SETENV_RESULT
) {
1330 if (asprintf(our_env
+ n_env
++, "SERVICE_RESULT=%s", service_result_to_string(s
->result
)) < 0)
1333 if (s
->main_exec_status
.pid
> 0 &&
1334 dual_timestamp_is_set(&s
->main_exec_status
.exit_timestamp
)) {
1335 if (asprintf(our_env
+ n_env
++, "EXIT_CODE=%s", sigchld_code_to_string(s
->main_exec_status
.code
)) < 0)
1338 if (s
->main_exec_status
.code
== CLD_EXITED
)
1339 r
= asprintf(our_env
+ n_env
++, "EXIT_STATUS=%i", s
->main_exec_status
.status
);
1341 r
= asprintf(our_env
+ n_env
++, "EXIT_STATUS=%s", signal_to_string(s
->main_exec_status
.status
));
1347 r
= manager_set_exec_params(UNIT(s
)->manager
, &exec_params
);
1351 final_env
= strv_env_merge(2, exec_params
.environment
, our_env
, NULL
);
1355 if ((flags
& EXEC_IS_CONTROL
) && UNIT(s
)->cgroup_path
) {
1356 path
= strjoina(UNIT(s
)->cgroup_path
, "/control");
1357 (void) cg_create(SYSTEMD_CGROUP_CONTROLLER
, path
);
1359 path
= UNIT(s
)->cgroup_path
;
1361 exec_params
.flags
|= MANAGER_IS_SYSTEM(UNIT(s
)->manager
) ? EXEC_NEW_KEYRING
: 0;
1362 exec_params
.argv
= c
->argv
;
1363 exec_params
.environment
= final_env
;
1364 exec_params
.fds
= fds
;
1365 exec_params
.fd_names
= fd_names
;
1366 exec_params
.n_storage_fds
= n_storage_fds
;
1367 exec_params
.n_socket_fds
= n_socket_fds
;
1368 exec_params
.cgroup_path
= path
;
1369 exec_params
.cgroup_delegate
= s
->cgroup_context
.delegate
;
1370 exec_params
.watchdog_usec
= s
->watchdog_usec
;
1371 exec_params
.selinux_context_net
= s
->socket_fd_selinux_context_net
;
1372 if (s
->type
== SERVICE_IDLE
)
1373 exec_params
.idle_pipe
= UNIT(s
)->manager
->idle_pipe
;
1374 exec_params
.stdin_fd
= s
->stdin_fd
;
1375 exec_params
.stdout_fd
= s
->stdout_fd
;
1376 exec_params
.stderr_fd
= s
->stderr_fd
;
1378 r
= exec_spawn(UNIT(s
),
1388 r
= unit_watch_pid(UNIT(s
), pid
);
1389 if (r
< 0) /* FIXME: we need to do something here */
1397 static int main_pid_good(Service
*s
) {
1400 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1403 /* If we know the pid file, then let's just check if it is
1405 if (s
->main_pid_known
) {
1407 /* If it's an alien child let's check if it is still
1409 if (s
->main_pid_alien
&& s
->main_pid
> 0)
1410 return pid_is_alive(s
->main_pid
);
1412 /* .. otherwise assume we'll get a SIGCHLD for it,
1413 * which we really should wait for to collect exit
1414 * status and code */
1415 return s
->main_pid
> 0;
1418 /* We don't know the pid */
1422 _pure_
static int control_pid_good(Service
*s
) {
1425 return s
->control_pid
> 0;
1428 static int cgroup_good(Service
*s
) {
1433 if (!UNIT(s
)->cgroup_path
)
1436 r
= cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, UNIT(s
)->cgroup_path
);
1443 static bool service_shall_restart(Service
*s
) {
1446 /* Don't restart after manual stops */
1447 if (s
->forbid_restart
)
1450 /* Never restart if this is configured as special exception */
1451 if (exit_status_set_test(&s
->restart_prevent_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
))
1454 /* Restart if the exit code/status are configured as restart triggers */
1455 if (exit_status_set_test(&s
->restart_force_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
))
1458 switch (s
->restart
) {
1460 case SERVICE_RESTART_NO
:
1463 case SERVICE_RESTART_ALWAYS
:
1466 case SERVICE_RESTART_ON_SUCCESS
:
1467 return s
->result
== SERVICE_SUCCESS
;
1469 case SERVICE_RESTART_ON_FAILURE
:
1470 return s
->result
!= SERVICE_SUCCESS
;
1472 case SERVICE_RESTART_ON_ABNORMAL
:
1473 return !IN_SET(s
->result
, SERVICE_SUCCESS
, SERVICE_FAILURE_EXIT_CODE
);
1475 case SERVICE_RESTART_ON_WATCHDOG
:
1476 return s
->result
== SERVICE_FAILURE_WATCHDOG
;
1478 case SERVICE_RESTART_ON_ABORT
:
1479 return IN_SET(s
->result
, SERVICE_FAILURE_SIGNAL
, SERVICE_FAILURE_CORE_DUMP
);
1482 assert_not_reached("unknown restart setting");
1486 static bool service_will_restart(Service
*s
) {
1489 if (s
->state
== SERVICE_AUTO_RESTART
)
1493 if (UNIT(s
)->job
->type
== JOB_START
)
1498 static void service_enter_dead(Service
*s
, ServiceResult f
, bool allow_restart
) {
1502 if (s
->result
== SERVICE_SUCCESS
)
1505 service_set_state(s
, s
->result
!= SERVICE_SUCCESS
? SERVICE_FAILED
: SERVICE_DEAD
);
1507 if (s
->result
!= SERVICE_SUCCESS
) {
1508 log_unit_warning(UNIT(s
), "Failed with result '%s'.", service_result_to_string(s
->result
));
1509 emergency_action(UNIT(s
)->manager
, s
->emergency_action
, UNIT(s
)->reboot_arg
, "service failed");
1512 if (allow_restart
&& service_shall_restart(s
)) {
1514 r
= service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->restart_usec
));
1518 service_set_state(s
, SERVICE_AUTO_RESTART
);
1520 /* If we shan't restart, then flush out the restart counter. But don't do that immediately, so that the
1521 * user can still introspect the counter. Do so on the next start. */
1522 s
->flush_n_restarts
= true;
1524 /* The next restart might not be a manual stop, hence reset the flag indicating manual stops */
1525 s
->forbid_restart
= false;
1527 /* We want fresh tmpdirs in case service is started again immediately */
1528 exec_runtime_destroy(s
->exec_runtime
);
1529 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
1531 if (s
->exec_context
.runtime_directory_preserve_mode
== EXEC_PRESERVE_NO
||
1532 (s
->exec_context
.runtime_directory_preserve_mode
== EXEC_PRESERVE_RESTART
&& !service_will_restart(s
)))
1533 /* Also, remove the runtime directory */
1534 exec_context_destroy_runtime_directory(&s
->exec_context
, UNIT(s
)->manager
->prefix
[EXEC_DIRECTORY_RUNTIME
]);
1536 /* Get rid of the IPC bits of the user */
1537 unit_unref_uid_gid(UNIT(s
), true);
1539 /* Release the user, and destroy it if we are the only remaining owner */
1540 dynamic_creds_destroy(&s
->dynamic_creds
);
1542 /* Try to delete the pid file. At this point it will be
1543 * out-of-date, and some software might be confused by it, so
1544 * let's remove it. */
1546 (void) unlink(s
->pid_file
);
1551 log_unit_warning_errno(UNIT(s
), r
, "Failed to run install restart timer: %m");
1552 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1555 static void service_enter_stop_post(Service
*s
, ServiceResult f
) {
1559 if (s
->result
== SERVICE_SUCCESS
)
1562 service_unwatch_control_pid(s
);
1563 unit_watch_all_pids(UNIT(s
));
1565 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
];
1566 if (s
->control_command
) {
1567 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
1569 r
= service_spawn(s
,
1571 s
->timeout_stop_usec
,
1572 EXEC_APPLY_PERMISSIONS
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
|EXEC_IS_CONTROL
|EXEC_SETENV_RESULT
,
1577 service_set_state(s
, SERVICE_STOP_POST
);
1579 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_SUCCESS
);
1584 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop-post' task: %m");
1585 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1588 static int state_to_kill_operation(ServiceState state
) {
1591 case SERVICE_STOP_SIGABRT
:
1594 case SERVICE_STOP_SIGTERM
:
1595 case SERVICE_FINAL_SIGTERM
:
1596 return KILL_TERMINATE
;
1598 case SERVICE_STOP_SIGKILL
:
1599 case SERVICE_FINAL_SIGKILL
:
1603 return _KILL_OPERATION_INVALID
;
1607 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
) {
1612 if (s
->result
== SERVICE_SUCCESS
)
1615 unit_watch_all_pids(UNIT(s
));
1617 r
= unit_kill_context(
1620 state_to_kill_operation(state
),
1629 r
= service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_stop_usec
));
1633 service_set_state(s
, state
);
1634 } else if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
) && s
->kill_context
.send_sigkill
)
1635 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_SUCCESS
);
1636 else if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
1637 service_enter_stop_post(s
, SERVICE_SUCCESS
);
1638 else if (state
== SERVICE_FINAL_SIGTERM
&& s
->kill_context
.send_sigkill
)
1639 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_SUCCESS
);
1641 service_enter_dead(s
, SERVICE_SUCCESS
, true);
1646 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
1648 if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
1649 service_enter_stop_post(s
, SERVICE_FAILURE_RESOURCES
);
1651 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1654 static void service_enter_stop_by_notify(Service
*s
) {
1657 unit_watch_all_pids(UNIT(s
));
1659 service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_stop_usec
));
1661 /* The service told us it's stopping, so it's as if we SIGTERM'd it. */
1662 service_set_state(s
, SERVICE_STOP_SIGTERM
);
1665 static void service_enter_stop(Service
*s
, ServiceResult f
) {
1670 if (s
->result
== SERVICE_SUCCESS
)
1673 service_unwatch_control_pid(s
);
1674 unit_watch_all_pids(UNIT(s
));
1676 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
];
1677 if (s
->control_command
) {
1678 s
->control_command_id
= SERVICE_EXEC_STOP
;
1680 r
= service_spawn(s
,
1682 s
->timeout_stop_usec
,
1683 EXEC_APPLY_PERMISSIONS
|EXEC_APPLY_CHROOT
|EXEC_IS_CONTROL
|EXEC_SETENV_RESULT
,
1688 service_set_state(s
, SERVICE_STOP
);
1690 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
1695 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop' task: %m");
1696 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1699 static bool service_good(Service
*s
) {
1703 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name_good
)
1706 main_pid_ok
= main_pid_good(s
);
1707 if (main_pid_ok
> 0) /* It's alive */
1709 if (main_pid_ok
== 0) /* It's dead */
1712 /* OK, we don't know anything about the main PID, maybe
1713 * because there is none. Let's check the control group
1716 return cgroup_good(s
) != 0;
1719 static void service_enter_running(Service
*s
, ServiceResult f
) {
1722 if (s
->result
== SERVICE_SUCCESS
)
1725 service_unwatch_control_pid(s
);
1727 if (service_good(s
)) {
1729 /* If there are any queued up sd_notify()
1730 * notifications, process them now */
1731 if (s
->notify_state
== NOTIFY_RELOADING
)
1732 service_enter_reload_by_notify(s
);
1733 else if (s
->notify_state
== NOTIFY_STOPPING
)
1734 service_enter_stop_by_notify(s
);
1736 service_set_state(s
, SERVICE_RUNNING
);
1737 service_arm_timer(s
, usec_add(UNIT(s
)->active_enter_timestamp
.monotonic
, s
->runtime_max_usec
));
1740 } else if (f
!= SERVICE_SUCCESS
)
1741 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
1742 else if (s
->remain_after_exit
)
1743 service_set_state(s
, SERVICE_EXITED
);
1745 service_enter_stop(s
, SERVICE_SUCCESS
);
1748 static void service_enter_start_post(Service
*s
) {
1752 service_unwatch_control_pid(s
);
1753 service_reset_watchdog(s
);
1755 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
];
1756 if (s
->control_command
) {
1757 s
->control_command_id
= SERVICE_EXEC_START_POST
;
1759 r
= service_spawn(s
,
1761 s
->timeout_start_usec
,
1762 EXEC_APPLY_PERMISSIONS
|EXEC_APPLY_CHROOT
|EXEC_IS_CONTROL
,
1767 service_set_state(s
, SERVICE_START_POST
);
1769 service_enter_running(s
, SERVICE_SUCCESS
);
1774 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-post' task: %m");
1775 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1778 static void service_kill_control_processes(Service
*s
) {
1781 if (!UNIT(s
)->cgroup_path
)
1784 p
= strjoina(UNIT(s
)->cgroup_path
, "/control");
1785 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER
, p
, SIGKILL
, CGROUP_SIGCONT
|CGROUP_IGNORE_SELF
|CGROUP_REMOVE
, NULL
, NULL
, NULL
);
1788 static void service_enter_start(Service
*s
) {
1796 service_unwatch_control_pid(s
);
1797 service_unwatch_main_pid(s
);
1799 /* We want to ensure that nobody leaks processes from
1800 * START_PRE here, so let's go on a killing spree, People
1801 * should not spawn long running processes from START_PRE. */
1802 service_kill_control_processes(s
);
1804 if (s
->type
== SERVICE_FORKING
) {
1805 s
->control_command_id
= SERVICE_EXEC_START
;
1806 c
= s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1808 s
->main_command
= NULL
;
1810 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1811 s
->control_command
= NULL
;
1813 c
= s
->main_command
= s
->exec_command
[SERVICE_EXEC_START
];
1817 if (s
->type
!= SERVICE_ONESHOT
) {
1818 /* There's no command line configured for the main command? Hmm, that is strange. This can only
1819 * happen if the configuration changes at runtime. In this case, let's enter a failure
1821 log_unit_error(UNIT(s
), "There's no 'start' task anymore we could start: %m");
1826 service_enter_start_post(s
);
1830 if (IN_SET(s
->type
, SERVICE_SIMPLE
, SERVICE_IDLE
))
1831 /* For simple + idle this is the main process. We don't apply any timeout here, but
1832 * service_enter_running() will later apply the .runtime_max_usec timeout. */
1833 timeout
= USEC_INFINITY
;
1835 timeout
= s
->timeout_start_usec
;
1837 r
= service_spawn(s
,
1840 EXEC_PASS_FDS
|EXEC_APPLY_PERMISSIONS
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
|EXEC_SET_WATCHDOG
,
1845 if (IN_SET(s
->type
, SERVICE_SIMPLE
, SERVICE_IDLE
)) {
1846 /* For simple services we immediately start
1847 * the START_POST binaries. */
1849 service_set_main_pid(s
, pid
);
1850 service_enter_start_post(s
);
1852 } else if (s
->type
== SERVICE_FORKING
) {
1854 /* For forking services we wait until the start
1855 * process exited. */
1857 s
->control_pid
= pid
;
1858 service_set_state(s
, SERVICE_START
);
1860 } else if (IN_SET(s
->type
, SERVICE_ONESHOT
, SERVICE_DBUS
, SERVICE_NOTIFY
)) {
1862 /* For oneshot services we wait until the start
1863 * process exited, too, but it is our main process. */
1865 /* For D-Bus services we know the main pid right away,
1866 * but wait for the bus name to appear on the
1867 * bus. Notify services are similar. */
1869 service_set_main_pid(s
, pid
);
1870 service_set_state(s
, SERVICE_START
);
1872 assert_not_reached("Unknown service type");
1877 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start' task: %m");
1878 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1881 static void service_enter_start_pre(Service
*s
) {
1886 service_unwatch_control_pid(s
);
1888 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
];
1889 if (s
->control_command
) {
1890 /* Before we start anything, let's clear up what might
1891 * be left from previous runs. */
1892 service_kill_control_processes(s
);
1894 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
1896 r
= service_spawn(s
,
1898 s
->timeout_start_usec
,
1899 EXEC_APPLY_PERMISSIONS
|EXEC_APPLY_CHROOT
|EXEC_IS_CONTROL
|EXEC_APPLY_TTY_STDIN
,
1904 service_set_state(s
, SERVICE_START_PRE
);
1906 service_enter_start(s
);
1911 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-pre' task: %m");
1912 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1915 static void service_enter_restart(Service
*s
) {
1916 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1921 if (UNIT(s
)->job
&& UNIT(s
)->job
->type
== JOB_STOP
) {
1922 /* Don't restart things if we are going down anyway */
1923 log_unit_info(UNIT(s
), "Stop job pending for unit, delaying automatic restart.");
1925 r
= service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->restart_usec
));
1932 /* Any units that are bound to this service must also be
1933 * restarted. We use JOB_RESTART (instead of the more obvious
1934 * JOB_START) here so that those dependency jobs will be added
1936 r
= manager_add_job(UNIT(s
)->manager
, JOB_RESTART
, UNIT(s
), JOB_FAIL
, &error
, NULL
);
1940 /* Count the jobs we enqueue for restarting. This counter is maintained as long as the unit isn't fully
1941 * stopped, i.e. as long as it remains up or remains in auto-start states. The use can reset the counter
1942 * explicitly however via the usual "systemctl reset-failure" logic. */
1944 s
->flush_n_restarts
= false;
1946 log_struct(LOG_INFO
,
1947 "MESSAGE_ID=" SD_MESSAGE_UNIT_RESTART_SCHEDULED_STR
,
1948 LOG_UNIT_ID(UNIT(s
)),
1949 LOG_UNIT_MESSAGE(UNIT(s
), "Scheduled restart job, restart counter is at %u.", s
->n_restarts
),
1950 "N_RESTARTS=%u", s
->n_restarts
,
1953 /* Notify clients about changed restart counter */
1954 unit_add_to_dbus_queue(UNIT(s
));
1956 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
1957 * it will be canceled as part of the service_stop() call that
1958 * is executed as part of JOB_RESTART. */
1963 log_unit_warning(UNIT(s
), "Failed to schedule restart job: %s", bus_error_message(&error
, -r
));
1964 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1967 static void service_enter_reload_by_notify(Service
*s
) {
1968 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1973 service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_start_usec
));
1974 service_set_state(s
, SERVICE_RELOAD
);
1976 /* service_enter_reload_by_notify is never called during a reload, thus no loops are possible. */
1977 r
= manager_propagate_reload(UNIT(s
)->manager
, UNIT(s
), JOB_FAIL
, &error
);
1979 log_unit_warning(UNIT(s
), "Failed to schedule propagation of reload: %s", bus_error_message(&error
, -r
));
1982 static void service_enter_reload(Service
*s
) {
1987 service_unwatch_control_pid(s
);
1988 s
->reload_result
= SERVICE_SUCCESS
;
1990 s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
];
1991 if (s
->control_command
) {
1992 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
1994 r
= service_spawn(s
,
1996 s
->timeout_start_usec
,
1997 EXEC_APPLY_PERMISSIONS
|EXEC_APPLY_CHROOT
|EXEC_IS_CONTROL
,
2002 service_set_state(s
, SERVICE_RELOAD
);
2004 service_enter_running(s
, SERVICE_SUCCESS
);
2009 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'reload' task: %m");
2010 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
2011 service_enter_running(s
, SERVICE_SUCCESS
);
2014 static void service_run_next_control(Service
*s
) {
2019 assert(s
->control_command
);
2020 assert(s
->control_command
->command_next
);
2022 assert(s
->control_command_id
!= SERVICE_EXEC_START
);
2024 s
->control_command
= s
->control_command
->command_next
;
2025 service_unwatch_control_pid(s
);
2027 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
2028 timeout
= s
->timeout_start_usec
;
2030 timeout
= s
->timeout_stop_usec
;
2032 r
= service_spawn(s
,
2035 EXEC_APPLY_PERMISSIONS
|EXEC_APPLY_CHROOT
|EXEC_IS_CONTROL
|
2036 (IN_SET(s
->control_command_id
, SERVICE_EXEC_START_PRE
, SERVICE_EXEC_STOP_POST
) ? EXEC_APPLY_TTY_STDIN
: 0)|
2037 (IN_SET(s
->control_command_id
, SERVICE_EXEC_STOP
, SERVICE_EXEC_STOP_POST
) ? EXEC_SETENV_RESULT
: 0),
2045 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next control task: %m");
2047 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_STOP
))
2048 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2049 else if (s
->state
== SERVICE_STOP_POST
)
2050 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
2051 else if (s
->state
== SERVICE_RELOAD
) {
2052 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
2053 service_enter_running(s
, SERVICE_SUCCESS
);
2055 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2058 static void service_run_next_main(Service
*s
) {
2063 assert(s
->main_command
);
2064 assert(s
->main_command
->command_next
);
2065 assert(s
->type
== SERVICE_ONESHOT
);
2067 s
->main_command
= s
->main_command
->command_next
;
2068 service_unwatch_main_pid(s
);
2070 r
= service_spawn(s
,
2072 s
->timeout_start_usec
,
2073 EXEC_PASS_FDS
|EXEC_APPLY_PERMISSIONS
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
|EXEC_SET_WATCHDOG
,
2078 service_set_main_pid(s
, pid
);
2083 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next main task: %m");
2084 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2087 static int service_start(Unit
*u
) {
2088 Service
*s
= SERVICE(u
);
2093 /* We cannot fulfill this request right now, try again later
2095 if (IN_SET(s
->state
,
2096 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
2097 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))
2100 /* Already on it! */
2101 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
))
2104 /* A service that will be restarted must be stopped first to
2105 * trigger BindsTo and/or OnFailure dependencies. If a user
2106 * does not want to wait for the holdoff time to elapse, the
2107 * service should be manually restarted, not started. We
2108 * simply return EAGAIN here, so that any start jobs stay
2109 * queued, and assume that the auto restart timer will
2110 * eventually trigger the restart. */
2111 if (s
->state
== SERVICE_AUTO_RESTART
)
2114 assert(IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_FAILED
));
2116 /* Make sure we don't enter a busy loop of some kind. */
2117 r
= unit_start_limit_test(u
);
2119 service_enter_dead(s
, SERVICE_FAILURE_START_LIMIT_HIT
, false);
2123 r
= unit_acquire_invocation_id(u
);
2127 s
->result
= SERVICE_SUCCESS
;
2128 s
->reload_result
= SERVICE_SUCCESS
;
2129 s
->main_pid_known
= false;
2130 s
->main_pid_alien
= false;
2131 s
->forbid_restart
= false;
2132 s
->reset_cpu_usage
= true;
2134 s
->status_text
= mfree(s
->status_text
);
2135 s
->status_errno
= 0;
2137 s
->notify_state
= NOTIFY_UNKNOWN
;
2139 s
->watchdog_override_enable
= false;
2140 s
->watchdog_override_usec
= 0;
2142 /* This is not an automatic restart? Flush the restart counter then */
2143 if (s
->flush_n_restarts
) {
2145 s
->flush_n_restarts
= false;
2148 service_enter_start_pre(s
);
2152 static int service_stop(Unit
*u
) {
2153 Service
*s
= SERVICE(u
);
2157 /* Don't create restart jobs from manual stops. */
2158 s
->forbid_restart
= true;
2161 if (IN_SET(s
->state
,
2162 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
2163 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))
2166 /* A restart will be scheduled or is in progress. */
2167 if (s
->state
== SERVICE_AUTO_RESTART
) {
2168 service_set_state(s
, SERVICE_DEAD
);
2172 /* If there's already something running we go directly into
2174 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RELOAD
)) {
2175 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
2179 assert(IN_SET(s
->state
, SERVICE_RUNNING
, SERVICE_EXITED
));
2181 service_enter_stop(s
, SERVICE_SUCCESS
);
2185 static int service_reload(Unit
*u
) {
2186 Service
*s
= SERVICE(u
);
2190 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
2192 service_enter_reload(s
);
2196 _pure_
static bool service_can_reload(Unit
*u
) {
2197 Service
*s
= SERVICE(u
);
2201 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
2204 static unsigned service_exec_command_index(Unit
*u
, ServiceExecCommand id
, ExecCommand
*current
) {
2205 Service
*s
= SERVICE(u
);
2207 ExecCommand
*first
, *c
;
2211 first
= s
->exec_command
[id
];
2213 /* Figure out where we are in the list by walking back to the beginning */
2214 for (c
= current
; c
!= first
; c
= c
->command_prev
)
2220 static int service_serialize_exec_command(Unit
*u
, FILE *f
, ExecCommand
*command
) {
2221 Service
*s
= SERVICE(u
);
2222 ServiceExecCommand id
;
2226 _cleanup_free_
char *args
= NULL
, *p
= NULL
;
2227 size_t allocated
= 0, length
= 0;
2235 if (command
== s
->control_command
) {
2237 id
= s
->control_command_id
;
2240 id
= SERVICE_EXEC_START
;
2243 idx
= service_exec_command_index(u
, id
, command
);
2245 STRV_FOREACH(arg
, command
->argv
) {
2247 _cleanup_free_
char *e
= NULL
;
2249 e
= xescape(*arg
, WHITESPACE
);
2254 if (!GREEDY_REALLOC(args
, allocated
, length
+ 1 + n
+ 1))
2258 args
[length
++] = ' ';
2260 memcpy(args
+ length
, e
, n
);
2264 if (!GREEDY_REALLOC(args
, allocated
, length
+ 1))
2268 p
= xescape(command
->path
, WHITESPACE
);
2272 fprintf(f
, "%s-command=%s %u %s %s\n", type
, service_exec_command_to_string(id
), idx
, p
, args
);
2277 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
2278 Service
*s
= SERVICE(u
);
2286 unit_serialize_item(u
, f
, "state", service_state_to_string(s
->state
));
2287 unit_serialize_item(u
, f
, "result", service_result_to_string(s
->result
));
2288 unit_serialize_item(u
, f
, "reload-result", service_result_to_string(s
->reload_result
));
2290 if (s
->control_pid
> 0)
2291 unit_serialize_item_format(u
, f
, "control-pid", PID_FMT
, s
->control_pid
);
2293 if (s
->main_pid_known
&& s
->main_pid
> 0)
2294 unit_serialize_item_format(u
, f
, "main-pid", PID_FMT
, s
->main_pid
);
2296 unit_serialize_item(u
, f
, "main-pid-known", yes_no(s
->main_pid_known
));
2297 unit_serialize_item(u
, f
, "bus-name-good", yes_no(s
->bus_name_good
));
2298 unit_serialize_item(u
, f
, "bus-name-owner", s
->bus_name_owner
);
2300 unit_serialize_item_format(u
, f
, "n-restarts", "%u", s
->n_restarts
);
2301 unit_serialize_item(u
, f
, "n-restarts", yes_no(s
->flush_n_restarts
));
2303 r
= unit_serialize_item_escaped(u
, f
, "status-text", s
->status_text
);
2307 service_serialize_exec_command(u
, f
, s
->control_command
);
2308 service_serialize_exec_command(u
, f
, s
->main_command
);
2310 r
= unit_serialize_item_fd(u
, f
, fds
, "stdin-fd", s
->stdin_fd
);
2313 r
= unit_serialize_item_fd(u
, f
, fds
, "stdout-fd", s
->stdout_fd
);
2316 r
= unit_serialize_item_fd(u
, f
, fds
, "stderr-fd", s
->stderr_fd
);
2320 if (UNIT_ISSET(s
->accept_socket
)) {
2321 r
= unit_serialize_item(u
, f
, "accept-socket", UNIT_DEREF(s
->accept_socket
)->id
);
2326 r
= unit_serialize_item_fd(u
, f
, fds
, "socket-fd", s
->socket_fd
);
2330 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
2331 _cleanup_free_
char *c
= NULL
;
2334 copy
= fdset_put_dup(fds
, fs
->fd
);
2338 c
= cescape(fs
->fdname
);
2340 unit_serialize_item_format(u
, f
, "fd-store-fd", "%i %s", copy
, strempty(c
));
2343 if (s
->main_exec_status
.pid
> 0) {
2344 unit_serialize_item_format(u
, f
, "main-exec-status-pid", PID_FMT
, s
->main_exec_status
.pid
);
2345 dual_timestamp_serialize(f
, "main-exec-status-start", &s
->main_exec_status
.start_timestamp
);
2346 dual_timestamp_serialize(f
, "main-exec-status-exit", &s
->main_exec_status
.exit_timestamp
);
2348 if (dual_timestamp_is_set(&s
->main_exec_status
.exit_timestamp
)) {
2349 unit_serialize_item_format(u
, f
, "main-exec-status-code", "%i", s
->main_exec_status
.code
);
2350 unit_serialize_item_format(u
, f
, "main-exec-status-status", "%i", s
->main_exec_status
.status
);
2354 dual_timestamp_serialize(f
, "watchdog-timestamp", &s
->watchdog_timestamp
);
2356 unit_serialize_item(u
, f
, "forbid-restart", yes_no(s
->forbid_restart
));
2358 if (s
->watchdog_override_enable
)
2359 unit_serialize_item_format(u
, f
, "watchdog-override-usec", USEC_FMT
, s
->watchdog_override_usec
);
2364 static int service_deserialize_exec_command(Unit
*u
, const char *key
, const char *value
) {
2365 Service
*s
= SERVICE(u
);
2367 unsigned idx
= 0, i
;
2368 bool control
, found
= false;
2369 ServiceExecCommand id
= _SERVICE_EXEC_COMMAND_INVALID
;
2370 ExecCommand
*command
= NULL
;
2371 _cleanup_free_
char *path
= NULL
;
2372 _cleanup_strv_free_
char **argv
= NULL
;
2374 enum ExecCommandState
{
2375 STATE_EXEC_COMMAND_TYPE
,
2376 STATE_EXEC_COMMAND_INDEX
,
2377 STATE_EXEC_COMMAND_PATH
,
2378 STATE_EXEC_COMMAND_ARGS
,
2379 _STATE_EXEC_COMMAND_MAX
,
2380 _STATE_EXEC_COMMAND_INVALID
= -1,
2387 control
= streq(key
, "control-command");
2389 state
= STATE_EXEC_COMMAND_TYPE
;
2392 _cleanup_free_
char *arg
= NULL
;
2394 r
= extract_first_word(&value
, &arg
, NULL
, EXTRACT_CUNESCAPE
);
2401 case STATE_EXEC_COMMAND_TYPE
:
2402 id
= service_exec_command_from_string(arg
);
2406 state
= STATE_EXEC_COMMAND_INDEX
;
2408 case STATE_EXEC_COMMAND_INDEX
:
2409 r
= safe_atou(arg
, &idx
);
2413 state
= STATE_EXEC_COMMAND_PATH
;
2415 case STATE_EXEC_COMMAND_PATH
:
2418 state
= STATE_EXEC_COMMAND_ARGS
;
2420 if (!path_is_absolute(path
))
2423 case STATE_EXEC_COMMAND_ARGS
:
2424 r
= strv_extend(&argv
, arg
);
2429 assert_not_reached("Unknown error at deserialization of exec command");
2434 if (state
!= STATE_EXEC_COMMAND_ARGS
)
2437 /* Let's check whether exec command on given offset matches data that we just deserialized */
2438 for (command
= s
->exec_command
[id
], i
= 0; command
; command
= command
->command_next
, i
++) {
2442 found
= strv_equal(argv
, command
->argv
) && streq(command
->path
, path
);
2447 /* Command at the index we serialized is different, let's look for command that exactly
2448 * matches but is on different index. If there is no such command we will not resume execution. */
2449 for (command
= s
->exec_command
[id
]; command
; command
= command
->command_next
)
2450 if (strv_equal(command
->argv
, argv
) && streq(command
->path
, path
))
2454 if (command
&& control
)
2455 s
->control_command
= command
;
2457 s
->main_command
= command
;
2459 log_unit_warning(u
, "Current command vanished from the unit file, execution of the command list won't be resumed.");
2464 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
2465 Service
*s
= SERVICE(u
);
2473 if (streq(key
, "state")) {
2476 state
= service_state_from_string(value
);
2478 log_unit_debug(u
, "Failed to parse state value: %s", value
);
2480 s
->deserialized_state
= state
;
2481 } else if (streq(key
, "result")) {
2484 f
= service_result_from_string(value
);
2486 log_unit_debug(u
, "Failed to parse result value: %s", value
);
2487 else if (f
!= SERVICE_SUCCESS
)
2490 } else if (streq(key
, "reload-result")) {
2493 f
= service_result_from_string(value
);
2495 log_unit_debug(u
, "Failed to parse reload result value: %s", value
);
2496 else if (f
!= SERVICE_SUCCESS
)
2497 s
->reload_result
= f
;
2499 } else if (streq(key
, "control-pid")) {
2502 if (parse_pid(value
, &pid
) < 0)
2503 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
2505 s
->control_pid
= pid
;
2506 } else if (streq(key
, "main-pid")) {
2509 if (parse_pid(value
, &pid
) < 0)
2510 log_unit_debug(u
, "Failed to parse main-pid value: %s", value
);
2512 service_set_main_pid(s
, pid
);
2513 unit_watch_pid(UNIT(s
), pid
);
2515 } else if (streq(key
, "main-pid-known")) {
2518 b
= parse_boolean(value
);
2520 log_unit_debug(u
, "Failed to parse main-pid-known value: %s", value
);
2522 s
->main_pid_known
= b
;
2523 } else if (streq(key
, "bus-name-good")) {
2526 b
= parse_boolean(value
);
2528 log_unit_debug(u
, "Failed to parse bus-name-good value: %s", value
);
2530 s
->bus_name_good
= b
;
2531 } else if (streq(key
, "bus-name-owner")) {
2532 r
= free_and_strdup(&s
->bus_name_owner
, value
);
2534 log_unit_error_errno(u
, r
, "Unable to deserialize current bus owner %s: %m", value
);
2535 } else if (streq(key
, "status-text")) {
2538 r
= cunescape(value
, 0, &t
);
2540 log_unit_debug_errno(u
, r
, "Failed to unescape status text: %s", value
);
2542 free(s
->status_text
);
2546 } else if (streq(key
, "accept-socket")) {
2549 r
= manager_load_unit(u
->manager
, value
, NULL
, NULL
, &socket
);
2551 log_unit_debug_errno(u
, r
, "Failed to load accept-socket unit: %s", value
);
2553 unit_ref_set(&s
->accept_socket
, socket
);
2554 SOCKET(socket
)->n_connections
++;
2557 } else if (streq(key
, "socket-fd")) {
2560 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2561 log_unit_debug(u
, "Failed to parse socket-fd value: %s", value
);
2563 asynchronous_close(s
->socket_fd
);
2564 s
->socket_fd
= fdset_remove(fds
, fd
);
2566 } else if (streq(key
, "fd-store-fd")) {
2571 pf
= strcspn(value
, WHITESPACE
);
2572 fdv
= strndupa(value
, pf
);
2574 if (safe_atoi(fdv
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2575 log_unit_debug(u
, "Failed to parse fd-store-fd value: %s", value
);
2577 _cleanup_free_
char *t
= NULL
;
2581 fdn
+= strspn(fdn
, WHITESPACE
);
2582 (void) cunescape(fdn
, 0, &t
);
2584 r
= service_add_fd_store(s
, fd
, t
);
2586 log_unit_error_errno(u
, r
, "Failed to add fd to store: %m");
2588 fdset_remove(fds
, fd
);
2591 } else if (streq(key
, "main-exec-status-pid")) {
2594 if (parse_pid(value
, &pid
) < 0)
2595 log_unit_debug(u
, "Failed to parse main-exec-status-pid value: %s", value
);
2597 s
->main_exec_status
.pid
= pid
;
2598 } else if (streq(key
, "main-exec-status-code")) {
2601 if (safe_atoi(value
, &i
) < 0)
2602 log_unit_debug(u
, "Failed to parse main-exec-status-code value: %s", value
);
2604 s
->main_exec_status
.code
= i
;
2605 } else if (streq(key
, "main-exec-status-status")) {
2608 if (safe_atoi(value
, &i
) < 0)
2609 log_unit_debug(u
, "Failed to parse main-exec-status-status value: %s", value
);
2611 s
->main_exec_status
.status
= i
;
2612 } else if (streq(key
, "main-exec-status-start"))
2613 dual_timestamp_deserialize(value
, &s
->main_exec_status
.start_timestamp
);
2614 else if (streq(key
, "main-exec-status-exit"))
2615 dual_timestamp_deserialize(value
, &s
->main_exec_status
.exit_timestamp
);
2616 else if (streq(key
, "watchdog-timestamp"))
2617 dual_timestamp_deserialize(value
, &s
->watchdog_timestamp
);
2618 else if (streq(key
, "forbid-restart")) {
2621 b
= parse_boolean(value
);
2623 log_unit_debug(u
, "Failed to parse forbid-restart value: %s", value
);
2625 s
->forbid_restart
= b
;
2626 } else if (streq(key
, "stdin-fd")) {
2629 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2630 log_unit_debug(u
, "Failed to parse stdin-fd value: %s", value
);
2632 asynchronous_close(s
->stdin_fd
);
2633 s
->stdin_fd
= fdset_remove(fds
, fd
);
2634 s
->exec_context
.stdio_as_fds
= true;
2636 } else if (streq(key
, "stdout-fd")) {
2639 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2640 log_unit_debug(u
, "Failed to parse stdout-fd value: %s", value
);
2642 asynchronous_close(s
->stdout_fd
);
2643 s
->stdout_fd
= fdset_remove(fds
, fd
);
2644 s
->exec_context
.stdio_as_fds
= true;
2646 } else if (streq(key
, "stderr-fd")) {
2649 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2650 log_unit_debug(u
, "Failed to parse stderr-fd value: %s", value
);
2652 asynchronous_close(s
->stderr_fd
);
2653 s
->stderr_fd
= fdset_remove(fds
, fd
);
2654 s
->exec_context
.stdio_as_fds
= true;
2656 } else if (streq(key
, "watchdog-override-usec")) {
2657 usec_t watchdog_override_usec
;
2658 if (timestamp_deserialize(value
, &watchdog_override_usec
) < 0)
2659 log_unit_debug(u
, "Failed to parse watchdog_override_usec value: %s", value
);
2661 s
->watchdog_override_enable
= true;
2662 s
->watchdog_override_usec
= watchdog_override_usec
;
2664 } else if (STR_IN_SET(key
, "main-command", "control-command")) {
2665 r
= service_deserialize_exec_command(u
, key
, value
);
2667 log_unit_debug_errno(u
, r
, "Failed to parse serialized command \"%s\": %m", value
);
2669 } else if (streq(key
, "n-restarts")) {
2670 r
= safe_atou(value
, &s
->n_restarts
);
2672 log_unit_debug_errno(u
, r
, "Failed to parse serialized restart counter '%s': %m", value
);
2674 } else if (streq(key
, "flush-n-restarts")) {
2675 r
= parse_boolean(value
);
2677 log_unit_debug_errno(u
, r
, "Failed to parse serialized flush restart counter setting '%s': %m", value
);
2679 s
->flush_n_restarts
= r
;
2681 log_unit_debug(u
, "Unknown serialization key: %s", key
);
2686 _pure_
static UnitActiveState
service_active_state(Unit
*u
) {
2687 const UnitActiveState
*table
;
2691 table
= SERVICE(u
)->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
2693 return table
[SERVICE(u
)->state
];
2696 static const char *service_sub_state_to_string(Unit
*u
) {
2699 return service_state_to_string(SERVICE(u
)->state
);
2702 static bool service_check_gc(Unit
*u
) {
2703 Service
*s
= SERVICE(u
);
2707 /* Never clean up services that still have a process around,
2708 * even if the service is formally dead. */
2709 if (cgroup_good(s
) > 0 ||
2710 main_pid_good(s
) > 0 ||
2711 control_pid_good(s
) > 0)
2717 static int service_retry_pid_file(Service
*s
) {
2720 assert(s
->pid_file
);
2721 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2723 r
= service_load_pid_file(s
, false);
2727 service_unwatch_pid_file(s
);
2729 service_enter_running(s
, SERVICE_SUCCESS
);
2733 static int service_watch_pid_file(Service
*s
) {
2736 log_unit_debug(UNIT(s
), "Setting watch for PID file %s", s
->pid_file_pathspec
->path
);
2738 r
= path_spec_watch(s
->pid_file_pathspec
, service_dispatch_io
);
2742 /* the pidfile might have appeared just before we set the watch */
2743 log_unit_debug(UNIT(s
), "Trying to read PID file %s in case it changed", s
->pid_file_pathspec
->path
);
2744 service_retry_pid_file(s
);
2748 log_unit_error_errno(UNIT(s
), r
, "Failed to set a watch for PID file %s: %m", s
->pid_file_pathspec
->path
);
2749 service_unwatch_pid_file(s
);
2753 static int service_demand_pid_file(Service
*s
) {
2756 assert(s
->pid_file
);
2757 assert(!s
->pid_file_pathspec
);
2759 ps
= new0(PathSpec
, 1);
2764 ps
->path
= strdup(s
->pid_file
);
2770 path_kill_slashes(ps
->path
);
2772 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2773 * keep their PID file open all the time. */
2774 ps
->type
= PATH_MODIFIED
;
2775 ps
->inotify_fd
= -1;
2777 s
->pid_file_pathspec
= ps
;
2779 return service_watch_pid_file(s
);
2782 static int service_dispatch_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
) {
2783 PathSpec
*p
= userdata
;
2788 s
= SERVICE(p
->unit
);
2792 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2793 assert(s
->pid_file_pathspec
);
2794 assert(path_spec_owns_inotify_fd(s
->pid_file_pathspec
, fd
));
2796 log_unit_debug(UNIT(s
), "inotify event");
2798 if (path_spec_fd_event(p
, events
) < 0)
2801 if (service_retry_pid_file(s
) == 0)
2804 if (service_watch_pid_file(s
) < 0)
2810 service_unwatch_pid_file(s
);
2811 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2815 static void service_notify_cgroup_empty_event(Unit
*u
) {
2816 Service
*s
= SERVICE(u
);
2820 log_unit_debug(u
, "cgroup is empty");
2824 /* Waiting for SIGCHLD is usually more interesting,
2825 * because it includes return codes/signals. Which is
2826 * why we ignore the cgroup events for most cases,
2827 * except when we don't know pid which to expect the
2831 if (s
->type
== SERVICE_NOTIFY
) {
2832 /* No chance of getting a ready notification anymore */
2833 service_enter_stop_post(s
, SERVICE_FAILURE_PROTOCOL
);
2839 case SERVICE_START_POST
:
2840 if (s
->pid_file_pathspec
) {
2841 /* Give up hoping for the daemon to write its PID file */
2842 log_unit_warning(u
, "Daemon never wrote its PID file. Failing.");
2844 service_unwatch_pid_file(s
);
2845 if (s
->state
== SERVICE_START
)
2846 service_enter_stop_post(s
, SERVICE_FAILURE_PROTOCOL
);
2848 service_enter_stop(s
, SERVICE_FAILURE_PROTOCOL
);
2852 case SERVICE_RUNNING
:
2853 /* service_enter_running() will figure out what to do */
2854 service_enter_running(s
, SERVICE_SUCCESS
);
2857 case SERVICE_STOP_SIGABRT
:
2858 case SERVICE_STOP_SIGTERM
:
2859 case SERVICE_STOP_SIGKILL
:
2861 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2862 service_enter_stop_post(s
, SERVICE_SUCCESS
);
2866 case SERVICE_STOP_POST
:
2867 case SERVICE_FINAL_SIGTERM
:
2868 case SERVICE_FINAL_SIGKILL
:
2869 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2870 service_enter_dead(s
, SERVICE_SUCCESS
, true);
2879 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
2880 Service
*s
= SERVICE(u
);
2886 if (is_clean_exit(code
, status
, s
->type
== SERVICE_ONESHOT
? EXIT_CLEAN_COMMAND
: EXIT_CLEAN_DAEMON
, &s
->success_status
))
2887 f
= SERVICE_SUCCESS
;
2888 else if (code
== CLD_EXITED
)
2889 f
= SERVICE_FAILURE_EXIT_CODE
;
2890 else if (code
== CLD_KILLED
)
2891 f
= SERVICE_FAILURE_SIGNAL
;
2892 else if (code
== CLD_DUMPED
)
2893 f
= SERVICE_FAILURE_CORE_DUMP
;
2895 assert_not_reached("Unknown code");
2897 if (s
->main_pid
== pid
) {
2898 /* Forking services may occasionally move to a new PID.
2899 * As long as they update the PID file before exiting the old
2900 * PID, they're fine. */
2901 if (service_load_pid_file(s
, false) == 0)
2905 exec_status_exit(&s
->main_exec_status
, &s
->exec_context
, pid
, code
, status
);
2907 if (s
->main_command
) {
2908 /* If this is not a forking service than the
2909 * main process got started and hence we copy
2910 * the exit status so that it is recorded both
2911 * as main and as control process exit
2914 s
->main_command
->exec_status
= s
->main_exec_status
;
2916 if (s
->main_command
->ignore
)
2917 f
= SERVICE_SUCCESS
;
2918 } else if (s
->exec_command
[SERVICE_EXEC_START
]) {
2920 /* If this is a forked process, then we should
2921 * ignore the return value if this was
2922 * configured for the starter process */
2924 if (s
->exec_command
[SERVICE_EXEC_START
]->ignore
)
2925 f
= SERVICE_SUCCESS
;
2928 /* When this is a successful exit, let's log about the exit code on DEBUG level. If this is a failure
2929 * and the process exited on its own via exit(), then let's make this a NOTICE, under the assumption
2930 * that the service already logged the reason at a higher log level on its own. However, if the service
2931 * died due to a signal, then it most likely didn't say anything about any reason, hence let's raise
2932 * our log level to WARNING then. */
2934 log_struct(f
== SERVICE_SUCCESS
? LOG_DEBUG
:
2935 (code
== CLD_EXITED
? LOG_NOTICE
: LOG_WARNING
),
2936 LOG_UNIT_MESSAGE(u
, "Main process exited, code=%s, status=%i/%s",
2937 sigchld_code_to_string(code
), status
,
2938 strna(code
== CLD_EXITED
2939 ? exit_status_to_string(status
, EXIT_STATUS_FULL
)
2940 : signal_to_string(status
))),
2941 "EXIT_CODE=%s", sigchld_code_to_string(code
),
2942 "EXIT_STATUS=%i", status
,
2946 if (s
->result
== SERVICE_SUCCESS
)
2949 if (s
->main_command
&&
2950 s
->main_command
->command_next
&&
2951 s
->type
== SERVICE_ONESHOT
&&
2952 f
== SERVICE_SUCCESS
) {
2954 /* There is another command to *
2955 * execute, so let's do that. */
2957 log_unit_debug(u
, "Running next main command for state %s.", service_state_to_string(s
->state
));
2958 service_run_next_main(s
);
2962 /* The service exited, so the service is officially
2964 s
->main_command
= NULL
;
2968 case SERVICE_START_POST
:
2969 case SERVICE_RELOAD
:
2971 /* Need to wait until the operation is
2976 if (s
->type
== SERVICE_ONESHOT
) {
2977 /* This was our main goal, so let's go on */
2978 if (f
== SERVICE_SUCCESS
)
2979 service_enter_start_post(s
);
2981 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
2983 } else if (s
->type
== SERVICE_NOTIFY
) {
2984 /* Only enter running through a notification, so that the
2985 * SERVICE_START state signifies that no ready notification
2986 * has been received */
2987 if (f
!= SERVICE_SUCCESS
)
2988 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
2989 else if (!s
->remain_after_exit
)
2990 /* The service has never been active */
2991 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_PROTOCOL
);
2997 case SERVICE_RUNNING
:
2998 service_enter_running(s
, f
);
3001 case SERVICE_STOP_SIGABRT
:
3002 case SERVICE_STOP_SIGTERM
:
3003 case SERVICE_STOP_SIGKILL
:
3005 if (!control_pid_good(s
))
3006 service_enter_stop_post(s
, f
);
3008 /* If there is still a control process, wait for that first */
3011 case SERVICE_STOP_POST
:
3012 case SERVICE_FINAL_SIGTERM
:
3013 case SERVICE_FINAL_SIGKILL
:
3015 if (!control_pid_good(s
))
3016 service_enter_dead(s
, f
, true);
3020 assert_not_reached("Uh, main process died at wrong time.");
3024 } else if (s
->control_pid
== pid
) {
3027 if (s
->control_command
) {
3028 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
3030 if (s
->control_command
->ignore
)
3031 f
= SERVICE_SUCCESS
;
3034 log_unit_full(u
, f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
, 0,
3035 "Control process exited, code=%s status=%i",
3036 sigchld_code_to_string(code
), status
);
3038 if (s
->result
== SERVICE_SUCCESS
)
3041 /* Immediately get rid of the cgroup, so that the
3042 * kernel doesn't delay the cgroup empty messages for
3043 * the service cgroup any longer than necessary */
3044 service_kill_control_processes(s
);
3046 if (s
->control_command
&&
3047 s
->control_command
->command_next
&&
3048 f
== SERVICE_SUCCESS
) {
3050 /* There is another command to *
3051 * execute, so let's do that. */
3053 log_unit_debug(u
, "Running next control command for state %s.", service_state_to_string(s
->state
));
3054 service_run_next_control(s
);
3057 /* No further commands for this step, so let's
3058 * figure out what to do next */
3060 s
->control_command
= NULL
;
3061 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
3063 log_unit_debug(u
, "Got final SIGCHLD for state %s.", service_state_to_string(s
->state
));
3067 case SERVICE_START_PRE
:
3068 if (f
== SERVICE_SUCCESS
)
3069 service_enter_start(s
);
3071 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
3075 if (s
->type
!= SERVICE_FORKING
)
3076 /* Maybe spurious event due to a reload that changed the type? */
3079 if (f
!= SERVICE_SUCCESS
) {
3080 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
3085 bool has_start_post
;
3088 /* Let's try to load the pid file here if we can.
3089 * The PID file might actually be created by a START_POST
3090 * script. In that case don't worry if the loading fails. */
3092 has_start_post
= !!s
->exec_command
[SERVICE_EXEC_START_POST
];
3093 r
= service_load_pid_file(s
, !has_start_post
);
3094 if (!has_start_post
&& r
< 0) {
3095 r
= service_demand_pid_file(s
);
3096 if (r
< 0 || !cgroup_good(s
))
3097 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_PROTOCOL
);
3101 service_search_main_pid(s
);
3103 service_enter_start_post(s
);
3106 case SERVICE_START_POST
:
3107 if (f
!= SERVICE_SUCCESS
) {
3108 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
3115 r
= service_load_pid_file(s
, true);
3117 r
= service_demand_pid_file(s
);
3118 if (r
< 0 || !cgroup_good(s
))
3119 service_enter_stop(s
, SERVICE_FAILURE_PROTOCOL
);
3123 service_search_main_pid(s
);
3125 service_enter_running(s
, SERVICE_SUCCESS
);
3128 case SERVICE_RELOAD
:
3129 if (f
== SERVICE_SUCCESS
)
3130 if (service_load_pid_file(s
, true) < 0)
3131 service_search_main_pid(s
);
3133 s
->reload_result
= f
;
3134 service_enter_running(s
, SERVICE_SUCCESS
);
3138 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
3141 case SERVICE_STOP_SIGABRT
:
3142 case SERVICE_STOP_SIGTERM
:
3143 case SERVICE_STOP_SIGKILL
:
3144 if (main_pid_good(s
) <= 0)
3145 service_enter_stop_post(s
, f
);
3147 /* If there is still a service
3148 * process around, wait until
3149 * that one quit, too */
3152 case SERVICE_STOP_POST
:
3153 case SERVICE_FINAL_SIGTERM
:
3154 case SERVICE_FINAL_SIGKILL
:
3155 if (main_pid_good(s
) <= 0)
3156 service_enter_dead(s
, f
, true);
3160 assert_not_reached("Uh, control process died at wrong time.");
3165 /* Notify clients about changed exit status */
3166 unit_add_to_dbus_queue(u
);
3168 /* We got one SIGCHLD for the service, let's watch all
3169 * processes that are now running of the service, and watch
3170 * that. Among the PIDs we then watch will be children
3171 * reassigned to us, which hopefully allows us to identify
3172 * when all children are gone */
3173 unit_tidy_watch_pids(u
, s
->main_pid
, s
->control_pid
);
3174 unit_watch_all_pids(u
);
3176 /* If the PID set is empty now, then let's finish this off
3177 (On unified we use proper notifications) */
3178 if (cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER
) == 0 && set_isempty(u
->pids
))
3179 service_notify_cgroup_empty_event(u
);
3182 static int service_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
3183 Service
*s
= SERVICE(userdata
);
3186 assert(source
== s
->timer_event_source
);
3190 case SERVICE_START_PRE
:
3192 log_unit_warning(UNIT(s
), "%s operation timed out. Terminating.", s
->state
== SERVICE_START
? "Start" : "Start-pre");
3193 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
3196 case SERVICE_START_POST
:
3197 log_unit_warning(UNIT(s
), "Start-post operation timed out. Stopping.");
3198 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
3201 case SERVICE_RUNNING
:
3202 log_unit_warning(UNIT(s
), "Service reached runtime time limit. Stopping.");
3203 service_enter_stop(s
, SERVICE_FAILURE_TIMEOUT
);
3206 case SERVICE_RELOAD
:
3207 log_unit_warning(UNIT(s
), "Reload operation timed out. Killing reload process.");
3208 service_kill_control_processes(s
);
3209 s
->reload_result
= SERVICE_FAILURE_TIMEOUT
;
3210 service_enter_running(s
, SERVICE_SUCCESS
);
3214 log_unit_warning(UNIT(s
), "Stopping timed out. Terminating.");
3215 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
3218 case SERVICE_STOP_SIGABRT
:
3219 log_unit_warning(UNIT(s
), "State 'stop-sigabrt' timed out. Terminating.");
3220 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
3223 case SERVICE_STOP_SIGTERM
:
3224 if (s
->kill_context
.send_sigkill
) {
3225 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Killing.");
3226 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
3228 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Skipping SIGKILL.");
3229 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
3234 case SERVICE_STOP_SIGKILL
:
3235 /* Uh, we sent a SIGKILL and it is still not gone?
3236 * Must be something we cannot kill, so let's just be
3237 * weirded out and continue */
3239 log_unit_warning(UNIT(s
), "Processes still around after SIGKILL. Ignoring.");
3240 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
3243 case SERVICE_STOP_POST
:
3244 log_unit_warning(UNIT(s
), "State 'stop-post' timed out. Terminating.");
3245 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
3248 case SERVICE_FINAL_SIGTERM
:
3249 if (s
->kill_context
.send_sigkill
) {
3250 log_unit_warning(UNIT(s
), "State 'stop-final-sigterm' timed out. Killing.");
3251 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
3253 log_unit_warning(UNIT(s
), "State 'stop-final-sigterm' timed out. Skipping SIGKILL. Entering failed mode.");
3254 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, false);
3259 case SERVICE_FINAL_SIGKILL
:
3260 log_unit_warning(UNIT(s
), "Processes still around after final SIGKILL. Entering failed mode.");
3261 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, true);
3264 case SERVICE_AUTO_RESTART
:
3265 log_unit_info(UNIT(s
),
3266 s
->restart_usec
> 0 ?
3267 "Service hold-off time over, scheduling restart." :
3268 "Service has no hold-off time, scheduling restart.");
3269 service_enter_restart(s
);
3273 assert_not_reached("Timeout at wrong time.");
3279 static int service_dispatch_watchdog(sd_event_source
*source
, usec_t usec
, void *userdata
) {
3280 Service
*s
= SERVICE(userdata
);
3281 char t
[FORMAT_TIMESPAN_MAX
];
3282 usec_t watchdog_usec
;
3285 assert(source
== s
->watchdog_event_source
);
3287 watchdog_usec
= service_get_watchdog_usec(s
);
3289 log_unit_error(UNIT(s
), "Watchdog timeout (limit %s)!",
3290 format_timespan(t
, sizeof(t
), watchdog_usec
, 1));
3292 service_enter_signal(s
, SERVICE_STOP_SIGABRT
, SERVICE_FAILURE_WATCHDOG
);
3297 static void service_notify_message(Unit
*u
, pid_t pid
, char **tags
, FDSet
*fds
) {
3298 Service
*s
= SERVICE(u
);
3299 _cleanup_free_
char *cc
= NULL
;
3300 bool notify_dbus
= false;
3305 cc
= strv_join(tags
, ", ");
3307 if (s
->notify_access
== NOTIFY_NONE
) {
3308 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception is disabled.", pid
);
3310 } else if (s
->notify_access
== NOTIFY_MAIN
&& pid
!= s
->main_pid
) {
3311 if (s
->main_pid
!= 0)
3312 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID "PID_FMT
, pid
, s
->main_pid
);
3314 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID which is currently not known", pid
);
3316 } else if (s
->notify_access
== NOTIFY_EXEC
&& pid
!= s
->main_pid
&& pid
!= s
->control_pid
) {
3317 if (s
->main_pid
!= 0 && s
->control_pid
!= 0)
3318 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID "PID_FMT
" and control PID "PID_FMT
,
3319 pid
, s
->main_pid
, s
->control_pid
);
3320 else if (s
->main_pid
!= 0)
3321 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID "PID_FMT
, pid
, s
->main_pid
);
3322 else if (s
->control_pid
!= 0)
3323 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for control PID "PID_FMT
, pid
, s
->control_pid
);
3325 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID and control PID which are currently not known", pid
);
3328 log_unit_debug(u
, "Got notification message from PID "PID_FMT
" (%s)", pid
, isempty(cc
) ? "n/a" : cc
);
3330 /* Interpret MAINPID= */
3331 e
= strv_find_startswith(tags
, "MAINPID=");
3332 if (e
&& IN_SET(s
->state
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
)) {
3333 if (parse_pid(e
, &pid
) < 0)
3334 log_unit_warning(u
, "Failed to parse MAINPID= field in notification message: %s", e
);
3335 else if (pid
== s
->control_pid
)
3336 log_unit_warning(u
, "A control process cannot also be the main process");
3337 else if (pid
== getpid_cached() || pid
== 1)
3338 log_unit_warning(u
, "Service manager can't be main process, ignoring sd_notify() MAINPID= field");
3340 service_set_main_pid(s
, pid
);
3341 unit_watch_pid(UNIT(s
), pid
);
3346 /* Interpret RELOADING= */
3347 if (strv_find(tags
, "RELOADING=1")) {
3349 s
->notify_state
= NOTIFY_RELOADING
;
3351 if (s
->state
== SERVICE_RUNNING
)
3352 service_enter_reload_by_notify(s
);
3357 /* Interpret READY= */
3358 if (strv_find(tags
, "READY=1")) {
3360 s
->notify_state
= NOTIFY_READY
;
3362 /* Type=notify services inform us about completed
3363 * initialization with READY=1 */
3364 if (s
->type
== SERVICE_NOTIFY
&& s
->state
== SERVICE_START
)
3365 service_enter_start_post(s
);
3367 /* Sending READY=1 while we are reloading informs us
3368 * that the reloading is complete */
3369 if (s
->state
== SERVICE_RELOAD
&& s
->control_pid
== 0)
3370 service_enter_running(s
, SERVICE_SUCCESS
);
3375 /* Interpret STOPPING= */
3376 if (strv_find(tags
, "STOPPING=1")) {
3378 s
->notify_state
= NOTIFY_STOPPING
;
3380 if (s
->state
== SERVICE_RUNNING
)
3381 service_enter_stop_by_notify(s
);
3386 /* Interpret STATUS= */
3387 e
= strv_find_startswith(tags
, "STATUS=");
3389 _cleanup_free_
char *t
= NULL
;
3392 if (!utf8_is_valid(e
))
3393 log_unit_warning(u
, "Status message in notification message is not UTF-8 clean.");
3401 if (!streq_ptr(s
->status_text
, t
)) {
3403 free_and_replace(s
->status_text
, t
);
3409 /* Interpret ERRNO= */
3410 e
= strv_find_startswith(tags
, "ERRNO=");
3414 if (safe_atoi(e
, &status_errno
) < 0 || status_errno
< 0)
3415 log_unit_warning(u
, "Failed to parse ERRNO= field in notification message: %s", e
);
3417 if (s
->status_errno
!= status_errno
) {
3418 s
->status_errno
= status_errno
;
3424 /* Interpret WATCHDOG= */
3425 if (strv_find(tags
, "WATCHDOG=1"))
3426 service_reset_watchdog(s
);
3428 if (strv_find(tags
, "FDSTORE=1")) {
3431 name
= strv_find_startswith(tags
, "FDNAME=");
3432 if (name
&& !fdname_is_valid(name
)) {
3433 log_unit_warning(u
, "Passed FDNAME= name is invalid, ignoring.");
3437 service_add_fd_store_set(s
, fds
, name
);
3440 e
= strv_find_startswith(tags
, "WATCHDOG_USEC=");
3442 usec_t watchdog_override_usec
;
3443 if (safe_atou64(e
, &watchdog_override_usec
) < 0)
3444 log_unit_warning(u
, "Failed to parse WATCHDOG_USEC=%s", e
);
3446 service_reset_watchdog_timeout(s
, watchdog_override_usec
);
3449 /* Notify clients about changed status or main pid */
3451 unit_add_to_dbus_queue(u
);
3454 static int service_get_timeout(Unit
*u
, usec_t
*timeout
) {
3455 Service
*s
= SERVICE(u
);
3459 if (!s
->timer_event_source
)
3462 r
= sd_event_source_get_time(s
->timer_event_source
, &t
);
3465 if (t
== USEC_INFINITY
)
3472 static void service_bus_name_owner_change(
3475 const char *old_owner
,
3476 const char *new_owner
) {
3478 Service
*s
= SERVICE(u
);
3484 assert(streq(s
->bus_name
, name
));
3485 assert(old_owner
|| new_owner
);
3487 if (old_owner
&& new_owner
)
3488 log_unit_debug(u
, "D-Bus name %s changed owner from %s to %s", name
, old_owner
, new_owner
);
3490 log_unit_debug(u
, "D-Bus name %s no longer registered by %s", name
, old_owner
);
3492 log_unit_debug(u
, "D-Bus name %s now registered by %s", name
, new_owner
);
3494 s
->bus_name_good
= !!new_owner
;
3496 /* Track the current owner, so we can reconstruct changes after a daemon reload */
3497 r
= free_and_strdup(&s
->bus_name_owner
, new_owner
);
3499 log_unit_error_errno(u
, r
, "Unable to set new bus name owner %s: %m", new_owner
);
3503 if (s
->type
== SERVICE_DBUS
) {
3505 /* service_enter_running() will figure out what to
3507 if (s
->state
== SERVICE_RUNNING
)
3508 service_enter_running(s
, SERVICE_SUCCESS
);
3509 else if (s
->state
== SERVICE_START
&& new_owner
)
3510 service_enter_start_post(s
);
3512 } else if (new_owner
&&
3514 (s
->state
== SERVICE_START
||
3515 s
->state
== SERVICE_START_POST
||
3516 s
->state
== SERVICE_RUNNING
||
3517 s
->state
== SERVICE_RELOAD
)) {
3519 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
3522 /* Try to acquire PID from bus service */
3524 r
= sd_bus_get_name_creds(u
->manager
->api_bus
, name
, SD_BUS_CREDS_PID
, &creds
);
3526 r
= sd_bus_creds_get_pid(creds
, &pid
);
3528 log_unit_debug(u
, "D-Bus name %s is now owned by process " PID_FMT
, name
, pid
);
3530 service_set_main_pid(s
, pid
);
3531 unit_watch_pid(UNIT(s
), pid
);
3536 int service_set_socket_fd(Service
*s
, int fd
, Socket
*sock
, bool selinux_context_net
) {
3537 _cleanup_free_
char *peer
= NULL
;
3543 /* This is called by the socket code when instantiating a new service for a stream socket and the socket needs
3544 * to be configured. We take ownership of the passed fd on success. */
3546 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
3549 if (s
->socket_fd
>= 0)
3552 if (s
->state
!= SERVICE_DEAD
)
3555 if (getpeername_pretty(fd
, true, &peer
) >= 0) {
3557 if (UNIT(s
)->description
) {
3558 _cleanup_free_
char *a
;
3560 a
= strjoin(UNIT(s
)->description
, " (", peer
, ")");
3564 r
= unit_set_description(UNIT(s
), a
);
3566 r
= unit_set_description(UNIT(s
), peer
);
3572 r
= unit_add_two_dependencies(UNIT(sock
), UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT(s
), false);
3577 s
->socket_fd_selinux_context_net
= selinux_context_net
;
3579 unit_ref_set(&s
->accept_socket
, UNIT(sock
));
3583 static void service_reset_failed(Unit
*u
) {
3584 Service
*s
= SERVICE(u
);
3588 if (s
->state
== SERVICE_FAILED
)
3589 service_set_state(s
, SERVICE_DEAD
);
3591 s
->result
= SERVICE_SUCCESS
;
3592 s
->reload_result
= SERVICE_SUCCESS
;
3594 s
->flush_n_restarts
= false;
3597 static int service_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
3598 Service
*s
= SERVICE(u
);
3600 return unit_kill_common(u
, who
, signo
, s
->main_pid
, s
->control_pid
, error
);
3603 static int service_main_pid(Unit
*u
) {
3604 Service
*s
= SERVICE(u
);
3611 static int service_control_pid(Unit
*u
) {
3612 Service
*s
= SERVICE(u
);
3616 return s
->control_pid
;
3619 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
3620 [SERVICE_RESTART_NO
] = "no",
3621 [SERVICE_RESTART_ON_SUCCESS
] = "on-success",
3622 [SERVICE_RESTART_ON_FAILURE
] = "on-failure",
3623 [SERVICE_RESTART_ON_ABNORMAL
] = "on-abnormal",
3624 [SERVICE_RESTART_ON_WATCHDOG
] = "on-watchdog",
3625 [SERVICE_RESTART_ON_ABORT
] = "on-abort",
3626 [SERVICE_RESTART_ALWAYS
] = "always",
3629 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
3631 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
3632 [SERVICE_SIMPLE
] = "simple",
3633 [SERVICE_FORKING
] = "forking",
3634 [SERVICE_ONESHOT
] = "oneshot",
3635 [SERVICE_DBUS
] = "dbus",
3636 [SERVICE_NOTIFY
] = "notify",
3637 [SERVICE_IDLE
] = "idle"
3640 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
3642 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
3643 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
3644 [SERVICE_EXEC_START
] = "ExecStart",
3645 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
3646 [SERVICE_EXEC_RELOAD
] = "ExecReload",
3647 [SERVICE_EXEC_STOP
] = "ExecStop",
3648 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
3651 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
3653 static const char* const notify_state_table
[_NOTIFY_STATE_MAX
] = {
3654 [NOTIFY_UNKNOWN
] = "unknown",
3655 [NOTIFY_READY
] = "ready",
3656 [NOTIFY_RELOADING
] = "reloading",
3657 [NOTIFY_STOPPING
] = "stopping",
3660 DEFINE_STRING_TABLE_LOOKUP(notify_state
, NotifyState
);
3662 static const char* const service_result_table
[_SERVICE_RESULT_MAX
] = {
3663 [SERVICE_SUCCESS
] = "success",
3664 [SERVICE_FAILURE_RESOURCES
] = "resources",
3665 [SERVICE_FAILURE_PROTOCOL
] = "protocol",
3666 [SERVICE_FAILURE_TIMEOUT
] = "timeout",
3667 [SERVICE_FAILURE_EXIT_CODE
] = "exit-code",
3668 [SERVICE_FAILURE_SIGNAL
] = "signal",
3669 [SERVICE_FAILURE_CORE_DUMP
] = "core-dump",
3670 [SERVICE_FAILURE_WATCHDOG
] = "watchdog",
3671 [SERVICE_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
3674 DEFINE_STRING_TABLE_LOOKUP(service_result
, ServiceResult
);
3676 const UnitVTable service_vtable
= {
3677 .object_size
= sizeof(Service
),
3678 .exec_context_offset
= offsetof(Service
, exec_context
),
3679 .cgroup_context_offset
= offsetof(Service
, cgroup_context
),
3680 .kill_context_offset
= offsetof(Service
, kill_context
),
3681 .exec_runtime_offset
= offsetof(Service
, exec_runtime
),
3682 .dynamic_creds_offset
= offsetof(Service
, dynamic_creds
),
3688 .private_section
= "Service",
3690 .init
= service_init
,
3691 .done
= service_done
,
3692 .load
= service_load
,
3693 .release_resources
= service_release_resources
,
3695 .coldplug
= service_coldplug
,
3697 .dump
= service_dump
,
3699 .start
= service_start
,
3700 .stop
= service_stop
,
3701 .reload
= service_reload
,
3703 .can_reload
= service_can_reload
,
3705 .kill
= service_kill
,
3707 .serialize
= service_serialize
,
3708 .deserialize_item
= service_deserialize_item
,
3710 .active_state
= service_active_state
,
3711 .sub_state_to_string
= service_sub_state_to_string
,
3713 .check_gc
= service_check_gc
,
3715 .sigchld_event
= service_sigchld_event
,
3717 .reset_failed
= service_reset_failed
,
3719 .notify_cgroup_empty
= service_notify_cgroup_empty_event
,
3720 .notify_message
= service_notify_message
,
3722 .main_pid
= service_main_pid
,
3723 .control_pid
= service_control_pid
,
3725 .bus_name_owner_change
= service_bus_name_owner_change
,
3727 .bus_vtable
= bus_service_vtable
,
3728 .bus_set_property
= bus_service_set_property
,
3729 .bus_commit_properties
= bus_service_commit_properties
,
3731 .get_timeout
= service_get_timeout
,
3732 .can_transient
= true,
3734 .status_message_formats
= {
3735 .starting_stopping
= {
3736 [0] = "Starting %s...",
3737 [1] = "Stopping %s...",
3739 .finished_start_job
= {
3740 [JOB_DONE
] = "Started %s.",
3741 [JOB_FAILED
] = "Failed to start %s.",
3743 .finished_stop_job
= {
3744 [JOB_DONE
] = "Stopped %s.",
3745 [JOB_FAILED
] = "Stopped (with error) %s.",