1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
30 #include "load-fragment.h"
31 #include "load-dropin.h"
34 #include "unit-name.h"
35 #include "unit-printf.h"
36 #include "dbus-service.h"
38 #include "exit-status.h"
40 #include "path-util.h"
45 #include "bus-error.h"
47 #include "bus-kernel.h"
48 #include "formats-util.h"
49 #include "process-util.h"
50 #include "signal-util.h"
52 static const UnitActiveState state_translation_table
[_SERVICE_STATE_MAX
] = {
53 [SERVICE_DEAD
] = UNIT_INACTIVE
,
54 [SERVICE_START_PRE
] = UNIT_ACTIVATING
,
55 [SERVICE_START
] = UNIT_ACTIVATING
,
56 [SERVICE_START_POST
] = UNIT_ACTIVATING
,
57 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
58 [SERVICE_EXITED
] = UNIT_ACTIVE
,
59 [SERVICE_RELOAD
] = UNIT_RELOADING
,
60 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
61 [SERVICE_STOP_SIGABRT
] = UNIT_DEACTIVATING
,
62 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
63 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
64 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
65 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
66 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
67 [SERVICE_FAILED
] = UNIT_FAILED
,
68 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
71 /* For Type=idle we never want to delay any other jobs, hence we
72 * consider idle jobs active as soon as we start working on them */
73 static const UnitActiveState state_translation_table_idle
[_SERVICE_STATE_MAX
] = {
74 [SERVICE_DEAD
] = UNIT_INACTIVE
,
75 [SERVICE_START_PRE
] = UNIT_ACTIVE
,
76 [SERVICE_START
] = UNIT_ACTIVE
,
77 [SERVICE_START_POST
] = UNIT_ACTIVE
,
78 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
79 [SERVICE_EXITED
] = UNIT_ACTIVE
,
80 [SERVICE_RELOAD
] = UNIT_RELOADING
,
81 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
82 [SERVICE_STOP_SIGABRT
] = UNIT_DEACTIVATING
,
83 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
84 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
85 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
86 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
87 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
88 [SERVICE_FAILED
] = UNIT_FAILED
,
89 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
92 static int service_dispatch_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
);
93 static int service_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
94 static int service_dispatch_watchdog(sd_event_source
*source
, usec_t usec
, void *userdata
);
96 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
);
97 static void service_enter_reload_by_notify(Service
*s
);
99 static void service_init(Unit
*u
) {
100 Service
*s
= SERVICE(u
);
103 assert(u
->load_state
== UNIT_STUB
);
105 s
->timeout_start_usec
= u
->manager
->default_timeout_start_usec
;
106 s
->timeout_stop_usec
= u
->manager
->default_timeout_stop_usec
;
107 s
->restart_usec
= u
->manager
->default_restart_usec
;
108 s
->type
= _SERVICE_TYPE_INVALID
;
110 s
->bus_endpoint_fd
= -1;
111 s
->stdin_fd
= s
->stdout_fd
= s
->stderr_fd
= -1;
112 s
->guess_main_pid
= true;
114 RATELIMIT_INIT(s
->start_limit
, u
->manager
->default_start_limit_interval
, u
->manager
->default_start_limit_burst
);
116 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
119 static void service_unwatch_control_pid(Service
*s
) {
122 if (s
->control_pid
<= 0)
125 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
129 static void service_unwatch_main_pid(Service
*s
) {
132 if (s
->main_pid
<= 0)
135 unit_unwatch_pid(UNIT(s
), s
->main_pid
);
139 static void service_unwatch_pid_file(Service
*s
) {
140 if (!s
->pid_file_pathspec
)
143 log_unit_debug(UNIT(s
), "Stopping watch for PID file %s", s
->pid_file_pathspec
->path
);
144 path_spec_unwatch(s
->pid_file_pathspec
);
145 path_spec_done(s
->pid_file_pathspec
);
146 s
->pid_file_pathspec
= mfree(s
->pid_file_pathspec
);
149 static int service_set_main_pid(Service
*s
, pid_t pid
) {
160 if (s
->main_pid
== pid
&& s
->main_pid_known
)
163 if (s
->main_pid
!= pid
) {
164 service_unwatch_main_pid(s
);
165 exec_status_start(&s
->main_exec_status
, pid
);
169 s
->main_pid_known
= true;
171 if (get_parent_of_pid(pid
, &ppid
) >= 0 && ppid
!= getpid()) {
172 log_unit_warning(UNIT(s
), "Supervising process "PID_FMT
" which is not our child. We'll most likely not notice when it exits.", pid
);
173 s
->main_pid_alien
= true;
175 s
->main_pid_alien
= false;
180 static void service_close_socket_fd(Service
*s
) {
183 s
->socket_fd
= asynchronous_close(s
->socket_fd
);
186 static void service_connection_unref(Service
*s
) {
189 if (!UNIT_ISSET(s
->accept_socket
))
192 socket_connection_unref(SOCKET(UNIT_DEREF(s
->accept_socket
)));
193 unit_ref_unset(&s
->accept_socket
);
196 static void service_stop_watchdog(Service
*s
) {
199 s
->watchdog_event_source
= sd_event_source_unref(s
->watchdog_event_source
);
200 s
->watchdog_timestamp
= DUAL_TIMESTAMP_NULL
;
203 static void service_start_watchdog(Service
*s
) {
208 if (s
->watchdog_usec
<= 0)
211 if (s
->watchdog_event_source
) {
212 r
= sd_event_source_set_time(s
->watchdog_event_source
, s
->watchdog_timestamp
.monotonic
+ s
->watchdog_usec
);
214 log_unit_warning_errno(UNIT(s
), r
, "Failed to reset watchdog timer: %m");
218 r
= sd_event_source_set_enabled(s
->watchdog_event_source
, SD_EVENT_ONESHOT
);
220 r
= sd_event_add_time(
221 UNIT(s
)->manager
->event
,
222 &s
->watchdog_event_source
,
224 s
->watchdog_timestamp
.monotonic
+ s
->watchdog_usec
, 0,
225 service_dispatch_watchdog
, s
);
227 log_unit_warning_errno(UNIT(s
), r
, "Failed to add watchdog timer: %m");
231 (void) sd_event_source_set_description(s
->watchdog_event_source
, "service-watchdog");
233 /* Let's process everything else which might be a sign
234 * of living before we consider a service died. */
235 r
= sd_event_source_set_priority(s
->watchdog_event_source
, SD_EVENT_PRIORITY_IDLE
);
239 log_unit_warning_errno(UNIT(s
), r
, "Failed to install watchdog timer: %m");
242 static void service_reset_watchdog(Service
*s
) {
245 dual_timestamp_get(&s
->watchdog_timestamp
);
246 service_start_watchdog(s
);
249 static void service_fd_store_unlink(ServiceFDStore
*fs
) {
255 assert(fs
->service
->n_fd_store
> 0);
256 LIST_REMOVE(fd_store
, fs
->service
->fd_store
, fs
);
257 fs
->service
->n_fd_store
--;
260 if (fs
->event_source
) {
261 sd_event_source_set_enabled(fs
->event_source
, SD_EVENT_OFF
);
262 sd_event_source_unref(fs
->event_source
);
270 static void service_release_resources(Unit
*u
) {
271 Service
*s
= SERVICE(u
);
275 if (!s
->fd_store
&& s
->stdin_fd
< 0 && s
->stdout_fd
< 0 && s
->stderr_fd
< 0)
278 log_unit_debug(u
, "Releasing all resources.");
280 s
->stdin_fd
= safe_close(s
->stdin_fd
);
281 s
->stdout_fd
= safe_close(s
->stdout_fd
);
282 s
->stderr_fd
= safe_close(s
->stderr_fd
);
285 service_fd_store_unlink(s
->fd_store
);
287 assert(s
->n_fd_store
== 0);
290 static void service_done(Unit
*u
) {
291 Service
*s
= SERVICE(u
);
295 s
->pid_file
= mfree(s
->pid_file
);
296 s
->status_text
= mfree(s
->status_text
);
297 s
->reboot_arg
= mfree(s
->reboot_arg
);
299 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
300 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
301 s
->control_command
= NULL
;
302 s
->main_command
= NULL
;
304 exit_status_set_free(&s
->restart_prevent_status
);
305 exit_status_set_free(&s
->restart_force_status
);
306 exit_status_set_free(&s
->success_status
);
308 /* This will leak a process, but at least no memory or any of
310 service_unwatch_main_pid(s
);
311 service_unwatch_control_pid(s
);
312 service_unwatch_pid_file(s
);
315 unit_unwatch_bus_name(u
, s
->bus_name
);
316 s
->bus_name
= mfree(s
->bus_name
);
319 s
->bus_endpoint_fd
= safe_close(s
->bus_endpoint_fd
);
320 service_close_socket_fd(s
);
321 service_connection_unref(s
);
323 unit_ref_unset(&s
->accept_socket
);
325 service_stop_watchdog(s
);
327 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
329 service_release_resources(u
);
332 static int on_fd_store_io(sd_event_source
*e
, int fd
, uint32_t revents
, void *userdata
) {
333 ServiceFDStore
*fs
= userdata
;
338 /* If we get either EPOLLHUP or EPOLLERR, it's time to remove this entry from the fd store */
339 service_fd_store_unlink(fs
);
343 static int service_add_fd_store(Service
*s
, int fd
, const char *name
) {
350 if (s
->n_fd_store
>= s
->n_fd_store_max
)
353 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
354 r
= same_fd(fs
->fd
, fd
);
358 /* Already included */
364 fs
= new0(ServiceFDStore
, 1);
370 fs
->fdname
= strdup(name
?: "stored");
376 r
= sd_event_add_io(UNIT(s
)->manager
->event
, &fs
->event_source
, fd
, 0, on_fd_store_io
, fs
);
383 (void) sd_event_source_set_description(fs
->event_source
, "service-fd-store");
385 LIST_PREPEND(fd_store
, s
->fd_store
, fs
);
391 static int service_add_fd_store_set(Service
*s
, FDSet
*fds
, const char *name
) {
396 if (fdset_size(fds
) <= 0)
399 while (s
->n_fd_store
< s
->n_fd_store_max
) {
400 _cleanup_close_
int fd
= -1;
402 fd
= fdset_steal_first(fds
);
406 r
= service_add_fd_store(s
, fd
, name
);
408 return log_unit_error_errno(UNIT(s
), r
, "Couldn't add fd to fd store: %m");
410 log_unit_debug(UNIT(s
), "Added fd to fd store.");
415 if (fdset_size(fds
) > 0)
416 log_unit_warning(UNIT(s
), "Tried to store more fds than FDStoreMax=%u allows, closing remaining.", s
->n_fd_store_max
);
421 static int service_arm_timer(Service
*s
, usec_t usec
) {
426 if (s
->timer_event_source
) {
427 r
= sd_event_source_set_time(s
->timer_event_source
, now(CLOCK_MONOTONIC
) + usec
);
431 return sd_event_source_set_enabled(s
->timer_event_source
, SD_EVENT_ONESHOT
);
434 r
= sd_event_add_time(
435 UNIT(s
)->manager
->event
,
436 &s
->timer_event_source
,
438 now(CLOCK_MONOTONIC
) + usec
, 0,
439 service_dispatch_timer
, s
);
443 (void) sd_event_source_set_description(s
->timer_event_source
, "service-timer");
448 static int service_verify(Service
*s
) {
451 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
454 if (!s
->exec_command
[SERVICE_EXEC_START
] && !s
->exec_command
[SERVICE_EXEC_STOP
]) {
455 log_unit_error(UNIT(s
), "Service lacks both ExecStart= and ExecStop= setting. Refusing.");
459 if (s
->type
!= SERVICE_ONESHOT
&& !s
->exec_command
[SERVICE_EXEC_START
]) {
460 log_unit_error(UNIT(s
), "Service has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
464 if (!s
->remain_after_exit
&& !s
->exec_command
[SERVICE_EXEC_START
]) {
465 log_unit_error(UNIT(s
), "Service has no ExecStart= setting, which is only allowed for RemainAfterExit=yes services. Refusing.");
469 if (s
->type
!= SERVICE_ONESHOT
&& s
->exec_command
[SERVICE_EXEC_START
]->command_next
) {
470 log_unit_error(UNIT(s
), "Service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
474 if (s
->type
== SERVICE_ONESHOT
&& s
->restart
!= SERVICE_RESTART_NO
) {
475 log_unit_error(UNIT(s
), "Service has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing.");
479 if (s
->type
== SERVICE_ONESHOT
&& !exit_status_set_is_empty(&s
->restart_force_status
)) {
480 log_unit_error(UNIT(s
), "Service has RestartForceStatus= set, which isn't allowed for Type=oneshot services. Refusing.");
484 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
) {
485 log_unit_error(UNIT(s
), "Service is of type D-Bus but no D-Bus service name has been specified. Refusing.");
489 if (s
->bus_name
&& s
->type
!= SERVICE_DBUS
)
490 log_unit_warning(UNIT(s
), "Service has a D-Bus service name specified, but is not of type dbus. Ignoring.");
492 if (s
->exec_context
.pam_name
&& !(s
->kill_context
.kill_mode
== KILL_CONTROL_GROUP
|| s
->kill_context
.kill_mode
== KILL_MIXED
)) {
493 log_unit_error(UNIT(s
), "Service has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing.");
497 if (s
->usb_function_descriptors
&& !s
->usb_function_strings
)
498 log_unit_warning(UNIT(s
), "Service has USBFunctionDescriptors= setting, but no USBFunctionStrings=. Ignoring.");
500 if (!s
->usb_function_descriptors
&& s
->usb_function_strings
)
501 log_unit_warning(UNIT(s
), "Service has USBFunctionStrings= setting, but no USBFunctionDescriptors=. Ignoring.");
506 static int service_add_default_dependencies(Service
*s
) {
511 /* Add a number of automatic dependencies useful for the
512 * majority of services. */
514 /* First, pull in base system */
515 r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, NULL
, true);
519 /* Second, activate normal shutdown */
520 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true);
523 static void service_fix_output(Service
*s
) {
526 /* If nothing has been explicitly configured, patch default
527 * output in. If input is socket/tty we avoid this however,
528 * since in that case we want output to default to the same
529 * place as we read input from. */
531 if (s
->exec_context
.std_error
== EXEC_OUTPUT_INHERIT
&&
532 s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
533 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
534 s
->exec_context
.std_error
= UNIT(s
)->manager
->default_std_error
;
536 if (s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
537 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
538 s
->exec_context
.std_output
= UNIT(s
)->manager
->default_std_output
;
541 static int service_add_extras(Service
*s
) {
546 if (s
->type
== _SERVICE_TYPE_INVALID
) {
547 /* Figure out a type automatically */
549 s
->type
= SERVICE_DBUS
;
550 else if (s
->exec_command
[SERVICE_EXEC_START
])
551 s
->type
= SERVICE_SIMPLE
;
553 s
->type
= SERVICE_ONESHOT
;
556 /* Oneshot services have disabled start timeout by default */
557 if (s
->type
== SERVICE_ONESHOT
&& !s
->start_timeout_defined
)
558 s
->timeout_start_usec
= 0;
560 service_fix_output(s
);
562 r
= unit_patch_contexts(UNIT(s
));
566 r
= unit_add_exec_dependencies(UNIT(s
), &s
->exec_context
);
570 r
= unit_set_default_slice(UNIT(s
));
574 if (s
->type
== SERVICE_NOTIFY
&& s
->notify_access
== NOTIFY_NONE
)
575 s
->notify_access
= NOTIFY_MAIN
;
577 if (s
->watchdog_usec
> 0 && s
->notify_access
== NOTIFY_NONE
)
578 s
->notify_access
= NOTIFY_MAIN
;
583 n
= strjoina(s
->bus_name
, ".busname");
584 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, n
, NULL
, true);
588 r
= unit_watch_bus_name(UNIT(s
), s
->bus_name
);
590 return log_unit_error_errno(UNIT(s
), r
, "Two services allocated for the same bus name %s, refusing operation.", s
->bus_name
);
592 return log_unit_error_errno(UNIT(s
), r
, "Cannot watch bus name %s: %m", s
->bus_name
);
595 if (UNIT(s
)->default_dependencies
) {
596 r
= service_add_default_dependencies(s
);
604 static int service_load(Unit
*u
) {
605 Service
*s
= SERVICE(u
);
610 /* Load a .service file */
611 r
= unit_load_fragment(u
);
615 /* Still nothing found? Then let's give up */
616 if (u
->load_state
== UNIT_STUB
)
619 /* This is a new unit? Then let's add in some extras */
620 if (u
->load_state
== UNIT_LOADED
) {
622 /* We were able to load something, then let's add in
623 * the dropin directories. */
624 r
= unit_load_dropin(u
);
628 /* This is a new unit? Then let's add in some
630 r
= service_add_extras(s
);
635 return service_verify(s
);
638 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
639 ServiceExecCommand c
;
640 Service
*s
= SERVICE(u
);
645 prefix
= strempty(prefix
);
646 prefix2
= strjoina(prefix
, "\t");
649 "%sService State: %s\n"
651 "%sReload Result: %s\n"
652 "%sPermissionsStartOnly: %s\n"
653 "%sRootDirectoryStartOnly: %s\n"
654 "%sRemainAfterExit: %s\n"
655 "%sGuessMainPID: %s\n"
658 "%sNotifyAccess: %s\n"
659 "%sNotifyState: %s\n",
660 prefix
, service_state_to_string(s
->state
),
661 prefix
, service_result_to_string(s
->result
),
662 prefix
, service_result_to_string(s
->reload_result
),
663 prefix
, yes_no(s
->permissions_start_only
),
664 prefix
, yes_no(s
->root_directory_start_only
),
665 prefix
, yes_no(s
->remain_after_exit
),
666 prefix
, yes_no(s
->guess_main_pid
),
667 prefix
, service_type_to_string(s
->type
),
668 prefix
, service_restart_to_string(s
->restart
),
669 prefix
, notify_access_to_string(s
->notify_access
),
670 prefix
, notify_state_to_string(s
->notify_state
));
672 if (s
->control_pid
> 0)
674 "%sControl PID: "PID_FMT
"\n",
675 prefix
, s
->control_pid
);
679 "%sMain PID: "PID_FMT
"\n"
680 "%sMain PID Known: %s\n"
681 "%sMain PID Alien: %s\n",
683 prefix
, yes_no(s
->main_pid_known
),
684 prefix
, yes_no(s
->main_pid_alien
));
689 prefix
, s
->pid_file
);
694 "%sBus Name Good: %s\n",
696 prefix
, yes_no(s
->bus_name_good
));
698 kill_context_dump(&s
->kill_context
, f
, prefix
);
699 exec_context_dump(&s
->exec_context
, f
, prefix
);
701 for (c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
703 if (!s
->exec_command
[c
])
706 fprintf(f
, "%s-> %s:\n",
707 prefix
, service_exec_command_to_string(c
));
709 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
713 fprintf(f
, "%sStatus Text: %s\n",
714 prefix
, s
->status_text
);
716 if (s
->n_fd_store_max
> 0)
718 "%sFile Descriptor Store Max: %u\n"
719 "%sFile Descriptor Store Current: %u\n",
720 prefix
, s
->n_fd_store_max
,
721 prefix
, s
->n_fd_store
);
724 static int service_load_pid_file(Service
*s
, bool may_warn
) {
725 _cleanup_free_
char *k
= NULL
;
734 r
= read_one_line_file(s
->pid_file
, &k
);
737 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
));
741 r
= parse_pid(k
, &pid
);
744 log_unit_info_errno(UNIT(s
), r
, "Failed to read PID from file %s: %m", s
->pid_file
);
748 if (!pid_is_alive(pid
)) {
750 log_unit_info(UNIT(s
), "PID "PID_FMT
" read from file %s does not exist or is a zombie.", pid
, s
->pid_file
);
754 if (s
->main_pid_known
) {
755 if (pid
== s
->main_pid
)
758 log_unit_debug(UNIT(s
), "Main PID changing: "PID_FMT
" -> "PID_FMT
, s
->main_pid
, pid
);
760 service_unwatch_main_pid(s
);
761 s
->main_pid_known
= false;
763 log_unit_debug(UNIT(s
), "Main PID loaded: "PID_FMT
, pid
);
765 r
= service_set_main_pid(s
, pid
);
769 r
= unit_watch_pid(UNIT(s
), pid
);
771 /* FIXME: we need to do something here */
772 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" for service: %m", pid
);
779 static int service_search_main_pid(Service
*s
) {
785 /* If we know it anyway, don't ever fallback to unreliable
787 if (s
->main_pid_known
)
790 if (!s
->guess_main_pid
)
793 assert(s
->main_pid
<= 0);
795 r
= unit_search_main_pid(UNIT(s
), &pid
);
799 log_unit_debug(UNIT(s
), "Main PID guessed: "PID_FMT
, pid
);
800 r
= service_set_main_pid(s
, pid
);
804 r
= unit_watch_pid(UNIT(s
), pid
);
806 /* FIXME: we need to do something here */
807 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" from: %m", pid
);
814 static void service_set_state(Service
*s
, ServiceState state
) {
815 ServiceState old_state
;
816 const UnitActiveState
*table
;
820 table
= s
->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
822 old_state
= s
->state
;
825 service_unwatch_pid_file(s
);
828 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
830 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
831 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
,
832 SERVICE_AUTO_RESTART
))
833 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
836 SERVICE_START
, SERVICE_START_POST
,
837 SERVICE_RUNNING
, SERVICE_RELOAD
,
838 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
839 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
840 service_unwatch_main_pid(s
);
841 s
->main_command
= NULL
;
845 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
847 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
848 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
849 service_unwatch_control_pid(s
);
850 s
->control_command
= NULL
;
851 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
854 if (IN_SET(state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
855 unit_unwatch_all_pids(UNIT(s
));
858 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
859 SERVICE_RUNNING
, SERVICE_RELOAD
,
860 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
861 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
) &&
862 !(state
== SERVICE_DEAD
&& UNIT(s
)->job
)) {
863 service_close_socket_fd(s
);
864 service_connection_unref(s
);
867 if (!IN_SET(state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
868 service_stop_watchdog(s
);
870 /* For the inactive states unit_notify() will trim the cgroup,
871 * but for exit we have to do that ourselves... */
872 if (state
== SERVICE_EXITED
&& UNIT(s
)->manager
->n_reloading
<= 0)
873 unit_prune_cgroup(UNIT(s
));
875 /* For remain_after_exit services, let's see if we can "release" the
876 * hold on the console, since unit_notify() only does that in case of
878 if (state
== SERVICE_EXITED
&&
879 s
->remain_after_exit
&&
880 UNIT(s
)->manager
->n_on_console
> 0) {
884 ec
= unit_get_exec_context(UNIT(s
));
885 if (ec
&& exec_context_may_touch_console(ec
)) {
886 Manager
*m
= UNIT(s
)->manager
;
889 if (m
->n_on_console
== 0)
890 /* unset no_console_output flag, since the console is free */
891 m
->no_console_output
= false;
895 if (old_state
!= state
)
896 log_unit_debug(UNIT(s
), "Changed %s -> %s", service_state_to_string(old_state
), service_state_to_string(state
));
898 unit_notify(UNIT(s
), table
[old_state
], table
[state
], s
->reload_result
== SERVICE_SUCCESS
);
901 static int service_coldplug(Unit
*u
) {
902 Service
*s
= SERVICE(u
);
906 assert(s
->state
== SERVICE_DEAD
);
908 if (s
->deserialized_state
!= s
->state
) {
910 if (IN_SET(s
->deserialized_state
,
911 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
913 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
914 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
918 k
= IN_SET(s
->deserialized_state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RELOAD
) ? s
->timeout_start_usec
: s
->timeout_stop_usec
;
920 /* For the start/stop timeouts 0 means off */
922 r
= service_arm_timer(s
, k
);
928 if (s
->deserialized_state
== SERVICE_AUTO_RESTART
) {
930 /* The restart timeouts 0 means immediately */
931 r
= service_arm_timer(s
, s
->restart_usec
);
936 if (pid_is_unwaited(s
->main_pid
) &&
937 ((s
->deserialized_state
== SERVICE_START
&& IN_SET(s
->type
, SERVICE_FORKING
, SERVICE_DBUS
, SERVICE_ONESHOT
, SERVICE_NOTIFY
)) ||
938 IN_SET(s
->deserialized_state
,
939 SERVICE_START
, SERVICE_START_POST
,
940 SERVICE_RUNNING
, SERVICE_RELOAD
,
941 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
942 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))) {
943 r
= unit_watch_pid(UNIT(s
), s
->main_pid
);
948 if (pid_is_unwaited(s
->control_pid
) &&
949 IN_SET(s
->deserialized_state
,
950 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
952 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
953 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
954 r
= unit_watch_pid(UNIT(s
), s
->control_pid
);
959 if (!IN_SET(s
->deserialized_state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
960 unit_watch_all_pids(UNIT(s
));
962 if (IN_SET(s
->deserialized_state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
963 service_start_watchdog(s
);
965 service_set_state(s
, s
->deserialized_state
);
971 static int service_collect_fds(Service
*s
, int **fds
, char ***fd_names
) {
972 _cleanup_strv_free_
char **rfd_names
= NULL
;
973 _cleanup_free_
int *rfds
= NULL
;
980 if (s
->socket_fd
>= 0) {
982 /* Pass the per-connection socket */
987 rfds
[0] = s
->socket_fd
;
989 rfd_names
= strv_new("connection", NULL
);
998 /* Pass all our configured sockets for singleton services */
1000 SET_FOREACH(u
, UNIT(s
)->dependencies
[UNIT_TRIGGERED_BY
], i
) {
1001 _cleanup_free_
int *cfds
= NULL
;
1005 if (u
->type
!= UNIT_SOCKET
)
1010 cn_fds
= socket_collect_fds(sock
, &cfds
);
1025 t
= realloc(rfds
, (rn_fds
+ cn_fds
) * sizeof(int));
1029 memcpy(t
+ rn_fds
, cfds
, cn_fds
* sizeof(int));
1035 r
= strv_extend_n(&rfd_names
, socket_fdname(sock
), cn_fds
);
1041 if (s
->n_fd_store
> 0) {
1046 t
= realloc(rfds
, (rn_fds
+ s
->n_fd_store
) * sizeof(int));
1052 nl
= realloc(rfd_names
, (rn_fds
+ s
->n_fd_store
+ 1) * sizeof(char*));
1058 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
1059 rfds
[rn_fds
] = fs
->fd
;
1060 rfd_names
[rn_fds
] = strdup(strempty(fs
->fdname
));
1061 if (!rfd_names
[rn_fds
])
1067 rfd_names
[rn_fds
] = NULL
;
1071 *fd_names
= rfd_names
;
1079 static int service_spawn(
1084 bool apply_permissions
,
1086 bool apply_tty_stdin
,
1090 _cleanup_strv_free_
char **argv
= NULL
, **final_env
= NULL
, **our_env
= NULL
, **fd_names
= NULL
;
1091 _cleanup_free_
char *bus_endpoint_path
= NULL
;
1092 _cleanup_free_
int *fds
= NULL
;
1093 unsigned n_fds
= 0, n_env
= 0;
1097 ExecParameters exec_params
= {
1098 .apply_permissions
= apply_permissions
,
1099 .apply_chroot
= apply_chroot
,
1100 .apply_tty_stdin
= apply_tty_stdin
,
1101 .bus_endpoint_fd
= -1,
1113 (void) unit_realize_cgroup(UNIT(s
));
1114 if (s
->reset_cpu_usage
) {
1115 (void) unit_reset_cpu_usage(UNIT(s
));
1116 s
->reset_cpu_usage
= false;
1119 r
= unit_setup_exec_runtime(UNIT(s
));
1124 s
->exec_context
.std_input
== EXEC_INPUT_SOCKET
||
1125 s
->exec_context
.std_output
== EXEC_OUTPUT_SOCKET
||
1126 s
->exec_context
.std_error
== EXEC_OUTPUT_SOCKET
) {
1128 r
= service_collect_fds(s
, &fds
, &fd_names
);
1136 r
= service_arm_timer(s
, timeout
);
1140 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
1142 r
= unit_full_printf_strv(UNIT(s
), c
->argv
, &argv
);
1146 our_env
= new0(char*, 6);
1152 if (is_control
? s
->notify_access
== NOTIFY_ALL
: s
->notify_access
!= NOTIFY_NONE
)
1153 if (asprintf(our_env
+ n_env
++, "NOTIFY_SOCKET=%s", UNIT(s
)->manager
->notify_socket
) < 0) {
1158 if (s
->main_pid
> 0)
1159 if (asprintf(our_env
+ n_env
++, "MAINPID="PID_FMT
, s
->main_pid
) < 0) {
1164 if (UNIT(s
)->manager
->running_as
!= MANAGER_SYSTEM
)
1165 if (asprintf(our_env
+ n_env
++, "MANAGERPID="PID_FMT
, getpid()) < 0) {
1170 if (s
->socket_fd
>= 0) {
1171 union sockaddr_union sa
;
1172 socklen_t salen
= sizeof(sa
);
1174 r
= getpeername(s
->socket_fd
, &sa
.sa
, &salen
);
1180 if (IN_SET(sa
.sa
.sa_family
, AF_INET
, AF_INET6
)) {
1181 _cleanup_free_
char *addr
= NULL
;
1185 r
= sockaddr_pretty(&sa
.sa
, salen
, true, false, &addr
);
1189 t
= strappend("REMOTE_ADDR=", addr
);
1194 our_env
[n_env
++] = t
;
1196 port
= sockaddr_port(&sa
.sa
);
1202 if (asprintf(&t
, "REMOTE_PORT=%u", port
) < 0) {
1206 our_env
[n_env
++] = t
;
1210 final_env
= strv_env_merge(2, UNIT(s
)->manager
->environment
, our_env
, NULL
);
1216 if (is_control
&& UNIT(s
)->cgroup_path
) {
1217 path
= strjoina(UNIT(s
)->cgroup_path
, "/control");
1218 cg_create(SYSTEMD_CGROUP_CONTROLLER
, path
);
1220 path
= UNIT(s
)->cgroup_path
;
1222 if (s
->exec_context
.bus_endpoint
) {
1223 r
= bus_kernel_create_endpoint(UNIT(s
)->manager
->running_as
== MANAGER_SYSTEM
? "system" : "user",
1224 UNIT(s
)->id
, &bus_endpoint_path
);
1228 /* Pass the fd to the exec_params so that the child process can upload the policy.
1229 * Keep a reference to the fd in the service, so the endpoint is kept alive as long
1230 * as the service is running. */
1231 exec_params
.bus_endpoint_fd
= s
->bus_endpoint_fd
= r
;
1234 exec_params
.argv
= argv
;
1235 exec_params
.fds
= fds
;
1236 exec_params
.fd_names
= fd_names
;
1237 exec_params
.n_fds
= n_fds
;
1238 exec_params
.environment
= final_env
;
1239 exec_params
.confirm_spawn
= UNIT(s
)->manager
->confirm_spawn
;
1240 exec_params
.cgroup_supported
= UNIT(s
)->manager
->cgroup_supported
;
1241 exec_params
.cgroup_path
= path
;
1242 exec_params
.cgroup_delegate
= s
->cgroup_context
.delegate
;
1243 exec_params
.runtime_prefix
= manager_get_runtime_prefix(UNIT(s
)->manager
);
1244 exec_params
.watchdog_usec
= s
->watchdog_usec
;
1245 exec_params
.bus_endpoint_path
= bus_endpoint_path
;
1246 exec_params
.selinux_context_net
= s
->socket_fd_selinux_context_net
;
1247 if (s
->type
== SERVICE_IDLE
)
1248 exec_params
.idle_pipe
= UNIT(s
)->manager
->idle_pipe
;
1249 exec_params
.stdin_fd
= s
->stdin_fd
;
1250 exec_params
.stdout_fd
= s
->stdout_fd
;
1251 exec_params
.stderr_fd
= s
->stderr_fd
;
1253 r
= exec_spawn(UNIT(s
),
1262 r
= unit_watch_pid(UNIT(s
), pid
);
1264 /* FIXME: we need to do something here */
1273 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
1278 static int main_pid_good(Service
*s
) {
1281 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1284 /* If we know the pid file, then let's just check if it is
1286 if (s
->main_pid_known
) {
1288 /* If it's an alien child let's check if it is still
1290 if (s
->main_pid_alien
&& s
->main_pid
> 0)
1291 return pid_is_alive(s
->main_pid
);
1293 /* .. otherwise assume we'll get a SIGCHLD for it,
1294 * which we really should wait for to collect exit
1295 * status and code */
1296 return s
->main_pid
> 0;
1299 /* We don't know the pid */
1303 _pure_
static int control_pid_good(Service
*s
) {
1306 return s
->control_pid
> 0;
1309 static int cgroup_good(Service
*s
) {
1314 if (!UNIT(s
)->cgroup_path
)
1317 r
= cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, UNIT(s
)->cgroup_path
);
1324 static bool service_shall_restart(Service
*s
) {
1327 /* Don't restart after manual stops */
1328 if (s
->forbid_restart
)
1331 /* Never restart if this is configured as special exception */
1332 if (exit_status_set_test(&s
->restart_prevent_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
))
1335 /* Restart if the exit code/status are configured as restart triggers */
1336 if (exit_status_set_test(&s
->restart_force_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
))
1339 switch (s
->restart
) {
1341 case SERVICE_RESTART_NO
:
1344 case SERVICE_RESTART_ALWAYS
:
1347 case SERVICE_RESTART_ON_SUCCESS
:
1348 return s
->result
== SERVICE_SUCCESS
;
1350 case SERVICE_RESTART_ON_FAILURE
:
1351 return s
->result
!= SERVICE_SUCCESS
;
1353 case SERVICE_RESTART_ON_ABNORMAL
:
1354 return !IN_SET(s
->result
, SERVICE_SUCCESS
, SERVICE_FAILURE_EXIT_CODE
);
1356 case SERVICE_RESTART_ON_WATCHDOG
:
1357 return s
->result
== SERVICE_FAILURE_WATCHDOG
;
1359 case SERVICE_RESTART_ON_ABORT
:
1360 return IN_SET(s
->result
, SERVICE_FAILURE_SIGNAL
, SERVICE_FAILURE_CORE_DUMP
);
1363 assert_not_reached("unknown restart setting");
1367 static void service_enter_dead(Service
*s
, ServiceResult f
, bool allow_restart
) {
1371 if (f
!= SERVICE_SUCCESS
)
1374 service_set_state(s
, s
->result
!= SERVICE_SUCCESS
? SERVICE_FAILED
: SERVICE_DEAD
);
1376 if (s
->result
!= SERVICE_SUCCESS
) {
1377 log_unit_warning(UNIT(s
), "Failed with result '%s'.", service_result_to_string(s
->result
));
1378 failure_action(UNIT(s
)->manager
, s
->failure_action
, s
->reboot_arg
);
1381 if (allow_restart
&& service_shall_restart(s
)) {
1383 r
= service_arm_timer(s
, s
->restart_usec
);
1387 service_set_state(s
, SERVICE_AUTO_RESTART
);
1390 /* The next restart might not be a manual stop, hence reset the flag indicating manual stops */
1391 s
->forbid_restart
= false;
1393 /* We want fresh tmpdirs in case service is started again immediately */
1394 exec_runtime_destroy(s
->exec_runtime
);
1395 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
1397 /* Also, remove the runtime directory in */
1398 exec_context_destroy_runtime_directory(&s
->exec_context
, manager_get_runtime_prefix(UNIT(s
)->manager
));
1400 /* Try to delete the pid file. At this point it will be
1401 * out-of-date, and some software might be confused by it, so
1402 * let's remove it. */
1404 unlink_noerrno(s
->pid_file
);
1409 log_unit_warning_errno(UNIT(s
), r
, "Failed to run install restart timer: %m");
1410 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1413 static void service_enter_stop_post(Service
*s
, ServiceResult f
) {
1417 if (f
!= SERVICE_SUCCESS
)
1420 service_unwatch_control_pid(s
);
1421 unit_watch_all_pids(UNIT(s
));
1423 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
];
1424 if (s
->control_command
) {
1425 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
1427 r
= service_spawn(s
,
1429 s
->timeout_stop_usec
,
1431 !s
->permissions_start_only
,
1432 !s
->root_directory_start_only
,
1439 service_set_state(s
, SERVICE_STOP_POST
);
1441 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_SUCCESS
);
1446 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop-post' task: %m");
1447 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1450 static int state_to_kill_operation(ServiceState state
) {
1453 case SERVICE_STOP_SIGABRT
:
1456 case SERVICE_STOP_SIGTERM
:
1457 case SERVICE_FINAL_SIGTERM
:
1458 return KILL_TERMINATE
;
1460 case SERVICE_STOP_SIGKILL
:
1461 case SERVICE_FINAL_SIGKILL
:
1465 return _KILL_OPERATION_INVALID
;
1469 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
) {
1474 if (f
!= SERVICE_SUCCESS
)
1477 unit_watch_all_pids(UNIT(s
));
1479 r
= unit_kill_context(
1482 state_to_kill_operation(state
),
1491 if (s
->timeout_stop_usec
> 0) {
1492 r
= service_arm_timer(s
, s
->timeout_stop_usec
);
1497 service_set_state(s
, state
);
1498 } else if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
) && s
->kill_context
.send_sigkill
)
1499 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_SUCCESS
);
1500 else if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
1501 service_enter_stop_post(s
, SERVICE_SUCCESS
);
1502 else if (state
== SERVICE_FINAL_SIGTERM
&& s
->kill_context
.send_sigkill
)
1503 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_SUCCESS
);
1505 service_enter_dead(s
, SERVICE_SUCCESS
, true);
1510 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
1512 if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
1513 service_enter_stop_post(s
, SERVICE_FAILURE_RESOURCES
);
1515 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1518 static void service_enter_stop_by_notify(Service
*s
) {
1521 unit_watch_all_pids(UNIT(s
));
1523 if (s
->timeout_stop_usec
> 0)
1524 service_arm_timer(s
, s
->timeout_stop_usec
);
1526 /* The service told us it's stopping, so it's as if we SIGTERM'd it. */
1527 service_set_state(s
, SERVICE_STOP_SIGTERM
);
1530 static void service_enter_stop(Service
*s
, ServiceResult f
) {
1535 if (f
!= SERVICE_SUCCESS
)
1538 service_unwatch_control_pid(s
);
1539 unit_watch_all_pids(UNIT(s
));
1541 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
];
1542 if (s
->control_command
) {
1543 s
->control_command_id
= SERVICE_EXEC_STOP
;
1545 r
= service_spawn(s
,
1547 s
->timeout_stop_usec
,
1549 !s
->permissions_start_only
,
1550 !s
->root_directory_start_only
,
1557 service_set_state(s
, SERVICE_STOP
);
1559 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
1564 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop' task: %m");
1565 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1568 static bool service_good(Service
*s
) {
1572 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name_good
)
1575 main_pid_ok
= main_pid_good(s
);
1576 if (main_pid_ok
> 0) /* It's alive */
1578 if (main_pid_ok
== 0) /* It's dead */
1581 /* OK, we don't know anything about the main PID, maybe
1582 * because there is none. Let's check the control group
1585 return cgroup_good(s
) != 0;
1588 static void service_enter_running(Service
*s
, ServiceResult f
) {
1591 if (f
!= SERVICE_SUCCESS
)
1594 if (service_good(s
)) {
1596 /* If there are any queued up sd_notify()
1597 * notifications, process them now */
1598 if (s
->notify_state
== NOTIFY_RELOADING
)
1599 service_enter_reload_by_notify(s
);
1600 else if (s
->notify_state
== NOTIFY_STOPPING
)
1601 service_enter_stop_by_notify(s
);
1603 service_set_state(s
, SERVICE_RUNNING
);
1605 } else if (s
->remain_after_exit
)
1606 service_set_state(s
, SERVICE_EXITED
);
1608 service_enter_stop(s
, SERVICE_SUCCESS
);
1611 static void service_enter_start_post(Service
*s
) {
1615 service_unwatch_control_pid(s
);
1616 service_reset_watchdog(s
);
1618 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
];
1619 if (s
->control_command
) {
1620 s
->control_command_id
= SERVICE_EXEC_START_POST
;
1622 r
= service_spawn(s
,
1624 s
->timeout_start_usec
,
1626 !s
->permissions_start_only
,
1627 !s
->root_directory_start_only
,
1634 service_set_state(s
, SERVICE_START_POST
);
1636 service_enter_running(s
, SERVICE_SUCCESS
);
1641 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-post' task: %m");
1642 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1645 static void service_kill_control_processes(Service
*s
) {
1648 if (!UNIT(s
)->cgroup_path
)
1651 p
= strjoina(UNIT(s
)->cgroup_path
, "/control");
1652 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER
, p
, SIGKILL
, true, true, true, NULL
);
1655 static void service_enter_start(Service
*s
) {
1662 service_unwatch_control_pid(s
);
1663 service_unwatch_main_pid(s
);
1665 /* We want to ensure that nobody leaks processes from
1666 * START_PRE here, so let's go on a killing spree, People
1667 * should not spawn long running processes from START_PRE. */
1668 service_kill_control_processes(s
);
1670 if (s
->type
== SERVICE_FORKING
) {
1671 s
->control_command_id
= SERVICE_EXEC_START
;
1672 c
= s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1674 s
->main_command
= NULL
;
1676 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1677 s
->control_command
= NULL
;
1679 c
= s
->main_command
= s
->exec_command
[SERVICE_EXEC_START
];
1683 assert(s
->type
== SERVICE_ONESHOT
);
1684 service_enter_start_post(s
);
1688 r
= service_spawn(s
,
1690 IN_SET(s
->type
, SERVICE_FORKING
, SERVICE_DBUS
, SERVICE_NOTIFY
, SERVICE_ONESHOT
) ? s
->timeout_start_usec
: 0,
1700 if (s
->type
== SERVICE_SIMPLE
|| s
->type
== SERVICE_IDLE
) {
1701 /* For simple services we immediately start
1702 * the START_POST binaries. */
1704 service_set_main_pid(s
, pid
);
1705 service_enter_start_post(s
);
1707 } else if (s
->type
== SERVICE_FORKING
) {
1709 /* For forking services we wait until the start
1710 * process exited. */
1712 s
->control_pid
= pid
;
1713 service_set_state(s
, SERVICE_START
);
1715 } else if (s
->type
== SERVICE_ONESHOT
||
1716 s
->type
== SERVICE_DBUS
||
1717 s
->type
== SERVICE_NOTIFY
) {
1719 /* For oneshot services we wait until the start
1720 * process exited, too, but it is our main process. */
1722 /* For D-Bus services we know the main pid right away,
1723 * but wait for the bus name to appear on the
1724 * bus. Notify services are similar. */
1726 service_set_main_pid(s
, pid
);
1727 service_set_state(s
, SERVICE_START
);
1729 assert_not_reached("Unknown service type");
1734 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start' task: %m");
1735 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1738 static void service_enter_start_pre(Service
*s
) {
1743 service_unwatch_control_pid(s
);
1745 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
];
1746 if (s
->control_command
) {
1747 /* Before we start anything, let's clear up what might
1748 * be left from previous runs. */
1749 service_kill_control_processes(s
);
1751 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
1753 r
= service_spawn(s
,
1755 s
->timeout_start_usec
,
1757 !s
->permissions_start_only
,
1758 !s
->root_directory_start_only
,
1765 service_set_state(s
, SERVICE_START_PRE
);
1767 service_enter_start(s
);
1772 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-pre' task: %m");
1773 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1776 static void service_enter_restart(Service
*s
) {
1777 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
1782 if (UNIT(s
)->job
&& UNIT(s
)->job
->type
== JOB_STOP
) {
1783 /* Don't restart things if we are going down anyway */
1784 log_unit_info(UNIT(s
), "Stop job pending for unit, delaying automatic restart.");
1786 r
= service_arm_timer(s
, s
->restart_usec
);
1793 /* Any units that are bound to this service must also be
1794 * restarted. We use JOB_RESTART (instead of the more obvious
1795 * JOB_START) here so that those dependency jobs will be added
1797 r
= manager_add_job(UNIT(s
)->manager
, JOB_RESTART
, UNIT(s
), JOB_FAIL
, false, &error
, NULL
);
1801 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
1802 * it will be canceled as part of the service_stop() call that
1803 * is executed as part of JOB_RESTART. */
1805 log_unit_debug(UNIT(s
), "Scheduled restart job.");
1809 log_unit_warning(UNIT(s
), "Failed to schedule restart job: %s", bus_error_message(&error
, -r
));
1810 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1813 static void service_enter_reload_by_notify(Service
*s
) {
1816 if (s
->timeout_start_usec
> 0)
1817 service_arm_timer(s
, s
->timeout_start_usec
);
1819 service_set_state(s
, SERVICE_RELOAD
);
1822 static void service_enter_reload(Service
*s
) {
1827 service_unwatch_control_pid(s
);
1828 s
->reload_result
= SERVICE_SUCCESS
;
1830 s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
];
1831 if (s
->control_command
) {
1832 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
1834 r
= service_spawn(s
,
1836 s
->timeout_start_usec
,
1838 !s
->permissions_start_only
,
1839 !s
->root_directory_start_only
,
1846 service_set_state(s
, SERVICE_RELOAD
);
1848 service_enter_running(s
, SERVICE_SUCCESS
);
1853 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'reload' task: %m");
1854 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
1855 service_enter_running(s
, SERVICE_SUCCESS
);
1858 static void service_run_next_control(Service
*s
) {
1862 assert(s
->control_command
);
1863 assert(s
->control_command
->command_next
);
1865 assert(s
->control_command_id
!= SERVICE_EXEC_START
);
1867 s
->control_command
= s
->control_command
->command_next
;
1868 service_unwatch_control_pid(s
);
1870 r
= service_spawn(s
,
1872 IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
) ? s
->timeout_start_usec
: s
->timeout_stop_usec
,
1874 !s
->permissions_start_only
,
1875 !s
->root_directory_start_only
,
1876 s
->control_command_id
== SERVICE_EXEC_START_PRE
||
1877 s
->control_command_id
== SERVICE_EXEC_STOP_POST
,
1886 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next control task: %m");
1888 if (s
->state
== SERVICE_START_PRE
)
1889 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1890 else if (s
->state
== SERVICE_STOP
)
1891 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1892 else if (s
->state
== SERVICE_STOP_POST
)
1893 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1894 else if (s
->state
== SERVICE_RELOAD
) {
1895 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
1896 service_enter_running(s
, SERVICE_SUCCESS
);
1898 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1901 static void service_run_next_main(Service
*s
) {
1906 assert(s
->main_command
);
1907 assert(s
->main_command
->command_next
);
1908 assert(s
->type
== SERVICE_ONESHOT
);
1910 s
->main_command
= s
->main_command
->command_next
;
1911 service_unwatch_main_pid(s
);
1913 r
= service_spawn(s
,
1915 s
->timeout_start_usec
,
1925 service_set_main_pid(s
, pid
);
1930 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next main task: %m");
1931 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1934 static int service_start_limit_test(Service
*s
) {
1937 if (ratelimit_test(&s
->start_limit
))
1940 log_unit_warning(UNIT(s
), "Start request repeated too quickly.");
1942 return failure_action(UNIT(s
)->manager
, s
->start_limit_action
, s
->reboot_arg
);
1945 static int service_start(Unit
*u
) {
1946 Service
*s
= SERVICE(u
);
1951 /* We cannot fulfill this request right now, try again later
1953 if (IN_SET(s
->state
,
1954 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1955 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))
1958 /* Already on it! */
1959 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
))
1962 /* A service that will be restarted must be stopped first to
1963 * trigger BindsTo and/or OnFailure dependencies. If a user
1964 * does not want to wait for the holdoff time to elapse, the
1965 * service should be manually restarted, not started. We
1966 * simply return EAGAIN here, so that any start jobs stay
1967 * queued, and assume that the auto restart timer will
1968 * eventually trigger the restart. */
1969 if (s
->state
== SERVICE_AUTO_RESTART
)
1972 assert(IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_FAILED
));
1974 /* Make sure we don't enter a busy loop of some kind. */
1975 r
= service_start_limit_test(s
);
1977 service_enter_dead(s
, SERVICE_FAILURE_START_LIMIT
, false);
1981 s
->result
= SERVICE_SUCCESS
;
1982 s
->reload_result
= SERVICE_SUCCESS
;
1983 s
->main_pid_known
= false;
1984 s
->main_pid_alien
= false;
1985 s
->forbid_restart
= false;
1986 s
->reset_cpu_usage
= true;
1988 s
->status_text
= mfree(s
->status_text
);
1989 s
->status_errno
= 0;
1991 s
->notify_state
= NOTIFY_UNKNOWN
;
1993 service_enter_start_pre(s
);
1997 static int service_stop(Unit
*u
) {
1998 Service
*s
= SERVICE(u
);
2002 /* Don't create restart jobs from manual stops. */
2003 s
->forbid_restart
= true;
2006 if (IN_SET(s
->state
,
2007 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
2008 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))
2011 /* A restart will be scheduled or is in progress. */
2012 if (s
->state
== SERVICE_AUTO_RESTART
) {
2013 service_set_state(s
, SERVICE_DEAD
);
2017 /* If there's already something running we go directly into
2019 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RELOAD
)) {
2020 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
2024 assert(IN_SET(s
->state
, SERVICE_RUNNING
, SERVICE_EXITED
));
2026 service_enter_stop(s
, SERVICE_SUCCESS
);
2030 static int service_reload(Unit
*u
) {
2031 Service
*s
= SERVICE(u
);
2035 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
2037 service_enter_reload(s
);
2041 _pure_
static bool service_can_reload(Unit
*u
) {
2042 Service
*s
= SERVICE(u
);
2046 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
2049 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
2050 Service
*s
= SERVICE(u
);
2058 unit_serialize_item(u
, f
, "state", service_state_to_string(s
->state
));
2059 unit_serialize_item(u
, f
, "result", service_result_to_string(s
->result
));
2060 unit_serialize_item(u
, f
, "reload-result", service_result_to_string(s
->reload_result
));
2062 if (s
->control_pid
> 0)
2063 unit_serialize_item_format(u
, f
, "control-pid", PID_FMT
, s
->control_pid
);
2065 if (s
->main_pid_known
&& s
->main_pid
> 0)
2066 unit_serialize_item_format(u
, f
, "main-pid", PID_FMT
, s
->main_pid
);
2068 unit_serialize_item(u
, f
, "main-pid-known", yes_no(s
->main_pid_known
));
2069 unit_serialize_item(u
, f
, "bus-name-good", yes_no(s
->bus_name_good
));
2071 r
= unit_serialize_item_escaped(u
, f
, "status-text", s
->status_text
);
2075 /* FIXME: There's a minor uncleanliness here: if there are
2076 * multiple commands attached here, we will start from the
2077 * first one again */
2078 if (s
->control_command_id
>= 0)
2079 unit_serialize_item(u
, f
, "control-command", service_exec_command_to_string(s
->control_command_id
));
2081 r
= unit_serialize_item_fd(u
, f
, fds
, "stdin-fd", s
->stdin_fd
);
2084 r
= unit_serialize_item_fd(u
, f
, fds
, "stdout-fd", s
->stdout_fd
);
2087 r
= unit_serialize_item_fd(u
, f
, fds
, "stderr-fd", s
->stderr_fd
);
2091 r
= unit_serialize_item_fd(u
, f
, fds
, "socket-fd", s
->socket_fd
);
2094 r
= unit_serialize_item_fd(u
, f
, fds
, "endpoint-fd", s
->bus_endpoint_fd
);
2098 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
2099 _cleanup_free_
char *c
= NULL
;
2102 copy
= fdset_put_dup(fds
, fs
->fd
);
2106 c
= cescape(fs
->fdname
);
2108 unit_serialize_item_format(u
, f
, "fd-store-fd", "%i %s", copy
, strempty(c
));
2111 if (s
->main_exec_status
.pid
> 0) {
2112 unit_serialize_item_format(u
, f
, "main-exec-status-pid", PID_FMT
, s
->main_exec_status
.pid
);
2113 dual_timestamp_serialize(f
, "main-exec-status-start", &s
->main_exec_status
.start_timestamp
);
2114 dual_timestamp_serialize(f
, "main-exec-status-exit", &s
->main_exec_status
.exit_timestamp
);
2116 if (dual_timestamp_is_set(&s
->main_exec_status
.exit_timestamp
)) {
2117 unit_serialize_item_format(u
, f
, "main-exec-status-code", "%i", s
->main_exec_status
.code
);
2118 unit_serialize_item_format(u
, f
, "main-exec-status-status", "%i", s
->main_exec_status
.status
);
2122 if (dual_timestamp_is_set(&s
->watchdog_timestamp
))
2123 dual_timestamp_serialize(f
, "watchdog-timestamp", &s
->watchdog_timestamp
);
2125 unit_serialize_item(u
, f
, "forbid-restart", yes_no(s
->forbid_restart
));
2130 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
2131 Service
*s
= SERVICE(u
);
2139 if (streq(key
, "state")) {
2142 state
= service_state_from_string(value
);
2144 log_unit_debug(u
, "Failed to parse state value: %s", value
);
2146 s
->deserialized_state
= state
;
2147 } else if (streq(key
, "result")) {
2150 f
= service_result_from_string(value
);
2152 log_unit_debug(u
, "Failed to parse result value: %s", value
);
2153 else if (f
!= SERVICE_SUCCESS
)
2156 } else if (streq(key
, "reload-result")) {
2159 f
= service_result_from_string(value
);
2161 log_unit_debug(u
, "Failed to parse reload result value: %s", value
);
2162 else if (f
!= SERVICE_SUCCESS
)
2163 s
->reload_result
= f
;
2165 } else if (streq(key
, "control-pid")) {
2168 if (parse_pid(value
, &pid
) < 0)
2169 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
2171 s
->control_pid
= pid
;
2172 } else if (streq(key
, "main-pid")) {
2175 if (parse_pid(value
, &pid
) < 0)
2176 log_unit_debug(u
, "Failed to parse main-pid value: %s", value
);
2178 service_set_main_pid(s
, pid
);
2179 unit_watch_pid(UNIT(s
), pid
);
2181 } else if (streq(key
, "main-pid-known")) {
2184 b
= parse_boolean(value
);
2186 log_unit_debug(u
, "Failed to parse main-pid-known value: %s", value
);
2188 s
->main_pid_known
= b
;
2189 } else if (streq(key
, "bus-name-good")) {
2192 b
= parse_boolean(value
);
2194 log_unit_debug(u
, "Failed to parse bus-name-good value: %s", value
);
2196 s
->bus_name_good
= b
;
2197 } else if (streq(key
, "status-text")) {
2200 r
= cunescape(value
, 0, &t
);
2202 log_unit_debug_errno(u
, r
, "Failed to unescape status text: %s", value
);
2204 free(s
->status_text
);
2208 } else if (streq(key
, "control-command")) {
2209 ServiceExecCommand id
;
2211 id
= service_exec_command_from_string(value
);
2213 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
2215 s
->control_command_id
= id
;
2216 s
->control_command
= s
->exec_command
[id
];
2218 } else if (streq(key
, "socket-fd")) {
2221 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2222 log_unit_debug(u
, "Failed to parse socket-fd value: %s", value
);
2224 asynchronous_close(s
->socket_fd
);
2225 s
->socket_fd
= fdset_remove(fds
, fd
);
2227 } else if (streq(key
, "endpoint-fd")) {
2230 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2231 log_unit_debug(u
, "Failed to parse endpoint-fd value: %s", value
);
2233 safe_close(s
->bus_endpoint_fd
);
2234 s
->bus_endpoint_fd
= fdset_remove(fds
, fd
);
2236 } else if (streq(key
, "fd-store-fd")) {
2241 pf
= strcspn(value
, WHITESPACE
);
2242 fdv
= strndupa(value
, pf
);
2244 if (safe_atoi(fdv
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2245 log_unit_debug(u
, "Failed to parse fd-store-fd value: %s", value
);
2247 _cleanup_free_
char *t
= NULL
;
2251 fdn
+= strspn(fdn
, WHITESPACE
);
2252 (void) cunescape(fdn
, 0, &t
);
2254 r
= service_add_fd_store(s
, fd
, t
);
2256 log_unit_error_errno(u
, r
, "Failed to add fd to store: %m");
2258 fdset_remove(fds
, fd
);
2261 } else if (streq(key
, "main-exec-status-pid")) {
2264 if (parse_pid(value
, &pid
) < 0)
2265 log_unit_debug(u
, "Failed to parse main-exec-status-pid value: %s", value
);
2267 s
->main_exec_status
.pid
= pid
;
2268 } else if (streq(key
, "main-exec-status-code")) {
2271 if (safe_atoi(value
, &i
) < 0)
2272 log_unit_debug(u
, "Failed to parse main-exec-status-code value: %s", value
);
2274 s
->main_exec_status
.code
= i
;
2275 } else if (streq(key
, "main-exec-status-status")) {
2278 if (safe_atoi(value
, &i
) < 0)
2279 log_unit_debug(u
, "Failed to parse main-exec-status-status value: %s", value
);
2281 s
->main_exec_status
.status
= i
;
2282 } else if (streq(key
, "main-exec-status-start"))
2283 dual_timestamp_deserialize(value
, &s
->main_exec_status
.start_timestamp
);
2284 else if (streq(key
, "main-exec-status-exit"))
2285 dual_timestamp_deserialize(value
, &s
->main_exec_status
.exit_timestamp
);
2286 else if (streq(key
, "watchdog-timestamp"))
2287 dual_timestamp_deserialize(value
, &s
->watchdog_timestamp
);
2288 else if (streq(key
, "forbid-restart")) {
2291 b
= parse_boolean(value
);
2293 log_unit_debug(u
, "Failed to parse forbid-restart value: %s", value
);
2295 s
->forbid_restart
= b
;
2296 } else if (streq(key
, "stdin-fd")) {
2299 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2300 log_unit_debug(u
, "Failed to parse stdin-fd value: %s", value
);
2302 asynchronous_close(s
->stdin_fd
);
2303 s
->stdin_fd
= fdset_remove(fds
, fd
);
2305 } else if (streq(key
, "stdout-fd")) {
2308 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2309 log_unit_debug(u
, "Failed to parse stdout-fd value: %s", value
);
2311 asynchronous_close(s
->stdout_fd
);
2312 s
->stdout_fd
= fdset_remove(fds
, fd
);
2314 } else if (streq(key
, "stderr-fd")) {
2317 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2318 log_unit_debug(u
, "Failed to parse stderr-fd value: %s", value
);
2320 asynchronous_close(s
->stderr_fd
);
2321 s
->stderr_fd
= fdset_remove(fds
, fd
);
2324 log_unit_debug(u
, "Unknown serialization key: %s", key
);
2329 _pure_
static UnitActiveState
service_active_state(Unit
*u
) {
2330 const UnitActiveState
*table
;
2334 table
= SERVICE(u
)->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
2336 return table
[SERVICE(u
)->state
];
2339 static const char *service_sub_state_to_string(Unit
*u
) {
2342 return service_state_to_string(SERVICE(u
)->state
);
2345 static bool service_check_gc(Unit
*u
) {
2346 Service
*s
= SERVICE(u
);
2350 /* Never clean up services that still have a process around,
2351 * even if the service is formally dead. */
2352 if (cgroup_good(s
) > 0 ||
2353 main_pid_good(s
) > 0 ||
2354 control_pid_good(s
) > 0)
2360 _pure_
static bool service_check_snapshot(Unit
*u
) {
2361 Service
*s
= SERVICE(u
);
2365 return s
->socket_fd
< 0;
2368 static int service_retry_pid_file(Service
*s
) {
2371 assert(s
->pid_file
);
2372 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2374 r
= service_load_pid_file(s
, false);
2378 service_unwatch_pid_file(s
);
2380 service_enter_running(s
, SERVICE_SUCCESS
);
2384 static int service_watch_pid_file(Service
*s
) {
2387 log_unit_debug(UNIT(s
), "Setting watch for PID file %s", s
->pid_file_pathspec
->path
);
2389 r
= path_spec_watch(s
->pid_file_pathspec
, service_dispatch_io
);
2393 /* the pidfile might have appeared just before we set the watch */
2394 log_unit_debug(UNIT(s
), "Trying to read PID file %s in case it changed", s
->pid_file_pathspec
->path
);
2395 service_retry_pid_file(s
);
2399 log_unit_error_errno(UNIT(s
), r
, "Failed to set a watch for PID file %s: %m", s
->pid_file_pathspec
->path
);
2400 service_unwatch_pid_file(s
);
2404 static int service_demand_pid_file(Service
*s
) {
2407 assert(s
->pid_file
);
2408 assert(!s
->pid_file_pathspec
);
2410 ps
= new0(PathSpec
, 1);
2415 ps
->path
= strdup(s
->pid_file
);
2421 path_kill_slashes(ps
->path
);
2423 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2424 * keep their PID file open all the time. */
2425 ps
->type
= PATH_MODIFIED
;
2426 ps
->inotify_fd
= -1;
2428 s
->pid_file_pathspec
= ps
;
2430 return service_watch_pid_file(s
);
2433 static int service_dispatch_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
) {
2434 PathSpec
*p
= userdata
;
2439 s
= SERVICE(p
->unit
);
2443 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2444 assert(s
->pid_file_pathspec
);
2445 assert(path_spec_owns_inotify_fd(s
->pid_file_pathspec
, fd
));
2447 log_unit_debug(UNIT(s
), "inotify event");
2449 if (path_spec_fd_event(p
, events
) < 0)
2452 if (service_retry_pid_file(s
) == 0)
2455 if (service_watch_pid_file(s
) < 0)
2461 service_unwatch_pid_file(s
);
2462 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2466 static void service_notify_cgroup_empty_event(Unit
*u
) {
2467 Service
*s
= SERVICE(u
);
2471 log_unit_debug(u
, "cgroup is empty");
2475 /* Waiting for SIGCHLD is usually more interesting,
2476 * because it includes return codes/signals. Which is
2477 * why we ignore the cgroup events for most cases,
2478 * except when we don't know pid which to expect the
2482 case SERVICE_START_POST
:
2483 /* If we were hoping for the daemon to write its PID file,
2484 * we can give up now. */
2485 if (s
->pid_file_pathspec
) {
2486 log_unit_warning(u
, "Daemon never wrote its PID file. Failing.");
2488 service_unwatch_pid_file(s
);
2489 if (s
->state
== SERVICE_START
)
2490 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2492 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2496 case SERVICE_RUNNING
:
2497 /* service_enter_running() will figure out what to do */
2498 service_enter_running(s
, SERVICE_SUCCESS
);
2501 case SERVICE_STOP_SIGABRT
:
2502 case SERVICE_STOP_SIGTERM
:
2503 case SERVICE_STOP_SIGKILL
:
2505 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2506 service_enter_stop_post(s
, SERVICE_SUCCESS
);
2510 case SERVICE_STOP_POST
:
2511 case SERVICE_FINAL_SIGTERM
:
2512 case SERVICE_FINAL_SIGKILL
:
2513 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2514 service_enter_dead(s
, SERVICE_SUCCESS
, true);
2523 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
2524 Service
*s
= SERVICE(u
);
2530 if (UNIT(s
)->fragment_path
? is_clean_exit(code
, status
, &s
->success_status
) :
2531 is_clean_exit_lsb(code
, status
, &s
->success_status
))
2532 f
= SERVICE_SUCCESS
;
2533 else if (code
== CLD_EXITED
)
2534 f
= SERVICE_FAILURE_EXIT_CODE
;
2535 else if (code
== CLD_KILLED
)
2536 f
= SERVICE_FAILURE_SIGNAL
;
2537 else if (code
== CLD_DUMPED
)
2538 f
= SERVICE_FAILURE_CORE_DUMP
;
2540 assert_not_reached("Unknown code");
2542 if (s
->main_pid
== pid
) {
2543 /* Forking services may occasionally move to a new PID.
2544 * As long as they update the PID file before exiting the old
2545 * PID, they're fine. */
2546 if (service_load_pid_file(s
, false) == 0)
2550 exec_status_exit(&s
->main_exec_status
, &s
->exec_context
, pid
, code
, status
);
2552 if (s
->main_command
) {
2553 /* If this is not a forking service than the
2554 * main process got started and hence we copy
2555 * the exit status so that it is recorded both
2556 * as main and as control process exit
2559 s
->main_command
->exec_status
= s
->main_exec_status
;
2561 if (s
->main_command
->ignore
)
2562 f
= SERVICE_SUCCESS
;
2563 } else if (s
->exec_command
[SERVICE_EXEC_START
]) {
2565 /* If this is a forked process, then we should
2566 * ignore the return value if this was
2567 * configured for the starter process */
2569 if (s
->exec_command
[SERVICE_EXEC_START
]->ignore
)
2570 f
= SERVICE_SUCCESS
;
2573 log_struct(f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
2575 LOG_UNIT_MESSAGE(u
, "Main process exited, code=%s, status=%i/%s",
2576 sigchld_code_to_string(code
), status
,
2577 strna(code
== CLD_EXITED
2578 ? exit_status_to_string(status
, EXIT_STATUS_FULL
)
2579 : signal_to_string(status
))),
2580 "EXIT_CODE=%s", sigchld_code_to_string(code
),
2581 "EXIT_STATUS=%i", status
,
2584 if (f
!= SERVICE_SUCCESS
)
2587 if (s
->main_command
&&
2588 s
->main_command
->command_next
&&
2589 f
== SERVICE_SUCCESS
) {
2591 /* There is another command to *
2592 * execute, so let's do that. */
2594 log_unit_debug(u
, "Running next main command for state %s.", service_state_to_string(s
->state
));
2595 service_run_next_main(s
);
2599 /* The service exited, so the service is officially
2601 s
->main_command
= NULL
;
2605 case SERVICE_START_POST
:
2606 case SERVICE_RELOAD
:
2608 /* Need to wait until the operation is
2613 if (s
->type
== SERVICE_ONESHOT
) {
2614 /* This was our main goal, so let's go on */
2615 if (f
== SERVICE_SUCCESS
)
2616 service_enter_start_post(s
);
2618 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2624 case SERVICE_RUNNING
:
2625 service_enter_running(s
, f
);
2628 case SERVICE_STOP_SIGABRT
:
2629 case SERVICE_STOP_SIGTERM
:
2630 case SERVICE_STOP_SIGKILL
:
2632 if (!control_pid_good(s
))
2633 service_enter_stop_post(s
, f
);
2635 /* If there is still a control process, wait for that first */
2638 case SERVICE_STOP_POST
:
2639 case SERVICE_FINAL_SIGTERM
:
2640 case SERVICE_FINAL_SIGKILL
:
2642 if (!control_pid_good(s
))
2643 service_enter_dead(s
, f
, true);
2647 assert_not_reached("Uh, main process died at wrong time.");
2651 } else if (s
->control_pid
== pid
) {
2654 if (s
->control_command
) {
2655 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
2657 if (s
->control_command
->ignore
)
2658 f
= SERVICE_SUCCESS
;
2661 log_unit_full(u
, f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
, 0,
2662 "Control process exited, code=%s status=%i",
2663 sigchld_code_to_string(code
), status
);
2665 if (f
!= SERVICE_SUCCESS
)
2668 /* Immediately get rid of the cgroup, so that the
2669 * kernel doesn't delay the cgroup empty messages for
2670 * the service cgroup any longer than necessary */
2671 service_kill_control_processes(s
);
2673 if (s
->control_command
&&
2674 s
->control_command
->command_next
&&
2675 f
== SERVICE_SUCCESS
) {
2677 /* There is another command to *
2678 * execute, so let's do that. */
2680 log_unit_debug(u
, "Running next control command for state %s.", service_state_to_string(s
->state
));
2681 service_run_next_control(s
);
2684 /* No further commands for this step, so let's
2685 * figure out what to do next */
2687 s
->control_command
= NULL
;
2688 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
2690 log_unit_debug(u
, "Got final SIGCHLD for state %s.", service_state_to_string(s
->state
));
2694 case SERVICE_START_PRE
:
2695 if (f
== SERVICE_SUCCESS
)
2696 service_enter_start(s
);
2698 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2702 if (s
->type
!= SERVICE_FORKING
)
2703 /* Maybe spurious event due to a reload that changed the type? */
2706 if (f
!= SERVICE_SUCCESS
) {
2707 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2712 bool has_start_post
;
2715 /* Let's try to load the pid file here if we can.
2716 * The PID file might actually be created by a START_POST
2717 * script. In that case don't worry if the loading fails. */
2719 has_start_post
= !!s
->exec_command
[SERVICE_EXEC_START_POST
];
2720 r
= service_load_pid_file(s
, !has_start_post
);
2721 if (!has_start_post
&& r
< 0) {
2722 r
= service_demand_pid_file(s
);
2723 if (r
< 0 || !cgroup_good(s
))
2724 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2728 (void) service_search_main_pid(s
);
2730 service_enter_start_post(s
);
2733 case SERVICE_START_POST
:
2734 if (f
!= SERVICE_SUCCESS
) {
2735 service_enter_stop(s
, f
);
2742 r
= service_load_pid_file(s
, true);
2744 r
= service_demand_pid_file(s
);
2745 if (r
< 0 || !cgroup_good(s
))
2746 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2750 (void) service_search_main_pid(s
);
2752 service_enter_running(s
, SERVICE_SUCCESS
);
2755 case SERVICE_RELOAD
:
2756 if (f
== SERVICE_SUCCESS
) {
2757 service_load_pid_file(s
, true);
2758 (void) service_search_main_pid(s
);
2761 s
->reload_result
= f
;
2762 service_enter_running(s
, SERVICE_SUCCESS
);
2766 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
2769 case SERVICE_STOP_SIGABRT
:
2770 case SERVICE_STOP_SIGTERM
:
2771 case SERVICE_STOP_SIGKILL
:
2772 if (main_pid_good(s
) <= 0)
2773 service_enter_stop_post(s
, f
);
2775 /* If there is still a service
2776 * process around, wait until
2777 * that one quit, too */
2780 case SERVICE_STOP_POST
:
2781 case SERVICE_FINAL_SIGTERM
:
2782 case SERVICE_FINAL_SIGKILL
:
2783 if (main_pid_good(s
) <= 0)
2784 service_enter_dead(s
, f
, true);
2788 assert_not_reached("Uh, control process died at wrong time.");
2793 /* Notify clients about changed exit status */
2794 unit_add_to_dbus_queue(u
);
2796 /* We got one SIGCHLD for the service, let's watch all
2797 * processes that are now running of the service, and watch
2798 * that. Among the PIDs we then watch will be children
2799 * reassigned to us, which hopefully allows us to identify
2800 * when all children are gone */
2801 unit_tidy_watch_pids(u
, s
->main_pid
, s
->control_pid
);
2802 unit_watch_all_pids(u
);
2804 /* If the PID set is empty now, then let's finish this off */
2805 if (set_isempty(u
->pids
))
2806 service_notify_cgroup_empty_event(u
);
2809 static int service_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
2810 Service
*s
= SERVICE(userdata
);
2813 assert(source
== s
->timer_event_source
);
2817 case SERVICE_START_PRE
:
2819 log_unit_warning(UNIT(s
), "%s operation timed out. Terminating.", s
->state
== SERVICE_START
? "Start" : "Start-pre");
2820 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2823 case SERVICE_START_POST
:
2824 log_unit_warning(UNIT(s
), "Start-post operation timed out. Stopping.");
2825 service_enter_stop(s
, SERVICE_FAILURE_TIMEOUT
);
2828 case SERVICE_RELOAD
:
2829 log_unit_warning(UNIT(s
), "Reload operation timed out. Stopping.");
2830 service_unwatch_control_pid(s
);
2831 service_kill_control_processes(s
);
2832 s
->reload_result
= SERVICE_FAILURE_TIMEOUT
;
2833 service_enter_running(s
, SERVICE_SUCCESS
);
2837 log_unit_warning(UNIT(s
), "Stopping timed out. Terminating.");
2838 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2841 case SERVICE_STOP_SIGABRT
:
2842 log_unit_warning(UNIT(s
), "State 'stop-sigabrt' timed out. Terminating.");
2843 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2846 case SERVICE_STOP_SIGTERM
:
2847 if (s
->kill_context
.send_sigkill
) {
2848 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Killing.");
2849 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
2851 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Skipping SIGKILL.");
2852 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
2857 case SERVICE_STOP_SIGKILL
:
2858 /* Uh, we sent a SIGKILL and it is still not gone?
2859 * Must be something we cannot kill, so let's just be
2860 * weirded out and continue */
2862 log_unit_warning(UNIT(s
), "Processes still around after SIGKILL. Ignoring.");
2863 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
2866 case SERVICE_STOP_POST
:
2867 log_unit_warning(UNIT(s
), "State 'stop-post' timed out. Terminating.");
2868 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2871 case SERVICE_FINAL_SIGTERM
:
2872 if (s
->kill_context
.send_sigkill
) {
2873 log_unit_warning(UNIT(s
), "State 'stop-final-sigterm' timed out. Killing.");
2874 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
2876 log_unit_warning(UNIT(s
), "State 'stop-final-sigterm' timed out. Skipping SIGKILL. Entering failed mode.");
2877 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, false);
2882 case SERVICE_FINAL_SIGKILL
:
2883 log_unit_warning(UNIT(s
), "Processes still around after final SIGKILL. Entering failed mode.");
2884 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, true);
2887 case SERVICE_AUTO_RESTART
:
2888 log_unit_info(UNIT(s
),
2889 s
->restart_usec
> 0 ?
2890 "Service hold-off time over, scheduling restart." :
2891 "Service has no hold-off time, scheduling restart.");
2892 service_enter_restart(s
);
2896 assert_not_reached("Timeout at wrong time.");
2902 static int service_dispatch_watchdog(sd_event_source
*source
, usec_t usec
, void *userdata
) {
2903 Service
*s
= SERVICE(userdata
);
2904 char t
[FORMAT_TIMESPAN_MAX
];
2907 assert(source
== s
->watchdog_event_source
);
2909 log_unit_error(UNIT(s
), "Watchdog timeout (limit %s)!",
2910 format_timespan(t
, sizeof(t
), s
->watchdog_usec
, 1));
2912 service_enter_signal(s
, SERVICE_STOP_SIGABRT
, SERVICE_FAILURE_WATCHDOG
);
2917 static void service_notify_message(Unit
*u
, pid_t pid
, char **tags
, FDSet
*fds
) {
2918 Service
*s
= SERVICE(u
);
2919 _cleanup_free_
char *cc
= NULL
;
2920 bool notify_dbus
= false;
2925 cc
= strv_join(tags
, ", ");
2927 if (s
->notify_access
== NOTIFY_NONE
) {
2928 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception is disabled.", pid
);
2930 } else if (s
->notify_access
== NOTIFY_MAIN
&& pid
!= s
->main_pid
) {
2931 if (s
->main_pid
!= 0)
2932 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID "PID_FMT
, pid
, s
->main_pid
);
2934 log_unit_debug(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID which is currently not known", pid
);
2937 log_unit_debug(u
, "Got notification message from PID "PID_FMT
" (%s)", pid
, isempty(cc
) ? "n/a" : cc
);
2939 /* Interpret MAINPID= */
2940 e
= strv_find_startswith(tags
, "MAINPID=");
2941 if (e
&& IN_SET(s
->state
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
)) {
2942 if (parse_pid(e
, &pid
) < 0)
2943 log_unit_warning(u
, "Failed to parse MAINPID= field in notification message: %s", e
);
2945 service_set_main_pid(s
, pid
);
2946 unit_watch_pid(UNIT(s
), pid
);
2951 /* Interpret RELOADING= */
2952 if (strv_find(tags
, "RELOADING=1")) {
2954 s
->notify_state
= NOTIFY_RELOADING
;
2956 if (s
->state
== SERVICE_RUNNING
)
2957 service_enter_reload_by_notify(s
);
2962 /* Interpret READY= */
2963 if (strv_find(tags
, "READY=1")) {
2965 s
->notify_state
= NOTIFY_READY
;
2967 /* Type=notify services inform us about completed
2968 * initialization with READY=1 */
2969 if (s
->type
== SERVICE_NOTIFY
&& s
->state
== SERVICE_START
)
2970 service_enter_start_post(s
);
2972 /* Sending READY=1 while we are reloading informs us
2973 * that the reloading is complete */
2974 if (s
->state
== SERVICE_RELOAD
&& s
->control_pid
== 0)
2975 service_enter_running(s
, SERVICE_SUCCESS
);
2980 /* Interpret STOPPING= */
2981 if (strv_find(tags
, "STOPPING=1")) {
2983 s
->notify_state
= NOTIFY_STOPPING
;
2985 if (s
->state
== SERVICE_RUNNING
)
2986 service_enter_stop_by_notify(s
);
2991 /* Interpret STATUS= */
2992 e
= strv_find_startswith(tags
, "STATUS=");
2994 _cleanup_free_
char *t
= NULL
;
2997 if (!utf8_is_valid(e
))
2998 log_unit_warning(u
, "Status message in notification message is not UTF-8 clean.");
3006 if (!streq_ptr(s
->status_text
, t
)) {
3008 free(s
->status_text
);
3016 /* Interpret ERRNO= */
3017 e
= strv_find_startswith(tags
, "ERRNO=");
3021 if (safe_atoi(e
, &status_errno
) < 0 || status_errno
< 0)
3022 log_unit_warning(u
, "Failed to parse ERRNO= field in notification message: %s", e
);
3024 if (s
->status_errno
!= status_errno
) {
3025 s
->status_errno
= status_errno
;
3031 /* Interpret WATCHDOG= */
3032 if (strv_find(tags
, "WATCHDOG=1"))
3033 service_reset_watchdog(s
);
3035 if (strv_find(tags
, "FDSTORE=1")) {
3038 name
= strv_find_startswith(tags
, "FDNAME=");
3039 if (name
&& !fdname_is_valid(name
)) {
3040 log_unit_warning(u
, "Passed FDNAME= name is invalid, ignoring.");
3044 service_add_fd_store_set(s
, fds
, name
);
3047 /* Notify clients about changed status or main pid */
3049 unit_add_to_dbus_queue(u
);
3052 static int service_get_timeout(Unit
*u
, uint64_t *timeout
) {
3053 Service
*s
= SERVICE(u
);
3056 if (!s
->timer_event_source
)
3059 r
= sd_event_source_get_time(s
->timer_event_source
, timeout
);
3066 static void service_bus_name_owner_change(
3069 const char *old_owner
,
3070 const char *new_owner
) {
3072 Service
*s
= SERVICE(u
);
3078 assert(streq(s
->bus_name
, name
));
3079 assert(old_owner
|| new_owner
);
3081 if (old_owner
&& new_owner
)
3082 log_unit_debug(u
, "D-Bus name %s changed owner from %s to %s", name
, old_owner
, new_owner
);
3084 log_unit_debug(u
, "D-Bus name %s no longer registered by %s", name
, old_owner
);
3086 log_unit_debug(u
, "D-Bus name %s now registered by %s", name
, new_owner
);
3088 s
->bus_name_good
= !!new_owner
;
3090 if (s
->type
== SERVICE_DBUS
) {
3092 /* service_enter_running() will figure out what to
3094 if (s
->state
== SERVICE_RUNNING
)
3095 service_enter_running(s
, SERVICE_SUCCESS
);
3096 else if (s
->state
== SERVICE_START
&& new_owner
)
3097 service_enter_start_post(s
);
3099 } else if (new_owner
&&
3101 (s
->state
== SERVICE_START
||
3102 s
->state
== SERVICE_START_POST
||
3103 s
->state
== SERVICE_RUNNING
||
3104 s
->state
== SERVICE_RELOAD
)) {
3106 _cleanup_bus_creds_unref_ sd_bus_creds
*creds
= NULL
;
3109 /* Try to acquire PID from bus service */
3111 r
= sd_bus_get_name_creds(u
->manager
->api_bus
, name
, SD_BUS_CREDS_PID
, &creds
);
3113 r
= sd_bus_creds_get_pid(creds
, &pid
);
3115 log_unit_debug(u
, "D-Bus name %s is now owned by process %u", name
, (unsigned) pid
);
3117 service_set_main_pid(s
, pid
);
3118 unit_watch_pid(UNIT(s
), pid
);
3123 int service_set_socket_fd(Service
*s
, int fd
, Socket
*sock
, bool selinux_context_net
) {
3124 _cleanup_free_
char *peer
= NULL
;
3130 /* This is called by the socket code when instantiating a new
3131 * service for a stream socket and the socket needs to be
3134 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
3137 if (s
->socket_fd
>= 0)
3140 if (s
->state
!= SERVICE_DEAD
)
3143 if (getpeername_pretty(fd
, &peer
) >= 0) {
3145 if (UNIT(s
)->description
) {
3146 _cleanup_free_
char *a
;
3148 a
= strjoin(UNIT(s
)->description
, " (", peer
, ")", NULL
);
3152 r
= unit_set_description(UNIT(s
), a
);
3154 r
= unit_set_description(UNIT(s
), peer
);
3161 s
->socket_fd_selinux_context_net
= selinux_context_net
;
3163 unit_ref_set(&s
->accept_socket
, UNIT(sock
));
3165 return unit_add_two_dependencies(UNIT(sock
), UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT(s
), false);
3168 static void service_reset_failed(Unit
*u
) {
3169 Service
*s
= SERVICE(u
);
3173 if (s
->state
== SERVICE_FAILED
)
3174 service_set_state(s
, SERVICE_DEAD
);
3176 s
->result
= SERVICE_SUCCESS
;
3177 s
->reload_result
= SERVICE_SUCCESS
;
3179 RATELIMIT_RESET(s
->start_limit
);
3182 static int service_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
3183 Service
*s
= SERVICE(u
);
3185 return unit_kill_common(u
, who
, signo
, s
->main_pid
, s
->control_pid
, error
);
3188 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
3189 [SERVICE_RESTART_NO
] = "no",
3190 [SERVICE_RESTART_ON_SUCCESS
] = "on-success",
3191 [SERVICE_RESTART_ON_FAILURE
] = "on-failure",
3192 [SERVICE_RESTART_ON_ABNORMAL
] = "on-abnormal",
3193 [SERVICE_RESTART_ON_WATCHDOG
] = "on-watchdog",
3194 [SERVICE_RESTART_ON_ABORT
] = "on-abort",
3195 [SERVICE_RESTART_ALWAYS
] = "always",
3198 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
3200 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
3201 [SERVICE_SIMPLE
] = "simple",
3202 [SERVICE_FORKING
] = "forking",
3203 [SERVICE_ONESHOT
] = "oneshot",
3204 [SERVICE_DBUS
] = "dbus",
3205 [SERVICE_NOTIFY
] = "notify",
3206 [SERVICE_IDLE
] = "idle"
3209 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
3211 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
3212 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
3213 [SERVICE_EXEC_START
] = "ExecStart",
3214 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
3215 [SERVICE_EXEC_RELOAD
] = "ExecReload",
3216 [SERVICE_EXEC_STOP
] = "ExecStop",
3217 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
3220 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
3222 static const char* const notify_access_table
[_NOTIFY_ACCESS_MAX
] = {
3223 [NOTIFY_NONE
] = "none",
3224 [NOTIFY_MAIN
] = "main",
3225 [NOTIFY_ALL
] = "all"
3228 DEFINE_STRING_TABLE_LOOKUP(notify_access
, NotifyAccess
);
3230 static const char* const notify_state_table
[_NOTIFY_STATE_MAX
] = {
3231 [NOTIFY_UNKNOWN
] = "unknown",
3232 [NOTIFY_READY
] = "ready",
3233 [NOTIFY_RELOADING
] = "reloading",
3234 [NOTIFY_STOPPING
] = "stopping",
3237 DEFINE_STRING_TABLE_LOOKUP(notify_state
, NotifyState
);
3239 static const char* const service_result_table
[_SERVICE_RESULT_MAX
] = {
3240 [SERVICE_SUCCESS
] = "success",
3241 [SERVICE_FAILURE_RESOURCES
] = "resources",
3242 [SERVICE_FAILURE_TIMEOUT
] = "timeout",
3243 [SERVICE_FAILURE_EXIT_CODE
] = "exit-code",
3244 [SERVICE_FAILURE_SIGNAL
] = "signal",
3245 [SERVICE_FAILURE_CORE_DUMP
] = "core-dump",
3246 [SERVICE_FAILURE_WATCHDOG
] = "watchdog",
3247 [SERVICE_FAILURE_START_LIMIT
] = "start-limit"
3250 DEFINE_STRING_TABLE_LOOKUP(service_result
, ServiceResult
);
3252 const UnitVTable service_vtable
= {
3253 .object_size
= sizeof(Service
),
3254 .exec_context_offset
= offsetof(Service
, exec_context
),
3255 .cgroup_context_offset
= offsetof(Service
, cgroup_context
),
3256 .kill_context_offset
= offsetof(Service
, kill_context
),
3257 .exec_runtime_offset
= offsetof(Service
, exec_runtime
),
3263 .private_section
= "Service",
3265 .init
= service_init
,
3266 .done
= service_done
,
3267 .load
= service_load
,
3268 .release_resources
= service_release_resources
,
3270 .coldplug
= service_coldplug
,
3272 .dump
= service_dump
,
3274 .start
= service_start
,
3275 .stop
= service_stop
,
3276 .reload
= service_reload
,
3278 .can_reload
= service_can_reload
,
3280 .kill
= service_kill
,
3282 .serialize
= service_serialize
,
3283 .deserialize_item
= service_deserialize_item
,
3285 .active_state
= service_active_state
,
3286 .sub_state_to_string
= service_sub_state_to_string
,
3288 .check_gc
= service_check_gc
,
3289 .check_snapshot
= service_check_snapshot
,
3291 .sigchld_event
= service_sigchld_event
,
3293 .reset_failed
= service_reset_failed
,
3295 .notify_cgroup_empty
= service_notify_cgroup_empty_event
,
3296 .notify_message
= service_notify_message
,
3298 .bus_name_owner_change
= service_bus_name_owner_change
,
3300 .bus_vtable
= bus_service_vtable
,
3301 .bus_set_property
= bus_service_set_property
,
3302 .bus_commit_properties
= bus_service_commit_properties
,
3304 .get_timeout
= service_get_timeout
,
3305 .can_transient
= true,
3307 .status_message_formats
= {
3308 .starting_stopping
= {
3309 [0] = "Starting %s...",
3310 [1] = "Stopping %s...",
3312 .finished_start_job
= {
3313 [JOB_DONE
] = "Started %s.",
3314 [JOB_FAILED
] = "Failed to start %s.",
3316 .finished_stop_job
= {
3317 [JOB_DONE
] = "Stopped %s.",
3318 [JOB_FAILED
] = "Stopped (with error) %s.",