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 (IN_SET(watchdog_usec
, 0, 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 if (s
->n_keep_fd_store
> 0)
300 log_unit_debug(UNIT(s
), "Releasing all stored fds");
302 service_fd_store_unlink(s
->fd_store
);
304 assert(s
->n_fd_store
== 0);
307 static void service_release_resources(Unit
*u
) {
308 Service
*s
= SERVICE(u
);
312 if (!s
->fd_store
&& s
->stdin_fd
< 0 && s
->stdout_fd
< 0 && s
->stderr_fd
< 0)
315 log_unit_debug(u
, "Releasing resources.");
317 s
->stdin_fd
= safe_close(s
->stdin_fd
);
318 s
->stdout_fd
= safe_close(s
->stdout_fd
);
319 s
->stderr_fd
= safe_close(s
->stderr_fd
);
321 service_release_fd_store(s
);
324 static void service_done(Unit
*u
) {
325 Service
*s
= SERVICE(u
);
329 s
->pid_file
= mfree(s
->pid_file
);
330 s
->status_text
= mfree(s
->status_text
);
332 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
333 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
334 s
->control_command
= NULL
;
335 s
->main_command
= NULL
;
337 dynamic_creds_unref(&s
->dynamic_creds
);
339 exit_status_set_free(&s
->restart_prevent_status
);
340 exit_status_set_free(&s
->restart_force_status
);
341 exit_status_set_free(&s
->success_status
);
343 /* This will leak a process, but at least no memory or any of
345 service_unwatch_main_pid(s
);
346 service_unwatch_control_pid(s
);
347 service_unwatch_pid_file(s
);
350 unit_unwatch_bus_name(u
, s
->bus_name
);
351 s
->bus_name
= mfree(s
->bus_name
);
354 s
->bus_name_owner
= mfree(s
->bus_name_owner
);
356 service_close_socket_fd(s
);
357 s
->peer
= socket_peer_unref(s
->peer
);
359 unit_ref_unset(&s
->accept_socket
);
361 service_stop_watchdog(s
);
363 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
365 service_release_resources(u
);
368 static int on_fd_store_io(sd_event_source
*e
, int fd
, uint32_t revents
, void *userdata
) {
369 ServiceFDStore
*fs
= userdata
;
374 /* If we get either EPOLLHUP or EPOLLERR, it's time to remove this entry from the fd store */
375 log_unit_debug(UNIT(fs
->service
),
376 "Received %s on stored fd %d (%s), closing.",
377 revents
& EPOLLERR
? "EPOLLERR" : "EPOLLHUP",
378 fs
->fd
, strna(fs
->fdname
));
379 service_fd_store_unlink(fs
);
383 static int service_add_fd_store(Service
*s
, int fd
, const char *name
) {
387 /* fd is always consumed if we return >= 0 */
392 if (s
->n_fd_store
>= s
->n_fd_store_max
)
393 return -EXFULL
; /* Our store is full.
394 * Use this errno rather than E[NM]FILE to distinguish from
395 * the case where systemd itself hits the file limit. */
397 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
398 r
= same_fd(fs
->fd
, fd
);
403 return 0; /* fd already included */
407 fs
= new0(ServiceFDStore
, 1);
413 fs
->fdname
= strdup(name
?: "stored");
419 r
= sd_event_add_io(UNIT(s
)->manager
->event
, &fs
->event_source
, fd
, 0, on_fd_store_io
, fs
);
420 if (r
< 0 && r
!= -EPERM
) { /* EPERM indicates fds that aren't pollable, which is OK */
425 (void) sd_event_source_set_description(fs
->event_source
, "service-fd-store");
427 LIST_PREPEND(fd_store
, s
->fd_store
, fs
);
430 return 1; /* fd newly stored */
433 static int service_add_fd_store_set(Service
*s
, FDSet
*fds
, const char *name
) {
438 while (fdset_size(fds
) > 0) {
439 _cleanup_close_
int fd
= -1;
441 fd
= fdset_steal_first(fds
);
445 r
= service_add_fd_store(s
, fd
, name
);
447 return log_unit_warning_errno(UNIT(s
), r
,
448 "Cannot store more fds than FileDescriptorStoreMax=%u, closing remaining.",
451 return log_unit_error_errno(UNIT(s
), r
, "Failed to add fd to store: %m");
453 log_unit_debug(UNIT(s
), "Added fd %u (%s) to fd store.", fd
, strna(name
));
460 static int service_arm_timer(Service
*s
, usec_t usec
) {
465 if (s
->timer_event_source
) {
466 r
= sd_event_source_set_time(s
->timer_event_source
, usec
);
470 return sd_event_source_set_enabled(s
->timer_event_source
, SD_EVENT_ONESHOT
);
473 if (usec
== USEC_INFINITY
)
476 r
= sd_event_add_time(
477 UNIT(s
)->manager
->event
,
478 &s
->timer_event_source
,
481 service_dispatch_timer
, s
);
485 (void) sd_event_source_set_description(s
->timer_event_source
, "service-timer");
490 static int service_verify(Service
*s
) {
493 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
496 if (!s
->exec_command
[SERVICE_EXEC_START
] && !s
->exec_command
[SERVICE_EXEC_STOP
]) {
497 log_unit_error(UNIT(s
), "Service lacks both ExecStart= and ExecStop= setting. Refusing.");
501 if (s
->type
!= SERVICE_ONESHOT
&& !s
->exec_command
[SERVICE_EXEC_START
]) {
502 log_unit_error(UNIT(s
), "Service has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
506 if (!s
->remain_after_exit
&& !s
->exec_command
[SERVICE_EXEC_START
]) {
507 log_unit_error(UNIT(s
), "Service has no ExecStart= setting, which is only allowed for RemainAfterExit=yes services. Refusing.");
511 if (s
->type
!= SERVICE_ONESHOT
&& s
->exec_command
[SERVICE_EXEC_START
]->command_next
) {
512 log_unit_error(UNIT(s
), "Service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
516 if (s
->type
== SERVICE_ONESHOT
&& s
->restart
!= SERVICE_RESTART_NO
) {
517 log_unit_error(UNIT(s
), "Service has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing.");
521 if (s
->type
== SERVICE_ONESHOT
&& !exit_status_set_is_empty(&s
->restart_force_status
)) {
522 log_unit_error(UNIT(s
), "Service has RestartForceStatus= set, which isn't allowed for Type=oneshot services. Refusing.");
526 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
) {
527 log_unit_error(UNIT(s
), "Service is of type D-Bus but no D-Bus service name has been specified. Refusing.");
531 if (s
->bus_name
&& s
->type
!= SERVICE_DBUS
)
532 log_unit_warning(UNIT(s
), "Service has a D-Bus service name specified, but is not of type dbus. Ignoring.");
534 if (s
->exec_context
.pam_name
&& !IN_SET(s
->kill_context
.kill_mode
, KILL_CONTROL_GROUP
, KILL_MIXED
)) {
535 log_unit_error(UNIT(s
), "Service has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing.");
539 if (s
->usb_function_descriptors
&& !s
->usb_function_strings
)
540 log_unit_warning(UNIT(s
), "Service has USBFunctionDescriptors= setting, but no USBFunctionStrings=. Ignoring.");
542 if (!s
->usb_function_descriptors
&& s
->usb_function_strings
)
543 log_unit_warning(UNIT(s
), "Service has USBFunctionStrings= setting, but no USBFunctionDescriptors=. Ignoring.");
545 if (s
->runtime_max_usec
!= USEC_INFINITY
&& s
->type
== SERVICE_ONESHOT
)
546 log_unit_warning(UNIT(s
), "MaxRuntimeSec= has no effect in combination with Type=oneshot. Ignoring.");
551 static int service_add_default_dependencies(Service
*s
) {
556 if (!UNIT(s
)->default_dependencies
)
559 /* Add a number of automatic dependencies useful for the
560 * majority of services. */
562 if (MANAGER_IS_SYSTEM(UNIT(s
)->manager
)) {
563 /* First, pull in the really early boot stuff, and
564 * require it, so that we fail if we can't acquire
567 r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_SYSINIT_TARGET
, NULL
, true, UNIT_DEPENDENCY_DEFAULT
);
572 /* In the --user instance there's no sysinit.target,
573 * in that case require basic.target instead. */
575 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, NULL
, true, UNIT_DEPENDENCY_DEFAULT
);
580 /* Second, if the rest of the base system is in the same
581 * transaction, order us after it, but do not pull it in or
582 * even require it. */
583 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, SPECIAL_BASIC_TARGET
, NULL
, true, UNIT_DEPENDENCY_DEFAULT
);
587 /* Third, add us in for normal shutdown. */
588 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true, UNIT_DEPENDENCY_DEFAULT
);
591 static void service_fix_output(Service
*s
) {
594 /* If nothing has been explicitly configured, patch default
595 * output in. If input is socket/tty we avoid this however,
596 * since in that case we want output to default to the same
597 * place as we read input from. */
599 if (s
->exec_context
.std_error
== EXEC_OUTPUT_INHERIT
&&
600 s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
601 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
602 s
->exec_context
.std_error
= UNIT(s
)->manager
->default_std_error
;
604 if (s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
605 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
606 s
->exec_context
.std_output
= UNIT(s
)->manager
->default_std_output
;
609 static int service_setup_bus_name(Service
*s
) {
617 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_REQUIRES
, SPECIAL_DBUS_SOCKET
, NULL
, true, UNIT_DEPENDENCY_FILE
);
619 return log_unit_error_errno(UNIT(s
), r
, "Failed to add dependency on " SPECIAL_DBUS_SOCKET
": %m");
621 /* We always want to be ordered against dbus.socket if both are in the transaction. */
622 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, SPECIAL_DBUS_SOCKET
, NULL
, true, UNIT_DEPENDENCY_FILE
);
624 return log_unit_error_errno(UNIT(s
), r
, "Failed to add dependency on " SPECIAL_DBUS_SOCKET
": %m");
626 r
= unit_watch_bus_name(UNIT(s
), s
->bus_name
);
628 return log_unit_error_errno(UNIT(s
), r
, "Two services allocated for the same bus name %s, refusing operation.", s
->bus_name
);
630 return log_unit_error_errno(UNIT(s
), r
, "Cannot watch bus name %s: %m", s
->bus_name
);
635 static int service_add_extras(Service
*s
) {
640 if (s
->type
== _SERVICE_TYPE_INVALID
) {
641 /* Figure out a type automatically */
643 s
->type
= SERVICE_DBUS
;
644 else if (s
->exec_command
[SERVICE_EXEC_START
])
645 s
->type
= SERVICE_SIMPLE
;
647 s
->type
= SERVICE_ONESHOT
;
650 /* Oneshot services have disabled start timeout by default */
651 if (s
->type
== SERVICE_ONESHOT
&& !s
->start_timeout_defined
)
652 s
->timeout_start_usec
= USEC_INFINITY
;
654 service_fix_output(s
);
656 r
= unit_patch_contexts(UNIT(s
));
660 r
= unit_add_exec_dependencies(UNIT(s
), &s
->exec_context
);
664 r
= unit_set_default_slice(UNIT(s
));
668 if (s
->type
== SERVICE_NOTIFY
&& s
->notify_access
== NOTIFY_NONE
)
669 s
->notify_access
= NOTIFY_MAIN
;
671 if (s
->watchdog_usec
> 0 && s
->notify_access
== NOTIFY_NONE
)
672 s
->notify_access
= NOTIFY_MAIN
;
674 r
= service_add_default_dependencies(s
);
678 r
= service_setup_bus_name(s
);
685 static int service_load(Unit
*u
) {
686 Service
*s
= SERVICE(u
);
691 /* Load a .service file */
692 r
= unit_load_fragment(u
);
696 /* Still nothing found? Then let's give up */
697 if (u
->load_state
== UNIT_STUB
)
700 /* This is a new unit? Then let's add in some extras */
701 if (u
->load_state
== UNIT_LOADED
) {
703 /* We were able to load something, then let's add in
704 * the dropin directories. */
705 r
= unit_load_dropin(u
);
709 /* This is a new unit? Then let's add in some
711 r
= service_add_extras(s
);
716 return service_verify(s
);
719 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
720 ServiceExecCommand c
;
721 Service
*s
= SERVICE(u
);
726 prefix
= strempty(prefix
);
727 prefix2
= strjoina(prefix
, "\t");
730 "%sService State: %s\n"
732 "%sReload Result: %s\n"
733 "%sPermissionsStartOnly: %s\n"
734 "%sRootDirectoryStartOnly: %s\n"
735 "%sRemainAfterExit: %s\n"
736 "%sGuessMainPID: %s\n"
739 "%sNotifyAccess: %s\n"
740 "%sNotifyState: %s\n",
741 prefix
, service_state_to_string(s
->state
),
742 prefix
, service_result_to_string(s
->result
),
743 prefix
, service_result_to_string(s
->reload_result
),
744 prefix
, yes_no(s
->permissions_start_only
),
745 prefix
, yes_no(s
->root_directory_start_only
),
746 prefix
, yes_no(s
->remain_after_exit
),
747 prefix
, yes_no(s
->guess_main_pid
),
748 prefix
, service_type_to_string(s
->type
),
749 prefix
, service_restart_to_string(s
->restart
),
750 prefix
, notify_access_to_string(s
->notify_access
),
751 prefix
, notify_state_to_string(s
->notify_state
));
753 if (s
->control_pid
> 0)
755 "%sControl PID: "PID_FMT
"\n",
756 prefix
, s
->control_pid
);
760 "%sMain PID: "PID_FMT
"\n"
761 "%sMain PID Known: %s\n"
762 "%sMain PID Alien: %s\n",
764 prefix
, yes_no(s
->main_pid_known
),
765 prefix
, yes_no(s
->main_pid_alien
));
770 prefix
, s
->pid_file
);
775 "%sBus Name Good: %s\n",
777 prefix
, yes_no(s
->bus_name_good
));
779 if (UNIT_ISSET(s
->accept_socket
))
781 "%sAccept Socket: %s\n",
782 prefix
, UNIT_DEREF(s
->accept_socket
)->id
);
784 kill_context_dump(&s
->kill_context
, f
, prefix
);
785 exec_context_dump(&s
->exec_context
, f
, prefix
);
787 for (c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
789 if (!s
->exec_command
[c
])
792 fprintf(f
, "%s-> %s:\n",
793 prefix
, service_exec_command_to_string(c
));
795 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
799 fprintf(f
, "%sStatus Text: %s\n",
800 prefix
, s
->status_text
);
802 if (s
->n_fd_store_max
> 0)
804 "%sFile Descriptor Store Max: %u\n"
805 "%sFile Descriptor Store Current: %u\n",
806 prefix
, s
->n_fd_store_max
,
807 prefix
, s
->n_fd_store
);
809 cgroup_context_dump(&s
->cgroup_context
, f
, prefix
);
812 static int service_load_pid_file(Service
*s
, bool may_warn
) {
813 _cleanup_free_
char *k
= NULL
;
822 r
= read_one_line_file(s
->pid_file
, &k
);
825 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
));
829 r
= parse_pid(k
, &pid
);
832 log_unit_info_errno(UNIT(s
), r
, "Failed to read PID from file %s: %m", s
->pid_file
);
836 if (!pid_is_alive(pid
)) {
838 log_unit_info(UNIT(s
), "PID "PID_FMT
" read from file %s does not exist or is a zombie.", pid
, s
->pid_file
);
842 if (s
->main_pid_known
) {
843 if (pid
== s
->main_pid
)
846 log_unit_debug(UNIT(s
), "Main PID changing: "PID_FMT
" -> "PID_FMT
, s
->main_pid
, pid
);
848 service_unwatch_main_pid(s
);
849 s
->main_pid_known
= false;
851 log_unit_debug(UNIT(s
), "Main PID loaded: "PID_FMT
, pid
);
853 r
= service_set_main_pid(s
, pid
);
857 r
= unit_watch_pid(UNIT(s
), pid
);
858 if (r
< 0) /* FIXME: we need to do something here */
859 return log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" for service: %m", pid
);
864 static void service_search_main_pid(Service
*s
) {
870 /* If we know it anyway, don't ever fallback to unreliable
872 if (s
->main_pid_known
)
875 if (!s
->guess_main_pid
)
878 assert(s
->main_pid
<= 0);
880 if (unit_search_main_pid(UNIT(s
), &pid
) < 0)
883 log_unit_debug(UNIT(s
), "Main PID guessed: "PID_FMT
, pid
);
884 if (service_set_main_pid(s
, pid
) < 0)
887 r
= unit_watch_pid(UNIT(s
), pid
);
889 /* FIXME: we need to do something here */
890 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" from: %m", pid
);
893 static void service_set_state(Service
*s
, ServiceState state
) {
894 ServiceState old_state
;
895 const UnitActiveState
*table
;
899 table
= s
->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
901 old_state
= s
->state
;
904 service_unwatch_pid_file(s
);
907 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
910 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
911 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
,
912 SERVICE_AUTO_RESTART
))
913 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
916 SERVICE_START
, SERVICE_START_POST
,
917 SERVICE_RUNNING
, SERVICE_RELOAD
,
918 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
919 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
920 service_unwatch_main_pid(s
);
921 s
->main_command
= NULL
;
925 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
927 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
928 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
929 service_unwatch_control_pid(s
);
930 s
->control_command
= NULL
;
931 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
934 if (IN_SET(state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
935 unit_unwatch_all_pids(UNIT(s
));
938 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
939 SERVICE_RUNNING
, SERVICE_RELOAD
,
940 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
941 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
) &&
942 !(state
== SERVICE_DEAD
&& UNIT(s
)->job
))
943 service_close_socket_fd(s
);
945 if (!IN_SET(state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
946 service_stop_watchdog(s
);
948 /* For the inactive states unit_notify() will trim the cgroup,
949 * but for exit we have to do that ourselves... */
950 if (state
== SERVICE_EXITED
&& !MANAGER_IS_RELOADING(UNIT(s
)->manager
))
951 unit_prune_cgroup(UNIT(s
));
953 /* For remain_after_exit services, let's see if we can "release" the
954 * hold on the console, since unit_notify() only does that in case of
956 if (state
== SERVICE_EXITED
&&
957 s
->remain_after_exit
&&
958 UNIT(s
)->manager
->n_on_console
> 0) {
962 ec
= unit_get_exec_context(UNIT(s
));
963 if (ec
&& exec_context_may_touch_console(ec
)) {
964 Manager
*m
= UNIT(s
)->manager
;
967 if (m
->n_on_console
== 0)
968 /* unset no_console_output flag, since the console is free */
969 m
->no_console_output
= false;
973 if (old_state
!= state
)
974 log_unit_debug(UNIT(s
), "Changed %s -> %s", service_state_to_string(old_state
), service_state_to_string(state
));
976 unit_notify(UNIT(s
), table
[old_state
], table
[state
], s
->reload_result
== SERVICE_SUCCESS
);
979 static usec_t
service_coldplug_timeout(Service
*s
) {
982 switch (s
->deserialized_state
) {
984 case SERVICE_START_PRE
:
986 case SERVICE_START_POST
:
988 return usec_add(UNIT(s
)->state_change_timestamp
.monotonic
, s
->timeout_start_usec
);
990 case SERVICE_RUNNING
:
991 return usec_add(UNIT(s
)->active_enter_timestamp
.monotonic
, s
->runtime_max_usec
);
994 case SERVICE_STOP_SIGABRT
:
995 case SERVICE_STOP_SIGTERM
:
996 case SERVICE_STOP_SIGKILL
:
997 case SERVICE_STOP_POST
:
998 case SERVICE_FINAL_SIGTERM
:
999 case SERVICE_FINAL_SIGKILL
:
1000 return usec_add(UNIT(s
)->state_change_timestamp
.monotonic
, s
->timeout_stop_usec
);
1002 case SERVICE_AUTO_RESTART
:
1003 return usec_add(UNIT(s
)->inactive_enter_timestamp
.monotonic
, s
->restart_usec
);
1006 return USEC_INFINITY
;
1010 static int service_coldplug(Unit
*u
) {
1011 Service
*s
= SERVICE(u
);
1015 assert(s
->state
== SERVICE_DEAD
);
1017 if (s
->deserialized_state
== s
->state
)
1020 r
= service_arm_timer(s
, service_coldplug_timeout(s
));
1024 if (s
->main_pid
> 0 &&
1025 pid_is_unwaited(s
->main_pid
) &&
1026 ((s
->deserialized_state
== SERVICE_START
&& IN_SET(s
->type
, SERVICE_FORKING
, SERVICE_DBUS
, SERVICE_ONESHOT
, SERVICE_NOTIFY
)) ||
1027 IN_SET(s
->deserialized_state
,
1028 SERVICE_START
, SERVICE_START_POST
,
1029 SERVICE_RUNNING
, SERVICE_RELOAD
,
1030 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1031 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))) {
1032 r
= unit_watch_pid(UNIT(s
), s
->main_pid
);
1037 if (s
->control_pid
> 0 &&
1038 pid_is_unwaited(s
->control_pid
) &&
1039 IN_SET(s
->deserialized_state
,
1040 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
1042 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1043 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
1044 r
= unit_watch_pid(UNIT(s
), s
->control_pid
);
1049 if (!IN_SET(s
->deserialized_state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
1050 unit_watch_all_pids(UNIT(s
));
1052 if (IN_SET(s
->deserialized_state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
1053 service_start_watchdog(s
);
1055 if (!IN_SET(s
->deserialized_state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
1056 (void) unit_setup_dynamic_creds(u
);
1058 if (UNIT_ISSET(s
->accept_socket
)) {
1059 Socket
* socket
= SOCKET(UNIT_DEREF(s
->accept_socket
));
1061 if (socket
->max_connections_per_source
> 0) {
1064 /* Make a best-effort attempt at bumping the connection count */
1065 if (socket_acquire_peer(socket
, s
->socket_fd
, &peer
) > 0) {
1066 socket_peer_unref(s
->peer
);
1072 service_set_state(s
, s
->deserialized_state
);
1076 static int service_collect_fds(Service
*s
,
1079 unsigned *n_storage_fds
,
1080 unsigned *n_socket_fds
) {
1082 _cleanup_strv_free_
char **rfd_names
= NULL
;
1083 _cleanup_free_
int *rfds
= NULL
;
1084 unsigned rn_socket_fds
= 0, rn_storage_fds
= 0;
1090 assert(n_socket_fds
);
1092 if (s
->socket_fd
>= 0) {
1094 /* Pass the per-connection socket */
1099 rfds
[0] = s
->socket_fd
;
1101 rfd_names
= strv_new("connection", NULL
);
1111 /* Pass all our configured sockets for singleton services */
1113 HASHMAP_FOREACH_KEY(v
, u
, UNIT(s
)->dependencies
[UNIT_TRIGGERED_BY
], i
) {
1114 _cleanup_free_
int *cfds
= NULL
;
1118 if (u
->type
!= UNIT_SOCKET
)
1123 cn_fds
= socket_collect_fds(sock
, &cfds
);
1132 rn_socket_fds
= cn_fds
;
1138 t
= realloc(rfds
, (rn_socket_fds
+ cn_fds
) * sizeof(int));
1142 memcpy(t
+ rn_socket_fds
, cfds
, cn_fds
* sizeof(int));
1145 rn_socket_fds
+= cn_fds
;
1148 r
= strv_extend_n(&rfd_names
, socket_fdname(sock
), cn_fds
);
1154 if (s
->n_fd_store
> 0) {
1160 t
= realloc(rfds
, (rn_socket_fds
+ s
->n_fd_store
) * sizeof(int));
1166 nl
= realloc(rfd_names
, (rn_socket_fds
+ s
->n_fd_store
+ 1) * sizeof(char*));
1171 n_fds
= rn_socket_fds
;
1173 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
1174 rfds
[n_fds
] = fs
->fd
;
1175 rfd_names
[n_fds
] = strdup(strempty(fs
->fdname
));
1176 if (!rfd_names
[n_fds
])
1183 rfd_names
[n_fds
] = NULL
;
1187 *fd_names
= rfd_names
;
1188 *n_socket_fds
= rn_socket_fds
;
1189 *n_storage_fds
= rn_storage_fds
;
1197 static bool service_exec_needs_notify_socket(Service
*s
, ExecFlags flags
) {
1200 /* Notifications are accepted depending on the process and
1201 * the access setting of the service:
1202 * process: \ access: NONE MAIN EXEC ALL
1203 * main no yes yes yes
1204 * control no no yes yes
1205 * other (forked) no no no yes */
1207 if (flags
& EXEC_IS_CONTROL
)
1208 /* A control process */
1209 return IN_SET(s
->notify_access
, NOTIFY_EXEC
, NOTIFY_ALL
);
1211 /* We only spawn main processes and control processes, so any
1212 * process that is not a control process is a main process */
1213 return s
->notify_access
!= NOTIFY_NONE
;
1216 static int service_spawn(
1223 _cleanup_strv_free_
char **final_env
= NULL
, **our_env
= NULL
, **fd_names
= NULL
;
1224 _cleanup_free_
int *fds
= NULL
;
1225 unsigned n_storage_fds
= 0, n_socket_fds
= 0, n_env
= 0;
1228 ExecParameters exec_params
= {
1241 if (flags
& EXEC_IS_CONTROL
) {
1242 /* If this is a control process, mask the permissions/chroot application if this is requested. */
1243 if (s
->permissions_start_only
)
1244 exec_params
.flags
&= ~EXEC_APPLY_SANDBOXING
;
1245 if (s
->root_directory_start_only
)
1246 exec_params
.flags
&= ~EXEC_APPLY_CHROOT
;
1249 (void) unit_realize_cgroup(UNIT(s
));
1250 if (s
->reset_accounting
) {
1251 (void) unit_reset_cpu_accounting(UNIT(s
));
1252 (void) unit_reset_ip_accounting(UNIT(s
));
1253 s
->reset_accounting
= false;
1256 unit_export_state_files(UNIT(s
));
1258 r
= unit_setup_exec_runtime(UNIT(s
));
1262 r
= unit_setup_dynamic_creds(UNIT(s
));
1266 if ((flags
& EXEC_PASS_FDS
) ||
1267 s
->exec_context
.std_input
== EXEC_INPUT_SOCKET
||
1268 s
->exec_context
.std_output
== EXEC_OUTPUT_SOCKET
||
1269 s
->exec_context
.std_error
== EXEC_OUTPUT_SOCKET
) {
1271 r
= service_collect_fds(s
, &fds
, &fd_names
, &n_storage_fds
, &n_socket_fds
);
1275 log_unit_debug(UNIT(s
), "Passing %i fds to service", n_storage_fds
+ n_socket_fds
);
1278 r
= service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), timeout
));
1282 our_env
= new0(char*, 9);
1286 if (service_exec_needs_notify_socket(s
, flags
))
1287 if (asprintf(our_env
+ n_env
++, "NOTIFY_SOCKET=%s", UNIT(s
)->manager
->notify_socket
) < 0)
1290 if (s
->main_pid
> 0)
1291 if (asprintf(our_env
+ n_env
++, "MAINPID="PID_FMT
, s
->main_pid
) < 0)
1294 if (MANAGER_IS_USER(UNIT(s
)->manager
))
1295 if (asprintf(our_env
+ n_env
++, "MANAGERPID="PID_FMT
, getpid_cached()) < 0)
1298 if (s
->socket_fd
>= 0) {
1299 union sockaddr_union sa
;
1300 socklen_t salen
= sizeof(sa
);
1302 r
= getpeername(s
->socket_fd
, &sa
.sa
, &salen
);
1306 /* ENOTCONN is legitimate if the endpoint disappeared on shutdown.
1307 * This connection is over, but the socket unit lives on. */
1308 if (r
!= -ENOTCONN
|| !IN_SET(s
->control_command_id
, SERVICE_EXEC_STOP
, SERVICE_EXEC_STOP_POST
))
1312 if (r
== 0 && IN_SET(sa
.sa
.sa_family
, AF_INET
, AF_INET6
, AF_VSOCK
)) {
1313 _cleanup_free_
char *addr
= NULL
;
1317 r
= sockaddr_pretty(&sa
.sa
, salen
, true, false, &addr
);
1321 t
= strappend("REMOTE_ADDR=", addr
);
1324 our_env
[n_env
++] = t
;
1326 r
= sockaddr_port(&sa
.sa
, &port
);
1330 if (asprintf(&t
, "REMOTE_PORT=%u", port
) < 0)
1332 our_env
[n_env
++] = t
;
1336 if (flags
& EXEC_SETENV_RESULT
) {
1337 if (asprintf(our_env
+ n_env
++, "SERVICE_RESULT=%s", service_result_to_string(s
->result
)) < 0)
1340 if (s
->main_exec_status
.pid
> 0 &&
1341 dual_timestamp_is_set(&s
->main_exec_status
.exit_timestamp
)) {
1342 if (asprintf(our_env
+ n_env
++, "EXIT_CODE=%s", sigchld_code_to_string(s
->main_exec_status
.code
)) < 0)
1345 if (s
->main_exec_status
.code
== CLD_EXITED
)
1346 r
= asprintf(our_env
+ n_env
++, "EXIT_STATUS=%i", s
->main_exec_status
.status
);
1348 r
= asprintf(our_env
+ n_env
++, "EXIT_STATUS=%s", signal_to_string(s
->main_exec_status
.status
));
1354 manager_set_exec_params(UNIT(s
)->manager
, &exec_params
);
1355 unit_set_exec_params(UNIT(s
), &exec_params
);
1357 final_env
= strv_env_merge(2, exec_params
.environment
, our_env
, NULL
);
1361 if ((flags
& EXEC_IS_CONTROL
) && UNIT(s
)->cgroup_path
) {
1362 exec_params
.cgroup_path
= strjoina(UNIT(s
)->cgroup_path
, "/control");
1363 (void) cg_create(SYSTEMD_CGROUP_CONTROLLER
, exec_params
.cgroup_path
);
1366 /* System services should get a new keyring by default. */
1367 SET_FLAG(exec_params
.flags
, EXEC_NEW_KEYRING
, MANAGER_IS_SYSTEM(UNIT(s
)->manager
));
1369 /* System D-Bus needs nss-systemd disabled, so that we don't deadlock */
1370 SET_FLAG(exec_params
.flags
, EXEC_NSS_BYPASS_BUS
,
1371 MANAGER_IS_SYSTEM(UNIT(s
)->manager
) && unit_has_name(UNIT(s
), SPECIAL_DBUS_SERVICE
));
1373 exec_params
.argv
= c
->argv
;
1374 exec_params
.environment
= final_env
;
1375 exec_params
.fds
= fds
;
1376 exec_params
.fd_names
= fd_names
;
1377 exec_params
.n_storage_fds
= n_storage_fds
;
1378 exec_params
.n_socket_fds
= n_socket_fds
;
1379 exec_params
.watchdog_usec
= s
->watchdog_usec
;
1380 exec_params
.selinux_context_net
= s
->socket_fd_selinux_context_net
;
1381 if (s
->type
== SERVICE_IDLE
)
1382 exec_params
.idle_pipe
= UNIT(s
)->manager
->idle_pipe
;
1383 exec_params
.stdin_fd
= s
->stdin_fd
;
1384 exec_params
.stdout_fd
= s
->stdout_fd
;
1385 exec_params
.stderr_fd
= s
->stderr_fd
;
1387 r
= exec_spawn(UNIT(s
),
1397 r
= unit_watch_pid(UNIT(s
), pid
);
1398 if (r
< 0) /* FIXME: we need to do something here */
1406 static int main_pid_good(Service
*s
) {
1409 /* Returns 0 if the pid is dead, > 0 if it is good, < 0 if we don't know */
1411 /* If we know the pid file, then let's just check if it is
1413 if (s
->main_pid_known
) {
1415 /* If it's an alien child let's check if it is still
1417 if (s
->main_pid_alien
&& s
->main_pid
> 0)
1418 return pid_is_alive(s
->main_pid
);
1420 /* .. otherwise assume we'll get a SIGCHLD for it,
1421 * which we really should wait for to collect exit
1422 * status and code */
1423 return s
->main_pid
> 0;
1426 /* We don't know the pid */
1430 static int control_pid_good(Service
*s
) {
1433 /* Returns 0 if the control PID is dead, > 0 if it is good. We never actually return < 0 here, but in order to
1434 * make this function as similar as possible to main_pid_good() and cgroup_good(), we pretend that < 0 also
1435 * means: we can't figure it out. */
1437 return s
->control_pid
> 0;
1440 static int cgroup_good(Service
*s
) {
1445 /* Returns 0 if the cgroup is empty or doesn't exist, > 0 if it is exists and is populated, < 0 if we can't
1448 if (!UNIT(s
)->cgroup_path
)
1451 r
= cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, UNIT(s
)->cgroup_path
);
1458 static bool service_shall_restart(Service
*s
) {
1461 /* Don't restart after manual stops */
1462 if (s
->forbid_restart
)
1465 /* Never restart if this is configured as special exception */
1466 if (exit_status_set_test(&s
->restart_prevent_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
))
1469 /* Restart if the exit code/status are configured as restart triggers */
1470 if (exit_status_set_test(&s
->restart_force_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
))
1473 switch (s
->restart
) {
1475 case SERVICE_RESTART_NO
:
1478 case SERVICE_RESTART_ALWAYS
:
1481 case SERVICE_RESTART_ON_SUCCESS
:
1482 return s
->result
== SERVICE_SUCCESS
;
1484 case SERVICE_RESTART_ON_FAILURE
:
1485 return s
->result
!= SERVICE_SUCCESS
;
1487 case SERVICE_RESTART_ON_ABNORMAL
:
1488 return !IN_SET(s
->result
, SERVICE_SUCCESS
, SERVICE_FAILURE_EXIT_CODE
);
1490 case SERVICE_RESTART_ON_WATCHDOG
:
1491 return s
->result
== SERVICE_FAILURE_WATCHDOG
;
1493 case SERVICE_RESTART_ON_ABORT
:
1494 return IN_SET(s
->result
, SERVICE_FAILURE_SIGNAL
, SERVICE_FAILURE_CORE_DUMP
);
1497 assert_not_reached("unknown restart setting");
1501 static bool service_will_restart(Service
*s
) {
1504 if (s
->state
== SERVICE_AUTO_RESTART
)
1508 if (UNIT(s
)->job
->type
== JOB_START
)
1513 static void service_enter_dead(Service
*s
, ServiceResult f
, bool allow_restart
) {
1518 /* If there's a stop job queued before we enter the DEAD state, we shouldn't act on Restart=, in order to not
1519 * undo what has already been enqueued. */
1520 if (unit_stop_pending(UNIT(s
)))
1521 allow_restart
= false;
1523 if (s
->result
== SERVICE_SUCCESS
)
1526 if (s
->result
!= SERVICE_SUCCESS
)
1527 log_unit_warning(UNIT(s
), "Failed with result '%s'.", service_result_to_string(s
->result
));
1529 /* Make sure service_release_resources() doesn't destroy our FD store, while we are changing through
1530 * SERVICE_FAILED/SERVICE_DEAD before entering into SERVICE_AUTO_RESTART. */
1531 s
->n_keep_fd_store
++;
1533 service_set_state(s
, s
->result
!= SERVICE_SUCCESS
? SERVICE_FAILED
: SERVICE_DEAD
);
1535 if (s
->result
!= SERVICE_SUCCESS
)
1536 emergency_action(UNIT(s
)->manager
, s
->emergency_action
, UNIT(s
)->reboot_arg
, "service failed");
1538 if (allow_restart
&& service_shall_restart(s
)) {
1540 r
= service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->restart_usec
));
1542 s
->n_keep_fd_store
--;
1546 service_set_state(s
, SERVICE_AUTO_RESTART
);
1548 /* If we shan't restart, then flush out the restart counter. But don't do that immediately, so that the
1549 * user can still introspect the counter. Do so on the next start. */
1550 s
->flush_n_restarts
= true;
1552 /* The new state is in effect, let's decrease the fd store ref counter again. Let's also readd us to the GC
1553 * queue, so that the fd store is possibly gc'ed again */
1554 s
->n_keep_fd_store
--;
1555 unit_add_to_gc_queue(UNIT(s
));
1557 /* The next restart might not be a manual stop, hence reset the flag indicating manual stops */
1558 s
->forbid_restart
= false;
1560 /* We want fresh tmpdirs in case service is started again immediately */
1561 exec_runtime_destroy(s
->exec_runtime
);
1562 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
1564 if (s
->exec_context
.runtime_directory_preserve_mode
== EXEC_PRESERVE_NO
||
1565 (s
->exec_context
.runtime_directory_preserve_mode
== EXEC_PRESERVE_RESTART
&& !service_will_restart(s
)))
1566 /* Also, remove the runtime directory */
1567 exec_context_destroy_runtime_directory(&s
->exec_context
, UNIT(s
)->manager
->prefix
[EXEC_DIRECTORY_RUNTIME
]);
1569 /* Get rid of the IPC bits of the user */
1570 unit_unref_uid_gid(UNIT(s
), true);
1572 /* Release the user, and destroy it if we are the only remaining owner */
1573 dynamic_creds_destroy(&s
->dynamic_creds
);
1575 /* Try to delete the pid file. At this point it will be
1576 * out-of-date, and some software might be confused by it, so
1577 * let's remove it. */
1579 (void) unlink(s
->pid_file
);
1584 log_unit_warning_errno(UNIT(s
), r
, "Failed to run install restart timer: %m");
1585 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1588 static void service_enter_stop_post(Service
*s
, ServiceResult f
) {
1592 if (s
->result
== SERVICE_SUCCESS
)
1595 service_unwatch_control_pid(s
);
1596 unit_watch_all_pids(UNIT(s
));
1598 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
];
1599 if (s
->control_command
) {
1600 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
1602 r
= service_spawn(s
,
1604 s
->timeout_stop_usec
,
1605 EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
|EXEC_IS_CONTROL
|EXEC_SETENV_RESULT
,
1610 service_set_state(s
, SERVICE_STOP_POST
);
1612 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_SUCCESS
);
1617 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop-post' task: %m");
1618 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1621 static int state_to_kill_operation(ServiceState state
) {
1624 case SERVICE_STOP_SIGABRT
:
1627 case SERVICE_STOP_SIGTERM
:
1628 case SERVICE_FINAL_SIGTERM
:
1629 return KILL_TERMINATE
;
1631 case SERVICE_STOP_SIGKILL
:
1632 case SERVICE_FINAL_SIGKILL
:
1636 return _KILL_OPERATION_INVALID
;
1640 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
) {
1645 if (s
->result
== SERVICE_SUCCESS
)
1648 unit_watch_all_pids(UNIT(s
));
1650 r
= unit_kill_context(
1653 state_to_kill_operation(state
),
1661 r
= service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_stop_usec
));
1665 service_set_state(s
, state
);
1666 } else if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
) && s
->kill_context
.send_sigkill
)
1667 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_SUCCESS
);
1668 else if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
1669 service_enter_stop_post(s
, SERVICE_SUCCESS
);
1670 else if (state
== SERVICE_FINAL_SIGTERM
&& s
->kill_context
.send_sigkill
)
1671 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_SUCCESS
);
1673 service_enter_dead(s
, SERVICE_SUCCESS
, true);
1678 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
1680 if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
1681 service_enter_stop_post(s
, SERVICE_FAILURE_RESOURCES
);
1683 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1686 static void service_enter_stop_by_notify(Service
*s
) {
1689 unit_watch_all_pids(UNIT(s
));
1691 service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_stop_usec
));
1693 /* The service told us it's stopping, so it's as if we SIGTERM'd it. */
1694 service_set_state(s
, SERVICE_STOP_SIGTERM
);
1697 static void service_enter_stop(Service
*s
, ServiceResult f
) {
1702 if (s
->result
== SERVICE_SUCCESS
)
1705 service_unwatch_control_pid(s
);
1706 unit_watch_all_pids(UNIT(s
));
1708 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
];
1709 if (s
->control_command
) {
1710 s
->control_command_id
= SERVICE_EXEC_STOP
;
1712 r
= service_spawn(s
,
1714 s
->timeout_stop_usec
,
1715 EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_IS_CONTROL
|EXEC_SETENV_RESULT
,
1720 service_set_state(s
, SERVICE_STOP
);
1722 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
1727 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop' task: %m");
1728 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1731 static bool service_good(Service
*s
) {
1735 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name_good
)
1738 main_pid_ok
= main_pid_good(s
);
1739 if (main_pid_ok
> 0) /* It's alive */
1741 if (main_pid_ok
== 0) /* It's dead */
1744 /* OK, we don't know anything about the main PID, maybe
1745 * because there is none. Let's check the control group
1748 return cgroup_good(s
) != 0;
1751 static void service_enter_running(Service
*s
, ServiceResult f
) {
1754 if (s
->result
== SERVICE_SUCCESS
)
1757 service_unwatch_control_pid(s
);
1759 if (service_good(s
)) {
1761 /* If there are any queued up sd_notify()
1762 * notifications, process them now */
1763 if (s
->notify_state
== NOTIFY_RELOADING
)
1764 service_enter_reload_by_notify(s
);
1765 else if (s
->notify_state
== NOTIFY_STOPPING
)
1766 service_enter_stop_by_notify(s
);
1768 service_set_state(s
, SERVICE_RUNNING
);
1769 service_arm_timer(s
, usec_add(UNIT(s
)->active_enter_timestamp
.monotonic
, s
->runtime_max_usec
));
1772 } else if (f
!= SERVICE_SUCCESS
)
1773 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
1774 else if (s
->remain_after_exit
)
1775 service_set_state(s
, SERVICE_EXITED
);
1777 service_enter_stop(s
, SERVICE_SUCCESS
);
1780 static void service_enter_start_post(Service
*s
) {
1784 service_unwatch_control_pid(s
);
1785 service_reset_watchdog(s
);
1787 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
];
1788 if (s
->control_command
) {
1789 s
->control_command_id
= SERVICE_EXEC_START_POST
;
1791 r
= service_spawn(s
,
1793 s
->timeout_start_usec
,
1794 EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_IS_CONTROL
,
1799 service_set_state(s
, SERVICE_START_POST
);
1801 service_enter_running(s
, SERVICE_SUCCESS
);
1806 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-post' task: %m");
1807 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1810 static void service_kill_control_processes(Service
*s
) {
1815 if (s
->control_pid
> 0) {
1816 r
= kill_and_sigcont(s
->control_pid
, SIGKILL
);
1818 _cleanup_free_
char *comm
= NULL
;
1820 (void) get_process_comm(s
->control_pid
, &comm
);
1822 log_unit_debug_errno(UNIT(s
), r
, "Failed to kill control process " PID_FMT
" (%s), ignoring: %m",
1823 s
->control_pid
, strna(comm
));
1827 if (UNIT(s
)->cgroup_path
) {
1828 _cleanup_set_free_ Set
*pid_set
= NULL
;
1831 if (s
->control_pid
> 0) {
1832 r
= set_make(&pid_set
, PID_TO_PTR(s
->control_pid
), NULL
);
1839 p
= strjoina(UNIT(s
)->cgroup_path
, "/control");
1840 r
= cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER
, p
, SIGKILL
, CGROUP_SIGCONT
|CGROUP_IGNORE_SELF
|CGROUP_REMOVE
, pid_set
, NULL
, NULL
);
1842 log_unit_debug_errno(UNIT(s
), r
, "Failed to send SIGKILL to processes of control group %s: %m", p
);
1846 static void service_enter_start(Service
*s
) {
1854 service_unwatch_control_pid(s
);
1855 service_unwatch_main_pid(s
);
1857 /* We want to ensure that nobody leaks processes from
1858 * START_PRE here, so let's go on a killing spree, People
1859 * should not spawn long running processes from START_PRE. */
1860 service_kill_control_processes(s
);
1862 if (s
->type
== SERVICE_FORKING
) {
1863 s
->control_command_id
= SERVICE_EXEC_START
;
1864 c
= s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1866 s
->main_command
= NULL
;
1868 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1869 s
->control_command
= NULL
;
1871 c
= s
->main_command
= s
->exec_command
[SERVICE_EXEC_START
];
1875 if (s
->type
!= SERVICE_ONESHOT
) {
1876 /* There's no command line configured for the main command? Hmm, that is strange. This can only
1877 * happen if the configuration changes at runtime. In this case, let's enter a failure
1879 log_unit_error(UNIT(s
), "There's no 'start' task anymore we could start: %m");
1884 service_enter_start_post(s
);
1888 if (IN_SET(s
->type
, SERVICE_SIMPLE
, SERVICE_IDLE
))
1889 /* For simple + idle this is the main process. We don't apply any timeout here, but
1890 * service_enter_running() will later apply the .runtime_max_usec timeout. */
1891 timeout
= USEC_INFINITY
;
1893 timeout
= s
->timeout_start_usec
;
1895 r
= service_spawn(s
,
1898 EXEC_PASS_FDS
|EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
|EXEC_SET_WATCHDOG
,
1903 if (IN_SET(s
->type
, SERVICE_SIMPLE
, SERVICE_IDLE
)) {
1904 /* For simple services we immediately start
1905 * the START_POST binaries. */
1907 service_set_main_pid(s
, pid
);
1908 service_enter_start_post(s
);
1910 } else if (s
->type
== SERVICE_FORKING
) {
1912 /* For forking services we wait until the start
1913 * process exited. */
1915 s
->control_pid
= pid
;
1916 service_set_state(s
, SERVICE_START
);
1918 } else if (IN_SET(s
->type
, SERVICE_ONESHOT
, SERVICE_DBUS
, SERVICE_NOTIFY
)) {
1920 /* For oneshot services we wait until the start
1921 * process exited, too, but it is our main process. */
1923 /* For D-Bus services we know the main pid right away,
1924 * but wait for the bus name to appear on the
1925 * bus. Notify services are similar. */
1927 service_set_main_pid(s
, pid
);
1928 service_set_state(s
, SERVICE_START
);
1930 assert_not_reached("Unknown service type");
1935 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start' task: %m");
1936 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1939 static void service_enter_start_pre(Service
*s
) {
1944 service_unwatch_control_pid(s
);
1946 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
];
1947 if (s
->control_command
) {
1948 /* Before we start anything, let's clear up what might
1949 * be left from previous runs. */
1950 service_kill_control_processes(s
);
1952 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
1954 r
= service_spawn(s
,
1956 s
->timeout_start_usec
,
1957 EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_IS_CONTROL
|EXEC_APPLY_TTY_STDIN
,
1962 service_set_state(s
, SERVICE_START_PRE
);
1964 service_enter_start(s
);
1969 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-pre' task: %m");
1970 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1973 static void service_enter_restart(Service
*s
) {
1974 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1979 if (UNIT(s
)->job
&& UNIT(s
)->job
->type
== JOB_STOP
) {
1980 /* Don't restart things if we are going down anyway */
1981 log_unit_info(UNIT(s
), "Stop job pending for unit, delaying automatic restart.");
1983 r
= service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->restart_usec
));
1990 /* Any units that are bound to this service must also be
1991 * restarted. We use JOB_RESTART (instead of the more obvious
1992 * JOB_START) here so that those dependency jobs will be added
1994 r
= manager_add_job(UNIT(s
)->manager
, JOB_RESTART
, UNIT(s
), JOB_FAIL
, &error
, NULL
);
1998 /* Count the jobs we enqueue for restarting. This counter is maintained as long as the unit isn't fully
1999 * stopped, i.e. as long as it remains up or remains in auto-start states. The use can reset the counter
2000 * explicitly however via the usual "systemctl reset-failure" logic. */
2002 s
->flush_n_restarts
= false;
2004 log_struct(LOG_INFO
,
2005 "MESSAGE_ID=" SD_MESSAGE_UNIT_RESTART_SCHEDULED_STR
,
2006 LOG_UNIT_ID(UNIT(s
)),
2007 LOG_UNIT_INVOCATION_ID(UNIT(s
)),
2008 LOG_UNIT_MESSAGE(UNIT(s
), "Scheduled restart job, restart counter is at %u.", s
->n_restarts
),
2009 "N_RESTARTS=%u", s
->n_restarts
,
2012 /* Notify clients about changed restart counter */
2013 unit_add_to_dbus_queue(UNIT(s
));
2015 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
2016 * it will be canceled as part of the service_stop() call that
2017 * is executed as part of JOB_RESTART. */
2022 log_unit_warning(UNIT(s
), "Failed to schedule restart job: %s", bus_error_message(&error
, -r
));
2023 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
2026 static void service_enter_reload_by_notify(Service
*s
) {
2027 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2032 service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_start_usec
));
2033 service_set_state(s
, SERVICE_RELOAD
);
2035 /* service_enter_reload_by_notify is never called during a reload, thus no loops are possible. */
2036 r
= manager_propagate_reload(UNIT(s
)->manager
, UNIT(s
), JOB_FAIL
, &error
);
2038 log_unit_warning(UNIT(s
), "Failed to schedule propagation of reload: %s", bus_error_message(&error
, -r
));
2041 static void service_enter_reload(Service
*s
) {
2046 service_unwatch_control_pid(s
);
2047 s
->reload_result
= SERVICE_SUCCESS
;
2049 s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
];
2050 if (s
->control_command
) {
2051 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
2053 r
= service_spawn(s
,
2055 s
->timeout_start_usec
,
2056 EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_IS_CONTROL
,
2061 service_set_state(s
, SERVICE_RELOAD
);
2063 service_enter_running(s
, SERVICE_SUCCESS
);
2068 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'reload' task: %m");
2069 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
2070 service_enter_running(s
, SERVICE_SUCCESS
);
2073 static void service_run_next_control(Service
*s
) {
2078 assert(s
->control_command
);
2079 assert(s
->control_command
->command_next
);
2081 assert(s
->control_command_id
!= SERVICE_EXEC_START
);
2083 s
->control_command
= s
->control_command
->command_next
;
2084 service_unwatch_control_pid(s
);
2086 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
2087 timeout
= s
->timeout_start_usec
;
2089 timeout
= s
->timeout_stop_usec
;
2091 r
= service_spawn(s
,
2094 EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_IS_CONTROL
|
2095 (IN_SET(s
->control_command_id
, SERVICE_EXEC_START_PRE
, SERVICE_EXEC_STOP_POST
) ? EXEC_APPLY_TTY_STDIN
: 0)|
2096 (IN_SET(s
->control_command_id
, SERVICE_EXEC_STOP
, SERVICE_EXEC_STOP_POST
) ? EXEC_SETENV_RESULT
: 0),
2104 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next control task: %m");
2106 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START_POST
, SERVICE_STOP
))
2107 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2108 else if (s
->state
== SERVICE_STOP_POST
)
2109 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
2110 else if (s
->state
== SERVICE_RELOAD
) {
2111 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
2112 service_enter_running(s
, SERVICE_SUCCESS
);
2114 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2117 static void service_run_next_main(Service
*s
) {
2122 assert(s
->main_command
);
2123 assert(s
->main_command
->command_next
);
2124 assert(s
->type
== SERVICE_ONESHOT
);
2126 s
->main_command
= s
->main_command
->command_next
;
2127 service_unwatch_main_pid(s
);
2129 r
= service_spawn(s
,
2131 s
->timeout_start_usec
,
2132 EXEC_PASS_FDS
|EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
|EXEC_SET_WATCHDOG
,
2137 service_set_main_pid(s
, pid
);
2142 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next main task: %m");
2143 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2146 static int service_start(Unit
*u
) {
2147 Service
*s
= SERVICE(u
);
2152 /* We cannot fulfill this request right now, try again later
2154 if (IN_SET(s
->state
,
2155 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
2156 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))
2159 /* Already on it! */
2160 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
))
2163 /* A service that will be restarted must be stopped first to
2164 * trigger BindsTo and/or OnFailure dependencies. If a user
2165 * does not want to wait for the holdoff time to elapse, the
2166 * service should be manually restarted, not started. We
2167 * simply return EAGAIN here, so that any start jobs stay
2168 * queued, and assume that the auto restart timer will
2169 * eventually trigger the restart. */
2170 if (s
->state
== SERVICE_AUTO_RESTART
)
2173 assert(IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_FAILED
));
2175 /* Make sure we don't enter a busy loop of some kind. */
2176 r
= unit_start_limit_test(u
);
2178 service_enter_dead(s
, SERVICE_FAILURE_START_LIMIT_HIT
, false);
2182 r
= unit_acquire_invocation_id(u
);
2186 s
->result
= SERVICE_SUCCESS
;
2187 s
->reload_result
= SERVICE_SUCCESS
;
2188 s
->main_pid_known
= false;
2189 s
->main_pid_alien
= false;
2190 s
->forbid_restart
= false;
2191 s
->reset_accounting
= true;
2193 s
->status_text
= mfree(s
->status_text
);
2194 s
->status_errno
= 0;
2196 s
->notify_state
= NOTIFY_UNKNOWN
;
2198 s
->watchdog_override_enable
= false;
2199 s
->watchdog_override_usec
= 0;
2201 /* This is not an automatic restart? Flush the restart counter then */
2202 if (s
->flush_n_restarts
) {
2204 s
->flush_n_restarts
= false;
2207 service_enter_start_pre(s
);
2211 static int service_stop(Unit
*u
) {
2212 Service
*s
= SERVICE(u
);
2216 /* Don't create restart jobs from manual stops. */
2217 s
->forbid_restart
= true;
2220 if (IN_SET(s
->state
,
2221 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
2222 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))
2225 /* A restart will be scheduled or is in progress. */
2226 if (s
->state
== SERVICE_AUTO_RESTART
) {
2227 service_set_state(s
, SERVICE_DEAD
);
2231 /* If there's already something running we go directly into
2233 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RELOAD
)) {
2234 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
2238 assert(IN_SET(s
->state
, SERVICE_RUNNING
, SERVICE_EXITED
));
2240 service_enter_stop(s
, SERVICE_SUCCESS
);
2244 static int service_reload(Unit
*u
) {
2245 Service
*s
= SERVICE(u
);
2249 assert(IN_SET(s
->state
, SERVICE_RUNNING
, SERVICE_EXITED
));
2251 service_enter_reload(s
);
2255 _pure_
static bool service_can_reload(Unit
*u
) {
2256 Service
*s
= SERVICE(u
);
2260 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
2263 static unsigned service_exec_command_index(Unit
*u
, ServiceExecCommand id
, ExecCommand
*current
) {
2264 Service
*s
= SERVICE(u
);
2266 ExecCommand
*first
, *c
;
2270 first
= s
->exec_command
[id
];
2272 /* Figure out where we are in the list by walking back to the beginning */
2273 for (c
= current
; c
!= first
; c
= c
->command_prev
)
2279 static int service_serialize_exec_command(Unit
*u
, FILE *f
, ExecCommand
*command
) {
2280 Service
*s
= SERVICE(u
);
2281 ServiceExecCommand id
;
2285 _cleanup_free_
char *args
= NULL
, *p
= NULL
;
2286 size_t allocated
= 0, length
= 0;
2294 if (command
== s
->control_command
) {
2296 id
= s
->control_command_id
;
2299 id
= SERVICE_EXEC_START
;
2302 idx
= service_exec_command_index(u
, id
, command
);
2304 STRV_FOREACH(arg
, command
->argv
) {
2306 _cleanup_free_
char *e
= NULL
;
2308 e
= xescape(*arg
, WHITESPACE
);
2313 if (!GREEDY_REALLOC(args
, allocated
, length
+ 1 + n
+ 1))
2317 args
[length
++] = ' ';
2319 memcpy(args
+ length
, e
, n
);
2323 if (!GREEDY_REALLOC(args
, allocated
, length
+ 1))
2327 p
= xescape(command
->path
, WHITESPACE
);
2331 fprintf(f
, "%s-command=%s %u %s %s\n", type
, service_exec_command_to_string(id
), idx
, p
, args
);
2336 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
2337 Service
*s
= SERVICE(u
);
2345 unit_serialize_item(u
, f
, "state", service_state_to_string(s
->state
));
2346 unit_serialize_item(u
, f
, "result", service_result_to_string(s
->result
));
2347 unit_serialize_item(u
, f
, "reload-result", service_result_to_string(s
->reload_result
));
2349 if (s
->control_pid
> 0)
2350 unit_serialize_item_format(u
, f
, "control-pid", PID_FMT
, s
->control_pid
);
2352 if (s
->main_pid_known
&& s
->main_pid
> 0)
2353 unit_serialize_item_format(u
, f
, "main-pid", PID_FMT
, s
->main_pid
);
2355 unit_serialize_item(u
, f
, "main-pid-known", yes_no(s
->main_pid_known
));
2356 unit_serialize_item(u
, f
, "bus-name-good", yes_no(s
->bus_name_good
));
2357 unit_serialize_item(u
, f
, "bus-name-owner", s
->bus_name_owner
);
2359 unit_serialize_item_format(u
, f
, "n-restarts", "%u", s
->n_restarts
);
2360 unit_serialize_item(u
, f
, "flush-n-restarts", yes_no(s
->flush_n_restarts
));
2362 r
= unit_serialize_item_escaped(u
, f
, "status-text", s
->status_text
);
2366 service_serialize_exec_command(u
, f
, s
->control_command
);
2367 service_serialize_exec_command(u
, f
, s
->main_command
);
2369 r
= unit_serialize_item_fd(u
, f
, fds
, "stdin-fd", s
->stdin_fd
);
2372 r
= unit_serialize_item_fd(u
, f
, fds
, "stdout-fd", s
->stdout_fd
);
2375 r
= unit_serialize_item_fd(u
, f
, fds
, "stderr-fd", s
->stderr_fd
);
2379 if (UNIT_ISSET(s
->accept_socket
)) {
2380 r
= unit_serialize_item(u
, f
, "accept-socket", UNIT_DEREF(s
->accept_socket
)->id
);
2385 r
= unit_serialize_item_fd(u
, f
, fds
, "socket-fd", s
->socket_fd
);
2389 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
2390 _cleanup_free_
char *c
= NULL
;
2393 copy
= fdset_put_dup(fds
, fs
->fd
);
2397 c
= cescape(fs
->fdname
);
2399 unit_serialize_item_format(u
, f
, "fd-store-fd", "%i %s", copy
, strempty(c
));
2402 if (s
->main_exec_status
.pid
> 0) {
2403 unit_serialize_item_format(u
, f
, "main-exec-status-pid", PID_FMT
, s
->main_exec_status
.pid
);
2404 dual_timestamp_serialize(f
, "main-exec-status-start", &s
->main_exec_status
.start_timestamp
);
2405 dual_timestamp_serialize(f
, "main-exec-status-exit", &s
->main_exec_status
.exit_timestamp
);
2407 if (dual_timestamp_is_set(&s
->main_exec_status
.exit_timestamp
)) {
2408 unit_serialize_item_format(u
, f
, "main-exec-status-code", "%i", s
->main_exec_status
.code
);
2409 unit_serialize_item_format(u
, f
, "main-exec-status-status", "%i", s
->main_exec_status
.status
);
2413 dual_timestamp_serialize(f
, "watchdog-timestamp", &s
->watchdog_timestamp
);
2415 unit_serialize_item(u
, f
, "forbid-restart", yes_no(s
->forbid_restart
));
2417 if (s
->watchdog_override_enable
)
2418 unit_serialize_item_format(u
, f
, "watchdog-override-usec", USEC_FMT
, s
->watchdog_override_usec
);
2423 static int service_deserialize_exec_command(Unit
*u
, const char *key
, const char *value
) {
2424 Service
*s
= SERVICE(u
);
2426 unsigned idx
= 0, i
;
2427 bool control
, found
= false;
2428 ServiceExecCommand id
= _SERVICE_EXEC_COMMAND_INVALID
;
2429 ExecCommand
*command
= NULL
;
2430 _cleanup_free_
char *path
= NULL
;
2431 _cleanup_strv_free_
char **argv
= NULL
;
2433 enum ExecCommandState
{
2434 STATE_EXEC_COMMAND_TYPE
,
2435 STATE_EXEC_COMMAND_INDEX
,
2436 STATE_EXEC_COMMAND_PATH
,
2437 STATE_EXEC_COMMAND_ARGS
,
2438 _STATE_EXEC_COMMAND_MAX
,
2439 _STATE_EXEC_COMMAND_INVALID
= -1,
2446 control
= streq(key
, "control-command");
2448 state
= STATE_EXEC_COMMAND_TYPE
;
2451 _cleanup_free_
char *arg
= NULL
;
2453 r
= extract_first_word(&value
, &arg
, NULL
, EXTRACT_CUNESCAPE
);
2460 case STATE_EXEC_COMMAND_TYPE
:
2461 id
= service_exec_command_from_string(arg
);
2465 state
= STATE_EXEC_COMMAND_INDEX
;
2467 case STATE_EXEC_COMMAND_INDEX
:
2468 r
= safe_atou(arg
, &idx
);
2472 state
= STATE_EXEC_COMMAND_PATH
;
2474 case STATE_EXEC_COMMAND_PATH
:
2477 state
= STATE_EXEC_COMMAND_ARGS
;
2479 if (!path_is_absolute(path
))
2482 case STATE_EXEC_COMMAND_ARGS
:
2483 r
= strv_extend(&argv
, arg
);
2488 assert_not_reached("Unknown error at deserialization of exec command");
2493 if (state
!= STATE_EXEC_COMMAND_ARGS
)
2496 /* Let's check whether exec command on given offset matches data that we just deserialized */
2497 for (command
= s
->exec_command
[id
], i
= 0; command
; command
= command
->command_next
, i
++) {
2501 found
= strv_equal(argv
, command
->argv
) && streq(command
->path
, path
);
2506 /* Command at the index we serialized is different, let's look for command that exactly
2507 * matches but is on different index. If there is no such command we will not resume execution. */
2508 for (command
= s
->exec_command
[id
]; command
; command
= command
->command_next
)
2509 if (strv_equal(command
->argv
, argv
) && streq(command
->path
, path
))
2513 if (command
&& control
)
2514 s
->control_command
= command
;
2516 s
->main_command
= command
;
2518 log_unit_warning(u
, "Current command vanished from the unit file, execution of the command list won't be resumed.");
2523 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
2524 Service
*s
= SERVICE(u
);
2532 if (streq(key
, "state")) {
2535 state
= service_state_from_string(value
);
2537 log_unit_debug(u
, "Failed to parse state value: %s", value
);
2539 s
->deserialized_state
= state
;
2540 } else if (streq(key
, "result")) {
2543 f
= service_result_from_string(value
);
2545 log_unit_debug(u
, "Failed to parse result value: %s", value
);
2546 else if (f
!= SERVICE_SUCCESS
)
2549 } else if (streq(key
, "reload-result")) {
2552 f
= service_result_from_string(value
);
2554 log_unit_debug(u
, "Failed to parse reload result value: %s", value
);
2555 else if (f
!= SERVICE_SUCCESS
)
2556 s
->reload_result
= f
;
2558 } else if (streq(key
, "control-pid")) {
2561 if (parse_pid(value
, &pid
) < 0)
2562 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
2564 s
->control_pid
= pid
;
2565 } else if (streq(key
, "main-pid")) {
2568 if (parse_pid(value
, &pid
) < 0)
2569 log_unit_debug(u
, "Failed to parse main-pid value: %s", value
);
2571 service_set_main_pid(s
, pid
);
2572 unit_watch_pid(UNIT(s
), pid
);
2574 } else if (streq(key
, "main-pid-known")) {
2577 b
= parse_boolean(value
);
2579 log_unit_debug(u
, "Failed to parse main-pid-known value: %s", value
);
2581 s
->main_pid_known
= b
;
2582 } else if (streq(key
, "bus-name-good")) {
2585 b
= parse_boolean(value
);
2587 log_unit_debug(u
, "Failed to parse bus-name-good value: %s", value
);
2589 s
->bus_name_good
= b
;
2590 } else if (streq(key
, "bus-name-owner")) {
2591 r
= free_and_strdup(&s
->bus_name_owner
, value
);
2593 log_unit_error_errno(u
, r
, "Unable to deserialize current bus owner %s: %m", value
);
2594 } else if (streq(key
, "status-text")) {
2597 r
= cunescape(value
, 0, &t
);
2599 log_unit_debug_errno(u
, r
, "Failed to unescape status text: %s", value
);
2601 free(s
->status_text
);
2605 } else if (streq(key
, "accept-socket")) {
2608 r
= manager_load_unit(u
->manager
, value
, NULL
, NULL
, &socket
);
2610 log_unit_debug_errno(u
, r
, "Failed to load accept-socket unit: %s", value
);
2612 unit_ref_set(&s
->accept_socket
, socket
);
2613 SOCKET(socket
)->n_connections
++;
2616 } else if (streq(key
, "socket-fd")) {
2619 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2620 log_unit_debug(u
, "Failed to parse socket-fd value: %s", value
);
2622 asynchronous_close(s
->socket_fd
);
2623 s
->socket_fd
= fdset_remove(fds
, fd
);
2625 } else if (streq(key
, "fd-store-fd")) {
2630 pf
= strcspn(value
, WHITESPACE
);
2631 fdv
= strndupa(value
, pf
);
2633 if (safe_atoi(fdv
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2634 log_unit_debug(u
, "Failed to parse fd-store-fd value: %s", value
);
2636 _cleanup_free_
char *t
= NULL
;
2640 fdn
+= strspn(fdn
, WHITESPACE
);
2641 (void) cunescape(fdn
, 0, &t
);
2643 r
= service_add_fd_store(s
, fd
, t
);
2645 log_unit_error_errno(u
, r
, "Failed to add fd to store: %m");
2647 fdset_remove(fds
, fd
);
2650 } else if (streq(key
, "main-exec-status-pid")) {
2653 if (parse_pid(value
, &pid
) < 0)
2654 log_unit_debug(u
, "Failed to parse main-exec-status-pid value: %s", value
);
2656 s
->main_exec_status
.pid
= pid
;
2657 } else if (streq(key
, "main-exec-status-code")) {
2660 if (safe_atoi(value
, &i
) < 0)
2661 log_unit_debug(u
, "Failed to parse main-exec-status-code value: %s", value
);
2663 s
->main_exec_status
.code
= i
;
2664 } else if (streq(key
, "main-exec-status-status")) {
2667 if (safe_atoi(value
, &i
) < 0)
2668 log_unit_debug(u
, "Failed to parse main-exec-status-status value: %s", value
);
2670 s
->main_exec_status
.status
= i
;
2671 } else if (streq(key
, "main-exec-status-start"))
2672 dual_timestamp_deserialize(value
, &s
->main_exec_status
.start_timestamp
);
2673 else if (streq(key
, "main-exec-status-exit"))
2674 dual_timestamp_deserialize(value
, &s
->main_exec_status
.exit_timestamp
);
2675 else if (streq(key
, "watchdog-timestamp"))
2676 dual_timestamp_deserialize(value
, &s
->watchdog_timestamp
);
2677 else if (streq(key
, "forbid-restart")) {
2680 b
= parse_boolean(value
);
2682 log_unit_debug(u
, "Failed to parse forbid-restart value: %s", value
);
2684 s
->forbid_restart
= b
;
2685 } else if (streq(key
, "stdin-fd")) {
2688 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2689 log_unit_debug(u
, "Failed to parse stdin-fd value: %s", value
);
2691 asynchronous_close(s
->stdin_fd
);
2692 s
->stdin_fd
= fdset_remove(fds
, fd
);
2693 s
->exec_context
.stdio_as_fds
= true;
2695 } else if (streq(key
, "stdout-fd")) {
2698 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2699 log_unit_debug(u
, "Failed to parse stdout-fd value: %s", value
);
2701 asynchronous_close(s
->stdout_fd
);
2702 s
->stdout_fd
= fdset_remove(fds
, fd
);
2703 s
->exec_context
.stdio_as_fds
= true;
2705 } else if (streq(key
, "stderr-fd")) {
2708 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2709 log_unit_debug(u
, "Failed to parse stderr-fd value: %s", value
);
2711 asynchronous_close(s
->stderr_fd
);
2712 s
->stderr_fd
= fdset_remove(fds
, fd
);
2713 s
->exec_context
.stdio_as_fds
= true;
2715 } else if (streq(key
, "watchdog-override-usec")) {
2716 usec_t watchdog_override_usec
;
2717 if (timestamp_deserialize(value
, &watchdog_override_usec
) < 0)
2718 log_unit_debug(u
, "Failed to parse watchdog_override_usec value: %s", value
);
2720 s
->watchdog_override_enable
= true;
2721 s
->watchdog_override_usec
= watchdog_override_usec
;
2723 } else if (STR_IN_SET(key
, "main-command", "control-command")) {
2724 r
= service_deserialize_exec_command(u
, key
, value
);
2726 log_unit_debug_errno(u
, r
, "Failed to parse serialized command \"%s\": %m", value
);
2728 } else if (streq(key
, "n-restarts")) {
2729 r
= safe_atou(value
, &s
->n_restarts
);
2731 log_unit_debug_errno(u
, r
, "Failed to parse serialized restart counter '%s': %m", value
);
2733 } else if (streq(key
, "flush-n-restarts")) {
2734 r
= parse_boolean(value
);
2736 log_unit_debug_errno(u
, r
, "Failed to parse serialized flush restart counter setting '%s': %m", value
);
2738 s
->flush_n_restarts
= r
;
2740 log_unit_debug(u
, "Unknown serialization key: %s", key
);
2745 _pure_
static UnitActiveState
service_active_state(Unit
*u
) {
2746 const UnitActiveState
*table
;
2750 table
= SERVICE(u
)->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
2752 return table
[SERVICE(u
)->state
];
2755 static const char *service_sub_state_to_string(Unit
*u
) {
2758 return service_state_to_string(SERVICE(u
)->state
);
2761 static bool service_check_gc(Unit
*u
) {
2762 Service
*s
= SERVICE(u
);
2766 /* Never clean up services that still have a process around,
2767 * even if the service is formally dead. */
2768 if (cgroup_good(s
) > 0 ||
2769 main_pid_good(s
) > 0 ||
2770 control_pid_good(s
) > 0)
2776 static int service_retry_pid_file(Service
*s
) {
2779 assert(s
->pid_file
);
2780 assert(IN_SET(s
->state
, SERVICE_START
, SERVICE_START_POST
));
2782 r
= service_load_pid_file(s
, false);
2786 service_unwatch_pid_file(s
);
2788 service_enter_running(s
, SERVICE_SUCCESS
);
2792 static int service_watch_pid_file(Service
*s
) {
2795 log_unit_debug(UNIT(s
), "Setting watch for PID file %s", s
->pid_file_pathspec
->path
);
2797 r
= path_spec_watch(s
->pid_file_pathspec
, service_dispatch_io
);
2801 /* the pidfile might have appeared just before we set the watch */
2802 log_unit_debug(UNIT(s
), "Trying to read PID file %s in case it changed", s
->pid_file_pathspec
->path
);
2803 service_retry_pid_file(s
);
2807 log_unit_error_errno(UNIT(s
), r
, "Failed to set a watch for PID file %s: %m", s
->pid_file_pathspec
->path
);
2808 service_unwatch_pid_file(s
);
2812 static int service_demand_pid_file(Service
*s
) {
2815 assert(s
->pid_file
);
2816 assert(!s
->pid_file_pathspec
);
2818 ps
= new0(PathSpec
, 1);
2823 ps
->path
= strdup(s
->pid_file
);
2829 path_kill_slashes(ps
->path
);
2831 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2832 * keep their PID file open all the time. */
2833 ps
->type
= PATH_MODIFIED
;
2834 ps
->inotify_fd
= -1;
2836 s
->pid_file_pathspec
= ps
;
2838 return service_watch_pid_file(s
);
2841 static int service_dispatch_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
) {
2842 PathSpec
*p
= userdata
;
2847 s
= SERVICE(p
->unit
);
2851 assert(IN_SET(s
->state
, SERVICE_START
, SERVICE_START_POST
));
2852 assert(s
->pid_file_pathspec
);
2853 assert(path_spec_owns_inotify_fd(s
->pid_file_pathspec
, fd
));
2855 log_unit_debug(UNIT(s
), "inotify event");
2857 if (path_spec_fd_event(p
, events
) < 0)
2860 if (service_retry_pid_file(s
) == 0)
2863 if (service_watch_pid_file(s
) < 0)
2869 service_unwatch_pid_file(s
);
2870 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2874 static void service_notify_cgroup_empty_event(Unit
*u
) {
2875 Service
*s
= SERVICE(u
);
2879 log_unit_debug(u
, "cgroup is empty");
2883 /* Waiting for SIGCHLD is usually more interesting,
2884 * because it includes return codes/signals. Which is
2885 * why we ignore the cgroup events for most cases,
2886 * except when we don't know pid which to expect the
2890 if (s
->type
== SERVICE_NOTIFY
&&
2891 main_pid_good(s
) == 0 &&
2892 control_pid_good(s
) == 0) {
2893 /* No chance of getting a ready notification anymore */
2894 service_enter_stop_post(s
, SERVICE_FAILURE_PROTOCOL
);
2900 case SERVICE_START_POST
:
2901 if (s
->pid_file_pathspec
&&
2902 main_pid_good(s
) == 0 &&
2903 control_pid_good(s
) == 0) {
2905 /* Give up hoping for the daemon to write its PID file */
2906 log_unit_warning(u
, "Daemon never wrote its PID file. Failing.");
2908 service_unwatch_pid_file(s
);
2909 if (s
->state
== SERVICE_START
)
2910 service_enter_stop_post(s
, SERVICE_FAILURE_PROTOCOL
);
2912 service_enter_stop(s
, SERVICE_FAILURE_PROTOCOL
);
2916 case SERVICE_RUNNING
:
2917 /* service_enter_running() will figure out what to do */
2918 service_enter_running(s
, SERVICE_SUCCESS
);
2921 case SERVICE_STOP_SIGABRT
:
2922 case SERVICE_STOP_SIGTERM
:
2923 case SERVICE_STOP_SIGKILL
:
2925 if (main_pid_good(s
) <= 0 && control_pid_good(s
) <= 0)
2926 service_enter_stop_post(s
, SERVICE_SUCCESS
);
2930 case SERVICE_STOP_POST
:
2931 case SERVICE_FINAL_SIGTERM
:
2932 case SERVICE_FINAL_SIGKILL
:
2933 if (main_pid_good(s
) <= 0 && control_pid_good(s
) <= 0)
2934 service_enter_dead(s
, SERVICE_SUCCESS
, true);
2943 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
2944 Service
*s
= SERVICE(u
);
2950 if (is_clean_exit(code
, status
, s
->type
== SERVICE_ONESHOT
? EXIT_CLEAN_COMMAND
: EXIT_CLEAN_DAEMON
, &s
->success_status
))
2951 f
= SERVICE_SUCCESS
;
2952 else if (code
== CLD_EXITED
)
2953 f
= SERVICE_FAILURE_EXIT_CODE
;
2954 else if (code
== CLD_KILLED
)
2955 f
= SERVICE_FAILURE_SIGNAL
;
2956 else if (code
== CLD_DUMPED
)
2957 f
= SERVICE_FAILURE_CORE_DUMP
;
2959 assert_not_reached("Unknown code");
2961 if (s
->main_pid
== pid
) {
2962 /* Forking services may occasionally move to a new PID.
2963 * As long as they update the PID file before exiting the old
2964 * PID, they're fine. */
2965 if (service_load_pid_file(s
, false) == 0)
2969 exec_status_exit(&s
->main_exec_status
, &s
->exec_context
, pid
, code
, status
);
2971 if (s
->main_command
) {
2972 /* If this is not a forking service than the
2973 * main process got started and hence we copy
2974 * the exit status so that it is recorded both
2975 * as main and as control process exit
2978 s
->main_command
->exec_status
= s
->main_exec_status
;
2980 if (s
->main_command
->flags
& EXEC_COMMAND_IGNORE_FAILURE
)
2981 f
= SERVICE_SUCCESS
;
2982 } else if (s
->exec_command
[SERVICE_EXEC_START
]) {
2984 /* If this is a forked process, then we should
2985 * ignore the return value if this was
2986 * configured for the starter process */
2988 if (s
->exec_command
[SERVICE_EXEC_START
]->flags
& EXEC_COMMAND_IGNORE_FAILURE
)
2989 f
= SERVICE_SUCCESS
;
2992 /* When this is a successful exit, let's log about the exit code on DEBUG level. If this is a failure
2993 * and the process exited on its own via exit(), then let's make this a NOTICE, under the assumption
2994 * that the service already logged the reason at a higher log level on its own. However, if the service
2995 * died due to a signal, then it most likely didn't say anything about any reason, hence let's raise
2996 * our log level to WARNING then. */
2998 log_struct(f
== SERVICE_SUCCESS
? LOG_DEBUG
:
2999 (code
== CLD_EXITED
? LOG_NOTICE
: LOG_WARNING
),
3000 LOG_UNIT_MESSAGE(u
, "Main process exited, code=%s, status=%i/%s",
3001 sigchld_code_to_string(code
), status
,
3002 strna(code
== CLD_EXITED
3003 ? exit_status_to_string(status
, EXIT_STATUS_FULL
)
3004 : signal_to_string(status
))),
3005 "EXIT_CODE=%s", sigchld_code_to_string(code
),
3006 "EXIT_STATUS=%i", status
,
3008 LOG_UNIT_INVOCATION_ID(u
),
3011 if (s
->result
== SERVICE_SUCCESS
)
3014 if (s
->main_command
&&
3015 s
->main_command
->command_next
&&
3016 s
->type
== SERVICE_ONESHOT
&&
3017 f
== SERVICE_SUCCESS
) {
3019 /* There is another command to *
3020 * execute, so let's do that. */
3022 log_unit_debug(u
, "Running next main command for state %s.", service_state_to_string(s
->state
));
3023 service_run_next_main(s
);
3027 /* The service exited, so the service is officially
3029 s
->main_command
= NULL
;
3033 case SERVICE_START_POST
:
3034 case SERVICE_RELOAD
:
3036 /* Need to wait until the operation is
3041 if (s
->type
== SERVICE_ONESHOT
) {
3042 /* This was our main goal, so let's go on */
3043 if (f
== SERVICE_SUCCESS
)
3044 service_enter_start_post(s
);
3046 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
3048 } else if (s
->type
== SERVICE_NOTIFY
) {
3049 /* Only enter running through a notification, so that the
3050 * SERVICE_START state signifies that no ready notification
3051 * has been received */
3052 if (f
!= SERVICE_SUCCESS
)
3053 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
3054 else if (!s
->remain_after_exit
|| s
->notify_access
== NOTIFY_MAIN
)
3055 /* The service has never been and will never be active */
3056 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_PROTOCOL
);
3062 case SERVICE_RUNNING
:
3063 service_enter_running(s
, f
);
3066 case SERVICE_STOP_SIGABRT
:
3067 case SERVICE_STOP_SIGTERM
:
3068 case SERVICE_STOP_SIGKILL
:
3070 if (control_pid_good(s
) <= 0)
3071 service_enter_stop_post(s
, f
);
3073 /* If there is still a control process, wait for that first */
3076 case SERVICE_STOP_POST
:
3077 case SERVICE_FINAL_SIGTERM
:
3078 case SERVICE_FINAL_SIGKILL
:
3080 if (control_pid_good(s
) <= 0)
3081 service_enter_dead(s
, f
, true);
3085 assert_not_reached("Uh, main process died at wrong time.");
3089 } else if (s
->control_pid
== pid
) {
3092 if (s
->control_command
) {
3093 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
3095 if (s
->control_command
->flags
& EXEC_COMMAND_IGNORE_FAILURE
)
3096 f
= SERVICE_SUCCESS
;
3099 log_unit_full(u
, f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
, 0,
3100 "Control process exited, code=%s status=%i",
3101 sigchld_code_to_string(code
), status
);
3103 if (s
->result
== SERVICE_SUCCESS
)
3106 /* Immediately get rid of the cgroup, so that the
3107 * kernel doesn't delay the cgroup empty messages for
3108 * the service cgroup any longer than necessary */
3109 service_kill_control_processes(s
);
3111 if (s
->control_command
&&
3112 s
->control_command
->command_next
&&
3113 f
== SERVICE_SUCCESS
) {
3115 /* There is another command to *
3116 * execute, so let's do that. */
3118 log_unit_debug(u
, "Running next control command for state %s.", service_state_to_string(s
->state
));
3119 service_run_next_control(s
);
3122 /* No further commands for this step, so let's
3123 * figure out what to do next */
3125 s
->control_command
= NULL
;
3126 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
3128 log_unit_debug(u
, "Got final SIGCHLD for state %s.", service_state_to_string(s
->state
));
3132 case SERVICE_START_PRE
:
3133 if (f
== SERVICE_SUCCESS
)
3134 service_enter_start(s
);
3136 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
3140 if (s
->type
!= SERVICE_FORKING
)
3141 /* Maybe spurious event due to a reload that changed the type? */
3144 if (f
!= SERVICE_SUCCESS
) {
3145 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
3150 bool has_start_post
;
3153 /* Let's try to load the pid file here if we can.
3154 * The PID file might actually be created by a START_POST
3155 * script. In that case don't worry if the loading fails. */
3157 has_start_post
= !!s
->exec_command
[SERVICE_EXEC_START_POST
];
3158 r
= service_load_pid_file(s
, !has_start_post
);
3159 if (!has_start_post
&& r
< 0) {
3160 r
= service_demand_pid_file(s
);
3161 if (r
< 0 || cgroup_good(s
) == 0)
3162 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_PROTOCOL
);
3166 service_search_main_pid(s
);
3168 service_enter_start_post(s
);
3171 case SERVICE_START_POST
:
3172 if (f
!= SERVICE_SUCCESS
) {
3173 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
3180 r
= service_load_pid_file(s
, true);
3182 r
= service_demand_pid_file(s
);
3183 if (r
< 0 || cgroup_good(s
) == 0)
3184 service_enter_stop(s
, SERVICE_FAILURE_PROTOCOL
);
3188 service_search_main_pid(s
);
3190 service_enter_running(s
, SERVICE_SUCCESS
);
3193 case SERVICE_RELOAD
:
3194 if (f
== SERVICE_SUCCESS
)
3195 if (service_load_pid_file(s
, true) < 0)
3196 service_search_main_pid(s
);
3198 s
->reload_result
= f
;
3199 service_enter_running(s
, SERVICE_SUCCESS
);
3203 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
3206 case SERVICE_STOP_SIGABRT
:
3207 case SERVICE_STOP_SIGTERM
:
3208 case SERVICE_STOP_SIGKILL
:
3209 if (main_pid_good(s
) <= 0)
3210 service_enter_stop_post(s
, f
);
3212 /* If there is still a service
3213 * process around, wait until
3214 * that one quit, too */
3217 case SERVICE_STOP_POST
:
3218 case SERVICE_FINAL_SIGTERM
:
3219 case SERVICE_FINAL_SIGKILL
:
3220 if (main_pid_good(s
) <= 0)
3221 service_enter_dead(s
, f
, true);
3225 assert_not_reached("Uh, control process died at wrong time.");
3230 /* Notify clients about changed exit status */
3231 unit_add_to_dbus_queue(u
);
3233 /* We got one SIGCHLD for the service, let's watch all
3234 * processes that are now running of the service, and watch
3235 * that. Among the PIDs we then watch will be children
3236 * reassigned to us, which hopefully allows us to identify
3237 * when all children are gone */
3238 unit_tidy_watch_pids(u
, s
->main_pid
, s
->control_pid
);
3239 unit_watch_all_pids(u
);
3241 /* If the PID set is empty now, then let's finish this off
3242 (On unified we use proper notifications) */
3243 if (cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER
) == 0 && set_isempty(u
->pids
))
3244 unit_add_to_cgroup_empty_queue(u
);
3247 static int service_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
3248 Service
*s
= SERVICE(userdata
);
3251 assert(source
== s
->timer_event_source
);
3255 case SERVICE_START_PRE
:
3257 log_unit_warning(UNIT(s
), "%s operation timed out. Terminating.", s
->state
== SERVICE_START
? "Start" : "Start-pre");
3258 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
3261 case SERVICE_START_POST
:
3262 log_unit_warning(UNIT(s
), "Start-post operation timed out. Stopping.");
3263 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
3266 case SERVICE_RUNNING
:
3267 log_unit_warning(UNIT(s
), "Service reached runtime time limit. Stopping.");
3268 service_enter_stop(s
, SERVICE_FAILURE_TIMEOUT
);
3271 case SERVICE_RELOAD
:
3272 log_unit_warning(UNIT(s
), "Reload operation timed out. Killing reload process.");
3273 service_kill_control_processes(s
);
3274 s
->reload_result
= SERVICE_FAILURE_TIMEOUT
;
3275 service_enter_running(s
, SERVICE_SUCCESS
);
3279 log_unit_warning(UNIT(s
), "Stopping timed out. Terminating.");
3280 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
3283 case SERVICE_STOP_SIGABRT
:
3284 log_unit_warning(UNIT(s
), "State 'stop-sigabrt' timed out. Terminating.");
3285 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
3288 case SERVICE_STOP_SIGTERM
:
3289 if (s
->kill_context
.send_sigkill
) {
3290 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Killing.");
3291 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
3293 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Skipping SIGKILL.");
3294 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
3299 case SERVICE_STOP_SIGKILL
:
3300 /* Uh, we sent a SIGKILL and it is still not gone?
3301 * Must be something we cannot kill, so let's just be
3302 * weirded out and continue */
3304 log_unit_warning(UNIT(s
), "Processes still around after SIGKILL. Ignoring.");
3305 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
3308 case SERVICE_STOP_POST
:
3309 log_unit_warning(UNIT(s
), "State 'stop-post' timed out. Terminating.");
3310 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
3313 case SERVICE_FINAL_SIGTERM
:
3314 if (s
->kill_context
.send_sigkill
) {
3315 log_unit_warning(UNIT(s
), "State 'stop-final-sigterm' timed out. Killing.");
3316 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
3318 log_unit_warning(UNIT(s
), "State 'stop-final-sigterm' timed out. Skipping SIGKILL. Entering failed mode.");
3319 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, false);
3324 case SERVICE_FINAL_SIGKILL
:
3325 log_unit_warning(UNIT(s
), "Processes still around after final SIGKILL. Entering failed mode.");
3326 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, true);
3329 case SERVICE_AUTO_RESTART
:
3330 log_unit_info(UNIT(s
),
3331 s
->restart_usec
> 0 ?
3332 "Service hold-off time over, scheduling restart." :
3333 "Service has no hold-off time, scheduling restart.");
3334 service_enter_restart(s
);
3338 assert_not_reached("Timeout at wrong time.");
3344 static int service_dispatch_watchdog(sd_event_source
*source
, usec_t usec
, void *userdata
) {
3345 Service
*s
= SERVICE(userdata
);
3346 char t
[FORMAT_TIMESPAN_MAX
];
3347 usec_t watchdog_usec
;
3350 assert(source
== s
->watchdog_event_source
);
3352 watchdog_usec
= service_get_watchdog_usec(s
);
3354 log_unit_error(UNIT(s
), "Watchdog timeout (limit %s)!",
3355 format_timespan(t
, sizeof(t
), watchdog_usec
, 1));
3357 service_enter_signal(s
, SERVICE_STOP_SIGABRT
, SERVICE_FAILURE_WATCHDOG
);
3362 static void service_notify_message(Unit
*u
, pid_t pid
, char **tags
, FDSet
*fds
) {
3363 Service
*s
= SERVICE(u
);
3364 _cleanup_free_
char *cc
= NULL
;
3365 bool notify_dbus
= false;
3370 cc
= strv_join(tags
, ", ");
3372 if (s
->notify_access
== NOTIFY_NONE
) {
3373 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception is disabled.", pid
);
3375 } else if (s
->notify_access
== NOTIFY_MAIN
&& pid
!= s
->main_pid
) {
3376 if (s
->main_pid
!= 0)
3377 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID "PID_FMT
, pid
, s
->main_pid
);
3379 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID which is currently not known", pid
);
3381 } else if (s
->notify_access
== NOTIFY_EXEC
&& pid
!= s
->main_pid
&& pid
!= s
->control_pid
) {
3382 if (s
->main_pid
!= 0 && s
->control_pid
!= 0)
3383 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
,
3384 pid
, s
->main_pid
, s
->control_pid
);
3385 else if (s
->main_pid
!= 0)
3386 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID "PID_FMT
, pid
, s
->main_pid
);
3387 else if (s
->control_pid
!= 0)
3388 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for control PID "PID_FMT
, pid
, s
->control_pid
);
3390 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
);
3393 log_unit_debug(u
, "Got notification message from PID "PID_FMT
" (%s)", pid
, isempty(cc
) ? "n/a" : cc
);
3395 /* Interpret MAINPID= */
3396 e
= strv_find_startswith(tags
, "MAINPID=");
3397 if (e
&& IN_SET(s
->state
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
)) {
3398 if (parse_pid(e
, &pid
) < 0)
3399 log_unit_warning(u
, "Failed to parse MAINPID= field in notification message: %s", e
);
3400 else if (pid
== s
->control_pid
)
3401 log_unit_warning(u
, "A control process cannot also be the main process");
3402 else if (pid
== getpid_cached() || pid
== 1)
3403 log_unit_warning(u
, "Service manager can't be main process, ignoring sd_notify() MAINPID= field");
3405 service_set_main_pid(s
, pid
);
3406 unit_watch_pid(UNIT(s
), pid
);
3411 /* Interpret RELOADING= */
3412 if (strv_find(tags
, "RELOADING=1")) {
3414 s
->notify_state
= NOTIFY_RELOADING
;
3416 if (s
->state
== SERVICE_RUNNING
)
3417 service_enter_reload_by_notify(s
);
3422 /* Interpret READY= */
3423 if (strv_find(tags
, "READY=1")) {
3425 s
->notify_state
= NOTIFY_READY
;
3427 /* Type=notify services inform us about completed
3428 * initialization with READY=1 */
3429 if (s
->type
== SERVICE_NOTIFY
&& s
->state
== SERVICE_START
)
3430 service_enter_start_post(s
);
3432 /* Sending READY=1 while we are reloading informs us
3433 * that the reloading is complete */
3434 if (s
->state
== SERVICE_RELOAD
&& s
->control_pid
== 0)
3435 service_enter_running(s
, SERVICE_SUCCESS
);
3440 /* Interpret STOPPING= */
3441 if (strv_find(tags
, "STOPPING=1")) {
3443 s
->notify_state
= NOTIFY_STOPPING
;
3445 if (s
->state
== SERVICE_RUNNING
)
3446 service_enter_stop_by_notify(s
);
3451 /* Interpret STATUS= */
3452 e
= strv_find_startswith(tags
, "STATUS=");
3454 _cleanup_free_
char *t
= NULL
;
3457 if (!utf8_is_valid(e
))
3458 log_unit_warning(u
, "Status message in notification message is not UTF-8 clean.");
3466 if (!streq_ptr(s
->status_text
, t
)) {
3467 free_and_replace(s
->status_text
, t
);
3472 /* Interpret ERRNO= */
3473 e
= strv_find_startswith(tags
, "ERRNO=");
3477 if (safe_atoi(e
, &status_errno
) < 0 || status_errno
< 0)
3478 log_unit_warning(u
, "Failed to parse ERRNO= field in notification message: %s", e
);
3480 if (s
->status_errno
!= status_errno
) {
3481 s
->status_errno
= status_errno
;
3487 /* Interpret WATCHDOG= */
3488 if (strv_find(tags
, "WATCHDOG=1"))
3489 service_reset_watchdog(s
);
3491 if (strv_find(tags
, "FDSTORE=1")) {
3494 name
= strv_find_startswith(tags
, "FDNAME=");
3495 if (name
&& !fdname_is_valid(name
)) {
3496 log_unit_warning(u
, "Passed FDNAME= name is invalid, ignoring.");
3500 service_add_fd_store_set(s
, fds
, name
);
3503 e
= strv_find_startswith(tags
, "WATCHDOG_USEC=");
3505 usec_t watchdog_override_usec
;
3506 if (safe_atou64(e
, &watchdog_override_usec
) < 0)
3507 log_unit_warning(u
, "Failed to parse WATCHDOG_USEC=%s", e
);
3509 service_reset_watchdog_timeout(s
, watchdog_override_usec
);
3512 /* Notify clients about changed status or main pid */
3514 unit_add_to_dbus_queue(u
);
3517 static int service_get_timeout(Unit
*u
, usec_t
*timeout
) {
3518 Service
*s
= SERVICE(u
);
3522 if (!s
->timer_event_source
)
3525 r
= sd_event_source_get_time(s
->timer_event_source
, &t
);
3528 if (t
== USEC_INFINITY
)
3535 static void service_bus_name_owner_change(
3538 const char *old_owner
,
3539 const char *new_owner
) {
3541 Service
*s
= SERVICE(u
);
3547 assert(streq(s
->bus_name
, name
));
3548 assert(old_owner
|| new_owner
);
3550 if (old_owner
&& new_owner
)
3551 log_unit_debug(u
, "D-Bus name %s changed owner from %s to %s", name
, old_owner
, new_owner
);
3553 log_unit_debug(u
, "D-Bus name %s no longer registered by %s", name
, old_owner
);
3555 log_unit_debug(u
, "D-Bus name %s now registered by %s", name
, new_owner
);
3557 s
->bus_name_good
= !!new_owner
;
3559 /* Track the current owner, so we can reconstruct changes after a daemon reload */
3560 r
= free_and_strdup(&s
->bus_name_owner
, new_owner
);
3562 log_unit_error_errno(u
, r
, "Unable to set new bus name owner %s: %m", new_owner
);
3566 if (s
->type
== SERVICE_DBUS
) {
3568 /* service_enter_running() will figure out what to
3570 if (s
->state
== SERVICE_RUNNING
)
3571 service_enter_running(s
, SERVICE_SUCCESS
);
3572 else if (s
->state
== SERVICE_START
&& new_owner
)
3573 service_enter_start_post(s
);
3575 } else if (new_owner
&&
3583 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
3586 /* Try to acquire PID from bus service */
3588 r
= sd_bus_get_name_creds(u
->manager
->api_bus
, name
, SD_BUS_CREDS_PID
, &creds
);
3590 r
= sd_bus_creds_get_pid(creds
, &pid
);
3592 log_unit_debug(u
, "D-Bus name %s is now owned by process " PID_FMT
, name
, pid
);
3594 service_set_main_pid(s
, pid
);
3595 unit_watch_pid(UNIT(s
), pid
);
3600 int service_set_socket_fd(Service
*s
, int fd
, Socket
*sock
, bool selinux_context_net
) {
3601 _cleanup_free_
char *peer
= NULL
;
3607 /* This is called by the socket code when instantiating a new service for a stream socket and the socket needs
3608 * to be configured. We take ownership of the passed fd on success. */
3610 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
3613 if (s
->socket_fd
>= 0)
3616 if (s
->state
!= SERVICE_DEAD
)
3619 if (getpeername_pretty(fd
, true, &peer
) >= 0) {
3621 if (UNIT(s
)->description
) {
3622 _cleanup_free_
char *a
;
3624 a
= strjoin(UNIT(s
)->description
, " (", peer
, ")");
3628 r
= unit_set_description(UNIT(s
), a
);
3630 r
= unit_set_description(UNIT(s
), peer
);
3636 r
= unit_add_two_dependencies(UNIT(sock
), UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT(s
), false, UNIT_DEPENDENCY_IMPLICIT
);
3641 s
->socket_fd_selinux_context_net
= selinux_context_net
;
3643 unit_ref_set(&s
->accept_socket
, UNIT(sock
));
3647 static void service_reset_failed(Unit
*u
) {
3648 Service
*s
= SERVICE(u
);
3652 if (s
->state
== SERVICE_FAILED
)
3653 service_set_state(s
, SERVICE_DEAD
);
3655 s
->result
= SERVICE_SUCCESS
;
3656 s
->reload_result
= SERVICE_SUCCESS
;
3658 s
->flush_n_restarts
= false;
3661 static int service_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
3662 Service
*s
= SERVICE(u
);
3666 return unit_kill_common(u
, who
, signo
, s
->main_pid
, s
->control_pid
, error
);
3669 static int service_main_pid(Unit
*u
) {
3670 Service
*s
= SERVICE(u
);
3677 static int service_control_pid(Unit
*u
) {
3678 Service
*s
= SERVICE(u
);
3682 return s
->control_pid
;
3685 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
3686 [SERVICE_RESTART_NO
] = "no",
3687 [SERVICE_RESTART_ON_SUCCESS
] = "on-success",
3688 [SERVICE_RESTART_ON_FAILURE
] = "on-failure",
3689 [SERVICE_RESTART_ON_ABNORMAL
] = "on-abnormal",
3690 [SERVICE_RESTART_ON_WATCHDOG
] = "on-watchdog",
3691 [SERVICE_RESTART_ON_ABORT
] = "on-abort",
3692 [SERVICE_RESTART_ALWAYS
] = "always",
3695 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
3697 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
3698 [SERVICE_SIMPLE
] = "simple",
3699 [SERVICE_FORKING
] = "forking",
3700 [SERVICE_ONESHOT
] = "oneshot",
3701 [SERVICE_DBUS
] = "dbus",
3702 [SERVICE_NOTIFY
] = "notify",
3703 [SERVICE_IDLE
] = "idle"
3706 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
3708 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
3709 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
3710 [SERVICE_EXEC_START
] = "ExecStart",
3711 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
3712 [SERVICE_EXEC_RELOAD
] = "ExecReload",
3713 [SERVICE_EXEC_STOP
] = "ExecStop",
3714 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
3717 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
3719 static const char* const notify_state_table
[_NOTIFY_STATE_MAX
] = {
3720 [NOTIFY_UNKNOWN
] = "unknown",
3721 [NOTIFY_READY
] = "ready",
3722 [NOTIFY_RELOADING
] = "reloading",
3723 [NOTIFY_STOPPING
] = "stopping",
3726 DEFINE_STRING_TABLE_LOOKUP(notify_state
, NotifyState
);
3728 static const char* const service_result_table
[_SERVICE_RESULT_MAX
] = {
3729 [SERVICE_SUCCESS
] = "success",
3730 [SERVICE_FAILURE_RESOURCES
] = "resources",
3731 [SERVICE_FAILURE_PROTOCOL
] = "protocol",
3732 [SERVICE_FAILURE_TIMEOUT
] = "timeout",
3733 [SERVICE_FAILURE_EXIT_CODE
] = "exit-code",
3734 [SERVICE_FAILURE_SIGNAL
] = "signal",
3735 [SERVICE_FAILURE_CORE_DUMP
] = "core-dump",
3736 [SERVICE_FAILURE_WATCHDOG
] = "watchdog",
3737 [SERVICE_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
3740 DEFINE_STRING_TABLE_LOOKUP(service_result
, ServiceResult
);
3742 const UnitVTable service_vtable
= {
3743 .object_size
= sizeof(Service
),
3744 .exec_context_offset
= offsetof(Service
, exec_context
),
3745 .cgroup_context_offset
= offsetof(Service
, cgroup_context
),
3746 .kill_context_offset
= offsetof(Service
, kill_context
),
3747 .exec_runtime_offset
= offsetof(Service
, exec_runtime
),
3748 .dynamic_creds_offset
= offsetof(Service
, dynamic_creds
),
3754 .private_section
= "Service",
3756 .init
= service_init
,
3757 .done
= service_done
,
3758 .load
= service_load
,
3759 .release_resources
= service_release_resources
,
3761 .coldplug
= service_coldplug
,
3763 .dump
= service_dump
,
3765 .start
= service_start
,
3766 .stop
= service_stop
,
3767 .reload
= service_reload
,
3769 .can_reload
= service_can_reload
,
3771 .kill
= service_kill
,
3773 .serialize
= service_serialize
,
3774 .deserialize_item
= service_deserialize_item
,
3776 .active_state
= service_active_state
,
3777 .sub_state_to_string
= service_sub_state_to_string
,
3779 .check_gc
= service_check_gc
,
3781 .sigchld_event
= service_sigchld_event
,
3783 .reset_failed
= service_reset_failed
,
3785 .notify_cgroup_empty
= service_notify_cgroup_empty_event
,
3786 .notify_message
= service_notify_message
,
3788 .main_pid
= service_main_pid
,
3789 .control_pid
= service_control_pid
,
3791 .bus_name_owner_change
= service_bus_name_owner_change
,
3793 .bus_vtable
= bus_service_vtable
,
3794 .bus_set_property
= bus_service_set_property
,
3795 .bus_commit_properties
= bus_service_commit_properties
,
3797 .get_timeout
= service_get_timeout
,
3798 .can_transient
= true,
3800 .status_message_formats
= {
3801 .starting_stopping
= {
3802 [0] = "Starting %s...",
3803 [1] = "Stopping %s...",
3805 .finished_start_job
= {
3806 [JOB_DONE
] = "Started %s.",
3807 [JOB_FAILED
] = "Failed to start %s.",
3809 .finished_stop_job
= {
3810 [JOB_DONE
] = "Stopped %s.",
3811 [JOB_FAILED
] = "Stopped (with error) %s.",